extensions/sqlite/sqlite-source/func.c
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 02:58:58 2008 -0500 (2008-09-14)
changeset 2287 017a725c9d17
parent 1952 b7aa2b626807
permissions -rw-r--r--
Eliminated head for dead branch: sourcemod-1.1.0
ds@1952
     1
/*
ds@1952
     2
** 2002 February 23
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** This file contains the C functions that implement various SQL
ds@1952
    13
** functions of SQLite.  
ds@1952
    14
**
ds@1952
    15
** There is only one exported symbol in this file - the function
ds@1952
    16
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
ds@1952
    17
** All other code has file scope.
ds@1952
    18
**
ds@1952
    19
** $Id$
ds@1952
    20
*/
ds@1952
    21
#include "sqliteInt.h"
ds@1952
    22
#include <ctype.h>
ds@1952
    23
#include <stdlib.h>
ds@1952
    24
#include <assert.h>
ds@1952
    25
#include "vdbeInt.h"
ds@1952
    26
ds@1952
    27
ds@1952
    28
/*
ds@1952
    29
** Return the collating function associated with a function.
ds@1952
    30
*/
ds@1952
    31
static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
ds@1952
    32
  return context->pColl;
ds@1952
    33
}
ds@1952
    34
ds@1952
    35
/*
ds@1952
    36
** Implementation of the non-aggregate min() and max() functions
ds@1952
    37
*/
ds@1952
    38
static void minmaxFunc(
ds@1952
    39
  sqlite3_context *context,
ds@1952
    40
  int argc,
ds@1952
    41
  sqlite3_value **argv
ds@1952
    42
){
ds@1952
    43
  int i;
ds@1952
    44
  int mask;    /* 0 for min() or 0xffffffff for max() */
ds@1952
    45
  int iBest;
ds@1952
    46
  CollSeq *pColl;
ds@1952
    47
ds@1952
    48
  if( argc==0 ) return;
ds@1952
    49
  mask = sqlite3_user_data(context)==0 ? 0 : -1;
ds@1952
    50
  pColl = sqlite3GetFuncCollSeq(context);
ds@1952
    51
  assert( pColl );
ds@1952
    52
  assert( mask==-1 || mask==0 );
ds@1952
    53
  iBest = 0;
ds@1952
    54
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
ds@1952
    55
  for(i=1; i<argc; i++){
ds@1952
    56
    if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
ds@1952
    57
    if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
ds@1952
    58
      iBest = i;
ds@1952
    59
    }
ds@1952
    60
  }
ds@1952
    61
  sqlite3_result_value(context, argv[iBest]);
ds@1952
    62
}
ds@1952
    63
ds@1952
    64
/*
ds@1952
    65
** Return the type of the argument.
ds@1952
    66
*/
ds@1952
    67
static void typeofFunc(
ds@1952
    68
  sqlite3_context *context,
ds@1952
    69
  int argc,
ds@1952
    70
  sqlite3_value **argv
ds@1952
    71
){
ds@1952
    72
  const char *z = 0;
ds@1952
    73
  switch( sqlite3_value_type(argv[0]) ){
ds@1952
    74
    case SQLITE_NULL:    z = "null";    break;
ds@1952
    75
    case SQLITE_INTEGER: z = "integer"; break;
ds@1952
    76
    case SQLITE_TEXT:    z = "text";    break;
ds@1952
    77
    case SQLITE_FLOAT:   z = "real";    break;
ds@1952
    78
    case SQLITE_BLOB:    z = "blob";    break;
ds@1952
    79
  }
ds@1952
    80
  sqlite3_result_text(context, z, -1, SQLITE_STATIC);
ds@1952
    81
}
ds@1952
    82
ds@1952
    83
ds@1952
    84
/*
ds@1952
    85
** Implementation of the length() function
ds@1952
    86
*/
ds@1952
    87
static void lengthFunc(
ds@1952
    88
  sqlite3_context *context,
ds@1952
    89
  int argc,
ds@1952
    90
  sqlite3_value **argv
ds@1952
    91
){
ds@1952
    92
  int len;
ds@1952
    93
ds@1952
    94
  assert( argc==1 );
ds@1952
    95
  switch( sqlite3_value_type(argv[0]) ){
ds@1952
    96
    case SQLITE_BLOB:
ds@1952
    97
    case SQLITE_INTEGER:
ds@1952
    98
    case SQLITE_FLOAT: {
ds@1952
    99
      sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
ds@1952
   100
      break;
ds@1952
   101
    }
ds@1952
   102
    case SQLITE_TEXT: {
ds@1952
   103
      const unsigned char *z = sqlite3_value_text(argv[0]);
ds@1952
   104
      if( z==0 ) return;
ds@1952
   105
      len = 0;
ds@1952
   106
      while( *z ){
ds@1952
   107
        len++;
ds@1952
   108
        SQLITE_SKIP_UTF8(z);
ds@1952
   109
      }
ds@1952
   110
      sqlite3_result_int(context, len);
ds@1952
   111
      break;
ds@1952
   112
    }
ds@1952
   113
    default: {
ds@1952
   114
      sqlite3_result_null(context);
ds@1952
   115
      break;
ds@1952
   116
    }
ds@1952
   117
  }
ds@1952
   118
}
ds@1952
   119
ds@1952
   120
/*
ds@1952
   121
** Implementation of the abs() function
ds@1952
   122
*/
ds@1952
   123
static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
ds@1952
   124
  assert( argc==1 );
ds@1952
   125
  switch( sqlite3_value_type(argv[0]) ){
ds@1952
   126
    case SQLITE_INTEGER: {
ds@1952
   127
      i64 iVal = sqlite3_value_int64(argv[0]);
ds@1952
   128
      if( iVal<0 ){
ds@1952
   129
        if( (iVal<<1)==0 ){
ds@1952
   130
          sqlite3_result_error(context, "integer overflow", -1);
ds@1952
   131
          return;
ds@1952
   132
        }
ds@1952
   133
        iVal = -iVal;
ds@1952
   134
      } 
ds@1952
   135
      sqlite3_result_int64(context, iVal);
ds@1952
   136
      break;
ds@1952
   137
    }
ds@1952
   138
    case SQLITE_NULL: {
ds@1952
   139
      sqlite3_result_null(context);
ds@1952
   140
      break;
ds@1952
   141
    }
ds@1952
   142
    default: {
ds@1952
   143
      double rVal = sqlite3_value_double(argv[0]);
ds@1952
   144
      if( rVal<0 ) rVal = -rVal;
ds@1952
   145
      sqlite3_result_double(context, rVal);
ds@1952
   146
      break;
ds@1952
   147
    }
ds@1952
   148
  }
ds@1952
   149
}
ds@1952
   150
ds@1952
   151
/*
ds@1952
   152
** Implementation of the substr() function.
ds@1952
   153
**
ds@1952
   154
** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
ds@1952
   155
** p1 is 1-indexed.  So substr(x,1,1) returns the first character
ds@1952
   156
** of x.  If x is text, then we actually count UTF-8 characters.
ds@1952
   157
** If x is a blob, then we count bytes.
ds@1952
   158
**
ds@1952
   159
** If p1 is negative, then we begin abs(p1) from the end of x[].
ds@1952
   160
*/
ds@1952
   161
static void substrFunc(
ds@1952
   162
  sqlite3_context *context,
ds@1952
   163
  int argc,
ds@1952
   164
  sqlite3_value **argv
ds@1952
   165
){
ds@1952
   166
  const unsigned char *z;
ds@1952
   167
  const unsigned char *z2;
ds@1952
   168
  int len;
ds@1952
   169
  int p0type;
ds@1952
   170
  i64 p1, p2;
ds@1952
   171
ds@1952
   172
  assert( argc==3 );
ds@1952
   173
  p0type = sqlite3_value_type(argv[0]);
ds@1952
   174
  if( p0type==SQLITE_BLOB ){
ds@1952
   175
    len = sqlite3_value_bytes(argv[0]);
ds@1952
   176
    z = sqlite3_value_blob(argv[0]);
ds@1952
   177
    if( z==0 ) return;
ds@1952
   178
    assert( len==sqlite3_value_bytes(argv[0]) );
ds@1952
   179
  }else{
ds@1952
   180
    z = sqlite3_value_text(argv[0]);
ds@1952
   181
    if( z==0 ) return;
ds@1952
   182
    len = 0;
ds@1952
   183
    for(z2=z; *z2; len++){
ds@1952
   184
      SQLITE_SKIP_UTF8(z2);
ds@1952
   185
    }
ds@1952
   186
  }
ds@1952
   187
  p1 = sqlite3_value_int(argv[1]);
ds@1952
   188
  p2 = sqlite3_value_int(argv[2]);
ds@1952
   189
  if( p1<0 ){
ds@1952
   190
    p1 += len;
ds@1952
   191
    if( p1<0 ){
ds@1952
   192
      p2 += p1;
ds@1952
   193
      p1 = 0;
ds@1952
   194
    }
ds@1952
   195
  }else if( p1>0 ){
ds@1952
   196
    p1--;
ds@1952
   197
  }
ds@1952
   198
  if( p1+p2>len ){
ds@1952
   199
    p2 = len-p1;
ds@1952
   200
  }
ds@1952
   201
  if( p0type!=SQLITE_BLOB ){
ds@1952
   202
    while( *z && p1 ){
ds@1952
   203
      SQLITE_SKIP_UTF8(z);
ds@1952
   204
      p1--;
ds@1952
   205
    }
ds@1952
   206
    for(z2=z; *z2 && p2; p2--){
ds@1952
   207
      SQLITE_SKIP_UTF8(z2);
ds@1952
   208
    }
ds@1952
   209
    sqlite3_result_text(context, (char*)z, z2-z, SQLITE_TRANSIENT);
ds@1952
   210
  }else{
ds@1952
   211
    if( p2<0 ) p2 = 0;
ds@1952
   212
    sqlite3_result_blob(context, (char*)&z[p1], p2, SQLITE_TRANSIENT);
ds@1952
   213
  }
ds@1952
   214
}
ds@1952
   215
ds@1952
   216
