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

📄 func.c

📁 调用sqlite开源数据的小程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/*** 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.110 2005/09/08 20:37:43 drh Exp $*/#include "sqliteInt.h"#include <ctype.h>#include <math.h>#include <stdlib.h>#include <assert.h>#include "vdbeInt.h"#include "os.h"/*** Return the collating function associated with a function.*/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[500];  /* larger than the %f representation of the largest double */  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]);  sqlite3_snprintf(sizeof(zBuf),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 };/* The correct SQL-92 behavior is for the LIKE operator to ignore** case.  Thus  'a' LIKE 'A' would be true. */static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator** is case sensitive causing 'a' LIKE 'A' to be false */static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };/*** 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; 

⌨️ 快捷键说明

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