00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <ctype.h>
00022 #include <math.h>
00023 #include <stdlib.h>
00024 #include <assert.h>
00025 #include "sqliteInt.h"
00026 #include "os.h"
00027
00028
00029
00030
00031 static void minmaxFunc(sqlite_func *context, int argc, const char **argv){
00032 const char *zBest;
00033 int i;
00034 int (*xCompare)(const char*, const char*);
00035 int mask;
00036
00037 if( argc==0 ) return;
00038 mask = (int)sqlite_user_data(context);
00039 zBest = argv[0];
00040 if( zBest==0 ) return;
00041 if( argv[1][0]=='n' ){
00042 xCompare = sqliteCompare;
00043 }else{
00044 xCompare = strcmp;
00045 }
00046 for(i=2; i<argc; i+=2){
00047 if( argv[i]==0 ) return;
00048 if( (xCompare(argv[i], zBest)^mask)<0 ){
00049 zBest = argv[i];
00050 }
00051 }
00052 sqlite_set_result_string(context, zBest, -1);
00053 }
00054
00055
00056
00057
00058 static void typeofFunc(sqlite_func *context, int argc, const char **argv){
00059 assert( argc==2 );
00060 sqlite_set_result_string(context, argv[1], -1);
00061 }
00062
00063
00064
00065
00066 static void lengthFunc(sqlite_func *context, int argc, const char **argv){
00067 const char *z;
00068 int len;
00069
00070 assert( argc==1 );
00071 z = argv[0];
00072 if( z==0 ) return;
00073 #ifdef SQLITE_UTF8
00074 for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
00075 #else
00076 len = strlen(z);
00077 #endif
00078 sqlite_set_result_int(context, len);
00079 }
00080
00081
00082
00083
00084 static void absFunc(sqlite_func *context, int argc, const char **argv){
00085 const char *z;
00086 assert( argc==1 );
00087 z = argv[0];
00088 if( z==0 ) return;
00089 if( z[0]=='-' && isdigit(z[1]) ) z++;
00090 sqlite_set_result_string(context, z, -1);
00091 }
00092
00093
00094
00095
00096 static void substrFunc(sqlite_func *context, int argc, const char **argv){
00097 const char *z;
00098 #ifdef SQLITE_UTF8
00099 const char *z2;
00100 int i;
00101 #endif
00102 int p1, p2, len;
00103 assert( argc==3 );
00104 z = argv[0];
00105 if( z==0 ) return;
00106 p1 = atoi(argv[1]?argv[1]:0);
00107 p2 = atoi(argv[2]?argv[2]:0);
00108 #ifdef SQLITE_UTF8
00109 for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
00110 #else
00111 len = strlen(z);
00112 #endif
00113 if( p1<0 ){
00114 p1 += len;
00115 if( p1<0 ){
00116 p2 += p1;
00117 p1 = 0;
00118 }
00119 }else if( p1>0 ){
00120 p1--;
00121 }
00122 if( p1+p2>len ){
00123 p2 = len-p1;
00124 }
00125 #ifdef SQLITE_UTF8
00126 for(i=0; i<p1 && z[i]; i++){
00127 if( (z[i]&0xc0)==0x80 ) p1++;
00128 }
00129 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
00130 for(; i<p1+p2 && z[i]; i++){
00131 if( (z[i]&0xc0)==0x80 ) p2++;
00132 }
00133 while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
00134 #endif
00135 if( p2<0 ) p2 = 0;
00136 sqlite_set_result_string(context, &z[p1], p2);
00137 }
00138
00139
00140
00141
00142 static void roundFunc(sqlite_func *context, int argc, const char **argv){
00143 int n;
00144 double r;
00145 char zBuf[100];
00146 assert( argc==1 || argc==2 );
00147 if( argv[0]==0 || (argc==2 && argv[1]==0) ) return;
00148 n = argc==2 ? atoi(argv[1]) : 0;
00149 if( n>30 ) n = 30;
00150 if( n<0 ) n = 0;
00151 r = sqliteAtoF(argv[0], 0);
00152 sprintf(zBuf,"%.*f",n,r);
00153 sqlite_set_result_string(context, zBuf, -1);
00154 }
00155
00156
00157
00158
00159 static void upperFunc(sqlite_func *context, int argc, const char **argv){
00160 unsigned char *z;
00161 int i;
00162 if( argc<1 || argv[0]==0 ) return;
00163 z = (unsigned char*)sqlite_set_result_string(context, argv[0], -1);
00164 if( z==0 ) return;
00165 for(i=0; z[i]; i++){
00166 if( islower(z[i]) ) z[i] = toupper(z[i]);
00167 }
00168 }
00169 static void lowerFunc(sqlite_func *context, int argc, const char **argv){
00170 unsigned char *z;
00171 int i;
00172 if( argc<1 || argv[0]==0 ) return;
00173 z = (unsigned char*)sqlite_set_result_string(context, argv[0], -1);
00174 if( z==0 ) return;
00175 for(i=0; z[i]; i++){
00176 if( isupper(z[i]) ) z[i] = tolower(z[i]);
00177 }
00178 }
00179
00180
00181
00182
00183
00184
00185 static void ifnullFunc(sqlite_func *context, int argc, const char **argv){
00186 int i;
00187 for(i=0; i<argc; i++){
00188 if( argv[i] ){
00189 sqlite_set_result_string(context, argv[i], -1);
00190 break;
00191 }
00192 }
00193 }
00194
00195
00196
00197
00198 static void randomFunc(sqlite_func *context, int argc, const char **argv){
00199 int r;
00200 sqliteRandomness(sizeof(r), &r);
00201 sqlite_set_result_int(context, r);
00202 }
00203
00204
00205
00206
00207
00208 static void last_insert_rowid(sqlite_func *context, int arg, const char **argv){
00209 sqlite *db = sqlite_user_data(context);
00210 sqlite_set_result_int(context, sqlite_last_insert_rowid(db));
00211 }
00212
00213
00214
00215
00216
00217 static void change_count(sqlite_func *context, int arg, const char **argv){
00218 sqlite *db = sqlite_user_data(context);
00219 sqlite_set_result_int(context, sqlite_changes(db));
00220 }
00221
00222
00223
00224
00225
00226 static void last_statement_change_count(sqlite_func *context, int arg,
00227 const char **argv){
00228 sqlite *db = sqlite_user_data(context);
00229 sqlite_set_result_int(context, sqlite_last_statement_changes(db));
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 static void likeFunc(sqlite_func *context, int arg, const char **argv){
00242 if( argv[0]==0 || argv[1]==0 ) return;
00243 sqlite_set_result_int(context,
00244 sqliteLikeCompare((const unsigned char*)argv[0],
00245 (const unsigned char*)argv[1]));
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 static void globFunc(sqlite_func *context, int arg, const char **argv){
00258 if( argv[0]==0 || argv[1]==0 ) return;
00259 sqlite_set_result_int(context,
00260 sqliteGlobCompare((const unsigned char*)argv[0],
00261 (const unsigned char*)argv[1]));
00262 }
00263
00264
00265
00266
00267
00268
00269 static void nullifFunc(sqlite_func *context, int argc, const char **argv){
00270 if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){
00271 sqlite_set_result_string(context, argv[0], -1);
00272 }
00273 }
00274
00275
00276
00277
00278
00279 static void versionFunc(sqlite_func *context, int argc, const char **argv){
00280 sqlite_set_result_string(context, sqlite_version, -1);
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 static void quoteFunc(sqlite_func *context, int argc, const char **argv){
00295 if( argc<1 ) return;
00296 if( argv[0]==0 ){
00297 sqlite_set_result_string(context, "NULL", 4);
00298 }else if( sqliteIsNumber(argv[0]) ){
00299 sqlite_set_result_string(context, argv[0], -1);
00300 }else{
00301 int i,j,n;
00302 char *z;
00303 for(i=n=0; argv[0][i]; i++){ if( argv[0][i]=='\'' ) n++; }
00304 z = sqliteMalloc( i+n+3 );
00305 if( z==0 ) return;
00306 z[0] = '\'';
00307 for(i=0, j=1; argv[0][i]; i++){
00308 z[j++] = argv[0][i];
00309 if( argv[0][i]=='\'' ){
00310 z[j++] = '\'';
00311 }
00312 }
00313 z[j++] = '\'';
00314 z[j] = 0;
00315 sqlite_set_result_string(context, z, j);
00316 sqliteFree(z);
00317 }
00318 }
00319
00320 #ifdef SQLITE_SOUNDEX
00321
00322
00323
00324 static void soundexFunc(sqlite_func *context, int argc, const char **argv){
00325 char zResult[8];
00326 const char *zIn;
00327 int i, j;
00328 static const unsigned char iCode[] = {
00329 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00330 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00331 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00332 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00333 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
00334 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
00335 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
00336 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
00337 };
00338 assert( argc==1 );
00339 zIn = argv[0];
00340 for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
00341 if( zIn[i] ){
00342 zResult[0] = toupper(zIn[i]);
00343 for(j=1; j<4 && zIn[i]; i++){
00344 int code = iCode[zIn[i]&0x7f];
00345 if( code>0 ){
00346 zResult[j++] = code + '0';
00347 }
00348 }
00349 while( j<4 ){
00350 zResult[j++] = '0';
00351 }
00352 zResult[j] = 0;
00353 sqlite_set_result_string(context, zResult, 4);
00354 }else{
00355 sqlite_set_result_string(context, "?000", 4);
00356 }
00357 }
00358 #endif
00359
00360 #ifdef SQLITE_TEST
00361
00362
00363
00364
00365 static void randStr(sqlite_func *context, int argc, const char **argv){
00366 static const unsigned char zSrc[] =
00367 "abcdefghijklmnopqrstuvwxyz"
00368 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00369 "0123456789"
00370 ".-!,:*^+=_|?/<> ";
00371 int iMin, iMax, n, r, i;
00372 unsigned char zBuf[1000];
00373 if( argc>=1 ){
00374 iMin = atoi(argv[0]);
00375 if( iMin<0 ) iMin = 0;
00376 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
00377 }else{
00378 iMin = 1;
00379 }
00380 if( argc>=2 ){
00381 iMax = atoi(argv[1]);
00382 if( iMax<iMin ) iMax = iMin;
00383 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
00384 }else{
00385 iMax = 50;
00386 }
00387 n = iMin;
00388 if( iMax>iMin ){
00389 sqliteRandomness(sizeof(r), &r);
00390 r &= 0x7fffffff;
00391 n += r%(iMax + 1 - iMin);
00392 }
00393 assert( n<sizeof(zBuf) );
00394 sqliteRandomness(n, zBuf);
00395 for(i=0; i<n; i++){
00396 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
00397 }
00398 zBuf[n] = 0;
00399 sqlite_set_result_string(context, zBuf, n);
00400 }
00401 #endif
00402
00403
00404
00405
00406
00407 typedef struct SumCtx SumCtx;
00408 struct SumCtx {
00409 double sum;
00410 int cnt;
00411 };
00412
00413
00414
00415
00416 static void sumStep(sqlite_func *context, int argc, const char **argv){
00417 SumCtx *p;
00418 if( argc<1 ) return;
00419 p = sqlite_aggregate_context(context, sizeof(*p));
00420 if( p && argv[0] ){
00421 p->sum += sqliteAtoF(argv[0], 0);
00422 p->cnt++;
00423 }
00424 }
00425 static void sumFinalize(sqlite_func *context){
00426 SumCtx *p;
00427 p = sqlite_aggregate_context(context, sizeof(*p));
00428 sqlite_set_result_double(context, p ? p->sum : 0.0);
00429 }
00430 static void avgFinalize(sqlite_func *context){
00431 SumCtx *p;
00432 p = sqlite_aggregate_context(context, sizeof(*p));
00433 if( p && p->cnt>0 ){
00434 sqlite_set_result_double(context, p->sum/(double)p->cnt);
00435 }
00436 }
00437
00438
00439
00440
00441
00442 typedef struct StdDevCtx StdDevCtx;
00443 struct StdDevCtx {
00444 double sum;
00445 double sum2;
00446 int cnt;
00447 };
00448
00449 #if 0
00450
00451
00452
00453 static void stdDevStep(sqlite_func *context, int argc, const char **argv){
00454 StdDevCtx *p;
00455 double x;
00456 if( argc<1 ) return;
00457 p = sqlite_aggregate_context(context, sizeof(*p));
00458 if( p && argv[0] ){
00459 x = sqliteAtoF(argv[0], 0);
00460 p->sum += x;
00461 p->sum2 += x*x;
00462 p->cnt++;
00463 }
00464 }
00465 static void stdDevFinalize(sqlite_func *context){
00466 double rN = sqlite_aggregate_count(context);
00467 StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
00468 if( p && p->cnt>1 ){
00469 double rCnt = cnt;
00470 sqlite_set_result_double(context,
00471 sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
00472 }
00473 }
00474 #endif
00475
00476
00477
00478
00479
00480 typedef struct CountCtx CountCtx;
00481 struct CountCtx {
00482 int n;
00483 };
00484
00485
00486
00487
00488 static void countStep(sqlite_func *context, int argc, const char **argv){
00489 CountCtx *p;
00490 p = sqlite_aggregate_context(context, sizeof(*p));
00491 if( (argc==0 || argv[0]) && p ){
00492 p->n++;
00493 }
00494 }
00495 static void countFinalize(sqlite_func *context){
00496 CountCtx *p;
00497 p = sqlite_aggregate_context(context, sizeof(*p));
00498 sqlite_set_result_int(context, p ? p->n : 0);
00499 }
00500
00501
00502
00503
00504
00505 typedef struct MinMaxCtx MinMaxCtx;
00506 struct MinMaxCtx {
00507 char *z;
00508 char zBuf[28];
00509 };
00510
00511
00512
00513
00514 static void minmaxStep(sqlite_func *context, int argc, const char **argv){
00515 MinMaxCtx *p;
00516 int (*xCompare)(const char*, const char*);
00517 int mask;
00518
00519 assert( argc==2 );
00520 if( argv[0]==0 ) return;
00521 if( argv[1][0]=='n' ){
00522 xCompare = sqliteCompare;
00523 }else{
00524 xCompare = strcmp;
00525 }
00526 mask = (int)sqlite_user_data(context);
00527 assert( mask==0 || mask==-1 );
00528 p = sqlite_aggregate_context(context, sizeof(*p));
00529 if( p==0 || argc<1 ) return;
00530 if( p->z==0 || (xCompare(argv[0],p->z)^mask)<0 ){
00531 int len;
00532 if( p->zBuf[0] ){
00533 sqliteFree(p->z);
00534 }
00535 len = strlen(argv[0]);
00536 if( len < sizeof(p->zBuf)-1 ){
00537 p->z = &p->zBuf[1];
00538 p->zBuf[0] = 0;
00539 }else{
00540 p->z = sqliteMalloc( len+1 );
00541 p->zBuf[0] = 1;
00542 if( p->z==0 ) return;
00543 }
00544 strcpy(p->z, argv[0]);
00545 }
00546 }
00547 static void minMaxFinalize(sqlite_func *context){
00548 MinMaxCtx *p;
00549 p = sqlite_aggregate_context(context, sizeof(*p));
00550 if( p && p->z && p->zBuf[0]<2 ){
00551 sqlite_set_result_string(context, p->z, strlen(p->z));
00552 }
00553 if( p && p->zBuf[0] ){
00554 sqliteFree(p->z);
00555 }
00556 }
00557
00558
00559
00560
00561
00562
00563 void sqliteRegisterBuiltinFunctions(sqlite *db){
00564 static struct {
00565 char *zName;
00566 signed char nArg;
00567 signed char dataType;
00568 u8 argType;
00569 void (*xFunc)(sqlite_func*,int,const char**);
00570 } aFuncs[] = {
00571 { "min", -1, SQLITE_ARGS, 0, minmaxFunc },
00572 { "min", 0, 0, 0, 0 },
00573 { "max", -1, SQLITE_ARGS, 2, minmaxFunc },
00574 { "max", 0, 0, 2, 0 },
00575 { "typeof", 1, SQLITE_TEXT, 0, typeofFunc },
00576 { "length", 1, SQLITE_NUMERIC, 0, lengthFunc },
00577 { "substr", 3, SQLITE_TEXT, 0, substrFunc },
00578 { "abs", 1, SQLITE_NUMERIC, 0, absFunc },
00579 { "round", 1, SQLITE_NUMERIC, 0, roundFunc },
00580 { "round", 2, SQLITE_NUMERIC, 0, roundFunc },
00581 { "upper", 1, SQLITE_TEXT, 0, upperFunc },
00582 { "lower", 1, SQLITE_TEXT, 0, lowerFunc },
00583 { "coalesce", -1, SQLITE_ARGS, 0, ifnullFunc },
00584 { "coalesce", 0, 0, 0, 0 },
00585 { "coalesce", 1, 0, 0, 0 },
00586 { "ifnull", 2, SQLITE_ARGS, 0, ifnullFunc },
00587 { "random", -1, SQLITE_NUMERIC, 0, randomFunc },
00588 { "like", 2, SQLITE_NUMERIC, 0, likeFunc },
00589 { "glob", 2, SQLITE_NUMERIC, 0, globFunc },
00590 { "nullif", 2, SQLITE_ARGS, 0, nullifFunc },
00591 { "sqlite_version",0,SQLITE_TEXT, 0, versionFunc},
00592 { "quote", 1, SQLITE_ARGS, 0, quoteFunc },
00593 { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
00594 { "change_count", 0, SQLITE_NUMERIC, 1, change_count },
00595 { "last_statement_change_count",
00596 0, SQLITE_NUMERIC, 1, last_statement_change_count },
00597 #ifdef SQLITE_SOUNDEX
00598 { "soundex", 1, SQLITE_TEXT, 0, soundexFunc},
00599 #endif
00600 #ifdef SQLITE_TEST
00601 { "randstr", 2, SQLITE_TEXT, 0, randStr },
00602 #endif
00603 };
00604 static struct {
00605 char *zName;
00606 signed char nArg;
00607 signed char dataType;
00608 u8 argType;
00609 void (*xStep)(sqlite_func*,int,const char**);
00610 void (*xFinalize)(sqlite_func*);
00611 } aAggs[] = {
00612 { "min", 1, 0, 0, minmaxStep, minMaxFinalize },
00613 { "max", 1, 0, 2, minmaxStep, minMaxFinalize },
00614 { "sum", 1, SQLITE_NUMERIC, 0, sumStep, sumFinalize },
00615 { "avg", 1, SQLITE_NUMERIC, 0, sumStep, avgFinalize },
00616 { "count", 0, SQLITE_NUMERIC, 0, countStep, countFinalize },
00617 { "count", 1, SQLITE_NUMERIC, 0, countStep, countFinalize },
00618 #if 0
00619 { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep, stdDevFinalize },
00620 #endif
00621 };
00622 static const char *azTypeFuncs[] = { "min", "max", "typeof" };
00623 int i;
00624
00625 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
00626 void *pArg;
00627 switch( aFuncs[i].argType ){
00628 case 0: pArg = 0; break;
00629 case 1: pArg = db; break;
00630 case 2: pArg = (void*)(-1); break;
00631 }
00632 sqlite_create_function(db, aFuncs[i].zName,
00633 aFuncs[i].nArg, aFuncs[i].xFunc, pArg);
00634 if( aFuncs[i].xFunc ){
00635 sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
00636 }
00637 }
00638 for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
00639 void *pArg;
00640 switch( aAggs[i].argType ){
00641 case 0: pArg = 0; break;
00642 case 1: pArg = db; break;
00643 case 2: pArg = (void*)(-1); break;
00644 }
00645 sqlite_create_aggregate(db, aAggs[i].zName,
00646 aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg);
00647 sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType);
00648 }
00649 for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
00650 int n = strlen(azTypeFuncs[i]);
00651 FuncDef *p = sqliteHashFind(&db->aFunc, azTypeFuncs[i], n);
00652 while( p ){
00653 p->includeTypes = 1;
00654 p = p->pNext;
00655 }
00656 }
00657 sqliteRegisterDateTimeFunctions(db);
00658 }