/*
ds@1952
   217
** Implementation of the round() function
ds@1952
   218
*/
ds@1952
   219
static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
ds@1952
   220
  int n = 0;
ds@1952
   221
  double r;
ds@1952
   222
  char zBuf[500];  /* larger than the %f representation of the largest double */
ds@1952
   223
  assert( argc==1 || argc==2 );
ds@1952
   224
  if( argc==2 ){
ds@1952
   225
    if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
ds@1952
   226
    n = sqlite3_value_int(argv[1]);
ds@1952
   227
    if( n>30 ) n = 30;
ds@1952
   228
    if( n<0 ) n = 0;
ds@1952
   229
  }
ds@1952
   230
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
ds@1952
   231
  r = sqlite3_value_double(argv[0]);
ds@1952
   232
  sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r);
ds@1952
   233
  sqlite3AtoF(zBuf, &r);
ds@1952
   234
  sqlite3_result_double(context, r);
ds@1952
   235
}
ds@1952
   236
ds@1952
   237
/*
ds@1952
   238
** Allocate nByte bytes of space using sqlite3_malloc(). If the
ds@1952
   239
** allocation fails, call sqlite3_result_error_nomem() to notify
ds@1952
   240
** the database handle that malloc() has failed.
ds@1952
   241
*/
ds@1952
   242
static void *contextMalloc(sqlite3_context *context, int nByte){
ds@1952
   243
  char *z = sqlite3_malloc(nByte);
ds@1952
   244
  if( !z && nByte>0 ){
ds@1952
   245
    sqlite3_result_error_nomem(context);
ds@1952
   246
  }
ds@1952
   247
  return z;
ds@1952
   248
}
ds@1952
   249
ds@1952
   250
/*
ds@1952
   251
** Implementation of the upper() and lower() SQL functions.
ds@1952
   252
*/
ds@1952
   253
static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
ds@1952
   254
  char *z1;
ds@1952
   255
  const char *z2;
ds@1952
   256
  int i, n;
ds@1952
   257
  if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
ds@1952
   258
  z2 = (char*)sqlite3_value_text(argv[0]);
ds@1952
   259
  n = sqlite3_value_bytes(argv[0]);
ds@1952
   260
  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
ds@1952
   261
  assert( z2==(char*)sqlite3_value_text(argv[0]) );
ds@1952
   262
  if( z2 ){
ds@1952
   263
    z1 = contextMalloc(context, n+1);
ds@1952
   264
    if( z1 ){
ds@1952
   265
      memcpy(z1, z2, n+1);
ds@1952
   266
      for(i=0; z1[i]; i++){
ds@1952
   267
        z1[i] = toupper(z1[i]);
ds@1952
   268
      }
ds@1952
   269
      sqlite3_result_text(context, z1, -1, sqlite3_free);
ds@1952
   270
    }
ds@1952
   271
  }
ds@1952
   272
}
ds@1952
   273
static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
ds@1952
   274
  char *z1;
ds@1952
   275
  const char *z2;
ds@1952
   276
  int i, n;
ds@1952
   277
  if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;
ds@1952
   278
  z2 = (char*)sqlite3_value_text(argv[0]);
ds@1952
   279
  n = sqlite3_value_bytes(argv[0]);
ds@1952
   280
  /* Verify that the call to _bytes() does not invalidate the _text() pointer */
ds@1952
   281
  assert( z2==(char*)sqlite3_value_text(argv[0]) );
ds@1952
   282
  if( z2 ){
ds@1952
   283
    z1 = contextMalloc(context, n+1);
ds@1952
   284
    if( z1 ){
ds@1952
   285
      memcpy(z1, z2, n+1);
ds@1952
   286
      for(i=0; z1[i]; i++){
ds@1952
   287
        z1[i] = tolower(z1[i]);
ds@1952
   288
      }
ds@1952
   289
      sqlite3_result_text(context, z1, -1, sqlite3_free);
ds@1952
   290
    }
ds@1952
   291
  }
ds@1952
   292
}
ds@1952
   293
ds@1952
   294
/*
ds@1952
   295
** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  
ds@1952
   296
** All three do the same thing.  They return the first non-NULL
ds@1952
   297
** argument.
ds@1952
   298
*/
ds@1952
   299
static void ifnullFunc(
ds@1952
   300
  sqlite3_context *context,
ds@1952
   301
  int argc,
ds@1952
   302
  sqlite3_value **argv
ds@1952
   303
){
ds@1952
   304
  int i;
ds@1952
   305
  for(i=0; i<argc; i++){
ds@1952
   306
    if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
ds@1952
   307
      sqlite3_result_value(context, argv[i]);
ds@1952
   308
      break;
ds@1952
   309
    }
ds@1952
   310
  }
ds@1952
   311
}
ds@1952
   312
ds@1952
   313
/*
ds@1952
   314
** Implementation of random().  Return a random integer.  
ds@1952
   315
*/
ds@1952
   316
static void randomFunc(
ds@1952
   317
  sqlite3_context *context,
ds@1952
   318
  int argc,
ds@1952
   319
  sqlite3_value **argv
ds@1952
   320
){
ds@1952
   321
  sqlite_int64 r;
ds@1952
   322
  sqlite3Randomness(sizeof(r), &r);
ds@1952
   323
  if( (r<<1)==0 ) r = 0;  /* Prevent 0x8000.... as the result so that we */
ds@1952
   324
                          /* can always do abs() of the result */
ds@1952
   325
  sqlite3_result_int64(context, r);
ds@1952
   326
}
ds@1952
   327
ds@1952
   328
/*
ds@1952
   329
** Implementation of randomblob(N).  Return a random blob
ds@1952
   330
** that is N bytes long.
ds@1952
   331
*/
ds@1952
   332
static void randomBlob(
ds@1952
   333
  sqlite3_context *context,
ds@1952
   334
  int argc,
ds@1952
   335
  sqlite3_value **argv
ds@1952
   336
){
ds@1952
   337
  int n;
ds@1952
   338
  unsigned char *p;
ds@1952
   339
  assert( argc==1 );
ds@1952
   340
  n = sqlite3_value_int(argv[0]);
ds@1952
   341
  if( n<1 ){
ds@1952
   342
    n = 1;
ds@1952
   343
  }
ds@1952
   344
  if( n>SQLITE_MAX_LENGTH ){
ds@1952
   345
    sqlite3_result_error_toobig(context);
ds@1952
   346
    return;
ds@1952
   347
  }
ds@1952
   348
  p = contextMalloc(context, n);
ds@1952
   349
  if( p ){
ds@1952
   350
    sqlite3Randomness(n, p);
ds@1952
   351
    sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
ds@1952
   352
  }
ds@1952
   353
}
ds@1952
   354
ds@1952
   355
/*
ds@1952
   356
** Implementation of the last_insert_rowid() SQL function.  The return
ds@1952
   357
** value is the same as the sqlite3_last_insert_rowid() API function.
ds@1952
   358
*/
ds@1952
   359
static void last_insert_rowid(
ds@1952
   360
  sqlite3_context *context, 
ds@1952
   361
  int arg, 
ds@1952
   362
  sqlite3_value **argv
ds@1952
   363
){
ds@1952
   364
  sqlite3 *db = sqlite3_user_data(context);
ds@1952
   365
  sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
ds@1952
   366
}
ds@1952
   367
ds@1952
   368
/*
ds@1952
   369
** Implementation of the changes() SQL function.  The return value is the
ds@1952
   370
** same as the sqlite3_changes() API function.
ds@1952
   371
*/
ds@1952
   372
static void changes(
ds@1952
   373
  sqlite3_context *context,
ds@1952
   374
  int arg,
ds@1952
   375
  sqlite3_value **argv
ds@1952
   376
){
ds@1952
   377
  sqlite3 *db = sqlite3_user_data(context);
ds@1952
   378
  sqlite3_result_int(context, sqlite3_changes(db));
ds@1952
   379
}
ds@1952
   380
ds@1952
   381
/*
ds@1952
   382
** Implementation of the total_changes() SQL function.  The return value is
ds@1952
   383
** the same as the sqlite3_total_changes() API function.
ds@1952
   384
*/
ds@1952
   385
static void total_changes(
ds@1952
   386
  sqlite3_context *context,
ds@1952
   387
  int arg,
ds@1952
   388
  sqlite3_value **argv
ds@1952
   389
){
ds@1952
   390
  sqlite3 *db = sqlite3_user_data(context);
ds@1952
   391
  sqlite3_result_int(context, sqlite3_total_changes(db));
ds@1952
   392
}
ds@1952
   393
ds@1952
   394
/*
ds@1952
   395
** A structure defining how to do GLOB-style comparisons.
ds@1952
   396
*/
ds@1952
   397
struct compareInfo {
ds@1952
   398
  u8 matchAll;
ds@1952
   399
  u8 matchOne;
ds@1952
   400
  u8 matchSet;
ds@1952
   401
  u8 noCase;
ds@1952
   402
};
ds@1952
   403
ds@1952
   404
static const struct compareInfo globInfo = { '*', '?', '[', 0 };
ds@1952
   405
/* The correct SQL-92 behavior is for the LIKE operator to ignore
ds@1952
   406
** case.  Thus  'a' LIKE 'A' would be true. */
ds@1952
   407
static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
ds@1952
   408
/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
ds@1952
   409
** is case sensitive causing 'a' LIKE 'A' to be false */
ds@1952
   410
static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
ds@1952
   411
ds@1952
   412
/*
ds@1952
   413
** Compare two UTF-8 strings for equality where the first string can
ds@1952
   414
** potentially be a "glob" expression.  Return true (1) if they
ds@1952
   415
** are the same and false (0) if they are different.
ds@1952
   416
**
ds@1952
   417
** Globbing rules:
ds@1952
   418
**
ds@1952
   419
**      '*'       Matches any sequence of zero or more characters.
ds@1952
   420
**
ds@1952
   421
**      '?'       Matches exactly one character.
ds@1952
   422
**
ds@1952
   423
**     [...]      Matches one character from the enclosed list of
ds@1952
   424
**                characters.
ds@1952
   425
**
ds@1952
   426
**     [^...]     Matches one character not in the enclosed list.
ds@1952
   427
**
ds@1952
   428
** With the [...] and [^...] matching, a ']' character can be included
ds@1952
   429
** in the list by making it the first character after '[' or '^'.  A
ds@1952
   430
** range of characters can be specified using '-'.  Example:
ds@1952
   431
** "[a-z]" matches any single lower-case letter.  To match a '-', make
ds@1952
   432
** it the last character in the list.
ds@1952
   433
**
ds@1952
   434
** This routine is usually quick, but can be N**2 in the worst case.
ds@1952
   435
**
ds@1952
   436
** Hints: to match '*' or '?', put them in "[]".  Like this:
ds@1952
   437
**
ds@1952
   438
**         abc[*]xyz        Matches "abc*xyz" only
ds@1952
   439
*/
ds@1952
   440
static int patternCompare(
ds@1952
   441
  const u8 *zPattern,              /* The glob pattern */
ds@1952
   442
  const u8 *zString,               /* The string to compare against the glob */
ds@1952
   443
  const struct compareInfo *pInfo, /* Information about how to do the compare */
ds@1952
   444
  const int esc                    /* The escape character */
ds@1952
   445
){
ds@1952
   446
  int c, c2;
ds@1952
   447
  int invert;
ds@1952
   448
  int seen;
ds@1952
   449
  u8 matchOne = pInfo->matchOne;
ds@1952
   450
  u8 matchAll = pInfo->matchAll;
ds@1952
   451
  u8 matchSet = pInfo->matchSet;
ds@1952
   452
  u8 noCase = pInfo->noCase; 
ds@1952
   453
  int prevEscape = 0;     /* True if the previous character was 'escape' */
ds@1952
   454
ds@1952
   455
  while( (c = sqlite3Utf8Read(zPattern,0,&zPattern))!=0 ){
ds@1952
   456
    if( !prevEscape && c==matchAll ){
ds@1952
   457
      while( (c=sqlite3Utf8Read(zPattern,0,&zPattern)) == matchAll
ds@1952
   458
               || c == matchOne ){
ds@1952
   459
        if( c==matchOne && sqlite3Utf8Read(zString, 0, &zString)==0 ){
ds@1952
   460
          return 0;
ds@1952
   461
        }
ds@1952
   462
      }
ds@1952
   463
      if( c==0 ){
ds@1952
   464
        return 1;
ds@1952
   465
      }else if( c==esc ){
ds@1952
   466
        c = sqlite3Utf8Read(zPattern, 0, &zPattern);
ds@1952
   467
        if( c==0 ){
ds@1952
   468
          return 0;
ds@1952
   469
        }
ds@1952
   470
      }else if( c==matchSet ){
ds@1952
   471
        assert( esc==0 );         /* This is GLOB, not LIKE */
ds@1952
   472
        assert( matchSet<0x80 );  /* '[' is a single-byte character */
ds@1952
   473
        while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
ds@1952
   474
          SQLITE_SKIP_UTF8(zString);
ds@1952
   475
        }
ds@1952
   476
        return *zString!=0;
ds@1952
   477
      }
ds@1952
   478
      while( (c2 = sqlite3Utf8Read(zString,0,&zString))!=0 ){
ds@1952
   479
        if( noCase ){
ds@1952
   480
          c2 = c2<0x80 ? sqlite3UpperToLower[c2] : c2;
ds@1952
   481
          c = c<0x80 ? sqlite3UpperToLower[c] : c;
ds@1952
   482
          while( c2 != 0 && c2 != c ){
ds@1952
   483
            c2 = sqlite3Utf8Read(zString, 0, &zString);
ds@1952
   484
            if( c2<0x80 ) c2 = sqlite3UpperToLower[c2];
ds@1952
   485
          }
ds@1952
   486
        }else{
ds@1952
   487
          while( c2 != 0 && c2 != c ){
ds@1952
   488
            c2 = sqlite3Utf8Read(zString, 0, &zString);
ds@1952
   489
          }
ds@1952
   490
        }
ds@1952
   491
        if( c2==0 ) return 0;
ds@1952
   492
        if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
ds@1952
   493
      }
ds@1952
   494
      return 0;
ds@1952
   495
    }else if( !prevEscape && c==matchOne ){
ds@1952
   496
      if( sqlite3Utf8Read(zString, 0, &zString)==0 ){
ds@1952
   497
        return 0;
ds@1952
   498
      }
ds@1952
   499
    }else if( c==matchSet ){
ds@1952
   500
      int prior_c = 0;
ds@1952
   501
      assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
ds@1952
   502
      seen = 0;
ds@1952
   503
      invert = 0;
ds@1952
   504
      c = sqlite3Utf8Read(zString, 0, &zString);
ds@1952
   505
      if( c==0 ) return 0;
ds@1952
   506
      c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
ds@1952
   507
      if( c2=='^' ){
ds@1952
   508
        invert = 1;
ds@1952
   509
        c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
ds@1952
   510
      }
ds@1952
   511
      if( c2==']' ){
ds@1952
   512
        if( c==']' ) seen = 1;
ds@1952
   513
        c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
ds@1952
   514
      }
ds@1952
   515
      while( c2 && c2!=']' ){
ds@1952
   516
        if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
ds@1952
   517
          c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
ds@1952
   518
          if( c>=prior_c && c<=c2 ) seen = 1;
ds@1952
   519
          prior_c = 0;
ds@1952
   520
        }else{
ds@1952
   521
          if( c==c2 ){
ds@1952
   522
            seen = 1;
ds@1952
   523
          }
ds@1952
   524
          prior_c = c2;
ds@1952
   525
        }
ds@1952
   526
        c2 = sqlite3Utf8Read(zPattern, 0, &zPattern);
ds@1952
   527
      }
ds@1952
   528
      if( c2==0 || (seen ^ invert)==0 ){
ds@1952
   529
        return 0;
ds@1952
   530
      }
ds@1952
   531
    }else if( esc==c && !prevEscape ){
ds@1952
   532
      prevEscape = 1;
ds@1952
   533
    }else{
ds@1952
   534
      c2 = sqlite3Utf8Read(zString, 0, &zString);
ds@1952
   535
      if( noCase ){
ds@1952
   536
        c = c<0x80 ? sqlite3UpperToLower[c] : c;
ds@1952
   537
        c2 = c2<0x80 ? sqlite3UpperToLower[c2] : c2;
ds@1952
   538
      }
ds@1952
   539
      if( c!=c2 ){
ds@1952
   540
        return 0;
ds@1952
   541
      }
ds@1952
   542
      prevEscape = 0;
ds@1952
   543
    }
ds@1952
   544
  }
ds@1952
   545
  return *zString==0;
ds@1952
   546
}
ds@1952
   547
ds@1952
   548
/*
ds@1952
   549
** Count the number of times that the LIKE operator (or GLOB which is
ds@1952
   550
** just a variation of LIKE) gets called.  This is used for testing
ds@1952
   551
** only.
ds@1952
   552
*/
ds@1952
   553
#ifdef SQLITE_TEST
ds@1952
   554
int sqlite3_like_count = 0;
ds@1952
   555
#endif
ds@1952
   556
ds@1952
   557
ds@1952
   558
/*
ds@1952
   559
** Implementation of the like() SQL function.  This function implements
ds@1952
   560
** the build-in LIKE operator.  The first argument to the function is the
ds@1952
   561
** pattern and the second argument is the string.  So, the SQL statements:
ds@1952
   562
**
ds@1952
   563
**       A LIKE B
ds@1952
   564
**
ds@1952
   565
** is implemented as like(B,A).
ds@1952
   566
**
ds@1952
   567
** This same function (with a different compareInfo structure) computes
ds@1952
   568
** the GLOB operator.
ds@1952
   569
*/
ds@1952
   570
static void likeFunc(
ds@1952
   571
  sqlite3_context *context, 
ds@1952
   572
  int argc, 
ds@1952
   573
  sqlite3_value **argv
ds@1952
   574
){
ds@1952
   575
  const unsigned char *zA, *zB;
ds@1952
   576
  int escape = 0;
ds@1952
   577
ds@1952
   578
  zB = sqlite3_value_text(argv[0]);
ds@1952
   579
  zA = sqlite3_value_text(argv[1]);
ds@1952
   580
ds@1952
   581
  /* Limit the length of the LIKE or GLOB pattern to avoid problems
ds@1952
   582
  ** of deep recursion and N*N behavior in patternCompare().
ds@1952
   583
  */
ds@1952
   584
  if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
ds@1952
   585
    sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
ds@1952
   586
    return;
ds@1952
   587
  }
ds@1952
   588
  assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
ds@1952
   589
ds@1952
   590
  if( argc==3 ){
ds@1952
   591
    /* The escape character string must consist of a single UTF-8 character.
ds@1952
   592
    ** Otherwise, return an error.
ds@1952
   593
    */
ds@1952
   594
    const unsigned char *zEsc = sqlite3_value_text(argv[2]);
ds@1952
   595
    if( zEsc==0 ) return;
ds@1952
   596
    if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
ds@1952
   597
      sqlite3_result_error(context, 
ds@1952
   598
          "ESCAPE expression must be a single character", -1);
ds@1952
   599
      return;
ds@1952
   600
    }
ds@1952
   601
    escape = sqlite3Utf8Read(zEsc, 0, &zEsc);
ds@1952
   602
  }
ds@1952
   603
  if( zA && zB ){
ds@1952
   604
    struct compareInfo *pInfo = sqlite3_user_data(context);
ds@1952
   605
#ifdef SQLITE_TEST
ds@1952
   606
    sqlite3_like_count++;
ds@1952
   607
#endif
ds@1952
   608
    
ds@1952
   609
    sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
ds@1952
   610
  }
ds@1952
   611
}
ds@1952
   612
ds@1952
   613
/*
ds@1952
   614
** Implementation of the NULLIF(x,y) function.  The result is the first
ds@1952
   615
** argument if the arguments are different.  The result is NULL if the
ds@1952
   616
** arguments are equal to each other.
ds@1952
   617
*/
ds@1952
   618
