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

📄 func.c

📁 sqlite-3.4.1,嵌入式数据库.是一个功能强大的开源数据库,给学习和研发以及小型公司的发展带来了全所未有的好处.
💻 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.161 2007/06/22 15:21:16 danielk1977 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 unsigned char *z = sqlite3_value_text(argv[0]);      if( z==0 ) return;      len = 0;      while( *z ){        len++;        SQLITE_SKIP_UTF8(z);      }      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 ){        if( (iVal<<1)==0 ){          sqlite3_result_error(context, "integer overflow", -1);          return;        }        iVal = -iVal;      }       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;      sqlite3_result_double(context, rVal);      break;    }  }}/*** Implementation of the substr() function.**** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.** p1 is 1-indexed.  So substr(x,1,1) returns the first character** of x.  If x is text, then we actually count UTF-8 characters.** If x is a blob, then we count bytes.**** If p1 is negative, then we begin abs(p1) from the end of x[].*/static void substrFunc(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  const unsigned char *z;  const unsigned char *z2;  int len;  int p0type;  i64 p1, p2;  assert( argc==3 );  p0type = sqlite3_value_type(argv[0]);  if( p0type==SQLITE_BLOB ){    len = sqlite3_value_bytes(argv[0]);    z = sqlite3_value_blob(argv[0]);    if( z==0 ) return;    assert( len==sqlite3_value_bytes(argv[0]) );  }else{    z = sqlite3_value_text(argv[0]);    if( z==0 ) return;    len = 0;    for(z2=z; *z2; len++){      SQLITE_SKIP_UTF8(z2);    }  }  p1 = sqlite3_value_int(argv[1]);  p2 = sqlite3_value_int(argv[2]);  if( p1<0 ){    p1 += len;    if( p1<0 ){      p2 += p1;      p1 = 0;    }  }else if( p1>0 ){    p1--;  }  if( p1+p2>len ){    p2 = len-p1;  }  if( p0type!=SQLITE_BLOB ){    while( *z && p1 ){      SQLITE_SKIP_UTF8(z);      p1--;    }    for(z2=z; *z2 && p2; p2--){      SQLITE_SKIP_UTF8(z2);    }    sqlite3_result_text(context, (char*)z, z2-z, SQLITE_TRANSIENT);  }else{    if( p2<0 ) p2 = 0;    sqlite3_result_blob(context, (char*)&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( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;  r = sqlite3_value_double(argv[0]);  sqlite3_snprintf(sizeof(zBuf),zBuf,"%.*f",n,r);  sqlite3AtoF(zBuf, &r);  sqlite3_result_double(context, r);}/*** Implementation of the upper() and lower() SQL functions.*/static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){  char *z1;  const char *z2;  int i, n;  if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;  z2 = (char*)sqlite3_value_text(argv[0]);  n = sqlite3_value_bytes(argv[0]);  /* Verify that the call to _bytes() does not invalidate the _text() pointer */  assert( z2==(char*)sqlite3_value_text(argv[0]) );  if( z2 ){    z1 = sqlite3_malloc(n+1);    if( z1 ){      memcpy(z1, z2, n+1);      for(i=0; z1[i]; i++){        z1[i] = toupper(z1[i]);      }      sqlite3_result_text(context, z1, -1, sqlite3_free);    }  }}static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){  char *z1;  const char *z2;  int i, n;  if( argc<1 || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return;  z2 = (char*)sqlite3_value_text(argv[0]);  n = sqlite3_value_bytes(argv[0]);  /* Verify that the call to _bytes() does not invalidate the _text() pointer */  assert( z2==(char*)sqlite3_value_text(argv[0]) );  if( z2 ){    z1 = sqlite3_malloc(n+1);    if( z1 ){      memcpy(z1, z2, n+1);      for(i=0; z1[i]; i++){        z1[i] = tolower(z1[i]);      }      sqlite3_result_text(context, z1, -1, sqlite3_free);    }  }}/*** 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){  sqlite_int64 r;  sqlite3Randomness(sizeof(r), &r);  if( (r<<1)==0 ) r = 0;  /* Prevent 0x8000.... as the result so that we */                          /* can always do abs() of the result */  sqlite3_result_int64(context, r);}/*** Implementation of randomblob(N).  Return a random blob** that is N bytes long.*/static void randomBlob(  sqlite3_context *context,  int argc,  sqlite3_value **argv){  int n;  unsigned char *p;  assert( argc==1 );  n = sqlite3_value_int(argv[0]);  if( n<1 ){    n = 1;  }  if( n>SQLITE_MAX_LENGTH ){    sqlite3_result_error_toobig(context);    return;  }  p = sqliteMalloc(n);  if( p ){    sqlite3Randomness(n, p);    sqlite3_result_blob(context, (char*)p, n, sqlite3FreeX);  }}/*** 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 };/*** Read a single UTF-8 character and return its value.*/u32 sqlite3ReadUtf8(const unsigned char *z){  u32 c;  SQLITE_READ_UTF8(z, c);  return c;}/*** 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;          SQLITE_SKIP_UTF8(zString);        }        zPattern++;      }      if( c && esc && sqlite3ReadUtf8(&zPattern[1])==esc ){        u8 const *zTemp = &zPattern[1];        SQLITE_SKIP_UTF8(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 ){          SQLITE_SKIP_UTF8(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;          SQLITE_SKIP_UTF8(zString);        }        return 0;      }    }else if( !prevEscape && c==matchOne ){      if( *zString==0 ) return 0;      SQLITE_SKIP_UTF8(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 = sqlite3ReadUtf8(zString);

⌨️ 快捷键说明

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