0000: 2f 2a 0a 2a 2a 20 32 30 30 32 20 46 65 62 72 75 /*.** 2002 Febru
0010: 61 72 79 20 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65 ary 23.**.** The
0020: 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
0030: 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
0040: 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
0050: 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
0060: 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
0070: 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
0080: 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
0090: 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
00a0: 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
00b0: 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
00c0: 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
00d0: 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
00e0: 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
00f0: 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
0100: 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
0110: 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
0120: 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
0180: 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
0190: 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
01a0: 20 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f implement vario
01b0: 75 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 us SQL.** functi
01c0: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 ons of SQLite.
01d0: 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 .**.** There is
01e0: 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74 65 only one exporte
01f0: 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69 73 d symbol in this
0200: 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e 63 file - the func
0210: 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 52 65 tion.** sqliteRe
0220: 67 69 73 74 65 72 42 75 69 6c 64 69 6e 46 75 6e gisterBuildinFun
0230: 63 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 ctions() found a
0240: 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 t the bottom of
0250: 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c the file..** All
0260: 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 other code has
0270: 66 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2f 0a 23 file scope..*/.#
0280: 69 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 49 include "sqliteI
0290: 6e 74 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 3c nt.h".#include <
02a0: 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 stdlib.h>.#inclu
02b0: 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 de <assert.h>.#i
02c0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
02d0: 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
02e0: 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6d 61 74 68 .# include <math
02f0: 2e 68 3e 0a 23 65 6e 64 69 66 0a 23 69 6e 63 6c .h>.#endif.#incl
0300: 75 64 65 20 22 76 64 62 65 49 6e 74 2e 68 22 0a ude "vdbeInt.h".
0310: 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
0320: 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 e collating func
0330: 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 tion associated
0340: 77 69 74 68 20 61 20 66 75 6e 63 74 69 6f 6e 2e with a function.
0350: 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 .*/.static CollS
0360: 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 46 75 eq *sqlite3GetFu
0370: 6e 63 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 ncCollSeq(sqlite
0380: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
0390: 78 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f xt){. return co
03a0: 6e 74 65 78 74 2d 3e 70 43 6f 6c 6c 3b 0a 7d 0a ntext->pColl;.}.
03b0: 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20 ./*.** Indicate
03c0: 74 68 61 74 20 74 68 65 20 61 63 63 75 6d 75 6c that the accumul
03d0: 61 74 6f 72 20 6c 6f 61 64 20 73 68 6f 75 6c 64 ator load should
03e0: 20 62 65 20 73 6b 69 70 70 65 64 20 6f 6e 20 74 be skipped on t
03f0: 68 69 73 0a 2a 2a 20 69 74 65 72 61 74 69 6f 6e his.** iteration
0400: 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61 74 of the aggregat
0410: 65 20 6c 6f 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 e loop..*/.stati
0420: 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 6b c void sqlite3Sk
0430: 69 70 41 63 63 75 6d 75 6c 61 74 6f 72 4c 6f 61 ipAccumulatorLoa
0440: 64 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 d(sqlite3_contex
0450: 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 63 t *context){. c
0460: 6f 6e 74 65 78 74 2d 3e 73 6b 69 70 46 6c 61 67 ontext->skipFlag
0470: 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 = 1;.}../*.** I
0480: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
0490: 20 74 68 65 20 6e 6f 6e 2d 61 67 67 72 65 67 61 the non-aggrega
04a0: 74 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 te min() and max
04b0: 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a () functions.*/.
04c0: 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d static void minm
04d0: 61 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 axFunc(. sqlite
04e0: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
04f0: 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a xt,. int argc,.
0500: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
0510: 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 **argv.){. int
0520: 69 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20 i;. int mask;
0530: 20 20 2f 2a 20 30 20 66 6f 72 20 6d 69 6e 28 29 /* 0 for min()
0540: 20 6f 72 20 30 78 66 66 66 66 66 66 66 66 20 66 or 0xffffffff f
0550: 6f 72 20 6d 61 78 28 29 20 2a 2f 0a 20 20 69 6e or max() */. in
0560: 74 20 69 42 65 73 74 3b 0a 20 20 43 6f 6c 6c 53 t iBest;. CollS
0570: 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 61 73 eq *pColl;.. as
0580: 73 65 72 74 28 20 61 72 67 63 3e 31 20 29 3b 0a sert( argc>1 );.
0590: 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 mask = sqlite3
05a0: 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 _user_data(conte
05b0: 78 74 29 3d 3d 30 20 3f 20 30 20 3a 20 2d 31 3b xt)==0 ? 0 : -1;
05c0: 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 . pColl = sqlit
05d0: 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 e3GetFuncCollSeq
05e0: 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 (context);. ass
05f0: 65 72 74 28 20 70 43 6f 6c 6c 20 29 3b 0a 20 20 ert( pColl );.
0600: 61 73 73 65 72 74 28 20 6d 61 73 6b 3d 3d 2d 31 assert( mask==-1
0610: 20 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 || mask==0 );.
0620: 20 69 42 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 iBest = 0;. if
0630: 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ( sqlite3_value_
0640: 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 type(argv[0])==S
0650: 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 QLITE_NULL ) ret
0660: 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 urn;. for(i=1;
0670: 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 i<argc; i++){.
0680: 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 if( sqlite3_va
0690: 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d lue_type(argv[i]
06a0: 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 )==SQLITE_NULL )
06b0: 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 return;. if(
06c0: 20 28 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 (sqlite3MemComp
06d0: 61 72 65 28 61 72 67 76 5b 69 42 65 73 74 5d 2c are(argv[iBest],
06e0: 20 61 72 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c 29 argv[i], pColl)
06f0: 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a 20 20 20 ^mask)>=0 ){.
0700: 20 20 20 74 65 73 74 63 61 73 65 28 20 6d 61 73 testcase( mas
0710: 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 42 k==0 );. iB
0720: 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 est = i;. }.
0730: 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 }. sqlite3_res
0740: 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 ult_value(contex
0750: 74 2c 20 61 72 67 76 5b 69 42 65 73 74 5d 29 3b t, argv[iBest]);
0760: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
0770: 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 the type of the
0780: 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 argument..*/.st
0790: 61 74 69 63 20 76 6f 69 64 20 74 79 70 65 6f 66 atic void typeof
07a0: 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
07b0: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
07c0: 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c ,. int NotUsed,
07d0: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
07e0: 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e **argv.){. con
07f0: 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a st char *z = 0;.
0800: 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
0810: 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 73 ER(NotUsed);. s
0820: 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 witch( sqlite3_v
0830: 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 alue_type(argv[0
0840: 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 ]) ){. case S
0850: 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7a QLITE_INTEGER: z
0860: 20 3d 20 22 69 6e 74 65 67 65 72 22 3b 20 62 72 = "integer"; br
0870: 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 eak;. case SQ
0880: 4c 49 54 45 5f 54 45 58 54 3a 20 20 20 20 7a 20 LITE_TEXT: z
0890: 3d 20 22 74 65 78 74 22 3b 20 20 20 20 62 72 65 = "text"; bre
08a0: 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ak;. case SQL
08b0: 49 54 45 5f 46 4c 4f 41 54 3a 20 20 20 7a 20 3d ITE_FLOAT: z =
08c0: 20 22 72 65 61 6c 22 3b 20 20 20 20 62 72 65 61 "real"; brea
08d0: 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 k;. case SQLI
08e0: 54 45 5f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d 20 TE_BLOB: z =
08f0: 22 62 6c 6f 62 22 3b 20 20 20 20 62 72 65 61 6b "blob"; break
0900: 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 ;. default:
0910: 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20 22 z = "
0920: 6e 75 6c 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b null"; break;
0930: 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 . }. sqlite3_r
0940: 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
0950: 78 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 xt, z, -1, SQLIT
0960: 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a 2f E_STATIC);.}.../
0970: 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
0980: 69 6f 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 ion of the lengt
0990: 68 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a h() function.*/.
09a0: 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 6e 67 static void leng
09b0: 74 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 thFunc(. sqlite
09c0: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
09d0: 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a xt,. int argc,.
09e0: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
09f0: 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 **argv.){. int
0a00: 6c 65 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 len;.. assert(
0a10: 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 argc==1 );. UNU
0a20: 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 SED_PARAMETER(ar
0a30: 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 gc);. switch( s
0a40: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
0a50: 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 e(argv[0]) ){.
0a60: 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c case SQLITE_BL
0a70: 4f 42 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c OB:. case SQL
0a80: 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 ITE_INTEGER:.
0a90: 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f case SQLITE_FLO
0aa0: 41 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 AT: {. sqli
0ab0: 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 te3_result_int(c
0ac0: 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f ontext, sqlite3_
0ad0: 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 value_bytes(argv
0ae0: 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 62 72 65 [0]));. bre
0af0: 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
0b00: 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 se SQLITE_TEXT:
0b10: 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e {. const un
0b20: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d signed char *z =
0b30: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
0b40: 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 ext(argv[0]);.
0b50: 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 if( z==0 ) r
0b60: 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 6c 65 6e eturn;. len
0b70: 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c = 0;. whil
0b80: 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20 20 20 20 e( *z ){.
0b90: 20 6c 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 len++;.
0ba0: 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 SQLITE_SKIP_UTF8
0bb0: 28 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 (z);. }.
0bc0: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
0bd0: 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 6c t_int(context, l
0be0: 65 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b en);. break
0bf0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
0c00: 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c ult: {. sql
0c10: 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c ite3_result_null
0c20: 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 (context);.
0c30: 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
0c40: 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 }.}../*.** Imple
0c50: 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
0c60: 20 61 62 73 28 29 20 66 75 6e 63 74 69 6f 6e 2e abs() function.
0c70: 0a 2a 2a 0a 2a 2a 20 49 4d 50 3a 20 52 2d 32 33 .**.** IMP: R-23
0c80: 39 37 39 2d 32 36 38 35 35 20 54 68 65 20 61 62 979-26855 The ab
0c90: 73 28 58 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 s(X) function re
0ca0: 74 75 72 6e 73 20 74 68 65 20 61 62 73 6f 6c 75 turns the absolu
0cb0: 74 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 74 te value of.** t
0cc0: 68 65 20 6e 75 6d 65 72 69 63 20 61 72 67 75 6d he numeric argum
0cd0: 65 6e 74 20 58 2e 20 0a 2a 2f 0a 73 74 61 74 69 ent X. .*/.stati
0ce0: 63 20 76 6f 69 64 20 61 62 73 46 75 6e 63 28 73 c void absFunc(s
0cf0: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
0d00: 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 context, int arg
0d10: 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 c, sqlite3_value
0d20: 20 2a 2a 61 72 67 76 29 7b 0a 20 20 61 73 73 65 **argv){. asse
0d30: 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 rt( argc==1 );.
0d40: 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
0d50: 52 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 R(argc);. switc
0d60: 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 h( sqlite3_value
0d70: 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 _type(argv[0]) )
0d80: 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
0d90: 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 E_INTEGER: {.
0da0: 20 20 20 69 36 34 20 69 56 61 6c 20 3d 20 73 71 i64 iVal = sq
0db0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 lite3_value_int6
0dc0: 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 4(argv[0]);.
0dd0: 20 20 69 66 28 20 69 56 61 6c 3c 30 20 29 7b 0a if( iVal<0 ){.
0de0: 20 20 20 20 20 20 20 20 69 66 28 20 28 69 56 61 if( (iVa
0df0: 6c 3c 3c 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 l<<1)==0 ){.
0e00: 20 20 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d /* IMP: R-
0e10: 33 35 34 36 30 2d 31 35 30 38 34 20 49 66 20 58 35460-15084 If X
0e20: 20 69 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 is the integer
0e30: 2d 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 -922337203685477
0e40: 35 38 30 37 20 74 68 65 6e 0a 20 20 20 20 20 20 5807 then.
0e50: 20 20 20 20 2a 2a 20 61 62 73 28 58 29 20 74 68 ** abs(X) th
0e60: 72 6f 77 73 20 61 6e 20 69 6e 74 65 67 65 72 20 rows an integer
0e70: 6f 76 65 72 66 6c 6f 77 20 65 72 72 6f 72 20 73 overflow error s
0e80: 69 6e 63 65 20 74 68 65 72 65 20 69 73 20 6e 6f ince there is no
0e90: 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65 71 . ** eq
0ea0: 75 69 76 61 6c 65 6e 74 20 70 6f 73 69 74 69 76 uivalent positiv
0eb0: 65 20 36 34 2d 62 69 74 20 74 77 6f 20 63 6f 6d e 64-bit two com
0ec0: 70 6c 65 6d 65 6e 74 20 76 61 6c 75 65 2e 20 2a plement value. *
0ed0: 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 /. sqli
0ee0: 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
0ef0: 28 63 6f 6e 74 65 78 74 2c 20 22 69 6e 74 65 67 (context, "integ
0f00: 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c 20 2d 31 er overflow", -1
0f10: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 );. ret
0f20: 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 urn;. }.
0f30: 20 20 20 20 20 20 20 69 56 61 6c 20 3d 20 2d 69 iVal = -i
0f40: 56 61 6c 3b 0a 20 20 20 20 20 20 7d 20 0a 20 20 Val;. } .
0f50: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
0f60: 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 lt_int64(context
0f70: 2c 20 69 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 , iVal);. b
0f80: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
0f90: 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c case SQLITE_NULL
0fa0: 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49 4d 50 : {. /* IMP
0fb0: 3a 20 52 2d 33 37 34 33 34 2d 31 39 39 32 39 20 : R-37434-19929
0fc0: 41 62 73 28 58 29 20 72 65 74 75 72 6e 73 20 4e Abs(X) returns N
0fd0: 55 4c 4c 20 69 66 20 58 20 69 73 20 4e 55 4c 4c ULL if X is NULL
0fe0: 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 . */. sqlit
0ff0: 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 e3_result_null(c
1000: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 ontext);. b
1010: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1020: 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
1030: 20 2f 2a 20 42 65 63 61 75 73 65 20 73 71 6c 69 /* Because sqli
1040: 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 te3_value_double
1050: 28 29 20 72 65 74 75 72 6e 73 20 30 2e 30 20 69 () returns 0.0 i
1060: 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 f the argument i
1070: 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 73 s not. ** s
1080: 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 63 61 omething that ca
1090: 6e 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 n be converted i
10a0: 6e 74 6f 20 61 20 6e 75 6d 62 65 72 2c 20 77 65 nto a number, we
10b0: 20 68 61 76 65 3a 0a 20 20 20 20 20 20 2a 2a 20 have:. **
10c0: 49 4d 50 3a 20 52 2d 35 37 33 32 36 2d 33 31 35 IMP: R-57326-315
10d0: 34 31 20 41 62 73 28 58 29 20 72 65 74 75 72 6e 41 Abs(X) return
10e0: 20 30 2e 30 20 69 66 20 58 20 69 73 20 61 20 73 0.0 if X is a s
10f0: 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 68 tring or blob th
1100: 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 61 6e 6e at. ** cann
1110: 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 ot be converted
1120: 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c to a numeric val
1130: 75 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ue. . */.
1140: 20 20 20 20 64 6f 75 62 6c 65 20 72 56 61 6c 20 double rVal
1150: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1160: 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b double(argv[0]);
1170: 0a 20 20 20 20 20 20 69 66 28 20 72 56 61 6c 3c . if( rVal<
1180: 30 20 29 20 72 56 61 6c 20 3d 20 2d 72 56 61 6c 0 ) rVal = -rVal
1190: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
11a0: 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f result_double(co
11b0: 6e 74 65 78 74 2c 20 72 56 61 6c 29 3b 0a 20 20 ntext, rVal);.
11c0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
11d0: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d . }.}../*.** Im
11e0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
11f0: 74 68 65 20 69 6e 73 74 72 28 29 20 66 75 6e 63 the instr() func
1200: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73 74 tion..**.** inst
1210: 72 28 68 61 79 73 74 61 63 6b 2c 6e 65 65 64 6c r(haystack,needl
1220: 65 29 20 66 69 6e 64 73 20 74 68 65 20 66 69 72 e) finds the fir
1230: 73 74 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 st occurrence of
1240: 20 6e 65 65 64 6c 65 0a 2a 2a 20 69 6e 20 68 61 needle.** in ha
1250: 79 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 ystack and retur
1260: 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
1270: 20 70 72 65 76 69 6f 75 73 20 63 68 61 72 61 63 previous charac
1280: 74 65 72 73 20 70 6c 75 73 20 31 2c 0a 2a 2a 20 ters plus 1,.**
1290: 6f 72 20 30 20 69 66 20 6e 65 65 64 6c 65 20 64 or 0 if needle d
12a0: 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 77 69 oes not occur wi
12b0: 74 68 69 6e 20 68 61 79 73 74 61 63 6b 2e 0a 2a thin haystack..*
12c0: 2a 0a 2a 2a 20 49 66 20 62 6f 74 68 20 68 61 79 *.** If both hay
12d0: 73 74 61 63 6b 20 61 6e 64 20 6e 65 65 64 6c 65 stack and needle
12e0: 20 61 72 65 20 42 4c 4f 42 73 2c 20 74 68 65 6e are BLOBs, then
12f0: 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6f the result is o
1300: 6e 65 20 6d 6f 72 65 20 74 68 61 6e 0a 2a 2a 20 ne more than.**
1310: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
1320: 74 65 73 20 69 6e 20 68 61 79 73 74 61 63 6b 20 tes in haystack
1330: 70 72 69 6f 72 20 74 6f 20 74 68 65 20 66 69 72 prior to the fir
1340: 73 74 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 st occurrence of
1350: 20 6e 65 65 64 6c 65 2c 0a 2a 2a 20 6f 72 20 30 needle,.** or 0
1360: 20 69 66 20 6e 65 65 64 6c 65 20 6e 65 76 65 72 if needle never
1370: 20 6f 63 63 75 72 73 20 69 6e 20 68 61 79 73 74 occurs in hayst
1380: 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ack..*/.static v
1390: 6f 69 64 20 69 6e 73 74 72 46 75 6e 63 28 0a 20 oid instrFunc(.
13a0: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
13b0: 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
13c0: 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 argc,. sqlite3
13d0: 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
13e0: 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 . const unsigne
13f0: 64 20 63 68 61 72 20 2a 7a 48 61 79 73 74 61 63 d char *zHaystac
1400: 6b 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 k;. const unsig
1410: 6e 65 64 20 63 68 61 72 20 2a 7a 4e 65 65 64 6c ned char *zNeedl
1420: 65 3b 0a 20 20 69 6e 74 20 6e 48 61 79 73 74 61 e;. int nHaysta
1430: 63 6b 3b 0a 20 20 69 6e 74 20 6e 4e 65 65 64 6c ck;. int nNeedl
1440: 65 3b 0a 20 20 69 6e 74 20 74 79 70 65 48 61 79 e;. int typeHay
1450: 73 74 61 63 6b 2c 20 74 79 70 65 4e 65 65 64 6c stack, typeNeedl
1460: 65 3b 0a 20 20 69 6e 74 20 4e 20 3d 20 31 3b 0a e;. int N = 1;.
1470: 20 20 69 6e 74 20 69 73 54 65 78 74 3b 0a 0a 20 int isText;..
1480: 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1490: 52 28 61 72 67 63 29 3b 0a 20 20 74 79 70 65 48 R(argc);. typeH
14a0: 61 79 73 74 61 63 6b 20 3d 20 73 71 6c 69 74 65 aystack = sqlite
14b0: 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
14c0: 76 5b 30 5d 29 3b 0a 20 20 74 79 70 65 4e 65 65 v[0]);. typeNee
14d0: 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 dle = sqlite3_va
14e0: 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 5d lue_type(argv[1]
14f0: 29 3b 0a 20 20 69 66 28 20 74 79 70 65 48 61 79 );. if( typeHay
1500: 73 74 61 63 6b 3d 3d 53 51 4c 49 54 45 5f 4e 55 stack==SQLITE_NU
1510: 4c 4c 20 7c 7c 20 74 79 70 65 4e 65 65 64 6c 65 LL || typeNeedle
1520: 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 ==SQLITE_NULL )
1530: 72 65 74 75 72 6e 3b 0a 20 20 6e 48 61 79 73 74 return;. nHayst
1540: 61 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 ack = sqlite3_va
1550: 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 lue_bytes(argv[0
1560: 5d 29 3b 0a 20 20 6e 4e 65 65 64 6c 65 20 3d 20 ]);. nNeedle =
1570: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 sqlite3_value_by
1580: 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 tes(argv[1]);.
1590: 69 66 28 20 74 79 70 65 48 61 79 73 74 61 63 6b if( typeHaystack
15a0: 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 26 26 ==SQLITE_BLOB &&
15b0: 20 74 79 70 65 4e 65 65 64 6c 65 3d 3d 53 51 4c typeNeedle==SQL
15c0: 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 ITE_BLOB ){.
15d0: 7a 48 61 79 73 74 61 63 6b 20 3d 20 73 71 6c 69 zHaystack = sqli
15e0: 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 te3_value_blob(a
15f0: 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 7a 4e 65 rgv[0]);. zNe
1600: 65 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 edle = sqlite3_v
1610: 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 31 alue_blob(argv[1
1620: 5d 29 3b 0a 20 20 20 20 69 73 54 65 78 74 20 3d ]);. isText =
1630: 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
1640: 20 7a 48 61 79 73 74 61 63 6b 20 3d 20 73 71 6c zHaystack = sql
1650: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
1660: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 7a 4e argv[0]);. zN
1670: 65 65 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 5f eedle = sqlite3_
1680: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
1690: 31 5d 29 3b 0a 20 20 20 20 69 73 54 65 78 74 20 1]);. isText
16a0: 3d 20 31 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 = 1;. }. while
16b0: 28 20 6e 4e 65 65 64 6c 65 3c 3d 6e 48 61 79 73 ( nNeedle<=nHays
16c0: 74 61 63 6b 20 26 26 20 6d 65 6d 63 6d 70 28 7a tack && memcmp(z
16d0: 48 61 79 73 74 61 63 6b 2c 20 7a 4e 65 65 64 6c Haystack, zNeedl
16e0: 65 2c 20 6e 4e 65 65 64 6c 65 29 21 3d 30 20 29 e, nNeedle)!=0 )
16f0: 7b 0a 20 20 20 20 4e 2b 2b 3b 0a 20 20 20 20 64 {. N++;. d
1700: 6f 7b 0a 20 20 20 20 20 20 6e 48 61 79 73 74 61 o{. nHaysta
1710: 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 7a 48 61 79 ck--;. zHay
1720: 73 74 61 63 6b 2b 2b 3b 0a 20 20 20 20 7d 77 68 stack++;. }wh
1730: 69 6c 65 28 20 69 73 54 65 78 74 20 26 26 20 28 ile( isText && (
1740: 7a 48 61 79 73 74 61 63 6b 5b 30 5d 26 30 78 63 zHaystack[0]&0xc
1750: 30 29 3d 3d 30 78 38 30 20 29 3b 0a 20 20 7d 0a 0)==0x80 );. }.
1760: 20 20 69 66 28 20 6e 4e 65 65 64 6c 65 3e 6e 48 if( nNeedle>nH
1770: 61 79 73 74 61 63 6b 20 29 20 4e 20 3d 20 30 3b aystack ) N = 0;
1780: 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
1790: 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 4e t_int(context, N
17a0: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c );.}../*.** Impl
17b0: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
17c0: 65 20 73 75 62 73 74 72 28 29 20 66 75 6e 63 74 e substr() funct
17d0: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 73 74 ion..**.** subst
17e0: 72 28 78 2c 70 31 2c 70 32 29 20 20 72 65 74 75 r(x,p1,p2) retu
17f0: 72 6e 73 20 70 32 20 63 68 61 72 61 63 74 65 72 rns p2 character
1800: 73 20 6f 66 20 78 5b 5d 20 62 65 67 69 6e 6e 69 s of x[] beginni
1810: 6e 67 20 77 69 74 68 20 70 31 2e 0a 2a 2a 20 70 ng with p1..** p
1820: 31 20 69 73 20 31 2d 69 6e 64 65 78 65 64 2e 20 1 is 1-indexed.
1830: 20 53 6f 20 73 75 62 73 74 72 28 78 2c 31 2c 31 So substr(x,1,1
1840: 29 20 72 65 74 75 72 6e 73 20 74 68 65 20 66 69 ) returns the fi
1850: 72 73 74 20 63 68 61 72 61 63 74 65 72 0a 2a 2a rst character.**
1860: 20 6f 66 20 78 2e 20 20 49 66 20 78 20 69 73 20 of x. If x is
1870: 74 65 78 74 2c 20 74 68 65 6e 20 77 65 20 61 63 text, then we ac
1880: 74 75 61 6c 6c 79 20 63 6f 75 6e 74 20 55 54 46 tually count UTF
1890: 2d 38 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a -8 characters..*
18a0: 2a 20 49 66 20 78 20 69 73 20 61 20 62 6c 6f 62 * If x is a blob
18b0: 2c 20 74 68 65 6e 20 77 65 20 63 6f 75 6e 74 20 , then we count
18c0: 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 bytes..**.** If
18d0: 70 31 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 p1 is negative,
18e0: 74 68 65 6e 20 77 65 20 62 65 67 69 6e 20 61 62 then we begin ab
18f0: 73 28 70 31 29 20 66 72 6f 6d 20 74 68 65 20 65 s(p1) from the e
1900: 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a 2a 0a 2a 2a nd of x[]..**.**
1910: 20 49 66 20 70 32 20 69 73 20 6e 65 67 61 74 69 If p2 is negati
1920: 76 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 70 ve, return the p
1930: 32 20 63 68 61 72 61 63 74 65 72 73 20 70 72 65 2 characters pre
1940: 63 65 65 64 69 6e 67 20 70 31 2e 0a 2a 2f 0a 73 ceeding p1..*/.s
1950: 74 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 tatic void subst
1960: 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 rFunc(. sqlite3
1970: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
1980: 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
1990: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
19a0: 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 *argv.){. const
19b0: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
19c0: 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 z;. const unsig
19d0: 6e 65 64 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 ned char *z2;.
19e0: 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 70 int len;. int p
19f0: 30 74 79 70 65 3b 0a 20 20 69 36 34 20 70 31 2c 0type;. i64 p1,
1a00: 20 70 32 3b 0a 20 20 69 6e 74 20 6e 65 67 50 32 p2;. int negP2
1a10: 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 = 0;.. assert(
1a20: 20 61 72 67 63 3d 3d 33 20 7c 7c 20 61 72 67 63 argc==3 || argc
1a30: 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c ==2 );. if( sql
1a40: 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
1a50: 61 72 67 76 5b 31 5d 29 3d 3d 53 51 4c 49 54 45 argv[1])==SQLITE
1a60: 5f 4e 55 4c 4c 0a 20 20 20 7c 7c 20 28 61 72 67 _NULL. || (arg
1a70: 63 3d 3d 33 20 26 26 20 73 71 6c 69 74 65 33 5f c==3 && sqlite3_
1a80: 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b value_type(argv[
1a90: 32 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 2])==SQLITE_NULL
1aa0: 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ). ){. retur
1ab0: 6e 3b 0a 20 20 7d 0a 20 20 70 30 74 79 70 65 20 n;. }. p0type
1ac0: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1ad0: 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 type(argv[0]);.
1ae0: 20 70 31 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 p1 = sqlite3_va
1af0: 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 lue_int(argv[1])
1b00: 3b 0a 20 20 69 66 28 20 70 30 74 79 70 65 3d 3d ;. if( p0type==
1b10: 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 SQLITE_BLOB ){.
1b20: 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 len = sqlite3
1b30: 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 _value_bytes(arg
1b40: 76 5b 30 5d 29 3b 0a 20 20 20 20 7a 20 3d 20 73 v[0]);. z = s
1b50: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f qlite3_value_blo
1b60: 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 b(argv[0]);.
1b70: 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 if( z==0 ) retur
1b80: 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c n;. assert( l
1b90: 65 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 en==sqlite3_valu
1ba0: 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 e_bytes(argv[0])
1bb0: 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
1bc0: 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c z = sqlite3_val
1bd0: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
1be0: 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 ;. if( z==0 )
1bf0: 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6c 65 6e return;. len
1c00: 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 31 = 0;. if( p1
1c10: 3c 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 <0 ){. for(
1c20: 7a 32 3d 7a 3b 20 2a 7a 32 3b 20 6c 65 6e 2b 2b z2=z; *z2; len++
1c30: 29 7b 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 ){. SQLIT
1c40: 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b E_SKIP_UTF8(z2);
1c50: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1c60: 20 7d 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 }. if( argc==3
1c70: 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c ){. p2 = sql
1c80: 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 ite3_value_int(a
1c90: 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 rgv[2]);. if(
1ca0: 20 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 p2<0 ){. p
1cb0: 32 20 3d 20 2d 70 32 3b 0a 20 20 20 20 20 20 6e 2 = -p2;. n
1cc0: 65 67 50 32 20 3d 20 31 3b 0a 20 20 20 20 7d 0a egP2 = 1;. }.
1cd0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 32 20 }else{. p2
1ce0: 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 = sqlite3_contex
1cf0: 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 t_db_handle(cont
1d00: 65 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ext)->aLimit[SQL
1d10: 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
1d20: 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 31 3c ];. }. if( p1<
1d30: 30 20 29 7b 0a 20 20 20 20 70 31 20 2b 3d 20 6c 0 ){. p1 += l
1d40: 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 31 3c 30 en;. if( p1<0
1d50: 20 29 7b 0a 20 20 20 20 20 20 70 32 20 2b 3d 20 ){. p2 +=
1d60: 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32 p1;. if( p2
1d70: 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 20 20 <0 ) p2 = 0;.
1d80: 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d p1 = 0;. }
1d90: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 31 3e . }else if( p1>
1da0: 30 20 29 7b 0a 20 20 20 20 70 31 2d 2d 3b 0a 20 0 ){. p1--;.
1db0: 20 7d 65 6c 73 65 20 69 66 28 20 70 32 3e 30 20 }else if( p2>0
1dc0: 29 7b 0a 20 20 20 20 70 32 2d 2d 3b 0a 20 20 7d ){. p2--;. }
1dd0: 0a 20 20 69 66 28 20 6e 65 67 50 32 20 29 7b 0a . if( negP2 ){.
1de0: 20 20 20 20 70 31 20 2d 3d 20 70 32 3b 0a 20 20 p1 -= p2;.
1df0: 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 if( p1<0 ){.
1e00: 20 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 p2 += p1;.
1e10: 20 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 p1 = 0;.
1e20: 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 }. }. assert(
1e30: 70 31 3e 3d 30 20 26 26 20 70 32 3e 3d 30 20 29 p1>=0 && p2>=0 )
1e40: 3b 0a 20 20 69 66 28 20 70 30 74 79 70 65 21 3d ;. if( p0type!=
1e50: 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 SQLITE_BLOB ){.
1e60: 20 20 20 77 68 69 6c 65 28 20 2a 7a 20 26 26 20 while( *z &&
1e70: 70 31 20 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 p1 ){. SQLI
1e80: 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b TE_SKIP_UTF8(z);
1e90: 0a 20 20 20 20 20 20 70 31 2d 2d 3b 0a 20 20 20 . p1--;.
1ea0: 20 7d 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b }. for(z2=z;
1eb0: 20 2a 7a 32 20 26 26 20 70 32 3b 20 70 32 2d 2d *z2 && p2; p2--
1ec0: 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f ){. SQLITE_
1ed0: 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20 SKIP_UTF8(z2);.
1ee0: 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1ef0: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
1f00: 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 text, (char*)z,
1f10: 28 69 6e 74 29 28 7a 32 2d 7a 29 2c 20 53 51 4c (int)(z2-z), SQL
1f20: 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a ITE_TRANSIENT);.
1f30: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 }else{. if(
1f40: 20 70 31 2b 70 32 3e 6c 65 6e 20 29 7b 0a 20 20 p1+p2>len ){.
1f50: 20 20 20 20 70 32 20 3d 20 6c 65 6e 2d 70 31 3b p2 = len-p1;
1f60: 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 . if( p2<0
1f70: 29 20 70 32 20 3d 20 30 3b 0a 20 20 20 20 7d 0a ) p2 = 0;. }.
1f80: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
1f90: 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c lt_blob(context,
1fa0: 20 28 63 68 61 72 2a 29 26 7a 5b 70 31 5d 2c 20 (char*)&z[p1],
1fb0: 28 69 6e 74 29 70 32 2c 20 53 51 4c 49 54 45 5f (int)p2, SQLITE_
1fc0: 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a TRANSIENT);. }.
1fd0: 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
1fe0: 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 ntation of the r
1ff0: 6f 75 6e 64 28 29 20 66 75 6e 63 74 69 6f 6e 0a ound() function.
2000: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
2010: 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
2020: 50 4f 49 4e 54 0a 73 74 61 74 69 63 20 76 6f 69 POINT.static voi
2030: 64 20 72 6f 75 6e 64 46 75 6e 63 28 73 71 6c 69 d roundFunc(sqli
2040: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
2050: 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 text, int argc,
2060: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
2070: 61 72 67 76 29 7b 0a 20 20 69 6e 74 20 6e 20 3d argv){. int n =
2080: 20 30 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 0;. double r;.
2090: 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 0a 20 20 char *zBuf;.
20a0: 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 assert( argc==1
20b0: 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 || argc==2 );.
20c0: 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 if( argc==2 ){.
20d0: 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 if( SQLITE_NU
20e0: 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 LL==sqlite3_valu
20f0: 65 5f 74 79 70 65 28 61 72 67 76 5b 31 5d 29 20 e_type(argv[1])
2100: 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6e 20 ) return;. n
2110: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
2120: 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 int(argv[1]);.
2130: 20 20 69 66 28 20 6e 3e 33 30 20 29 20 6e 20 3d if( n>30 ) n =
2140: 20 33 30 3b 0a 20 20 20 20 69 66 28 20 6e 3c 30 30;. if( n<0
2150: 20 29 20 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 ) n = 0;. }.
2160: 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 if( sqlite3_valu
2170: 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d e_type(argv[0])=
2180: 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 =SQLITE_NULL ) r
2190: 65 74 75 72 6e 3b 0a 20 20 72 20 3d 20 73 71 6c eturn;. r = sql
21a0: 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c ite3_value_doubl
21b0: 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a e(argv[0]);. /*
21c0: 20 49 66 20 59 3d 3d 30 20 61 6e 64 20 58 20 77 If Y==0 and X w
21d0: 69 6c 6c 20 66 69 74 20 69 6e 20 61 20 36 34 2d ill fit in a 64-
21e0: 62 69 74 20 69 6e 74 2c 0a 20 20 2a 2a 20 68 61 bit int,. ** ha
21f0: 6e 64 6c 65 20 74 68 65 20 72 6f 75 6e 64 69 6e ndle the roundin
2200: 67 20 64 69 72 65 63 74 6c 79 2c 0a 20 20 2a 2a g directly,. **
2210: 20 6f 74 68 65 72 77 69 73 65 20 75 73 65 20 70 otherwise use p
2220: 72 69 6e 74 66 2e 0a 20 20 2a 2f 0a 20 20 69 66 rintf.. */. if
2230: 28 20 6e 3d 3d 30 20 26 26 20 72 3e 3d 30 20 26 ( n==0 && r>=0 &
2240: 26 20 72 3c 4c 41 52 47 45 53 54 5f 49 4e 54 36 & r<LARGEST_INT6
2250: 34 2d 31 20 29 7b 0a 20 20 20 20 72 20 3d 20 28 4-1 ){. r = (
2260: 64 6f 75 62 6c 65 29 28 28 73 71 6c 69 74 65 5f double)((sqlite_
2270: 69 6e 74 36 34 29 28 72 2b 30 2e 35 29 29 3b 0a int64)(r+0.5));.
2280: 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 30 }else if( n==0
2290: 20 26 26 20 72 3c 30 20 26 26 20 28 2d 72 29 3c && r<0 && (-r)<
22a0: 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 31 20 LARGEST_INT64-1
22b0: 29 7b 0a 20 20 20 20 72 20 3d 20 2d 28 64 6f 75 ){. r = -(dou
22c0: 62 6c 65 29 28 28 73 71 6c 69 74 65 5f 69 6e 74 ble)((sqlite_int
22d0: 36 34 29 28 28 2d 72 29 2b 30 2e 35 29 29 3b 0a 64)((-r)+0.5));.
22e0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 42 75 }else{. zBu
22f0: 66 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 f = sqlite3_mpri
2300: 6e 74 66 28 22 25 2e 2a 66 22 2c 6e 2c 72 29 3b ntf("%.*f",n,r);
2310: 0a 20 20 20 20 69 66 28 20 7a 42 75 66 3d 3d 30 . if( zBuf==0
2320: 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
2330: 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 3_result_error_n
2340: 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 omem(context);.
2350: 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
2360: 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 }. sqlite3At
2370: 6f 46 28 7a 42 75 66 2c 20 26 72 2c 20 73 71 6c oF(zBuf, &r, sql
2380: 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 42 75 ite3Strlen30(zBu
2390: 66 29 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 f), SQLITE_UTF8)
23a0: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
23b0: 65 65 28 7a 42 75 66 29 3b 0a 20 20 7d 0a 20 20 ee(zBuf);. }.
23c0: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 sqlite3_result_d
23d0: 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 72 ouble(context, r
23e0: 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a );.}.#endif../*.
23f0: 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 ** Allocate nByt
2400: 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 e bytes of space
2410: 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 6d using sqlite3_m
2420: 61 6c 6c 6f 63 28 29 2e 20 49 66 20 74 68 65 0a alloc(). If the.
2430: 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ** allocation fa
2440: 69 6c 73 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 ils, call sqlite
2450: 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 3_result_error_n
2460: 6f 6d 65 6d 28 29 20 74 6f 20 6e 6f 74 69 66 79 omem() to notify
2470: 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
2480: 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6d 61 6c handle that mal
2490: 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64 loc() has failed
24a0: 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c and return NULL
24b0: 2e 0a 2a 2a 20 49 66 20 6e 42 79 74 65 20 69 73 ..** If nByte is
24c0: 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
24d0: 20 6d 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 maximum string
24e0: 6f 72 20 62 6c 6f 62 20 6c 65 6e 67 74 68 2c 20 or blob length,
24f0: 74 68 65 6e 0a 2a 2a 20 72 61 69 73 65 20 61 6e then.** raise an
2500: 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 SQLITE_TOOBIG e
2510: 78 63 65 70 74 69 6f 6e 20 61 6e 64 20 72 65 74 xception and ret
2520: 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 urn NULL..*/.sta
2530: 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e 74 65 78 tic void *contex
2540: 74 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f tMalloc(sqlite3_
2550: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
2560: 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 , i64 nByte){.
2570: 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 char *z;. sqlit
2580: 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 e3 *db = sqlite3
2590: 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
25a0: 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 le(context);. a
25b0: 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 ssert( nByte>0 )
25c0: 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 42 ;. testcase( nB
25d0: 79 74 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b yte==db->aLimit[
25e0: 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
25f0: 47 54 48 5d 20 29 3b 0a 20 20 74 65 73 74 63 61 GTH] );. testca
2600: 73 65 28 20 6e 42 79 74 65 3d 3d 64 62 2d 3e 61 se( nByte==db->a
2610: 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
2620: 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a IT_LENGTH]+1 );.
2630: 20 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e if( nByte>db->
2640: 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
2650: 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
2660: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
2670: 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 t_error_toobig(c
2680: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7a 20 3d ontext);. z =
2690: 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
26a0: 20 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c z = sqlite3Mall
26b0: 6f 63 28 28 69 6e 74 29 6e 42 79 74 65 29 3b 0a oc((int)nByte);.
26c0: 20 20 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 if( !z ){.
26d0: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
26e0: 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 lt_error_nomem(c
26f0: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 ontext);. }.
2700: 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d }. return z;.}
2710: 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
2720: 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 75 70 tation of the up
2730: 70 65 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 per() and lower(
2740: 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e ) SQL functions.
2750: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
2760: 75 70 70 65 72 46 75 6e 63 28 73 71 6c 69 74 65 upperFunc(sqlite
2770: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
2780: 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 xt, int argc, sq
2790: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
27a0: 67 76 29 7b 0a 20 20 63 68 61 72 20 2a 7a 31 3b gv){. char *z1;
27b0: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
27c0: 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 2;. int i, n;.
27d0: 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
27e0: 52 28 61 72 67 63 29 3b 0a 20 20 7a 32 20 3d 20 R(argc);. z2 =
27f0: 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 (char*)sqlite3_v
2800: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
2810: 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 ]);. n = sqlite
2820: 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 3_value_bytes(ar
2830: 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 gv[0]);. /* Ver
2840: 69 66 79 20 74 68 61 74 20 74 68 65 20 63 61 6c ify that the cal
2850: 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 20 64 6f l to _bytes() do
2860: 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 es not invalidat
2870: 65 20 74 68 65 20 5f 74 65 78 74 28 29 20 70 6f e the _text() po
2880: 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 inter */. asser
2890: 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 t( z2==(char*)sq
28a0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
28b0: 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 (argv[0]) );. i
28c0: 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 f( z2 ){. z1
28d0: 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 = contextMalloc(
28e0: 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e context, ((i64)n
28f0: 29 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 )+1);. if( z1
2900: 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d ){. for(i=
2910: 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 0; i<n; i++){.
2920: 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 28 63 z1[i] = (c
2930: 68 61 72 29 73 71 6c 69 74 65 33 54 6f 75 70 70 har)sqlite3Toupp
2940: 65 72 28 7a 32 5b 69 5d 29 3b 0a 20 20 20 20 20 er(z2[i]);.
2950: 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
2960: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
2970: 74 65 78 74 2c 20 7a 31 2c 20 6e 2c 20 73 71 6c text, z1, n, sql
2980: 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 ite3_free);.
2990: 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 }. }.}.static v
29a0: 6f 69 64 20 6c 6f 77 65 72 46 75 6e 63 28 73 71 oid lowerFunc(sq
29b0: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
29c0: 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 ontext, int argc
29d0: 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 , sqlite3_value
29e0: 2a 2a 61 72 67 76 29 7b 0a 20 20 63 68 61 72 20 **argv){. char
29f0: 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 *z1;. const cha
2a00: 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 r *z2;. int i,
2a10: 6e 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 n;. UNUSED_PARA
2a20: 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 7a METER(argc);. z
2a30: 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 2 = (char*)sqlit
2a40: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
2a50: 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 gv[0]);. n = sq
2a60: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
2a70: 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a s(argv[0]);. /*
2a80: 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 Verify that the
2a90: 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 call to _bytes(
2aa0: 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c ) does not inval
2ab0: 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 idate the _text(
2ac0: 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 ) pointer */. a
2ad0: 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 ssert( z2==(char
2ae0: 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *)sqlite3_value_
2af0: 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b text(argv[0]) );
2b00: 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 . if( z2 ){.
2b10: 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c z1 = contextMal
2b20: 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 loc(context, ((i
2b30: 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66 64)n)+1);. if
2b40: 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 66 6f ( z1 ){. fo
2b50: 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 r(i=0; i<n; i++)
2b60: 7b 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 {. z1[i]
2b70: 3d 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 = sqlite3Tolower
2b80: 28 7a 32 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d (z2[i]);. }
2b90: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
2ba0: 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
2bb0: 78 74 2c 20 7a 31 2c 20 6e 2c 20 73 71 6c 69 74 xt, z1, n, sqlit
2bc0: 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a e3_free);. }.
2bd0: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 }.}../*.** The
2be0: 20 43 4f 41 4c 45 53 43 45 28 29 20 61 6e 64 20 COALESCE() and
2bf0: 49 46 4e 55 4c 4c 28 29 20 66 75 6e 63 74 69 6f IFNULL() functio
2c00: 6e 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 ns are implement
2c10: 65 64 20 61 73 20 56 44 42 45 20 63 6f 64 65 20 ed as VDBE code
2c20: 73 6f 0a 2a 2a 20 74 68 61 74 20 75 6e 75 73 65 so.** that unuse
2c30: 64 20 61 72 67 75 6d 65 6e 74 20 76 61 6c 75 65 d argument value
2c40: 73 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f s do not have to
2c50: 20 62 65 20 63 6f 6d 70 75 74 65 64 2e 20 20 48 be computed. H
2c60: 6f 77 65 76 65 72 2c 20 77 65 0a 2a 2a 20 73 74 owever, we.** st
2c70: 69 6c 6c 20 6e 65 65 64 20 73 6f 6d 65 20 6b 69 ill need some ki
2c80: 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 69 nd of function i
2c90: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f mplementation fo
2ca0: 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 73 20 r this routines
2cb0: 69 6e 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 in.** the functi
2cc0: 6f 6e 20 74 61 62 6c 65 2e 20 20 54 68 61 74 20 on table. That
2cd0: 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 function impleme
2ce0: 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 6e 65 76 ntation will nev
2cf0: 65 72 20 62 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 er be called.**
2d00: 73 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61 so it doesn't ma
2d10: 74 74 65 72 20 77 68 61 74 20 74 68 65 20 69 6d tter what the im
2d20: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 2e plementation is.
2d30: 20 20 57 65 20 6d 69 67 68 74 20 61 73 20 77 65 We might as we
2d40: 6c 6c 20 75 73 65 0a 2a 2a 20 74 68 65 20 22 76 ll use.** the "v
2d50: 65 72 73 69 6f 6e 28 29 22 20 66 75 6e 63 74 69 ersion()" functi
2d60: 6f 6e 20 61 73 20 61 20 73 75 62 73 74 69 74 75 on as a substitu
2d70: 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 te..*/.#define i
2d80: 66 6e 75 6c 6c 46 75 6e 63 20 76 65 72 73 69 6f fnullFunc versio
2d90: 6e 46 75 6e 63 20 20 20 2f 2a 20 53 75 62 73 74 nFunc /* Subst
2da0: 69 74 75 74 65 20 66 75 6e 63 74 69 6f 6e 20 2d itute function -
2db0: 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 2a 2f never called */
2dc0: 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
2dd0: 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d tation of random
2de0: 28 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61 (). Return a ra
2df0: 6e 64 6f 6d 20 69 6e 74 65 67 65 72 2e 20 20 0a ndom integer. .
2e00: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 */.static void r
2e10: 61 6e 64 6f 6d 46 75 6e 63 28 0a 20 20 73 71 6c andomFunc(. sql
2e20: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
2e30: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 ntext,. int Not
2e40: 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f Used,. sqlite3_
2e50: 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 value **NotUsed2
2e60: 0a 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 .){. sqlite_int
2e70: 36 34 20 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 64 r;. UNUSED_P
2e80: 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 ARAMETER2(NotUse
2e90: 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 d, NotUsed2);.
2ea0: 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
2eb0: 73 73 28 73 69 7a 65 6f 66 28 72 29 2c 20 26 72 ss(sizeof(r), &r
2ec0: 29 3b 0a 20 20 69 66 28 20 72 3c 30 20 29 7b 0a );. if( r<0 ){.
2ed0: 20 20 20 20 2f 2a 20 57 65 20 6e 65 65 64 20 74 /* We need t
2ee0: 6f 20 70 72 65 76 65 6e 74 20 61 20 72 61 6e 64 o prevent a rand
2ef0: 6f 6d 20 6e 75 6d 62 65 72 20 6f 66 20 30 78 38 om number of 0x8
2f00: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 20 000000000000000
2f10: 0a 20 20 20 20 2a 2a 20 28 6f 72 20 2d 39 32 32 . ** (or -922
2f20: 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38 3372036854775808
2f30: 29 20 73 69 6e 63 65 20 77 68 65 6e 20 79 6f 75 ) since when you
2f40: 20 64 6f 20 61 62 73 28 29 20 6f 66 20 74 68 61 do abs() of tha
2f50: 74 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 t. ** number
2f60: 6f 66 20 79 6f 75 20 67 65 74 20 74 68 65 20 73 of you get the s
2f70: 61 6d 65 20 76 61 6c 75 65 20 62 61 63 6b 20 61 ame value back a
2f80: 67 61 69 6e 2e 20 20 54 6f 20 64 6f 20 74 68 69 gain. To do thi
2f90: 73 0a 20 20 20 20 2a 2a 20 69 6e 20 61 20 77 61 s. ** in a wa
2fa0: 79 20 74 68 61 74 20 69 73 20 74 65 73 74 61 62 y that is testab
2fb0: 6c 65 2c 20 6d 61 73 6b 20 74 68 65 20 73 69 67 le, mask the sig
2fc0: 6e 20 62 69 74 20 6f 66 66 20 6f 66 20 6e 65 67 n bit off of neg
2fd0: 61 74 69 76 65 0a 20 20 20 20 2a 2a 20 76 61 6c ative. ** val
2fe0: 75 65 73 2c 20 72 65 73 75 6c 74 69 6e 67 20 69 ues, resulting i
2ff0: 6e 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c n a positive val
3000: 75 65 2e 20 20 54 68 65 6e 20 74 61 6b 65 20 74 ue. Then take t
3010: 68 65 20 0a 20 20 20 20 2a 2a 20 32 73 20 63 6f he . ** 2s co
3020: 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 61 74 mplement of that
3030: 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 2e positive value.
3040: 20 20 54 68 65 20 65 6e 64 20 72 65 73 75 6c 74 The end result
3050: 20 63 61 6e 0a 20 20 20 20 2a 2a 20 74 68 65 72 can. ** ther
3060: 65 66 6f 72 65 20 62 65 20 6e 6f 20 6c 65 73 73 efore be no less
3070: 20 74 68 61 6e 20 2d 39 32 32 33 33 37 32 30 33 than -922337203
3080: 36 38 35 34 37 37 35 38 30 37 2e 0a 20 20 20 20 6854775807..
3090: 2a 2f 0a 20 20 20 20 72 20 3d 20 2d 28 72 20 26 */. r = -(r &
30a0: 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 3b LARGEST_INT64);
30b0: 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 . }. sqlite3_r
30c0: 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 esult_int64(cont
30d0: 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ext, r);.}../*.*
30e0: 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
30f0: 20 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 4e of randomblob(N
3100: 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61 6e ). Return a ran
3110: 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68 61 74 dom blob.** that
3120: 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 is N bytes long
3130: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
3140: 20 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20 20 73 randomBlob(. s
3150: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
3160: 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
3170: 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
3180: 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
3190: 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e int n;. unsign
31a0: 65 64 20 63 68 61 72 20 2a 70 3b 0a 20 20 61 73 ed char *p;. as
31b0: 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b sert( argc==1 );
31c0: 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
31d0: 54 45 52 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d TER(argc);. n =
31e0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 sqlite3_value_i
31f0: 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 nt(argv[0]);. i
3200: 66 28 20 6e 3c 31 20 29 7b 0a 20 20 20 20 6e 20 f( n<1 ){. n
3210: 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 63 = 1;. }. p = c
3220: 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e ontextMalloc(con
3230: 74 65 78 74 2c 20 6e 29 3b 0a 20 20 69 66 28 20 text, n);. if(
3240: 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 p ){. sqlite3
3250: 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 6e 2c 20 70 _randomness(n, p
3260: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 );. sqlite3_r
3270: 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 esult_blob(conte
3280: 78 74 2c 20 28 63 68 61 72 2a 29 70 2c 20 6e 2c xt, (char*)p, n,
3290: 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a sqlite3_free);.
32a0: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 }.}../*.** Imp
32b0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
32c0: 68 65 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 he last_insert_r
32d0: 6f 77 69 64 28 29 20 53 51 4c 20 66 75 6e 63 74 owid() SQL funct
32e0: 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e ion. The return
32f0: 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 74 68 65 .** value is the
3300: 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c same as the sql
3310: 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 ite3_last_insert
3320: 5f 72 6f 77 69 64 28 29 20 41 50 49 20 66 75 6e _rowid() API fun
3330: 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
3340: 20 76 6f 69 64 20 6c 61 73 74 5f 69 6e 73 65 72 void last_inser
3350: 74 5f 72 6f 77 69 64 28 0a 20 20 73 71 6c 69 74 t_rowid(. sqlit
3360: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
3370: 65 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55 ext, . int NotU
3380: 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f sed, . sqlite3_
3390: 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 value **NotUsed2
33a0: 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 .){. sqlite3 *d
33b0: 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 b = sqlite3_cont
33c0: 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f ext_db_handle(co
33d0: 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 ntext);. UNUSED
33e0: 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
33f0: 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
3400: 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 35 31 35 31 /* IMP: R-5151
3410: 33 2d 31 32 30 32 36 20 54 68 65 20 6c 61 73 74 3-12026 The last
3420: 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 _insert_rowid()
3430: 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 SQL function is
3440: 61 0a 20 20 2a 2a 20 77 72 61 70 70 65 72 20 61 a. ** wrapper a
3450: 72 6f 75 6e 64 20 74 68 65 20 73 71 6c 69 74 65 round the sqlite
3460: 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 3_last_insert_ro
3470: 77 69 64 28 29 20 43 2f 43 2b 2b 20 69 6e 74 65 wid() C/C++ inte
3480: 72 66 61 63 65 0a 20 20 2a 2a 20 66 75 6e 63 74 rface. ** funct
3490: 69 6f 6e 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ion. */. sqlite
34a0: 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 3_result_int64(c
34b0: 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f ontext, sqlite3_
34c0: 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 last_insert_rowi
34d0: 64 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a d(db));.}../*.**
34e0: 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
34f0: 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 28 29 of the changes()
3500: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a SQL function..*
3510: 2a 0a 2a 2a 20 49 4d 50 3a 20 52 2d 36 32 30 37 *.** IMP: R-6207
3520: 33 2d 31 31 32 30 39 20 54 68 65 20 63 68 61 6e 3-11209 The chan
3530: 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 ges() SQL functi
3540: 6f 6e 20 69 73 20 61 20 77 72 61 70 70 65 72 0a on is a wrapper.
3550: 2a 2a 20 61 72 6f 75 6e 64 20 74 68 65 20 73 71 ** around the sq
3560: 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 lite3_changes()
3570: 43 2f 43 2b 2b 20 66 75 6e 63 74 69 6f 6e 20 61 C/C++ function a
3580: 6e 64 20 68 65 6e 63 65 20 66 6f 6c 6c 6f 77 73 nd hence follows
3590: 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 72 75 6c the same.** rul
35a0: 65 73 20 66 6f 72 20 63 6f 75 6e 74 69 6e 67 20 es for counting
35b0: 63 68 61 6e 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 changes..*/.stat
35c0: 69 63 20 76 6f 69 64 20 63 68 61 6e 67 65 73 28 ic void changes(
35d0: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
35e0: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
35f0: 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 nt NotUsed,. sq
3600: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f lite3_value **No
3610: 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 tUsed2.){. sqli
3620: 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 te3 *db = sqlite
3630: 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
3640: 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
3650: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
3660: 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
3670: 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f ed2);. sqlite3_
3680: 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 result_int(conte
3690: 78 74 2c 20 73 71 6c 69 74 65 33 5f 63 68 61 6e xt, sqlite3_chan
36a0: 67 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a ges(db));.}../*.
36b0: 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
36c0: 6e 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 5f 63 n of the total_c
36d0: 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e hanges() SQL fun
36e0: 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 ction. The retu
36f0: 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 rn value is.** t
3700: 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 he same as the s
3710: 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 qlite3_total_cha
3720: 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e 63 74 nges() API funct
3730: 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ion..*/.static v
3740: 6f 69 64 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 oid total_change
3750: 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e s(. sqlite3_con
3760: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
3770: 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 int NotUsed,.
3780: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
3790: 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 NotUsed2.){. sq
37a0: 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
37b0: 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
37c0: 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
37d0: 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
37e0: 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 ER2(NotUsed, Not
37f0: 55 73 65 64 32 29 3b 0a 20 20 2f 2a 20 49 4d 50 Used2);. /* IMP
3800: 3a 20 52 2d 35 32 37 35 36 2d 34 31 39 39 33 20 : R-52756-41993
3810: 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
3820: 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e a wrapper aroun
3830: 64 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 d the. ** sqlit
3840: 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
3850: 28 29 20 43 2f 43 2b 2b 20 69 6e 74 65 72 66 61 () C/C++ interfa
3860: 63 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ce. */. sqlite3
3870: 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 _result_int(cont
3880: 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 74 6f 74 ext, sqlite3_tot
3890: 61 6c 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b al_changes(db));
38a0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 .}../*.** A stru
38b0: 63 74 75 72 65 20 64 65 66 69 6e 69 6e 67 20 68 cture defining h
38c0: 6f 77 20 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74 ow to do GLOB-st
38d0: 79 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e yle comparisons.
38e0: 0a 2a 2f 0a 73 74 72 75 63 74 20 63 6f 6d 70 61 .*/.struct compa
38f0: 72 65 49 6e 66 6f 20 7b 0a 20 20 75 38 20 6d 61 reInfo {. u8 ma
3900: 74 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 tchAll;. u8 mat
3910: 63 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 chOne;. u8 matc
3920: 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 hSet;. u8 noCas
3930: 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 e;.};../*.** For
3940: 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 6d LIKE and GLOB m
3950: 61 74 63 68 69 6e 67 20 6f 6e 20 45 42 43 44 49 atching on EBCDI
3960: 43 20 6d 61 63 68 69 6e 65 73 2c 20 61 73 73 75 C machines, assu
3970: 6d 65 20 74 68 61 74 20 65 76 65 72 79 0a 2a 2a me that every.**
3980: 20 63 68 61 72 61 63 74 65 72 20 69 73 20 65 78 character is ex
3990: 61 63 74 6c 79 20 6f 6e 65 20 62 79 74 65 20 69 actly one byte i
39a0: 6e 20 73 69 7a 65 2e 20 20 41 6c 73 6f 2c 20 61 n size. Also, a
39b0: 6c 6c 20 63 68 61 72 61 63 74 65 72 73 20 61 72 ll characters ar
39c0: 65 0a 2a 2a 20 61 62 6c 65 20 74 6f 20 70 61 72 e.** able to par
39d0: 74 69 63 69 70 61 74 65 20 69 6e 20 75 70 70 65 ticipate in uppe
39e0: 72 2d 63 61 73 65 2d 74 6f 2d 6c 6f 77 65 72 2d r-case-to-lower-
39f0: 63 61 73 65 20 6d 61 70 70 69 6e 67 73 20 69 6e case mappings in
3a00: 20 45 42 43 44 49 43 0a 2a 2a 20 77 68 65 72 65 EBCDIC.** where
3a10: 61 73 20 6f 6e 6c 79 20 63 68 61 72 61 63 74 65 as only characte
3a20: 72 73 20 6c 65 73 73 20 74 68 61 6e 20 30 78 38 rs less than 0x8
3a30: 30 20 64 6f 20 69 6e 20 41 53 43 49 49 2e 0a 2a 0 do in ASCII..*
3a40: 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 /.#if defined(SQ
3a50: 4c 49 54 45 5f 45 42 43 44 49 43 29 0a 23 20 64 LITE_EBCDIC).# d
3a60: 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55 74 66 efine sqlite3Utf
3a70: 38 52 65 61 64 28 41 29 20 20 20 20 28 2a 28 28 8Read(A) (*((
3a80: 2a 41 29 2b 2b 29 29 0a 23 20 64 65 66 69 6e 65 *A)++)).# define
3a90: 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 GlogUpperToLowe
3aa0: 72 28 41 29 20 20 20 41 20 3d 20 73 71 6c 69 74 r(A) A = sqlit
3ab0: 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41 e3UpperToLower[A
3ac0: 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ].#else.# define
3ad0: 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 GlogUpperToLowe
3ae0: 72 28 41 29 20 20 20 69 66 28 20 21 28 28 41 29 r(A) if( !((A)
3af0: 26 7e 30 78 37 66 29 20 29 7b 20 41 20 3d 20 73 &~0x7f) ){ A = s
3b00: 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 qlite3UpperToLow
3b10: 65 72 5b 41 5d 3b 20 7d 0a 23 65 6e 64 69 66 0a er[A]; }.#endif.
3b20: 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 .static const st
3b30: 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f ruct compareInfo
3b40: 20 67 6c 6f 62 49 6e 66 6f 20 3d 20 7b 20 27 2a globInfo = { '*
3b50: 27 2c 20 27 3f 27 2c 20 27 5b 27 2c 20 30 20 7d ', '?', '[', 0 }
3b60: 3b 0a 2f 2a 20 54 68 65 20 63 6f 72 72 65 63 74 ;./* The correct
3b70: 20 53 51 4c 2d 39 32 20 62 65 68 61 76 69 6f 72 SQL-92 behavior
3b80: 20 69 73 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 is for the LIKE
3b90: 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 69 67 6e operator to ign
3ba0: 6f 72 65 0a 2a 2a 20 63 61 73 65 2e 20 20 54 68 ore.** case. Th
3bb0: 75 73 20 20 27 61 27 20 4c 49 4b 45 20 27 41 27 us 'a' LIKE 'A'
3bc0: 20 77 6f 75 6c 64 20 62 65 20 74 72 75 65 2e 20 would be true.
3bd0: 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
3be0: 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e struct compareIn
3bf0: 66 6f 20 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 20 fo likeInfoNorm
3c00: 3d 20 7b 20 27 25 27 2c 20 27 5f 27 2c 20 20 20 = { '%', '_',
3c10: 30 2c 20 31 20 7d 3b 0a 2f 2a 20 49 66 20 53 51 0, 1 };./* If SQ
3c20: 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e 53 49 54 LITE_CASE_SENSIT
3c30: 49 56 45 5f 4c 49 4b 45 20 69 73 20 64 65 66 69 IVE_LIKE is defi
3c40: 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 4c 49 ned, then the LI
3c50: 4b 45 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 69 KE operator.** i
3c60: 73 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 s case sensitive
3c70: 20 63 61 75 73 69 6e 67 20 27 61 27 20 4c 49 4b causing 'a' LIK
3c80: 45 20 27 41 27 20 74 6f 20 62 65 20 66 61 6c 73 E 'A' to be fals
3c90: 65 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 e */.static cons
3ca0: 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 t struct compare
3cb0: 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f 41 6c 74 Info likeInfoAlt
3cc0: 20 3d 20 7b 20 27 25 27 2c 20 27 5f 27 2c 20 20 = { '%', '_',
3cd0: 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 0, 0 };../*.**
3ce0: 43 6f 6d 70 61 72 65 20 74 77 6f 20 55 54 46 2d Compare two UTF-
3cf0: 38 20 73 74 72 69 6e 67 73 20 66 6f 72 20 65 71 8 strings for eq
3d00: 75 61 6c 69 74 79 20 77 68 65 72 65 20 74 68 65 uality where the
3d10: 20 66 69 72 73 74 20 73 74 72 69 6e 67 20 63 61 first string ca
3d20: 6e 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c 6c 79 n.** potentially
3d30: 20 62 65 20 61 20 22 67 6c 6f 62 22 20 65 78 70 be a "glob" exp
3d40: 72 65 73 73 69 6f 6e 2e 20 20 52 65 74 75 72 6e ression. Return
3d50: 20 74 72 75 65 20 28 31 29 20 69 66 20 74 68 65 true (1) if the
3d60: 79 0a 2a 2a 20 61 72 65 20 74 68 65 20 73 61 6d y.** are the sam
3d70: 65 20 61 6e 64 20 66 61 6c 73 65 20 28 30 29 20 e and false (0)
3d80: 69 66 20 74 68 65 79 20 61 72 65 20 64 69 66 66 if they are diff
3d90: 65 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 47 6c 6f erent..**.** Glo
3da0: 62 62 69 6e 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a bbing rules:.**.
3db0: 2a 2a 20 20 20 20 20 20 27 2a 27 20 20 20 20 20 ** '*'
3dc0: 20 20 4d 61 74 63 68 65 73 20 61 6e 79 20 73 65 Matches any se
3dd0: 71 75 65 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f quence of zero o
3de0: 72 20 6d 6f 72 65 20 63 68 61 72 61 63 74 65 72 r more character
3df0: 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 3f s..**.** '?
3e00: 27 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 ' Matches
3e10: 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 68 61 72 exactly one char
3e20: 61 63 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 acter..**.**
3e30: 20 5b 2e 2e 2e 5d 20 20 20 20 20 20 4d 61 74 63 [...] Matc
3e40: 68 65 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 hes one characte
3e50: 72 20 66 72 6f 6d 20 74 68 65 20 65 6e 63 6c 6f r from the enclo
3e60: 73 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 sed list of.**
3e70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 68 ch
3e80: 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 aracters..**.**
3e90: 20 20 20 20 5b 5e 2e 2e 2e 5d 20 20 20 20 20 4d [^...] M
3ea0: 61 74 63 68 65 73 20 6f 6e 65 20 63 68 61 72 61 atches one chara
3eb0: 63 74 65 72 20 6e 6f 74 20 69 6e 20 74 68 65 20 cter not in the
3ec0: 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74 2e 0a 2a enclosed list..*
3ed0: 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 5b 2e *.** With the [.
3ee0: 2e 2e 5d 20 61 6e 64 20 5b 5e 2e 2e 2e 5d 20 6d ..] and [^...] m
3ef0: 61 74 63 68 69 6e 67 2c 20 61 20 27 5d 27 20 63 atching, a ']' c
3f00: 68 61 72 61 63 74 65 72 20 63 61 6e 20 62 65 20 haracter can be
3f10: 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e 20 74 included.** in t
3f20: 68 65 20 6c 69 73 74 20 62 79 20 6d 61 6b 69 6e he list by makin
3f30: 67 20 69 74 20 74 68 65 20 66 69 72 73 74 20 63 g it the first c
3f40: 68 61 72 61 63 74 65 72 20 61 66 74 65 72 20 27 haracter after '
3f50: 5b 27 20 6f 72 20 27 5e 27 2e 20 20 41 0a 2a 2a [' or '^'. A.**
3f60: 20 72 61 6e 67 65 20 6f 66 20 63 68 61 72 61 63 range of charac
3f70: 74 65 72 73 20 63 61 6e 20 62 65 20 73 70 65 63 ters can be spec
3f80: 69 66 69 65 64 20 75 73 69 6e 67 20 27 2d 27 2e ified using '-'.
3f90: 20 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 22 5b Example:.** "[
3fa0: 61 2d 7a 5d 22 20 6d 61 74 63 68 65 73 20 61 6e a-z]" matches an
3fb0: 79 20 73 69 6e 67 6c 65 20 6c 6f 77 65 72 2d 63 y single lower-c
3fc0: 61 73 65 20 6c 65 74 74 65 72 2e 20 20 54 6f 20 ase letter. To
3fd0: 6d 61 74 63 68 20 61 20 27 2d 27 2c 20 6d 61 6b match a '-', mak
3fe0: 65 0a 2a 2a 20 69 74 20 74 68 65 20 6c 61 73 74 e.** it the last
3ff0: 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 character in th
4000: 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 e list..**.** Th
4010: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
4020: 75 61 6c 6c 79 20 71 75 69 63 6b 2c 20 62 75 74 ually quick, but
4030: 20 63 61 6e 20 62 65 20 4e 2a 2a 32 20 69 6e 20 can be N**2 in
4040: 74 68 65 20 77 6f 72 73 74 20 63 61 73 65 2e 0a the worst case..
4050: 2a 2a 0a 2a 2a 20 48 69 6e 74 73 3a 20 74 6f 20 **.** Hints: to
4060: 6d 61 74 63 68 20 27 2a 27 20 6f 72 20 27 3f 27 match '*' or '?'
4070: 2c 20 70 75 74 20 74 68 65 6d 20 69 6e 20 22 5b , put them in "[
4080: 5d 22 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a ]". Like this:.
4090: 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 62 **.** ab
40a0: 63 5b 2a 5d 78 79 7a 20 20 20 20 20 20 20 20 4d c[*]xyz M
40b0: 61 74 63 68 65 73 20 22 61 62 63 2a 78 79 7a 22 atches "abc*xyz"
40c0: 20 6f 6e 6c 79 0a 2a 2f 0a 73 74 61 74 69 63 20 only.*/.static
40d0: 69 6e 74 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 int patternCompa
40e0: 72 65 28 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a re(. const u8 *
40f0: 7a 50 61 74 74 65 72 6e 2c 20 20 20 20 20 20 20 zPattern,
4100: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 67 6c /* The gl
4110: 6f 62 20 70 61 74 74 65 72 6e 20 2a 2f 0a 20 20 ob pattern */.
4120: 63 6f 6e 73 74 20 75 38 20 2a 7a 53 74 72 69 6e const u8 *zStrin
4130: 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g,
4140: 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 20 74 /* The string t
4150: 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 o compare agains
4160: 74 20 74 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 t the glob */.
4170: 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d const struct com
4180: 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 2c pareInfo *pInfo,
4190: 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
41a0: 61 62 6f 75 74 20 68 6f 77 20 74 6f 20 64 6f 20 about how to do
41b0: 74 68 65 20 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 the compare */.
41c0: 20 75 33 32 20 65 73 63 20 20 20 20 20 20 20 20 u32 esc
41d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
41e0: 20 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 /* The escape
41f0: 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 29 7b 0a character */.){.
4200: 20 20 75 33 32 20 63 2c 20 63 32 3b 0a 20 20 69 u32 c, c2;. i
4210: 6e 74 20 69 6e 76 65 72 74 3b 0a 20 20 69 6e 74 nt invert;. int
4220: 20 73 65 65 6e 3b 0a 20 20 75 38 20 6d 61 74 63 seen;. u8 matc
4230: 68 4f 6e 65 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 hOne = pInfo->ma
4240: 74 63 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61 74 tchOne;. u8 mat
4250: 63 68 41 6c 6c 20 3d 20 70 49 6e 66 6f 2d 3e 6d chAll = pInfo->m
4260: 61 74 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 atchAll;. u8 ma
4270: 74 63 68 53 65 74 20 3d 20 70 49 6e 66 6f 2d 3e tchSet = pInfo->
4280: 6d 61 74 63 68 53 65 74 3b 0a 20 20 75 38 20 6e matchSet;. u8 n
4290: 6f 43 61 73 65 20 3d 20 70 49 6e 66 6f 2d 3e 6e oCase = pInfo->n
42a0: 6f 43 61 73 65 3b 20 0a 20 20 69 6e 74 20 70 72 oCase; . int pr
42b0: 65 76 45 73 63 61 70 65 20 3d 20 30 3b 20 20 20 evEscape = 0;
42c0: 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
42d0: 20 70 72 65 76 69 6f 75 73 20 63 68 61 72 61 63 previous charac
42e0: 74 65 72 20 77 61 73 20 27 65 73 63 61 70 65 27 ter was 'escape'
42f0: 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 28 63 */.. while( (c
4300: 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 = sqlite3Utf8Re
4310: 61 64 28 26 7a 50 61 74 74 65 72 6e 29 29 21 3d ad(&zPattern))!=
4320: 30 20 29 7b 0a 20 20 20 20 69 66 28 20 63 3d 3d 0 ){. if( c==
4330: 6d 61 74 63 68 41 6c 6c 20 26 26 20 21 70 72 65 matchAll && !pre
4340: 76 45 73 63 61 70 65 20 29 7b 0a 20 20 20 20 20 vEscape ){.
4350: 20 77 68 69 6c 65 28 20 28 63 3d 73 71 6c 69 74 while( (c=sqlit
4360: 65 33 55 74 66 38 52 65 61 64 28 26 7a 50 61 74 e3Utf8Read(&zPat
4370: 74 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63 68 41 tern)) == matchA
4380: 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ll.
4390: 20 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63 68 4f || c == matchO
43a0: 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 ne ){. if
43b0: 28 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 26 26 ( c==matchOne &&
43c0: 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
43d0: 28 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b (&zString)==0 ){
43e0: 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
43f0: 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 n 0;. }.
4400: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
4410: 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 c==0 ){.
4420: 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
4430: 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 65 73 }else if( c==es
4440: 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 20 3d c ){. c =
4450: 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
4460: 28 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 (&zPattern);.
4470: 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b if( c==0 ){
4480: 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
4490: 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 n 0;. }.
44a0: 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 }else if( c
44b0: 3d 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a 20 20 ==matchSet ){.
44c0: 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 73 assert( es
44d0: 63 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 c==0 );
44e0: 2f 2a 20 54 68 69 73 20 69 73 20 47 4c 4f 42 2c /* This is GLOB,
44f0: 20 6e 6f 74 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 not LIKE */.
4500: 20 20 20 20 20 61 73 73 65 72 74 28 20 6d 61 74 assert( mat
4510: 63 68 53 65 74 3c 30 78 38 30 20 29 3b 20 20 2f chSet<0x80 ); /
4520: 2a 20 27 5b 27 20 69 73 20 61 20 73 69 6e 67 6c * '[' is a singl
4530: 65 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 e-byte character
4540: 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c */. whil
4550: 65 28 20 2a 7a 53 74 72 69 6e 67 20 26 26 20 70 e( *zString && p
4560: 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 26 7a atternCompare(&z
4570: 50 61 74 74 65 72 6e 5b 2d 31 5d 2c 7a 53 74 72 Pattern[-1],zStr
4580: 69 6e 67 2c 70 49 6e 66 6f 2c 65 73 63 29 3d 3d ing,pInfo,esc)==
4590: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 53 0 ){. S
45a0: 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 QLITE_SKIP_UTF8(
45b0: 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 zString);.
45c0: 20 20 7d 0a 20 20 20 20 20 20 20 20 72 65 74 75 }. retu
45d0: 72 6e 20 2a 7a 53 74 72 69 6e 67 21 3d 30 3b 0a rn *zString!=0;.
45e0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 }. wh
45f0: 69 6c 65 28 20 28 63 32 20 3d 20 73 71 6c 69 74 ile( (c2 = sqlit
4600: 65 33 55 74 66 38 52 65 61 64 28 26 7a 53 74 72 e3Utf8Read(&zStr
4610: 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 ing))!=0 ){.
4620: 20 20 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29 if( noCase )
4630: 7b 0a 20 20 20 20 20 20 20 20 20 20 47 6c 6f 67 {. Glog
4640: 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29 UpperToLower(c2)
4650: 3b 0a 20 20 20 20 20 20 20 20 20 20 47 6c 6f 67 ;. Glog
4660: 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 29 3b UpperToLower(c);
4670: 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
4680: 28 20 63 32 20 21 3d 20 30 20 26 26 20 63 32 20 ( c2 != 0 && c2
4690: 21 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 != c ){.
46a0: 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 c2 = sqlite3
46b0: 55 74 66 38 52 65 61 64 28 26 7a 53 74 72 69 6e Utf8Read(&zStrin
46c0: 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 g);.
46d0: 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 GlogUpperToLower
46e0: 28 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (c2);.
46f0: 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
4700: 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
4710: 28 20 63 32 20 21 3d 20 30 20 26 26 20 63 32 20 ( c2 != 0 && c2
4720: 21 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 != c ){.
4730: 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 c2 = sqlite3
4740: 55 74 66 38 52 65 61 64 28 26 7a 53 74 72 69 6e Utf8Read(&zStrin
4750: 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a g);. }.
4760: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
4770: 20 20 69 66 28 20 63 32 3d 3d 30 20 29 20 72 65 if( c2==0 ) re
4780: 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 turn 0;.
4790: 69 66 28 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 if( patternCompa
47a0: 72 65 28 7a 50 61 74 74 65 72 6e 2c 7a 53 74 72 re(zPattern,zStr
47b0: 69 6e 67 2c 70 49 6e 66 6f 2c 65 73 63 29 20 29 ing,pInfo,esc) )
47c0: 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
47d0: 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 }. return
47e0: 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0;. }else if(
47f0: 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 26 26 20 c==matchOne &&
4800: 21 70 72 65 76 45 73 63 61 70 65 20 29 7b 0a 20 !prevEscape ){.
4810: 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
4820: 55 74 66 38 52 65 61 64 28 26 7a 53 74 72 69 6e Utf8Read(&zStrin
4830: 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 g)==0 ){.
4840: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 return 0;.
4850: 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
4860: 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a c==matchSet ){.
4870: 20 20 20 20 20 20 75 33 32 20 70 72 69 6f 72 5f u32 prior_
4880: 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 c = 0;. ass
4890: 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b 20 20 ert( esc==0 );
48a0: 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 6f /* This only o
48b0: 63 63 75 72 73 20 66 6f 72 20 47 4c 4f 42 2c 20 ccurs for GLOB,
48c0: 6e 6f 74 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 not LIKE */.
48d0: 20 20 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20 seen = 0;.
48e0: 20 20 69 6e 76 65 72 74 20 3d 20 30 3b 0a 20 20 invert = 0;.
48f0: 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 c = sqlite3U
4900: 74 66 38 52 65 61 64 28 26 7a 53 74 72 69 6e 67 tf8Read(&zString
4910: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d );. if( c==
4920: 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
4930: 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 c2 = sqlite3
4940: 55 74 66 38 52 65 61 64 28 26 7a 50 61 74 74 65 Utf8Read(&zPatte
4950: 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 rn);. if( c
4960: 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20 20 20 2=='^' ){.
4970: 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a 20 20 invert = 1;.
4980: 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 c2 = sqlit
4990: 65 33 55 74 66 38 52 65 61 64 28 26 7a 50 61 74 e3Utf8Read(&zPat
49a0: 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 tern);. }.
49b0: 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5d 27 if( c2==']'
49c0: 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
49d0: 63 3d 3d 27 5d 27 20 29 20 73 65 65 6e 20 3d 20 c==']' ) seen =
49e0: 31 3b 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20 1;. c2 =
49f0: 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
4a00: 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 &zPattern);.
4a10: 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 }. while(
4a20: 20 63 32 20 26 26 20 63 32 21 3d 27 5d 27 20 29 c2 && c2!=']' )
4a30: 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 32 {. if( c2
4a40: 3d 3d 27 2d 27 20 26 26 20 7a 50 61 74 74 65 72 =='-' && zPatter
4a50: 6e 5b 30 5d 21 3d 27 5d 27 20 26 26 20 7a 50 61 n[0]!=']' && zPa
4a60: 74 74 65 72 6e 5b 30 5d 21 3d 30 20 26 26 20 70 ttern[0]!=0 && p
4a70: 72 69 6f 72 5f 63 3e 30 20 29 7b 0a 20 20 20 20 rior_c>0 ){.
4a80: 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 c2 = sqlit
4a90: 65 33 55 74 66 38 52 65 61 64 28 26 7a 50 61 74 e3Utf8Read(&zPat
4aa0: 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 tern);.
4ab0: 20 69 66 28 20 63 3e 3d 70 72 69 6f 72 5f 63 20 if( c>=prior_c
4ac0: 26 26 20 63 3c 3d 63 32 20 29 20 73 65 65 6e 20 && c<=c2 ) seen
4ad0: 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 = 1;. p
4ae0: 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20 20 rior_c = 0;.
4af0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
4b00: 20 20 20 20 20 69 66 28 20 63 3d 3d 63 32 20 29 if( c==c2 )
4b10: 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 65 {. se
4b20: 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 en = 1;.
4b30: 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 72 }. pr
4b40: 69 6f 72 5f 63 20 3d 20 63 32 3b 0a 20 20 20 20 ior_c = c2;.
4b50: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 32 }. c2
4b60: 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 = sqlite3Utf8Re
4b70: 61 64 28 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 ad(&zPattern);.
4b80: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
4b90: 20 63 32 3d 3d 30 20 7c 7c 20 28 73 65 65 6e 20 c2==0 || (seen
4ba0: 5e 20 69 6e 76 65 72 74 29 3d 3d 30 20 29 7b 0a ^ invert)==0 ){.
4bb0: 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 return 0
4bc0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
4bd0: 6c 73 65 20 69 66 28 20 65 73 63 3d 3d 63 20 26 lse if( esc==c &
4be0: 26 20 21 70 72 65 76 45 73 63 61 70 65 20 29 7b & !prevEscape ){
4bf0: 0a 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70 . prevEscap
4c00: 65 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 e = 1;. }else
4c10: 7b 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c {. c2 = sql
4c20: 69 74 65 33 55 74 66 38 52 65 61 64 28 26 7a 53 ite3Utf8Read(&zS
4c30: 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 tring);. if
4c40: 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20 ( noCase ){.
4c50: 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c GlogUpperToL
4c60: 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20 20 20 ower(c);.
4c70: 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 GlogUpperToLowe
4c80: 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 r(c2);. }.
4c90: 20 20 20 20 20 69 66 28 20 63 21 3d 63 32 20 29 if( c!=c2 )
4ca0: 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
4cb0: 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
4cc0: 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 prevEscape = 0
4cd0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
4ce0: 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d 30 turn *zString==0
4cf0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
4d00: 71 6c 69 74 65 33 5f 73 74 72 67 6c 6f 62 28 29 qlite3_strglob()
4d10: 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 69 interface..*/.i
4d20: 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 72 67 6c nt sqlite3_strgl
4d30: 6f 62 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a ob(const char *z
4d40: 47 6c 6f 62 50 61 74 74 65 72 6e 2c 20 63 6f 6e GlobPattern, con
4d50: 73 74 20 63 68 61 72 20 2a 7a 53 74 72 69 6e 67 st char *zString
4d60: 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 61 74 74 ){. return patt
4d70: 65 72 6e 43 6f 6d 70 61 72 65 28 28 75 38 2a 29 ernCompare((u8*)
4d80: 7a 47 6c 6f 62 50 61 74 74 65 72 6e 2c 20 28 75 zGlobPattern, (u
4d90: 38 2a 29 7a 53 74 72 69 6e 67 2c 20 26 67 6c 6f 8*)zString, &glo
4da0: 62 49 6e 66 6f 2c 20 30 29 3d 3d 30 3b 0a 7d 0a bInfo, 0)==0;.}.
4db0: 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 ./*.** Count the
4dc0: 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
4dd0: 20 74 68 61 74 20 74 68 65 20 4c 49 4b 45 20 6f that the LIKE o
4de0: 70 65 72 61 74 6f 72 20 28 6f 72 20 47 4c 4f 42 perator (or GLOB
4df0: 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 6a 75 73 which is.** jus
4e00: 74 20 61 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 t a variation of
4e10: 20 4c 49 4b 45 29 20 67 65 74 73 20 63 61 6c 6c LIKE) gets call
4e20: 65 64 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 ed. This is use
4e30: 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a d for testing.**
4e40: 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 only..*/.#ifdef
4e50: 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 SQLITE_TEST.int
4e60: 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f sqlite3_like_co
4e70: 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a unt = 0;.#endif.
4e80: 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
4e90: 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 tation of the li
4ea0: 6b 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f ke() SQL functio
4eb0: 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f n. This functio
4ec0: 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 n implements.**
4ed0: 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b the build-in LIK
4ee0: 45 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 E operator. The
4ef0: 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
4f00: 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 to the function
4f10: 69 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 is the.** patter
4f20: 6e 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 n and the second
4f30: 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
4f40: 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 string. So, th
4f50: 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 e SQL statements
4f60: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 :.**.** A
4f70: 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 LIKE B.**.** is
4f80: 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c implemented as l
4f90: 69 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a 20 ike(B,A)..**.**
4fa0: 54 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74 69 This same functi
4fb0: 6f 6e 20 28 77 69 74 68 20 61 20 64 69 66 66 65 on (with a diffe
4fc0: 72 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f rent compareInfo
4fd0: 20 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d 70 structure) comp
4fe0: 75 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f 42 utes.** the GLOB
4ff0: 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 operator..*/.st
5000: 61 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46 75 atic void likeFu
5010: 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
5020: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 ntext *context,
5030: 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 . int argc, .
5040: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
5050: 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 argv.){. const
5060: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
5070: 41 2c 20 2a 7a 42 3b 0a 20 20 75 33 32 20 65 73 A, *zB;. u32 es
5080: 63 61 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 cape = 0;. int
5090: 6e 50 61 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 nPat;. sqlite3
50a0: 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f *db = sqlite3_co
50b0: 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
50c0: 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 7a 42 20 context);.. zB
50d0: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
50e0: 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 text(argv[0]);.
50f0: 20 7a 41 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 zA = sqlite3_va
5100: 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d lue_text(argv[1]
5110: 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74 );.. /* Limit t
5120: 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 he length of the
5130: 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 LIKE or GLOB pa
5140: 74 74 65 72 6e 20 74 6f 20 61 76 6f 69 64 20 70 ttern to avoid p
5150: 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20 roblems. ** of
5160: 64 65 65 70 20 72 65 63 75 72 73 69 6f 6e 20 61 deep recursion a
5170: 6e 64 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20 nd N*N behavior
5180: 69 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 in patternCompar
5190: 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 6e 50 61 74 e().. */. nPat
51a0: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
51b0: 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b _bytes(argv[0]);
51c0: 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 . testcase( nPa
51d0: 74 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 t==db->aLimit[SQ
51e0: 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f LITE_LIMIT_LIKE_
51f0: 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 20 PATTERN_LENGTH]
5200: 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e );. testcase( n
5210: 50 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b Pat==db->aLimit[
5220: 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b SQLITE_LIMIT_LIK
5230: 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 E_PATTERN_LENGTH
5240: 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 50 61 ]+1 );. if( nPa
5250: 74 20 3e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 t > db->aLimit[S
5260: 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 QLITE_LIMIT_LIKE
5270: 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d _PATTERN_LENGTH]
5280: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
5290: 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e result_error(con
52a0: 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47 text, "LIKE or G
52b0: 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 6f 20 LOB pattern too
52c0: 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a 20 complex", -1);.
52d0: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 return;. }.
52e0: 20 61 73 73 65 72 74 28 20 7a 42 3d 3d 73 71 6c assert( zB==sql
52f0: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
5300: 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 argv[0]) ); /*
5310: 45 6e 63 6f 64 69 6e 67 20 64 69 64 20 6e 6f 74 Encoding did not
5320: 20 63 68 61 6e 67 65 20 2a 2f 0a 0a 20 20 69 66 change */.. if
5330: 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 ( argc==3 ){.
5340: 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63 /* The escape c
5350: 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 haracter string
5360: 6d 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 must consist of
5370: 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 a single UTF-8 c
5380: 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a haracter.. **
5390: 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 Otherwise, retu
53a0: 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 rn an error..
53b0: 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e */. const un
53c0: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 73 signed char *zEs
53d0: 63 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 c = sqlite3_valu
53e0: 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b e_text(argv[2]);
53f0: 0a 20 20 20 20 69 66 28 20 7a 45 73 63 3d 3d 30 . if( zEsc==0
5400: 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 ) return;. i
5410: 66 28 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 f( sqlite3Utf8Ch
5420: 61 72 4c 65 6e 28 28 63 68 61 72 2a 29 7a 45 73 arLen((char*)zEs
5430: 63 2c 20 2d 31 29 21 3d 31 20 29 7b 0a 20 20 20 c, -1)!=1 ){.
5440: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
5450: 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c t_error(context,
5460: 20 0a 20 20 20 20 20 20 20 20 20 20 22 45 53 43 . "ESC
5470: 41 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d APE expression m
5480: 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 ust be a single
5490: 63 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b character", -1);
54a0: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
54b0: 20 20 20 7d 0a 20 20 20 20 65 73 63 61 70 65 20 }. escape
54c0: 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 = sqlite3Utf8Rea
54d0: 64 28 26 7a 45 73 63 29 3b 0a 20 20 7d 0a 20 20 d(&zEsc);. }.
54e0: 69 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b 0a if( zA && zB ){.
54f0: 20 20 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61 struct compa
5500: 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 reInfo *pInfo =
5510: 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
5520: 61 28 63 6f 6e 74 65 78 74 29 3b 0a 23 69 66 64 a(context);.#ifd
5530: 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 ef SQLITE_TEST.
5540: 20 20 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f sqlite3_like_
5550: 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a count++;.#endif.
5560: 20 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
5570: 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 _result_int(cont
5580: 65 78 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d 70 ext, patternComp
5590: 61 72 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e 66 are(zB, zA, pInf
55a0: 6f 2c 20 65 73 63 61 70 65 29 29 3b 0a 20 20 7d o, escape));. }
55b0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
55c0: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
55d0: 4e 55 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e 63 NULLIF(x,y) func
55e0: 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75 6c tion. The resul
55f0: 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a t is the first.*
5600: 2a 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74 68 * argument if th
5610: 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 e arguments are
5620: 64 69 66 66 65 72 65 6e 74 2e 20 20 54 68 65 20 different. The
5630: 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20 69 result is NULL i
5640: 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e f the.** argumen
5650: 74 73 20 61 72 65 20 65 71 75 61 6c 20 74 6f 20 ts are equal to
5660: 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 73 each other..*/.s
5670: 74 61 74 69 63 20 76 6f 69 64 20 6e 75 6c 6c 69 tatic void nulli
5680: 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 fFunc(. sqlite3
5690: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
56a0: 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 t,. int NotUsed
56b0: 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
56c0: 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 43 6f e **argv.){. Co
56d0: 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 llSeq *pColl = s
56e0: 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c qlite3GetFuncCol
56f0: 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 lSeq(context);.
5700: 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
5710: 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 R(NotUsed);. if
5720: 28 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 ( sqlite3MemComp
5730: 61 72 65 28 61 72 67 76 5b 30 5d 2c 20 61 72 67 are(argv[0], arg
5740: 76 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30 20 v[1], pColl)!=0
5750: 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 ){. sqlite3_r
5760: 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 esult_value(cont
5770: 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 ext, argv[0]);.
5780: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c }.}../*.** Impl
5790: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
57a0: 65 20 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e e sqlite_version
57b0: 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 () function. Th
57c0: 65 20 72 65 73 75 6c 74 20 69 73 20 74 68 65 20 e result is the
57d0: 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 version.** of th
57e0: 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 e SQLite library
57f0: 20 74 68 61 74 20 69 73 20 72 75 6e 6e 69 6e 67 that is running
5800: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
5810: 20 76 65 72 73 69 6f 6e 46 75 6e 63 28 0a 20 20 versionFunc(.
5820: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
5830: 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
5840: 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 NotUsed,. sqlit
5850: 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 e3_value **NotUs
5860: 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f ed2.){. UNUSED_
5870: 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 PARAMETER2(NotUs
5880: 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 ed, NotUsed2);.
5890: 20 2f 2a 20 49 4d 50 3a 20 52 2d 34 38 36 39 39 /* IMP: R-48699
58a0: 2d 34 38 36 31 37 20 54 68 69 73 20 66 75 6e 63 -48617 This func
58b0: 74 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 77 tion is an SQL w
58c0: 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 rapper around th
58d0: 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6c e. ** sqlite3_l
58e0: 69 62 76 65 72 73 69 6f 6e 28 29 20 43 2d 69 6e ibversion() C-in
58f0: 74 65 72 66 61 63 65 2e 20 2a 2f 0a 20 20 73 71 terface. */. sq
5900: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
5910: 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 t(context, sqlit
5920: 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 2c e3_libversion(),
5930: 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 -1, SQLITE_STAT
5940: 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d IC);.}../*.** Im
5950: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
5960: 74 68 65 20 73 71 6c 69 74 65 5f 73 6f 75 72 63 the sqlite_sourc
5970: 65 5f 69 64 28 29 20 66 75 6e 63 74 69 6f 6e 2e e_id() function.
5980: 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 The result is a
5990: 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 string.** that
59a0: 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 70 identifies the p
59b0: 61 72 74 69 63 75 6c 61 72 20 76 65 72 73 69 6f articular versio
59c0: 6e 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 20 n of the source
59d0: 63 6f 64 65 20 75 73 65 64 20 74 6f 20 62 75 69 code used to bui
59e0: 6c 64 0a 2a 2a 20 53 51 4c 69 74 65 2e 0a 2a 2f ld.** SQLite..*/
59f0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6f 75 .static void sou
5a00: 72 63 65 69 64 46 75 6e 63 28 0a 20 20 73 71 6c rceidFunc(. sql
5a10: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
5a20: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 ntext,. int Not
5a30: 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f Used,. sqlite3_
5a40: 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 value **NotUsed2
5a50: 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 .){. UNUSED_PAR
5a60: 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c AMETER2(NotUsed,
5a70: 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 2f 2a NotUsed2);. /*
5a80: 20 49 4d 50 3a 20 52 2d 32 34 34 37 30 2d 33 31 IMP: R-24470-31
5a90: 31 33 36 20 54 68 69 73 20 66 75 6e 63 74 69 6f 136 This functio
5aa0: 6e 20 69 73 20 61 6e 20 53 51 4c 20 77 72 61 70 n is an SQL wrap
5ab0: 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 0a 20 per around the.
5ac0: 20 2a 2a 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 ** sqlite3_sour
5ad0: 63 65 69 64 28 29 20 43 20 69 6e 74 65 72 66 61 ceid() C interfa
5ae0: 63 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ce. */. sqlite3
5af0: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
5b00: 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 73 6f text, sqlite3_so
5b10: 75 72 63 65 69 64 28 29 2c 20 2d 31 2c 20 53 51 urceid(), -1, SQ
5b20: 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a LITE_STATIC);.}.
5b30: 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
5b40: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
5b50: 69 74 65 5f 6c 6f 67 28 29 20 66 75 6e 63 74 69 ite_log() functi
5b60: 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 61 20 77 on. This is a w
5b70: 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a rapper around.**
5b80: 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 29 2e 20 sqlite3_log().
5b90: 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
5ba0: 65 20 69 73 20 4e 55 4c 4c 2e 20 20 54 68 65 20 e is NULL. The
5bb0: 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 20 function exists
5bc0: 70 75 72 65 6c 79 20 66 6f 72 0a 2a 2a 20 69 74 purely for.** it
5bd0: 73 20 73 69 64 65 2d 65 66 66 65 63 74 73 2e 0a s side-effects..
5be0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 */.static void e
5bf0: 72 72 6c 6f 67 46 75 6e 63 28 0a 20 20 73 71 6c rrlogFunc(. sql
5c00: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
5c10: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
5c20: 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
5c30: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 55 ue **argv.){. U
5c40: 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
5c50: 61 72 67 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f argc);. UNUSED_
5c60: 50 41 52 41 4d 45 54 45 52 28 63 6f 6e 74 65 78 PARAMETER(contex
5c70: 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6c 6f t);. sqlite3_lo
5c80: 67 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f g(sqlite3_value_
5c90: 69 6e 74 28 61 72 67 76 5b 30 5d 29 2c 20 22 25 int(argv[0]), "%
5ca0: 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 s", sqlite3_valu
5cb0: 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 29 e_text(argv[1]))
5cc0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
5cd0: 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
5ce0: 20 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f sqlite_compileo
5cf0: 70 74 69 6f 6e 5f 75 73 65 64 28 29 20 66 75 6e ption_used() fun
5d00: 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 72 65 ction..** The re
5d10: 73 75 6c 74 20 69 73 20 61 6e 20 69 6e 74 65 67 sult is an integ
5d20: 65 72 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 er that identifi
5d30: 65 73 20 69 66 20 74 68 65 20 63 6f 6d 70 69 6c es if the compil
5d40: 65 72 20 6f 70 74 69 6f 6e 0a 2a 2a 20 77 61 73 er option.** was
5d50: 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 53 used to build S
5d60: 51 4c 69 74 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 QLite..*/.#ifnde
5d70: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f f SQLITE_OMIT_CO
5d80: 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 MPILEOPTION_DIAG
5d90: 53 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f S.static void co
5da0: 6d 70 69 6c 65 6f 70 74 69 6f 6e 75 73 65 64 46 mpileoptionusedF
5db0: 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
5dc0: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
5dd0: 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
5de0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
5df0: 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 rgv.){. const c
5e00: 68 61 72 20 2a 7a 4f 70 74 4e 61 6d 65 3b 0a 20 har *zOptName;.
5e10: 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 assert( argc==1
5e20: 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 );. UNUSED_PAR
5e30: 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 AMETER(argc);.
5e40: 2f 2a 20 49 4d 50 3a 20 52 2d 33 39 35 36 34 2d /* IMP: R-39564-
5e50: 33 36 33 30 35 20 54 68 65 20 73 71 6c 69 74 65 36305 The sqlite
5e60: 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 _compileoption_u
5e70: 73 65 64 28 29 20 53 51 4c 0a 20 20 2a 2a 20 66 sed() SQL. ** f
5e80: 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61 unction is a wra
5e90: 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 pper around the
5ea0: 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f sqlite3_compileo
5eb0: 70 74 69 6f 6e 5f 75 73 65 64 28 29 20 43 2f 43 ption_used() C/C
5ec0: 2b 2b 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e ++. ** function
5ed0: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 4f .. */. if( (zO
5ee0: 70 74 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 ptName = (const
5ef0: 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 char*)sqlite3_va
5f00: 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d lue_text(argv[0]
5f10: 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c ))!=0 ){. sql
5f20: 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 ite3_result_int(
5f30: 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 context, sqlite3
5f40: 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 _compileoption_u
5f50: 73 65 64 28 7a 4f 70 74 4e 61 6d 65 29 29 3b 0a sed(zOptName));.
5f60: 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 }.}.#endif /*
5f70: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 SQLITE_OMIT_COMP
5f80: 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 20 ILEOPTION_DIAGS
5f90: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d */../*.** Implem
5fa0: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
5fb0: 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 sqlite_compileop
5fc0: 74 69 6f 6e 5f 67 65 74 28 29 20 66 75 6e 63 74 tion_get() funct
5fd0: 69 6f 6e 2e 20 0a 2a 2a 20 54 68 65 20 72 65 73 ion. .** The res
5fe0: 75 6c 74 20 69 73 20 61 20 73 74 72 69 6e 67 20 ult is a string
5ff0: 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 20 that identifies
6000: 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74 the compiler opt
6010: 69 6f 6e 73 20 0a 2a 2a 20 75 73 65 64 20 74 6f ions .** used to
6020: 20 62 75 69 6c 64 20 53 51 4c 69 74 65 2e 0a 2a build SQLite..*
6030: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
6040: 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 _OMIT_COMPILEOPT
6050: 49 4f 4e 5f 44 49 41 47 53 0a 73 74 61 74 69 63 ION_DIAGS.static
6060: 20 76 6f 69 64 20 63 6f 6d 70 69 6c 65 6f 70 74 void compileopt
6070: 69 6f 6e 67 65 74 46 75 6e 63 28 0a 20 20 73 71 iongetFunc(. sq
6080: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
6090: 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
60a0: 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
60b0: 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
60c0: 69 6e 74 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 int n;. assert(
60d0: 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e argc==1 );. UN
60e0: 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 USED_PARAMETER(a
60f0: 72 67 63 29 3b 0a 20 20 2f 2a 20 49 4d 50 3a 20 rgc);. /* IMP:
6100: 52 2d 30 34 39 32 32 2d 32 34 30 37 36 20 54 68 R-04922-24076 Th
6110: 65 20 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 e sqlite_compile
6120: 6f 70 74 69 6f 6e 5f 67 65 74 28 29 20 53 51 4c option_get() SQL
6130: 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 function. ** i
6140: 73 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 s a wrapper arou
6150: 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 nd the sqlite3_c
6160: 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 ompileoption_get
6170: 28 29 20 43 2f 43 2b 2b 20 66 75 6e 63 74 69 6f () C/C++ functio
6180: 6e 2e 0a 20 20 2a 2f 0a 20 20 6e 20 3d 20 73 71 n.. */. n = sq
6190: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 lite3_value_int(
61a0: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 argv[0]);. sqli
61b0: 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
61c0: 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 context, sqlite3
61d0: 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 _compileoption_g
61e0: 65 74 28 6e 29 2c 20 2d 31 2c 20 53 51 4c 49 54 et(n), -1, SQLIT
61f0: 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 23 65 6e E_STATIC);.}.#en
6200: 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
6210: 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e IT_COMPILEOPTION
6220: 5f 44 49 41 47 53 20 2a 2f 0a 0a 2f 2a 20 41 72 _DIAGS */../* Ar
6230: 72 61 79 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 ray for converti
6240: 6e 67 20 66 72 6f 6d 20 68 61 6c 66 2d 62 79 74 ng from half-byt
6250: 65 73 20 28 6e 79 62 62 6c 65 73 29 20 69 6e 74 es (nybbles) int
6260: 6f 20 41 53 43 49 49 20 68 65 78 0a 2a 2a 20 64 o ASCII hex.** d
6270: 69 67 69 74 73 2e 20 2a 2f 0a 73 74 61 74 69 63 igits. */.static
6280: 20 63 6f 6e 73 74 20 63 68 61 72 20 68 65 78 64 const char hexd
6290: 69 67 69 74 73 5b 5d 20 3d 20 7b 0a 20 20 27 30 igits[] = {. '0
62a0: 27 2c 20 27 31 27 2c 20 27 32 27 2c 20 27 33 27 ', '1', '2', '3'
62b0: 2c 20 27 34 27 2c 20 27 35 27 2c 20 27 36 27 2c , '4', '5', '6',
62c0: 20 27 37 27 2c 0a 20 20 27 38 27 2c 20 27 39 27 '7',. '8', '9'
62d0: 2c 20 27 41 27 2c 20 27 42 27 2c 20 27 43 27 2c , 'A', 'B', 'C',
62e0: 20 27 44 27 2c 20 27 45 27 2c 20 27 46 27 20 0a 'D', 'E', 'F' .
62f0: 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 58 50 45 52 49 };../*.** EXPERI
6300: 4d 45 4e 54 41 4c 20 2d 20 54 68 69 73 20 69 73 MENTAL - This is
6310: 20 6e 6f 74 20 61 6e 20 6f 66 66 69 63 69 61 6c not an official
6320: 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 function. The
6330: 69 6e 74 65 72 66 61 63 65 20 6d 61 79 0a 2a 2a interface may.**
6340: 20 63 68 61 6e 67 65 2e 20 20 54 68 69 73 20 66 change. This f
6350: 75 6e 63 74 69 6f 6e 20 6d 61 79 20 64 69 73 61 unction may disa
6360: 70 70 65 61 72 2e 20 20 44 6f 20 6e 6f 74 20 77 ppear. Do not w
6370: 72 69 74 65 20 63 6f 64 65 20 74 68 61 74 20 64 rite code that d
6380: 65 70 65 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68 69 epends.** on thi
6390: 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a s function..**.*
63a0: 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
63b0: 20 6f 66 20 74 68 65 20 51 55 4f 54 45 28 29 20 of the QUOTE()
63c0: 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 function. This
63d0: 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 function takes a
63e0: 20 73 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d single.** argum
63f0: 65 6e 74 2e 20 20 49 66 20 74 68 65 20 61 72 67 ent. If the arg
6400: 75 6d 65 6e 74 20 69 73 20 6e 75 6d 65 72 69 63 ument is numeric
6410: 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c , the return val
6420: 75 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 ue is the same a
6430: 73 0a 2a 2a 20 74 68 65 20 61 72 67 75 6d 65 6e s.** the argumen
6440: 74 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d t. If the argum
6450: 65 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 ent is NULL, the
6460: 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 return value is
6470: 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 the string.** "
6480: 4e 55 4c 4c 22 2e 20 20 4f 74 68 65 72 77 69 73 NULL". Otherwis
6490: 65 2c 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 e, the argument
64a0: 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 73 is enclosed in s
64b0: 69 6e 67 6c 65 20 71 75 6f 74 65 73 20 77 69 74 ingle quotes wit
64c0: 68 0a 2a 2a 20 73 69 6e 67 6c 65 2d 71 75 6f 74 h.** single-quot
64d0: 65 20 65 73 63 61 70 65 73 2e 0a 2a 2f 0a 73 74 e escapes..*/.st
64e0: 61 74 69 63 20 76 6f 69 64 20 71 75 6f 74 65 46 atic void quoteF
64f0: 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 unc(sqlite3_cont
6500: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e ext *context, in
6510: 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f t argc, sqlite3_
6520: 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 value **argv){.
6530: 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 assert( argc==1
6540: 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 );. UNUSED_PAR
6550: 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 AMETER(argc);.
6560: 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f switch( sqlite3_
6570: 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b value_type(argv[
6580: 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 0]) ){. case
6590: 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a SQLITE_FLOAT: {.
65a0: 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 31 2c double r1,
65b0: 20 72 32 3b 0a 20 20 20 20 20 20 63 68 61 72 20 r2;. char
65c0: 7a 42 75 66 5b 35 30 5d 3b 0a 20 20 20 20 20 20 zBuf[50];.
65d0: 72 31 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c r1 = sqlite3_val
65e0: 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 ue_double(argv[0
65f0: 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ]);. sqlite
6600: 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
6610: 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 f(zBuf), zBuf, "
6620: 25 21 2e 31 35 67 22 2c 20 72 31 29 3b 0a 20 20 %!.15g", r1);.
6630: 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 sqlite3AtoF(
6640: 7a 42 75 66 2c 20 26 72 32 2c 20 32 30 2c 20 53 zBuf, &r2, 20, S
6650: 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 QLITE_UTF8);.
6660: 20 20 20 69 66 28 20 72 31 21 3d 72 32 20 29 7b if( r1!=r2 ){
6670: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
6680: 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 _snprintf(sizeof
6690: 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 (zBuf), zBuf, "%
66a0: 21 2e 32 30 65 22 2c 20 72 31 29 3b 0a 20 20 20 !.20e", r1);.
66b0: 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
66c0: 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 e3_result_text(c
66d0: 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 ontext, zBuf, -1
66e0: 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
66f0: 4e 54 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b NT);. break
6700: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
6710: 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a SQLITE_INTEGER:
6720: 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 {. sqlite3
6730: 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f _result_value(co
6740: 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b ntext, argv[0]);
6750: 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
6760: 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
6770: 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 ITE_BLOB: {.
6780: 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 char *zText =
6790: 30 3b 0a 20 20 20 20 20 20 63 68 61 72 20 63 6f 0;. char co
67a0: 6e 73 74 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c nst *zBlob = sql
67b0: 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 ite3_value_blob(
67c0: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 argv[0]);.
67d0: 69 6e 74 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 int nBlob = sqli
67e0: 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
67f0: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 argv[0]);.
6800: 61 73 73 65 72 74 28 20 7a 42 6c 6f 62 3d 3d 73 assert( zBlob==s
6810: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f qlite3_value_blo
6820: 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 2f 2a b(argv[0]) ); /*
6830: 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 No encoding cha
6840: 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 65 nge */. zTe
6850: 78 74 20 3d 20 28 63 68 61 72 20 2a 29 63 6f 6e xt = (char *)con
6860: 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 textMalloc(conte
6870: 78 74 2c 20 28 32 2a 28 69 36 34 29 6e 42 6c 6f xt, (2*(i64)nBlo
6880: 62 29 2b 34 29 3b 20 0a 20 20 20 20 20 20 69 66 b)+4); . if
6890: 28 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20 ( zText ){.
68a0: 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
68b0: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 6c for(i=0; i<nBl
68c0: 6f 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ob; i++){.
68d0: 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b zText[(i*2)+
68e0: 32 5d 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28 2] = hexdigits[(
68f0: 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 29 26 30 78 30 zBlob[i]>>4)&0x0
6900: 46 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 F];. zT
6910: 65 78 74 5b 28 69 2a 32 29 2b 33 5d 20 3d 20 68 ext[(i*2)+3] = h
6920: 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b exdigits[(zBlob[
6930: 69 5d 29 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 i])&0x0F];.
6940: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 54 65 }. zTe
6950: 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20 xt[(nBlob*2)+2]
6960: 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 = '\'';.
6970: 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b zText[(nBlob*2)+
6980: 33 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 3] = '\0';.
6990: 20 20 20 7a 54 65 78 74 5b 30 5d 20 3d 20 27 58 zText[0] = 'X
69a0: 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 ';. zText
69b0: 5b 31 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 [1] = '\'';.
69c0: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
69d0: 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
69e0: 20 7a 54 65 78 74 2c 20 2d 31 2c 20 53 51 4c 49 zText, -1, SQLI
69f0: 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 TE_TRANSIENT);.
6a00: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
6a10: 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20 20 20 ree(zText);.
6a20: 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
6a30: 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
6a40: 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 SQLITE_TEXT: {.
6a50: 20 20 20 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 20 int i,j;.
6a60: 20 20 20 20 75 36 34 20 6e 3b 0a 20 20 20 20 20 u64 n;.
6a70: 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
6a80: 63 68 61 72 20 2a 7a 41 72 67 20 3d 20 73 71 6c char *zArg = sql
6a90: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
6aa0: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 argv[0]);.
6ab0: 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 20 char *z;..
6ac0: 69 66 28 20 7a 41 72 67 3d 3d 30 20 29 20 72 65 if( zArg==0 ) re
6ad0: 74 75 72 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 turn;. for(
6ae0: 69 3d 30 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b 69 i=0, n=0; zArg[i
6af0: 5d 3b 20 69 2b 2b 29 7b 20 69 66 28 20 7a 41 72 ]; i++){ if( zAr
6b00: 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b g[i]=='\'' ) n++
6b10: 3b 20 7d 0a 20 20 20 20 20 20 7a 20 3d 20 63 6f ; }. z = co
6b20: 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 ntextMalloc(cont
6b30: 65 78 74 2c 20 28 28 69 36 34 29 69 29 2b 28 28 ext, ((i64)i)+((
6b40: 69 36 34 29 6e 29 2b 33 29 3b 0a 20 20 20 20 20 i64)n)+3);.
6b50: 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 if( z ){.
6b60: 20 20 7a 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a 20 z[0] = '\'';.
6b70: 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 for(i=0,
6b80: 6a 3d 31 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b j=1; zArg[i]; i+
6b90: 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b +){. z[
6ba0: 6a 2b 2b 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a j++] = zArg[i];.
6bb0: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 41 if( zA
6bc0: 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 rg[i]=='\'' ){.
6bd0: 20 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b z[j++
6be0: 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 ] = '\'';.
6bf0: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
6c00: 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d z[j++] =
6c10: 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 7a '\'';. z
6c20: 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 [j] = 0;.
6c30: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
6c40: 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c text(context, z,
6c50: 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 j, sqlite3_free
6c60: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
6c70: 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
6c80: 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
6c90: 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
6ca0: 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 e3_value_type(ar
6cb0: 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e gv[0])==SQLITE_N
6cc0: 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ULL );. sql
6cd0: 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
6ce0: 28 63 6f 6e 74 65 78 74 2c 20 22 4e 55 4c 4c 22 (context, "NULL"
6cf0: 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 , 4, SQLITE_STAT
6d00: 49 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b IC);. break
6d10: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f ;. }. }.}../
6d20: 2a 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 *.** EXPERIMENTA
6d30: 4c 20 2d 20 54 68 69 73 20 69 73 20 6e 6f 74 20 L - This is not
6d40: 61 6e 20 6f 66 66 69 63 69 61 6c 20 66 75 6e 63 an official func
6d50: 74 69 6f 6e 2e 20 20 54 68 65 20 69 6e 74 65 72 tion. The inter
6d60: 66 61 63 65 20 6d 61 79 0a 2a 2a 20 63 68 61 6e face may.** chan
6d70: 67 65 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 ge. This functi
6d80: 6f 6e 20 6d 61 79 20 64 69 73 61 70 70 65 61 72 on may disappear
6d90: 2e 20 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 . Do not write
6da0: 63 6f 64 65 20 74 68 61 74 20 64 65 70 65 6e 64 code that depend
6db0: 73 0a 2a 2a 20 6f 6e 20 74 68 69 73 20 66 75 6e s.** on this fun
6dc0: 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 ction..**.** Imp
6dd0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
6de0: 68 65 20 74 6f 69 6e 74 65 67 65 72 28 29 20 66 he tointeger() f
6df0: 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 unction. This f
6e00: 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 0a unction takes a.
6e10: 2a 2a 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 ** single argume
6e20: 6e 74 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 nt. If the argu
6e30: 6d 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 67 ment is an integ
6e40: 65 72 20 6f 72 20 69 73 20 61 20 64 6f 75 62 6c er or is a doubl
6e50: 65 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 e that.** can be
6e60: 20 6c 6f 73 73 6c 65 73 73 6c 79 20 63 6f 6e 76 losslessly conv
6e70: 65 72 74 65 64 20 74 6f 20 61 6e 20 69 6e 74 65 erted to an inte
6e80: 67 65 72 2c 20 74 68 65 20 72 65 74 75 72 6e 20 ger, the return
6e90: 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 value is the.**
6ea0: 73 61 6d 65 20 61 73 20 74 68 65 20 61 72 67 75 same as the argu
6eb0: 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 61 72 ment. If the ar
6ec0: 67 75 6d 65 6e 74 20 69 73 20 61 20 64 6f 75 62 gument is a doub
6ed0: 6c 65 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 le that cannot b
6ee0: 65 0a 2a 2a 20 6c 6f 73 73 6c 65 73 73 6c 79 20 e.** losslessly
6ef0: 72 65 70 72 65 73 65 6e 74 65 64 20 61 73 20 61 represented as a
6f00: 6e 20 69 6e 74 65 67 65 72 2c 20 74 68 65 20 72 n integer, the r
6f10: 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 4e eturn value is N
6f20: 55 4c 4c 2e 0a 2a 2a 20 49 66 20 74 68 65 20 61 ULL..** If the a
6f30: 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c 4c 2c rgument is NULL,
6f40: 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
6f50: 65 20 69 73 20 4e 55 4c 4c 2e 20 20 4f 74 68 65 e is NULL. Othe
6f60: 72 77 69 73 65 2c 20 61 6e 0a 2a 2a 20 61 74 74 rwise, an.** att
6f70: 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 empt is made to
6f80: 63 6f 6e 76 65 72 74 20 74 68 65 20 61 72 67 75 convert the argu
6f90: 6d 65 6e 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 ment to an integ
6fa0: 65 72 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 er. If the.** c
6fb0: 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 73 75 63 onversion is suc
6fc0: 63 65 73 73 66 75 6c 2c 20 74 68 65 20 69 6e 74 cessful, the int
6fd0: 65 67 65 72 20 76 61 6c 75 65 20 69 73 20 72 65 eger value is re
6fe0: 74 75 72 6e 65 64 3b 20 6f 74 68 65 72 77 69 73 turned; otherwis
6ff0: 65 2c 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 e,.** NULL is re
7000: 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 turned..*/.stati
7010: 63 20 76 6f 69 64 20 74 6f 69 6e 74 65 67 65 72 c void tointeger
7020: 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
7030: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
7040: 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
7050: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
7060: 61 72 67 76 0a 29 7b 0a 20 20 61 73 73 65 72 74 argv.){. assert
7070: 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 ( argc==1 );. U
7080: 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
7090: 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28 argc);. switch(
70a0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
70b0: 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a ype(argv[0]) ){.
70c0: 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
70d0: 46 4c 4f 41 54 3a 0a 23 69 66 6e 64 65 66 20 53 FLOAT:.#ifndef S
70e0: 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
70f0: 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 7b 0a ING_POINT. {.
7100: 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 56 61 double rVa
7110: 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
7120: 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d e_double(argv[0]
7130: 29 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 );. double
7140: 72 49 6e 74 56 61 6c 20 3d 20 30 2e 30 3b 0a 20 rIntVal = 0.0;.
7150: 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 if( !sqlite
7160: 33 49 73 4e 61 4e 28 72 56 61 6c 29 20 26 26 20 3IsNaN(rVal) &&
7170: 6d 6f 64 66 28 72 56 61 6c 2c 20 26 72 49 6e 74 modf(rVal, &rInt
7180: 56 61 6c 29 3d 3d 30 2e 30 20 26 26 0a 20 20 20 Val)==0.0 &&.
7190: 20 20 20 20 20 20 20 72 49 6e 74 56 61 6c 3e 3d rIntVal>=
71a0: 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 26 SMALLEST_INT64 &
71b0: 26 20 72 49 6e 74 56 61 6c 3c 3d 4c 41 52 47 45 & rIntVal<=LARGE
71c0: 53 54 5f 49 4e 54 36 34 20 29 7b 0a 20 20 20 20 ST_INT64 ){.
71d0: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
71e0: 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 lt_int64(context
71f0: 2c 20 28 69 36 34 29 72 49 6e 74 56 61 6c 29 3b , (i64)rIntVal);
7200: 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b . return;
7210: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
7220: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 qlite3_result_nu
7230: 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 ll(context);.
7240: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
7250: 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 #endif. case
7260: 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 SQLITE_INTEGER:
7270: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
7280: 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e result_int64(con
7290: 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 text, sqlite3_va
72a0: 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 lue_int64(argv[0
72b0: 5d 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ]));. break
72c0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
72d0: 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 SQLITE_BLOB:.
72e0: 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 case SQLITE_TE
72f0: 58 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 XT: {. cons
7300: 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
7310: 2a 7a 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f *zStr = sqlite3_
7320: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
7330: 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 0]);. if( z
7340: 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 69 Str ){. i
7350: 6e 74 20 6e 53 74 72 20 3d 20 73 71 6c 69 74 65 nt nStr = sqlite
7360: 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 3_value_bytes(ar
7370: 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 gv[0]);.
7380: 69 66 28 20 6e 53 74 72 20 29 7b 0a 20 20 20 20 if( nStr ){.
7390: 20 20 20 20 20 20 69 36 34 20 69 56 61 6c 3b 0a i64 iVal;.
73a0: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 73 if( !s
73b0: 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 53 74 qlite3Atoi64(zSt
73c0: 72 2c 20 26 69 56 61 6c 2c 20 6e 53 74 72 2c 20 r, &iVal, nStr,
73d0: 53 51 4c 49 54 45 5f 55 54 46 38 29 20 29 7b 0a SQLITE_UTF8) ){.
73e0: 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
73f0: 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 te3_result_int64
7400: 28 63 6f 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b (context, iVal);
7410: 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 . ret
7420: 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d urn;. }
7430: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7440: 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
7450: 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e _result_null(con
7460: 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 72 65 text);. bre
7470: 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
7480: 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 fault: {. a
7490: 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76 ssert( sqlite3_v
74a0: 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 alue_type(argv[0
74b0: 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 ])==SQLITE_NULL
74c0: 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
74d0: 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e _result_null(con
74e0: 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 72 65 text);. bre
74f0: 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a ak;. }. }.}.
7500: 0a 2f 2a 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e ./*.** EXPERIMEN
7510: 54 41 4c 20 2d 20 54 68 69 73 20 69 73 20 6e 6f TAL - This is no
7520: 74 20 61 6e 20 6f 66 66 69 63 69 61 6c 20 66 75 t an official fu
7530: 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 69 6e 74 nction. The int
7540: 65 72 66 61 63 65 20 6d 61 79 0a 2a 2a 20 63 68 erface may.** ch
7550: 61 6e 67 65 2e 20 20 54 68 69 73 20 66 75 6e 63 ange. This func
7560: 74 69 6f 6e 20 6d 61 79 20 64 69 73 61 70 70 65 tion may disappe
7570: 61 72 2e 20 20 44 6f 20 6e 6f 74 20 77 72 69 74 ar. Do not writ
7580: 65 20 63 6f 64 65 20 74 68 61 74 20 64 65 70 65 e code that depe
7590: 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68 69 73 20 66 nds.** on this f
75a0: 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 unction..**.** I
75b0: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
75c0: 20 74 68 65 20 74 6f 72 65 61 6c 28 29 20 66 75 the toreal() fu
75d0: 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 nction. This fu
75e0: 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 0a 2a nction takes a.*
75f0: 2a 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e * single argumen
7600: 74 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d t. If the argum
7610: 65 6e 74 20 69 73 20 61 20 64 6f 75 62 6c 65 20 ent is a double
7620: 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 or is an integer
7630: 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 that.** can be
7640: 6c 6f 73 73 6c 65 73 73 6c 79 20 63 6f 6e 76 65 losslessly conve
7650: 72 74 65 64 20 74 6f 20 61 20 64 6f 75 62 6c 65 rted to a double
7660: 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c , the return val
7670: 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d ue is the.** sam
7680: 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e e as the argumen
7690: 74 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d t. If the argum
76a0: 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65 ent is an intege
76b0: 72 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65 r that cannot be
76c0: 0a 2a 2a 20 6c 6f 73 73 6c 65 73 73 6c 79 20 72 .** losslessly r
76d0: 65 70 72 65 73 65 6e 74 65 64 20 61 73 20 61 20 epresented as a
76e0: 64 6f 75 62 6c 65 2c 20 74 68 65 20 72 65 74 75 double, the retu
76f0: 72 6e 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c rn value is NULL
7700: 2e 0a 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 ..** If the argu
7710: 6d 65 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 ment is NULL, th
7720: 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 e return value i
7730: 73 20 4e 55 4c 4c 2e 20 20 4f 74 68 65 72 77 69 s NULL. Otherwi
7740: 73 65 2c 20 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 se, an.** attemp
7750: 74 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6f 6e t is made to con
7760: 76 65 72 74 20 74 68 65 20 61 72 67 75 6d 65 6e vert the argumen
7770: 74 20 74 6f 20 61 20 64 6f 75 62 6c 65 2e 20 20 t to a double.
7780: 49 66 20 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 72 If the.** conver
7790: 73 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 sion is successf
77a0: 75 6c 2c 20 74 68 65 20 64 6f 75 62 6c 65 20 76 ul, the double v
77b0: 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 alue is returned
77c0: 3b 20 6f 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20 ; otherwise,.**
77d0: 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 NULL is returned
77e0: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
77f0: 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
7800: 47 5f 50 4f 49 4e 54 0a 73 74 61 74 69 63 20 76 G_POINT.static v
7810: 6f 69 64 20 74 6f 72 65 61 6c 46 75 6e 63 28 0a oid torealFunc(.
7820: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
7830: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
7840: 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
7850: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
7860: 7b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 {. assert( argc
7870: 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f ==1 );. UNUSED_
7880: 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b PARAMETER(argc);
7890: 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 . switch( sqlit
78a0: 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 e3_value_type(ar
78b0: 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 gv[0]) ){. ca
78c0: 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a se SQLITE_FLOAT:
78d0: 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 {. sqlite3
78e0: 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 _result_double(c
78f0: 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f ontext, sqlite3_
7900: 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 value_double(arg
7910: 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 62 72 v[0]));. br
7920: 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
7930: 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 ase SQLITE_INTEG
7940: 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 36 34 20 ER: {. i64
7950: 69 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 iVal = sqlite3_v
7960: 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b alue_int64(argv[
7970: 30 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c 0]);. doubl
7980: 65 20 72 56 61 6c 20 3d 20 28 64 6f 75 62 6c 65 e rVal = (double
7990: 29 69 56 61 6c 3b 0a 20 20 20 20 20 20 69 66 28 )iVal;. if(
79a0: 20 69 56 61 6c 3d 3d 72 56 61 6c 20 29 7b 0a 20 iVal==rVal ){.
79b0: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 sqlite3_r
79c0: 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e esult_double(con
79d0: 74 65 78 74 2c 20 72 56 61 6c 29 3b 0a 20 20 20 text, rVal);.
79e0: 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
79f0: 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
7a00: 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 e3_result_null(c
7a10: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 ontext);. b
7a20: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
7a30: 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 case SQLITE_BLOB
7a40: 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 :. case SQLIT
7a50: 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 E_TEXT: {.
7a60: 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
7a70: 68 61 72 20 2a 7a 53 74 72 20 3d 20 73 71 6c 69 har *zStr = sqli
7a80: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
7a90: 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 rgv[0]);. i
7aa0: 66 28 20 7a 53 74 72 20 29 7b 0a 20 20 20 20 20 f( zStr ){.
7ab0: 20 20 20 69 6e 74 20 6e 53 74 72 20 3d 20 73 71 int nStr = sq
7ac0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
7ad0: 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 s(argv[0]);.
7ae0: 20 20 20 20 69 66 28 20 6e 53 74 72 20 29 7b 0a if( nStr ){.
7af0: 20 20 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 double
7b00: 20 72 56 61 6c 3b 0a 20 20 20 20 20 20 20 20 20 rVal;.
7b10: 20 69 66 28 20 73 71 6c 69 74 65 33 41 74 6f 46 if( sqlite3AtoF
7b20: 28 7a 53 74 72 2c 20 26 72 56 61 6c 2c 20 6e 53 (zStr, &rVal, nS
7b30: 74 72 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 tr, SQLITE_UTF8)
7b40: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
7b50: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 sqlite3_result_d
7b60: 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 72 ouble(context, r
7b70: 56 61 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 Val);.
7b80: 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
7b90: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
7ba0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
7bb0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c lite3_result_nul
7bc0: 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 l(context);.
7bd0: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
7be0: 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
7bf0: 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
7c00: 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 te3_value_type(a
7c10: 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f rgv[0])==SQLITE_
7c20: 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73 71 NULL );. sq
7c30: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c lite3_result_nul
7c40: 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 l(context);.
7c50: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
7c60: 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a }.}.#endif../*.
7c70: 2a 2a 20 54 68 65 20 75 6e 69 63 6f 64 65 28 29 ** The unicode()
7c80: 20 66 75 6e 63 74 69 6f 6e 2e 20 20 52 65 74 75 function. Retu
7c90: 72 6e 20 74 68 65 20 69 6e 74 65 67 65 72 20 75 rn the integer u
7ca0: 6e 69 63 6f 64 65 20 63 6f 64 65 2d 70 6f 69 6e nicode code-poin
7cb0: 74 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 t value.** for t
7cc0: 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 he first charact
7cd0: 65 72 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 er of the input
7ce0: 73 74 72 69 6e 67 2e 20 0a 2a 2f 0a 73 74 61 74 string. .*/.stat
7cf0: 69 63 20 76 6f 69 64 20 75 6e 69 63 6f 64 65 46 ic void unicodeF
7d00: 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
7d10: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
7d20: 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
7d30: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
7d40: 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 rgv.){. const u
7d50: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 20 nsigned char *z
7d60: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
7d70: 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 text(argv[0]);.
7d80: 20 28 76 6f 69 64 29 61 72 67 63 3b 0a 20 20 69 (void)argc;. i
7d90: 66 28 20 7a 20 26 26 20 7a 5b 30 5d 20 29 20 73 f( z && z[0] ) s
7da0: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
7db0: 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 t(context, sqlit
7dc0: 65 33 55 74 66 38 52 65 61 64 28 26 7a 29 29 3b e3Utf8Read(&z));
7dd0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 68 .}../*.** The ch
7de0: 61 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 61 ar() function ta
7df0: 6b 65 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 kes zero or more
7e00: 20 61 72 67 75 6d 65 6e 74 73 2c 20 65 61 63 68 arguments, each
7e10: 20 6f 66 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 of which is.**
7e20: 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 74 20 an integer. It
7e30: 63 6f 6e 73 74 72 75 63 74 73 20 61 20 73 74 72 constructs a str
7e40: 69 6e 67 20 77 68 65 72 65 20 65 61 63 68 20 63 ing where each c
7e50: 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 20 haracter of the
7e60: 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 74 68 65 string.** is the
7e70: 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 unicode charact
7e80: 65 72 20 66 6f 72 20 74 68 65 20 63 6f 72 72 65 er for the corre
7e90: 73 70 6f 6e 64 69 6e 67 20 69 6e 74 65 67 65 72 sponding integer
7ea0: 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 argument..*/.st
7eb0: 61 74 69 63 20 76 6f 69 64 20 63 68 61 72 46 75 atic void charFu
7ec0: 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
7ed0: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
7ee0: 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
7ef0: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
7f00: 67 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 gv.){. unsigned
7f10: 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 4f 75 74 3b char *z, *zOut;
7f20: 0a 20 20 69 6e 74 20 69 3b 0a 20 20 7a 4f 75 74 . int i;. zOut
7f30: 20 3d 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d = z = sqlite3_m
7f40: 61 6c 6c 6f 63 28 20 61 72 67 63 2a 34 20 29 3b alloc( argc*4 );
7f50: 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 . if( z==0 ){.
7f60: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
7f70: 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f t_error_nomem(co
7f80: 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 74 75 ntext);. retu
7f90: 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d rn;. }. for(i=
7fa0: 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0; i<argc; i++){
7fb0: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 . sqlite3_int
7fc0: 36 34 20 78 3b 0a 20 20 20 20 75 6e 73 69 67 6e 64 x;. unsign
7fd0: 65 64 20 63 3b 0a 20 20 20 20 78 20 3d 20 73 71 ed c;. x = sq
7fe0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 lite3_value_int6
7ff0: 34 28 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 4(argv[i]);.
8000: 69 66 28 20 78 3c 30 20 7c 7c 20 78 3e 30 78 31 if( x<0 || x>0x1
8010: 30 66 66 66 66 20 29 20 78 20 3d 20 30 78 66 66 0ffff ) x = 0xff
8020: 66 64 3b 0a 20 20 20 20 63 20 3d 20 28 75 6e 73 fd;. c = (uns
8030: 69 67 6e 65 64 29 28 78 20 26 20 30 78 31 66 66 igned)(x & 0x1ff
8040: 66 66 66 29 3b 0a 20 20 20 20 69 66 28 20 63 3c fff);. if( c<
8050: 30 78 30 30 30 38 30 20 29 7b 0a 20 20 20 20 20 0x00080 ){.
8060: 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
8070: 63 26 30 78 46 46 29 3b 0a 20 20 20 20 7d 65 6c c&0xFF);. }el
8080: 73 65 20 69 66 28 20 63 3c 30 78 30 30 38 30 30 se if( c<0x00800
8090: 20 29 7b 0a 20 20 20 20 20 20 2a 7a 4f 75 74 2b ){. *zOut+
80a0: 2b 20 3d 20 30 78 43 30 20 2b 20 28 75 38 29 28 + = 0xC0 + (u8)(
80b0: 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 0a 20 20 (c>>6)&0x1F);.
80c0: 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 *zOut++ = 0x
80d0: 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 80 + (u8)(c & 0x
80e0: 33 46 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 3F);. }else i
80f0: 66 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b 0a f( c<0x10000 ){.
8100: 20 20 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 *zOut++ =
8110: 30 78 45 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 0xE0 + (u8)((c>>
8120: 31 32 29 26 30 78 30 46 29 3b 0a 20 20 20 20 20 12)&0x0F);.
8130: 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 *zOut++ = 0x80
8140: 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 20 26 20 + (u8)((c>>6) &
8150: 30 78 33 46 29 3b 0a 20 20 20 20 20 20 2a 7a 4f 0x3F);. *zO
8160: 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 ut++ = 0x80 + (u
8170: 38 29 28 63 20 26 20 30 78 33 46 29 3b 0a 20 20 8)(c & 0x3F);.
8180: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a }else{. *
8190: 7a 4f 75 74 2b 2b 20 3d 20 30 78 46 30 20 2b 20 zOut++ = 0xF0 +
81a0: 28 75 38 29 28 28 63 3e 3e 31 38 29 20 26 20 30 (u8)((c>>18) & 0
81b0: 78 30 37 29 3b 0a 20 20 20 20 20 20 2a 7a 4f 75 x07);. *zOu
81c0: 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 t++ = 0x80 + (u8
81d0: 29 28 28 63 3e 3e 31 32 29 20 26 20 30 78 33 46 )((c>>12) & 0x3F
81e0: 29 3b 0a 20 20 20 20 20 20 2a 7a 4f 75 74 2b 2b );. *zOut++
81f0: 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 = 0x80 + (u8)((
8200: 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 0a 20 c>>6) & 0x3F);.
8210: 20 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 *zOut++ = 0
8220: 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 x80 + (u8)(c & 0
8230: 78 33 46 29 3b 0a 20 20 20 20 7d 20 20 20 20 20 x3F);. }
8240: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8250: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8260: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
8270: 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 . }. sqlite3_r
8280: 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
8290: 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 28 69 xt, (char*)z, (i
82a0: 6e 74 29 28 7a 4f 75 74 2d 7a 29 2c 20 73 71 6c nt)(zOut-z), sql
82b0: 69 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a 2f ite3_free);.}../
82c0: 2a 0a 2a 2a 20 54 68 65 20 68 65 78 28 29 20 66 *.** The hex() f
82d0: 75 6e 63 74 69 6f 6e 2e 20 20 49 6e 74 65 72 70 unction. Interp
82e0: 72 65 74 20 74 68 65 20 61 72 67 75 6d 65 6e 74 ret the argument
82f0: 20 61 73 20 61 20 62 6c 6f 62 2e 20 20 52 65 74 as a blob. Ret
8300: 75 72 6e 0a 2a 2a 20 61 20 68 65 78 61 64 65 63 urn.** a hexadec
8310: 69 6d 61 6c 20 72 65 6e 64 65 72 69 6e 67 20 61 imal rendering a
8320: 73 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 s text..*/.stati
8330: 63 20 76 6f 69 64 20 68 65 78 46 75 6e 63 28 0a c void hexFunc(.
8340: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
8350: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
8360: 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
8370: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
8380: 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 {. int i, n;.
8390: 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
83a0: 68 61 72 20 2a 70 42 6c 6f 62 3b 0a 20 20 63 68 har *pBlob;. ch
83b0: 61 72 20 2a 7a 48 65 78 2c 20 2a 7a 3b 0a 20 20 ar *zHex, *z;.
83c0: 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 assert( argc==1
83d0: 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
83e0: 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 70 METER(argc);. p
83f0: 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 76 Blob = sqlite3_v
8400: 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 alue_blob(argv[0
8410: 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 ]);. n = sqlite
8420: 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 3_value_bytes(ar
8430: 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 gv[0]);. assert
8440: 28 20 70 42 6c 6f 62 3d 3d 73 71 6c 69 74 65 33 ( pBlob==sqlite3
8450: 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 _value_blob(argv
8460: 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 [0]) ); /* No e
8470: 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a ncoding change *
8480: 2f 0a 20 20 7a 20 3d 20 7a 48 65 78 20 3d 20 63 /. z = zHex = c
8490: 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e ontextMalloc(con
84a0: 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2a 32 text, ((i64)n)*2
84b0: 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 7a 48 65 + 1);. if( zHe
84c0: 78 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 x ){. for(i=0
84d0: 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c 6f ; i<n; i++, pBlo
84e0: 62 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 b++){. unsi
84f0: 67 6e 65 64 20 63 68 61 72 20 63 20 3d 20 2a 70 gned char c = *p
8500: 42 6c 6f 62 3b 0a 20 20 20 20 20 20 2a 28 7a 2b Blob;. *(z+
8510: 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28 +) = hexdigits[(
8520: 63 3e 3e 34 29 26 30 78 66 5d 3b 0a 20 20 20 20 c>>4)&0xf];.
8530: 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69 *(z++) = hexdi
8540: 67 69 74 73 5b 63 26 30 78 66 5d 3b 0a 20 20 20 gits[c&0xf];.
8550: 20 7d 0a 20 20 20 20 2a 7a 20 3d 20 30 3b 0a 20 }. *z = 0;.
8560: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
8570: 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
8580: 7a 48 65 78 2c 20 6e 2a 32 2c 20 73 71 6c 69 74 zHex, n*2, sqlit
8590: 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a e3_free);. }.}.
85a0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 62 ./*.** The zerob
85b0: 6c 6f 62 28 4e 29 20 66 75 6e 63 74 69 6f 6e 20 lob(N) function
85c0: 72 65 74 75 72 6e 73 20 61 20 7a 65 72 6f 2d 66 returns a zero-f
85d0: 69 6c 6c 65 64 20 62 6c 6f 62 20 6f 66 20 73 69 illed blob of si
85e0: 7a 65 20 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 ze N bytes..*/.s
85f0: 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f 62 tatic void zerob
8600: 6c 6f 62 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 lobFunc(. sqlit
8610: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
8620: 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c ext,. int argc,
8630: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
8640: 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 36 34 **argv.){. i64
8650: 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 n;. sqlite3 *d
8660: 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 b = sqlite3_cont
8670: 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f ext_db_handle(co
8680: 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72 74 ntext);. assert
8690: 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 ( argc==1 );. U
86a0: 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
86b0: 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73 71 6c argc);. n = sql
86c0: 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 ite3_value_int64
86d0: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65 73 (argv[0]);. tes
86e0: 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e 61 4c tcase( n==db->aL
86f0: 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
8700: 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74 T_LENGTH] );. t
8710: 65 73 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e estcase( n==db->
8720: 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
8730: 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b MIT_LENGTH]+1 );
8740: 0a 20 20 69 66 28 20 6e 3e 64 62 2d 3e 61 4c 69 . if( n>db->aLi
8750: 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
8760: 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 _LENGTH] ){.
8770: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
8780: 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 rror_toobig(cont
8790: 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ext);. }else{.
87a0: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
87b0: 74 5f 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e 74 65 t_zeroblob(conte
87c0: 78 74 2c 20 28 69 6e 74 29 6e 29 3b 20 2f 2a 20 xt, (int)n); /*
87d0: 49 4d 50 3a 20 52 2d 30 30 32 39 33 2d 36 34 39 IMP: R-00293-649
87e0: 39 34 20 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 94 */. }.}../*.
87f0: 2a 2a 20 54 68 65 20 72 65 70 6c 61 63 65 28 29 ** The replace()
8800: 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 72 65 function. Thre
8810: 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 e arguments are
8820: 61 6c 6c 20 73 74 72 69 6e 67 73 3a 20 63 61 6c all strings: cal
8830: 6c 0a 2a 2a 20 74 68 65 6d 20 41 2c 20 42 2c 20 l.** them A, B,
8840: 61 6e 64 20 43 2e 20 54 68 65 20 72 65 73 75 6c and C. The resul
8850: 74 20 69 73 20 61 6c 73 6f 20 61 20 73 74 72 69 t is also a stri
8860: 6e 67 20 77 68 69 63 68 20 69 73 20 64 65 72 69 ng which is deri
8870: 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 79 ved.** from A by
8880: 20 72 65 70 6c 61 63 69 6e 67 20 65 76 65 72 79 replacing every
8890: 20 6f 63 63 75 72 61 6e 63 65 20 6f 66 20 42 20 occurance of B
88a0: 77 69 74 68 20 43 2e 20 20 54 68 65 20 6d 61 74 with C. The mat
88b0: 63 68 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 78 ch.** must be ex
88c0: 61 63 74 2e 20 20 43 6f 6c 6c 61 74 69 6e 67 20 act. Collating
88d0: 73 65 71 75 65 6e 63 65 73 20 61 72 65 20 6e 6f sequences are no
88e0: 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 t used..*/.stati
88f0: 63 20 76 6f 69 64 20 72 65 70 6c 61 63 65 46 75 c void replaceFu
8900: 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
8910: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
8920: 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
8930: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
8940: 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e gv.){. const un
8950: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 signed char *zSt
8960: 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 r; /* The
8970: 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 41 20 input string A
8980: 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 */. const unsig
8990: 6e 65 64 20 63 68 61 72 20 2a 7a 50 61 74 74 65 ned char *zPatte
89a0: 72 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 70 61 rn; /* The pa
89b0: 74 74 65 72 6e 20 73 74 72 69 6e 67 20 42 20 2a ttern string B *
89c0: 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e /. const unsign
89d0: 65 64 20 63 68 61 72 20 2a 7a 52 65 70 3b 20 20 ed char *zRep;
89e0: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 70 /* The rep
89f0: 6c 61 63 65 6d 65 6e 74 20 73 74 72 69 6e 67 20 lacement string
8a00: 43 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 C */. unsigned
8a10: 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 char *zOut;
8a20: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
8a30: 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 output */. int
8a40: 6e 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 nStr;
8a50: 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
8a60: 7a 53 74 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 zStr */. int nP
8a70: 61 74 74 65 72 6e 3b 20 20 20 20 20 20 20 20 20 attern;
8a80: 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 50 /* Size of zP
8a90: 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 attern */. int
8aa0: 6e 52 65 70 3b 20 20 20 20 20 20 20 20 20 20 20 nRep;
8ab0: 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
8ac0: 7a 52 65 70 20 2a 2f 0a 20 20 69 36 34 20 6e 4f zRep */. i64 nO
8ad0: 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ut;
8ae0: 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 69 /* Maximum si
8af0: 7a 65 20 6f 66 20 7a 4f 75 74 20 2a 2f 0a 20 20 ze of zOut */.
8b00: 69 6e 74 20 6c 6f 6f 70 4c 69 6d 69 74 3b 20 20 int loopLimit;
8b10: 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 /* Last
8b20: 20 7a 53 74 72 5b 5d 20 74 68 61 74 20 6d 69 67 zStr[] that mig
8b30: 68 74 20 6d 61 74 63 68 20 7a 50 61 74 74 65 72 ht match zPatter
8b40: 6e 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 n[] */. int i,
8b50: 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 j;
8b60: 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
8b70: 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 rs */.. assert(
8b80: 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 55 4e argc==3 );. UN
8b90: 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 USED_PARAMETER(a
8ba0: 72 67 63 29 3b 0a 20 20 7a 53 74 72 20 3d 20 73 rgc);. zStr = s
8bb0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
8bc0: 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 t(argv[0]);. if
8bd0: 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65 74 75 ( zStr==0 ) retu
8be0: 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73 71 6c rn;. nStr = sql
8bf0: 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
8c00: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 (argv[0]);. ass
8c10: 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c 69 74 ert( zStr==sqlit
8c20: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
8c30: 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f gv[0]) ); /* No
8c40: 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 encoding change
8c50: 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e 20 3d */. zPattern =
8c60: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
8c70: 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 ext(argv[1]);.
8c80: 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d 30 20 if( zPattern==0
8c90: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 ){. assert( s
8ca0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
8cb0: 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53 51 4c 49 e(argv[1])==SQLI
8cc0: 54 45 5f 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 TE_NULL.
8cd0: 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 63 || sqlite3_c
8ce0: 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
8cf0: 28 63 6f 6e 74 65 78 74 29 2d 3e 6d 61 6c 6c 6f (context)->mallo
8d00: 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 cFailed );. r
8d10: 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 eturn;. }. if(
8d20: 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d 3d 30 20 zPattern[0]==0
8d30: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 ){. assert( s
8d40: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
8d50: 65 28 61 72 67 76 5b 31 5d 29 21 3d 53 51 4c 49 e(argv[1])!=SQLI
8d60: 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 73 TE_NULL );. s
8d70: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 qlite3_result_va
8d80: 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 lue(context, arg
8d90: 76 5b 30 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 v[0]);. retur
8da0: 6e 3b 0a 20 20 7d 0a 20 20 6e 50 61 74 74 65 72 n;. }. nPatter
8db0: 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 n = sqlite3_valu
8dc0: 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d 29 e_bytes(argv[1])
8dd0: 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 50 61 74 ;. assert( zPat
8de0: 74 65 72 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 tern==sqlite3_va
8df0: 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d lue_text(argv[1]
8e00: 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f ) ); /* No enco
8e10: 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 ding change */.
8e20: 20 7a 52 65 70 20 3d 20 73 71 6c 69 74 65 33 5f zRep = sqlite3_
8e30: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
8e40: 32 5d 29 3b 0a 20 20 69 66 28 20 7a 52 65 70 3d 2]);. if( zRep=
8e50: 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e =0 ) return;. n
8e60: 52 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 Rep = sqlite3_va
8e70: 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 32 lue_bytes(argv[2
8e80: 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 52 ]);. assert( zR
8e90: 65 70 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 ep==sqlite3_valu
8ea0: 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 20 e_text(argv[2])
8eb0: 29 3b 0a 20 20 6e 4f 75 74 20 3d 20 6e 53 74 72 );. nOut = nStr
8ec0: 20 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 + 1;. assert(
8ed0: 6e 4f 75 74 3c 53 51 4c 49 54 45 5f 4d 41 58 5f nOut<SQLITE_MAX_
8ee0: 4c 45 4e 47 54 48 20 29 3b 0a 20 20 7a 4f 75 74 LENGTH );. zOut
8ef0: 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 = contextMalloc
8f00: 28 63 6f 6e 74 65 78 74 2c 20 28 69 36 34 29 6e (context, (i64)n
8f10: 4f 75 74 29 3b 0a 20 20 69 66 28 20 7a 4f 75 74 Out);. if( zOut
8f20: 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
8f30: 6e 3b 0a 20 20 7d 0a 20 20 6c 6f 6f 70 4c 69 6d n;. }. loopLim
8f40: 69 74 20 3d 20 6e 53 74 72 20 2d 20 6e 50 61 74 it = nStr - nPat
8f50: 74 65 72 6e 3b 20 20 0a 20 20 66 6f 72 28 69 3d tern; . for(i=
8f60: 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f 70 4c 69 6d 69 j=0; i<=loopLimi
8f70: 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 t; i++){. if(
8f80: 20 7a 53 74 72 5b 69 5d 21 3d 7a 50 61 74 74 65 zStr[i]!=zPatte
8f90: 72 6e 5b 30 5d 20 7c 7c 20 6d 65 6d 63 6d 70 28 rn[0] || memcmp(
8fa0: 26 7a 53 74 72 5b 69 5d 2c 20 7a 50 61 74 74 65 &zStr[i], zPatte
8fb0: 72 6e 2c 20 6e 50 61 74 74 65 72 6e 29 20 29 7b rn, nPattern) ){
8fc0: 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 2b 2b 5d . zOut[j++]
8fd0: 20 3d 20 7a 53 74 72 5b 69 5d 3b 0a 20 20 20 20 = zStr[i];.
8fe0: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 38 20 }else{. u8
8ff0: 2a 7a 4f 6c 64 3b 0a 20 20 20 20 20 20 73 71 6c *zOld;. sql
9000: 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 ite3 *db = sqlit
9010: 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
9020: 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 ndle(context);.
9030: 20 20 20 20 20 6e 4f 75 74 20 2b 3d 20 6e 52 65 nOut += nRe
9040: 70 20 2d 20 6e 50 61 74 74 65 72 6e 3b 0a 20 20 p - nPattern;.
9050: 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 4f testcase( nO
9060: 75 74 2d 31 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 ut-1==db->aLimit
9070: 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 [SQLITE_LIMIT_LE
9080: 4e 47 54 48 5d 20 29 3b 0a 20 20 20 20 20 20 74 NGTH] );. t
9090: 65 73 74 63 61 73 65 28 20 6e 4f 75 74 2d 32 3d estcase( nOut-2=
90a0: 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 =db->aLimit[SQLI
90b0: 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
90c0: 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f );. if( nO
90d0: 75 74 2d 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b ut-1>db->aLimit[
90e0: 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
90f0: 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 GTH] ){.
9100: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
9110: 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 rror_toobig(cont
9120: 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 ext);. sq
9130: 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f 75 74 29 lite3_free(zOut)
9140: 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
9150: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
9160: 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20 20 20 zOld = zOut;.
9170: 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 zOut = sqlite
9180: 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 74 2c 20 3_realloc(zOut,
9190: 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20 20 20 (int)nOut);.
91a0: 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b if( zOut==0 ){
91b0: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
91c0: 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f _result_error_no
91d0: 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 mem(context);.
91e0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
91f0: 65 65 28 7a 4f 6c 64 29 3b 0a 20 20 20 20 20 20 ee(zOld);.
9200: 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
9210: 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 }. memcpy(&
9220: 7a 4f 75 74 5b 6a 5d 2c 20 7a 52 65 70 2c 20 6e zOut[j], zRep, n
9230: 52 65 70 29 3b 0a 20 20 20 20 20 20 6a 20 2b 3d Rep);. j +=
9240: 20 6e 52 65 70 3b 0a 20 20 20 20 20 20 69 20 2b nRep;. i +
9250: 3d 20 6e 50 61 74 74 65 72 6e 2d 31 3b 0a 20 20 = nPattern-1;.
9260: 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 }. }. assert
9270: 28 20 6a 2b 6e 53 74 72 2d 69 2b 31 3d 3d 6e 4f ( j+nStr-i+1==nO
9280: 75 74 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 ut );. memcpy(&
9290: 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53 74 72 5b 69 zOut[j], &zStr[i
92a0: 5d 2c 20 6e 53 74 72 2d 69 29 3b 0a 20 20 6a 20 ], nStr-i);. j
92b0: 2b 3d 20 6e 53 74 72 20 2d 20 69 3b 0a 20 20 61 += nStr - i;. a
92c0: 73 73 65 72 74 28 20 6a 3c 3d 6e 4f 75 74 20 29 ssert( j<=nOut )
92d0: 3b 0a 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 30 3b ;. zOut[j] = 0;
92e0: 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
92f0: 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
9300: 28 63 68 61 72 2a 29 7a 4f 75 74 2c 20 6a 2c 20 (char*)zOut, j,
9310: 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 7d sqlite3_free);.}
9320: 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
9330: 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 54 52 tation of the TR
9340: 49 4d 28 29 2c 20 4c 54 52 49 4d 28 29 2c 20 61 IM(), LTRIM(), a
9350: 6e 64 20 52 54 52 49 4d 28 29 20 66 75 6e 63 74 nd RTRIM() funct
9360: 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 75 73 65 ions..** The use
9370: 72 64 61 74 61 20 69 73 20 30 78 31 20 66 6f 72 rdata is 0x1 for
9380: 20 6c 65 66 74 20 74 72 69 6d 2c 20 30 78 32 20 left trim, 0x2
9390: 66 6f 72 20 72 69 67 68 74 20 74 72 69 6d 2c 20 for right trim,
93a0: 30 78 33 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0x3 for both..*/
93b0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72 69 .static void tri
93c0: 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 mFunc(. sqlite3
93d0: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
93e0: 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
93f0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
9400: 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 *argv.){. const
9410: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
9420: 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 zIn; /*
9430: 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a Input string */.
9440: 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
9450: 20 63 68 61 72 20 2a 7a 43 68 61 72 53 65 74 3b char *zCharSet;
9460: 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 63 68 /* Set of ch
9470: 61 72 61 63 74 65 72 73 20 74 6f 20 74 72 69 6d aracters to trim
9480: 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 3b 20 20 */. int nIn;
9490: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
94a0: 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
94b0: 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 69 6e r of bytes in in
94c0: 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 put */. int fla
94d0: 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
94e0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a /* 1:
94f0: 20 74 72 69 6d 6c 65 66 74 20 20 32 3a 20 74 72 trimleft 2: tr
9500: 69 6d 72 69 67 68 74 20 20 33 3a 20 74 72 69 6d imright 3: trim
9510: 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 */. int i;
9520: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9530: 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
9540: 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 counter */. uns
9550: 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4c 65 6e igned char *aLen
9560: 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f = 0; /
9570: 2a 20 4c 65 6e 67 74 68 20 6f 66 20 65 61 63 68 * Length of each
9580: 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a 43 character in zC
9590: 68 61 72 53 65 74 20 2a 2f 0a 20 20 75 6e 73 69 harSet */. unsi
95a0: 67 6e 65 64 20 63 68 61 72 20 2a 2a 61 7a 43 68 gned char **azCh
95b0: 61 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a ar = 0; /*
95c0: 20 49 6e 64 69 76 69 64 75 61 6c 20 63 68 61 72 Individual char
95d0: 61 63 74 65 72 73 20 69 6e 20 7a 43 68 61 72 53 acters in zCharS
95e0: 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 et */. int nCha
95f0: 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
9600: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
9610: 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 ber of character
9620: 73 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f s in zCharSet */
9630: 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f .. if( sqlite3_
9640: 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b value_type(argv[
9650: 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0])==SQLITE_NULL
9660: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ){. return;.
9670: 20 20 7d 0a 20 20 7a 49 6e 20 3d 20 73 71 6c 69 }. zIn = sqli
9680: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
9690: 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a rgv[0]);. if( z
96a0: 49 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a In==0 ) return;.
96b0: 20 20 6e 49 6e 20 3d 20 73 71 6c 69 74 65 33 5f nIn = sqlite3_
96c0: 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 value_bytes(argv
96d0: 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 [0]);. assert(
96e0: 7a 49 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c zIn==sqlite3_val
96f0: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
9700: 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d );. if( argc==
9710: 31 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 1 ){. static
9720: 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
9730: 68 61 72 20 6c 65 6e 4f 6e 65 5b 5d 20 3d 20 7b har lenOne[] = {
9740: 20 31 20 7d 3b 0a 20 20 20 20 73 74 61 74 69 63 1 };. static
9750: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
9760: 20 63 6f 6e 73 74 20 61 7a 4f 6e 65 5b 5d 20 3d const azOne[] =
9770: 20 7b 20 28 75 38 2a 29 22 20 22 20 7d 3b 0a 20 { (u8*)" " };.
9780: 20 20 20 6e 43 68 61 72 20 3d 20 31 3b 0a 20 20 nChar = 1;.
9790: 20 20 61 4c 65 6e 20 3d 20 28 75 38 2a 29 6c 65 aLen = (u8*)le
97a0: 6e 4f 6e 65 3b 0a 20 20 20 20 61 7a 43 68 61 72 nOne;. azChar
97b0: 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 = (unsigned cha
97c0: 72 20 2a 2a 29 61 7a 4f 6e 65 3b 0a 20 20 20 20 r **)azOne;.
97d0: 7a 43 68 61 72 53 65 74 20 3d 20 30 3b 0a 20 20 zCharSet = 0;.
97e0: 7d 65 6c 73 65 20 69 66 28 20 28 7a 43 68 61 72 }else if( (zChar
97f0: 53 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 Set = sqlite3_va
9800: 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d lue_text(argv[1]
9810: 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ))==0 ){. ret
9820: 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 urn;. }else{.
9830: 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
9840: 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 66 6f char *z;. fo
9850: 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20 6e 43 r(z=zCharSet, nC
9860: 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 har=0; *z; nChar
9870: 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 ++){. SQLIT
9880: 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a E_SKIP_UTF8(z);.
9890: 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 43 }. if( nC
98a0: 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 20 20 61 har>0 ){. a
98b0: 7a 43 68 61 72 20 3d 20 63 6f 6e 74 65 78 74 4d zChar = contextM
98c0: 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 alloc(context, (
98d0: 28 69 36 34 29 6e 43 68 61 72 29 2a 28 73 69 7a (i64)nChar)*(siz
98e0: 65 6f 66 28 63 68 61 72 2a 29 2b 31 29 29 3b 0a eof(char*)+1));.
98f0: 20 20 20 20 20 20 69 66 28 20 61 7a 43 68 61 72 if( azChar
9900: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. r
9910: 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 eturn;. }.
9920: 20 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 6e 73 aLen = (uns
9930: 69 67 6e 65 64 20 63 68 61 72 2a 29 26 61 7a 43 igned char*)&azC
9940: 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 20 20 20 har[nChar];.
9950: 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74 for(z=zCharSet
9960: 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e , nChar=0; *z; n
9970: 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20 20 Char++){.
9980: 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 20 3d azChar[nChar] =
9990: 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 (unsigned char
99a0: 2a 29 7a 3b 0a 20 20 20 20 20 20 20 20 53 51 4c *)z;. SQL
99b0: 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 ITE_SKIP_UTF8(z)
99c0: 3b 0a 20 20 20 20 20 20 20 20 61 4c 65 6e 5b 6e ;. aLen[n
99d0: 43 68 61 72 5d 20 3d 20 28 75 38 29 28 7a 20 2d Char] = (u8)(z -
99e0: 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 29 3b azChar[nChar]);
99f0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
9a00: 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 72 3e 30 }. if( nChar>0
9a10: 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 ){. flags =
9a20: 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e SQLITE_PTR_TO_IN
9a30: 54 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 T(sqlite3_user_d
9a40: 61 74 61 28 63 6f 6e 74 65 78 74 29 29 3b 0a 20 ata(context));.
9a50: 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 31 if( flags & 1
9a60: 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 ){. while(
9a70: 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 nIn>0 ){.
9a80: 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 int len = 0;.
9a90: 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 for(i=0;
9aa0: 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 i<nChar; i++){.
9ab0: 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61 len = a
9ac0: 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 Len[i];.
9ad0: 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20 26 if( len<=nIn &
9ae0: 26 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20 61 7a & memcmp(zIn, az
9af0: 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d 3d 30 Char[i], len)==0
9b00: 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 ) break;.
9b10: 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
9b20: 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 6b i>=nChar ) break
9b30: 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e 20 2b 3d ;. zIn +=
9b40: 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6e 49 len;. nI
9b50: 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 n -= len;.
9b60: 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
9b70: 66 6c 61 67 73 20 26 20 32 20 29 7b 0a 20 20 20 flags & 2 ){.
9b80: 20 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30 20 while( nIn>0
9b90: 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c ){. int l
9ba0: 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 en = 0;.
9bb0: 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 for(i=0; i<nChar
9bc0: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
9bd0: 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b len = aLen[i];
9be0: 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c . if( l
9bf0: 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d en<=nIn && memcm
9c00: 70 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c p(&zIn[nIn-len],
9c10: 61 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d 3d azChar[i],len)==
9c20: 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 0 ) break;.
9c30: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
9c40: 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 i>=nChar ) brea
9c50: 6b 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d k;. nIn -
9c60: 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 = len;. }.
9c70: 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 43 68 }. if( zCh
9c80: 61 72 53 65 74 20 29 7b 0a 20 20 20 20 20 20 73 arSet ){. s
9c90: 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 68 qlite3_free(azCh
9ca0: 61 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ar);. }. }.
9cb0: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
9cc0: 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 text(context, (c
9cd0: 68 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53 har*)zIn, nIn, S
9ce0: 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
9cf0: 3b 0a 7d 0a 0a 0a 2f 2a 20 49 4d 50 3a 20 52 2d ;.}.../* IMP: R-
9d00: 32 35 33 36 31 2d 31 36 31 35 30 20 54 68 69 73 25361-16150 This
9d10: 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6d 69 function is omi
9d20: 74 74 65 64 20 66 72 6f 6d 20 53 51 4c 69 74 65 tted from SQLite
9d30: 20 62 79 20 64 65 66 61 75 6c 74 2e 20 49 74 0a by default. It.
9d40: 2a 2a 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c ** is only avail
9d50: 61 62 6c 65 20 69 66 20 74 68 65 20 53 51 4c 49 able if the SQLI
9d60: 54 45 5f 53 4f 55 4e 44 45 58 20 63 6f 6d 70 69 TE_SOUNDEX compi
9d70: 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 69 le-time option i
9d80: 73 20 75 73 65 64 0a 2a 2a 20 77 68 65 6e 20 53 s used.** when S
9d90: 51 4c 69 74 65 20 69 73 20 62 75 69 6c 74 2e 0a QLite is built..
9da0: 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
9db0: 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a 20 43 _SOUNDEX./*.** C
9dc0: 6f 6d 70 75 74 65 20 74 68 65 20 73 6f 75 6e 64 ompute the sound
9dd0: 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 ex encoding of a
9de0: 20 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 word..**.** IMP
9df0: 3a 20 52 2d 35 39 37 38 32 2d 30 30 30 37 32 20 : R-59782-00072
9e00: 54 68 65 20 73 6f 75 6e 64 65 78 28 58 29 20 66 The soundex(X) f
9e10: 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
9e20: 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 a string that is
9e30: 20 74 68 65 0a 2a 2a 20 73 6f 75 6e 64 65 78 20 the.** soundex
9e40: 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 encoding of the
9e50: 73 74 72 69 6e 67 20 58 2e 20 0a 2a 2f 0a 73 74 string X. .*/.st
9e60: 61 74 69 63 20 76 6f 69 64 20 73 6f 75 6e 64 65 atic void sounde
9e70: 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 xFunc(. sqlite3
9e80: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
9e90: 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
9ea0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
9eb0: 2a 61 72 67 76 0a 29 7b 0a 20 20 63 68 61 72 20 *argv.){. char
9ec0: 7a 52 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f zResult[8];. co
9ed0: 6e 73 74 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69 nst u8 *zIn;. i
9ee0: 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69 nt i, j;. stati
9ef0: 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
9f00: 20 63 68 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20 char iCode[] =
9f10: 7b 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 {. 0, 0, 0, 0
9f20: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
9f30: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
9f40: 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 0, 0,. 0, 0,
9f50: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
9f60: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
9f70: 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 0, 0, 0,. 0,
9f80: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
9f90: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
9fa0: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 , 0, 0, 0, 0,.
9fb0: 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0, 0, 0, 0, 0,
9fc0: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
9fd0: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
9fe0: 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 ,. 0, 0, 1, 2
9ff0: 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c , 3, 0, 1, 2, 0,
a000: 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 0, 2, 2, 4, 5,
a010: 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 5, 0,. 1, 2,
a020: 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 6, 2, 3, 0, 1, 0
a030: 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c , 2, 0, 2, 0, 0,
a040: 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 0, 0, 0,. 0,
a050: 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 0, 1, 2, 3, 0,
a060: 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 1, 2, 0, 0, 2, 2
a070: 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 , 4, 5, 5, 0,.
a080: 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 1, 2, 6, 2, 3,
a090: 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 0, 1, 0, 2, 0,
a0a0: 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2, 0, 0, 0, 0, 0
a0b0: 2c 0a 20 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 ,. };. assert(
a0c0: 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49 argc==1 );. zI
a0d0: 6e 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 n = (u8*)sqlite3
a0e0: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
a0f0: 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d [0]);. if( zIn=
a100: 3d 30 20 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29 =0 ) zIn = (u8*)
a110: 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a "";. for(i=0; z
a120: 49 6e 5b 69 5d 20 26 26 20 21 73 71 6c 69 74 65 In[i] && !sqlite
a130: 33 49 73 61 6c 70 68 61 28 7a 49 6e 5b 69 5d 29 3Isalpha(zIn[i])
a140: 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 7a ; i++){}. if( z
a150: 49 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75 38 20 In[i] ){. u8
a160: 70 72 65 76 63 6f 64 65 20 3d 20 69 43 6f 64 65 prevcode = iCode
a170: 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 [zIn[i]&0x7f];.
a180: 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 zResult[0] =
a190: 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 7a sqlite3Toupper(z
a1a0: 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28 In[i]);. for(
a1b0: 6a 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b j=1; j<4 && zIn[
a1c0: 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 i]; i++){.
a1d0: 69 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 65 int code = iCode
a1e0: 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 [zIn[i]&0x7f];.
a1f0: 20 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 20 if( code>0
a200: 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 ){. if( c
a210: 6f 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 7b ode!=prevcode ){
a220: 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 76 63 . prevc
a230: 6f 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20 ode = code;.
a240: 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b zResult[j+
a250: 2b 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b +] = code + '0';
a260: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a270: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a280: 70 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20 prevcode = 0;.
a290: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
a2a0: 77 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20 while( j<4 ){.
a2b0: 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d zResult[j++]
a2c0: 20 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20 = '0';. }.
a2d0: 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30 zResult[j] = 0
a2e0: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
a2f0: 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
a300: 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53 t, zResult, 4, S
a310: 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
a320: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
a330: 2a 20 49 4d 50 3a 20 52 2d 36 34 38 39 34 2d 35 * IMP: R-64894-5
a340: 30 33 32 31 20 54 68 65 20 73 74 72 69 6e 67 20 0321 The string
a350: 22 3f 30 30 30 22 20 69 73 20 72 65 74 75 72 6e "?000" is return
a360: 65 64 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 ed if the argume
a370: 6e 74 0a 20 20 20 20 2a 2a 20 69 73 20 4e 55 4c nt. ** is NUL
a380: 4c 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f L or contains no
a390: 20 41 53 43 49 49 20 61 6c 70 68 61 62 65 74 69 ASCII alphabeti
a3a0: 63 20 63 68 61 72 61 63 74 65 72 73 2e 20 2a 2f c characters. */
a3b0: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 . sqlite3_res
a3c0: 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
a3d0: 2c 20 22 3f 30 30 30 22 2c 20 34 2c 20 53 51 4c , "?000", 4, SQL
a3e0: 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d ITE_STATIC);. }
a3f0: 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
a400: 49 54 45 5f 53 4f 55 4e 44 45 58 20 2a 2f 0a 0a ITE_SOUNDEX */..
a410: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
a420: 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
a430: 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 74 ON./*.** A funct
a440: 69 6f 6e 20 74 68 61 74 20 6c 6f 61 64 73 20 61 ion that loads a
a450: 20 73 68 61 72 65 64 2d 6c 69 62 72 61 72 79 20 shared-library
a460: 65 78 74 65 6e 73 69 6f 6e 20 74 68 65 6e 20 72 extension then r
a470: 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a eturns NULL..*/.
a480: 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 64 static void load
a490: 45 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 Ext(sqlite3_cont
a4a0: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e ext *context, in
a4b0: 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f t argc, sqlite3_
a4c0: 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 value **argv){.
a4d0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
a4e0: 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 le = (const char
a4f0: 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *)sqlite3_value
a500: 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a _text(argv[0]);.
a510: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
a520: 72 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a roc;. sqlite3 *
a530: 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
a540: 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
a550: 6f 6e 74 65 78 74 29 3b 0a 20 20 63 68 61 72 20 ontext);. char
a560: 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 *zErrMsg = 0;..
a570: 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a if( argc==2 ){.
a580: 20 20 20 20 7a 50 72 6f 63 20 3d 20 28 63 6f 6e zProc = (con
a590: 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 st char *)sqlite
a5a0: 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
a5b0: 76 5b 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a v[1]);. }else{.
a5c0: 20 20 20 20 7a 50 72 6f 63 20 3d 20 30 3b 0a 20 zProc = 0;.
a5d0: 20 7d 0a 20 20 69 66 28 20 7a 46 69 6c 65 20 26 }. if( zFile &
a5e0: 26 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 & sqlite3_load_e
a5f0: 78 74 65 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69 xtension(db, zFi
a600: 6c 65 2c 20 7a 50 72 6f 63 2c 20 26 7a 45 72 72 le, zProc, &zErr
a610: 4d 73 67 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 Msg) ){. sqli
a620: 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
a630: 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 4d 73 (context, zErrMs
a640: 67 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 g, -1);. sqli
a650: 74 65 33 5f 66 72 65 65 28 7a 45 72 72 4d 73 67 te3_free(zErrMsg
a660: 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a );. }.}.#endif.
a670: 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
a680: 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
a690: 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 wing structure h
a6a0: 6f 6c 64 73 20 74 68 65 20 63 6f 6e 74 65 78 74 olds the context
a6b0: 20 6f 66 20 61 0a 2a 2a 20 73 75 6d 28 29 20 6f of a.** sum() o
a6c0: 72 20 61 76 67 28 29 20 61 67 67 72 65 67 61 74 r avg() aggregat
a6d0: 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 2e 0a 2a e computation..*
a6e0: 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
a6f0: 20 53 75 6d 43 74 78 20 53 75 6d 43 74 78 3b 0a SumCtx SumCtx;.
a700: 73 74 72 75 63 74 20 53 75 6d 43 74 78 20 7b 0a struct SumCtx {.
a710: 20 20 64 6f 75 62 6c 65 20 72 53 75 6d 3b 20 20 double rSum;
a720: 20 20 20 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 /* Floating
a730: 70 6f 69 6e 74 20 73 75 6d 20 2a 2f 0a 20 20 69 point sum */. i
a740: 36 34 20 69 53 75 6d 3b 20 20 20 20 20 20 20 20 64 iSum;
a750: 20 2f 2a 20 49 6e 74 65 67 65 72 20 73 75 6d 20 /* Integer sum
a760: 2a 2f 20 20 20 0a 20 20 69 36 34 20 63 6e 74 3b */ . i64 cnt;
a770: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
a780: 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
a790: 73 75 6d 6d 65 64 20 2a 2f 0a 20 20 75 38 20 6f summed */. u8 o
a7a0: 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 2f 2a verflow; /*
a7b0: 20 54 72 75 65 20 69 66 20 69 6e 74 65 67 65 72 True if integer
a7c0: 20 6f 76 65 72 66 6c 6f 77 20 73 65 65 6e 20 2a overflow seen *
a7d0: 2f 0a 20 20 75 38 20 61 70 70 72 6f 78 3b 20 20 /. u8 approx;
a7e0: 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a7f0: 20 6e 6f 6e 2d 69 6e 74 65 67 65 72 20 76 61 6c non-integer val
a800: 75 65 20 77 61 73 20 69 6e 70 75 74 20 74 6f 20 ue was input to
a810: 74 68 65 20 73 75 6d 20 2a 2f 0a 7d 3b 0a 0a 2f the sum */.};../
a820: 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 *.** Routines us
a830: 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 ed to compute th
a840: 65 20 73 75 6d 2c 20 61 76 65 72 61 67 65 2c 20 e sum, average,
a850: 61 6e 64 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a and total..**.**
a860: 20 54 68 65 20 53 55 4d 28 29 20 66 75 6e 63 74 The SUM() funct
a870: 69 6f 6e 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 ion follows the
a880: 28 62 72 6f 6b 65 6e 29 20 53 51 4c 20 73 74 61 (broken) SQL sta
a890: 6e 64 61 72 64 20 77 68 69 63 68 20 6d 65 61 6e ndard which mean
a8a0: 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 72 65 74 s.** that it ret
a8b0: 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69 74 20 urns NULL if it
a8c0: 73 75 6d 73 20 6f 76 65 72 20 6e 6f 20 69 6e 70 sums over no inp
a8d0: 75 74 73 2e 20 20 54 4f 54 41 4c 20 72 65 74 75 uts. TOTAL retu
a8e0: 72 6e 73 0a 2a 2a 20 30 2e 30 20 69 6e 20 74 68 rns.** 0.0 in th
a8f0: 61 74 20 63 61 73 65 2e 20 20 49 6e 20 61 64 64 at case. In add
a900: 69 74 69 6f 6e 2c 20 54 4f 54 41 4c 20 61 6c 77 ition, TOTAL alw
a910: 61 79 73 20 72 65 74 75 72 6e 73 20 61 20 66 6c ays returns a fl
a920: 6f 61 74 20 77 68 65 72 65 0a 2a 2a 20 53 55 4d oat where.** SUM
a930: 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 61 6e might return an
a940: 20 69 6e 74 65 67 65 72 20 69 66 20 69 74 20 6e integer if it n
a950: 65 76 65 72 20 65 6e 63 6f 75 6e 74 65 72 73 20 ever encounters
a960: 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 a floating point
a970: 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 54 4f 54 41 .** value. TOTA
a980: 4c 20 6e 65 76 65 72 20 66 61 69 6c 73 2c 20 62 L never fails, b
a990: 75 74 20 53 55 4d 20 6d 69 67 68 74 20 74 68 72 ut SUM might thr
a9a0: 6f 75 67 68 20 61 6e 20 65 78 63 65 70 74 69 6f ough an exceptio
a9b0: 6e 20 69 66 0a 2a 2a 20 69 74 20 6f 76 65 72 66 n if.** it overf
a9c0: 6c 6f 77 73 20 61 6e 20 69 6e 74 65 67 65 72 2e lows an integer.
a9d0: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
a9e0: 73 75 6d 53 74 65 70 28 73 71 6c 69 74 65 33 5f sumStep(sqlite3_
a9f0: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
aa00: 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 , int argc, sqli
aa10: 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
aa20: 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a ){. SumCtx *p;.
aa30: 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 61 73 int type;. as
aa40: 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b sert( argc==1 );
aa50: 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
aa60: 54 45 52 28 61 72 67 63 29 3b 0a 20 20 70 20 3d TER(argc);. p =
aa70: 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 sqlite3_aggrega
aa80: 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 te_context(conte
aa90: 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b xt, sizeof(*p));
aaa0: 0a 20 20 74 79 70 65 20 3d 20 73 71 6c 69 74 65 . type = sqlite
aab0: 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 3_value_numeric_
aac0: 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 type(argv[0]);.
aad0: 20 69 66 28 20 70 20 26 26 20 74 79 70 65 21 3d if( p && type!=
aae0: 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 SQLITE_NULL ){.
aaf0: 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20 p->cnt++;.
ab00: 20 69 66 28 20 74 79 70 65 3d 3d 53 51 4c 49 54 if( type==SQLIT
ab10: 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 E_INTEGER ){.
ab20: 20 20 20 69 36 34 20 76 20 3d 20 73 71 6c 69 74 i64 v = sqlit
ab30: 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 e3_value_int64(a
ab40: 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70 rgv[0]);. p
ab50: 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b 0a 20 20 20 ->rSum += v;.
ab60: 20 20 20 69 66 28 20 28 70 2d 3e 61 70 70 72 6f if( (p->appro
ab70: 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f 77 29 3d 3d x|p->overflow)==
ab80: 30 20 26 26 20 73 71 6c 69 74 65 33 41 64 64 49 0 && sqlite3AddI
ab90: 6e 74 36 34 28 26 70 2d 3e 69 53 75 6d 2c 20 76 nt64(&p->iSum, v
aba0: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e ) ){. p->
abb0: 6f 76 65 72 66 6c 6f 77 20 3d 20 31 3b 0a 20 20 overflow = 1;.
abc0: 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
abd0: 0a 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b . p->rSum +
abe0: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
abf0: 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b double(argv[0]);
ac00: 0a 20 20 20 20 20 20 70 2d 3e 61 70 70 72 6f 78 . p->approx
ac10: 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 1;. }. }.
ac20: 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 }.static void su
ac30: 6d 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 mFinalize(sqlite
ac40: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
ac50: 78 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 xt){. SumCtx *p
ac60: 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f ;. p = sqlite3_
ac70: 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
ac80: 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 t(context, 0);.
ac90: 20 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 if( p && p->cnt
aca0: 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d >0 ){. if( p-
acb0: 3e 6f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 >overflow ){.
acc0: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
acd0: 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c t_error(context,
ace0: 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f "integer overflo
acf0: 77 22 2c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c 73 w",-1);. }els
ad00: 65 20 69 66 28 20 70 2d 3e 61 70 70 72 6f 78 20 e if( p->approx
ad10: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
ad20: 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 _result_double(c
ad30: 6f 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 29 ontext, p->rSum)
ad40: 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
ad50: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
ad60: 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c t_int64(context,
ad70: 20 70 2d 3e 69 53 75 6d 29 3b 0a 20 20 20 20 7d p->iSum);. }
ad80: 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f . }.}.static vo
ad90: 69 64 20 61 76 67 46 69 6e 61 6c 69 7a 65 28 73 id avgFinalize(s
ada0: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
adb0: 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 context){. SumC
adc0: 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c tx *p;. p = sql
add0: 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
ade0: 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 ontext(context,
adf0: 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 0);. if( p && p
ae00: 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 73 ->cnt>0 ){. s
ae10: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f qlite3_result_do
ae20: 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d uble(context, p-
ae30: 3e 72 53 75 6d 2f 28 64 6f 75 62 6c 65 29 70 2d >rSum/(double)p-
ae40: 3e 63 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 >cnt);. }.}.sta
ae50: 74 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 46 69 tic void totalFi
ae60: 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 nalize(sqlite3_c
ae70: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 ontext *context)
ae80: 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 {. SumCtx *p;.
ae90: 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 p = sqlite3_agg
aea0: 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 regate_context(c
aeb0: 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 2f 2a ontext, 0);. /*
aec0: 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 (double)0 In ca
aed0: 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 se of SQLITE_OMI
aee0: 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
aef0: 2e 2e 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ... */. sqlite3
af00: 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 _result_double(c
af10: 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d 3e 72 ontext, p ? p->r
af20: 53 75 6d 20 3a 20 28 64 6f 75 62 6c 65 29 30 29 Sum : (double)0)
af30: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 ;.}../*.** The f
af40: 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
af50: 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f re keeps track o
af60: 66 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 f state informat
af70: 69 6f 6e 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 ion for the.** c
af80: 6f 75 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 ount() aggregate
af90: 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 function..*/.ty
afa0: 70 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 75 pedef struct Cou
afb0: 6e 74 43 74 78 20 43 6f 75 6e 74 43 74 78 3b 0a ntCtx CountCtx;.
afc0: 73 74 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20 struct CountCtx
afd0: 7b 0a 20 20 69 36 34 20 6e 3b 0a 7d 3b 0a 0a 2f {. i64 n;.};../
afe0: 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f *.** Routines to
aff0: 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 implement the c
b000: 6f 75 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 ount() aggregate
b010: 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
b020: 61 74 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 53 atic void countS
b030: 74 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 tep(sqlite3_cont
b040: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e ext *context, in
b050: 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f t argc, sqlite3_
b060: 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 value **argv){.
b070: 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20 CountCtx *p;.
b080: 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 p = sqlite3_aggr
b090: 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f egate_context(co
b0a0: 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 ntext, sizeof(*p
b0b0: 29 29 3b 0a 20 20 69 66 28 20 28 61 72 67 63 3d ));. if( (argc=
b0c0: 3d 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55 4c =0 || SQLITE_NUL
b0d0: 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 L!=sqlite3_value
b0e0: 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 20 _type(argv[0]))
b0f0: 26 26 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e && p ){. p->n
b100: 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 ++;. }..#ifndef
b110: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 SQLITE_OMIT_DEP
b120: 52 45 43 41 54 45 44 0a 20 20 2f 2a 20 54 68 65 RECATED. /* The
b130: 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 sqlite3_aggrega
b140: 74 65 5f 63 6f 75 6e 74 28 29 20 66 75 6e 63 74 te_count() funct
b150: 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 ion is deprecate
b160: 64 2e 20 20 42 75 74 20 6a 75 73 74 20 74 6f 20 d. But just to
b170: 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 69 make. ** sure i
b180: 74 20 73 74 69 6c 6c 20 6f 70 65 72 61 74 65 73 t still operates
b190: 20 63 6f 72 72 65 63 74 6c 79 2c 20 76 65 72 69 correctly, veri
b1a0: 66 79 20 74 68 61 74 20 69 74 73 20 63 6f 75 6e fy that its coun
b1b0: 74 20 61 67 72 65 65 73 20 77 69 74 68 20 6f 75 t agrees with ou
b1c0: 72 20 0a 20 20 2a 2a 20 69 6e 74 65 72 6e 61 6c r . ** internal
b1d0: 20 63 6f 75 6e 74 20 77 68 65 6e 20 75 73 69 6e count when usin
b1e0: 67 20 63 6f 75 6e 74 28 2a 29 20 61 6e 64 20 77 g count(*) and w
b1f0: 68 65 6e 20 74 68 65 20 74 6f 74 61 6c 20 63 6f hen the total co
b200: 75 6e 74 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 unt can be. **
b210: 65 78 70 72 65 73 73 65 64 20 61 73 20 61 20 33 expressed as a 3
b220: 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 20 2a 2-bit integer. *
b230: 2f 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 /. assert( argc
b240: 3d 3d 31 20 7c 7c 20 70 3d 3d 30 20 7c 7c 20 70 ==1 || p==0 || p
b250: 2d 3e 6e 3e 30 78 37 66 66 66 66 66 66 66 0a 20 ->n>0x7fffffff.
b260: 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6e || p->n
b270: 3d 3d 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 ==sqlite3_aggreg
b280: 61 74 65 5f 63 6f 75 6e 74 28 63 6f 6e 74 65 78 ate_count(contex
b290: 74 29 20 29 3b 0a 23 65 6e 64 69 66 0a 7d 20 20 t) );.#endif.}
b2a0: 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f .static void co
b2b0: 75 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 untFinalize(sqli
b2c0: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
b2d0: 74 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74 43 74 text){. CountCt
b2e0: 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 x *p;. p = sqli
b2f0: 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
b300: 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 ntext(context, 0
b310: 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 );. sqlite3_res
b320: 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 ult_int64(contex
b330: 74 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 30 29 t, p ? p->n : 0)
b340: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 ;.}../*.** Routi
b350: 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 nes to implement
b360: 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 min() and max()
b370: 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
b380: 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ions..*/.static
b390: 76 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 70 28 void minmaxStep(
b3a0: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
b3b0: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 xt *context, .
b3c0: 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20 20 int NotUsed, .
b3d0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
b3e0: 61 72 67 76 0a 29 7b 0a 20 20 4d 65 6d 20 2a 70 argv.){. Mem *p
b3f0: 41 72 67 20 20 3d 20 28 4d 65 6d 20 2a 29 61 72 Arg = (Mem *)ar
b400: 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d 20 2a 70 42 gv[0];. Mem *pB
b410: 65 73 74 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 est;. UNUSED_PA
b420: 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
b430: 3b 0a 0a 20 20 70 42 65 73 74 20 3d 20 28 4d 65 ;.. pBest = (Me
b440: 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72 m *)sqlite3_aggr
b450: 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f egate_context(co
b460: 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 ntext, sizeof(*p
b470: 42 65 73 74 29 29 3b 0a 20 20 69 66 28 20 21 70 Best));. if( !p
b480: 42 65 73 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a Best ) return;..
b490: 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 if( sqlite3_va
b4a0: 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d lue_type(argv[0]
b4b0: 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 )==SQLITE_NULL )
b4c0: 7b 0a 20 20 20 20 69 66 28 20 70 42 65 73 74 2d {. if( pBest-
b4d0: 3e 66 6c 61 67 73 20 29 20 73 71 6c 69 74 65 33 >flags ) sqlite3
b4e0: 53 6b 69 70 41 63 63 75 6d 75 6c 61 74 6f 72 4c SkipAccumulatorL
b4f0: 6f 61 64 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 oad(context);.
b500: 7d 65 6c 73 65 20 69 66 28 20 70 42 65 73 74 2d }else if( pBest-
b510: 3e 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 6e >flags ){. in
b520: 74 20 6d 61 78 3b 0a 20 20 20 20 69 6e 74 20 63 t max;. int c
b530: 6d 70 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 mp;. CollSeq
b540: 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 *pColl = sqlite3
b550: 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 GetFuncCollSeq(c
b560: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 2f 2a 20 ontext);. /*
b570: 54 68 69 73 20 73 74 65 70 20 66 75 6e 63 74 69 This step functi
b580: 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 20 62 on is used for b
b590: 6f 74 68 20 74 68 65 20 6d 69 6e 28 29 20 61 6e oth the min() an
b5a0: 64 20 6d 61 78 28 29 20 61 67 67 72 65 67 61 74 d max() aggregat
b5b0: 65 73 2c 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f es,. ** the o
b5c0: 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 62 nly difference b
b5d0: 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 62 etween the two b
b5e0: 65 69 6e 67 20 74 68 61 74 20 74 68 65 20 73 65 eing that the se
b5f0: 6e 73 65 20 6f 66 20 74 68 65 0a 20 20 20 20 2a nse of the. *
b600: 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 * comparison is
b610: 69 6e 76 65 72 74 65 64 2e 20 46 6f 72 20 74 68 inverted. For th
b620: 65 20 6d 61 78 28 29 20 61 67 67 72 65 67 61 74 e max() aggregat
b630: 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 71 e, the. ** sq
b640: 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 lite3_user_data(
b650: 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 ) function retur
b660: 6e 73 20 28 76 6f 69 64 20 2a 29 2d 31 2e 20 46 ns (void *)-1. F
b670: 6f 72 20 6d 69 6e 28 29 20 69 74 0a 20 20 20 20 or min() it.
b680: 2a 2a 20 72 65 74 75 72 6e 73 20 28 76 6f 69 64 ** returns (void
b690: 20 2a 29 64 62 2c 20 77 68 65 72 65 20 64 62 20 *)db, where db
b6a0: 69 73 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 is the sqlite3*
b6b0: 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74 65 72 database pointer
b6c0: 2e 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 66 6f .. ** Therefo
b6d0: 72 65 20 74 68 65 20 6e 65 78 74 20 73 74 61 74 re the next stat
b6e0: 65 6d 65 6e 74 20 73 65 74 73 20 76 61 72 69 61 ement sets varia
b6f0: 62 6c 65 20 27 6d 61 78 27 20 74 6f 20 31 20 66 ble 'max' to 1 f
b700: 6f 72 20 74 68 65 20 6d 61 78 28 29 0a 20 20 20 or the max().
b710: 20 2a 2a 20 61 67 67 72 65 67 61 74 65 2c 20 6f ** aggregate, o
b720: 72 20 30 20 66 6f 72 20 6d 69 6e 28 29 2e 0a 20 r 0 for min()..
b730: 20 20 20 2a 2f 0a 20 20 20 20 6d 61 78 20 3d 20 */. max =
b740: 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
b750: 61 28 63 6f 6e 74 65 78 74 29 21 3d 30 3b 0a 20 a(context)!=0;.
b760: 20 20 20 63 6d 70 20 3d 20 73 71 6c 69 74 65 33 cmp = sqlite3
b770: 4d 65 6d 43 6f 6d 70 61 72 65 28 70 42 65 73 74 MemCompare(pBest
b780: 2c 20 70 41 72 67 2c 20 70 43 6f 6c 6c 29 3b 0a , pArg, pColl);.
b790: 20 20 20 20 69 66 28 20 28 6d 61 78 20 26 26 20 if( (max &&
b7a0: 63 6d 70 3c 30 29 20 7c 7c 20 28 21 6d 61 78 20 cmp<0) || (!max
b7b0: 26 26 20 63 6d 70 3e 30 29 20 29 7b 0a 20 20 20 && cmp>0) ){.
b7c0: 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
b7d0: 6d 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41 72 mCopy(pBest, pAr
b7e0: 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 g);. }else{.
b7f0: 20 20 20 20 20 73 71 6c 69 74 65 33 53 6b 69 70 sqlite3Skip
b800: 41 63 63 75 6d 75 6c 61 74 6f 72 4c 6f 61 64 28 AccumulatorLoad(
b810: 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a context);. }.
b820: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
b830: 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 ite3VdbeMemCopy(
b840: 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 pBest, pArg);.
b850: 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 }.}.static void
b860: 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 73 minMaxFinalize(s
b870: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
b880: 63 6f 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c 69 context){. sqli
b890: 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 73 3b te3_value *pRes;
b8a0: 0a 20 20 70 52 65 73 20 3d 20 28 73 71 6c 69 74 . pRes = (sqlit
b8b0: 65 33 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69 74 e3_value *)sqlit
b8c0: 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e e3_aggregate_con
b8d0: 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 text(context, 0)
b8e0: 3b 0a 20 20 69 66 28 20 70 52 65 73 20 29 7b 0a ;. if( pRes ){.
b8f0: 20 20 20 20 69 66 28 20 70 52 65 73 2d 3e 66 6c if( pRes->fl
b900: 61 67 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ags ){. sql
b910: 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 ite3_result_valu
b920: 65 28 63 6f 6e 74 65 78 74 2c 20 70 52 65 73 29 e(context, pRes)
b930: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
b940: 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
b950: 65 28 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a 0a e(pRes);. }.}..
b960: 2f 2a 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e 63 /*.** group_conc
b970: 61 74 28 45 58 50 52 2c 20 3f 53 45 50 41 52 41 at(EXPR, ?SEPARA
b980: 54 4f 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63 20 TOR?).*/.static
b990: 76 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 void groupConcat
b9a0: 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 5f Step(. sqlite3_
b9b0: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
b9c0: 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
b9d0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
b9e0: 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 argv.){. const
b9f0: 63 68 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53 74 char *zVal;. St
ba00: 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a rAccum *pAccum;.
ba10: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 const char *zS
ba20: 65 70 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 ep;. int nVal,
ba30: 6e 53 65 70 3b 0a 20 20 61 73 73 65 72 74 28 20 nSep;. assert(
ba40: 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d argc==1 || argc=
ba50: 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 =2 );. if( sqli
ba60: 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 te3_value_type(a
ba70: 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f rgv[0])==SQLITE_
ba80: 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 NULL ) return;.
ba90: 20 70 41 63 63 75 6d 20 3d 20 28 53 74 72 41 63 pAccum = (StrAc
baa0: 63 75 6d 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 cum*)sqlite3_agg
bab0: 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 regate_context(c
bac0: 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a ontext, sizeof(*
bad0: 70 41 63 63 75 6d 29 29 3b 0a 0a 20 20 69 66 28 pAccum));.. if(
bae0: 20 70 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 73 pAccum ){. s
baf0: 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c qlite3 *db = sql
bb00: 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f ite3_context_db_
bb10: 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b handle(context);
bb20: 0a 20 20 20 20 69 6e 74 20 66 69 72 73 74 54 65 . int firstTe
bb30: 72 6d 20 3d 20 70 41 63 63 75 6d 2d 3e 75 73 65 rm = pAccum->use
bb40: 4d 61 6c 6c 6f 63 3d 3d 30 3b 0a 20 20 20 20 70 Malloc==0;. p
bb50: 41 63 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c 6f 63 Accum->useMalloc
bb60: 20 3d 20 32 3b 0a 20 20 20 20 70 41 63 63 75 6d = 2;. pAccum
bb70: 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 64 62 2d 3e ->mxAlloc = db->
bb80: 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
bb90: 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 MIT_LENGTH];.
bba0: 20 69 66 28 20 21 66 69 72 73 74 54 65 72 6d 20 if( !firstTerm
bbb0: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 ){. if( arg
bbc0: 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 c==2 ){.
bbd0: 7a 53 65 70 20 3d 20 28 63 68 61 72 2a 29 73 71 zSep = (char*)sq
bbe0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
bbf0: 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 (argv[1]);.
bc00: 20 20 20 6e 53 65 70 20 3d 20 73 71 6c 69 74 65 nSep = sqlite
bc10: 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 3_value_bytes(ar
bc20: 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 65 gv[1]);. }e
bc30: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 53 65 lse{. zSe
bc40: 70 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20 20 p = ",";.
bc50: 20 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 20 20 nSep = 1;.
bc60: 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
bc70: 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 StrAccumAppend(p
bc80: 41 63 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 65 Accum, zSep, nSe
bc90: 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 56 p);. }. zV
bca0: 61 6c 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 al = (char*)sqli
bcb0: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
bcc0: 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 56 61 rgv[0]);. nVa
bcd0: 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
bce0: 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 e_bytes(argv[0])
bcf0: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 ;. sqlite3Str
bd00: 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 AccumAppend(pAcc
bd10: 75 6d 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 3b um, zVal, nVal);
bd20: 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f . }.}.static vo
bd30: 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 id groupConcatFi
bd40: 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 nalize(sqlite3_c
bd50: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 ontext *context)
bd60: 7b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 {. StrAccum *pA
bd70: 63 63 75 6d 3b 0a 20 20 70 41 63 63 75 6d 20 3d ccum;. pAccum =
bd80: 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 sqlite3_aggrega
bd90: 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 te_context(conte
bda0: 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 41 xt, 0);. if( pA
bdb0: 63 63 75 6d 20 29 7b 0a 20 20 20 20 69 66 28 20 ccum ){. if(
bdc0: 70 41 63 63 75 6d 2d 3e 74 6f 6f 42 69 67 20 29 pAccum->tooBig )
bdd0: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
bde0: 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f result_error_too
bdf0: 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 big(context);.
be00: 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 63 63 }else if( pAcc
be10: 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 um->mallocFailed
be20: 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
be30: 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 3_result_error_n
be40: 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 omem(context);.
be50: 20 20 20 7d 65 6c 73 65 7b 20 20 20 20 0a 20 20 }else{ .
be60: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
be70: 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
be80: 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
be90: 46 69 6e 69 73 68 28 70 41 63 63 75 6d 29 2c 20 Finish(pAccum),
bea0: 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 -1, .
beb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
bec0: 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 qlite3_free);.
bed0: 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
bee0: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f This routine do
bef0: 65 73 20 70 65 72 2d 63 6f 6e 6e 65 63 74 69 6f es per-connectio
bf00: 6e 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 n function regis
bf10: 74 72 61 74 69 6f 6e 2e 20 20 4d 6f 73 74 0a 2a tration. Most.*
bf20: 2a 20 6f 66 20 74 68 65 20 62 75 69 6c 74 2d 69 * of the built-i
bf30: 6e 20 66 75 6e 63 74 69 6f 6e 73 20 61 62 6f 76 n functions abov
bf40: 65 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 e are part of th
bf50: 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f e global functio
bf60: 6e 20 73 65 74 2e 0a 2a 2a 20 54 68 69 73 20 72 n set..** This r
bf70: 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 64 65 61 6c outine only deal
bf80: 73 20 77 69 74 68 20 74 68 6f 73 65 20 74 68 61 s with those tha
bf90: 74 20 61 72 65 20 6e 6f 74 20 67 6c 6f 62 61 6c t are not global
bfa0: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
bfb0: 33 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e 3RegisterBuiltin
bfc0: 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 Functions(sqlite
bfd0: 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 3 *db){. int rc
bfe0: 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c = sqlite3_overl
bff0: 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c oad_function(db,
c000: 20 22 4d 41 54 43 48 22 2c 20 32 29 3b 0a 20 20 "MATCH", 2);.
c010: 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
c020: 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d TE_NOMEM || rc==
c030: 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 SQLITE_OK );. i
c040: 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f f( rc==SQLITE_NO
c050: 4d 45 4d 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d MEM ){. db->m
c060: 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b allocFailed = 1;
c070: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 . }.}../*.** Se
c080: 74 20 74 68 65 20 4c 49 4b 45 4f 50 54 20 66 6c t the LIKEOPT fl
c090: 61 67 20 6f 6e 20 74 68 65 20 32 2d 61 72 67 75 ag on the 2-argu
c0a0: 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e 20 77 69 ment function wi
c0b0: 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d th the given nam
c0c0: 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
c0d0: 64 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67 d setLikeOptFlag
c0e0: 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f (sqlite3 *db, co
c0f0: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
c100: 20 75 38 20 66 6c 61 67 56 61 6c 29 7b 0a 20 20 u8 flagVal){.
c110: 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 FuncDef *pDef;.
c120: 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 pDef = sqlite3F
c130: 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 indFunction(db,
c140: 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 zName, sqlite3St
c150: 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 0a 20 rlen30(zName),.
c160: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c170: 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 53 2, S
c180: 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a QLITE_UTF8, 0);.
c190: 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 44 65 if( ALWAYS(pDe
c1a0: 66 29 20 29 7b 0a 20 20 20 20 70 44 65 66 2d 3e f) ){. pDef->
c1b0: 66 6c 61 67 73 20 3d 20 66 6c 61 67 56 61 6c 3b flags = flagVal;
c1c0: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
c1d0: 67 69 73 74 65 72 20 74 68 65 20 62 75 69 6c 74 gister the built
c1e0: 2d 69 6e 20 4c 49 4b 45 20 61 6e 64 20 47 4c 4f -in LIKE and GLO
c1f0: 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 B functions. Th
c200: 65 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 0a e caseSensitive.
c210: 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 64 65 74 ** parameter det
c220: 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 ermines whether
c230: 6f 72 20 6e 6f 74 20 74 68 65 20 4c 49 4b 45 20 or not the LIKE
c240: 6f 70 65 72 61 74 6f 72 20 69 73 20 63 61 73 65 operator is case
c250: 0a 2a 2a 20 73 65 6e 73 69 74 69 76 65 2e 20 20 .** sensitive.
c260: 47 4c 4f 42 20 69 73 20 61 6c 77 61 79 73 20 63 GLOB is always c
c270: 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a ase sensitive..*
c280: 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 /.void sqlite3Re
c290: 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 gisterLikeFuncti
c2a0: 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ons(sqlite3 *db,
c2b0: 20 69 6e 74 20 63 61 73 65 53 65 6e 73 69 74 69 int caseSensiti
c2c0: 76 65 29 7b 0a 20 20 73 74 72 75 63 74 20 63 6f ve){. struct co
c2d0: 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f mpareInfo *pInfo
c2e0: 3b 0a 20 20 69 66 28 20 63 61 73 65 53 65 6e 73 ;. if( caseSens
c2f0: 69 74 69 76 65 20 29 7b 0a 20 20 20 20 70 49 6e itive ){. pIn
c300: 66 6f 20 3d 20 28 73 74 72 75 63 74 20 63 6f 6d fo = (struct com
c310: 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 pareInfo*)&likeI
c320: 6e 66 6f 41 6c 74 3b 0a 20 20 7d 65 6c 73 65 7b nfoAlt;. }else{
c330: 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 . pInfo = (st
c340: 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f ruct compareInfo
c350: 2a 29 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 3b *)&likeInfoNorm;
c360: 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 43 72 . }. sqlite3Cr
c370: 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 6c 69 eateFunc(db, "li
c380: 6b 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 ke", 2, SQLITE_U
c390: 54 46 38 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 TF8, pInfo, like
c3a0: 46 75 6e 63 2c 20 30 2c 20 30 2c 20 30 29 3b 0a Func, 0, 0, 0);.
c3b0: 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 sqlite3CreateF
c3c0: 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 unc(db, "like",
c3d0: 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 3, SQLITE_UTF8,
c3e0: 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c pInfo, likeFunc,
c3f0: 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 0, 0, 0);. sql
c400: 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 ite3CreateFunc(d
c410: 62 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20 53 51 b, "glob", 2, SQ
c420: 4c 49 54 45 5f 55 54 46 38 2c 20 0a 20 20 20 20 LITE_UTF8, .
c430: 20 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 (struct compar
c440: 65 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f eInfo*)&globInfo
c450: 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 , likeFunc, 0, 0
c460: 2c 20 30 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f , 0);. setLikeO
c470: 70 74 46 6c 61 67 28 64 62 2c 20 22 67 6c 6f 62 ptFlag(db, "glob
c480: 22 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c ", SQLITE_FUNC_L
c490: 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e IKE | SQLITE_FUN
c4a0: 43 5f 43 41 53 45 29 3b 0a 20 20 73 65 74 4c 69 C_CASE);. setLi
c4b0: 6b 65 4f 70 74 46 6c 61 67 28 64 62 2c 20 22 6c keOptFlag(db, "l
c4c0: 69 6b 65 22 2c 20 0a 20 20 20 20 20 20 63 61 73 ike", . cas
c4d0: 65 53 65 6e 73 69 74 69 76 65 20 3f 20 28 53 51 eSensitive ? (SQ
c4e0: 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c LITE_FUNC_LIKE |
c4f0: 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 SQLITE_FUNC_CAS
c500: 45 29 20 3a 20 53 51 4c 49 54 45 5f 46 55 4e 43 E) : SQLITE_FUNC
c510: 5f 4c 49 4b 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _LIKE);.}../*.**
c520: 20 70 45 78 70 72 20 70 6f 69 6e 74 73 20 74 6f pExpr points to
c530: 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 an expression w
c540: 68 69 63 68 20 69 6d 70 6c 65 6d 65 6e 74 73 20 hich implements
c550: 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 66 0a a function. If.
c560: 2a 2a 20 69 74 20 69 73 20 61 70 70 72 6f 70 72 ** it is appropr
c570: 69 61 74 65 20 74 6f 20 61 70 70 6c 79 20 74 68 iate to apply th
c580: 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74 e LIKE optimizat
c590: 69 6f 6e 20 74 6f 20 74 68 61 74 20 66 75 6e 63 ion to that func
c5a0: 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 73 65 74 tion.** then set
c5b0: 20 61 57 63 5b 30 5d 20 74 68 72 6f 75 67 68 20 aWc[0] through
c5c0: 61 57 63 5b 32 5d 20 74 6f 20 74 68 65 20 77 69 aWc[2] to the wi
c5d0: 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65 72 ldcard character
c5e0: 73 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 s and.** return
c5f0: 54 52 55 45 2e 20 20 49 66 20 74 68 65 20 66 75 TRUE. If the fu
c600: 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 20 nction is not a
c610: 4c 49 4b 45 2d 73 74 79 6c 65 20 66 75 6e 63 74 LIKE-style funct
c620: 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75 ion then.** retu
c630: 72 6e 20 46 41 4c 53 45 2e 0a 2a 2f 0a 69 6e 74 rn FALSE..*/.int
c640: 20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 sqlite3IsLikeFu
c650: 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a nction(sqlite3 *
c660: 64 62 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c db, Expr *pExpr,
c670: 20 69 6e 74 20 2a 70 49 73 4e 6f 63 61 73 65 2c int *pIsNocase,
c680: 20 63 68 61 72 20 2a 61 57 63 29 7b 0a 20 20 46 char *aWc){. F
c690: 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 20 uncDef *pDef;.
c6a0: 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 if( pExpr->op!=T
c6b0: 4b 5f 46 55 4e 43 54 49 4f 4e 20 0a 20 20 20 7c K_FUNCTION . |
c6c0: 7c 20 21 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 | !pExpr->x.pLis
c6d0: 74 20 0a 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e t . || pExpr->
c6e0: 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d x.pList->nExpr!=
c6f0: 32 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 2. ){. retur
c700: 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 n 0;. }. asser
c710: 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 t( !ExprHasPrope
c720: 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 rty(pExpr, EP_xI
c730: 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 70 44 sSelect) );. pD
c740: 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 ef = sqlite3Find
c750: 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 45 78 Function(db, pEx
c760: 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 0a 20 pr->u.zToken, .
c770: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c780: 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
c790: 74 65 33 53 74 72 6c 65 6e 33 30 28 70 45 78 70 te3Strlen30(pExp
c7a0: 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0a 20 20 r->u.zToken),.
c7b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7c0: 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 53 51 2, SQ
c7d0: 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 LITE_UTF8, 0);.
c7e0: 20 69 66 28 20 4e 45 56 45 52 28 70 44 65 66 3d if( NEVER(pDef=
c7f0: 3d 30 29 20 7c 7c 20 28 70 44 65 66 2d 3e 66 6c =0) || (pDef->fl
c800: 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e ags & SQLITE_FUN
c810: 43 5f 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 20 C_LIKE)==0 ){.
c820: 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
c830: 0a 20 20 2f 2a 20 54 68 65 20 6d 65 6d 63 70 79 . /* The memcpy
c840: 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 () statement ass
c850: 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 77 69 umes that the wi
c860: 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65 72 ldcard character
c870: 73 20 61 72 65 0a 20 20 2a 2a 20 74 68 65 20 66 s are. ** the f
c880: 69 72 73 74 20 74 68 72 65 65 20 73 74 61 74 65 irst three state
c890: 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d ments in the com
c8a0: 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 pareInfo structu
c8b0: 72 65 2e 20 20 54 68 65 0a 20 20 2a 2a 20 61 73 re. The. ** as
c8c0: 73 65 72 74 73 28 29 20 74 68 61 74 20 66 6f 6c serts() that fol
c8d0: 6c 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20 low verify that
c8e0: 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f 0a assumption. */.
c8f0: 20 20 6d 65 6d 63 70 79 28 61 57 63 2c 20 70 44 memcpy(aWc, pD
c900: 65 66 2d 3e 70 55 73 65 72 44 61 74 61 2c 20 33 ef->pUserData, 3
c910: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 68 );. assert( (ch
c920: 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 ar*)&likeInfoAlt
c930: 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 == (char*)&like
c940: 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 41 6c 6c InfoAlt.matchAll
c950: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26 28 );. assert( &(
c960: 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f (char*)&likeInfo
c970: 41 6c 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61 72 Alt)[1] == (char
c980: 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d *)&likeInfoAlt.m
c990: 61 74 63 68 4f 6e 65 20 29 3b 0a 20 20 61 73 73 atchOne );. ass
c9a0: 65 72 74 28 20 26 28 28 63 68 61 72 2a 29 26 6c ert( &((char*)&l
c9b0: 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d ikeInfoAlt)[2] =
c9c0: 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e = (char*)&likeIn
c9d0: 66 6f 41 6c 74 2e 6d 61 74 63 68 53 65 74 20 29 foAlt.matchSet )
c9e0: 3b 0a 20 20 2a 70 49 73 4e 6f 63 61 73 65 20 3d ;. *pIsNocase =
c9f0: 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 (pDef->flags &
ca00: 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 SQLITE_FUNC_CASE
ca10: 29 3d 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 )==0;. return 1
ca20: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 61 ;.}../*.** All a
ca30: 6c 6c 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 ll of the FuncDe
ca40: 66 20 73 74 72 75 63 74 75 72 65 73 20 69 6e 20 f structures in
ca50: 74 68 65 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 the aBuiltinFunc
ca60: 5b 5d 20 61 72 72 61 79 20 61 62 6f 76 65 0a 2a [] array above.*
ca70: 2a 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 * to the global
ca80: 66 75 6e 63 74 69 6f 6e 20 68 61 73 68 20 74 61 function hash ta
ca90: 62 6c 65 2e 20 20 54 68 69 73 20 6f 63 63 75 72 ble. This occur
caa0: 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 20 s at start-time
cab0: 28 61 73 0a 2a 2a 20 61 20 63 6f 6e 73 65 71 75 (as.** a consequ
cac0: 65 6e 63 65 20 6f 66 20 63 61 6c 6c 69 6e 67 20 ence of calling
cad0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
cae0: 7a 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 ze())..**.** Aft
caf0: 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 er this routine
cb00: 72 75 6e 73 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c runs.*/.void sql
cb10: 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 ite3RegisterGlob
cb20: 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 alFunctions(void
cb30: 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 ){. /*. ** The
cb40: 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 following array
cb50: 20 68 6f 6c 64 73 20 46 75 6e 63 44 65 66 20 73 holds FuncDef s
cb60: 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 61 6c tructures for al
cb70: 6c 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f l of the functio
cb80: 6e 73 0a 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 ns. ** defined
cb90: 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 20 20 in this file..
cba0: 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61 72 72 61 **. ** The arra
cbb0: 79 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 y cannot be cons
cbc0: 74 61 6e 74 20 73 69 6e 63 65 20 63 68 61 6e 67 tant since chang
cbd0: 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 es are made to t
cbe0: 68 65 0a 20 20 2a 2a 20 46 75 6e 63 44 65 66 2e he. ** FuncDef.
cbf0: 70 48 61 73 68 20 65 6c 65 6d 65 6e 74 73 20 61 pHash elements a
cc00: 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 54 t start-time. T
cc10: 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 he elements of t
cc20: 68 69 73 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 his array. ** a
cc30: 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66 74 re read-only aft
cc40: 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f er initializatio
cc50: 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 20 n is complete..
cc60: 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 53 51 4c */. static SQL
cc70: 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 20 ITE_WSD FuncDef
cc80: 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 3d aBuiltinFunc[] =
cc90: 20 7b 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 {. FUNCTION(
cca0: 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 ltrim,
ccb0: 20 20 20 20 31 2c 20 31 2c 20 30 2c 20 74 72 69 1, 1, 0, tri
ccc0: 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c mFunc ),
ccd0: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 . FUNCTION(lt
cce0: 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 rim,
ccf0: 20 20 32 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 2, 1, 0, trimF
cd00: 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 unc ),.
cd10: 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 FUNCTION(rtri
cd20: 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m,
cd30: 31 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 1, 2, 0, trimFun
cd40: 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 c ),.
cd50: 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c FUNCTION(rtrim,
cd60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 2,
cd70: 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 2, 0, trimFunc
cd80: 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 ),. F
cd90: 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 UNCTION(trim,
cda0: 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 33 1, 3
cdb0: 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 , 0, trimFunc
cdc0: 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e ),. FUN
cdd0: 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 CTION(trim,
cde0: 20 20 20 20 20 20 20 20 20 20 32 2c 20 33 2c 20 2, 3,
cdf0: 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 0, trimFunc
ce00: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
ce10: 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 ION(min,
ce20: 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 31 2c -1, 0, 1,
ce30: 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20 20 20 20 minmaxFunc
ce40: 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
ce50: 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 N(min,
ce60: 20 20 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 30 0, 0, 1, 0
ce70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce80: 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 ),. AGGREGATE
ce90: 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 (min,
cea0: 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 1, 0, 1, min
ceb0: 6d 61 78 53 74 65 70 2c 20 20 20 20 20 20 6d 69 maxStep, mi
cec0: 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a nMaxFinalize ),.
ced0: 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 FUNCTION(max
cee0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
cef0: 2d 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 -1, 1, 1, minmax
cf00: 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 Func ),.
cf10: 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 FUNCTION(max,
cf20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
cf30: 2c 20 31 2c 20 31 2c 20 30 20 20 20 20 20 20 20 , 1, 1, 0
cf40: 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 ),.
cf50: 41 47 47 52 45 47 41 54 45 28 6d 61 78 2c 20 20 AGGREGATE(max,
cf60: 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 1,
cf70: 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70 1, 1, minmaxStep
cf80: 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e , minMaxFin
cf90: 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20 46 55 4e alize ),. FUN
cfa0: 43 54 49 4f 4e 32 28 74 79 70 65 6f 66 2c 20 20 CTION2(typeof,
cfb0: 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
cfc0: 30 2c 20 74 79 70 65 6f 66 46 75 6e 63 2c 20 20 0, typeofFunc,
cfd0: 53 51 4c 49 54 45 5f 46 55 4e 43 5f 54 59 50 45 SQLITE_FUNC_TYPE
cfe0: 4f 46 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f OF),. FUNCTIO
cff0: 4e 32 28 6c 65 6e 67 74 68 2c 20 20 20 20 20 20 N2(length,
d000: 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 1, 0, 0, l
d010: 65 6e 67 74 68 46 75 6e 63 2c 20 20 53 51 4c 49 engthFunc, SQLI
d020: 54 45 5f 46 55 4e 43 5f 4c 45 4e 47 54 48 29 2c TE_FUNC_LENGTH),
d030: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 69 6e . FUNCTION(in
d040: 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 str,
d050: 20 20 32 2c 20 30 2c 20 30 2c 20 69 6e 73 74 72 2, 0, 0, instr
d060: 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 Func ),.
d070: 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73 FUNCTION(subs
d080: 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 tr,
d090: 32 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46 2, 0, 0, substrF
d0a0: 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 unc ),.
d0b0: 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73 74 72 FUNCTION(substr
d0c0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 33 2c , 3,
d0d0: 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46 75 6e 0, 0, substrFun
d0e0: 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 c ),. F
d0f0: 55 4e 43 54 49 4f 4e 28 75 6e 69 63 6f 64 65 2c UNCTION(unicode,
d100: 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 1, 0
d110: 2c 20 30 2c 20 75 6e 69 63 6f 64 65 46 75 6e 63 , 0, unicodeFunc
d120: 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e ),. FUN
d130: 43 54 49 4f 4e 28 63 68 61 72 2c 20 20 20 20 20 CTION(char,
d140: 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 -1, 0,
d150: 30 2c 20 63 68 61 72 46 75 6e 63 20 20 20 20 20 0, charFunc
d160: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
d170: 49 4f 4e 28 61 62 73 2c 20 20 20 20 20 20 20 20 ION(abs,
d180: 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 1, 0, 0,
d190: 20 61 62 73 46 75 6e 63 20 20 20 20 20 20 20 20 absFunc
d1a0: 20 20 29 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c ),.#ifndef SQL
d1b0: 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
d1c0: 47 5f 50 4f 49 4e 54 0a 20 20 20 20 46 55 4e 43 G_POINT. FUNC
d1d0: 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20 TION(round,
d1e0: 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
d1f0: 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 20 , roundFunc
d200: 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
d210: 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20 20 20 ON(round,
d220: 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 2, 0, 0,
d230: 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 20 20 20 roundFunc
d240: 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 46 ),.#endif. F
d250: 55 4e 43 54 49 4f 4e 28 75 70 70 65 72 2c 20 20 UNCTION(upper,
d260: 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 1, 0
d270: 2c 20 30 2c 20 75 70 70 65 72 46 75 6e 63 20 20 , 0, upperFunc
d280: 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e ),. FUN
d290: 43 54 49 4f 4e 28 6c 6f 77 65 72 2c 20 20 20 20 CTION(lower,
d2a0: 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
d2b0: 30 2c 20 6c 6f 77 65 72 46 75 6e 63 20 20 20 20 0, lowerFunc
d2c0: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
d2d0: 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 ION(coalesce,
d2e0: 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 1, 0, 0,
d2f0: 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
d300: 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
d310: 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 N(coalesce,
d320: 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0
d330: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d340: 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 32 ),. FUNCTION2
d350: 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 (coalesce,
d360: 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 69 66 6e -1, 0, 0, ifn
d370: 75 6c 6c 46 75 6e 63 2c 20 20 53 51 4c 49 54 45 ullFunc, SQLITE
d380: 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 43 45 29 2c _FUNC_COALESCE),
d390: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 68 65 . FUNCTION(he
d3a0: 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x,
d3b0: 20 20 31 2c 20 30 2c 20 30 2c 20 68 65 78 46 75 1, 0, 0, hexFu
d3c0: 6e 63 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 nc ),.
d3d0: 20 20 20 46 55 4e 43 54 49 4f 4e 32 28 69 66 6e FUNCTION2(ifn
d3e0: 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 ull,
d3f0: 32 2c 20 30 2c 20 30 2c 20 69 66 6e 75 6c 6c 46 2, 0, 0, ifnullF
d400: 75 6e 63 2c 20 20 53 51 4c 49 54 45 5f 46 55 4e unc, SQLITE_FUN
d410: 43 5f 43 4f 41 4c 45 53 43 45 29 2c 0a 20 20 20 C_COALESCE),.
d420: 20 46 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d FUNCTION(random
d430: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c , 0,
d440: 20 30 2c 20 30 2c 20 72 61 6e 64 6f 6d 46 75 6e 0, 0, randomFun
d450: 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 c ),. F
d460: 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c UNCTION(randombl
d470: 6f 62 2c 20 20 20 20 20 20 20 20 20 31 2c 20 30 ob, 1, 0
d480: 2c 20 30 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 , 0, randomBlob
d490: 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e ),. FUN
d4a0: 43 54 49 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20 20 CTION(nullif,
d4b0: 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 2, 0,
d4c0: 31 2c 20 6e 75 6c 6c 69 66 46 75 6e 63 20 20 20 1, nullifFunc
d4d0: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
d4e0: 49 4f 4e 28 73 71 6c 69 74 65 5f 76 65 72 73 69 ION(sqlite_versi
d4f0: 6f 6e 2c 20 20 20 20 20 30 2c 20 30 2c 20 30 2c on, 0, 0, 0,
d500: 20 76 65 72 73 69 6f 6e 46 75 6e 63 20 20 20 20 versionFunc
d510: 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
d520: 4e 28 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f N(sqlite_source_
d530: 69 64 2c 20 20 20 30 2c 20 30 2c 20 30 2c 20 73 id, 0, 0, 0, s
d540: 6f 75 72 63 65 69 64 46 75 6e 63 20 20 20 20 20 ourceidFunc
d550: 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
d560: 73 71 6c 69 74 65 5f 6c 6f 67 2c 20 20 20 20 20 sqlite_log,
d570: 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 65 72 72 2, 0, 0, err
d580: 6c 6f 67 46 75 6e 63 20 20 20 20 20 20 20 29 2c logFunc ),
d590: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
d5a0: 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 OMIT_COMPILEOPTI
d5b0: 4f 4e 5f 44 49 41 47 53 0a 20 20 20 20 46 55 4e ON_DIAGS. FUN
d5c0: 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 63 6f 6d CTION(sqlite_com
d5d0: 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 2c pileoption_used,
d5e0: 31 2c 20 30 2c 20 30 2c 20 63 6f 6d 70 69 6c 65 1, 0, 0, compile
d5f0: 6f 70 74 69 6f 6e 75 73 65 64 46 75 6e 63 20 20 optionusedFunc
d600: 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
d610: 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 sqlite_compileop
d620: 74 69 6f 6e 5f 67 65 74 2c 20 31 2c 20 30 2c 20 tion_get, 1, 0,
d630: 30 2c 20 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 0, compileoption
d640: 67 65 74 46 75 6e 63 20 20 29 2c 0a 23 65 6e 64 getFunc ),.#end
d650: 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
d660: 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f T_COMPILEOPTION_
d670: 44 49 41 47 53 20 2a 2f 0a 20 20 20 20 46 55 4e DIAGS */. FUN
d680: 43 54 49 4f 4e 28 71 75 6f 74 65 2c 20 20 20 20 CTION(quote,
d690: 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
d6a0: 30 2c 20 71 75 6f 74 65 46 75 6e 63 20 20 20 20 0, quoteFunc
d6b0: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
d6c0: 49 4f 4e 28 74 6f 69 6e 74 65 67 65 72 2c 20 20 ION(tointeger,
d6d0: 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 1, 0, 0,
d6e0: 20 74 6f 69 6e 74 65 67 65 72 46 75 6e 63 20 20 tointegerFunc
d6f0: 20 20 29 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c ),.#ifndef SQL
d700: 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
d710: 47 5f 50 4f 49 4e 54 0a 20 20 20 20 46 55 4e 43 G_POINT. FUNC
d720: 54 49 4f 4e 28 74 6f 72 65 61 6c 2c 20 20 20 20 TION(toreal,
d730: 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
d740: 2c 20 74 6f 72 65 61 6c 46 75 6e 63 20 20 20 20 , torealFunc
d750: 20 20 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 20 ),.#endif.
d760: 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73 74 5f 69 FUNCTION(last_i
d770: 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20 20 30 2c nsert_rowid, 0,
d780: 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69 6e 73 65 0, 0, last_inse
d790: 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20 20 20 46 rt_rowid),. F
d7a0: 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67 65 73 2c UNCTION(changes,
d7b0: 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 0, 0
d7c0: 2c 20 30 2c 20 63 68 61 6e 67 65 73 20 20 20 20 , 0, changes
d7d0: 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e ),. FUN
d7e0: 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63 68 61 6e CTION(total_chan
d7f0: 67 65 73 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 ges, 0, 0,
d800: 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 0, total_changes
d810: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
d820: 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20 20 20 20 ION(replace,
d830: 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30 2c 3, 0, 0,
d840: 20 72 65 70 6c 61 63 65 46 75 6e 63 20 20 20 20 replaceFunc
d850: 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
d860: 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20 20 20 20 N(zeroblob,
d870: 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 7a 1, 0, 0, z
d880: 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 20 20 20 eroblobFunc
d890: 29 2c 0a 20 20 23 69 66 64 65 66 20 53 51 4c 49 ),. #ifdef SQLI
d8a0: 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20 20 20 46 TE_SOUNDEX. F
d8b0: 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64 65 78 2c UNCTION(soundex,
d8c0: 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 1, 0
d8d0: 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46 75 6e 63 , 0, soundexFunc
d8e0: 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e 64 69 ),. #endi
d8f0: 66 0a 20 20 23 69 66 6e 64 65 66 20 53 51 4c 49 f. #ifndef SQLI
d900: 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 TE_OMIT_LOAD_EXT
d910: 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55 4e 43 54 ENSION. FUNCT
d920: 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 69 ION(load_extensi
d930: 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c 20 30 2c on, 1, 0, 0,
d940: 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20 20 loadExt
d950: 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
d960: 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e N(load_extension
d970: 2c 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 6c , 2, 0, 0, l
d980: 6f 61 64 45 78 74 20 20 20 20 20 20 20 20 20 20 oadExt
d990: 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20 20 20 ),. #endif.
d9a0: 41 47 47 52 45 47 41 54 45 28 73 75 6d 2c 20 20 AGGREGATE(sum,
d9b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 1,
d9c0: 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 0, 0, sumStep,
d9d0: 20 20 20 20 20 20 20 73 75 6d 46 69 6e 61 6c 69 sumFinali
d9e0: 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 ze ),. AGG
d9f0: 52 45 47 41 54 45 28 74 6f 74 61 6c 2c 20 20 20 REGATE(total,
da00: 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
da10: 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20 0, sumStep,
da20: 20 20 20 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a totalFinaliz
da30: 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52 e ),. AGGR
da40: 45 47 41 54 45 28 61 76 67 2c 20 20 20 20 20 20 EGATE(avg,
da50: 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
da60: 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20 20 , sumStep,
da70: 20 20 20 61 76 67 46 69 6e 61 6c 69 7a 65 20 20 avgFinalize
da80: 20 20 29 2c 0a 20 2f 2a 20 41 47 47 52 45 47 41 ),. /* AGGREGA
da90: 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 20 TE(count,
daa0: 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 0, 0, 0, c
dab0: 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 20 ountStep,
dac0: 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29 countFinalize )
dad0: 2c 20 2a 2f 0a 20 20 20 20 7b 30 2c 53 51 4c 49 , */. {0,SQLI
dae0: 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 46 TE_UTF8,SQLITE_F
daf0: 55 4e 43 5f 43 4f 55 4e 54 2c 30 2c 30 2c 30 2c UNC_COUNT,0,0,0,
db00: 63 6f 75 6e 74 53 74 65 70 2c 63 6f 75 6e 74 46 countStep,countF
db10: 69 6e 61 6c 69 7a 65 2c 22 63 6f 75 6e 74 22 2c inalize,"count",
db20: 30 2c 30 7d 2c 0a 20 20 20 20 41 47 47 52 45 47 0,0},. AGGREG
db30: 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 ATE(count,
db40: 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 1, 0, 0,
db50: 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 countStep,
db60: 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 countFinalize
db70: 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 ),. AGGREGATE
db80: 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 20 (group_concat,
db90: 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 67 72 6f 1, 0, 0, gro
dba0: 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 72 upConcatStep, gr
dbb0: 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a oupConcatFinaliz
dbc0: 65 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 e),. AGGREGAT
dbd0: 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 E(group_concat,
dbe0: 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 67 72 2, 0, 0, gr
dbf0: 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 oupConcatStep, g
dc00: 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 roupConcatFinali
dc10: 7a 65 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b 45 ze),. . LIKE
dc20: 46 55 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26 67 FUNC(glob, 2, &g
dc30: 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45 5f lobInfo, SQLITE_
dc40: 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 FUNC_LIKE|SQLITE
dc50: 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 23 _FUNC_CASE),. #
dc60: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41 53 ifdef SQLITE_CAS
dc70: 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 E_SENSITIVE_LIKE
dc80: 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 . LIKEFUNC(li
dc90: 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 6f ke, 2, &likeInfo
dca0: 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 Alt, SQLITE_FUNC
dcb0: 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e _LIKE|SQLITE_FUN
dcc0: 43 5f 43 41 53 45 29 2c 0a 20 20 20 20 4c 49 4b C_CASE),. LIK
dcd0: 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20 26 EFUNC(like, 3, &
dce0: 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 4c likeInfoAlt, SQL
dcf0: 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 ITE_FUNC_LIKE|SQ
dd00: 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c LITE_FUNC_CASE),
dd10: 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 4c 49 4b . #else. LIK
dd20: 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 26 EFUNC(like, 2, &
dd30: 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53 51 likeInfoNorm, SQ
dd40: 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 2c LITE_FUNC_LIKE),
dd50: 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 . LIKEFUNC(li
dd60: 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 6f ke, 3, &likeInfo
dd70: 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 4e Norm, SQLITE_FUN
dd80: 43 5f 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64 69 C_LIKE),. #endi
dd90: 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69 3b f. };.. int i;
dda0: 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a . FuncDefHash *
ddb0: 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 pHash = &GLOBAL(
ddc0: 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c FuncDefHash, sql
ddd0: 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 ite3GlobalFuncti
dde0: 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 ons);. FuncDef
ddf0: 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 *aFunc = (FuncDe
de00: 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 f*)&GLOBAL(FuncD
de10: 65 66 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 ef, aBuiltinFunc
de20: 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 );.. for(i=0; i
de30: 3c 41 72 72 61 79 53 69 7a 65 28 61 42 75 69 6c <ArraySize(aBuil
de40: 74 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b 0a tinFunc); i++){.
de50: 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 sqlite3FuncD
de60: 65 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 efInsert(pHash,
de70: 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a &aFunc[i]);. }.
de80: 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 sqlite3Registe
de90: 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f rDateTimeFunctio
dea0: 6e 73 28 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 ns();.#ifndef SQ
deb0: 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 LITE_OMIT_ALTERT
dec0: 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 41 6c ABLE. sqlite3Al
ded0: 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 29 3b 0a terFunctions();.
dee0: 23 65 6e 64 69 66 0a 7d 0a #endif.}.