static void nullifFunc(
ds@1952
   619
  sqlite3_context *context,
ds@1952
   620
  int argc,
ds@1952
   621
  sqlite3_value **argv
ds@1952
   622
){
ds@1952
   623
  CollSeq *pColl = sqlite3GetFuncCollSeq(context);
ds@1952
   624
  if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
ds@1952
   625
    sqlite3_result_value(context, argv[0]);
ds@1952
   626
  }
ds@1952
   627
}
ds@1952
   628
ds@1952
   629
/*
ds@1952
   630
** Implementation of the VERSION(*) function.  The result is the version
ds@1952
   631
** of the SQLite library that is running.
ds@1952
   632
*/
ds@1952
   633
static void versionFunc(
ds@1952
   634
  sqlite3_context *context,
ds@1952
   635
  int argc,
ds@1952
   636
  sqlite3_value **argv
ds@1952
   637
){
ds@1952
   638
  sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
ds@1952
   639
}
ds@1952
   640
ds@1952
   641
/* Array for converting from half-bytes (nybbles) into ASCII hex
ds@1952
   642
** digits. */
ds@1952
   643
static const char hexdigits[] = {
ds@1952
   644
  '0', '1', '2', '3', '4', '5', '6', '7',
ds@1952
   645
  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
ds@1952
   646
};
ds@1952
   647
ds@1952
   648
/*
ds@1952
   649
** EXPERIMENTAL - This is not an official function.  The interface may
ds@1952
   650
** change.  This function may disappear.  Do not write code that depends
ds@1952
   651
** on this function.
ds@1952
   652
**
ds@1952
   653
** Implementation of the QUOTE() function.  This function takes a single
ds@1952
   654
** argument.  If the argument is numeric, the return value is the same as
ds@1952
   655
** the argument.  If the argument is NULL, the return value is the string
ds@1952
   656
** "NULL".  Otherwise, the argument is enclosed in single quotes with
ds@1952
   657
** single-quote escapes.
ds@1952
   658
*/
ds@1952
   659
static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
ds@1952
   660
  if( argc<1 ) return;
ds@1952
   661
  switch( sqlite3_value_type(argv[0]) ){
ds@1952
   662
    case SQLITE_NULL: {
ds@1952
   663
      sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
ds@1952
   664
      break;
ds@1952
   665
    }
ds@1952
   666
    case SQLITE_INTEGER:
ds@1952
   667
    case SQLITE_FLOAT: {
ds@1952
   668
      sqlite3_result_value(context, argv[0]);
ds@1952
   669
      break;
ds@1952
   670
    }
ds@1952
   671
    case SQLITE_BLOB: {
ds@1952
   672
      char *zText = 0;
ds@1952
   673
      char const *zBlob = sqlite3_value_blob(argv[0]);
ds@1952
   674
      int nBlob = sqlite3_value_bytes(argv[0]);
ds@1952
   675
      assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
ds@1952
   676
ds@1952
   677
      if( 2*nBlob+4>SQLITE_MAX_LENGTH ){
ds@1952
   678
        sqlite3_result_error_toobig(context);
ds@1952
   679
        return;
ds@1952
   680
      }
ds@1952
   681
      zText = (char *)contextMalloc(context, (2*nBlob)+4); 
ds@1952
   682
      if( zText ){
ds@1952
   683
        int i;
ds@1952
   684
        for(i=0; i<nBlob; i++){
ds@1952
   685
          zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
ds@1952
   686
          zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
ds@1952
   687
        }
ds@1952
   688
        zText[(nBlob*2)+2] = '\'';
ds@1952
   689
        zText[(nBlob*2)+3] = '\0';
ds@1952
   690
        zText[0] = 'X';
ds@1952
   691
        zText[1] = '\'';
ds@1952
   692
        sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
ds@1952
   693
        sqlite3_free(zText);
ds@1952
   694
      }
ds@1952
   695
      break;
ds@1952
   696
    }
ds@1952
   697
    case SQLITE_TEXT: {
ds@1952
   698
      int i,j;
ds@1952
   699
      u64 n;
ds@1952
   700
      const unsigned char *zArg = sqlite3_value_text(argv[0]);
ds@1952
   701
      char *z;
ds@1952
   702
ds@1952
   703
      if( zArg==0 ) return;
ds@1952
   704
      for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
ds@1952
   705
      if( i+n+3>SQLITE_MAX_LENGTH ){
ds@1952
   706
        sqlite3_result_error_toobig(context);
ds@1952
   707
        return;
ds@1952
   708
      }
ds@1952
   709
      z = contextMalloc(context, i+n+3);
ds@1952
   710
      if( z ){
ds@1952
   711
        z[0] = '\'';
ds@1952
   712
        for(i=0, j=1; zArg[i]; i++){
ds@1952
   713
          z[j++] = zArg[i];
ds@1952
   714
          if( zArg[i]=='\'' ){
ds@1952
   715
            z[j++] = '\'';
ds@1952
   716
          }
ds@1952
   717
        }
ds@1952
   718
        z[j++] = '\'';
ds@1952
   719
        z[j] = 0;
ds@1952
   720
        sqlite3_result_text(context, z, j, sqlite3_free);
ds@1952
   721
      }
ds@1952
   722
    }
ds@1952
   723
  }
ds@1952
   724
}
ds@1952
   725
ds@1952
   726
/*
ds@1952
   727
** The hex() function.  Interpret the argument as a blob.  Return
ds@1952
   728
** a hexadecimal rendering as text.
ds@1952
   729
*/
ds@1952
   730
static void hexFunc(
ds@1952
   731
  sqlite3_context *context,
ds@1952
   732
  int argc,
ds@1952
   733
  sqlite3_value **argv
ds@1952
   734
){
ds@1952
   735
  int i, n;
ds@1952
   736
  const unsigned char *pBlob;
ds@1952
   737
  char *zHex, *z;
ds@1952
   738
  assert( argc==1 );
ds@1952
   739
  pBlob = sqlite3_value_blob(argv[0]);
ds@1952
   740
  n = sqlite3_value_bytes(argv[0]);
ds@1952
   741
  if( n*2+1>SQLITE_MAX_LENGTH ){
ds@1952
   742
    sqlite3_result_error_toobig(context);
ds@1952
   743
    return;
ds@1952
   744
  }
ds@1952
   745
  assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
ds@1952
   746
  z = zHex = contextMalloc(context, n*2 + 1);
ds@1952
   747
  if( zHex ){
ds@1952
   748
    for(i=0; i<n; i++, pBlob++){
ds@1952
   749
      unsigned char c = *pBlob;
ds@1952
   750
      *(z++) = hexdigits[(c>>4)&0xf];
ds@1952
   751
      *(z++) = hexdigits[c&0xf];
ds@1952
   752
    }
ds@1952
   753
    *z = 0;
ds@1952
   754
    sqlite3_result_text(context, zHex, n*2, sqlite3_free);
ds@1952
   755
  }
ds@1952
   756
}
ds@1952
   757
ds@1952
   758
/*
ds@1952
   759
** The zeroblob(N) function returns a zero-filled blob of size N bytes.
ds@1952
   760
*/
ds@1952
   761
static void zeroblobFunc(
ds@1952
   762
  sqlite3_context *context,
ds@1952
   763
  int argc,
ds@1952
   764
  sqlite3_value **argv
ds@1952
   765
){
ds@1952
   766
  i64 n;
ds@1952
   767
  assert( argc==1 );
ds@1952
   768
  n = sqlite3_value_int64(argv[0]);
ds@1952
   769
  if( n>SQLITE_MAX_LENGTH ){
ds@1952
   770
    sqlite3_result_error_toobig(context);
ds@1952
   771
  }else{
ds@1952
   772
    sqlite3_result_zeroblob(context, n);
ds@1952
   773
  }
ds@1952
   774
}
ds@1952
   775
ds@1952
   776
/*
ds@1952
   777
** The replace() function.  Three arguments are all strings: call
ds@1952
   778
** them A, B, and C. The result is also a string which is derived
ds@1952
   779
** from A by replacing every occurance of B with C.  The match
ds@1952
   780
** must be exact.  Collating sequences are not used.
ds@1952
   781
*/
ds@1952
   782
static void replaceFunc(
ds@1952
   783
  sqlite3_context *context,
ds@1952
   784
  int argc,
ds@1952
   785
  sqlite3_value **argv
ds@1952
   786
){
ds@1952
   787
  const unsigned char *zStr;        /* The input string A */
ds@1952
   788
  const unsigned char *zPattern;    /* The pattern string B */
ds@1952
   789
  const unsigned char *zRep;        /* The replacement string C */
ds@1952
   790
  unsigned char *zOut;              /* The output */
ds@1952
   791
  int nStr;                /* Size of zStr */
ds@1952
   792
  int nPattern;            /* Size of zPattern */
ds@1952
   793
  int nRep;                /* Size of zRep */
ds@1952
   794
  i64 nOut;                /* Maximum size of zOut */
ds@1952
   795
  int loopLimit;           /* Last zStr[] that might match zPattern[] */
ds@1952
   796
  int i, j;                /* Loop counters */
ds@1952
   797
ds@1952
   798
  assert( argc==3 );
ds@1952
   799
  zStr = sqlite3_value_text(argv[0]);
ds@1952
   800
  if( zStr==0 ) return;
ds@1952
   801
  nStr = sqlite3_value_bytes(argv[0]);
ds@1952
   802
  assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
ds@1952
   803
  zPattern = sqlite3_value_text(argv[1]);
ds@1952
   804
  if( zPattern==0 || zPattern[0]==0 ) return;
ds@1952
   805
  nPattern = sqlite3_value_bytes(argv[1]);
ds@1952
   806
  assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
ds@1952
   807
  zRep = sqlite3_value_text(argv[2]);
ds@1952
   808
  if( zRep==0 ) return;
ds@1952
   809
  nRep = sqlite3_value_bytes(argv[2]);
ds@1952
   810
  assert( zRep==sqlite3_value_text(argv[2]) );
ds@1952
   811
  nOut = nStr + 1;
ds@1952
   812
  assert( nOut<SQLITE_MAX_LENGTH );
ds@1952
   813
  zOut = contextMalloc(context, (int)nOut);
ds@1952
   814
  if( zOut==0 ){
ds@1952
   815
    return;
ds@1952
   816
  }
ds@1952
   817
  loopLimit = nStr - nPattern;  
ds@1952
   818
  for(i=j=0; i<=loopLimit; i++){
ds@1952
   819
    if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
ds@1952
   820
      zOut[j++] = zStr[i];
ds@1952
   821
    }else{
ds@1952
   822
      u8 *zOld;
ds@1952
   823
      nOut += nRep - nPattern;
ds@1952
   824
      if( nOut>=SQLITE_MAX_LENGTH ){
ds@1952
   825
        sqlite3_result_error_toobig(context);
ds@1952
   826
        sqlite3_free(zOut);
ds@1952
   827
        return;
ds@1952
   828
      }
ds@1952
   829
      zOld = zOut;
ds@1952
   830
      zOut = sqlite3_realloc(zOut, (int)nOut);
ds@1952
   831
      if( zOut==0 ){
ds@1952
   832
        sqlite3_result_error_nomem(context);
ds@1952
   833
        sqlite3_free(zOld);
ds@1952
   834
        return;
ds@1952
   835
      }
ds@1952
   836
      memcpy(&zOut[j], zRep, nRep);
ds@1952
   837
      j += nRep;
ds@1952
   838
      i += nPattern-1;
ds@1952
   839
    }
ds@1952
   840
  }
