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 56 64 62 65 4f 70 20 2a 70 4f 70 ){. VdbeOp *pOp
0320: 3b 0a 20 20 61 73 73 65 72 74 28 20 63 6f 6e 74 ;. assert( cont
0330: 65 78 74 2d 3e 70 56 64 62 65 21 3d 30 20 29 3b ext->pVdbe!=0 );
0340: 0a 20 20 70 4f 70 20 3d 20 26 63 6f 6e 74 65 78 . pOp = &contex
0350: 74 2d 3e 70 56 64 62 65 2d 3e 61 4f 70 5b 63 6f t->pVdbe->aOp[co
0360: 6e 74 65 78 74 2d 3e 69 4f 70 2d 31 5d 3b 0a 20 ntext->iOp-1];.
0370: 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 assert( pOp->op
0380: 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 code==OP_CollSeq
0390: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
03a0: 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f p->p4type==P4_CO
03b0: 4c 4c 53 45 51 20 29 3b 0a 20 20 72 65 74 75 72 LLSEQ );. retur
03c0: 6e 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3b n pOp->p4.pColl;
03d0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 .}../*.** Indica
03e0: 74 65 20 74 68 61 74 20 74 68 65 20 61 63 63 75 te that the accu
03f0: 6d 75 6c 61 74 6f 72 20 6c 6f 61 64 20 73 68 6f mulator load sho
0400: 75 6c 64 20 62 65 20 73 6b 69 70 70 65 64 20 6f uld be skipped o
0410: 6e 20 74 68 69 73 0a 2a 2a 20 69 74 65 72 61 74 n this.** iterat
0420: 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67 72 65 ion of the aggre
0430: 67 61 74 65 20 6c 6f 6f 70 2e 0a 2a 2f 0a 73 74 gate loop..*/.st
0440: 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
0450: 33 53 6b 69 70 41 63 63 75 6d 75 6c 61 74 6f 72 3SkipAccumulator
0460: 4c 6f 61 64 28 73 71 6c 69 74 65 33 5f 63 6f 6e Load(sqlite3_con
0470: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a text *context){.
0480: 20 20 63 6f 6e 74 65 78 74 2d 3e 73 6b 69 70 46 context->skipF
0490: 6c 61 67 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a lag = 1;.}../*.*
04a0: 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
04b0: 20 6f 66 20 74 68 65 20 6e 6f 6e 2d 61 67 67 72 of the non-aggr
04c0: 65 67 61 74 65 20 6d 69 6e 28 29 20 61 6e 64 20 egate min() and
04d0: 6d 61 78 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a max() functions.
04e0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
04f0: 69 6e 6d 61 78 46 75 6e 63 28 0a 20 20 73 71 6c inmaxFunc(. sql
0500: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
0510: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
0520: 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
0530: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 ue **argv.){. i
0540: 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d 61 73 6b nt i;. int mask
0550: 3b 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 69 ; /* 0 for mi
0560: 6e 28 29 20 6f 72 20 30 78 66 66 66 66 66 66 66 n() or 0xfffffff
0570: 66 20 66 6f 72 20 6d 61 78 28 29 20 2a 2f 0a 20 f for max() */.
0580: 20 69 6e 74 20 69 42 65 73 74 3b 0a 20 20 43 6f int iBest;. Co
0590: 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 llSeq *pColl;..
05a0: 20 61 73 73 65 72 74 28 20 61 72 67 63 3e 31 20 assert( argc>1
05b0: 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69 );. mask = sqli
05c0: 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f te3_user_data(co
05d0: 6e 74 65 78 74 29 3d 3d 30 20 3f 20 30 20 3a 20 ntext)==0 ? 0 :
05e0: 2d 31 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 -1;. pColl = sq
05f0: 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c lite3GetFuncColl
0600: 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 Seq(context);.
0610: 61 73 73 65 72 74 28 20 70 43 6f 6c 6c 20 29 3b assert( pColl );
0620: 0a 20 20 61 73 73 65 72 74 28 20 6d 61 73 6b 3d . assert( mask=
0630: 3d 2d 31 20 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 =-1 || mask==0 )
0640: 3b 0a 20 20 69 42 65 73 74 20 3d 20 30 3b 0a 20 ;. iBest = 0;.
0650: 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c if( sqlite3_val
0660: 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 ue_type(argv[0])
0670: 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 ==SQLITE_NULL )
0680: 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d return;. for(i=
0690: 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 1; i<argc; i++){
06a0: 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
06b0: 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 _value_type(argv
06c0: 5b 69 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c [i])==SQLITE_NUL
06d0: 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 L ) return;.
06e0: 69 66 28 20 28 73 71 6c 69 74 65 33 4d 65 6d 43 if( (sqlite3MemC
06f0: 6f 6d 70 61 72 65 28 61 72 67 76 5b 69 42 65 73 ompare(argv[iBes
0700: 74 5d 2c 20 61 72 67 76 5b 69 5d 2c 20 70 43 6f t], argv[i], pCo
0710: 6c 6c 29 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a ll)^mask)>=0 ){.
0720: 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
0730: 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 mask==0 );.
0740: 20 69 42 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 iBest = i;.
0750: 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f }. }. sqlite3_
0760: 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e result_value(con
0770: 74 65 78 74 2c 20 61 72 67 76 5b 69 42 65 73 74 text, argv[iBest
0780: 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ]);.}../*.** Ret
0790: 75 72 6e 20 74 68 65 20 74 79 70 65 20 6f 66 20 urn the type of
07a0: 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f the argument..*/
07b0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 79 70 .static void typ
07c0: 65 6f 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 eofFunc(. sqlit
07d0: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
07e0: 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 ext,. int NotUs
07f0: 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 ed,. sqlite3_va
0800: 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
0810: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 const char *z =
0820: 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 0;. UNUSED_PARA
0830: 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
0840: 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 switch( sqlite
0850: 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
0860: 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 v[0]) ){. cas
0870: 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 e SQLITE_INTEGER
0880: 3a 20 7a 20 3d 20 22 69 6e 74 65 67 65 72 22 3b : z = "integer";
0890: 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 break;. case
08a0: 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 20 20 SQLITE_TEXT:
08b0: 20 7a 20 3d 20 22 74 65 78 74 22 3b 20 20 20 20 z = "text";
08c0: 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
08d0: 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 20 20 SQLITE_FLOAT:
08e0: 7a 20 3d 20 22 72 65 61 6c 22 3b 20 20 20 20 62 z = "real"; b
08f0: 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 reak;. case S
0900: 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 20 20 20 7a QLITE_BLOB: z
0910: 20 3d 20 22 62 6c 6f 62 22 3b 20 20 20 20 62 72 = "blob"; br
0920: 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 eak;. default
0930: 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 20 : z
0940: 3d 20 22 6e 75 6c 6c 22 3b 20 20 20 20 62 72 65 = "null"; bre
0950: 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ak;. }. sqlite
0960: 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
0970: 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 ntext, z, -1, SQ
0980: 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a LITE_STATIC);.}.
0990: 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
09a0: 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 65 tation of the le
09b0: 6e 67 74 68 28 29 20 66 75 6e 63 74 69 6f 6e 0a ngth() function.
09c0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c */.static void l
09d0: 65 6e 67 74 68 46 75 6e 63 28 0a 20 20 73 71 6c engthFunc(. sql
09e0: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
09f0: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
0a00: 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
0a10: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 ue **argv.){. i
0a20: 6e 74 20 6c 65 6e 3b 0a 0a 20 20 61 73 73 65 72 nt len;.. asser
0a30: 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 t( argc==1 );.
0a40: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
0a50: 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68 (argc);. switch
0a60: 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ( sqlite3_value_
0a70: 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b type(argv[0]) ){
0a80: 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
0a90: 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61 73 65 20 _BLOB:. case
0aa0: 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a SQLITE_INTEGER:.
0ab0: 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
0ac0: 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73 FLOAT: {. s
0ad0: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
0ae0: 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 t(context, sqlit
0af0: 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
0b00: 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 rgv[0]));.
0b10: 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
0b20: 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 case SQLITE_TEX
0b30: 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 T: {. const
0b40: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
0b50: 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 z = sqlite3_valu
0b60: 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b e_text(argv[0]);
0b70: 0a 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 . if( z==0
0b80: 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 ) return;.
0b90: 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 len = 0;. w
0ba0: 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20 hile( *z ){.
0bb0: 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20 20 20 20 len++;.
0bc0: 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 SQLITE_SKIP_U
0bd0: 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 7d 0a TF8(z);. }.
0be0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 sqlite3_re
0bf0: 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 sult_int(context
0c00: 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20 20 62 72 , len);. br
0c10: 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
0c20: 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
0c30: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e sqlite3_result_n
0c40: 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 ull(context);.
0c50: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
0c60: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d . }.}../*.** Im
0c70: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
0c80: 74 68 65 20 61 62 73 28 29 20 66 75 6e 63 74 69 the abs() functi
0c90: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 3a 20 52 on..**.** IMP: R
0ca0: 2d 32 33 39 37 39 2d 32 36 38 35 35 20 54 68 65 -23979-26855 The
0cb0: 20 61 62 73 28 58 29 20 66 75 6e 63 74 69 6f 6e abs(X) function
0cc0: 20 72 65 74 75 72 6e 73 20 74 68 65 20 61 62 73 returns the abs
0cd0: 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f 66 0a 2a olute value of.*
0ce0: 2a 20 74 68 65 20 6e 75 6d 65 72 69 63 20 61 72 * the numeric ar
0cf0: 67 75 6d 65 6e 74 20 58 2e 20 0a 2a 2f 0a 73 74 gument X. .*/.st
0d00: 61 74 69 63 20 76 6f 69 64 20 61 62 73 46 75 6e atic void absFun
0d10: 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 c(sqlite3_contex
0d20: 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 t *context, int
0d30: 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 argc, sqlite3_va
0d40: 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 61 lue **argv){. a
0d50: 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 ssert( argc==1 )
0d60: 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
0d70: 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 73 77 ETER(argc);. sw
0d80: 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 itch( sqlite3_va
0d90: 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d lue_type(argv[0]
0da0: 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 ) ){. case SQ
0db0: 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a LITE_INTEGER: {.
0dc0: 20 20 20 20 20 20 69 36 34 20 69 56 61 6c 20 3d i64 iVal =
0dd0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 sqlite3_value_i
0de0: 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 nt64(argv[0]);.
0df0: 20 20 20 20 20 69 66 28 20 69 56 61 6c 3c 30 20 if( iVal<0
0e00: 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 ){. if( i
0e10: 56 61 6c 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e Val==SMALLEST_IN
0e20: 54 36 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 T64 ){.
0e30: 20 2f 2a 20 49 4d 50 3a 20 52 2d 33 31 36 37 36 /* IMP: R-31676
0e40: 2d 34 35 35 30 39 20 49 66 20 58 20 69 73 20 74 -45509 If X is t
0e50: 68 65 20 69 6e 74 65 67 65 72 20 2d 39 32 32 33 he integer -9223
0e60: 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38 0a 372036854775808.
0e70: 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 ** the
0e80: 6e 20 61 62 73 28 58 29 20 74 68 72 6f 77 73 20 n abs(X) throws
0e90: 61 6e 20 69 6e 74 65 67 65 72 20 6f 76 65 72 66 an integer overf
0ea0: 6c 6f 77 20 65 72 72 6f 72 20 73 69 6e 63 65 20 low error since
0eb0: 74 68 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 20 there is no.
0ec0: 20 20 20 20 20 20 2a 2a 20 65 71 75 69 76 61 6c ** equival
0ed0: 65 6e 74 20 70 6f 73 69 74 69 76 65 20 36 34 2d ent positive 64-
0ee0: 62 69 74 20 74 77 6f 20 63 6f 6d 70 6c 65 6d 65 bit two compleme
0ef0: 6e 74 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 nt value. */.
0f00: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 sqlite3_r
0f10: 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 esult_error(cont
0f20: 65 78 74 2c 20 22 69 6e 74 65 67 65 72 20 6f 76 ext, "integer ov
0f30: 65 72 66 6c 6f 77 22 2c 20 2d 31 29 3b 0a 20 20 erflow", -1);.
0f40: 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a return;.
0f50: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
0f60: 20 20 69 56 61 6c 20 3d 20 2d 69 56 61 6c 3b 0a iVal = -iVal;.
0f70: 20 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 73 } . s
0f80: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
0f90: 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 69 56 61 t64(context, iVa
0fa0: 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b l);. break;
0fb0: 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
0fc0: 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 SQLITE_NULL: {.
0fd0: 20 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 33 /* IMP: R-3
0fe0: 37 34 33 34 2d 31 39 39 32 39 20 41 62 73 28 58 7434-19929 Abs(X
0ff0: 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 ) returns NULL i
1000: 66 20 58 20 69 73 20 4e 55 4c 4c 2e 20 2a 2f 0a f X is NULL. */.
1010: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 sqlite3_re
1020: 73 75 6c 74 5f 6e 75 6c 6c 28 63 6f 6e 74 65 78 sult_null(contex
1030: 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b t);. break;
1040: 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
1050: 6c 74 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 42 lt: {. /* B
1060: 65 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 76 ecause sqlite3_v
1070: 61 6c 75 65 5f 64 6f 75 62 6c 65 28 29 20 72 65 alue_double() re
1080: 74 75 72 6e 73 20 30 2e 30 20 69 66 20 74 68 65 turns 0.0 if the
1090: 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 argument is not
10a0: 0a 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 74 68 . ** someth
10b0: 69 6e 67 20 74 68 61 74 20 63 61 6e 20 62 65 20 ing that can be
10c0: 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 converted into a
10d0: 20 6e 75 6d 62 65 72 2c 20 77 65 20 68 61 76 65 number, we have
10e0: 3a 0a 20 20 20 20 20 20 2a 2a 20 49 4d 50 3a 20 :. ** IMP:
10f0: 52 2d 30 31 39 39 32 2d 30 30 35 31 39 20 41 62 R-01992-00519 Ab
1100: 73 28 58 29 20 72 65 74 75 72 6e 73 20 30 2e 30 s(X) returns 0.0
1110: 20 69 66 20 58 20 69 73 20 61 20 73 74 72 69 6e if X is a strin
1120: 67 20 6f 72 20 62 6c 6f 62 0a 20 20 20 20 20 20 g or blob.
1130: 2a 2a 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 ** that cannot b
1140: 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 e converted to a
1150: 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 2e 0a numeric value..
1160: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 */. d
1170: 6f 75 62 6c 65 20 72 56 61 6c 20 3d 20 73 71 6c ouble rVal = sql
1180: 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c ite3_value_doubl
1190: 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 e(argv[0]);.
11a0: 20 20 69 66 28 20 72 56 61 6c 3c 30 20 29 20 72 if( rVal<0 ) r
11b0: 56 61 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20 20 20 Val = -rVal;.
11c0: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
11d0: 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 t_double(context
11e0: 2c 20 72 56 61 6c 29 3b 0a 20 20 20 20 20 20 62 , rVal);. b
11f0: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
1200: 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
1210: 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 69 ntation of the i
1220: 6e 73 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 2e nstr() function.
1230: 0a 2a 2a 0a 2a 2a 20 69 6e 73 74 72 28 68 61 79 .**.** instr(hay
1240: 73 74 61 63 6b 2c 6e 65 65 64 6c 65 29 20 66 69 stack,needle) fi
1250: 6e 64 73 20 74 68 65 20 66 69 72 73 74 20 6f 63 nds the first oc
1260: 63 75 72 72 65 6e 63 65 20 6f 66 20 6e 65 65 64 currence of need
1270: 6c 65 0a 2a 2a 20 69 6e 20 68 61 79 73 74 61 63 le.** in haystac
1280: 6b 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 k and returns th
1290: 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 65 76 e number of prev
12a0: 69 6f 75 73 20 63 68 61 72 61 63 74 65 72 73 20 ious characters
12b0: 70 6c 75 73 20 31 2c 0a 2a 2a 20 6f 72 20 30 20 plus 1,.** or 0
12c0: 69 66 20 6e 65 65 64 6c 65 20 64 6f 65 73 20 6e if needle does n
12d0: 6f 74 20 6f 63 63 75 72 20 77 69 74 68 69 6e 20 ot occur within
12e0: 68 61 79 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 haystack..**.**
12f0: 49 66 20 62 6f 74 68 20 68 61 79 73 74 61 63 6b If both haystack
1300: 20 61 6e 64 20 6e 65 65 64 6c 65 20 61 72 65 20 and needle are
1310: 42 4c 4f 42 73 2c 20 74 68 65 6e 20 74 68 65 20 BLOBs, then the
1320: 72 65 73 75 6c 74 20 69 73 20 6f 6e 65 20 6d 6f result is one mo
1330: 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6e re than.** the n
1340: 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1350: 6e 20 68 61 79 73 74 61 63 6b 20 70 72 69 6f 72 n haystack prior
1360: 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 6f 63 to the first oc
1370: 63 75 72 72 65 6e 63 65 20 6f 66 20 6e 65 65 64 currence of need
1380: 6c 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 6e le,.** or 0 if n
1390: 65 65 64 6c 65 20 6e 65 76 65 72 20 6f 63 63 75 eedle never occu
13a0: 72 73 20 69 6e 20 68 61 79 73 74 61 63 6b 2e 0a rs in haystack..
13b0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 */.static void i
13c0: 6e 73 74 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 nstrFunc(. sqli
13d0: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
13e0: 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
13f0: 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
1400: 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f e **argv.){. co
1410: 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
1420: 72 20 2a 7a 48 61 79 73 74 61 63 6b 3b 0a 20 20 r *zHaystack;.
1430: 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1440: 68 61 72 20 2a 7a 4e 65 65 64 6c 65 3b 0a 20 20 har *zNeedle;.
1450: 69 6e 74 20 6e 48 61 79 73 74 61 63 6b 3b 0a 20 int nHaystack;.
1460: 20 69 6e 74 20 6e 4e 65 65 64 6c 65 3b 0a 20 20 int nNeedle;.
1470: 69 6e 74 20 74 79 70 65 48 61 79 73 74 61 63 6b int typeHaystack
1480: 2c 20 74 79 70 65 4e 65 65 64 6c 65 3b 0a 20 20 , typeNeedle;.
1490: 69 6e 74 20 4e 20 3d 20 31 3b 0a 20 20 69 6e 74 int N = 1;. int
14a0: 20 69 73 54 65 78 74 3b 0a 0a 20 20 55 4e 55 53 isText;.. UNUS
14b0: 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 ED_PARAMETER(arg
14c0: 63 29 3b 0a 20 20 74 79 70 65 48 61 79 73 74 61 c);. typeHaysta
14d0: 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c ck = sqlite3_val
14e0: 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 ue_type(argv[0])
14f0: 3b 0a 20 20 74 79 70 65 4e 65 65 64 6c 65 20 3d ;. typeNeedle =
1500: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1510: 79 70 65 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 ype(argv[1]);.
1520: 69 66 28 20 74 79 70 65 48 61 79 73 74 61 63 6b if( typeHaystack
1530: 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 7c 7c ==SQLITE_NULL ||
1540: 20 74 79 70 65 4e 65 65 64 6c 65 3d 3d 53 51 4c typeNeedle==SQL
1550: 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 ITE_NULL ) retur
1560: 6e 3b 0a 20 20 6e 48 61 79 73 74 61 63 6b 20 3d n;. nHaystack =
1570: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
1580: 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 ytes(argv[0]);.
1590: 20 6e 4e 65 65 64 6c 65 20 3d 20 73 71 6c 69 74 nNeedle = sqlit
15a0: 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
15b0: 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 6e rgv[1]);. if( n
15c0: 4e 65 65 64 6c 65 3e 30 20 29 7b 0a 20 20 20 20 Needle>0 ){.
15d0: 69 66 28 20 74 79 70 65 48 61 79 73 74 61 63 6b if( typeHaystack
15e0: 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 26 26 ==SQLITE_BLOB &&
15f0: 20 74 79 70 65 4e 65 65 64 6c 65 3d 3d 53 51 4c typeNeedle==SQL
1600: 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 ITE_BLOB ){.
1610: 20 20 7a 48 61 79 73 74 61 63 6b 20 3d 20 73 71 zHaystack = sq
1620: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
1630: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 (argv[0]);.
1640: 20 7a 4e 65 65 64 6c 65 20 3d 20 73 71 6c 69 74 zNeedle = sqlit
1650: 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 e3_value_blob(ar
1660: 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 69 73 gv[1]);. is
1670: 54 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 7d 65 Text = 0;. }e
1680: 6c 73 65 7b 0a 20 20 20 20 20 20 7a 48 61 79 73 lse{. zHays
1690: 74 61 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 76 tack = sqlite3_v
16a0: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
16b0: 5d 29 3b 0a 20 20 20 20 20 20 7a 4e 65 65 64 6c ]);. zNeedl
16c0: 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 e = sqlite3_valu
16d0: 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b e_text(argv[1]);
16e0: 0a 20 20 20 20 20 20 69 73 54 65 78 74 20 3d 20 . isText =
16f0: 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
1700: 20 7a 4e 65 65 64 6c 65 3d 3d 30 20 7c 7c 20 28 zNeedle==0 || (
1710: 6e 48 61 79 73 74 61 63 6b 20 26 26 20 7a 48 61 nHaystack && zHa
1720: 79 73 74 61 63 6b 3d 3d 30 29 20 29 20 72 65 74 ystack==0) ) ret
1730: 75 72 6e 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 urn;. while(
1740: 6e 4e 65 65 64 6c 65 3c 3d 6e 48 61 79 73 74 61 nNeedle<=nHaysta
1750: 63 6b 20 26 26 20 6d 65 6d 63 6d 70 28 7a 48 61 ck && memcmp(zHa
1760: 79 73 74 61 63 6b 2c 20 7a 4e 65 65 64 6c 65 2c ystack, zNeedle,
1770: 20 6e 4e 65 65 64 6c 65 29 21 3d 30 20 29 7b 0a nNeedle)!=0 ){.
1780: 20 20 20 20 20 20 4e 2b 2b 3b 0a 20 20 20 20 20 N++;.
1790: 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 6e 48 61 do{. nHa
17a0: 79 73 74 61 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 ystack--;.
17b0: 20 20 7a 48 61 79 73 74 61 63 6b 2b 2b 3b 0a 20 zHaystack++;.
17c0: 20 20 20 20 20 7d 77 68 69 6c 65 28 20 69 73 54 }while( isT
17d0: 65 78 74 20 26 26 20 28 7a 48 61 79 73 74 61 63 ext && (zHaystac
17e0: 6b 5b 30 5d 26 30 78 63 30 29 3d 3d 30 78 38 30 k[0]&0xc0)==0x80
17f0: 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 );. }. if
1800: 28 20 6e 4e 65 65 64 6c 65 3e 6e 48 61 79 73 74 ( nNeedle>nHayst
1810: 61 63 6b 20 29 20 4e 20 3d 20 30 3b 0a 20 20 7d ack ) N = 0;. }
1820: 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
1830: 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 4e t_int(context, N
1840: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c );.}../*.** Impl
1850: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
1860: 65 20 70 72 69 6e 74 66 28 29 20 66 75 6e 63 74 e printf() funct
1870: 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ion..*/.static v
1880: 6f 69 64 20 70 72 69 6e 74 66 46 75 6e 63 28 0a oid printfFunc(.
1890: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
18a0: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
18b0: 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
18c0: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
18d0: 7b 0a 20 20 50 72 69 6e 74 66 41 72 67 75 6d 65 {. PrintfArgume
18e0: 6e 74 73 20 78 3b 0a 20 20 53 74 72 41 63 63 75 nts x;. StrAccu
18f0: 6d 20 73 74 72 3b 0a 20 20 63 6f 6e 73 74 20 63 m str;. const c
1900: 68 61 72 20 2a 7a 46 6f 72 6d 61 74 3b 0a 20 20 har *zFormat;.
1910: 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 int n;. sqlite3
1920: 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 *db = sqlite3_c
1930: 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
1940: 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 69 66 (context);.. if
1950: 28 20 61 72 67 63 3e 3d 31 20 26 26 20 28 7a 46 ( argc>=1 && (zF
1960: 6f 72 6d 61 74 20 3d 20 28 63 6f 6e 73 74 20 63 ormat = (const c
1970: 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c har*)sqlite3_val
1980: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
1990: 29 21 3d 30 20 29 7b 0a 20 20 20 20 78 2e 6e 41 )!=0 ){. x.nA
19a0: 72 67 20 3d 20 61 72 67 63 2d 31 3b 0a 20 20 20 rg = argc-1;.
19b0: 20 78 2e 6e 55 73 65 64 20 3d 20 30 3b 0a 20 20 x.nUsed = 0;.
19c0: 20 20 78 2e 61 70 41 72 67 20 3d 20 61 72 67 76 x.apArg = argv
19d0: 2b 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 +1;. sqlite3S
19e0: 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 74 72 trAccumInit(&str
19f0: 2c 20 64 62 2c 20 30 2c 20 30 2c 20 64 62 2d 3e , db, 0, 0, db->
1a00: 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1a10: 4d 49 54 5f 4c 45 4e 47 54 48 5d 29 3b 0a 20 20 MIT_LENGTH]);.
1a20: 20 20 73 74 72 2e 70 72 69 6e 74 66 46 6c 61 67 str.printfFlag
1a30: 73 20 3d 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 s = SQLITE_PRINT
1a40: 46 5f 53 51 4c 46 55 4e 43 3b 0a 20 20 20 20 73 F_SQLFUNC;. s
1a50: 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 26 73 qlite3XPrintf(&s
1a60: 74 72 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 78 29 tr, zFormat, &x)
1a70: 3b 0a 20 20 20 20 6e 20 3d 20 73 74 72 2e 6e 43 ;. n = str.nC
1a80: 68 61 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 har;. sqlite3
1a90: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
1aa0: 74 65 78 74 2c 20 73 71 6c 69 74 65 33 53 74 72 text, sqlite3Str
1ab0: 41 63 63 75 6d 46 69 6e 69 73 68 28 26 73 74 72 AccumFinish(&str
1ac0: 29 2c 20 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 ), n,.
1ad0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 SQ
1ae0: 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 LITE_DYNAMIC);.
1af0: 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c }.}../*.** Impl
1b00: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
1b10: 65 20 73 75 62 73 74 72 28 29 20 66 75 6e 63 74 e substr() funct
1b20: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 73 74 ion..**.** subst
1b30: 72 28 78 2c 70 31 2c 70 32 29 20 20 72 65 74 75 r(x,p1,p2) retu
1b40: 72 6e 73 20 70 32 20 63 68 61 72 61 63 74 65 72 rns p2 character
1b50: 73 20 6f 66 20 78 5b 5d 20 62 65 67 69 6e 6e 69 s of x[] beginni
1b60: 6e 67 20 77 69 74 68 20 70 31 2e 0a 2a 2a 20 70 ng with p1..** p
1b70: 31 20 69 73 20 31 2d 69 6e 64 65 78 65 64 2e 20 1 is 1-indexed.
1b80: 20 53 6f 20 73 75 62 73 74 72 28 78 2c 31 2c 31 So substr(x,1,1
1b90: 29 20 72 65 74 75 72 6e 73 20 74 68 65 20 66 69 ) returns the fi
1ba0: 72 73 74 20 63 68 61 72 61 63 74 65 72 0a 2a 2a rst character.**
1bb0: 20 6f 66 20 78 2e 20 20 49 66 20 78 20 69 73 20 of x. If x is
1bc0: 74 65 78 74 2c 20 74 68 65 6e 20 77 65 20 61 63 text, then we ac
1bd0: 74 75 61 6c 6c 79 20 63 6f 75 6e 74 20 55 54 46 tually count UTF
1be0: 2d 38 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a -8 characters..*
1bf0: 2a 20 49 66 20 78 20 69 73 20 61 20 62 6c 6f 62 * If x is a blob
1c00: 2c 20 74 68 65 6e 20 77 65 20 63 6f 75 6e 74 20 , then we count
1c10: 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 bytes..**.** If
1c20: 70 31 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 p1 is negative,
1c30: 74 68 65 6e 20 77 65 20 62 65 67 69 6e 20 61 62 then we begin ab
1c40: 73 28 70 31 29 20 66 72 6f 6d 20 74 68 65 20 65 s(p1) from the e
1c50: 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a 2a 0a 2a 2a nd of x[]..**.**
1c60: 20 49 66 20 70 32 20 69 73 20 6e 65 67 61 74 69 If p2 is negati
1c70: 76 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 70 ve, return the p
1c80: 32 20 63 68 61 72 61 63 74 65 72 73 20 70 72 65 2 characters pre
1c90: 63 65 64 69 6e 67 20 70 31 2e 0a 2a 2f 0a 73 74 ceding p1..*/.st
1ca0: 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 72 atic void substr
1cb0: 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
1cc0: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
1cd0: 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
1ce0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
1cf0: 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 argv.){. const
1d00: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
1d10: 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e ;. const unsign
1d20: 65 64 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 ed char *z2;. i
1d30: 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 70 30 nt len;. int p0
1d40: 74 79 70 65 3b 0a 20 20 69 36 34 20 70 31 2c 20 type;. i64 p1,
1d50: 70 32 3b 0a 20 20 69 6e 74 20 6e 65 67 50 32 20 p2;. int negP2
1d60: 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 = 0;.. assert(
1d70: 61 72 67 63 3d 3d 33 20 7c 7c 20 61 72 67 63 3d argc==3 || argc=
1d80: 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 =2 );. if( sqli
1d90: 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 te3_value_type(a
1da0: 72 67 76 5b 31 5d 29 3d 3d 53 51 4c 49 54 45 5f rgv[1])==SQLITE_
1db0: 4e 55 4c 4c 0a 20 20 20 7c 7c 20 28 61 72 67 63 NULL. || (argc
1dc0: 3d 3d 33 20 26 26 20 73 71 6c 69 74 65 33 5f 76 ==3 && sqlite3_v
1dd0: 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 32 alue_type(argv[2
1de0: 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 29 ])==SQLITE_NULL)
1df0: 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e . ){. return
1e00: 3b 0a 20 20 7d 0a 20 20 70 30 74 79 70 65 20 3d ;. }. p0type =
1e10: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1e20: 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 ype(argv[0]);.
1e30: 70 31 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c p1 = sqlite3_val
1e40: 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b ue_int(argv[1]);
1e50: 0a 20 20 69 66 28 20 70 30 74 79 70 65 3d 3d 53 . if( p0type==S
1e60: 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 QLITE_BLOB ){.
1e70: 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f len = sqlite3_
1e80: 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 value_bytes(argv
1e90: 5b 30 5d 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 [0]);. z = sq
1ea0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
1eb0: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 (argv[0]);. i
1ec0: 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e f( z==0 ) return
1ed0: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 ;. assert( le
1ee0: 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 n==sqlite3_value
1ef0: 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 20 _bytes(argv[0])
1f00: 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1f10: 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 z = sqlite3_valu
1f20: 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b e_text(argv[0]);
1f30: 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 . if( z==0 )
1f40: 72 65 74 75 72 6e 3b 0a 20 20 20 20 6c 65 6e 20 return;. len
1f50: 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 31 3c = 0;. if( p1<
1f60: 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 7a 0 ){. for(z
1f70: 32 3d 7a 3b 20 2a 7a 32 3b 20 6c 65 6e 2b 2b 29 2=z; *z2; len++)
1f80: 7b 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 {. SQLITE
1f90: 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a _SKIP_UTF8(z2);.
1fa0: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1fb0: 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f }.#ifdef SQLITE_
1fc0: 53 55 42 53 54 52 5f 43 4f 4d 50 41 54 49 42 49 SUBSTR_COMPATIBI
1fd0: 4c 49 54 59 0a 20 20 2f 2a 20 49 66 20 53 55 42 LITY. /* If SUB
1fe0: 53 54 52 5f 43 4f 4d 50 41 54 49 42 49 4c 49 54 STR_COMPATIBILIT
1ff0: 59 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 65 Y is defined the
2000: 6e 20 73 75 62 73 74 72 28 58 2c 30 2c 4e 29 20 n substr(X,0,N)
2010: 77 6f 72 6b 20 74 68 65 20 73 61 6d 65 20 61 73 work the same as
2020: 0a 20 20 2a 2a 20 61 73 20 73 75 62 73 74 72 28 . ** as substr(
2030: 58 2c 31 2c 4e 29 20 2d 20 69 74 20 72 65 74 75 X,1,N) - it retu
2040: 72 6e 73 20 74 68 65 20 66 69 72 73 74 20 4e 20 rns the first N
2050: 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 58 2e characters of X.
2060: 20 20 54 68 69 73 0a 20 20 2a 2a 20 69 73 20 65 This. ** is e
2070: 73 73 65 6e 74 69 61 6c 6c 79 20 61 20 62 61 63 ssentially a bac
2080: 6b 2d 6f 75 74 20 6f 66 20 74 68 65 20 62 75 67 k-out of the bug
2090: 2d 66 69 78 20 69 6e 20 63 68 65 63 6b 2d 69 6e -fix in check-in
20a0: 20 5b 35 66 63 31 32 35 64 33 36 32 64 66 34 62 [5fc125d362df4b
20b0: 38 5d 0a 20 20 2a 2a 20 66 72 6f 6d 20 32 30 30 8]. ** from 200
20c0: 39 2d 30 32 2d 30 32 20 66 6f 72 20 63 6f 6d 70 9-02-02 for comp
20d0: 61 74 69 62 69 6c 69 74 79 20 6f 66 20 61 70 70 atibility of app
20e0: 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 65 lications that e
20f0: 78 70 6c 6f 69 74 65 64 20 74 68 65 0a 20 20 2a xploited the. *
2100: 2a 20 6f 6c 64 20 62 75 67 67 79 20 62 65 68 61 * old buggy beha
2110: 76 69 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 vior. */. if( p
2120: 31 3d 3d 30 20 29 20 70 31 20 3d 20 31 3b 20 2f 1==0 ) p1 = 1; /
2130: 2a 20 3c 72 64 61 72 3a 2f 2f 70 72 6f 62 6c 65 * <rdar://proble
2140: 6d 2f 36 37 37 38 33 33 39 3e 20 2a 2f 0a 23 65 m/6778339> */.#e
2150: 6e 64 69 66 0a 20 20 69 66 28 20 61 72 67 63 3d ndif. if( argc=
2160: 3d 33 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 73 =3 ){. p2 = s
2170: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
2180: 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 (argv[2]);. i
2190: 66 28 20 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 f( p2<0 ){.
21a0: 20 70 32 20 3d 20 2d 70 32 3b 0a 20 20 20 20 20 p2 = -p2;.
21b0: 20 6e 65 67 50 32 20 3d 20 31 3b 0a 20 20 20 20 negP2 = 1;.
21c0: 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 }. }else{. p
21d0: 32 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 2 = sqlite3_cont
21e0: 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f ext_db_handle(co
21f0: 6e 74 65 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53 ntext)->aLimit[S
2200: 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
2210: 54 48 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 TH];. }. if( p
2220: 31 3c 30 20 29 7b 0a 20 20 20 20 70 31 20 2b 3d 1<0 ){. p1 +=
2230: 20 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 31 len;. if( p1
2240: 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32 20 2b <0 ){. p2 +
2250: 3d 20 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 = p1;. if(
2260: 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 p2<0 ) p2 = 0;.
2270: 20 20 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 p1 = 0;.
2280: 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }. }else if( p
2290: 31 3e 30 20 29 7b 0a 20 20 20 20 70 31 2d 2d 3b 1>0 ){. p1--;
22a0: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 32 3e . }else if( p2>
22b0: 30 20 29 7b 0a 20 20 20 20 70 32 2d 2d 3b 0a 20 0 ){. p2--;.
22c0: 20 7d 0a 20 20 69 66 28 20 6e 65 67 50 32 20 29 }. if( negP2 )
22d0: 7b 0a 20 20 20 20 70 31 20 2d 3d 20 70 32 3b 0a {. p1 -= p2;.
22e0: 20 20 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a if( p1<0 ){.
22f0: 20 20 20 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a p2 += p1;.
2300: 20 20 20 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 p1 = 0;.
2310: 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 }. }. assert
2320: 28 20 70 31 3e 3d 30 20 26 26 20 70 32 3e 3d 30 ( p1>=0 && p2>=0
2330: 20 29 3b 0a 20 20 69 66 28 20 70 30 74 79 70 65 );. if( p0type
2340: 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b !=SQLITE_BLOB ){
2350: 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 20 26 . while( *z &
2360: 26 20 70 31 20 29 7b 0a 20 20 20 20 20 20 53 51 & p1 ){. SQ
2370: 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a LITE_SKIP_UTF8(z
2380: 29 3b 0a 20 20 20 20 20 20 70 31 2d 2d 3b 0a 20 );. p1--;.
2390: 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 7a 32 3d }. for(z2=
23a0: 7a 3b 20 2a 7a 32 20 26 26 20 70 32 3b 20 70 32 z; *z2 && p2; p2
23b0: 2d 2d 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 --){. SQLIT
23c0: 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b E_SKIP_UTF8(z2);
23d0: 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
23e0: 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 36 34 e3_result_text64
23f0: 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a (context, (char*
2400: 29 7a 2c 20 7a 32 2d 7a 2c 20 53 51 4c 49 54 45 )z, z2-z, SQLITE
2410: 5f 54 52 41 4e 53 49 45 4e 54 2c 0a 20 20 20 20 _TRANSIENT,.
2420: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2430: 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 SQLITE_UTF
2440: 38 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 8);. }else{.
2450: 20 69 66 28 20 70 31 2b 70 32 3e 6c 65 6e 20 29 if( p1+p2>len )
2460: 7b 0a 20 20 20 20 20 20 70 32 20 3d 20 6c 65 6e {. p2 = len
2470: 2d 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 -p1;. if( p
2480: 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 20 2<0 ) p2 = 0;.
2490: 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
24a0: 72 65 73 75 6c 74 5f 62 6c 6f 62 36 34 28 63 6f result_blob64(co
24b0: 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 26 7a ntext, (char*)&z
24c0: 5b 70 31 5d 2c 20 28 75 36 34 29 70 32 2c 20 53 [p1], (u64)p2, S
24d0: 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
24e0: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
24f0: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
2500: 20 74 68 65 20 72 6f 75 6e 64 28 29 20 66 75 6e the round() fun
2510: 63 74 69 6f 6e 0a 2a 2f 0a 23 69 66 6e 64 65 66 ction.*/.#ifndef
2520: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
2530: 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 73 74 61 74 ATING_POINT.stat
2540: 69 63 20 76 6f 69 64 20 72 6f 75 6e 64 46 75 6e ic void roundFun
2550: 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 c(sqlite3_contex
2560: 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 t *context, int
2570: 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 argc, sqlite3_va
2580: 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 69 lue **argv){. i
2590: 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 75 62 nt n = 0;. doub
25a0: 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 42 le r;. char *zB
25b0: 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 uf;. assert( ar
25c0: 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 gc==1 || argc==2
25d0: 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d );. if( argc==
25e0: 32 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 2 ){. if( SQL
25f0: 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 ITE_NULL==sqlite
2600: 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
2610: 76 5b 31 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a v[1]) ) return;.
2620: 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f n = sqlite3_
2630: 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 value_int(argv[1
2640: 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 33 30 ]);. if( n>30
2650: 20 29 20 6e 20 3d 20 33 30 3b 0a 20 20 20 20 69 ) n = 30;. i
2660: 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a f( n<0 ) n = 0;.
2670: 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
2680: 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
2690: 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 v[0])==SQLITE_NU
26a0: 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 72 LL ) return;. r
26b0: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
26c0: 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 _double(argv[0])
26d0: 3b 0a 20 20 2f 2a 20 49 66 20 59 3d 3d 30 20 61 ;. /* If Y==0 a
26e0: 6e 64 20 58 20 77 69 6c 6c 20 66 69 74 20 69 6e nd X will fit in
26f0: 20 61 20 36 34 2d 62 69 74 20 69 6e 74 2c 0a 20 a 64-bit int,.
2700: 20 2a 2a 20 68 61 6e 64 6c 65 20 74 68 65 20 72 ** handle the r
2710: 6f 75 6e 64 69 6e 67 20 64 69 72 65 63 74 6c 79 ounding directly
2720: 2c 0a 20 20 2a 2a 20 6f 74 68 65 72 77 69 73 65 ,. ** otherwise
2730: 20 75 73 65 20 70 72 69 6e 74 66 2e 0a 20 20 2a use printf.. *
2740: 2f 0a 20 20 69 66 28 20 6e 3d 3d 30 20 26 26 20 /. if( n==0 &&
2750: 72 3e 3d 30 20 26 26 20 72 3c 4c 41 52 47 45 53 r>=0 && r<LARGES
2760: 54 5f 49 4e 54 36 34 2d 31 20 29 7b 0a 20 20 20 T_INT64-1 ){.
2770: 20 72 20 3d 20 28 64 6f 75 62 6c 65 29 28 28 73 r = (double)((s
2780: 71 6c 69 74 65 5f 69 6e 74 36 34 29 28 72 2b 30 qlite_int64)(r+0
2790: 2e 35 29 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 .5));. }else if
27a0: 28 20 6e 3d 3d 30 20 26 26 20 72 3c 30 20 26 26 ( n==0 && r<0 &&
27b0: 20 28 2d 72 29 3c 4c 41 52 47 45 53 54 5f 49 4e (-r)<LARGEST_IN
27c0: 54 36 34 2d 31 20 29 7b 0a 20 20 20 20 72 20 3d T64-1 ){. r =
27d0: 20 2d 28 64 6f 75 62 6c 65 29 28 28 73 71 6c 69 -(double)((sqli
27e0: 74 65 5f 69 6e 74 36 34 29 28 28 2d 72 29 2b 30 te_int64)((-r)+0
27f0: 2e 35 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 .5));. }else{.
2800: 20 20 20 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 zBuf = sqlite
2810: 33 5f 6d 70 72 69 6e 74 66 28 22 25 2e 2a 66 22 3_mprintf("%.*f"
2820: 2c 6e 2c 72 29 3b 0a 20 20 20 20 69 66 28 20 7a ,n,r);. if( z
2830: 42 75 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Buf==0 ){.
2840: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2850: 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 rror_nomem(conte
2860: 78 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 xt);. retur
2870: 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c n;. }. sql
2880: 69 74 65 33 41 74 6f 46 28 7a 42 75 66 2c 20 26 ite3AtoF(zBuf, &
2890: 72 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e r, sqlite3Strlen
28a0: 33 30 28 7a 42 75 66 29 2c 20 53 51 4c 49 54 45 30(zBuf), SQLITE
28b0: 5f 55 54 46 38 29 3b 0a 20 20 20 20 73 71 6c 69 _UTF8);. sqli
28c0: 74 65 33 5f 66 72 65 65 28 7a 42 75 66 29 3b 0a te3_free(zBuf);.
28d0: 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 }. sqlite3_re
28e0: 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 sult_double(cont
28f0: 65 78 74 2c 20 72 29 3b 0a 7d 0a 23 65 6e 64 69 ext, r);.}.#endi
2900: 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 f../*.** Allocat
2910: 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 e nByte bytes of
2920: 20 73 70 61 63 65 20 75 73 69 6e 67 20 73 71 6c space using sql
2930: 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 ite3Malloc(). If
2940: 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 the.** allocati
2950: 6f 6e 20 66 61 69 6c 73 2c 20 63 61 6c 6c 20 73 on fails, call s
2960: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2970: 72 6f 72 5f 6e 6f 6d 65 6d 28 29 20 74 6f 20 6e ror_nomem() to n
2980: 6f 74 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 otify.** the dat
2990: 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 abase handle tha
29a0: 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 t malloc() has f
29b0: 61 69 6c 65 64 20 61 6e 64 20 72 65 74 75 72 6e ailed and return
29c0: 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 6e 42 79 NULL..** If nBy
29d0: 74 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 te is larger tha
29e0: 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 74 n the maximum st
29f0: 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 65 6e ring or blob len
2a00: 67 74 68 2c 20 74 68 65 6e 0a 2a 2a 20 72 61 69 gth, then.** rai
2a10: 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f 4f se an SQLITE_TOO
2a20: 42 49 47 20 65 78 63 65 70 74 69 6f 6e 20 61 6e BIG exception an
2a30: 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a d return NULL..*
2a40: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 /.static void *c
2a50: 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 73 71 6c ontextMalloc(sql
2a60: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
2a70: 6e 74 65 78 74 2c 20 69 36 34 20 6e 42 79 74 65 ntext, i64 nByte
2a80: 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 ){. char *z;.
2a90: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 sqlite3 *db = sq
2aa0: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
2ab0: 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
2ac0: 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 ;. assert( nByt
2ad0: 65 3e 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73 e>0 );. testcas
2ae0: 65 28 20 6e 42 79 74 65 3d 3d 64 62 2d 3e 61 4c e( nByte==db->aL
2af0: 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
2b00: 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74 T_LENGTH] );. t
2b10: 65 73 74 63 61 73 65 28 20 6e 42 79 74 65 3d 3d estcase( nByte==
2b20: 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
2b30: 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b E_LIMIT_LENGTH]+
2b40: 31 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 1 );. if( nByte
2b50: 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
2b60: 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
2b70: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
2b80: 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f result_error_too
2b90: 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 big(context);.
2ba0: 20 20 7a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 z = 0;. }else
2bb0: 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 {. z = sqlite
2bc0: 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 3Malloc(nByte);.
2bd0: 20 20 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 if( !z ){.
2be0: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
2bf0: 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 lt_error_nomem(c
2c00: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 ontext);. }.
2c10: 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d }. return z;.}
2c20: 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
2c30: 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 75 70 tation of the up
2c40: 70 65 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 per() and lower(
2c50: 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e ) SQL functions.
2c60: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
2c70: 75 70 70 65 72 46 75 6e 63 28 73 71 6c 69 74 65 upperFunc(sqlite
2c80: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
2c90: 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 xt, int argc, sq
2ca0: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
2cb0: 67 76 29 7b 0a 20 20 63 68 61 72 20 2a 7a 31 3b gv){. char *z1;
2cc0: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
2cd0: 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 2;. int i, n;.
2ce0: 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
2cf0: 52 28 61 72 67 63 29 3b 0a 20 20 7a 32 20 3d 20 R(argc);. z2 =
2d00: 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 (char*)sqlite3_v
2d10: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
2d20: 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 ]);. n = sqlite
2d30: 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 3_value_bytes(ar
2d40: 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 gv[0]);. /* Ver
2d50: 69 66 79 20 74 68 61 74 20 74 68 65 20 63 61 6c ify that the cal
2d60: 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 20 64 6f l to _bytes() do
2d70: 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 es not invalidat
2d80: 65 20 74 68 65 20 5f 74 65 78 74 28 29 20 70 6f e the _text() po
2d90: 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 inter */. asser
2da0: 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 t( z2==(char*)sq
2db0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
2dc0: 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 (argv[0]) );. i
2dd0: 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 f( z2 ){. z1
2de0: 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 = contextMalloc(
2df0: 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e context, ((i64)n
2e00: 29 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 )+1);. if( z1
2e10: 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d ){. for(i=
2e20: 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 0; i<n; i++){.
2e30: 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 28 63 z1[i] = (c
2e40: 68 61 72 29 73 71 6c 69 74 65 33 54 6f 75 70 70 har)sqlite3Toupp
2e50: 65 72 28 7a 32 5b 69 5d 29 3b 0a 20 20 20 20 20 er(z2[i]);.
2e60: 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
2e70: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
2e80: 74 65 78 74 2c 20 7a 31 2c 20 6e 2c 20 73 71 6c text, z1, n, sql
2e90: 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 ite3_free);.
2ea0: 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 }. }.}.static v
2eb0: 6f 69 64 20 6c 6f 77 65 72 46 75 6e 63 28 73 71 oid lowerFunc(sq
2ec0: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
2ed0: 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 ontext, int argc
2ee0: 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 , sqlite3_value
2ef0: 2a 2a 61 72 67 76 29 7b 0a 20 20 63 68 61 72 20 **argv){. char
2f00: 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 *z1;. const cha
2f10: 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 r *z2;. int i,
2f20: 6e 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 n;. UNUSED_PARA
2f30: 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 7a METER(argc);. z
2f40: 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 2 = (char*)sqlit
2f50: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
2f60: 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 gv[0]);. n = sq
2f70: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
2f80: 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a s(argv[0]);. /*
2f90: 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 Verify that the
2fa0: 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 call to _bytes(
2fb0: 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c ) does not inval
2fc0: 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 idate the _text(
2fd0: 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 ) pointer */. a
2fe0: 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 ssert( z2==(char
2ff0: 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *)sqlite3_value_
3000: 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b text(argv[0]) );
3010: 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 . if( z2 ){.
3020: 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c z1 = contextMal
3030: 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 loc(context, ((i
3040: 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66 64)n)+1);. if
3050: 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 66 6f ( z1 ){. fo
3060: 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 r(i=0; i<n; i++)
3070: 7b 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 {. z1[i]
3080: 3d 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 = sqlite3Tolower
3090: 28 7a 32 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d (z2[i]);. }
30a0: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
30b0: 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
30c0: 78 74 2c 20 7a 31 2c 20 6e 2c 20 73 71 6c 69 74 xt, z1, n, sqlit
30d0: 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a e3_free);. }.
30e0: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d }.}../*.** Som
30f0: 65 20 66 75 6e 63 74 69 6f 6e 73 20 6c 69 6b 65 e functions like
3100: 20 43 4f 41 4c 45 53 43 45 28 29 20 61 6e 64 20 COALESCE() and
3110: 49 46 4e 55 4c 4c 28 29 20 61 6e 64 20 55 4e 4c IFNULL() and UNL
3120: 49 4b 45 4c 59 28 29 20 61 72 65 20 69 6d 70 6c IKELY() are impl
3130: 65 6d 65 6e 74 65 64 0a 2a 2a 20 61 73 20 56 44 emented.** as VD
3140: 42 45 20 63 6f 64 65 20 73 6f 20 74 68 61 74 20 BE code so that
3150: 75 6e 75 73 65 64 20 61 72 67 75 6d 65 6e 74 20 unused argument
3160: 76 61 6c 75 65 73 20 64 6f 20 6e 6f 74 20 68 61 values do not ha
3170: 76 65 20 74 6f 20 62 65 20 63 6f 6d 70 75 74 65 ve to be compute
3180: 64 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 77 d..** However, w
3190: 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 73 6f 6d e still need som
31a0: 65 20 6b 69 6e 64 20 6f 66 20 66 75 6e 63 74 69 e kind of functi
31b0: 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f on implementatio
31c0: 6e 20 66 6f 72 20 74 68 69 73 0a 2a 2a 20 72 6f n for this.** ro
31d0: 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20 66 75 utines in the fu
31e0: 6e 63 74 69 6f 6e 20 74 61 62 6c 65 2e 20 20 54 nction table. T
31f0: 68 65 20 6e 6f 6f 70 46 75 6e 63 20 6d 61 63 72 he noopFunc macr
3200: 6f 20 70 72 6f 76 69 64 65 73 20 74 68 69 73 2e o provides this.
3210: 0a 2a 2a 20 6e 6f 6f 70 46 75 6e 63 20 77 69 6c .** noopFunc wil
3220: 6c 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c 65 l never be calle
3230: 64 20 73 6f 20 69 74 20 64 6f 65 73 6e 27 74 20 d so it doesn't
3240: 6d 61 74 74 65 72 20 77 68 61 74 20 74 68 65 20 matter what the
3250: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
3260: 2a 20 69 73 2e 20 20 57 65 20 6d 69 67 68 74 20 * is. We might
3270: 61 73 20 77 65 6c 6c 20 75 73 65 20 74 68 65 20 as well use the
3280: 22 76 65 72 73 69 6f 6e 28 29 22 20 66 75 6e 63 "version()" func
3290: 74 69 6f 6e 20 61 73 20 61 20 73 75 62 73 74 69 tion as a substi
32a0: 74 75 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 tute..*/.#define
32b0: 20 6e 6f 6f 70 46 75 6e 63 20 76 65 72 73 69 6f noopFunc versio
32c0: 6e 46 75 6e 63 20 20 20 2f 2a 20 53 75 62 73 74 nFunc /* Subst
32d0: 69 74 75 74 65 20 66 75 6e 63 74 69 6f 6e 20 2d itute function -
32e0: 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 2a 2f never called */
32f0: 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
3300: 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d tation of random
3310: 28 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61 (). Return a ra
3320: 6e 64 6f 6d 20 69 6e 74 65 67 65 72 2e 20 20 0a ndom integer. .
3330: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 */.static void r
3340: 61 6e 64 6f 6d 46 75 6e 63 28 0a 20 20 73 71 6c andomFunc(. sql
3350: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
3360: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 ntext,. int Not
3370: 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f Used,. sqlite3_
3380: 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 value **NotUsed2
3390: 0a 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 .){. sqlite_int
33a0: 36 34 20 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 64 r;. UNUSED_P
33b0: 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 ARAMETER2(NotUse
33c0: 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 d, NotUsed2);.
33d0: 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
33e0: 73 73 28 73 69 7a 65 6f 66 28 72 29 2c 20 26 72 ss(sizeof(r), &r
33f0: 29 3b 0a 20 20 69 66 28 20 72 3c 30 20 29 7b 0a );. if( r<0 ){.
3400: 20 20 20 20 2f 2a 20 57 65 20 6e 65 65 64 20 74 /* We need t
3410: 6f 20 70 72 65 76 65 6e 74 20 61 20 72 61 6e 64 o prevent a rand
3420: 6f 6d 20 6e 75 6d 62 65 72 20 6f 66 20 30 78 38 om number of 0x8
3430: 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 20 000000000000000
3440: 0a 20 20 20 20 2a 2a 20 28 6f 72 20 2d 39 32 32 . ** (or -922
3450: 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38 3372036854775808
3460: 29 20 73 69 6e 63 65 20 77 68 65 6e 20 79 6f 75 ) since when you
3470: 20 64 6f 20 61 62 73 28 29 20 6f 66 20 74 68 61 do abs() of tha
3480: 74 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 t. ** number
3490: 6f 66 20 79 6f 75 20 67 65 74 20 74 68 65 20 73 of you get the s
34a0: 61 6d 65 20 76 61 6c 75 65 20 62 61 63 6b 20 61 ame value back a
34b0: 67 61 69 6e 2e 20 20 54 6f 20 64 6f 20 74 68 69 gain. To do thi
34c0: 73 0a 20 20 20 20 2a 2a 20 69 6e 20 61 20 77 61 s. ** in a wa
34d0: 79 20 74 68 61 74 20 69 73 20 74 65 73 74 61 62 y that is testab
34e0: 6c 65 2c 20 6d 61 73 6b 20 74 68 65 20 73 69 67 le, mask the sig
34f0: 6e 20 62 69 74 20 6f 66 66 20 6f 66 20 6e 65 67 n bit off of neg
3500: 61 74 69 76 65 0a 20 20 20 20 2a 2a 20 76 61 6c ative. ** val
3510: 75 65 73 2c 20 72 65 73 75 6c 74 69 6e 67 20 69 ues, resulting i
3520: 6e 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c n a positive val
3530: 75 65 2e 20 20 54 68 65 6e 20 74 61 6b 65 20 74 ue. Then take t
3540: 68 65 20 0a 20 20 20 20 2a 2a 20 32 73 20 63 6f he . ** 2s co
3550: 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 61 74 mplement of that
3560: 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 2e positive value.
3570: 20 20 54 68 65 20 65 6e 64 20 72 65 73 75 6c 74 The end result
3580: 20 63 61 6e 0a 20 20 20 20 2a 2a 20 74 68 65 72 can. ** ther
3590: 65 66 6f 72 65 20 62 65 20 6e 6f 20 6c 65 73 73 efore be no less
35a0: 20 74 68 61 6e 20 2d 39 32 32 33 33 37 32 30 33 than -922337203
35b0: 36 38 35 34 37 37 35 38 30 37 2e 0a 20 20 20 20 6854775807..
35c0: 2a 2f 0a 20 20 20 20 72 20 3d 20 2d 28 72 20 26 */. r = -(r &
35d0: 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 3b LARGEST_INT64);
35e0: 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 . }. sqlite3_r
35f0: 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 esult_int64(cont
3600: 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ext, r);.}../*.*
3610: 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
3620: 20 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 4e of randomblob(N
3630: 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61 6e ). Return a ran
3640: 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68 61 74 dom blob.** that
3650: 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 is N bytes long
3660: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
3670: 20 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20 20 73 randomBlob(. s
3680: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
3690: 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
36a0: 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
36b0: 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
36c0: 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e int n;. unsign
36d0: 65 64 20 63 68 61 72 20 2a 70 3b 0a 20 20 61 73 ed char *p;. as
36e0: 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b sert( argc==1 );
36f0: 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
3700: 54 45 52 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d TER(argc);. n =
3710: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 sqlite3_value_i
3720: 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 nt(argv[0]);. i
3730: 66 28 20 6e 3c 31 20 29 7b 0a 20 20 20 20 6e 20 f( n<1 ){. n
3740: 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 63 = 1;. }. p = c
3750: 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e ontextMalloc(con
3760: 74 65 78 74 2c 20 6e 29 3b 0a 20 20 69 66 28 20 text, n);. if(
3770: 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 p ){. sqlite3
3780: 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 6e 2c 20 70 _randomness(n, p
3790: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 );. sqlite3_r
37a0: 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 esult_blob(conte
37b0: 78 74 2c 20 28 63 68 61 72 2a 29 70 2c 20 6e 2c xt, (char*)p, n,
37c0: 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a sqlite3_free);.
37d0: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 }.}../*.** Imp
37e0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
37f0: 68 65 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 he last_insert_r
3800: 6f 77 69 64 28 29 20 53 51 4c 20 66 75 6e 63 74 owid() SQL funct
3810: 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e ion. The return
3820: 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 74 68 65 .** value is the
3830: 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c same as the sql
3840: 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 ite3_last_insert
3850: 5f 72 6f 77 69 64 28 29 20 41 50 49 20 66 75 6e _rowid() API fun
3860: 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
3870: 20 76 6f 69 64 20 6c 61 73 74 5f 69 6e 73 65 72 void last_inser
3880: 74 5f 72 6f 77 69 64 28 0a 20 20 73 71 6c 69 74 t_rowid(. sqlit
3890: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
38a0: 65 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55 ext, . int NotU
38b0: 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f sed, . sqlite3_
38c0: 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 value **NotUsed2
38d0: 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 .){. sqlite3 *d
38e0: 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 b = sqlite3_cont
38f0: 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f ext_db_handle(co
3900: 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 ntext);. UNUSED
3910: 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
3920: 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
3930: 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 35 31 35 31 /* IMP: R-5151
3940: 33 2d 31 32 30 32 36 20 54 68 65 20 6c 61 73 74 3-12026 The last
3950: 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 _insert_rowid()
3960: 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 SQL function is
3970: 61 0a 20 20 2a 2a 20 77 72 61 70 70 65 72 20 61 a. ** wrapper a
3980: 72 6f 75 6e 64 20 74 68 65 20 73 71 6c 69 74 65 round the sqlite
3990: 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 3_last_insert_ro
39a0: 77 69 64 28 29 20 43 2f 43 2b 2b 20 69 6e 74 65 wid() C/C++ inte
39b0: 72 66 61 63 65 0a 20 20 2a 2a 20 66 75 6e 63 74 rface. ** funct
39c0: 69 6f 6e 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ion. */. sqlite
39d0: 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 3_result_int64(c
39e0: 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f ontext, sqlite3_
39f0: 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 last_insert_rowi
3a00: 64 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a d(db));.}../*.**
3a10: 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
3a20: 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 28 29 of the changes()
3a30: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a SQL function..*
3a40: 2a 0a 2a 2a 20 49 4d 50 3a 20 52 2d 36 32 30 37 *.** IMP: R-6207
3a50: 33 2d 31 31 32 30 39 20 54 68 65 20 63 68 61 6e 3-11209 The chan
3a60: 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 ges() SQL functi
3a70: 6f 6e 20 69 73 20 61 20 77 72 61 70 70 65 72 0a on is a wrapper.
3a80: 2a 2a 20 61 72 6f 75 6e 64 20 74 68 65 20 73 71 ** around the sq
3a90: 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 lite3_changes()
3aa0: 43 2f 43 2b 2b 20 66 75 6e 63 74 69 6f 6e 20 61 C/C++ function a
3ab0: 6e 64 20 68 65 6e 63 65 20 66 6f 6c 6c 6f 77 73 nd hence follows
3ac0: 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 72 75 6c the same.** rul
3ad0: 65 73 20 66 6f 72 20 63 6f 75 6e 74 69 6e 67 20 es for counting
3ae0: 63 68 61 6e 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 changes..*/.stat
3af0: 69 63 20 76 6f 69 64 20 63 68 61 6e 67 65 73 28 ic void changes(
3b00: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
3b10: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
3b20: 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 nt NotUsed,. sq
3b30: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f lite3_value **No
3b40: 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 tUsed2.){. sqli
3b50: 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 te3 *db = sqlite
3b60: 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
3b70: 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
3b80: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
3b90: 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
3ba0: 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f ed2);. sqlite3_
3bb0: 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 result_int(conte
3bc0: 78 74 2c 20 73 71 6c 69 74 65 33 5f 63 68 61 6e xt, sqlite3_chan
3bd0: 67 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a ges(db));.}../*.
3be0: 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
3bf0: 6e 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 5f 63 n of the total_c
3c00: 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e hanges() SQL fun
3c10: 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 ction. The retu
3c20: 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 rn value is.** t
3c30: 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 he same as the s
3c40: 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 qlite3_total_cha
3c50: 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e 63 74 nges() API funct
3c60: 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ion..*/.static v
3c70: 6f 69 64 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 oid total_change
3c80: 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e s(. sqlite3_con
3c90: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
3ca0: 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 int NotUsed,.
3cb0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
3cc0: 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 NotUsed2.){. sq
3cd0: 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
3ce0: 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
3cf0: 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
3d00: 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
3d10: 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 ER2(NotUsed, Not
3d20: 55 73 65 64 32 29 3b 0a 20 20 2f 2a 20 49 4d 50 Used2);. /* IMP
3d30: 3a 20 52 2d 35 32 37 35 36 2d 34 31 39 39 33 20 : R-52756-41993
3d40: 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
3d50: 20 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e a wrapper aroun
3d60: 64 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 d the. ** sqlit
3d70: 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
3d80: 28 29 20 43 2f 43 2b 2b 20 69 6e 74 65 72 66 61 () C/C++ interfa
3d90: 63 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ce. */. sqlite3
3da0: 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 _result_int(cont
3db0: 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 74 6f 74 ext, sqlite3_tot
3dc0: 61 6c 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b al_changes(db));
3dd0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 .}../*.** A stru
3de0: 63 74 75 72 65 20 64 65 66 69 6e 69 6e 67 20 68 cture defining h
3df0: 6f 77 20 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74 ow to do GLOB-st
3e00: 79 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e yle comparisons.
3e10: 0a 2a 2f 0a 73 74 72 75 63 74 20 63 6f 6d 70 61 .*/.struct compa
3e20: 72 65 49 6e 66 6f 20 7b 0a 20 20 75 38 20 6d 61 reInfo {. u8 ma
3e30: 74 63 68 41 6c 6c 3b 20 20 20 20 20 20 20 20 20 tchAll;
3e40: 20 2f 2a 20 22 2a 22 20 6f 72 20 22 25 22 20 2a /* "*" or "%" *
3e50: 2f 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 3b /. u8 matchOne;
3e60: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 22 3f 22 /* "?"
3e70: 20 6f 72 20 22 5f 22 20 2a 2f 0a 20 20 75 38 20 or "_" */. u8
3e80: 6d 61 74 63 68 53 65 74 3b 20 20 20 20 20 20 20 matchSet;
3e90: 20 20 20 2f 2a 20 22 5b 22 20 6f 72 20 30 20 2a /* "[" or 0 *
3ea0: 2f 0a 20 20 75 38 20 6e 6f 43 61 73 65 3b 20 20 /. u8 noCase;
3eb0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 72 75 /* tru
3ec0: 65 20 74 6f 20 69 67 6e 6f 72 65 20 63 61 73 65 e to ignore case
3ed0: 20 64 69 66 66 65 72 65 6e 63 65 73 20 2a 2f 0a differences */.
3ee0: 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 4c 49 };../*.** For LI
3ef0: 4b 45 20 61 6e 64 20 47 4c 4f 42 20 6d 61 74 63 KE and GLOB matc
3f00: 68 69 6e 67 20 6f 6e 20 45 42 43 44 49 43 20 6d hing on EBCDIC m
3f10: 61 63 68 69 6e 65 73 2c 20 61 73 73 75 6d 65 20 achines, assume
3f20: 74 68 61 74 20 65 76 65 72 79 0a 2a 2a 20 63 68 that every.** ch
3f30: 61 72 61 63 74 65 72 20 69 73 20 65 78 61 63 74 aracter is exact
3f40: 6c 79 20 6f 6e 65 20 62 79 74 65 20 69 6e 20 73 ly one byte in s
3f50: 69 7a 65 2e 20 20 41 6c 73 6f 2c 20 70 72 6f 76 ize. Also, prov
3f60: 64 65 20 74 68 65 20 55 74 66 38 52 65 61 64 28 de the Utf8Read(
3f70: 29 0a 2a 2a 20 6d 61 63 72 6f 20 66 6f 72 20 66 ).** macro for f
3f80: 61 73 74 20 72 65 61 64 69 6e 67 20 6f 66 20 74 ast reading of t
3f90: 68 65 20 6e 65 78 74 20 63 68 61 72 61 63 74 65 he next characte
3fa0: 72 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 r in the common
3fb0: 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 case where.** th
3fc0: 65 20 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 e next character
3fd0: 20 69 73 20 41 53 43 49 49 2e 0a 2a 2f 0a 23 69 is ASCII..*/.#i
3fe0: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
3ff0: 5f 45 42 43 44 49 43 29 0a 23 20 64 65 66 69 6e _EBCDIC).# defin
4000: 65 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 e sqlite3Utf8Rea
4010: 64 28 41 29 20 20 20 20 20 20 20 20 28 2a 28 28 d(A) (*((
4020: 2a 41 29 2b 2b 29 29 0a 23 20 64 65 66 69 6e 65 *A)++)).# define
4030: 20 55 74 66 38 52 65 61 64 28 41 29 20 20 20 20 Utf8Read(A)
4040: 20 20 20 20 20 20 20 20 20 20 20 28 2a 28 41 2b (*(A+
4050: 2b 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 +)).#else.# defi
4060: 6e 65 20 55 74 66 38 52 65 61 64 28 41 29 20 20 ne Utf8Read(A)
4070: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 41 5b (A[
4080: 30 5d 3c 30 78 38 30 3f 2a 28 41 2b 2b 29 3a 73 0]<0x80?*(A++):s
4090: 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 26 qlite3Utf8Read(&
40a0: 41 29 29 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 A)).#endif..stat
40b0: 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 ic const struct
40c0: 63 6f 6d 70 61 72 65 49 6e 66 6f 20 67 6c 6f 62 compareInfo glob
40d0: 49 6e 66 6f 20 3d 20 7b 20 27 2a 27 2c 20 27 3f Info = { '*', '?
40e0: 27 2c 20 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a 20 ', '[', 0 };./*
40f0: 54 68 65 20 63 6f 72 72 65 63 74 20 53 51 4c 2d The correct SQL-
4100: 39 32 20 62 65 68 61 76 69 6f 72 20 69 73 20 66 92 behavior is f
4110: 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 or the LIKE oper
4120: 61 74 6f 72 20 74 6f 20 69 67 6e 6f 72 65 0a 2a ator to ignore.*
4130: 2a 20 63 61 73 65 2e 20 20 54 68 75 73 20 20 27 * case. Thus '
4140: 61 27 20 4c 49 4b 45 20 27 41 27 20 77 6f 75 6c a' LIKE 'A' woul
4150: 64 20 62 65 20 74 72 75 65 2e 20 2a 2f 0a 73 74 d be true. */.st
4160: 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 atic const struc
4170: 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 t compareInfo li
4180: 6b 65 49 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20 27 keInfoNorm = { '
4190: 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 31 20 %', '_', 0, 1
41a0: 7d 3b 0a 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f };./* If SQLITE_
41b0: 43 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c CASE_SENSITIVE_L
41c0: 49 4b 45 20 69 73 20 64 65 66 69 6e 65 64 2c 20 IKE is defined,
41d0: 74 68 65 6e 20 74 68 65 20 4c 49 4b 45 20 6f 70 then the LIKE op
41e0: 65 72 61 74 6f 72 0a 2a 2a 20 69 73 20 63 61 73 erator.** is cas
41f0: 65 20 73 65 6e 73 69 74 69 76 65 20 63 61 75 73 e sensitive caus
4200: 69 6e 67 20 27 61 27 20 4c 49 4b 45 20 27 41 27 ing 'a' LIKE 'A'
4210: 20 74 6f 20 62 65 20 66 61 6c 73 65 20 2a 2f 0a to be false */.
4220: 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 static const str
4230: 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 uct compareInfo
4240: 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 20 7b 20 likeInfoAlt = {
4250: 27 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 30 '%', '_', 0, 0
4260: 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 };../*.** Possi
4270: 62 6c 65 20 65 72 72 6f 72 20 72 65 74 75 72 6e ble error return
4280: 73 20 66 72 6f 6d 20 70 61 74 74 65 72 6e 4d 61 s from patternMa
4290: 74 63 68 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 tch().*/.#define
42a0: 20 53 51 4c 49 54 45 5f 4d 41 54 43 48 20 20 20 SQLITE_MATCH
42b0: 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 0.#def
42c0: 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4d 41 54 ine SQLITE_NOMAT
42d0: 43 48 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 CH 1.#
42e0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 4f define SQLITE_NO
42f0: 57 49 4c 44 43 41 52 44 4d 41 54 43 48 20 20 20 WILDCARDMATCH
4300: 32 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 2../*.** Compare
4310: 20 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69 6e two UTF-8 strin
4320: 67 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79 20 gs for equality
4330: 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74 20 where the first
4340: 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 20 47 string is.** a G
4350: 4c 4f 42 20 6f 72 20 4c 49 4b 45 20 65 78 70 72 LOB or LIKE expr
4360: 65 73 73 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 ession. Return
4370: 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 values:.**.**
4380: 20 53 51 4c 49 54 45 5f 4d 41 54 43 48 3a 20 20 SQLITE_MATCH:
4390: 20 20 20 20 20 20 20 20 20 20 4d 61 74 63 68 0a Match.
43a0: 2a 2a 20 20 20 20 53 51 4c 49 54 45 5f 4e 4f 4d ** SQLITE_NOM
43b0: 41 54 43 48 3a 20 20 20 20 20 20 20 20 20 20 4e ATCH: N
43c0: 6f 20 6d 61 74 63 68 0a 2a 2a 20 20 20 20 53 51 o match.** SQ
43d0: 4c 49 54 45 5f 4e 4f 57 49 4c 44 43 41 52 44 4d LITE_NOWILDCARDM
43e0: 41 54 43 48 3a 20 20 4e 6f 20 6d 61 74 63 68 20 ATCH: No match
43f0: 69 6e 20 73 70 69 74 65 20 6f 66 20 68 61 76 69 in spite of havi
4400: 6e 67 20 2a 20 6f 72 20 25 20 77 69 6c 64 63 61 ng * or % wildca
4410: 72 64 73 2e 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62 62 rds..**.** Globb
4420: 69 6e 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a ing rules:.**.**
4430: 20 20 20 20 20 20 27 2a 27 20 20 20 20 20 20 20 '*'
4440: 4d 61 74 63 68 65 73 20 61 6e 79 20 73 65 71 75 Matches any sequ
4450: 65 6e 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 ence of zero or
4460: 6d 6f 72 65 20 63 68 61 72 61 63 74 65 72 73 2e more characters.
4470: 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 3f 27 20 .**.** '?'
4480: 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 65 78 Matches ex
4490: 61 63 74 6c 79 20 6f 6e 65 20 63 68 61 72 61 63 actly one charac
44a0: 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b ter..**.** [
44b0: 2e 2e 2e 5d 20 20 20 20 20 20 4d 61 74 63 68 65 ...] Matche
44c0: 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 s one character
44d0: 66 72 6f 6d 20 74 68 65 20 65 6e 63 6c 6f 73 65 from the enclose
44e0: 64 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20 20 d list of.**
44f0: 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 char
4500: 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 acters..**.**
4510: 20 20 5b 5e 2e 2e 2e 5d 20 20 20 20 20 4d 61 74 [^...] Mat
4520: 63 68 65 73 20 6f 6e 65 20 63 68 61 72 61 63 74 ches one charact
4530: 65 72 20 6e 6f 74 20 69 6e 20 74 68 65 20 65 6e er not in the en
4540: 63 6c 6f 73 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a closed list..**.
4550: 2a 2a 20 57 69 74 68 20 74 68 65 20 5b 2e 2e 2e ** With the [...
4560: 5d 20 61 6e 64 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 ] and [^...] mat
4570: 63 68 69 6e 67 2c 20 61 20 27 5d 27 20 63 68 61 ching, a ']' cha
4580: 72 61 63 74 65 72 20 63 61 6e 20 62 65 20 69 6e racter can be in
4590: 63 6c 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 cluded.** in the
45a0: 20 6c 69 73 74 20 62 79 20 6d 61 6b 69 6e 67 20 list by making
45b0: 69 74 20 74 68 65 20 66 69 72 73 74 20 63 68 61 it the first cha
45c0: 72 61 63 74 65 72 20 61 66 74 65 72 20 27 5b 27 racter after '['
45d0: 20 6f 72 20 27 5e 27 2e 20 20 41 0a 2a 2a 20 72 or '^'. A.** r
45e0: 61 6e 67 65 20 6f 66 20 63 68 61 72 61 63 74 65 ange of characte
45f0: 72 73 20 63 61 6e 20 62 65 20 73 70 65 63 69 66 rs can be specif
4600: 69 65 64 20 75 73 69 6e 67 20 27 2d 27 2e 20 20 ied using '-'.
4610: 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20 22 5b 61 2d Example:.** "[a-
4620: 7a 5d 22 20 6d 61 74 63 68 65 73 20 61 6e 79 20 z]" matches any
4630: 73 69 6e 67 6c 65 20 6c 6f 77 65 72 2d 63 61 73 single lower-cas
4640: 65 20 6c 65 74 74 65 72 2e 20 20 54 6f 20 6d 61 e letter. To ma
4650: 74 63 68 20 61 20 27 2d 27 2c 20 6d 61 6b 65 0a tch a '-', make.
4660: 2a 2a 20 69 74 20 74 68 65 20 6c 61 73 74 20 63 ** it the last c
4670: 68 61 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 haracter in the
4680: 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 4c 69 6b 65 list..**.** Like
4690: 20 6d 61 74 63 68 69 6e 67 20 72 75 6c 65 73 3a matching rules:
46a0: 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 20 27 25 27 .** .** '%'
46b0: 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 61 Matches a
46c0: 6e 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20 7a ny sequence of z
46d0: 65 72 6f 20 6f 72 20 6d 6f 72 65 20 63 68 61 72 ero or more char
46e0: 61 63 74 65 72 73 0a 2a 2a 0a 2a 2a 2a 20 20 20 acters.**.***
46f0: 20 20 27 5f 27 20 20 20 20 20 20 20 4d 61 74 63 '_' Matc
4700: 68 65 73 20 61 6e 79 20 6f 6e 65 20 63 68 61 72 hes any one char
4710: 61 63 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 acter.**.**
4720: 20 45 63 20 20 20 20 20 20 20 20 57 68 65 72 65 Ec Where
4730: 20 45 20 69 73 20 74 68 65 20 22 65 73 63 22 20 E is the "esc"
4740: 63 68 61 72 61 63 74 65 72 20 61 6e 64 20 63 20 character and c
4750: 69 73 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 is any other.**
4760: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 c
4770: 68 61 72 61 63 74 65 72 2c 20 69 6e 63 6c 75 64 haracter, includ
4780: 69 6e 67 20 27 25 27 2c 20 27 5f 27 2c 20 61 6e ing '%', '_', an
4790: 64 20 65 73 63 2c 20 6d 61 74 63 68 20 65 78 61 d esc, match exa
47a0: 63 74 6c 79 20 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 ctly c..**.** Th
47b0: 65 20 63 6f 6d 6d 65 6e 74 73 20 77 69 74 68 69 e comments withi
47c0: 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 75 n this routine u
47d0: 73 75 61 6c 6c 79 20 61 73 73 75 6d 65 20 67 6c sually assume gl
47e0: 6f 62 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2a 0a ob matching..**.
47f0: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
4800: 69 73 20 75 73 75 61 6c 6c 79 20 71 75 69 63 6b is usually quick
4810: 2c 20 62 75 74 20 63 61 6e 20 62 65 20 4e 2a 2a , but can be N**
4820: 32 20 69 6e 20 74 68 65 20 77 6f 72 73 74 20 63 2 in the worst c
4830: 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ase..*/.static i
4840: 6e 74 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 nt patternCompar
4850: 65 28 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a e(. const u8 *z
4860: 50 61 74 74 65 72 6e 2c 20 20 20 20 20 20 20 20 Pattern,
4870: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 67 6c 6f /* The glo
4880: 62 20 70 61 74 74 65 72 6e 20 2a 2f 0a 20 20 63 b pattern */. c
4890: 6f 6e 73 74 20 75 38 20 2a 7a 53 74 72 69 6e 67 onst u8 *zString
48a0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
48b0: 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 20 74 6f /* The string to
48c0: 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 compare against
48d0: 20 74 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 63 the glob */. c
48e0: 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 onst struct comp
48f0: 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 2c 20 areInfo *pInfo,
4900: 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
4910: 62 6f 75 74 20 68 6f 77 20 74 6f 20 64 6f 20 74 bout how to do t
4920: 68 65 20 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 20 he compare */.
4930: 75 33 32 20 6d 61 74 63 68 4f 74 68 65 72 20 20 u32 matchOther
4940: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4950: 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63 /* The escape c
4960: 68 61 72 20 28 4c 49 4b 45 29 20 6f 72 20 27 5b har (LIKE) or '[
4970: 27 20 28 47 4c 4f 42 29 20 2a 2f 0a 29 7b 0a 20 ' (GLOB) */.){.
4980: 20 75 33 32 20 63 2c 20 63 32 3b 20 20 20 20 20 u32 c, c2;
4990: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49a0: 20 20 2f 2a 20 4e 65 78 74 20 70 61 74 74 65 72 /* Next patter
49b0: 6e 20 61 6e 64 20 69 6e 70 75 74 20 73 74 72 69 n and input stri
49c0: 6e 67 20 63 68 61 72 73 20 2a 2f 0a 20 20 75 33 ng chars */. u3
49d0: 32 20 6d 61 74 63 68 4f 6e 65 20 3d 20 70 49 6e 2 matchOne = pIn
49e0: 66 6f 2d 3e 6d 61 74 63 68 4f 6e 65 3b 20 20 2f fo->matchOne; /
49f0: 2a 20 22 3f 22 20 6f 72 20 22 5f 22 20 2a 2f 0a * "?" or "_" */.
4a00: 20 20 75 33 32 20 6d 61 74 63 68 41 6c 6c 20 3d u32 matchAll =
4a10: 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 41 6c 6c pInfo->matchAll
4a20: 3b 20 20 2f 2a 20 22 2a 22 20 6f 72 20 22 25 22 ; /* "*" or "%"
4a30: 20 2a 2f 0a 20 20 75 38 20 6e 6f 43 61 73 65 20 */. u8 noCase
4a40: 3d 20 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73 65 3b = pInfo->noCase;
4a50: 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
4a60: 66 20 75 70 70 65 72 63 61 73 65 3d 3d 6c 6f 77 f uppercase==low
4a70: 65 72 63 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 ercase */. cons
4a80: 74 20 75 38 20 2a 7a 45 73 63 61 70 65 64 20 3d t u8 *zEscaped =
4a90: 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 0; /*
4aa0: 4f 6e 65 20 70 61 73 74 20 74 68 65 20 6c 61 73 One past the las
4ab0: 74 20 65 73 63 61 70 65 64 20 69 6e 70 75 74 20 t escaped input
4ac0: 63 68 61 72 20 2a 2f 0a 20 20 0a 20 20 77 68 69 char */. . whi
4ad0: 6c 65 28 20 28 63 20 3d 20 55 74 66 38 52 65 61 le( (c = Utf8Rea
4ae0: 64 28 7a 50 61 74 74 65 72 6e 29 29 21 3d 30 20 d(zPattern))!=0
4af0: 29 7b 0a 20 20 20 20 69 66 28 20 63 3d 3d 6d 61 ){. if( c==ma
4b00: 74 63 68 41 6c 6c 20 29 7b 20 20 2f 2a 20 4d 61 tchAll ){ /* Ma
4b10: 74 63 68 20 22 2a 22 20 2a 2f 0a 20 20 20 20 20 tch "*" */.
4b20: 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 6d 75 /* Skip over mu
4b30: 6c 74 69 70 6c 65 20 22 2a 22 20 63 68 61 72 61 ltiple "*" chara
4b40: 63 74 65 72 73 20 69 6e 20 74 68 65 20 70 61 74 cters in the pat
4b50: 74 65 72 6e 2e 20 20 49 66 20 74 68 65 72 65 0a tern. If there.
4b60: 20 20 20 20 20 20 2a 2a 20 61 72 65 20 61 6c 73 ** are als
4b70: 6f 20 22 3f 22 20 63 68 61 72 61 63 74 65 72 73 o "?" characters
4b80: 2c 20 73 6b 69 70 20 74 68 6f 73 65 20 61 73 20 , skip those as
4b90: 77 65 6c 6c 2c 20 62 75 74 20 63 6f 6e 73 75 6d well, but consum
4ba0: 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 73 69 6e e a. ** sin
4bb0: 67 6c 65 20 63 68 61 72 61 63 74 65 72 20 6f 66 gle character of
4bc0: 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e the input strin
4bd0: 67 20 66 6f 72 20 65 61 63 68 20 22 3f 22 20 73 g for each "?" s
4be0: 6b 69 70 70 65 64 20 2a 2f 0a 20 20 20 20 20 20 kipped */.
4bf0: 77 68 69 6c 65 28 20 28 63 3d 55 74 66 38 52 65 while( (c=Utf8Re
4c00: 61 64 28 7a 50 61 74 74 65 72 6e 29 29 20 3d 3d ad(zPattern)) ==
4c10: 20 6d 61 74 63 68 41 6c 6c 20 7c 7c 20 63 20 3d matchAll || c =
4c20: 3d 20 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 = matchOne ){.
4c30: 20 20 20 20 20 20 69 66 28 20 63 3d 3d 6d 61 74 if( c==mat
4c40: 63 68 4f 6e 65 20 26 26 20 73 71 6c 69 74 65 33 chOne && sqlite3
4c50: 55 74 66 38 52 65 61 64 28 26 7a 53 74 72 69 6e Utf8Read(&zStrin
4c60: 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 g)==0 ){.
4c70: 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
4c80: 5f 4e 4f 57 49 4c 44 43 41 52 44 4d 41 54 43 48 _NOWILDCARDMATCH
4c90: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
4ca0: 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d }. if( c=
4cb0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
4cc0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 41 54 43 turn SQLITE_MATC
4cd0: 48 3b 20 20 20 2f 2a 20 22 2a 22 20 61 74 20 74 H; /* "*" at t
4ce0: 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 he end of the pa
4cf0: 74 74 65 72 6e 20 6d 61 74 63 68 65 73 20 2a 2f ttern matches */
4d00: 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
4d10: 20 63 3d 3d 6d 61 74 63 68 4f 74 68 65 72 20 29 c==matchOther )
4d20: 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 {. if( pI
4d30: 6e 66 6f 2d 3e 6d 61 74 63 68 53 65 74 3d 3d 30 nfo->matchSet==0
4d40: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 ){. c
4d50: 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 = sqlite3Utf8Rea
4d60: 64 28 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 d(&zPattern);.
4d70: 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 if( c==0
4d80: 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
4d90: 5f 4e 4f 57 49 4c 44 43 41 52 44 4d 41 54 43 48 _NOWILDCARDMATCH
4da0: 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
4db0: 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 22 5b . /* "[
4dc0: 2e 2e 2e 5d 22 20 69 6d 6d 65 64 69 61 74 65 6c ...]" immediatel
4dd0: 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 22 2a y follows the "*
4de0: 22 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 64 ". We have to d
4df0: 6f 20 61 20 73 6c 6f 77 0a 20 20 20 20 20 20 20 o a slow.
4e00: 20 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65 20 ** recursive
4e10: 73 65 61 72 63 68 20 69 6e 20 74 68 69 73 20 63 search in this c
4e20: 61 73 65 2c 20 62 75 74 20 69 74 20 69 73 20 61 ase, but it is a
4e30: 6e 20 75 6e 75 73 75 61 6c 20 63 61 73 65 2e 20 n unusual case.
4e40: 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 */. ass
4e50: 65 72 74 28 20 6d 61 74 63 68 4f 74 68 65 72 3c ert( matchOther<
4e60: 30 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20 0x80 ); /* '['
4e70: 69 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 is a single-byte
4e80: 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 character */.
4e90: 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a while( *
4ea0: 7a 53 74 72 69 6e 67 20 29 7b 0a 20 20 20 20 20 zString ){.
4eb0: 20 20 20 20 20 20 20 69 6e 74 20 62 4d 61 74 63 int bMatc
4ec0: 68 20 3d 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 h = patternCompa
4ed0: 72 65 28 26 7a 50 61 74 74 65 72 6e 5b 2d 31 5d re(&zPattern[-1]
4ee0: 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 6d ,zString,pInfo,m
4ef0: 61 74 63 68 4f 74 68 65 72 29 3b 0a 20 20 20 20 atchOther);.
4f00: 20 20 20 20 20 20 20 20 69 66 28 20 62 4d 61 74 if( bMat
4f10: 63 68 21 3d 53 51 4c 49 54 45 5f 4e 4f 4d 41 54 ch!=SQLITE_NOMAT
4f20: 43 48 20 29 20 72 65 74 75 72 6e 20 62 4d 61 74 CH ) return bMat
4f30: 63 68 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ch;.
4f40: 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 SQLITE_SKIP_UTF8
4f50: 28 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 (zString);.
4f60: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
4f70: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
4f80: 4f 57 49 4c 44 43 41 52 44 4d 41 54 43 48 3b 0a OWILDCARDMATCH;.
4f90: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
4fa0: 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41 74 20 74 }.. /* At t
4fb0: 68 69 73 20 70 6f 69 6e 74 20 76 61 72 69 61 62 his point variab
4fc0: 6c 65 20 63 20 63 6f 6e 74 61 69 6e 73 20 74 68 le c contains th
4fd0: 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 e first characte
4fe0: 72 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 2a r of the. *
4ff0: 2a 20 70 61 74 74 65 72 6e 20 73 74 72 69 6e 67 * pattern string
5000: 20 70 61 73 74 20 74 68 65 20 22 2a 22 2e 20 20 past the "*".
5010: 53 65 61 72 63 68 20 69 6e 20 74 68 65 20 69 6e Search in the in
5020: 70 75 74 20 73 74 72 69 6e 67 20 66 6f 72 20 74 put string for t
5030: 68 65 0a 20 20 20 20 20 20 2a 2a 20 66 69 72 73 he. ** firs
5040: 74 20 6d 61 74 63 68 69 6e 67 20 63 68 61 72 61 t matching chara
5050: 63 74 65 72 20 61 6e 64 20 72 65 63 75 72 73 69 cter and recursi
5060: 76 65 6c 79 20 63 6f 6e 74 69 6e 75 65 20 74 68 vely continue th
5070: 65 20 6d 61 74 63 68 20 66 72 6f 6d 0a 20 20 20 e match from.
5080: 20 20 20 2a 2a 20 74 68 61 74 20 70 6f 69 6e 74 ** that point
5090: 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
50a0: 20 2a 2a 20 46 6f 72 20 61 20 63 61 73 65 2d 69 ** For a case-i
50b0: 6e 73 65 6e 73 69 74 69 76 65 20 73 65 61 72 63 nsensitive searc
50c0: 68 2c 20 73 65 74 20 76 61 72 69 61 62 6c 65 20 h, set variable
50d0: 63 78 20 74 6f 20 62 65 20 74 68 65 20 73 61 6d cx to be the sam
50e0: 65 20 61 73 0a 20 20 20 20 20 20 2a 2a 20 63 20 e as. ** c
50f0: 62 75 74 20 69 6e 20 74 68 65 20 6f 74 68 65 72 but in the other
5100: 20 63 61 73 65 20 61 6e 64 20 73 65 61 72 63 68 case and search
5110: 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e the input strin
5120: 67 20 66 6f 72 20 65 69 74 68 65 72 0a 20 20 20 g for either.
5130: 20 20 20 2a 2a 20 63 20 6f 72 20 63 78 2e 0a 20 ** c or cx..
5140: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
5150: 28 20 63 3c 3d 30 78 38 30 20 29 7b 0a 20 20 20 ( c<=0x80 ){.
5160: 20 20 20 20 20 75 33 32 20 63 78 3b 0a 20 20 20 u32 cx;.
5170: 20 20 20 20 20 69 6e 74 20 62 4d 61 74 63 68 3b int bMatch;
5180: 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43 . if( noC
5190: 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ase ){.
51a0: 20 63 78 20 3d 20 73 71 6c 69 74 65 33 54 6f 75 cx = sqlite3Tou
51b0: 70 70 65 72 28 63 29 3b 0a 20 20 20 20 20 20 20 pper(c);.
51c0: 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 54 6f c = sqlite3To
51d0: 6c 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20 20 lower(c);.
51e0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
51f0: 20 20 20 63 78 20 3d 20 63 3b 0a 20 20 20 20 20 cx = c;.
5200: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69 }. whi
5210: 6c 65 28 20 28 63 32 20 3d 20 2a 28 7a 53 74 72 le( (c2 = *(zStr
5220: 69 6e 67 2b 2b 29 29 21 3d 30 20 29 7b 0a 20 20 ing++))!=0 ){.
5230: 20 20 20 20 20 20 20 20 69 66 28 20 63 32 21 3d if( c2!=
5240: 63 20 26 26 20 63 32 21 3d 63 78 20 29 20 63 6f c && c2!=cx ) co
5250: 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 ntinue;.
5260: 20 20 62 4d 61 74 63 68 20 3d 20 70 61 74 74 65 bMatch = patte
5270: 72 6e 43 6f 6d 70 61 72 65 28 7a 50 61 74 74 65 rnCompare(zPatte
5280: 72 6e 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f rn,zString,pInfo
5290: 2c 6d 61 74 63 68 4f 74 68 65 72 29 3b 0a 20 20 ,matchOther);.
52a0: 20 20 20 20 20 20 20 20 69 66 28 20 62 4d 61 74 if( bMat
52b0: 63 68 21 3d 53 51 4c 49 54 45 5f 4e 4f 4d 41 54 ch!=SQLITE_NOMAT
52c0: 43 48 20 29 20 72 65 74 75 72 6e 20 62 4d 61 74 CH ) return bMat
52d0: 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ch;. }.
52e0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
52f0: 20 20 20 69 6e 74 20 62 4d 61 74 63 68 3b 0a 20 int bMatch;.
5300: 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 while( (c
5310: 32 20 3d 20 55 74 66 38 52 65 61 64 28 7a 53 74 2 = Utf8Read(zSt
5320: 72 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 ring))!=0 ){.
5330: 20 20 20 20 20 20 20 69 66 28 20 63 32 21 3d 63 if( c2!=c
5340: 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
5350: 20 20 20 20 20 20 20 62 4d 61 74 63 68 20 3d 20 bMatch =
5360: 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a patternCompare(z
5370: 50 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e 67 2c Pattern,zString,
5380: 70 49 6e 66 6f 2c 6d 61 74 63 68 4f 74 68 65 72 pInfo,matchOther
5390: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
53a0: 20 62 4d 61 74 63 68 21 3d 53 51 4c 49 54 45 5f bMatch!=SQLITE_
53b0: 4e 4f 4d 41 54 43 48 20 29 20 72 65 74 75 72 6e NOMATCH ) return
53c0: 20 62 4d 61 74 63 68 3b 0a 20 20 20 20 20 20 20 bMatch;.
53d0: 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
53e0: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
53f0: 4f 57 49 4c 44 43 41 52 44 4d 41 54 43 48 3b 0a OWILDCARDMATCH;.
5400: 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 3d }. if( c=
5410: 3d 6d 61 74 63 68 4f 74 68 65 72 20 29 7b 0a 20 =matchOther ){.
5420: 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e if( pInfo->
5430: 6d 61 74 63 68 53 65 74 3d 3d 30 20 29 7b 0a 20 matchSet==0 ){.
5440: 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 c = sqlit
5450: 65 33 55 74 66 38 52 65 61 64 28 26 7a 50 61 74 e3Utf8Read(&zPat
5460: 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 tern);. i
5470: 66 28 20 63 3d 3d 30 20 29 20 72 65 74 75 72 6e f( c==0 ) return
5480: 20 53 51 4c 49 54 45 5f 4e 4f 4d 41 54 43 48 3b SQLITE_NOMATCH;
5490: 0a 20 20 20 20 20 20 20 20 7a 45 73 63 61 70 65 . zEscape
54a0: 64 20 3d 20 7a 50 61 74 74 65 72 6e 3b 0a 20 20 d = zPattern;.
54b0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
54c0: 20 20 20 75 33 32 20 70 72 69 6f 72 5f 63 20 3d u32 prior_c =
54d0: 20 30 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 0;. int
54e0: 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 seen = 0;.
54f0: 20 20 69 6e 74 20 69 6e 76 65 72 74 20 3d 20 30 int invert = 0
5500: 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 ;. c = sq
5510: 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 26 7a lite3Utf8Read(&z
5520: 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 String);.
5530: 20 69 66 28 20 63 3d 3d 30 20 29 20 72 65 74 75 if( c==0 ) retu
5540: 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 41 54 43 rn SQLITE_NOMATC
5550: 48 3b 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20 H;. c2 =
5560: 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
5570: 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 &zPattern);.
5580: 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5e 27 20 if( c2=='^'
5590: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 76 ){. inv
55a0: 65 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ert = 1;.
55b0: 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 c2 = sqlite3U
55c0: 74 66 38 52 65 61 64 28 26 7a 50 61 74 74 65 72 tf8Read(&zPatter
55d0: 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 n);. }.
55e0: 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5d if( c2==']
55f0: 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ' ){. i
5600: 66 28 20 63 3d 3d 27 5d 27 20 29 20 73 65 65 6e f( c==']' ) seen
5610: 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 = 1;.
5620: 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 c2 = sqlite3Utf8
5630: 52 65 61 64 28 26 7a 50 61 74 74 65 72 6e 29 3b Read(&zPattern);
5640: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
5650: 20 20 20 77 68 69 6c 65 28 20 63 32 20 26 26 20 while( c2 &&
5660: 63 32 21 3d 27 5d 27 20 29 7b 0a 20 20 20 20 20 c2!=']' ){.
5670: 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27 2d 27 if( c2=='-'
5680: 20 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 && zPattern[0]!
5690: 3d 27 5d 27 20 26 26 20 7a 50 61 74 74 65 72 6e =']' && zPattern
56a0: 5b 30 5d 21 3d 30 20 26 26 20 70 72 69 6f 72 5f [0]!=0 && prior_
56b0: 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 c>0 ){.
56c0: 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 c2 = sqlite3U
56d0: 74 66 38 52 65 61 64 28 26 7a 50 61 74 74 65 72 tf8Read(&zPatter
56e0: 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 n);.
56f0: 69 66 28 20 63 3e 3d 70 72 69 6f 72 5f 63 20 26 if( c>=prior_c &
5700: 26 20 63 3c 3d 63 32 20 29 20 73 65 65 6e 20 3d & c<=c2 ) seen =
5710: 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1;.
5720: 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20 prior_c = 0;.
5730: 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
5740: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3d if( c=
5750: 3d 63 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 =c2 ){.
5760: 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b 0a 20 seen = 1;.
5770: 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
5780: 20 20 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 prior_c
5790: 20 3d 20 63 32 3b 0a 20 20 20 20 20 20 20 20 20 = c2;.
57a0: 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 32 20 }. c2
57b0: 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 = sqlite3Utf8Rea
57c0: 64 28 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 d(&zPattern);.
57d0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
57e0: 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20 28 73 65 if( c2==0 || (se
57f0: 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d 3d 30 20 en ^ invert)==0
5800: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 ){. ret
5810: 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 41 54 urn SQLITE_NOMAT
5820: 43 48 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 CH;. }.
5830: 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a continue;.
5840: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
5850: 20 20 63 32 20 3d 20 55 74 66 38 52 65 61 64 28 c2 = Utf8Read(
5860: 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 69 66 zString);. if
5870: 28 20 63 3d 3d 63 32 20 29 20 63 6f 6e 74 69 6e ( c==c2 ) contin
5880: 75 65 3b 0a 20 20 20 20 69 66 28 20 6e 6f 43 61 ue;. if( noCa
5890: 73 65 20 20 26 26 20 73 71 6c 69 74 65 33 54 6f se && sqlite3To
58a0: 6c 6f 77 65 72 28 63 29 3d 3d 73 71 6c 69 74 65 lower(c)==sqlite
58b0: 33 54 6f 6c 6f 77 65 72 28 63 32 29 20 26 26 20 3Tolower(c2) &&
58c0: 63 3c 30 78 38 30 20 26 26 20 63 32 3c 30 78 38 c<0x80 && c2<0x8
58d0: 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69 0 ){. conti
58e0: 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 nue;. }. i
58f0: 66 28 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 26 f( c==matchOne &
5900: 26 20 7a 50 61 74 74 65 72 6e 21 3d 7a 45 73 63 & zPattern!=zEsc
5910: 61 70 65 64 20 26 26 20 63 32 21 3d 30 20 29 20 aped && c2!=0 )
5920: 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 65 continue;. re
5930: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 41 turn SQLITE_NOMA
5940: 54 43 48 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 TCH;. }. retur
5950: 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d 30 20 3f 20 n *zString==0 ?
5960: 53 51 4c 49 54 45 5f 4d 41 54 43 48 20 3a 20 53 SQLITE_MATCH : S
5970: 51 4c 49 54 45 5f 4e 4f 4d 41 54 43 48 3b 0a 7d QLITE_NOMATCH;.}
5980: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
5990: 74 65 33 5f 73 74 72 67 6c 6f 62 28 29 20 69 6e te3_strglob() in
59a0: 74 65 72 66 61 63 65 2e 20 20 52 65 74 75 72 6e terface. Return
59b0: 20 30 20 6f 6e 20 61 20 6d 61 74 63 68 20 28 6c 0 on a match (l
59c0: 69 6b 65 20 73 74 72 63 6d 70 28 29 29 20 61 6e ike strcmp()) an
59d0: 64 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 d.** non-zero if
59e0: 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 there is no mat
59f0: 63 68 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 ch..*/.int sqlit
5a00: 65 33 5f 73 74 72 67 6c 6f 62 28 63 6f 6e 73 74 e3_strglob(const
5a10: 20 63 68 61 72 20 2a 7a 47 6c 6f 62 50 61 74 74 char *zGlobPatt
5a20: 65 72 6e 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 ern, const char
5a30: 2a 7a 53 74 72 69 6e 67 29 7b 0a 20 20 72 65 74 *zString){. ret
5a40: 75 72 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 urn patternCompa
5a50: 72 65 28 28 75 38 2a 29 7a 47 6c 6f 62 50 61 74 re((u8*)zGlobPat
5a60: 74 65 72 6e 2c 20 28 75 38 2a 29 7a 53 74 72 69 tern, (u8*)zStri
5a70: 6e 67 2c 20 26 67 6c 6f 62 49 6e 66 6f 2c 20 27 ng, &globInfo, '
5a80: 5b 27 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 [');.}../*.** Th
5a90: 65 20 73 71 6c 69 74 65 33 5f 73 74 72 6c 69 6b e sqlite3_strlik
5aa0: 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 20 e() interface.
5ab0: 52 65 74 75 72 6e 20 30 20 6f 6e 20 61 20 6d 61 Return 0 on a ma
5ac0: 74 63 68 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f tch and non-zero
5ad0: 20 66 6f 72 0a 2a 2a 20 61 20 6d 69 73 73 20 2d for.** a miss -
5ae0: 20 6c 69 6b 65 20 73 74 72 63 6d 70 28 29 2e 0a like strcmp()..
5af0: 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 */.int sqlite3_s
5b00: 74 72 6c 69 6b 65 28 63 6f 6e 73 74 20 63 68 61 trlike(const cha
5b10: 72 20 2a 7a 50 61 74 74 65 72 6e 2c 20 63 6f 6e r *zPattern, con
5b20: 73 74 20 63 68 61 72 20 2a 7a 53 74 72 2c 20 75 st char *zStr, u
5b30: 6e 73 69 67 6e 65 64 20 69 6e 74 20 65 73 63 29 nsigned int esc)
5b40: 7b 0a 20 20 72 65 74 75 72 6e 20 70 61 74 74 65 {. return patte
5b50: 72 6e 43 6f 6d 70 61 72 65 28 28 75 38 2a 29 7a rnCompare((u8*)z
5b60: 50 61 74 74 65 72 6e 2c 20 28 75 38 2a 29 7a 53 Pattern, (u8*)zS
5b70: 74 72 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 tr, &likeInfoNor
5b80: 6d 2c 20 65 73 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a m, esc);.}../*.*
5b90: 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 * Count the numb
5ba0: 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 er of times that
5bb0: 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 the LIKE operat
5bc0: 6f 72 20 28 6f 72 20 47 4c 4f 42 20 77 68 69 63 or (or GLOB whic
5bd0: 68 20 69 73 0a 2a 2a 20 6a 75 73 74 20 61 20 76 h is.** just a v
5be0: 61 72 69 61 74 69 6f 6e 20 6f 66 20 4c 49 4b 45 ariation of LIKE
5bf0: 29 20 67 65 74 73 20 63 61 6c 6c 65 64 2e 20 20 ) gets called.
5c00: 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 This is used for
5c10: 20 74 65 73 74 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 testing.** only
5c20: 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
5c30: 54 45 5f 54 45 53 54 0a 69 6e 74 20 73 71 6c 69 TE_TEST.int sqli
5c40: 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 20 3d te3_like_count =
5c50: 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 0;.#endif.../*.
5c60: 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
5c70: 6e 20 6f 66 20 74 68 65 20 6c 69 6b 65 28 29 20 n of the like()
5c80: 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 SQL function. T
5c90: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 his function imp
5ca0: 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 65 20 62 lements.** the b
5cb0: 75 69 6c 64 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 uild-in LIKE ope
5cc0: 72 61 74 6f 72 2e 20 20 54 68 65 20 66 69 72 73 rator. The firs
5cd0: 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 t argument to th
5ce0: 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 e function is th
5cf0: 65 0a 2a 2a 20 70 61 74 74 65 72 6e 20 61 6e 64 e.** pattern and
5d00: 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
5d10: 6d 65 6e 74 20 69 73 20 74 68 65 20 73 74 72 69 ment is the stri
5d20: 6e 67 2e 20 20 53 6f 2c 20 74 68 65 20 53 51 4c ng. So, the SQL
5d30: 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a statements:.**.
5d40: 2a 2a 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20 ** A LIKE
5d50: 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 B.**.** is imple
5d60: 6d 65 6e 74 65 64 20 61 73 20 6c 69 6b 65 28 42 mented as like(B
5d70: 2c 41 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ,A)..**.** This
5d80: 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 28 77 same function (w
5d90: 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 ith a different
5da0: 63 6f 6d 70 61 72 65 49 6e 66 6f 20 73 74 72 75 compareInfo stru
5db0: 63 74 75 72 65 29 20 63 6f 6d 70 75 74 65 73 0a cture) computes.
5dc0: 2a 2a 20 74 68 65 20 47 4c 4f 42 20 6f 70 65 72 ** the GLOB oper
5dd0: 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ator..*/.static
5de0: 76 6f 69 64 20 6c 69 6b 65 46 75 6e 63 28 0a 20 void likeFunc(.
5df0: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
5e00: 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e *context, . in
5e10: 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 t argc, . sqlit
5e20: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
5e30: 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 ){. const unsig
5e40: 6e 65 64 20 63 68 61 72 20 2a 7a 41 2c 20 2a 7a ned char *zA, *z
5e50: 42 3b 0a 20 20 75 33 32 20 65 73 63 61 70 65 3b B;. u32 escape;
5e60: 0a 20 20 69 6e 74 20 6e 50 61 74 3b 0a 20 20 73 . int nPat;. s
5e70: 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c qlite3 *db = sql
5e80: 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f ite3_context_db_
5e90: 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b handle(context);
5ea0: 0a 20 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 . struct compar
5eb0: 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 73 eInfo *pInfo = s
5ec0: 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 qlite3_user_data
5ed0: 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 23 69 66 64 (context);..#ifd
5ee0: 65 66 20 53 51 4c 49 54 45 5f 4c 49 4b 45 5f 44 ef SQLITE_LIKE_D
5ef0: 4f 45 53 4e 54 5f 4d 41 54 43 48 5f 42 4c 4f 42 OESNT_MATCH_BLOB
5f00: 53 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f S. if( sqlite3_
5f10: 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b value_type(argv[
5f20: 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 0])==SQLITE_BLOB
5f30: 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 76 . || sqlite3_v
5f40: 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 alue_type(argv[1
5f50: 5d 29 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 0a ])==SQLITE_BLOB.
5f60: 20 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 ){.#ifdef SQLI
5f70: 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 TE_TEST. sqli
5f80: 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b te3_like_count++
5f90: 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c ;.#endif. sql
5fa0: 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 ite3_result_int(
5fb0: 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 20 context, 0);.
5fc0: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e return;. }.#en
5fd0: 64 69 66 0a 20 20 7a 42 20 3d 20 73 71 6c 69 74 dif. zB = sqlit
5fe0: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
5ff0: 67 76 5b 30 5d 29 3b 0a 20 20 7a 41 20 3d 20 73 gv[0]);. zA = s
6000: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
6010: 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 2f t(argv[1]);.. /
6020: 2a 20 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e 67 * Limit the leng
6030: 74 68 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f th of the LIKE o
6040: 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 r GLOB pattern t
6050: 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 o avoid problems
6060: 0a 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72 65 . ** of deep re
6070: 63 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 cursion and N*N
6080: 62 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74 74 behavior in patt
6090: 65 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a 20 20 ernCompare()..
60a0: 2a 2f 0a 20 20 6e 50 61 74 20 3d 20 73 71 6c 69 */. nPat = sqli
60b0: 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
60c0: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65 73 74 argv[0]);. test
60d0: 63 61 73 65 28 20 6e 50 61 74 3d 3d 64 62 2d 3e case( nPat==db->
60e0: 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
60f0: 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e MIT_LIKE_PATTERN
6100: 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74 65 _LENGTH] );. te
6110: 73 74 63 61 73 65 28 20 6e 50 61 74 3d 3d 64 62 stcase( nPat==db
6120: 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f ->aLimit[SQLITE_
6130: 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 LIMIT_LIKE_PATTE
6140: 52 4e 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a RN_LENGTH]+1 );.
6150: 20 20 69 66 28 20 6e 50 61 74 20 3e 20 64 62 2d if( nPat > db-
6160: 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
6170: 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 IMIT_LIKE_PATTER
6180: 4e 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 N_LENGTH] ){.
6190: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
61a0: 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 error(context, "
61b0: 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 LIKE or GLOB pat
61c0: 74 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 tern too complex
61d0: 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 ", -1);. retu
61e0: 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rn;. }. assert
61f0: 28 20 7a 42 3d 3d 73 71 6c 69 74 65 33 5f 76 61 ( zB==sqlite3_va
6200: 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d lue_text(argv[0]
6210: 29 20 29 3b 20 20 2f 2a 20 45 6e 63 6f 64 69 6e ) ); /* Encodin
6220: 67 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67 65 g did not change
6230: 20 2a 2f 0a 0a 20 20 69 66 28 20 61 72 67 63 3d */.. if( argc=
6240: 3d 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 =3 ){. /* The
6250: 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 escape characte
6260: 72 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 6f r string must co
6270: 6e 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 6c nsist of a singl
6280: 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 e UTF-8 characte
6290: 72 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 r.. ** Otherw
62a0: 69 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 ise, return an e
62b0: 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 rror.. */.
62c0: 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
62d0: 63 68 61 72 20 2a 7a 45 73 63 20 3d 20 73 71 6c char *zEsc = sql
62e0: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
62f0: 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 argv[2]);. if
6300: 28 20 7a 45 73 63 3d 3d 30 20 29 20 72 65 74 75 ( zEsc==0 ) retu
6310: 72 6e 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 rn;. if( sqli
6320: 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 28 te3Utf8CharLen((
6330: 63 68 61 72 2a 29 7a 45 73 63 2c 20 2d 31 29 21 char*)zEsc, -1)!
6340: 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =1 ){. sqli
6350: 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
6360: 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 (context, .
6370: 20 20 20 20 20 22 45 53 43 41 50 45 20 65 78 70 "ESCAPE exp
6380: 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 ression must be
6390: 61 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 a single charact
63a0: 65 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 er", -1);.
63b0: 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 return;. }.
63c0: 20 20 65 73 63 61 70 65 20 3d 20 73 71 6c 69 74 escape = sqlit
63d0: 65 33 55 74 66 38 52 65 61 64 28 26 7a 45 73 63 e3Utf8Read(&zEsc
63e0: 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
63f0: 65 73 63 61 70 65 20 3d 20 70 49 6e 66 6f 2d 3e escape = pInfo->
6400: 6d 61 74 63 68 53 65 74 3b 0a 20 20 7d 0a 20 20 matchSet;. }.
6410: 69 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b 0a if( zA && zB ){.
6420: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
6430: 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c ST. sqlite3_l
6440: 69 6b 65 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e ike_count++;.#en
6450: 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 5f dif. sqlite3_
6460: 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 result_int(conte
6470: 78 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 xt, patternCompa
6480: 72 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e 66 6f re(zB, zA, pInfo
6490: 2c 20 65 73 63 61 70 65 29 3d 3d 53 51 4c 49 54 , escape)==SQLIT
64a0: 45 5f 4d 41 54 43 48 29 3b 0a 20 20 7d 0a 7d 0a E_MATCH);. }.}.
64b0: 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
64c0: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4e 55 4c ation of the NUL
64d0: 4c 49 46 28 78 2c 79 29 20 66 75 6e 63 74 69 6f LIF(x,y) functio
64e0: 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 n. The result i
64f0: 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 s the first.** a
6500: 72 67 75 6d 65 6e 74 20 69 66 20 74 68 65 20 61 rgument if the a
6510: 72 67 75 6d 65 6e 74 73 20 61 72 65 20 64 69 66 rguments are dif
6520: 66 65 72 65 6e 74 2e 20 20 54 68 65 20 72 65 73 ferent. The res
6530: 75 6c 74 20 69 73 20 4e 55 4c 4c 20 69 66 20 74 ult is NULL if t
6540: 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 he.** arguments
6550: 61 72 65 20 65 71 75 61 6c 20 74 6f 20 65 61 63 are equal to eac
6560: 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 h other..*/.stat
6570: 69 63 20 76 6f 69 64 20 6e 75 6c 6c 69 66 46 75 ic void nullifFu
6580: 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
6590: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
65a0: 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 int NotUsed,.
65b0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
65c0: 2a 61 72 67 76 0a 29 7b 0a 20 20 43 6f 6c 6c 53 *argv.){. CollS
65d0: 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 eq *pColl = sqli
65e0: 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 te3GetFuncCollSe
65f0: 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e q(context);. UN
6600: 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
6610: 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20 73 otUsed);. if( s
6620: 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 qlite3MemCompare
6630: 28 61 72 67 76 5b 30 5d 2c 20 61 72 67 76 5b 31 (argv[0], argv[1
6640: 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30 20 29 7b 0a ], pColl)!=0 ){.
6650: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
6660: 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 lt_value(context
6670: 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7d 0a , argv[0]);. }.
6680: 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
6690: 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ntation of the s
66a0: 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e 28 29 20 qlite_version()
66b0: 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 function. The r
66c0: 65 73 75 6c 74 20 69 73 20 74 68 65 20 76 65 72 esult is the ver
66d0: 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 sion.** of the S
66e0: 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 74 68 QLite library th
66f0: 61 74 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a at is running..*
6700: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 65 /.static void ve
6710: 72 73 69 6f 6e 46 75 6e 63 28 0a 20 20 73 71 6c rsionFunc(. sql
6720: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
6730: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 ntext,. int Not
6740: 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f Used,. sqlite3_
6750: 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 value **NotUsed2
6760: 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 .){. UNUSED_PAR
6770: 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c AMETER2(NotUsed,
6780: 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 2f 2a NotUsed2);. /*
6790: 20 49 4d 50 3a 20 52 2d 34 38 36 39 39 2d 34 38 IMP: R-48699-48
67a0: 36 31 37 20 54 68 69 73 20 66 75 6e 63 74 69 6f 617 This functio
67b0: 6e 20 69 73 20 61 6e 20 53 51 4c 20 77 72 61 70 n is an SQL wrap
67c0: 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 0a 20 per around the.
67d0: 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 ** sqlite3_libv
67e0: 65 72 73 69 6f 6e 28 29 20 43 2d 69 6e 74 65 72 ersion() C-inter
67f0: 66 61 63 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 face. */. sqlit
6800: 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 e3_result_text(c
6810: 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f ontext, sqlite3_
6820: 6c 69 62 76 65 72 73 69 6f 6e 28 29 2c 20 2d 31 libversion(), -1
6830: 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 , SQLITE_STATIC)
6840: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
6850: 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
6860: 20 73 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 sqlite_source_i
6870: 64 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 d() function. Th
6880: 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 73 74 e result is a st
6890: 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 69 64 65 ring.** that ide
68a0: 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 72 74 ntifies the part
68b0: 69 63 75 6c 61 72 20 76 65 72 73 69 6f 6e 20 6f icular version o
68c0: 66 20 74 68 65 20 73 6f 75 72 63 65 20 63 6f 64 f the source cod
68d0: 65 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 0a e used to build.
68e0: 2a 2a 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 74 ** SQLite..*/.st
68f0: 61 74 69 63 20 76 6f 69 64 20 73 6f 75 72 63 65 atic void source
6900: 69 64 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 idFunc(. sqlite
6910: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
6920: 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
6930: 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
6940: 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b ue **NotUsed2.){
6950: 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
6960: 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
6970: 74 55 73 65 64 32 29 3b 0a 20 20 2f 2a 20 49 4d tUsed2);. /* IM
6980: 50 3a 20 52 2d 32 34 34 37 30 2d 33 31 31 33 36 P: R-24470-31136
6990: 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
69a0: 73 20 61 6e 20 53 51 4c 20 77 72 61 70 70 65 72 s an SQL wrapper
69b0: 20 61 72 6f 75 6e 64 20 74 68 65 0a 20 20 2a 2a around the. **
69c0: 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 sqlite3_sourcei
69d0: 64 28 29 20 43 20 69 6e 74 65 72 66 61 63 65 2e d() C interface.
69e0: 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 */. sqlite3_re
69f0: 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
6a00: 74 2c 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 t, sqlite3_sourc
6a10: 65 69 64 28 29 2c 20 2d 31 2c 20 53 51 4c 49 54 eid(), -1, SQLIT
6a20: 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a E_STATIC);.}../*
6a30: 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
6a40: 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
6a50: 5f 6c 6f 67 28 29 20 66 75 6e 63 74 69 6f 6e 2e _log() function.
6a60: 20 20 54 68 69 73 20 69 73 20 61 20 77 72 61 70 This is a wrap
6a70: 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 73 71 per around.** sq
6a80: 6c 69 74 65 33 5f 6c 6f 67 28 29 2e 20 20 54 68 lite3_log(). Th
6a90: 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 e return value i
6aa0: 73 20 4e 55 4c 4c 2e 20 20 54 68 65 20 66 75 6e s NULL. The fun
6ab0: 63 74 69 6f 6e 20 65 78 69 73 74 73 20 70 75 72 ction exists pur
6ac0: 65 6c 79 20 66 6f 72 0a 2a 2a 20 69 74 73 20 73 ely for.** its s
6ad0: 69 64 65 2d 65 66 66 65 63 74 73 2e 0a 2a 2f 0a ide-effects..*/.
6ae0: 73 74 61 74 69 63 20 76 6f 69 64 20 65 72 72 6c static void errl
6af0: 6f 67 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 ogFunc(. sqlite
6b00: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
6b10: 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a xt,. int argc,.
6b20: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
6b30: 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 55 4e 55 53 **argv.){. UNUS
6b40: 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 ED_PARAMETER(arg
6b50: 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 c);. UNUSED_PAR
6b60: 41 4d 45 54 45 52 28 63 6f 6e 74 65 78 74 29 3b AMETER(context);
6b70: 0a 20 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 73 . sqlite3_log(s
6b80: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
6b90: 28 61 72 67 76 5b 30 5d 29 2c 20 22 25 73 22 2c (argv[0]), "%s",
6ba0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
6bb0: 65 78 74 28 61 72 67 76 5b 31 5d 29 29 3b 0a 7d ext(argv[1]));.}
6bc0: 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
6bd0: 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 tation of the sq
6be0: 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 lite_compileopti
6bf0: 6f 6e 5f 75 73 65 64 28 29 20 66 75 6e 63 74 69 on_used() functi
6c00: 6f 6e 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c on..** The resul
6c10: 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 t is an integer
6c20: 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 20 that identifies
6c30: 69 66 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 if the compiler
6c40: 6f 70 74 69 6f 6e 0a 2a 2a 20 77 61 73 20 75 73 option.** was us
6c50: 65 64 20 74 6f 20 62 75 69 6c 64 20 53 51 4c 69 ed to build SQLi
6c60: 74 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 te..*/.#ifndef S
6c70: 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 QLITE_OMIT_COMPI
6c80: 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 0a 73 LEOPTION_DIAGS.s
6c90: 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 69 tatic void compi
6ca0: 6c 65 6f 70 74 69 6f 6e 75 73 65 64 46 75 6e 63 leoptionusedFunc
6cb0: 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
6cc0: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
6cd0: 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
6ce0: 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
6cf0: 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 .){. const char
6d00: 20 2a 7a 4f 70 74 4e 61 6d 65 3b 0a 20 20 61 73 *zOptName;. as
6d10: 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b sert( argc==1 );
6d20: 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
6d30: 54 45 52 28 61 72 67 63 29 3b 0a 20 20 2f 2a 20 TER(argc);. /*
6d40: 49 4d 50 3a 20 52 2d 33 39 35 36 34 2d 33 36 33 IMP: R-39564-363
6d50: 30 35 20 54 68 65 20 73 71 6c 69 74 65 5f 63 6f 05 The sqlite_co
6d60: 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 mpileoption_used
6d70: 28 29 20 53 51 4c 0a 20 20 2a 2a 20 66 75 6e 63 () SQL. ** func
6d80: 74 69 6f 6e 20 69 73 20 61 20 77 72 61 70 70 65 tion is a wrappe
6d90: 72 20 61 72 6f 75 6e 64 20 74 68 65 20 73 71 6c r around the sql
6da0: 69 74 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 ite3_compileopti
6db0: 6f 6e 5f 75 73 65 64 28 29 20 43 2f 43 2b 2b 0a on_used() C/C++.
6dc0: 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 ** function..
6dd0: 20 2a 2f 0a 20 20 69 66 28 20 28 7a 4f 70 74 4e */. if( (zOptN
6de0: 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 ame = (const cha
6df0: 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 r*)sqlite3_value
6e00: 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 29 21 _text(argv[0]))!
6e10: 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
6e20: 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 3_result_int(con
6e30: 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f text, sqlite3_co
6e40: 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 mpileoption_used
6e50: 28 7a 4f 70 74 4e 61 6d 65 29 29 3b 0a 20 20 7d (zOptName));. }
6e60: 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
6e70: 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 ITE_OMIT_COMPILE
6e80: 4f 50 54 49 4f 4e 5f 44 49 41 47 53 20 2a 2f 0a OPTION_DIAGS */.
6e90: 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
6ea0: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
6eb0: 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f ite_compileoptio
6ec0: 6e 5f 67 65 74 28 29 20 66 75 6e 63 74 69 6f 6e n_get() function
6ed0: 2e 20 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 . .** The result
6ee0: 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 is a string tha
6ef0: 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 t identifies the
6f00: 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6f 6e compiler option
6f10: 73 20 0a 2a 2a 20 75 73 65 64 20 74 6f 20 62 75 s .** used to bu
6f20: 69 6c 64 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 ild SQLite..*/.#
6f30: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
6f40: 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e IT_COMPILEOPTION
6f50: 5f 44 49 41 47 53 0a 73 74 61 74 69 63 20 76 6f _DIAGS.static vo
6f60: 69 64 20 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e id compileoption
6f70: 67 65 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 getFunc(. sqlit
6f80: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
6f90: 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c ext,. int argc,
6fa0: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
6fb0: 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 **argv.){. int
6fc0: 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 n;. assert( ar
6fd0: 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 gc==1 );. UNUSE
6fe0: 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 D_PARAMETER(argc
6ff0: 29 3b 0a 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 30 );. /* IMP: R-0
7000: 34 39 32 32 2d 32 34 30 37 36 20 54 68 65 20 73 4922-24076 The s
7010: 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 74 qlite_compileopt
7020: 69 6f 6e 5f 67 65 74 28 29 20 53 51 4c 20 66 75 ion_get() SQL fu
7030: 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 61 nction. ** is a
7040: 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 wrapper around
7050: 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 the sqlite3_comp
7060: 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 29 20 ileoption_get()
7070: 43 2f 43 2b 2b 20 66 75 6e 63 74 69 6f 6e 2e 0a C/C++ function..
7080: 20 20 2a 2f 0a 20 20 6e 20 3d 20 73 71 6c 69 74 */. n = sqlit
7090: 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 e3_value_int(arg
70a0: 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 v[0]);. sqlite3
70b0: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
70c0: 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f text, sqlite3_co
70d0: 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 mpileoption_get(
70e0: 6e 29 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 n), -1, SQLITE_S
70f0: 54 41 54 49 43 29 3b 0a 7d 0a 23 65 6e 64 69 66 TATIC);.}.#endif
7100: 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
7110: 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 COMPILEOPTION_DI
7120: 41 47 53 20 2a 2f 0a 0a 2f 2a 20 41 72 72 61 79 AGS */../* Array
7130: 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 for converting
7140: 66 72 6f 6d 20 68 61 6c 66 2d 62 79 74 65 73 20 from half-bytes
7150: 28 6e 79 62 62 6c 65 73 29 20 69 6e 74 6f 20 41 (nybbles) into A
7160: 53 43 49 49 20 68 65 78 0a 2a 2a 20 64 69 67 69 SCII hex.** digi
7170: 74 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f ts. */.static co
7180: 6e 73 74 20 63 68 61 72 20 68 65 78 64 69 67 69 nst char hexdigi
7190: 74 73 5b 5d 20 3d 20 7b 0a 20 20 27 30 27 2c 20 ts[] = {. '0',
71a0: 27 31 27 2c 20 27 32 27 2c 20 27 33 27 2c 20 27 '1', '2', '3', '
71b0: 34 27 2c 20 27 35 27 2c 20 27 36 27 2c 20 27 37 4', '5', '6', '7
71c0: 27 2c 0a 20 20 27 38 27 2c 20 27 39 27 2c 20 27 ',. '8', '9', '
71d0: 41 27 2c 20 27 42 27 2c 20 27 43 27 2c 20 27 44 A', 'B', 'C', 'D
71e0: 27 2c 20 27 45 27 2c 20 27 46 27 20 0a 7d 3b 0a ', 'E', 'F' .};.
71f0: 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
7200: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 51 55 4f ation of the QUO
7210: 54 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 TE() function.
7220: 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 61 This function ta
7230: 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 kes a single.**
7240: 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 argument. If th
7250: 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 75 e argument is nu
7260: 6d 65 72 69 63 2c 20 74 68 65 20 72 65 74 75 72 meric, the retur
7270: 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73 n value is the s
7280: 61 6d 65 20 61 73 0a 2a 2a 20 74 68 65 20 61 72 ame as.** the ar
7290: 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 gument. If the
72a0: 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c 4c argument is NULL
72b0: 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c , the return val
72c0: 75 65 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 ue is the string
72d0: 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20 4f 74 68 .** "NULL". Oth
72e0: 65 72 77 69 73 65 2c 20 74 68 65 20 61 72 67 75 erwise, the argu
72f0: 6d 65 6e 74 20 69 73 20 65 6e 63 6c 6f 73 65 64 ment is enclosed
7300: 20 69 6e 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 in single quote
7310: 73 20 77 69 74 68 0a 2a 2a 20 73 69 6e 67 6c 65 s with.** single
7320: 2d 71 75 6f 74 65 20 65 73 63 61 70 65 73 2e 0a -quote escapes..
7330: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 71 */.static void q
7340: 75 6f 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33 uoteFunc(sqlite3
7350: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7360: 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c t, int argc, sql
7370: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
7380: 76 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61 72 v){. assert( ar
7390: 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 gc==1 );. UNUSE
73a0: 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 D_PARAMETER(argc
73b0: 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c );. switch( sql
73c0: 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
73d0: 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 argv[0]) ){.
73e0: 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 case SQLITE_FLOA
73f0: 54 3a 20 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c T: {. doubl
7400: 65 20 72 31 2c 20 72 32 3b 0a 20 20 20 20 20 20 e r1, r2;.
7410: 63 68 61 72 20 7a 42 75 66 5b 35 30 5d 3b 0a 20 char zBuf[50];.
7420: 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 r1 = sqlite
7430: 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 3_value_double(a
7440: 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 73 rgv[0]);. s
7450: 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
7460: 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 sizeof(zBuf), zB
7470: 75 66 2c 20 22 25 21 2e 31 35 67 22 2c 20 72 31 uf, "%!.15g", r1
7480: 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
7490: 41 74 6f 46 28 7a 42 75 66 2c 20 26 72 32 2c 20 AtoF(zBuf, &r2,
74a0: 32 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 20, SQLITE_UTF8)
74b0: 3b 0a 20 20 20 20 20 20 69 66 28 20 72 31 21 3d ;. if( r1!=
74c0: 72 32 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 r2 ){. sq
74d0: 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
74e0: 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 izeof(zBuf), zBu
74f0: 66 2c 20 22 25 21 2e 32 30 65 22 2c 20 72 31 29 f, "%!.20e", r1)
7500: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
7510: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
7520: 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 ext(context, zBu
7530: 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 f, -1, SQLITE_TR
7540: 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 ANSIENT);.
7550: 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
7560: 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 case SQLITE_INT
7570: 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 73 71 EGER: {. sq
7580: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c lite3_result_val
7590: 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 ue(context, argv
75a0: 5b 30 5d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 [0]);. brea
75b0: 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
75c0: 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b e SQLITE_BLOB: {
75d0: 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 65 . char *zTe
75e0: 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 68 xt = 0;. ch
75f0: 61 72 20 63 6f 6e 73 74 20 2a 7a 42 6c 6f 62 20 ar const *zBlob
7600: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
7610: 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 blob(argv[0]);.
7620: 20 20 20 20 20 69 6e 74 20 6e 42 6c 6f 62 20 3d int nBlob =
7630: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
7640: 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 ytes(argv[0]);.
7650: 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 42 6c assert( zBl
7660: 6f 62 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 ob==sqlite3_valu
7670: 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20 e_blob(argv[0])
7680: 29 3b 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e ); /* No encodin
7690: 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 g change */.
76a0: 20 20 7a 54 65 78 74 20 3d 20 28 63 68 61 72 20 zText = (char
76b0: 2a 29 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 *)contextMalloc(
76c0: 63 6f 6e 74 65 78 74 2c 20 28 32 2a 28 69 36 34 context, (2*(i64
76d0: 29 6e 42 6c 6f 62 29 2b 34 29 3b 20 0a 20 20 20 )nBlob)+4); .
76e0: 20 20 20 69 66 28 20 7a 54 65 78 74 20 29 7b 0a if( zText ){.
76f0: 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 int i;.
7700: 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 for(i=0;
7710: 69 3c 6e 42 6c 6f 62 3b 20 69 2b 2b 29 7b 0a 20 i<nBlob; i++){.
7720: 20 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 zText[(
7730: 69 2a 32 29 2b 32 5d 20 3d 20 68 65 78 64 69 67 i*2)+2] = hexdig
7740: 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 its[(zBlob[i]>>4
7750: 29 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 )&0x0F];.
7760: 20 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b 33 zText[(i*2)+3
7770: 5d 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28 7a ] = hexdigits[(z
7780: 42 6c 6f 62 5b 69 5d 29 26 30 78 30 46 5d 3b 0a Blob[i])&0x0F];.
7790: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
77a0: 20 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 zText[(nBlob*2
77b0: 29 2b 32 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 )+2] = '\'';.
77c0: 20 20 20 20 20 7a 54 65 78 74 5b 28 6e 42 6c 6f zText[(nBlo
77d0: 62 2a 32 29 2b 33 5d 20 3d 20 27 5c 30 27 3b 0a b*2)+3] = '\0';.
77e0: 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 30 5d zText[0]
77f0: 20 3d 20 27 58 27 3b 0a 20 20 20 20 20 20 20 20 = 'X';.
7800: 7a 54 65 78 74 5b 31 5d 20 3d 20 27 5c 27 27 3b zText[1] = '\'';
7810: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
7820: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
7830: 74 65 78 74 2c 20 7a 54 65 78 74 2c 20 2d 31 2c text, zText, -1,
7840: 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
7850: 54 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 T);. sqli
7860: 74 65 33 5f 66 72 65 65 28 7a 54 65 78 74 29 3b te3_free(zText);
7870: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
7880: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
7890: 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 case SQLITE_TEXT
78a0: 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c : {. int i,
78b0: 6a 3b 0a 20 20 20 20 20 20 75 36 34 20 6e 3b 0a j;. u64 n;.
78c0: 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 const unsi
78d0: 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 72 67 20 gned char *zArg
78e0: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
78f0: 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 text(argv[0]);.
7900: 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 char *z;..
7910: 20 20 20 20 20 69 66 28 20 7a 41 72 67 3d 3d 30 if( zArg==0
7920: 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 ) return;.
7930: 20 66 6f 72 28 69 3d 30 2c 20 6e 3d 30 3b 20 7a for(i=0, n=0; z
7940: 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b 20 69 66 Arg[i]; i++){ if
7950: 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 ( zArg[i]=='\''
7960: 29 20 6e 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 7a ) n++; }. z
7970: 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 = contextMalloc
7980: 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 (context, ((i64)
7990: 69 29 2b 28 28 69 36 34 29 6e 29 2b 33 29 3b 0a i)+((i64)n)+3);.
79a0: 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 if( z ){.
79b0: 20 20 20 20 20 20 20 7a 5b 30 5d 20 3d 20 27 5c z[0] = '\
79c0: 27 27 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 '';. for(
79d0: 69 3d 30 2c 20 6a 3d 31 3b 20 7a 41 72 67 5b 69 i=0, j=1; zArg[i
79e0: 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 ]; i++){.
79f0: 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 41 72 67 z[j++] = zArg
7a00: 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 [i];. i
7a10: 66 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27 27 f( zArg[i]=='\''
7a20: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
7a30: 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 z[j++] = '\'';.
7a40: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
7a50: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 5b 6a }. z[j
7a60: 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 ++] = '\'';.
7a70: 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 z[j] = 0;.
7a80: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 sqlite3_re
7a90: 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
7aa0: 74 2c 20 7a 2c 20 6a 2c 20 73 71 6c 69 74 65 33 t, z, j, sqlite3
7ab0: 5f 66 72 65 65 29 3b 0a 20 20 20 20 20 20 7d 0a _free);. }.
7ac0: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7ad0: 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
7ae0: 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
7af0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
7b00: 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c pe(argv[0])==SQL
7b10: 49 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 ITE_NULL );.
7b20: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
7b30: 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 _text(context, "
7b40: 4e 55 4c 4c 22 2c 20 34 2c 20 53 51 4c 49 54 45 NULL", 4, SQLITE
7b50: 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 _STATIC);.
7b60: 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d break;. }. }
7b70: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75 6e .}../*.** The un
7b80: 69 63 6f 64 65 28 29 20 66 75 6e 63 74 69 6f 6e icode() function
7b90: 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e . Return the in
7ba0: 74 65 67 65 72 20 75 6e 69 63 6f 64 65 20 63 6f teger unicode co
7bb0: 64 65 2d 70 6f 69 6e 74 20 76 61 6c 75 65 0a 2a de-point value.*
7bc0: 2a 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 * for the first
7bd0: 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 character of the
7be0: 20 69 6e 70 75 74 20 73 74 72 69 6e 67 2e 20 0a input string. .
7bf0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
7c00: 6e 69 63 6f 64 65 46 75 6e 63 28 0a 20 20 73 71 nicodeFunc(. sq
7c10: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
7c20: 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
7c30: 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
7c40: 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
7c50: 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
7c60: 68 61 72 20 2a 7a 20 3d 20 73 71 6c 69 74 65 33 har *z = sqlite3
7c70: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
7c80: 5b 30 5d 29 3b 0a 20 20 28 76 6f 69 64 29 61 72 [0]);. (void)ar
7c90: 67 63 3b 0a 20 20 69 66 28 20 7a 20 26 26 20 7a gc;. if( z && z
7ca0: 5b 30 5d 20 29 20 73 71 6c 69 74 65 33 5f 72 65 [0] ) sqlite3_re
7cb0: 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 sult_int(context
7cc0: 2c 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 , sqlite3Utf8Rea
7cd0: 64 28 26 7a 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a d(&z));.}../*.**
7ce0: 20 54 68 65 20 63 68 61 72 28 29 20 66 75 6e 63 The char() func
7cf0: 74 69 6f 6e 20 74 61 6b 65 73 20 7a 65 72 6f 20 tion takes zero
7d00: 6f 72 20 6d 6f 72 65 20 61 72 67 75 6d 65 6e 74 or more argument
7d10: 73 2c 20 65 61 63 68 20 6f 66 20 77 68 69 63 68 s, each of which
7d20: 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 is.** an intege
7d30: 72 2e 20 20 49 74 20 63 6f 6e 73 74 72 75 63 74 r. It construct
7d40: 73 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 s a string where
7d50: 20 65 61 63 68 20 63 68 61 72 61 63 74 65 72 20 each character
7d60: 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a of the string.**
7d70: 20 69 73 20 74 68 65 20 75 6e 69 63 6f 64 65 20 is the unicode
7d80: 63 68 61 72 61 63 74 65 72 20 66 6f 72 20 74 68 character for th
7d90: 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
7da0: 69 6e 74 65 67 65 72 20 61 72 67 75 6d 65 6e 74 integer argument
7db0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
7dc0: 20 63 68 61 72 46 75 6e 63 28 0a 20 20 73 71 6c charFunc(. sql
7dd0: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
7de0: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
7df0: 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
7e00: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 ue **argv.){. u
7e10: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c nsigned char *z,
7e20: 20 2a 7a 4f 75 74 3b 0a 20 20 69 6e 74 20 69 3b *zOut;. int i;
7e30: 0a 20 20 7a 4f 75 74 20 3d 20 7a 20 3d 20 73 71 . zOut = z = sq
7e40: 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 36 34 28 20 lite3_malloc64(
7e50: 61 72 67 63 2a 34 2b 31 20 29 3b 0a 20 20 69 66 argc*4+1 );. if
7e60: 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 ( z==0 ){. sq
7e70: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
7e80: 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 or_nomem(context
7e90: 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 );. return;.
7ea0: 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
7eb0: 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 argc; i++){.
7ec0: 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 78 3b sqlite3_int64 x;
7ed0: 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 3b . unsigned c;
7ee0: 0a 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33 . x = sqlite3
7ef0: 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 _value_int64(arg
7f00: 76 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 78 v[i]);. if( x
7f10: 3c 30 20 7c 7c 20 78 3e 30 78 31 30 66 66 66 66 <0 || x>0x10ffff
7f20: 20 29 20 78 20 3d 20 30 78 66 66 66 64 3b 0a 20 ) x = 0xfffd;.
7f30: 20 20 20 63 20 3d 20 28 75 6e 73 69 67 6e 65 64 c = (unsigned
7f40: 29 28 78 20 26 20 30 78 31 66 66 66 66 66 29 3b )(x & 0x1fffff);
7f50: 0a 20 20 20 20 69 66 28 20 63 3c 30 78 30 30 30 . if( c<0x000
7f60: 38 30 20 29 7b 0a 20 20 20 20 20 20 2a 7a 4f 75 80 ){. *zOu
7f70: 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 46 t++ = (u8)(c&0xF
7f80: 46 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 F);. }else if
7f90: 28 20 63 3c 30 78 30 30 38 30 30 20 29 7b 0a 20 ( c<0x00800 ){.
7fa0: 20 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 *zOut++ = 0
7fb0: 78 43 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 xC0 + (u8)((c>>6
7fc0: 29 26 30 78 31 46 29 3b 0a 20 20 20 20 20 20 2a )&0x1F);. *
7fd0: 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 zOut++ = 0x80 +
7fe0: 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 0a (u8)(c & 0x3F);.
7ff0: 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3c }else if( c<
8000: 30 78 31 30 30 30 30 20 29 7b 0a 20 20 20 20 20 0x10000 ){.
8010: 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 *zOut++ = 0xE0
8020: 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 26 30 + (u8)((c>>12)&0
8030: 78 30 46 29 3b 0a 20 20 20 20 20 20 2a 7a 4f 75 x0F);. *zOu
8040: 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 t++ = 0x80 + (u8
8050: 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 )((c>>6) & 0x3F)
8060: 3b 0a 20 20 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 ;. *zOut++
8070: 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 = 0x80 + (u8)(c
8080: 26 20 30 78 33 46 29 3b 0a 20 20 20 20 7d 65 6c & 0x3F);. }el
8090: 73 65 7b 0a 20 20 20 20 20 20 2a 7a 4f 75 74 2b se{. *zOut+
80a0: 2b 20 3d 20 30 78 46 30 20 2b 20 28 75 38 29 28 + = 0xF0 + (u8)(
80b0: 28 63 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b (c>>18) & 0x07);
80c0: 0a 20 20 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d . *zOut++ =
80d0: 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 0x80 + (u8)((c>
80e0: 3e 31 32 29 20 26 20 30 78 33 46 29 3b 0a 20 20 >12) & 0x3F);.
80f0: 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 *zOut++ = 0x
8100: 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 80 + (u8)((c>>6)
8110: 20 26 20 30 78 33 46 29 3b 0a 20 20 20 20 20 20 & 0x3F);.
8120: 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b *zOut++ = 0x80 +
8130: 20 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b (u8)(c & 0x3F);
8140: 0a 20 20 20 20 7d 20 20 20 20 20 20 20 20 20 20 . }
8150: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8160: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8170: 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a \. }.
8180: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
8190: 5f 74 65 78 74 36 34 28 63 6f 6e 74 65 78 74 2c _text64(context,
81a0: 20 28 63 68 61 72 2a 29 7a 2c 20 7a 4f 75 74 2d (char*)z, zOut-
81b0: 7a 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 2c z, sqlite3_free,
81c0: 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d SQLITE_UTF8);.}
81d0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 78 28 ../*.** The hex(
81e0: 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 6e 74 ) function. Int
81f0: 65 72 70 72 65 74 20 74 68 65 20 61 72 67 75 6d erpret the argum
8200: 65 6e 74 20 61 73 20 61 20 62 6c 6f 62 2e 20 20 ent as a blob.
8210: 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68 65 78 61 Return.** a hexa
8220: 64 65 63 69 6d 61 6c 20 72 65 6e 64 65 72 69 6e decimal renderin
8230: 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 g as text..*/.st
8240: 61 74 69 63 20 76 6f 69 64 20 68 65 78 46 75 6e atic void hexFun
8250: 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
8260: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
8270: 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c int argc,. sql
8280: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
8290: 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b v.){. int i, n;
82a0: 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 . const unsigne
82b0: 64 20 63 68 61 72 20 2a 70 42 6c 6f 62 3b 0a 20 d char *pBlob;.
82c0: 20 63 68 61 72 20 2a 7a 48 65 78 2c 20 2a 7a 3b char *zHex, *z;
82d0: 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d . assert( argc=
82e0: 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 =1 );. UNUSED_P
82f0: 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a ARAMETER(argc);.
8300: 20 20 70 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 pBlob = sqlite
8310: 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 3_value_blob(arg
8320: 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c v[0]);. n = sql
8330: 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
8340: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 (argv[0]);. ass
8350: 65 72 74 28 20 70 42 6c 6f 62 3d 3d 73 71 6c 69 ert( pBlob==sqli
8360: 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 te3_value_blob(a
8370: 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e rgv[0]) ); /* N
8380: 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 o encoding chang
8390: 65 20 2a 2f 0a 20 20 7a 20 3d 20 7a 48 65 78 20 e */. z = zHex
83a0: 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 = contextMalloc(
83b0: 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e context, ((i64)n
83c0: 29 2a 32 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 )*2 + 1);. if(
83d0: 7a 48 65 78 20 29 7b 0a 20 20 20 20 66 6f 72 28 zHex ){. for(
83e0: 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 i=0; i<n; i++, p
83f0: 42 6c 6f 62 2b 2b 29 7b 0a 20 20 20 20 20 20 75 Blob++){. u
8400: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 20 3d nsigned char c =
8410: 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 20 20 2a *pBlob;. *
8420: 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74 (z++) = hexdigit
8430: 73 5b 28 63 3e 3e 34 29 26 30 78 66 5d 3b 0a 20 s[(c>>4)&0xf];.
8440: 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 *(z++) = he
8450: 78 64 69 67 69 74 73 5b 63 26 30 78 66 5d 3b 0a xdigits[c&0xf];.
8460: 20 20 20 20 7d 0a 20 20 20 20 2a 7a 20 3d 20 30 }. *z = 0
8470: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
8480: 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
8490: 74 2c 20 7a 48 65 78 2c 20 6e 2a 32 2c 20 73 71 t, zHex, n*2, sq
84a0: 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d lite3_free);. }
84b0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 7a 65 .}../*.** The ze
84c0: 72 6f 62 6c 6f 62 28 4e 29 20 66 75 6e 63 74 69 roblob(N) functi
84d0: 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 7a 65 72 on returns a zer
84e0: 6f 2d 66 69 6c 6c 65 64 20 62 6c 6f 62 20 6f 66 o-filled blob of
84f0: 20 73 69 7a 65 20 4e 20 62 79 74 65 73 2e 0a 2a size N bytes..*
8500: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 7a 65 /.static void ze
8510: 72 6f 62 6c 6f 62 46 75 6e 63 28 0a 20 20 73 71 roblobFunc(. sq
8520: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
8530: 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
8540: 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
8550: 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
8560: 69 36 34 20 6e 3b 0a 20 20 69 6e 74 20 72 63 3b i64 n;. int rc;
8570: 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d . assert( argc=
8580: 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 =1 );. UNUSED_P
8590: 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a ARAMETER(argc);.
85a0: 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 n = sqlite3_va
85b0: 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 lue_int64(argv[0
85c0: 5d 29 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 20 ]);. if( n<0 )
85d0: 6e 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 n = 0;. rc = sq
85e0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 lite3_result_zer
85f0: 6f 62 6c 6f 62 36 34 28 63 6f 6e 74 65 78 74 2c oblob64(context,
8600: 20 6e 29 3b 20 2f 2a 20 49 4d 50 3a 20 52 2d 30 n); /* IMP: R-0
8610: 30 32 39 33 2d 36 34 39 39 34 20 2a 2f 0a 20 20 0293-64994 */.
8620: 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 73 71 if( rc ){. sq
8630: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
8640: 6f 72 5f 63 6f 64 65 28 63 6f 6e 74 65 78 74 2c or_code(context,
8650: 20 72 63 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a rc);. }.}../*.
8660: 2a 2a 20 54 68 65 20 72 65 70 6c 61 63 65 28 29 ** The replace()
8670: 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 72 65 function. Thre
8680: 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 e arguments are
8690: 61 6c 6c 20 73 74 72 69 6e 67 73 3a 20 63 61 6c all strings: cal
86a0: 6c 0a 2a 2a 20 74 68 65 6d 20 41 2c 20 42 2c 20 l.** them A, B,
86b0: 61 6e 64 20 43 2e 20 54 68 65 20 72 65 73 75 6c and C. The resul
86c0: 74 20 69 73 20 61 6c 73 6f 20 61 20 73 74 72 69 t is also a stri
86d0: 6e 67 20 77 68 69 63 68 20 69 73 20 64 65 72 69 ng which is deri
86e0: 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 79 ved.** from A by
86f0: 20 72 65 70 6c 61 63 69 6e 67 20 65 76 65 72 79 replacing every
8700: 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 20 42 occurrence of B
8710: 20 77 69 74 68 20 43 2e 20 20 54 68 65 20 6d 61 with C. The ma
8720: 74 63 68 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 tch.** must be e
8730: 78 61 63 74 2e 20 20 43 6f 6c 6c 61 74 69 6e 67 xact. Collating
8740: 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 20 6e sequences are n
8750: 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 ot used..*/.stat
8760: 69 63 20 76 6f 69 64 20 72 65 70 6c 61 63 65 46 ic void replaceF
8770: 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
8780: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
8790: 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
87a0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
87b0: 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 rgv.){. const u
87c0: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 nsigned char *zS
87d0: 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 tr; /* Th
87e0: 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 41 e input string A
87f0: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 */. const unsi
8800: 67 6e 65 64 20 63 68 61 72 20 2a 7a 50 61 74 74 gned char *zPatt
8810: 65 72 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 70 ern; /* The p
8820: 61 74 74 65 72 6e 20 73 74 72 69 6e 67 20 42 20 attern string B
8830: 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 */. const unsig
8840: 6e 65 64 20 63 68 61 72 20 2a 7a 52 65 70 3b 20 ned char *zRep;
8850: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 /* The re
8860: 70 6c 61 63 65 6d 65 6e 74 20 73 74 72 69 6e 67 placement string
8870: 20 43 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 C */. unsigned
8880: 20 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 char *zOut;
8890: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
88a0: 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 output */. int
88b0: 20 6e 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 nStr;
88c0: 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
88d0: 20 7a 53 74 72 20 2a 2f 0a 20 20 69 6e 74 20 6e zStr */. int n
88e0: 50 61 74 74 65 72 6e 3b 20 20 20 20 20 20 20 20 Pattern;
88f0: 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a /* Size of z
8900: 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 Pattern */. int
8910: 20 6e 52 65 70 3b 20 20 20 20 20 20 20 20 20 20 nRep;
8920: 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
8930: 20 7a 52 65 70 20 2a 2f 0a 20 20 69 36 34 20 6e zRep */. i64 n
8940: 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 Out;
8950: 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 /* Maximum s
8960: 69 7a 65 20 6f 66 20 7a 4f 75 74 20 2a 2f 0a 20 ize of zOut */.
8970: 20 69 6e 74 20 6c 6f 6f 70 4c 69 6d 69 74 3b 20 int loopLimit;
8980: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 /* Las
8990: 74 20 7a 53 74 72 5b 5d 20 74 68 61 74 20 6d 69 t zStr[] that mi
89a0: 67 68 74 20 6d 61 74 63 68 20 7a 50 61 74 74 65 ght match zPatte
89b0: 72 6e 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c rn[] */. int i,
89c0: 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 j;
89d0: 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
89e0: 65 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ers */.. assert
89f0: 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 55 ( argc==3 );. U
8a00: 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
8a10: 61 72 67 63 29 3b 0a 20 20 7a 53 74 72 20 3d 20 argc);. zStr =
8a20: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
8a30: 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 xt(argv[0]);. i
8a40: 66 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65 74 f( zStr==0 ) ret
8a50: 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73 71 urn;. nStr = sq
8a60: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
8a70: 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 s(argv[0]);. as
8a80: 73 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c 69 sert( zStr==sqli
8a90: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
8aa0: 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e rgv[0]) ); /* N
8ab0: 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 o encoding chang
8ac0: 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e 20 e */. zPattern
8ad0: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
8ae0: 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 text(argv[1]);.
8af0: 20 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d 30 if( zPattern==0
8b00: 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
8b10: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
8b20: 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53 51 4c pe(argv[1])==SQL
8b30: 49 54 45 5f 4e 55 4c 4c 0a 20 20 20 20 20 20 20 ITE_NULL.
8b40: 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f || sqlite3_
8b50: 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
8b60: 65 28 63 6f 6e 74 65 78 74 29 2d 3e 6d 61 6c 6c e(context)->mall
8b70: 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 ocFailed );.
8b80: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 return;. }. if
8b90: 28 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d 3d 30 ( zPattern[0]==0
8ba0: 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
8bb0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
8bc0: 70 65 28 61 72 67 76 5b 31 5d 29 21 3d 53 51 4c pe(argv[1])!=SQL
8bd0: 49 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 ITE_NULL );.
8be0: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 sqlite3_result_v
8bf0: 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 alue(context, ar
8c00: 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72 65 74 75 gv[0]);. retu
8c10: 72 6e 3b 0a 20 20 7d 0a 20 20 6e 50 61 74 74 65 rn;. }. nPatte
8c20: 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c rn = sqlite3_val
8c30: 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d ue_bytes(argv[1]
8c40: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 50 61 );. assert( zPa
8c50: 74 74 65 72 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 ttern==sqlite3_v
8c60: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 alue_text(argv[1
8c70: 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 ]) ); /* No enc
8c80: 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a oding change */.
8c90: 20 20 7a 52 65 70 20 3d 20 73 71 6c 69 74 65 33 zRep = sqlite3
8ca0: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
8cb0: 5b 32 5d 29 3b 0a 20 20 69 66 28 20 7a 52 65 70 [2]);. if( zRep
8cc0: 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
8cd0: 6e 52 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 nRep = sqlite3_v
8ce0: 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b alue_bytes(argv[
8cf0: 32 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 2]);. assert( z
8d00: 52 65 70 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c Rep==sqlite3_val
8d10: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 ue_text(argv[2])
8d20: 20 29 3b 0a 20 20 6e 4f 75 74 20 3d 20 6e 53 74 );. nOut = nSt
8d30: 72 20 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 28 r + 1;. assert(
8d40: 20 6e 4f 75 74 3c 53 51 4c 49 54 45 5f 4d 41 58 nOut<SQLITE_MAX
8d50: 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 7a 4f 75 _LENGTH );. zOu
8d60: 74 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f t = contextMallo
8d70: 63 28 63 6f 6e 74 65 78 74 2c 20 28 69 36 34 29 c(context, (i64)
8d80: 6e 4f 75 74 29 3b 0a 20 20 69 66 28 20 7a 4f 75 nOut);. if( zOu
8d90: 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 t==0 ){. retu
8da0: 72 6e 3b 0a 20 20 7d 0a 20 20 6c 6f 6f 70 4c 69 rn;. }. loopLi
8db0: 6d 69 74 20 3d 20 6e 53 74 72 20 2d 20 6e 50 61 mit = nStr - nPa
8dc0: 74 74 65 72 6e 3b 20 20 0a 20 20 66 6f 72 28 69 ttern; . for(i
8dd0: 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f 70 4c 69 6d =j=0; i<=loopLim
8de0: 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 it; i++){. if
8df0: 28 20 7a 53 74 72 5b 69 5d 21 3d 7a 50 61 74 74 ( zStr[i]!=zPatt
8e00: 65 72 6e 5b 30 5d 20 7c 7c 20 6d 65 6d 63 6d 70 ern[0] || memcmp
8e10: 28 26 7a 53 74 72 5b 69 5d 2c 20 7a 50 61 74 74 (&zStr[i], zPatt
8e20: 65 72 6e 2c 20 6e 50 61 74 74 65 72 6e 29 20 29 ern, nPattern) )
8e30: 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 2b 2b {. zOut[j++
8e40: 5d 20 3d 20 7a 53 74 72 5b 69 5d 3b 0a 20 20 20 ] = zStr[i];.
8e50: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 38 }else{. u8
8e60: 20 2a 7a 4f 6c 64 3b 0a 20 20 20 20 20 20 73 71 *zOld;. sq
8e70: 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
8e80: 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
8e90: 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
8ea0: 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d 20 6e 52 nOut += nR
8eb0: 65 70 20 2d 20 6e 50 61 74 74 65 72 6e 3b 0a 20 ep - nPattern;.
8ec0: 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e testcase( n
8ed0: 4f 75 74 2d 31 3d 3d 64 62 2d 3e 61 4c 69 6d 69 Out-1==db->aLimi
8ee0: 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
8ef0: 45 4e 47 54 48 5d 20 29 3b 0a 20 20 20 20 20 20 ENGTH] );.
8f00: 74 65 73 74 63 61 73 65 28 20 6e 4f 75 74 2d 32 testcase( nOut-2
8f10: 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ==db->aLimit[SQL
8f20: 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
8f30: 5d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e ] );. if( n
8f40: 4f 75 74 2d 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 Out-1>db->aLimit
8f50: 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 [SQLITE_LIMIT_LE
8f60: 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 20 NGTH] ){.
8f70: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
8f80: 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e error_toobig(con
8f90: 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73 text);. s
8fa0: 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f 75 74 qlite3_free(zOut
8fb0: 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
8fc0: 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 n;. }.
8fd0: 20 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20 20 zOld = zOut;.
8fe0: 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 zOut = sqlit
8ff0: 65 33 5f 72 65 61 6c 6c 6f 63 36 34 28 7a 4f 75 e3_realloc64(zOu
9000: 74 2c 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 t, (int)nOut);.
9010: 20 20 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 if( zOut==0
9020: 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
9030: 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
9040: 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b _nomem(context);
9050: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
9060: 5f 66 72 65 65 28 7a 4f 6c 64 29 3b 0a 20 20 20 _free(zOld);.
9070: 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
9080: 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 }. memcp
9090: 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20 7a 52 65 70 y(&zOut[j], zRep
90a0: 2c 20 6e 52 65 70 29 3b 0a 20 20 20 20 20 20 6a , nRep);. j
90b0: 20 2b 3d 20 6e 52 65 70 3b 0a 20 20 20 20 20 20 += nRep;.
90c0: 69 20 2b 3d 20 6e 50 61 74 74 65 72 6e 2d 31 3b i += nPattern-1;
90d0: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 . }. }. ass
90e0: 65 72 74 28 20 6a 2b 6e 53 74 72 2d 69 2b 31 3d ert( j+nStr-i+1=
90f0: 3d 6e 4f 75 74 20 29 3b 0a 20 20 6d 65 6d 63 70 =nOut );. memcp
9100: 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53 74 y(&zOut[j], &zSt
9110: 72 5b 69 5d 2c 20 6e 53 74 72 2d 69 29 3b 0a 20 r[i], nStr-i);.
9120: 20 6a 20 2b 3d 20 6e 53 74 72 20 2d 20 69 3b 0a j += nStr - i;.
9130: 20 20 61 73 73 65 72 74 28 20 6a 3c 3d 6e 4f 75 assert( j<=nOu
9140: 74 20 29 3b 0a 20 20 7a 4f 75 74 5b 6a 5d 20 3d t );. zOut[j] =
9150: 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 0;. sqlite3_re
9160: 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
9170: 74 2c 20 28 63 68 61 72 2a 29 7a 4f 75 74 2c 20 t, (char*)zOut,
9180: 6a 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 j, sqlite3_free)
9190: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
91a0: 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
91b0: 20 54 52 49 4d 28 29 2c 20 4c 54 52 49 4d 28 29 TRIM(), LTRIM()
91c0: 2c 20 61 6e 64 20 52 54 52 49 4d 28 29 20 66 75 , and RTRIM() fu
91d0: 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 nctions..** The
91e0: 75 73 65 72 64 61 74 61 20 69 73 20 30 78 31 20 userdata is 0x1
91f0: 66 6f 72 20 6c 65 66 74 20 74 72 69 6d 2c 20 30 for left trim, 0
9200: 78 32 20 66 6f 72 20 72 69 67 68 74 20 74 72 69 x2 for right tri
9210: 6d 2c 20 30 78 33 20 66 6f 72 20 62 6f 74 68 2e m, 0x3 for both.
9220: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
9230: 74 72 69 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 trimFunc(. sqli
9240: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
9250: 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
9260: 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
9270: 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f e **argv.){. co
9280: 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
9290: 72 20 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 r *zIn;
92a0: 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 /* Input string
92b0: 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 */. const unsig
92c0: 6e 65 64 20 63 68 61 72 20 2a 7a 43 68 61 72 53 ned char *zCharS
92d0: 65 74 3b 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 et; /* Set of
92e0: 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 characters to t
92f0: 72 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e rim */. int nIn
9300: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
9310: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
9320: 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
9330: 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 input */. int
9340: 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
9350: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
9360: 20 31 3a 20 74 72 69 6d 6c 65 66 74 20 20 32 3a 1: trimleft 2:
9370: 20 74 72 69 6d 72 69 67 68 74 20 20 33 3a 20 74 trimright 3: t
9380: 72 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 rim */. int i;
9390: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
93a0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
93b0: 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
93c0: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 unsigned char *a
93d0: 4c 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 Len = 0;
93e0: 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 65 /* Length of e
93f0: 61 63 68 20 63 68 61 72 61 63 74 65 72 20 69 6e ach character in
9400: 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 75 zCharSet */. u
9410: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 61 nsigned char **a
9420: 7a 43 68 61 72 20 3d 20 30 3b 20 20 20 20 20 20 zChar = 0;
9430: 20 2f 2a 20 49 6e 64 69 76 69 64 75 61 6c 20 63 /* Individual c
9440: 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68 haracters in zCh
9450: 61 72 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e arSet */. int n
9460: 43 68 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 Char;
9470: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
9480: 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 Number of charac
9490: 74 65 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74 ters in zCharSet
94a0: 20 2a 2f 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 */.. if( sqlit
94b0: 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 e3_value_type(ar
94c0: 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e gv[0])==SQLITE_N
94d0: 55 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ULL ){. retur
94e0: 6e 3b 0a 20 20 7d 0a 20 20 7a 49 6e 20 3d 20 73 n;. }. zIn = s
94f0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
9500: 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 t(argv[0]);. if
9510: 28 20 7a 49 6e 3d 3d 30 20 29 20 72 65 74 75 72 ( zIn==0 ) retur
9520: 6e 3b 0a 20 20 6e 49 6e 20 3d 20 73 71 6c 69 74 n;. nIn = sqlit
9530: 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
9540: 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 rgv[0]);. asser
9550: 74 28 20 7a 49 6e 3d 3d 73 71 6c 69 74 65 33 5f t( zIn==sqlite3_
9560: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
9570: 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 61 72 67 0]) );. if( arg
9580: 63 3d 3d 31 20 29 7b 0a 20 20 20 20 73 74 61 74 c==1 ){. stat
9590: 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 ic const unsigne
95a0: 64 20 63 68 61 72 20 6c 65 6e 4f 6e 65 5b 5d 20 d char lenOne[]
95b0: 3d 20 7b 20 31 20 7d 3b 0a 20 20 20 20 73 74 61 = { 1 };. sta
95c0: 74 69 63 20 75 6e 73 69 67 6e 65 64 20 63 68 61 tic unsigned cha
95d0: 72 20 2a 20 63 6f 6e 73 74 20 61 7a 4f 6e 65 5b r * const azOne[
95e0: 5d 20 3d 20 7b 20 28 75 38 2a 29 22 20 22 20 7d ] = { (u8*)" " }
95f0: 3b 0a 20 20 20 20 6e 43 68 61 72 20 3d 20 31 3b ;. nChar = 1;
9600: 0a 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 38 2a . aLen = (u8*
9610: 29 6c 65 6e 4f 6e 65 3b 0a 20 20 20 20 61 7a 43 )lenOne;. azC
9620: 68 61 72 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 har = (unsigned
9630: 63 68 61 72 20 2a 2a 29 61 7a 4f 6e 65 3b 0a 20 char **)azOne;.
9640: 20 20 20 7a 43 68 61 72 53 65 74 20 3d 20 30 3b zCharSet = 0;
9650: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 7a 43 . }else if( (zC
9660: 68 61 72 53 65 74 20 3d 20 73 71 6c 69 74 65 33 harSet = sqlite3
9670: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
9680: 5b 31 5d 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 [1]))==0 ){.
9690: 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b return;. }else{
96a0: 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 . const unsig
96b0: 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 ned char *z;.
96c0: 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c for(z=zCharSet,
96d0: 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 nChar=0; *z; nC
96e0: 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 har++){. SQ
96f0: 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a LITE_SKIP_UTF8(z
9700: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
9710: 20 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 nChar>0 ){.
9720: 20 20 61 7a 43 68 61 72 20 3d 20 63 6f 6e 74 65 azChar = conte
9730: 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 xtMalloc(context
9740: 2c 20 28 28 69 36 34 29 6e 43 68 61 72 29 2a 28 , ((i64)nChar)*(
9750: 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2b 31 29 sizeof(char*)+1)
9760: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61 7a 43 );. if( azC
9770: 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 har==0 ){.
9780: 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
9790: 7d 0a 20 20 20 20 20 20 61 4c 65 6e 20 3d 20 28 }. aLen = (
97a0: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 unsigned char*)&
97b0: 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 azChar[nChar];.
97c0: 20 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 for(z=zChar
97d0: 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a Set, nChar=0; *z
97e0: 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 ; nChar++){.
97f0: 20 20 20 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 azChar[nChar
9800: 5d 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 ] = (unsigned ch
9810: 61 72 20 2a 29 7a 3b 0a 20 20 20 20 20 20 20 20 ar *)z;.
9820: 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 SQLITE_SKIP_UTF8
9830: 28 7a 29 3b 0a 20 20 20 20 20 20 20 20 61 4c 65 (z);. aLe
9840: 6e 5b 6e 43 68 61 72 5d 20 3d 20 28 75 38 29 28 n[nChar] = (u8)(
9850: 7a 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 z - azChar[nChar
9860: 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ]);. }.
9870: 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 }. }. if( nCha
9880: 72 3e 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 r>0 ){. flags
9890: 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f = SQLITE_PTR_TO
98a0: 5f 49 4e 54 28 73 71 6c 69 74 65 33 5f 75 73 65 _INT(sqlite3_use
98b0: 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 29 r_data(context))
98c0: 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 ;. if( flags
98d0: 26 20 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69 & 1 ){. whi
98e0: 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 le( nIn>0 ){.
98f0: 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 int len = 0
9900: 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d ;. for(i=
9910: 30 3b 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 0; i<nChar; i++)
9920: 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 20 {. len
9930: 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 = aLen[i];.
9940: 20 20 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 if( len<=nI
9950: 6e 20 26 26 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c n && memcmp(zIn,
9960: 20 61 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 azChar[i], len)
9970: 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ==0 ) break;.
9980: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
9990: 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 f( i>=nChar ) br
99a0: 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e eak;. zIn
99b0: 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 += len;.
99c0: 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 nIn -= len;.
99d0: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
99e0: 66 28 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a f( flags & 2 ){.
99f0: 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e while( nIn
9a00: 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e >0 ){. in
9a10: 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 t len = 0;.
9a20: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 for(i=0; i<nC
9a30: 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 har; i++){.
9a40: 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b len = aLen[
9a50: 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 i];. if
9a60: 28 20 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 ( len<=nIn && me
9a70: 6d 63 6d 70 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 mcmp(&zIn[nIn-le
9a80: 6e 5d 2c 61 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e n],azChar[i],len
9a90: 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 )==0 ) break;.
9aa0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
9ab0: 69 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 if( i>=nChar ) b
9ac0: 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 6e 49 reak;. nI
9ad0: 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 n -= len;.
9ae0: 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
9af0: 7a 43 68 61 72 53 65 74 20 29 7b 0a 20 20 20 20 zCharSet ){.
9b00: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 sqlite3_free(a
9b10: 7a 43 68 61 72 29 3b 0a 20 20 20 20 7d 0a 20 20 zChar);. }.
9b20: 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 }. sqlite3_resu
9b30: 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
9b40: 20 28 63 68 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e (char*)zIn, nIn
9b50: 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
9b60: 4e 54 29 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 NT);.}...#ifdef
9b70: 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e SQLITE_ENABLE_UN
9b80: 4b 4e 4f 57 4e 5f 53 51 4c 5f 46 55 4e 43 54 49 KNOWN_SQL_FUNCTI
9b90: 4f 4e 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 75 6e ON./*.** The "un
9ba0: 6b 6e 6f 77 6e 22 20 66 75 6e 63 74 69 6f 6e 20 known" function
9bb0: 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 is automatically
9bc0: 20 73 75 62 73 74 69 74 75 74 65 64 20 69 6e 20 substituted in
9bd0: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 6e 79 20 place of.** any
9be0: 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 66 75 6e unrecognized fun
9bf0: 63 74 69 6f 6e 20 6e 61 6d 65 20 77 68 65 6e 20 ction name when
9c00: 64 6f 69 6e 67 20 61 6e 20 45 58 50 4c 41 49 4e doing an EXPLAIN
9c10: 20 6f 72 20 45 58 50 4c 41 49 4e 20 51 55 45 52 or EXPLAIN QUER
9c20: 59 20 50 4c 41 4e 0a 2a 2a 20 77 68 65 6e 20 74 Y PLAN.** when t
9c30: 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 he SQLITE_ENABLE
9c40: 5f 55 4e 4b 4e 4f 57 4e 5f 46 55 4e 43 54 49 4f _UNKNOWN_FUNCTIO
9c50: 4e 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f N compile-time o
9c60: 70 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 0a 2a ption is used..*
9c70: 2a 20 57 68 65 6e 20 74 68 65 20 22 73 71 6c 69 * When the "sqli
9c80: 74 65 33 22 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e te3" command-lin
9c90: 65 20 73 68 65 6c 6c 20 69 73 20 62 75 69 6c 74 e shell is built
9ca0: 20 75 73 69 6e 67 20 74 68 69 73 20 66 75 6e 63 using this func
9cb0: 74 69 6f 6e 61 6c 69 74 79 2c 0a 2a 2a 20 74 68 tionality,.** th
9cc0: 61 74 20 61 6c 6c 6f 77 73 20 61 6e 20 45 58 50 at allows an EXP
9cd0: 4c 41 49 4e 20 6f 72 20 45 58 50 4c 41 49 4e 20 LAIN or EXPLAIN
9ce0: 51 55 45 52 59 20 50 4c 41 4e 20 66 6f 72 20 63 QUERY PLAN for c
9cf0: 6f 6d 70 6c 65 78 20 71 75 65 72 69 65 73 0a 2a omplex queries.*
9d00: 2a 20 69 6e 76 6f 6c 76 69 6e 67 20 61 70 70 6c * involving appl
9d10: 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
9d20: 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65 20 functions to be
9d30: 65 78 61 6d 69 6e 65 64 20 69 6e 20 61 20 67 65 examined in a ge
9d40: 6e 65 72 69 63 0a 2a 2a 20 73 71 6c 69 74 65 33 neric.** sqlite3
9d50: 20 73 68 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 shell..*/.stati
9d60: 63 20 76 6f 69 64 20 75 6e 6b 6e 6f 77 6e 46 75 c void unknownFu
9d70: 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
9d80: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
9d90: 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
9da0: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
9db0: 67 76 0a 29 7b 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 gv.){. /* no-op
9dc0: 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 53 */.}.#endif /*S
9dd0: 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4b QLITE_ENABLE_UNK
9de0: 4e 4f 57 4e 5f 53 51 4c 5f 46 55 4e 43 54 49 4f NOWN_SQL_FUNCTIO
9df0: 4e 2a 2f 0a 0a 0a 2f 2a 20 49 4d 50 3a 20 52 2d N*/.../* IMP: R-
9e00: 32 35 33 36 31 2d 31 36 31 35 30 20 54 68 69 73 25361-16150 This
9e10: 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6d 69 function is omi
9e20: 74 74 65 64 20 66 72 6f 6d 20 53 51 4c 69 74 65 tted from SQLite
9e30: 20 62 79 20 64 65 66 61 75 6c 74 2e 20 49 74 0a by default. It.
9e40: 2a 2a 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c ** is only avail
9e50: 61 62 6c 65 20 69 66 20 74 68 65 20 53 51 4c 49 able if the SQLI
9e60: 54 45 5f 53 4f 55 4e 44 45 58 20 63 6f 6d 70 69 TE_SOUNDEX compi
9e70: 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 69 le-time option i
9e80: 73 20 75 73 65 64 0a 2a 2a 20 77 68 65 6e 20 53 s used.** when S
9e90: 51 4c 69 74 65 20 69 73 20 62 75 69 6c 74 2e 0a QLite is built..
9ea0: 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
9eb0: 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a 20 43 _SOUNDEX./*.** C
9ec0: 6f 6d 70 75 74 65 20 74 68 65 20 73 6f 75 6e 64 ompute the sound
9ed0: 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 ex encoding of a
9ee0: 20 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 word..**.** IMP
9ef0: 3a 20 52 2d 35 39 37 38 32 2d 30 30 30 37 32 20 : R-59782-00072
9f00: 54 68 65 20 73 6f 75 6e 64 65 78 28 58 29 20 66 The soundex(X) f
9f10: 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
9f20: 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 a string that is
9f30: 20 74 68 65 0a 2a 2a 20 73 6f 75 6e 64 65 78 20 the.** soundex
9f40: 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 encoding of the
9f50: 73 74 72 69 6e 67 20 58 2e 20 0a 2a 2f 0a 73 74 string X. .*/.st
9f60: 61 74 69 63 20 76 6f 69 64 20 73 6f 75 6e 64 65 atic void sounde
9f70: 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 xFunc(. sqlite3
9f80: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
9f90: 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
9fa0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
9fb0: 2a 61 72 67 76 0a 29 7b 0a 20 20 63 68 61 72 20 *argv.){. char
9fc0: 7a 52 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f zResult[8];. co
9fd0: 6e 73 74 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69 nst u8 *zIn;. i
9fe0: 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69 nt i, j;. stati
9ff0: 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
a000: 20 63 68 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20 char iCode[] =
a010: 7b 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 {. 0, 0, 0, 0
a020: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
a030: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
a040: 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 0, 0,. 0, 0,
a050: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
a060: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
a070: 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 0, 0, 0,. 0,
a080: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
a090: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
a0a0: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 , 0, 0, 0, 0,.
a0b0: 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0, 0, 0, 0, 0,
a0c0: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
a0d0: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
a0e0: 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 ,. 0, 0, 1, 2
a0f0: 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c , 3, 0, 1, 2, 0,
a100: 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 0, 2, 2, 4, 5,
a110: 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 5, 0,. 1, 2,
a120: 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 6, 2, 3, 0, 1, 0
a130: 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c , 2, 0, 2, 0, 0,
a140: 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 0, 0, 0,. 0,
a150: 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 0, 1, 2, 3, 0,
a160: 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 1, 2, 0, 0, 2, 2
a170: 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 , 4, 5, 5, 0,.
a180: 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 1, 2, 6, 2, 3,
a190: 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 0, 1, 0, 2, 0,
a1a0: 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2, 0, 0, 0, 0, 0
a1b0: 2c 0a 20 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 ,. };. assert(
a1c0: 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49 argc==1 );. zI
a1d0: 6e 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 n = (u8*)sqlite3
a1e0: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
a1f0: 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d [0]);. if( zIn=
a200: 3d 30 20 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29 =0 ) zIn = (u8*)
a210: 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a "";. for(i=0; z
a220: 49 6e 5b 69 5d 20 26 26 20 21 73 71 6c 69 74 65 In[i] && !sqlite
a230: 33 49 73 61 6c 70 68 61 28 7a 49 6e 5b 69 5d 29 3Isalpha(zIn[i])
a240: 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 7a ; i++){}. if( z
a250: 49 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75 38 20 In[i] ){. u8
a260: 70 72 65 76 63 6f 64 65 20 3d 20 69 43 6f 64 65 prevcode = iCode
a270: 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 [zIn[i]&0x7f];.
a280: 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 zResult[0] =
a290: 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 7a sqlite3Toupper(z
a2a0: 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28 In[i]);. for(
a2b0: 6a 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b j=1; j<4 && zIn[
a2c0: 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 i]; i++){.
a2d0: 69 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 65 int code = iCode
a2e0: 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 [zIn[i]&0x7f];.
a2f0: 20 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 20 if( code>0
a300: 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 ){. if( c
a310: 6f 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 7b ode!=prevcode ){
a320: 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 76 63 . prevc
a330: 6f 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20 ode = code;.
a340: 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b zResult[j+
a350: 2b 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b +] = code + '0';
a360: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a370: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a380: 70 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20 prevcode = 0;.
a390: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
a3a0: 77 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20 while( j<4 ){.
a3b0: 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d zResult[j++]
a3c0: 20 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20 = '0';. }.
a3d0: 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30 zResult[j] = 0
a3e0: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 ;. sqlite3_re
a3f0: 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
a400: 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53 t, zResult, 4, S
a410: 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
a420: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
a430: 2a 20 49 4d 50 3a 20 52 2d 36 34 38 39 34 2d 35 * IMP: R-64894-5
a440: 30 33 32 31 20 54 68 65 20 73 74 72 69 6e 67 20 0321 The string
a450: 22 3f 30 30 30 22 20 69 73 20 72 65 74 75 72 6e "?000" is return
a460: 65 64 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 ed if the argume
a470: 6e 74 0a 20 20 20 20 2a 2a 20 69 73 20 4e 55 4c nt. ** is NUL
a480: 4c 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f L or contains no
a490: 20 41 53 43 49 49 20 61 6c 70 68 61 62 65 74 69 ASCII alphabeti
a4a0: 63 20 63 68 61 72 61 63 74 65 72 73 2e 20 2a 2f c characters. */
a4b0: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 . sqlite3_res
a4c0: 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
a4d0: 2c 20 22 3f 30 30 30 22 2c 20 34 2c 20 53 51 4c , "?000", 4, SQL
a4e0: 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d ITE_STATIC);. }
a4f0: 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
a500: 49 54 45 5f 53 4f 55 4e 44 45 58 20 2a 2f 0a 0a ITE_SOUNDEX */..
a510: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
a520: 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
a530: 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 74 ON./*.** A funct
a540: 69 6f 6e 20 74 68 61 74 20 6c 6f 61 64 73 20 61 ion that loads a
a550: 20 73 68 61 72 65 64 2d 6c 69 62 72 61 72 79 20 shared-library
a560: 65 78 74 65 6e 73 69 6f 6e 20 74 68 65 6e 20 72 extension then r
a570: 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a eturns NULL..*/.
a580: 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 64 static void load
a590: 45 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 Ext(sqlite3_cont
a5a0: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e ext *context, in
a5b0: 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f t argc, sqlite3_
a5c0: 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 value **argv){.
a5d0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
a5e0: 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 le = (const char
a5f0: 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 *)sqlite3_value
a600: 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a _text(argv[0]);.
a610: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 const char *zP
a620: 72 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a roc;. sqlite3 *
a630: 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
a640: 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
a650: 6f 6e 74 65 78 74 29 3b 0a 20 20 63 68 61 72 20 ontext);. char
a660: 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 *zErrMsg = 0;..
a670: 20 2f 2a 20 44 69 73 61 6c 6c 6f 77 20 74 68 65 /* Disallow the
a680: 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 load_extension(
a690: 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 75 ) SQL function u
a6a0: 6e 6c 65 73 73 20 74 68 65 20 53 51 4c 49 54 45 nless the SQLITE
a6b0: 5f 4c 6f 61 64 45 78 74 46 75 6e 63 0a 20 20 2a _LoadExtFunc. *
a6c0: 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 * flag is set.
a6d0: 53 65 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f See the sqlite3_
a6e0: 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 enable_load_exte
a6f0: 6e 73 69 6f 6e 28 29 20 41 50 49 2e 0a 20 20 2a nsion() API.. *
a700: 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 /. if( (db->fla
a710: 67 73 20 26 20 53 51 4c 49 54 45 5f 4c 6f 61 64 gs & SQLITE_Load
a720: 45 78 74 46 75 6e 63 29 3d 3d 30 20 29 7b 0a 20 ExtFunc)==0 ){.
a730: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
a740: 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c t_error(context,
a750: 20 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 "not authorized
a760: 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 ", -1);. retu
a770: 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 61 rn;. }.. if( a
a780: 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 7a 50 rgc==2 ){. zP
a790: 72 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 roc = (const cha
a7a0: 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 r *)sqlite3_valu
a7b0: 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b e_text(argv[1]);
a7c0: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 50 . }else{. zP
a7d0: 72 6f 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 roc = 0;. }. i
a7e0: 66 28 20 7a 46 69 6c 65 20 26 26 20 73 71 6c 69 f( zFile && sqli
a7f0: 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 te3_load_extensi
a800: 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a 50 on(db, zFile, zP
a810: 72 6f 63 2c 20 26 7a 45 72 72 4d 73 67 29 20 29 roc, &zErrMsg) )
a820: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 {. sqlite3_re
a830: 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 sult_error(conte
a840: 78 74 2c 20 7a 45 72 72 4d 73 67 2c 20 2d 31 29 xt, zErrMsg, -1)
a850: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
a860: 65 65 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d ee(zErrMsg);. }
a870: 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a .}.#endif.../*.*
a880: 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
a890: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
a8a0: 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 74 tructure holds t
a8b0: 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 61 0a he context of a.
a8c0: 2a 2a 20 73 75 6d 28 29 20 6f 72 20 61 76 67 28 ** sum() or avg(
a8d0: 29 20 61 67 67 72 65 67 61 74 65 20 63 6f 6d 70 ) aggregate comp
a8e0: 75 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 utation..*/.type
a8f0: 64 65 66 20 73 74 72 75 63 74 20 53 75 6d 43 74 def struct SumCt
a900: 78 20 53 75 6d 43 74 78 3b 0a 73 74 72 75 63 74 x SumCtx;.struct
a910: 20 53 75 6d 43 74 78 20 7b 0a 20 20 64 6f 75 62 SumCtx {. doub
a920: 6c 65 20 72 53 75 6d 3b 20 20 20 20 20 20 2f 2a le rSum; /*
a930: 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 Floating point
a940: 73 75 6d 20 2a 2f 0a 20 20 69 36 34 20 69 53 75 sum */. i64 iSu
a950: 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e m; /* In
a960: 74 65 67 65 72 20 73 75 6d 20 2a 2f 20 20 20 0a teger sum */ .
a970: 20 20 69 36 34 20 63 6e 74 3b 20 20 20 20 20 20 i64 cnt;
a980: 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
a990: 20 65 6c 65 6d 65 6e 74 73 20 73 75 6d 6d 65 64 elements summed
a9a0: 20 2a 2f 0a 20 20 75 38 20 6f 76 65 72 66 6c 6f */. u8 overflo
a9b0: 77 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 w; /* True
a9c0: 69 66 20 69 6e 74 65 67 65 72 20 6f 76 65 72 66 if integer overf
a9d0: 6c 6f 77 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 low seen */. u8
a9e0: 20 61 70 70 72 6f 78 3b 20 20 20 20 20 20 20 20 approx;
a9f0: 2f 2a 20 54 72 75 65 20 69 66 20 6e 6f 6e 2d 69 /* True if non-i
aa00: 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 61 73 nteger value was
aa10: 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20 73 75 input to the su
aa20: 6d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 m */.};../*.** R
aa30: 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 outines used to
aa40: 63 6f 6d 70 75 74 65 20 74 68 65 20 73 75 6d 2c compute the sum,
aa50: 20 61 76 65 72 61 67 65 2c 20 61 6e 64 20 74 6f average, and to
aa60: 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 tal..**.** The S
aa70: 55 4d 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f UM() function fo
aa80: 6c 6c 6f 77 73 20 74 68 65 20 28 62 72 6f 6b 65 llows the (broke
aa90: 6e 29 20 53 51 4c 20 73 74 61 6e 64 61 72 64 20 n) SQL standard
aaa0: 77 68 69 63 68 20 6d 65 61 6e 73 0a 2a 2a 20 74 which means.** t
aab0: 68 61 74 20 69 74 20 72 65 74 75 72 6e 73 20 4e hat it returns N
aac0: 55 4c 4c 20 69 66 20 69 74 20 73 75 6d 73 20 6f ULL if it sums o
aad0: 76 65 72 20 6e 6f 20 69 6e 70 75 74 73 2e 20 20 ver no inputs.
aae0: 54 4f 54 41 4c 20 72 65 74 75 72 6e 73 0a 2a 2a TOTAL returns.**
aaf0: 20 30 2e 30 20 69 6e 20 74 68 61 74 20 63 61 73 0.0 in that cas
ab00: 65 2e 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c e. In addition,
ab10: 20 54 4f 54 41 4c 20 61 6c 77 61 79 73 20 72 65 TOTAL always re
ab20: 74 75 72 6e 73 20 61 20 66 6c 6f 61 74 20 77 68 turns a float wh
ab30: 65 72 65 0a 2a 2a 20 53 55 4d 20 6d 69 67 68 74 ere.** SUM might
ab40: 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 return an integ
ab50: 65 72 20 69 66 20 69 74 20 6e 65 76 65 72 20 65 er if it never e
ab60: 6e 63 6f 75 6e 74 65 72 73 20 61 20 66 6c 6f 61 ncounters a floa
ab70: 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 ting point.** va
ab80: 6c 75 65 2e 20 20 54 4f 54 41 4c 20 6e 65 76 65 lue. TOTAL neve
ab90: 72 20 66 61 69 6c 73 2c 20 62 75 74 20 53 55 4d r fails, but SUM
aba0: 20 6d 69 67 68 74 20 74 68 72 6f 75 67 68 20 61 might through a
abb0: 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 66 0a 2a n exception if.*
abc0: 2a 20 69 74 20 6f 76 65 72 66 6c 6f 77 73 20 61 * it overflows a
abd0: 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 73 74 n integer..*/.st
abe0: 61 74 69 63 20 76 6f 69 64 20 73 75 6d 53 74 65 atic void sumSte
abf0: 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 p(sqlite3_contex
ac00: 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 t *context, int
ac10: 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 argc, sqlite3_va
ac20: 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 53 lue **argv){. S
ac30: 75 6d 43 74 78 20 2a 70 3b 0a 20 20 69 6e 74 20 umCtx *p;. int
ac40: 74 79 70 65 3b 0a 20 20 61 73 73 65 72 74 28 20 type;. assert(
ac50: 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 argc==1 );. UNU
ac60: 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 SED_PARAMETER(ar
ac70: 67 63 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 gc);. p = sqlit
ac80: 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e e3_aggregate_con
ac90: 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 text(context, si
aca0: 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 74 79 70 zeof(*p));. typ
acb0: 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 e = sqlite3_valu
acc0: 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 61 e_numeric_type(a
acd0: 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 70 rgv[0]);. if( p
ace0: 20 26 26 20 74 79 70 65 21 3d 53 51 4c 49 54 45 && type!=SQLITE
acf0: 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 2d 3e _NULL ){. p->
ad00: 63 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20 74 cnt++;. if( t
ad10: 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 ype==SQLITE_INTE
ad20: 47 45 52 20 29 7b 0a 20 20 20 20 20 20 69 36 34 GER ){. i64
ad30: 20 76 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c v = sqlite3_val
ad40: 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d ue_int64(argv[0]
ad50: 29 3b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75 6d );. p->rSum
ad60: 20 2b 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 += v;. if(
ad70: 20 28 70 2d 3e 61 70 70 72 6f 78 7c 70 2d 3e 6f (p->approx|p->o
ad80: 76 65 72 66 6c 6f 77 29 3d 3d 30 20 26 26 20 73 verflow)==0 && s
ad90: 71 6c 69 74 65 33 41 64 64 49 6e 74 36 34 28 26 qlite3AddInt64(&
ada0: 70 2d 3e 69 53 75 6d 2c 20 76 29 20 29 7b 0a 20 p->iSum, v) ){.
adb0: 20 20 20 20 20 20 20 70 2d 3e 6f 76 65 72 66 6c p->overfl
adc0: 6f 77 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a ow = 1;. }.
add0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
ade0: 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69 p->rSum += sqli
adf0: 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 te3_value_double
ae00: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 (argv[0]);.
ae10: 20 70 2d 3e 61 70 70 72 6f 78 20 3d 20 31 3b 0a p->approx = 1;.
ae20: 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 }. }.}.stat
ae30: 69 63 20 76 6f 69 64 20 73 75 6d 46 69 6e 61 6c ic void sumFinal
ae40: 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ize(sqlite3_cont
ae50: 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 ext *context){.
ae60: 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 SumCtx *p;. p
ae70: 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 = sqlite3_aggreg
ae80: 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 ate_context(cont
ae90: 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 ext, 0);. if( p
aea0: 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a && p->cnt>0 ){.
aeb0: 20 20 20 20 69 66 28 20 70 2d 3e 6f 76 65 72 66 if( p->overf
aec0: 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c low ){. sql
aed0: 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
aee0: 72 28 63 6f 6e 74 65 78 74 2c 22 69 6e 74 65 67 r(context,"integ
aef0: 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c 2d 31 29 er overflow",-1)
af00: 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
af10: 70 2d 3e 61 70 70 72 6f 78 20 29 7b 0a 20 20 20 p->approx ){.
af20: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
af30: 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 t_double(context
af40: 2c 20 70 2d 3e 72 53 75 6d 29 3b 0a 20 20 20 20 , p->rSum);.
af50: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
af60: 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 ite3_result_int6
af70: 34 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53 4(context, p->iS
af80: 75 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d um);. }. }.}
af90: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 76 67 .static void avg
afa0: 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 Finalize(sqlite3
afb0: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
afc0: 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b t){. SumCtx *p;
afd0: 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 . p = sqlite3_a
afe0: 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 ggregate_context
aff0: 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 (context, 0);.
b000: 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e if( p && p->cnt>
b010: 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
b020: 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 _result_double(c
b030: 6f 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 2f ontext, p->rSum/
b040: 28 64 6f 75 62 6c 65 29 70 2d 3e 63 6e 74 29 3b (double)p->cnt);
b050: 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f . }.}.static vo
b060: 69 64 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 id totalFinalize
b070: 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
b080: 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 *context){. Su
b090: 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 mCtx *p;. p = s
b0a0: 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 qlite3_aggregate
b0b0: 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 _context(context
b0c0: 2c 20 30 29 3b 0a 20 20 2f 2a 20 28 64 6f 75 62 , 0);. /* (doub
b0d0: 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 le)0 In case of
b0e0: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
b0f0: 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f TING_POINT... */
b100: 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c . sqlite3_resul
b110: 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 t_double(context
b120: 2c 20 70 20 3f 20 70 2d 3e 72 53 75 6d 20 3a 20 , p ? p->rSum :
b130: 28 64 6f 75 62 6c 65 29 30 29 3b 0a 7d 0a 0a 2f (double)0);.}../
b140: 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
b150: 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 ng structure kee
b160: 70 73 20 74 72 61 63 6b 20 6f 66 20 73 74 61 74 ps track of stat
b170: 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f e information fo
b180: 72 20 74 68 65 0a 2a 2a 20 63 6f 75 6e 74 28 29 r the.** count()
b190: 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
b1a0: 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ion..*/.typedef
b1b0: 73 74 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20 struct CountCtx
b1c0: 43 6f 75 6e 74 43 74 78 3b 0a 73 74 72 75 63 74 CountCtx;.struct
b1d0: 20 43 6f 75 6e 74 43 74 78 20 7b 0a 20 20 69 36 CountCtx {. i6
b1e0: 34 20 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 4 n;.};../*.** R
b1f0: 6f 75 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 outines to imple
b200: 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 28 29 ment the count()
b210: 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
b220: 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ion..*/.static v
b230: 6f 69 64 20 63 6f 75 6e 74 53 74 65 70 28 73 71 oid countStep(sq
b240: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
b250: 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 ontext, int argc
b260: 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 , sqlite3_value
b270: 2a 2a 61 72 67 76 29 7b 0a 20 20 43 6f 75 6e 74 **argv){. Count
b280: 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 Ctx *p;. p = sq
b290: 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
b2a0: 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c context(context,
b2b0: 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 sizeof(*p));.
b2c0: 69 66 28 20 28 61 72 67 63 3d 3d 30 20 7c 7c 20 if( (argc==0 ||
b2d0: 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c SQLITE_NULL!=sql
b2e0: 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
b2f0: 61 72 67 76 5b 30 5d 29 29 20 26 26 20 70 20 29 argv[0])) && p )
b300: 7b 0a 20 20 20 20 70 2d 3e 6e 2b 2b 3b 0a 20 20 {. p->n++;.
b310: 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
b320: 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 E_OMIT_DEPRECATE
b330: 44 0a 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 D. /* The sqlit
b340: 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 e3_aggregate_cou
b350: 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 nt() function is
b360: 20 64 65 70 72 65 63 61 74 65 64 2e 20 20 42 75 deprecated. Bu
b370: 74 20 6a 75 73 74 20 74 6f 20 6d 61 6b 65 0a 20 t just to make.
b380: 20 2a 2a 20 73 75 72 65 20 69 74 20 73 74 69 6c ** sure it stil
b390: 6c 20 6f 70 65 72 61 74 65 73 20 63 6f 72 72 65 l operates corre
b3a0: 63 74 6c 79 2c 20 76 65 72 69 66 79 20 74 68 61 ctly, verify tha
b3b0: 74 20 69 74 73 20 63 6f 75 6e 74 20 61 67 72 65 t its count agre
b3c0: 65 73 20 77 69 74 68 20 6f 75 72 20 0a 20 20 2a es with our . *
b3d0: 2a 20 69 6e 74 65 72 6e 61 6c 20 63 6f 75 6e 74 * internal count
b3e0: 20 77 68 65 6e 20 75 73 69 6e 67 20 63 6f 75 6e when using coun
b3f0: 74 28 2a 29 20 61 6e 64 20 77 68 65 6e 20 74 68 t(*) and when th
b400: 65 20 74 6f 74 61 6c 20 63 6f 75 6e 74 20 63 61 e total count ca
b410: 6e 20 62 65 0a 20 20 2a 2a 20 65 78 70 72 65 73 n be. ** expres
b420: 73 65 64 20 61 73 20 61 20 33 32 2d 62 69 74 20 sed as a 32-bit
b430: 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 61 73 integer. */. as
b440: 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c sert( argc==1 ||
b450: 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 3e 30 78 p==0 || p->n>0x
b460: 37 66 66 66 66 66 66 66 0a 20 20 20 20 20 20 20 7fffffff.
b470: 20 20 20 7c 7c 20 70 2d 3e 6e 3d 3d 73 71 6c 69 || p->n==sqli
b480: 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
b490: 75 6e 74 28 63 6f 6e 74 65 78 74 29 20 29 3b 0a unt(context) );.
b4a0: 23 65 6e 64 69 66 0a 7d 20 20 20 0a 73 74 61 74 #endif.} .stat
b4b0: 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 46 69 6e ic void countFin
b4c0: 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f alize(sqlite3_co
b4d0: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b ntext *context){
b4e0: 0a 20 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a . CountCtx *p;.
b4f0: 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 p = sqlite3_ag
b500: 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
b510: 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 73 context, 0);. s
b520: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
b530: 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f t64(context, p ?
b540: 20 70 2d 3e 6e 20 3a 20 30 29 3b 0a 7d 0a 0a 2f p->n : 0);.}../
b550: 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f *.** Routines to
b560: 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 69 6e 28 29 implement min()
b570: 20 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72 65 and max() aggre
b580: 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a gate functions..
b590: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
b5a0: 69 6e 6d 61 78 53 74 65 70 28 0a 20 20 73 71 6c inmaxStep(. sql
b5b0: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
b5c0: 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f ntext, . int No
b5d0: 74 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 tUsed, . sqlite
b5e0: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
b5f0: 7b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 20 20 3d {. Mem *pArg =
b600: 20 28 4d 65 6d 20 2a 29 61 72 67 76 5b 30 5d 3b (Mem *)argv[0];
b610: 0a 20 20 4d 65 6d 20 2a 70 42 65 73 74 3b 0a 20 . Mem *pBest;.
b620: 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
b630: 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 70 R(NotUsed);.. p
b640: 42 65 73 74 20 3d 20 28 4d 65 6d 20 2a 29 73 71 Best = (Mem *)sq
b650: 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
b660: 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c context(context,
b670: 20 73 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 29 sizeof(*pBest))
b680: 3b 0a 20 20 69 66 28 20 21 70 42 65 73 74 20 29 ;. if( !pBest )
b690: 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 return;.. if(
b6a0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
b6b0: 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c pe(argv[0])==SQL
b6c0: 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 ITE_NULL ){.
b6d0: 69 66 28 20 70 42 65 73 74 2d 3e 66 6c 61 67 73 if( pBest->flags
b6e0: 20 29 20 73 71 6c 69 74 65 33 53 6b 69 70 41 63 ) sqlite3SkipAc
b6f0: 63 75 6d 75 6c 61 74 6f 72 4c 6f 61 64 28 63 6f cumulatorLoad(co
b700: 6e 74 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65 20 ntext);. }else
b710: 69 66 28 20 70 42 65 73 74 2d 3e 66 6c 61 67 73 if( pBest->flags
b720: 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 61 78 3b ){. int max;
b730: 0a 20 20 20 20 69 6e 74 20 63 6d 70 3b 0a 20 20 . int cmp;.
b740: 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
b750: 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e = sqlite3GetFun
b760: 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 cCollSeq(context
b770: 29 3b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 73 );. /* This s
b780: 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 tep function is
b790: 75 73 65 64 20 66 6f 72 20 62 6f 74 68 20 74 68 used for both th
b7a0: 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 e min() and max(
b7b0: 29 20 61 67 67 72 65 67 61 74 65 73 2c 0a 20 20 ) aggregates,.
b7c0: 20 20 2a 2a 20 74 68 65 20 6f 6e 6c 79 20 64 69 ** the only di
b7d0: 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e fference between
b7e0: 20 74 68 65 20 74 77 6f 20 62 65 69 6e 67 20 74 the two being t
b7f0: 68 61 74 20 74 68 65 20 73 65 6e 73 65 20 6f 66 hat the sense of
b800: 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 the. ** comp
b810: 61 72 69 73 6f 6e 20 69 73 20 69 6e 76 65 72 74 arison is invert
b820: 65 64 2e 20 46 6f 72 20 74 68 65 20 6d 61 78 28 ed. For the max(
b830: 29 20 61 67 67 72 65 67 61 74 65 2c 20 74 68 65 ) aggregate, the
b840: 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f . ** sqlite3_
b850: 75 73 65 72 5f 64 61 74 61 28 29 20 66 75 6e 63 user_data() func
b860: 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 28 76 6f tion returns (vo
b870: 69 64 20 2a 29 2d 31 2e 20 46 6f 72 20 6d 69 6e id *)-1. For min
b880: 28 29 20 69 74 0a 20 20 20 20 2a 2a 20 72 65 74 () it. ** ret
b890: 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 64 62 2c urns (void *)db,
b8a0: 20 77 68 65 72 65 20 64 62 20 69 73 20 74 68 65 where db is the
b8b0: 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 sqlite3* databa
b8c0: 73 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20 20 20 se pointer..
b8d0: 2a 2a 20 54 68 65 72 65 66 6f 72 65 20 74 68 65 ** Therefore the
b8e0: 20 6e 65 78 74 20 73 74 61 74 65 6d 65 6e 74 20 next statement
b8f0: 73 65 74 73 20 76 61 72 69 61 62 6c 65 20 27 6d sets variable 'm
b900: 61 78 27 20 74 6f 20 31 20 66 6f 72 20 74 68 65 ax' to 1 for the
b910: 20 6d 61 78 28 29 0a 20 20 20 20 2a 2a 20 61 67 max(). ** ag
b920: 67 72 65 67 61 74 65 2c 20 6f 72 20 30 20 66 6f gregate, or 0 fo
b930: 72 20 6d 69 6e 28 29 2e 0a 20 20 20 20 2a 2f 0a r min().. */.
b940: 20 20 20 20 6d 61 78 20 3d 20 73 71 6c 69 74 65 max = sqlite
b950: 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 3_user_data(cont
b960: 65 78 74 29 21 3d 30 3b 0a 20 20 20 20 63 6d 70 ext)!=0;. cmp
b970: 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d = sqlite3MemCom
b980: 70 61 72 65 28 70 42 65 73 74 2c 20 70 41 72 67 pare(pBest, pArg
b990: 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 , pColl);. if
b9a0: 28 20 28 6d 61 78 20 26 26 20 63 6d 70 3c 30 29 ( (max && cmp<0)
b9b0: 20 7c 7c 20 28 21 6d 61 78 20 26 26 20 63 6d 70 || (!max && cmp
b9c0: 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c >0) ){. sql
b9d0: 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 ite3VdbeMemCopy(
b9e0: 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 pBest, pArg);.
b9f0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 }else{. s
ba00: 71 6c 69 74 65 33 53 6b 69 70 41 63 63 75 6d 75 qlite3SkipAccumu
ba10: 6c 61 74 6f 72 4c 6f 61 64 28 63 6f 6e 74 65 78 latorLoad(contex
ba20: 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 t);. }. }els
ba30: 65 7b 0a 20 20 20 20 70 42 65 73 74 2d 3e 64 62 e{. pBest->db
ba40: 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 = sqlite3_conte
ba50: 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e xt_db_handle(con
ba60: 74 65 78 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 text);. sqlit
ba70: 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42 e3VdbeMemCopy(pB
ba80: 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a est, pArg);. }.
ba90: 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 }.static void mi
baa0: 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 73 71 6c nMaxFinalize(sql
bab0: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
bac0: 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c 69 74 65 ntext){. sqlite
bad0: 33 5f 76 61 6c 75 65 20 2a 70 52 65 73 3b 0a 20 3_value *pRes;.
bae0: 20 70 52 65 73 20 3d 20 28 73 71 6c 69 74 65 33 pRes = (sqlite3
baf0: 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69 74 65 33 _value *)sqlite3
bb00: 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 _aggregate_conte
bb10: 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a xt(context, 0);.
bb20: 20 20 69 66 28 20 70 52 65 73 20 29 7b 0a 20 20 if( pRes ){.
bb30: 20 20 69 66 28 20 70 52 65 73 2d 3e 66 6c 61 67 if( pRes->flag
bb40: 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 s ){. sqlit
bb50: 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 e3_result_value(
bb60: 63 6f 6e 74 65 78 74 2c 20 70 52 65 73 29 3b 0a context, pRes);.
bb70: 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
bb80: 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
bb90: 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a pRes);. }.}../*
bba0: 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74 .** group_concat
bbb0: 28 45 58 50 52 2c 20 3f 53 45 50 41 52 41 54 4f (EXPR, ?SEPARATO
bbc0: 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f R?).*/.static vo
bbd0: 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 id groupConcatSt
bbe0: 65 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f ep(. sqlite3_co
bbf0: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
bc00: 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
bc10: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
bc20: 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 gv.){. const ch
bc30: 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53 74 72 41 ar *zVal;. StrA
bc40: 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20 ccum *pAccum;.
bc50: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70 const char *zSep
bc60: 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 6e 53 ;. int nVal, nS
bc70: 65 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 ep;. assert( ar
bc80: 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 gc==1 || argc==2
bc90: 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 );. if( sqlite
bca0: 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
bcb0: 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 v[0])==SQLITE_NU
bcc0: 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 LL ) return;. p
bcd0: 41 63 63 75 6d 20 3d 20 28 53 74 72 41 63 63 75 Accum = (StrAccu
bce0: 6d 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 m*)sqlite3_aggre
bcf0: 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e gate_context(con
bd00: 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 41 text, sizeof(*pA
bd10: 63 63 75 6d 29 29 3b 0a 0a 20 20 69 66 28 20 70 ccum));.. if( p
bd20: 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 73 71 6c Accum ){. sql
bd30: 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 ite3 *db = sqlit
bd40: 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
bd50: 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 ndle(context);.
bd60: 20 20 20 69 6e 74 20 66 69 72 73 74 54 65 72 6d int firstTerm
bd70: 20 3d 20 70 41 63 63 75 6d 2d 3e 6d 78 41 6c 6c = pAccum->mxAll
bd80: 6f 63 3d 3d 30 3b 0a 20 20 20 20 70 41 63 63 75 oc==0;. pAccu
bd90: 6d 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 64 62 2d m->mxAlloc = db-
bda0: 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
bdb0: 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 IMIT_LENGTH];.
bdc0: 20 20 69 66 28 20 21 66 69 72 73 74 54 65 72 6d if( !firstTerm
bdd0: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72 ){. if( ar
bde0: 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 gc==2 ){.
bdf0: 20 7a 53 65 70 20 3d 20 28 63 68 61 72 2a 29 73 zSep = (char*)s
be00: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
be10: 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 t(argv[1]);.
be20: 20 20 20 20 6e 53 65 70 20 3d 20 73 71 6c 69 74 nSep = sqlit
be30: 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
be40: 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d rgv[1]);. }
be50: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 53 else{. zS
be60: 65 70 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20 ep = ",";.
be70: 20 20 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 20 nSep = 1;.
be80: 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a 53 }. if( zS
be90: 65 70 20 29 20 73 71 6c 69 74 65 33 53 74 72 41 ep ) sqlite3StrA
bea0: 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 ccumAppend(pAccu
beb0: 6d 2c 20 7a 53 65 70 2c 20 6e 53 65 70 29 3b 0a m, zSep, nSep);.
bec0: 20 20 20 20 7d 0a 20 20 20 20 7a 56 61 6c 20 3d }. zVal =
bed0: 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f (char*)sqlite3_
bee0: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
bef0: 30 5d 29 3b 0a 20 20 20 20 6e 56 61 6c 20 3d 20 0]);. nVal =
bf00: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 sqlite3_value_by
bf10: 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 tes(argv[0]);.
bf20: 20 20 69 66 28 20 7a 56 61 6c 20 29 20 73 71 6c if( zVal ) sql
bf30: 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
bf40: 6e 64 28 70 41 63 63 75 6d 2c 20 7a 56 61 6c 2c nd(pAccum, zVal,
bf50: 20 6e 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 73 74 nVal);. }.}.st
bf60: 61 74 69 63 20 76 6f 69 64 20 67 72 6f 75 70 43 atic void groupC
bf70: 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65 28 73 71 oncatFinalize(sq
bf80: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
bf90: 6f 6e 74 65 78 74 29 7b 0a 20 20 53 74 72 41 63 ontext){. StrAc
bfa0: 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20 70 cum *pAccum;. p
bfb0: 41 63 63 75 6d 20 3d 20 73 71 6c 69 74 65 33 5f Accum = sqlite3_
bfc0: 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 aggregate_contex
bfd0: 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 t(context, 0);.
bfe0: 20 69 66 28 20 70 41 63 63 75 6d 20 29 7b 0a 20 if( pAccum ){.
bff0: 20 20 20 69 66 28 20 70 41 63 63 75 6d 2d 3e 61 if( pAccum->a
c000: 63 63 45 72 72 6f 72 3d 3d 53 54 52 41 43 43 55 ccError==STRACCU
c010: 4d 5f 54 4f 4f 42 49 47 20 29 7b 0a 20 20 20 20 M_TOOBIG ){.
c020: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
c030: 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f _error_toobig(co
c040: 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 ntext);. }els
c050: 65 20 69 66 28 20 70 41 63 63 75 6d 2d 3e 61 63 e if( pAccum->ac
c060: 63 45 72 72 6f 72 3d 3d 53 54 52 41 43 43 55 4d cError==STRACCUM
c070: 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 _NOMEM ){.
c080: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
c090: 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 rror_nomem(conte
c0a0: 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 20 xt);. }else{
c0b0: 20 20 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 . sqlite
c0c0: 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
c0d0: 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 53 74 ntext, sqlite3St
c0e0: 72 41 63 63 75 6d 46 69 6e 69 73 68 28 70 41 63 rAccumFinish(pAc
c0f0: 63 75 6d 29 2c 20 2d 31 2c 20 0a 20 20 20 20 20 cum), -1, .
c100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c110: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
c120: 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a e);. }. }.}.
c130: 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
c140: 69 6e 65 20 64 6f 65 73 20 70 65 72 2d 63 6f 6e ine does per-con
c150: 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e nection function
c160: 20 72 65 67 69 73 74 72 61 74 69 6f 6e 2e 20 20 registration.
c170: 4d 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65 20 62 Most.** of the b
c180: 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e uilt-in function
c190: 73 20 61 62 6f 76 65 20 61 72 65 20 70 61 72 74 s above are part
c1a0: 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 of the global f
c1b0: 75 6e 63 74 69 6f 6e 20 73 65 74 2e 0a 2a 2a 20 unction set..**
c1c0: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c This routine onl
c1d0: 79 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 6f y deals with tho
c1e0: 73 65 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 se that are not
c1f0: 67 6c 6f 62 61 6c 2e 0a 2a 2f 0a 76 6f 69 64 20 global..*/.void
c200: 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 50 sqlite3RegisterP
c210: 65 72 43 6f 6e 6e 65 63 74 69 6f 6e 42 75 69 6c erConnectionBuil
c220: 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c tinFunctions(sql
c230: 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 ite3 *db){. int
c240: 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 rc = sqlite3_ov
c250: 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 erload_function(
c260: 64 62 2c 20 22 4d 41 54 43 48 22 2c 20 32 29 3b db, "MATCH", 2);
c270: 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
c280: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 QLITE_NOMEM || r
c290: 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c==SQLITE_OK );.
c2a0: 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
c2b0: 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 _NOMEM ){. sq
c2c0: 6c 69 74 65 33 4f 6f 6d 46 61 75 6c 74 28 64 62 lite3OomFault(db
c2d0: 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
c2e0: 53 65 74 20 74 68 65 20 4c 49 4b 45 4f 50 54 20 Set the LIKEOPT
c2f0: 66 6c 61 67 20 6f 6e 20 74 68 65 20 32 2d 61 72 flag on the 2-ar
c300: 67 75 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e 20 gument function
c310: 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e with the given n
c320: 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ame..*/.static v
c330: 6f 69 64 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c oid setLikeOptFl
c340: 61 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ag(sqlite3 *db,
c350: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
c360: 65 2c 20 75 38 20 66 6c 61 67 56 61 6c 29 7b 0a e, u8 flagVal){.
c370: 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b FuncDef *pDef;
c380: 0a 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 . pDef = sqlite
c390: 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 3FindFunction(db
c3a0: 2c 20 7a 4e 61 6d 65 2c 20 32 2c 20 53 51 4c 49 , zName, 2, SQLI
c3b0: 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 TE_UTF8, 0);. i
c3c0: 66 28 20 41 4c 57 41 59 53 28 70 44 65 66 29 20 f( ALWAYS(pDef)
c3d0: 29 7b 0a 20 20 20 20 70 44 65 66 2d 3e 66 75 6e ){. pDef->fun
c3e0: 63 46 6c 61 67 73 20 7c 3d 20 66 6c 61 67 56 61 cFlags |= flagVa
c3f0: 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 l;. }.}../*.**
c400: 52 65 67 69 73 74 65 72 20 74 68 65 20 62 75 69 Register the bui
c410: 6c 74 2d 69 6e 20 4c 49 4b 45 20 61 6e 64 20 47 lt-in LIKE and G
c420: 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 LOB functions.
c430: 54 68 65 20 63 61 73 65 53 65 6e 73 69 74 69 76 The caseSensitiv
c440: 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 64 e.** parameter d
c450: 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 etermines whethe
c460: 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 4c 49 4b r or not the LIK
c470: 45 20 6f 70 65 72 61 74 6f 72 20 69 73 20 63 61 E operator is ca
c480: 73 65 0a 2a 2a 20 73 65 6e 73 69 74 69 76 65 2e se.** sensitive.
c490: 20 20 47 4c 4f 42 20 69 73 20 61 6c 77 61 79 73 GLOB is always
c4a0: 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e case sensitive.
c4b0: 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 .*/.void sqlite3
c4c0: 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 RegisterLikeFunc
c4d0: 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 tions(sqlite3 *d
c4e0: 62 2c 20 69 6e 74 20 63 61 73 65 53 65 6e 73 69 b, int caseSensi
c4f0: 74 69 76 65 29 7b 0a 20 20 73 74 72 75 63 74 20 tive){. struct
c500: 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e compareInfo *pIn
c510: 66 6f 3b 0a 20 20 69 66 28 20 63 61 73 65 53 65 fo;. if( caseSe
c520: 6e 73 69 74 69 76 65 20 29 7b 0a 20 20 20 20 70 nsitive ){. p
c530: 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74 20 63 Info = (struct c
c540: 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b ompareInfo*)&lik
c550: 65 49 6e 66 6f 41 6c 74 3b 0a 20 20 7d 65 6c 73 eInfoAlt;. }els
c560: 65 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 28 e{. pInfo = (
c570: 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e struct compareIn
c580: 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 fo*)&likeInfoNor
c590: 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 m;. }. sqlite3
c5a0: 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 CreateFunc(db, "
c5b0: 6c 69 6b 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 like", 2, SQLITE
c5c0: 5f 55 54 46 38 2c 20 70 49 6e 66 6f 2c 20 6c 69 _UTF8, pInfo, li
c5d0: 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 30 29 keFunc, 0, 0, 0)
c5e0: 3b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 ;. sqlite3Creat
c5f0: 65 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 eFunc(db, "like"
c600: 2c 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 , 3, SQLITE_UTF8
c610: 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e , pInfo, likeFun
c620: 63 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73 c, 0, 0, 0);. s
c630: 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 qlite3CreateFunc
c640: 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20 (db, "glob", 2,
c650: 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 0a 20 20 SQLITE_UTF8, .
c660: 20 20 20 20 28 73 74 72 75 63 74 20 63 6f 6d 70 (struct comp
c670: 61 72 65 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e areInfo*)&globIn
c680: 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c fo, likeFunc, 0,
c690: 20 30 2c 20 30 29 3b 0a 20 20 73 65 74 4c 69 6b 0, 0);. setLik
c6a0: 65 4f 70 74 46 6c 61 67 28 64 62 2c 20 22 67 6c eOptFlag(db, "gl
c6b0: 6f 62 22 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 ob", SQLITE_FUNC
c6c0: 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46 _LIKE | SQLITE_F
c6d0: 55 4e 43 5f 43 41 53 45 29 3b 0a 20 20 73 65 74 UNC_CASE);. set
c6e0: 4c 69 6b 65 4f 70 74 46 6c 61 67 28 64 62 2c 20 LikeOptFlag(db,
c6f0: 22 6c 69 6b 65 22 2c 20 0a 20 20 20 20 20 20 63 "like", . c
c700: 61 73 65 53 65 6e 73 69 74 69 76 65 20 3f 20 28 aseSensitive ? (
c710: 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 SQLITE_FUNC_LIKE
c720: 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 | SQLITE_FUNC_C
c730: 41 53 45 29 20 3a 20 53 51 4c 49 54 45 5f 46 55 ASE) : SQLITE_FU
c740: 4e 43 5f 4c 49 4b 45 29 3b 0a 7d 0a 0a 2f 2a 0a NC_LIKE);.}../*.
c750: 2a 2a 20 70 45 78 70 72 20 70 6f 69 6e 74 73 20 ** pExpr points
c760: 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e to an expression
c770: 20 77 68 69 63 68 20 69 6d 70 6c 65 6d 65 6e 74 which implement
c780: 73 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 s a function. I
c790: 66 0a 2a 2a 20 69 74 20 69 73 20 61 70 70 72 6f f.** it is appro
c7a0: 70 72 69 61 74 65 20 74 6f 20 61 70 70 6c 79 20 priate to apply
c7b0: 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a the LIKE optimiz
c7c0: 61 74 69 6f 6e 20 74 6f 20 74 68 61 74 20 66 75 ation to that fu
c7d0: 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 73 nction.** then s
c7e0: 65 74 20 61 57 63 5b 30 5d 20 74 68 72 6f 75 67 et aWc[0] throug
c7f0: 68 20 61 57 63 5b 32 5d 20 74 6f 20 74 68 65 20 h aWc[2] to the
c800: 77 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 wildcard charact
c810: 65 72 73 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 ers and.** retur
c820: 6e 20 54 52 55 45 2e 20 20 49 66 20 74 68 65 20 n TRUE. If the
c830: 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 function is not
c840: 61 20 4c 49 4b 45 2d 73 74 79 6c 65 20 66 75 6e a LIKE-style fun
c850: 63 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 72 65 ction then.** re
c860: 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a turn FALSE..**.*
c870: 2a 20 2a 70 49 73 4e 6f 63 61 73 65 20 69 73 20 * *pIsNocase is
c880: 73 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 75 set to true if u
c890: 70 70 65 72 63 61 73 65 20 61 6e 64 20 6c 6f 77 ppercase and low
c8a0: 65 72 63 61 73 65 20 61 72 65 20 65 71 75 69 76 ercase are equiv
c8b0: 61 6c 65 6e 74 20 66 6f 72 0a 2a 2a 20 74 68 65 alent for.** the
c8c0: 20 66 75 6e 63 74 69 6f 6e 20 28 64 65 66 61 75 function (defau
c8d0: 6c 74 20 66 6f 72 20 4c 49 4b 45 29 2e 20 20 49 lt for LIKE). I
c8e0: 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 6d f the function m
c8f0: 61 6b 65 73 20 74 68 65 20 64 69 73 74 69 6e 63 akes the distinc
c900: 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 tion.** between
c910: 75 70 70 65 72 63 61 73 65 20 61 6e 64 20 6c 6f uppercase and lo
c920: 77 65 72 63 61 73 65 20 28 61 73 20 64 6f 65 73 wercase (as does
c930: 20 47 4c 4f 42 29 20 74 68 65 6e 20 2a 70 49 73 GLOB) then *pIs
c940: 4e 6f 63 61 73 65 20 69 73 20 73 65 74 20 74 6f Nocase is set to
c950: 0a 2a 2a 20 66 61 6c 73 65 2e 0a 2a 2f 0a 69 6e .** false..*/.in
c960: 74 20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 t sqlite3IsLikeF
c970: 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 unction(sqlite3
c980: 2a 64 62 2c 20 45 78 70 72 20 2a 70 45 78 70 72 *db, Expr *pExpr
c990: 2c 20 69 6e 74 20 2a 70 49 73 4e 6f 63 61 73 65 , int *pIsNocase
c9a0: 2c 20 63 68 61 72 20 2a 61 57 63 29 7b 0a 20 20 , char *aWc){.
c9b0: 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 FuncDef *pDef;.
c9c0: 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d if( pExpr->op!=
c9d0: 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 0a 20 20 20 TK_FUNCTION .
c9e0: 7c 7c 20 21 70 45 78 70 72 2d 3e 78 2e 70 4c 69 || !pExpr->x.pLi
c9f0: 73 74 20 0a 20 20 20 7c 7c 20 70 45 78 70 72 2d st . || pExpr-
ca00: 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 >x.pList->nExpr!
ca10: 3d 32 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 =2. ){. retu
ca20: 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 rn 0;. }. asse
ca30: 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
ca40: 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 erty(pExpr, EP_x
ca50: 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 70 IsSelect) );. p
ca60: 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e Def = sqlite3Fin
ca70: 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 45 dFunction(db, pE
ca80: 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 32 xpr->u.zToken, 2
ca90: 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 , SQLITE_UTF8, 0
caa0: 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 );. if( NEVER(p
cab0: 44 65 66 3d 3d 30 29 20 7c 7c 20 28 70 44 65 66 Def==0) || (pDef
cac0: 2d 3e 66 75 6e 63 46 6c 61 67 73 20 26 20 53 51 ->funcFlags & SQ
cad0: 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 3d LITE_FUNC_LIKE)=
cae0: 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
caf0: 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 0;. }.. /* Th
cb00: 65 20 6d 65 6d 63 70 79 28 29 20 73 74 61 74 65 e memcpy() state
cb10: 6d 65 6e 74 20 61 73 73 75 6d 65 73 20 74 68 61 ment assumes tha
cb20: 74 20 74 68 65 20 77 69 6c 64 63 61 72 64 20 63 t the wildcard c
cb30: 68 61 72 61 63 74 65 72 73 20 61 72 65 0a 20 20 haracters are.
cb40: 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 68 72 ** the first thr
cb50: 65 65 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e ee statements in
cb60: 20 74 68 65 20 63 6f 6d 70 61 72 65 49 6e 66 6f the compareInfo
cb70: 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 structure. The
cb80: 0a 20 20 2a 2a 20 61 73 73 65 72 74 73 28 29 20 . ** asserts()
cb90: 74 68 61 74 20 66 6f 6c 6c 6f 77 20 76 65 72 69 that follow veri
cba0: 66 79 20 74 68 61 74 20 61 73 73 75 6d 70 74 69 fy that assumpti
cbb0: 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 on. */. memcpy
cbc0: 28 61 57 63 2c 20 70 44 65 66 2d 3e 70 55 73 65 (aWc, pDef->pUse
cbd0: 72 44 61 74 61 2c 20 33 29 3b 0a 20 20 61 73 73 rData, 3);. ass
cbe0: 65 72 74 28 20 28 63 68 61 72 2a 29 26 6c 69 6b ert( (char*)&lik
cbf0: 65 49 6e 66 6f 41 6c 74 20 3d 3d 20 28 63 68 61 eInfoAlt == (cha
cc00: 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e r*)&likeInfoAlt.
cc10: 6d 61 74 63 68 41 6c 6c 20 29 3b 0a 20 20 61 73 matchAll );. as
cc20: 73 65 72 74 28 20 26 28 28 63 68 61 72 2a 29 26 sert( &((char*)&
cc30: 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 31 5d 20 likeInfoAlt)[1]
cc40: 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 == (char*)&likeI
cc50: 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 4f 6e 65 20 nfoAlt.matchOne
cc60: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26 28 28 );. assert( &((
cc70: 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 char*)&likeInfoA
cc80: 6c 74 29 5b 32 5d 20 3d 3d 20 28 63 68 61 72 2a lt)[2] == (char*
cc90: 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 )&likeInfoAlt.ma
cca0: 74 63 68 53 65 74 20 29 3b 0a 20 20 2a 70 49 73 tchSet );. *pIs
ccb0: 4e 6f 63 61 73 65 20 3d 20 28 70 44 65 66 2d 3e Nocase = (pDef->
ccc0: 66 75 6e 63 46 6c 61 67 73 20 26 20 53 51 4c 49 funcFlags & SQLI
ccd0: 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3d 3d 30 TE_FUNC_CASE)==0
cce0: 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a ;. return 1;.}.
ccf0: 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 ./*.** All of th
cd00: 65 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 e FuncDef struct
cd10: 75 72 65 73 20 69 6e 20 74 68 65 20 61 42 75 69 ures in the aBui
cd20: 6c 74 69 6e 46 75 6e 63 5b 5d 20 61 72 72 61 79 ltinFunc[] array
cd30: 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 74 68 65 above.** to the
cd40: 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e global function
cd50: 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 hash table. Th
cd60: 69 73 20 6f 63 63 75 72 73 20 61 74 20 73 74 61 is occurs at sta
cd70: 72 74 2d 74 69 6d 65 20 28 61 73 0a 2a 2a 20 61 rt-time (as.** a
cd80: 20 63 6f 6e 73 65 71 75 65 6e 63 65 20 6f 66 20 consequence of
cd90: 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f calling sqlite3_
cda0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 29 2e 0a 2a initialize())..*
cdb0: 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 *.** After this
cdc0: 72 6f 75 74 69 6e 65 20 72 75 6e 73 0a 2a 2f 0a routine runs.*/.
cdd0: 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 void sqlite3Regi
cde0: 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 sterBuiltinFunct
cdf0: 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20 2f 2a ions(void){. /*
ce00: 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . ** The follow
ce10: 69 6e 67 20 61 72 72 61 79 20 68 6f 6c 64 73 20 ing array holds
ce20: 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 FuncDef structur
ce30: 65 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 es for all of th
ce40: 65 20 66 75 6e 63 74 69 6f 6e 73 0a 20 20 2a 2a e functions. **
ce50: 20 64 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73 defined in this
ce60: 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a file.. **. **
ce70: 20 54 68 65 20 61 72 72 61 79 20 63 61 6e 6e 6f The array canno
ce80: 74 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 73 69 t be constant si
ce90: 6e 63 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 nce changes are
cea0: 6d 61 64 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a made to the. **
ceb0: 20 46 75 6e 63 44 65 66 2e 70 48 61 73 68 20 65 FuncDef.pHash e
cec0: 6c 65 6d 65 6e 74 73 20 61 74 20 73 74 61 72 74 lements at start
ced0: 2d 74 69 6d 65 2e 20 20 54 68 65 20 65 6c 65 6d -time. The elem
cee0: 65 6e 74 73 20 6f 66 20 74 68 69 73 20 61 72 72 ents of this arr
cef0: 61 79 0a 20 20 2a 2a 20 61 72 65 20 72 65 61 64 ay. ** are read
cf00: 2d 6f 6e 6c 79 20 61 66 74 65 72 20 69 6e 69 74 -only after init
cf10: 69 61 6c 69 7a 61 74 69 6f 6e 20 69 73 20 63 6f ialization is co
cf20: 6d 70 6c 65 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a mplete.. **. *
cf30: 2a 20 46 6f 72 20 70 65 61 6b 20 65 66 66 69 63 * For peak effic
cf40: 69 65 6e 63 79 2c 20 70 75 74 20 74 68 65 20 6d iency, put the m
cf50: 6f 73 74 20 66 72 65 71 75 65 6e 74 6c 79 20 75 ost frequently u
cf60: 73 65 64 20 66 75 6e 63 74 69 6f 6e 20 6c 61 73 sed function las
cf70: 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 t.. */. static
cf80: 20 46 75 6e 63 44 65 66 20 61 42 75 69 6c 74 69 FuncDef aBuilti
cf90: 6e 46 75 6e 63 5b 5d 20 3d 20 7b 0a 23 69 66 64 nFunc[] = {.#ifd
cfa0: 65 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 ef SQLITE_SOUNDE
cfb0: 58 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 X. FUNCTION(s
cfc0: 6f 75 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 oundex,
cfd0: 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 1, 0, 0, soun
cfe0: 64 65 78 46 75 6e 63 20 20 20 20 20 20 29 2c 0a dexFunc ),.
cff0: 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
d000: 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f QLITE_OMIT_LOAD_
d010: 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20 20 56 46 EXTENSION. VF
d020: 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 UNCTION(load_ext
d030: 65 6e 73 69 6f 6e 2c 20 20 20 20 31 2c 20 30 2c ension, 1, 0,
d040: 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 0, loadExt
d050: 20 20 20 20 20 29 2c 0a 20 20 20 20 56 46 55 4e ),. VFUN
d060: 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e CTION(load_exten
d070: 73 69 6f 6e 2c 20 20 20 20 32 2c 20 30 2c 20 30 sion, 2, 0, 0
d080: 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20 , loadExt
d090: 20 20 20 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66 ),.#endif.#if
d0a0: 20 53 51 4c 49 54 45 5f 55 53 45 52 5f 41 55 54 SQLITE_USER_AUT
d0b0: 48 45 4e 54 49 43 41 54 49 4f 4e 0a 20 20 20 20 HENTICATION.
d0c0: 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f FUNCTION(sqlite_
d0d0: 63 72 79 70 74 2c 20 20 20 20 20 20 20 32 2c 20 crypt, 2,
d0e0: 30 2c 20 30 2c 20 73 71 6c 69 74 65 33 43 72 79 0, 0, sqlite3Cry
d0f0: 70 74 46 75 6e 63 20 29 2c 0a 23 65 6e 64 69 66 ptFunc ),.#endif
d100: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
d110: 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 OMIT_COMPILEOPTI
d120: 4f 4e 5f 44 49 41 47 53 0a 20 20 20 20 44 46 55 ON_DIAGS. DFU
d130: 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 63 6f NCTION(sqlite_co
d140: 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 mpileoption_used
d150: 2c 31 2c 20 30 2c 20 30 2c 20 63 6f 6d 70 69 6c ,1, 0, 0, compil
d160: 65 6f 70 74 69 6f 6e 75 73 65 64 46 75 6e 63 20 eoptionusedFunc
d170: 20 29 2c 0a 20 20 20 20 44 46 55 4e 43 54 49 4f ),. DFUNCTIO
d180: 4e 28 73 71 6c 69 74 65 5f 63 6f 6d 70 69 6c 65 N(sqlite_compile
d190: 6f 70 74 69 6f 6e 5f 67 65 74 2c 20 31 2c 20 30 option_get, 1, 0
d1a0: 2c 20 30 2c 20 63 6f 6d 70 69 6c 65 6f 70 74 69 , 0, compileopti
d1b0: 6f 6e 67 65 74 46 75 6e 63 20 20 29 2c 0a 23 65 ongetFunc ),.#e
d1c0: 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
d1d0: 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f MIT_COMPILEOPTIO
d1e0: 4e 5f 44 49 41 47 53 20 2a 2f 0a 20 20 20 20 46 N_DIAGS */. F
d1f0: 55 4e 43 54 49 4f 4e 32 28 75 6e 6c 69 6b 65 6c UNCTION2(unlikel
d200: 79 2c 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 y, 1, 0
d210: 2c 20 30 2c 20 6e 6f 6f 70 46 75 6e 63 2c 20 20 , 0, noopFunc,
d220: 53 51 4c 49 54 45 5f 46 55 4e 43 5f 55 4e 4c 49 SQLITE_FUNC_UNLI
d230: 4b 45 4c 59 29 2c 0a 20 20 20 20 46 55 4e 43 54 KELY),. FUNCT
d240: 49 4f 4e 32 28 6c 69 6b 65 6c 69 68 6f 6f 64 2c ION2(likelihood,
d250: 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 2, 0, 0,
d260: 20 6e 6f 6f 70 46 75 6e 63 2c 20 20 53 51 4c 49 noopFunc, SQLI
d270: 54 45 5f 46 55 4e 43 5f 55 4e 4c 49 4b 45 4c 59 TE_FUNC_UNLIKELY
d280: 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 32 ),. FUNCTION2
d290: 28 6c 69 6b 65 6c 79 2c 20 20 20 20 20 20 20 20 (likely,
d2a0: 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6e 6f 6f 1, 0, 0, noo
d2b0: 70 46 75 6e 63 2c 20 20 53 51 4c 49 54 45 5f 46 pFunc, SQLITE_F
d2c0: 55 4e 43 5f 55 4e 4c 49 4b 45 4c 59 29 2c 0a 23 UNC_UNLIKELY),.#
d2d0: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
d2e0: 55 47 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 32 UG. FUNCTION2
d2f0: 28 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 (affinity,
d300: 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6e 6f 6f 1, 0, 0, noo
d310: 70 46 75 6e 63 2c 20 20 53 51 4c 49 54 45 5f 46 pFunc, SQLITE_F
d320: 55 4e 43 5f 41 46 46 49 4e 49 54 59 29 2c 0a 23 UNC_AFFINITY),.#
d330: 65 6e 64 69 66 0a 20 20 20 20 46 55 4e 43 54 49 endif. FUNCTI
d340: 4f 4e 28 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 ON(ltrim,
d350: 20 20 20 20 20 20 20 31 2c 20 31 2c 20 30 2c 20 1, 1, 0,
d360: 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 trimFunc
d370: 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e ),. FUNCTION
d380: 28 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 (ltrim,
d390: 20 20 20 20 20 32 2c 20 31 2c 20 30 2c 20 74 72 2, 1, 0, tr
d3a0: 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 imFunc )
d3b0: 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 ,. FUNCTION(r
d3c0: 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 trim,
d3d0: 20 20 20 31 2c 20 32 2c 20 30 2c 20 74 72 69 6d 1, 2, 0, trim
d3e0: 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a Func ),.
d3f0: 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 FUNCTION(rtr
d400: 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 im,
d410: 20 32 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 2, 2, 0, trimFu
d420: 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 nc ),.
d430: 20 20 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c FUNCTION(trim,
d440: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
d450: 2c 20 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 , 3, 0, trimFunc
d460: 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 ),.
d470: 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 FUNCTION(trim,
d480: 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 2,
d490: 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 3, 0, trimFunc
d4a0: 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 ),. FU
d4b0: 4e 43 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 NCTION(min,
d4c0: 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c -1, 0,
d4d0: 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20 1, minmaxFunc
d4e0: 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
d4f0: 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 TION(min,
d500: 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 31 0, 0, 1
d510: 2c 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 , 0
d520: 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 ),. AGGREG
d530: 41 54 45 32 28 6d 69 6e 2c 20 20 20 20 20 20 20 ATE2(min,
d540: 20 20 20 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 1, 0, 1,
d550: 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20 20 20 minmaxStep,
d560: 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 2c minMaxFinalize,
d570: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
d580: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d590: 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 SQLIT
d5a0: 45 5f 46 55 4e 43 5f 4d 49 4e 4d 41 58 20 29 2c E_FUNC_MINMAX ),
d5b0: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 . FUNCTION(ma
d5c0: 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x,
d5d0: 20 2d 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 -1, 1, 1, minma
d5e0: 78 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 xFunc ),.
d5f0: 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c FUNCTION(max,
d600: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d610: 30 2c 20 31 2c 20 31 2c 20 30 20 20 20 20 20 20 0, 1, 1, 0
d620: 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 ),.
d630: 20 41 47 47 52 45 47 41 54 45 32 28 6d 61 78 2c AGGREGATE2(max,
d640: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 1,
d650: 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 1, 1, minmaxSte
d660: 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 p, minMaxFi
d670: 6e 61 6c 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 nalize,.
d680: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d690: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d6a0: 20 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4d 49 SQLITE_FUNC_MI
d6b0: 4e 4d 41 58 20 29 2c 0a 20 20 20 20 46 55 4e 43 NMAX ),. FUNC
d6c0: 54 49 4f 4e 32 28 74 79 70 65 6f 66 2c 20 20 20 TION2(typeof,
d6d0: 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
d6e0: 2c 20 74 79 70 65 6f 66 46 75 6e 63 2c 20 20 53 , typeofFunc, S
d6f0: 51 4c 49 54 45 5f 46 55 4e 43 5f 54 59 50 45 4f QLITE_FUNC_TYPEO
d700: 46 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e F),. FUNCTION
d710: 32 28 6c 65 6e 67 74 68 2c 20 20 20 20 20 20 20 2(length,
d720: 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 65 1, 0, 0, le
d730: 6e 67 74 68 46 75 6e 63 2c 20 20 53 51 4c 49 54 ngthFunc, SQLIT
d740: 45 5f 46 55 4e 43 5f 4c 45 4e 47 54 48 29 2c 0a E_FUNC_LENGTH),.
d750: 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 69 6e 73 FUNCTION(ins
d760: 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 tr,
d770: 20 32 2c 20 30 2c 20 30 2c 20 69 6e 73 74 72 46 2, 0, 0, instrF
d780: 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 unc ),.
d790: 20 20 46 55 4e 43 54 49 4f 4e 28 70 72 69 6e 74 FUNCTION(print
d7a0: 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 f, -1
d7b0: 2c 20 30 2c 20 30 2c 20 70 72 69 6e 74 66 46 75 , 0, 0, printfFu
d7c0: 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 nc ),.
d7d0: 46 55 4e 43 54 49 4f 4e 28 75 6e 69 63 6f 64 65 FUNCTION(unicode
d7e0: 2c 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 , 1,
d7f0: 30 2c 20 30 2c 20 75 6e 69 63 6f 64 65 46 75 6e 0, 0, unicodeFun
d800: 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 c ),. FU
d810: 4e 43 54 49 4f 4e 28 63 68 61 72 2c 20 20 20 20 NCTION(char,
d820: 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c -1, 0,
d830: 20 30 2c 20 63 68 61 72 46 75 6e 63 20 20 20 20 0, charFunc
d840: 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
d850: 54 49 4f 4e 28 61 62 73 2c 20 20 20 20 20 20 20 TION(abs,
d860: 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
d870: 2c 20 61 62 73 46 75 6e 63 20 20 20 20 20 20 20 , absFunc
d880: 20 20 20 29 2c 0a 23 69 66 6e 64 65 66 20 53 51 ),.#ifndef SQ
d890: 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
d8a0: 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 46 55 4e NG_POINT. FUN
d8b0: 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 CTION(round,
d8c0: 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
d8d0: 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 0, roundFunc
d8e0: 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
d8f0: 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20 20 ION(round,
d900: 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 2, 0, 0,
d910: 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 20 20 roundFunc
d920: 20 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 ),.#endif.
d930: 46 55 4e 43 54 49 4f 4e 28 75 70 70 65 72 2c 20 FUNCTION(upper,
d940: 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 1,
d950: 30 2c 20 30 2c 20 75 70 70 65 72 46 75 6e 63 20 0, 0, upperFunc
d960: 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 ),. FU
d970: 4e 43 54 49 4f 4e 28 6c 6f 77 65 72 2c 20 20 20 NCTION(lower,
d980: 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 1, 0,
d990: 20 30 2c 20 6c 6f 77 65 72 46 75 6e 63 20 20 20 0, lowerFunc
d9a0: 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
d9b0: 54 49 4f 4e 28 68 65 78 2c 20 20 20 20 20 20 20 TION(hex,
d9c0: 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
d9d0: 2c 20 68 65 78 46 75 6e 63 20 20 20 20 20 20 20 , hexFunc
d9e0: 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
d9f0: 4f 4e 32 28 69 66 6e 75 6c 6c 2c 20 20 20 20 20 ON2(ifnull,
da00: 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 2, 0, 0,
da10: 6e 6f 6f 70 46 75 6e 63 2c 20 20 53 51 4c 49 54 noopFunc, SQLIT
da20: 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 43 45 29 E_FUNC_COALESCE)
da30: 2c 0a 20 20 20 20 56 46 55 4e 43 54 49 4f 4e 28 ,. VFUNCTION(
da40: 72 61 6e 64 6f 6d 2c 20 20 20 20 20 20 20 20 20 random,
da50: 20 20 20 30 2c 20 30 2c 20 30 2c 20 72 61 6e 64 0, 0, 0, rand
da60: 6f 6d 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a omFunc ),.
da70: 20 20 20 20 56 46 55 4e 43 54 49 4f 4e 28 72 61 VFUNCTION(ra
da80: 6e 64 6f 6d 62 6c 6f 62 2c 20 20 20 20 20 20 20 ndomblob,
da90: 20 31 2c 20 30 2c 20 30 2c 20 72 61 6e 64 6f 6d 1, 0, 0, random
daa0: 42 6c 6f 62 20 20 20 20 20 20 20 29 2c 0a 20 20 Blob ),.
dab0: 20 20 46 55 4e 43 54 49 4f 4e 28 6e 75 6c 6c 69 FUNCTION(nulli
dac0: 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 32 f, 2
dad0: 2c 20 30 2c 20 31 2c 20 6e 75 6c 6c 69 66 46 75 , 0, 1, nullifFu
dae0: 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 nc ),.
daf0: 44 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 DFUNCTION(sqlite
db00: 5f 76 65 72 73 69 6f 6e 2c 20 20 20 20 30 2c 20 _version, 0,
db10: 30 2c 20 30 2c 20 76 65 72 73 69 6f 6e 46 75 6e 0, 0, versionFun
db20: 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 44 46 c ),. DF
db30: 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 73 UNCTION(sqlite_s
db40: 6f 75 72 63 65 5f 69 64 2c 20 20 30 2c 20 30 2c ource_id, 0, 0,
db50: 20 30 2c 20 73 6f 75 72 63 65 69 64 46 75 6e 63 0, sourceidFunc
db60: 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
db70: 54 49 4f 4e 28 73 71 6c 69 74 65 5f 6c 6f 67 2c TION(sqlite_log,
db80: 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2, 0, 0
db90: 2c 20 65 72 72 6c 6f 67 46 75 6e 63 20 20 20 20 , errlogFunc
dba0: 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
dbb0: 4f 4e 28 71 75 6f 74 65 2c 20 20 20 20 20 20 20 ON(quote,
dbc0: 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 1, 0, 0,
dbd0: 71 75 6f 74 65 46 75 6e 63 20 20 20 20 20 20 20 quoteFunc
dbe0: 20 29 2c 0a 20 20 20 20 56 46 55 4e 43 54 49 4f ),. VFUNCTIO
dbf0: 4e 28 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f N(last_insert_ro
dc00: 77 69 64 2c 20 30 2c 20 30 2c 20 30 2c 20 6c 61 wid, 0, 0, 0, la
dc10: 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 29 st_insert_rowid)
dc20: 2c 0a 20 20 20 20 56 46 55 4e 43 54 49 4f 4e 28 ,. VFUNCTION(
dc30: 63 68 61 6e 67 65 73 2c 20 20 20 20 20 20 20 20 changes,
dc40: 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 68 61 6e 0, 0, 0, chan
dc50: 67 65 73 20 20 20 20 20 20 20 20 20 20 29 2c 0a ges ),.
dc60: 20 20 20 20 56 46 55 4e 43 54 49 4f 4e 28 74 6f VFUNCTION(to
dc70: 74 61 6c 5f 63 68 61 6e 67 65 73 2c 20 20 20 20 tal_changes,
dc80: 20 30 2c 20 30 2c 20 30 2c 20 74 6f 74 61 6c 5f 0, 0, 0, total_
dc90: 63 68 61 6e 67 65 73 20 20 20 20 29 2c 0a 20 20 changes ),.
dca0: 20 20 46 55 4e 43 54 49 4f 4e 28 72 65 70 6c 61 FUNCTION(repla
dcb0: 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 33 ce, 3
dcc0: 2c 20 30 2c 20 30 2c 20 72 65 70 6c 61 63 65 46 , 0, 0, replaceF
dcd0: 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 unc ),.
dce0: 46 55 4e 43 54 49 4f 4e 28 7a 65 72 6f 62 6c 6f FUNCTION(zeroblo
dcf0: 62 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 b, 1,
dd00: 30 2c 20 30 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 0, 0, zeroblobFu
dd10: 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 nc ),. FU
dd20: 4e 43 54 49 4f 4e 28 73 75 62 73 74 72 2c 20 20 NCTION(substr,
dd30: 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 2, 0,
dd40: 20 30 2c 20 73 75 62 73 74 72 46 75 6e 63 20 20 0, substrFunc
dd50: 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
dd60: 54 49 4f 4e 28 73 75 62 73 74 72 2c 20 20 20 20 TION(substr,
dd70: 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30 3, 0, 0
dd80: 2c 20 73 75 62 73 74 72 46 75 6e 63 20 20 20 20 , substrFunc
dd90: 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 ),. AGGREG
dda0: 41 54 45 28 73 75 6d 2c 20 20 20 20 20 20 20 20 ATE(sum,
ddb0: 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 1, 0, 0,
ddc0: 73 75 6d 53 74 65 70 2c 20 20 20 20 20 20 20 20 sumStep,
ddd0: 20 73 75 6d 46 69 6e 61 6c 69 7a 65 20 20 20 20 sumFinalize
dde0: 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 ),. AGGREGATE
ddf0: 28 74 6f 74 61 6c 2c 20 20 20 20 20 20 20 20 20 (total,
de00: 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 1, 0, 0, sum
de10: 53 74 65 70 2c 20 20 20 20 20 20 20 20 20 74 6f Step, to
de20: 74 61 6c 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 talFinalize )
de30: 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28 ,. AGGREGATE(
de40: 61 76 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 avg,
de50: 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 1, 0, 0, sumS
de60: 74 65 70 2c 20 20 20 20 20 20 20 20 20 61 76 67 tep, avg
de70: 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 Finalize ),.
de80: 20 20 20 41 47 47 52 45 47 41 54 45 32 28 63 6f AGGREGATE2(co
de90: 75 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 unt,
dea0: 30 2c 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53 74 0, 0, 0, countSt
deb0: 65 70 2c 20 20 20 20 20 20 20 63 6f 75 6e 74 46 ep, countF
dec0: 69 6e 61 6c 69 7a 65 2c 0a 20 20 20 20 20 20 20 inalize,.
ded0: 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46 SQLITE_F
dee0: 55 4e 43 5f 43 4f 55 4e 54 20 20 29 2c 0a 20 20 UNC_COUNT ),.
def0: 20 20 41 47 47 52 45 47 41 54 45 28 63 6f 75 6e AGGREGATE(coun
df00: 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 31 t, 1
df10: 2c 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53 74 65 , 0, 0, countSte
df20: 70 2c 20 20 20 20 20 20 20 63 6f 75 6e 74 46 69 p, countFi
df30: 6e 61 6c 69 7a 65 20 20 29 2c 0a 20 20 20 20 41 nalize ),. A
df40: 47 47 52 45 47 41 54 45 28 67 72 6f 75 70 5f 63 GGREGATE(group_c
df50: 6f 6e 63 61 74 2c 20 20 20 20 20 20 31 2c 20 30 oncat, 1, 0
df60: 2c 20 30 2c 20 67 72 6f 75 70 43 6f 6e 63 61 74 , 0, groupConcat
df70: 53 74 65 70 2c 20 67 72 6f 75 70 43 6f 6e 63 61 Step, groupConca
df80: 74 46 69 6e 61 6c 69 7a 65 29 2c 0a 20 20 20 20 tFinalize),.
df90: 41 47 47 52 45 47 41 54 45 28 67 72 6f 75 70 5f AGGREGATE(group_
dfa0: 63 6f 6e 63 61 74 2c 20 20 20 20 20 20 32 2c 20 concat, 2,
dfb0: 30 2c 20 30 2c 20 67 72 6f 75 70 43 6f 6e 63 61 0, 0, groupConca
dfc0: 74 53 74 65 70 2c 20 67 72 6f 75 70 43 6f 6e 63 tStep, groupConc
dfd0: 61 74 46 69 6e 61 6c 69 7a 65 29 2c 0a 20 20 0a atFinalize),. .
dfe0: 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 67 6c 6f LIKEFUNC(glo
dff0: 62 2c 20 32 2c 20 26 67 6c 6f 62 49 6e 66 6f 2c b, 2, &globInfo,
e000: 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b SQLITE_FUNC_LIK
e010: 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 E|SQLITE_FUNC_CA
e020: 53 45 29 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 SE),.#ifdef SQLI
e030: 54 45 5f 43 41 53 45 5f 53 45 4e 53 49 54 49 56 TE_CASE_SENSITIV
e040: 45 5f 4c 49 4b 45 0a 20 20 20 20 4c 49 4b 45 46 E_LIKE. LIKEF
e050: 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 26 6c 69 UNC(like, 2, &li
e060: 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 4c 49 54 keInfoAlt, SQLIT
e070: 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 E_FUNC_LIKE|SQLI
e080: 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 TE_FUNC_CASE),.
e090: 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65 LIKEFUNC(like
e0a0: 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 6f 41 6c , 3, &likeInfoAl
e0b0: 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c t, SQLITE_FUNC_L
e0c0: 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f IKE|SQLITE_FUNC_
e0d0: 43 41 53 45 29 2c 0a 23 65 6c 73 65 0a 20 20 20 CASE),.#else.
e0e0: 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 LIKEFUNC(like,
e0f0: 32 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2, &likeInfoNorm
e100: 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 , SQLITE_FUNC_LI
e110: 4b 45 29 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e KE),. LIKEFUN
e120: 43 28 6c 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 C(like, 3, &like
e130: 49 6e 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 InfoNorm, SQLITE
e140: 5f 46 55 4e 43 5f 4c 49 4b 45 29 2c 0a 23 65 6e _FUNC_LIKE),.#en
e150: 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 dif.#ifdef SQLIT
e160: 45 5f 45 4e 41 42 4c 45 5f 55 4e 4b 4e 4f 57 4e E_ENABLE_UNKNOWN
e170: 5f 53 51 4c 5f 46 55 4e 43 54 49 4f 4e 0a 20 20 _SQL_FUNCTION.
e180: 20 20 46 55 4e 43 54 49 4f 4e 28 75 6e 6b 6e 6f FUNCTION(unkno
e190: 77 6e 2c 20 20 20 20 20 20 20 20 20 20 20 2d 31 wn, -1
e1a0: 2c 20 30 2c 20 30 2c 20 75 6e 6b 6e 6f 77 6e 46 , 0, 0, unknownF
e1b0: 75 6e 63 20 20 20 20 20 20 29 2c 0a 23 65 6e 64 unc ),.#end
e1c0: 69 66 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 if. FUNCTION(
e1d0: 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 coalesce,
e1e0: 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 30 20 20 1, 0, 0, 0
e1f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c ),
e200: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f . FUNCTION(co
e210: 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20 20 alesce,
e220: 20 20 30 2c 20 30 2c 20 30 2c 20 30 20 20 20 20 0, 0, 0, 0
e230: 20 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 ),.
e240: 20 20 20 46 55 4e 43 54 49 4f 4e 32 28 63 6f 61 FUNCTION2(coa
e250: 6c 65 73 63 65 2c 20 20 20 20 20 20 20 20 20 2d lesce, -
e260: 31 2c 20 30 2c 20 30 2c 20 6e 6f 6f 70 46 75 6e 1, 0, 0, noopFun
e270: 63 2c 20 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f c, SQLITE_FUNC_
e280: 43 4f 41 4c 45 53 43 45 29 2c 0a 20 20 7d 3b 0a COALESCE),. };.
e290: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
e2a0: 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 MIT_ALTERTABLE.
e2b0: 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e sqlite3AlterFun
e2c0: 63 74 69 6f 6e 73 28 29 3b 0a 23 65 6e 64 69 66 ctions();.#endif
e2d0: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
e2e0: 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 ITE_ENABLE_STAT3
e2f0: 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c ) || defined(SQL
e300: 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 34 ITE_ENABLE_STAT4
e310: 29 0a 20 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 ). sqlite3Analy
e320: 7a 65 46 75 6e 63 74 69 6f 6e 73 28 29 3b 0a 23 zeFunctions();.#
e330: 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 52 endif. sqlite3R
e340: 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 egisterDateTimeF
e350: 75 6e 63 74 69 6f 6e 73 28 29 3b 0a 20 20 73 71 unctions();. sq
e360: 6c 69 74 65 33 49 6e 73 65 72 74 42 75 69 6c 74 lite3InsertBuilt
e370: 69 6e 46 75 6e 63 73 28 61 42 75 69 6c 74 69 6e inFuncs(aBuiltin
e380: 46 75 6e 63 2c 20 41 72 72 61 79 53 69 7a 65 28 Func, ArraySize(
e390: 61 42 75 69 6c 74 69 6e 46 75 6e 63 29 29 3b 0a aBuiltinFunc));.
e3a0: 0a 23 69 66 20 30 20 20 2f 2a 20 45 6e 61 62 6c .#if 0 /* Enabl
e3b0: 65 20 74 6f 20 70 72 69 6e 74 20 6f 75 74 20 68 e to print out h
e3c0: 6f 77 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 ow the built-in
e3d0: 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 68 61 functions are ha
e3e0: 73 68 65 64 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 shed */. {.
e3f0: 69 6e 74 20 69 3b 0a 20 20 20 20 46 75 6e 63 44 int i;. FuncD
e400: 65 66 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 69 ef *p;. for(i
e410: 3d 30 3b 20 69 3c 53 51 4c 49 54 45 5f 46 55 4e =0; i<SQLITE_FUN
e420: 43 5f 48 41 53 48 5f 53 5a 3b 20 69 2b 2b 29 7b C_HASH_SZ; i++){
e430: 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 46 . printf("F
e440: 55 4e 43 2d 48 41 53 48 20 25 30 32 64 3a 22 2c UNC-HASH %02d:",
e450: 20 69 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 i);. for(p
e460: 3d 73 71 6c 69 74 65 33 42 75 69 6c 74 69 6e 46 =sqlite3BuiltinF
e470: 75 6e 63 74 69 6f 6e 73 2e 61 5b 69 5d 3b 20 70 unctions.a[i]; p
e480: 3b 20 70 3d 70 2d 3e 75 2e 70 48 61 73 68 29 7b ; p=p->u.pHash){
e490: 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d . int n =
e4a0: 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
e4b0: 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 (p->zName);.
e4c0: 20 20 20 20 69 6e 74 20 68 20 3d 20 70 2d 3e 7a int h = p->z
e4d0: 4e 61 6d 65 5b 30 5d 20 2b 20 6e 3b 0a 20 20 20 Name[0] + n;.
e4e0: 20 20 20 20 20 70 72 69 6e 74 66 28 22 20 25 73 printf(" %s
e4f0: 28 25 64 29 22 2c 20 70 2d 3e 7a 4e 61 6d 65 2c (%d)", p->zName,
e500: 20 68 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 h);. }.
e510: 20 20 20 70 72 69 6e 74 66 28 22 5c 6e 22 29 3b printf("\n");
e520: 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 . }. }.#endi
e530: 66 0a 7d 0a f.}.