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 2d 6c 61 6e 67 75 61 67 65 20 69 6d 70 6c 65 C-language imple
01a0: 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 6d mentations for m
01b0: 61 6e 79 20 6f 66 20 74 68 65 20 53 51 4c 0a 2a any of the SQL.*
01c0: 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 53 * functions of S
01d0: 51 4c 69 74 65 2e 20 20 28 53 6f 6d 65 20 66 75 QLite. (Some fu
01e0: 6e 63 74 69 6f 6e 2c 20 61 6e 64 20 69 6e 20 70 nction, and in p
01f0: 61 72 74 69 63 75 6c 61 72 20 74 68 65 20 64 61 articular the da
0200: 74 65 20 61 6e 64 0a 2a 2a 20 74 69 6d 65 20 66 te and.** time f
0210: 75 6e 63 74 69 6f 6e 73 2c 20 61 72 65 20 69 6d unctions, are im
0220: 70 6c 65 6d 65 6e 74 65 64 20 73 65 70 61 72 61 plemented separa
0230: 74 65 6c 79 2e 29 0a 2a 2f 0a 23 69 6e 63 6c 75 tely.).*/.#inclu
0240: 64 65 20 22 73 71 6c 69 74 65 49 6e 74 2e 68 22 de "sqliteInt.h"
0250: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 .#include <stdli
0260: 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 61 b.h>.#include <a
0270: 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 ssert.h>.#includ
0280: 65 20 22 76 64 62 65 49 6e 74 2e 68 22 0a 0a 2f e "vdbeInt.h"../
0290: 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
02a0: 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 collating functi
02b0: 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 on associated wi
02c0: 74 68 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a th a function..*
02d0: 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 /.static CollSeq
02e0: 20 2a 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 *sqlite3GetFunc
02f0: 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 5f CollSeq(sqlite3_
0300: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
0310: 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e 74 ){. return cont
0320: 65 78 74 2d 3e 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f ext->pColl;.}../
0330: 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20 74 68 *.** Indicate th
0340: 61 74 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 at the accumulat
0350: 6f 72 20 6c 6f 61 64 20 73 68 6f 75 6c 64 20 62 or load should b
0360: 65 20 73 6b 69 70 70 65 64 20 6f 6e 20 74 68 69 e skipped on thi
0370: 73 0a 2a 2a 20 69 74 65 72 61 74 69 6f 6e 20 6f s.** iteration o
0380: 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 f the aggregate
0390: 6c 6f 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 loop..*/.static
03a0: 76 6f 69 64 20 73 71 6c 69 74 65 33 53 6b 69 70 void sqlite3Skip
03b0: 41 63 63 75 6d 75 6c 61 74 6f 72 4c 6f 61 64 28 AccumulatorLoad(
03c0: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
03d0: 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 63 6f 6e *context){. con
03e0: 74 65 78 74 2d 3e 73 6b 69 70 46 6c 61 67 20 3d text->skipFlag =
03f0: 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 1;.}../*.** Imp
0400: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
0410: 68 65 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 he non-aggregate
0420: 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 min() and max()
0430: 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74 functions.*/.st
0440: 61 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 atic void minmax
0450: 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
0460: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
0470: 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
0480: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
0490: 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b argv.){. int i;
04a0: 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20 . int mask;
04b0: 2f 2a 20 30 20 66 6f 72 20 6d 69 6e 28 29 20 6f /* 0 for min() o
04c0: 72 20 30 78 66 66 66 66 66 66 66 66 20 66 6f 72 r 0xffffffff for
04d0: 20 6d 61 78 28 29 20 2a 2f 0a 20 20 69 6e 74 20 max() */. int
04e0: 69 42 65 73 74 3b 0a 20 20 43 6f 6c 6c 53 65 71 iBest;. CollSeq
04f0: 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 61 73 73 65 *pColl;.. asse
0500: 72 74 28 20 61 72 67 63 3e 31 20 29 3b 0a 20 20 rt( argc>1 );.
0510: 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 5f 75 mask = sqlite3_u
0520: 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 ser_data(context
0530: 29 3d 3d 30 20 3f 20 30 20 3a 20 2d 31 3b 0a 20 )==0 ? 0 : -1;.
0540: 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 pColl = sqlite3
0550: 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 63 GetFuncCollSeq(c
0560: 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72 ontext);. asser
0570: 74 28 20 70 43 6f 6c 6c 20 29 3b 0a 20 20 61 73 t( pColl );. as
0580: 73 65 72 74 28 20 6d 61 73 6b 3d 3d 2d 31 20 7c sert( mask==-1 |
0590: 7c 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 69 | mask==0 );. i
05a0: 42 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 Best = 0;. if(
05b0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
05c0: 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c pe(argv[0])==SQL
05d0: 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 ITE_NULL ) retur
05e0: 6e 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c n;. for(i=1; i<
05f0: 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 argc; i++){.
0600: 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 if( sqlite3_valu
0610: 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29 3d e_type(argv[i])=
0620: 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 =SQLITE_NULL ) r
0630: 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 28 eturn;. if( (
0640: 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 sqlite3MemCompar
0650: 65 28 61 72 67 76 5b 69 42 65 73 74 5d 2c 20 61 e(argv[iBest], a
0660: 72 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c 29 5e 6d rgv[i], pColl)^m
0670: 61 73 6b 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 ask)>=0 ){.
0680: 20 74 65 73 74 63 61 73 65 28 20 6d 61 73 6b 3d testcase( mask=
0690: 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 42 65 73 =0 );. iBes
06a0: 74 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d t = i;. }. }
06b0: 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
06c0: 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c t_value(context,
06d0: 20 61 72 67 76 5b 69 42 65 73 74 5d 29 3b 0a 7d argv[iBest]);.}
06e0: 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
06f0: 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 20 61 he type of the a
0700: 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 rgument..*/.stat
0710: 69 63 20 76 6f 69 64 20 74 79 70 65 6f 66 46 75 ic void typeofFu
0720: 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
0730: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
0740: 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 int NotUsed,.
0750: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
0760: 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 *argv.){. const
0770: 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20 20 char *z = 0;.
0780: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
0790: 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 73 77 69 (NotUsed);. swi
07a0: 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c tch( sqlite3_val
07b0: 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 ue_type(argv[0])
07c0: 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ){. case SQL
07d0: 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7a 20 3d ITE_INTEGER: z =
07e0: 20 22 69 6e 74 65 67 65 72 22 3b 20 62 72 65 61 "integer"; brea
07f0: 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 k;. case SQLI
0800: 54 45 5f 54 45 58 54 3a 20 20 20 20 7a 20 3d 20 TE_TEXT: z =
0810: 22 74 65 78 74 22 3b 20 20 20 20 62 72 65 61 6b "text"; break
0820: 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 ;. case SQLIT
0830: 45 5f 46 4c 4f 41 54 3a 20 20 20 7a 20 3d 20 22 E_FLOAT: z = "
0840: 72 65 61 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b real"; break;
0850: 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
0860: 5f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d 20 22 62 _BLOB: z = "b
0870: 6c 6f 62 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a lob"; break;.
0880: 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20 default:
0890: 20 20 20 20 20 20 20 20 20 7a 20 3d 20 22 6e 75 z = "nu
08a0: 6c 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 ll"; break;.
08b0: 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 }. sqlite3_res
08c0: 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
08d0: 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f , z, -1, SQLITE_
08e0: 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a 2f 2a 0a STATIC);.}.../*.
08f0: 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
0900: 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 68 28 n of the length(
0910: 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73 74 ) function.*/.st
0920: 61 74 69 63 20 76 6f 69 64 20 6c 65 6e 67 74 68 atic void length
0930: 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
0940: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
0950: 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
0960: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
0970: 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6c 65 argv.){. int le
0980: 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 n;.. assert( ar
0990: 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 gc==1 );. UNUSE
09a0: 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 D_PARAMETER(argc
09b0: 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c );. switch( sql
09c0: 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
09d0: 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 argv[0]) ){.
09e0: 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 case SQLITE_BLOB
09f0: 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 :. case SQLIT
0a00: 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 E_INTEGER:. c
0a10: 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 ase SQLITE_FLOAT
0a20: 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
0a30: 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 3_result_int(con
0a40: 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 text, sqlite3_va
0a50: 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 lue_bytes(argv[0
0a60: 5d 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ]));. break
0a70: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
0a80: 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a SQLITE_TEXT: {.
0a90: 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 const unsi
0aa0: 67 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d 20 73 gned char *z = s
0ab0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
0ac0: 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 t(argv[0]);.
0ad0: 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 if( z==0 ) ret
0ae0: 75 72 6e 3b 0a 20 20 20 20 20 20 6c 65 6e 20 3d urn;. len =
0af0: 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 0;. while(
0b00: 20 2a 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6c *z ){. l
0b10: 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 53 51 en++;. SQ
0b20: 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a LITE_SKIP_UTF8(z
0b30: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
0b40: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
0b50: 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 6c 65 6e int(context, len
0b60: 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
0b70: 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
0b80: 74 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 t: {. sqlit
0b90: 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 e3_result_null(c
0ba0: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 ontext);. b
0bb0: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
0bc0: 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
0bd0: 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 ntation of the a
0be0: 62 73 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a bs() function..*
0bf0: 2a 0a 2a 2a 20 49 4d 50 3a 20 52 2d 32 33 39 37 *.** IMP: R-2397
0c00: 39 2d 32 36 38 35 35 20 54 68 65 20 61 62 73 28 9-26855 The abs(
0c10: 58 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 X) function retu
0c20: 72 6e 73 20 74 68 65 20 61 62 73 6f 6c 75 74 65 rns the absolute
0c30: 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 74 68 65 value of.** the
0c40: 20 6e 75 6d 65 72 69 63 20 61 72 67 75 6d 65 6e numeric argumen
0c50: 74 20 58 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 t X. .*/.static
0c60: 76 6f 69 64 20 61 62 73 46 75 6e 63 28 73 71 6c void absFunc(sql
0c70: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
0c80: 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c ntext, int argc,
0c90: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
0ca0: 2a 61 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74 *argv){. assert
0cb0: 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 ( argc==1 );. U
0cc0: 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
0cd0: 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28 argc);. switch(
0ce0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
0cf0: 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a ype(argv[0]) ){.
0d00: 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
0d10: 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 INTEGER: {.
0d20: 20 69 36 34 20 69 56 61 6c 20 3d 20 73 71 6c 69 i64 iVal = sqli
0d30: 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 te3_value_int64(
0d40: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 argv[0]);.
0d50: 69 66 28 20 69 56 61 6c 3c 30 20 29 7b 0a 20 20 if( iVal<0 ){.
0d60: 20 20 20 20 20 20 69 66 28 20 69 56 61 6c 3d 3d if( iVal==
0d70: 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 29 SMALLEST_INT64 )
0d80: 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 {. /* I
0d90: 4d 50 3a 20 52 2d 33 31 36 37 36 2d 34 35 35 30 MP: R-31676-4550
0da0: 39 20 49 66 20 58 20 69 73 20 74 68 65 20 69 6e 9 If X is the in
0db0: 74 65 67 65 72 20 2d 39 32 32 33 33 37 32 30 33 teger -922337203
0dc0: 36 38 35 34 37 37 35 38 30 38 0a 20 20 20 20 20 6854775808.
0dd0: 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 62 73 ** then abs
0de0: 28 58 29 20 74 68 72 6f 77 73 20 61 6e 20 69 6e (X) throws an in
0df0: 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 65 teger overflow e
0e00: 72 72 6f 72 20 73 69 6e 63 65 20 74 68 65 72 65 rror since there
0e10: 20 69 73 20 6e 6f 0a 20 20 20 20 20 20 20 20 20 is no.
0e20: 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 70 ** equivalent p
0e30: 6f 73 69 74 69 76 65 20 36 34 2d 62 69 74 20 74 ositive 64-bit t
0e40: 77 6f 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 76 61 wo complement va
0e50: 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 lue. */.
0e60: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
0e70: 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 _error(context,
0e80: 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f "integer overflo
0e90: 77 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 w", -1);.
0ea0: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 return;.
0eb0: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 56 61 }. iVa
0ec0: 6c 20 3d 20 2d 69 56 61 6c 3b 0a 20 20 20 20 20 l = -iVal;.
0ed0: 20 7d 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 } . sqlite
0ee0: 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 3_result_int64(c
0ef0: 6f 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b 0a 20 ontext, iVal);.
0f00: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
0f10: 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
0f20: 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 E_NULL: {.
0f30: 2f 2a 20 49 4d 50 3a 20 52 2d 33 37 34 33 34 2d /* IMP: R-37434-
0f40: 31 39 39 32 39 20 41 62 73 28 58 29 20 72 65 74 19929 Abs(X) ret
0f50: 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 58 20 69 urns NULL if X i
0f60: 73 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 s NULL. */.
0f70: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
0f80: 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 null(context);.
0f90: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
0fa0: 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b }. default: {
0fb0: 0a 20 20 20 20 20 20 2f 2a 20 42 65 63 61 75 73 . /* Becaus
0fc0: 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f e sqlite3_value_
0fd0: 64 6f 75 62 6c 65 28 29 20 72 65 74 75 72 6e 73 double() returns
0fe0: 20 30 2e 30 20 69 66 20 74 68 65 20 61 72 67 75 0.0 if the argu
0ff0: 6d 65 6e 74 20 69 73 20 6e 6f 74 0a 20 20 20 20 ment is not.
1000: 20 20 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 74 ** something t
1010: 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6e 76 65 hat can be conve
1020: 72 74 65 64 20 69 6e 74 6f 20 61 20 6e 75 6d 62 rted into a numb
1030: 65 72 2c 20 77 65 20 68 61 76 65 3a 0a 20 20 20 er, we have:.
1040: 20 20 20 2a 2a 20 49 4d 50 3a 20 52 2d 35 37 33 ** IMP: R-573
1050: 32 36 2d 33 31 35 34 31 20 41 62 73 28 58 29 20 26-31541 Abs(X)
1060: 72 65 74 75 72 6e 20 30 2e 30 20 69 66 20 58 20 return 0.0 if X
1070: 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 is a string or b
1080: 6c 6f 62 20 74 68 61 74 0a 20 20 20 20 20 20 2a lob that. *
1090: 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 * cannot be conv
10a0: 65 72 74 65 64 20 74 6f 20 61 20 6e 75 6d 65 72 erted to a numer
10b0: 69 63 20 76 61 6c 75 65 2e 20 0a 20 20 20 20 20 ic value. .
10c0: 20 2a 2f 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 */. double
10d0: 20 72 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f rVal = sqlite3_
10e0: 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 value_double(arg
10f0: 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 v[0]);. if(
1100: 20 72 56 61 6c 3c 30 20 29 20 72 56 61 6c 20 3d rVal<0 ) rVal =
1110: 20 2d 72 56 61 6c 3b 0a 20 20 20 20 20 20 73 71 -rVal;. sq
1120: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 lite3_result_dou
1130: 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 72 56 61 ble(context, rVa
1140: 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b l);. break;
1150: 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
1160: 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
1170: 6f 6e 20 6f 66 20 74 68 65 20 69 6e 73 74 72 28 on of the instr(
1180: 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a ) function..**.*
1190: 2a 20 69 6e 73 74 72 28 68 61 79 73 74 61 63 6b * instr(haystack
11a0: 2c 6e 65 65 64 6c 65 29 20 66 69 6e 64 73 20 74 ,needle) finds t
11b0: 68 65 20 66 69 72 73 74 20 6f 63 63 75 72 72 65 he first occurre
11c0: 6e 63 65 20 6f 66 20 6e 65 65 64 6c 65 0a 2a 2a nce of needle.**
11d0: 20 69 6e 20 68 61 79 73 74 61 63 6b 20 61 6e 64 in haystack and
11e0: 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
11f0: 62 65 72 20 6f 66 20 70 72 65 76 69 6f 75 73 20 ber of previous
1200: 63 68 61 72 61 63 74 65 72 73 20 70 6c 75 73 20 characters plus
1210: 31 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 6e 65 1,.** or 0 if ne
1220: 65 64 6c 65 20 64 6f 65 73 20 6e 6f 74 20 6f 63 edle does not oc
1230: 63 75 72 20 77 69 74 68 69 6e 20 68 61 79 73 74 cur within hayst
1240: 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f ack..**.** If bo
1250: 74 68 20 68 61 79 73 74 61 63 6b 20 61 6e 64 20 th haystack and
1260: 6e 65 65 64 6c 65 20 61 72 65 20 42 4c 4f 42 73 needle are BLOBs
1270: 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c , then the resul
1280: 74 20 69 73 20 6f 6e 65 20 6d 6f 72 65 20 74 68 t is one more th
1290: 61 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 an.** the number
12a0: 20 6f 66 20 62 79 74 65 73 20 69 6e 20 68 61 79 of bytes in hay
12b0: 73 74 61 63 6b 20 70 72 69 6f 72 20 74 6f 20 74 stack prior to t
12c0: 68 65 20 66 69 72 73 74 20 6f 63 63 75 72 72 65 he first occurre
12d0: 6e 63 65 20 6f 66 20 6e 65 65 64 6c 65 2c 0a 2a nce of needle,.*
12e0: 2a 20 6f 72 20 30 20 69 66 20 6e 65 65 64 6c 65 * or 0 if needle
12f0: 20 6e 65 76 65 72 20 6f 63 63 75 72 73 20 69 6e never occurs in
1300: 20 68 61 79 73 74 61 63 6b 2e 0a 2a 2f 0a 73 74 haystack..*/.st
1310: 61 74 69 63 20 76 6f 69 64 20 69 6e 73 74 72 46 atic void instrF
1320: 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
1330: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
1340: 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
1350: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
1360: 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 rgv.){. const u
1370: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 48 nsigned char *zH
1380: 61 79 73 74 61 63 6b 3b 0a 20 20 63 6f 6e 73 74 aystack;. const
1390: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
13a0: 7a 4e 65 65 64 6c 65 3b 0a 20 20 69 6e 74 20 6e zNeedle;. int n
13b0: 48 61 79 73 74 61 63 6b 3b 0a 20 20 69 6e 74 20 Haystack;. int
13c0: 6e 4e 65 65 64 6c 65 3b 0a 20 20 69 6e 74 20 74 nNeedle;. int t
13d0: 79 70 65 48 61 79 73 74 61 63 6b 2c 20 74 79 70 ypeHaystack, typ
13e0: 65 4e 65 65 64 6c 65 3b 0a 20 20 69 6e 74 20 4e eNeedle;. int N
13f0: 20 3d 20 31 3b 0a 20 20 69 6e 74 20 69 73 54 65 = 1;. int isTe
1400: 78 74 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 xt;.. UNUSED_PA
1410: 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 RAMETER(argc);.
1420: 20 74 79 70 65 48 61 79 73 74 61 63 6b 20 3d 20 typeHaystack =
1430: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
1440: 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 74 pe(argv[0]);. t
1450: 79 70 65 4e 65 65 64 6c 65 20 3d 20 73 71 6c 69 ypeNeedle = sqli
1460: 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 te3_value_type(a
1470: 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 74 rgv[1]);. if( t
1480: 79 70 65 48 61 79 73 74 61 63 6b 3d 3d 53 51 4c ypeHaystack==SQL
1490: 49 54 45 5f 4e 55 4c 4c 20 7c 7c 20 74 79 70 65 ITE_NULL || type
14a0: 4e 65 65 64 6c 65 3d 3d 53 51 4c 49 54 45 5f 4e Needle==SQLITE_N
14b0: 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ULL ) return;.
14c0: 6e 48 61 79 73 74 61 63 6b 20 3d 20 73 71 6c 69 nHaystack = sqli
14d0: 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
14e0: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 4e 65 65 argv[0]);. nNee
14f0: 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 dle = sqlite3_va
1500: 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 lue_bytes(argv[1
1510: 5d 29 3b 0a 20 20 69 66 28 20 74 79 70 65 48 61 ]);. if( typeHa
1520: 79 73 74 61 63 6b 3d 3d 53 51 4c 49 54 45 5f 42 ystack==SQLITE_B
1530: 4c 4f 42 20 26 26 20 74 79 70 65 4e 65 65 64 6c LOB && typeNeedl
1540: 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 e==SQLITE_BLOB )
1550: 7b 0a 20 20 20 20 7a 48 61 79 73 74 61 63 6b 20 {. zHaystack
1560: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1570: 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 blob(argv[0]);.
1580: 20 20 20 7a 4e 65 65 64 6c 65 20 3d 20 73 71 6c zNeedle = sql
1590: 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 ite3_value_blob(
15a0: 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 73 argv[1]);. is
15b0: 54 65 78 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 Text = 0;. }els
15c0: 65 7b 0a 20 20 20 20 7a 48 61 79 73 74 61 63 6b e{. zHaystack
15d0: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
15e0: 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a _text(argv[0]);.
15f0: 20 20 20 20 7a 4e 65 65 64 6c 65 20 3d 20 73 71 zNeedle = sq
1600: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1610: 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 (argv[1]);. i
1620: 73 54 65 78 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 sText = 1;. }.
1630: 20 77 68 69 6c 65 28 20 6e 4e 65 65 64 6c 65 3c while( nNeedle<
1640: 3d 6e 48 61 79 73 74 61 63 6b 20 26 26 20 6d 65 =nHaystack && me
1650: 6d 63 6d 70 28 7a 48 61 79 73 74 61 63 6b 2c 20 mcmp(zHaystack,
1660: 7a 4e 65 65 64 6c 65 2c 20 6e 4e 65 65 64 6c 65 zNeedle, nNeedle
1670: 29 21 3d 30 20 29 7b 0a 20 20 20 20 4e 2b 2b 3b )!=0 ){. N++;
1680: 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 6e . do{. n
1690: 48 61 79 73 74 61 63 6b 2d 2d 3b 0a 20 20 20 20 Haystack--;.
16a0: 20 20 7a 48 61 79 73 74 61 63 6b 2b 2b 3b 0a 20 zHaystack++;.
16b0: 20 20 20 7d 77 68 69 6c 65 28 20 69 73 54 65 78 }while( isTex
16c0: 74 20 26 26 20 28 7a 48 61 79 73 74 61 63 6b 5b t && (zHaystack[
16d0: 30 5d 26 30 78 63 30 29 3d 3d 30 78 38 30 20 29 0]&0xc0)==0x80 )
16e0: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 4e 65 65 ;. }. if( nNee
16f0: 64 6c 65 3e 6e 48 61 79 73 74 61 63 6b 20 29 20 dle>nHaystack )
1700: 4e 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 N = 0;. sqlite3
1710: 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 _result_int(cont
1720: 65 78 74 2c 20 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ext, N);.}../*.*
1730: 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
1740: 20 6f 66 20 74 68 65 20 70 72 69 6e 74 66 28 29 of the printf()
1750: 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
1760: 61 74 69 63 20 76 6f 69 64 20 70 72 69 6e 74 66 atic void printf
1770: 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
1780: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
1790: 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
17a0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
17b0: 61 72 67 76 0a 29 7b 0a 20 20 50 72 69 6e 74 66 argv.){. Printf
17c0: 41 72 67 75 6d 65 6e 74 73 20 78 3b 0a 20 20 53 Arguments x;. S
17d0: 74 72 41 63 63 75 6d 20 73 74 72 3b 0a 20 20 63 trAccum str;. c
17e0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
17f0: 61 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 at;. int n;..
1800: 69 66 28 20 61 72 67 63 3e 3d 31 20 26 26 20 28 if( argc>=1 && (
1810: 7a 46 6f 72 6d 61 74 20 3d 20 28 63 6f 6e 73 74 zFormat = (const
1820: 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 char*)sqlite3_v
1830: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
1840: 5d 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 78 2e ]))!=0 ){. x.
1850: 6e 41 72 67 20 3d 20 61 72 67 63 2d 31 3b 0a 20 nArg = argc-1;.
1860: 20 20 20 78 2e 6e 55 73 65 64 20 3d 20 30 3b 0a x.nUsed = 0;.
1870: 20 20 20 20 78 2e 61 70 41 72 67 20 3d 20 61 72 x.apArg = ar
1880: 67 76 2b 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 gv+1;. sqlite
1890: 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 3StrAccumInit(&s
18a0: 74 72 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 tr, 0, 0, SQLITE
18b0: 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b 0a 20 20 _MAX_LENGTH);.
18c0: 20 20 73 74 72 2e 64 62 20 3d 20 73 71 6c 69 74 str.db = sqlit
18d0: 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
18e0: 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 ndle(context);.
18f0: 20 20 20 73 71 6c 69 74 65 33 58 50 72 69 6e 74 sqlite3XPrint
1900: 66 28 26 73 74 72 2c 20 53 51 4c 49 54 45 5f 50 f(&str, SQLITE_P
1910: 52 49 4e 54 46 5f 53 51 4c 46 55 4e 43 2c 20 7a RINTF_SQLFUNC, z
1920: 46 6f 72 6d 61 74 2c 20 26 78 29 3b 0a 20 20 20 Format, &x);.
1930: 20 6e 20 3d 20 73 74 72 2e 6e 43 68 61 72 3b 0a n = str.nChar;.
1940: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
1950: 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
1960: 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
1970: 46 69 6e 69 73 68 28 26 73 74 72 29 2c 20 6e 2c Finish(&str), n,
1980: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1990: 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
19a0: 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a DYNAMIC);. }.}.
19b0: 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
19c0: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 62 ation of the sub
19d0: 73 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a str() function..
19e0: 2a 2a 0a 2a 2a 20 73 75 62 73 74 72 28 78 2c 70 **.** substr(x,p
19f0: 31 2c 70 32 29 20 20 72 65 74 75 72 6e 73 20 70 1,p2) returns p
1a00: 32 20 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 2 characters of
1a10: 78 5b 5d 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 x[] beginning wi
1a20: 74 68 20 70 31 2e 0a 2a 2a 20 70 31 20 69 73 20 th p1..** p1 is
1a30: 31 2d 69 6e 64 65 78 65 64 2e 20 20 53 6f 20 73 1-indexed. So s
1a40: 75 62 73 74 72 28 78 2c 31 2c 31 29 20 72 65 74 ubstr(x,1,1) ret
1a50: 75 72 6e 73 20 74 68 65 20 66 69 72 73 74 20 63 urns the first c
1a60: 68 61 72 61 63 74 65 72 0a 2a 2a 20 6f 66 20 78 haracter.** of x
1a70: 2e 20 20 49 66 20 78 20 69 73 20 74 65 78 74 2c . If x is text,
1a80: 20 74 68 65 6e 20 77 65 20 61 63 74 75 61 6c 6c then we actuall
1a90: 79 20 63 6f 75 6e 74 20 55 54 46 2d 38 20 63 68 y count UTF-8 ch
1aa0: 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20 aracters..** If
1ab0: 78 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68 65 x is a blob, the
1ac0: 6e 20 77 65 20 63 6f 75 6e 74 20 62 79 74 65 73 n we count bytes
1ad0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 31 20 69 73 ..**.** If p1 is
1ae0: 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 negative, then
1af0: 77 65 20 62 65 67 69 6e 20 61 62 73 28 70 31 29 we begin abs(p1)
1b00: 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 from the end of
1b10: 20 78 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 x[]..**.** If p
1b20: 32 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 72 2 is negative, r
1b30: 65 74 75 72 6e 20 74 68 65 20 70 32 20 63 68 61 eturn the p2 cha
1b40: 72 61 63 74 65 72 73 20 70 72 65 63 65 64 69 6e racters precedin
1b50: 67 20 70 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 g p1..*/.static
1b60: 76 6f 69 64 20 73 75 62 73 74 72 46 75 6e 63 28 void substrFunc(
1b70: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
1b80: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
1b90: 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
1ba0: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
1bb0: 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 ){. const unsig
1bc0: 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 ned char *z;. c
1bd0: 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
1be0: 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 6c 65 ar *z2;. int le
1bf0: 6e 3b 0a 20 20 69 6e 74 20 70 30 74 79 70 65 3b n;. int p0type;
1c00: 0a 20 20 69 36 34 20 70 31 2c 20 70 32 3b 0a 20 . i64 p1, p2;.
1c10: 20 69 6e 74 20 6e 65 67 50 32 20 3d 20 30 3b 0a int negP2 = 0;.
1c20: 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d . assert( argc=
1c30: 3d 33 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b =3 || argc==2 );
1c40: 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 . if( sqlite3_v
1c50: 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 alue_type(argv[1
1c60: 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0a ])==SQLITE_NULL.
1c70: 20 20 20 7c 7c 20 28 61 72 67 63 3d 3d 33 20 26 || (argc==3 &
1c80: 26 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f & sqlite3_value_
1c90: 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3d 3d 53 type(argv[2])==S
1ca0: 51 4c 49 54 45 5f 4e 55 4c 4c 29 0a 20 20 29 7b QLITE_NULL). ){
1cb0: 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
1cc0: 0a 20 20 70 30 74 79 70 65 20 3d 20 73 71 6c 69 . p0type = sqli
1cd0: 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 te3_value_type(a
1ce0: 72 67 76 5b 30 5d 29 3b 0a 20 20 70 31 20 3d 20 rgv[0]);. p1 =
1cf0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e sqlite3_value_in
1d00: 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 t(argv[1]);. if
1d10: 28 20 70 30 74 79 70 65 3d 3d 53 51 4c 49 54 45 ( p0type==SQLITE
1d20: 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 6c 65 6e _BLOB ){. len
1d30: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
1d40: 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b _bytes(argv[0]);
1d50: 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 . z = sqlite3
1d60: 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 _value_blob(argv
1d70: 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d [0]);. if( z=
1d80: 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 =0 ) return;.
1d90: 20 61 73 73 65 72 74 28 20 6c 65 6e 3d 3d 73 71 assert( len==sq
1da0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
1db0: 73 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 s(argv[0]) );.
1dc0: 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 }else{. z = s
1dd0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1de0: 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 t(argv[0]);.
1df0: 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 if( z==0 ) retur
1e00: 6e 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a n;. len = 0;.
1e10: 20 20 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a if( p1<0 ){.
1e20: 20 20 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20 for(z2=z;
1e30: 2a 7a 32 3b 20 6c 65 6e 2b 2b 29 7b 0a 20 20 20 *z2; len++){.
1e40: 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 SQLITE_SKIP
1e50: 5f 55 54 46 38 28 7a 32 29 3b 0a 20 20 20 20 20 _UTF8(z2);.
1e60: 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 }. }. }. i
1e70: 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 f( argc==3 ){.
1e80: 20 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f 76 p2 = sqlite3_v
1e90: 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d alue_int(argv[2]
1ea0: 29 3b 0a 20 20 20 20 69 66 28 20 70 32 3c 30 20 );. if( p2<0
1eb0: 29 7b 0a 20 20 20 20 20 20 70 32 20 3d 20 2d 70 ){. p2 = -p
1ec0: 32 3b 0a 20 20 20 20 20 20 6e 65 67 50 32 20 3d 2;. negP2 =
1ed0: 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 1;. }. }els
1ee0: 65 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c 69 e{. p2 = sqli
1ef0: 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
1f00: 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e andle(context)->
1f10: 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1f20: 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d MIT_LENGTH];. }
1f30: 0a 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 . if( p1<0 ){.
1f40: 20 20 20 70 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 p1 += len;.
1f50: 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 if( p1<0 ){.
1f60: 20 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 p2 += p1;.
1f70: 20 20 20 20 69 66 28 20 70 32 3c 30 20 29 20 70 if( p2<0 ) p
1f80: 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 31 20 2 = 0;. p1
1f90: 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c = 0;. }. }el
1fa0: 73 65 20 69 66 28 20 70 31 3e 30 20 29 7b 0a 20 se if( p1>0 ){.
1fb0: 20 20 20 70 31 2d 2d 3b 0a 20 20 7d 65 6c 73 65 p1--;. }else
1fc0: 20 69 66 28 20 70 32 3e 30 20 29 7b 0a 20 20 20 if( p2>0 ){.
1fd0: 20 70 32 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 p2--;. }. if(
1fe0: 20 6e 65 67 50 32 20 29 7b 0a 20 20 20 20 70 31 negP2 ){. p1
1ff0: 20 2d 3d 20 70 32 3b 0a 20 20 20 20 69 66 28 20 -= p2;. if(
2000: 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32 p1<0 ){. p2
2010: 20 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20 70 31 += p1;. p1
2020: 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
2030: 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 assert( p1>=0
2040: 26 26 20 70 32 3e 3d 30 20 29 3b 0a 20 20 69 66 && p2>=0 );. if
2050: 28 20 70 30 74 79 70 65 21 3d 53 51 4c 49 54 45 ( p0type!=SQLITE
2060: 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 77 68 69 _BLOB ){. whi
2070: 6c 65 28 20 2a 7a 20 26 26 20 70 31 20 29 7b 0a le( *z && p1 ){.
2080: 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 SQLITE_SKI
2090: 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 P_UTF8(z);.
20a0: 20 70 31 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 p1--;. }.
20b0: 20 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 20 26 for(z2=z; *z2 &
20c0: 26 20 70 32 3b 20 70 32 2d 2d 29 7b 0a 20 20 20 & p2; p2--){.
20d0: 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 SQLITE_SKIP_U
20e0: 54 46 38 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 TF8(z2);. }.
20f0: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
2100: 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
2110: 28 63 68 61 72 2a 29 7a 2c 20 28 69 6e 74 29 28 (char*)z, (int)(
2120: 7a 32 2d 7a 29 2c 20 53 51 4c 49 54 45 5f 54 52 z2-z), SQLITE_TR
2130: 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 ANSIENT);. }els
2140: 65 7b 0a 20 20 20 20 69 66 28 20 70 31 2b 70 32 e{. if( p1+p2
2150: 3e 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 70 32 >len ){. p2
2160: 20 3d 20 6c 65 6e 2d 70 31 3b 0a 20 20 20 20 20 = len-p1;.
2170: 20 69 66 28 20 70 32 3c 30 20 29 20 70 32 20 3d if( p2<0 ) p2 =
2180: 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 0;. }. sq
2190: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f lite3_result_blo
21a0: 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 b(context, (char
21b0: 2a 29 26 7a 5b 70 31 5d 2c 20 28 69 6e 74 29 70 *)&z[p1], (int)p
21c0: 32 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 2, SQLITE_TRANSI
21d0: 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ENT);. }.}../*.
21e0: 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
21f0: 6e 20 6f 66 20 74 68 65 20 72 6f 75 6e 64 28 29 n of the round()
2200: 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 23 69 66 function.*/.#if
2210: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
2220: 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a _FLOATING_POINT.
2230: 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 75 6e static void roun
2240: 64 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f dFunc(sqlite3_co
2250: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 ntext *context,
2260: 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 int argc, sqlite
2270: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 3_value **argv){
2280: 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 . int n = 0;.
2290: 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 68 61 72 double r;. char
22a0: 20 2a 7a 42 75 66 3b 0a 20 20 61 73 73 65 72 74 *zBuf;. assert
22b0: 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 ( argc==1 || arg
22c0: 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 61 72 c==2 );. if( ar
22d0: 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 69 66 28 gc==2 ){. if(
22e0: 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 SQLITE_NULL==sq
22f0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
2300: 28 61 72 67 76 5b 31 5d 29 20 29 20 72 65 74 75 (argv[1]) ) retu
2310: 72 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 rn;. n = sqli
2320: 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 te3_value_int(ar
2330: 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 gv[1]);. if(
2340: 6e 3e 33 30 20 29 20 6e 20 3d 20 33 30 3b 0a 20 n>30 ) n = 30;.
2350: 20 20 20 69 66 28 20 6e 3c 30 20 29 20 6e 20 3d if( n<0 ) n =
2360: 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 0;. }. if( sq
2370: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
2380: 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 (argv[0])==SQLIT
2390: 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b E_NULL ) return;
23a0: 0a 20 20 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 . r = sqlite3_v
23b0: 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 alue_double(argv
23c0: 5b 30 5d 29 3b 0a 20 20 2f 2a 20 49 66 20 59 3d [0]);. /* If Y=
23d0: 3d 30 20 61 6e 64 20 58 20 77 69 6c 6c 20 66 69 =0 and X will fi
23e0: 74 20 69 6e 20 61 20 36 34 2d 62 69 74 20 69 6e t in a 64-bit in
23f0: 74 2c 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74 t,. ** handle t
2400: 68 65 20 72 6f 75 6e 64 69 6e 67 20 64 69 72 65 he rounding dire
2410: 63 74 6c 79 2c 0a 20 20 2a 2a 20 6f 74 68 65 72 ctly,. ** other
2420: 77 69 73 65 20 75 73 65 20 70 72 69 6e 74 66 2e wise use printf.
2430: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 3d 3d 30 . */. if( n==0
2440: 20 26 26 20 72 3e 3d 30 20 26 26 20 72 3c 4c 41 && r>=0 && r<LA
2450: 52 47 45 53 54 5f 49 4e 54 36 34 2d 31 20 29 7b RGEST_INT64-1 ){
2460: 0a 20 20 20 20 72 20 3d 20 28 64 6f 75 62 6c 65 . r = (double
2470: 29 28 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 )((sqlite_int64)
2480: 28 72 2b 30 2e 35 29 29 3b 0a 20 20 7d 65 6c 73 (r+0.5));. }els
2490: 65 20 69 66 28 20 6e 3d 3d 30 20 26 26 20 72 3c e if( n==0 && r<
24a0: 30 20 26 26 20 28 2d 72 29 3c 4c 41 52 47 45 53 0 && (-r)<LARGES
24b0: 54 5f 49 4e 54 36 34 2d 31 20 29 7b 0a 20 20 20 T_INT64-1 ){.
24c0: 20 72 20 3d 20 2d 28 64 6f 75 62 6c 65 29 28 28 r = -(double)((
24d0: 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 28 28 2d sqlite_int64)((-
24e0: 72 29 2b 30 2e 35 29 29 3b 0a 20 20 7d 65 6c 73 r)+0.5));. }els
24f0: 65 7b 0a 20 20 20 20 7a 42 75 66 20 3d 20 73 71 e{. zBuf = sq
2500: 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 lite3_mprintf("%
2510: 2e 2a 66 22 2c 6e 2c 72 29 3b 0a 20 20 20 20 69 .*f",n,r);. i
2520: 66 28 20 7a 42 75 66 3d 3d 30 20 29 7b 0a 20 20 f( zBuf==0 ){.
2530: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
2540: 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 lt_error_nomem(c
2550: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 72 ontext);. r
2560: 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 eturn;. }.
2570: 20 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 42 75 sqlite3AtoF(zBu
2580: 66 2c 20 26 72 2c 20 73 71 6c 69 74 65 33 53 74 f, &r, sqlite3St
2590: 72 6c 65 6e 33 30 28 7a 42 75 66 29 2c 20 53 51 rlen30(zBuf), SQ
25a0: 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 LITE_UTF8);.
25b0: 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 42 75 sqlite3_free(zBu
25c0: 66 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 f);. }. sqlite
25d0: 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 3_result_double(
25e0: 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a 7d 0a 23 context, r);.}.#
25f0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c endif../*.** All
2600: 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 ocate nByte byte
2610: 73 20 6f 66 20 73 70 61 63 65 20 75 73 69 6e 67 s of space using
2620: 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
2630: 29 2e 20 49 66 20 74 68 65 0a 2a 2a 20 61 6c 6c ). If the.** all
2640: 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 63 ocation fails, c
2650: 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 all sqlite3_resu
2660: 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 29 lt_error_nomem()
2670: 20 74 6f 20 6e 6f 74 69 66 79 0a 2a 2a 20 74 68 to notify.** th
2680: 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
2690: 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 e that malloc()
26a0: 68 61 73 20 66 61 69 6c 65 64 20 61 6e 64 20 72 has failed and r
26b0: 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 eturn NULL..** I
26c0: 66 20 6e 42 79 74 65 20 69 73 20 6c 61 72 67 65 f nByte is large
26d0: 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d r than the maxim
26e0: 75 6d 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f um string or blo
26f0: 62 20 6c 65 6e 67 74 68 2c 20 74 68 65 6e 0a 2a b length, then.*
2700: 2a 20 72 61 69 73 65 20 61 6e 20 53 51 4c 49 54 * raise an SQLIT
2710: 45 5f 54 4f 4f 42 49 47 20 65 78 63 65 70 74 69 E_TOOBIG excepti
2720: 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 on and return NU
2730: 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f LL..*/.static vo
2740: 69 64 20 2a 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f id *contextMallo
2750: 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 c(sqlite3_contex
2760: 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 36 34 20 t *context, i64
2770: 6e 42 79 74 65 29 7b 0a 20 20 63 68 61 72 20 2a nByte){. char *
2780: 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 z;. sqlite3 *db
2790: 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 = sqlite3_conte
27a0: 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e xt_db_handle(con
27b0: 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72 74 28 text);. assert(
27c0: 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 74 65 nByte>0 );. te
27d0: 73 74 63 61 73 65 28 20 6e 42 79 74 65 3d 3d 64 stcase( nByte==d
27e0: 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
27f0: 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 _LIMIT_LENGTH] )
2800: 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 42 ;. testcase( nB
2810: 79 74 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b yte==db->aLimit[
2820: 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
2830: 47 54 48 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20 GTH]+1 );. if(
2840: 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 nByte>db->aLimit
2850: 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 [SQLITE_LIMIT_LE
2860: 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c NGTH] ){. sql
2870: 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2880: 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 r_toobig(context
2890: 29 3b 0a 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 );. z = 0;.
28a0: 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 }else{. z = s
28b0: 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 qlite3Malloc(nBy
28c0: 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 te);. if( !z
28d0: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
28e0: 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f _result_error_no
28f0: 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 mem(context);.
2900: 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
2910: 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 z;.}../*.** Imp
2920: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
2930: 68 65 20 75 70 70 65 72 28 29 20 61 6e 64 20 6c he upper() and l
2940: 6f 77 65 72 28 29 20 53 51 4c 20 66 75 6e 63 74 ower() SQL funct
2950: 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ions..*/.static
2960: 76 6f 69 64 20 75 70 70 65 72 46 75 6e 63 28 73 void upperFunc(s
2970: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
2980: 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 context, int arg
2990: 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 c, sqlite3_value
29a0: 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63 68 61 72 **argv){. char
29b0: 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 *z1;. const ch
29c0: 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c ar *z2;. int i,
29d0: 20 6e 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 n;. UNUSED_PAR
29e0: 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 AMETER(argc);.
29f0: 7a 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 z2 = (char*)sqli
2a00: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
2a10: 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 rgv[0]);. n = s
2a20: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
2a30: 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f es(argv[0]);. /
2a40: 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 * Verify that th
2a50: 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 e call to _bytes
2a60: 28 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 () does not inva
2a70: 6c 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 lidate the _text
2a80: 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 () pointer */.
2a90: 61 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 assert( z2==(cha
2aa0: 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 r*)sqlite3_value
2ab0: 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 _text(argv[0]) )
2ac0: 3b 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 ;. if( z2 ){.
2ad0: 20 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 z1 = contextMa
2ae0: 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 lloc(context, ((
2af0: 69 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 i64)n)+1);. i
2b00: 66 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 66 f( z1 ){. f
2b10: 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b or(i=0; i<n; i++
2b20: 29 7b 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d ){. z1[i]
2b30: 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 = (char)sqlite3
2b40: 54 6f 75 70 70 65 72 28 7a 32 5b 69 5d 29 3b 0a Toupper(z2[i]);.
2b50: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
2b60: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2b70: 74 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 6e t(context, z1, n
2b80: 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b , sqlite3_free);
2b90: 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 . }. }.}.sta
2ba0: 74 69 63 20 76 6f 69 64 20 6c 6f 77 65 72 46 75 tic void lowerFu
2bb0: 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nc(sqlite3_conte
2bc0: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 xt *context, int
2bd0: 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 argc, sqlite3_v
2be0: 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 alue **argv){.
2bf0: 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 char *z1;. cons
2c00: 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e t char *z2;. in
2c10: 74 20 69 2c 20 6e 3b 0a 20 20 55 4e 55 53 45 44 t i, n;. UNUSED
2c20: 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 _PARAMETER(argc)
2c30: 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61 72 2a 29 ;. z2 = (char*)
2c40: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2c50: 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e xt(argv[0]);. n
2c60: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
2c70: 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b _bytes(argv[0]);
2c80: 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 . /* Verify tha
2c90: 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5f 62 t the call to _b
2ca0: 79 74 65 73 28 29 20 64 6f 65 73 20 6e 6f 74 20 ytes() does not
2cb0: 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 5f invalidate the _
2cc0: 74 65 78 74 28 29 20 70 6f 69 6e 74 65 72 20 2a text() pointer *
2cd0: 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 32 3d 3d /. assert( z2==
2ce0: 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 (char*)sqlite3_v
2cf0: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
2d00: 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 32 20 29 ]) );. if( z2 )
2d10: 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74 65 {. z1 = conte
2d20: 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 xtMalloc(context
2d30: 2c 20 28 28 69 36 34 29 6e 29 2b 31 29 3b 0a 20 , ((i64)n)+1);.
2d40: 20 20 20 69 66 28 20 7a 31 20 29 7b 0a 20 20 20 if( z1 ){.
2d50: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b for(i=0; i<n;
2d60: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a i++){. z
2d70: 31 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 54 6f 1[i] = sqlite3To
2d80: 6c 6f 77 65 72 28 7a 32 5b 69 5d 29 3b 0a 20 20 lower(z2[i]);.
2d90: 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
2da0: 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
2db0: 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 6e 2c 20 context, z1, n,
2dc0: 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 sqlite3_free);.
2dd0: 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
2de0: 2a 20 53 6f 6d 65 20 66 75 6e 63 74 69 6f 6e 73 * Some functions
2df0: 20 6c 69 6b 65 20 43 4f 41 4c 45 53 43 45 28 29 like COALESCE()
2e00: 20 61 6e 64 20 49 46 4e 55 4c 4c 28 29 20 61 6e and IFNULL() an
2e10: 64 20 55 4e 4c 49 4b 45 4c 59 28 29 20 61 72 65 d UNLIKELY() are
2e20: 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 implemented.**
2e30: 61 73 20 56 44 42 45 20 63 6f 64 65 20 73 6f 20 as VDBE code so
2e40: 74 68 61 74 20 75 6e 75 73 65 64 20 61 72 67 75 that unused argu
2e50: 6d 65 6e 74 20 76 61 6c 75 65 73 20 64 6f 20 6e ment values do n
2e60: 6f 74 20 68 61 76 65 20 74 6f 20 62 65 20 63 6f ot have to be co
2e70: 6d 70 75 74 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 mputed..** Howev
2e80: 65 72 2c 20 77 65 20 73 74 69 6c 6c 20 6e 65 65 er, we still nee
2e90: 64 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 66 d some kind of f
2ea0: 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e unction implemen
2eb0: 74 61 74 69 6f 6e 20 66 6f 72 20 74 68 69 73 0a tation for this.
2ec0: 2a 2a 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 ** routines in t
2ed0: 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 61 62 6c he function tabl
2ee0: 65 2e 20 20 54 68 65 20 6e 6f 6f 70 46 75 6e 63 e. The noopFunc
2ef0: 20 6d 61 63 72 6f 20 70 72 6f 76 69 64 65 73 20 macro provides
2f00: 74 68 69 73 2e 0a 2a 2a 20 6e 6f 6f 70 46 75 6e this..** noopFun
2f10: 63 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 c will never be
2f20: 63 61 6c 6c 65 64 20 73 6f 20 69 74 20 64 6f 65 called so it doe
2f30: 73 6e 27 74 20 6d 61 74 74 65 72 20 77 68 61 74 sn't matter what
2f40: 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
2f50: 69 6f 6e 0a 2a 2a 20 69 73 2e 20 20 57 65 20 6d ion.** is. We m
2f60: 69 67 68 74 20 61 73 20 77 65 6c 6c 20 75 73 65 ight as well use
2f70: 20 74 68 65 20 22 76 65 72 73 69 6f 6e 28 29 22 the "version()"
2f80: 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 61 20 73 function as a s
2f90: 75 62 73 74 69 74 75 74 65 2e 0a 2a 2f 0a 23 64 ubstitute..*/.#d
2fa0: 65 66 69 6e 65 20 6e 6f 6f 70 46 75 6e 63 20 76 efine noopFunc v
2fb0: 65 72 73 69 6f 6e 46 75 6e 63 20 20 20 2f 2a 20 ersionFunc /*
2fc0: 53 75 62 73 74 69 74 75 74 65 20 66 75 6e 63 74 Substitute funct
2fd0: 69 6f 6e 20 2d 20 6e 65 76 65 72 20 63 61 6c 6c ion - never call
2fe0: 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 ed */../*.** Imp
2ff0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 lementation of r
3000: 61 6e 64 6f 6d 28 29 2e 20 20 52 65 74 75 72 6e andom(). Return
3010: 20 61 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 a random intege
3020: 72 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 r. .*/.static v
3030: 6f 69 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28 0a oid randomFunc(.
3040: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
3050: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
3060: 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c t NotUsed,. sql
3070: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 ite3_value **Not
3080: 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 Used2.){. sqlit
3090: 65 5f 69 6e 74 36 34 20 72 3b 0a 20 20 55 4e 55 e_int64 r;. UNU
30a0: 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e SED_PARAMETER2(N
30b0: 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 otUsed, NotUsed2
30c0: 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e );. sqlite3_ran
30d0: 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 domness(sizeof(r
30e0: 29 2c 20 26 72 29 3b 0a 20 20 69 66 28 20 72 3c ), &r);. if( r<
30f0: 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 6e 0 ){. /* We n
3100: 65 65 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 eed to prevent a
3110: 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 6f random number o
3120: 66 20 30 78 38 30 30 30 30 30 30 30 30 30 30 30 f 0x800000000000
3130: 30 30 30 30 20 0a 20 20 20 20 2a 2a 20 28 6f 72 0000 . ** (or
3140: 20 2d 39 32 32 33 33 37 32 30 33 36 38 35 34 37 -92233720368547
3150: 37 35 38 30 38 29 20 73 69 6e 63 65 20 77 68 65 75808) since whe
3160: 6e 20 79 6f 75 20 64 6f 20 61 62 73 28 29 20 6f n you do abs() o
3170: 66 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 6e 75 f that. ** nu
3180: 6d 62 65 72 20 6f 66 20 79 6f 75 20 67 65 74 20 mber of you get
3190: 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 62 the same value b
31a0: 61 63 6b 20 61 67 61 69 6e 2e 20 20 54 6f 20 64 ack again. To d
31b0: 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 6e o this. ** in
31c0: 20 61 20 77 61 79 20 74 68 61 74 20 69 73 20 74 a way that is t
31d0: 65 73 74 61 62 6c 65 2c 20 6d 61 73 6b 20 74 68 estable, mask th
31e0: 65 20 73 69 67 6e 20 62 69 74 20 6f 66 66 20 6f e sign bit off o
31f0: 66 20 6e 65 67 61 74 69 76 65 0a 20 20 20 20 2a f negative. *
3200: 2a 20 76 61 6c 75 65 73 2c 20 72 65 73 75 6c 74 * values, result
3210: 69 6e 67 20 69 6e 20 61 20 70 6f 73 69 74 69 76 ing in a positiv
3220: 65 20 76 61 6c 75 65 2e 20 20 54 68 65 6e 20 74 e value. Then t
3230: 61 6b 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 ake the . **
3240: 32 73 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 6f 66 2s complement of
3250: 20 74 68 61 74 20 70 6f 73 69 74 69 76 65 20 76 that positive v
3260: 61 6c 75 65 2e 20 20 54 68 65 20 65 6e 64 20 72 alue. The end r
3270: 65 73 75 6c 74 20 63 61 6e 0a 20 20 20 20 2a 2a esult can. **
3280: 20 74 68 65 72 65 66 6f 72 65 20 62 65 20 6e 6f therefore be no
3290: 20 6c 65 73 73 20 74 68 61 6e 20 2d 39 32 32 33 less than -9223
32a0: 33 37 32 30 33 36 38 35 34 37 37 35 38 30 37 2e 372036854775807.
32b0: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 20 3d 20 . */. r =
32c0: 2d 28 72 20 26 20 4c 41 52 47 45 53 54 5f 49 4e -(r & LARGEST_IN
32d0: 54 36 34 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 T64);. }. sqli
32e0: 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 te3_result_int64
32f0: 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a 7d 0a (context, r);.}.
3300: 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
3310: 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 62 ation of randomb
3320: 6c 6f 62 28 4e 29 2e 20 20 52 65 74 75 72 6e 20 lob(N). Return
3330: 61 20 72 61 6e 64 6f 6d 20 62 6c 6f 62 0a 2a 2a a random blob.**
3340: 20 74 68 61 74 20 69 73 20 4e 20 62 79 74 65 73 that is N bytes
3350: 20 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 long..*/.static
3360: 20 76 6f 69 64 20 72 61 6e 64 6f 6d 42 6c 6f 62 void randomBlob
3370: 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
3380: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
3390: 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
33a0: 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
33b0: 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 .){. int n;. u
33c0: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 3b nsigned char *p;
33d0: 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d . assert( argc=
33e0: 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 =1 );. UNUSED_P
33f0: 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a ARAMETER(argc);.
3400: 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 n = sqlite3_va
3410: 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d 29 lue_int(argv[0])
3420: 3b 0a 20 20 69 66 28 20 6e 3c 31 20 29 7b 0a 20 ;. if( n<1 ){.
3430: 20 20 20 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 n = 1;. }.
3440: 70 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f p = contextMallo
3450: 63 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b 0a 20 c(context, n);.
3460: 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 if( p ){. sq
3470: 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
3480: 28 6e 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 (n, p);. sqli
3490: 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 te3_result_blob(
34a0: 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 context, (char*)
34b0: 70 2c 20 6e 2c 20 73 71 6c 69 74 65 33 5f 66 72 p, n, sqlite3_fr
34c0: 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ee);. }.}../*.*
34d0: 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
34e0: 20 6f 66 20 74 68 65 20 6c 61 73 74 5f 69 6e 73 of the last_ins
34f0: 65 72 74 5f 72 6f 77 69 64 28 29 20 53 51 4c 20 ert_rowid() SQL
3500: 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 function. The r
3510: 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 eturn.** value i
3520: 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 s the same as th
3530: 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 e sqlite3_last_i
3540: 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 41 50 nsert_rowid() AP
3550: 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 I function..*/.s
3560: 74 61 74 69 63 20 76 6f 69 64 20 6c 61 73 74 5f tatic void last_
3570: 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 0a 20 20 insert_rowid(.
3580: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
3590: 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 *context, . int
35a0: 20 4e 6f 74 55 73 65 64 2c 20 0a 20 20 73 71 6c NotUsed, . sql
35b0: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 ite3_value **Not
35c0: 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 Used2.){. sqlit
35d0: 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 e3 *db = sqlite3
35e0: 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
35f0: 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 le(context);. U
3600: 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
3610: 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
3620: 64 32 29 3b 0a 20 20 2f 2a 20 49 4d 50 3a 20 52 d2);. /* IMP: R
3630: 2d 35 31 35 31 33 2d 31 32 30 32 36 20 54 68 65 -51513-12026 The
3640: 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 last_insert_row
3650: 69 64 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f id() SQL functio
3660: 6e 20 69 73 20 61 0a 20 20 2a 2a 20 77 72 61 70 n is a. ** wrap
3670: 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 73 per around the s
3680: 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 qlite3_last_inse
3690: 72 74 5f 72 6f 77 69 64 28 29 20 43 2f 43 2b 2b rt_rowid() C/C++
36a0: 20 69 6e 74 65 72 66 61 63 65 0a 20 20 2a 2a 20 interface. **
36b0: 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 73 function. */. s
36c0: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
36d0: 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c t64(context, sql
36e0: 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 ite3_last_insert
36f0: 5f 72 6f 77 69 64 28 64 62 29 29 3b 0a 7d 0a 0a _rowid(db));.}..
3700: 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
3710: 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 61 6e tion of the chan
3720: 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 ges() SQL functi
3730: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 3a 20 52 on..**.** IMP: R
3740: 2d 36 32 30 37 33 2d 31 31 32 30 39 20 54 68 65 -62073-11209 The
3750: 20 63 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 changes() SQL f
3760: 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61 unction is a wra
3770: 70 70 65 72 0a 2a 2a 20 61 72 6f 75 6e 64 20 74 pper.** around t
3780: 68 65 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 he sqlite3_chang
3790: 65 73 28 29 20 43 2f 43 2b 2b 20 66 75 6e 63 74 es() C/C++ funct
37a0: 69 6f 6e 20 61 6e 64 20 68 65 6e 63 65 20 66 6f ion and hence fo
37b0: 6c 6c 6f 77 73 20 74 68 65 20 73 61 6d 65 0a 2a llows the same.*
37c0: 2a 20 72 75 6c 65 73 20 66 6f 72 20 63 6f 75 6e * rules for coun
37d0: 74 69 6e 67 20 63 68 61 6e 67 65 73 2e 0a 2a 2f ting changes..*/
37e0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 61 .static void cha
37f0: 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 65 33 5f nges(. sqlite3_
3800: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
3810: 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c ,. int NotUsed,
3820: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
3830: 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 **NotUsed2.){.
3840: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 sqlite3 *db = s
3850: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
3860: 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 b_handle(context
3870: 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
3880: 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 METER2(NotUsed,
3890: 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c NotUsed2);. sql
38a0: 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 ite3_result_int(
38b0: 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 context, sqlite3
38c0: 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d _changes(db));.}
38d0: 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
38e0: 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 6f tation of the to
38f0: 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 53 51 tal_changes() SQ
3900: 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 L function. The
3910: 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 return value is
3920: 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 .** the same as
3930: 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 the sqlite3_tota
3940: 6c 5f 63 68 61 6e 67 65 73 28 29 20 41 50 49 20 l_changes() API
3950: 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 function..*/.sta
3960: 74 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 5f 63 tic void total_c
3970: 68 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 65 hanges(. sqlite
3980: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
3990: 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
39a0: 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
39b0: 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b ue **NotUsed2.){
39c0: 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
39d0: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
39e0: 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 _db_handle(conte
39f0: 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 xt);. UNUSED_PA
3a00: 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 RAMETER2(NotUsed
3a10: 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 2f , NotUsed2);. /
3a20: 2a 20 49 4d 50 3a 20 52 2d 35 32 37 35 36 2d 34 * IMP: R-52756-4
3a30: 31 39 39 33 20 54 68 69 73 20 66 75 6e 63 74 69 1993 This functi
3a40: 6f 6e 20 69 73 20 61 20 77 72 61 70 70 65 72 20 on is a wrapper
3a50: 61 72 6f 75 6e 64 20 74 68 65 0a 20 20 2a 2a 20 around the. **
3a60: 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 sqlite3_total_ch
3a70: 61 6e 67 65 73 28 29 20 43 2f 43 2b 2b 20 69 6e anges() C/C++ in
3a80: 74 65 72 66 61 63 65 2e 20 2a 2f 0a 20 20 73 71 terface. */. sq
3a90: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 lite3_result_int
3aa0: 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 (context, sqlite
3ab0: 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 3_total_changes(
3ac0: 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 db));.}../*.** A
3ad0: 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69 6e structure defin
3ae0: 69 6e 67 20 68 6f 77 20 74 6f 20 64 6f 20 47 4c ing how to do GL
3af0: 4f 42 2d 73 74 79 6c 65 20 63 6f 6d 70 61 72 69 OB-style compari
3b00: 73 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 sons..*/.struct
3b10: 63 6f 6d 70 61 72 65 49 6e 66 6f 20 7b 0a 20 20 compareInfo {.
3b20: 75 38 20 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 75 u8 matchAll;. u
3b30: 38 20 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75 38 8 matchOne;. u8
3b40: 20 6d 61 74 63 68 53 65 74 3b 0a 20 20 75 38 20 matchSet;. u8
3b50: 6e 6f 43 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a noCase;.};../*.*
3b60: 2a 20 46 6f 72 20 4c 49 4b 45 20 61 6e 64 20 47 * For LIKE and G
3b70: 4c 4f 42 20 6d 61 74 63 68 69 6e 67 20 6f 6e 20 LOB matching on
3b80: 45 42 43 44 49 43 20 6d 61 63 68 69 6e 65 73 2c EBCDIC machines,
3b90: 20 61 73 73 75 6d 65 20 74 68 61 74 20 65 76 65 assume that eve
3ba0: 72 79 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 ry.** character
3bb0: 69 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 62 is exactly one b
3bc0: 79 74 65 20 69 6e 20 73 69 7a 65 2e 20 20 41 6c yte in size. Al
3bd0: 73 6f 2c 20 61 6c 6c 20 63 68 61 72 61 63 74 65 so, all characte
3be0: 72 73 20 61 72 65 0a 2a 2a 20 61 62 6c 65 20 74 rs are.** able t
3bf0: 6f 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e o participate in
3c00: 20 75 70 70 65 72 2d 63 61 73 65 2d 74 6f 2d 6c upper-case-to-l
3c10: 6f 77 65 72 2d 63 61 73 65 20 6d 61 70 70 69 6e ower-case mappin
3c20: 67 73 20 69 6e 20 45 42 43 44 49 43 0a 2a 2a 20 gs in EBCDIC.**
3c30: 77 68 65 72 65 61 73 20 6f 6e 6c 79 20 63 68 61 whereas only cha
3c40: 72 61 63 74 65 72 73 20 6c 65 73 73 20 74 68 61 racters less tha
3c50: 6e 20 30 78 38 30 20 64 6f 20 69 6e 20 41 53 43 n 0x80 do in ASC
3c60: 49 49 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e II..*/.#if defin
3c70: 65 64 28 53 51 4c 49 54 45 5f 45 42 43 44 49 43 ed(SQLITE_EBCDIC
3c80: 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
3c90: 65 33 55 74 66 38 52 65 61 64 28 41 29 20 20 20 e3Utf8Read(A)
3ca0: 20 28 2a 28 28 2a 41 29 2b 2b 29 29 0a 23 20 64 (*((*A)++)).# d
3cb0: 65 66 69 6e 65 20 47 6c 6f 62 55 70 70 65 72 54 efine GlobUpperT
3cc0: 6f 4c 6f 77 65 72 28 41 29 20 20 20 41 20 3d 20 oLower(A) A =
3cd0: 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f sqlite3UpperToLo
3ce0: 77 65 72 5b 41 5d 0a 23 65 6c 73 65 0a 23 20 64 wer[A].#else.# d
3cf0: 65 66 69 6e 65 20 47 6c 6f 62 55 70 70 65 72 54 efine GlobUpperT
3d00: 6f 4c 6f 77 65 72 28 41 29 20 20 20 69 66 28 20 oLower(A) if(
3d10: 21 28 28 41 29 26 7e 30 78 37 66 29 20 29 7b 20 !((A)&~0x7f) ){
3d20: 41 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 A = sqlite3Upper
3d30: 54 6f 4c 6f 77 65 72 5b 41 5d 3b 20 7d 0a 23 65 ToLower[A]; }.#e
3d40: 6e 64 69 66 0a 0a 73 74 61 74 69 63 20 63 6f 6e ndif..static con
3d50: 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 st struct compar
3d60: 65 49 6e 66 6f 20 67 6c 6f 62 49 6e 66 6f 20 3d eInfo globInfo =
3d70: 20 7b 20 27 2a 27 2c 20 27 3f 27 2c 20 27 5b 27 { '*', '?', '['
3d80: 2c 20 30 20 7d 3b 0a 2f 2a 20 54 68 65 20 63 6f , 0 };./* The co
3d90: 72 72 65 63 74 20 53 51 4c 2d 39 32 20 62 65 68 rrect SQL-92 beh
3da0: 61 76 69 6f 72 20 69 73 20 66 6f 72 20 74 68 65 avior is for the
3db0: 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 LIKE operator t
3dc0: 6f 20 69 67 6e 6f 72 65 0a 2a 2a 20 63 61 73 65 o ignore.** case
3dd0: 2e 20 20 54 68 75 73 20 20 27 61 27 20 4c 49 4b . Thus 'a' LIK
3de0: 45 20 27 41 27 20 77 6f 75 6c 64 20 62 65 20 74 E 'A' would be t
3df0: 72 75 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 rue. */.static c
3e00: 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 onst struct comp
3e10: 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f areInfo likeInfo
3e20: 4e 6f 72 6d 20 3d 20 7b 20 27 25 27 2c 20 27 5f Norm = { '%', '_
3e30: 27 2c 20 20 20 30 2c 20 31 20 7d 3b 0a 2f 2a 20 ', 0, 1 };./*
3e40: 49 66 20 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 If SQLITE_CASE_S
3e50: 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 20 69 73 ENSITIVE_LIKE is
3e60: 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 defined, then t
3e70: 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 he LIKE operator
3e80: 0a 2a 2a 20 69 73 20 63 61 73 65 20 73 65 6e 73 .** is case sens
3e90: 69 74 69 76 65 20 63 61 75 73 69 6e 67 20 27 61 itive causing 'a
3ea0: 27 20 4c 49 4b 45 20 27 41 27 20 74 6f 20 62 65 ' LIKE 'A' to be
3eb0: 20 66 61 6c 73 65 20 2a 2f 0a 73 74 61 74 69 63 false */.static
3ec0: 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f const struct co
3ed0: 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e mpareInfo likeIn
3ee0: 66 6f 41 6c 74 20 3d 20 7b 20 27 25 27 2c 20 27 foAlt = { '%', '
3ef0: 5f 27 2c 20 20 20 30 2c 20 30 20 7d 3b 0a 0a 2f _', 0, 0 };../
3f00: 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f *.** Compare two
3f10: 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 UTF-8 strings f
3f20: 6f 72 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 or equality wher
3f30: 65 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69 e the first stri
3f40: 6e 67 20 63 61 6e 0a 2a 2a 20 70 6f 74 65 6e 74 ng can.** potent
3f50: 69 61 6c 6c 79 20 62 65 20 61 20 22 67 6c 6f 62 ially be a "glob
3f60: 22 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 52 " expression. R
3f70: 65 74 75 72 6e 20 74 72 75 65 20 28 31 29 20 69 eturn true (1) i
3f80: 66 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 74 68 f they.** are th
3f90: 65 20 73 61 6d 65 20 61 6e 64 20 66 61 6c 73 65 e same and false
3fa0: 20 28 30 29 20 69 66 20 74 68 65 79 20 61 72 65 (0) if they are
3fb0: 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 0a 2a different..**.*
3fc0: 2a 20 47 6c 6f 62 62 69 6e 67 20 72 75 6c 65 73 * Globbing rules
3fd0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 2a 27 :.**.** '*'
3fe0: 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 61 Matches a
3ff0: 6e 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20 7a ny sequence of z
4000: 65 72 6f 20 6f 72 20 6d 6f 72 65 20 63 68 61 72 ero or more char
4010: 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 acters..**.**
4020: 20 20 20 27 3f 27 20 20 20 20 20 20 20 4d 61 74 '?' Mat
4030: 63 68 65 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 ches exactly one
4040: 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 0a 2a character..**.*
4050: 2a 20 20 20 20 20 5b 2e 2e 2e 5d 20 20 20 20 20 * [...]
4060: 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 63 68 61 Matches one cha
4070: 72 61 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20 racter from the
4080: 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74 20 6f 66 enclosed list of
4090: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
40a0: 20 20 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a characters..*
40b0: 2a 0a 2a 2a 20 20 20 20 20 5b 5e 2e 2e 2e 5d 20 *.** [^...]
40c0: 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 Matches one
40d0: 63 68 61 72 61 63 74 65 72 20 6e 6f 74 20 69 6e character not in
40e0: 20 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 the enclosed li
40f0: 73 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 st..**.** With t
4100: 68 65 20 5b 2e 2e 2e 5d 20 61 6e 64 20 5b 5e 2e he [...] and [^.
4110: 2e 2e 5d 20 6d 61 74 63 68 69 6e 67 2c 20 61 20 ..] matching, a
4120: 27 5d 27 20 63 68 61 72 61 63 74 65 72 20 63 61 ']' character ca
4130: 6e 20 62 65 20 69 6e 63 6c 75 64 65 64 0a 2a 2a n be included.**
4140: 20 69 6e 20 74 68 65 20 6c 69 73 74 20 62 79 20 in the list by
4150: 6d 61 6b 69 6e 67 20 69 74 20 74 68 65 20 66 69 making it the fi
4160: 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66 rst character af
4170: 74 65 72 20 27 5b 27 20 6f 72 20 27 5e 27 2e 20 ter '[' or '^'.
4180: 20 41 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 63 A.** range of c
4190: 68 61 72 61 63 74 65 72 73 20 63 61 6e 20 62 65 haracters can be
41a0: 20 73 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 specified using
41b0: 20 27 2d 27 2e 20 20 45 78 61 6d 70 6c 65 3a 0a '-'. Example:.
41c0: 2a 2a 20 22 5b 61 2d 7a 5d 22 20 6d 61 74 63 68 ** "[a-z]" match
41d0: 65 73 20 61 6e 79 20 73 69 6e 67 6c 65 20 6c 6f es any single lo
41e0: 77 65 72 2d 63 61 73 65 20 6c 65 74 74 65 72 2e wer-case letter.
41f0: 20 20 54 6f 20 6d 61 74 63 68 20 61 20 27 2d 27 To match a '-'
4200: 2c 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 74 68 65 , make.** it the
4210: 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20 last character
4220: 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a in the list..**.
4230: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
4240: 69 73 20 75 73 75 61 6c 6c 79 20 71 75 69 63 6b is usually quick
4250: 2c 20 62 75 74 20 63 61 6e 20 62 65 20 4e 2a 2a , but can be N**
4260: 32 20 69 6e 20 74 68 65 20 77 6f 72 73 74 20 63 2 in the worst c
4270: 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 48 69 6e 74 73 ase..**.** Hints
4280: 3a 20 74 6f 20 6d 61 74 63 68 20 27 2a 27 20 6f : to match '*' o
4290: 72 20 27 3f 27 2c 20 70 75 74 20 74 68 65 6d 20 r '?', put them
42a0: 69 6e 20 22 5b 5d 22 2e 20 20 4c 69 6b 65 20 74 in "[]". Like t
42b0: 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 his:.**.**
42c0: 20 20 20 61 62 63 5b 2a 5d 78 79 7a 20 20 20 20 abc[*]xyz
42d0: 20 20 20 20 4d 61 74 63 68 65 73 20 22 61 62 63 Matches "abc
42e0: 2a 78 79 7a 22 20 6f 6e 6c 79 0a 2a 2f 0a 73 74 *xyz" only.*/.st
42f0: 61 74 69 63 20 69 6e 74 20 70 61 74 74 65 72 6e atic int pattern
4300: 43 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 Compare(. const
4310: 20 75 38 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 u8 *zPattern,
4320: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
4330: 68 65 20 67 6c 6f 62 20 70 61 74 74 65 72 6e 20 he glob pattern
4340: 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a */. const u8 *z
4350: 53 74 72 69 6e 67 2c 20 20 20 20 20 20 20 20 20 String,
4360: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 /* The str
4370: 69 6e 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 ing to compare a
4380: 67 61 69 6e 73 74 20 74 68 65 20 67 6c 6f 62 20 gainst the glob
4390: 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63 */. const struc
43a0: 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 t compareInfo *p
43b0: 49 6e 66 6f 2c 20 2f 2a 20 49 6e 66 6f 72 6d 61 Info, /* Informa
43c0: 74 69 6f 6e 20 61 62 6f 75 74 20 68 6f 77 20 74 tion about how t
43d0: 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 65 o do the compare
43e0: 20 2a 2f 0a 20 20 75 33 32 20 65 73 63 20 20 20 */. u32 esc
43f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4400: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 /* The es
4410: 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20 2a cape character *
4420: 2f 0a 29 7b 0a 20 20 75 33 32 20 63 2c 20 63 32 /.){. u32 c, c2
4430: 3b 0a 20 20 69 6e 74 20 69 6e 76 65 72 74 3b 0a ;. int invert;.
4440: 20 20 69 6e 74 20 73 65 65 6e 3b 0a 20 20 75 38 int seen;. u8
4450: 20 6d 61 74 63 68 4f 6e 65 20 3d 20 70 49 6e 66 matchOne = pInf
4460: 6f 2d 3e 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75 o->matchOne;. u
4470: 38 20 6d 61 74 63 68 41 6c 6c 20 3d 20 70 49 6e 8 matchAll = pIn
4480: 66 6f 2d 3e 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 fo->matchAll;.
4490: 75 38 20 6d 61 74 63 68 53 65 74 20 3d 20 70 49 u8 matchSet = pI
44a0: 6e 66 6f 2d 3e 6d 61 74 63 68 53 65 74 3b 0a 20 nfo->matchSet;.
44b0: 20 75 38 20 6e 6f 43 61 73 65 20 3d 20 70 49 6e u8 noCase = pIn
44c0: 66 6f 2d 3e 6e 6f 43 61 73 65 3b 20 0a 20 20 69 fo->noCase; . i
44d0: 6e 74 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 nt prevEscape =
44e0: 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 0; /* True i
44f0: 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 f the previous c
4500: 68 61 72 61 63 74 65 72 20 77 61 73 20 27 65 73 haracter was 'es
4510: 63 61 70 65 27 20 2a 2f 0a 0a 20 20 77 68 69 6c cape' */.. whil
4520: 65 28 20 28 63 20 3d 20 73 71 6c 69 74 65 33 55 e( (c = sqlite3U
4530: 74 66 38 52 65 61 64 28 26 7a 50 61 74 74 65 72 tf8Read(&zPatter
4540: 6e 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 n))!=0 ){. if
4550: 28 20 63 3d 3d 6d 61 74 63 68 41 6c 6c 20 26 26 ( c==matchAll &&
4560: 20 21 70 72 65 76 45 73 63 61 70 65 20 29 7b 0a !prevEscape ){.
4570: 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d while( (c=
4580: 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
4590: 26 7a 50 61 74 74 65 72 6e 29 29 20 3d 3d 20 6d &zPattern)) == m
45a0: 61 74 63 68 41 6c 6c 0a 20 20 20 20 20 20 20 20 atchAll.
45b0: 20 20 20 20 20 20 20 7c 7c 20 63 20 3d 3d 20 6d || c == m
45c0: 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20 atchOne ){.
45d0: 20 20 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 4f if( c==matchO
45e0: 6e 65 20 26 26 20 73 71 6c 69 74 65 33 55 74 66 ne && sqlite3Utf
45f0: 38 52 65 61 64 28 26 7a 53 74 72 69 6e 67 29 3d 8Read(&zString)=
4600: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
4610: 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 return 0;.
4620: 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
4630: 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 if( c==0 ){.
4640: 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
4650: 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
4660: 63 3d 3d 65 73 63 20 29 7b 0a 20 20 20 20 20 20 c==esc ){.
4670: 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 c = sqlite3Utf
4680: 38 52 65 61 64 28 26 7a 50 61 74 74 65 72 6e 29 8Read(&zPattern)
4690: 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d ;. if( c=
46a0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
46b0: 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 return 0;.
46c0: 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 }. }else
46d0: 69 66 28 20 63 3d 3d 6d 61 74 63 68 53 65 74 20 if( c==matchSet
46e0: 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
46f0: 74 28 20 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 t( esc==0 );
4700: 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 /* This is
4710: 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20 2a GLOB, not LIKE *
4720: 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 /. assert
4730: 28 20 6d 61 74 63 68 53 65 74 3c 30 78 38 30 20 ( matchSet<0x80
4740: 29 3b 20 20 2f 2a 20 27 5b 27 20 69 73 20 61 20 ); /* '[' is a
4750: 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72 single-byte char
4760: 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 acter */.
4770: 20 77 68 69 6c 65 28 20 2a 7a 53 74 72 69 6e 67 while( *zString
4780: 20 26 26 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 && patternCompa
4790: 72 65 28 26 7a 50 61 74 74 65 72 6e 5b 2d 31 5d re(&zPattern[-1]
47a0: 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 ,zString,pInfo,e
47b0: 73 63 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 sc)==0 ){.
47c0: 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f SQLITE_SKIP_
47d0: 55 54 46 38 28 7a 53 74 72 69 6e 67 29 3b 0a 20 UTF8(zString);.
47e0: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
47f0: 20 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 return *zString
4800: 21 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 !=0;. }.
4810: 20 20 20 77 68 69 6c 65 28 20 28 63 32 20 3d 20 while( (c2 =
4820: 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
4830: 26 7a 53 74 72 69 6e 67 29 29 21 3d 30 20 29 7b &zString))!=0 ){
4840: 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43 . if( noC
4850: 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ase ){.
4860: 20 47 6c 6f 62 55 70 70 65 72 54 6f 4c 6f 77 65 GlobUpperToLowe
4870: 72 28 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20 r(c2);.
4880: 20 47 6c 6f 62 55 70 70 65 72 54 6f 4c 6f 77 65 GlobUpperToLowe
4890: 72 28 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 r(c);.
48a0: 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26 while( c2 != 0 &
48b0: 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 & c2 != c ){.
48c0: 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 c2 = sq
48d0: 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 26 7a lite3Utf8Read(&z
48e0: 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 String);.
48f0: 20 20 20 20 20 47 6c 6f 62 55 70 70 65 72 54 6f GlobUpperTo
4900: 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20 Lower(c2);.
4910: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
4920: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
4930: 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26 while( c2 != 0 &
4940: 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 & c2 != c ){.
4950: 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 c2 = sq
4960: 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 26 7a lite3Utf8Read(&z
4970: 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 String);.
4980: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
4990: 20 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30 if( c2==0
49a0: 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
49b0: 20 20 20 20 20 69 66 28 20 70 61 74 74 65 72 6e if( pattern
49c0: 43 6f 6d 70 61 72 65 28 7a 50 61 74 74 65 72 6e Compare(zPattern
49d0: 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 ,zString,pInfo,e
49e0: 73 63 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a sc) ) return 1;.
49f0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
4a00: 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 turn 0;. }els
4a10: 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 4f 6e e if( c==matchOn
4a20: 65 20 26 26 20 21 70 72 65 76 45 73 63 61 70 65 e && !prevEscape
4a30: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 ){. if( sq
4a40: 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 26 7a lite3Utf8Read(&z
4a50: 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20 20 String)==0 ){.
4a60: 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
4a70: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
4a80: 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 53 65 e if( c==matchSe
4a90: 74 20 29 7b 0a 20 20 20 20 20 20 75 33 32 20 70 t ){. u32 p
4aa0: 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20 20 rior_c = 0;.
4ab0: 20 20 61 73 73 65 72 74 28 20 65 73 63 3d 3d 30 assert( esc==0
4ac0: 20 29 3b 20 20 20 20 2f 2a 20 54 68 69 73 20 6f ); /* This o
4ad0: 6e 6c 79 20 6f 63 63 75 72 73 20 66 6f 72 20 47 nly occurs for G
4ae0: 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20 2a 2f LOB, not LIKE */
4af0: 0a 20 20 20 20 20 20 73 65 65 6e 20 3d 20 30 3b . seen = 0;
4b00: 0a 20 20 20 20 20 20 69 6e 76 65 72 74 20 3d 20 . invert =
4b10: 30 3b 0a 20 20 20 20 20 20 63 20 3d 20 73 71 6c 0;. c = sql
4b20: 69 74 65 33 55 74 66 38 52 65 61 64 28 26 7a 53 ite3Utf8Read(&zS
4b30: 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 tring);. if
4b40: 28 20 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( c==0 ) return
4b50: 30 3b 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71 0;. c2 = sq
4b60: 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 26 7a lite3Utf8Read(&z
4b70: 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 Pattern);.
4b80: 69 66 28 20 63 32 3d 3d 27 5e 27 20 29 7b 0a 20 if( c2=='^' ){.
4b90: 20 20 20 20 20 20 20 69 6e 76 65 72 74 20 3d 20 invert =
4ba0: 31 3b 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20 1;. c2 =
4bb0: 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
4bc0: 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 &zPattern);.
4bd0: 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 32 }. if( c2
4be0: 3d 3d 27 5d 27 20 29 7b 0a 20 20 20 20 20 20 20 ==']' ){.
4bf0: 20 69 66 28 20 63 3d 3d 27 5d 27 20 29 20 73 65 if( c==']' ) se
4c00: 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 en = 1;.
4c10: 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 c2 = sqlite3Utf8
4c20: 52 65 61 64 28 26 7a 50 61 74 74 65 72 6e 29 3b Read(&zPattern);
4c30: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 . }. w
4c40: 68 69 6c 65 28 20 63 32 20 26 26 20 63 32 21 3d hile( c2 && c2!=
4c50: 27 5d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ']' ){. i
4c60: 66 28 20 63 32 3d 3d 27 2d 27 20 26 26 20 7a 50 f( c2=='-' && zP
4c70: 61 74 74 65 72 6e 5b 30 5d 21 3d 27 5d 27 20 26 attern[0]!=']' &
4c80: 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 30 & zPattern[0]!=0
4c90: 20 26 26 20 70 72 69 6f 72 5f 63 3e 30 20 29 7b && prior_c>0 ){
4ca0: 0a 20 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 . c2 =
4cb0: 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
4cc0: 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 &zPattern);.
4cd0: 20 20 20 20 20 20 69 66 28 20 63 3e 3d 70 72 69 if( c>=pri
4ce0: 6f 72 5f 63 20 26 26 20 63 3c 3d 63 32 20 29 20 or_c && c<=c2 )
4cf0: 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 seen = 1;.
4d00: 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b prior_c = 0;
4d10: 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
4d20: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3d if( c=
4d30: 3d 63 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 =c2 ){.
4d40: 20 20 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 seen = 1;.
4d50: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
4d60: 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 63 32 3b prior_c = c2;
4d70: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
4d80: 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 c2 = sqlite3U
4d90: 74 66 38 52 65 61 64 28 26 7a 50 61 74 74 65 72 tf8Read(&zPatter
4da0: 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 n);. }.
4db0: 20 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20 28 if( c2==0 || (
4dc0: 73 65 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d 3d seen ^ invert)==
4dd0: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 0 ){. ret
4de0: 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 urn 0;. }.
4df0: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73 63 }else if( esc
4e00: 3d 3d 63 20 26 26 20 21 70 72 65 76 45 73 63 61 ==c && !prevEsca
4e10: 70 65 20 29 7b 0a 20 20 20 20 20 20 70 72 65 76 pe ){. prev
4e20: 45 73 63 61 70 65 20 3d 20 31 3b 0a 20 20 20 20 Escape = 1;.
4e30: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 32 20 }else{. c2
4e40: 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 = sqlite3Utf8Rea
4e50: 64 28 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 d(&zString);.
4e60: 20 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29 7b if( noCase ){
4e70: 0a 20 20 20 20 20 20 20 20 47 6c 6f 62 55 70 70 . GlobUpp
4e80: 65 72 54 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 erToLower(c);.
4e90: 20 20 20 20 20 20 47 6c 6f 62 55 70 70 65 72 54 GlobUpperT
4ea0: 6f 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20 20 20 oLower(c2);.
4eb0: 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 21 }. if( c!
4ec0: 3d 63 32 20 29 7b 0a 20 20 20 20 20 20 20 20 72 =c2 ){. r
4ed0: 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d eturn 0;. }
4ee0: 0a 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70 . prevEscap
4ef0: 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d e = 0;. }. }
4f00: 0a 20 20 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 . return *zStri
4f10: 6e 67 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ng==0;.}../*.**
4f20: 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 72 67 The sqlite3_strg
4f30: 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 2e lob() interface.
4f40: 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f .*/.int sqlite3_
4f50: 73 74 72 67 6c 6f 62 28 63 6f 6e 73 74 20 63 68 strglob(const ch
4f60: 61 72 20 2a 7a 47 6c 6f 62 50 61 74 74 65 72 6e ar *zGlobPattern
4f70: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 , const char *zS
4f80: 74 72 69 6e 67 29 7b 0a 20 20 72 65 74 75 72 6e tring){. return
4f90: 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 patternCompare(
4fa0: 28 75 38 2a 29 7a 47 6c 6f 62 50 61 74 74 65 72 (u8*)zGlobPatter
4fb0: 6e 2c 20 28 75 38 2a 29 7a 53 74 72 69 6e 67 2c n, (u8*)zString,
4fc0: 20 26 67 6c 6f 62 49 6e 66 6f 2c 20 30 29 3d 3d &globInfo, 0)==
4fd0: 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 0;.}../*.** Coun
4fe0: 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
4ff0: 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 4c times that the L
5000: 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f 72 IKE operator (or
5010: 20 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a 2a GLOB which is.*
5020: 2a 20 6a 75 73 74 20 61 20 76 61 72 69 61 74 69 * just a variati
5030: 6f 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74 73 on of LIKE) gets
5040: 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 called. This i
5050: 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 s used for testi
5060: 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 ng.** only..*/.#
5070: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
5080: 54 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 T.int sqlite3_li
5090: 6b 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 ke_count = 0;.#e
50a0: 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 ndif.../*.** Imp
50b0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
50c0: 68 65 20 6c 69 6b 65 28 29 20 53 51 4c 20 66 75 he like() SQL fu
50d0: 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 nction. This fu
50e0: 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 nction implement
50f0: 73 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 s.** the build-i
5100: 6e 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2e n LIKE operator.
5110: 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 The first argu
5120: 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 ment to the func
5130: 74 69 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 70 tion is the.** p
5140: 61 74 74 65 72 6e 20 61 6e 64 20 74 68 65 20 73 attern and the s
5150: 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
5160: 73 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 53 s the string. S
5170: 6f 2c 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 o, the SQL state
5180: 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ments:.**.**
5190: 20 20 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a A LIKE B.**.*
51a0: 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 * is implemented
51b0: 20 61 73 20 6c 69 6b 65 28 42 2c 41 29 2e 0a 2a as like(B,A)..*
51c0: 2a 0a 2a 2a 20 54 68 69 73 20 73 61 6d 65 20 66 *.** This same f
51d0: 75 6e 63 74 69 6f 6e 20 28 77 69 74 68 20 61 20 unction (with a
51e0: 64 69 66 66 65 72 65 6e 74 20 63 6f 6d 70 61 72 different compar
51f0: 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 29 eInfo structure)
5200: 20 63 6f 6d 70 75 74 65 73 0a 2a 2a 20 74 68 65 computes.** the
5210: 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 2e 0a GLOB operator..
5220: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c */.static void l
5230: 69 6b 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 ikeFunc(. sqlit
5240: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
5250: 65 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 ext, . int argc
5260: 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c , . sqlite3_val
5270: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 ue **argv.){. c
5280: 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
5290: 61 72 20 2a 7a 41 2c 20 2a 7a 42 3b 0a 20 20 75 ar *zA, *zB;. u
52a0: 33 32 20 65 73 63 61 70 65 20 3d 20 30 3b 0a 20 32 escape = 0;.
52b0: 20 69 6e 74 20 6e 50 61 74 3b 0a 20 20 73 71 6c int nPat;. sql
52c0: 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 ite3 *db = sqlit
52d0: 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
52e0: 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a ndle(context);..
52f0: 20 20 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76 zB = sqlite3_v
5300: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
5310: 5d 29 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74 ]);. zA = sqlit
5320: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
5330: 67 76 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69 gv[1]);.. /* Li
5340: 6d 69 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f mit the length o
5350: 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c f the LIKE or GL
5360: 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76 OB pattern to av
5370: 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a oid problems. *
5380: 2a 20 6f 66 20 64 65 65 70 20 72 65 63 75 72 73 * of deep recurs
5390: 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61 ion and N*N beha
53a0: 76 69 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43 vior in patternC
53b0: 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20 ompare().. */.
53c0: 20 6e 50 61 74 20 3d 20 73 71 6c 69 74 65 33 5f nPat = sqlite3_
53d0: 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 value_bytes(argv
53e0: 5b 30 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 [0]);. testcase
53f0: 28 20 6e 50 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d ( nPat==db->aLim
5400: 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
5410: 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e LIKE_PATTERN_LEN
5420: 47 54 48 5d 20 29 3b 0a 20 20 74 65 73 74 63 61 GTH] );. testca
5430: 73 65 28 20 6e 50 61 74 3d 3d 64 62 2d 3e 61 4c se( nPat==db->aL
5440: 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
5450: 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c T_LIKE_PATTERN_L
5460: 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 69 66 ENGTH]+1 );. if
5470: 28 20 6e 50 61 74 20 3e 20 64 62 2d 3e 61 4c 69 ( nPat > db->aLi
5480: 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
5490: 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 _LIKE_PATTERN_LE
54a0: 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c NGTH] ){. sql
54b0: 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
54c0: 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b 45 r(context, "LIKE
54d0: 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e or GLOB pattern
54e0: 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20 2d too complex", -
54f0: 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 1);. return;.
5500: 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 42 }. assert( zB
5510: 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ==sqlite3_value_
5520: 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b text(argv[0]) );
5530: 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 64 69 /* Encoding di
5540: 64 20 6e 6f 74 20 63 68 61 6e 67 65 20 2a 2f 0a d not change */.
5550: 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29 . if( argc==3 )
5560: 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 {. /* The esc
5570: 61 70 65 20 63 68 61 72 61 63 74 65 72 20 73 74 ape character st
5580: 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69 73 ring must consis
5590: 74 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 55 54 t of a single UT
55a0: 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 F-8 character..
55b0: 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c ** Otherwise,
55c0: 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 return an error
55d0: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 6f 6e .. */. con
55e0: 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
55f0: 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69 74 65 33 *zEsc = sqlite3
5600: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
5610: 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 [2]);. if( zE
5620: 73 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a sc==0 ) return;.
5630: 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 55 if( sqlite3U
5640: 74 66 38 43 68 61 72 4c 65 6e 28 28 63 68 61 72 tf8CharLen((char
5650: 2a 29 7a 45 73 63 2c 20 2d 31 29 21 3d 31 20 29 *)zEsc, -1)!=1 )
5660: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
5670: 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e result_error(con
5680: 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20 text, .
5690: 20 22 45 53 43 41 50 45 20 65 78 70 72 65 73 73 "ESCAPE express
56a0: 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 20 73 69 ion must be a si
56b0: 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72 22 2c ngle character",
56c0: 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 -1);. retu
56d0: 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 73 rn;. }. es
56e0: 63 61 70 65 20 3d 20 73 71 6c 69 74 65 33 55 74 cape = sqlite3Ut
56f0: 66 38 52 65 61 64 28 26 7a 45 73 63 29 3b 0a 20 f8Read(&zEsc);.
5700: 20 7d 0a 20 20 69 66 28 20 7a 41 20 26 26 20 7a }. if( zA && z
5710: 42 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 B ){. struct
5720: 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e compareInfo *pIn
5730: 66 6f 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 fo = sqlite3_use
5740: 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b r_data(context);
5750: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
5760: 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f EST. sqlite3_
5770: 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 like_count++;.#e
5780: 6e 64 69 66 0a 20 20 20 20 0a 20 20 20 20 73 71 ndif. . sq
5790: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 lite3_result_int
57a0: 28 63 6f 6e 74 65 78 74 2c 20 70 61 74 74 65 72 (context, patter
57b0: 6e 43 6f 6d 70 61 72 65 28 7a 42 2c 20 7a 41 2c nCompare(zB, zA,
57c0: 20 70 49 6e 66 6f 2c 20 65 73 63 61 70 65 29 29 pInfo, escape))
57d0: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
57e0: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
57f0: 20 74 68 65 20 4e 55 4c 4c 49 46 28 78 2c 79 29 the NULLIF(x,y)
5800: 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 function. The
5810: 72 65 73 75 6c 74 20 69 73 20 74 68 65 20 66 69 result is the fi
5820: 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 rst.** argument
5830: 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 if the arguments
5840: 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 20 are different.
5850: 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e The result is N
5860: 55 4c 4c 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 ULL if the.** ar
5870: 67 75 6d 65 6e 74 73 20 61 72 65 20 65 71 75 61 guments are equa
5880: 6c 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 2e l to each other.
5890: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
58a0: 6e 75 6c 6c 69 66 46 75 6e 63 28 0a 20 20 73 71 nullifFunc(. sq
58b0: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
58c0: 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f ontext,. int No
58d0: 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 tUsed,. sqlite3
58e0: 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
58f0: 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c . CollSeq *pCol
5900: 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75 l = sqlite3GetFu
5910: 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78 ncCollSeq(contex
5920: 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 t);. UNUSED_PAR
5930: 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
5940: 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d 65 . if( sqlite3Me
5950: 6d 43 6f 6d 70 61 72 65 28 61 72 67 76 5b 30 5d mCompare(argv[0]
5960: 2c 20 61 72 67 76 5b 31 5d 2c 20 70 43 6f 6c 6c , argv[1], pColl
5970: 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 )!=0 ){. sqli
5980: 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
5990: 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 (context, argv[0
59a0: 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a ]);. }.}../*.**
59b0: 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
59c0: 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 76 65 of the sqlite_ve
59d0: 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e rsion() function
59e0: 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 . The result is
59f0: 20 74 68 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 the version.**
5a00: 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 of the SQLite li
5a10: 62 72 61 72 79 20 74 68 61 74 20 69 73 20 72 75 brary that is ru
5a20: 6e 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 nning..*/.static
5a30: 20 76 6f 69 64 20 76 65 72 73 69 6f 6e 46 75 6e void versionFun
5a40: 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
5a50: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
5a60: 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 int NotUsed,.
5a70: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
5a80: 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e NotUsed2.){. UN
5a90: 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 USED_PARAMETER2(
5aa0: 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 NotUsed, NotUsed
5ab0: 32 29 3b 0a 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 2);. /* IMP: R-
5ac0: 34 38 36 39 39 2d 34 38 36 31 37 20 54 68 69 73 48699-48617 This
5ad0: 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20 function is an
5ae0: 53 51 4c 20 77 72 61 70 70 65 72 20 61 72 6f 75 SQL wrapper arou
5af0: 6e 64 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 nd the. ** sqli
5b00: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 29 te3_libversion()
5b10: 20 43 2d 69 6e 74 65 72 66 61 63 65 2e 20 2a 2f C-interface. */
5b20: 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
5b30: 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
5b40: 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 sqlite3_libversi
5b50: 6f 6e 28 29 2c 20 2d 31 2c 20 53 51 4c 49 54 45 on(), -1, SQLITE
5b60: 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a _STATIC);.}../*.
5b70: 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
5b80: 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f n of the sqlite_
5b90: 73 6f 75 72 63 65 5f 69 64 28 29 20 66 75 6e 63 source_id() func
5ba0: 74 69 6f 6e 2e 20 54 68 65 20 72 65 73 75 6c 74 tion. The result
5bb0: 20 69 73 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 is a string.**
5bc0: 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 20 that identifies
5bd0: 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 the particular v
5be0: 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 6f ersion of the so
5bf0: 75 72 63 65 20 63 6f 64 65 20 75 73 65 64 20 74 urce code used t
5c00: 6f 20 62 75 69 6c 64 0a 2a 2a 20 53 51 4c 69 74 o build.** SQLit
5c10: 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
5c20: 64 20 73 6f 75 72 63 65 69 64 46 75 6e 63 28 0a d sourceidFunc(.
5c30: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
5c40: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
5c50: 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c t NotUsed,. sql
5c60: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 ite3_value **Not
5c70: 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 Used2.){. UNUSE
5c80: 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 D_PARAMETER2(Not
5c90: 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b Used, NotUsed2);
5ca0: 0a 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 32 34 34 . /* IMP: R-244
5cb0: 37 30 2d 33 31 31 33 36 20 54 68 69 73 20 66 75 70-31136 This fu
5cc0: 6e 63 74 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c nction is an SQL
5cd0: 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 wrapper around
5ce0: 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 the. ** sqlite3
5cf0: 5f 73 6f 75 72 63 65 69 64 28 29 20 43 20 69 6e _sourceid() C in
5d00: 74 65 72 66 61 63 65 2e 20 2a 2f 0a 20 20 73 71 terface. */. sq
5d10: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
5d20: 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 t(context, sqlit
5d30: 65 33 5f 73 6f 75 72 63 65 69 64 28 29 2c 20 2d e3_sourceid(), -
5d40: 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 1, SQLITE_STATIC
5d50: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c );.}../*.** Impl
5d60: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
5d70: 65 20 73 71 6c 69 74 65 5f 6c 6f 67 28 29 20 66 e sqlite_log() f
5d80: 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 unction. This i
5d90: 73 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 s a wrapper arou
5da0: 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 6f nd.** sqlite3_lo
5db0: 67 28 29 2e 20 20 54 68 65 20 72 65 74 75 72 6e g(). The return
5dc0: 20 76 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2e 20 value is NULL.
5dd0: 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 65 78 The function ex
5de0: 69 73 74 73 20 70 75 72 65 6c 79 20 66 6f 72 0a ists purely for.
5df0: 2a 2a 20 69 74 73 20 73 69 64 65 2d 65 66 66 65 ** its side-effe
5e00: 63 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 cts..*/.static v
5e10: 6f 69 64 20 65 72 72 6c 6f 67 46 75 6e 63 28 0a oid errlogFunc(.
5e20: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
5e30: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
5e40: 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
5e50: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
5e60: 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
5e70: 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 55 4e ETER(argc);. UN
5e80: 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 63 USED_PARAMETER(c
5e90: 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 ontext);. sqlit
5ea0: 65 33 5f 6c 6f 67 28 73 71 6c 69 74 65 33 5f 76 e3_log(sqlite3_v
5eb0: 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 30 5d alue_int(argv[0]
5ec0: 29 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 ), "%s", sqlite3
5ed0: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
5ee0: 5b 31 5d 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 [1]));.}../*.**
5ef0: 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
5f00: 66 20 74 68 65 20 73 71 6c 69 74 65 5f 63 6f 6d f the sqlite_com
5f10: 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 28 pileoption_used(
5f20: 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 ) function..** T
5f30: 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6e 20 he result is an
5f40: 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 64 65 integer that ide
5f50: 6e 74 69 66 69 65 73 20 69 66 20 74 68 65 20 63 ntifies if the c
5f60: 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6f 6e 0a 2a ompiler option.*
5f70: 2a 20 77 61 73 20 75 73 65 64 20 74 6f 20 62 75 * was used to bu
5f80: 69 6c 64 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 ild SQLite..*/.#
5f90: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
5fa0: 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e IT_COMPILEOPTION
5fb0: 5f 44 49 41 47 53 0a 73 74 61 74 69 63 20 76 6f _DIAGS.static vo
5fc0: 69 64 20 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e id compileoption
5fd0: 75 73 65 64 46 75 6e 63 28 0a 20 20 73 71 6c 69 usedFunc(. sqli
5fe0: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
5ff0: 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
6000: 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
6010: 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f e **argv.){. co
6020: 6e 73 74 20 63 68 61 72 20 2a 7a 4f 70 74 4e 61 nst char *zOptNa
6030: 6d 65 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 me;. assert( ar
6040: 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 gc==1 );. UNUSE
6050: 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 D_PARAMETER(argc
6060: 29 3b 0a 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 33 );. /* IMP: R-3
6070: 39 35 36 34 2d 33 36 33 30 35 20 54 68 65 20 73 9564-36305 The s
6080: 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 74 qlite_compileopt
6090: 69 6f 6e 5f 75 73 65 64 28 29 20 53 51 4c 0a 20 ion_used() SQL.
60a0: 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 ** function is
60b0: 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 a wrapper around
60c0: 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d the sqlite3_com
60d0: 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 28 pileoption_used(
60e0: 29 20 43 2f 43 2b 2b 0a 20 20 2a 2a 20 66 75 6e ) C/C++. ** fun
60f0: 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 ction.. */. if
6100: 28 20 28 7a 4f 70 74 4e 61 6d 65 20 3d 20 28 63 ( (zOptName = (c
6110: 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 onst char*)sqlit
6120: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
6130: 67 76 5b 30 5d 29 29 21 3d 30 20 29 7b 0a 20 20 gv[0]))!=0 ){.
6140: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
6150: 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 _int(context, sq
6160: 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 lite3_compileopt
6170: 69 6f 6e 5f 75 73 65 64 28 7a 4f 70 74 4e 61 6d ion_used(zOptNam
6180: 65 29 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 e));. }.}.#endi
6190: 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
61a0: 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 _COMPILEOPTION_D
61b0: 49 41 47 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 IAGS */../*.** I
61c0: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
61d0: 20 74 68 65 20 73 71 6c 69 74 65 5f 63 6f 6d 70 the sqlite_comp
61e0: 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 29 20 ileoption_get()
61f0: 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 20 54 68 function. .** Th
6200: 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 73 74 e result is a st
6210: 72 69 6e 67 20 74 68 61 74 20 69 64 65 6e 74 69 ring that identi
6220: 66 69 65 73 20 74 68 65 20 63 6f 6d 70 69 6c 65 fies the compile
6230: 72 20 6f 70 74 69 6f 6e 73 20 0a 2a 2a 20 75 73 r options .** us
6240: 65 64 20 74 6f 20 62 75 69 6c 64 20 53 51 4c 69 ed to build SQLi
6250: 74 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 te..*/.#ifndef S
6260: 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 QLITE_OMIT_COMPI
6270: 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 0a 73 LEOPTION_DIAGS.s
6280: 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 69 tatic void compi
6290: 6c 65 6f 70 74 69 6f 6e 67 65 74 46 75 6e 63 28 leoptiongetFunc(
62a0: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
62b0: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
62c0: 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
62d0: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
62e0: 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 61 73 ){. int n;. as
62f0: 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b sert( argc==1 );
6300: 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
6310: 54 45 52 28 61 72 67 63 29 3b 0a 20 20 2f 2a 20 TER(argc);. /*
6320: 49 4d 50 3a 20 52 2d 30 34 39 32 32 2d 32 34 30 IMP: R-04922-240
6330: 37 36 20 54 68 65 20 73 71 6c 69 74 65 5f 63 6f 76 The sqlite_co
6340: 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 mpileoption_get(
6350: 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 0a 20 ) SQL function.
6360: 20 2a 2a 20 69 73 20 61 20 77 72 61 70 70 65 72 ** is a wrapper
6370: 20 61 72 6f 75 6e 64 20 74 68 65 20 73 71 6c 69 around the sqli
6380: 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f te3_compileoptio
6390: 6e 5f 67 65 74 28 29 20 43 2f 43 2b 2b 20 66 75 n_get() C/C++ fu
63a0: 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 6e nction.. */. n
63b0: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
63c0: 5f 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 _int(argv[0]);.
63d0: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
63e0: 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 text(context, sq
63f0: 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 lite3_compileopt
6400: 69 6f 6e 5f 67 65 74 28 6e 29 2c 20 2d 31 2c 20 ion_get(n), -1,
6410: 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a SQLITE_STATIC);.
6420: 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
6430: 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f TE_OMIT_COMPILEO
6440: 50 54 49 4f 4e 5f 44 49 41 47 53 20 2a 2f 0a 0a PTION_DIAGS */..
6450: 2f 2a 20 41 72 72 61 79 20 66 6f 72 20 63 6f 6e /* Array for con
6460: 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 68 61 6c verting from hal
6470: 66 2d 62 79 74 65 73 20 28 6e 79 62 62 6c 65 73 f-bytes (nybbles
6480: 29 20 69 6e 74 6f 20 41 53 43 49 49 20 68 65 78 ) into ASCII hex
6490: 0a 2a 2a 20 64 69 67 69 74 73 2e 20 2a 2f 0a 73 .** digits. */.s
64a0: 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
64b0: 20 68 65 78 64 69 67 69 74 73 5b 5d 20 3d 20 7b hexdigits[] = {
64c0: 0a 20 20 27 30 27 2c 20 27 31 27 2c 20 27 32 27 . '0', '1', '2'
64d0: 2c 20 27 33 27 2c 20 27 34 27 2c 20 27 35 27 2c , '3', '4', '5',
64e0: 20 27 36 27 2c 20 27 37 27 2c 0a 20 20 27 38 27 '6', '7',. '8'
64f0: 2c 20 27 39 27 2c 20 27 41 27 2c 20 27 42 27 2c , '9', 'A', 'B',
6500: 20 27 43 27 2c 20 27 44 27 2c 20 27 45 27 2c 20 'C', 'D', 'E',
6510: 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 'F' .};../*.** I
6520: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
6530: 20 74 68 65 20 51 55 4f 54 45 28 29 20 66 75 6e the QUOTE() fun
6540: 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e ction. This fun
6550: 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 ction takes a si
6560: 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 ngle.** argument
6570: 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 . If the argume
6580: 6e 74 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 74 nt is numeric, t
6590: 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
65a0: 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 0a 2a is the same as.*
65b0: 2a 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 20 * the argument.
65c0: 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 If the argument
65d0: 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 is NULL, the re
65e0: 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 turn value is th
65f0: 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 4e 55 4c e string.** "NUL
6600: 4c 22 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 L". Otherwise,
6610: 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 the argument is
6620: 65 6e 63 6c 6f 73 65 64 20 69 6e 20 73 69 6e 67 enclosed in sing
6630: 6c 65 20 71 75 6f 74 65 73 20 77 69 74 68 0a 2a le quotes with.*
6640: 2a 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 20 65 * single-quote e
6650: 73 63 61 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 scapes..*/.stati
6660: 63 20 76 6f 69 64 20 71 75 6f 74 65 46 75 6e 63 c void quoteFunc
6670: 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
6680: 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 *context, int a
6690: 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c rgc, sqlite3_val
66a0: 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 61 73 ue **argv){. as
66b0: 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b sert( argc==1 );
66c0: 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
66d0: 54 45 52 28 61 72 67 63 29 3b 0a 20 20 73 77 69 TER(argc);. swi
66e0: 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c tch( sqlite3_val
66f0: 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 ue_type(argv[0])
6700: 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ){. case SQL
6710: 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 ITE_FLOAT: {.
6720: 20 20 20 64 6f 75 62 6c 65 20 72 31 2c 20 72 32 double r1, r2
6730: 3b 0a 20 20 20 20 20 20 63 68 61 72 20 7a 42 75 ;. char zBu
6740: 66 5b 35 30 5d 3b 0a 20 20 20 20 20 20 72 31 20 f[50];. r1
6750: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
6760: 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b double(argv[0]);
6770: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
6780: 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
6790: 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 21 2e Buf), zBuf, "%!.
67a0: 31 35 67 22 2c 20 72 31 29 3b 0a 20 20 20 20 20 15g", r1);.
67b0: 20 73 71 6c 69 74 65 33 41 74 6f 46 28 7a 42 75 sqlite3AtoF(zBu
67c0: 66 2c 20 26 72 32 2c 20 32 30 2c 20 53 51 4c 49 f, &r2, 20, SQLI
67d0: 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20 TE_UTF8);.
67e0: 69 66 28 20 72 31 21 3d 72 32 20 29 7b 0a 20 20 if( r1!=r2 ){.
67f0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
6800: 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 printf(sizeof(zB
6810: 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 21 2e 32 uf), zBuf, "%!.2
6820: 30 65 22 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 0e", r1);.
6830: 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
6840: 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 result_text(cont
6850: 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 ext, zBuf, -1, S
6860: 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
6870: 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
6880: 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 }. case SQ
6890: 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a LITE_INTEGER: {.
68a0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 sqlite3_re
68b0: 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 sult_value(conte
68c0: 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 xt, argv[0]);.
68d0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
68e0: 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
68f0: 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 63 _BLOB: {. c
6900: 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a har *zText = 0;.
6910: 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 char const
6920: 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 *zBlob = sqlite
6930: 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 3_value_blob(arg
6940: 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74 v[0]);. int
6950: 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 nBlob = sqlite3
6960: 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 _value_bytes(arg
6970: 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 v[0]);. ass
6980: 65 72 74 28 20 7a 42 6c 6f 62 3d 3d 73 71 6c 69 ert( zBlob==sqli
6990: 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 te3_value_blob(a
69a0: 72 67 76 5b 30 5d 29 20 29 3b 20 2f 2a 20 4e 6f rgv[0]) ); /* No
69b0: 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 encoding change
69c0: 20 2a 2f 0a 20 20 20 20 20 20 7a 54 65 78 74 20 */. zText
69d0: 3d 20 28 63 68 61 72 20 2a 29 63 6f 6e 74 65 78 = (char *)contex
69e0: 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c tMalloc(context,
69f0: 20 28 32 2a 28 69 36 34 29 6e 42 6c 6f 62 29 2b (2*(i64)nBlob)+
6a00: 34 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 7a 4); . if( z
6a10: 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Text ){.
6a20: 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 int i;. f
6a30: 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62 3b or(i=0; i<nBlob;
6a40: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 i++){.
6a50: 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b 32 5d 20 zText[(i*2)+2]
6a60: 3d 20 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c = hexdigits[(zBl
6a70: 6f 62 5b 69 5d 3e 3e 34 29 26 30 78 30 46 5d 3b ob[i]>>4)&0x0F];
6a80: 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 78 74 . zText
6a90: 5b 28 69 2a 32 29 2b 33 5d 20 3d 20 68 65 78 64 [(i*2)+3] = hexd
6aa0: 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 29 igits[(zBlob[i])
6ab0: 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 &0x0F];.
6ac0: 7d 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b }. zText[
6ad0: 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27 (nBlob*2)+2] = '
6ae0: 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 \'';. zTe
6af0: 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20 xt[(nBlob*2)+3]
6b00: 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 = '\0';.
6b10: 7a 54 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0a zText[0] = 'X';.
6b20: 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 31 5d zText[1]
6b30: 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 = '\'';.
6b40: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
6b50: 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 54 text(context, zT
6b60: 65 78 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f ext, -1, SQLITE_
6b70: 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 TRANSIENT);.
6b80: 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
6b90: 28 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7d (zText);. }
6ba0: 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
6bb0: 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
6bc0: 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 ITE_TEXT: {.
6bd0: 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 20 20 20 20 int i,j;.
6be0: 20 75 36 34 20 6e 3b 0a 20 20 20 20 20 20 63 6f u64 n;. co
6bf0: 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
6c00: 72 20 2a 7a 41 72 67 20 3d 20 73 71 6c 69 74 65 r *zArg = sqlite
6c10: 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
6c20: 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 63 68 61 v[0]);. cha
6c30: 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 20 69 66 28 r *z;.. if(
6c40: 20 7a 41 72 67 3d 3d 30 20 29 20 72 65 74 75 72 zArg==0 ) retur
6c50: 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 n;. for(i=0
6c60: 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20 , n=0; zArg[i];
6c70: 69 2b 2b 29 7b 20 69 66 28 20 7a 41 72 67 5b 69 i++){ if( zArg[i
6c80: 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d ]=='\'' ) n++; }
6c90: 0a 20 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65 . z = conte
6ca0: 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 xtMalloc(context
6cb0: 2c 20 28 28 69 36 34 29 69 29 2b 28 28 69 36 34 , ((i64)i)+((i64
6cc0: 29 6e 29 2b 33 29 3b 0a 20 20 20 20 20 20 69 66 )n)+3);. if
6cd0: 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 7a ( z ){. z
6ce0: 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 [0] = '\'';.
6cf0: 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31 for(i=0, j=1
6d00: 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b ; zArg[i]; i++){
6d10: 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b . z[j++
6d20: 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a 20 20 20 ] = zArg[i];.
6d30: 20 20 20 20 20 20 20 69 66 28 20 7a 41 72 67 5b if( zArg[
6d40: 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 i]=='\'' ){.
6d50: 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d z[j++] =
6d60: 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 20 '\'';.
6d70: 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
6d80: 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c z[j++] = '\
6d90: 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 5d '';. z[j]
6da0: 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 = 0;. sq
6db0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
6dc0: 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c t(context, z, j,
6dd0: 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a sqlite3_free);.
6de0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
6df0: 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
6e00: 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
6e10: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
6e20: 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b value_type(argv[
6e30: 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0])==SQLITE_NULL
6e40: 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
6e50: 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
6e60: 6e 74 65 78 74 2c 20 22 4e 55 4c 4c 22 2c 20 34 ntext, "NULL", 4
6e70: 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 , SQLITE_STATIC)
6e80: 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
6e90: 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
6ea0: 2a 20 54 68 65 20 75 6e 69 63 6f 64 65 28 29 20 * The unicode()
6eb0: 66 75 6e 63 74 69 6f 6e 2e 20 20 52 65 74 75 72 function. Retur
6ec0: 6e 20 74 68 65 20 69 6e 74 65 67 65 72 20 75 6e n the integer un
6ed0: 69 63 6f 64 65 20 63 6f 64 65 2d 70 6f 69 6e 74 icode code-point
6ee0: 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 value.** for th
6ef0: 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 e first characte
6f00: 72 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 73 r of the input s
6f10: 74 72 69 6e 67 2e 20 0a 2a 2f 0a 73 74 61 74 69 tring. .*/.stati
6f20: 63 20 76 6f 69 64 20 75 6e 69 63 6f 64 65 46 75 c void unicodeFu
6f30: 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
6f40: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
6f50: 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
6f60: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
6f70: 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e gv.){. const un
6f80: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d signed char *z =
6f90: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
6fa0: 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 ext(argv[0]);.
6fb0: 28 76 6f 69 64 29 61 72 67 63 3b 0a 20 20 69 66 (void)argc;. if
6fc0: 28 20 7a 20 26 26 20 7a 5b 30 5d 20 29 20 73 71 ( z && z[0] ) sq
6fd0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 lite3_result_int
6fe0: 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 (context, sqlite
6ff0: 33 55 74 66 38 52 65 61 64 28 26 7a 29 29 3b 0a 3Utf8Read(&z));.
7000: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 68 61 }../*.** The cha
7010: 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b r() function tak
7020: 65 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 es zero or more
7030: 61 72 67 75 6d 65 6e 74 73 2c 20 65 61 63 68 20 arguments, each
7040: 6f 66 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 61 of which is.** a
7050: 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 74 20 63 n integer. It c
7060: 6f 6e 73 74 72 75 63 74 73 20 61 20 73 74 72 69 onstructs a stri
7070: 6e 67 20 77 68 65 72 65 20 65 61 63 68 20 63 68 ng where each ch
7080: 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 20 73 aracter of the s
7090: 74 72 69 6e 67 0a 2a 2a 20 69 73 20 74 68 65 20 tring.** is the
70a0: 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 unicode characte
70b0: 72 20 66 6f 72 20 74 68 65 20 63 6f 72 72 65 73 r for the corres
70c0: 70 6f 6e 64 69 6e 67 20 69 6e 74 65 67 65 72 20 ponding integer
70d0: 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 argument..*/.sta
70e0: 74 69 63 20 76 6f 69 64 20 63 68 61 72 46 75 6e tic void charFun
70f0: 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
7100: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
7110: 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c int argc,. sql
7120: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
7130: 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 v.){. unsigned
7140: 63 68 61 72 20 2a 7a 2c 20 2a 7a 4f 75 74 3b 0a char *z, *zOut;.
7150: 20 20 69 6e 74 20 69 3b 0a 20 20 7a 4f 75 74 20 int i;. zOut
7160: 3d 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 = z = sqlite3_ma
7170: 6c 6c 6f 63 28 20 61 72 67 63 2a 34 2b 31 20 29 lloc( argc*4+1 )
7180: 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a ;. if( z==0 ){.
7190: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
71a0: 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 lt_error_nomem(c
71b0: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 74 ontext);. ret
71c0: 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 urn;. }. for(i
71d0: 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 =0; i<argc; i++)
71e0: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e {. sqlite3_in
71f0: 74 36 34 20 78 3b 0a 20 20 20 20 75 6e 73 69 67 t64 x;. unsig
7200: 6e 65 64 20 63 3b 0a 20 20 20 20 78 20 3d 20 73 ned c;. x = s
7210: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
7220: 36 34 28 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 64(argv[i]);.
7230: 20 69 66 28 20 78 3c 30 20 7c 7c 20 78 3e 30 78 if( x<0 || x>0x
7240: 31 30 66 66 66 66 20 29 20 78 20 3d 20 30 78 66 10ffff ) x = 0xf
7250: 66 66 64 3b 0a 20 20 20 20 63 20 3d 20 28 75 6e ffd;. c = (un
7260: 73 69 67 6e 65 64 29 28 78 20 26 20 30 78 31 66 signed)(x & 0x1f
7270: 66 66 66 66 29 3b 0a 20 20 20 20 69 66 28 20 63 ffff);. if( c
7280: 3c 30 78 30 30 30 38 30 20 29 7b 0a 20 20 20 20 <0x00080 ){.
7290: 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
72a0: 28 63 26 30 78 46 46 29 3b 0a 20 20 20 20 7d 65 (c&0xFF);. }e
72b0: 6c 73 65 20 69 66 28 20 63 3c 30 78 30 30 38 30 lse if( c<0x0080
72c0: 30 20 29 7b 0a 20 20 20 20 20 20 2a 7a 4f 75 74 0 ){. *zOut
72d0: 2b 2b 20 3d 20 30 78 43 30 20 2b 20 28 75 38 29 ++ = 0xC0 + (u8)
72e0: 28 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 0a 20 ((c>>6)&0x1F);.
72f0: 20 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 *zOut++ = 0
7300: 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 x80 + (u8)(c & 0
7310: 78 33 46 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 x3F);. }else
7320: 69 66 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b if( c<0x10000 ){
7330: 0a 20 20 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d . *zOut++ =
7340: 20 30 78 45 30 20 2b 20 28 75 38 29 28 28 63 3e 0xE0 + (u8)((c>
7350: 3e 31 32 29 26 30 78 30 46 29 3b 0a 20 20 20 20 >12)&0x0F);.
7360: 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 *zOut++ = 0x80
7370: 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 20 26 + (u8)((c>>6) &
7380: 20 30 78 33 46 29 3b 0a 20 20 20 20 20 20 2a 7a 0x3F);. *z
7390: 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 Out++ = 0x80 + (
73a0: 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 0a 20 u8)(c & 0x3F);.
73b0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
73c0: 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 46 30 20 2b *zOut++ = 0xF0 +
73d0: 20 28 75 38 29 28 28 63 3e 3e 31 38 29 20 26 20 (u8)((c>>18) &
73e0: 30 78 30 37 29 3b 0a 20 20 20 20 20 20 2a 7a 4f 0x07);. *zO
73f0: 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 ut++ = 0x80 + (u
7400: 38 29 28 28 63 3e 3e 31 32 29 20 26 20 30 78 33 8)((c>>12) & 0x3
7410: 46 29 3b 0a 20 20 20 20 20 20 2a 7a 4f 75 74 2b F);. *zOut+
7420: 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 + = 0x80 + (u8)(
7430: 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 0a (c>>6) & 0x3F);.
7440: 20 20 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 *zOut++ =
7450: 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 0x80 + (u8)(c &
7460: 30 78 33 46 29 3b 0a 20 20 20 20 7d 20 20 20 20 0x3F);. }
7470: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7480: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7490: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
74a0: 5c 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f \. }. sqlite3_
74b0: 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 result_text(cont
74c0: 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 28 ext, (char*)z, (
74d0: 69 6e 74 29 28 7a 4f 75 74 2d 7a 29 2c 20 73 71 int)(zOut-z), sq
74e0: 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a lite3_free);.}..
74f0: 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 78 28 29 20 /*.** The hex()
7500: 66 75 6e 63 74 69 6f 6e 2e 20 20 49 6e 74 65 72 function. Inter
7510: 70 72 65 74 20 74 68 65 20 61 72 67 75 6d 65 6e pret the argumen
7520: 74 20 61 73 20 61 20 62 6c 6f 62 2e 20 20 52 65 t as a blob. Re
7530: 74 75 72 6e 0a 2a 2a 20 61 20 68 65 78 61 64 65 turn.** a hexade
7540: 63 69 6d 61 6c 20 72 65 6e 64 65 72 69 6e 67 20 cimal rendering
7550: 61 73 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 as text..*/.stat
7560: 69 63 20 76 6f 69 64 20 68 65 78 46 75 6e 63 28 ic void hexFunc(
7570: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
7580: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
7590: 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
75a0: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
75b0: 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 ){. int i, n;.
75c0: 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
75d0: 63 68 61 72 20 2a 70 42 6c 6f 62 3b 0a 20 20 63 char *pBlob;. c
75e0: 68 61 72 20 2a 7a 48 65 78 2c 20 2a 7a 3b 0a 20 har *zHex, *z;.
75f0: 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 assert( argc==1
7600: 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 );. UNUSED_PAR
7610: 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 AMETER(argc);.
7620: 70 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f pBlob = sqlite3_
7630: 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b value_blob(argv[
7640: 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 0]);. n = sqlit
7650: 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
7660: 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 rgv[0]);. asser
7670: 74 28 20 70 42 6c 6f 62 3d 3d 73 71 6c 69 74 65 t( pBlob==sqlite
7680: 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 3_value_blob(arg
7690: 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 v[0]) ); /* No
76a0: 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 encoding change
76b0: 2a 2f 0a 20 20 7a 20 3d 20 7a 48 65 78 20 3d 20 */. z = zHex =
76c0: 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f contextMalloc(co
76d0: 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2a ntext, ((i64)n)*
76e0: 32 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 7a 48 2 + 1);. if( zH
76f0: 65 78 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d ex ){. for(i=
7700: 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c 0; i<n; i++, pBl
7710: 6f 62 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e 73 ob++){. uns
7720: 69 67 6e 65 64 20 63 68 61 72 20 63 20 3d 20 2a igned char c = *
7730: 70 42 6c 6f 62 3b 0a 20 20 20 20 20 20 2a 28 7a pBlob;. *(z
7740: 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b ++) = hexdigits[
7750: 28 63 3e 3e 34 29 26 30 78 66 5d 3b 0a 20 20 20 (c>>4)&0xf];.
7760: 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 *(z++) = hexd
7770: 69 67 69 74 73 5b 63 26 30 78 66 5d 3b 0a 20 20 igits[c&0xf];.
7780: 20 20 7d 0a 20 20 20 20 2a 7a 20 3d 20 30 3b 0a }. *z = 0;.
7790: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
77a0: 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
77b0: 20 7a 48 65 78 2c 20 6e 2a 32 2c 20 73 71 6c 69 zHex, n*2, sqli
77c0: 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d te3_free);. }.}
77d0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 7a 65 72 6f ../*.** The zero
77e0: 62 6c 6f 62 28 4e 29 20 66 75 6e 63 74 69 6f 6e blob(N) function
77f0: 20 72 65 74 75 72 6e 73 20 61 20 7a 65 72 6f 2d returns a zero-
7800: 66 69 6c 6c 65 64 20 62 6c 6f 62 20 6f 66 20 73 filled blob of s
7810: 69 7a 65 20 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a ize N bytes..*/.
7820: 73 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f static void zero
7830: 62 6c 6f 62 46 75 6e 63 28 0a 20 20 73 71 6c 69 blobFunc(. sqli
7840: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
7850: 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
7860: 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
7870: 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 36 e **argv.){. i6
7880: 34 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 4 n;. sqlite3 *
7890: 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
78a0: 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
78b0: 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72 ontext);. asser
78c0: 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 t( argc==1 );.
78d0: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
78e0: 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73 71 (argc);. n = sq
78f0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 lite3_value_int6
7900: 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65 4(argv[0]);. te
7910: 73 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e 61 stcase( n==db->a
7920: 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
7930: 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 IT_LENGTH] );.
7940: 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d testcase( n==db-
7950: 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
7960: 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 IMIT_LENGTH]+1 )
7970: 3b 0a 20 20 69 66 28 20 6e 3e 64 62 2d 3e 61 4c ;. if( n>db->aL
7980: 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
7990: 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
79a0: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
79b0: 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e error_toobig(con
79c0: 74 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a text);. }else{.
79d0: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
79e0: 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e 74 lt_zeroblob(cont
79f0: 65 78 74 2c 20 28 69 6e 74 29 6e 29 3b 20 2f 2a ext, (int)n); /*
7a00: 20 49 4d 50 3a 20 52 2d 30 30 32 39 33 2d 36 34 IMP: R-00293-64
7a10: 39 39 34 20 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 994 */. }.}../*
7a20: 0a 2a 2a 20 54 68 65 20 72 65 70 6c 61 63 65 28 .** The replace(
7a30: 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 72 ) function. Thr
7a40: 65 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 ee arguments are
7a50: 20 61 6c 6c 20 73 74 72 69 6e 67 73 3a 20 63 61 all strings: ca
7a60: 6c 6c 0a 2a 2a 20 74 68 65 6d 20 41 2c 20 42 2c ll.** them A, B,
7a70: 20 61 6e 64 20 43 2e 20 54 68 65 20 72 65 73 75 and C. The resu
7a80: 6c 74 20 69 73 20 61 6c 73 6f 20 61 20 73 74 72 lt is also a str
7a90: 69 6e 67 20 77 68 69 63 68 20 69 73 20 64 65 72 ing which is der
7aa0: 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 ived.** from A b
7ab0: 79 20 72 65 70 6c 61 63 69 6e 67 20 65 76 65 72 y replacing ever
7ac0: 79 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 20 y occurrence of
7ad0: 42 20 77 69 74 68 20 43 2e 20 20 54 68 65 20 6d B with C. The m
7ae0: 61 74 63 68 0a 2a 2a 20 6d 75 73 74 20 62 65 20 atch.** must be
7af0: 65 78 61 63 74 2e 20 20 43 6f 6c 6c 61 74 69 6e exact. Collatin
7b00: 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 20 g sequences are
7b10: 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 not used..*/.sta
7b20: 74 69 63 20 76 6f 69 64 20 72 65 70 6c 61 63 65 tic void replace
7b30: 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
7b40: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
7b50: 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
7b60: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
7b70: 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 argv.){. const
7b80: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
7b90: 53 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 Str; /* T
7ba0: 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 he input string
7bb0: 41 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 A */. const uns
7bc0: 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 50 61 74 igned char *zPat
7bd0: 74 65 72 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 tern; /* The
7be0: 70 61 74 74 65 72 6e 20 73 74 72 69 6e 67 20 42 pattern string B
7bf0: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 */. const unsi
7c00: 67 6e 65 64 20 63 68 61 72 20 2a 7a 52 65 70 3b gned char *zRep;
7c10: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
7c20: 65 70 6c 61 63 65 6d 65 6e 74 20 73 74 72 69 6e eplacement strin
7c30: 67 20 43 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 g C */. unsigne
7c40: 64 20 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 d char *zOut;
7c50: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
7c60: 65 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e e output */. in
7c70: 74 20 6e 53 74 72 3b 20 20 20 20 20 20 20 20 20 t nStr;
7c80: 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
7c90: 66 20 7a 53 74 72 20 2a 2f 0a 20 20 69 6e 74 20 f zStr */. int
7ca0: 6e 50 61 74 74 65 72 6e 3b 20 20 20 20 20 20 20 nPattern;
7cb0: 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
7cc0: 7a 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e zPattern */. in
7cd0: 74 20 6e 52 65 70 3b 20 20 20 20 20 20 20 20 20 t nRep;
7ce0: 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
7cf0: 66 20 7a 52 65 70 20 2a 2f 0a 20 20 69 36 34 20 f zRep */. i64
7d00: 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 nOut;
7d10: 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
7d20: 73 69 7a 65 20 6f 66 20 7a 4f 75 74 20 2a 2f 0a size of zOut */.
7d30: 20 20 69 6e 74 20 6c 6f 6f 70 4c 69 6d 69 74 3b int loopLimit;
7d40: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
7d50: 73 74 20 7a 53 74 72 5b 5d 20 74 68 61 74 20 6d st zStr[] that m
7d60: 69 67 68 74 20 6d 61 74 63 68 20 7a 50 61 74 74 ight match zPatt
7d70: 65 72 6e 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 ern[] */. int i
7d80: 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 , j;
7d90: 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
7da0: 74 65 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 ters */.. asser
7db0: 74 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 t( argc==3 );.
7dc0: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
7dd0: 28 61 72 67 63 29 3b 0a 20 20 7a 53 74 72 20 3d (argc);. zStr =
7de0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
7df0: 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 ext(argv[0]);.
7e00: 69 66 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65 if( zStr==0 ) re
7e10: 74 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73 turn;. nStr = s
7e20: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
7e30: 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 es(argv[0]);. a
7e40: 73 73 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c ssert( zStr==sql
7e50: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
7e60: 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 argv[0]) ); /*
7e70: 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e No encoding chan
7e80: 67 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e ge */. zPattern
7e90: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
7ea0: 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a _text(argv[1]);.
7eb0: 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d if( zPattern==
7ec0: 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
7ed0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
7ee0: 79 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53 51 ype(argv[1])==SQ
7ef0: 4c 49 54 45 5f 4e 55 4c 4c 0a 20 20 20 20 20 20 LITE_NULL.
7f00: 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 || sqlite3
7f10: 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 _context_db_hand
7f20: 6c 65 28 63 6f 6e 74 65 78 74 29 2d 3e 6d 61 6c le(context)->mal
7f30: 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 locFailed );.
7f40: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
7f50: 66 28 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d 3d f( zPattern[0]==
7f60: 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
7f70: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
7f80: 79 70 65 28 61 72 67 76 5b 31 5d 29 21 3d 53 51 ype(argv[1])!=SQ
7f90: 4c 49 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 LITE_NULL );.
7fa0: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
7fb0: 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 value(context, a
7fc0: 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72 65 74 rgv[0]);. ret
7fd0: 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 50 61 74 74 urn;. }. nPatt
7fe0: 65 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 ern = sqlite3_va
7ff0: 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 lue_bytes(argv[1
8000: 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 50 ]);. assert( zP
8010: 61 74 74 65 72 6e 3d 3d 73 71 6c 69 74 65 33 5f attern==sqlite3_
8020: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
8030: 31 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 1]) ); /* No en
8040: 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f coding change */
8050: 0a 20 20 7a 52 65 70 20 3d 20 73 71 6c 69 74 65 . zRep = sqlite
8060: 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
8070: 76 5b 32 5d 29 3b 0a 20 20 69 66 28 20 7a 52 65 v[2]);. if( zRe
8080: 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 p==0 ) return;.
8090: 20 6e 52 65 70 20 3d 20 73 71 6c 69 74 65 33 5f nRep = sqlite3_
80a0: 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 value_bytes(argv
80b0: 5b 32 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 [2]);. assert(
80c0: 7a 52 65 70 3d 3d 73 71 6c 69 74 65 33 5f 76 61 zRep==sqlite3_va
80d0: 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d lue_text(argv[2]
80e0: 29 20 29 3b 0a 20 20 6e 4f 75 74 20 3d 20 6e 53 ) );. nOut = nS
80f0: 74 72 20 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 tr + 1;. assert
8100: 28 20 6e 4f 75 74 3c 53 51 4c 49 54 45 5f 4d 41 ( nOut<SQLITE_MA
8110: 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 7a 4f X_LENGTH );. zO
8120: 75 74 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c ut = contextMall
8130: 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 69 36 34 oc(context, (i64
8140: 29 6e 4f 75 74 29 3b 0a 20 20 69 66 28 20 7a 4f )nOut);. if( zO
8150: 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 ut==0 ){. ret
8160: 75 72 6e 3b 0a 20 20 7d 0a 20 20 6c 6f 6f 70 4c urn;. }. loopL
8170: 69 6d 69 74 20 3d 20 6e 53 74 72 20 2d 20 6e 50 imit = nStr - nP
8180: 61 74 74 65 72 6e 3b 20 20 0a 20 20 66 6f 72 28 attern; . for(
8190: 69 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f 70 4c 69 i=j=0; i<=loopLi
81a0: 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 mit; i++){. i
81b0: 66 28 20 7a 53 74 72 5b 69 5d 21 3d 7a 50 61 74 f( zStr[i]!=zPat
81c0: 74 65 72 6e 5b 30 5d 20 7c 7c 20 6d 65 6d 63 6d tern[0] || memcm
81d0: 70 28 26 7a 53 74 72 5b 69 5d 2c 20 7a 50 61 74 p(&zStr[i], zPat
81e0: 74 65 72 6e 2c 20 6e 50 61 74 74 65 72 6e 29 20 tern, nPattern)
81f0: 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 2b ){. zOut[j+
8200: 2b 5d 20 3d 20 7a 53 74 72 5b 69 5d 3b 0a 20 20 +] = zStr[i];.
8210: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 }else{. u
8220: 38 20 2a 7a 4f 6c 64 3b 0a 20 20 20 20 20 20 73 8 *zOld;. s
8230: 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c qlite3 *db = sql
8240: 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f ite3_context_db_
8250: 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b handle(context);
8260: 0a 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d 20 6e . nOut += n
8270: 52 65 70 20 2d 20 6e 50 61 74 74 65 72 6e 3b 0a Rep - nPattern;.
8280: 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
8290: 6e 4f 75 74 2d 31 3d 3d 64 62 2d 3e 61 4c 69 6d nOut-1==db->aLim
82a0: 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
82b0: 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 20 20 20 LENGTH] );.
82c0: 20 74 65 73 74 63 61 73 65 28 20 6e 4f 75 74 2d testcase( nOut-
82d0: 32 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 2==db->aLimit[SQ
82e0: 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
82f0: 48 5d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 H] );. if(
8300: 6e 4f 75 74 2d 31 3e 64 62 2d 3e 61 4c 69 6d 69 nOut-1>db->aLimi
8310: 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
8320: 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 ENGTH] ){.
8330: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
8340: 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f _error_toobig(co
8350: 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 ntext);.
8360: 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f 75 sqlite3_free(zOu
8370: 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 t);. retu
8380: 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rn;. }.
8390: 20 20 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20 zOld = zOut;.
83a0: 20 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 zOut = sqli
83b0: 74 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 74 te3_realloc(zOut
83c0: 2c 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20 , (int)nOut);.
83d0: 20 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 if( zOut==0
83e0: 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
83f0: 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f e3_result_error_
8400: 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a nomem(context);.
8410: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
8420: 66 72 65 65 28 7a 4f 6c 64 29 3b 0a 20 20 20 20 free(zOld);.
8430: 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
8440: 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 }. memcpy
8450: 28 26 7a 4f 75 74 5b 6a 5d 2c 20 7a 52 65 70 2c (&zOut[j], zRep,
8460: 20 6e 52 65 70 29 3b 0a 20 20 20 20 20 20 6a 20 nRep);. j
8470: 2b 3d 20 6e 52 65 70 3b 0a 20 20 20 20 20 20 69 += nRep;. i
8480: 20 2b 3d 20 6e 50 61 74 74 65 72 6e 2d 31 3b 0a += nPattern-1;.
8490: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 }. }. asse
84a0: 72 74 28 20 6a 2b 6e 53 74 72 2d 69 2b 31 3d 3d rt( j+nStr-i+1==
84b0: 6e 4f 75 74 20 29 3b 0a 20 20 6d 65 6d 63 70 79 nOut );. memcpy
84c0: 28 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53 74 72 (&zOut[j], &zStr
84d0: 5b 69 5d 2c 20 6e 53 74 72 2d 69 29 3b 0a 20 20 [i], nStr-i);.
84e0: 6a 20 2b 3d 20 6e 53 74 72 20 2d 20 69 3b 0a 20 j += nStr - i;.
84f0: 20 61 73 73 65 72 74 28 20 6a 3c 3d 6e 4f 75 74 assert( j<=nOut
8500: 20 29 3b 0a 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 );. zOut[j] =
8510: 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 0;. sqlite3_res
8520: 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
8530: 2c 20 28 63 68 61 72 2a 29 7a 4f 75 74 2c 20 6a , (char*)zOut, j
8540: 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b , sqlite3_free);
8550: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
8560: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
8570: 54 52 49 4d 28 29 2c 20 4c 54 52 49 4d 28 29 2c TRIM(), LTRIM(),
8580: 20 61 6e 64 20 52 54 52 49 4d 28 29 20 66 75 6e and RTRIM() fun
8590: 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 75 ctions..** The u
85a0: 73 65 72 64 61 74 61 20 69 73 20 30 78 31 20 66 serdata is 0x1 f
85b0: 6f 72 20 6c 65 66 74 20 74 72 69 6d 2c 20 30 78 or left trim, 0x
85c0: 32 20 66 6f 72 20 72 69 67 68 74 20 74 72 69 6d 2 for right trim
85d0: 2c 20 30 78 33 20 66 6f 72 20 62 6f 74 68 2e 0a , 0x3 for both..
85e0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 */.static void t
85f0: 72 69 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 rimFunc(. sqlit
8600: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
8610: 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c ext,. int argc,
8620: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
8630: 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e **argv.){. con
8640: 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
8650: 20 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 2f *zIn; /
8660: 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a * Input string *
8670: 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e /. const unsign
8680: 65 64 20 63 68 61 72 20 2a 7a 43 68 61 72 53 65 ed char *zCharSe
8690: 74 3b 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 t; /* Set of
86a0: 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 72 characters to tr
86b0: 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 3b im */. int nIn;
86c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
86d0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
86e0: 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
86f0: 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 66 input */. int f
8700: 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
8710: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
8720: 31 3a 20 74 72 69 6d 6c 65 66 74 20 20 32 3a 20 1: trimleft 2:
8730: 74 72 69 6d 72 69 67 68 74 20 20 33 3a 20 74 72 trimright 3: tr
8740: 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 im */. int i;
8750: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8760: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
8770: 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 p counter */. u
8780: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4c nsigned char *aL
8790: 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 en = 0;
87a0: 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 65 61 /* Length of ea
87b0: 63 68 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 ch character in
87c0: 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 75 6e zCharSet */. un
87d0: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 61 7a signed char **az
87e0: 43 68 61 72 20 3d 20 30 3b 20 20 20 20 20 20 20 Char = 0;
87f0: 2f 2a 20 49 6e 64 69 76 69 64 75 61 6c 20 63 68 /* Individual ch
8800: 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68 61 aracters in zCha
8810: 72 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 rSet */. int nC
8820: 68 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 har;
8830: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
8840: 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
8850: 65 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74 20 ers in zCharSet
8860: 2a 2f 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */.. if( sqlite
8870: 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
8880: 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 v[0])==SQLITE_NU
8890: 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e LL ){. return
88a0: 3b 0a 20 20 7d 0a 20 20 7a 49 6e 20 3d 20 73 71 ;. }. zIn = sq
88b0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
88c0: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 (argv[0]);. if(
88d0: 20 7a 49 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e zIn==0 ) return
88e0: 3b 0a 20 20 6e 49 6e 20 3d 20 73 71 6c 69 74 65 ;. nIn = sqlite
88f0: 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 3_value_bytes(ar
8900: 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 gv[0]);. assert
8910: 28 20 7a 49 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 ( zIn==sqlite3_v
8920: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
8930: 5d 29 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63 ]) );. if( argc
8940: 3d 3d 31 20 29 7b 0a 20 20 20 20 73 74 61 74 69 ==1 ){. stati
8950: 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
8960: 20 63 68 61 72 20 6c 65 6e 4f 6e 65 5b 5d 20 3d char lenOne[] =
8970: 20 7b 20 31 20 7d 3b 0a 20 20 20 20 73 74 61 74 { 1 };. stat
8980: 69 63 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 ic unsigned char
8990: 20 2a 20 63 6f 6e 73 74 20 61 7a 4f 6e 65 5b 5d * const azOne[]
89a0: 20 3d 20 7b 20 28 75 38 2a 29 22 20 22 20 7d 3b = { (u8*)" " };
89b0: 0a 20 20 20 20 6e 43 68 61 72 20 3d 20 31 3b 0a . nChar = 1;.
89c0: 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 38 2a 29 aLen = (u8*)
89d0: 6c 65 6e 4f 6e 65 3b 0a 20 20 20 20 61 7a 43 68 lenOne;. azCh
89e0: 61 72 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 ar = (unsigned c
89f0: 68 61 72 20 2a 2a 29 61 7a 4f 6e 65 3b 0a 20 20 har **)azOne;.
8a00: 20 20 7a 43 68 61 72 53 65 74 20 3d 20 30 3b 0a zCharSet = 0;.
8a10: 20 20 7d 65 6c 73 65 20 69 66 28 20 28 7a 43 68 }else if( (zCh
8a20: 61 72 53 65 74 20 3d 20 73 71 6c 69 74 65 33 5f arSet = sqlite3_
8a30: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
8a40: 31 5d 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 1]))==0 ){. r
8a50: 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a eturn;. }else{.
8a60: 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e const unsign
8a70: 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 ed char *z;.
8a80: 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20 for(z=zCharSet,
8a90: 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 nChar=0; *z; nCh
8aa0: 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c ar++){. SQL
8ab0: 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 ITE_SKIP_UTF8(z)
8ac0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
8ad0: 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 20 nChar>0 ){.
8ae0: 20 61 7a 43 68 61 72 20 3d 20 63 6f 6e 74 65 78 azChar = contex
8af0: 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c tMalloc(context,
8b00: 20 28 28 69 36 34 29 6e 43 68 61 72 29 2a 28 73 ((i64)nChar)*(s
8b10: 69 7a 65 6f 66 28 63 68 61 72 2a 29 2b 31 29 29 izeof(char*)+1))
8b20: 3b 0a 20 20 20 20 20 20 69 66 28 20 61 7a 43 68 ;. if( azCh
8b30: 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ar==0 ){.
8b40: 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
8b50: 0a 20 20 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 . aLen = (u
8b60: 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 61 nsigned char*)&a
8b70: 7a 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 20 zChar[nChar];.
8b80: 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 for(z=zCharS
8b90: 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b et, nChar=0; *z;
8ba0: 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 nChar++){.
8bb0: 20 20 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d azChar[nChar]
8bc0: 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 = (unsigned cha
8bd0: 72 20 2a 29 7a 3b 0a 20 20 20 20 20 20 20 20 53 r *)z;. S
8be0: 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 QLITE_SKIP_UTF8(
8bf0: 7a 29 3b 0a 20 20 20 20 20 20 20 20 61 4c 65 6e z);. aLen
8c00: 5b 6e 43 68 61 72 5d 20 3d 20 28 75 38 29 28 7a [nChar] = (u8)(z
8c10: 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d - azChar[nChar]
8c20: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
8c30: 0a 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 72 . }. if( nChar
8c40: 3e 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20 >0 ){. flags
8c50: 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f = SQLITE_PTR_TO_
8c60: 49 4e 54 28 73 71 6c 69 74 65 33 5f 75 73 65 72 INT(sqlite3_user
8c70: 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 29 3b _data(context));
8c80: 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 . if( flags &
8c90: 20 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 1 ){. whil
8ca0: 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 e( nIn>0 ){.
8cb0: 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b int len = 0;
8cc0: 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 . for(i=0
8cd0: 3b 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b ; i<nChar; i++){
8ce0: 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d . len =
8cf0: 20 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 aLen[i];.
8d00: 20 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e if( len<=nIn
8d10: 20 26 26 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20 && memcmp(zIn,
8d20: 61 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d azChar[i], len)=
8d30: 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
8d40: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
8d50: 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 ( i>=nChar ) bre
8d60: 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e 20 ak;. zIn
8d70: 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 += len;.
8d80: 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 nIn -= len;.
8d90: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
8da0: 28 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a 20 ( flags & 2 ){.
8db0: 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e while( nIn>
8dc0: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 0 ){. int
8dd0: 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 len = 0;.
8de0: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 for(i=0; i<nCh
8df0: 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ar; i++){.
8e00: 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 len = aLen[i
8e10: 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ];. if(
8e20: 20 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d len<=nIn && mem
8e30: 63 6d 70 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e cmp(&zIn[nIn-len
8e40: 5d 2c 61 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 ],azChar[i],len)
8e50: 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ==0 ) break;.
8e60: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
8e70: 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 f( i>=nChar ) br
8e80: 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e eak;. nIn
8e90: 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d -= len;. }
8ea0: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a . }. if( z
8eb0: 43 68 61 72 53 65 74 20 29 7b 0a 20 20 20 20 20 CharSet ){.
8ec0: 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a sqlite3_free(az
8ed0: 43 68 61 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Char);. }. }
8ee0: 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
8ef0: 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
8f00: 28 63 68 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c (char*)zIn, nIn,
8f10: 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
8f20: 54 29 3b 0a 7d 0a 0a 0a 2f 2a 20 49 4d 50 3a 20 T);.}.../* IMP:
8f30: 52 2d 32 35 33 36 31 2d 31 36 31 35 30 20 54 68 R-25361-16150 Th
8f40: 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f is function is o
8f50: 6d 69 74 74 65 64 20 66 72 6f 6d 20 53 51 4c 69 mitted from SQLi
8f60: 74 65 20 62 79 20 64 65 66 61 75 6c 74 2e 20 49 te by default. I
8f70: 74 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 61 76 61 t.** is only ava
8f80: 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20 53 51 ilable if the SQ
8f90: 4c 49 54 45 5f 53 4f 55 4e 44 45 58 20 63 6f 6d LITE_SOUNDEX com
8fa0: 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e pile-time option
8fb0: 20 69 73 20 75 73 65 64 0a 2a 2a 20 77 68 65 6e is used.** when
8fc0: 20 53 51 4c 69 74 65 20 69 73 20 62 75 69 6c 74 SQLite is built
8fd0: 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
8fe0: 54 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a TE_SOUNDEX./*.**
8ff0: 20 43 6f 6d 70 75 74 65 20 74 68 65 20 73 6f 75 Compute the sou
9000: 6e 64 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 ndex encoding of
9010: 20 61 20 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 49 a word..**.** I
9020: 4d 50 3a 20 52 2d 35 39 37 38 32 2d 30 30 30 37 MP: R-59782-0007
9030: 32 20 54 68 65 20 73 6f 75 6e 64 65 78 28 58 29 2 The soundex(X)
9040: 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
9050: 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 s a string that
9060: 69 73 20 74 68 65 0a 2a 2a 20 73 6f 75 6e 64 65 is the.** sounde
9070: 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 x encoding of th
9080: 65 20 73 74 72 69 6e 67 20 58 2e 20 0a 2a 2f 0a e string X. .*/.
9090: 73 74 61 74 69 63 20 76 6f 69 64 20 73 6f 75 6e static void soun
90a0: 64 65 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 dexFunc(. sqlit
90b0: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
90c0: 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c ext,. int argc,
90d0: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
90e0: 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 68 61 **argv.){. cha
90f0: 72 20 7a 52 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 r zResult[8];.
9100: 63 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 3b 0a 20 const u8 *zIn;.
9110: 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 61 int i, j;. sta
9120: 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e tic const unsign
9130: 65 64 20 63 68 61 72 20 69 43 6f 64 65 5b 5d 20 ed char iCode[]
9140: 3d 20 7b 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c = {. 0, 0, 0,
9150: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
9160: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
9170: 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 , 0, 0,. 0, 0
9180: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
9190: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
91a0: 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 0, 0, 0, 0,.
91b0: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
91c0: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
91d0: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 0, 0, 0, 0, 0,.
91e0: 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0,
91f0: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
9200: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
9210: 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 0,. 0, 0, 1,
9220: 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 2, 3, 0, 1, 2,
9230: 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 0, 0, 2, 2, 4, 5
9240: 2c 20 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 , 5, 0,. 1, 2
9250: 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c , 6, 2, 3, 0, 1,
9260: 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 0, 2, 0, 2, 0,
9270: 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 0, 0, 0, 0,.
9280: 30 2c 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 0, 0, 1, 2, 3, 0
9290: 2c 20 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c , 1, 2, 0, 0, 2,
92a0: 20 32 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 2, 4, 5, 5, 0,.
92b0: 20 20 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 1, 2, 6, 2,
92c0: 33 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 3, 0, 1, 0, 2, 0
92d0: 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 2, 0, 0, 0, 0,
92e0: 20 30 2c 0a 20 20 7d 3b 0a 20 20 61 73 73 65 72 0,. };. asser
92f0: 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 t( argc==1 );.
9300: 7a 49 6e 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 zIn = (u8*)sqlit
9310: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
9320: 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 gv[0]);. if( zI
9330: 6e 3d 3d 30 20 29 20 7a 49 6e 20 3d 20 28 75 38 n==0 ) zIn = (u8
9340: 2a 29 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b *)"";. for(i=0;
9350: 20 7a 49 6e 5b 69 5d 20 26 26 20 21 73 71 6c 69 zIn[i] && !sqli
9360: 74 65 33 49 73 61 6c 70 68 61 28 7a 49 6e 5b 69 te3Isalpha(zIn[i
9370: 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 ]); i++){}. if(
9380: 20 7a 49 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75 zIn[i] ){. u
9390: 38 20 70 72 65 76 63 6f 64 65 20 3d 20 69 43 6f 8 prevcode = iCo
93a0: 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b de[zIn[i]&0x7f];
93b0: 0a 20 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20 . zResult[0]
93c0: 3d 20 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 = sqlite3Toupper
93d0: 28 7a 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f (zIn[i]);. fo
93e0: 72 28 6a 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 r(j=1; j<4 && zI
93f0: 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 n[i]; i++){.
9400: 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f int code = iCo
9410: 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b de[zIn[i]&0x7f];
9420: 0a 20 20 20 20 20 20 69 66 28 20 63 6f 64 65 3e . if( code>
9430: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 0 ){. if(
9440: 20 63 6f 64 65 21 3d 70 72 65 76 63 6f 64 65 20 code!=prevcode
9450: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 ){. pre
9460: 76 63 6f 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 vcode = code;.
9470: 20 20 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b zResult[
9480: 6a 2b 2b 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 j++] = code + '0
9490: 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 ';. }.
94a0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
94b0: 20 20 70 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a prevcode = 0;.
94c0: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
94d0: 20 20 77 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a while( j<4 ){.
94e0: 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b zResult[j+
94f0: 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a +] = '0';. }.
9500: 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d zResult[j] =
9510: 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 0;. sqlite3_
9520: 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 result_text(cont
9530: 65 78 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c ext, zResult, 4,
9540: 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
9550: 54 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 T);. }else{.
9560: 20 2f 2a 20 49 4d 50 3a 20 52 2d 36 34 38 39 34 /* IMP: R-64894
9570: 2d 35 30 33 32 31 20 54 68 65 20 73 74 72 69 6e -50321 The strin
9580: 67 20 22 3f 30 30 30 22 20 69 73 20 72 65 74 75 g "?000" is retu
9590: 72 6e 65 64 20 69 66 20 74 68 65 20 61 72 67 75 rned if the argu
95a0: 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 69 73 20 4e ment. ** is N
95b0: 55 4c 4c 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 ULL or contains
95c0: 6e 6f 20 41 53 43 49 49 20 61 6c 70 68 61 62 65 no ASCII alphabe
95d0: 74 69 63 20 63 68 61 72 61 63 74 65 72 73 2e 20 tic characters.
95e0: 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 */. sqlite3_r
95f0: 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
9600: 78 74 2c 20 22 3f 30 30 30 22 2c 20 34 2c 20 53 xt, "?000", 4, S
9610: 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
9620: 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.}.#endif /* S
9630: 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 20 2a 2f QLITE_SOUNDEX */
9640: 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
9650: 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e _OMIT_LOAD_EXTEN
9660: 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66 75 6e SION./*.** A fun
9670: 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f 61 64 73 ction that loads
9680: 20 61 20 73 68 61 72 65 64 2d 6c 69 62 72 61 72 a shared-librar
9690: 79 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 65 6e y extension then
96a0: 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a returns NULL..*
96b0: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f /.static void lo
96c0: 61 64 45 78 74 28 73 71 6c 69 74 65 33 5f 63 6f adExt(sqlite3_co
96d0: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 ntext *context,
96e0: 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 int argc, sqlite
96f0: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 3_value **argv){
9700: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
9710: 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 File = (const ch
9720: 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c ar *)sqlite3_val
9730: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
9740: 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
9750: 7a 50 72 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 zProc;. sqlite3
9760: 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 *db = sqlite3_c
9770: 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
9780: 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63 68 61 (context);. cha
9790: 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a r *zErrMsg = 0;.
97a0: 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 . if( argc==2 )
97b0: 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 28 63 {. zProc = (c
97c0: 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 onst char *)sqli
97d0: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
97e0: 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 rgv[1]);. }else
97f0: 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 30 3b {. zProc = 0;
9800: 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46 69 6c 65 . }. if( zFile
9810: 20 26 26 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 && sqlite3_load
9820: 5f 65 78 74 65 6e 73 69 6f 6e 28 64 62 2c 20 7a _extension(db, z
9830: 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 26 7a 45 File, zProc, &zE
9840: 72 72 4d 73 67 29 20 29 7b 0a 20 20 20 20 73 71 rrMsg) ){. sq
9850: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
9860: 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 or(context, zErr
9870: 4d 73 67 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 Msg, -1);. sq
9880: 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 4d lite3_free(zErrM
9890: 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 sg);. }.}.#endi
98a0: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 f.../*.** An ins
98b0: 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
98c0: 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
98d0: 20 68 6f 6c 64 73 20 74 68 65 20 63 6f 6e 74 65 holds the conte
98e0: 78 74 20 6f 66 20 61 0a 2a 2a 20 73 75 6d 28 29 xt of a.** sum()
98f0: 20 6f 72 20 61 76 67 28 29 20 61 67 67 72 65 67 or avg() aggreg
9900: 61 74 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 2e ate computation.
9910: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
9920: 63 74 20 53 75 6d 43 74 78 20 53 75 6d 43 74 78 ct SumCtx SumCtx
9930: 3b 0a 73 74 72 75 63 74 20 53 75 6d 43 74 78 20 ;.struct SumCtx
9940: 7b 0a 20 20 64 6f 75 62 6c 65 20 72 53 75 6d 3b {. double rSum;
9950: 20 20 20 20 20 20 2f 2a 20 46 6c 6f 61 74 69 6e /* Floatin
9960: 67 20 70 6f 69 6e 74 20 73 75 6d 20 2a 2f 0a 20 g point sum */.
9970: 20 69 36 34 20 69 53 75 6d 3b 20 20 20 20 20 20 i64 iSum;
9980: 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 73 75 /* Integer su
9990: 6d 20 2a 2f 20 20 20 0a 20 20 69 36 34 20 63 6e m */ . i64 cn
99a0: 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e t; /* N
99b0: 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 umber of element
99c0: 73 20 73 75 6d 6d 65 64 20 2a 2f 0a 20 20 75 38 s summed */. u8
99d0: 20 6f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 overflow;
99e0: 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74 65 67 /* True if integ
99f0: 65 72 20 6f 76 65 72 66 6c 6f 77 20 73 65 65 6e er overflow seen
9a00: 20 2a 2f 0a 20 20 75 38 20 61 70 70 72 6f 78 3b */. u8 approx;
9a10: 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
9a20: 69 66 20 6e 6f 6e 2d 69 6e 74 65 67 65 72 20 76 if non-integer v
9a30: 61 6c 75 65 20 77 61 73 20 69 6e 70 75 74 20 74 alue was input t
9a40: 6f 20 74 68 65 20 73 75 6d 20 2a 2f 0a 7d 3b 0a o the sum */.};.
9a50: 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 ./*.** Routines
9a60: 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 used to compute
9a70: 74 68 65 20 73 75 6d 2c 20 61 76 65 72 61 67 65 the sum, average
9a80: 2c 20 61 6e 64 20 74 6f 74 61 6c 2e 0a 2a 2a 0a , and total..**.
9a90: 2a 2a 20 54 68 65 20 53 55 4d 28 29 20 66 75 6e ** The SUM() fun
9aa0: 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73 20 74 68 ction follows th
9ab0: 65 20 28 62 72 6f 6b 65 6e 29 20 53 51 4c 20 73 e (broken) SQL s
9ac0: 74 61 6e 64 61 72 64 20 77 68 69 63 68 20 6d 65 tandard which me
9ad0: 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 72 ans.** that it r
9ae0: 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69 eturns NULL if i
9af0: 74 20 73 75 6d 73 20 6f 76 65 72 20 6e 6f 20 69 t sums over no i
9b00: 6e 70 75 74 73 2e 20 20 54 4f 54 41 4c 20 72 65 nputs. TOTAL re
9b10: 74 75 72 6e 73 0a 2a 2a 20 30 2e 30 20 69 6e 20 turns.** 0.0 in
9b20: 74 68 61 74 20 63 61 73 65 2e 20 20 49 6e 20 61 that case. In a
9b30: 64 64 69 74 69 6f 6e 2c 20 54 4f 54 41 4c 20 61 ddition, TOTAL a
9b40: 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 61 20 lways returns a
9b50: 66 6c 6f 61 74 20 77 68 65 72 65 0a 2a 2a 20 53 float where.** S
9b60: 55 4d 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 UM might return
9b70: 61 6e 20 69 6e 74 65 67 65 72 20 69 66 20 69 74 an integer if it
9b80: 20 6e 65 76 65 72 20 65 6e 63 6f 75 6e 74 65 72 never encounter
9b90: 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 s a floating poi
9ba0: 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 54 4f nt.** value. TO
9bb0: 54 41 4c 20 6e 65 76 65 72 20 66 61 69 6c 73 2c TAL never fails,
9bc0: 20 62 75 74 20 53 55 4d 20 6d 69 67 68 74 20 74 but SUM might t
9bd0: 68 72 6f 75 67 68 20 61 6e 20 65 78 63 65 70 74 hrough an except
9be0: 69 6f 6e 20 69 66 0a 2a 2a 20 69 74 20 6f 76 65 ion if.** it ove
9bf0: 72 66 6c 6f 77 73 20 61 6e 20 69 6e 74 65 67 65 rflows an intege
9c00: 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
9c10: 64 20 73 75 6d 53 74 65 70 28 73 71 6c 69 74 65 d sumStep(sqlite
9c20: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
9c30: 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 xt, int argc, sq
9c40: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
9c50: 67 76 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 gv){. SumCtx *p
9c60: 3b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 ;. int type;.
9c70: 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 assert( argc==1
9c80: 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
9c90: 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 70 METER(argc);. p
9ca0: 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 = sqlite3_aggre
9cb0: 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e gate_context(con
9cc0: 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29 text, sizeof(*p)
9cd0: 29 3b 0a 20 20 74 79 70 65 20 3d 20 73 71 6c 69 );. type = sqli
9ce0: 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 te3_value_numeri
9cf0: 63 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b c_type(argv[0]);
9d00: 0a 20 20 69 66 28 20 70 20 26 26 20 74 79 70 65 . if( p && type
9d10: 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b !=SQLITE_NULL ){
9d20: 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 . p->cnt++;.
9d30: 20 20 20 69 66 28 20 74 79 70 65 3d 3d 53 51 4c if( type==SQL
9d40: 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 ITE_INTEGER ){.
9d50: 20 20 20 20 20 69 36 34 20 76 20 3d 20 73 71 6c i64 v = sql
9d60: 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 ite3_value_int64
9d70: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 (argv[0]);.
9d80: 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b 0a 20 p->rSum += v;.
9d90: 20 20 20 20 20 69 66 28 20 28 70 2d 3e 61 70 70 if( (p->app
9da0: 72 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f 77 29 rox|p->overflow)
9db0: 3d 3d 30 20 26 26 20 73 71 6c 69 74 65 33 41 64 ==0 && sqlite3Ad
9dc0: 64 49 6e 74 36 34 28 26 70 2d 3e 69 53 75 6d 2c dInt64(&p->iSum,
9dd0: 20 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 v) ){. p
9de0: 2d 3e 6f 76 65 72 66 6c 6f 77 20 3d 20 31 3b 0a ->overflow = 1;.
9df0: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
9e00: 65 7b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75 6d e{. p->rSum
9e10: 20 2b 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 += sqlite3_valu
9e20: 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d e_double(argv[0]
9e30: 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70 70 72 );. p->appr
9e40: 6f 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ox = 1;. }.
9e50: 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 }.}.static void
9e60: 73 75 6d 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 sumFinalize(sqli
9e70: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
9e80: 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 text){. SumCtx
9e90: 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 *p;. p = sqlite
9ea0: 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 3_aggregate_cont
9eb0: 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b ext(context, 0);
9ec0: 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 63 . if( p && p->c
9ed0: 6e 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 nt>0 ){. if(
9ee0: 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 p->overflow ){.
9ef0: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
9f00: 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 ult_error(contex
9f10: 74 2c 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66 t,"integer overf
9f20: 6c 6f 77 22 2c 2d 31 29 3b 0a 20 20 20 20 7d 65 low",-1);. }e
9f30: 6c 73 65 20 69 66 28 20 70 2d 3e 61 70 70 72 6f lse if( p->appro
9f40: 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 x ){. sqlit
9f50: 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 e3_result_double
9f60: 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 (context, p->rSu
9f70: 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 m);. }else{.
9f80: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
9f90: 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 ult_int64(contex
9fa0: 74 2c 20 70 2d 3e 69 53 75 6d 29 3b 0a 20 20 20 t, p->iSum);.
9fb0: 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 }. }.}.static
9fc0: 76 6f 69 64 20 61 76 67 46 69 6e 61 6c 69 7a 65 void avgFinalize
9fd0: 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
9fe0: 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 *context){. Su
9ff0: 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 mCtx *p;. p = s
a000: 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 qlite3_aggregate
a010: 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 _context(context
a020: 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26 , 0);. if( p &&
a030: 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 p->cnt>0 ){.
a040: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
a050: 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 double(context,
a060: 70 2d 3e 72 53 75 6d 2f 28 64 6f 75 62 6c 65 29 p->rSum/(double)
a070: 70 2d 3e 63 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 73 p->cnt);. }.}.s
a080: 74 61 74 69 63 20 76 6f 69 64 20 74 6f 74 61 6c tatic void total
a090: 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 Finalize(sqlite3
a0a0: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
a0b0: 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b t){. SumCtx *p;
a0c0: 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 . p = sqlite3_a
a0d0: 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 ggregate_context
a0e0: 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 (context, 0);.
a0f0: 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 /* (double)0 In
a100: 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f case of SQLITE_O
a110: 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
a120: 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 NT... */. sqlit
a130: 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 e3_result_double
a140: 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d (context, p ? p-
a150: 3e 72 53 75 6d 20 3a 20 28 64 6f 75 62 6c 65 29 >rSum : (double)
a160: 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 0);.}../*.** The
a170: 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
a180: 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b ture keeps track
a190: 20 6f 66 20 73 74 61 74 65 20 69 6e 66 6f 72 6d of state inform
a1a0: 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 0a 2a 2a ation for the.**
a1b0: 20 63 6f 75 6e 74 28 29 20 61 67 67 72 65 67 61 count() aggrega
a1c0: 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a te function..*/.
a1d0: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 43 typedef struct C
a1e0: 6f 75 6e 74 43 74 78 20 43 6f 75 6e 74 43 74 78 ountCtx CountCtx
a1f0: 3b 0a 73 74 72 75 63 74 20 43 6f 75 6e 74 43 74 ;.struct CountCt
a200: 78 20 7b 0a 20 20 69 36 34 20 6e 3b 0a 7d 3b 0a x {. i64 n;.};.
a210: 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 ./*.** Routines
a220: 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
a230: 20 63 6f 75 6e 74 28 29 20 61 67 67 72 65 67 61 count() aggrega
a240: 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a te function..*/.
a250: 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 75 6e static void coun
a260: 74 53 74 65 70 28 73 71 6c 69 74 65 33 5f 63 6f tStep(sqlite3_co
a270: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 ntext *context,
a280: 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 int argc, sqlite
a290: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 3_value **argv){
a2a0: 0a 20 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a . CountCtx *p;.
a2b0: 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 p = sqlite3_ag
a2c0: 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
a2d0: 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 context, sizeof(
a2e0: 2a 70 29 29 3b 0a 20 20 69 66 28 20 28 61 72 67 *p));. if( (arg
a2f0: 63 3d 3d 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4e c==0 || SQLITE_N
a300: 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c ULL!=sqlite3_val
a310: 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 ue_type(argv[0])
a320: 29 20 26 26 20 70 20 29 7b 0a 20 20 20 20 70 2d ) && p ){. p-
a330: 3e 6e 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 >n++;. }..#ifnd
a340: 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
a350: 45 50 52 45 43 41 54 45 44 0a 20 20 2f 2a 20 54 EPRECATED. /* T
a360: 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 he sqlite3_aggre
a370: 67 61 74 65 5f 63 6f 75 6e 74 28 29 20 66 75 6e gate_count() fun
a380: 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61 ction is depreca
a390: 74 65 64 2e 20 20 42 75 74 20 6a 75 73 74 20 74 ted. But just t
a3a0: 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 o make. ** sure
a3b0: 20 69 74 20 73 74 69 6c 6c 20 6f 70 65 72 61 74 it still operat
a3c0: 65 73 20 63 6f 72 72 65 63 74 6c 79 2c 20 76 65 es correctly, ve
a3d0: 72 69 66 79 20 74 68 61 74 20 69 74 73 20 63 6f rify that its co
a3e0: 75 6e 74 20 61 67 72 65 65 73 20 77 69 74 68 20 unt agrees with
a3f0: 6f 75 72 20 0a 20 20 2a 2a 20 69 6e 74 65 72 6e our . ** intern
a400: 61 6c 20 63 6f 75 6e 74 20 77 68 65 6e 20 75 73 al count when us
a410: 69 6e 67 20 63 6f 75 6e 74 28 2a 29 20 61 6e 64 ing count(*) and
a420: 20 77 68 65 6e 20 74 68 65 20 74 6f 74 61 6c 20 when the total
a430: 63 6f 75 6e 74 20 63 61 6e 20 62 65 0a 20 20 2a count can be. *
a440: 2a 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 * expressed as a
a450: 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 32-bit integer.
a460: 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 61 72 */. assert( ar
a470: 67 63 3d 3d 31 20 7c 7c 20 70 3d 3d 30 20 7c 7c gc==1 || p==0 ||
a480: 20 70 2d 3e 6e 3e 30 78 37 66 66 66 66 66 66 66 p->n>0x7fffffff
a490: 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d . || p-
a4a0: 3e 6e 3d 3d 73 71 6c 69 74 65 33 5f 61 67 67 72 >n==sqlite3_aggr
a4b0: 65 67 61 74 65 5f 63 6f 75 6e 74 28 63 6f 6e 74 egate_count(cont
a4c0: 65 78 74 29 20 29 3b 0a 23 65 6e 64 69 66 0a 7d ext) );.#endif.}
a4d0: 20 20 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .static void
a4e0: 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71 countFinalize(sq
a4f0: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
a500: 6f 6e 74 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74 ontext){. Count
a510: 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 Ctx *p;. p = sq
a520: 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
a530: 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c context(context,
a540: 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 0);. sqlite3_r
a550: 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 esult_int64(cont
a560: 65 78 74 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 ext, p ? p->n :
a570: 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 0);.}../*.** Rou
a580: 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 tines to impleme
a590: 6e 74 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 nt min() and max
a5a0: 28 29 20 61 67 67 72 65 67 61 74 65 20 66 75 6e () aggregate fun
a5b0: 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 ctions..*/.stati
a5c0: 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 c void minmaxSte
a5d0: 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e p(. sqlite3_con
a5e0: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a text *context, .
a5f0: 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a int NotUsed, .
a600: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
a610: 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 4d 65 6d 20 **argv.){. Mem
a620: 2a 70 41 72 67 20 20 3d 20 28 4d 65 6d 20 2a 29 *pArg = (Mem *)
a630: 61 72 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d 20 2a argv[0];. Mem *
a640: 70 42 65 73 74 3b 0a 20 20 55 4e 55 53 45 44 5f pBest;. UNUSED_
a650: 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
a660: 64 29 3b 0a 0a 20 20 70 42 65 73 74 20 3d 20 28 d);.. pBest = (
a670: 4d 65 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 Mem *)sqlite3_ag
a680: 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
a690: 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 context, sizeof(
a6a0: 2a 70 42 65 73 74 29 29 3b 0a 20 20 69 66 28 20 *pBest));. if(
a6b0: 21 70 42 65 73 74 20 29 20 72 65 74 75 72 6e 3b !pBest ) return;
a6c0: 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f .. if( sqlite3_
a6d0: 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b value_type(argv[
a6e0: 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0])==SQLITE_NULL
a6f0: 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42 65 73 ){. if( pBes
a700: 74 2d 3e 66 6c 61 67 73 20 29 20 73 71 6c 69 74 t->flags ) sqlit
a710: 65 33 53 6b 69 70 41 63 63 75 6d 75 6c 61 74 6f e3SkipAccumulato
a720: 72 4c 6f 61 64 28 63 6f 6e 74 65 78 74 29 3b 0a rLoad(context);.
a730: 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 65 73 }else if( pBes
a740: 74 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 t->flags ){.
a750: 69 6e 74 20 6d 61 78 3b 0a 20 20 20 20 69 6e 74 int max;. int
a760: 20 63 6d 70 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 cmp;. CollSe
a770: 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 q *pColl = sqlit
a780: 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 e3GetFuncCollSeq
a790: 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 2f (context);. /
a7a0: 2a 20 54 68 69 73 20 73 74 65 70 20 66 75 6e 63 * This step func
a7b0: 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 tion is used for
a7c0: 20 62 6f 74 68 20 74 68 65 20 6d 69 6e 28 29 20 both the min()
a7d0: 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72 65 67 and max() aggreg
a7e0: 61 74 65 73 2c 0a 20 20 20 20 2a 2a 20 74 68 65 ates,. ** the
a7f0: 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 only difference
a800: 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f between the two
a810: 20 62 65 69 6e 67 20 74 68 61 74 20 74 68 65 20 being that the
a820: 73 65 6e 73 65 20 6f 66 20 74 68 65 0a 20 20 20 sense of the.
a830: 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 ** comparison i
a840: 73 20 69 6e 76 65 72 74 65 64 2e 20 46 6f 72 20 s inverted. For
a850: 74 68 65 20 6d 61 78 28 29 20 61 67 67 72 65 67 the max() aggreg
a860: 61 74 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 ate, the. **
a870: 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
a880: 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 a() function ret
a890: 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 2d 31 2e urns (void *)-1.
a8a0: 20 46 6f 72 20 6d 69 6e 28 29 20 69 74 0a 20 20 For min() it.
a8b0: 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 28 76 6f ** returns (vo
a8c0: 69 64 20 2a 29 64 62 2c 20 77 68 65 72 65 20 64 id *)db, where d
a8d0: 62 20 69 73 20 74 68 65 20 73 71 6c 69 74 65 33 b is the sqlite3
a8e0: 2a 20 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74 * database point
a8f0: 65 72 2e 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 er.. ** There
a900: 66 6f 72 65 20 74 68 65 20 6e 65 78 74 20 73 74 fore the next st
a910: 61 74 65 6d 65 6e 74 20 73 65 74 73 20 76 61 72 atement sets var
a920: 69 61 62 6c 65 20 27 6d 61 78 27 20 74 6f 20 31 iable 'max' to 1
a930: 20 66 6f 72 20 74 68 65 20 6d 61 78 28 29 0a 20 for the max().
a940: 20 20 20 2a 2a 20 61 67 67 72 65 67 61 74 65 2c ** aggregate,
a950: 20 6f 72 20 30 20 66 6f 72 20 6d 69 6e 28 29 2e or 0 for min().
a960: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 61 78 20 . */. max
a970: 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 = sqlite3_user_d
a980: 61 74 61 28 63 6f 6e 74 65 78 74 29 21 3d 30 3b ata(context)!=0;
a990: 0a 20 20 20 20 63 6d 70 20 3d 20 73 71 6c 69 74 . cmp = sqlit
a9a0: 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 42 65 e3MemCompare(pBe
a9b0: 73 74 2c 20 70 41 72 67 2c 20 70 43 6f 6c 6c 29 st, pArg, pColl)
a9c0: 3b 0a 20 20 20 20 69 66 28 20 28 6d 61 78 20 26 ;. if( (max &
a9d0: 26 20 63 6d 70 3c 30 29 20 7c 7c 20 28 21 6d 61 & cmp<0) || (!ma
a9e0: 78 20 26 26 20 63 6d 70 3e 30 29 20 29 7b 0a 20 x && cmp>0) ){.
a9f0: 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
aa00: 4d 65 6d 43 6f 70 79 28 70 42 65 73 74 2c 20 70 MemCopy(pBest, p
aa10: 41 72 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Arg);. }else{
aa20: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 6b . sqlite3Sk
aa30: 69 70 41 63 63 75 6d 75 6c 61 74 6f 72 4c 6f 61 ipAccumulatorLoa
aa40: 64 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 d(context);.
aa50: 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 }. }else{. s
aa60: 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 qlite3VdbeMemCop
aa70: 79 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a y(pBest, pArg);.
aa80: 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 }.}.static voi
aa90: 64 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 d minMaxFinalize
aaa0: 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
aab0: 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 73 71 *context){. sq
aac0: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 lite3_value *pRe
aad0: 73 3b 0a 20 20 70 52 65 73 20 3d 20 28 73 71 6c s;. pRes = (sql
aae0: 69 74 65 33 5f 76 61 6c 75 65 20 2a 29 73 71 6c ite3_value *)sql
aaf0: 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
ab00: 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 ontext(context,
ab10: 30 29 3b 0a 20 20 69 66 28 20 70 52 65 73 20 29 0);. if( pRes )
ab20: 7b 0a 20 20 20 20 69 66 28 20 70 52 65 73 2d 3e {. if( pRes->
ab30: 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 73 flags ){. s
ab40: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 qlite3_result_va
ab50: 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 70 52 65 lue(context, pRe
ab60: 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 s);. }. sq
ab70: 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
ab80: 61 73 65 28 70 52 65 73 29 3b 0a 20 20 7d 0a 7d ase(pRes);. }.}
ab90: 0a 0a 2f 2a 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f ../*.** group_co
aba0: 6e 63 61 74 28 45 58 50 52 2c 20 3f 53 45 50 41 ncat(EXPR, ?SEPA
abb0: 52 41 54 4f 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 RATOR?).*/.stati
abc0: 63 20 76 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 c void groupConc
abd0: 61 74 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 atStep(. sqlite
abe0: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
abf0: 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a xt,. int argc,.
ac00: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
ac10: 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 **argv.){. cons
ac20: 74 20 63 68 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 t char *zVal;.
ac30: 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d StrAccum *pAccum
ac40: 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
ac50: 7a 53 65 70 3b 0a 20 20 69 6e 74 20 6e 56 61 6c zSep;. int nVal
ac60: 2c 20 6e 53 65 70 3b 0a 20 20 61 73 73 65 72 74 , nSep;. assert
ac70: 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 ( argc==1 || arg
ac80: 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 71 c==2 );. if( sq
ac90: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
aca0: 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 (argv[0])==SQLIT
acb0: 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b E_NULL ) return;
acc0: 0a 20 20 70 41 63 63 75 6d 20 3d 20 28 53 74 72 . pAccum = (Str
acd0: 41 63 63 75 6d 2a 29 73 71 6c 69 74 65 33 5f 61 Accum*)sqlite3_a
ace0: 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 ggregate_context
acf0: 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 (context, sizeof
ad00: 28 2a 70 41 63 63 75 6d 29 29 3b 0a 0a 20 20 69 (*pAccum));.. i
ad10: 66 28 20 70 41 63 63 75 6d 20 29 7b 0a 20 20 20 f( pAccum ){.
ad20: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 sqlite3 *db = s
ad30: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
ad40: 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 b_handle(context
ad50: 29 3b 0a 20 20 20 20 69 6e 74 20 66 69 72 73 74 );. int first
ad60: 54 65 72 6d 20 3d 20 70 41 63 63 75 6d 2d 3e 75 Term = pAccum->u
ad70: 73 65 4d 61 6c 6c 6f 63 3d 3d 30 3b 0a 20 20 20 seMalloc==0;.
ad80: 20 70 41 63 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c pAccum->useMall
ad90: 6f 63 20 3d 20 32 3b 0a 20 20 20 20 70 41 63 63 oc = 2;. pAcc
ada0: 75 6d 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 64 62 um->mxAlloc = db
adb0: 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
adc0: 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 LIMIT_LENGTH];.
add0: 20 20 20 69 66 28 20 21 66 69 72 73 74 54 65 72 if( !firstTer
ade0: 6d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 m ){. if( a
adf0: 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 rgc==2 ){.
ae00: 20 20 7a 53 65 70 20 3d 20 28 63 68 61 72 2a 29 zSep = (char*)
ae10: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
ae20: 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 xt(argv[1]);.
ae30: 20 20 20 20 20 6e 53 65 70 20 3d 20 73 71 6c 69 nSep = sqli
ae40: 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
ae50: 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 argv[1]);.
ae60: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a }else{. z
ae70: 53 65 70 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 Sep = ",";.
ae80: 20 20 20 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 nSep = 1;.
ae90: 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e }. if( n
aea0: 53 65 70 20 29 20 73 71 6c 69 74 65 33 53 74 72 Sep ) sqlite3Str
aeb0: 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 AccumAppend(pAcc
aec0: 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 65 70 29 3b um, zSep, nSep);
aed0: 0a 20 20 20 20 7d 0a 20 20 20 20 7a 56 61 6c 20 . }. zVal
aee0: 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 = (char*)sqlite3
aef0: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
af00: 5b 30 5d 29 3b 0a 20 20 20 20 6e 56 61 6c 20 3d [0]);. nVal =
af10: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
af20: 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 ytes(argv[0]);.
af30: 20 20 20 69 66 28 20 7a 56 61 6c 20 29 20 73 71 if( zVal ) sq
af40: 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
af50: 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 56 61 6c end(pAccum, zVal
af60: 2c 20 6e 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 73 , nVal);. }.}.s
af70: 74 61 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70 tatic void group
af80: 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65 28 73 ConcatFinalize(s
af90: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
afa0: 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 74 72 41 context){. StrA
afb0: 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20 ccum *pAccum;.
afc0: 70 41 63 63 75 6d 20 3d 20 73 71 6c 69 74 65 33 pAccum = sqlite3
afd0: 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 _aggregate_conte
afe0: 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a xt(context, 0);.
aff0: 20 20 69 66 28 20 70 41 63 63 75 6d 20 29 7b 0a if( pAccum ){.
b000: 20 20 20 20 69 66 28 20 70 41 63 63 75 6d 2d 3e if( pAccum->
b010: 61 63 63 45 72 72 6f 72 3d 3d 53 54 52 41 43 43 accError==STRACC
b020: 55 4d 5f 54 4f 4f 42 49 47 20 29 7b 0a 20 20 20 UM_TOOBIG ){.
b030: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
b040: 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 t_error_toobig(c
b050: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c ontext);. }el
b060: 73 65 20 69 66 28 20 70 41 63 63 75 6d 2d 3e 61 se if( pAccum->a
b070: 63 63 45 72 72 6f 72 3d 3d 53 54 52 41 43 43 55 ccError==STRACCU
b080: 4d 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 M_NOMEM ){.
b090: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
b0a0: 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 error_nomem(cont
b0b0: 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ext);. }else{
b0c0: 20 20 20 20 0a 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
b0d0: 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 e3_result_text(c
b0e0: 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 53 ontext, sqlite3S
b0f0: 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 70 41 trAccumFinish(pA
b100: 63 63 75 6d 29 2c 20 2d 31 2c 20 0a 20 20 20 20 ccum), -1, .
b110: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b120: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
b130: 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ee);. }. }.}
b140: 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
b150: 74 69 6e 65 20 64 6f 65 73 20 70 65 72 2d 63 6f tine does per-co
b160: 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 69 6f nnection functio
b170: 6e 20 72 65 67 69 73 74 72 61 74 69 6f 6e 2e 20 n registration.
b180: 20 4d 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 20 Most.** of the
b190: 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f built-in functio
b1a0: 6e 73 20 61 62 6f 76 65 20 61 72 65 20 70 61 72 ns above are par
b1b0: 74 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 t of the global
b1c0: 66 75 6e 63 74 69 6f 6e 20 73 65 74 2e 0a 2a 2a function set..**
b1d0: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e This routine on
b1e0: 6c 79 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 ly deals with th
b1f0: 6f 73 65 20 74 68 61 74 20 61 72 65 20 6e 6f 74 ose that are not
b200: 20 67 6c 6f 62 61 6c 2e 0a 2a 2f 0a 76 6f 69 64 global..*/.void
b210: 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 sqlite3Register
b220: 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 BuiltinFunctions
b230: 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
b240: 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 int rc = sqlite
b250: 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 3_overload_funct
b260: 69 6f 6e 28 64 62 2c 20 22 4d 41 54 43 48 22 2c ion(db, "MATCH",
b270: 20 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 2);. assert( r
b280: 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
b290: 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b || rc==SQLITE_OK
b2a0: 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 );. if( rc==SQ
b2b0: 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 LITE_NOMEM ){.
b2c0: 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
b2d0: 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f ed = 1;. }.}../
b2e0: 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 4c 49 4b *.** Set the LIK
b2f0: 45 4f 50 54 20 66 6c 61 67 20 6f 6e 20 74 68 65 EOPT flag on the
b300: 20 32 2d 61 72 67 75 6d 65 6e 74 20 66 75 6e 63 2-argument func
b310: 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67 69 tion with the gi
b320: 76 65 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 ven name..*/.sta
b330: 74 69 63 20 76 6f 69 64 20 73 65 74 4c 69 6b 65 tic void setLike
b340: 4f 70 74 46 6c 61 67 28 73 71 6c 69 74 65 33 20 OptFlag(sqlite3
b350: 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *db, const char
b360: 2a 7a 4e 61 6d 65 2c 20 75 38 20 66 6c 61 67 56 *zName, u8 flagV
b370: 61 6c 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a al){. FuncDef *
b380: 70 44 65 66 3b 0a 20 20 70 44 65 66 20 3d 20 73 pDef;. pDef = s
b390: 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 qlite3FindFuncti
b3a0: 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 73 71 on(db, zName, sq
b3b0: 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e lite3Strlen30(zN
b3c0: 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 ame),.
b3d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b3e0: 20 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 2, SQLITE_UTF
b3f0: 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 41 4c 57 8, 0);. if( ALW
b400: 41 59 53 28 70 44 65 66 29 20 29 7b 0a 20 20 20 AYS(pDef) ){.
b410: 20 70 44 65 66 2d 3e 66 75 6e 63 46 6c 61 67 73 pDef->funcFlags
b420: 20 7c 3d 20 66 6c 61 67 56 61 6c 3b 0a 20 20 7d |= flagVal;. }
b430: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 .}../*.** Regist
b440: 65 72 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 er the built-in
b450: 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 66 75 LIKE and GLOB fu
b460: 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 63 61 nctions. The ca
b470: 73 65 53 65 6e 73 69 74 69 76 65 0a 2a 2a 20 70 seSensitive.** p
b480: 61 72 61 6d 65 74 65 72 20 64 65 74 65 72 6d 69 arameter determi
b490: 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e nes whether or n
b4a0: 6f 74 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 ot the LIKE oper
b4b0: 61 74 6f 72 20 69 73 20 63 61 73 65 0a 2a 2a 20 ator is case.**
b4c0: 73 65 6e 73 69 74 69 76 65 2e 20 20 47 4c 4f 42 sensitive. GLOB
b4d0: 20 69 73 20 61 6c 77 61 79 73 20 63 61 73 65 20 is always case
b4e0: 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2f 0a 76 6f sensitive..*/.vo
b4f0: 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 id sqlite3Regist
b500: 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 erLikeFunctions(
b510: 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
b520: 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 29 7b caseSensitive){
b530: 0a 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 . struct compar
b540: 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 eInfo *pInfo;.
b550: 69 66 28 20 63 61 73 65 53 65 6e 73 69 74 69 76 if( caseSensitiv
b560: 65 20 29 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d e ){. pInfo =
b570: 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 (struct compare
b580: 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 Info*)&likeInfoA
b590: 6c 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 lt;. }else{.
b5a0: 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74 pInfo = (struct
b5b0: 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c compareInfo*)&l
b5c0: 69 6b 65 49 6e 66 6f 4e 6f 72 6d 3b 0a 20 20 7d ikeInfoNorm;. }
b5d0: 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 . sqlite3Create
b5e0: 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c Func(db, "like",
b5f0: 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 2, SQLITE_UTF8,
b600: 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 pInfo, likeFunc
b610: 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 , 0, 0, 0);. sq
b620: 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 lite3CreateFunc(
b630: 64 62 2c 20 22 6c 69 6b 65 22 2c 20 33 2c 20 53 db, "like", 3, S
b640: 51 4c 49 54 45 5f 55 54 46 38 2c 20 70 49 6e 66 QLITE_UTF8, pInf
b650: 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 o, likeFunc, 0,
b660: 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 0, 0);. sqlite3
b670: 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 CreateFunc(db, "
b680: 67 6c 6f 62 22 2c 20 32 2c 20 53 51 4c 49 54 45 glob", 2, SQLITE
b690: 5f 55 54 46 38 2c 20 0a 20 20 20 20 20 20 28 73 _UTF8, . (s
b6a0: 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 truct compareInf
b6b0: 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c 20 6c 69 o*)&globInfo, li
b6c0: 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 30 29 keFunc, 0, 0, 0)
b6d0: 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c ;. setLikeOptFl
b6e0: 61 67 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 53 ag(db, "glob", S
b6f0: 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 QLITE_FUNC_LIKE
b700: 7c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 | SQLITE_FUNC_CA
b710: 53 45 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 SE);. setLikeOp
b720: 74 46 6c 61 67 28 64 62 2c 20 22 6c 69 6b 65 22 tFlag(db, "like"
b730: 2c 20 0a 20 20 20 20 20 20 63 61 73 65 53 65 6e , . caseSen
b740: 73 69 74 69 76 65 20 3f 20 28 53 51 4c 49 54 45 sitive ? (SQLITE
b750: 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c _FUNC_LIKE | SQL
b760: 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 20 3a ITE_FUNC_CASE) :
b770: 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b SQLITE_FUNC_LIK
b780: 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 E);.}../*.** pEx
b790: 70 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 pr points to an
b7a0: 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 expression which
b7b0: 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66 75 implements a fu
b7c0: 6e 63 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 69 nction. If.** i
b7d0: 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 t is appropriate
b7e0: 20 74 6f 20 61 70 70 6c 79 20 74 68 65 20 4c 49 to apply the LI
b7f0: 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 KE optimization
b800: 74 6f 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e to that function
b810: 0a 2a 2a 20 74 68 65 6e 20 73 65 74 20 61 57 63 .** then set aWc
b820: 5b 30 5d 20 74 68 72 6f 75 67 68 20 61 57 63 5b [0] through aWc[
b830: 32 5d 20 74 6f 20 74 68 65 20 77 69 6c 64 63 61 2] to the wildca
b840: 72 64 20 63 68 61 72 61 63 74 65 72 73 20 61 6e rd characters an
b850: 64 0a 2a 2a 20 72 65 74 75 72 6e 20 54 52 55 45 d.** return TRUE
b860: 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 . If the functi
b870: 6f 6e 20 69 73 20 6e 6f 74 20 61 20 4c 49 4b 45 on is not a LIKE
b880: 2d 73 74 79 6c 65 20 66 75 6e 63 74 69 6f 6e 20 -style function
b890: 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 46 then.** return F
b8a0: 41 4c 53 45 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c ALSE..*/.int sql
b8b0: 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 ite3IsLikeFuncti
b8c0: 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 on(sqlite3 *db,
b8d0: 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 Expr *pExpr, int
b8e0: 20 2a 70 49 73 4e 6f 63 61 73 65 2c 20 63 68 61 *pIsNocase, cha
b8f0: 72 20 2a 61 57 63 29 7b 0a 20 20 46 75 6e 63 44 r *aWc){. FuncD
b900: 65 66 20 2a 70 44 65 66 3b 0a 20 20 69 66 28 20 ef *pDef;. if(
b910: 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 pExpr->op!=TK_FU
b920: 4e 43 54 49 4f 4e 20 0a 20 20 20 7c 7c 20 21 70 NCTION . || !p
b930: 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 0a 20 Expr->x.pList .
b940: 20 20 7c 7c 20 70 45 78 70 72 2d 3e 78 2e 70 4c || pExpr->x.pL
b950: 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 0a 20 20 ist->nExpr!=2.
b960: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
b970: 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 . }. assert( !
b980: 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
b990: 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c pExpr, EP_xIsSel
b9a0: 65 63 74 29 20 29 3b 0a 20 20 70 44 65 66 20 3d ect) );. pDef =
b9b0: 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 sqlite3FindFunc
b9c0: 74 69 6f 6e 28 64 62 2c 20 70 45 78 70 72 2d 3e tion(db, pExpr->
b9d0: 75 2e 7a 54 6f 6b 65 6e 2c 20 0a 20 20 20 20 20 u.zToken, .
b9e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b9f0: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 sqlite3S
ba00: 74 72 6c 65 6e 33 30 28 70 45 78 70 72 2d 3e 75 trlen30(pExpr->u
ba10: 2e 7a 54 6f 6b 65 6e 29 2c 0a 20 20 20 20 20 20 .zToken),.
ba20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ba30: 20 20 20 20 20 20 20 32 2c 20 53 51 4c 49 54 45 2, SQLITE
ba40: 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 28 _UTF8, 0);. if(
ba50: 20 4e 45 56 45 52 28 70 44 65 66 3d 3d 30 29 20 NEVER(pDef==0)
ba60: 7c 7c 20 28 70 44 65 66 2d 3e 66 75 6e 63 46 6c || (pDef->funcFl
ba70: 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e ags & SQLITE_FUN
ba80: 43 5f 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 20 C_LIKE)==0 ){.
ba90: 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
baa0: 0a 20 20 2f 2a 20 54 68 65 20 6d 65 6d 63 70 79 . /* The memcpy
bab0: 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 () statement ass
bac0: 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 77 69 umes that the wi
bad0: 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65 72 ldcard character
bae0: 73 20 61 72 65 0a 20 20 2a 2a 20 74 68 65 20 66 s are. ** the f
baf0: 69 72 73 74 20 74 68 72 65 65 20 73 74 61 74 65 irst three state
bb00: 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f 6d ments in the com
bb10: 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 pareInfo structu
bb20: 72 65 2e 20 20 54 68 65 0a 20 20 2a 2a 20 61 73 re. The. ** as
bb30: 73 65 72 74 73 28 29 20 74 68 61 74 20 66 6f 6c serts() that fol
bb40: 6c 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 20 low verify that
bb50: 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f 0a assumption. */.
bb60: 20 20 6d 65 6d 63 70 79 28 61 57 63 2c 20 70 44 memcpy(aWc, pD
bb70: 65 66 2d 3e 70 55 73 65 72 44 61 74 61 2c 20 33 ef->pUserData, 3
bb80: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 68 );. assert( (ch
bb90: 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 ar*)&likeInfoAlt
bba0: 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 == (char*)&like
bbb0: 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 41 6c 6c InfoAlt.matchAll
bbc0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26 28 );. assert( &(
bbd0: 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f (char*)&likeInfo
bbe0: 41 6c 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61 72 Alt)[1] == (char
bbf0: 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d *)&likeInfoAlt.m
bc00: 61 74 63 68 4f 6e 65 20 29 3b 0a 20 20 61 73 73 atchOne );. ass
bc10: 65 72 74 28 20 26 28 28 63 68 61 72 2a 29 26 6c ert( &((char*)&l
bc20: 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d ikeInfoAlt)[2] =
bc30: 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e = (char*)&likeIn
bc40: 66 6f 41 6c 74 2e 6d 61 74 63 68 53 65 74 20 29 foAlt.matchSet )
bc50: 3b 0a 20 20 2a 70 49 73 4e 6f 63 61 73 65 20 3d ;. *pIsNocase =
bc60: 20 28 70 44 65 66 2d 3e 66 75 6e 63 46 6c 61 67 (pDef->funcFlag
bc70: 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f s & SQLITE_FUNC_
bc80: 43 41 53 45 29 3d 3d 30 3b 0a 20 20 72 65 74 75 CASE)==0;. retu
bc90: 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 rn 1;.}../*.** A
bca0: 6c 6c 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 ll of the FuncDe
bcb0: 66 20 73 74 72 75 63 74 75 72 65 73 20 69 6e 20 f structures in
bcc0: 74 68 65 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 the aBuiltinFunc
bcd0: 5b 5d 20 61 72 72 61 79 20 61 62 6f 76 65 0a 2a [] array above.*
bce0: 2a 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 * to the global
bcf0: 66 75 6e 63 74 69 6f 6e 20 68 61 73 68 20 74 61 function hash ta
bd00: 62 6c 65 2e 20 20 54 68 69 73 20 6f 63 63 75 72 ble. This occur
bd10: 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 20 s at start-time
bd20: 28 61 73 0a 2a 2a 20 61 20 63 6f 6e 73 65 71 75 (as.** a consequ
bd30: 65 6e 63 65 20 6f 66 20 63 61 6c 6c 69 6e 67 20 ence of calling
bd40: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
bd50: 7a 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 ze())..**.** Aft
bd60: 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 er this routine
bd70: 72 75 6e 73 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c runs.*/.void sql
bd80: 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62 ite3RegisterGlob
bd90: 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 alFunctions(void
bda0: 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 ){. /*. ** The
bdb0: 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 following array
bdc0: 20 68 6f 6c 64 73 20 46 75 6e 63 44 65 66 20 73 holds FuncDef s
bdd0: 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 61 6c tructures for al
bde0: 6c 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f l of the functio
bdf0: 6e 73 0a 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 ns. ** defined
be00: 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 20 20 in this file..
be10: 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61 72 72 61 **. ** The arra
be20: 79 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 y cannot be cons
be30: 74 61 6e 74 20 73 69 6e 63 65 20 63 68 61 6e 67 tant since chang
be40: 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 es are made to t
be50: 68 65 0a 20 20 2a 2a 20 46 75 6e 63 44 65 66 2e he. ** FuncDef.
be60: 70 48 61 73 68 20 65 6c 65 6d 65 6e 74 73 20 61 pHash elements a
be70: 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 54 t start-time. T
be80: 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 he elements of t
be90: 68 69 73 20 61 72 72 61 79 0a 20 20 2a 2a 20 61 his array. ** a
bea0: 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66 74 re read-only aft
beb0: 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f er initializatio
bec0: 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 20 n is complete..
bed0: 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 53 51 4c */. static SQL
bee0: 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 20 ITE_WSD FuncDef
bef0: 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 3d aBuiltinFunc[] =
bf00: 20 7b 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 {. FUNCTION(
bf10: 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 ltrim,
bf20: 20 20 20 20 31 2c 20 31 2c 20 30 2c 20 74 72 69 1, 1, 0, tri
bf30: 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c mFunc ),
bf40: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 . FUNCTION(lt
bf50: 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 rim,
bf60: 20 20 32 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 2, 1, 0, trimF
bf70: 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 unc ),.
bf80: 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 FUNCTION(rtri
bf90: 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m,
bfa0: 31 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 1, 2, 0, trimFun
bfb0: 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 c ),.
bfc0: 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c FUNCTION(rtrim,
bfd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 2,
bfe0: 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 2, 0, trimFunc
bff0: 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 ),. F
c000: 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 UNCTION(trim,
c010: 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 33 1, 3
c020: 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 , 0, trimFunc
c030: 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e ),. FUN
c040: 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 CTION(trim,
c050: 20 20 20 20 20 20 20 20 20 20 32 2c 20 33 2c 20 2, 3,
c060: 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 0, trimFunc
c070: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
c080: 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 ION(min,
c090: 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 31 2c -1, 0, 1,
c0a0: 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20 20 20 20 minmaxFunc
c0b0: 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
c0c0: 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 N(min,
c0d0: 20 20 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 30 0, 0, 1, 0
c0e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c0f0: 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 ),. AGGREGATE
c100: 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 (min,
c110: 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 1, 0, 1, min
c120: 6d 61 78 53 74 65 70 2c 20 20 20 20 20 20 6d 69 maxStep, mi
c130: 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a nMaxFinalize ),.
c140: 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 FUNCTION(max
c150: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
c160: 2d 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 -1, 1, 1, minmax
c170: 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 Func ),.
c180: 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 FUNCTION(max,
c190: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
c1a0: 2c 20 31 2c 20 31 2c 20 30 20 20 20 20 20 20 20 , 1, 1, 0
c1b0: 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 ),.
c1c0: 41 47 47 52 45 47 41 54 45 28 6d 61 78 2c 20 20 AGGREGATE(max,
c1d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 1,
c1e0: 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70 1, 1, minmaxStep
c1f0: 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e , minMaxFin
c200: 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20 46 55 4e alize ),. FUN
c210: 43 54 49 4f 4e 32 28 74 79 70 65 6f 66 2c 20 20 CTION2(typeof,
c220: 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
c230: 30 2c 20 74 79 70 65 6f 66 46 75 6e 63 2c 20 20 0, typeofFunc,
c240: 53 51 4c 49 54 45 5f 46 55 4e 43 5f 54 59 50 45 SQLITE_FUNC_TYPE
c250: 4f 46 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f OF),. FUNCTIO
c260: 4e 32 28 6c 65 6e 67 74 68 2c 20 20 20 20 20 20 N2(length,
c270: 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 1, 0, 0, l
c280: 65 6e 67 74 68 46 75 6e 63 2c 20 20 53 51 4c 49 engthFunc, SQLI
c290: 54 45 5f 46 55 4e 43 5f 4c 45 4e 47 54 48 29 2c TE_FUNC_LENGTH),
c2a0: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 69 6e . FUNCTION(in
c2b0: 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 str,
c2c0: 20 20 32 2c 20 30 2c 20 30 2c 20 69 6e 73 74 72 2, 0, 0, instr
c2d0: 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 Func ),.
c2e0: 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73 FUNCTION(subs
c2f0: 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 tr,
c300: 32 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46 2, 0, 0, substrF
c310: 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 unc ),.
c320: 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73 74 72 FUNCTION(substr
c330: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 33 2c , 3,
c340: 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46 75 6e 0, 0, substrFun
c350: 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 c ),. F
c360: 55 4e 43 54 49 4f 4e 28 70 72 69 6e 74 66 2c 20 UNCTION(printf,
c370: 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 -1, 0
c380: 2c 20 30 2c 20 70 72 69 6e 74 66 46 75 6e 63 20 , 0, printfFunc
c390: 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e ),. FUN
c3a0: 43 54 49 4f 4e 28 75 6e 69 63 6f 64 65 2c 20 20 CTION(unicode,
c3b0: 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
c3c0: 30 2c 20 75 6e 69 63 6f 64 65 46 75 6e 63 20 20 0, unicodeFunc
c3d0: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
c3e0: 49 4f 4e 28 63 68 61 72 2c 20 20 20 20 20 20 20 ION(char,
c3f0: 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c -1, 0, 0,
c400: 20 63 68 61 72 46 75 6e 63 20 20 20 20 20 20 20 charFunc
c410: 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
c420: 4e 28 61 62 73 2c 20 20 20 20 20 20 20 20 20 20 N(abs,
c430: 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 61 1, 0, 0, a
c440: 62 73 46 75 6e 63 20 20 20 20 20 20 20 20 20 20 bsFunc
c450: 29 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ),.#ifndef SQLIT
c460: 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
c470: 50 4f 49 4e 54 0a 20 20 20 20 46 55 4e 43 54 49 POINT. FUNCTI
c480: 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20 20 20 ON(round,
c490: 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 1, 0, 0,
c4a0: 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 20 20 20 roundFunc
c4b0: 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
c4c0: 28 72 6f 75 6e 64 2c 20 20 20 20 20 20 20 20 20 (round,
c4d0: 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 72 6f 2, 0, 0, ro
c4e0: 75 6e 64 46 75 6e 63 20 20 20 20 20 20 20 20 29 undFunc )
c4f0: 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 46 55 4e ,.#endif. FUN
c500: 43 54 49 4f 4e 28 75 70 70 65 72 2c 20 20 20 20 CTION(upper,
c510: 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
c520: 30 2c 20 75 70 70 65 72 46 75 6e 63 20 20 20 20 0, upperFunc
c530: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
c540: 49 4f 4e 28 6c 6f 77 65 72 2c 20 20 20 20 20 20 ION(lower,
c550: 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 1, 0, 0,
c560: 20 6c 6f 77 65 72 46 75 6e 63 20 20 20 20 20 20 lowerFunc
c570: 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
c580: 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 N(coalesce,
c590: 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 30 1, 0, 0, 0
c5a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c5b0: 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
c5c0: 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 coalesce,
c5d0: 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 20 20 0, 0, 0, 0
c5e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c ),
c5f0: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 32 28 63 . FUNCTION2(c
c600: 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20 oalesce,
c610: 20 2d 31 2c 20 30 2c 20 30 2c 20 6e 6f 6f 70 46 -1, 0, 0, noopF
c620: 75 6e 63 2c 20 20 53 51 4c 49 54 45 5f 46 55 4e unc, SQLITE_FUN
c630: 43 5f 43 4f 41 4c 45 53 43 45 29 2c 0a 20 20 20 C_COALESCE),.
c640: 20 46 55 4e 43 54 49 4f 4e 28 68 65 78 2c 20 20 FUNCTION(hex,
c650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 1,
c660: 20 30 2c 20 30 2c 20 68 65 78 46 75 6e 63 20 20 0, 0, hexFunc
c670: 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 ),. F
c680: 55 4e 43 54 49 4f 4e 32 28 69 66 6e 75 6c 6c 2c UNCTION2(ifnull,
c690: 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2, 0
c6a0: 2c 20 30 2c 20 6e 6f 6f 70 46 75 6e 63 2c 20 20 , 0, noopFunc,
c6b0: 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c SQLITE_FUNC_COAL
c6c0: 45 53 43 45 29 2c 0a 20 20 20 20 46 55 4e 43 54 ESCE),. FUNCT
c6d0: 49 4f 4e 32 28 75 6e 6c 69 6b 65 6c 79 2c 20 20 ION2(unlikely,
c6e0: 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 1, 0, 0,
c6f0: 20 6e 6f 6f 70 46 75 6e 63 2c 20 20 53 51 4c 49 noopFunc, SQLI
c700: 54 45 5f 46 55 4e 43 5f 55 4e 4c 49 4b 45 4c 59 TE_FUNC_UNLIKELY
c710: 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 32 ),. FUNCTION2
c720: 28 6c 69 6b 65 6c 69 68 6f 6f 64 2c 20 20 20 20 (likelihood,
c730: 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 6e 6f 6f 2, 0, 0, noo
c740: 70 46 75 6e 63 2c 20 20 53 51 4c 49 54 45 5f 46 pFunc, SQLITE_F
c750: 55 4e 43 5f 55 4e 4c 49 4b 45 4c 59 29 2c 0a 20 UNC_UNLIKELY),.
c760: 20 20 20 46 55 4e 43 54 49 4f 4e 32 28 6c 69 6b FUNCTION2(lik
c770: 65 6c 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 ely,
c780: 31 2c 20 30 2c 20 30 2c 20 6e 6f 6f 70 46 75 6e 1, 0, 0, noopFun
c790: 63 2c 20 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f c, SQLITE_FUNC_
c7a0: 55 4e 4c 49 4b 45 4c 59 29 2c 0a 20 20 20 20 56 UNLIKELY),. V
c7b0: 46 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 2c FUNCTION(random,
c7c0: 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 0, 0
c7d0: 2c 20 30 2c 20 72 61 6e 64 6f 6d 46 75 6e 63 20 , 0, randomFunc
c7e0: 20 20 20 20 20 20 29 2c 0a 20 20 20 20 56 46 55 ),. VFU
c7f0: 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c 6f NCTION(randomblo
c800: 62 2c 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 b, 1, 0,
c810: 30 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 20 20 0, randomBlob
c820: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
c830: 49 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20 20 20 20 ION(nullif,
c840: 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 31 2c 2, 0, 1,
c850: 20 6e 75 6c 6c 69 66 46 75 6e 63 20 20 20 20 20 nullifFunc
c860: 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
c870: 4e 28 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e N(sqlite_version
c880: 2c 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 76 , 0, 0, 0, v
c890: 65 72 73 69 6f 6e 46 75 6e 63 20 20 20 20 20 20 ersionFunc
c8a0: 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
c8b0: 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 sqlite_source_id
c8c0: 2c 20 20 20 30 2c 20 30 2c 20 30 2c 20 73 6f 75 , 0, 0, 0, sou
c8d0: 72 63 65 69 64 46 75 6e 63 20 20 20 20 20 29 2c rceidFunc ),
c8e0: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 71 . FUNCTION(sq
c8f0: 6c 69 74 65 5f 6c 6f 67 2c 20 20 20 20 20 20 20 lite_log,
c900: 20 20 32 2c 20 30 2c 20 30 2c 20 65 72 72 6c 6f 2, 0, 0, errlo
c910: 67 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 23 gFunc ),.#
c920: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
c930: 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e IT_COMPILEOPTION
c940: 5f 44 49 41 47 53 0a 20 20 20 20 46 55 4e 43 54 _DIAGS. FUNCT
c950: 49 4f 4e 28 73 71 6c 69 74 65 5f 63 6f 6d 70 69 ION(sqlite_compi
c960: 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 2c 31 2c leoption_used,1,
c970: 20 30 2c 20 30 2c 20 63 6f 6d 70 69 6c 65 6f 70 0, 0, compileop
c980: 74 69 6f 6e 75 73 65 64 46 75 6e 63 20 20 29 2c tionusedFunc ),
c990: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 71 . FUNCTION(sq
c9a0: 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 lite_compileopti
c9b0: 6f 6e 5f 67 65 74 2c 20 31 2c 20 30 2c 20 30 2c on_get, 1, 0, 0,
c9c0: 20 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 67 65 compileoptionge
c9d0: 74 46 75 6e 63 20 20 29 2c 0a 23 65 6e 64 69 66 tFunc ),.#endif
c9e0: 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
c9f0: 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 COMPILEOPTION_DI
ca00: 41 47 53 20 2a 2f 0a 20 20 20 20 46 55 4e 43 54 AGS */. FUNCT
ca10: 49 4f 4e 28 71 75 6f 74 65 2c 20 20 20 20 20 20 ION(quote,
ca20: 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 1, 0, 0,
ca30: 20 71 75 6f 74 65 46 75 6e 63 20 20 20 20 20 20 quoteFunc
ca40: 20 20 29 2c 0a 20 20 20 20 56 46 55 4e 43 54 49 ),. VFUNCTI
ca50: 4f 4e 28 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 ON(last_insert_r
ca60: 6f 77 69 64 2c 20 30 2c 20 30 2c 20 30 2c 20 6c owid, 0, 0, 0, l
ca70: 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 ast_insert_rowid
ca80: 29 2c 0a 20 20 20 20 56 46 55 4e 43 54 49 4f 4e ),. VFUNCTION
ca90: 28 63 68 61 6e 67 65 73 2c 20 20 20 20 20 20 20 (changes,
caa0: 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 68 61 0, 0, 0, cha
cab0: 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 29 2c nges ),
cac0: 0a 20 20 20 20 56 46 55 4e 43 54 49 4f 4e 28 74 . VFUNCTION(t
cad0: 6f 74 61 6c 5f 63 68 61 6e 67 65 73 2c 20 20 20 otal_changes,
cae0: 20 20 30 2c 20 30 2c 20 30 2c 20 74 6f 74 61 6c 0, 0, 0, total
caf0: 5f 63 68 61 6e 67 65 73 20 20 20 20 29 2c 0a 20 _changes ),.
cb00: 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 65 70 6c FUNCTION(repl
cb10: 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ace,
cb20: 33 2c 20 30 2c 20 30 2c 20 72 65 70 6c 61 63 65 3, 0, 0, replace
cb30: 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 Func ),.
cb40: 20 46 55 4e 43 54 49 4f 4e 28 7a 65 72 6f 62 6c FUNCTION(zerobl
cb50: 6f 62 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c ob, 1,
cb60: 20 30 2c 20 30 2c 20 7a 65 72 6f 62 6c 6f 62 46 0, 0, zeroblobF
cb70: 75 6e 63 20 20 20 20 20 29 2c 0a 20 20 23 69 66 unc ),. #if
cb80: 64 65 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 def SQLITE_SOUND
cb90: 45 58 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 EX. FUNCTION(
cba0: 73 6f 75 6e 64 65 78 2c 20 20 20 20 20 20 20 20 soundex,
cbb0: 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 6f 75 1, 0, 0, sou
cbc0: 6e 64 65 78 46 75 6e 63 20 20 20 20 20 20 29 2c ndexFunc ),
cbd0: 0a 20 20 23 65 6e 64 69 66 0a 20 20 23 69 66 6e . #endif. #ifn
cbe0: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
cbf0: 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 20 LOAD_EXTENSION.
cc00: 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 FUNCTION(load
cc10: 5f 65 78 74 65 6e 73 69 6f 6e 2c 20 20 20 20 20 _extension,
cc20: 31 2c 20 30 2c 20 30 2c 20 6c 6f 61 64 45 78 74 1, 0, 0, loadExt
cc30: 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 ),.
cc40: 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 FUNCTION(load_e
cc50: 78 74 65 6e 73 69 6f 6e 2c 20 20 20 20 20 32 2c xtension, 2,
cc60: 20 30 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 0, 0, loadExt
cc70: 20 20 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e ),. #en
cc80: 64 69 66 0a 20 20 20 20 41 47 47 52 45 47 41 54 dif. AGGREGAT
cc90: 45 28 73 75 6d 2c 20 20 20 20 20 20 20 20 20 20 E(sum,
cca0: 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 1, 0, 0, su
ccb0: 6d 53 74 65 70 2c 20 20 20 20 20 20 20 20 20 73 mStep, s
ccc0: 75 6d 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c umFinalize ),
ccd0: 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28 74 . AGGREGATE(t
cce0: 6f 74 61 6c 2c 20 20 20 20 20 20 20 20 20 20 20 otal,
ccf0: 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 1, 0, 0, sumSt
cd00: 65 70 2c 20 20 20 20 20 20 20 20 20 74 6f 74 61 ep, tota
cd10: 6c 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a lFinalize ),.
cd20: 20 20 20 20 41 47 47 52 45 47 41 54 45 28 61 76 AGGREGATE(av
cd30: 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g,
cd40: 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 1, 0, 0, sumSte
cd50: 70 2c 20 20 20 20 20 20 20 20 20 61 76 67 46 69 p, avgFi
cd60: 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 2f 2a nalize ),. /*
cd70: 20 41 47 47 52 45 47 41 54 45 28 63 6f 75 6e 74 AGGREGATE(count
cd80: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c , 0,
cd90: 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53 74 65 70 0, 0, countStep
cda0: 2c 20 20 20 20 20 20 20 63 6f 75 6e 74 46 69 6e , countFin
cdb0: 61 6c 69 7a 65 20 20 29 2c 20 2a 2f 0a 20 20 20 alize ), */.
cdc0: 20 7b 30 2c 53 51 4c 49 54 45 5f 55 54 46 38 7c {0,SQLITE_UTF8|
cdd0: 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e SQLITE_FUNC_COUN
cde0: 54 2c 30 2c 30 2c 30 2c 63 6f 75 6e 74 53 74 65 T,0,0,0,countSte
cdf0: 70 2c 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 2c p,countFinalize,
ce00: 22 63 6f 75 6e 74 22 2c 30 2c 30 7d 2c 0a 20 20 "count",0,0},.
ce10: 20 20 41 47 47 52 45 47 41 54 45 28 63 6f 75 6e AGGREGATE(coun
ce20: 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 31 t, 1
ce30: 2c 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53 74 65 , 0, 0, countSte
ce40: 70 2c 20 20 20 20 20 20 20 63 6f 75 6e 74 46 69 p, countFi
ce50: 6e 61 6c 69 7a 65 20 20 29 2c 0a 20 20 20 20 41 nalize ),. A
ce60: 47 47 52 45 47 41 54 45 28 67 72 6f 75 70 5f 63 GGREGATE(group_c
ce70: 6f 6e 63 61 74 2c 20 20 20 20 20 20 31 2c 20 30 oncat, 1, 0
ce80: 2c 20 30 2c 20 67 72 6f 75 70 43 6f 6e 63 61 74 , 0, groupConcat
ce90: 53 74 65 70 2c 20 67 72 6f 75 70 43 6f 6e 63 61 Step, groupConca
cea0: 74 46 69 6e 61 6c 69 7a 65 29 2c 0a 20 20 20 20 tFinalize),.
ceb0: 41 47 47 52 45 47 41 54 45 28 67 72 6f 75 70 5f AGGREGATE(group_
cec0: 63 6f 6e 63 61 74 2c 20 20 20 20 20 20 32 2c 20 concat, 2,
ced0: 30 2c 20 30 2c 20 67 72 6f 75 70 43 6f 6e 63 61 0, 0, groupConca
cee0: 74 53 74 65 70 2c 20 67 72 6f 75 70 43 6f 6e 63 tStep, groupConc
cef0: 61 74 46 69 6e 61 6c 69 7a 65 29 2c 0a 20 20 0a atFinalize),. .
cf00: 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 67 6c 6f LIKEFUNC(glo
cf10: 62 2c 20 32 2c 20 26 67 6c 6f 62 49 6e 66 6f 2c b, 2, &globInfo,
cf20: 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b SQLITE_FUNC_LIK
cf30: 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 E|SQLITE_FUNC_CA
cf40: 53 45 29 2c 0a 20 20 23 69 66 64 65 66 20 53 51 SE),. #ifdef SQ
cf50: 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e 53 49 54 LITE_CASE_SENSIT
cf60: 49 56 45 5f 4c 49 4b 45 0a 20 20 20 20 4c 49 4b IVE_LIKE. LIK
cf70: 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 26 EFUNC(like, 2, &
cf80: 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 4c likeInfoAlt, SQL
cf90: 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 ITE_FUNC_LIKE|SQ
cfa0: 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c LITE_FUNC_CASE),
cfb0: 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 . LIKEFUNC(li
cfc0: 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 6f ke, 3, &likeInfo
cfd0: 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 Alt, SQLITE_FUNC
cfe0: 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e _LIKE|SQLITE_FUN
cff0: 43 5f 43 41 53 45 29 2c 0a 20 20 23 65 6c 73 65 C_CASE),. #else
d000: 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 . LIKEFUNC(li
d010: 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 6f ke, 2, &likeInfo
d020: 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 4e Norm, SQLITE_FUN
d030: 43 5f 4c 49 4b 45 29 2c 0a 20 20 20 20 4c 49 4b C_LIKE),. LIK
d040: 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20 26 EFUNC(like, 3, &
d050: 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53 51 likeInfoNorm, SQ
d060: 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 2c LITE_FUNC_LIKE),
d070: 0a 20 20 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a . #endif. };..
d080: 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 6e 63 44 int i;. FuncD
d090: 65 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 efHash *pHash =
d0a0: 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 &GLOBAL(FuncDefH
d0b0: 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 ash, sqlite3Glob
d0c0: 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 alFunctions);.
d0d0: 46 75 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 3d FuncDef *aFunc =
d0e0: 20 28 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f 42 (FuncDef*)&GLOB
d0f0: 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 42 75 69 AL(FuncDef, aBui
d100: 6c 74 69 6e 46 75 6e 63 29 3b 0a 0a 20 20 66 6f ltinFunc);.. fo
d110: 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 r(i=0; i<ArraySi
d120: 7a 65 28 61 42 75 69 6c 74 69 6e 46 75 6e 63 29 ze(aBuiltinFunc)
d130: 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 ; i++){. sqli
d140: 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 te3FuncDefInsert
d150: 28 70 48 61 73 68 2c 20 26 61 46 75 6e 63 5b 69 (pHash, &aFunc[i
d160: 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ]);. }. sqlite
d170: 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 3RegisterDateTim
d180: 65 46 75 6e 63 74 69 6f 6e 73 28 29 3b 0a 23 69 eFunctions();.#i
d190: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
d1a0: 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 73 T_ALTERTABLE. s
d1b0: 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 qlite3AlterFunct
d1c0: 69 6f 6e 73 28 29 3b 0a 23 65 6e 64 69 66 0a 23 ions();.#endif.#
d1d0: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
d1e0: 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 29 20 E_ENABLE_STAT3)
d1f0: 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
d200: 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 34 29 0a E_ENABLE_STAT4).
d210: 20 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 sqlite3Analyze
d220: 46 75 6e 63 74 69 6f 6e 73 28 29 3b 0a 23 65 6e Functions();.#en
d230: 64 69 66 0a 7d 0a dif.}.