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*874abbedSdrh ** $Id: func.c,v 1.124 2006/02/23 21:51:13 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]); 1050bce8354Sdrh for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; } 106f4479501Sdrh sqlite3_result_int(context, len); 107f9b596ebSdrh break; 108f9b596ebSdrh } 109f9b596ebSdrh default: { 110f9b596ebSdrh sqlite3_result_null(context); 111f9b596ebSdrh break; 112f9b596ebSdrh } 113f9b596ebSdrh } 1140bce8354Sdrh } 1150bce8354Sdrh 1160bce8354Sdrh /* 1170bce8354Sdrh ** Implementation of the abs() function 1180bce8354Sdrh */ 1190ae8b831Sdanielk1977 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 1200bce8354Sdrh assert( argc==1 ); 121f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 1229c054830Sdrh case SQLITE_INTEGER: { 123f93bbbeaSdanielk1977 i64 iVal = sqlite3_value_int64(argv[0]); 12452fc849aSdrh if( iVal<0 ){ 12552fc849aSdrh if( (iVal<<1)==0 ){ 12652fc849aSdrh sqlite3_result_error(context, "integer overflow", -1); 12752fc849aSdrh return; 12852fc849aSdrh } 12952fc849aSdrh iVal = -iVal; 13052fc849aSdrh } 131f93bbbeaSdanielk1977 sqlite3_result_int64(context, iVal); 132f9b596ebSdrh break; 133f9b596ebSdrh } 1349c054830Sdrh case SQLITE_NULL: { 135f9b596ebSdrh sqlite3_result_null(context); 136f9b596ebSdrh break; 137f9b596ebSdrh } 138f9b596ebSdrh default: { 139f93bbbeaSdanielk1977 double rVal = sqlite3_value_double(argv[0]); 14052fc849aSdrh if( rVal<0 ) rVal = -rVal; 141f93bbbeaSdanielk1977 sqlite3_result_double(context, rVal); 142f9b596ebSdrh break; 143f9b596ebSdrh } 144f9b596ebSdrh } 1450bce8354Sdrh } 1460bce8354Sdrh 1470bce8354Sdrh /* 1480bce8354Sdrh ** Implementation of the substr() function 1490bce8354Sdrh */ 150f9b596ebSdrh static void substrFunc( 151f9b596ebSdrh sqlite3_context *context, 152f9b596ebSdrh int argc, 153f9b596ebSdrh sqlite3_value **argv 154f9b596ebSdrh ){ 1552646da7eSdrh const unsigned char *z; 1562646da7eSdrh const unsigned char *z2; 1570bce8354Sdrh int i; 1580bce8354Sdrh int p1, p2, len; 159f9b596ebSdrh 1600bce8354Sdrh assert( argc==3 ); 1614f26d6c4Sdrh z = sqlite3_value_text(argv[0]); 1620bce8354Sdrh if( z==0 ) return; 16351ad0ecdSdanielk1977 p1 = sqlite3_value_int(argv[1]); 16451ad0ecdSdanielk1977 p2 = sqlite3_value_int(argv[2]); 16547c8a679Sdrh for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; } 1660bce8354Sdrh if( p1<0 ){ 16789425d5eSdrh p1 += len; 168653bc759Sdrh if( p1<0 ){ 169653bc759Sdrh p2 += p1; 170653bc759Sdrh p1 = 0; 171653bc759Sdrh } 1720bce8354Sdrh }else if( p1>0 ){ 1730bce8354Sdrh p1--; 1740bce8354Sdrh } 1750bce8354Sdrh if( p1+p2>len ){ 1760bce8354Sdrh p2 = len-p1; 1770bce8354Sdrh } 17877396304Sdrh for(i=0; i<p1 && z[i]; i++){ 17947c8a679Sdrh if( (z[i]&0xc0)==0x80 ) p1++; 1800bce8354Sdrh } 18147c8a679Sdrh while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; } 18277396304Sdrh for(; i<p1+p2 && z[i]; i++){ 18347c8a679Sdrh if( (z[i]&0xc0)==0x80 ) p2++; 1840bce8354Sdrh } 18547c8a679Sdrh while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; } 186653bc759Sdrh if( p2<0 ) p2 = 0; 1872646da7eSdrh sqlite3_result_text(context, (char*)&z[p1], p2, SQLITE_TRANSIENT); 1880bce8354Sdrh } 1890bce8354Sdrh 1900bce8354Sdrh /* 1910bce8354Sdrh ** Implementation of the round() function 1920bce8354Sdrh */ 1930ae8b831Sdanielk1977 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 19451ad0ecdSdanielk1977 int n = 0; 1950bce8354Sdrh double r; 196592ac8cbSdrh char zBuf[500]; /* larger than the %f representation of the largest double */ 1970bce8354Sdrh assert( argc==1 || argc==2 ); 19851ad0ecdSdanielk1977 if( argc==2 ){ 1999c054830Sdrh if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; 20051ad0ecdSdanielk1977 n = sqlite3_value_int(argv[1]); 2010bce8354Sdrh if( n>30 ) n = 30; 2020bce8354Sdrh if( n<0 ) n = 0; 20351ad0ecdSdanielk1977 } 2049c054830Sdrh if( SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 2054f26d6c4Sdrh r = sqlite3_value_double(argv[0]); 206e866fcb9Sdrh sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r); 207d8123366Sdanielk1977 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 2080bce8354Sdrh } 209dc04c583Sdrh 210dc04c583Sdrh /* 211dc04c583Sdrh ** Implementation of the upper() and lower() SQL functions. 212dc04c583Sdrh */ 2130ae8b831Sdanielk1977 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 2148cd9db00Sdrh unsigned char *z; 215dc04c583Sdrh int i; 2169c054830Sdrh if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 217c572ef7fSdanielk1977 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1); 218dc04c583Sdrh if( z==0 ) return; 2192646da7eSdrh strcpy((char*)z, (char*)sqlite3_value_text(argv[0])); 220dc04c583Sdrh for(i=0; z[i]; i++){ 2214c755c0fSdrh z[i] = toupper(z[i]); 222dc04c583Sdrh } 2232646da7eSdrh sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT); 2247e18c259Sdanielk1977 sqliteFree(z); 225dc04c583Sdrh } 2260ae8b831Sdanielk1977 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 2278cd9db00Sdrh unsigned char *z; 228dc04c583Sdrh int i; 2299c054830Sdrh if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; 230c572ef7fSdanielk1977 z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1); 231dc04c583Sdrh if( z==0 ) return; 2322646da7eSdrh strcpy((char*)z, (char*)sqlite3_value_text(argv[0])); 233dc04c583Sdrh for(i=0; z[i]; i++){ 2344c755c0fSdrh z[i] = tolower(z[i]); 235dc04c583Sdrh } 2362646da7eSdrh sqlite3_result_text(context, (char*)z, -1, SQLITE_TRANSIENT); 2377e18c259Sdanielk1977 sqliteFree(z); 238dc04c583Sdrh } 239dc04c583Sdrh 240dc04c583Sdrh /* 241fbc99082Sdrh ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. 242b6c9e6e6Sjplyon ** All three do the same thing. They return the first non-NULL 243b6c9e6e6Sjplyon ** argument. 2443212e182Sdrh */ 245f9b596ebSdrh static void ifnullFunc( 246f9b596ebSdrh sqlite3_context *context, 247f9b596ebSdrh int argc, 248f9b596ebSdrh sqlite3_value **argv 249f9b596ebSdrh ){ 250fbc99082Sdrh int i; 251fbc99082Sdrh for(i=0; i<argc; i++){ 2529c054830Sdrh if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){ 253f4479501Sdrh sqlite3_result_value(context, argv[i]); 254fbc99082Sdrh break; 255fbc99082Sdrh } 256fbc99082Sdrh } 2573212e182Sdrh } 2583212e182Sdrh 2593212e182Sdrh /* 260f9ffac96Sdrh ** Implementation of random(). Return a random integer. 261f9ffac96Sdrh */ 262f9b596ebSdrh static void randomFunc( 263f9b596ebSdrh sqlite3_context *context, 264f9b596ebSdrh int argc, 265f9b596ebSdrh sqlite3_value **argv 266f9b596ebSdrh ){ 26752fc849aSdrh sqlite_int64 r; 2684adee20fSdanielk1977 sqlite3Randomness(sizeof(r), &r); 269*874abbedSdrh if( (r<<1)==0 ) r = 0; /* Prevent 0x8000.... as the result so that we */ 270*874abbedSdrh /* can always do abs() of the result */ 27152fc849aSdrh sqlite3_result_int64(context, r); 272f9ffac96Sdrh } 273f9ffac96Sdrh 274f9ffac96Sdrh /* 2756ed41ad7Sdrh ** Implementation of the last_insert_rowid() SQL function. The return 27624b03fd0Sdanielk1977 ** value is the same as the sqlite3_last_insert_rowid() API function. 2776ed41ad7Sdrh */ 27851ad0ecdSdanielk1977 static void last_insert_rowid( 2790ae8b831Sdanielk1977 sqlite3_context *context, 28051ad0ecdSdanielk1977 int arg, 28151ad0ecdSdanielk1977 sqlite3_value **argv 28251ad0ecdSdanielk1977 ){ 2839bb575fdSdrh sqlite3 *db = sqlite3_user_data(context); 284f9b596ebSdrh sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); 2856ed41ad7Sdrh } 2866ed41ad7Sdrh 287f146a776Srdc /* 288b28af71aSdanielk1977 ** Implementation of the changes() SQL function. The return value is the 289b28af71aSdanielk1977 ** same as the sqlite3_changes() API function. 290f146a776Srdc */ 291b28af71aSdanielk1977 static void changes( 292f9b596ebSdrh sqlite3_context *context, 293f9b596ebSdrh int arg, 294f9b596ebSdrh sqlite3_value **argv 295f9b596ebSdrh ){ 2969bb575fdSdrh sqlite3 *db = sqlite3_user_data(context); 297f4479501Sdrh sqlite3_result_int(context, sqlite3_changes(db)); 298b0c374ffSrdc } 299f146a776Srdc 300f146a776Srdc /* 301b28af71aSdanielk1977 ** Implementation of the total_changes() SQL function. The return value is 302b28af71aSdanielk1977 ** the same as the sqlite3_total_changes() API function. 303f146a776Srdc */ 304b28af71aSdanielk1977 static void total_changes( 3050ae8b831Sdanielk1977 sqlite3_context *context, 30651ad0ecdSdanielk1977 int arg, 30751ad0ecdSdanielk1977 sqlite3_value **argv 30851ad0ecdSdanielk1977 ){ 3099bb575fdSdrh sqlite3 *db = sqlite3_user_data(context); 310b28af71aSdanielk1977 sqlite3_result_int(context, sqlite3_total_changes(db)); 311b0c374ffSrdc } 312b0c374ffSrdc 3136ed41ad7Sdrh /* 3144e5ffc5fSdrh ** A structure defining how to do GLOB-style comparisons. 315d02eb1fdSdanielk1977 */ 3164e5ffc5fSdrh struct compareInfo { 3174e5ffc5fSdrh u8 matchAll; 3184e5ffc5fSdrh u8 matchOne; 3194e5ffc5fSdrh u8 matchSet; 3204e5ffc5fSdrh u8 noCase; 321d02eb1fdSdanielk1977 }; 32255ef4d97Sdrh 3234e5ffc5fSdrh static const struct compareInfo globInfo = { '*', '?', '[', 0 }; 32470031fa3Sdrh /* The correct SQL-92 behavior is for the LIKE operator to ignore 32570031fa3Sdrh ** case. Thus 'a' LIKE 'A' would be true. */ 32655ef4d97Sdrh static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; 32770031fa3Sdrh /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator 32870031fa3Sdrh ** is case sensitive causing 'a' LIKE 'A' to be false */ 32955ef4d97Sdrh static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; 330d02eb1fdSdanielk1977 331d02eb1fdSdanielk1977 /* 3324e5ffc5fSdrh ** X is a pointer to the first byte of a UTF-8 character. Increment 3334e5ffc5fSdrh ** X so that it points to the next character. This only works right 3344e5ffc5fSdrh ** if X points to a well-formed UTF-8 string. 335d02eb1fdSdanielk1977 */ 3364e5ffc5fSdrh #define sqliteNextChar(X) while( (0xc0&*++(X))==0x80 ){} 3374e5ffc5fSdrh #define sqliteCharVal(X) sqlite3ReadUtf8(X) 338d02eb1fdSdanielk1977 339d02eb1fdSdanielk1977 340d02eb1fdSdanielk1977 /* 3414e5ffc5fSdrh ** Compare two UTF-8 strings for equality where the first string can 3424e5ffc5fSdrh ** potentially be a "glob" expression. Return true (1) if they 3434e5ffc5fSdrh ** are the same and false (0) if they are different. 3440ac65892Sdrh ** 3454e5ffc5fSdrh ** Globbing rules: 3460ac65892Sdrh ** 3474e5ffc5fSdrh ** '*' Matches any sequence of zero or more characters. 348d02eb1fdSdanielk1977 ** 3494e5ffc5fSdrh ** '?' Matches exactly one character. 3504e5ffc5fSdrh ** 3514e5ffc5fSdrh ** [...] Matches one character from the enclosed list of 3524e5ffc5fSdrh ** characters. 3534e5ffc5fSdrh ** 3544e5ffc5fSdrh ** [^...] Matches one character not in the enclosed list. 3554e5ffc5fSdrh ** 3564e5ffc5fSdrh ** With the [...] and [^...] matching, a ']' character can be included 3574e5ffc5fSdrh ** in the list by making it the first character after '[' or '^'. A 3584e5ffc5fSdrh ** range of characters can be specified using '-'. Example: 3594e5ffc5fSdrh ** "[a-z]" matches any single lower-case letter. To match a '-', make 3604e5ffc5fSdrh ** it the last character in the list. 3614e5ffc5fSdrh ** 3624e5ffc5fSdrh ** This routine is usually quick, but can be N**2 in the worst case. 3634e5ffc5fSdrh ** 3644e5ffc5fSdrh ** Hints: to match '*' or '?', put them in "[]". Like this: 3654e5ffc5fSdrh ** 3664e5ffc5fSdrh ** abc[*]xyz Matches "abc*xyz" only 3670ac65892Sdrh */ 3687c6303c0Sdanielk1977 static int patternCompare( 3694e5ffc5fSdrh const u8 *zPattern, /* The glob pattern */ 3704e5ffc5fSdrh const u8 *zString, /* The string to compare against the glob */ 3717c6303c0Sdanielk1977 const struct compareInfo *pInfo, /* Information about how to do the compare */ 3727c6303c0Sdanielk1977 const int esc /* The escape character */ 37351ad0ecdSdanielk1977 ){ 374ad7dd425Sdanielk1977 register int c; 3754e5ffc5fSdrh int invert; 3764e5ffc5fSdrh int seen; 3774e5ffc5fSdrh int c2; 3784e5ffc5fSdrh u8 matchOne = pInfo->matchOne; 3794e5ffc5fSdrh u8 matchAll = pInfo->matchAll; 3804e5ffc5fSdrh u8 matchSet = pInfo->matchSet; 3814e5ffc5fSdrh u8 noCase = pInfo->noCase; 3827c6303c0Sdanielk1977 int prevEscape = 0; /* True if the previous character was 'escape' */ 383d02eb1fdSdanielk1977 3844e5ffc5fSdrh while( (c = *zPattern)!=0 ){ 3857c6303c0Sdanielk1977 if( !prevEscape && c==matchAll ){ 3864e5ffc5fSdrh while( (c=zPattern[1]) == matchAll || c == matchOne ){ 3874e5ffc5fSdrh if( c==matchOne ){ 3884e5ffc5fSdrh if( *zString==0 ) return 0; 3894e5ffc5fSdrh sqliteNextChar(zString); 390d02eb1fdSdanielk1977 } 3914e5ffc5fSdrh zPattern++; 3924e5ffc5fSdrh } 39320fc0887Sdrh if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){ 3947c6303c0Sdanielk1977 u8 const *zTemp = &zPattern[1]; 3957c6303c0Sdanielk1977 sqliteNextChar(zTemp); 3967c6303c0Sdanielk1977 c = *zTemp; 3977c6303c0Sdanielk1977 } 3984e5ffc5fSdrh if( c==0 ) return 1; 3994e5ffc5fSdrh if( c==matchSet ){ 4007c6303c0Sdanielk1977 assert( esc==0 ); /* This is GLOB, not LIKE */ 4017c6303c0Sdanielk1977 while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){ 4024e5ffc5fSdrh sqliteNextChar(zString); 4034e5ffc5fSdrh } 4044e5ffc5fSdrh return *zString!=0; 405d02eb1fdSdanielk1977 }else{ 4064e5ffc5fSdrh while( (c2 = *zString)!=0 ){ 4074e5ffc5fSdrh if( noCase ){ 4084e5ffc5fSdrh c2 = sqlite3UpperToLower[c2]; 4094e5ffc5fSdrh c = sqlite3UpperToLower[c]; 4104e5ffc5fSdrh while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; } 411ad7dd425Sdanielk1977 }else{ 4124e5ffc5fSdrh while( c2 != 0 && c2 != c ){ c2 = *++zString; } 413ad7dd425Sdanielk1977 } 4144e5ffc5fSdrh if( c2==0 ) return 0; 4157c6303c0Sdanielk1977 if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1; 4164e5ffc5fSdrh sqliteNextChar(zString); 4174e5ffc5fSdrh } 4184e5ffc5fSdrh return 0; 4194e5ffc5fSdrh } 4207c6303c0Sdanielk1977 }else if( !prevEscape && c==matchOne ){ 4214e5ffc5fSdrh if( *zString==0 ) return 0; 4224e5ffc5fSdrh sqliteNextChar(zString); 4234e5ffc5fSdrh zPattern++; 4244e5ffc5fSdrh }else if( c==matchSet ){ 4254e5ffc5fSdrh int prior_c = 0; 4267c6303c0Sdanielk1977 assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ 4274e5ffc5fSdrh seen = 0; 4284e5ffc5fSdrh invert = 0; 4294e5ffc5fSdrh c = sqliteCharVal(zString); 4304e5ffc5fSdrh if( c==0 ) return 0; 4314e5ffc5fSdrh c2 = *++zPattern; 4324e5ffc5fSdrh if( c2=='^' ){ invert = 1; c2 = *++zPattern; } 4334e5ffc5fSdrh if( c2==']' ){ 4344e5ffc5fSdrh if( c==']' ) seen = 1; 4354e5ffc5fSdrh c2 = *++zPattern; 4364e5ffc5fSdrh } 4374e5ffc5fSdrh while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){ 4384e5ffc5fSdrh if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){ 4394e5ffc5fSdrh zPattern++; 4404e5ffc5fSdrh c2 = sqliteCharVal(zPattern); 4414e5ffc5fSdrh if( c>=prior_c && c<=c2 ) seen = 1; 4424e5ffc5fSdrh prior_c = 0; 4434e5ffc5fSdrh }else if( c==c2 ){ 4444e5ffc5fSdrh seen = 1; 4454e5ffc5fSdrh prior_c = c2; 446d02eb1fdSdanielk1977 }else{ 4474e5ffc5fSdrh prior_c = c2; 448d02eb1fdSdanielk1977 } 4494e5ffc5fSdrh sqliteNextChar(zPattern); 450d02eb1fdSdanielk1977 } 4514e5ffc5fSdrh if( c2==0 || (seen ^ invert)==0 ) return 0; 4524e5ffc5fSdrh sqliteNextChar(zString); 4534e5ffc5fSdrh zPattern++; 45420fc0887Sdrh }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){ 4557c6303c0Sdanielk1977 prevEscape = 1; 4567c6303c0Sdanielk1977 sqliteNextChar(zPattern); 457d02eb1fdSdanielk1977 }else{ 4584e5ffc5fSdrh if( noCase ){ 4594e5ffc5fSdrh if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0; 4604e5ffc5fSdrh }else{ 4614e5ffc5fSdrh if( c != *zString ) return 0; 4624e5ffc5fSdrh } 4634e5ffc5fSdrh zPattern++; 4644e5ffc5fSdrh zString++; 4657c6303c0Sdanielk1977 prevEscape = 0; 46651ad0ecdSdanielk1977 } 4670ac65892Sdrh } 4684e5ffc5fSdrh return *zString==0; 4694e5ffc5fSdrh } 4704e5ffc5fSdrh 47155ef4d97Sdrh /* 47255ef4d97Sdrh ** Count the number of times that the LIKE operator (or GLOB which is 47355ef4d97Sdrh ** just a variation of LIKE) gets called. This is used for testing 47455ef4d97Sdrh ** only. 47555ef4d97Sdrh */ 47655ef4d97Sdrh #ifdef SQLITE_TEST 47755ef4d97Sdrh int sqlite3_like_count = 0; 47855ef4d97Sdrh #endif 47955ef4d97Sdrh 4803f6b0874Sdanielk1977 4813f6b0874Sdanielk1977 /* 4823f6b0874Sdanielk1977 ** Implementation of the like() SQL function. This function implements 4833f6b0874Sdanielk1977 ** the build-in LIKE operator. The first argument to the function is the 4843f6b0874Sdanielk1977 ** pattern and the second argument is the string. So, the SQL statements: 4853f6b0874Sdanielk1977 ** 4863f6b0874Sdanielk1977 ** A LIKE B 4873f6b0874Sdanielk1977 ** 4883f6b0874Sdanielk1977 ** is implemented as like(B,A). 4893f6b0874Sdanielk1977 ** 49055ef4d97Sdrh ** This same function (with a different compareInfo structure) computes 49155ef4d97Sdrh ** the GLOB operator. 4923f6b0874Sdanielk1977 */ 4933f6b0874Sdanielk1977 static void likeFunc( 4943f6b0874Sdanielk1977 sqlite3_context *context, 4953f6b0874Sdanielk1977 int argc, 4963f6b0874Sdanielk1977 sqlite3_value **argv 4973f6b0874Sdanielk1977 ){ 4983f6b0874Sdanielk1977 const unsigned char *zA = sqlite3_value_text(argv[0]); 4993f6b0874Sdanielk1977 const unsigned char *zB = sqlite3_value_text(argv[1]); 5007c6303c0Sdanielk1977 int escape = 0; 5017c6303c0Sdanielk1977 if( argc==3 ){ 5027c6303c0Sdanielk1977 /* The escape character string must consist of a single UTF-8 character. 5037c6303c0Sdanielk1977 ** Otherwise, return an error. 5047c6303c0Sdanielk1977 */ 5057c6303c0Sdanielk1977 const unsigned char *zEsc = sqlite3_value_text(argv[2]); 5062646da7eSdrh if( sqlite3utf8CharLen((char*)zEsc, -1)!=1 ){ 5077c6303c0Sdanielk1977 sqlite3_result_error(context, 5087c6303c0Sdanielk1977 "ESCAPE expression must be a single character", -1); 5097c6303c0Sdanielk1977 return; 5107c6303c0Sdanielk1977 } 5117c6303c0Sdanielk1977 escape = sqlite3ReadUtf8(zEsc); 5127c6303c0Sdanielk1977 } 5133f6b0874Sdanielk1977 if( zA && zB ){ 51455ef4d97Sdrh struct compareInfo *pInfo = sqlite3_user_data(context); 51555ef4d97Sdrh #ifdef SQLITE_TEST 51655ef4d97Sdrh sqlite3_like_count++; 51755ef4d97Sdrh #endif 51855ef4d97Sdrh sqlite3_result_int(context, patternCompare(zA, zB, pInfo, escape)); 51951ad0ecdSdanielk1977 } 5208912d106Sdrh } 5218912d106Sdrh 5228912d106Sdrh /* 5238912d106Sdrh ** Implementation of the NULLIF(x,y) function. The result is the first 5248912d106Sdrh ** argument if the arguments are different. The result is NULL if the 5258912d106Sdrh ** arguments are equal to each other. 5268912d106Sdrh */ 527f9b596ebSdrh static void nullifFunc( 528f9b596ebSdrh sqlite3_context *context, 529f9b596ebSdrh int argc, 530f9b596ebSdrh sqlite3_value **argv 531f9b596ebSdrh ){ 532dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 533dc1bdc4fSdanielk1977 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ 534f4479501Sdrh sqlite3_result_value(context, argv[0]); 5358912d106Sdrh } 5360ac65892Sdrh } 5370ac65892Sdrh 538647cb0e1Sdrh /* 539647cb0e1Sdrh ** Implementation of the VERSION(*) function. The result is the version 540647cb0e1Sdrh ** of the SQLite library that is running. 541647cb0e1Sdrh */ 542f9b596ebSdrh static void versionFunc( 543f9b596ebSdrh sqlite3_context *context, 544f9b596ebSdrh int argc, 545f9b596ebSdrh sqlite3_value **argv 546f9b596ebSdrh ){ 547d8123366Sdanielk1977 sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); 548647cb0e1Sdrh } 549647cb0e1Sdrh 550d641d646Sdanielk1977 55147394703Sdrh /* 55247394703Sdrh ** EXPERIMENTAL - This is not an official function. The interface may 55347394703Sdrh ** change. This function may disappear. Do not write code that depends 55447394703Sdrh ** on this function. 55547394703Sdrh ** 55647394703Sdrh ** Implementation of the QUOTE() function. This function takes a single 55747394703Sdrh ** argument. If the argument is numeric, the return value is the same as 55847394703Sdrh ** the argument. If the argument is NULL, the return value is the string 55947394703Sdrh ** "NULL". Otherwise, the argument is enclosed in single quotes with 56047394703Sdrh ** single-quote escapes. 56147394703Sdrh */ 5620ae8b831Sdanielk1977 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 56347394703Sdrh if( argc<1 ) return; 564f9b596ebSdrh switch( sqlite3_value_type(argv[0]) ){ 5659c054830Sdrh case SQLITE_NULL: { 566d8123366Sdanielk1977 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); 567f9b596ebSdrh break; 568f9b596ebSdrh } 5699c054830Sdrh case SQLITE_INTEGER: 5709c054830Sdrh case SQLITE_FLOAT: { 571f4479501Sdrh sqlite3_result_value(context, argv[0]); 572f9b596ebSdrh break; 573f9b596ebSdrh } 5743f41e976Sdanielk1977 case SQLITE_BLOB: { 5753f41e976Sdanielk1977 static const char hexdigits[] = { 5763f41e976Sdanielk1977 '0', '1', '2', '3', '4', '5', '6', '7', 5773f41e976Sdanielk1977 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 5783f41e976Sdanielk1977 }; 5793f41e976Sdanielk1977 char *zText = 0; 5803f41e976Sdanielk1977 int nBlob = sqlite3_value_bytes(argv[0]); 5813f41e976Sdanielk1977 char const *zBlob = sqlite3_value_blob(argv[0]); 5823f41e976Sdanielk1977 5833f41e976Sdanielk1977 zText = (char *)sqliteMalloc((2*nBlob)+4); 5843f41e976Sdanielk1977 if( !zText ){ 5853f41e976Sdanielk1977 sqlite3_result_error(context, "out of memory", -1); 5863f41e976Sdanielk1977 }else{ 5873f41e976Sdanielk1977 int i; 5883f41e976Sdanielk1977 for(i=0; i<nBlob; i++){ 5893f41e976Sdanielk1977 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; 5903f41e976Sdanielk1977 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; 5913f41e976Sdanielk1977 } 5923f41e976Sdanielk1977 zText[(nBlob*2)+2] = '\''; 5933f41e976Sdanielk1977 zText[(nBlob*2)+3] = '\0'; 5943f41e976Sdanielk1977 zText[0] = 'X'; 5953f41e976Sdanielk1977 zText[1] = '\''; 596d8123366Sdanielk1977 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); 5973f41e976Sdanielk1977 sqliteFree(zText); 5983f41e976Sdanielk1977 } 5993f41e976Sdanielk1977 break; 6003f41e976Sdanielk1977 } 6019c054830Sdrh case SQLITE_TEXT: { 60247394703Sdrh int i,j,n; 6032646da7eSdrh const unsigned char *zArg = sqlite3_value_text(argv[0]); 60447394703Sdrh char *z; 605f9b596ebSdrh 60651ad0ecdSdanielk1977 for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } 60747394703Sdrh z = sqliteMalloc( i+n+3 ); 60847394703Sdrh if( z==0 ) return; 60947394703Sdrh z[0] = '\''; 61051ad0ecdSdanielk1977 for(i=0, j=1; zArg[i]; i++){ 61151ad0ecdSdanielk1977 z[j++] = zArg[i]; 61251ad0ecdSdanielk1977 if( zArg[i]=='\'' ){ 61347394703Sdrh z[j++] = '\''; 61447394703Sdrh } 61547394703Sdrh } 61647394703Sdrh z[j++] = '\''; 61747394703Sdrh z[j] = 0; 618d8123366Sdanielk1977 sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); 61947394703Sdrh sqliteFree(z); 62047394703Sdrh } 62147394703Sdrh } 622f9b596ebSdrh } 62347394703Sdrh 624d24cc427Sdrh #ifdef SQLITE_SOUNDEX 625d24cc427Sdrh /* 626d24cc427Sdrh ** Compute the soundex encoding of a word. 627d24cc427Sdrh */ 6280ae8b831Sdanielk1977 static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ 629d24cc427Sdrh char zResult[8]; 6304c755c0fSdrh const u8 *zIn; 631d24cc427Sdrh int i, j; 632d24cc427Sdrh static const unsigned char iCode[] = { 633d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 634d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 635d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 636d24cc427Sdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 637d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 638d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 639d24cc427Sdrh 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, 640d24cc427Sdrh 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 641d24cc427Sdrh }; 642d24cc427Sdrh assert( argc==1 ); 6434c755c0fSdrh zIn = (u8*)sqlite3_value_text(argv[0]); 644d24cc427Sdrh for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} 645d24cc427Sdrh if( zIn[i] ){ 646d24cc427Sdrh zResult[0] = toupper(zIn[i]); 647d24cc427Sdrh for(j=1; j<4 && zIn[i]; i++){ 648d24cc427Sdrh int code = iCode[zIn[i]&0x7f]; 649d24cc427Sdrh if( code>0 ){ 650d24cc427Sdrh zResult[j++] = code + '0'; 651d24cc427Sdrh } 652d24cc427Sdrh } 653d24cc427Sdrh while( j<4 ){ 654d24cc427Sdrh zResult[j++] = '0'; 655d24cc427Sdrh } 656d24cc427Sdrh zResult[j] = 0; 657d8123366Sdanielk1977 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); 658d24cc427Sdrh }else{ 659d8123366Sdanielk1977 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); 660d24cc427Sdrh } 661d24cc427Sdrh } 662d24cc427Sdrh #endif 663d24cc427Sdrh 664193a6b41Sdrh #ifdef SQLITE_TEST 665193a6b41Sdrh /* 666193a6b41Sdrh ** This function generates a string of random characters. Used for 667193a6b41Sdrh ** generating test data. 668193a6b41Sdrh */ 6690ae8b831Sdanielk1977 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ 670bbd82df6Sdrh static const unsigned char zSrc[] = 671193a6b41Sdrh "abcdefghijklmnopqrstuvwxyz" 672193a6b41Sdrh "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 673193a6b41Sdrh "0123456789" 674193a6b41Sdrh ".-!,:*^+=_|?/<> "; 675193a6b41Sdrh int iMin, iMax, n, r, i; 676bbd82df6Sdrh unsigned char zBuf[1000]; 677193a6b41Sdrh if( argc>=1 ){ 678f9b596ebSdrh iMin = sqlite3_value_int(argv[0]); 679193a6b41Sdrh if( iMin<0 ) iMin = 0; 680193a6b41Sdrh if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; 681193a6b41Sdrh }else{ 682193a6b41Sdrh iMin = 1; 683193a6b41Sdrh } 684193a6b41Sdrh if( argc>=2 ){ 685f9b596ebSdrh iMax = sqlite3_value_int(argv[1]); 686193a6b41Sdrh if( iMax<iMin ) iMax = iMin; 6871dba7279Sdrh if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; 688193a6b41Sdrh }else{ 689193a6b41Sdrh iMax = 50; 690193a6b41Sdrh } 691193a6b41Sdrh n = iMin; 692193a6b41Sdrh if( iMax>iMin ){ 6934adee20fSdanielk1977 sqlite3Randomness(sizeof(r), &r); 694bbd82df6Sdrh r &= 0x7fffffff; 695193a6b41Sdrh n += r%(iMax + 1 - iMin); 696193a6b41Sdrh } 6971dba7279Sdrh assert( n<sizeof(zBuf) ); 6984adee20fSdanielk1977 sqlite3Randomness(n, zBuf); 699193a6b41Sdrh for(i=0; i<n; i++){ 700bbd82df6Sdrh zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; 701193a6b41Sdrh } 702193a6b41Sdrh zBuf[n] = 0; 7032646da7eSdrh sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); 704d8123366Sdanielk1977 } 7050e3d7476Sdrh #endif /* SQLITE_TEST */ 706d8123366Sdanielk1977 7070e3d7476Sdrh #ifdef SQLITE_TEST 708d8123366Sdanielk1977 /* 709d8123366Sdanielk1977 ** The following two SQL functions are used to test returning a text 710d8123366Sdanielk1977 ** result with a destructor. Function 'test_destructor' takes one argument 711d8123366Sdanielk1977 ** and returns the same argument interpreted as TEXT. A destructor is 712d8123366Sdanielk1977 ** passed with the sqlite3_result_text() call. 713d8123366Sdanielk1977 ** 714d8123366Sdanielk1977 ** SQL function 'test_destructor_count' returns the number of outstanding 715d8123366Sdanielk1977 ** allocations made by 'test_destructor'; 716d8123366Sdanielk1977 ** 717d8123366Sdanielk1977 ** WARNING: Not threadsafe. 718d8123366Sdanielk1977 */ 719d8123366Sdanielk1977 static int test_destructor_count_var = 0; 720d8123366Sdanielk1977 static void destructor(void *p){ 721d8123366Sdanielk1977 char *zVal = (char *)p; 722d8123366Sdanielk1977 assert(zVal); 723d8123366Sdanielk1977 zVal--; 724d8123366Sdanielk1977 sqliteFree(zVal); 725d8123366Sdanielk1977 test_destructor_count_var--; 726d8123366Sdanielk1977 } 727d8123366Sdanielk1977 static void test_destructor( 728d8123366Sdanielk1977 sqlite3_context *pCtx, 729d8123366Sdanielk1977 int nArg, 730d8123366Sdanielk1977 sqlite3_value **argv 731d8123366Sdanielk1977 ){ 732d8123366Sdanielk1977 char *zVal; 733f4618891Sdanielk1977 int len; 7349bb575fdSdrh sqlite3 *db = sqlite3_user_data(pCtx); 735f4618891Sdanielk1977 736d8123366Sdanielk1977 test_destructor_count_var++; 737d8123366Sdanielk1977 assert( nArg==1 ); 738d8123366Sdanielk1977 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 73914db2665Sdanielk1977 len = sqlite3ValueBytes(argv[0], ENC(db)); 740f4618891Sdanielk1977 zVal = sqliteMalloc(len+3); 741f4618891Sdanielk1977 zVal[len] = 0; 742f4618891Sdanielk1977 zVal[len-1] = 0; 743d8123366Sdanielk1977 assert( zVal ); 744d8123366Sdanielk1977 zVal++; 74514db2665Sdanielk1977 memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len); 74614db2665Sdanielk1977 if( ENC(db)==SQLITE_UTF8 ){ 747d8123366Sdanielk1977 sqlite3_result_text(pCtx, zVal, -1, destructor); 7486c62608fSdrh #ifndef SQLITE_OMIT_UTF16 74914db2665Sdanielk1977 }else if( ENC(db)==SQLITE_UTF16LE ){ 750f4618891Sdanielk1977 sqlite3_result_text16le(pCtx, zVal, -1, destructor); 751f4618891Sdanielk1977 }else{ 752f4618891Sdanielk1977 sqlite3_result_text16be(pCtx, zVal, -1, destructor); 7536c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 754f4618891Sdanielk1977 } 755d8123366Sdanielk1977 } 756d8123366Sdanielk1977 static void test_destructor_count( 757d8123366Sdanielk1977 sqlite3_context *pCtx, 758d8123366Sdanielk1977 int nArg, 759d8123366Sdanielk1977 sqlite3_value **argv 760d8123366Sdanielk1977 ){ 761d8123366Sdanielk1977 sqlite3_result_int(pCtx, test_destructor_count_var); 762193a6b41Sdrh } 7630e3d7476Sdrh #endif /* SQLITE_TEST */ 7643f6b0874Sdanielk1977 7650e3d7476Sdrh #ifdef SQLITE_TEST 7660e3d7476Sdrh /* 7670e3d7476Sdrh ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() 7680e3d7476Sdrh ** interface. 7690e3d7476Sdrh ** 7700e3d7476Sdrh ** The test_auxdata() SQL function attempts to register each of its arguments 7710e3d7476Sdrh ** as auxiliary data. If there are no prior registrations of aux data for 7720e3d7476Sdrh ** that argument (meaning the argument is not a constant or this is its first 7730e3d7476Sdrh ** call) then the result for that argument is 0. If there is a prior 7740e3d7476Sdrh ** registration, the result for that argument is 1. The overall result 7750e3d7476Sdrh ** is the individual argument results separated by spaces. 7760e3d7476Sdrh */ 7773f6b0874Sdanielk1977 static void free_test_auxdata(void *p) {sqliteFree(p);} 7783f6b0874Sdanielk1977 static void test_auxdata( 7793f6b0874Sdanielk1977 sqlite3_context *pCtx, 7803f6b0874Sdanielk1977 int nArg, 7813f6b0874Sdanielk1977 sqlite3_value **argv 7823f6b0874Sdanielk1977 ){ 7833f6b0874Sdanielk1977 int i; 7843f6b0874Sdanielk1977 char *zRet = sqliteMalloc(nArg*2); 7853f6b0874Sdanielk1977 if( !zRet ) return; 7863f6b0874Sdanielk1977 for(i=0; i<nArg; i++){ 7872646da7eSdrh char const *z = (char*)sqlite3_value_text(argv[i]); 7883f6b0874Sdanielk1977 if( z ){ 7893f6b0874Sdanielk1977 char *zAux = sqlite3_get_auxdata(pCtx, i); 7903f6b0874Sdanielk1977 if( zAux ){ 7913f6b0874Sdanielk1977 zRet[i*2] = '1'; 7923f6b0874Sdanielk1977 if( strcmp(zAux, z) ){ 7933f6b0874Sdanielk1977 sqlite3_result_error(pCtx, "Auxilary data corruption", -1); 7943f6b0874Sdanielk1977 return; 7953f6b0874Sdanielk1977 } 7963f6b0874Sdanielk1977 }else{ 7973f6b0874Sdanielk1977 zRet[i*2] = '0'; 7983f6b0874Sdanielk1977 zAux = sqliteStrDup(z); 7993f6b0874Sdanielk1977 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); 8003f6b0874Sdanielk1977 } 8013f6b0874Sdanielk1977 zRet[i*2+1] = ' '; 8023f6b0874Sdanielk1977 } 8033f6b0874Sdanielk1977 } 8043f6b0874Sdanielk1977 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); 8053f6b0874Sdanielk1977 } 8060e3d7476Sdrh #endif /* SQLITE_TEST */ 807193a6b41Sdrh 80801427a62Sdanielk1977 #ifdef SQLITE_TEST 80901427a62Sdanielk1977 /* 81001427a62Sdanielk1977 ** A function to test error reporting from user functions. This function 81101427a62Sdanielk1977 ** returns a copy of it's first argument as an error. 81201427a62Sdanielk1977 */ 81301427a62Sdanielk1977 static void test_error( 81401427a62Sdanielk1977 sqlite3_context *pCtx, 81501427a62Sdanielk1977 int nArg, 81601427a62Sdanielk1977 sqlite3_value **argv 81701427a62Sdanielk1977 ){ 8182646da7eSdrh sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0); 81901427a62Sdanielk1977 } 82001427a62Sdanielk1977 #endif /* SQLITE_TEST */ 82101427a62Sdanielk1977 8220ac65892Sdrh /* 823d3a149efSdrh ** An instance of the following structure holds the context of a 824dd5baa95Sdrh ** sum() or avg() aggregate computation. 825dd5baa95Sdrh */ 826dd5baa95Sdrh typedef struct SumCtx SumCtx; 827dd5baa95Sdrh struct SumCtx { 8288c08e861Sdrh double rSum; /* Floating point sum */ 8298c08e861Sdrh i64 iSum; /* Integer sum */ 830cf85a51cSdrh i64 cnt; /* Number of elements summed */ 8318c08e861Sdrh u8 overflow; /* True if integer overflow seen */ 8328c08e861Sdrh u8 approx; /* True if non-integer value was input to the sum */ 833dd5baa95Sdrh }; 834dd5baa95Sdrh 835dd5baa95Sdrh /* 836a97fdd3bSdrh ** Routines used to compute the sum, average, and total. 837a97fdd3bSdrh ** 838a97fdd3bSdrh ** The SUM() function follows the (broken) SQL standard which means 839a97fdd3bSdrh ** that it returns NULL if it sums over no inputs. TOTAL returns 840a97fdd3bSdrh ** 0.0 in that case. In addition, TOTAL always returns a float where 841a97fdd3bSdrh ** SUM might return an integer if it never encounters a floating point 842a97fdd3bSdrh ** value. 84329d72108Sdrh ** 84429d72108Sdrh ** I am told that SUM() should raise an exception if it encounters 84529d72108Sdrh ** a integer overflow. But after pondering this, I decided that 84629d72108Sdrh ** behavior leads to brittle programs. So instead, I have coded 84729d72108Sdrh ** SUM() to revert to using floating point if it encounters an 84829d72108Sdrh ** integer overflow. The answer may not be exact, but it will be 84929d72108Sdrh ** close. If the SUM() function returns an integer, the value is 85029d72108Sdrh ** exact. If SUM() returns a floating point value, it means the 85129d72108Sdrh ** value might be approximated. 852dd5baa95Sdrh */ 8530ae8b831Sdanielk1977 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 854dd5baa95Sdrh SumCtx *p; 8553d1d95e6Sdrh int type; 8563f219f46Sdrh assert( argc==1 ); 8574f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 85829d72108Sdrh type = sqlite3_value_numeric_type(argv[0]); 8593d1d95e6Sdrh if( p && type!=SQLITE_NULL ){ 860739105c7Sdrh p->cnt++; 86129d72108Sdrh if( type==SQLITE_INTEGER ){ 8628c08e861Sdrh i64 v = sqlite3_value_int64(argv[0]); 8638c08e861Sdrh p->rSum += v; 8648c08e861Sdrh if( (p->approx|p->overflow)==0 ){ 8658c08e861Sdrh i64 iNewSum = p->iSum + v; 8668c08e861Sdrh int s1 = p->iSum >> (sizeof(i64)*8-1); 8678c08e861Sdrh int s2 = v >> (sizeof(i64)*8-1); 8688c08e861Sdrh int s3 = iNewSum >> (sizeof(i64)*8-1); 8698c08e861Sdrh p->overflow = (s1&s2&~s3) | (~s1&~s2&s3); 8708c08e861Sdrh p->iSum = iNewSum; 87129d72108Sdrh } 87229d72108Sdrh }else{ 8738c08e861Sdrh p->rSum += sqlite3_value_double(argv[0]); 87429d72108Sdrh p->approx = 1; 8753f219f46Sdrh } 876739105c7Sdrh } 877dd5baa95Sdrh } 8780ae8b831Sdanielk1977 static void sumFinalize(sqlite3_context *context){ 879dd5baa95Sdrh SumCtx *p; 880abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 881c2bd913aSdrh if( p && p->cnt>0 ){ 8828c08e861Sdrh if( p->overflow ){ 8838c08e861Sdrh sqlite3_result_error(context,"integer overflow",-1); 8848c08e861Sdrh }else if( p->approx ){ 8858c08e861Sdrh sqlite3_result_double(context, p->rSum); 886c2bd913aSdrh }else{ 8878c08e861Sdrh sqlite3_result_int64(context, p->iSum); 8883d1d95e6Sdrh } 889dd5baa95Sdrh } 890c2bd913aSdrh } 8910ae8b831Sdanielk1977 static void avgFinalize(sqlite3_context *context){ 892dd5baa95Sdrh SumCtx *p; 893abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 894739105c7Sdrh if( p && p->cnt>0 ){ 8958c08e861Sdrh sqlite3_result_double(context, p->rSum/(double)p->cnt); 896dd5baa95Sdrh } 897dd5baa95Sdrh } 898a97fdd3bSdrh static void totalFinalize(sqlite3_context *context){ 899a97fdd3bSdrh SumCtx *p; 900a97fdd3bSdrh p = sqlite3_aggregate_context(context, 0); 9018c08e861Sdrh sqlite3_result_double(context, p ? p->rSum : 0.0); 902a97fdd3bSdrh } 903dd5baa95Sdrh 904dd5baa95Sdrh /* 9050bce8354Sdrh ** The following structure keeps track of state information for the 9060bce8354Sdrh ** count() aggregate function. 9070bce8354Sdrh */ 9080bce8354Sdrh typedef struct CountCtx CountCtx; 9090bce8354Sdrh struct CountCtx { 910fc6ad39cSdrh i64 n; 9110bce8354Sdrh }; 912dd5baa95Sdrh 9130bce8354Sdrh /* 9140bce8354Sdrh ** Routines to implement the count() aggregate function. 9150bce8354Sdrh */ 9160ae8b831Sdanielk1977 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 9170bce8354Sdrh CountCtx *p; 9184f26d6c4Sdrh p = sqlite3_aggregate_context(context, sizeof(*p)); 9199c054830Sdrh if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ 9200bce8354Sdrh p->n++; 9210bce8354Sdrh } 9220bce8354Sdrh } 9230ae8b831Sdanielk1977 static void countFinalize(sqlite3_context *context){ 9240bce8354Sdrh CountCtx *p; 925abfcea25Sdrh p = sqlite3_aggregate_context(context, 0); 926fc6ad39cSdrh sqlite3_result_int64(context, p ? p->n : 0); 9270bce8354Sdrh } 9280bce8354Sdrh 9290bce8354Sdrh /* 9300bce8354Sdrh ** Routines to implement min() and max() aggregate functions. 9310bce8354Sdrh */ 9320ae8b831Sdanielk1977 static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ 93388208050Sdanielk1977 Mem *pArg = (Mem *)argv[0]; 9349eb516c0Sdrh Mem *pBest; 9359eb516c0Sdrh 9369eb516c0Sdrh if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 9379eb516c0Sdrh pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); 9383aeab9e4Sdanielk1977 if( !pBest ) return; 939268380caSdrh 94088208050Sdanielk1977 if( pBest->flags ){ 9419eb516c0Sdrh int max; 9429eb516c0Sdrh int cmp; 943dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3GetFuncCollSeq(context); 9447e18c259Sdanielk1977 /* This step function is used for both the min() and max() aggregates, 9457e18c259Sdanielk1977 ** the only difference between the two being that the sense of the 9467e18c259Sdanielk1977 ** comparison is inverted. For the max() aggregate, the 9477e18c259Sdanielk1977 ** sqlite3_user_data() function returns (void *)-1. For min() it 9487e18c259Sdanielk1977 ** returns (void *)db, where db is the sqlite3* database pointer. 9497e18c259Sdanielk1977 ** Therefore the next statement sets variable 'max' to 1 for the max() 9507e18c259Sdanielk1977 ** aggregate, or 0 for min(). 9517e18c259Sdanielk1977 */ 95288208050Sdanielk1977 max = ((sqlite3_user_data(context)==(void *)-1)?1:0); 953dc1bdc4fSdanielk1977 cmp = sqlite3MemCompare(pBest, pArg, pColl); 95488208050Sdanielk1977 if( (max && cmp<0) || (!max && cmp>0) ){ 9557e18c259Sdanielk1977 sqlite3VdbeMemCopy(pBest, pArg); 95688208050Sdanielk1977 } 9570bce8354Sdrh }else{ 9587e18c259Sdanielk1977 sqlite3VdbeMemCopy(pBest, pArg); 9590bce8354Sdrh } 9600bce8354Sdrh } 9610ae8b831Sdanielk1977 static void minMaxFinalize(sqlite3_context *context){ 96288208050Sdanielk1977 sqlite3_value *pRes; 963abfcea25Sdrh pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); 964abfcea25Sdrh if( pRes ){ 96588208050Sdanielk1977 if( pRes->flags ){ 966f4479501Sdrh sqlite3_result_value(context, pRes); 9670bce8354Sdrh } 968b20e56b4Sdanielk1977 sqlite3VdbeMemRelease(pRes); 9690bce8354Sdrh } 970abfcea25Sdrh } 971dd5baa95Sdrh 9724e5ffc5fSdrh 973d3a149efSdrh /* 974a2ed5601Sdrh ** This function registered all of the above C functions as SQL 975a2ed5601Sdrh ** functions. This should be the only routine in this file with 976a2ed5601Sdrh ** external linkage. 977dc04c583Sdrh */ 9789bb575fdSdrh void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ 9795719628aSdrh static const struct { 9800bce8354Sdrh char *zName; 981268380caSdrh signed char nArg; 982268380caSdrh u8 argType; /* 0: none. 1: db 2: (-1) */ 983d02eb1fdSdanielk1977 u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */ 984dc1bdc4fSdanielk1977 u8 needCollSeq; 9850ae8b831Sdanielk1977 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); 9860bce8354Sdrh } aFuncs[] = { 987d8123366Sdanielk1977 { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc }, 988d8123366Sdanielk1977 { "min", 0, 0, SQLITE_UTF8, 1, 0 }, 989d8123366Sdanielk1977 { "max", -1, 2, SQLITE_UTF8, 1, minmaxFunc }, 990d8123366Sdanielk1977 { "max", 0, 2, SQLITE_UTF8, 1, 0 }, 991d8123366Sdanielk1977 { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc }, 992d8123366Sdanielk1977 { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc }, 993d8123366Sdanielk1977 { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc }, 9946c62608fSdrh #ifndef SQLITE_OMIT_UTF16 995f4618891Sdanielk1977 { "substr", 3, 0, SQLITE_UTF16LE, 0, sqlite3utf16Substr }, 9966c62608fSdrh #endif 997d8123366Sdanielk1977 { "abs", 1, 0, SQLITE_UTF8, 0, absFunc }, 998d8123366Sdanielk1977 { "round", 1, 0, SQLITE_UTF8, 0, roundFunc }, 999d8123366Sdanielk1977 { "round", 2, 0, SQLITE_UTF8, 0, roundFunc }, 1000d8123366Sdanielk1977 { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc }, 1001d8123366Sdanielk1977 { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc }, 1002d8123366Sdanielk1977 { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc }, 1003d8123366Sdanielk1977 { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 }, 1004d8123366Sdanielk1977 { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 }, 1005d8123366Sdanielk1977 { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc }, 1006d8123366Sdanielk1977 { "random", -1, 0, SQLITE_UTF8, 0, randomFunc }, 100794a98365Sdrh { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc }, 1008d8123366Sdanielk1977 { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc}, 1009d8123366Sdanielk1977 { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc }, 1010d8123366Sdanielk1977 { "last_insert_rowid", 0, 1, SQLITE_UTF8, 0, last_insert_rowid }, 1011b28af71aSdanielk1977 { "changes", 0, 1, SQLITE_UTF8, 0, changes }, 1012b28af71aSdanielk1977 { "total_changes", 0, 1, SQLITE_UTF8, 0, total_changes }, 1013d24cc427Sdrh #ifdef SQLITE_SOUNDEX 1014d8123366Sdanielk1977 { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc}, 1015d24cc427Sdrh #endif 1016193a6b41Sdrh #ifdef SQLITE_TEST 1017d8123366Sdanielk1977 { "randstr", 2, 0, SQLITE_UTF8, 0, randStr }, 1018f4618891Sdanielk1977 { "test_destructor", 1, 1, SQLITE_UTF8, 0, test_destructor}, 1019d8123366Sdanielk1977 { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count}, 10203f6b0874Sdanielk1977 { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata}, 102101427a62Sdanielk1977 { "test_error", 1, 0, SQLITE_UTF8, 0, test_error}, 1022193a6b41Sdrh #endif 10230bce8354Sdrh }; 10245719628aSdrh static const struct { 10250bce8354Sdrh char *zName; 1026268380caSdrh signed char nArg; 1027268380caSdrh u8 argType; 1028dc1bdc4fSdanielk1977 u8 needCollSeq; 10290ae8b831Sdanielk1977 void (*xStep)(sqlite3_context*,int,sqlite3_value**); 10300ae8b831Sdanielk1977 void (*xFinalize)(sqlite3_context*); 10310bce8354Sdrh } aAggs[] = { 1032dc1bdc4fSdanielk1977 { "min", 1, 0, 1, minmaxStep, minMaxFinalize }, 1033dc1bdc4fSdanielk1977 { "max", 1, 2, 1, minmaxStep, minMaxFinalize }, 1034dc1bdc4fSdanielk1977 { "sum", 1, 0, 0, sumStep, sumFinalize }, 1035a97fdd3bSdrh { "total", 1, 0, 0, sumStep, totalFinalize }, 1036dc1bdc4fSdanielk1977 { "avg", 1, 0, 0, sumStep, avgFinalize }, 1037dc1bdc4fSdanielk1977 { "count", 0, 0, 0, countStep, countFinalize }, 1038dc1bdc4fSdanielk1977 { "count", 1, 0, 0, countStep, countFinalize }, 10390bce8354Sdrh }; 10400bce8354Sdrh int i; 10410bce8354Sdrh 10420bce8354Sdrh for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ 1043c572ef7fSdanielk1977 void *pArg = 0; 1044c572ef7fSdanielk1977 switch( aFuncs[i].argType ){ 1045c572ef7fSdanielk1977 case 1: pArg = db; break; 1046c572ef7fSdanielk1977 case 2: pArg = (void *)(-1); break; 1047c572ef7fSdanielk1977 } 1048771151b6Sdanielk1977 sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg, 1049f9d64d2cSdanielk1977 aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0); 1050dc1bdc4fSdanielk1977 if( aFuncs[i].needCollSeq ){ 1051dc1bdc4fSdanielk1977 FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, 1052dc1bdc4fSdanielk1977 strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0); 1053dc1bdc4fSdanielk1977 if( pFunc && aFuncs[i].needCollSeq ){ 1054dc1bdc4fSdanielk1977 pFunc->needCollSeq = 1; 1055dc1bdc4fSdanielk1977 } 1056dc1bdc4fSdanielk1977 } 10570bce8354Sdrh } 10581f01ec1bSdrh #ifndef SQLITE_OMIT_ALTERTABLE 10591f01ec1bSdrh sqlite3AlterFunctions(db); 10601f01ec1bSdrh #endif 1061198bf391Sdrh #ifndef SQLITE_OMIT_PARSER 1062f744bb56Sdanielk1977 sqlite3AttachFunctions(db); 1063198bf391Sdrh #endif 10640bce8354Sdrh for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){ 1065c572ef7fSdanielk1977 void *pArg = 0; 1066c572ef7fSdanielk1977 switch( aAggs[i].argType ){ 1067c572ef7fSdanielk1977 case 1: pArg = db; break; 1068c572ef7fSdanielk1977 case 2: pArg = (void *)(-1); break; 1069c572ef7fSdanielk1977 } 1070771151b6Sdanielk1977 sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, 1071f9d64d2cSdanielk1977 pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize); 1072dc1bdc4fSdanielk1977 if( aAggs[i].needCollSeq ){ 1073dc1bdc4fSdanielk1977 FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName, 1074d8123366Sdanielk1977 strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0); 1075dc1bdc4fSdanielk1977 if( pFunc && aAggs[i].needCollSeq ){ 1076dc1bdc4fSdanielk1977 pFunc->needCollSeq = 1; 1077dc1bdc4fSdanielk1977 } 1078dc1bdc4fSdanielk1977 } 1079268380caSdrh } 10804adee20fSdanielk1977 sqlite3RegisterDateTimeFunctions(db); 1081fd9e1f31Sdanielk1977 #ifdef SQLITE_SSE 1082fd9e1f31Sdanielk1977 sqlite3SseFunctions(db); 1083fd9e1f31Sdanielk1977 #endif 108455ef4d97Sdrh #ifdef SQLITE_CASE_SENSITIVE_LIKE 108555ef4d97Sdrh sqlite3RegisterLikeFunctions(db, 1); 108655ef4d97Sdrh #else 108755ef4d97Sdrh sqlite3RegisterLikeFunctions(db, 0); 108855ef4d97Sdrh #endif 108955ef4d97Sdrh } 109055ef4d97Sdrh 109155ef4d97Sdrh /* 109255ef4d97Sdrh ** Set the LIKEOPT flag on the 2-argument function with the given name. 109355ef4d97Sdrh */ 1094d64fe2f3Sdrh static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){ 109555ef4d97Sdrh FuncDef *pDef; 109655ef4d97Sdrh pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0); 109755ef4d97Sdrh if( pDef ){ 1098d64fe2f3Sdrh pDef->flags = flagVal; 109955ef4d97Sdrh } 110055ef4d97Sdrh } 110155ef4d97Sdrh 110255ef4d97Sdrh /* 110355ef4d97Sdrh ** Register the built-in LIKE and GLOB functions. The caseSensitive 110455ef4d97Sdrh ** parameter determines whether or not the LIKE operator is case 110555ef4d97Sdrh ** sensitive. GLOB is always case sensitive. 110655ef4d97Sdrh */ 110755ef4d97Sdrh void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ 110855ef4d97Sdrh struct compareInfo *pInfo; 110955ef4d97Sdrh if( caseSensitive ){ 111055ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoAlt; 111155ef4d97Sdrh }else{ 111255ef4d97Sdrh pInfo = (struct compareInfo*)&likeInfoNorm; 111355ef4d97Sdrh } 1114771151b6Sdanielk1977 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0); 1115771151b6Sdanielk1977 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0); 1116771151b6Sdanielk1977 sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 111755ef4d97Sdrh (struct compareInfo*)&globInfo, likeFunc, 0,0); 1118d64fe2f3Sdrh setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); 1119d64fe2f3Sdrh setLikeOptFlag(db, "like", 1120d64fe2f3Sdrh caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); 112155ef4d97Sdrh } 112255ef4d97Sdrh 112355ef4d97Sdrh /* 112455ef4d97Sdrh ** pExpr points to an expression which implements a function. If 112555ef4d97Sdrh ** it is appropriate to apply the LIKE optimization to that function 112655ef4d97Sdrh ** then set aWc[0] through aWc[2] to the wildcard characters and 112755ef4d97Sdrh ** return TRUE. If the function is not a LIKE-style function then 112855ef4d97Sdrh ** return FALSE. 112955ef4d97Sdrh */ 1130d64fe2f3Sdrh int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ 113155ef4d97Sdrh FuncDef *pDef; 113255ef4d97Sdrh if( pExpr->op!=TK_FUNCTION ){ 113355ef4d97Sdrh return 0; 113455ef4d97Sdrh } 113555ef4d97Sdrh if( pExpr->pList->nExpr!=2 ){ 113655ef4d97Sdrh return 0; 113755ef4d97Sdrh } 11382646da7eSdrh pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2, 113955ef4d97Sdrh SQLITE_UTF8, 0); 1140d64fe2f3Sdrh if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){ 114155ef4d97Sdrh return 0; 114255ef4d97Sdrh } 114355ef4d97Sdrh 114455ef4d97Sdrh /* The memcpy() statement assumes that the wildcard characters are 114555ef4d97Sdrh ** the first three statements in the compareInfo structure. The 114655ef4d97Sdrh ** asserts() that follow verify that assumption 114755ef4d97Sdrh */ 114855ef4d97Sdrh memcpy(aWc, pDef->pUserData, 3); 114955ef4d97Sdrh assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); 115055ef4d97Sdrh assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); 115155ef4d97Sdrh assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); 1152d64fe2f3Sdrh *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0; 115355ef4d97Sdrh return 1; 1154dc04c583Sdrh } 1155