Back to index

php5  5.3.10
Classes | Typedefs | Functions
func.c File Reference
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include "sqliteInt.h"
#include "os.h"

Go to the source code of this file.

Classes

struct  SumCtx
struct  StdDevCtx
struct  CountCtx
struct  MinMaxCtx

Typedefs

typedef struct SumCtx
typedef struct StdDevCtx
typedef struct CountCtx
typedef struct MinMaxCtx

Functions

static void minmaxFunc (sqlite_func *context, int argc, const char **argv)
static void typeofFunc (sqlite_func *context, int argc, const char **argv)
static void lengthFunc (sqlite_func *context, int argc, const char **argv)
static void absFunc (sqlite_func *context, int argc, const char **argv)
static void substrFunc (sqlite_func *context, int argc, const char **argv)
static void roundFunc (sqlite_func *context, int argc, const char **argv)
static void upperFunc (sqlite_func *context, int argc, const char **argv)
static void lowerFunc (sqlite_func *context, int argc, const char **argv)
static void ifnullFunc (sqlite_func *context, int argc, const char **argv)
static void randomFunc (sqlite_func *context, int argc, const char **argv)
static void last_insert_rowid (sqlite_func *context, int arg, const char **argv)
static void change_count (sqlite_func *context, int arg, const char **argv)
static void last_statement_change_count (sqlite_func *context, int arg, const char **argv)
static void likeFunc (sqlite_func *context, int arg, const char **argv)
static void globFunc (sqlite_func *context, int arg, const char **argv)
static void nullifFunc (sqlite_func *context, int argc, const char **argv)
static void versionFunc (sqlite_func *context, int argc, const char **argv)
static void quoteFunc (sqlite_func *context, int argc, const char **argv)
static void sumStep (sqlite_func *context, int argc, const char **argv)
static void sumFinalize (sqlite_func *context)
static void avgFinalize (sqlite_func *context)
static void countStep (sqlite_func *context, int argc, const char **argv)
static void countFinalize (sqlite_func *context)
static void minmaxStep (sqlite_func *context, int argc, const char **argv)
static void minMaxFinalize (sqlite_func *context)
void sqliteRegisterBuiltinFunctions (sqlite *db)

Class Documentation

struct SumCtx

Definition at line 408 of file func.c.

Class Members
u8 approx
int cnt
i64 cnt
i64 iSum
u8 overflow
double rSum
double sum
struct StdDevCtx

Definition at line 443 of file func.c.

Class Members
int cnt
double sum
double sum2
struct CountCtx

Definition at line 481 of file func.c.

Class Members
int n
i64 n
struct MinMaxCtx

Definition at line 506 of file func.c.

Class Members
char * z
char zBuf

Typedef Documentation

typedef struct CountCtx

Definition at line 480 of file func.c.

typedef struct MinMaxCtx

Definition at line 505 of file func.c.

typedef struct StdDevCtx

Definition at line 442 of file func.c.

typedef struct SumCtx

Definition at line 407 of file func.c.


Function Documentation