ds@1952
   841
  assert( j+nStr-i+1==nOut );
ds@1952
   842
  memcpy(&zOut[j], &zStr[i], nStr-i);
ds@1952
   843
  j += nStr - i;
ds@1952
   844
  assert( j<=nOut );
ds@1952
   845
  zOut[j] = 0;
ds@1952
   846
  sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
ds@1952
   847
}
ds@1952
   848
ds@1952
   849
/*
ds@1952
   850
** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
ds@1952
   851
** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
ds@1952
   852
*/
ds@1952
   853
static void trimFunc(
ds@1952
   854
  sqlite3_context *context,
ds@1952
   855
  int argc,
ds@1952
   856
  sqlite3_value **argv
ds@1952
   857
){
ds@1952
   858
  const unsigned char *zIn;         /* Input string */
ds@1952
   859
  const unsigned char *zCharSet;    /* Set of characters to trim */
ds@1952
   860
  int nIn;                          /* Number of bytes in input */
ds@1952
   861
  int flags;                        /* 1: trimleft  2: trimright  3: trim */
ds@1952
   862
  int i;                            /* Loop counter */
ds@1952
   863
  unsigned char *aLen;              /* Length of each character in zCharSet */
ds@1952
   864
  const unsigned char **azChar;     /* Individual characters in zCharSet */
ds@1952
   865
  int nChar;                        /* Number of characters in zCharSet */
ds@1952
   866
ds@1952
   867
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
ds@1952
   868
    return;
ds@1952
   869
  }
ds@1952
   870
  zIn = sqlite3_value_text(argv[0]);
ds@1952
   871
  if( zIn==0 ) return;
ds@1952
   872
  nIn = sqlite3_value_bytes(argv[0]);
ds@1952
   873
  assert( zIn==sqlite3_value_text(argv[0]) );
ds@1952
   874
  if( argc==1 ){
ds@1952
   875
    static const unsigned char lenOne[] = { 1 };
ds@1952
   876
    static const unsigned char *azOne[] = { (u8*)" " };
ds@1952
   877
    nChar = 1;
ds@1952
   878
    aLen = (u8*)lenOne;
ds@1952
   879
    azChar = azOne;
ds@1952
   880
    zCharSet = 0;
ds@1952
   881
  }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
ds@1952
   882
    return;
ds@1952
   883
  }else{
ds@1952
   884
    const unsigned char *z;
ds@1952
   885
    for(z=zCharSet, nChar=0; *z; nChar++){
ds@1952
   886
      SQLITE_SKIP_UTF8(z);
ds@1952
   887
    }
ds@1952
   888
    if( nChar>0 ){
ds@1952
   889
      azChar = contextMalloc(context, nChar*(sizeof(char*)+1));
ds@1952
   890
      if( azChar==0 ){
ds@1952
   891
        return;
ds@1952
   892
      }
ds@1952
   893
      aLen = (unsigned char*)&azChar[nChar];
ds@1952
   894
      for(z=zCharSet, nChar=0; *z; nChar++){
ds@1952
   895
        azChar[nChar] = z;
ds@1952
   896
        SQLITE_SKIP_UTF8(z);
ds@1952
   897
        aLen[nChar] = z - azChar[nChar];
ds@1952
   898
      }
ds@1952
   899
    }
ds@1952
   900
  }
ds@1952
   901
  if( nChar>0 ){
ds@1952
   902
    flags = (int)sqlite3_user_data(context);
ds@1952
   903
    if( flags & 1 ){
ds@1952
   904
      while( nIn>0 ){
ds@1952
   905
        int len;
ds@1952
   906
        for(i=0; i<nChar; i++){
ds@1952
   907
          len = aLen[i];
ds@1952
   908
          if( memcmp(zIn, azChar[i], len)==0 ) break;
ds@1952
   909
        }
ds@1952
   910
        if( i>=nChar ) break;
ds@1952
   911
        zIn += len;
ds@1952
   912
        nIn -= len;
ds@1952
   913
      }
ds@1952
   914
    }
ds@1952
   915
    if( flags & 2 ){
ds@1952
   916
      while( nIn>0 ){
ds@1952
   917
        int len;
ds@1952
   918
        for(i=0; i<nChar; i++){
ds@1952
   919
          len = aLen[i];
ds@1952
   920
          if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
ds@1952
   921
        }
ds@1952
   922
        if( i>=nChar ) break;
ds@1952
   923
        nIn -= len;
ds@1952
   924
      }
ds@1952
   925
    }
ds@1952
   926
    if( zCharSet ){
ds@1952
   927
      sqlite3_free(azChar);
ds@1952
   928
    }
ds@1952
   929
  }
ds@1952
   930
  sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
ds@1952
   931
}
ds@1952
   932
ds@1952
   933
#ifdef SQLITE_SOUNDEX
ds@1952
   934
/*
ds@1952
   935
** Compute the soundex encoding of a word.
ds@1952
   936
*/
ds@1952
   937
static void soundexFunc(
ds@1952
   938
  sqlite3_context *context,
ds@1952
   939
  int argc,
ds@1952
   940
  sqlite3_value **argv
ds@1952
   941
){
ds@1952
   942
  char zResult[8];
ds@1952
   943
  const u8 *zIn;
ds@1952
   944
  int i, j;
ds@1952
   945
  static const unsigned char iCode[] = {
ds@1952
   946
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ds@1952
   947
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ds@1952
   948
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ds@1952
   949
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ds@1952
   950
    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
ds@1952
   951
    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
ds@1952
   952
    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
ds@1952
   953
    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
ds@1952
   954
  };
ds@1952
   955
  assert( argc==1 );
ds@1952
   956
  zIn = (u8*)sqlite3_value_text(argv[0]);
ds@1952
   957
  if( zIn==0 ) zIn = (u8*)"";
ds@1952
   958
  for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
ds@1952
   959
  if( zIn[i] ){
ds@1952
   960
    u8 prevcode = iCode[zIn[i]&0x7f];
ds@1952
   961
    zResult[0] = toupper(zIn[i]);
ds@1952
   962
    for(j=1; j<4 && zIn[i]; i++){
ds@1952
   963
      int code = iCode[zIn[i]&0x7f];
ds@1952
   964
      if( code>0 ){
ds@1952
   965
        if( code!=prevcode ){
ds@1952
   966
          prevcode = code;
ds@1952
   967
          zResult[j++] = code + '0';
ds@1952
   968
        }
ds@1952
   969
      }else{
ds@1952
   970
        prevcode = 0;
ds@1952
   971
      }
ds@1952
   972
    }
ds@1952
   973
    while( j<4 ){
ds@1952
   974
      zResult[j++] = '0';
ds@1952
   975
    }
ds@1952
   976
    zResult[j] = 0;
ds@1952
   977
    sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
ds@1952
   978
  }else{
ds@1952
   979
    sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
ds@1952
   980
  }
ds@1952
   981
}
ds@1952
   982
#endif
ds@1952
   983
ds@1952
   984
#ifndef SQLITE_OMIT_LOAD_EXTENSION
ds@1952
   985
/*
ds@1952
   986
** A function that loads a shared-library extension then returns NULL.
ds@1952
   987
*/
ds@1952
   988
static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
ds@1952
   989
  const char *zFile = (const char *)sqlite3_value_text(argv[0]);
ds@1952
   990
  const char *zProc;
ds@1952
   991
  sqlite3 *db = sqlite3_user_data(context);
ds@1952
   992
  char *zErrMsg = 0;
ds@1952
   993
ds@1952
   994
  if( argc==2 ){
ds@1952
   995
    zProc = (const char *)sqlite3_value_text(argv[1]);
ds@1952
   996
  }else{
ds@1952
   997
    zProc = 0;
ds@1952
   998
  }
ds@1952
   999
  if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
ds@1952
  1000
    sqlite3_result_error(context, zErrMsg, -1);
ds@1952
  1001
    sqlite3_free(zErrMsg);
ds@1952
  1002
  }
ds@1952
  1003
}
ds@1952
  1004
#endif
ds@1952
  1005
ds@1952
  1006
#ifdef SQLITE_TEST
ds@1952
  1007
/*
ds@1952
  1008
** This function generates a string of random characters.  Used for
ds@1952
  1009
** generating test data.
ds@1952
  1010
*/
ds@1952
  1011
static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
ds@1952
  1012
  static const unsigned char zSrc[] = 
ds@1952
  1013
     "abcdefghijklmnopqrstuvwxyz"
ds@1952
  1014
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ds@1952
  1015
     "0123456789"
ds@1952
  1016
     ".-!,:*^+=_|?/<> ";
ds@1952
  1017
  int iMin, iMax, n, r, i;
ds@1952
  1018
  unsigned char zBuf[1000];
ds@1952
  1019
ds@1952
  1020
  /* It used to be possible to call randstr() with any number of arguments,
ds@1952
  1021
  ** but now it is registered with SQLite as requiring exactly 2.
ds@1952
  1022
  */
ds@1952
  1023
  assert(argc==2);
ds@1952
  1024
ds@1952
  1025
  iMin = sqlite3_value_int(argv[0]);
ds@1952
  1026
  if( iMin<0 ) iMin = 0;
ds@1952
  1027
  if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
ds@1952
  1028
  iMax = sqlite3_value_int(argv[1]);
ds@1952
  1029
  if( iMax<iMin ) iMax = iMin;
ds@1952
  1030
  if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
ds@1952
  1031
  n = iMin;
ds@1952
  1032
  if( iMax>iMin ){
ds@1952
  1033
    sqlite3Randomness(sizeof(r), &r);
ds@1952
  1034
    r &= 0x7fffffff;
ds@1952
  1035
    n += r%(iMax + 1 - iMin);
ds@1952
  1036
  }
ds@1952
  1037
  assert( n<sizeof(zBuf) );
ds@1952
  1038
  sqlite3Randomness(n, zBuf);
ds@1952
  1039
  for(i=0; i<n; i++){
ds@1952
  1040
    zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
ds@1952
  1041
  }
ds@1952
  1042
  zBuf[n] = 0;
ds@1952
  1043
  sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
ds@1952
  1044
}
ds@1952
  1045
#endif /* SQLITE_TEST */
ds@1952
  1046
ds@1952
  1047
#ifdef SQLITE_TEST
ds@1952
  1048
/*
ds@1952
  1049
** The following two SQL functions are used to test returning a text
ds@1952
  1050
** result with a destructor. Function 'test_destructor' takes one argument
ds@1952
  1051
** and returns the same argument interpreted as TEXT. A destructor is
ds@1952
  1052
** passed with the sqlite3_result_text() call.
ds@1952
  1053
**
ds@1952
  1054
** SQL function 'test_destructor_count' returns the number of outstanding 
ds@1952
  1055
** allocations made by 'test_destructor';
ds@1952
  1056
**
ds@1952
  1057
** WARNING: Not threadsafe.
ds@1952
  1058
*/
ds@1952
  1059
static int test_destructor_count_var = 0;
ds@1952
  1060
static void destructor(void *p){
ds@1952
  1061
  char *zVal = (char *)p;
ds@1952
  1062
  assert(zVal);
ds@1952
  1063
  zVal--;
ds@1952
  1064
  sqlite3_free(zVal);
ds@1952
  1065
  test_destructor_count_var--;
ds@1952
  1066
}
ds@1952
  1067
static void test_destructor(
ds@1952
  1068
  sqlite3_context *pCtx, 
ds@1952
  1069
  int nArg,
ds@1952
  1070
  sqlite3_value **argv
ds@1952
  1071
){
ds@1952
  1072
  char *zVal;
ds@1952
  1073
  int len;
ds@1952
  1074
  sqlite3 *db = sqlite3_user_data(pCtx);
ds@1952
  1075
 
ds@1952
  1076
  test_destructor_count_var++;
ds@1952
  1077
  assert( nArg==1 );
ds@1952
  1078
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
ds@1952
  1079
  len = sqlite3ValueBytes(argv[0], ENC(db)); 
ds@1952
  1080
  zVal = contextMalloc(pCtx, len+3);
ds@1952
  1081
  if( !zVal ){
ds@1952
  1082
    return;
ds@1952
  1083
  }
ds@1952
  1084
  zVal[len+1] = 0;
ds@1952
  1085
  zVal[len+2] = 0;
ds@1952
  1086
  zVal++;
ds@1952
  1087
  memcpy(zVal, sqlite3ValueText(argv[0], ENC(db)), len);
ds@1952
  1088
  if( ENC(db)==SQLITE_UTF8 ){
ds@1952
  1089
    sqlite3_result_text(pCtx, zVal, -1, destructor);
ds@1952
  1090
#ifndef SQLITE_OMIT_UTF16
ds@1952
  1091
  }else if( ENC(db)==SQLITE_UTF16LE ){
ds@1952
  1092
    sqlite3_result_text16le(pCtx, zVal, -1, destructor);
ds@1952
  1093
  }else{
ds@1952
  1094
    sqlite3_result_text16be(pCtx, zVal, -1, destructor);
ds@1952
  1095
#endif /* SQLITE_OMIT_UTF16 */
ds@1952
  1096
  }
ds@1952
  1097
}
ds@1952
  1098
static void test_destructor_count(
ds@1952
  1099
  sqlite3_context *pCtx, 
ds@1952
  1100
  int nArg,
ds@1952
  1101
  sqlite3_value **argv
ds@1952
  1102
){
ds@1952
  1103
  sqlite3_result_int(pCtx, test_destructor_count_var);
ds@1952
  1104
}
ds@1952
  1105
#endif /* SQLITE_TEST */
ds@1952
  1106
ds@1952
  1107
#ifdef SQLITE_TEST
ds@1952
  1108
/*
ds@1952
  1109
** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata()
ds@1952
  1110
** interface.
ds@1952
  1111
**
ds@1952
  1112
** The test_auxdata() SQL function attempts to register each of its arguments
ds@1952
  1113
** as auxiliary data.  If there are no prior registrations of aux data for
ds@1952
  1114
** that argument (meaning the argument is not a constant or this is its first
ds@1952
  1115
** call) then the result for that argument is 0.  If there is a prior
ds@1952
  1116
** registration, the result for that argument is 1.  The overall result
ds@1952
  1117
** is the individual argument results separated by spaces.
ds@1952
  1118
*/
ds@1952
  1119
static void free_test_auxdata(void *p) {sqlite3_free(p);}
ds@1952
  1120
static void test_auxdata(
ds@1952
  1121
  sqlite3_context *pCtx, 
ds@1952
  1122
  int nArg,
ds@1952
  1123
  sqlite3_value **argv
ds@1952
  1124
){
ds@1952
  1125
  int i;
ds@1952
  1126
  char *zRet = contextMalloc(pCtx, nArg*2);
ds@1952
  1127
  if( !zRet ) return;
ds@1952
  1128
  memset(zRet, 0, nArg*2);
ds@1952
  1129
  for(i=0; i<nArg; i++){
ds@1952
  1130
    char const *z = (char*)sqlite3_value_text(argv[i]);
ds@1952
  1131
    if( z ){
ds@1952
  1132
      char *zAux = sqlite3_get_auxdata(pCtx, i);
ds@1952
  1133
      if( zAux ){
ds@1952
  1134
        zRet[i*2] = '1';
ds@1952
  1135
        if( strcmp(zAux, z) ){
ds@1952
  1136
          sqlite3_result_error(pCtx, "Auxilary data corruption", -1);
ds@1952
  1137
          return;
ds@1952
  1138
        }
ds@1952
  1139
      }else {
ds@1952
  1140
        zRet[i*2] = '0';
ds@1952
  1141
      }
ds@1952
  1142
ds@1952
  1143
      zAux = contextMalloc(pCtx, strlen(z)+1);
ds@1952
  1144
      if( zAux ){
ds@1952
  1145
        strcpy(zAux, z);
ds@1952
  1146
        sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
ds@1952
  1147
      }
ds@1952
  1148
      zRet[i*2+1] = ' ';
ds@1952
  1149
    }
ds@1952
  1150
  }
ds@1952
  1151
  sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
ds@1952
  1152
}
ds@1952
  1153
#endif /* SQLITE_TEST */
ds@1952
  1154
ds@1952
  1155
#ifdef SQLITE_TEST
ds@1952
  1156
/*
ds@1952
  1157
** A function to test error reporting from user functions. This function
ds@1952
  1158
** returns a copy of it's first argument as an error.
ds@1952
  1159
*/
ds@1952
  1160
static void test_error(
ds@1952
  1161
  sqlite3_context *pCtx, 
ds@1952
  1162
  int nArg,
ds@1952
  1163
  sqlite3_value **argv
ds@1952
  1164
){
ds@1952
  1165
  sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), 0);
ds@1952
  1166
}
ds@1952
  1167
#endif /* SQLITE_TEST */
ds@1952
  1168
ds@1952
  1169
/*
ds@1952
  1170
** An instance of the following structure holds the context of a
ds@1952
  1171
** sum() or avg() aggregate computation.
ds@1952
  1172
*/
ds@1952
  1173
typedef struct SumCtx SumCtx;
ds@1952
  1174
struct SumCtx {
ds@1952
  1175
  double rSum;      /* Floating point sum */
ds@1952
  1176
  i64 iSum;         /* Integer sum */   
ds@1952
  1177
  i64 cnt;          /* Number of elements summed */
ds@1952
  1178
  u8 overflow;      /* True if integer overflow seen */
ds@1952
  1179
  u8 approx;        /* True if non-integer value was input to the sum */
ds@1952
  1180
};
ds@1952
  1181
ds@1952
  1182
/*
ds@1952
  1183
** Routines used to compute the sum, average, and total.
ds@1952
  1184
**
ds@1952
  1185
** The SUM() function follows the (broken) SQL standard which means
ds@1952
  1186
** that it returns NULL if it sums over no inputs.  TOTAL returns
ds@1952
  1187
** 0.0 in that case.  In addition, TOTAL always returns a float where
ds@1952
  1188
** SUM might return an integer if it never encounters a floating point
ds@1952
  1189
** value.  TOTAL never fails, but SUM might through an exception if
ds@1952
  1190
** it overflows an integer.
ds@1952
  1191
*/
ds@1952
  1192
static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
ds@1952
  1193
  SumCtx *p;
ds@1952
  1194
  int type;
ds@1952
  1195
  assert( argc==1 );
ds@1952
  1196
  p = sqlite3_aggregate_context(context, sizeof(*p));
ds@1952
  1197
  type = sqlite3_value_numeric_type(argv[0]);
