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

📄 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.98 2005/05/24 12:01:02 danielk1977 Exp $*/#include "sqliteInt.h"#include <ctype.h>#include <math.h>#include <stdlib.h>#include <assert.h>#include "vdbeInt.h"#include "os.h"static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){  return context->pColl;}/*** Implementation of the non-aggregate min() and max() functions*/static void minmaxFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  int i;  int mask;    /* 0 for min() or 0xffffffff for max() */  int iBest;  CollSeq *pColl;  if( argc==0 ) return;  mask = sqlite3_user_data(context)==0 ? 0 : -1;  pColl = sqlite3GetFuncCollSeq(context);  assert( pColl );  assert( mask==-1 || mask==0 );  iBest = 0;  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;  for(i=1; i<argc; i++){    if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;    if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){      iBest = i;    }  }  sqlite3_result_value(context, argv[iBest]);}/*** Return the type of the argument.*/static void typeofFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  const char *z = 0;  switch( sqlite3_value_type(argv[0]) ){    case SQLITE_NULL:    z = "null";    break;    case SQLITE_INTEGER: z = "integer"; break;    case SQLITE_TEXT:    z = "text";    break;    case SQLITE_FLOAT:   z = "real";    break;    case SQLITE_BLOB:    z = "blob";    break;  }  sqlite3_result_text(context, z, -1, SQLITE_STATIC);}/*** Implementation of the length() function*/static void lengthFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  int len;  assert( argc==1 );  switch( sqlite3_value_type(argv[0]) ){    case SQLITE_BLOB:    case SQLITE_INTEGER:    case SQLITE_FLOAT: {      sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));      break;    }    case SQLITE_TEXT: {      const char *z = sqlite3_value_text(argv[0]);      for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }      sqlite3_result_int(context, len);      break;    }    default: {      sqlite3_result_null(context);      break;    }  }}/*** Implementation of the abs() function*/static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){  assert( argc==1 );  switch( sqlite3_value_type(argv[0]) ){    case SQLITE_INTEGER: {      i64 iVal = sqlite3_value_int64(argv[0]);      if( iVal<0 ) iVal = iVal * -1;      sqlite3_result_int64(context, iVal);      break;    }    case SQLITE_NULL: {      sqlite3_result_null(context);      break;    }    default: {      double rVal = sqlite3_value_double(argv[0]);      if( rVal<0 ) rVal = rVal * -1.0;      sqlite3_result_double(context, rVal);      break;    }  }}/*** Implementation of the substr() function*/static void substrFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  const char *z;  const char *z2;  int i;  int p1, p2, len;  assert( argc==3 );  z = sqlite3_value_text(argv[0]);  if( z==0 ) return;  p1 = sqlite3_value_int(argv[1]);  p2 = sqlite3_value_int(argv[2]);  for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }  if( p1<0 ){    p1 += len;    if( p1<0 ){      p2 += p1;      p1 = 0;    }  }else if( p1>0 ){    p1--;  }  if( p1+p2>len ){    p2 = len-p1;  }  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++; }  if( p2<0 ) p2 = 0;  sqlite3_result_text(context, &z[p1], p2, SQLITE_TRANSIENT);}/*** Implementation of the round() function*/static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){  int n = 0;  double r;  char zBuf[100];  assert( argc==1 || argc==2 );  if( argc==2 ){    if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;    n = sqlite3_value_int(argv[1]);    if( n>30 ) n = 30;    if( n<0 ) n = 0;  }  if( SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;  r = sqlite3_value_double(argv[0]);  sprintf(zBuf,"%.*f",n,r);  sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);}/*** Implementation of the upper() and lower() SQL functions.*/static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){  unsigned char *z;  int i;  if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;  z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);  if( z==0 ) return;  strcpy(z, sqlite3_value_text(argv[0]));  for(i=0; z[i]; i++){    z[i] = toupper(z[i]);  }  sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);  sqliteFree(z);}static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){  unsigned char *z;  int i;  if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;  z = sqliteMalloc(sqlite3_value_bytes(argv[0])+1);  if( z==0 ) return;  strcpy(z, sqlite3_value_text(argv[0]));  for(i=0; z[i]; i++){    z[i] = tolower(z[i]);  }  sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);  sqliteFree(z);}/*** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  ** All three do the same thing.  They return the first non-NULL** argument.*/static void ifnullFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  int i;  for(i=0; i<argc; i++){    if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){      sqlite3_result_value(context, argv[i]);      break;    }  }}/*** Implementation of random().  Return a random integer.  */static void randomFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  int r;  sqlite3Randomness(sizeof(r), &r);  sqlite3_result_int(context, r);}/*** Implementation of the last_insert_rowid() SQL function.  The return** value is the same as the sqlite3_last_insert_rowid() API function.*/static void last_insert_rowid(  sqlite3_context *context,   int arg,   sqlite3_value **argv){  sqlite3 *db = sqlite3_user_data(context);  sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));}/*** Implementation of the changes() SQL function.  The return value is the** same as the sqlite3_changes() API function.*/static void changes(  sqlite3_context *context,  int arg,  sqlite3_value **argv){  sqlite3 *db = sqlite3_user_data(context);  sqlite3_result_int(context, sqlite3_changes(db));}/*** Implementation of the total_changes() SQL function.  The return value is** the same as the sqlite3_total_changes() API function.*/static void total_changes(  sqlite3_context *context,  int arg,  sqlite3_value **argv){  sqlite3 *db = sqlite3_user_data(context);  sqlite3_result_int(context, sqlite3_total_changes(db));}/*** A structure defining how to do GLOB-style comparisons.*/struct compareInfo {  u8 matchAll;  u8 matchOne;  u8 matchSet;  u8 noCase;};static const struct compareInfo globInfo = { '*', '?', '[', 0 };static const struct compareInfo likeInfo = { '%', '_',   0, 1 };/*** X is a pointer to the first byte of a UTF-8 character.  Increment** X so that it points to the next character.  This only works right** if X points to a well-formed UTF-8 string.*/#define sqliteNextChar(X)  while( (0xc0&*++(X))==0x80 ){}#define sqliteCharVal(X)   sqlite3ReadUtf8(X)/*** Compare two UTF-8 strings for equality where the first string can** potentially be a "glob" expression.  Return true (1) if they** are the same and false (0) if they are different.**** Globbing rules:****      '*'       Matches any sequence of zero or more characters.****      '?'       Matches exactly one character.****     [...]      Matches one character from the enclosed list of**                characters.****     [^...]     Matches one character not in the enclosed list.**** With the [...] and [^...] matching, a ']' character can be included** in the list by making it the first character after '[' or '^'.  A** range of characters can be specified using '-'.  Example:** "[a-z]" matches any single lower-case letter.  To match a '-', make** it the last character in the list.**** This routine is usually quick, but can be N**2 in the worst case.**** Hints: to match '*' or '?', put them in "[]".  Like this:****         abc[*]xyz        Matches "abc*xyz" only*/static int patternCompare(  const u8 *zPattern,              /* The glob pattern */  const u8 *zString,               /* The string to compare against the glob */  const struct compareInfo *pInfo, /* Information about how to do the compare */  const int esc                    /* The escape character */){  register int c;  int invert;  int seen;  int c2;  u8 matchOne = pInfo->matchOne;  u8 matchAll = pInfo->matchAll;  u8 matchSet = pInfo->matchSet;  u8 noCase = pInfo->noCase;   int prevEscape = 0;     /* True if the previous character was 'escape' */  while( (c = *zPattern)!=0 ){    if( !prevEscape && c==matchAll ){      while( (c=zPattern[1]) == matchAll || c == matchOne ){        if( c==matchOne ){          if( *zString==0 ) return 0;          sqliteNextChar(zString);        }        zPattern++;      }      if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){        u8 const *zTemp = &zPattern[1];        sqliteNextChar(zTemp);        c = *zTemp;      }      if( c==0 ) return 1;      if( c==matchSet ){        assert( esc==0 );   /* This is GLOB, not LIKE */        while( *zString && patternCompare(&zPattern[1],zString,pInfo,esc)==0 ){          sqliteNextChar(zString);        }        return *zString!=0;      }else{        while( (c2 = *zString)!=0 ){          if( noCase ){            c2 = sqlite3UpperToLower[c2];            c = sqlite3UpperToLower[c];            while( c2 != 0 && c2 != c ){ c2 = sqlite3UpperToLower[*++zString]; }          }else{            while( c2 != 0 && c2 != c ){ c2 = *++zString; }          }          if( c2==0 ) return 0;          if( patternCompare(&zPattern[1],zString,pInfo,esc) ) return 1;          sqliteNextChar(zString);        }        return 0;      }    }else if( !prevEscape && c==matchOne ){      if( *zString==0 ) return 0;      sqliteNextChar(zString);      zPattern++;    }else if( c==matchSet ){      int prior_c = 0;      assert( esc==0 );    /* This only occurs for GLOB, not LIKE */      seen = 0;      invert = 0;      c = sqliteCharVal(zString);      if( c==0 ) return 0;      c2 = *++zPattern;      if( c2=='^' ){ invert = 1; c2 = *++zPattern; }      if( c2==']' ){        if( c==']' ) seen = 1;        c2 = *++zPattern;      }      while( (c2 = sqliteCharVal(zPattern))!=0 && c2!=']' ){        if( c2=='-' && zPattern[1]!=']' && zPattern[1]!=0 && prior_c>0 ){          zPattern++;          c2 = sqliteCharVal(zPattern);          if( c>=prior_c && c<=c2 ) seen = 1;          prior_c = 0;        }else if( c==c2 ){          seen = 1;          prior_c = c2;        }else{          prior_c = c2;        }        sqliteNextChar(zPattern);      }      if( c2==0 || (seen ^ invert)==0 ) return 0;      sqliteNextChar(zString);      zPattern++;    }else if( esc && !prevEscape && sqlite3ReadUtf8(zPattern)==esc){      prevEscape = 1;      sqliteNextChar(zPattern);    }else{      if( noCase ){        if( sqlite3UpperToLower[c] != sqlite3UpperToLower[*zString] ) return 0;      }else{        if( c != *zString ) return 0;      }      zPattern++;      zString++;      prevEscape = 0;    }  }  return *zString==0;}/*** Implementation of the like() SQL function.  This function implements** the build-in LIKE operator.  The first argument to the function is the** pattern and the second argument is the string.  So, the SQL statements:****       A LIKE B**** is implemented as like(B,A).**** If the pointer retrieved by via a call to sqlite3_user_data() is** not NULL, then this function uses UTF-16. Otherwise UTF-8.*/static void likeFunc(  sqlite3_context *context,   int argc,   sqlite3_value **argv){  const unsigned char *zA = sqlite3_value_text(argv[0]);  const unsigned char *zB = sqlite3_value_text(argv[1]);  int escape = 0;  if( argc==3 ){    /* The escape character string must consist of a single UTF-8 character.    ** Otherwise, return an error.    */    const unsigned char *zEsc = sqlite3_value_text(argv[2]);    if( sqlite3utf8CharLen(zEsc, -1)!=1 ){      sqlite3_result_error(context,           "ESCAPE expression must be a single character", -1);      return;    }    escape = sqlite3ReadUtf8(zEsc);  }  if( zA && zB ){    sqlite3_result_int(context, patternCompare(zA, zB, &likeInfo, escape));  }}/*** 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(B,A).*/static void globFunc(sqlite3_context *context, int arg, sqlite3_value **argv){  const unsigned char *zA = sqlite3_value_text(argv[0]);  const unsigned char *zB = sqlite3_value_text(argv[1]);  if( zA && zB ){    sqlite3_result_int(context, patternCompare(zA, zB, &globInfo, 0));  }}/*** 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(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  CollSeq *pColl = sqlite3GetFuncCollSeq(context);  if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){    sqlite3_result_value(context, argv[0]);  }}

⌨️ 快捷键说明

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