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*709cff33Sdrh ** $Id: func.c,v 1.141 2007/04/27 01:18:03 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; 218dc04c583Sdrh int i; 2199c054830Sdrh if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 2207a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 2217a521cfbSdrh if( z2 ){ 2227a521cfbSdrh z1 = sqlite3_malloc(sqlite3_value_bytes(argv[0])+1); 2237a521cfbSdrh if( z1 ){ 2247a521cfbSdrh strcpy(z1, z2); 2257a521cfbSdrh for(i=0; z1[i]; i++){ 2267a521cfbSdrh z1[i] = toupper(z1[i]); 227dc04c583Sdrh } 2287a521cfbSdrh sqlite3_result_text(context, z1, -1, sqlite3_free); 2297a521cfbSdrh } 2307a521cfbSdrh } 231dc04c583Sdrh } 2320ae8b831Sdanielk1977 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 2337a521cfbSdrh char *z1; 2347a521cfbSdrh const char *z2; 235dc04c583Sdrh int i; 2369c054830Sdrh if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 2377a521cfbSdrh z2 = (char*)sqlite3_value_text(argv[0]); 2387a521cfbSdrh if( z2 ){ 2397a521cfbSdrh z1 = sqlite3_malloc(sqlite3_value_bytes(argv[0])+1); 2407a521cfbSdrh if( z1 ){ 2417a521cfbSdrh strcpy(z1, z2); 2427a521cfbSdrh for(i=0; z1[i]; i++){ 2437a521cfbSdrh z1[i] = tolower(z1[i]); 244dc04c583Sdrh } 2457a521cfbSdrh sqlite3_result_text(context, z1, -1, sqlite3_free); 2467a521cfbSdrh } 2477a521cfbSdrh } 248dc04c583Sdrh } 249dc04c583Sdrh 250dc04c583Sdrh /* 251fbc99082Sdrh ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. 252b6c9e6e6Sjplyon ** All three do the same thing. They return the first non-NULL 253b6c9e6e6Sjplyon ** argument. 2543212e182Sdrh */ 255f9b596ebSdrh static void ifnullFunc( 256f9b596ebSdrh sqlite3_context *context, 257f9b596ebSdrh int argc, 258f9b596ebSdrh sqlite3_value **argv 259f9b596ebSdrh ){ 260fbc99082Sdrh int i; 261fbc99082Sdrh for(i=0; i<argc; i++){ 2629c054830Sdrh if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ 263f4479501Sdrh sqlite3_result_value(context, argv[i]); 264fbc99082Sdrh break; 265fbc99082Sdrh } 266fbc99082Sdrh } 2673212e182Sdrh } 2683212e182Sdrh 2693212e182Sdrh /* 270f9ffac96Sdrh ** Implementation of random(). Return a random integer. 271f9ffac96Sdrh */ 272f9b596ebSdrh static void randomFunc( 273f9b596ebSdrh sqlite3_context *context, 274f9b596ebSdrh int argc, 275f9b596ebSdrh sqlite3_value **argv 276f9b596ebSdrh ){ 27752fc849aSdrh sqlite_int64 r; 2784adee20fSdanielk1977 sqlite3Randomness(sizeof(r), &r); 279874abbedSdrh if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */ 280874abbedSdrh /* can always do abs() of the result */ 28152fc849aSdrh sqlite3_result_int64(context, r); 282f9ffac96Sdrh } 283f9ffac96Sdrh 284f9ffac96Sdrh /* 285137c728fSdrh ** Implementation of randomblob(N). Return a random blob 286137c728fSdrh ** that is N bytes long. 28763cf66f0Sdrh */ 288137c728fSdrh static void randomBlob( 28963cf66f0Sdrh sqlite3_context *context, 29063cf66f0Sdrh int argc, 29163cf66f0Sdrh sqlite3_value **argv 29263cf66f0Sdrh ){ 293137c728fSdrh int n; 294137c728fSdrh unsigned char *p; 29563cf66f0Sdrh assert( argc==1 ); 29663cf66f0Sdrh n = sqlite3_value_int(argv[0]); 297137c728fSdrh if( n<1 ) n = 1; 298137c728fSdrh p = sqlite3_malloc(n); 299137c728fSdrh sqlite3Randomness(n, p); 300137c728fSdrh sqlite3_result_blob(context, (char*)p, n, sqlite3_free); 30163cf66f0Sdrh } 30263cf66f0Sdrh 30363cf66f0Sdrh /* 3046ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function. The return 30524b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function. 3066ed41ad7Sdrh */ 30751ad0ecdSdanielk1977 static void last_insert_rowid( 3080ae8b831Sdanielk1977 sqlite3_context *context, 30951ad0ecdSdanielk1977 int arg, 31051ad0ecdSdanielk1977 sqlite3_value **argv 31151ad0ecdSdanielk1977 ){ 3129bb575fdSdrh sqlite3 *db = sqlite3_user_data(context); 313f9b596ebSdrh sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); 3146ed41ad7Sdrh } 3156ed41ad7Sdrh 316f146a776Srdc /* 317b28af71aSdanielk1977 ** Implementation of the changes() SQL function. The return value is the 318b28af71aSdanielk1977 ** same as the sqlite3_changes() API function. 319f146a776Srdc */ 320b28af71aSdanielk1977 static void changes( 321f9b596ebSdrh sqlite3_context *context, 322f9b596ebSdrh int arg, 323f9b596ebSdrh sqlite3_value **argv 324f9b596ebSdrh ){ 3259bb575fdSdrh sqlite3 *db = sqlite3_user_data(context); 326f4479501Sdrh sqlite3_result_int(context, sqlite3_changes(db)); 327b0c374ffSrdc } 328f146a776Srdc 329f146a776Srdc /* 330b28af71aSdanielk1977 ** Implementation of the total_changes() SQL function. The return value is 331b28af71aSdanielk1977 ** the same as the sqlite3_total_changes() API function. 332f146a776Srdc */ 333b28af71aSdanielk1977 static void total_changes( 3340ae8b831Sdanielk1977 sqlite3_context *context, 33551ad0ecdSdanielk1977 int arg, 33651ad0ecdSdanielk1977 sqlite3_value **argv 33751ad0ecdSdanielk1977 ){ 3389bb575fdSdrh sqlite3 *db = sqlite3_user_data(context); 339b28af71aSdanielk1977 sqlite3_result_int(context, sqlite3_total_changes(db)); 340b0c374ffSrdc } 341b0c374ffSrdc 3426ed41ad7Sdrh /* 3434e5ffc5fSdrh ** A structure defining how to do GLOB-style comparisons. 344d02eb1fdSdanielk1977 */ 3454e5ffc5fSdrh struct compareInfo { 3464e5ffc5fSdrh u8 matchAll; 3474e5ffc5fSdrh u8 matchOne; 3484e5ffc5fSdrh u8 matchSet; 3494e5ffc5fSdrh u8 noCase; 350d02eb1fdSdanielk1977 }; 35155ef4d97Sdrh 3524e5ffc5fSdrh static const struct compareInfo globInfo = { '*', '?', '[', 0 }; 35370031fa3Sdrh /* The correct SQL-92 behavior is for the LIKE operator to ignore 35470031fa3Sdrh ** case. Thus 'a' LIKE 'A' would be true. */ 35555ef4d97Sdrh static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; 35670031fa3Sdrh /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator 35770031fa3Sdrh ** is case sensitive causing 'a' LIKE 'A' to be false */ 35855ef4d97Sdrh static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; 359d02eb1fdSdanielk1977 360d02eb1fdSdanielk1977 /* 3614e5ffc5fSdrh ** X is a pointer to the first byte of a UTF-8 character. Increment 3624e5ffc5fSdrh ** X so that it points to the next character. This only works right 3634e5ffc5fSdrh ** if X points to a well-formed UTF-8 string. 364d02eb1fdSdanielk1977 */ 3654e5ffc5fSdrh #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} 3664e5ffc5fSdrh #define sqliteCharVal(X) sqlite3ReadUtf8(X) 367d02eb1fdSdanielk1977 368d02eb1fdSdanielk1977 369d02eb1fdSdanielk1977 /* 3704e5ffc5fSdrh ** Compare two UTF-8 strings for equality where the first string can 3714e5ffc5fSdrh ** potentially be a "glob" expression. Return true (1) if they 3724e5ffc5fSdrh ** are the same and false (0) if they are different. 3730ac65892Sdrh ** 3744e5ffc5fSdrh ** Globbing rules: 3750ac65892Sdrh ** 3764e5ffc5fSdrh ** '*' Matches any sequence of zero or more characters. 377d02eb1fdSdanielk1977 ** 3784e5ffc5fSdrh ** '?' Matches exactly one character. 3794e5ffc5fSdrh ** 3804e5ffc5fSdrh ** [...] Matches one character from the enclosed list of 3814e5ffc5fSdrh ** characters. 3824e5ffc5fSdrh ** 3834e5ffc5fSdrh ** [^...] Matches one character not in the enclosed list. 3844e5ffc5fSdrh ** 3854e5ffc5fSdrh ** With the [...] and [^...] matching, a ']' character can be included 3864e5ffc5fSdrh ** in the list by making it the first character after '[' or '^'. A 3874e5ffc5fSdrh ** range of characters can be specified using '-'. Example: 3884e5ffc5fSdrh ** "[a-z]" matches any single lower-case letter. To match a '-', make 3894e5ffc5fSdrh ** it the last character in the list. 3904e5ffc5fSdrh ** 3914e5ffc5fSdrh ** This routine is usually quick, but can be N**2 in the worst case. 3924e5ffc5fSdrh ** 3934e5ffc5fSdrh ** Hints: to match '*' or '?', put them in "[]". Like this: 3944e5ffc5fSdrh ** 3954e5ffc5fSdrh ** abc[*]xyz Matches "abc*xyz" only 3960ac65892Sdrh */ 3977c6303c0Sdanielk1977 static int patternCompare( 3984e5ffc5fSdrh const u8 *zPattern, /* The glob pattern */ 3994e5ffc5fSdrh const u8 *zString, /* The string to compare against the glob */ 4007c6303c0Sdanielk1977 const struct compareInfo *pInfo, /* Information about how to do the compare */ 4017c6303c0Sdanielk1977 const int esc /* The escape character */ 40251ad0ecdSdanielk1977 ){ 403ad7dd425Sdanielk1977 register int c; 4044e5ffc5fSdrh int invert; 4054e5ffc5fSdrh int seen; 4064e5ffc5fSdrh int c2; 4074e5ffc5fSdrh u8 matchOne = pInfo->matchOne; 4084e5ffc5fSdrh u8 matchAll = pInfo->matchAll; 4094e5ffc5fSdrh u8 matchSet = pInfo->matchSet; 4104e5ffc5fSdrh u8 noCase = pInfo->noCase; 4117c6303c0Sdanielk1977 int prevEscape = 0; /* True if the previous character was 'escape' */ 412d02eb1fdSdanielk1977 4134e5ffc5fSdrh while( (c = *zPattern)!=0 ){ 4147c6303c0Sdanielk1977 if( !prevEscape && c==matchAll ){ 4154e5ffc5fSdrh while( (c=zPattern[1]) == matchAll || c == matchOne ){ 4164e5ffc5fSdrh if( c==matchOne ){ 4174e5ffc5fSdrh if( *zString==0 ) return 0; 4184e5ffc5fSdrh sqliteNextChar(zString); 419d02eb1fdSdanielk1977 } 4204e5ffc5fSdrh zPattern++; 4214e5ffc5fSdrh } 42220fc0887Sdrh if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){ 4237c6303c0Sdanielk1977 u8 const *zTemp = &zPattern[1]; 4247c6303c0Sdanielk1977 sqliteNextChar(zTemp); 4257c6303c0Sdanielk1977 c = *zTemp; 4267c6303c0Sdanielk1977 } 4274e5ffc5fSdrh if( c==0 ) return 1; 4284e5ffc5fSdrh if( c==matchSet ){ 4297c6303c0Sdanielk1977 assert( esc==0 ); /* This is GLOB, not LIKE */ 4307c6303c0Sdanielk1977 while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){ 4314e5ffc5fSdrh sqliteNextChar(zString); 4324e5ffc5fSdrh } 4334e5ffc5fSdrh return *zString!=0; 434d02eb1fdSdanielk1977 }else{ 4354e5ffc5fSdrh while( (c2 = *zString)!=0 ){ 4364e5ffc5fSdrh if( noCase ){ 4374e5ffc5fSdrh c2 = sqlite3UpperToLower[c2]; 4384e5ffc5fSdrh c = sqlite3UpperToLower[c]; 4394e5ffc5fSdrh while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; } 440ad7dd425Sdanielk1977 }else{ 4414e5ffc5fSdrh while( c2 != 0 && c2 != c ){ c2 = *++zString; } 442ad7dd425Sdanielk1977 } 4434e5ffc5fSdrh if( c2==0 ) return 0; 4447c6303c0Sdanielk1977 if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1; 4454e5ffc5fSdrh sqliteNextChar(zString); 4464e5ffc5fSdrh } 4474e5ffc5fSdrh return 0; 4484e5ffc5fSdrh } 4497c6303c0Sdanielk1977 }else if( !prevEscape && c==matchOne ){ 4504e5ffc5fSdrh if( *zString==0 ) return 0; 4514e5ffc5fSdrh sqliteNextChar(zString); 4524e5ffc5fSdrh zPattern++; 4534e5ffc5fSdrh }else if( c==matchSet ){ 4544e5ffc5fSdrh int prior_c = 0; 4557c6303c0Sdanielk1977 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ 4564e5ffc5fSdrh seen = 0; 4574e5ffc5fSdrh invert = 0; 4584e5ffc5fSdrh c = sqliteCharVal(zString); 4594e5ffc5fSdrh if( c==0 ) return 0; 4604e5ffc5fSdrh c2 = *++zPattern; 4614e5ffc5fSdrh if( c2=='^' ){ invert = 1; c2 = *++zPattern; } 4624e5ffc5fSdrh if( c2==']' ){ 4634e5ffc5fSdrh if( c==']' ) seen = 1; 4644e5ffc5fSdrh c2 = *++zPattern; 4654e5ffc5fSdrh } 4664e5ffc5fSdrh while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ 4674e5ffc5fSdrh if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ 4684e5ffc5fSdrh zPattern++; 4694e5ffc5fSdrh c2 = sqliteCharVal(zPattern); 4704e5ffc5fSdrh if( c>=prior_c && c<=c2 ) seen = 1; 4714e5ffc5fSdrh prior_c = 0; 4724e5ffc5fSdrh }else if( c==c2 ){ 4734e5ffc5fSdrh seen = 1; 4744e5ffc5fSdrh prior_c = c2; 475d02eb1fdSdanielk1977 }else{ 4764e5ffc5fSdrh prior_c = c2; 477d02eb1fdSdanielk1977 } 4784e5ffc5fSdrh sqliteNextChar(zPattern); 479d02eb1fdSdanielk1977 } 4804e5ffc5fSdrh if( c2==0 || (seen ^ invert)==0 ) return 0; 4814e5ffc5fSdrh sqliteNextChar(zString); 4824e5ffc5fSdrh zPattern++; 48320fc0887Sdrh }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){ 4847c6303c0Sdanielk1977 prevEscape = 1; 4857c6303c0Sdanielk1977 sqliteNextChar(zPattern); 486d02eb1fdSdanielk1977 }else{ 4874e5ffc5fSdrh if( noCase ){ 4884e5ffc5fSdrh if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0; 4894e5ffc5fSdrh }else{ 4904e5ffc5fSdrh if( c != *zString ) return 0; 4914e5ffc5fSdrh } 4924e5ffc5fSdrh zPattern++; 4934e5ffc5fSdrh zString++; 4947c6303c0Sdanielk1977 prevEscape = 0; 49551ad0ecdSdanielk1977 } 4960ac65892Sdrh } 4974e5ffc5fSdrh return *zString==0; 4984e5ffc5fSdrh } 4994e5ffc5fSdrh 50055ef4d97Sdrh /* 50155ef4d97Sdrh ** Count the number of times that the LIKE operator (or GLOB which is 50255ef4d97Sdrh ** just a variation of LIKE) gets called. This is used for testing 50355ef4d97Sdrh ** only. 50455ef4d97Sdrh */ 50555ef4d97Sdrh #ifdef SQLITE_TEST 50655ef4d97Sdrh int sqlite3_like_count = 0; 50755ef4d97Sdrh #endif 50855ef4d97Sdrh 5093f6b0874Sdanielk1977 5103f6b0874Sdanielk1977 /* 5113f6b0874Sdanielk1977 ** Implementation of the like() SQL function. This function implements 5123f6b0874Sdanielk1977 ** the build-in LIKE operator. The first argument to the function is the 5133f6b0874Sdanielk1977 ** pattern and the second argument is the string. So, the SQL statements: 5143f6b0874Sdanielk1977 ** 5153f6b0874Sdanielk1977 ** A LIKE B 5163f6b0874Sdanielk1977 ** 5173f6b0874Sdanielk1977 ** is implemented as like(B,A). 5183f6b0874Sdanielk1977 ** 51955ef4d97Sdrh ** This same function (with a different compareInfo structure) computes 52055ef4d97Sdrh ** the GLOB operator. 5213f6b0874Sdanielk1977 */ 5223f6b0874Sdanielk1977 static void likeFunc( 5233f6b0874Sdanielk1977 sqlite3_context *context, 5243f6b0874Sdanielk1977 int argc, 5253f6b0874Sdanielk1977 sqlite3_value **argv 5263f6b0874Sdanielk1977 ){ 5273f6b0874Sdanielk1977 const unsigned char *zA = sqlite3_value_text(argv[0]); 5283f6b0874Sdanielk1977 const unsigned char *zB = sqlite3_value_text(argv[1]); 5297c6303c0Sdanielk1977 int escape = 0; 5307c6303c0Sdanielk1977 if( argc==3 ){ 5317c6303c0Sdanielk1977 /* The escape character string must consist of a single UTF-8 character. 5327c6303c0Sdanielk1977 ** Otherwise, return an error. 5337c6303c0Sdanielk1977 */ 5347c6303c0Sdanielk1977 const unsigned char *zEsc = sqlite3_value_text(argv[2]); 5357a521cfbSdrh if( zEsc==0 ) return; 5362646da7eSdrh if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){ 5377c6303c0Sdanielk1977 sqlite3_result_error(context, 5387c6303c0Sdanielk1977 "ESCAPE expression must be a single character", -1); 5397c6303c0Sdanielk1977 return; 5407c6303c0Sdanielk1977 } 5417c6303c0Sdanielk1977 escape = sqlite3ReadUtf8(zEsc); 5427c6303c0Sdanielk1977 } 5433f6b0874Sdanielk1977 if( zA && zB ){ 54455ef4d97Sdrh struct compareInfo *pInfo = sqlite3_user_data(context); 54555ef4d97Sdrh #ifdef SQLITE_TEST 54655ef4d97Sdrh sqlite3_like_count++; 54755ef4d97Sdrh #endif 54855ef4d97Sdrh sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape)); 54951ad0ecdSdanielk1977 } 5508912d106Sdrh } 5518912d106Sdrh 5528912d106Sdrh /* 5538912d106Sdrh ** Implementation of the NULLIF(x,y) function. The result is the first 5548912d106Sdrh ** argument if the arguments are different. The result is NULL if the 5558912d106Sdrh ** arguments are equal to each other. 5568912d106Sdrh */ 557f9b596ebSdrh static void nullifFunc( 558f9b596ebSdrh sqlite3_context *context, 559f9b596ebSdrh int argc, 560f9b596ebSdrh sqlite3_value **argv 561f9b596ebSdrh ){ 562dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 563dc1bdc4fSdanielk1977 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ 564f4479501Sdrh sqlite3_result_value(context, argv[0]); 5658912d106Sdrh } 5660ac65892Sdrh } 5670ac65892Sdrh 568647cb0e1Sdrh /* 569647cb0e1Sdrh ** Implementation of the VERSION(*) function. The result is the version 570647cb0e1Sdrh ** of the SQLite library that is running. 571647cb0e1Sdrh */ 572f9b596ebSdrh static void versionFunc( 573f9b596ebSdrh sqlite3_context *context, 574f9b596ebSdrh int argc, 575f9b596ebSdrh sqlite3_value **argv 576f9b596ebSdrh ){ 577d8123366Sdanielk1977 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); 578647cb0e1Sdrh } 579647cb0e1Sdrh 580137c728fSdrh /* Array for converting from half-bytes (nybbles) into ASCII hex 581137c728fSdrh ** digits. */ 582137c728fSdrh static const char hexdigits[] = { 583137c728fSdrh '0', '1', '2', '3', '4', '5', '6', '7', 584137c728fSdrh '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 585137c728fSdrh }; 586d641d646Sdanielk1977 58747394703Sdrh /* 58847394703Sdrh ** EXPERIMENTAL - This is not an official function. The interface may 58947394703Sdrh ** change. This function may disappear. Do not write code that depends 59047394703Sdrh ** on this function. 59147394703Sdrh ** 59247394703Sdrh ** Implementation of the QUOTE() function. This function takes a single 59347394703Sdrh ** argument. If the argument is numeric, the return value is the same as 59447394703Sdrh ** the argument. If the argument is NULL, the return value is the string 59547394703Sdrh ** "NULL". Otherwise, the argument is enclosed in single quotes with 59647394703Sdrh ** single-quote escapes. 59747394703Sdrh */ 5980ae8b831Sdanielk1977 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 59947394703Sdrh if( argc<1 ) return; 600f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 6019c054830Sdrh case SQLITE_NULL: { 602d8123366Sdanielk1977 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 603f9b596ebSdrh break; 604f9b596ebSdrh } 6059c054830Sdrh case SQLITE_INTEGER: 6069c054830Sdrh case SQLITE_FLOAT: { 607f4479501Sdrh sqlite3_result_value(context, argv[0]); 608f9b596ebSdrh break; 609f9b596ebSdrh } 6103f41e976Sdanielk1977 case SQLITE_BLOB: { 6113f41e976Sdanielk1977 char *zText = 0; 6123f41e976Sdanielk1977 int nBlob = sqlite3_value_bytes(argv[0]); 6133f41e976Sdanielk1977 char const *zBlob = sqlite3_value_blob(argv[0]); 6143f41e976Sdanielk1977 6153f41e976Sdanielk1977 zText = (char *)sqliteMalloc((2*nBlob)+4); 6163f41e976Sdanielk1977 if( !zText ){ 6173f41e976Sdanielk1977 sqlite3_result_error(context, "out of memory", -1); 6183f41e976Sdanielk1977 }else{ 6193f41e976Sdanielk1977 int i; 6203f41e976Sdanielk1977 for(i=0; i<nBlob; i++){ 6213f41e976Sdanielk1977 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; 6223f41e976Sdanielk1977 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; 6233f41e976Sdanielk1977 } 6243f41e976Sdanielk1977 zText[(nBlob*2)+2] = '\''; 6253f41e976Sdanielk1977 zText[(nBlob*2)+3] = '\0'; 6263f41e976Sdanielk1977 zText[0] = 'X'; 6273f41e976Sdanielk1977 zText[1] = '\''; 628d8123366Sdanielk1977 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 6293f41e976Sdanielk1977 sqliteFree(zText); 6303f41e976Sdanielk1977 } 6313f41e976Sdanielk1977 break; 6323f41e976Sdanielk1977 } 6339c054830Sdrh case SQLITE_TEXT: { 63447394703Sdrh int i,j,n; 6352646da7eSdrh const unsigned char *zArg = sqlite3_value_text(argv[0]); 63647394703Sdrh char *z; 637f9b596ebSdrh 6387a521cfbSdrh if( zArg==0 ) return; 63951ad0ecdSdanielk1977 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } 64047394703Sdrh z = sqliteMalloc( i+n+3 ); 64147394703Sdrh if( z==0 ) return; 64247394703Sdrh z[0] = '\''; 64351ad0ecdSdanielk1977 for(i=0, j=1; zArg[i]; i++){ 64451ad0ecdSdanielk1977 z[j++] = zArg[i]; 64551ad0ecdSdanielk1977 if( zArg[i]=='\'' ){ 64647394703Sdrh z[j++] = '\''; 64747394703Sdrh } 64847394703Sdrh } 64947394703Sdrh z[j++] = '\''; 65047394703Sdrh z[j] = 0; 651d8123366Sdanielk1977 sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); 65247394703Sdrh sqliteFree(z); 65347394703Sdrh } 65447394703Sdrh } 655f9b596ebSdrh } 65647394703Sdrh 657137c728fSdrh /* 658137c728fSdrh ** The hex() function. Interpret the argument as a blob. Return 659137c728fSdrh ** a hexadecimal rendering as text. 660137c728fSdrh */ 661137c728fSdrh static void hexFunc( 662137c728fSdrh sqlite3_context *context, 663137c728fSdrh int argc, 664137c728fSdrh sqlite3_value **argv 665137c728fSdrh ){ 666137c728fSdrh int i, n; 667137c728fSdrh const unsigned char *pBlob; 668137c728fSdrh char *zHex, *z; 669137c728fSdrh assert( argc==1 ); 670137c728fSdrh n = sqlite3_value_bytes(argv[0]); 6711eb2538aSdrh pBlob = sqlite3_value_blob(argv[0]); 672137c728fSdrh z = zHex = sqlite3_malloc(n*2 + 1); 673137c728fSdrh if( zHex==0 ) return; 674137c728fSdrh for(i=0; i<n; i++, pBlob++){ 675137c728fSdrh unsigned char c = *pBlob; 676137c728fSdrh *(z++) = hexdigits[(c>>4)&0xf]; 677137c728fSdrh *(z++) = hexdigits[c&0xf]; 678137c728fSdrh } 679137c728fSdrh *z = 0; 680137c728fSdrh sqlite3_result_text(context, zHex, n*2, sqlite3_free); 681137c728fSdrh } 682137c728fSdrh 68326b6d90dSdrh /* 68426b6d90dSdrh ** The replace() function. Three arguments are all strings: call 68526b6d90dSdrh ** them A, B, and C. The result is also a string which is derived 68626b6d90dSdrh ** from A by replacing every occurance of B with C. The match 68726b6d90dSdrh ** must be exact. Collating sequences are not used. 68826b6d90dSdrh */ 68926b6d90dSdrh static void replaceFunc( 69026b6d90dSdrh sqlite3_context *context, 69126b6d90dSdrh int argc, 69226b6d90dSdrh sqlite3_value **argv 69326b6d90dSdrh ){ 69426b6d90dSdrh const unsigned char *zStr; /* The input string A */ 69526b6d90dSdrh const unsigned char *zPattern; /* The pattern string B */ 69626b6d90dSdrh const unsigned char *zRep; /* The replacement string C */ 69726b6d90dSdrh unsigned char *zOut; /* The output */ 69826b6d90dSdrh int nStr; /* Size of zStr */ 69926b6d90dSdrh int nPattern; /* Size of zPattern */ 70026b6d90dSdrh int nRep; /* Size of zRep */ 70126b6d90dSdrh int nOut; /* Maximum size of zOut */ 70226b6d90dSdrh int loopLimit; /* Last zStr[] that might match zPattern[] */ 70326b6d90dSdrh int i, j; /* Loop counters */ 70426b6d90dSdrh 70526b6d90dSdrh assert( argc==3 ); 70626b6d90dSdrh zStr = sqlite3_value_text(argv[0]); 7077a521cfbSdrh if( zStr==0 ) return; 708309b3386Sdrh nStr = sqlite3_value_bytes(argv[0]); 70926b6d90dSdrh zPattern = sqlite3_value_text(argv[1]); 710*709cff33Sdrh if( zPattern==0 || zPattern[0]==0 ) return; 711309b3386Sdrh nPattern = sqlite3_value_bytes(argv[1]); 71226b6d90dSdrh zRep = sqlite3_value_text(argv[2]); 7137a521cfbSdrh if( zRep==0 ) return; 714309b3386Sdrh nRep = sqlite3_value_bytes(argv[2]); 71526b6d90dSdrh if( nPattern>=nRep ){ 71626b6d90dSdrh nOut = nStr; 71726b6d90dSdrh }else{ 71826b6d90dSdrh nOut = (nStr/nPattern + 1)*nRep; 71926b6d90dSdrh } 72026b6d90dSdrh zOut = sqlite3_malloc(nOut+1); 72126b6d90dSdrh if( zOut==0 ) return; 72226b6d90dSdrh loopLimit = nStr - nPattern; 72326b6d90dSdrh for(i=j=0; i<=loopLimit; i++){ 72426b6d90dSdrh if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){ 72526b6d90dSdrh zOut[j++] = zStr[i]; 72626b6d90dSdrh }else{ 72726b6d90dSdrh memcpy(&zOut[j], zRep, nRep); 72826b6d90dSdrh j += nRep; 72926b6d90dSdrh i += nPattern-1; 73026b6d90dSdrh } 73126b6d90dSdrh } 73226b6d90dSdrh memcpy(&zOut[j], &zStr[i], nStr-i); 73326b6d90dSdrh j += nStr - i; 73426b6d90dSdrh assert( j<=nOut ); 73526b6d90dSdrh zOut[j] = 0; 73626b6d90dSdrh sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); 73726b6d90dSdrh } 73826b6d90dSdrh 739309b3386Sdrh /* 740309b3386Sdrh ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. 741309b3386Sdrh ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. 742309b3386Sdrh */ 743309b3386Sdrh static void trimFunc( 744309b3386Sdrh sqlite3_context *context, 745309b3386Sdrh int argc, 746309b3386Sdrh sqlite3_value **argv 747309b3386Sdrh ){ 748309b3386Sdrh const unsigned char *zIn; /* Input string */ 749309b3386Sdrh const unsigned char *zCharSet; /* Set of characters to trim */ 750309b3386Sdrh int nIn; /* Number of bytes in input */ 751309b3386Sdrh int flags; 752309b3386Sdrh int i; 753309b3386Sdrh unsigned char cFirst, cNext; 754309b3386Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ 755309b3386Sdrh return; 756309b3386Sdrh } 757309b3386Sdrh zIn = sqlite3_value_text(argv[0]); 7587a521cfbSdrh if( zIn==0 ) return; 759309b3386Sdrh nIn = sqlite3_value_bytes(argv[0]); 760309b3386Sdrh if( argc==1 ){ 761309b3386Sdrh static const unsigned char zSpace[] = " "; 762309b3386Sdrh zCharSet = zSpace; 7637a521cfbSdrh }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ 764309b3386Sdrh return; 765309b3386Sdrh } 766309b3386Sdrh cFirst = zCharSet[0]; 767309b3386Sdrh if( cFirst ){ 768309b3386Sdrh flags = (int)sqlite3_user_data(context); 769309b3386Sdrh if( flags & 1 ){ 770309b3386Sdrh for(; nIn>0; nIn--, zIn++){ 771309b3386Sdrh if( cFirst==zIn[0] ) continue; 772309b3386Sdrh for(i=1; zCharSet[i] && zCharSet[i]!=zIn[0]; i++){} 773309b3386Sdrh if( zCharSet[i]==0 ) break; 774309b3386Sdrh } 775309b3386Sdrh } 776309b3386Sdrh if( flags & 2 ){ 777309b3386Sdrh for(; nIn>0; nIn--){ 778309b3386Sdrh cNext = zIn[nIn-1]; 779309b3386Sdrh if( cFirst==cNext ) continue; 780309b3386Sdrh for(i=1; zCharSet[i] && zCharSet[i]!=cNext; i++){} 781309b3386Sdrh if( zCharSet[i]==0 ) break; 782309b3386Sdrh } 783309b3386Sdrh } 784309b3386Sdrh } 785309b3386Sdrh sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); 786309b3386Sdrh } 78726b6d90dSdrh 788d24cc427Sdrh #ifdef SQLITE_SOUNDEX 789d24cc427Sdrh /* 790d24cc427Sdrh ** Compute the soundex encoding of a word. 791d24cc427Sdrh */ 792137c728fSdrh static void soundexFunc( 793137c728fSdrh sqlite3_context *context, 794137c728fSdrh int argc, 795137c728fSdrh sqlite3_value **argv 796137c728fSdrh ){ 797d24cc427Sdrh char zResult[8]; 7984c755c0fSdrh const u8 *zIn; 799d24cc427Sdrh int i, j; 800d24cc427Sdrh static const unsigned char iCode[] = { 801d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 802d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 803d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 804d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 805d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 806d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 807d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 808d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 809d24cc427Sdrh }; 810d24cc427Sdrh assert( argc==1 ); 8114c755c0fSdrh zIn = (u8*)sqlite3_value_text(argv[0]); 812bdf67e0eSdrh if( zIn==0 ) zIn = (u8*)""; 813d24cc427Sdrh for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} 814d24cc427Sdrh if( zIn[i] ){ 815bdf67e0eSdrh u8 prevcode = iCode[zIn[i]&0x7f]; 816d24cc427Sdrh zResult[0] = toupper(zIn[i]); 817d24cc427Sdrh for(j=1; j<4 && zIn[i]; i++){ 818d24cc427Sdrh int code = iCode[zIn[i]&0x7f]; 819d24cc427Sdrh if( code>0 ){ 820bdf67e0eSdrh if( code!=prevcode ){ 821bdf67e0eSdrh prevcode = code; 822d24cc427Sdrh zResult[j++] = code + '0'; 823d24cc427Sdrh } 824bdf67e0eSdrh }else{ 825bdf67e0eSdrh prevcode = 0; 826bdf67e0eSdrh } 827d24cc427Sdrh } 828d24cc427Sdrh while( j<4 ){ 829d24cc427Sdrh zResult[j++] = '0'; 830d24cc427Sdrh } 831d24cc427Sdrh zResult[j] = 0; 832d8123366Sdanielk1977 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); 833d24cc427Sdrh }else{ 834d8123366Sdanielk1977 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); 835d24cc427Sdrh } 836d24cc427Sdrh } 837d24cc427Sdrh #endif 838d24cc427Sdrh 839fdb83b2fSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 840fdb83b2fSdrh /* 841fdb83b2fSdrh ** A function that loads a shared-library extension then returns NULL. 842fdb83b2fSdrh */ 843fdb83b2fSdrh static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ 84465fd59f7Sdanielk1977 const char *zFile = (const char *)sqlite3_value_text(argv[0]); 8457a521cfbSdrh const char *zProc; 846fdb83b2fSdrh sqlite3 *db = sqlite3_user_data(context); 847fdb83b2fSdrh char *zErrMsg = 0; 848fdb83b2fSdrh 849fdb83b2fSdrh if( argc==2 ){ 85065fd59f7Sdanielk1977 zProc = (const char *)sqlite3_value_text(argv[1]); 8517a521cfbSdrh }else{ 8527a521cfbSdrh zProc = 0; 853fdb83b2fSdrh } 8547a521cfbSdrh if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ 855fdb83b2fSdrh sqlite3_result_error(context, zErrMsg, -1); 856fdb83b2fSdrh sqlite3_free(zErrMsg); 857fdb83b2fSdrh } 858fdb83b2fSdrh } 859fdb83b2fSdrh #endif 860fdb83b2fSdrh 861193a6b41Sdrh #ifdef SQLITE_TEST 862193a6b41Sdrh /* 863193a6b41Sdrh ** This function generates a string of random characters. Used for 864193a6b41Sdrh ** generating test data. 865193a6b41Sdrh */ 8660ae8b831Sdanielk1977 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ 867bbd82df6Sdrh static const unsigned char zSrc[] = 868193a6b41Sdrh "abcdefghijklmnopqrstuvwxyz" 869193a6b41Sdrh "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 870193a6b41Sdrh "0123456789" 871193a6b41Sdrh ".-!,:*^+=_|?/<> "; 872193a6b41Sdrh int iMin, iMax, n, r, i; 873bbd82df6Sdrh unsigned char zBuf[1000]; 874193a6b41Sdrh if( argc>=1 ){ 875f9b596ebSdrh iMin = sqlite3_value_int(argv[0]); 876193a6b41Sdrh if( iMin<0 ) iMin = 0; 877193a6b41Sdrh if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; 878193a6b41Sdrh }else{ 879193a6b41Sdrh iMin = 1; 880193a6b41Sdrh } 881193a6b41Sdrh if( argc>=2 ){ 882f9b596ebSdrh iMax = sqlite3_value_int(argv[1]); 883193a6b41Sdrh if( iMax<iMin ) iMax = iMin; 8841dba7279Sdrh if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; 885193a6b41Sdrh }else{ 886193a6b41Sdrh iMax = 50; 887193a6b41Sdrh } 888193a6b41Sdrh n = iMin; 889193a6b41Sdrh if( iMax>iMin ){ 8904adee20fSdanielk1977 sqlite3Randomness(sizeof(r), &r); 891bbd82df6Sdrh r &= 0x7fffffff; 892193a6b41Sdrh n += r%(iMax + 1 - iMin); 893193a6b41Sdrh } 8941dba7279Sdrh assert( n<sizeof(zBuf) ); 8954adee20fSdanielk1977 sqlite3Randomness(n, zBuf); 896193a6b41Sdrh for(i=0; i<n; i++){ 897bbd82df6Sdrh zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; 898193a6b41Sdrh } 899193a6b41Sdrh zBuf[n] = 0; 9002646da7eSdrh sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); 901d8123366Sdanielk1977 } 9020e3d7476Sdrh #endif /* SQLITE_TEST */ 903d8123366Sdanielk1977 9040e3d7476Sdrh #ifdef SQLITE_TEST 905d8123366Sdanielk1977 /* 906d8123366Sdanielk1977 ** The following two SQL functions are used to test returning a text 907d8123366Sdanielk1977 ** result with a destructor. Function 'test_destructor' takes one argument 908d8123366Sdanielk1977 ** and returns the same argument interpreted as TEXT. A destructor is 909d8123366Sdanielk1977 ** passed with the sqlite3_result_text() call. 910d8123366Sdanielk1977 ** 911d8123366Sdanielk1977 ** SQL function 'test_destructor_count' returns the number of outstanding 912d8123366Sdanielk1977 ** allocations made by 'test_destructor'; 913d8123366Sdanielk1977 ** 914d8123366Sdanielk1977 ** WARNING: Not threadsafe. 915d8123366Sdanielk1977 */ 916d8123366Sdanielk1977 static int test_destructor_count_var = 0; 917d8123366Sdanielk1977 static void destructor(void *p){ 918d8123366Sdanielk1977 char *zVal = (char *)p; 919d8123366Sdanielk1977 assert(zVal); 920d8123366Sdanielk1977 zVal--; 921d8123366Sdanielk1977 sqliteFree(zVal); 922d8123366Sdanielk1977 test_destructor_count_var--; 923d8123366Sdanielk1977 } 924d8123366Sdanielk1977 static void test_destructor( 925d8123366Sdanielk1977 sqlite3_context *pCtx, 926d8123366Sdanielk1977 int nArg, 927d8123366Sdanielk1977 sqlite3_value **argv 928d8123366Sdanielk1977 ){ 929d8123366Sdanielk1977 char *zVal; 930f4618891Sdanielk1977 int len; 9319bb575fdSdrh sqlite3 *db = sqlite3_user_data(pCtx); 932f4618891Sdanielk1977 933d8123366Sdanielk1977 test_destructor_count_var++; 934d8123366Sdanielk1977 assert( nArg==1 ); 935d8123366Sdanielk1977 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 93614db2665Sdanielk1977 len = sqlite3ValueBytes(argv[0], ENC(db)); 937f4618891Sdanielk1977 zVal = sqliteMalloc(len+3); 938f4618891Sdanielk1977 zVal[len] = 0; 939f4618891Sdanielk1977 zVal[len-1] = 0; 940d8123366Sdanielk1977 assert( zVal ); 941d8123366Sdanielk1977 zVal++; 94214db2665Sdanielk1977 memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len); 94314db2665Sdanielk1977 if( ENC(db)==SQLITE_UTF8 ){ 944d8123366Sdanielk1977 sqlite3_result_text(pCtx, zVal, -1, destructor); 9456c62608fSdrh #ifndef SQLITE_OMIT_UTF16 94614db2665Sdanielk1977 }else if( ENC(db)==SQLITE_UTF16LE ){ 947f4618891Sdanielk1977 sqlite3_result_text16le(pCtx, zVal, -1, destructor); 948f4618891Sdanielk1977 }else{ 949f4618891Sdanielk1977 sqlite3_result_text16be(pCtx, zVal, -1, destructor); 9506c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 951f4618891Sdanielk1977 } 952d8123366Sdanielk1977 } 953d8123366Sdanielk1977 static void test_destructor_count( 954d8123366Sdanielk1977 sqlite3_context *pCtx, 955d8123366Sdanielk1977 int nArg, 956d8123366Sdanielk1977 sqlite3_value **argv 957d8123366Sdanielk1977 ){ 958d8123366Sdanielk1977 sqlite3_result_int(pCtx, test_destructor_count_var); 959193a6b41Sdrh } 9600e3d7476Sdrh #endif /* SQLITE_TEST */ 9613f6b0874Sdanielk1977 9620e3d7476Sdrh #ifdef SQLITE_TEST 9630e3d7476Sdrh /* 9640e3d7476Sdrh ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() 9650e3d7476Sdrh ** interface. 9660e3d7476Sdrh ** 9670e3d7476Sdrh ** The test_auxdata() SQL function attempts to register each of its arguments 9680e3d7476Sdrh ** as auxiliary data. If there are no prior registrations of aux data for 9690e3d7476Sdrh ** that argument (meaning the argument is not a constant or this is its first 9700e3d7476Sdrh ** call) then the result for that argument is 0. If there is a prior 9710e3d7476Sdrh ** registration, the result for that argument is 1. The overall result 9720e3d7476Sdrh ** is the individual argument results separated by spaces. 9730e3d7476Sdrh */ 9743f6b0874Sdanielk1977 static void free_test_auxdata(void *p) {sqliteFree(p);} 9753f6b0874Sdanielk1977 static void test_auxdata( 9763f6b0874Sdanielk1977 sqlite3_context *pCtx, 9773f6b0874Sdanielk1977 int nArg, 9783f6b0874Sdanielk1977 sqlite3_value **argv 9793f6b0874Sdanielk1977 ){ 9803f6b0874Sdanielk1977 int i; 9813f6b0874Sdanielk1977 char *zRet = sqliteMalloc(nArg*2); 9823f6b0874Sdanielk1977 if( !zRet ) return; 9833f6b0874Sdanielk1977 for(i=0; i<nArg; i++){ 9842646da7eSdrh char const *z = (char*)sqlite3_value_text(argv[i]); 9853f6b0874Sdanielk1977 if( z ){ 9863f6b0874Sdanielk1977 char *zAux = sqlite3_get_auxdata(pCtx, i); 9873f6b0874Sdanielk1977 if( zAux ){ 9883f6b0874Sdanielk1977 zRet[i*2] = '1'; 9893f6b0874Sdanielk1977 if( strcmp(zAux, z) ){ 9903f6b0874Sdanielk1977 sqlite3_result_error(pCtx, "Auxilary data corruption", -1); 9913f6b0874Sdanielk1977 return; 9923f6b0874Sdanielk1977 } 9933f6b0874Sdanielk1977 }else{ 9943f6b0874Sdanielk1977 zRet[i*2] = '0'; 9953f6b0874Sdanielk1977 zAux = sqliteStrDup(z); 9963f6b0874Sdanielk1977 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); 9973f6b0874Sdanielk1977 } 9983f6b0874Sdanielk1977 zRet[i*2+1] = ' '; 9993f6b0874Sdanielk1977 } 10003f6b0874Sdanielk1977 } 10013f6b0874Sdanielk1977 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); 10023f6b0874Sdanielk1977 } 10030e3d7476Sdrh #endif /* SQLITE_TEST */ 1004193a6b41Sdrh 100501427a62Sdanielk1977 #ifdef SQLITE_TEST 100601427a62Sdanielk1977 /* 100701427a62Sdanielk1977 ** A function to test error reporting from user functions. This function 100801427a62Sdanielk1977 ** returns a copy of it's first argument as an error. 100901427a62Sdanielk1977 */ 101001427a62Sdanielk1977 static void test_error( 101101427a62Sdanielk1977 sqlite3_context *pCtx, 101201427a62Sdanielk1977 int nArg, 101301427a62Sdanielk1977 sqlite3_value **argv 101401427a62Sdanielk1977 ){ 10152646da7eSdrh sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0); 101601427a62Sdanielk1977 } 101701427a62Sdanielk1977 #endif /* SQLITE_TEST */ 101801427a62Sdanielk1977 10190ac65892Sdrh /* 1020d3a149efSdrh ** An instance of the following structure holds the context of a 1021dd5baa95Sdrh ** sum() or avg() aggregate computation. 1022dd5baa95Sdrh */ 1023dd5baa95Sdrh typedef struct SumCtx SumCtx; 1024dd5baa95Sdrh struct SumCtx { 10258c08e861Sdrh double rSum; /* Floating point sum */ 10268c08e861Sdrh i64 iSum; /* Integer sum */ 1027cf85a51cSdrh i64 cnt; /* Number of elements summed */ 10288c08e861Sdrh u8 overflow; /* True if integer overflow seen */ 10298c08e861Sdrh u8 approx; /* True if non-integer value was input to the sum */ 1030dd5baa95Sdrh }; 1031dd5baa95Sdrh 1032dd5baa95Sdrh /* 1033a97fdd3bSdrh ** Routines used to compute the sum, average, and total. 1034a97fdd3bSdrh ** 1035a97fdd3bSdrh ** The SUM() function follows the (broken) SQL standard which means 1036a97fdd3bSdrh ** that it returns NULL if it sums over no inputs. TOTAL returns 1037a97fdd3bSdrh ** 0.0 in that case. In addition, TOTAL always returns a float where 1038a97fdd3bSdrh ** SUM might return an integer if it never encounters a floating point 1039c806d857Sdrh ** value. TOTAL never fails, but SUM might through an exception if 1040c806d857Sdrh ** it overflows an integer. 1041dd5baa95Sdrh */ 10420ae8b831Sdanielk1977 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 1043dd5baa95Sdrh SumCtx *p; 10443d1d95e6Sdrh int type; 10453f219f46Sdrh assert( argc==1 ); 10464f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 104729d72108Sdrh type = sqlite3_value_numeric_type(argv[0]); 10483d1d95e6Sdrh if( p && type!=SQLITE_NULL ){ 1049739105c7Sdrh p->cnt++; 105029d72108Sdrh if( type==SQLITE_INTEGER ){ 10518c08e861Sdrh i64 v = sqlite3_value_int64(argv[0]); 10528c08e861Sdrh p->rSum += v; 10538c08e861Sdrh if( (p->approx|p->overflow)==0 ){ 10548c08e861Sdrh i64 iNewSum = p->iSum + v; 10558c08e861Sdrh int s1 = p->iSum >> (sizeof(i64)*8-1); 10568c08e861Sdrh int s2 = v >> (sizeof(i64)*8-1); 10578c08e861Sdrh int s3 = iNewSum >> (sizeof(i64)*8-1); 10588c08e861Sdrh p->overflow = (s1&s2&~s3) | (~s1&~s2&s3); 10598c08e861Sdrh p->iSum = iNewSum; 106029d72108Sdrh } 106129d72108Sdrh }else{ 10628c08e861Sdrh p->rSum += sqlite3_value_double(argv[0]); 106329d72108Sdrh p->approx = 1; 10643f219f46Sdrh } 1065739105c7Sdrh } 1066dd5baa95Sdrh } 10670ae8b831Sdanielk1977 static void sumFinalize(sqlite3_context *context){ 1068dd5baa95Sdrh SumCtx *p; 1069abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1070c2bd913aSdrh if( p && p->cnt>0 ){ 10718c08e861Sdrh if( p->overflow ){ 10728c08e861Sdrh sqlite3_result_error(context,"integer overflow",-1); 10738c08e861Sdrh }else if( p->approx ){ 10748c08e861Sdrh sqlite3_result_double(context, p->rSum); 1075c2bd913aSdrh }else{ 10768c08e861Sdrh sqlite3_result_int64(context, p->iSum); 10773d1d95e6Sdrh } 1078dd5baa95Sdrh } 1079c2bd913aSdrh } 10800ae8b831Sdanielk1977 static void avgFinalize(sqlite3_context *context){ 1081dd5baa95Sdrh SumCtx *p; 1082abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1083739105c7Sdrh if( p && p->cnt>0 ){ 10848c08e861Sdrh sqlite3_result_double(context, p->rSum/(double)p->cnt); 1085dd5baa95Sdrh } 1086dd5baa95Sdrh } 1087a97fdd3bSdrh static void totalFinalize(sqlite3_context *context){ 1088a97fdd3bSdrh SumCtx *p; 1089a97fdd3bSdrh p = sqlite3_aggregate_context(context, 0); 10908c08e861Sdrh sqlite3_result_double(context, p ? p->rSum : 0.0); 1091a97fdd3bSdrh } 1092dd5baa95Sdrh 1093dd5baa95Sdrh /* 10940bce8354Sdrh ** The following structure keeps track of state information for the 10950bce8354Sdrh ** count() aggregate function. 10960bce8354Sdrh */ 10970bce8354Sdrh typedef struct CountCtx CountCtx; 10980bce8354Sdrh struct CountCtx { 1099fc6ad39cSdrh i64 n; 11000bce8354Sdrh }; 1101dd5baa95Sdrh 11020bce8354Sdrh /* 11030bce8354Sdrh ** Routines to implement the count() aggregate function. 11040bce8354Sdrh */ 11050ae8b831Sdanielk1977 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 11060bce8354Sdrh CountCtx *p; 11074f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 11089c054830Sdrh if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ 11090bce8354Sdrh p->n++; 11100bce8354Sdrh } 11110bce8354Sdrh } 11120ae8b831Sdanielk1977 static void countFinalize(sqlite3_context *context){ 11130bce8354Sdrh CountCtx *p; 1114abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 1115fc6ad39cSdrh sqlite3_result_int64(context, p ? p->n : 0); 11160bce8354Sdrh } 11170bce8354Sdrh 11180bce8354Sdrh /* 11190bce8354Sdrh ** Routines to implement min() and max() aggregate functions. 11200bce8354Sdrh */ 11210ae8b831Sdanielk1977 static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 112288208050Sdanielk1977 Mem *pArg = (Mem *)argv[0]; 11239eb516c0Sdrh Mem *pBest; 11249eb516c0Sdrh 11259eb516c0Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 11269eb516c0Sdrh pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); 11273aeab9e4Sdanielk1977 if( !pBest ) return; 1128268380caSdrh 112988208050Sdanielk1977 if( pBest->flags ){ 11309eb516c0Sdrh int max; 11319eb516c0Sdrh int cmp; 1132dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 11337e18c259Sdanielk1977 /* This step function is used for both the min() and max() aggregates, 11347e18c259Sdanielk1977 ** the only difference between the two being that the sense of the 11357e18c259Sdanielk1977 ** comparison is inverted. For the max() aggregate, the 11367e18c259Sdanielk1977 ** sqlite3_user_data() function returns (void *)-1. For min() it 11377e18c259Sdanielk1977 ** returns (void *)db, where db is the sqlite3* database pointer. 11387e18c259Sdanielk1977 ** Therefore the next statement sets variable 'max' to 1 for the max() 11397e18c259Sdanielk1977 ** aggregate, or 0 for min(). 11407e18c259Sdanielk1977 */ 1141309b3386Sdrh max = sqlite3_user_data(context)!=0; 1142dc1bdc4fSdanielk1977 cmp = sqlite3MemCompare(pBest, pArg, pColl); 114388208050Sdanielk1977 if( (max && cmp<0) || (!max && cmp>0) ){ 11447e18c259Sdanielk1977 sqlite3VdbeMemCopy(pBest, pArg); 114588208050Sdanielk1977 } 11460bce8354Sdrh }else{ 11477e18c259Sdanielk1977 sqlite3VdbeMemCopy(pBest, pArg); 11480bce8354Sdrh } 11490bce8354Sdrh } 11500ae8b831Sdanielk1977 static void minMaxFinalize(sqlite3_context *context){ 115188208050Sdanielk1977 sqlite3_value *pRes; 1152abfcea25Sdrh pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); 1153abfcea25Sdrh if( pRes ){ 115488208050Sdanielk1977 if( pRes->flags ){ 1155f4479501Sdrh sqlite3_result_value(context, pRes); 11560bce8354Sdrh } 1157b20e56b4Sdanielk1977 sqlite3VdbeMemRelease(pRes); 11580bce8354Sdrh } 1159abfcea25Sdrh } 1160dd5baa95Sdrh 11614e5ffc5fSdrh 1162d3a149efSdrh /* 1163a2ed5601Sdrh ** This function registered all of the above C functions as SQL 1164a2ed5601Sdrh ** functions. This should be the only routine in this file with 1165a2ed5601Sdrh ** external linkage. 1166dc04c583Sdrh */ 11679bb575fdSdrh void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ 11685719628aSdrh static const struct { 11690bce8354Sdrh char *zName; 1170268380caSdrh signed char nArg; 1171309b3386Sdrh u8 argType; /* ff: db 1: 0, 2: 1, 3: 2,... N: N-1. */ 1172d02eb1fdSdanielk1977 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */ 1173dc1bdc4fSdanielk1977 u8 needCollSeq; 11740ae8b831Sdanielk1977 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); 11750bce8354Sdrh } aFuncs[] = { 1176d8123366Sdanielk1977 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc }, 1177d8123366Sdanielk1977 { "min", 0, 0, SQLITE_UTF8, 1, 0 }, 1178309b3386Sdrh { "max", -1, 1, SQLITE_UTF8, 1, minmaxFunc }, 1179309b3386Sdrh { "max", 0, 1, SQLITE_UTF8, 1, 0 }, 1180d8123366Sdanielk1977 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc }, 1181d8123366Sdanielk1977 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc }, 1182d8123366Sdanielk1977 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc }, 11836c62608fSdrh #ifndef SQLITE_OMIT_UTF16 1184f4618891Sdanielk1977 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr }, 11856c62608fSdrh #endif 1186d8123366Sdanielk1977 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc }, 1187d8123366Sdanielk1977 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc }, 1188d8123366Sdanielk1977 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc }, 1189d8123366Sdanielk1977 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc }, 1190d8123366Sdanielk1977 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc }, 1191d8123366Sdanielk1977 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc }, 1192d8123366Sdanielk1977 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 }, 1193d8123366Sdanielk1977 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 }, 1194137c728fSdrh { "hex", 1, 0, SQLITE_UTF8, 0, hexFunc }, 1195d8123366Sdanielk1977 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc }, 1196d8123366Sdanielk1977 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc }, 1197137c728fSdrh { "randomblob", 1, 0, SQLITE_UTF8, 0, randomBlob }, 119894a98365Sdrh { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc }, 1199d8123366Sdanielk1977 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc}, 1200d8123366Sdanielk1977 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc }, 1201309b3386Sdrh { "last_insert_rowid", 0, 0xff, SQLITE_UTF8, 0, last_insert_rowid }, 1202309b3386Sdrh { "changes", 0, 0xff, SQLITE_UTF8, 0, changes }, 1203309b3386Sdrh { "total_changes", 0, 0xff, SQLITE_UTF8, 0, total_changes }, 120426b6d90dSdrh { "replace", 3, 0, SQLITE_UTF8, 0, replaceFunc }, 1205309b3386Sdrh { "ltrim", 1, 1, SQLITE_UTF8, 0, trimFunc }, 1206309b3386Sdrh { "ltrim", 2, 1, SQLITE_UTF8, 0, trimFunc }, 1207309b3386Sdrh { "rtrim", 1, 2, SQLITE_UTF8, 0, trimFunc }, 1208309b3386Sdrh { "rtrim", 2, 2, SQLITE_UTF8, 0, trimFunc }, 1209309b3386Sdrh { "trim", 1, 3, SQLITE_UTF8, 0, trimFunc }, 1210309b3386Sdrh { "trim", 2, 3, SQLITE_UTF8, 0, trimFunc }, 1211d24cc427Sdrh #ifdef SQLITE_SOUNDEX 1212d8123366Sdanielk1977 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc}, 1213d24cc427Sdrh #endif 1214fdb83b2fSdrh #ifndef SQLITE_OMIT_LOAD_EXTENSION 1215309b3386Sdrh { "load_extension", 1, 0xff, SQLITE_UTF8, 0, loadExt }, 1216309b3386Sdrh { "load_extension", 2, 0xff, SQLITE_UTF8, 0, loadExt }, 1217fdb83b2fSdrh #endif 1218193a6b41Sdrh #ifdef SQLITE_TEST 1219d8123366Sdanielk1977 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr }, 1220309b3386Sdrh { "test_destructor", 1, 0xff, SQLITE_UTF8, 0, test_destructor}, 1221d8123366Sdanielk1977 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count}, 12223f6b0874Sdanielk1977 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata}, 122301427a62Sdanielk1977 { "test_error", 1, 0, SQLITE_UTF8, 0, test_error}, 1224193a6b41Sdrh #endif 12250bce8354Sdrh }; 12265719628aSdrh static const struct { 12270bce8354Sdrh char *zName; 1228268380caSdrh signed char nArg; 1229268380caSdrh u8 argType; 1230dc1bdc4fSdanielk1977 u8 needCollSeq; 12310ae8b831Sdanielk1977 void (*xStep)(sqlite3_context*,int,sqlite3_value**); 12320ae8b831Sdanielk1977 void (*xFinalize)(sqlite3_context*); 12330bce8354Sdrh } aAggs[] = { 1234dc1bdc4fSdanielk1977 { "min", 1, 0, 1, minmaxStep, minMaxFinalize }, 1235309b3386Sdrh { "max", 1, 1, 1, minmaxStep, minMaxFinalize }, 1236dc1bdc4fSdanielk1977 { "sum", 1, 0, 0, sumStep, sumFinalize }, 1237a97fdd3bSdrh { "total", 1, 0, 0, sumStep, totalFinalize }, 1238dc1bdc4fSdanielk1977 { "avg", 1, 0, 0, sumStep, avgFinalize }, 1239dc1bdc4fSdanielk1977 { "count", 0, 0, 0, countStep, countFinalize }, 1240dc1bdc4fSdanielk1977 { "count", 1, 0, 0, countStep, countFinalize }, 12410bce8354Sdrh }; 12420bce8354Sdrh int i; 12430bce8354Sdrh 12440bce8354Sdrh for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 1245309b3386Sdrh void *pArg; 1246309b3386Sdrh u8 argType = aFuncs[i].argType; 1247309b3386Sdrh if( argType==0xff ){ 1248309b3386Sdrh pArg = db; 1249309b3386Sdrh }else{ 1250309b3386Sdrh pArg = (void*)(int)argType; 1251c572ef7fSdanielk1977 } 1252771151b6Sdanielk1977 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg, 1253f9d64d2cSdanielk1977 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0); 1254dc1bdc4fSdanielk1977 if( aFuncs[i].needCollSeq ){ 1255dc1bdc4fSdanielk1977 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, 1256dc1bdc4fSdanielk1977 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0); 1257dc1bdc4fSdanielk1977 if( pFunc && aFuncs[i].needCollSeq ){ 1258dc1bdc4fSdanielk1977 pFunc->needCollSeq = 1; 1259dc1bdc4fSdanielk1977 } 1260dc1bdc4fSdanielk1977 } 12610bce8354Sdrh } 12621f01ec1bSdrh #ifndef SQLITE_OMIT_ALTERTABLE 12631f01ec1bSdrh sqlite3AlterFunctions(db); 12641f01ec1bSdrh #endif 1265198bf391Sdrh #ifndef SQLITE_OMIT_PARSER 1266f744bb56Sdanielk1977 sqlite3AttachFunctions(db); 1267198bf391Sdrh #endif 12680bce8354Sdrh for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ 1269309b3386Sdrh void *pArg = (void*)(int)aAggs[i].argType; 1270771151b6Sdanielk1977 sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, 1271f9d64d2cSdanielk1977 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize); 1272dc1bdc4fSdanielk1977 if( aAggs[i].needCollSeq ){ 1273dc1bdc4fSdanielk1977 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName, 1274d8123366Sdanielk1977 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0); 1275dc1bdc4fSdanielk1977 if( pFunc && aAggs[i].needCollSeq ){ 1276dc1bdc4fSdanielk1977 pFunc->needCollSeq = 1; 1277dc1bdc4fSdanielk1977 } 1278dc1bdc4fSdanielk1977 } 1279268380caSdrh } 12804adee20fSdanielk1977 sqlite3RegisterDateTimeFunctions(db); 1281b7481e70Sdrh sqlite3_overload_function(db, "MATCH", 2); 1282fd9e1f31Sdanielk1977 #ifdef SQLITE_SSE 12833752785fSdrh (void)sqlite3SseFunctions(db); 1284fd9e1f31Sdanielk1977 #endif 128555ef4d97Sdrh #ifdef SQLITE_CASE_SENSITIVE_LIKE 128655ef4d97Sdrh sqlite3RegisterLikeFunctions(db, 1); 128755ef4d97Sdrh #else 128855ef4d97Sdrh sqlite3RegisterLikeFunctions(db, 0); 128955ef4d97Sdrh #endif 129055ef4d97Sdrh } 129155ef4d97Sdrh 129255ef4d97Sdrh /* 129355ef4d97Sdrh ** Set the LIKEOPT flag on the 2-argument function with the given name. 129455ef4d97Sdrh */ 1295d64fe2f3Sdrh static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){ 129655ef4d97Sdrh FuncDef *pDef; 129755ef4d97Sdrh pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0); 129855ef4d97Sdrh if( pDef ){ 1299d64fe2f3Sdrh pDef->flags = flagVal; 130055ef4d97Sdrh } 130155ef4d97Sdrh } 130255ef4d97Sdrh 130355ef4d97Sdrh /* 130455ef4d97Sdrh ** Register the built-in LIKE and GLOB functions. The caseSensitive 130555ef4d97Sdrh ** parameter determines whether or not the LIKE operator is case 130655ef4d97Sdrh ** sensitive. GLOB is always case sensitive. 130755ef4d97Sdrh */ 130855ef4d97Sdrh void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 130955ef4d97Sdrh struct compareInfo *pInfo; 131055ef4d97Sdrh if( caseSensitive ){ 131155ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoAlt; 131255ef4d97Sdrh }else{ 131355ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoNorm; 131455ef4d97Sdrh } 1315771151b6Sdanielk1977 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0); 1316771151b6Sdanielk1977 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0); 1317771151b6Sdanielk1977 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 131855ef4d97Sdrh (struct compareInfo*)&globInfo, likeFunc, 0,0); 1319d64fe2f3Sdrh setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); 1320d64fe2f3Sdrh setLikeOptFlag(db, "like", 1321d64fe2f3Sdrh caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); 132255ef4d97Sdrh } 132355ef4d97Sdrh 132455ef4d97Sdrh /* 132555ef4d97Sdrh ** pExpr points to an expression which implements a function. If 132655ef4d97Sdrh ** it is appropriate to apply the LIKE optimization to that function 132755ef4d97Sdrh ** then set aWc[0] through aWc[2] to the wildcard characters and 132855ef4d97Sdrh ** return TRUE. If the function is not a LIKE-style function then 132955ef4d97Sdrh ** return FALSE. 133055ef4d97Sdrh */ 1331d64fe2f3Sdrh int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 133255ef4d97Sdrh FuncDef *pDef; 133355ef4d97Sdrh if( pExpr->op!=TK_FUNCTION ){ 133455ef4d97Sdrh return 0; 133555ef4d97Sdrh } 133655ef4d97Sdrh if( pExpr->pList->nExpr!=2 ){ 133755ef4d97Sdrh return 0; 133855ef4d97Sdrh } 13392646da7eSdrh pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2, 134055ef4d97Sdrh SQLITE_UTF8, 0); 1341d64fe2f3Sdrh if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){ 134255ef4d97Sdrh return 0; 134355ef4d97Sdrh } 134455ef4d97Sdrh 134555ef4d97Sdrh /* The memcpy() statement assumes that the wildcard characters are 134655ef4d97Sdrh ** the first three statements in the compareInfo structure. The 134755ef4d97Sdrh ** asserts() that follow verify that assumption 134855ef4d97Sdrh */ 134955ef4d97Sdrh memcpy(aWc, pDef->pUserData, 3); 135055ef4d97Sdrh assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 135155ef4d97Sdrh assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 135255ef4d97Sdrh assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 1353d64fe2f3Sdrh *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0; 135455ef4d97Sdrh return 1; 1355dc04c583Sdrh } 1356