ds@1952
  1198
  if( p && type!=SQLITE_NULL ){
ds@1952
  1199
    p->cnt++;
ds@1952
  1200
    if( type==SQLITE_INTEGER ){
ds@1952
  1201
      i64 v = sqlite3_value_int64(argv[0]);
ds@1952
  1202
      p->rSum += v;
ds@1952
  1203
      if( (p->approx|p->overflow)==0 ){
ds@1952
  1204
        i64 iNewSum = p->iSum + v;
ds@1952
  1205
        int s1 = p->iSum >> (sizeof(i64)*8-1);
ds@1952
  1206
        int s2 = v       >> (sizeof(i64)*8-1);
ds@1952
  1207
        int s3 = iNewSum >> (sizeof(i64)*8-1);
ds@1952
  1208
        p->overflow = (s1&s2&~s3) | (~s1&~s2&s3);
ds@1952
  1209
        p->iSum = iNewSum;
ds@1952
  1210
      }
ds@1952
  1211
    }else{
ds@1952
  1212
      p->rSum += sqlite3_value_double(argv[0]);
ds@1952
  1213
      p->approx = 1;
ds@1952
  1214
    }
ds@1952
  1215
  }
ds@1952
  1216
}
ds@1952
  1217
static void sumFinalize(sqlite3_context *context){
ds@1952
  1218
  SumCtx *p;
ds@1952
  1219
  p = sqlite3_aggregate_context(context, 0);
ds@1952
  1220
  if( p && p->cnt>0 ){
ds@1952
  1221
    if( p->overflow ){
ds@1952
  1222
      sqlite3_result_error(context,"integer overflow",-1);
ds@1952
  1223
    }else if( p->approx ){
ds@1952
  1224
      sqlite3_result_double(context, p->rSum);
ds@1952
  1225
    }else{
ds@1952
  1226
      sqlite3_result_int64(context, p->iSum);
ds@1952
  1227
    }
ds@1952
  1228
  }
ds@1952
  1229
}
ds@1952
  1230
static void avgFinalize(sqlite3_context *context){
ds@1952
  1231
  SumCtx *p;
ds@1952
  1232
  p = sqlite3_aggregate_context(context, 0);
ds@1952
  1233
  if( p && p->cnt>0 ){
ds@1952
  1234
    sqlite3_result_double(context, p->rSum/(double)p->cnt);
ds@1952
  1235
  }
ds@1952
  1236
}
ds@1952
  1237
static void totalFinalize(sqlite3_context *context){
ds@1952
  1238
  SumCtx *p;
ds@1952
  1239
  p = sqlite3_aggregate_context(context, 0);
ds@1952
  1240
  sqlite3_result_double(context, p ? p->rSum : 0.0);
ds@1952
  1241
}
ds@1952
  1242
ds@1952
  1243
/*
ds@1952
  1244
** The following structure keeps track of state information for the
ds@1952
  1245
** count() aggregate function.
ds@1952
  1246
*/
ds@1952
  1247
typedef struct CountCtx CountCtx;
ds@1952
  1248
struct CountCtx {
ds@1952
  1249
  i64 n;
ds@1952
  1250
};
ds@1952
  1251
ds@1952
  1252
/*
ds@1952
  1253
** Routines to implement the count() aggregate function.
ds@1952
  1254
*/
ds@1952
  1255
static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
ds@1952
  1256
  CountCtx *p;
ds@1952
  1257
  p = sqlite3_aggregate_context(context, sizeof(*p));
ds@1952
  1258
  if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
ds@1952
  1259
    p->n++;
ds@1952
  1260
  }
ds@1952
  1261
}   
ds@1952
  1262
static void countFinalize(sqlite3_context *context){
ds@1952
  1263
  CountCtx *p;
ds@1952
  1264
  p = sqlite3_aggregate_context(context, 0);
ds@1952
  1265
  sqlite3_result_int64(context, p ? p->n : 0);
ds@1952
  1266
}
ds@1952
  1267
ds@1952
  1268
/*
ds@1952
  1269
** Routines to implement min() and max() aggregate functions.
ds@1952
  1270
*/
ds@1952
  1271
static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){
ds@1952
  1272
  Mem *pArg  = (Mem *)argv[0];
ds@1952
  1273
  Mem *pBest;
ds@1952
  1274
ds@1952
  1275
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
ds@1952
  1276
  pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
ds@1952
  1277
  if( !pBest ) return;
ds@1952
  1278
ds@1952
  1279
  if( pBest->flags ){
ds@1952
  1280
    int max;
ds@1952
  1281
    int cmp;
ds@1952
  1282
    CollSeq *pColl = sqlite3GetFuncCollSeq(context);
ds@1952
  1283
    /* This step function is used for both the min() and max() aggregates,
ds@1952
  1284
    ** the only difference between the two being that the sense of the
ds@1952
  1285
    ** comparison is inverted. For the max() aggregate, the
ds@1952
  1286
    ** sqlite3_user_data() function returns (void *)-1. For min() it
ds@1952
  1287
    ** returns (void *)db, where db is the sqlite3* database pointer.
ds@1952
  1288
    ** Therefore the next statement sets variable 'max' to 1 for the max()
ds@1952
  1289
    ** aggregate, or 0 for min().
ds@1952
  1290
    */
ds@1952
  1291
    max = sqlite3_user_data(context)!=0;
ds@1952
  1292
    cmp = sqlite3MemCompare(pBest, pArg, pColl);
ds@1952
  1293
    if( (max && cmp<0) || (!max && cmp>0) ){
ds@1952
  1294
      sqlite3VdbeMemCopy(pBest, pArg);
ds@1952
  1295
    }
ds@1952
  1296
  }else{
ds@1952
  1297
    sqlite3VdbeMemCopy(pBest, pArg);
ds@1952
  1298
  }
ds@1952
  1299
}
ds@1952
  1300
static void minMaxFinalize(sqlite3_context *context){
ds@1952
  1301
  sqlite3_value *pRes;
ds@1952
  1302
  pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
ds@1952
  1303
  if( pRes ){
ds@1952
  1304
    if( pRes->flags ){
ds@1952
  1305
      sqlite3_result_value(context, pRes);
ds@1952
  1306
    }
ds@1952
  1307
    sqlite3VdbeMemRelease(pRes);
ds@1952
  1308
  }
ds@1952
  1309
}
ds@1952
  1310
ds@1952
  1311
ds@1952
  1312
/*
ds@1952
  1313
** This function registered all of the above C functions as SQL
ds@1952
  1314
** functions.  This should be the only routine in this file with
ds@1952
  1315
** external linkage.
ds@1952
  1316
*/
ds@1952
  1317
void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
ds@1952
  1318
  static const struct {
ds@1952
  1319
     char *zName;
ds@1952
  1320
     signed char nArg;
ds@1952
  1321
     u8 argType;           /* ff: db   1: 0, 2: 1, 3: 2,...  N:  N-1. */
ds@1952
  1322
     u8 eTextRep;          /* 1: UTF-16.  0: UTF-8 */
ds@1952
  1323
     u8 needCollSeq;
ds@1952
  1324
     void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
ds@1952
  1325
  } aFuncs[] = {
ds@1952
  1326
    { "min",               -1, 0, SQLITE_UTF8,    1, minmaxFunc },
ds@1952
  1327
    { "min",                0, 0, SQLITE_UTF8,    1, 0          },
ds@1952
  1328
    { "max",               -1, 1, SQLITE_UTF8,    1, minmaxFunc },
ds@1952
  1329
    { "max",                0, 1, SQLITE_UTF8,    1, 0          },
ds@1952
  1330
    { "typeof",             1, 0, SQLITE_UTF8,    0, typeofFunc },
ds@1952
  1331
    { "length",             1, 0, SQLITE_UTF8,    0, lengthFunc },
ds@1952
  1332
    { "substr",             3, 0, SQLITE_UTF8,    0, substrFunc },
ds@1952
  1333
    { "abs",                1, 0, SQLITE_UTF8,    0, absFunc    },
ds@1952
  1334
    { "round",              1, 0, SQLITE_UTF8,    0, roundFunc  },
ds@1952
  1335
    { "round",              2, 0, SQLITE_UTF8,    0, roundFunc  },
ds@1952
  1336
    { "upper",              1, 0, SQLITE_UTF8,    0, upperFunc  },
ds@1952
  1337
    { "lower",              1, 0, SQLITE_UTF8,    0, lowerFunc  },
ds@1952
  1338
    { "coalesce",          -1, 0, SQLITE_UTF8,    0, ifnullFunc },
ds@1952
  1339
    { "coalesce",           0, 0, SQLITE_UTF8,    0, 0          },
ds@1952
  1340
    { "coalesce",           1, 0, SQLITE_UTF8,    0, 0          },
ds@1952
  1341
    { "hex",                1, 0, SQLITE_UTF8,    0, hexFunc    },
ds@1952
  1342
    { "ifnull",             2, 0, SQLITE_UTF8,    1, ifnullFunc },
ds@1952
  1343
    { "random",            -1, 0, SQLITE_UTF8,    0, randomFunc },
ds@1952
  1344
    { "randomblob",         1, 0, SQLITE_UTF8,    0, randomBlob },
ds@1952
  1345
    { "nullif",             2, 0, SQLITE_UTF8,    1, nullifFunc },
ds@1952
  1346
    { "sqlite_version",     0, 0, SQLITE_UTF8,    0, versionFunc},
ds@1952
  1347
    { "quote",              1, 0, SQLITE_UTF8,    0, quoteFunc  },
ds@1952
  1348
    { "last_insert_rowid",  0, 0xff, SQLITE_UTF8, 0, last_insert_rowid },
ds@1952
  1349
    { "changes",            0, 0xff, SQLITE_UTF8, 0, changes           },
ds@1952
  1350
    { "total_changes",      0, 0xff, SQLITE_UTF8, 0, total_changes     },
ds@1952
  1351
    { "replace",            3, 0, SQLITE_UTF8,    0, replaceFunc       },
ds@1952
  1352
    { "ltrim",              1, 1, SQLITE_UTF8,    0, trimFunc          },
ds@1952
  1353
    { "ltrim",              2, 1, SQLITE_UTF8,    0, trimFunc          },
ds@1952
  1354
    { "rtrim",              1, 2, SQLITE_UTF8,    0, trimFunc          },
ds@1952
  1355
    { "rtrim",              2, 2, SQLITE_UTF8,    0, trimFunc          },
ds@1952
  1356
    { "trim",               1, 3, SQLITE_UTF8,    0, trimFunc          },
ds@1952
  1357
    { "trim",               2, 3, SQLITE_UTF8,    0, trimFunc          },
ds@1952
  1358
    { "zeroblob",           1, 0, SQLITE_UTF8,    0, zeroblobFunc      },
ds@1952
  1359
#ifdef SQLITE_SOUNDEX
ds@1952
  1360
    { "soundex",            1, 0, SQLITE_UTF8,    0, soundexFunc},
ds@1952
  1361
#endif
ds@1952
  1362
#ifndef SQLITE_OMIT_LOAD_EXTENSION
ds@1952
  1363
    { "load_extension",     1, 0xff, SQLITE_UTF8, 0, loadExt },
ds@1952
  1364
    { "load_extension",     2, 0xff, SQLITE_UTF8, 0, loadExt },
ds@1952
  1365
#endif
ds@1952
  1366
#ifdef SQLITE_TEST
ds@1952
  1367
    { "randstr",               2, 0,    SQLITE_UTF8, 0, randStr    },
ds@1952
  1368
    { "test_destructor",       1, 0xff, SQLITE_UTF8, 0, test_destructor},
ds@1952
  1369
    { "test_destructor_count", 0, 0,    SQLITE_UTF8, 0, test_destructor_count},
ds@1952
  1370
    { "test_auxdata",         -1, 0,    SQLITE_UTF8, 0, test_auxdata},
ds@1952
  1371
    { "test_error",            1, 0,    SQLITE_UTF8, 0, test_error},
ds@1952
  1372
#endif
ds@1952
  1373
  };
