⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 func.c

📁 sqlite数据库管理系统开放源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*** 2002 February 23**** The author disclaims copyright to this source code.  In place of** a legal notice, here is a blessing:****    May you do good and not evil.**    May you find forgiveness for yourself and forgive others.**    May you share freely, never taking more than you give.***************************************************************************** This file contains the C functions that implement various SQL** functions of SQLite.  **** There is only one exported symbol in this file - the function** sqliteRegisterBuildinFunctions() found at the bottom of the file.** All other code has file scope.**** $Id: func.c,v 1.43.2.3 2004/07/18 23:03:11 drh Exp $*/#include <ctype.h>#include <math.h>#include <stdlib.h>#include <assert.h>#include "sqliteInt.h"#include "os.h"/*** Implementation of the non-aggregate min() and max() functions*/static void minmaxFunc(sqlite_func *context, int argc, const char **argv){  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);}/*** Return the type of the argument.*/static void typeofFunc(sqlite_func *context, int argc, const char **argv){  assert( argc==2 );  sqlite_set_result_string(context, argv[1], -1);}/*** Implementation of the length() function*/static void lengthFunc(sqlite_func *context, int argc, const char **argv){  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);}/*** Implementation of the abs() function*/static void absFunc(sqlite_func *context, int argc, const char **argv){  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);}/*** Implementation of the substr() function*/static void substrFunc(sqlite_func *context, int argc, const char **argv){  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);}/*** Implementation of the round() function*/static void roundFunc(sqlite_func *context, int argc, const char **argv){  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);}/*** Implementation of the upper() and lower() SQL functions.*/static void upperFunc(sqlite_func *context, int argc, const char **argv){  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]);  }}static void lowerFunc(sqlite_func *context, int argc, const char **argv){  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]);  }}/*** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  ** All three do the same thing.  They return the first non-NULL** argument.*/static void ifnullFunc(sqlite_func *context, int argc, const char **argv){  int i;  for(i=0; i<argc; i++){    if( argv[i] ){      sqlite_set_result_string(context, argv[i], -1);      break;    }  }}/*** Implementation of random().  Return a random integer.  */static void randomFunc(sqlite_func *context, int argc, const char **argv){  int r;  sqliteRandomness(sizeof(r), &r);  sqlite_set_result_int(context, r);}/*** Implementation of the last_insert_rowid() SQL function.  The return** value is the same as the sqlite_last_insert_rowid() API function.*/static void last_insert_rowid(sqlite_func *context, int arg, const char **argv){  sqlite *db = sqlite_user_data(context);  sqlite_set_result_int(context, sqlite_last_insert_rowid(db));}/*** Implementation of the change_count() SQL function.  The return** value is the same as the sqlite_changes() API function.*/static void change_count(sqlite_func *context, int arg, const char **argv){  sqlite *db = sqlite_user_data(context);  sqlite_set_result_int(context, sqlite_changes(db));}/*** Implementation of the last_statement_change_count() SQL function.  The** return value is the same as the sqlite_last_statement_changes() API function.*/static void last_statement_change_count(sqlite_func *context, int arg,                                        const char **argv){  sqlite *db = sqlite_user_data(context);  sqlite_set_result_int(context, sqlite_last_statement_changes(db));}/*** Implementation of the like() SQL function.  This function implements** the build-in LIKE operator.  The first argument to the function is the** string and the second argument is the pattern.  So, the SQL statements:****       A LIKE B**** is implemented as like(A,B).*/static void likeFunc(sqlite_func *context, int arg, const char **argv){  if( argv[0]==0 || argv[1]==0 ) return;  sqlite_set_result_int(context,     sqliteLikeCompare((const unsigned char*)argv[0],                      (const unsigned char*)argv[1]));}/*** Implementation of the glob() SQL function.  This function implements** the build-in GLOB operator.  The first argument to the function is the** string and the second argument is the pattern.  So, the SQL statements:****       A GLOB B**** is implemented as glob(A,B).*/static void globFunc(sqlite_func *context, int arg, const char **argv){  if( argv[0]==0 || argv[1]==0 ) return;  sqlite_set_result_int(context,    sqliteGlobCompare((const unsigned char*)argv[0],                      (const unsigned char*)argv[1]));}/*** Implementation of the NULLIF(x,y) function.  The result is the first** argument if the arguments are different.  The result is NULL if the** arguments are equal to each other.*/static void nullifFunc(sqlite_func *context, int argc, const char **argv){  if( argv[0]!=0 && sqliteCompare(argv[0],argv[1])!=0 ){    sqlite_set_result_string(context, argv[0], -1);  }}/*** Implementation of the VERSION(*) function.  The result is the version** of the SQLite library that is running.*/static void versionFunc(sqlite_func *context, int argc, const char **argv){  sqlite_set_result_string(context, sqlite_version, -1);}/*** EXPERIMENTAL - This is not an official function.  The interface may** change.  This function may disappear.  Do not write code that depends** on this function.**** Implementation of the QUOTE() function.  This function takes a single** argument.  If the argument is numeric, the return value is the same as** the argument.  If the argument is NULL, the return value is the string** "NULL".  Otherwise, the argument is enclosed in single quotes with** single-quote escapes.*/static void quoteFunc(sqlite_func *context, int argc, const char **argv){  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);  }}#ifdef SQLITE_SOUNDEX/*** Compute the soundex encoding of a word.*/static void soundexFunc(sqlite_func *context, int argc, const char **argv){  char zResult[8];  const char *zIn;  int i, j;  static const unsigned char iCode[] = {    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -