1dc04c583Sdrh /* 2dc04c583Sdrh ** 2002 February 23 3dc04c583Sdrh ** 4dc04c583Sdrh ** The author disclaims copyright to this source code. In place of 5dc04c583Sdrh ** a legal notice, here is a blessing: 6dc04c583Sdrh ** 7dc04c583Sdrh ** May you do good and not evil. 8dc04c583Sdrh ** May you find forgiveness for yourself and forgive others. 9dc04c583Sdrh ** May you share freely, never taking more than you give. 10dc04c583Sdrh ** 11dc04c583Sdrh ************************************************************************* 12dc04c583Sdrh ** This file contains the C functions that implement various SQL 13dc04c583Sdrh ** functions of SQLite. 14dc04c583Sdrh ** 15dc04c583Sdrh ** There is only one exported symbol in this file - the function 16dc04c583Sdrh ** sqliteRegisterBuildinFunctions() found at the bottom of the file. 17dc04c583Sdrh ** All other code has file scope. 18dc04c583Sdrh ** 19*5bb3eb9bSdrh ** $Id: func.c,v 1.145 2007/05/04 13:15:56 drh Exp $ 20dc04c583Sdrh */ 21b659e9bfSdrh #include "sqliteInt.h" 22dc04c583Sdrh #include <ctype.h> 23b37df7b9Sdrh /* #include <math.h> */ 24d3a149efSdrh #include <stdlib.h> 250bce8354Sdrh #include <assert.h> 2688208050Sdanielk1977 #include "vdbeInt.h" 27771d8c3bSdrh #include "os.h" 280bce8354Sdrh 2955ef4d97Sdrh /* 3055ef4d97Sdrh ** Return the collating function associated with a function. 3155ef4d97Sdrh */ 32dc1bdc4fSdanielk1977 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ 33dc1bdc4fSdanielk1977 return context->pColl; 34dc1bdc4fSdanielk1977 } 35dc1bdc4fSdanielk1977 360bce8354Sdrh /* 370bce8354Sdrh ** Implementation of the non-aggregate min() and max() functions 380bce8354Sdrh */ 39f9b596ebSdrh static void minmaxFunc( 40f9b596ebSdrh sqlite3_context *context, 41f9b596ebSdrh int argc, 42f9b596ebSdrh sqlite3_value **argv 43f9b596ebSdrh ){ 440bce8354Sdrh int i; 45268380caSdrh int mask; /* 0 for min() or 0xffffffff for max() */ 46f9b596ebSdrh int iBest; 47dc1bdc4fSdanielk1977 CollSeq *pColl; 480bce8354Sdrh 4989425d5eSdrh if( argc==0 ) return; 50c44af71cSdrh mask = sqlite3_user_data(context)==0 ? 0 : -1; 51dc1bdc4fSdanielk1977 pColl = sqlite3GetFuncCollSeq(context); 52dc1bdc4fSdanielk1977 assert( pColl ); 53c572ef7fSdanielk1977 assert( mask==-1 || mask==0 ); 54f9b596ebSdrh iBest = 0; 559c054830Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 56f9b596ebSdrh for(i=1; i<argc; i++){ 579c054830Sdrh if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return; 58dc1bdc4fSdanielk1977 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){ 59f9b596ebSdrh iBest = i; 600bce8354Sdrh } 610bce8354Sdrh } 62f4479501Sdrh sqlite3_result_value(context, argv[iBest]); 630bce8354Sdrh } 640bce8354Sdrh 65268380caSdrh /* 66268380caSdrh ** Return the type of the argument. 67268380caSdrh */ 68f9b596ebSdrh static void typeofFunc( 69f9b596ebSdrh sqlite3_context *context, 70f9b596ebSdrh int argc, 71f9b596ebSdrh sqlite3_value **argv 72f9b596ebSdrh ){ 7335bb9d02Sdanielk1977 const char *z = 0; 7435bb9d02Sdanielk1977 switch( sqlite3_value_type(argv[0]) ){ 759c054830Sdrh case SQLITE_NULL: z = "null"; break; 769c054830Sdrh case SQLITE_INTEGER: z = "integer"; break; 779c054830Sdrh case SQLITE_TEXT: z = "text"; break; 789c054830Sdrh case SQLITE_FLOAT: z = "real"; break; 799c054830Sdrh case SQLITE_BLOB: z = "blob"; break; 8035bb9d02Sdanielk1977 } 81d8123366Sdanielk1977 sqlite3_result_text(context, z, -1, SQLITE_STATIC); 820bce8354Sdrh } 830bce8354Sdrh 845708d2deSdrh 855708d2deSdrh /* 860bce8354Sdrh ** Implementation of the length() function 870bce8354Sdrh */ 88f9b596ebSdrh static void lengthFunc( 89f9b596ebSdrh sqlite3_context *context, 90f9b596ebSdrh int argc, 91f9b596ebSdrh sqlite3_value **argv 92f9b596ebSdrh ){ 930bce8354Sdrh int len; 940bce8354Sdrh 950bce8354Sdrh assert( argc==1 ); 96f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 979c054830Sdrh case SQLITE_BLOB: 989c054830Sdrh case SQLITE_INTEGER: 999c054830Sdrh case SQLITE_FLOAT: { 100f4479501Sdrh sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); 101f9b596ebSdrh break; 102f9b596ebSdrh } 1039c054830Sdrh case SQLITE_TEXT: { 1042646da7eSdrh const unsigned char *z = sqlite3_value_text(argv[0]); 1057a521cfbSdrh if( z==0 ) return; 1060bce8354Sdrh for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } 107f4479501Sdrh sqlite3_result_int(context, len); 108f9b596ebSdrh break; 109f9b596ebSdrh } 110f9b596ebSdrh default: { 111f9b596ebSdrh sqlite3_result_null(context); 112f9b596ebSdrh break; 113f9b596ebSdrh } 114f9b596ebSdrh } 1150bce8354Sdrh } 1160bce8354Sdrh 1170bce8354Sdrh /* 1180bce8354Sdrh ** Implementation of the abs() function 1190bce8354Sdrh */ 1200ae8b831Sdanielk1977 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 1210bce8354Sdrh assert( argc==1 ); 122f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 1239c054830Sdrh case SQLITE_INTEGER: { 124f93bbbeaSdanielk1977 i64 iVal = sqlite3_value_int64(argv[0]); 12552fc849aSdrh if( iVal<0 ){ 12652fc849aSdrh if( (iVal<<1)==0 ){ 12752fc849aSdrh sqlite3_result_error(context, "integer overflow", -1); 12852fc849aSdrh return; 12952fc849aSdrh } 13052fc849aSdrh iVal = -iVal; 13152fc849aSdrh } 132f93bbbeaSdanielk1977 sqlite3_result_int64(context, iVal); 133f9b596ebSdrh break; 134f9b596ebSdrh } 1359c054830Sdrh case SQLITE_NULL: { 136f9b596ebSdrh sqlite3_result_null(context); 137f9b596ebSdrh break; 138f9b596ebSdrh } 139f9b596ebSdrh default: { 140f93bbbeaSdanielk1977 double rVal = sqlite3_value_double(argv[0]); 14152fc849aSdrh if( rVal<0 ) rVal = -rVal; 142f93bbbeaSdanielk1977 sqlite3_result_double(context, rVal); 143f9b596ebSdrh break; 144f9b596ebSdrh } 145f9b596ebSdrh } 1460bce8354Sdrh } 1470bce8354Sdrh 1480bce8354Sdrh /* 1490bce8354Sdrh ** Implementation of the substr() function 1500bce8354Sdrh */ 151f9b596ebSdrh static void substrFunc( 152f9b596ebSdrh sqlite3_context *context, 153f9b596ebSdrh int argc, 154f9b596ebSdrh sqlite3_value **argv 155f9b596ebSdrh ){ 1562646da7eSdrh const unsigned char *z; 1572646da7eSdrh const unsigned char *z2; 1580bce8354Sdrh int i; 1590bce8354Sdrh int p1, p2, len; 160f9b596ebSdrh 1610bce8354Sdrh assert( argc==3 ); 1624f26d6c4Sdrh z = sqlite3_value_text(argv[0]); 1630bce8354Sdrh if( z==0 ) return; 16451ad0ecdSdanielk1977 p1 = sqlite3_value_int(argv[1]); 16551ad0ecdSdanielk1977 p2 = sqlite3_value_int(argv[2]); 16647c8a679Sdrh for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; } 1670bce8354Sdrh if( p1<0 ){ 16889425d5eSdrh p1 += len; 169653bc759Sdrh if( p1<0 ){ 170653bc759Sdrh p2 += p1; 171653bc759Sdrh p1 = 0; 172653bc759Sdrh } 1730bce8354Sdrh }else if( p1>0 ){ 1740bce8354Sdrh p1--; 1750bce8354Sdrh } 1760bce8354Sdrh if( p1+p2>len ){ 1770bce8354Sdrh p2 = len-p1; 1780bce8354Sdrh } 17977396304Sdrh for(i=0; i<p1 && z[i]; i++){ 18047c8a679Sdrh if( (z[i]&0xc0)==0x80 ) p1++; 1810bce8354Sdrh } 18247c8a679Sdrh while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; } 18377396304Sdrh for(; i<p1+p2 && z[i]; i++){ 18447c8a679Sdrh if( (z[i]&0xc0)==0x80 ) p2++; 1850bce8354Sdrh } 18647c8a679Sdrh while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; } 187653bc759Sdrh if( p2<0 ) p2 = 0; 1882646da7eSdrh sqlite3_result_text(context, (char*)&z[p1], p2, SQLITE_TRANSIENT); 1890bce8354Sdrh } 1900bce8354Sdrh 1910bce8354Sdrh /* 1920bce8354Sdrh ** Implementation of the round() function 1930bce8354Sdrh */ 1940ae8b831Sdanielk1977 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 19551ad0ecdSdanielk1977 int n = 0; 1960bce8354Sdrh double r; 197592ac8cbSdrh char zBuf[500]; /* larger than the %f representation of the largest double */ 1980bce8354Sdrh assert( argc==1 || argc==2 ); 19951ad0ecdSdanielk1977 if( argc==2 ){ 2009c054830Sdrh if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; 20151ad0ecdSdanielk1977 n = sqlite3_value_int(argv[1]); 2020bce8354Sdrh if( n>30 ) n = 30; 2030bce8354Sdrh if( n<0 ) n = 0; 20451ad0ecdSdanielk1977 } 205d589a92aSdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 2064f26d6c4Sdrh r = sqlite3_value_double(argv[0]); 207e866fcb9Sdrh sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r); 208502b962bSdrh sqlite3AtoF(zBuf, &r); 209502b962bSdrh sqlite3_result_double(context, r); 2100bce8354Sdrh } 211dc04c583Sdrh 212dc04c583Sdrh /* 213dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions. 214dc04c583Sdrh */ 2150ae8b831Sdanielk1977 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 2167a521cfbSdrh char *z1; 2177a521cfbSdrh const char *z2; 2189310ef23Sdrh int i, n; 2199c054830Sdrh if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 2209310ef23Sdrh n = sqlite3_value_bytes(argv[0]); 2217a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 2227a521cfbSdrh if( z2 ){ 2239310ef23Sdrh z1 = sqlite3_malloc(n+1); 2247a521cfbSdrh if( z1 ){ 225*5bb3eb9bSdrh memcpy(z1, z2, n+1); 2267a521cfbSdrh for(i=0; z1[i]; i++){ 2277a521cfbSdrh z1[i] = toupper(z1[i]); 228dc04c583Sdrh } 2297a521cfbSdrh sqlite3_result_text(context, z1, -1, sqlite3_free); 2307a521cfbSdrh } 2317a521cfbSdrh } 232dc04c583Sdrh } 2330ae8b831Sdanielk1977 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 2347a521cfbSdrh char *z1; 2357a521cfbSdrh const char *z2; 2369310ef23Sdrh int i, n; 2379c054830Sdrh if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 2389310ef23Sdrh n = sqlite3_value_bytes(argv[0]); 2397a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 2407a521cfbSdrh if( z2 ){ 2419310ef23Sdrh z1 = sqlite3_malloc(n+1); 2427a521cfbSdrh if( z1 ){ 243*5bb3eb9bSdrh memcpy(z1, z2, n+1); 2447a521cfbSdrh for(i=0; z1[i]; i++){ 2457a521cfbSdrh z1[i] = tolower(z1[i]); 246dc04c583Sdrh } 2477a521cfbSdrh sqlite3_result_text(context, z1, -1, sqlite3_free); 2487a521cfbSdrh } 2497a521cfbSdrh } 250dc04c583Sdrh } 251dc04c583Sdrh 252dc04c583Sdrh /* 253fbc99082Sdrh ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. 254b6c9e6e6Sjplyon ** All three do the same thing. They return the first non-NULL 255b6c9e6e6Sjplyon ** argument. 2563212e182Sdrh */ 257f9b596ebSdrh static void ifnullFunc( 258f9b596ebSdrh sqlite3_context *context, 259f9b596ebSdrh int argc, 260f9b596ebSdrh sqlite3_value **argv 261f9b596ebSdrh ){ 262fbc99082Sdrh int i; 263fbc99082Sdrh for(i=0; i<argc; i++){ 2649c054830Sdrh if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ 265f4479501Sdrh sqlite3_result_value(context, argv[i]); 266fbc99082Sdrh break; 267fbc99082Sdrh } 268fbc99082Sdrh } 2693212e182Sdrh } 2703212e182Sdrh 2713212e182Sdrh /* 272f9ffac96Sdrh ** Implementation of random(). Return a random integer. 273f9ffac96Sdrh */ 274f9b596ebSdrh static void randomFunc( 275f9b596ebSdrh sqlite3_context *context, 276f9b596ebSdrh int argc, 277f9b596ebSdrh sqlite3_value **argv 278f9b596ebSdrh ){ 27952fc849aSdrh sqlite_int64 r; 2804adee20fSdanielk1977 sqlite3Randomness(sizeof(r), &r); 281874abbedSdrh if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */ 282874abbedSdrh /* can always do abs() of the result */ 28352fc849aSdrh sqlite3_result_int64(context, r); 284f9ffac96Sdrh } 285f9ffac96Sdrh 286f9ffac96Sdrh /* 287137c728fSdrh ** Implementation of randomblob(N). Return a random blob 288137c728fSdrh ** that is N bytes long. 28963cf66f0Sdrh */ 290137c728fSdrh static void randomBlob( 29163cf66f0Sdrh sqlite3_context *context, 29263cf66f0Sdrh int argc, 29363cf66f0Sdrh sqlite3_value **argv 29463cf66f0Sdrh ){ 295137c728fSdrh int n; 296137c728fSdrh unsigned char *p; 29763cf66f0Sdrh assert( argc==1 ); 29863cf66f0Sdrh n = sqlite3_value_int(argv[0]); 299137c728fSdrh if( n<1 ) n = 1; 300137c728fSdrh p = sqlite3_malloc(n); 301137c728fSdrh sqlite3Randomness(n, p); 302137c728fSdrh sqlite3_result_blob(context, (char*)p, n, sqlite3_free); 30363cf66f0Sdrh } 30463cf66f0Sdrh 30563cf66f0Sdrh /* 3066ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function. The return 30724b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function. 3086ed41ad7Sdrh */ 30951ad0ecdSdanielk1977 static void last_insert_rowid( 3100ae8b831Sdanielk1977 sqlite3_context *context, 31151ad0ecdSdanielk1977 int arg, 31251ad0ecdSdanielk1977 sqlite3_value **argv 31351ad0ecdSdanielk1977 ){ 3149bb575fdSdrh sqlite3 *db = sqlite3_user_data(context); 315f9b596ebSdrh sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); 3166ed41ad7Sdrh } 3176ed41ad7Sdrh 318f146a776Srdc /* 319b28af71aSdanielk1977 ** Implementation of the changes() SQL function. The return value is the 320b28af71aSdanielk1977 ** same as the sqlite3_changes() API function. 321f146a776Srdc */ 322b28af71aSdanielk1977 static void changes( 323f9b596ebSdrh sqlite3_context *context, 324f9b596ebSdrh int arg, 325f9b596ebSdrh sqlite3_value **argv 326f9b596ebSdrh ){ 3279bb575fdSdrh sqlite3 *db = sqlite3_user_data(context); 328f4479501Sdrh sqlite3_result_int(context, sqlite3_changes(db)); 329b0c374ffSrdc } 330f146a776Srdc 331f146a776Srdc /* 332b28af71aSdanielk1977 ** Implementation of the total_changes() SQL function. The return value is 333b28af71aSdanielk1977 ** the same as the sqlite3_total_changes() API function. 334f146a776Srdc */ 335b28af71aSdanielk1977 static void total_changes( 3360ae8b831Sdanielk1977 sqlite3_context *context, 33751ad0ecdSdanielk1977 int arg, 33851ad0ecdSdanielk1977 sqlite3_value **argv 33951ad0ecdSdanielk1977 ){ 3409bb575fdSdrh sqlite3 *db = sqlite3_user_data(context); 341b28af71aSdanielk1977 sqlite3_result_int(context, sqlite3_total_changes(db)); 342b0c374ffSrdc } 343b0c374ffSrdc 3446ed41ad7Sdrh /* 3454e5ffc5fSdrh ** A structure defining how to do GLOB-style comparisons. 346d02eb1fdSdanielk1977 */ 3474e5ffc5fSdrh struct compareInfo { 3484e5ffc5fSdrh u8 matchAll; 3494e5ffc5fSdrh u8 matchOne; 3504e5ffc5fSdrh u8 matchSet; 3514e5ffc5fSdrh u8 noCase; 352d02eb1fdSdanielk1977 }; 35355ef4d97Sdrh 3544e5ffc5fSdrh static const struct compareInfo globInfo = { '*', '?', '[', 0 }; 35570031fa3Sdrh /* The correct SQL-92 behavior is for the LIKE operator to ignore 35670031fa3Sdrh ** case. Thus 'a' LIKE 'A' would be true. */ 35755ef4d97Sdrh static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; 35870031fa3Sdrh /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator 35970031fa3Sdrh ** is case sensitive causing 'a' LIKE 'A' to be false */ 36055ef4d97Sdrh static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; 361d02eb1fdSdanielk1977 362d02eb1fdSdanielk1977 /* 3634e5ffc5fSdrh ** X is a pointer to the first byte of a UTF-8 character. Increment 3644e5ffc5fSdrh ** X so that it points to the next character. This only works right 3654e5ffc5fSdrh ** if X points to a well-formed UTF-8 string. 366d02eb1fdSdanielk1977 */ 3674e5ffc5fSdrh #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} 3684e5ffc5fSdrh #define sqliteCharVal(X) sqlite3ReadUtf8(X) 369d02eb1fdSdanielk1977 370d02eb1fdSdanielk1977 371d02eb1fdSdanielk1977 /* 3724e5ffc5fSdrh ** Compare two UTF-8 strings for equality where the first string can 3734e5ffc5fSdrh ** potentially be a "glob" expression. Return true (1) if they 3744e5ffc5fSdrh ** are the same and false (0) if they are different. 3750ac65892Sdrh ** 3764e5ffc5fSdrh ** Globbing rules: 3770ac65892Sdrh ** 3784e5ffc5fSdrh ** '*' Matches any sequence of zero or more characters. 379d02eb1fdSdanielk1977 ** 3804e5ffc5fSdrh ** '?' Matches exactly one character. 3814e5ffc5fSdrh ** 3824e5ffc5fSdrh ** [...] Matches one character from the enclosed list of 3834e5ffc5fSdrh ** characters. 3844e5ffc5fSdrh ** 3854e5ffc5fSdrh ** [^...] Matches one character not in the enclosed list. 3864e5ffc5fSdrh ** 3874e5ffc5fSdrh ** With the [...] and [^...] matching, a ']' character can be included 3884e5ffc5fSdrh ** in the list by making it the first character after '[' or '^'. A 3894e5ffc5fSdrh ** range of characters can be specified using '-'. Example: 3904e5ffc5fSdrh ** "[a-z]" matches any single lower-case letter. To match a '-', make 3914e5ffc5fSdrh ** it the last character in the list. 3924e5ffc5fSdrh ** 3934e5ffc5fSdrh ** This routine is usually quick, but can be N**2 in the worst case. 3944e5ffc5fSdrh ** 3954e5ffc5fSdrh ** Hints: to match '*' or '?', put them in "[]". Like this: 3964e5ffc5fSdrh ** 3974e5ffc5fSdrh ** abc[*]xyz Matches "abc*xyz" only 3980ac65892Sdrh */ 3997c6303c0Sdanielk1977 static int patternCompare( 4004e5ffc5fSdrh const u8 *zPattern, /* The glob pattern */ 4014e5ffc5fSdrh const u8 *zString, /* The string to compare against the glob */ 4027c6303c0Sdanielk1977 const struct compareInfo *pInfo, /* Information about how to do the compare */ 4037c6303c0Sdanielk1977 const int esc /* The escape character */ 40451ad0ecdSdanielk1977 ){ 405ad7dd425Sdanielk1977 register int c; 4064e5ffc5fSdrh int invert; 4074e5ffc5fSdrh int seen; 4084e5ffc5fSdrh int c2; 4094e5ffc5fSdrh u8 matchOne = pInfo->matchOne; 4104e5ffc5fSdrh u8 matchAll = pInfo->matchAll; 4114e5ffc5fSdrh u8 matchSet = pInfo->matchSet; 4124e5ffc5fSdrh u8 noCase = pInfo->noCase; 4137c6303c0Sdanielk1977 int prevEscape = 0; /* True if the previous character was 'escape' */ 414d02eb1fdSdanielk1977 4154e5ffc5fSdrh while( (c = *zPattern)!=0 ){ 4167c6303c0Sdanielk1977 if( !prevEscape && c==matchAll ){ 4174e5ffc5fSdrh while( (c=zPattern[1]) == matchAll || c == matchOne ){ 4184e5ffc5fSdrh if( c==matchOne ){ 4194e5ffc5fSdrh if( *zString==0 ) return 0; 4204e5ffc5fSdrh sqliteNextChar(zString); 421d02eb1fdSdanielk1977 } 4224e5ffc5fSdrh zPattern++; 4234e5ffc5fSdrh } 42420fc0887Sdrh if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){ 4257c6303c0Sdanielk1977 u8 const *zTemp = &zPattern[1]; 4267c6303c0Sdanielk1977 sqliteNextChar(zTemp); 4277c6303c0Sdanielk1977 c = *zTemp; 4287c6303c0Sdanielk1977 } 4294e5ffc5fSdrh if( c==0 ) return 1; 4304e5ffc5fSdrh if( c==matchSet ){ 4317c6303c0Sdanielk1977 assert( esc==0 ); /* This is GLOB, not LIKE */ 4327c6303c0Sdanielk1977 while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){ 4334e5ffc5fSdrh sqliteNextChar(zString); 4344e5ffc5fSdrh } 4354e5ffc5fSdrh return *zString!=0; 436d02eb1fdSdanielk1977 }else{ 4374e5ffc5fSdrh while( (c2 = *zString)!=0 ){ 4384e5ffc5fSdrh if( noCase ){ 4394e5ffc5fSdrh c2 = sqlite3UpperToLower[c2]; 4404e5ffc5fSdrh c = sqlite3UpperToLower[c]; 4414e5ffc5fSdrh while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; } 442ad7dd425Sdanielk1977 }else{ 4434e5ffc5fSdrh while( c2 != 0 && c2 != c ){ c2 = *++zString; } 444ad7dd425Sdanielk1977 } 4454e5ffc5fSdrh if( c2==0 ) return 0; 4467c6303c0Sdanielk1977 if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1; 4474e5ffc5fSdrh sqliteNextChar(zString); 4484e5ffc5fSdrh } 4494e5ffc5fSdrh return 0; 4504e5ffc5fSdrh } 4517c6303c0Sdanielk1977 }else if( !prevEscape && c==matchOne ){ 4524e5ffc5fSdrh if( *zString==0 ) return 0; 4534e5ffc5fSdrh sqliteNextChar(zString); 4544e5ffc5fSdrh zPattern++; 4554e5ffc5fSdrh }else if( c==matchSet ){ 4564e5ffc5fSdrh int prior_c = 0; 4577c6303c0Sdanielk1977 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ 4584e5ffc5fSdrh seen = 0; 4594e5ffc5fSdrh invert = 0; 4604e5ffc5fSdrh c = sqliteCharVal(zString); 4614e5ffc5fSdrh if( c==0 ) return 0; 4624e5ffc5fSdrh c2 = *++zPattern; 4634e5ffc5fSdrh if( c2=='^' ){ invert = 1; c2 = *++zPattern; } 4644e5ffc5fSdrh if( c2==']' ){ 4654e5ffc5fSdrh if( c==']' ) seen = 1; 4664e5ffc5fSdrh c2 = *++zPattern; 4674e5ffc5fSdrh } 4684e5ffc5fSdrh while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ 4694e5ffc5fSdrh if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ 4704e5ffc5fSdrh zPattern++; 4714e5ffc5fSdrh c2 = sqliteCharVal(zPattern); 4724e5ffc5fSdrh if( c>=prior_c && c<=c2 ) seen = 1; 4734e5ffc5fSdrh prior_c = 0; 4744e5ffc5fSdrh }else if( c==c2 ){ 4754e5ffc5fSdrh seen = 1; 4764e5ffc5fSdrh prior_c = c2; 477d02eb1fdSdanielk1977 }else{ 4784e5ffc5fSdrh prior_c = c2; 479d02eb1fdSdanielk1977 } 4804e5ffc5fSdrh sqliteNextChar(zPattern); 481d02eb1fdSdanielk1977 } 4824e5ffc5fSdrh if( c2==0 || (seen ^ invert)==0 ) return 0; 4834e5ffc5fSdrh sqliteNextChar(zString); 4844e5ffc5fSdrh zPattern++; 48520fc0887Sdrh }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){ 4867c6303c0Sdanielk1977 prevEscape = 1; 4877c6303c0Sdanielk1977 sqliteNextChar(zPattern); 488d02eb1fdSdanielk1977 }else{ 4894e5ffc5fSdrh if( noCase ){ 4904e5ffc5fSdrh if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0; 4914e5ffc5fSdrh }else{ 4924e5ffc5fSdrh if( c != *zString ) return 0; 4934e5ffc5fSdrh } 4944e5ffc5fSdrh zPattern++; 4954e5ffc5fSdrh zString++; 4967c6303c0Sdanielk1977 prevEscape = 0; 49751ad0ecdSdanielk1977 } 4980ac65892Sdrh } 4994e5ffc5fSdrh return *zString==0; 5004e5ffc5fSdrh } 5014e5ffc5fSdrh 50255ef4d97Sdrh /* 50355ef4d97Sdrh ** Count the number of times that the LIKE operator (or GLOB which is 50455ef4d97Sdrh ** just a variation of LIKE) gets called. This is used for testing 50555ef4d97Sdrh ** only. 50655ef4d97Sdrh */ 50755ef4d97Sdrh #ifdef SQLITE_TEST 50855ef4d97Sdrh int sqlite3_like_count = 0; 50955ef4d97Sdrh #endif 51055ef4d97Sdrh 5113f6b0874Sdanielk1977 5123f6b0874Sdanielk1977 /* 5133f6b0874Sdanielk1977 ** Implementation of the like() SQL function. This function implements 5143f6b0874Sdanielk1977 ** the build-in LIKE operator. The first argument to the function is the 5153f6b0874Sdanielk1977 ** pattern and the second argument is the string. So, the SQL statements: 5163f6b0874Sdanielk1977 ** 5173f6b0874Sdanielk1977 ** A LIKE B 5183f6b0874Sdanielk1977 ** 5193f6b0874Sdanielk1977 ** is implemented as like(B,A). 5203f6b0874Sdanielk1977 ** 52155ef4d97Sdrh ** This same function (with a different compareInfo structure) computes 52255ef4d97Sdrh ** the GLOB operator. 5233f6b0874Sdanielk1977 */ 5243f6b0874Sdanielk1977 static void likeFunc( 5253f6b0874Sdanielk1977 sqlite3_context *context, 5263f6b0874Sdanielk1977 int argc, 5273f6b0874Sdanielk1977 sqlite3_value **argv 5283f6b0874Sdanielk1977 ){ 5293f6b0874Sdanielk1977 const unsigned char *zA = sqlite3_value_text(argv[0]); 5303f6b0874Sdanielk1977 const unsigned char *zB = sqlite3_value_text(argv[1]); 5317c6303c0Sdanielk1977 int escape = 0; 5327c6303c0Sdanielk1977 if( argc==3 ){ 5337c6303c0Sdanielk1977 /* The escape character string must consist of a single UTF-8 character. 5347c6303c0Sdanielk1977 ** Otherwise, return an error. 5357c6303c0Sdanielk1977 */ 5367c6303c0Sdanielk1977 const unsigned char *zEsc = sqlite3_value_text(argv[2]); 5377a521cfbSdrh if( zEsc==0 ) return; 5382646da7eSdrh if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){ 5397c6303c0Sdanielk1977 sqlite3_result_error(context, 5407c6303c0Sdanielk1977 "ESCAPE expression must be a single character", -1); 5417c6303c0Sdanielk1977 return; 5427c6303c0Sdanielk1977 } 5437c6303c0Sdanielk1977 escape = sqlite3ReadUtf8(zEsc); 5447c6303c0Sdanielk1977 } 5453f6b0874Sdanielk1977 if( zA && zB ){ 54655ef4d97Sdrh struct compareInfo *pInfo = sqlite3_user_data(context); 54755ef4d97Sdrh #ifdef SQLITE_TEST 54855ef4d97Sdrh sqlite3_like_count++; 54955ef4d97Sdrh #endif 55055ef4d97Sdrh sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape)); 55151ad0ecdSdanielk1977 } 5528912d106Sdrh } 5538912d106Sdrh 5548912d106Sdrh /* 5558912d106Sdrh ** Implementation of the NULLIF(x,y) function. The result is the first 5568912d106Sdrh ** argument if the arguments are different. The result is NULL if the 5578912d106Sdrh ** arguments are equal to each other. 5588912d106Sdrh */ 559f9b596ebSdrh static void nullifFunc( 560f9b596ebSdrh sqlite3_context *context, 561f9b596ebSdrh int argc, 562f9b596ebSdrh sqlite3_value **argv 563f9b596ebSdrh ){ 564dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 565dc1bdc4fSdanielk1977 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ 566f4479501Sdrh sqlite3_result_value(context, argv[0]); 5678912d106Sdrh } 5680ac65892Sdrh } 5690ac65892Sdrh 570647cb0e1Sdrh /* 571647cb0e1Sdrh ** Implementation of the VERSION(*) function. The result is the version 572647cb0e1Sdrh ** of the SQLite library that is running. 573647cb0e1Sdrh */ 574f9b596ebSdrh static void versionFunc( 575f9b596ebSdrh sqlite3_context *context, 576f9b596ebSdrh int argc, 577f9b596ebSdrh sqlite3_value **argv 578f9b596ebSdrh ){ 579d8123366Sdanielk1977 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); 580647cb0e1Sdrh } 581647cb0e1Sdrh 582137c728fSdrh /* Array for converting from half-bytes (nybbles) into ASCII hex 583137c728fSdrh ** digits. */ 584137c728fSdrh static const char hexdigits[] = { 585137c728fSdrh '0', '1', '2', '3', '4', '5', '6', '7', 586137c728fSdrh '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 587137c728fSdrh }; 588d641d646Sdanielk1977 58947394703Sdrh /* 59047394703Sdrh ** EXPERIMENTAL - This is not an official function. The interface may 59147394703Sdrh ** change. This function may disappear. Do not write code that depends 59247394703Sdrh ** on this function. 59347394703Sdrh ** 59447394703Sdrh ** Implementation of the QUOTE() function. This function takes a single 59547394703Sdrh ** argument. If the argument is numeric, the return value is the same as 59647394703Sdrh ** the argument. If the argument is NULL, the return value is the string 59747394703Sdrh ** "NULL". Otherwise, the argument is enclosed in single quotes with 59847394703Sdrh ** single-quote escapes. 59947394703Sdrh */ 6000ae8b831Sdanielk1977 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 60147394703Sdrh if( argc<1 ) return; 602f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 6039c054830Sdrh case SQLITE_NULL: { 604d8123366Sdanielk1977 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 605f9b596ebSdrh break; 606f9b596ebSdrh } 6079c054830Sdrh case SQLITE_INTEGER: 6089c054830Sdrh case SQLITE_FLOAT: { 609f4479501Sdrh sqlite3_result_value(context, argv[0]); 610f9b596ebSdrh break; 611f9b596ebSdrh } 6123f41e976Sdanielk1977 case SQLITE_BLOB: { 6133f41e976Sdanielk1977 char *zText = 0; 6143f41e976Sdanielk1977 int nBlob = sqlite3_value_bytes(argv[0]); 6153f41e976Sdanielk1977 char const *zBlob = sqlite3_value_blob(argv[0]); 6163f41e976Sdanielk1977 6173f41e976Sdanielk1977 zText = (char *)sqliteMalloc((2*nBlob)+4); 6183f41e976Sdanielk1977 if( !zText ){ 6193f41e976Sdanielk1977 sqlite3_result_error(context, "out of memory", -1); 6203f41e976Sdanielk1977 }else{ 6213f41e976Sdanielk1977 int i; 6223f41e976Sdanielk1977 for(i=0; i<nBlob; i++){ 6233f41e976Sdanielk1977 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; 6243f41e976Sdanielk1977 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; 6253f41e976Sdanielk1977 } 6263f41e976Sdanielk1977 zText[(nBlob*2)+2] = '\''; 6273f41e976Sdanielk1977 zText[(nBlob*2)+3] = '\0'; 6283f41e976Sdanielk1977 zText[0] = 'X'; 6293f41e976Sdanielk1977 zText[1] = '\''; 630d8123366Sdanielk1977 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 6313f41e976Sdanielk1977 sqliteFree(zText); 6323f41e976Sdanielk1977 } 6333f41e976Sdanielk1977 break; 6343f41e976Sdanielk1977 } 6359c054830Sdrh case SQLITE_TEXT: { 63647394703Sdrh int i,j,n; 6372646da7eSdrh const unsigned char *zArg = sqlite3_value_text(argv[0]); 63847394703Sdrh char *z; 639f9b596ebSdrh 6407a521cfbSdrh if( zArg==0 ) return; 64151ad0ecdSdanielk1977 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } 64247394703Sdrh z = sqliteMalloc( i+n+3 ); 64347394703Sdrh if( z==0 ) return; 64447394703Sdrh z[0] = '\''; 64551ad0ecdSdanielk1977 for(i=0, j=1; zArg[i]; i++){ 64651ad0ecdSdanielk1977 z[j++] = zArg[i]; 64751ad0ecdSdanielk1977 if( zArg[i]=='\'' ){ 64847394703Sdrh z[j++] = '\''; 64947394703Sdrh } 65047394703Sdrh } 65147394703Sdrh z[j++] = '\''; 65247394703Sdrh z[j] = 0; 653d8123366Sdanielk1977 sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); 65447394703Sdrh sqliteFree(z); 65547394703Sdrh } 65647394703Sdrh } 657f9b596ebSdrh } 65847394703Sdrh 659137c728fSdrh /* 660137c728fSdrh ** The hex() function. Interpret the argument as a blob. Return 661137c728fSdrh ** a hexadecimal rendering as text. 662137c728fSdrh */ 663137c728fSdrh static void hexFunc( 664137c728fSdrh sqlite3_context *context, 665137c728fSdrh int argc, 666137c728fSdrh sqlite3_value **argv 667137c728fSdrh ){ 668137c728fSdrh int i, n; 669137c728fSdrh const unsigned char *pBlob; 670137c728fSdrh char *zHex, *z; 671137c728fSdrh assert( argc==1 ); 672137c728fSdrh n = sqlite3_value_bytes(argv[0]); 6731eb2538aSdrh pBlob = sqlite3_value_blob(argv[0]); 674137c728fSdrh z = zHex = sqlite3_malloc(n*2 + 1); 675137c728fSdrh if( zHex==0 ) return; 676137c728fSdrh for(i=0; i<n; i++, pBlob++){ 677137c728fSdrh unsigned char c = *pBlob; 678137c728fSdrh *(z++) = hexdigits[(c>>4)&0xf]; 679137c728fSdrh *(z++) = hexdigits[c&0xf]; 680137c728fSdrh } 681137c728fSdrh *z = 0; 682137c728fSdrh sqlite3_result_text(context, zHex, n*2, sqlite3_free); 683137c728fSdrh } 684137c728fSdrh 68526b6d90dSdrh /* 6868cff382eSdrh ** The zeroblob(N) function returns a zero-filled blob of size N bytes. 6878cff382eSdrh */ 6888cff382eSdrh static void zeroblobFunc( 6898cff382eSdrh sqlite3_context *context, 6908cff382eSdrh int argc, 6918cff382eSdrh sqlite3_value **argv 6928cff382eSdrh ){ 6938cff382eSdrh int n; 6948cff382eSdrh assert( argc==1 ); 6958cff382eSdrh n = sqlite3_value_int(argv[0]); 6968cff382eSdrh sqlite3_result_zeroblob(context, n); 6978cff382eSdrh } 6988cff382eSdrh 6998cff382eSdrh /* 70026b6d90dSdrh ** The replace() function. Three arguments are all strings: call 70126b6d90dSdrh ** them A, B, and C. The result is also a string which is derived 70226b6d90dSdrh ** from A by replacing every occurance of B with C. The match 70326b6d90dSdrh ** must be exact. Collating sequences are not used. 70426b6d90dSdrh */ 70526b6d90dSdrh static void replaceFunc( 70626b6d90dSdrh sqlite3_context *context, 70726b6d90dSdrh int argc, 70826b6d90dSdrh sqlite3_value **argv 70926b6d90dSdrh ){ 71026b6d90dSdrh const unsigned char *zStr; /* The input string A */ 71126b6d90dSdrh const unsigned char *zPattern; /* The pattern string B */ 71226b6d90dSdrh const unsigned char *zRep; /* The replacement string C */ 71326b6d90dSdrh unsigned char *zOut; /* The output */ 71426b6d90dSdrh int nStr; /* Size of zStr */ 71526b6d90dSdrh int nPattern; /* Size of zPattern */ 71626b6d90dSdrh int nRep; /* Size of zRep */ 71726b6d90dSdrh int nOut; /* Maximum size of zOut */ 71826b6d90dSdrh int loopLimit; /* Last zStr[] that might match zPattern[] */ 71926b6d90dSdrh int i, j; /* Loop counters */ 72026b6d90dSdrh 72126b6d90dSdrh assert( argc==3 ); 7229310ef23Sdrh nStr = sqlite3_value_bytes(argv[0]); 72326b6d90dSdrh zStr = sqlite3_value_text(argv[0]); 7247a521cfbSdrh if( zStr==0 ) return; 7259310ef23Sdrh nPattern = sqlite3_value_bytes(argv[1]); 72626b6d90dSdrh zPattern = sqlite3_value_text(argv[1]); 727709cff33Sdrh if( zPattern==0 || zPattern[0]==0 ) return; 7289310ef23Sdrh nRep = sqlite3_value_bytes(argv[2]); 72926b6d90dSdrh zRep = sqlite3_value_text(argv[2]); 7307a521cfbSdrh if( zRep==0 ) return; 73126b6d90dSdrh if( nPattern>=nRep ){ 73226b6d90dSdrh nOut = nStr; 73326b6d90dSdrh }else{ 73426b6d90dSdrh nOut = (nStr/nPattern + 1)*nRep; 73526b6d90dSdrh } 73626b6d90dSdrh zOut = sqlite3_malloc(nOut+1); 73726b6d90dSdrh if( zOut==0 ) return; 73826b6d90dSdrh loopLimit = nStr - nPattern; 73926b6d90dSdrh for(i=j=0; i<=loopLimit; i++){ 74026b6d90dSdrh if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ 74126b6d90dSdrh zOut[j++] = zStr[i]; 74226b6d90dSdrh }else{ 74326b6d90dSdrh memcpy(&zOut[j], zRep, nRep); 74426b6d90dSdrh j += nRep; 74526b6d90dSdrh i += nPattern-1; 74626b6d90dSdrh } 74726b6d90dSdrh } 74826b6d90dSdrh memcpy(&zOut[j], &zStr[i], nStr-i); 74926b6d90dSdrh j += nStr - i; 75026b6d90dSdrh assert( j<=nOut ); 75126b6d90dSdrh zOut[j] = 0; 75226b6d90dSdrh sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); 75326b6d90dSdrh } 75426b6d90dSdrh 755309b3386Sdrh /* 756309b3386Sdrh ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. 757309b3386Sdrh ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. 758309b3386Sdrh */ 759309b3386Sdrh static void trimFunc( 760309b3386Sdrh sqlite3_context *context, 761309b3386Sdrh int argc, 762309b3386Sdrh sqlite3_value **argv 763309b3386Sdrh ){ 764309b3386Sdrh const unsigned char *zIn; /* Input string */ 765309b3386Sdrh const unsigned char *zCharSet; /* Set of characters to trim */ 766309b3386Sdrh int nIn; /* Number of bytes in input */ 767d1e3a616Sdrh int flags; /* 1: trimleft 2: trimright 3: trim */ 768d1e3a616Sdrh int i; /* Loop counter */ 769d1e3a616Sdrh unsigned char *aLen; /* Length of each character in zCharSet */ 770d1e3a616Sdrh const unsigned char **azChar; /* Individual characters in zCharSet */ 771d1e3a616Sdrh int nChar; /* Number of characters in zCharSet */ 772d1e3a616Sdrh 773309b3386Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 774309b3386Sdrh return; 775309b3386Sdrh } 7769310ef23Sdrh nIn = sqlite3_value_bytes(argv[0]); 777309b3386Sdrh zIn = sqlite3_value_text(argv[0]); 7787a521cfbSdrh if( zIn==0 ) return; 779309b3386Sdrh if( argc==1 ){ 780d1e3a616Sdrh static const unsigned char lenOne[] = { 1 }; 7818cff382eSdrh static const unsigned char *azOne[] = { (u8*)" " }; 782d1e3a616Sdrh nChar = 1; 7838cff382eSdrh aLen = (u8*)lenOne; 7848cff382eSdrh azChar = azOne; 785d1e3a616Sdrh zCharSet = 0; 7867a521cfbSdrh }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ 787309b3386Sdrh return; 788d1e3a616Sdrh }else{ 789d1e3a616Sdrh const unsigned char *z; 790d1e3a616Sdrh for(z=zCharSet, nChar=0; *z; nChar++){ 791d1e3a616Sdrh sqliteNextChar(z); 792309b3386Sdrh } 793d1e3a616Sdrh if( nChar>0 ){ 794d1e3a616Sdrh azChar = sqlite3_malloc( nChar*(sizeof(char*)+1) ); 795d1e3a616Sdrh if( azChar==0 ){ 796d1e3a616Sdrh return; 797d1e3a616Sdrh } 798d1e3a616Sdrh aLen = (unsigned char*)&azChar[nChar]; 799d1e3a616Sdrh for(z=zCharSet, nChar=0; *z; nChar++){ 800d1e3a616Sdrh azChar[nChar] = z; 801d1e3a616Sdrh sqliteNextChar(z); 802d1e3a616Sdrh aLen[nChar] = z - azChar[nChar]; 803d1e3a616Sdrh } 804d1e3a616Sdrh } 805d1e3a616Sdrh } 806d1e3a616Sdrh if( nChar>0 ){ 807309b3386Sdrh flags = (int)sqlite3_user_data(context); 808309b3386Sdrh if( flags & 1 ){ 809d1e3a616Sdrh while( nIn>0 ){ 810d1e3a616Sdrh int len; 811d1e3a616Sdrh for(i=0; i<nChar; i++){ 812d1e3a616Sdrh len = aLen[i]; 813d1e3a616Sdrh if( memcmp(zIn, azChar[i], len)==0 ) break; 814d1e3a616Sdrh } 815d1e3a616Sdrh if( i>=nChar ) break; 816d1e3a616Sdrh zIn += len; 817d1e3a616Sdrh nIn -= len; 818309b3386Sdrh } 819309b3386Sdrh } 820309b3386Sdrh if( flags & 2 ){ 821d1e3a616Sdrh while( nIn>0 ){ 822d1e3a616Sdrh int len; 823d1e3a616Sdrh for(i=0; i<nChar; i++){ 824d1e3a616Sdrh len = aLen[i]; 825d1e3a616Sdrh if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break; 826309b3386Sdrh } 827d1e3a616Sdrh if( i>=nChar ) break; 828d1e3a616Sdrh nIn -= len; 829d1e3a616Sdrh } 830d1e3a616Sdrh } 831d1e3a616Sdrh if( zCharSet ){ 832d1e3a616Sdrh sqlite3_free(azChar); 833309b3386Sdrh } 834309b3386Sdrh } 835309b3386Sdrh sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); 836309b3386Sdrh } 83726b6d90dSdrh 838d24cc427Sdrh #ifdef SQLITE_SOUNDEX 839d24cc427Sdrh /* 840d24cc427Sdrh ** Compute the soundex encoding of a word. 841d24cc427Sdrh */ 842137c728fSdrh static void soundexFunc( 843137c728fSdrh sqlite3_context *context, 844137c728fSdrh int argc, 845137c728fSdrh sqlite3_value **argv 846137c728fSdrh ){ 847d24cc427Sdrh char zResult[8]; 8484c755c0fSdrh const u8 *zIn; 849d24cc427Sdrh int i, j; 850d24cc427Sdrh static const unsigned char iCode[] = { 851d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 852d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 853d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 854d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 855d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 856d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 857d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 858d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 859d24cc427Sdrh }; 860d24cc427Sdrh assert( argc==1 ); 8614c755c0fSdrh zIn = (u8*)sqlite3_value_text(argv[0]); 862bdf67e0eSdrh if( zIn==0 ) zIn = (u8*)""; 863d24cc427Sdrh for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} 864d24cc427Sdrh if( zIn[i] ){ 865bdf67e0eSdrh u8 prevcode = iCode[zIn[i]&0x7f]; 866d24cc427Sdrh zResult[0] = toupper(zIn[i]); 867d24cc427Sdrh for(j=1; j<4 && zIn[i]; i++){ 868d24cc427Sdrh int code = iCode[zIn[i]&0x7f]; 869d24cc427Sdrh if( code>0 ){ 870bdf67e0eSdrh if( code!=prevcode ){ 871bdf67e0eSdrh prevcode = code; 872d24cc427Sdrh zResult[j++] = code + '0'; 873d24cc427Sdrh } 874bdf67e0eSdrh }else{ 875bdf67e0eSdrh prevcode = 0; 876bdf67e0eSdrh } 877d24cc427Sdrh } 878d24cc427Sdrh while( j<4 ){ 879d24cc427Sdrh zResult[j++] = '0'; 880d24cc427Sdrh } 881d24cc427Sdrh zResult[j] = 0; 882d8123366Sdanielk1977 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); 883d24cc427Sdrh }else{ 884d8123366Sdanielk1977 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); 885d24cc427Sdrh } 886d24cc427Sdrh } 887d24cc427Sdrh #endif 888d24cc427Sdrh 889fdb83b2fSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 890fdb83b2fSdrh /* 891fdb83b2fSdrh ** A function that loads a shared-library extension then returns NULL. 892fdb83b2fSdrh */ 893fdb83b2fSdrh static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ 89465fd59f7Sdanielk1977 const char *zFile = (const char *)sqlite3_value_text(argv[0]); 8957a521cfbSdrh const char *zProc; 896fdb83b2fSdrh sqlite3 *db = sqlite3_user_data(context); 897fdb83b2fSdrh char *zErrMsg = 0; 898fdb83b2fSdrh 899fdb83b2fSdrh if( argc==2 ){ 90065fd59f7Sdanielk1977 zProc = (const char *)sqlite3_value_text(argv[1]); 9017a521cfbSdrh }else{ 9027a521cfbSdrh zProc = 0; 903fdb83b2fSdrh } 9047a521cfbSdrh if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ 905fdb83b2fSdrh sqlite3_result_error(context, zErrMsg, -1); 906fdb83b2fSdrh sqlite3_free(zErrMsg); 907fdb83b2fSdrh } 908fdb83b2fSdrh } 909fdb83b2fSdrh #endif 910fdb83b2fSdrh 911193a6b41Sdrh #ifdef SQLITE_TEST 912193a6b41Sdrh /* 913193a6b41Sdrh ** This function generates a string of random characters. Used for 914193a6b41Sdrh ** generating test data. 915193a6b41Sdrh */ 9160ae8b831Sdanielk1977 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ 917bbd82df6Sdrh static const unsigned char zSrc[] = 918193a6b41Sdrh "abcdefghijklmnopqrstuvwxyz" 919193a6b41Sdrh "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 920193a6b41Sdrh "0123456789" 921193a6b41Sdrh ".-!,:*^+=_|?/<> "; 922193a6b41Sdrh int iMin, iMax, n, r, i; 923bbd82df6Sdrh unsigned char zBuf[1000]; 924193a6b41Sdrh if( argc>=1 ){ 925f9b596ebSdrh iMin = sqlite3_value_int(argv[0]); 926193a6b41Sdrh if( iMin<0 ) iMin = 0; 927193a6b41Sdrh if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; 928193a6b41Sdrh }else{ 929193a6b41Sdrh iMin = 1; 930193a6b41Sdrh } 931193a6b41Sdrh if( argc>=2 ){ 932f9b596ebSdrh iMax = sqlite3_value_int(argv[1]); 933193a6b41Sdrh if( iMax<iMin ) iMax = iMin; 9341dba7279Sdrh if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; 935193a6b41Sdrh }else{ 936193a6b41Sdrh iMax = 50; 937193a6b41Sdrh } 938193a6b41Sdrh n = iMin; 939193a6b41Sdrh if( iMax>iMin ){ 9404adee20fSdanielk1977 sqlite3Randomness(sizeof(r), &r); 941bbd82df6Sdrh r &= 0x7fffffff; 942193a6b41Sdrh n += r%(iMax + 1 - iMin); 943193a6b41Sdrh } 9441dba7279Sdrh assert( n<sizeof(zBuf) ); 9454adee20fSdanielk1977 sqlite3Randomness(n, zBuf); 946193a6b41Sdrh for(i=0; i<n; i++){ 947bbd82df6Sdrh zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; 948193a6b41Sdrh } 949193a6b41Sdrh zBuf[n] = 0; 9502646da7eSdrh sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); 951d8123366Sdanielk1977 } 9520e3d7476Sdrh #endif /* SQLITE_TEST */ 953d8123366Sdanielk1977 9540e3d7476Sdrh #ifdef SQLITE_TEST 955d8123366Sdanielk1977 /* 956d8123366Sdanielk1977 ** The following two SQL functions are used to test returning a text 957d8123366Sdanielk1977 ** result with a destructor. Function 'test_destructor' takes one argument 958d8123366Sdanielk1977 ** and returns the same argument interpreted as TEXT. A destructor is 959d8123366Sdanielk1977 ** passed with the sqlite3_result_text() call. 960d8123366Sdanielk1977 ** 961d8123366Sdanielk1977 ** SQL function 'test_destructor_count' returns the number of outstanding 962d8123366Sdanielk1977 ** allocations made by 'test_destructor'; 963d8123366Sdanielk1977 ** 964d8123366Sdanielk1977 ** WARNING: Not threadsafe. 965d8123366Sdanielk1977 */ 966d8123366Sdanielk1977 static int test_destructor_count_var = 0; 967d8123366Sdanielk1977 static void destructor(void *p){ 968d8123366Sdanielk1977 char *zVal = (char *)p; 969d8123366Sdanielk1977 assert(zVal); 970d8123366Sdanielk1977 zVal--; 971d8123366Sdanielk1977 sqliteFree(zVal); 972d8123366Sdanielk1977 test_destructor_count_var--; 973d8123366Sdanielk1977 } 974d8123366Sdanielk1977 static void test_destructor( 975d8123366Sdanielk1977 sqlite3_context *pCtx, 976d8123366Sdanielk1977 int nArg, 977d8123366Sdanielk1977 sqlite3_value **argv 978d8123366Sdanielk1977 ){ 979d8123366Sdanielk1977 char *zVal; 980f4618891Sdanielk1977 int len; 9819bb575fdSdrh sqlite3 *db = sqlite3_user_data(pCtx); 982f4618891Sdanielk1977 983d8123366Sdanielk1977 test_destructor_count_var++; 984d8123366Sdanielk1977 assert( nArg==1 ); 985d8123366Sdanielk1977 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 98614db2665Sdanielk1977 len = sqlite3ValueBytes(argv[0], ENC(db)); 987f4618891Sdanielk1977 zVal = sqliteMalloc(len+3); 988f4618891Sdanielk1977 zVal[len] = 0; 989f4618891Sdanielk1977 zVal[len-1] = 0; 990d8123366Sdanielk1977 assert( zVal ); 991d8123366Sdanielk1977 zVal++; 99214db2665Sdanielk1977 memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len); 99314db2665Sdanielk1977 if( ENC(db)==SQLITE_UTF8 ){ 994d8123366Sdanielk1977 sqlite3_result_text(pCtx, zVal, -1, destructor); 9956c62608fSdrh #ifndef SQLITE_OMIT_UTF16 99614db2665Sdanielk1977 }else if( ENC(db)==SQLITE_UTF16LE ){ 997f4618891Sdanielk1977 sqlite3_result_text16le(pCtx, zVal, -1, destructor); 998f4618891Sdanielk1977 }else{ 999f4618891Sdanielk1977 sqlite3_result_text16be(pCtx, zVal, -1, destructor); 10006c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 1001f4618891Sdanielk1977 } 1002d8123366Sdanielk1977 } 1003d8123366Sdanielk1977 static void test_destructor_count( 1004d8123366Sdanielk1977 sqlite3_context *pCtx, 1005d8123366Sdanielk1977 int nArg, 1006d8123366Sdanielk1977 sqlite3_value **argv 1007d8123366Sdanielk1977 ){ 1008d8123366Sdanielk1977 sqlite3_result_int(pCtx, test_destructor_count_var); 1009193a6b41Sdrh } 10100e3d7476Sdrh #endif /* SQLITE_TEST */ 10113f6b0874Sdanielk1977 10120e3d7476Sdrh #ifdef SQLITE_TEST 10130e3d7476Sdrh /* 10140e3d7476Sdrh ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() 10150e3d7476Sdrh ** interface. 10160e3d7476Sdrh ** 10170e3d7476Sdrh ** The test_auxdata() SQL function attempts to register each of its arguments 10180e3d7476Sdrh ** as auxiliary data. If there are no prior registrations of aux data for 10190e3d7476Sdrh ** that argument (meaning the argument is not a constant or this is its first 10200e3d7476Sdrh ** call) then the result for that argument is 0. If there is a prior 10210e3d7476Sdrh ** registration, the result for that argument is 1. The overall result 10220e3d7476Sdrh ** is the individual argument results separated by spaces. 10230e3d7476Sdrh */ 10243f6b0874Sdanielk1977 static void free_test_auxdata(void *p) {sqliteFree(p);} 10253f6b0874Sdanielk1977 static void test_auxdata( 10263f6b0874Sdanielk1977 sqlite3_context *pCtx, 10273f6b0874Sdanielk1977 int nArg, 10283f6b0874Sdanielk1977 sqlite3_value **argv 10293f6b0874Sdanielk1977 ){ 10303f6b0874Sdanielk1977 int i; 10313f6b0874Sdanielk1977 char *zRet = sqliteMalloc(nArg*2); 10323f6b0874Sdanielk1977 if( !zRet ) return; 10333f6b0874Sdanielk1977 for(i=0; i<nArg; i++){ 10342646da7eSdrh char const *z = (char*)sqlite3_value_text(argv[i]); 10353f6b0874Sdanielk1977 if( z ){ 10363f6b0874Sdanielk1977 char *zAux = sqlite3_get_auxdata(pCtx, i); 10373f6b0874Sdanielk1977 if( zAux ){ 10383f6b0874Sdanielk1977 zRet[i*2] = '1'; 10393f6b0874Sdanielk1977 if( strcmp(zAux, z) ){ 10403f6b0874Sdanielk1977 sqlite3_result_error(pCtx, "Auxilary data corruption", -1); 10413f6b0874Sdanielk1977 return; 10423f6b0874Sdanielk1977 } 10433f6b0874Sdanielk1977 }else{ 10443f6b0874Sdanielk1977 zRet[i*2] = '0'; 10453f6b0874Sdanielk1977 zAux = sqliteStrDup(z); 10463f6b0874Sdanielk1977 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); 10473f6b0874Sdanielk1977 } 10483f6b0874Sdanielk1977 zRet[i*2+1] = ' '; 10493f6b0874Sdanielk1977 } 10503f6b0874Sdanielk1977 } 10513f6b0874Sdanielk1977 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); 10523f6b0874Sdanielk1977 } 10530e3d7476Sdrh #endif /* SQLITE_TEST */ 1054193a6b41Sdrh 105501427a62Sdanielk1977 #ifdef SQLITE_TEST 105601427a62Sdanielk1977 /* 105701427a62Sdanielk1977 ** A function to test error reporting from user functions. This function 105801427a62Sdanielk1977 ** returns a copy of it's first argument as an error. 105901427a62Sdanielk1977 */ 106001427a62Sdanielk1977 static void test_error( 106101427a62Sdanielk1977 sqlite3_context *pCtx, 106201427a62Sdanielk1977 int nArg, 106301427a62Sdanielk1977 sqlite3_value **argv 106401427a62Sdanielk1977 ){ 10652646da7eSdrh sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0); 106601427a62Sdanielk1977 } 106701427a62Sdanielk1977 #endif /* SQLITE_TEST */ 106801427a62Sdanielk1977 10690ac65892Sdrh /* 1070d3a149efSdrh ** An instance of the following structure holds the context of a 1071dd5baa95Sdrh ** sum() or avg() aggregate computation. 1072dd5baa95Sdrh */ 1073dd5baa95Sdrh typedef struct SumCtx SumCtx; 1074dd5baa95Sdrh struct SumCtx { 10758c08e861Sdrh double rSum; /* Floating point sum */ 10768c08e861Sdrh i64 iSum; /* Integer sum */ 1077cf85a51cSdrh i64 cnt; /* Number of elements summed */ 10788c08e861Sdrh u8 overflow; /* True if integer overflow seen */ 10798c08e861Sdrh u8 approx; /* True if non-integer value was input to the sum */ 1080dd5baa95Sdrh }; 1081dd5baa95Sdrh 1082dd5baa95Sdrh /* 1083a97fdd3bSdrh ** Routines used to compute the sum, average, and total. 1084a97fdd3bSdrh ** 1085a97fdd3bSdrh ** The SUM() function follows the (broken) SQL standard which means 1086a97fdd3bSdrh ** that it returns NULL if it sums over no inputs. TOTAL returns 1087a97fdd3bSdrh ** 0.0 in that case. In addition, TOTAL always returns a float where 1088a97fdd3bSdrh ** SUM might return an integer if it never encounters a floating point 1089c806d857Sdrh ** value. TOTAL never fails, but SUM might through an exception if 1090c806d857Sdrh ** it overflows an integer. 1091dd5baa95Sdrh */ 10920ae8b831Sdanielk1977 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 1093dd5baa95Sdrh SumCtx *p; 10943d1d95e6Sdrh int type; 10953f219f46Sdrh assert( argc==1 ); 10964f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 109729d72108Sdrh type = sqlite3_value_numeric_type(argv[0]); 10983d1d95e6Sdrh if( p && type!=SQLITE_NULL ){ 1099739105c7Sdrh p->cnt++; 110029d72108Sdrh if( type==SQLITE_INTEGER ){ 11018c08e861Sdrh i64 v = sqlite3_value_int64(argv[0]); 11028c08e861Sdrh p->rSum += v; 11038c08e861Sdrh if( (p->approx|p->overflow)==0 ){ 11048c08e861Sdrh i64 iNewSum = p->iSum + v; 11058c08e861Sdrh int s1 = p->iSum >> (sizeof(i64)*8-1); 11068c08e861Sdrh int s2 = v >> (sizeof(i64)*8-1); 11078c08e861Sdrh int s3 = iNewSum >> (sizeof(i64)*8-1); 11088c08e861Sdrh p->overflow = (s1&s2&~s3) | (~s1&~s2&s3); 11098c08e861Sdrh p->iSum = iNewSum; 111029d72108Sdrh } 111129d72108Sdrh }else{ 11128c08e861Sdrh p->rSum += sqlite3_value_double(argv[0]); 111329d72108Sdrh p->approx = 1; 11143f219f46Sdrh } 1115739105c7Sdrh } 1116dd5baa95Sdrh } 11170ae8b831Sdanielk1977 static void sumFinalize(sqlite3_context *context){ 1118dd5baa95Sdrh SumCtx *p; 1119abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1120c2bd913aSdrh if( p && p->cnt>0 ){ 11218c08e861Sdrh if( p->overflow ){ 11228c08e861Sdrh sqlite3_result_error(context,"integer overflow",-1); 11238c08e861Sdrh }else if( p->approx ){ 11248c08e861Sdrh sqlite3_result_double(context, p->rSum); 1125c2bd913aSdrh }else{ 11268c08e861Sdrh sqlite3_result_int64(context, p->iSum); 11273d1d95e6Sdrh } 1128dd5baa95Sdrh } 1129c2bd913aSdrh } 11300ae8b831Sdanielk1977 static void avgFinalize(sqlite3_context *context){ 1131dd5baa95Sdrh SumCtx *p; 1132abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1133739105c7Sdrh if( p && p->cnt>0 ){ 11348c08e861Sdrh sqlite3_result_double(context, p->rSum/(double)p->cnt); 1135dd5baa95Sdrh } 1136dd5baa95Sdrh } 1137a97fdd3bSdrh static void totalFinalize(sqlite3_context *context){ 1138a97fdd3bSdrh SumCtx *p; 1139a97fdd3bSdrh p = sqlite3_aggregate_context(context, 0); 11408c08e861Sdrh sqlite3_result_double(context, p ? p->rSum : 0.0); 1141a97fdd3bSdrh } 1142dd5baa95Sdrh 1143dd5baa95Sdrh /* 11440bce8354Sdrh ** The following structure keeps track of state information for the 11450bce8354Sdrh ** count() aggregate function. 11460bce8354Sdrh */ 11470bce8354Sdrh typedef struct CountCtx CountCtx; 11480bce8354Sdrh struct CountCtx { 1149fc6ad39cSdrh i64 n; 11500bce8354Sdrh }; 1151dd5baa95Sdrh 11520bce8354Sdrh /* 11530bce8354Sdrh ** Routines to implement the count() aggregate function. 11540bce8354Sdrh */ 11550ae8b831Sdanielk1977 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 11560bce8354Sdrh CountCtx *p; 11574f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 11589c054830Sdrh if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ 11590bce8354Sdrh p->n++; 11600bce8354Sdrh } 11610bce8354Sdrh } 11620ae8b831Sdanielk1977 static void countFinalize(sqlite3_context *context){ 11630bce8354Sdrh CountCtx *p; 1164abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1165fc6ad39cSdrh sqlite3_result_int64(context, p ? p->n : 0); 11660bce8354Sdrh } 11670bce8354Sdrh 11680bce8354Sdrh /* 11690bce8354Sdrh ** Routines to implement min() and max() aggregate functions. 11700bce8354Sdrh */ 11710ae8b831Sdanielk1977 static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 117288208050Sdanielk1977 Mem *pArg = (Mem *)argv[0]; 11739eb516c0Sdrh Mem *pBest; 11749eb516c0Sdrh 11759eb516c0Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 11769eb516c0Sdrh pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); 11773aeab9e4Sdanielk1977 if( !pBest ) return; 1178268380caSdrh 117988208050Sdanielk1977 if( pBest->flags ){ 11809eb516c0Sdrh int max; 11819eb516c0Sdrh int cmp; 1182dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 11837e18c259Sdanielk1977 /* This step function is used for both the min() and max() aggregates, 11847e18c259Sdanielk1977 ** the only difference between the two being that the sense of the 11857e18c259Sdanielk1977 ** comparison is inverted. For the max() aggregate, the 11867e18c259Sdanielk1977 ** sqlite3_user_data() function returns (void *)-1. For min() it 11877e18c259Sdanielk1977 ** returns (void *)db, where db is the sqlite3* database pointer. 11887e18c259Sdanielk1977 ** Therefore the next statement sets variable 'max' to 1 for the max() 11897e18c259Sdanielk1977 ** aggregate, or 0 for min(). 11907e18c259Sdanielk1977 */ 1191309b3386Sdrh max = sqlite3_user_data(context)!=0; 1192dc1bdc4fSdanielk1977 cmp = sqlite3MemCompare(pBest, pArg, pColl); 119388208050Sdanielk1977 if( (max && cmp<0) || (!max && cmp>0) ){ 11947e18c259Sdanielk1977 sqlite3VdbeMemCopy(pBest, pArg); 119588208050Sdanielk1977 } 11960bce8354Sdrh }else{ 11977e18c259Sdanielk1977 sqlite3VdbeMemCopy(pBest, pArg); 11980bce8354Sdrh } 11990bce8354Sdrh } 12000ae8b831Sdanielk1977 static void minMaxFinalize(sqlite3_context *context){ 120188208050Sdanielk1977 sqlite3_value *pRes; 1202abfcea25Sdrh pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); 1203abfcea25Sdrh if( pRes ){ 120488208050Sdanielk1977 if( pRes->flags ){ 1205f4479501Sdrh sqlite3_result_value(context, pRes); 12060bce8354Sdrh } 1207b20e56b4Sdanielk1977 sqlite3VdbeMemRelease(pRes); 12080bce8354Sdrh } 1209abfcea25Sdrh } 1210dd5baa95Sdrh 12114e5ffc5fSdrh 1212d3a149efSdrh /* 1213a2ed5601Sdrh ** This function registered all of the above C functions as SQL 1214a2ed5601Sdrh ** functions. This should be the only routine in this file with 1215a2ed5601Sdrh ** external linkage. 1216dc04c583Sdrh */ 12179bb575fdSdrh void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ 12185719628aSdrh static const struct { 12190bce8354Sdrh char *zName; 1220268380caSdrh signed char nArg; 1221309b3386Sdrh u8 argType; /* ff: db 1: 0, 2: 1, 3: 2,... N: N-1. */ 1222d02eb1fdSdanielk1977 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */ 1223dc1bdc4fSdanielk1977 u8 needCollSeq; 12240ae8b831Sdanielk1977 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); 12250bce8354Sdrh } aFuncs[] = { 1226d8123366Sdanielk1977 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc }, 1227d8123366Sdanielk1977 { "min", 0, 0, SQLITE_UTF8, 1, 0 }, 1228309b3386Sdrh { "max", -1, 1, SQLITE_UTF8, 1, minmaxFunc }, 1229309b3386Sdrh { "max", 0, 1, SQLITE_UTF8, 1, 0 }, 1230d8123366Sdanielk1977 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc }, 1231d8123366Sdanielk1977 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc }, 1232d8123366Sdanielk1977 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc }, 12336c62608fSdrh #ifndef SQLITE_OMIT_UTF16 1234f4618891Sdanielk1977 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr }, 12356c62608fSdrh #endif 1236d8123366Sdanielk1977 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc }, 1237d8123366Sdanielk1977 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc }, 1238d8123366Sdanielk1977 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc }, 1239d8123366Sdanielk1977 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc }, 1240d8123366Sdanielk1977 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc }, 1241d8123366Sdanielk1977 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc }, 1242d8123366Sdanielk1977 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 }, 1243d8123366Sdanielk1977 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 }, 1244137c728fSdrh { "hex", 1, 0, SQLITE_UTF8, 0, hexFunc }, 1245d8123366Sdanielk1977 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc }, 1246d8123366Sdanielk1977 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc }, 1247137c728fSdrh { "randomblob", 1, 0, SQLITE_UTF8, 0, randomBlob }, 124894a98365Sdrh { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc }, 1249d8123366Sdanielk1977 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc}, 1250d8123366Sdanielk1977 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc }, 1251309b3386Sdrh { "last_insert_rowid", 0, 0xff, SQLITE_UTF8, 0, last_insert_rowid }, 1252309b3386Sdrh { "changes", 0, 0xff, SQLITE_UTF8, 0, changes }, 1253309b3386Sdrh { "total_changes", 0, 0xff, SQLITE_UTF8, 0, total_changes }, 125426b6d90dSdrh { "replace", 3, 0, SQLITE_UTF8, 0, replaceFunc }, 1255309b3386Sdrh { "ltrim", 1, 1, SQLITE_UTF8, 0, trimFunc }, 1256309b3386Sdrh { "ltrim", 2, 1, SQLITE_UTF8, 0, trimFunc }, 1257309b3386Sdrh { "rtrim", 1, 2, SQLITE_UTF8, 0, trimFunc }, 1258309b3386Sdrh { "rtrim", 2, 2, SQLITE_UTF8, 0, trimFunc }, 1259309b3386Sdrh { "trim", 1, 3, SQLITE_UTF8, 0, trimFunc }, 1260309b3386Sdrh { "trim", 2, 3, SQLITE_UTF8, 0, trimFunc }, 12618cff382eSdrh { "zeroblob", 1, 0, SQLITE_UTF8, 0, zeroblobFunc }, 1262d24cc427Sdrh #ifdef SQLITE_SOUNDEX 1263d8123366Sdanielk1977 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc}, 1264d24cc427Sdrh #endif 1265fdb83b2fSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 1266309b3386Sdrh { "load_extension", 1, 0xff, SQLITE_UTF8, 0, loadExt }, 1267309b3386Sdrh { "load_extension", 2, 0xff, SQLITE_UTF8, 0, loadExt }, 1268fdb83b2fSdrh #endif 1269193a6b41Sdrh #ifdef SQLITE_TEST 1270d8123366Sdanielk1977 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr }, 1271309b3386Sdrh { "test_destructor", 1, 0xff, SQLITE_UTF8, 0, test_destructor}, 1272d8123366Sdanielk1977 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count}, 12733f6b0874Sdanielk1977 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata}, 127401427a62Sdanielk1977 { "test_error", 1, 0, SQLITE_UTF8, 0, test_error}, 1275193a6b41Sdrh #endif 12760bce8354Sdrh }; 12775719628aSdrh static const struct { 12780bce8354Sdrh char *zName; 1279268380caSdrh signed char nArg; 1280268380caSdrh u8 argType; 1281dc1bdc4fSdanielk1977 u8 needCollSeq; 12820ae8b831Sdanielk1977 void (*xStep)(sqlite3_context*,int,sqlite3_value**); 12830ae8b831Sdanielk1977 void (*xFinalize)(sqlite3_context*); 12840bce8354Sdrh } aAggs[] = { 1285dc1bdc4fSdanielk1977 { "min", 1, 0, 1, minmaxStep, minMaxFinalize }, 1286309b3386Sdrh { "max", 1, 1, 1, minmaxStep, minMaxFinalize }, 1287dc1bdc4fSdanielk1977 { "sum", 1, 0, 0, sumStep, sumFinalize }, 1288a97fdd3bSdrh { "total", 1, 0, 0, sumStep, totalFinalize }, 1289dc1bdc4fSdanielk1977 { "avg", 1, 0, 0, sumStep, avgFinalize }, 1290dc1bdc4fSdanielk1977 { "count", 0, 0, 0, countStep, countFinalize }, 1291dc1bdc4fSdanielk1977 { "count", 1, 0, 0, countStep, countFinalize }, 12920bce8354Sdrh }; 12930bce8354Sdrh int i; 12940bce8354Sdrh 12950bce8354Sdrh for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 1296309b3386Sdrh void *pArg; 1297309b3386Sdrh u8 argType = aFuncs[i].argType; 1298309b3386Sdrh if( argType==0xff ){ 1299309b3386Sdrh pArg = db; 1300309b3386Sdrh }else{ 1301309b3386Sdrh pArg = (void*)(int)argType; 1302c572ef7fSdanielk1977 } 1303771151b6Sdanielk1977 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg, 1304f9d64d2cSdanielk1977 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0); 1305dc1bdc4fSdanielk1977 if( aFuncs[i].needCollSeq ){ 1306dc1bdc4fSdanielk1977 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, 1307dc1bdc4fSdanielk1977 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0); 1308dc1bdc4fSdanielk1977 if( pFunc && aFuncs[i].needCollSeq ){ 1309dc1bdc4fSdanielk1977 pFunc->needCollSeq = 1; 1310dc1bdc4fSdanielk1977 } 1311dc1bdc4fSdanielk1977 } 13120bce8354Sdrh } 13131f01ec1bSdrh #ifndef SQLITE_OMIT_ALTERTABLE 13141f01ec1bSdrh sqlite3AlterFunctions(db); 13151f01ec1bSdrh #endif 1316198bf391Sdrh #ifndef SQLITE_OMIT_PARSER 1317f744bb56Sdanielk1977 sqlite3AttachFunctions(db); 1318198bf391Sdrh #endif 13190bce8354Sdrh for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ 1320309b3386Sdrh void *pArg = (void*)(int)aAggs[i].argType; 1321771151b6Sdanielk1977 sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, 1322f9d64d2cSdanielk1977 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize); 1323dc1bdc4fSdanielk1977 if( aAggs[i].needCollSeq ){ 1324dc1bdc4fSdanielk1977 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName, 1325d8123366Sdanielk1977 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0); 1326dc1bdc4fSdanielk1977 if( pFunc && aAggs[i].needCollSeq ){ 1327dc1bdc4fSdanielk1977 pFunc->needCollSeq = 1; 1328dc1bdc4fSdanielk1977 } 1329dc1bdc4fSdanielk1977 } 1330268380caSdrh } 13314adee20fSdanielk1977 sqlite3RegisterDateTimeFunctions(db); 1332b7481e70Sdrh sqlite3_overload_function(db, "MATCH", 2); 1333fd9e1f31Sdanielk1977 #ifdef SQLITE_SSE 13343752785fSdrh (void)sqlite3SseFunctions(db); 1335fd9e1f31Sdanielk1977 #endif 133655ef4d97Sdrh #ifdef SQLITE_CASE_SENSITIVE_LIKE 133755ef4d97Sdrh sqlite3RegisterLikeFunctions(db, 1); 133855ef4d97Sdrh #else 133955ef4d97Sdrh sqlite3RegisterLikeFunctions(db, 0); 134055ef4d97Sdrh #endif 134155ef4d97Sdrh } 134255ef4d97Sdrh 134355ef4d97Sdrh /* 134455ef4d97Sdrh ** Set the LIKEOPT flag on the 2-argument function with the given name. 134555ef4d97Sdrh */ 1346d64fe2f3Sdrh static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){ 134755ef4d97Sdrh FuncDef *pDef; 134855ef4d97Sdrh pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0); 134955ef4d97Sdrh if( pDef ){ 1350d64fe2f3Sdrh pDef->flags = flagVal; 135155ef4d97Sdrh } 135255ef4d97Sdrh } 135355ef4d97Sdrh 135455ef4d97Sdrh /* 135555ef4d97Sdrh ** Register the built-in LIKE and GLOB functions. The caseSensitive 135655ef4d97Sdrh ** parameter determines whether or not the LIKE operator is case 135755ef4d97Sdrh ** sensitive. GLOB is always case sensitive. 135855ef4d97Sdrh */ 135955ef4d97Sdrh void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 136055ef4d97Sdrh struct compareInfo *pInfo; 136155ef4d97Sdrh if( caseSensitive ){ 136255ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoAlt; 136355ef4d97Sdrh }else{ 136455ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoNorm; 136555ef4d97Sdrh } 1366771151b6Sdanielk1977 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0); 1367771151b6Sdanielk1977 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0); 1368771151b6Sdanielk1977 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 136955ef4d97Sdrh (struct compareInfo*)&globInfo, likeFunc, 0,0); 1370d64fe2f3Sdrh setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); 1371d64fe2f3Sdrh setLikeOptFlag(db, "like", 1372d64fe2f3Sdrh caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); 137355ef4d97Sdrh } 137455ef4d97Sdrh 137555ef4d97Sdrh /* 137655ef4d97Sdrh ** pExpr points to an expression which implements a function. If 137755ef4d97Sdrh ** it is appropriate to apply the LIKE optimization to that function 137855ef4d97Sdrh ** then set aWc[0] through aWc[2] to the wildcard characters and 137955ef4d97Sdrh ** return TRUE. If the function is not a LIKE-style function then 138055ef4d97Sdrh ** return FALSE. 138155ef4d97Sdrh */ 1382d64fe2f3Sdrh int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 138355ef4d97Sdrh FuncDef *pDef; 138455ef4d97Sdrh if( pExpr->op!=TK_FUNCTION ){ 138555ef4d97Sdrh return 0; 138655ef4d97Sdrh } 138755ef4d97Sdrh if( pExpr->pList->nExpr!=2 ){ 138855ef4d97Sdrh return 0; 138955ef4d97Sdrh } 13902646da7eSdrh pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2, 139155ef4d97Sdrh SQLITE_UTF8, 0); 1392d64fe2f3Sdrh if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){ 139355ef4d97Sdrh return 0; 139455ef4d97Sdrh } 139555ef4d97Sdrh 139655ef4d97Sdrh /* The memcpy() statement assumes that the wildcard characters are 139755ef4d97Sdrh ** the first three statements in the compareInfo structure. The 139855ef4d97Sdrh ** asserts() that follow verify that assumption 139955ef4d97Sdrh */ 140055ef4d97Sdrh memcpy(aWc, pDef->pUserData, 3); 140155ef4d97Sdrh assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 140255ef4d97Sdrh assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 140355ef4d97Sdrh assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 1404d64fe2f3Sdrh *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0; 140555ef4d97Sdrh return 1; 1406dc04c583Sdrh } 1407