ds@1952
  1374
  static const struct {
ds@1952
  1375
    char *zName;
ds@1952
  1376
    signed char nArg;
ds@1952
  1377
    u8 argType;
ds@1952
  1378
    u8 needCollSeq;
ds@1952
  1379
    void (*xStep)(sqlite3_context*,int,sqlite3_value**);
ds@1952
  1380
    void (*xFinalize)(sqlite3_context*);
ds@1952
  1381
  } aAggs[] = {
ds@1952
  1382
    { "min",    1, 0, 1, minmaxStep,   minMaxFinalize },
ds@1952
  1383
    { "max",    1, 1, 1, minmaxStep,   minMaxFinalize },
ds@1952
  1384
    { "sum",    1, 0, 0, sumStep,      sumFinalize    },
ds@1952
  1385
    { "total",  1, 0, 0, sumStep,      totalFinalize    },
ds@1952
  1386
    { "avg",    1, 0, 0, sumStep,      avgFinalize    },
ds@1952
  1387
    { "count",  0, 0, 0, countStep,    countFinalize  },
ds@1952
  1388
    { "count",  1, 0, 0, countStep,    countFinalize  },
ds@1952
  1389
  };
ds@1952
  1390
  int i;
ds@1952
  1391
ds@1952
  1392
  for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
ds@1952
  1393
    void *pArg;
ds@1952
  1394
    u8 argType = aFuncs[i].argType;
ds@1952
  1395
    if( argType==0xff ){
ds@1952
  1396
      pArg = db;
ds@1952
  1397
    }else{
ds@1952
  1398
      pArg = (void*)(int)argType;
ds@1952
  1399
    }
ds@1952
  1400
    sqlite3CreateFunc(db, aFuncs[i].zName, aFuncs[i].nArg,
ds@1952
  1401
        aFuncs[i].eTextRep, pArg, aFuncs[i].xFunc, 0, 0);
ds@1952
  1402
    if( aFuncs[i].needCollSeq ){
ds@1952
  1403
      FuncDef *pFunc = sqlite3FindFunction(db, aFuncs[i].zName, 
ds@1952
  1404
          strlen(aFuncs[i].zName), aFuncs[i].nArg, aFuncs[i].eTextRep, 0);
ds@1952
  1405
      if( pFunc && aFuncs[i].needCollSeq ){
ds@1952
  1406
        pFunc->needCollSeq = 1;
ds@1952
  1407
      }
ds@1952
  1408
    }
ds@1952
  1409
  }
ds@1952
  1410
#ifndef SQLITE_OMIT_ALTERTABLE
ds@1952
  1411
  sqlite3AlterFunctions(db);
ds@1952
  1412
#endif
ds@1952
  1413
#ifndef SQLITE_OMIT_PARSER
ds@1952
  1414
  sqlite3AttachFunctions(db);
ds@1952
  1415
#endif
ds@1952
  1416
  for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
ds@1952
  1417
    void *pArg = (void*)(int)aAggs[i].argType;
ds@1952
  1418
    sqlite3CreateFunc(db, aAggs[i].zName, aAggs[i].nArg, SQLITE_UTF8, 
ds@1952
  1419
        pArg, 0, aAggs[i].xStep, aAggs[i].xFinalize);
ds@1952
  1420
    if( aAggs[i].needCollSeq ){
ds@1952
  1421
      FuncDef *pFunc = sqlite3FindFunction( db, aAggs[i].zName,
ds@1952
  1422
          strlen(aAggs[i].zName), aAggs[i].nArg, SQLITE_UTF8, 0);
ds@1952
  1423
      if( pFunc && aAggs[i].needCollSeq ){
ds@1952
  1424
        pFunc->needCollSeq = 1;
ds@1952
  1425
      }
ds@1952
  1426
    }
ds@1952
  1427
  }
ds@1952
  1428
  sqlite3RegisterDateTimeFunctions(db);
ds@1952
  1429
  if( !db->mallocFailed ){
ds@1952
  1430
    int rc = sqlite3_overload_function(db, "MATCH", 2);
ds@1952
  1431
    assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
ds@1952
  1432
    if( rc==SQLITE_NOMEM ){
ds@1952
  1433
      db->mallocFailed = 1;
ds@1952
  1434
    }
ds@1952
  1435
  }
ds@1952
  1436
#ifdef SQLITE_SSE
ds@1952
  1437
  (void)sqlite3SseFunctions(db);
ds@1952
  1438
#endif
ds@1952
  1439
#ifdef SQLITE_CASE_SENSITIVE_LIKE
ds@1952
  1440
  sqlite3RegisterLikeFunctions(db, 1);
ds@1952
  1441
#else
ds@1952
  1442
  sqlite3RegisterLikeFunctions(db, 0);
ds@1952
  1443
#endif
ds@1952
  1444
}
ds@1952
  1445
ds@1952
  1446
/*
ds@1952
  1447
** Set the LIKEOPT flag on the 2-argument function with the given name.
ds@1952
  1448
*/
ds@1952
  1449
static void setLikeOptFlag(sqlite3 *db, const char *zName, int flagVal){
ds@1952
  1450
  FuncDef *pDef;
ds@1952
  1451
  pDef = sqlite3FindFunction(db, zName, strlen(zName), 2, SQLITE_UTF8, 0);
ds@1952
  1452
  if( pDef ){
ds@1952
  1453
    pDef->flags = flagVal;
ds@1952
  1454
  }
ds@1952
  1455
}
ds@1952
  1456
ds@1952
  1457
/*
ds@1952
  1458
** Register the built-in LIKE and GLOB functions.  The caseSensitive
ds@1952
  1459
** parameter determines whether or not the LIKE operator is case
ds@1952
  1460
** sensitive.  GLOB is always case sensitive.
ds@1952
  1461
*/
ds@1952
  1462
void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
ds@1952
  1463
  struct compareInfo *pInfo;
ds@1952
  1464
  if( caseSensitive ){
ds@1952
  1465
    pInfo = (struct compareInfo*)&likeInfoAlt;
ds@1952
  1466
  }else{
ds@1952
  1467
    pInfo = (struct compareInfo*)&likeInfoNorm;
ds@1952
  1468
  }
ds@1952
  1469
  sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
ds@1952
  1470
  sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0);
ds@1952
  1471
  sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
ds@1952
  1472
      (struct compareInfo*)&globInfo, likeFunc, 0,0);
ds@1952
  1473
  setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
ds@1952
  1474
  setLikeOptFlag(db, "like", 
ds@1952
  1475
      caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
ds@1952
  1476
}
ds@1952
  1477
ds@1952
  1478
/*
ds@1952
  1479
** pExpr points to an expression which implements a function.  If
ds@1952
  1480
** it is appropriate to apply the LIKE optimization to that function
ds@1952
  1481
** then set aWc[0] through aWc[2] to the wildcard characters and
ds@1952
  1482
** return TRUE.  If the function is not a LIKE-style function then
ds@1952
  1483
** return FALSE.
ds@1952
  1484
*/
ds@1952
  1485
int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
ds@1952
  1486
  FuncDef *pDef;
ds@1952
  1487
  if( pExpr->op!=TK_FUNCTION || !pExpr->pList ){
ds@1952
  1488
    return 0;
ds@1952
  1489
  }
ds@1952
  1490
  if( pExpr->pList->nExpr!=2 ){
ds@1952
  1491
    return 0;
ds@1952
  1492
  }
ds@1952
  1493
  pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2,
ds@1952
  1494
                             SQLITE_UTF8, 0);
ds@1952
  1495
  if( pDef==0 || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
ds@1952
  1496
    return 0;
ds@1952
  1497
  }
ds@1952
  1498
ds@1952
  1499
  /* The memcpy() statement assumes that the wildcard characters are
ds@1952
  1500
  ** the first three statements in the compareInfo structure.  The
ds@1952
  1501
  ** asserts() that follow verify that assumption
ds@1952
  1502
  */
ds@1952
  1503
  memcpy(aWc, pDef->pUserData, 3);
ds@1952
  1504
  assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
ds@1952
  1505
  assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
ds@1952
  1506
  assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
ds@1952
  1507
  *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
ds@1952
  1508
  return 1;
ds@1952
  1509
}