2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A** 2002 February 23
2N/A**
2N/A** The author disclaims copyright to this source code. In place of
2N/A** a legal notice, here is a blessing:
2N/A**
2N/A** May you do good and not evil.
2N/A** May you find forgiveness for yourself and forgive others.
2N/A** May you share freely, never taking more than you give.
2N/A**
2N/A*************************************************************************
2N/A** This file contains the C functions that implement various SQL
2N/A** functions of SQLite.
2N/A**
2N/A** There is only one exported symbol in this file - the function
2N/A** sqliteRegisterBuildinFunctions() found at the bottom of the file.
2N/A** All other code has file scope.
2N/A**
2N/A** $Id: func.c,v 1.43.2.3 2004/07/18 23:03:11 drh Exp $
2N/A*/
2N/A#include <ctype.h>
2N/A#include <math.h>
2N/A#include <stdlib.h>
2N/A#include <sys/u8_textprep.h>
2N/A#include <assert.h>
2N/A#include "sqliteInt.h"
2N/A#include "os.h"
2N/A
2N/A/*
2N/A** Implementation of the non-aggregate min() and max() functions
2N/A*/
2N/Astatic void minmaxFunc(sqlite_func *context, int argc, const char **argv){
2N/A const char *zBest;
2N/A int i;
2N/A int (*xCompare)(const char*, const char*);
2N/A int mask; /* 0 for min() or 0xffffffff for max() */
2N/A
2N/A if( argc==0 ) return;
2N/A mask = (int)sqlite_user_data(context);
2N/A zBest = argv[0];
2N/A if( zBest==0 ) return;
2N/A if( argv[1][0]=='n' ){
2N/A xCompare = sqliteCompare;
2N/A }else{
2N/A xCompare = strcmp;
2N/A }
2N/A for(i=2; i<argc; i+=2){
2N/A if( argv[i]==0 ) return;
2N/A if( (xCompare(argv[i], zBest)^mask)<0 ){
2N/A zBest = argv[i];
2N/A }
2N/A }
2N/A sqlite_set_result_string(context, zBest, -1);
2N/A}
2N/A
2N/A/*
2N/A** Return the type of the argument.
2N/A*/
2N/Astatic void typeofFunc(sqlite_func *context, int argc, const char **argv){
2N/A assert( argc==2 );
2N/A sqlite_set_result_string(context, argv[1], -1);
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the length() function
2N/A*/
2N/Astatic void lengthFunc(sqlite_func *context, int argc, const char **argv){
2N/A const char *z;
2N/A int len;
2N/A
2N/A assert( argc==1 );
2N/A z = argv[0];
2N/A if( z==0 ) return;
2N/A#ifdef SQLITE_UTF8
2N/A for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
2N/A#else
2N/A len = strlen(z);
2N/A#endif
2N/A sqlite_set_result_int(context, len);
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the abs() function
2N/A*/
2N/Astatic void absFunc(sqlite_func *context, int argc, const char **argv){
2N/A const char *z;
2N/A assert( argc==1 );
2N/A z = argv[0];
2N/A if( z==0 ) return;
2N/A if( z[0]=='-' && isdigit(z[1]) ) z++;
2N/A sqlite_set_result_string(context, z, -1);
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the substr() function
2N/A*/
2N/Astatic void substrFunc(sqlite_func *context, int argc, const char **argv){
2N/A const char *z;
2N/A#ifdef SQLITE_UTF8
2N/A const char *z2;
2N/A int i;
2N/A#endif
2N/A int p1, p2, len;
2N/A assert( argc==3 );
2N/A z = argv[0];
2N/A if( z==0 ) return;
2N/A p1 = atoi(argv[1]?argv[1]:0);
2N/A p2 = atoi(argv[2]?argv[2]:0);
2N/A#ifdef SQLITE_UTF8
2N/A for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
2N/A#else
2N/A len = strlen(z);
2N/A#endif
2N/A if( p1<0 ){
2N/A p1 += len;
2N/A if( p1<0 ){
2N/A p2 += p1;
2N/A p1 = 0;
2N/A }
2N/A }else if( p1>0 ){
2N/A p1--;
2N/A }
2N/A if( p1+p2>len ){
2N/A p2 = len-p1;
2N/A }
2N/A#ifdef SQLITE_UTF8
2N/A for(i=0; i<p1 && z[i]; i++){
2N/A if( (z[i]&0xc0)==0x80 ) p1++;
2N/A }
2N/A while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
2N/A for(; i<p1+p2 && z[i]; i++){
2N/A if( (z[i]&0xc0)==0x80 ) p2++;
2N/A }
2N/A while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
2N/A#endif
2N/A if( p2<0 ) p2 = 0;
2N/A sqlite_set_result_string(context, &z[p1], p2);
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the round() function
2N/A*/
2N/Astatic void roundFunc(sqlite_func *context, int argc, const char **argv){
2N/A int n;
2N/A double r;
2N/A char zBuf[100];
2N/A assert( argc==1 || argc==2 );
2N/A if( argv[0]==0 || (argc==2 && argv[1]==0) ) return;
2N/A n = argc==2 ? atoi(argv[1]) : 0;
2N/A if( n>30 ) n = 30;
2N/A if( n<0 ) n = 0;
2N/A r = sqliteAtoF(argv[0], 0);
2N/A sprintf(zBuf,"%.*f",n,r);
2N/A sqlite_set_result_string(context, zBuf, -1);
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the upper() and lower() SQL functions.
2N/A*/
2N/Astatic void upperFunc(sqlite_func *context, int argc, const char **argv){
2N/A unsigned char *z;
2N/A int i;
2N/A if( argc<1 || argv[0]==0 ) return;
2N/A z = (unsigned char*)sqlite_set_result_string(context, argv[0], -1);
2N/A if( z==0 ) return;
2N/A for(i=0; z[i]; i++){
2N/A if( islower(z[i]) ) z[i] = toupper(z[i]);
2N/A }
2N/A}
2N/Astatic void lowerFunc(sqlite_func *context, int argc, const char **argv){
2N/A unsigned char *z;
2N/A int i;
2N/A if( argc<1 || argv[0]==0 ) return;
2N/A z = (unsigned char*)sqlite_set_result_string(context, argv[0], -1);
2N/A if( z==0 ) return;
2N/A for(i=0; z[i]; i++){
2N/A if( isupper(z[i]) ) z[i] = tolower(z[i]);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * A utility wrapper around u8_textprep_str() that returns an allocated
2N/A * string. The result must be freed or passed to
2N/A * sqlite_set_result_string().
2N/A *
2N/A * This is a Solaris-specific function, though it could be made
2N/A * portable. u8_textprep_str() and friends are CDDL'ed. This code was
2N/A * added to this file without changing the public domain notice, and
2N/A * therefore is in the public domain as well.
2N/A */
2N/Astatic
2N/Achar *
2N/Autf8textprep(const char *s, int flag)
2N/A{
2N/A char *res = NULL;
2N/A char *outs;
2N/A size_t inlen, outlen, inbytesleft, outbytesleft;
2N/A int rc, err;
2N/A
2N/A /*
2N/A * u8_textprep_str() does not allocate memory. The input and
2N/A * output buffers may differ in size (though that would be more
2N/A * likely when normalization is done). We have to loop over it...
2N/A *
2N/A * To improve the chances that we can avoid looping we add 10
2N/A * bytes of output buffer room the first go around.
2N/A */
2N/A inlen = inbytesleft = strlen(s);
2N/A outlen = outbytesleft = inlen + 10;
2N/A if ((res = sqliteMalloc(outlen)) == NULL)
2N/A return (NULL);
2N/A outs = res;
2N/A
2N/A while ((rc = u8_textprep_str((char *)s, &inbytesleft, outs,
2N/A &outbytesleft, flag, U8_UNICODE_LATEST, &err)) < 0 &&
2N/A err == E2BIG) {
2N/A if ((res = sqliteRealloc(res, outlen + inbytesleft)) == NULL)
2N/A return (NULL);
2N/A /* adjust input/output buffer pointers */
2N/A s += (inlen - inbytesleft);
2N/A outs = res + outlen - outbytesleft;
2N/A /* adjust outbytesleft and outlen */
2N/A outlen += inbytesleft;
2N/A outbytesleft += inbytesleft;
2N/A }
2N/A
2N/A if (rc < 0) {
2N/A free(res);
2N/A res = NULL;
2N/A return (NULL);
2N/A }
2N/A
2N/A res[outlen - outbytesleft] = '\0';
2N/A
2N/A return (res);
2N/A}
2N/A/*
2N/A * A Unicode-capable case-folding (to lower) function
2N/A *
2N/A * See block comment for case_fold_utf8().
2N/A */
2N/Astatic
2N/Avoid
2N/Alower_utf8Func(sqlite_func *context, int argc, const char **argv)
2N/A{
2N/A char *lower = NULL;
2N/A
2N/A /*
2N/A * SQLite functions can take many arguments, but this function
2N/A * uses only one, and we call sqlite_create_function() with
2N/A * nArg == 1.
2N/A */
2N/A assert(argc <= 1);
2N/A
2N/A if (argv[0] != NULL)
2N/A lower = utf8textprep(argv[0], U8_TEXTPREP_TOLOWER);
2N/A
2N/Aout:
2N/A (void) sqlite_set_result_string(context, lower, -1);
2N/A free(lower);
2N/A}
2N/Astatic
2N/Avoid
2N/Aupper_utf8Func(sqlite_func *context, int argc, const char **argv)
2N/A{
2N/A char *upper = NULL;
2N/A
2N/A /*
2N/A * SQLite functions can take many arguments, but this function
2N/A * uses only one, and we call sqlite_create_function() with
2N/A * nArg == 1.
2N/A */
2N/A assert(argc <= 1);
2N/A
2N/A if (argv[0] != NULL)
2N/A upper = utf8textprep(argv[0], U8_TEXTPREP_TOUPPER);
2N/A
2N/Aout:
2N/A (void) sqlite_set_result_string(context, upper, -1);
2N/A free(upper);
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the IFNULL(), NVL(), and COALESCE() functions.
2N/A** All three do the same thing. They return the first non-NULL
2N/A** argument.
2N/A*/
2N/Astatic void ifnullFunc(sqlite_func *context, int argc, const char **argv){
2N/A int i;
2N/A for(i=0; i<argc; i++){
2N/A if( argv[i] ){
2N/A sqlite_set_result_string(context, argv[i], -1);
2N/A break;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/*
2N/A** Implementation of random(). Return a random integer.
2N/A*/
2N/Astatic void randomFunc(sqlite_func *context, int argc, const char **argv){
2N/A int r;
2N/A sqliteRandomness(sizeof(r), &r);
2N/A sqlite_set_result_int(context, r);
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the last_insert_rowid() SQL function. The return
2N/A** value is the same as the sqlite_last_insert_rowid() API function.
2N/A*/
2N/Astatic void last_insert_rowid(sqlite_func *context, int arg, const char **argv){
2N/A sqlite *db = sqlite_user_data(context);
2N/A sqlite_set_result_int(context, sqlite_last_insert_rowid(db));
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the change_count() SQL function. The return
2N/A** value is the same as the sqlite_changes() API function.
2N/A*/
2N/Astatic void change_count(sqlite_func *context, int arg, const char **argv){
2N/A sqlite *db = sqlite_user_data(context);
2N/A sqlite_set_result_int(context, sqlite_changes(db));
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the last_statement_change_count() SQL function. The
2N/A** return value is the same as the sqlite_last_statement_changes() API function.
2N/A*/
2N/Astatic void last_statement_change_count(sqlite_func *context, int arg,
2N/A const char **argv){
2N/A sqlite *db = sqlite_user_data(context);
2N/A sqlite_set_result_int(context, sqlite_last_statement_changes(db));
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the like() SQL function. This function implements
2N/A** the build-in LIKE operator. The first argument to the function is the
2N/A** string and the second argument is the pattern. So, the SQL statements:
2N/A**
2N/A** A LIKE B
2N/A**
2N/A** is implemented as like(A,B).
2N/A*/
2N/Astatic void likeFunc(sqlite_func *context, int arg, const char **argv){
2N/A if( argv[0]==0 || argv[1]==0 ) return;
2N/A sqlite_set_result_int(context,
2N/A sqliteLikeCompare((const unsigned char*)argv[0],
2N/A (const unsigned char*)argv[1]));
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the glob() SQL function. This function implements
2N/A** the build-in GLOB operator. The first argument to the function is the
2N/A** string and the second argument is the pattern. So, the SQL statements:
2N/A**
2N/A** A GLOB B
2N/A**
2N/A** is implemented as glob(A,B).
2N/A*/
2N/Astatic void globFunc(sqlite_func *context, int arg, const char **argv){
2N/A if( argv[0]==0 || argv[1]==0 ) return;
2N/A sqlite_set_result_int(context,
2N/A sqliteGlobCompare((const unsigned char*)argv[0],
2N/A (const unsigned char*)argv[1]));
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the NULLIF(x,y) function. The result is the first
2N/A** argument if the arguments are different. The result is NULL if the
2N/A** arguments are equal to each other.
2N/A*/
2N/Astatic void nullifFunc(sqlite_func *context, int argc, const char **argv){
2N/A if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){
2N/A sqlite_set_result_string(context, argv[0], -1);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A** Implementation of the VERSION(*) function. The result is the version
2N/A** of the SQLite library that is running.
2N/A*/
2N/Astatic void versionFunc(sqlite_func *context, int argc, const char **argv){
2N/A sqlite_set_result_string(context, sqlite_version, -1);
2N/A}
2N/A
2N/A/*
2N/A** EXPERIMENTAL - This is not an official function. The interface may
2N/A** change. This function may disappear. Do not write code that depends
2N/A** on this function.
2N/A**
2N/A** Implementation of the QUOTE() function. This function takes a single
2N/A** argument. If the argument is numeric, the return value is the same as
2N/A** the argument. If the argument is NULL, the return value is the string
2N/A** "NULL". Otherwise, the argument is enclosed in single quotes with
2N/A** single-quote escapes.
2N/A*/
2N/Astatic void quoteFunc(sqlite_func *context, int argc, const char **argv){
2N/A if( argc<1 ) return;
2N/A if( argv[0]==0 ){
2N/A sqlite_set_result_string(context, "NULL", 4);
2N/A }else if( sqliteIsNumber(argv[0]) ){
2N/A sqlite_set_result_string(context, argv[0], -1);
2N/A }else{
2N/A int i,j,n;
2N/A char *z;
2N/A for(i=n=0; argv[0][i]; i++){ if( argv[0][i]=='\'' ) n++; }
2N/A z = sqliteMalloc( i+n+3 );
2N/A if( z==0 ) return;
2N/A z[0] = '\'';
2N/A for(i=0, j=1; argv[0][i]; i++){
2N/A z[j++] = argv[0][i];
2N/A if( argv[0][i]=='\'' ){
2N/A z[j++] = '\'';
2N/A }
2N/A }
2N/A z[j++] = '\'';
2N/A z[j] = 0;
2N/A sqlite_set_result_string(context, z, j);
2N/A sqliteFree(z);
2N/A }
2N/A}
2N/A
2N/A#ifdef SQLITE_SOUNDEX
2N/A/*
2N/A** Compute the soundex encoding of a word.
2N/A*/
2N/Astatic void soundexFunc(sqlite_func *context, int argc, const char **argv){
2N/A char zResult[8];
2N/A const char *zIn;
2N/A int i, j;
2N/A static const unsigned char iCode[] = {
2N/A 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2N/A 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2N/A 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2N/A 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2N/A 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
2N/A 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
2N/A 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
2N/A 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
2N/A };
2N/A assert( argc==1 );
2N/A zIn = argv[0];
2N/A for(i=0; zIn[i] && !isalpha(zIn[i]); i++){}
2N/A if( zIn[i] ){
2N/A zResult[0] = toupper(zIn[i]);
2N/A for(j=1; j<4 && zIn[i]; i++){
2N/A int code = iCode[zIn[i]&0x7f];
2N/A if( code>0 ){
2N/A zResult[j++] = code + '0';
2N/A }
2N/A }
2N/A while( j<4 ){
2N/A zResult[j++] = '0';
2N/A }
2N/A zResult[j] = 0;
2N/A sqlite_set_result_string(context, zResult, 4);
2N/A }else{
2N/A sqlite_set_result_string(context, "?000", 4);
2N/A }
2N/A}
2N/A#endif
2N/A
2N/A#ifdef SQLITE_TEST
2N/A/*
2N/A** This function generates a string of random characters. Used for
2N/A** generating test data.
2N/A*/
2N/Astatic void randStr(sqlite_func *context, int argc, const char **argv){
2N/A static const unsigned char zSrc[] =
2N/A "abcdefghijklmnopqrstuvwxyz"
2N/A "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2N/A "0123456789"
2N/A ".-!,:*^+=_|?/<> ";
2N/A int iMin, iMax, n, r, i;
2N/A unsigned char zBuf[1000];
2N/A if( argc>=1 ){
2N/A iMin = atoi(argv[0]);
2N/A if( iMin<0 ) iMin = 0;
2N/A if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
2N/A }else{
2N/A iMin = 1;
2N/A }
2N/A if( argc>=2 ){
2N/A iMax = atoi(argv[1]);
2N/A if( iMax<iMin ) iMax = iMin;
2N/A if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
2N/A }else{
2N/A iMax = 50;
2N/A }
2N/A n = iMin;
2N/A if( iMax>iMin ){
2N/A sqliteRandomness(sizeof(r), &r);
2N/A r &= 0x7fffffff;
2N/A n += r%(iMax + 1 - iMin);
2N/A }
2N/A assert( n<sizeof(zBuf) );
2N/A sqliteRandomness(n, zBuf);
2N/A for(i=0; i<n; i++){
2N/A zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
2N/A }
2N/A zBuf[n] = 0;
2N/A sqlite_set_result_string(context, zBuf, n);
2N/A}
2N/A#endif
2N/A
2N/A/*
2N/A** An instance of the following structure holds the context of a
2N/A** sum() or avg() aggregate computation.
2N/A*/
2N/Atypedef struct SumCtx SumCtx;
2N/Astruct SumCtx {
2N/A double sum; /* Sum of terms */
2N/A int cnt; /* Number of elements summed */
2N/A};
2N/A
2N/A/*
2N/A** Routines used to compute the sum or average.
2N/A*/
2N/Astatic void sumStep(sqlite_func *context, int argc, const char **argv){
2N/A SumCtx *p;
2N/A if( argc<1 ) return;
2N/A p = sqlite_aggregate_context(context, sizeof(*p));
2N/A if( p && argv[0] ){
2N/A p->sum += sqliteAtoF(argv[0], 0);
2N/A p->cnt++;
2N/A }
2N/A}
2N/Astatic void sumFinalize(sqlite_func *context){
2N/A SumCtx *p;
2N/A p = sqlite_aggregate_context(context, sizeof(*p));
2N/A sqlite_set_result_double(context, p ? p->sum : 0.0);
2N/A}
2N/Astatic void avgFinalize(sqlite_func *context){
2N/A SumCtx *p;
2N/A p = sqlite_aggregate_context(context, sizeof(*p));
2N/A if( p && p->cnt>0 ){
2N/A sqlite_set_result_double(context, p->sum/(double)p->cnt);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A** An instance of the following structure holds the context of a
2N/A** variance or standard deviation computation.
2N/A*/
2N/Atypedef struct StdDevCtx StdDevCtx;
2N/Astruct StdDevCtx {
2N/A double sum; /* Sum of terms */
2N/A double sum2; /* Sum of the squares of terms */
2N/A int cnt; /* Number of terms counted */
2N/A};
2N/A
2N/A#if 0 /* Omit because math library is required */
2N/A/*
2N/A** Routines used to compute the standard deviation as an aggregate.
2N/A*/
2N/Astatic void stdDevStep(sqlite_func *context, int argc, const char **argv){
2N/A StdDevCtx *p;
2N/A double x;
2N/A if( argc<1 ) return;
2N/A p = sqlite_aggregate_context(context, sizeof(*p));
2N/A if( p && argv[0] ){
2N/A x = sqliteAtoF(argv[0], 0);
2N/A p->sum += x;
2N/A p->sum2 += x*x;
2N/A p->cnt++;
2N/A }
2N/A}
2N/Astatic void stdDevFinalize(sqlite_func *context){
2N/A double rN = sqlite_aggregate_count(context);
2N/A StdDevCtx *p = sqlite_aggregate_context(context, sizeof(*p));
2N/A if( p && p->cnt>1 ){
2N/A double rCnt = cnt;
2N/A sqlite_set_result_double(context,
2N/A sqrt((p->sum2 - p->sum*p->sum/rCnt)/(rCnt-1.0)));
2N/A }
2N/A}
2N/A#endif
2N/A
2N/A/*
2N/A** The following structure keeps track of state information for the
2N/A** count() aggregate function.
2N/A*/
2N/Atypedef struct CountCtx CountCtx;
2N/Astruct CountCtx {
2N/A int n;
2N/A};
2N/A
2N/A/*
2N/A** Routines to implement the count() aggregate function.
2N/A*/
2N/Astatic void countStep(sqlite_func *context, int argc, const char **argv){
2N/A CountCtx *p;
2N/A p = sqlite_aggregate_context(context, sizeof(*p));
2N/A if( (argc==0 || argv[0]) && p ){
2N/A p->n++;
2N/A }
2N/A}
2N/Astatic void countFinalize(sqlite_func *context){
2N/A CountCtx *p;
2N/A p = sqlite_aggregate_context(context, sizeof(*p));
2N/A sqlite_set_result_int(context, p ? p->n : 0);
2N/A}
2N/A
2N/A/*
2N/A** This function tracks state information for the min() and max()
2N/A** aggregate functions.
2N/A*/
2N/Atypedef struct MinMaxCtx MinMaxCtx;
2N/Astruct MinMaxCtx {
2N/A char *z; /* The best so far */
2N/A char zBuf[28]; /* Space that can be used for storage */
2N/A};
2N/A
2N/A/*
2N/A** Routines to implement min() and max() aggregate functions.
2N/A*/
2N/Astatic void minmaxStep(sqlite_func *context, int argc, const char **argv){
2N/A MinMaxCtx *p;
2N/A int (*xCompare)(const char*, const char*);
2N/A int mask; /* 0 for min() or 0xffffffff for max() */
2N/A
2N/A assert( argc==2 );
2N/A if( argv[0]==0 ) return; /* Ignore NULL values */
2N/A if( argv[1][0]=='n' ){
2N/A xCompare = sqliteCompare;
2N/A }else{
2N/A xCompare = strcmp;
2N/A }
2N/A mask = (int)sqlite_user_data(context);
2N/A assert( mask==0 || mask==-1 );
2N/A p = sqlite_aggregate_context(context, sizeof(*p));
2N/A if( p==0 || argc<1 ) return;
2N/A if( p->z==0 || (xCompare(argv[0],p->z)^mask)<0 ){
2N/A int len;
2N/A if( p->zBuf[0] ){
2N/A sqliteFree(p->z);
2N/A }
2N/A len = strlen(argv[0]);
2N/A if( len < sizeof(p->zBuf)-1 ){
2N/A p->z = &p->zBuf[1];
2N/A p->zBuf[0] = 0;
2N/A }else{
2N/A p->z = sqliteMalloc( len+1 );
2N/A p->zBuf[0] = 1;
2N/A if( p->z==0 ) return;
2N/A }
2N/A strcpy(p->z, argv[0]);
2N/A }
2N/A}
2N/Astatic void minMaxFinalize(sqlite_func *context){
2N/A MinMaxCtx *p;
2N/A p = sqlite_aggregate_context(context, sizeof(*p));
2N/A if( p && p->z && p->zBuf[0]<2 ){
2N/A sqlite_set_result_string(context, p->z, strlen(p->z));
2N/A }
2N/A if( p && p->zBuf[0] ){
2N/A sqliteFree(p->z);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A** This function registered all of the above C functions as SQL
2N/A** functions. This should be the only routine in this file with
2N/A** external linkage.
2N/A*/
2N/Avoid sqliteRegisterBuiltinFunctions(sqlite *db){
2N/A static struct {
2N/A char *zName;
2N/A signed char nArg;
2N/A signed char dataType;
2N/A u8 argType; /* 0: none. 1: db 2: (-1) */
2N/A void (*xFunc)(sqlite_func*,int,const char**);
2N/A } aFuncs[] = {
2N/A { "min", -1, SQLITE_ARGS, 0, minmaxFunc },
2N/A { "min", 0, 0, 0, 0 },
2N/A { "max", -1, SQLITE_ARGS, 2, minmaxFunc },
2N/A { "max", 0, 0, 2, 0 },
2N/A { "typeof", 1, SQLITE_TEXT, 0, typeofFunc },
2N/A { "length", 1, SQLITE_NUMERIC, 0, lengthFunc },
2N/A { "substr", 3, SQLITE_TEXT, 0, substrFunc },
2N/A { "abs", 1, SQLITE_NUMERIC, 0, absFunc },
2N/A { "round", 1, SQLITE_NUMERIC, 0, roundFunc },
2N/A { "round", 2, SQLITE_NUMERIC, 0, roundFunc },
2N/A { "upper", 1, SQLITE_TEXT, 0, upperFunc },
2N/A { "lower", 1, SQLITE_TEXT, 0, lowerFunc },
2N/A { "lower_utf8", 1, SQLITE_TEXT, 0, lower_utf8Func },
2N/A { "upper_utf8", 1, SQLITE_TEXT, 0, upper_utf8Func },
2N/A { "coalesce", -1, SQLITE_ARGS, 0, ifnullFunc },
2N/A { "coalesce", 0, 0, 0, 0 },
2N/A { "coalesce", 1, 0, 0, 0 },
2N/A { "ifnull", 2, SQLITE_ARGS, 0, ifnullFunc },
2N/A { "random", -1, SQLITE_NUMERIC, 0, randomFunc },
2N/A { "like", 2, SQLITE_NUMERIC, 0, likeFunc },
2N/A { "glob", 2, SQLITE_NUMERIC, 0, globFunc },
2N/A { "nullif", 2, SQLITE_ARGS, 0, nullifFunc },
2N/A { "sqlite_version",0,SQLITE_TEXT, 0, versionFunc },
2N/A { "quote", 1, SQLITE_ARGS, 0, quoteFunc },
2N/A { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
2N/A { "change_count", 0, SQLITE_NUMERIC, 1, change_count },
2N/A { "last_statement_change_count",
2N/A 0, SQLITE_NUMERIC, 1, last_statement_change_count },
2N/A#ifdef SQLITE_SOUNDEX
2N/A { "soundex", 1, SQLITE_TEXT, 0, soundexFunc},
2N/A#endif
2N/A#ifdef SQLITE_TEST
2N/A { "randstr", 2, SQLITE_TEXT, 0, randStr },
2N/A#endif
2N/A };
2N/A static struct {
2N/A char *zName;
2N/A signed char nArg;
2N/A signed char dataType;
2N/A u8 argType;
2N/A void (*xStep)(sqlite_func*,int,const char**);
2N/A void (*xFinalize)(sqlite_func*);
2N/A } aAggs[] = {
2N/A { "min", 1, 0, 0, minmaxStep, minMaxFinalize },
2N/A { "max", 1, 0, 2, minmaxStep, minMaxFinalize },
2N/A { "sum", 1, SQLITE_NUMERIC, 0, sumStep, sumFinalize },
2N/A { "avg", 1, SQLITE_NUMERIC, 0, sumStep, avgFinalize },
2N/A { "count", 0, SQLITE_NUMERIC, 0, countStep, countFinalize },
2N/A { "count", 1, SQLITE_NUMERIC, 0, countStep, countFinalize },
2N/A#if 0
2N/A { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep, stdDevFinalize },
2N/A#endif
2N/A };
2N/A static const char *azTypeFuncs[] = { "min", "max", "typeof" };
2N/A int i;
2N/A
2N/A for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
2N/A void *pArg;
2N/A switch( aFuncs[i].argType ){
2N/A case 0: pArg = 0; break;
2N/A case 1: pArg = db; break;
2N/A case 2: pArg = (void*)(-1); break;
2N/A }
2N/A sqlite_create_function(db, aFuncs[i].zName,
2N/A aFuncs[i].nArg, aFuncs[i].xFunc, pArg);
2N/A if( aFuncs[i].xFunc ){
2N/A sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
2N/A }
2N/A }
2N/A for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
2N/A void *pArg;
2N/A switch( aAggs[i].argType ){
2N/A case 0: pArg = 0; break;
2N/A case 1: pArg = db; break;
2N/A case 2: pArg = (void*)(-1); break;
2N/A }
2N/A sqlite_create_aggregate(db, aAggs[i].zName,
2N/A aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg);
2N/A sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType);
2N/A }
2N/A for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
2N/A int n = strlen(azTypeFuncs[i]);
2N/A FuncDef *p = sqliteHashFind(&db->aFunc, azTypeFuncs[i], n);
2N/A while( p ){
2N/A p->includeTypes = 1;
2N/A p = p->pNext;
2N/A }
2N/A }
2N/A sqliteRegisterDateTimeFunctions(db);
2N/A}