static void absFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 84 of file func.c.

                                                                      {
  const char *z;
  assert( argc==1 );
  z = argv[0];
  if( z==0 ) return;
  if( z[0]=='-' && isdigit(z[1]) ) z++;
  sqlite_set_result_string(context, z, -1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void avgFinalize ( sqlite_func context) [static]

Definition at line 430 of file func.c.

                                             {
  SumCtx *p;
  p = sqlite_aggregate_context(context, sizeof(*p));
  if( p && p->cnt>0 ){
    sqlite_set_result_double(context, p->sum/(double)p->cnt);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void change_count ( sqlite_func context,
int  arg,
const char **  argv 
) [static]

Definition at line 217 of file func.c.

                                                                          {
  sqlite *db = sqlite_user_data(context);
  sqlite_set_result_int(context, sqlite_changes(db));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void countFinalize ( sqlite_func context) [static]

Definition at line 495 of file func.c.

                                               {
  CountCtx *p;
  p = sqlite_aggregate_context(context, sizeof(*p));
  sqlite_set_result_int(context, p ? p->n : 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void countStep ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 488 of file func.c.

                                                                        {
  CountCtx *p;
  p = sqlite_aggregate_context(context, sizeof(*p));
  if( (argc==0 || argv[0]) && p ){
    p->n++;
  }
}   

Here is the call graph for this function:

Here is the caller graph for this function:

static void globFunc ( sqlite_func context,
int  arg,
const char **  argv 
) [static]

Definition at line 257 of file func.c.

                                                                      {
  if( argv[0]==0 || argv[1]==0 ) return;
  sqlite_set_result_int(context,
    sqliteGlobCompare((const unsigned char*)argv[0],
                      (const unsigned char*)argv[1]));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void ifnullFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 185 of file func.c.

                                                                         {
  int i;
  for(i=0; i<argc; i++){
    if( argv[i] ){
      sqlite_set_result_string(context, argv[i], -1);
      break;
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void last_insert_rowid ( sqlite_func context,
int  arg,
const char **  argv 
) [static]

Definition at line 208 of file func.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static void last_statement_change_count ( sqlite_func context,
int  arg,
const char **  argv 
) [static]

Definition at line 226 of file func.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static void lengthFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 66 of file func.c.

                                                                         {
  const char *z;
  int len;

  assert( argc==1 );
  z = argv[0];
  if( z==0 ) return;
#ifdef SQLITE_UTF8
  for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
#else
  len = strlen(z);
#endif
  sqlite_set_result_int(context, len);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void likeFunc ( sqlite_func context,
int  arg,
const char **  argv 
) [static]

Definition at line 241 of file func.c.

                                                                      {
  if( argv[0]==0 || argv[1]==0 ) return;
  sqlite_set_result_int(context, 
    sqliteLikeCompare((const unsigned char*)argv[0],
                      (const unsigned char*)argv[1]));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void lowerFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 169 of file func.c.

                                                                        {
  unsigned char *z;
  int i;
  if( argc<1 || argv[0]==0 ) return;
  z = (unsigned char*)sqlite_set_result_string(context, argv[0], -1);
  if( z==0 ) return;
  for(i=0; z[i]; i++){
    if( isupper(z[i]) ) z[i] = tolower(z[i]);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void minMaxFinalize ( sqlite_func context) [static]

Definition at line 547 of file func.c.

                                                {
  MinMaxCtx *p;
  p = sqlite_aggregate_context(context, sizeof(*p));
  if( p && p->z && p->zBuf[0]<2 ){
    sqlite_set_result_string(context, p->z, strlen(p->z));
  }
  if( p && p->zBuf[0] ){
    sqliteFree(p->z);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void minmaxFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 31 of file func.c.

                                                                         {
  const char *zBest; 
  int i;
  int (*xCompare)(const char*, const char*);
  int mask;    /* 0 for min() or 0xffffffff for max() */

  if( argc==0 ) return;
  mask = (int)sqlite_user_data(context);
  zBest = argv[0];
  if( zBest==0 ) return;
  if( argv[1][0]=='n' ){
    xCompare = sqliteCompare;
  }else{
    xCompare = strcmp;
  }
  for(i=2; i<argc; i+=2){
    if( argv[i]==0 ) return;
    if( (xCompare(argv[i], zBest)^mask)<0 ){
      zBest = argv[i];
    }
  }
  sqlite_set_result_string(context, zBest, -1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void minmaxStep ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 514 of file func.c.

                                                                         {
  MinMaxCtx *p;
  int (*xCompare)(const char*, const char*);
  int mask;    /* 0 for min() or 0xffffffff for max() */

  assert( argc==2 );
  if( argv[0]==0 ) return;  /* Ignore NULL values */
  if( argv[1][0]=='n' ){
    xCompare = sqliteCompare;
  }else{
    xCompare = strcmp;
  }
  mask = (int)sqlite_user_data(context);
  assert( mask==0 || mask==-1 );
  p = sqlite_aggregate_context(context, sizeof(*p));
  if( p==0 || argc<1 ) return;
  if( p->z==0 || (xCompare(argv[0],p->z)^mask)<0 ){
    int len;
    if( p->zBuf[0] ){
      sqliteFree(p->z);
    }
    len = strlen(argv[0]);
    if( len < sizeof(p->zBuf)-1 ){
      p->z = &p->zBuf[1];
      p->zBuf[0] = 0;
    }else{
      p->z = sqliteMalloc( len+1 );
      p->zBuf[0] = 1;
      if( p->z==0 ) return;
    }
    strcpy(p->z, argv[0]);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void nullifFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 269 of file func.c.

                                                                         {
  if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){
    sqlite_set_result_string(context, argv[0], -1);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void quoteFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 294 of file func.c.

                                                                        {
  if( argc<1 ) return;
  if( argv[0]==0 ){
    sqlite_set_result_string(context, "NULL", 4);
  }else if( sqliteIsNumber(argv[0]) ){
    sqlite_set_result_string(context, argv[0], -1);
  }else{
    int i,j,n;
    char *z;
    for(i=n=0; argv[0][i]; i++){ if( argv[0][i]=='\'' ) n++; }
    z = sqliteMalloc( i+n+3 );
    if( z==0 ) return;
    z[0] = '\'';
    for(i=0, j=1; argv[0][i]; i++){
      z[j++] = argv[0][i];
      if( argv[0][i]=='\'' ){
        z[j++] = '\'';
      }
    }
    z[j++] = '\'';
    z[j] = 0;
    sqlite_set_result_string(context, z, j);
    sqliteFree(z);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void randomFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 198 of file func.c.

                                                                         {
  int r;
  sqliteRandomness(sizeof(r), &r);
  sqlite_set_result_int(context, r);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void roundFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 142 of file func.c.

                                                                        {
  int n;
  double r;
  char zBuf[100];
  assert( argc==1 || argc==2 );
  if( argv[0]==0 || (argc==2 && argv[1]==0) ) return;
  n = argc==2 ? atoi(argv[1]) : 0;
  if( n>30 ) n = 30;
  if( n<0 ) n = 0;
  r = sqliteAtoF(argv[0], 0);
  sprintf(zBuf,"%.*f",n,r);
  sqlite_set_result_string(context, zBuf, -1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 563 of file func.c.

                                               {
  static struct {
     char *zName;
     signed char nArg;
     signed char dataType;
     u8 argType;               /* 0: none.  1: db  2: (-1) */
     void (*xFunc)(sqlite_func*,int,const char**);
  } aFuncs[] = {
    { "min",       -1, SQLITE_ARGS,    0, minmaxFunc },
    { "min",        0, 0,              0, 0          },
    { "max",       -1, SQLITE_ARGS,    2, minmaxFunc },
    { "max",        0, 0,              2, 0          },
    { "typeof",     1, SQLITE_TEXT,    0, typeofFunc },
    { "length",     1, SQLITE_NUMERIC, 0, lengthFunc },
    { "substr",     3, SQLITE_TEXT,    0, substrFunc },
    { "abs",        1, SQLITE_NUMERIC, 0, absFunc    },
    { "round",      1, SQLITE_NUMERIC, 0, roundFunc  },
    { "round",      2, SQLITE_NUMERIC, 0, roundFunc  },
    { "upper",      1, SQLITE_TEXT,    0, upperFunc  },
    { "lower",      1, SQLITE_TEXT,    0, lowerFunc  },
    { "coalesce",  -1, SQLITE_ARGS,    0, ifnullFunc },
    { "coalesce",   0, 0,              0, 0          },
    { "coalesce",   1, 0,              0, 0          },
    { "ifnull",     2, SQLITE_ARGS,    0, ifnullFunc },
    { "random",    -1, SQLITE_NUMERIC, 0, randomFunc },
    { "like",       2, SQLITE_NUMERIC, 0, likeFunc   },
    { "glob",       2, SQLITE_NUMERIC, 0, globFunc   },
    { "nullif",     2, SQLITE_ARGS,    0, nullifFunc },
    { "sqlite_version",0,SQLITE_TEXT,  0, versionFunc},
    { "quote",      1, SQLITE_ARGS,    0, quoteFunc  },
    { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
    { "change_count",      0, SQLITE_NUMERIC, 1, change_count      },
    { "last_statement_change_count",
                           0, SQLITE_NUMERIC, 1, last_statement_change_count },
#ifdef SQLITE_SOUNDEX
    { "soundex",    1, SQLITE_TEXT,    0, soundexFunc},
#endif
#ifdef SQLITE_TEST
    { "randstr",    2, SQLITE_TEXT,    0, randStr    },
#endif
  };
  static struct {
    char *zName;
    signed char nArg;
    signed char dataType;
    u8 argType;
    void (*xStep)(sqlite_func*,int,const char**);
    void (*xFinalize)(sqlite_func*);
  } aAggs[] = {
    { "min",    1, 0,              0, minmaxStep,   minMaxFinalize },
    { "max",    1, 0,              2, minmaxStep,   minMaxFinalize },
    { "sum",    1, SQLITE_NUMERIC, 0, sumStep,      sumFinalize    },
    { "avg",    1, SQLITE_NUMERIC, 0, sumStep,      avgFinalize    },
    { "count",  0, SQLITE_NUMERIC, 0, countStep,    countFinalize  },
    { "count",  1, SQLITE_NUMERIC, 0, countStep,    countFinalize  },
#if 0
    { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep,   stdDevFinalize },
#endif
  };
  static const char *azTypeFuncs[] = { "min", "max", "typeof" };
  int i;

  for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
    void *pArg;
    switch( aFuncs[i].argType ){
      case 0:  pArg = 0;           break;
      case 1:  pArg = db;          break;
      case 2:  pArg = (void*)(-1); break;
    }
    sqlite_create_function(db, aFuncs[i].zName,
           aFuncs[i].nArg, aFuncs[i].xFunc, pArg);
    if( aFuncs[i].xFunc ){
      sqlite_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
    }
  }
  for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
    void *pArg;
    switch( aAggs[i].argType ){
      case 0:  pArg = 0;           break;
      case 1:  pArg = db;          break;
      case 2:  pArg = (void*)(-1); break;
    }
    sqlite_create_aggregate(db, aAggs[i].zName,
           aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, pArg);
    sqlite_function_type(db, aAggs[i].zName, aAggs[i].dataType);
  }
  for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
    int n = strlen(azTypeFuncs[i]);
    FuncDef *p = sqliteHashFind(&db->aFunc, azTypeFuncs[i], n);
    while( p ){
      p->includeTypes = 1;
      p = p->pNext;
    }
  }
  sqliteRegisterDateTimeFunctions(db);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void substrFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 96 of file func.c.

                                                                         {
  const char *z;
#ifdef SQLITE_UTF8
  const char *z2;
  int i;
#endif
  int p1, p2, len;
  assert( argc==3 );
  z = argv[0];
  if( z==0 ) return;
  p1 = atoi(argv[1]?argv[1]:0);
  p2 = atoi(argv[2]?argv[2]:0);
#ifdef SQLITE_UTF8
  for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
#else
  len = strlen(z);
#endif
  if( p1<0 ){
    p1 += len;
    if( p1<0 ){
      p2 += p1;
      p1 = 0;
    }
  }else if( p1>0 ){
    p1--;
  }
  if( p1+p2>len ){
    p2 = len-p1;
  }
#ifdef SQLITE_UTF8
  for(i=0; i<p1 && z[i]; i++){
    if( (z[i]&0xc0)==0x80 ) p1++;
  }
  while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
  for(; i<p1+p2 && z[i]; i++){
    if( (z[i]&0xc0)==0x80 ) p2++;
  }
  while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
#endif
  if( p2<0 ) p2 = 0;
  sqlite_set_result_string(context, &z[p1], p2);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void sumFinalize ( sqlite_func context) [static]

Definition at line 425 of file func.c.

                                             {
  SumCtx *p;
  p = sqlite_aggregate_context(context, sizeof(*p));
  sqlite_set_result_double(context, p ? p->sum : 0.0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void sumStep ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 416 of file func.c.

                                                                      {
  SumCtx *p;
  if( argc<1 ) return;
  p = sqlite_aggregate_context(context, sizeof(*p));
  if( p && argv[0] ){
    p->sum += sqliteAtoF(argv[0], 0);
    p->cnt++;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void typeofFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 58 of file func.c.

                                                                         {
  assert( argc==2 );
  sqlite_set_result_string(context, argv[1], -1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void upperFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 159 of file func.c.

                                                                        {
  unsigned char *z;
  int i;
  if( argc<1 || argv[0]==0 ) return;
  z = (unsigned char*)sqlite_set_result_string(context, argv[0], -1);
  if( z==0 ) return;
  for(i=0; z[i]; i++){
    if( islower(z[i]) ) z[i] = toupper(z[i]);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void versionFunc ( sqlite_func context,
int  argc,
const char **  argv 
) [static]

Definition at line 279 of file func.c.

Here is the call graph for this function:

Here is the caller graph for this function: