ds@1952: /* ds@1952: ** 2002 February 23 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This file contains the C functions that implement various SQL ds@1952: ** functions of SQLite. ds@1952: ** ds@1952: ** There is only one exported symbol in this file - the function ds@1952: ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ds@1952: ** All other code has file scope. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: #include ds@1952: #include ds@1952: #include "vdbeInt.h" ds@1952: ds@1952: ds@1952: /* ds@1952: ** Return the collating function associated with a function. ds@1952: */ ds@1952: static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ ds@1952: return context->pColl; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the non-aggregate min() and max() functions ds@1952: */ ds@1952: static void minmaxFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: int i; ds@1952: int mask; /* 0 for min() or 0xffffffff for max() */ ds@1952: int iBest; ds@1952: CollSeq *pColl; ds@1952: ds@1952: if( argc==0 ) return; ds@1952: mask = sqlite3_user_data(context)==0 ? 0 : -1; ds@1952: pColl = sqlite3GetFuncCollSeq(context); ds@1952: assert( pColl ); ds@1952: assert( mask==-1 || mask==0 ); ds@1952: iBest = 0; ds@1952: if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; ds@1952: for(i=1; i=0 ){ ds@1952: iBest = i; ds@1952: } ds@1952: } ds@1952: sqlite3_result_value(context, argv[iBest]); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Return the type of the argument. ds@1952: */ ds@1952: static void typeofFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: const char *z = 0; ds@1952: switch( sqlite3_value_type(argv[0]) ){ ds@1952: case SQLITE_NULL: z = "null"; break; ds@1952: case SQLITE_INTEGER: z = "integer"; break; ds@1952: case SQLITE_TEXT: z = "text"; break; ds@1952: case SQLITE_FLOAT: z = "real"; break; ds@1952: case SQLITE_BLOB: z = "blob"; break; ds@1952: } ds@1952: sqlite3_result_text(context, z, -1, SQLITE_STATIC); ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Implementation of the length() function ds@1952: */ ds@1952: static void lengthFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: int len; ds@1952: ds@1952: assert( argc==1 ); ds@1952: switch( sqlite3_value_type(argv[0]) ){ ds@1952: case SQLITE_BLOB: ds@1952: case SQLITE_INTEGER: ds@1952: case SQLITE_FLOAT: { ds@1952: sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); ds@1952: break; ds@1952: } ds@1952: case SQLITE_TEXT: { ds@1952: const unsigned char *z = sqlite3_value_text(argv[0]); ds@1952: if( z==0 ) return; ds@1952: len = 0; ds@1952: while( *z ){ ds@1952: len++; ds@1952: SQLITE_SKIP_UTF8(z); ds@1952: } ds@1952: sqlite3_result_int(context, len); ds@1952: break; ds@1952: } ds@1952: default: { ds@1952: sqlite3_result_null(context); ds@1952: break; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the abs() function ds@1952: */ ds@1952: static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ ds@1952: assert( argc==1 ); ds@1952: switch( sqlite3_value_type(argv[0]) ){ ds@1952: case SQLITE_INTEGER: { ds@1952: i64 iVal = sqlite3_value_int64(argv[0]); ds@1952: if( iVal<0 ){ ds@1952: if( (iVal<<1)==0 ){ ds@1952: sqlite3_result_error(context, "integer overflow", -1); ds@1952: return; ds@1952: } ds@1952: iVal = -iVal; ds@1952: } ds@1952: sqlite3_result_int64(context, iVal); ds@1952: break; ds@1952: } ds@1952: case SQLITE_NULL: { ds@1952: sqlite3_result_null(context); ds@1952: break; ds@1952: } ds@1952: default: { ds@1952: double rVal = sqlite3_value_double(argv[0]); ds@1952: if( rVal<0 ) rVal = -rVal; ds@1952: sqlite3_result_double(context, rVal); ds@1952: break; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the substr() function. ds@1952: ** ds@1952: ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. ds@1952: ** p1 is 1-indexed. So substr(x,1,1) returns the first character ds@1952: ** of x. If x is text, then we actually count UTF-8 characters. ds@1952: ** If x is a blob, then we count bytes. ds@1952: ** ds@1952: ** If p1 is negative, then we begin abs(p1) from the end of x[]. ds@1952: */ ds@1952: static void substrFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: const unsigned char *z; ds@1952: const unsigned char *z2; ds@1952: int len; ds@1952: int p0type; ds@1952: i64 p1, p2; ds@1952: ds@1952: assert( argc==3 ); ds@1952: p0type = sqlite3_value_type(argv[0]); ds@1952: if( p0type==SQLITE_BLOB ){ ds@1952: len = sqlite3_value_bytes(argv[0]); ds@1952: z = sqlite3_value_blob(argv[0]); ds@1952: if( z==0 ) return; ds@1952: assert( len==sqlite3_value_bytes(argv[0]) ); ds@1952: }else{ ds@1952: z = sqlite3_value_text(argv[0]); ds@1952: if( z==0 ) return; ds@1952: len = 0; ds@1952: for(z2=z; *z2; len++){ ds@1952: SQLITE_SKIP_UTF8(z2); ds@1952: } ds@1952: } ds@1952: p1 = sqlite3_value_int(argv[1]); ds@1952: p2 = sqlite3_value_int(argv[2]); ds@1952: if( p1<0 ){ ds@1952: p1 += len; ds@1952: if( p1<0 ){ ds@1952: p2 += p1; ds@1952: p1 = 0; ds@1952: } ds@1952: }else if( p1>0 ){ ds@1952: p1--; ds@1952: } ds@1952: if( p1+p2>len ){ ds@1952: p2 = len-p1; ds@1952: } ds@1952: if( p0type!=SQLITE_BLOB ){ ds@1952: while( *z && p1 ){ ds@1952: SQLITE_SKIP_UTF8(z); ds@1952: p1--; ds@1952: } ds@1952: for(z2=z; *z2 && p2; p2--){ ds@1952: SQLITE_SKIP_UTF8(z2); ds@1952: } ds@1952: sqlite3_result_text(context, (char*)z, z2-z, SQLITE_TRANSIENT); ds@1952: }else{ ds@1952: if( p2<0 ) p2 = 0; ds@1952: sqlite3_result_blob(context, (char*)&z[p1], p2, SQLITE_TRANSIENT); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the round() function ds@1952: */ ds@1952: static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ ds@1952: int n = 0; ds@1952: double r; ds@1952: char zBuf[500]; /* larger than the %f representation of the largest double */ ds@1952: assert( argc==1 || argc==2 ); ds@1952: if( argc==2 ){ ds@1952: if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; ds@1952: n = sqlite3_value_int(argv[1]); ds@1952: if( n>30 ) n = 30; ds@1952: if( n<0 ) n = 0; ds@1952: } ds@1952: if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; ds@1952: r = sqlite3_value_double(argv[0]); ds@1952: sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r); ds@1952: sqlite3AtoF(zBuf, &r); ds@1952: sqlite3_result_double(context, r); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Allocate nByte bytes of space using sqlite3_malloc(). If the ds@1952: ** allocation fails, call sqlite3_result_error_nomem() to notify ds@1952: ** the database handle that malloc() has failed. ds@1952: */ ds@1952: static void *contextMalloc(sqlite3_context *context, int nByte){ ds@1952: char *z = sqlite3_malloc(nByte); ds@1952: if( !z && nByte>0 ){ ds@1952: sqlite3_result_error_nomem(context); ds@1952: } ds@1952: return z; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the upper() and lower() SQL functions. ds@1952: */ ds@1952: static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ ds@1952: char *z1; ds@1952: const char *z2; ds@1952: int i, n; ds@1952: if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; ds@1952: z2 = (char*)sqlite3_value_text(argv[0]); ds@1952: n = sqlite3_value_bytes(argv[0]); ds@1952: /* Verify that the call to _bytes() does not invalidate the _text() pointer */ ds@1952: assert( z2==(char*)sqlite3_value_text(argv[0]) ); ds@1952: if( z2 ){ ds@1952: z1 = contextMalloc(context, n+1); ds@1952: if( z1 ){ ds@1952: memcpy(z1, z2, n+1); ds@1952: for(i=0; z1[i]; i++){ ds@1952: z1[i] = toupper(z1[i]); ds@1952: } ds@1952: sqlite3_result_text(context, z1, -1, sqlite3_free); ds@1952: } ds@1952: } ds@1952: } ds@1952: static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ ds@1952: char *z1; ds@1952: const char *z2; ds@1952: int i, n; ds@1952: if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; ds@1952: z2 = (char*)sqlite3_value_text(argv[0]); ds@1952: n = sqlite3_value_bytes(argv[0]); ds@1952: /* Verify that the call to _bytes() does not invalidate the _text() pointer */ ds@1952: assert( z2==(char*)sqlite3_value_text(argv[0]) ); ds@1952: if( z2 ){ ds@1952: z1 = contextMalloc(context, n+1); ds@1952: if( z1 ){ ds@1952: memcpy(z1, z2, n+1); ds@1952: for(i=0; z1[i]; i++){ ds@1952: z1[i] = tolower(z1[i]); ds@1952: } ds@1952: sqlite3_result_text(context, z1, -1, sqlite3_free); ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the IFNULL(), NVL(), and COALESCE() functions. ds@1952: ** All three do the same thing. They return the first non-NULL ds@1952: ** argument. ds@1952: */ ds@1952: static void ifnullFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: int i; ds@1952: for(i=0; iSQLITE_MAX_LENGTH ){ ds@1952: sqlite3_result_error_toobig(context); ds@1952: return; ds@1952: } ds@1952: p = contextMalloc(context, n); ds@1952: if( p ){ ds@1952: sqlite3Randomness(n, p); ds@1952: sqlite3_result_blob(context, (char*)p, n, sqlite3_free); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the last_insert_rowid() SQL function. The return ds@1952: ** value is the same as the sqlite3_last_insert_rowid() API function. ds@1952: */ ds@1952: static void last_insert_rowid( ds@1952: sqlite3_context *context, ds@1952: int arg, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: sqlite3 *db = sqlite3_user_data(context); ds@1952: sqlite3_result_int64(context, sqlite3_last_insert_rowid(db)); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the changes() SQL function. The return value is the ds@1952: ** same as the sqlite3_changes() API function. ds@1952: */ ds@1952: static void changes( ds@1952: sqlite3_context *context, ds@1952: int arg, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: sqlite3 *db = sqlite3_user_data(context); ds@1952: sqlite3_result_int(context, sqlite3_changes(db)); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the total_changes() SQL function. The return value is ds@1952: ** the same as the sqlite3_total_changes() API function. ds@1952: */ ds@1952: static void total_changes( ds@1952: sqlite3_context *context, ds@1952: int arg, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: sqlite3 *db = sqlite3_user_data(context); ds@1952: sqlite3_result_int(context, sqlite3_total_changes(db)); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** A structure defining how to do GLOB-style comparisons. ds@1952: */ ds@1952: struct compareInfo { ds@1952: u8 matchAll; ds@1952: u8 matchOne; ds@1952: u8 matchSet; ds@1952: u8 noCase; ds@1952: }; ds@1952: ds@1952: static const struct compareInfo globInfo = { '*', '?', '[', 0 }; ds@1952: /* The correct SQL-92 behavior is for the LIKE operator to ignore ds@1952: ** case. Thus 'a' LIKE 'A' would be true. */ ds@1952: static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 }; ds@1952: /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator ds@1952: ** is case sensitive causing 'a' LIKE 'A' to be false */ ds@1952: static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 }; ds@1952: ds@1952: /* ds@1952: ** Compare two UTF-8 strings for equality where the first string can ds@1952: ** potentially be a "glob" expression. Return true (1) if they ds@1952: ** are the same and false (0) if they are different. ds@1952: ** ds@1952: ** Globbing rules: ds@1952: ** ds@1952: ** '*' Matches any sequence of zero or more characters. ds@1952: ** ds@1952: ** '?' Matches exactly one character. ds@1952: ** ds@1952: ** [...] Matches one character from the enclosed list of ds@1952: ** characters. ds@1952: ** ds@1952: ** [^...] Matches one character not in the enclosed list. ds@1952: ** ds@1952: ** With the [...] and [^...] matching, a ']' character can be included ds@1952: ** in the list by making it the first character after '[' or '^'. A ds@1952: ** range of characters can be specified using '-'. Example: ds@1952: ** "[a-z]" matches any single lower-case letter. To match a '-', make ds@1952: ** it the last character in the list. ds@1952: ** ds@1952: ** This routine is usually quick, but can be N**2 in the worst case. ds@1952: ** ds@1952: ** Hints: to match '*' or '?', put them in "[]". Like this: ds@1952: ** ds@1952: ** abc[*]xyz Matches "abc*xyz" only ds@1952: */ ds@1952: static int patternCompare( ds@1952: const u8 *zPattern, /* The glob pattern */ ds@1952: const u8 *zString, /* The string to compare against the glob */ ds@1952: const struct compareInfo *pInfo, /* Information about how to do the compare */ ds@1952: const int esc /* The escape character */ ds@1952: ){ ds@1952: int c, c2; ds@1952: int invert; ds@1952: int seen; ds@1952: u8 matchOne = pInfo->matchOne; ds@1952: u8 matchAll = pInfo->matchAll; ds@1952: u8 matchSet = pInfo->matchSet; ds@1952: u8 noCase = pInfo->noCase; ds@1952: int prevEscape = 0; /* True if the previous character was 'escape' */ ds@1952: ds@1952: while( (c = sqlite3Utf8Read(zPattern,0,&zPattern))!=0 ){ ds@1952: if( !prevEscape && c==matchAll ){ ds@1952: while( (c=sqlite3Utf8Read(zPattern,0,&zPattern)) == matchAll ds@1952: || c == matchOne ){ ds@1952: if( c==matchOne && sqlite3Utf8Read(zString, 0, &zString)==0 ){ ds@1952: return 0; ds@1952: } ds@1952: } ds@1952: if( c==0 ){ ds@1952: return 1; ds@1952: }else if( c==esc ){ ds@1952: c = sqlite3Utf8Read(zPattern, 0, &zPattern); ds@1952: if( c==0 ){ ds@1952: return 0; ds@1952: } ds@1952: }else if( c==matchSet ){ ds@1952: assert( esc==0 ); /* This is GLOB, not LIKE */ ds@1952: assert( matchSet<0x80 ); /* '[' is a single-byte character */ ds@1952: while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ ds@1952: SQLITE_SKIP_UTF8(zString); ds@1952: } ds@1952: return *zString!=0; ds@1952: } ds@1952: while( (c2 = sqlite3Utf8Read(zString,0,&zString))!=0 ){ ds@1952: if( noCase ){ ds@1952: c2 = c2<0x80 ? sqlite3UpperToLower[c2] : c2; ds@1952: c = c<0x80 ? sqlite3UpperToLower[c] : c; ds@1952: while( c2 != 0 && c2 != c ){ ds@1952: c2 = sqlite3Utf8Read(zString, 0, &zString); ds@1952: if( c2<0x80 ) c2 = sqlite3UpperToLower[c2]; ds@1952: } ds@1952: }else{ ds@1952: while( c2 != 0 && c2 != c ){ ds@1952: c2 = sqlite3Utf8Read(zString, 0, &zString); ds@1952: } ds@1952: } ds@1952: if( c2==0 ) return 0; ds@1952: if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; ds@1952: } ds@1952: return 0; ds@1952: }else if( !prevEscape && c==matchOne ){ ds@1952: if( sqlite3Utf8Read(zString, 0, &zString)==0 ){ ds@1952: return 0; ds@1952: } ds@1952: }else if( c==matchSet ){ ds@1952: int prior_c = 0; ds@1952: assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ ds@1952: seen = 0; ds@1952: invert = 0; ds@1952: c = sqlite3Utf8Read(zString, 0, &zString); ds@1952: if( c==0 ) return 0; ds@1952: c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); ds@1952: if( c2=='^' ){ ds@1952: invert = 1; ds@1952: c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); ds@1952: } ds@1952: if( c2==']' ){ ds@1952: if( c==']' ) seen = 1; ds@1952: c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); ds@1952: } ds@1952: while( c2 && c2!=']' ){ ds@1952: if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ ds@1952: c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); ds@1952: if( c>=prior_c && c<=c2 ) seen = 1; ds@1952: prior_c = 0; ds@1952: }else{ ds@1952: if( c==c2 ){ ds@1952: seen = 1; ds@1952: } ds@1952: prior_c = c2; ds@1952: } ds@1952: c2 = sqlite3Utf8Read(zPattern, 0, &zPattern); ds@1952: } ds@1952: if( c2==0 || (seen ^ invert)==0 ){ ds@1952: return 0; ds@1952: } ds@1952: }else if( esc==c && !prevEscape ){ ds@1952: prevEscape = 1; ds@1952: }else{ ds@1952: c2 = sqlite3Utf8Read(zString, 0, &zString); ds@1952: if( noCase ){ ds@1952: c = c<0x80 ? sqlite3UpperToLower[c] : c; ds@1952: c2 = c2<0x80 ? sqlite3UpperToLower[c2] : c2; ds@1952: } ds@1952: if( c!=c2 ){ ds@1952: return 0; ds@1952: } ds@1952: prevEscape = 0; ds@1952: } ds@1952: } ds@1952: return *zString==0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Count the number of times that the LIKE operator (or GLOB which is ds@1952: ** just a variation of LIKE) gets called. This is used for testing ds@1952: ** only. ds@1952: */ ds@1952: #ifdef SQLITE_TEST ds@1952: int sqlite3_like_count = 0; ds@1952: #endif ds@1952: ds@1952: ds@1952: /* ds@1952: ** Implementation of the like() SQL function. This function implements ds@1952: ** the build-in LIKE operator. The first argument to the function is the ds@1952: ** pattern and the second argument is the string. So, the SQL statements: ds@1952: ** ds@1952: ** A LIKE B ds@1952: ** ds@1952: ** is implemented as like(B,A). ds@1952: ** ds@1952: ** This same function (with a different compareInfo structure) computes ds@1952: ** the GLOB operator. ds@1952: */ ds@1952: static void likeFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: const unsigned char *zA, *zB; ds@1952: int escape = 0; ds@1952: ds@1952: zB = sqlite3_value_text(argv[0]); ds@1952: zA = sqlite3_value_text(argv[1]); ds@1952: ds@1952: /* Limit the length of the LIKE or GLOB pattern to avoid problems ds@1952: ** of deep recursion and N*N behavior in patternCompare(). ds@1952: */ ds@1952: if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){ ds@1952: sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); ds@1952: return; ds@1952: } ds@1952: assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ ds@1952: ds@1952: if( argc==3 ){ ds@1952: /* The escape character string must consist of a single UTF-8 character. ds@1952: ** Otherwise, return an error. ds@1952: */ ds@1952: const unsigned char *zEsc = sqlite3_value_text(argv[2]); ds@1952: if( zEsc==0 ) return; ds@1952: if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ ds@1952: sqlite3_result_error(context, ds@1952: "ESCAPE expression must be a single character", -1); ds@1952: return; ds@1952: } ds@1952: escape = sqlite3Utf8Read(zEsc, 0, &zEsc); ds@1952: } ds@1952: if( zA && zB ){ ds@1952: struct compareInfo *pInfo = sqlite3_user_data(context); ds@1952: #ifdef SQLITE_TEST ds@1952: sqlite3_like_count++; ds@1952: #endif ds@1952: ds@1952: sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the NULLIF(x,y) function. The result is the first ds@1952: ** argument if the arguments are different. The result is NULL if the ds@1952: ** arguments are equal to each other. ds@1952: */ ds@1952: static void nullifFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: CollSeq *pColl = sqlite3GetFuncCollSeq(context); ds@1952: if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ ds@1952: sqlite3_result_value(context, argv[0]); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the VERSION(*) function. The result is the version ds@1952: ** of the SQLite library that is running. ds@1952: */ ds@1952: static void versionFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC); ds@1952: } ds@1952: ds@1952: /* Array for converting from half-bytes (nybbles) into ASCII hex ds@1952: ** digits. */ ds@1952: static const char hexdigits[] = { ds@1952: '0', '1', '2', '3', '4', '5', '6', '7', ds@1952: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ds@1952: }; ds@1952: ds@1952: /* ds@1952: ** EXPERIMENTAL - This is not an official function. The interface may ds@1952: ** change. This function may disappear. Do not write code that depends ds@1952: ** on this function. ds@1952: ** ds@1952: ** Implementation of the QUOTE() function. This function takes a single ds@1952: ** argument. If the argument is numeric, the return value is the same as ds@1952: ** the argument. If the argument is NULL, the return value is the string ds@1952: ** "NULL". Otherwise, the argument is enclosed in single quotes with ds@1952: ** single-quote escapes. ds@1952: */ ds@1952: static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ ds@1952: if( argc<1 ) return; ds@1952: switch( sqlite3_value_type(argv[0]) ){ ds@1952: case SQLITE_NULL: { ds@1952: sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); ds@1952: break; ds@1952: } ds@1952: case SQLITE_INTEGER: ds@1952: case SQLITE_FLOAT: { ds@1952: sqlite3_result_value(context, argv[0]); ds@1952: break; ds@1952: } ds@1952: case SQLITE_BLOB: { ds@1952: char *zText = 0; ds@1952: char const *zBlob = sqlite3_value_blob(argv[0]); ds@1952: int nBlob = sqlite3_value_bytes(argv[0]); ds@1952: assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ ds@1952: ds@1952: if( 2*nBlob+4>SQLITE_MAX_LENGTH ){ ds@1952: sqlite3_result_error_toobig(context); ds@1952: return; ds@1952: } ds@1952: zText = (char *)contextMalloc(context, (2*nBlob)+4); ds@1952: if( zText ){ ds@1952: int i; ds@1952: for(i=0; i>4)&0x0F]; ds@1952: zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; ds@1952: } ds@1952: zText[(nBlob*2)+2] = '\''; ds@1952: zText[(nBlob*2)+3] = '\0'; ds@1952: zText[0] = 'X'; ds@1952: zText[1] = '\''; ds@1952: sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); ds@1952: sqlite3_free(zText); ds@1952: } ds@1952: break; ds@1952: } ds@1952: case SQLITE_TEXT: { ds@1952: int i,j; ds@1952: u64 n; ds@1952: const unsigned char *zArg = sqlite3_value_text(argv[0]); ds@1952: char *z; ds@1952: ds@1952: if( zArg==0 ) return; ds@1952: for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } ds@1952: if( i+n+3>SQLITE_MAX_LENGTH ){ ds@1952: sqlite3_result_error_toobig(context); ds@1952: return; ds@1952: } ds@1952: z = contextMalloc(context, i+n+3); ds@1952: if( z ){ ds@1952: z[0] = '\''; ds@1952: for(i=0, j=1; zArg[i]; i++){ ds@1952: z[j++] = zArg[i]; ds@1952: if( zArg[i]=='\'' ){ ds@1952: z[j++] = '\''; ds@1952: } ds@1952: } ds@1952: z[j++] = '\''; ds@1952: z[j] = 0; ds@1952: sqlite3_result_text(context, z, j, sqlite3_free); ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The hex() function. Interpret the argument as a blob. Return ds@1952: ** a hexadecimal rendering as text. ds@1952: */ ds@1952: static void hexFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: int i, n; ds@1952: const unsigned char *pBlob; ds@1952: char *zHex, *z; ds@1952: assert( argc==1 ); ds@1952: pBlob = sqlite3_value_blob(argv[0]); ds@1952: n = sqlite3_value_bytes(argv[0]); ds@1952: if( n*2+1>SQLITE_MAX_LENGTH ){ ds@1952: sqlite3_result_error_toobig(context); ds@1952: return; ds@1952: } ds@1952: assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ ds@1952: z = zHex = contextMalloc(context, n*2 + 1); ds@1952: if( zHex ){ ds@1952: for(i=0; i>4)&0xf]; ds@1952: *(z++) = hexdigits[c&0xf]; ds@1952: } ds@1952: *z = 0; ds@1952: sqlite3_result_text(context, zHex, n*2, sqlite3_free); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The zeroblob(N) function returns a zero-filled blob of size N bytes. ds@1952: */ ds@1952: static void zeroblobFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: i64 n; ds@1952: assert( argc==1 ); ds@1952: n = sqlite3_value_int64(argv[0]); ds@1952: if( n>SQLITE_MAX_LENGTH ){ ds@1952: sqlite3_result_error_toobig(context); ds@1952: }else{ ds@1952: sqlite3_result_zeroblob(context, n); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The replace() function. Three arguments are all strings: call ds@1952: ** them A, B, and C. The result is also a string which is derived ds@1952: ** from A by replacing every occurance of B with C. The match ds@1952: ** must be exact. Collating sequences are not used. ds@1952: */ ds@1952: static void replaceFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: const unsigned char *zStr; /* The input string A */ ds@1952: const unsigned char *zPattern; /* The pattern string B */ ds@1952: const unsigned char *zRep; /* The replacement string C */ ds@1952: unsigned char *zOut; /* The output */ ds@1952: int nStr; /* Size of zStr */ ds@1952: int nPattern; /* Size of zPattern */ ds@1952: int nRep; /* Size of zRep */ ds@1952: i64 nOut; /* Maximum size of zOut */ ds@1952: int loopLimit; /* Last zStr[] that might match zPattern[] */ ds@1952: int i, j; /* Loop counters */ ds@1952: ds@1952: assert( argc==3 ); ds@1952: zStr = sqlite3_value_text(argv[0]); ds@1952: if( zStr==0 ) return; ds@1952: nStr = sqlite3_value_bytes(argv[0]); ds@1952: assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ ds@1952: zPattern = sqlite3_value_text(argv[1]); ds@1952: if( zPattern==0 || zPattern[0]==0 ) return; ds@1952: nPattern = sqlite3_value_bytes(argv[1]); ds@1952: assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ ds@1952: zRep = sqlite3_value_text(argv[2]); ds@1952: if( zRep==0 ) return; ds@1952: nRep = sqlite3_value_bytes(argv[2]); ds@1952: assert( zRep==sqlite3_value_text(argv[2]) ); ds@1952: nOut = nStr + 1; ds@1952: assert( nOut=SQLITE_MAX_LENGTH ){ ds@1952: sqlite3_result_error_toobig(context); ds@1952: sqlite3_free(zOut); ds@1952: return; ds@1952: } ds@1952: zOld = zOut; ds@1952: zOut = sqlite3_realloc(zOut, (int)nOut); ds@1952: if( zOut==0 ){ ds@1952: sqlite3_result_error_nomem(context); ds@1952: sqlite3_free(zOld); ds@1952: return; ds@1952: } ds@1952: memcpy(&zOut[j], zRep, nRep); ds@1952: j += nRep; ds@1952: i += nPattern-1; ds@1952: } ds@1952: } ds@1952: assert( j+nStr-i+1==nOut ); ds@1952: memcpy(&zOut[j], &zStr[i], nStr-i); ds@1952: j += nStr - i; ds@1952: assert( j<=nOut ); ds@1952: zOut[j] = 0; ds@1952: sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. ds@1952: ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. ds@1952: */ ds@1952: static void trimFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: const unsigned char *zIn; /* Input string */ ds@1952: const unsigned char *zCharSet; /* Set of characters to trim */ ds@1952: int nIn; /* Number of bytes in input */ ds@1952: int flags; /* 1: trimleft 2: trimright 3: trim */ ds@1952: int i; /* Loop counter */ ds@1952: unsigned char *aLen; /* Length of each character in zCharSet */ ds@1952: const unsigned char **azChar; /* Individual characters in zCharSet */ ds@1952: int nChar; /* Number of characters in zCharSet */ ds@1952: ds@1952: if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ ds@1952: return; ds@1952: } ds@1952: zIn = sqlite3_value_text(argv[0]); ds@1952: if( zIn==0 ) return; ds@1952: nIn = sqlite3_value_bytes(argv[0]); ds@1952: assert( zIn==sqlite3_value_text(argv[0]) ); ds@1952: if( argc==1 ){ ds@1952: static const unsigned char lenOne[] = { 1 }; ds@1952: static const unsigned char *azOne[] = { (u8*)" " }; ds@1952: nChar = 1; ds@1952: aLen = (u8*)lenOne; ds@1952: azChar = azOne; ds@1952: zCharSet = 0; ds@1952: }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ ds@1952: return; ds@1952: }else{ ds@1952: const unsigned char *z; ds@1952: for(z=zCharSet, nChar=0; *z; nChar++){ ds@1952: SQLITE_SKIP_UTF8(z); ds@1952: } ds@1952: if( nChar>0 ){ ds@1952: azChar = contextMalloc(context, nChar*(sizeof(char*)+1)); ds@1952: if( azChar==0 ){ ds@1952: return; ds@1952: } ds@1952: aLen = (unsigned char*)&azChar[nChar]; ds@1952: for(z=zCharSet, nChar=0; *z; nChar++){ ds@1952: azChar[nChar] = z; ds@1952: SQLITE_SKIP_UTF8(z); ds@1952: aLen[nChar] = z - azChar[nChar]; ds@1952: } ds@1952: } ds@1952: } ds@1952: if( nChar>0 ){ ds@1952: flags = (int)sqlite3_user_data(context); ds@1952: if( flags & 1 ){ ds@1952: while( nIn>0 ){ ds@1952: int len; ds@1952: for(i=0; i=nChar ) break; ds@1952: zIn += len; ds@1952: nIn -= len; ds@1952: } ds@1952: } ds@1952: if( flags & 2 ){ ds@1952: while( nIn>0 ){ ds@1952: int len; ds@1952: for(i=0; i=nChar ) break; ds@1952: nIn -= len; ds@1952: } ds@1952: } ds@1952: if( zCharSet ){ ds@1952: sqlite3_free(azChar); ds@1952: } ds@1952: } ds@1952: sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); ds@1952: } ds@1952: ds@1952: #ifdef SQLITE_SOUNDEX ds@1952: /* ds@1952: ** Compute the soundex encoding of a word. ds@1952: */ ds@1952: static void soundexFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: char zResult[8]; ds@1952: const u8 *zIn; ds@1952: int i, j; ds@1952: static const unsigned char iCode[] = { ds@1952: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ds@1952: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ds@1952: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ds@1952: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ds@1952: 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, ds@1952: 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, ds@1952: 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, ds@1952: 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, ds@1952: }; ds@1952: assert( argc==1 ); ds@1952: zIn = (u8*)sqlite3_value_text(argv[0]); ds@1952: if( zIn==0 ) zIn = (u8*)""; ds@1952: for(i=0; zIn[i] && !isalpha(zIn[i]); i++){} ds@1952: if( zIn[i] ){ ds@1952: u8 prevcode = iCode[zIn[i]&0x7f]; ds@1952: zResult[0] = toupper(zIn[i]); ds@1952: for(j=1; j<4 && zIn[i]; i++){ ds@1952: int code = iCode[zIn[i]&0x7f]; ds@1952: if( code>0 ){ ds@1952: if( code!=prevcode ){ ds@1952: prevcode = code; ds@1952: zResult[j++] = code + '0'; ds@1952: } ds@1952: }else{ ds@1952: prevcode = 0; ds@1952: } ds@1952: } ds@1952: while( j<4 ){ ds@1952: zResult[j++] = '0'; ds@1952: } ds@1952: zResult[j] = 0; ds@1952: sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); ds@1952: }else{ ds@1952: sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); ds@1952: } ds@1952: } ds@1952: #endif ds@1952: ds@1952: #ifndef SQLITE_OMIT_LOAD_EXTENSION ds@1952: /* ds@1952: ** A function that loads a shared-library extension then returns NULL. ds@1952: */ ds@1952: static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ ds@1952: const char *zFile = (const char *)sqlite3_value_text(argv[0]); ds@1952: const char *zProc; ds@1952: sqlite3 *db = sqlite3_user_data(context); ds@1952: char *zErrMsg = 0; ds@1952: ds@1952: if( argc==2 ){ ds@1952: zProc = (const char *)sqlite3_value_text(argv[1]); ds@1952: }else{ ds@1952: zProc = 0; ds@1952: } ds@1952: if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ ds@1952: sqlite3_result_error(context, zErrMsg, -1); ds@1952: sqlite3_free(zErrMsg); ds@1952: } ds@1952: } ds@1952: #endif ds@1952: ds@1952: #ifdef SQLITE_TEST ds@1952: /* ds@1952: ** This function generates a string of random characters. Used for ds@1952: ** generating test data. ds@1952: */ ds@1952: static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ ds@1952: static const unsigned char zSrc[] = ds@1952: "abcdefghijklmnopqrstuvwxyz" ds@1952: "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ds@1952: "0123456789" ds@1952: ".-!,:*^+=_|?/<> "; ds@1952: int iMin, iMax, n, r, i; ds@1952: unsigned char zBuf[1000]; ds@1952: ds@1952: /* It used to be possible to call randstr() with any number of arguments, ds@1952: ** but now it is registered with SQLite as requiring exactly 2. ds@1952: */ ds@1952: assert(argc==2); ds@1952: ds@1952: iMin = sqlite3_value_int(argv[0]); ds@1952: if( iMin<0 ) iMin = 0; ds@1952: if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; ds@1952: iMax = sqlite3_value_int(argv[1]); ds@1952: if( iMax=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; ds@1952: n = iMin; ds@1952: if( iMax>iMin ){ ds@1952: sqlite3Randomness(sizeof(r), &r); ds@1952: r &= 0x7fffffff; ds@1952: n += r%(iMax + 1 - iMin); ds@1952: } ds@1952: assert( ncnt++; ds@1952: if( type==SQLITE_INTEGER ){ ds@1952: i64 v = sqlite3_value_int64(argv[0]); ds@1952: p->rSum += v; ds@1952: if( (p->approx|p->overflow)==0 ){ ds@1952: i64 iNewSum = p->iSum + v; ds@1952: int s1 = p->iSum >> (sizeof(i64)*8-1); ds@1952: int s2 = v >> (sizeof(i64)*8-1); ds@1952: int s3 = iNewSum >> (sizeof(i64)*8-1); ds@1952: p->overflow = (s1&s2&~s3) | (~s1&~s2&s3); ds@1952: p->iSum = iNewSum; ds@1952: } ds@1952: }else{ ds@1952: p->rSum += sqlite3_value_double(argv[0]); ds@1952: p->approx = 1; ds@1952: } ds@1952: } ds@1952: } ds@1952: static void sumFinalize(sqlite3_context *context){ ds@1952: SumCtx *p; ds@1952: p = sqlite3_aggregate_context(context, 0); ds@1952: if( p && p->cnt>0 ){ ds@1952: if( p->overflow ){ ds@1952: sqlite3_result_error(context,"integer overflow",-1); ds@1952: }else if( p->approx ){ ds@1952: sqlite3_result_double(context, p->rSum); ds@1952: }else{ ds@1952: sqlite3_result_int64(context, p->iSum); ds@1952: } ds@1952: } ds@1952: } ds@1952: static void avgFinalize(sqlite3_context *context){ ds@1952: SumCtx *p; ds@1952: p = sqlite3_aggregate_context(context, 0); ds@1952: if( p && p->cnt>0 ){ ds@1952: sqlite3_result_double(context, p->rSum/(double)p->cnt); ds@1952: } ds@1952: } ds@1952: static void totalFinalize(sqlite3_context *context){ ds@1952: SumCtx *p; ds@1952: p = sqlite3_aggregate_context(context, 0); ds@1952: sqlite3_result_double(context, p ? p->rSum : 0.0); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The following structure keeps track of state information for the ds@1952: ** count() aggregate function. ds@1952: */ ds@1952: typedef struct CountCtx CountCtx; ds@1952: struct CountCtx { ds@1952: i64 n; ds@1952: }; ds@1952: ds@1952: /* ds@1952: ** Routines to implement the count() aggregate function. ds@1952: */ ds@1952: static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ ds@1952: CountCtx *p; ds@1952: p = sqlite3_aggregate_context(context, sizeof(*p)); ds@1952: if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ ds@1952: p->n++; ds@1952: } ds@1952: } ds@1952: static void countFinalize(sqlite3_context *context){ ds@1952: CountCtx *p; ds@1952: p = sqlite3_aggregate_context(context, 0); ds@1952: sqlite3_result_int64(context, p ? p->n : 0); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Routines to implement min() and max() aggregate functions. ds@1952: */ ds@1952: static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ ds@1952: Mem *pArg = (Mem *)argv[0]; ds@1952: Mem *pBest; ds@1952: ds@1952: if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; ds@1952: pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); ds@1952: if( !pBest ) return; ds@1952: ds@1952: if( pBest->flags ){ ds@1952: int max; ds@1952: int cmp; ds@1952: CollSeq *pColl = sqlite3GetFuncCollSeq(context); ds@1952: /* This step function is used for both the min() and max() aggregates, ds@1952: ** the only difference between the two being that the sense of the ds@1952: ** comparison is inverted. For the max() aggregate, the ds@1952: ** sqlite3_user_data() function returns (void *)-1. For min() it ds@1952: ** returns (void *)db, where db is the sqlite3* database pointer. ds@1952: ** Therefore the next statement sets variable 'max' to 1 for the max() ds@1952: ** aggregate, or 0 for min(). ds@1952: */ ds@1952: max = sqlite3_user_data(context)!=0; ds@1952: cmp = sqlite3MemCompare(pBest, pArg, pColl); ds@1952: if( (max && cmp<0) || (!max && cmp>0) ){ ds@1952: sqlite3VdbeMemCopy(pBest, pArg); ds@1952: } ds@1952: }else{ ds@1952: sqlite3VdbeMemCopy(pBest, pArg); ds@1952: } ds@1952: } ds@1952: static void minMaxFinalize(sqlite3_context *context){ ds@1952: sqlite3_value *pRes; ds@1952: pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); ds@1952: if( pRes ){ ds@1952: if( pRes->flags ){ ds@1952: sqlite3_result_value(context, pRes); ds@1952: } ds@1952: sqlite3VdbeMemRelease(pRes); ds@1952: } ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** This function registered all of the above C functions as SQL ds@1952: ** functions. This should be the only routine in this file with ds@1952: ** external linkage. ds@1952: */ ds@1952: void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ ds@1952: static const struct { ds@1952: char *zName; ds@1952: signed char nArg; ds@1952: u8 argType; /* ff: db 1: 0, 2: 1, 3: 2,... N: N-1. */ ds@1952: u8 eTextRep; /* 1: UTF-16. 0: UTF-8 */ ds@1952: u8 needCollSeq; ds@1952: void (*xFunc)(sqlite3_context*,int,sqlite3_value **); ds@1952: } aFuncs[] = { ds@1952: { "min", -1, 0, SQLITE_UTF8, 1, minmaxFunc }, ds@1952: { "min", 0, 0, SQLITE_UTF8, 1, 0 }, ds@1952: { "max", -1, 1, SQLITE_UTF8, 1, minmaxFunc }, ds@1952: { "max", 0, 1, SQLITE_UTF8, 1, 0 }, ds@1952: { "typeof", 1, 0, SQLITE_UTF8, 0, typeofFunc }, ds@1952: { "length", 1, 0, SQLITE_UTF8, 0, lengthFunc }, ds@1952: { "substr", 3, 0, SQLITE_UTF8, 0, substrFunc }, ds@1952: { "abs", 1, 0, SQLITE_UTF8, 0, absFunc }, ds@1952: { "round", 1, 0, SQLITE_UTF8, 0, roundFunc }, ds@1952: { "round", 2, 0, SQLITE_UTF8, 0, roundFunc }, ds@1952: { "upper", 1, 0, SQLITE_UTF8, 0, upperFunc }, ds@1952: { "lower", 1, 0, SQLITE_UTF8, 0, lowerFunc }, ds@1952: { "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc }, ds@1952: { "coalesce", 0, 0, SQLITE_UTF8, 0, 0 }, ds@1952: { "coalesce", 1, 0, SQLITE_UTF8, 0, 0 }, ds@1952: { "hex", 1, 0, SQLITE_UTF8, 0, hexFunc }, ds@1952: { "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc }, ds@1952: { "random", -1, 0, SQLITE_UTF8, 0, randomFunc }, ds@1952: { "randomblob", 1, 0, SQLITE_UTF8, 0, randomBlob }, ds@1952: { "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc }, ds@1952: { "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc}, ds@1952: { "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc }, ds@1952: { "last_insert_rowid", 0, 0xff, SQLITE_UTF8, 0, last_insert_rowid }, ds@1952: { "changes", 0, 0xff, SQLITE_UTF8, 0, changes }, ds@1952: { "total_changes", 0, 0xff, SQLITE_UTF8, 0, total_changes }, ds@1952: { "replace", 3, 0, SQLITE_UTF8, 0, replaceFunc }, ds@1952: { "ltrim", 1, 1, SQLITE_UTF8, 0, trimFunc }, ds@1952: { "ltrim", 2, 1, SQLITE_UTF8, 0, trimFunc }, ds@1952: { "rtrim", 1, 2, SQLITE_UTF8, 0, trimFunc }, ds@1952: { "rtrim", 2, 2, SQLITE_UTF8, 0, trimFunc }, ds@1952: { "trim", 1, 3, SQLITE_UTF8, 0, trimFunc }, ds@1952: { "trim", 2, 3, SQLITE_UTF8, 0, trimFunc }, ds@1952: { "zeroblob", 1, 0, SQLITE_UTF8, 0, zeroblobFunc }, ds@1952: #ifdef SQLITE_SOUNDEX ds@1952: { "soundex", 1, 0, SQLITE_UTF8, 0, soundexFunc}, ds@1952: #endif ds@1952: #ifndef SQLITE_OMIT_LOAD_EXTENSION ds@1952: { "load_extension", 1, 0xff, SQLITE_UTF8, 0, loadExt }, ds@1952: { "load_extension", 2, 0xff, SQLITE_UTF8, 0, loadExt }, ds@1952: #endif ds@1952: #ifdef SQLITE_TEST ds@1952: { "randstr", 2, 0, SQLITE_UTF8, 0, randStr }, ds@1952: { "test_destructor", 1, 0xff, SQLITE_UTF8, 0, test_destructor}, ds@1952: { "test_destructor_count", 0, 0, SQLITE_UTF8, 0, test_destructor_count}, ds@1952: { "test_auxdata", -1, 0, SQLITE_UTF8, 0, test_auxdata}, ds@1952: { "test_error", 1, 0, SQLITE_UTF8, 0, test_error}, ds@1952: #endif ds@1952: }; ds@1952: static const struct { ds@1952: char *zName; ds@1952: signed char nArg; ds@1952: u8 argType; ds@1952: u8 needCollSeq; ds@1952: void (*xStep)(sqlite3_context*,int,sqlite3_value**); ds@1952: void (*xFinalize)(sqlite3_context*); ds@1952: } aAggs[] = { ds@1952: { "min", 1, 0, 1, minmaxStep, minMaxFinalize }, ds@1952: { "max", 1, 1, 1, minmaxStep, minMaxFinalize }, ds@1952: { "sum", 1, 0, 0, sumStep, sumFinalize }, ds@1952: { "total", 1, 0, 0, sumStep, totalFinalize }, ds@1952: { "avg", 1, 0, 0, sumStep, avgFinalize }, ds@1952: { "count", 0, 0, 0, countStep, countFinalize }, ds@1952: { "count", 1, 0, 0, countStep, countFinalize }, ds@1952: }; ds@1952: int i; ds@1952: ds@1952: for(i=0; ineedCollSeq = 1; ds@1952: } ds@1952: } ds@1952: } ds@1952: #ifndef SQLITE_OMIT_ALTERTABLE ds@1952: sqlite3AlterFunctions(db); ds@1952: #endif ds@1952: #ifndef SQLITE_OMIT_PARSER ds@1952: sqlite3AttachFunctions(db); ds@1952: #endif ds@1952: for(i=0; ineedCollSeq = 1; ds@1952: } ds@1952: } ds@1952: } ds@1952: sqlite3RegisterDateTimeFunctions(db); ds@1952: if( !db->mallocFailed ){ ds@1952: int rc = sqlite3_overload_function(db, "MATCH", 2); ds@1952: assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); ds@1952: if( rc==SQLITE_NOMEM ){ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: } ds@1952: #ifdef SQLITE_SSE ds@1952: (void)sqlite3SseFunctions(db); ds@1952: #endif ds@1952: #ifdef SQLITE_CASE_SENSITIVE_LIKE ds@1952: sqlite3RegisterLikeFunctions(db, 1); ds@1952: #else ds@1952: sqlite3RegisterLikeFunctions(db, 0); ds@1952: #endif ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Set the LIKEOPT flag on the 2-argument function with the given name. ds@1952: */ ds@1952: static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){ ds@1952: FuncDef *pDef; ds@1952: pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0); ds@1952: if( pDef ){ ds@1952: pDef->flags = flagVal; ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Register the built-in LIKE and GLOB functions. The caseSensitive ds@1952: ** parameter determines whether or not the LIKE operator is case ds@1952: ** sensitive. GLOB is always case sensitive. ds@1952: */ ds@1952: void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ ds@1952: struct compareInfo *pInfo; ds@1952: if( caseSensitive ){ ds@1952: pInfo = (struct compareInfo*)&likeInfoAlt; ds@1952: }else{ ds@1952: pInfo = (struct compareInfo*)&likeInfoNorm; ds@1952: } ds@1952: sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0); ds@1952: sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0); ds@1952: sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, ds@1952: (struct compareInfo*)&globInfo, likeFunc, 0,0); ds@1952: setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); ds@1952: setLikeOptFlag(db, "like", ds@1952: caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** pExpr points to an expression which implements a function. If ds@1952: ** it is appropriate to apply the LIKE optimization to that function ds@1952: ** then set aWc[0] through aWc[2] to the wildcard characters and ds@1952: ** return TRUE. If the function is not a LIKE-style function then ds@1952: ** return FALSE. ds@1952: */ ds@1952: int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ ds@1952: FuncDef *pDef; ds@1952: if( pExpr->op!=TK_FUNCTION || !pExpr->pList ){ ds@1952: return 0; ds@1952: } ds@1952: if( pExpr->pList->nExpr!=2 ){ ds@1952: return 0; ds@1952: } ds@1952: pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2, ds@1952: SQLITE_UTF8, 0); ds@1952: if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){ ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* The memcpy() statement assumes that the wildcard characters are ds@1952: ** the first three statements in the compareInfo structure. The ds@1952: ** asserts() that follow verify that assumption ds@1952: */ ds@1952: memcpy(aWc, pDef->pUserData, 3); ds@1952: assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); ds@1952: assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); ds@1952: assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); ds@1952: *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0; ds@1952: return 1; ds@1952: }