📄 func.c
字号:
/*** 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.199 2008/08/21 20:21:35 drh Exp $*/#include "sqliteInt.h"#include <ctype.h>#include <stdlib.h>#include <assert.h>#include "vdbeInt.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 || argc==2 ); 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]); if( argc==3 ){ p2 = sqlite3_value_int(argv[2]); }else{ p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; } 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);}/*** Allocate nByte bytes of space using sqlite3_malloc(). If the** allocation fails, call sqlite3_result_error_nomem() to notify** the database handle that malloc() has failed.*/static void *contextMalloc(sqlite3_context *context, i64 nByte){ char *z; if( nByte>sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH] ){ sqlite3_result_error_toobig(context); z = 0; }else{ z = sqlite3Malloc(nByte); if( !z && nByte>0 ){ sqlite3_result_error_nomem(context); } } return z;}/*** 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 = contextMalloc(context, ((i64)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 = contextMalloc(context, ((i64)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; sqlite3_randomness(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; } p = contextMalloc(context, n); if( p ){ sqlite3_randomness(n, p); sqlite3_result_blob(context, (char*)p, n, sqlite3_free); }}/*** 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_context_db_handle(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_context_db_handle(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_context_db_handle(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;};/*** For LIKE and GLOB matching on EBCDIC machines, assume that every** character is exactly one byte in size. Also, all characters are** able to participate in upper-case-to-lower-case mappings in EBCDIC** whereas only characters less than 0x80 do in ASCII.*/#if defined(SQLITE_EBCDIC)# define sqlite3Utf8Read(A,B,C) (*(A++))# define GlogUpperToLower(A) A = sqlite3UpperToLower[A]#else# define GlogUpperToLower(A) if( A<0x80 ){ A = sqlite3UpperToLower[A]; }#endifstatic 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 };/*** 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -