0000: 2f 2a 0a 2a 2a 20 32 30 30 32 20 46 65 62 72 75 /*.** 2002 Febru
0010: 61 72 79 20 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65 ary 23.**.** The
0020: 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
0030: 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
0040: 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
0050: 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
0060: 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
0070: 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
0080: 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
0090: 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
00a0: 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
00b0: 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
00c0: 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
00d0: 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
00e0: 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
00f0: 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
0100: 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
0110: 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
0120: 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
0180: 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
0190: 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
01a0: 20 69 6d 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f implement vario
01b0: 75 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 us SQL.** functi
01c0: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 ons of SQLite.
01d0: 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 .**.** There is
01e0: 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74 65 only one exporte
01f0: 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69 73 d symbol in this
0200: 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e 63 file - the func
0210: 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 52 65 tion.** sqliteRe
0220: 67 69 73 74 65 72 42 75 69 6c 64 69 6e 46 75 6e gisterBuildinFun
0230: 63 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64 20 61 ctions() found a
0240: 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 t the bottom of
0250: 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c the file..** All
0260: 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 other code has
0270: 66 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2f 0a 23 file scope..*/.#
0280: 69 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 49 include "sqliteI
0290: 6e 74 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 3c nt.h".#include <
02a0: 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 stdlib.h>.#inclu
02b0: 64 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 de <assert.h>.#i
02c0: 6e 63 6c 75 64 65 20 22 76 64 62 65 49 6e 74 2e nclude "vdbeInt.
02d0: 68 22 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e h"../*.** Return
02e0: 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 the collating f
02f0: 75 6e 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 unction associat
0300: 65 64 20 77 69 74 68 20 61 20 66 75 6e 63 74 69 ed with a functi
0310: 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f on..*/.static Co
0320: 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 llSeq *sqlite3Ge
0330: 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 73 71 6c tFuncCollSeq(sql
0340: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
0350: 6e 74 65 78 74 29 7b 0a 20 20 72 65 74 75 72 6e ntext){. return
0360: 20 63 6f 6e 74 65 78 74 2d 3e 70 43 6f 6c 6c 3b context->pColl;
0370: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 .}../*.** Indica
0380: 74 65 20 74 68 61 74 20 74 68 65 20 61 63 63 75 te that the accu
0390: 6d 75 6c 61 74 6f 72 20 6c 6f 61 64 20 73 68 6f mulator load sho
03a0: 75 6c 64 20 62 65 20 73 6b 69 70 70 65 64 20 6f uld be skipped o
03b0: 6e 20 74 68 69 73 0a 2a 2a 20 69 74 65 72 61 74 n this.** iterat
03c0: 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67 72 65 ion of the aggre
03d0: 67 61 74 65 20 6c 6f 6f 70 2e 0a 2a 2f 0a 73 74 gate loop..*/.st
03e0: 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
03f0: 33 53 6b 69 70 41 63 63 75 6d 75 6c 61 74 6f 72 3SkipAccumulator
0400: 4c 6f 61 64 28 73 71 6c 69 74 65 33 5f 63 6f 6e Load(sqlite3_con
0410: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a text *context){.
0420: 20 20 63 6f 6e 74 65 78 74 2d 3e 73 6b 69 70 46 context->skipF
0430: 6c 61 67 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a lag = 1;.}../*.*
0440: 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
0450: 20 6f 66 20 74 68 65 20 6e 6f 6e 2d 61 67 67 72 of the non-aggr
0460: 65 67 61 74 65 20 6d 69 6e 28 29 20 61 6e 64 20 egate min() and
0470: 6d 61 78 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a max() functions.
0480: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
0490: 69 6e 6d 61 78 46 75 6e 63 28 0a 20 20 73 71 6c inmaxFunc(. sql
04a0: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
04b0: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
04c0: 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
04d0: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 ue **argv.){. i
04e0: 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d 61 73 6b nt i;. int mask
04f0: 3b 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 69 ; /* 0 for mi
0500: 6e 28 29 20 6f 72 20 30 78 66 66 66 66 66 66 66 n() or 0xfffffff
0510: 66 20 66 6f 72 20 6d 61 78 28 29 20 2a 2f 0a 20 f for max() */.
0520: 20 69 6e 74 20 69 42 65 73 74 3b 0a 20 20 43 6f int iBest;. Co
0530: 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 llSeq *pColl;..
0540: 20 61 73 73 65 72 74 28 20 61 72 67 63 3e 31 20 assert( argc>1
0550: 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69 );. mask = sqli
0560: 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f te3_user_data(co
0570: 6e 74 65 78 74 29 3d 3d 30 20 3f 20 30 20 3a 20 ntext)==0 ? 0 :
0580: 2d 31 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 -1;. pColl = sq
0590: 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c lite3GetFuncColl
05a0: 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 Seq(context);.
05b0: 61 73 73 65 72 74 28 20 70 43 6f 6c 6c 20 29 3b assert( pColl );
05c0: 0a 20 20 61 73 73 65 72 74 28 20 6d 61 73 6b 3d . assert( mask=
05d0: 3d 2d 31 20 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 =-1 || mask==0 )
05e0: 3b 0a 20 20 69 42 65 73 74 20 3d 20 30 3b 0a 20 ;. iBest = 0;.
05f0: 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c if( sqlite3_val
0600: 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 ue_type(argv[0])
0610: 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 ==SQLITE_NULL )
0620: 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d return;. for(i=
0630: 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 1; i<argc; i++){
0640: 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
0650: 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 _value_type(argv
0660: 5b 69 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c [i])==SQLITE_NUL
0670: 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 L ) return;.
0680: 69 66 28 20 28 73 71 6c 69 74 65 33 4d 65 6d 43 if( (sqlite3MemC
0690: 6f 6d 70 61 72 65 28 61 72 67 76 5b 69 42 65 73 ompare(argv[iBes
06a0: 74 5d 2c 20 61 72 67 76 5b 69 5d 2c 20 70 43 6f t], argv[i], pCo
06b0: 6c 6c 29 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a ll)^mask)>=0 ){.
06c0: 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
06d0: 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 mask==0 );.
06e0: 20 69 42 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 iBest = i;.
06f0: 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f }. }. sqlite3_
0700: 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e result_value(con
0710: 74 65 78 74 2c 20 61 72 67 76 5b 69 42 65 73 74 text, argv[iBest
0720: 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ]);.}../*.** Ret
0730: 75 72 6e 20 74 68 65 20 74 79 70 65 20 6f 66 20 urn the type of
0740: 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f the argument..*/
0750: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 79 70 .static void typ
0760: 65 6f 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 eofFunc(. sqlit
0770: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
0780: 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 ext,. int NotUs
0790: 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 ed,. sqlite3_va
07a0: 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
07b0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 const char *z =
07c0: 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 0;. UNUSED_PARA
07d0: 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
07e0: 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 switch( sqlite
07f0: 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
0800: 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 v[0]) ){. cas
0810: 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 e SQLITE_INTEGER
0820: 3a 20 7a 20 3d 20 22 69 6e 74 65 67 65 72 22 3b : z = "integer";
0830: 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 break;. case
0840: 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 20 20 SQLITE_TEXT:
0850: 20 7a 20 3d 20 22 74 65 78 74 22 3b 20 20 20 20 z = "text";
0860: 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
0870: 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 20 20 SQLITE_FLOAT:
0880: 7a 20 3d 20 22 72 65 61 6c 22 3b 20 20 20 20 62 z = "real"; b
0890: 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 reak;. case S
08a0: 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 20 20 20 7a QLITE_BLOB: z
08b0: 20 3d 20 22 62 6c 6f 62 22 3b 20 20 20 20 62 72 = "blob"; br
08c0: 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 eak;. default
08d0: 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 20 : z
08e0: 3d 20 22 6e 75 6c 6c 22 3b 20 20 20 20 62 72 65 = "null"; bre
08f0: 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ak;. }. sqlite
0900: 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
0910: 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 ntext, z, -1, SQ
0920: 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a LITE_STATIC);.}.
0930: 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
0940: 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 65 tation of the le
0950: 6e 67 74 68 28 29 20 66 75 6e 63 74 69 6f 6e 0a ngth() function.
0960: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c */.static void l
0970: 65 6e 67 74 68 46 75 6e 63 28 0a 20 20 73 71 6c engthFunc(. sql
0980: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
0990: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
09a0: 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
09b0: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 ue **argv.){. i
09c0: 6e 74 20 6c 65 6e 3b 0a 0a 20 20 61 73 73 65 72 nt len;.. asser
09d0: 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 t( argc==1 );.
09e0: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
09f0: 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68 (argc);. switch
0a00: 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ( sqlite3_value_
0a10: 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b type(argv[0]) ){
0a20: 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
0a30: 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61 73 65 20 _BLOB:. case
0a40: 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a SQLITE_INTEGER:.
0a50: 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
0a60: 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73 FLOAT: {. s
0a70: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
0a80: 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 t(context, sqlit
0a90: 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
0aa0: 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 rgv[0]));.
0ab0: 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
0ac0: 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 case SQLITE_TEX
0ad0: 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 T: {. const
0ae0: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
0af0: 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 z = sqlite3_valu
0b00: 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b e_text(argv[0]);
0b10: 0a 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 . if( z==0
0b20: 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 ) return;.
0b30: 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 len = 0;. w
0b40: 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20 hile( *z ){.
0b50: 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20 20 20 20 len++;.
0b60: 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 SQLITE_SKIP_U
0b70: 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 7d 0a TF8(z);. }.
0b80: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 sqlite3_re
0b90: 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 sult_int(context
0ba0: 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20 20 62 72 , len);. br
0bb0: 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
0bc0: 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
0bd0: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e sqlite3_result_n
0be0: 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 ull(context);.
0bf0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
0c00: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d . }.}../*.** Im
0c10: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
0c20: 74 68 65 20 61 62 73 28 29 20 66 75 6e 63 74 69 the abs() functi
0c30: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 3a 20 52 on..**.** IMP: R
0c40: 2d 32 33 39 37 39 2d 32 36 38 35 35 20 54 68 65 -23979-26855 The
0c50: 20 61 62 73 28 58 29 20 66 75 6e 63 74 69 6f 6e abs(X) function
0c60: 20 72 65 74 75 72 6e 73 20 74 68 65 20 61 62 73 returns the abs
0c70: 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f 66 0a 2a olute value of.*
0c80: 2a 20 74 68 65 20 6e 75 6d 65 72 69 63 20 61 72 * the numeric ar
0c90: 67 75 6d 65 6e 74 20 58 2e 20 0a 2a 2f 0a 73 74 gument X. .*/.st
0ca0: 61 74 69 63 20 76 6f 69 64 20 61 62 73 46 75 6e atic void absFun
0cb0: 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 c(sqlite3_contex
0cc0: 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 t *context, int
0cd0: 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 argc, sqlite3_va
0ce0: 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 61 lue **argv){. a
0cf0: 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 ssert( argc==1 )
0d00: 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
0d10: 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 73 77 ETER(argc);. sw
0d20: 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 itch( sqlite3_va
0d30: 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d lue_type(argv[0]
0d40: 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 ) ){. case SQ
0d50: 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a LITE_INTEGER: {.
0d60: 20 20 20 20 20 20 69 36 34 20 69 56 61 6c 20 3d i64 iVal =
0d70: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 sqlite3_value_i
0d80: 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 nt64(argv[0]);.
0d90: 20 20 20 20 20 69 66 28 20 69 56 61 6c 3c 30 20 if( iVal<0
0da0: 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 ){. if( (
0db0: 69 56 61 6c 3c 3c 31 29 3d 3d 30 20 29 7b 0a 20 iVal<<1)==0 ){.
0dc0: 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4d 50 3a /* IMP:
0dd0: 20 52 2d 33 35 34 36 30 2d 31 35 30 38 34 20 49 R-35460-15084 I
0de0: 66 20 58 20 69 73 20 74 68 65 20 69 6e 74 65 67 f X is the integ
0df0: 65 72 20 2d 39 32 32 33 33 37 32 30 33 36 38 35 er -922337203685
0e00: 34 37 37 35 38 30 37 20 74 68 65 6e 0a 20 20 20 4775807 then.
0e10: 20 20 20 20 20 20 20 2a 2a 20 61 62 73 28 58 29 ** abs(X)
0e20: 20 74 68 72 6f 77 73 20 61 6e 20 69 6e 74 65 67 throws an integ
0e30: 65 72 20 6f 76 65 72 66 6c 6f 77 20 65 72 72 6f er overflow erro
0e40: 72 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 r since there is
0e50: 20 6e 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a no. **
0e60: 20 65 71 75 69 76 61 6c 65 6e 74 20 70 6f 73 69 equivalent posi
0e70: 74 69 76 65 20 36 34 2d 62 69 74 20 74 77 6f 20 tive 64-bit two
0e80: 63 6f 6d 70 6c 65 6d 65 6e 74 20 76 61 6c 75 65 complement value
0e90: 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 . */. s
0ea0: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
0eb0: 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 69 6e ror(context, "in
0ec0: 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c teger overflow",
0ed0: 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 -1);.
0ee0: 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 return;.
0ef0: 7d 0a 20 20 20 20 20 20 20 20 69 56 61 6c 20 3d }. iVal =
0f00: 20 2d 69 56 61 6c 3b 0a 20 20 20 20 20 20 7d 20 -iVal;. }
0f10: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 . sqlite3_r
0f20: 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 esult_int64(cont
0f30: 65 78 74 2c 20 69 56 61 6c 29 3b 0a 20 20 20 20 ext, iVal);.
0f40: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
0f50: 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e case SQLITE_N
0f60: 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 ULL: {. /*
0f70: 49 4d 50 3a 20 52 2d 33 37 34 33 34 2d 31 39 39 IMP: R-37434-199
0f80: 32 39 20 41 62 73 28 58 29 20 72 65 74 75 72 6e 29 Abs(X) return
0f90: 73 20 4e 55 4c 4c 20 69 66 20 58 20 69 73 20 4e s NULL if X is N
0fa0: 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 ULL. */. sq
0fb0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c lite3_result_nul
0fc0: 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 l(context);.
0fd0: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
0fe0: 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
0ff0: 20 20 20 20 2f 2a 20 42 65 63 61 75 73 65 20 73 /* Because s
1000: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 qlite3_value_dou
1010: 62 6c 65 28 29 20 72 65 74 75 72 6e 73 20 30 2e ble() returns 0.
1020: 30 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 0 if the argumen
1030: 74 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a t is not. *
1040: 2a 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 * something that
1050: 20 63 61 6e 20 62 65 20 63 6f 6e 76 65 72 74 65 can be converte
1060: 64 20 69 6e 74 6f 20 61 20 6e 75 6d 62 65 72 2c d into a number,
1070: 20 77 65 20 68 61 76 65 3a 0a 20 20 20 20 20 20 we have:.
1080: 2a 2a 20 49 4d 50 3a 20 52 2d 35 37 33 32 36 2d ** IMP: R-57326-
1090: 33 31 35 34 31 20 41 62 73 28 58 29 20 72 65 74 31541 Abs(X) ret
10a0: 75 72 6e 20 30 2e 30 20 69 66 20 58 20 69 73 20 urn 0.0 if X is
10b0: 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 a string or blob
10c0: 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 that. ** c
10d0: 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 annot be convert
10e0: 65 64 20 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 ed to a numeric
10f0: 76 61 6c 75 65 2e 20 0a 20 20 20 20 20 20 2a 2f value. . */
1100: 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 56 . double rV
1110: 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
1120: 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 ue_double(argv[0
1130: 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 56 ]);. if( rV
1140: 61 6c 3c 30 20 29 20 72 56 61 6c 20 3d 20 2d 72 al<0 ) rVal = -r
1150: 56 61 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 Val;. sqlit
1160: 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 e3_result_double
1170: 28 63 6f 6e 74 65 78 74 2c 20 72 56 61 6c 29 3b (context, rVal);
1180: 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1190: 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
11a0: 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
11b0: 6f 66 20 74 68 65 20 73 75 62 73 74 72 28 29 20 of the substr()
11c0: 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
11d0: 73 75 62 73 74 72 28 78 2c 70 31 2c 70 32 29 20 substr(x,p1,p2)
11e0: 20 72 65 74 75 72 6e 73 20 70 32 20 63 68 61 72 returns p2 char
11f0: 61 63 74 65 72 73 20 6f 66 20 78 5b 5d 20 62 65 acters of x[] be
1200: 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 70 31 2e ginning with p1.
1210: 0a 2a 2a 20 70 31 20 69 73 20 31 2d 69 6e 64 65 .** p1 is 1-inde
1220: 78 65 64 2e 20 20 53 6f 20 73 75 62 73 74 72 28 xed. So substr(
1230: 78 2c 31 2c 31 29 20 72 65 74 75 72 6e 73 20 74 x,1,1) returns t
1240: 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 he first charact
1250: 65 72 0a 2a 2a 20 6f 66 20 78 2e 20 20 49 66 20 er.** of x. If
1260: 78 20 69 73 20 74 65 78 74 2c 20 74 68 65 6e 20 x is text, then
1270: 77 65 20 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e we actually coun
1280: 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 t UTF-8 characte
1290: 72 73 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20 61 rs..** If x is a
12a0: 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 63 blob, then we c
12b0: 6f 75 6e 74 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a ount bytes..**.*
12c0: 2a 20 49 66 20 70 31 20 69 73 20 6e 65 67 61 74 * If p1 is negat
12d0: 69 76 65 2c 20 74 68 65 6e 20 77 65 20 62 65 67 ive, then we beg
12e0: 69 6e 20 61 62 73 28 70 31 29 20 66 72 6f 6d 20 in abs(p1) from
12f0: 74 68 65 20 65 6e 64 20 6f 66 20 78 5b 5d 2e 0a the end of x[]..
1300: 2a 2a 0a 2a 2a 20 49 66 20 70 32 20 69 73 20 6e **.** If p2 is n
1310: 65 67 61 74 69 76 65 2c 20 72 65 74 75 72 6e 20 egative, return
1320: 74 68 65 20 70 32 20 63 68 61 72 61 63 74 65 72 the p2 character
1330: 73 20 70 72 65 63 65 65 64 69 6e 67 20 70 31 2e s preceeding p1.
1340: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1350: 73 75 62 73 74 72 46 75 6e 63 28 0a 20 20 73 71 substrFunc(. sq
1360: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
1370: 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
1380: 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
1390: 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
13a0: 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
13b0: 68 61 72 20 2a 7a 3b 0a 20 20 63 6f 6e 73 74 20 har *z;. const
13c0: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
13d0: 32 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 2;. int len;.
13e0: 69 6e 74 20 70 30 74 79 70 65 3b 0a 20 20 69 36 int p0type;. i6
13f0: 34 20 70 31 2c 20 70 32 3b 0a 20 20 69 6e 74 20 4 p1, p2;. int
1400: 6e 65 67 50 32 20 3d 20 30 3b 0a 0a 20 20 61 73 negP2 = 0;.. as
1410: 73 65 72 74 28 20 61 72 67 63 3d 3d 33 20 7c 7c sert( argc==3 ||
1420: 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66 argc==2 );. if
1430: 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ( sqlite3_value_
1440: 74 79 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53 type(argv[1])==S
1450: 51 4c 49 54 45 5f 4e 55 4c 4c 0a 20 20 20 7c 7c QLITE_NULL. ||
1460: 20 28 61 72 67 63 3d 3d 33 20 26 26 20 73 71 6c (argc==3 && sql
1470: 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
1480: 61 72 67 76 5b 32 5d 29 3d 3d 53 51 4c 49 54 45 argv[2])==SQLITE
1490: 5f 4e 55 4c 4c 29 0a 20 20 29 7b 0a 20 20 20 20 _NULL). ){.
14a0: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 30 return;. }. p0
14b0: 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 type = sqlite3_v
14c0: 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 alue_type(argv[0
14d0: 5d 29 3b 0a 20 20 70 31 20 3d 20 73 71 6c 69 74 ]);. p1 = sqlit
14e0: 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 e3_value_int(arg
14f0: 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 70 30 74 v[1]);. if( p0t
1500: 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 ype==SQLITE_BLOB
1510: 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 ){. len = sq
1520: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
1530: 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 s(argv[0]);.
1540: 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 z = sqlite3_valu
1550: 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b e_blob(argv[0]);
1560: 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 . if( z==0 )
1570: 72 65 74 75 72 6e 3b 0a 20 20 20 20 61 73 73 65 return;. asse
1580: 72 74 28 20 6c 65 6e 3d 3d 73 71 6c 69 74 65 33 rt( len==sqlite3
1590: 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 _value_bytes(arg
15a0: 76 5b 30 5d 29 20 29 3b 0a 20 20 7d 65 6c 73 65 v[0]) );. }else
15b0: 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 {. z = sqlite
15c0: 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
15d0: 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a v[0]);. if( z
15e0: 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
15f0: 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 69 len = 0;. i
1600: 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 f( p1<0 ){.
1610: 20 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 3b 20 for(z2=z; *z2;
1620: 6c 65 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 len++){.
1630: 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 SQLITE_SKIP_UTF8
1640: 28 7a 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 (z2);. }.
1650: 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 61 72 }. }. if( ar
1660: 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 70 32 20 gc==3 ){. p2
1670: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1680: 69 6e 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 int(argv[2]);.
1690: 20 20 69 66 28 20 70 32 3c 30 20 29 7b 0a 20 20 if( p2<0 ){.
16a0: 20 20 20 20 70 32 20 3d 20 2d 70 32 3b 0a 20 20 p2 = -p2;.
16b0: 20 20 20 20 6e 65 67 50 32 20 3d 20 31 3b 0a 20 negP2 = 1;.
16c0: 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
16d0: 20 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f 63 p2 = sqlite3_c
16e0: 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
16f0: 28 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d 69 (context)->aLimi
1700: 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1710: 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 69 66 ENGTH];. }. if
1720: 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 70 31 ( p1<0 ){. p1
1730: 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 69 66 28 += len;. if(
1740: 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 p1<0 ){. p
1750: 32 20 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20 69 2 += p1;. i
1760: 66 28 20 70 32 3c 30 20 29 20 70 32 20 3d 20 30 f( p2<0 ) p2 = 0
1770: 3b 0a 20 20 20 20 20 20 70 31 20 3d 20 30 3b 0a ;. p1 = 0;.
1780: 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
1790: 28 20 70 31 3e 30 20 29 7b 0a 20 20 20 20 70 31 ( p1>0 ){. p1
17a0: 2d 2d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 --;. }else if(
17b0: 70 32 3e 30 20 29 7b 0a 20 20 20 20 70 32 2d 2d p2>0 ){. p2--
17c0: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 50 ;. }. if( negP
17d0: 32 20 29 7b 0a 20 20 20 20 70 31 20 2d 3d 20 70 2 ){. p1 -= p
17e0: 32 3b 0a 20 20 20 20 69 66 28 20 70 31 3c 30 20 2;. if( p1<0
17f0: 29 7b 0a 20 20 20 20 20 20 70 32 20 2b 3d 20 70 ){. p2 += p
1800: 31 3b 0a 20 20 20 20 20 20 70 31 20 3d 20 30 3b 1;. p1 = 0;
1810: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 . }. }. ass
1820: 65 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 32 ert( p1>=0 && p2
1830: 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 30 74 >=0 );. if( p0t
1840: 79 70 65 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 ype!=SQLITE_BLOB
1850: 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a ){. while( *
1860: 7a 20 26 26 20 70 31 20 29 7b 0a 20 20 20 20 20 z && p1 ){.
1870: 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 SQLITE_SKIP_UTF
1880: 38 28 7a 29 3b 0a 20 20 20 20 20 20 70 31 2d 2d 8(z);. p1--
1890: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
18a0: 7a 32 3d 7a 3b 20 2a 7a 32 20 26 26 20 70 32 3b z2=z; *z2 && p2;
18b0: 20 70 32 2d 2d 29 7b 0a 20 20 20 20 20 20 53 51 p2--){. SQ
18c0: 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a LITE_SKIP_UTF8(z
18d0: 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 2);. }. sq
18e0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
18f0: 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 t(context, (char
1900: 2a 29 7a 2c 20 28 69 6e 74 29 28 7a 32 2d 7a 29 *)z, (int)(z2-z)
1910: 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
1920: 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 NT);. }else{.
1930: 20 20 69 66 28 20 70 31 2b 70 32 3e 6c 65 6e 20 if( p1+p2>len
1940: 29 7b 0a 20 20 20 20 20 20 70 32 20 3d 20 6c 65 ){. p2 = le
1950: 6e 2d 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 n-p1;. if(
1960: 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 p2<0 ) p2 = 0;.
1970: 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1980: 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e _result_blob(con
1990: 74 65 78 74 2c 20 28 63 68 61 72 2a 29 26 7a 5b text, (char*)&z[
19a0: 70 31 5d 2c 20 28 69 6e 74 29 70 32 2c 20 53 51 p1], (int)p2, SQ
19b0: 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b LITE_TRANSIENT);
19c0: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d . }.}../*.** Im
19d0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
19e0: 74 68 65 20 72 6f 75 6e 64 28 29 20 66 75 6e 63 the round() func
19f0: 74 69 6f 6e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 tion.*/.#ifndef
1a00: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
1a10: 54 49 4e 47 5f 50 4f 49 4e 54 0a 73 74 61 74 69 TING_POINT.stati
1a20: 63 20 76 6f 69 64 20 72 6f 75 6e 64 46 75 6e 63 c void roundFunc
1a30: 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1a40: 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 *context, int a
1a50: 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c rgc, sqlite3_val
1a60: 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 69 6e ue **argv){. in
1a70: 74 20 6e 20 3d 20 30 3b 0a 20 20 64 6f 75 62 6c t n = 0;. doubl
1a80: 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 42 75 e r;. char *zBu
1a90: 66 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 f;. assert( arg
1aa0: 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 c==1 || argc==2
1ab0: 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 );. if( argc==2
1ac0: 20 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 ){. if( SQLI
1ad0: 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 TE_NULL==sqlite3
1ae0: 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 _value_type(argv
1af0: 5b 31 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 [1]) ) return;.
1b00: 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 n = sqlite3_v
1b10: 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d alue_int(argv[1]
1b20: 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 33 30 20 );. if( n>30
1b30: 29 20 6e 20 3d 20 33 30 3b 0a 20 20 20 20 69 66 ) n = 30;. if
1b40: 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 ( n<0 ) n = 0;.
1b50: 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 }. if( sqlite3
1b60: 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 _value_type(argv
1b70: 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c [0])==SQLITE_NUL
1b80: 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 72 20 L ) return;. r
1b90: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1ba0: 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b double(argv[0]);
1bb0: 0a 20 20 2f 2a 20 49 66 20 59 3d 3d 30 20 61 6e . /* If Y==0 an
1bc0: 64 20 58 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 d X will fit in
1bd0: 61 20 36 34 2d 62 69 74 20 69 6e 74 2c 0a 20 20 a 64-bit int,.
1be0: 2a 2a 20 68 61 6e 64 6c 65 20 74 68 65 20 72 6f ** handle the ro
1bf0: 75 6e 64 69 6e 67 20 64 69 72 65 63 74 6c 79 2c unding directly,
1c00: 0a 20 20 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 . ** otherwise
1c10: 75 73 65 20 70 72 69 6e 74 66 2e 0a 20 20 2a 2f use printf.. */
1c20: 0a 20 20 69 66 28 20 6e 3d 3d 30 20 26 26 20 72 . if( n==0 && r
1c30: 3e 3d 30 20 26 26 20 72 3c 4c 41 52 47 45 53 54 >=0 && r<LARGEST
1c40: 5f 49 4e 54 36 34 2d 31 20 29 7b 0a 20 20 20 20 _INT64-1 ){.
1c50: 72 20 3d 20 28 64 6f 75 62 6c 65 29 28 28 73 71 r = (double)((sq
1c60: 6c 69 74 65 5f 69 6e 74 36 34 29 28 72 2b 30 2e lite_int64)(r+0.
1c70: 35 29 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 5));. }else if(
1c80: 20 6e 3d 3d 30 20 26 26 20 72 3c 30 20 26 26 20 n==0 && r<0 &&
1c90: 28 2d 72 29 3c 4c 41 52 47 45 53 54 5f 49 4e 54 (-r)<LARGEST_INT
1ca0: 36 34 2d 31 20 29 7b 0a 20 20 20 20 72 20 3d 20 64-1 ){. r =
1cb0: 2d 28 64 6f 75 62 6c 65 29 28 28 73 71 6c 69 74 -(double)((sqlit
1cc0: 65 5f 69 6e 74 36 34 29 28 28 2d 72 29 2b 30 2e e_int64)((-r)+0.
1cd0: 35 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 5));. }else{.
1ce0: 20 20 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33 zBuf = sqlite3
1cf0: 5f 6d 70 72 69 6e 74 66 28 22 25 2e 2a 66 22 2c _mprintf("%.*f",
1d00: 6e 2c 72 29 3b 0a 20 20 20 20 69 66 28 20 7a 42 n,r);. if( zB
1d10: 75 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 uf==0 ){. s
1d20: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
1d30: 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 ror_nomem(contex
1d40: 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e t);. return
1d50: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
1d60: 74 65 33 41 74 6f 46 28 7a 42 75 66 2c 20 26 72 te3AtoF(zBuf, &r
1d70: 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 , sqlite3Strlen3
1d80: 30 28 7a 42 75 66 29 2c 20 53 51 4c 49 54 45 5f 0(zBuf), SQLITE_
1d90: 55 54 46 38 29 3b 0a 20 20 20 20 73 71 6c 69 74 UTF8);. sqlit
1da0: 65 33 5f 66 72 65 65 28 7a 42 75 66 29 3b 0a 20 e3_free(zBuf);.
1db0: 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 }. sqlite3_res
1dc0: 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 ult_double(conte
1dd0: 78 74 2c 20 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 xt, r);.}.#endif
1de0: 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
1df0: 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 nByte bytes of
1e00: 73 70 61 63 65 20 75 73 69 6e 67 20 73 71 6c 69 space using sqli
1e10: 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49 66 te3_malloc(). If
1e20: 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 the.** allocati
1e30: 6f 6e 20 66 61 69 6c 73 2c 20 63 61 6c 6c 20 73 on fails, call s
1e40: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
1e50: 72 6f 72 5f 6e 6f 6d 65 6d 28 29 20 74 6f 20 6e ror_nomem() to n
1e60: 6f 74 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 otify.** the dat
1e70: 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61 abase handle tha
1e80: 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 t malloc() has f
1e90: 61 69 6c 65 64 20 61 6e 64 20 72 65 74 75 72 6e ailed and return
1ea0: 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 6e 42 79 NULL..** If nBy
1eb0: 74 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 te is larger tha
1ec0: 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 74 n the maximum st
1ed0: 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 65 6e ring or blob len
1ee0: 67 74 68 2c 20 74 68 65 6e 0a 2a 2a 20 72 61 69 gth, then.** rai
1ef0: 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f 4f se an SQLITE_TOO
1f00: 42 49 47 20 65 78 63 65 70 74 69 6f 6e 20 61 6e BIG exception an
1f10: 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a d return NULL..*
1f20: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 63 /.static void *c
1f30: 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 73 71 6c ontextMalloc(sql
1f40: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
1f50: 6e 74 65 78 74 2c 20 69 36 34 20 6e 42 79 74 65 ntext, i64 nByte
1f60: 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 ){. char *z;.
1f70: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 sqlite3 *db = sq
1f80: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
1f90: 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
1fa0: 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 ;. assert( nByt
1fb0: 65 3e 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73 e>0 );. testcas
1fc0: 65 28 20 6e 42 79 74 65 3d 3d 64 62 2d 3e 61 4c e( nByte==db->aL
1fd0: 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1fe0: 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74 T_LENGTH] );. t
1ff0: 65 73 74 63 61 73 65 28 20 6e 42 79 74 65 3d 3d estcase( nByte==
2000: 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
2010: 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b E_LIMIT_LENGTH]+
2020: 31 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 1 );. if( nByte
2030: 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
2040: 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
2050: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
2060: 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f result_error_too
2070: 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 big(context);.
2080: 20 20 7a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 z = 0;. }else
2090: 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 {. z = sqlite
20a0: 33 4d 61 6c 6c 6f 63 28 28 69 6e 74 29 6e 42 79 3Malloc((int)nBy
20b0: 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 te);. if( !z
20c0: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
20d0: 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f _result_error_no
20e0: 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 mem(context);.
20f0: 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
2100: 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 z;.}../*.** Imp
2110: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
2120: 68 65 20 75 70 70 65 72 28 29 20 61 6e 64 20 6c he upper() and l
2130: 6f 77 65 72 28 29 20 53 51 4c 20 66 75 6e 63 74 ower() SQL funct
2140: 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ions..*/.static
2150: 76 6f 69 64 20 75 70 70 65 72 46 75 6e 63 28 73 void upperFunc(s
2160: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
2170: 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 context, int arg
2180: 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 c, sqlite3_value
2190: 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63 68 61 72 **argv){. char
21a0: 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74 20 63 68 *z1;. const ch
21b0: 61 72 20 2a 7a 32 3b 0a 20 20 69 6e 74 20 69 2c ar *z2;. int i,
21c0: 20 6e 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 n;. UNUSED_PAR
21d0: 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 AMETER(argc);.
21e0: 7a 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 z2 = (char*)sqli
21f0: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
2200: 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 rgv[0]);. n = s
2210: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
2220: 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f es(argv[0]);. /
2230: 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68 * Verify that th
2240: 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 e call to _bytes
2250: 28 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 () does not inva
2260: 6c 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 lidate the _text
2270: 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 () pointer */.
2280: 61 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 assert( z2==(cha
2290: 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 r*)sqlite3_value
22a0: 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 _text(argv[0]) )
22b0: 3b 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 ;. if( z2 ){.
22c0: 20 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 z1 = contextMa
22d0: 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 lloc(context, ((
22e0: 69 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 i64)n)+1);. i
22f0: 66 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 66 f( z1 ){. f
2300: 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b or(i=0; i<n; i++
2310: 29 7b 0a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d ){. z1[i]
2320: 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 = (char)sqlite3
2330: 54 6f 75 70 70 65 72 28 7a 32 5b 69 5d 29 3b 0a Toupper(z2[i]);.
2340: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
2350: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2360: 74 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 6e t(context, z1, n
2370: 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b , sqlite3_free);
2380: 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 . }. }.}.sta
2390: 74 69 63 20 76 6f 69 64 20 6c 6f 77 65 72 46 75 tic void lowerFu
23a0: 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nc(sqlite3_conte
23b0: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 xt *context, int
23c0: 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 argc, sqlite3_v
23d0: 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 alue **argv){.
23e0: 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 char *z1;. cons
23f0: 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 6e t char *z2;. in
2400: 74 20 69 2c 20 6e 3b 0a 20 20 55 4e 55 53 45 44 t i, n;. UNUSED
2410: 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 _PARAMETER(argc)
2420: 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61 72 2a 29 ;. z2 = (char*)
2430: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2440: 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e xt(argv[0]);. n
2450: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
2460: 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b _bytes(argv[0]);
2470: 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 . /* Verify tha
2480: 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5f 62 t the call to _b
2490: 79 74 65 73 28 29 20 64 6f 65 73 20 6e 6f 74 20 ytes() does not
24a0: 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 5f invalidate the _
24b0: 74 65 78 74 28 29 20 70 6f 69 6e 74 65 72 20 2a text() pointer *
24c0: 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 32 3d 3d /. assert( z2==
24d0: 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 (char*)sqlite3_v
24e0: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
24f0: 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 32 20 29 ]) );. if( z2 )
2500: 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74 65 {. z1 = conte
2510: 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 xtMalloc(context
2520: 2c 20 28 28 69 36 34 29 6e 29 2b 31 29 3b 0a 20 , ((i64)n)+1);.
2530: 20 20 20 69 66 28 20 7a 31 20 29 7b 0a 20 20 20 if( z1 ){.
2540: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b for(i=0; i<n;
2550: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a i++){. z
2560: 31 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 54 6f 1[i] = sqlite3To
2570: 6c 6f 77 65 72 28 7a 32 5b 69 5d 29 3b 0a 20 20 lower(z2[i]);.
2580: 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
2590: 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
25a0: 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 6e 2c 20 context, z1, n,
25b0: 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 sqlite3_free);.
25c0: 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 }. }.}...#if
25d0: 20 30 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 0 /* This func
25e0: 74 69 6f 6e 20 69 73 20 6e 65 76 65 72 20 75 73 tion is never us
25f0: 65 64 2e 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 ed. */./*.** The
2600: 20 43 4f 41 4c 45 53 43 45 28 29 20 61 6e 64 20 COALESCE() and
2610: 49 46 4e 55 4c 4c 28 29 20 66 75 6e 63 74 69 6f IFNULL() functio
2620: 6e 73 20 75 73 65 64 20 74 6f 20 62 65 20 69 6d ns used to be im
2630: 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 73 68 6f plemented as sho
2640: 77 6e 0a 2a 2a 20 68 65 72 65 2e 20 20 42 75 74 wn.** here. But
2650: 20 6e 6f 77 20 74 68 65 79 20 61 72 65 20 69 6d now they are im
2660: 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 56 44 42 plemented as VDB
2670: 45 20 63 6f 64 65 20 73 6f 20 74 68 61 74 20 75 E code so that u
2680: 6e 75 73 65 64 20 61 72 67 75 6d 65 6e 74 73 0a nused arguments.
2690: 2a 2a 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 ** do not have t
26a0: 6f 20 62 65 20 63 6f 6d 70 75 74 65 64 2e 20 20 o be computed.
26b0: 54 68 69 73 20 6c 65 67 61 63 79 20 69 6d 70 6c This legacy impl
26c0: 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 72 65 ementation is re
26d0: 74 61 69 6e 65 64 20 61 73 0a 2a 2a 20 63 6f 6d tained as.** com
26e0: 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 ment..*/./*.** I
26f0: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
2700: 20 74 68 65 20 49 46 4e 55 4c 4c 28 29 2c 20 4e the IFNULL(), N
2710: 56 4c 28 29 2c 20 61 6e 64 20 43 4f 41 4c 45 53 VL(), and COALES
2720: 43 45 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 20 CE() functions.
2730: 20 0a 2a 2a 20 41 6c 6c 20 74 68 72 65 65 20 64 .** All three d
2740: 6f 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 o the same thing
2750: 2e 20 20 54 68 65 79 20 72 65 74 75 72 6e 20 74 . They return t
2760: 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 4e 55 4c he first non-NUL
2770: 4c 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a L.** argument..*
2780: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 66 /.static void if
2790: 6e 75 6c 6c 46 75 6e 63 28 0a 20 20 73 71 6c 69 nullFunc(. sqli
27a0: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
27b0: 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
27c0: 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
27d0: 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e e **argv.){. in
27e0: 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 t i;. for(i=0;
27f0: 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 i<argc; i++){.
2800: 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c if( SQLITE_NUL
2810: 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 L!=sqlite3_value
2820: 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29 20 29 _type(argv[i]) )
2830: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
2840: 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e result_value(con
2850: 74 65 78 74 2c 20 61 72 67 76 5b 69 5d 29 3b 0a text, argv[i]);.
2860: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2870: 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 }. }.}.#endif
2880: 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 23 /* NOT USED */.#
2890: 64 65 66 69 6e 65 20 69 66 6e 75 6c 6c 46 75 6e define ifnullFun
28a0: 63 20 76 65 72 73 69 6f 6e 46 75 6e 63 20 20 20 c versionFunc
28b0: 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 66 75 /* Substitute fu
28c0: 6e 63 74 69 6f 6e 20 2d 20 6e 65 76 65 72 20 63 nction - never c
28d0: 61 6c 6c 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 alled */../*.**
28e0: 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
28f0: 66 20 72 61 6e 64 6f 6d 28 29 2e 20 20 52 65 74 f random(). Ret
2900: 75 72 6e 20 61 20 72 61 6e 64 6f 6d 20 69 6e 74 urn a random int
2910: 65 67 65 72 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 eger. .*/.stati
2920: 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 46 75 6e c void randomFun
2930: 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
2940: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
2950: 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 int NotUsed,.
2960: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
2970: 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 NotUsed2.){. sq
2980: 6c 69 74 65 5f 69 6e 74 36 34 20 72 3b 0a 20 20 lite_int64 r;.
2990: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
29a0: 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 2(NotUsed, NotUs
29b0: 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f ed2);. sqlite3_
29c0: 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f randomness(sizeo
29d0: 66 28 72 29 2c 20 26 72 29 3b 0a 20 20 69 66 28 f(r), &r);. if(
29e0: 20 72 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 r<0 ){. /* W
29f0: 65 20 6e 65 65 64 20 74 6f 20 70 72 65 76 65 6e e need to preven
2a00: 74 20 61 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 t a random numbe
2a10: 72 20 6f 66 20 30 78 38 30 30 30 30 30 30 30 30 r of 0x800000000
2a20: 30 30 30 30 30 30 30 20 0a 20 20 20 20 2a 2a 20 0000000 . **
2a30: 28 6f 72 20 2d 39 32 32 33 33 37 32 30 33 36 38 (or -92233720368
2a40: 35 34 37 37 35 38 30 38 29 20 73 69 6e 63 65 20 54775808) since
2a50: 77 68 65 6e 20 79 6f 75 20 64 6f 20 61 62 73 28 when you do abs(
2a60: 29 20 6f 66 20 74 68 61 74 0a 20 20 20 20 2a 2a ) of that. **
2a70: 20 6e 75 6d 62 65 72 20 6f 66 20 79 6f 75 20 67 number of you g
2a80: 65 74 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 et the same valu
2a90: 65 20 62 61 63 6b 20 61 67 61 69 6e 2e 20 20 54 e back again. T
2aa0: 6f 20 64 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a o do this. **
2ab0: 20 69 6e 20 61 20 77 61 79 20 74 68 61 74 20 69 in a way that i
2ac0: 73 20 74 65 73 74 61 62 6c 65 2c 20 6d 61 73 6b s testable, mask
2ad0: 20 74 68 65 20 73 69 67 6e 20 62 69 74 20 6f 66 the sign bit of
2ae0: 66 20 6f 66 20 6e 65 67 61 74 69 76 65 0a 20 20 f of negative.
2af0: 20 20 2a 2a 20 76 61 6c 75 65 73 2c 20 72 65 73 ** values, res
2b00: 75 6c 74 69 6e 67 20 69 6e 20 61 20 70 6f 73 69 ulting in a posi
2b10: 74 69 76 65 20 76 61 6c 75 65 2e 20 20 54 68 65 tive value. The
2b20: 6e 20 74 61 6b 65 20 74 68 65 20 0a 20 20 20 20 n take the .
2b30: 2a 2a 20 32 73 20 63 6f 6d 70 6c 65 6d 65 6e 74 ** 2s complement
2b40: 20 6f 66 20 74 68 61 74 20 70 6f 73 69 74 69 76 of that positiv
2b50: 65 20 76 61 6c 75 65 2e 20 20 54 68 65 20 65 6e e value. The en
2b60: 64 20 72 65 73 75 6c 74 20 63 61 6e 0a 20 20 20 d result can.
2b70: 20 2a 2a 20 74 68 65 72 65 66 6f 72 65 20 62 65 ** therefore be
2b80: 20 6e 6f 20 6c 65 73 73 20 74 68 61 6e 20 2d 39 no less than -9
2b90: 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 2233720368547758
2ba0: 30 37 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 07.. */. r
2bb0: 20 3d 20 2d 28 72 20 26 20 4c 41 52 47 45 53 54 = -(r & LARGEST
2bc0: 5f 49 4e 54 36 34 29 3b 0a 20 20 7d 0a 20 20 73 _INT64);. }. s
2bd0: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
2be0: 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b t64(context, r);
2bf0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
2c00: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 entation of rand
2c10: 6f 6d 62 6c 6f 62 28 4e 29 2e 20 20 52 65 74 75 omblob(N). Retu
2c20: 72 6e 20 61 20 72 61 6e 64 6f 6d 20 62 6c 6f 62 rn a random blob
2c30: 0a 2a 2a 20 74 68 61 74 20 69 73 20 4e 20 62 79 .** that is N by
2c40: 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 tes long..*/.sta
2c50: 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 42 tic void randomB
2c60: 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 lob(. sqlite3_c
2c70: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
2c80: 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
2c90: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
2ca0: 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a rgv.){. int n;.
2cb0: 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
2cc0: 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 *p;. assert( ar
2cd0: 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 gc==1 );. UNUSE
2ce0: 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 D_PARAMETER(argc
2cf0: 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 );. n = sqlite3
2d00: 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b _value_int(argv[
2d10: 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3c 31 20 29 0]);. if( n<1 )
2d20: 7b 0a 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20 7d {. n = 1;. }
2d30: 0a 20 20 70 20 3d 20 63 6f 6e 74 65 78 74 4d 61 . p = contextMa
2d40: 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 29 lloc(context, n)
2d50: 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
2d60: 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
2d70: 65 73 73 28 6e 2c 20 70 29 3b 0a 20 20 20 20 73 ess(n, p);. s
2d80: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c qlite3_result_bl
2d90: 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 ob(context, (cha
2da0: 72 2a 29 70 2c 20 6e 2c 20 73 71 6c 69 74 65 33 r*)p, n, sqlite3
2db0: 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f _free);. }.}../
2dc0: 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
2dd0: 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61 73 74 5f ion of the last_
2de0: 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 53 insert_rowid() S
2df0: 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 QL function. Th
2e00: 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 e return.** valu
2e10: 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 e is the same as
2e20: 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 the sqlite3_las
2e30: 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 t_insert_rowid()
2e40: 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a API function..*
2e50: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 61 /.static void la
2e60: 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 st_insert_rowid(
2e70: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
2e80: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 xt *context, .
2e90: 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20 20 int NotUsed, .
2ea0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
2eb0: 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 NotUsed2.){. sq
2ec0: 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
2ed0: 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
2ee0: 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
2ef0: 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
2f00: 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 ER2(NotUsed, Not
2f10: 55 73 65 64 32 29 3b 0a 20 20 2f 2a 20 49 4d 50 Used2);. /* IMP
2f20: 3a 20 52 2d 35 31 35 31 33 2d 31 32 30 32 36 20 : R-51513-12026
2f30: 54 68 65 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f The last_insert_
2f40: 72 6f 77 69 64 28 29 20 53 51 4c 20 66 75 6e 63 rowid() SQL func
2f50: 74 69 6f 6e 20 69 73 20 61 0a 20 20 2a 2a 20 77 tion is a. ** w
2f60: 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 rapper around th
2f70: 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 e sqlite3_last_i
2f80: 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 43 2f nsert_rowid() C/
2f90: 43 2b 2b 20 69 6e 74 65 72 66 61 63 65 0a 20 20 C++ interface.
2fa0: 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a ** function. */.
2fb0: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
2fc0: 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 _int64(context,
2fd0: 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 sqlite3_last_ins
2fe0: 65 72 74 5f 72 6f 77 69 64 28 64 62 29 29 3b 0a ert_rowid(db));.
2ff0: 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
3000: 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 ntation of the c
3010: 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e hanges() SQL fun
3020: 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 ction..**.** IMP
3030: 3a 20 52 2d 36 32 30 37 33 2d 31 31 32 30 39 20 : R-62073-11209
3040: 54 68 65 20 63 68 61 6e 67 65 73 28 29 20 53 51 The changes() SQ
3050: 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 L function is a
3060: 77 72 61 70 70 65 72 0a 2a 2a 20 61 72 6f 75 6e wrapper.** aroun
3070: 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68 d the sqlite3_ch
3080: 61 6e 67 65 73 28 29 20 43 2f 43 2b 2b 20 66 75 anges() C/C++ fu
3090: 6e 63 74 69 6f 6e 20 61 6e 64 20 68 65 6e 63 65 nction and hence
30a0: 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 73 61 6d follows the sam
30b0: 65 0a 2a 2a 20 72 75 6c 65 73 20 66 6f 72 20 63 e.** rules for c
30c0: 6f 75 6e 74 69 6e 67 20 63 68 61 6e 67 65 73 2e ounting changes.
30d0: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
30e0: 63 68 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 changes(. sqlit
30f0: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
3100: 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 ext,. int NotUs
3110: 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 ed,. sqlite3_va
3120: 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 lue **NotUsed2.)
3130: 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
3140: 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 = sqlite3_contex
3150: 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 t_db_handle(cont
3160: 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 ext);. UNUSED_P
3170: 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 ARAMETER2(NotUse
3180: 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 d, NotUsed2);.
3190: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 sqlite3_result_i
31a0: 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 nt(context, sqli
31b0: 74 65 33 5f 63 68 61 6e 67 65 73 28 64 62 29 29 te3_changes(db))
31c0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
31d0: 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
31e0: 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 total_changes()
31f0: 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 SQL function.
3200: 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
3210: 20 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 is.** the same
3220: 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 as the sqlite3_t
3230: 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 41 otal_changes() A
3240: 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a PI function..*/.
3250: 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f 74 61 static void tota
3260: 6c 5f 63 68 61 6e 67 65 73 28 0a 20 20 73 71 6c l_changes(. sql
3270: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
3280: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 ntext,. int Not
3290: 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f Used,. sqlite3_
32a0: 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 value **NotUsed2
32b0: 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 .){. sqlite3 *d
32c0: 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 b = sqlite3_cont
32d0: 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f ext_db_handle(co
32e0: 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 ntext);. UNUSED
32f0: 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
3300: 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
3310: 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 35 32 37 35 /* IMP: R-5275
3320: 36 2d 34 31 39 39 33 20 54 68 69 73 20 66 75 6e 6-41993 This fun
3330: 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61 70 70 ction is a wrapp
3340: 65 72 20 61 72 6f 75 6e 64 20 74 68 65 0a 20 20 er around the.
3350: 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c ** sqlite3_total
3360: 5f 63 68 61 6e 67 65 73 28 29 20 43 2f 43 2b 2b _changes() C/C++
3370: 20 69 6e 74 65 72 66 61 63 65 2e 20 2a 2f 0a 20 interface. */.
3380: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
3390: 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c int(context, sql
33a0: 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 ite3_total_chang
33b0: 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a es(db));.}../*.*
33c0: 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 64 65 * A structure de
33d0: 66 69 6e 69 6e 67 20 68 6f 77 20 74 6f 20 64 6f fining how to do
33e0: 20 47 4c 4f 42 2d 73 74 79 6c 65 20 63 6f 6d 70 GLOB-style comp
33f0: 61 72 69 73 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 arisons..*/.stru
3400: 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 7b ct compareInfo {
3410: 0a 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 3b 0a . u8 matchAll;.
3420: 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 3b 0a 20 u8 matchOne;.
3430: 20 75 38 20 6d 61 74 63 68 53 65 74 3b 0a 20 20 u8 matchSet;.
3440: 75 38 20 6e 6f 43 61 73 65 3b 0a 7d 3b 0a 0a 2f u8 noCase;.};../
3450: 2a 0a 2a 2a 20 46 6f 72 20 4c 49 4b 45 20 61 6e *.** For LIKE an
3460: 64 20 47 4c 4f 42 20 6d 61 74 63 68 69 6e 67 20 d GLOB matching
3470: 6f 6e 20 45 42 43 44 49 43 20 6d 61 63 68 69 6e on EBCDIC machin
3480: 65 73 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 es, assume that
3490: 65 76 65 72 79 0a 2a 2a 20 63 68 61 72 61 63 74 every.** charact
34a0: 65 72 20 69 73 20 65 78 61 63 74 6c 79 20 6f 6e er is exactly on
34b0: 65 20 62 79 74 65 20 69 6e 20 73 69 7a 65 2e 20 e byte in size.
34c0: 20 41 6c 73 6f 2c 20 61 6c 6c 20 63 68 61 72 61 Also, all chara
34d0: 63 74 65 72 73 20 61 72 65 0a 2a 2a 20 61 62 6c cters are.** abl
34e0: 65 20 74 6f 20 70 61 72 74 69 63 69 70 61 74 65 e to participate
34f0: 20 69 6e 20 75 70 70 65 72 2d 63 61 73 65 2d 74 in upper-case-t
3500: 6f 2d 6c 6f 77 65 72 2d 63 61 73 65 20 6d 61 70 o-lower-case map
3510: 70 69 6e 67 73 20 69 6e 20 45 42 43 44 49 43 0a pings in EBCDIC.
3520: 2a 2a 20 77 68 65 72 65 61 73 20 6f 6e 6c 79 20 ** whereas only
3530: 63 68 61 72 61 63 74 65 72 73 20 6c 65 73 73 20 characters less
3540: 74 68 61 6e 20 30 78 38 30 20 64 6f 20 69 6e 20 than 0x80 do in
3550: 41 53 43 49 49 2e 0a 2a 2f 0a 23 69 66 20 64 65 ASCII..*/.#if de
3560: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 42 43 fined(SQLITE_EBC
3570: 44 49 43 29 0a 23 20 64 65 66 69 6e 65 20 73 71 DIC).# define sq
3580: 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 41 2c lite3Utf8Read(A,
3590: 43 29 20 20 28 2a 28 41 2b 2b 29 29 0a 23 20 64 C) (*(A++)).# d
35a0: 65 66 69 6e 65 20 47 6c 6f 67 55 70 70 65 72 54 efine GlogUpperT
35b0: 6f 4c 6f 77 65 72 28 41 29 20 20 20 41 20 3d 20 oLower(A) A =
35c0: 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f sqlite3UpperToLo
35d0: 77 65 72 5b 41 5d 0a 23 65 6c 73 65 0a 23 20 64 wer[A].#else.# d
35e0: 65 66 69 6e 65 20 47 6c 6f 67 55 70 70 65 72 54 efine GlogUpperT
35f0: 6f 4c 6f 77 65 72 28 41 29 20 20 20 69 66 28 20 oLower(A) if(
3600: 21 28 28 41 29 26 7e 30 78 37 66 29 20 29 7b 20 !((A)&~0x7f) ){
3610: 41 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 A = sqlite3Upper
3620: 54 6f 4c 6f 77 65 72 5b 41 5d 3b 20 7d 0a 23 65 ToLower[A]; }.#e
3630: 6e 64 69 66 0a 0a 73 74 61 74 69 63 20 63 6f 6e ndif..static con
3640: 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 st struct compar
3650: 65 49 6e 66 6f 20 67 6c 6f 62 49 6e 66 6f 20 3d eInfo globInfo =
3660: 20 7b 20 27 2a 27 2c 20 27 3f 27 2c 20 27 5b 27 { '*', '?', '['
3670: 2c 20 30 20 7d 3b 0a 2f 2a 20 54 68 65 20 63 6f , 0 };./* The co
3680: 72 72 65 63 74 20 53 51 4c 2d 39 32 20 62 65 68 rrect SQL-92 beh
3690: 61 76 69 6f 72 20 69 73 20 66 6f 72 20 74 68 65 avior is for the
36a0: 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 LIKE operator t
36b0: 6f 20 69 67 6e 6f 72 65 0a 2a 2a 20 63 61 73 65 o ignore.** case
36c0: 2e 20 20 54 68 75 73 20 20 27 61 27 20 4c 49 4b . Thus 'a' LIK
36d0: 45 20 27 41 27 20 77 6f 75 6c 64 20 62 65 20 74 E 'A' would be t
36e0: 72 75 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 rue. */.static c
36f0: 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 onst struct comp
3700: 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f areInfo likeInfo
3710: 4e 6f 72 6d 20 3d 20 7b 20 27 25 27 2c 20 27 5f Norm = { '%', '_
3720: 27 2c 20 20 20 30 2c 20 31 20 7d 3b 0a 2f 2a 20 ', 0, 1 };./*
3730: 49 66 20 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 If SQLITE_CASE_S
3740: 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 20 69 73 ENSITIVE_LIKE is
3750: 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 defined, then t
3760: 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 he LIKE operator
3770: 0a 2a 2a 20 69 73 20 63 61 73 65 20 73 65 6e 73 .** is case sens
3780: 69 74 69 76 65 20 63 61 75 73 69 6e 67 20 27 61 itive causing 'a
3790: 27 20 4c 49 4b 45 20 27 41 27 20 74 6f 20 62 65 ' LIKE 'A' to be
37a0: 20 66 61 6c 73 65 20 2a 2f 0a 73 74 61 74 69 63 false */.static
37b0: 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f const struct co
37c0: 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e mpareInfo likeIn
37d0: 66 6f 41 6c 74 20 3d 20 7b 20 27 25 27 2c 20 27 foAlt = { '%', '
37e0: 5f 27 2c 20 20 20 30 2c 20 30 20 7d 3b 0a 0a 2f _', 0, 0 };../
37f0: 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f *.** Compare two
3800: 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 UTF-8 strings f
3810: 6f 72 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 or equality wher
3820: 65 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69 e the first stri
3830: 6e 67 20 63 61 6e 0a 2a 2a 20 70 6f 74 65 6e 74 ng can.** potent
3840: 69 61 6c 6c 79 20 62 65 20 61 20 22 67 6c 6f 62 ially be a "glob
3850: 22 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 52 " expression. R
3860: 65 74 75 72 6e 20 74 72 75 65 20 28 31 29 20 69 eturn true (1) i
3870: 66 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 74 68 f they.** are th
3880: 65 20 73 61 6d 65 20 61 6e 64 20 66 61 6c 73 65 e same and false
3890: 20 28 30 29 20 69 66 20 74 68 65 79 20 61 72 65 (0) if they are
38a0: 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 0a 2a different..**.*
38b0: 2a 20 47 6c 6f 62 62 69 6e 67 20 72 75 6c 65 73 * Globbing rules
38c0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 2a 27 :.**.** '*'
38d0: 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 61 Matches a
38e0: 6e 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20 7a ny sequence of z
38f0: 65 72 6f 20 6f 72 20 6d 6f 72 65 20 63 68 61 72 ero or more char
3900: 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 acters..**.**
3910: 20 20 20 27 3f 27 20 20 20 20 20 20 20 4d 61 74 '?' Mat
3920: 63 68 65 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 ches exactly one
3930: 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 0a 2a character..**.*
3940: 2a 20 20 20 20 20 5b 2e 2e 2e 5d 20 20 20 20 20 * [...]
3950: 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 63 68 61 Matches one cha
3960: 72 61 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20 racter from the
3970: 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74 20 6f 66 enclosed list of
3980: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
3990: 20 20 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a characters..*
39a0: 2a 0a 2a 2a 20 20 20 20 20 5b 5e 2e 2e 2e 5d 20 *.** [^...]
39b0: 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 Matches one
39c0: 63 68 61 72 61 63 74 65 72 20 6e 6f 74 20 69 6e character not in
39d0: 20 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 the enclosed li
39e0: 73 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 st..**.** With t
39f0: 68 65 20 5b 2e 2e 2e 5d 20 61 6e 64 20 5b 5e 2e he [...] and [^.
3a00: 2e 2e 5d 20 6d 61 74 63 68 69 6e 67 2c 20 61 20 ..] matching, a
3a10: 27 5d 27 20 63 68 61 72 61 63 74 65 72 20 63 61 ']' character ca
3a20: 6e 20 62 65 20 69 6e 63 6c 75 64 65 64 0a 2a 2a n be included.**
3a30: 20 69 6e 20 74 68 65 20 6c 69 73 74 20 62 79 20 in the list by
3a40: 6d 61 6b 69 6e 67 20 69 74 20 74 68 65 20 66 69 making it the fi
3a50: 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66 rst character af
3a60: 74 65 72 20 27 5b 27 20 6f 72 20 27 5e 27 2e 20 ter '[' or '^'.
3a70: 20 41 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 63 A.** range of c
3a80: 68 61 72 61 63 74 65 72 73 20 63 61 6e 20 62 65 haracters can be
3a90: 20 73 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 specified using
3aa0: 20 27 2d 27 2e 20 20 45 78 61 6d 70 6c 65 3a 0a '-'. Example:.
3ab0: 2a 2a 20 22 5b 61 2d 7a 5d 22 20 6d 61 74 63 68 ** "[a-z]" match
3ac0: 65 73 20 61 6e 79 20 73 69 6e 67 6c 65 20 6c 6f es any single lo
3ad0: 77 65 72 2d 63 61 73 65 20 6c 65 74 74 65 72 2e wer-case letter.
3ae0: 20 20 54 6f 20 6d 61 74 63 68 20 61 20 27 2d 27 To match a '-'
3af0: 2c 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 74 68 65 , make.** it the
3b00: 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20 last character
3b10: 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a in the list..**.
3b20: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
3b30: 69 73 20 75 73 75 61 6c 6c 79 20 71 75 69 63 6b is usually quick
3b40: 2c 20 62 75 74 20 63 61 6e 20 62 65 20 4e 2a 2a , but can be N**
3b50: 32 20 69 6e 20 74 68 65 20 77 6f 72 73 74 20 63 2 in the worst c
3b60: 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 48 69 6e 74 73 ase..**.** Hints
3b70: 3a 20 74 6f 20 6d 61 74 63 68 20 27 2a 27 20 6f : to match '*' o
3b80: 72 20 27 3f 27 2c 20 70 75 74 20 74 68 65 6d 20 r '?', put them
3b90: 69 6e 20 22 5b 5d 22 2e 20 20 4c 69 6b 65 20 74 in "[]". Like t
3ba0: 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 his:.**.**
3bb0: 20 20 20 61 62 63 5b 2a 5d 78 79 7a 20 20 20 20 abc[*]xyz
3bc0: 20 20 20 20 4d 61 74 63 68 65 73 20 22 61 62 63 Matches "abc
3bd0: 2a 78 79 7a 22 20 6f 6e 6c 79 0a 2a 2f 0a 73 74 *xyz" only.*/.st
3be0: 61 74 69 63 20 69 6e 74 20 70 61 74 74 65 72 6e atic int pattern
3bf0: 43 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 Compare(. const
3c00: 20 75 38 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 u8 *zPattern,
3c10: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
3c20: 68 65 20 67 6c 6f 62 20 70 61 74 74 65 72 6e 20 he glob pattern
3c30: 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a */. const u8 *z
3c40: 53 74 72 69 6e 67 2c 20 20 20 20 20 20 20 20 20 String,
3c50: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 /* The str
3c60: 69 6e 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 ing to compare a
3c70: 67 61 69 6e 73 74 20 74 68 65 20 67 6c 6f 62 20 gainst the glob
3c80: 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63 */. const struc
3c90: 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 t compareInfo *p
3ca0: 49 6e 66 6f 2c 20 2f 2a 20 49 6e 66 6f 72 6d 61 Info, /* Informa
3cb0: 74 69 6f 6e 20 61 62 6f 75 74 20 68 6f 77 20 74 tion about how t
3cc0: 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 65 o do the compare
3cd0: 20 2a 2f 0a 20 20 75 33 32 20 65 73 63 20 20 20 */. u32 esc
3ce0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3cf0: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 /* The es
3d00: 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20 2a cape character *
3d10: 2f 0a 29 7b 0a 20 20 75 33 32 20 63 2c 20 63 32 /.){. u32 c, c2
3d20: 3b 0a 20 20 69 6e 74 20 69 6e 76 65 72 74 3b 0a ;. int invert;.
3d30: 20 20 69 6e 74 20 73 65 65 6e 3b 0a 20 20 75 38 int seen;. u8
3d40: 20 6d 61 74 63 68 4f 6e 65 20 3d 20 70 49 6e 66 matchOne = pInf
3d50: 6f 2d 3e 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75 o->matchOne;. u
3d60: 38 20 6d 61 74 63 68 41 6c 6c 20 3d 20 70 49 6e 8 matchAll = pIn
3d70: 66 6f 2d 3e 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 fo->matchAll;.
3d80: 75 38 20 6d 61 74 63 68 53 65 74 20 3d 20 70 49 u8 matchSet = pI
3d90: 6e 66 6f 2d 3e 6d 61 74 63 68 53 65 74 3b 0a 20 nfo->matchSet;.
3da0: 20 75 38 20 6e 6f 43 61 73 65 20 3d 20 70 49 6e u8 noCase = pIn
3db0: 66 6f 2d 3e 6e 6f 43 61 73 65 3b 20 0a 20 20 69 fo->noCase; . i
3dc0: 6e 74 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 nt prevEscape =
3dd0: 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 0; /* True i
3de0: 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 f the previous c
3df0: 68 61 72 61 63 74 65 72 20 77 61 73 20 27 65 73 haracter was 'es
3e00: 63 61 70 65 27 20 2a 2f 0a 0a 20 20 77 68 69 6c cape' */.. whil
3e10: 65 28 20 28 63 20 3d 20 73 71 6c 69 74 65 33 55 e( (c = sqlite3U
3e20: 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e tf8Read(zPattern
3e30: 2c 26 7a 50 61 74 74 65 72 6e 29 29 21 3d 30 20 ,&zPattern))!=0
3e40: 29 7b 0a 20 20 20 20 69 66 28 20 21 70 72 65 76 ){. if( !prev
3e50: 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61 74 Escape && c==mat
3e60: 63 68 41 6c 6c 20 29 7b 0a 20 20 20 20 20 20 77 chAll ){. w
3e70: 68 69 6c 65 28 20 28 63 3d 73 71 6c 69 74 65 33 hile( (c=sqlite3
3e80: 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 Utf8Read(zPatter
3e90: 6e 2c 26 7a 50 61 74 74 65 72 6e 29 29 20 3d 3d n,&zPattern)) ==
3ea0: 20 6d 61 74 63 68 41 6c 6c 0a 20 20 20 20 20 20 matchAll.
3eb0: 20 20 20 20 20 20 20 20 20 7c 7c 20 63 20 3d 3d || c ==
3ec0: 20 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 matchOne ){.
3ed0: 20 20 20 20 20 69 66 28 20 63 3d 3d 6d 61 74 63 if( c==matc
3ee0: 68 4f 6e 65 20 26 26 20 73 71 6c 69 74 65 33 55 hOne && sqlite3U
3ef0: 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c tf8Read(zString,
3f00: 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b &zString)==0 ){
3f10: 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
3f20: 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 n 0;. }.
3f30: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
3f40: 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 c==0 ){.
3f50: 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 return 1;.
3f60: 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 65 73 }else if( c==es
3f70: 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 20 3d c ){. c =
3f80: 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
3f90: 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 (zPattern, &zPat
3fa0: 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 tern);. i
3fb0: 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 f( c==0 ){.
3fc0: 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
3fd0: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
3fe0: 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 else if( c==matc
3ff0: 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 hSet ){.
4000: 61 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29 assert( esc==0 )
4010: 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 ; /* Thi
4020: 73 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c s is GLOB, not L
4030: 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 IKE */. a
4040: 73 73 65 72 74 28 20 6d 61 74 63 68 53 65 74 3c ssert( matchSet<
4050: 30 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20 0x80 ); /* '['
4060: 69 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 is a single-byte
4070: 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 character */.
4080: 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 while( *zS
4090: 74 72 69 6e 67 20 26 26 20 70 61 74 74 65 72 6e tring && pattern
40a0: 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72 Compare(&zPatter
40b0: 6e 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 49 n[-1],zString,pI
40c0: 6e 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a 20 nfo,esc)==0 ){.
40d0: 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
40e0: 53 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69 6e SKIP_UTF8(zStrin
40f0: 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 g);. }.
4100: 20 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a 53 return *zS
4110: 74 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20 20 tring!=0;.
4120: 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 }. while( (
4130: 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 c2 = sqlite3Utf8
4140: 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 26 7a 53 Read(zString,&zS
4150: 74 72 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20 20 tring))!=0 ){.
4160: 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73 65 if( noCase
4170: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 47 6c ){. Gl
4180: 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 ogUpperToLower(c
4190: 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 47 6c 2);. Gl
41a0: 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 ogUpperToLower(c
41b0: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 );. whi
41c0: 6c 65 28 20 63 32 20 21 3d 20 30 20 26 26 20 63 le( c2 != 0 && c
41d0: 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 2 != c ){.
41e0: 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 c2 = sqlit
41f0: 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 e3Utf8Read(zStri
4200: 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 ng, &zString);.
4210: 20 20 20 20 20 20 20 20 20 20 20 47 6c 6f 67 55 GlogU
4220: 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b pperToLower(c2);
4230: 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
4240: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
4250: 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 while( c2
4260: 21 3d 20 30 20 26 26 20 63 32 20 21 3d 20 63 20 != 0 && c2 != c
4270: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 ){. c
4280: 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 2 = sqlite3Utf8R
4290: 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 ead(zString, &zS
42a0: 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 tring);.
42b0: 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
42c0: 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30 20 if( c2==0
42d0: 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 ) return 0;.
42e0: 20 20 20 20 69 66 28 20 70 61 74 74 65 72 6e 43 if( patternC
42f0: 6f 6d 70 61 72 65 28 7a 50 61 74 74 65 72 6e 2c ompare(zPattern,
4300: 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73 zString,pInfo,es
4310: 63 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 c) ) return 1;.
4320: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 }. ret
4330: 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 urn 0;. }else
4340: 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65 if( !prevEscape
4350: 20 26 26 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 && c==matchOne
4360: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c ){. if( sql
4370: 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 ite3Utf8Read(zSt
4380: 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3d ring, &zString)=
4390: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
43a0: 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a turn 0;. }.
43b0: 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d }else if( c=
43c0: 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a 20 20 20 =matchSet ){.
43d0: 20 20 20 75 33 32 20 70 72 69 6f 72 5f 63 20 3d u32 prior_c =
43e0: 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 0;. assert
43f0: 28 20 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 2f ( esc==0 ); /
4400: 2a 20 54 68 69 73 20 6f 6e 6c 79 20 6f 63 63 75 * This only occu
4410: 72 73 20 66 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74 rs for GLOB, not
4420: 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 73 LIKE */. s
4430: 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 een = 0;. i
4440: 6e 76 65 72 74 20 3d 20 30 3b 0a 20 20 20 20 20 nvert = 0;.
4450: 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 c = sqlite3Utf8
4460: 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a Read(zString, &z
4470: 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 String);. i
4480: 66 28 20 63 3d 3d 30 20 29 20 72 65 74 75 72 6e f( c==0 ) return
4490: 20 30 3b 0a 20 20 20 20 20 20 63 32 20 3d 20 73 0;. c2 = s
44a0: 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
44b0: 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 Pattern, &zPatte
44c0: 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 rn);. if( c
44d0: 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20 20 20 2=='^' ){.
44e0: 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a 20 20 invert = 1;.
44f0: 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 c2 = sqlit
4500: 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 e3Utf8Read(zPatt
4510: 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b ern, &zPattern);
4520: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
4530: 66 28 20 63 32 3d 3d 27 5d 27 20 29 7b 0a 20 20 f( c2==']' ){.
4540: 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 5d 27 if( c==']'
4550: 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 ) seen = 1;.
4560: 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 c2 = sqlite
4570: 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 3Utf8Read(zPatte
4580: 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a rn, &zPattern);.
4590: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 }. wh
45a0: 69 6c 65 28 20 63 32 20 26 26 20 63 32 21 3d 27 ile( c2 && c2!='
45b0: 5d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 ]' ){. if
45c0: 28 20 63 32 3d 3d 27 2d 27 20 26 26 20 7a 50 61 ( c2=='-' && zPa
45d0: 74 74 65 72 6e 5b 30 5d 21 3d 27 5d 27 20 26 26 ttern[0]!=']' &&
45e0: 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 30 20 zPattern[0]!=0
45f0: 26 26 20 70 72 69 6f 72 5f 63 3e 30 20 29 7b 0a && prior_c>0 ){.
4600: 20 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 c2 = s
4610: 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
4620: 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 Pattern, &zPatte
4630: 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 rn);. i
4640: 66 28 20 63 3e 3d 70 72 69 6f 72 5f 63 20 26 26 f( c>=prior_c &&
4650: 20 63 3c 3d 63 32 20 29 20 73 65 65 6e 20 3d 20 c<=c2 ) seen =
4660: 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 69 1;. pri
4670: 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 or_c = 0;.
4680: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
4690: 20 20 20 69 66 28 20 63 3d 3d 63 32 20 29 7b 0a if( c==c2 ){.
46a0: 20 20 20 20 20 20 20 20 20 20 20 20 73 65 65 6e seen
46b0: 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 = 1;.
46c0: 7d 0a 20 20 20 20 20 20 20 20 20 20 70 72 69 6f }. prio
46d0: 72 5f 63 20 3d 20 63 32 3b 0a 20 20 20 20 20 20 r_c = c2;.
46e0: 20 20 7d 0a 20 20 20 20 20 20 20 20 63 32 20 3d }. c2 =
46f0: 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
4700: 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 (zPattern, &zPat
4710: 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 tern);. }.
4720: 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30 20 7c if( c2==0 |
4730: 7c 20 28 73 65 65 6e 20 5e 20 69 6e 76 65 72 74 | (seen ^ invert
4740: 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
4750: 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 return 0;.
4760: 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
4770: 65 73 63 3d 3d 63 20 26 26 20 21 70 72 65 76 45 esc==c && !prevE
4780: 73 63 61 70 65 20 29 7b 0a 20 20 20 20 20 20 70 scape ){. p
4790: 72 65 76 45 73 63 61 70 65 20 3d 20 31 3b 0a 20 revEscape = 1;.
47a0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
47b0: 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 c2 = sqlite3Utf8
47c0: 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a Read(zString, &z
47d0: 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 String);. i
47e0: 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 f( noCase ){.
47f0: 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f GlogUpperTo
4800: 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20 20 Lower(c);.
4810: 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 GlogUpperToLow
4820: 65 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d 0a er(c2);. }.
4830: 20 20 20 20 20 20 69 66 28 20 63 21 3d 63 32 20 if( c!=c2
4840: 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
4850: 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 n 0;. }.
4860: 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 prevEscape =
4870: 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 0;. }. }. r
4880: 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d eturn *zString==
4890: 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 0;.}../*.** Coun
48a0: 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
48b0: 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 4c times that the L
48c0: 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f 72 IKE operator (or
48d0: 20 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a 2a GLOB which is.*
48e0: 2a 20 6a 75 73 74 20 61 20 76 61 72 69 61 74 69 * just a variati
48f0: 6f 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74 73 on of LIKE) gets
4900: 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 called. This i
4910: 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 s used for testi
4920: 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 ng.** only..*/.#
4930: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
4940: 54 0a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 T.int sqlite3_li
4950: 6b 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 ke_count = 0;.#e
4960: 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 ndif.../*.** Imp
4970: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
4980: 68 65 20 6c 69 6b 65 28 29 20 53 51 4c 20 66 75 he like() SQL fu
4990: 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 nction. This fu
49a0: 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 nction implement
49b0: 73 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 s.** the build-i
49c0: 6e 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2e n LIKE operator.
49d0: 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 The first argu
49e0: 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 ment to the func
49f0: 74 69 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 70 tion is the.** p
4a00: 61 74 74 65 72 6e 20 61 6e 64 20 74 68 65 20 73 attern and the s
4a10: 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
4a20: 73 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 53 s the string. S
4a30: 6f 2c 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 o, the SQL state
4a40: 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ments:.**.**
4a50: 20 20 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a A LIKE B.**.*
4a60: 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 * is implemented
4a70: 20 61 73 20 6c 69 6b 65 28 42 2c 41 29 2e 0a 2a as like(B,A)..*
4a80: 2a 0a 2a 2a 20 54 68 69 73 20 73 61 6d 65 20 66 *.** This same f
4a90: 75 6e 63 74 69 6f 6e 20 28 77 69 74 68 20 61 20 unction (with a
4aa0: 64 69 66 66 65 72 65 6e 74 20 63 6f 6d 70 61 72 different compar
4ab0: 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 29 eInfo structure)
4ac0: 20 63 6f 6d 70 75 74 65 73 0a 2a 2a 20 74 68 65 computes.** the
4ad0: 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 2e 0a GLOB operator..
4ae0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c */.static void l
4af0: 69 6b 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 ikeFunc(. sqlit
4b00: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
4b10: 65 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 ext, . int argc
4b20: 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c , . sqlite3_val
4b30: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 ue **argv.){. c
4b40: 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
4b50: 61 72 20 2a 7a 41 2c 20 2a 7a 42 3b 0a 20 20 75 ar *zA, *zB;. u
4b60: 33 32 20 65 73 63 61 70 65 20 3d 20 30 3b 0a 20 32 escape = 0;.
4b70: 20 69 6e 74 20 6e 50 61 74 3b 0a 20 20 73 71 6c int nPat;. sql
4b80: 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 ite3 *db = sqlit
4b90: 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 e3_context_db_ha
4ba0: 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a ndle(context);..
4bb0: 20 20 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76 zB = sqlite3_v
4bc0: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
4bd0: 5d 29 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74 ]);. zA = sqlit
4be0: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
4bf0: 67 76 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69 gv[1]);.. /* Li
4c00: 6d 69 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f mit the length o
4c10: 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c f the LIKE or GL
4c20: 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76 OB pattern to av
4c30: 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a oid problems. *
4c40: 2a 20 6f 66 20 64 65 65 70 20 72 65 63 75 72 73 * of deep recurs
4c50: 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61 ion and N*N beha
4c60: 76 69 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43 vior in patternC
4c70: 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20 ompare().. */.
4c80: 20 6e 50 61 74 20 3d 20 73 71 6c 69 74 65 33 5f nPat = sqlite3_
4c90: 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 value_bytes(argv
4ca0: 5b 30 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 [0]);. testcase
4cb0: 28 20 6e 50 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d ( nPat==db->aLim
4cc0: 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
4cd0: 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e LIKE_PATTERN_LEN
4ce0: 47 54 48 5d 20 29 3b 0a 20 20 74 65 73 74 63 61 GTH] );. testca
4cf0: 73 65 28 20 6e 50 61 74 3d 3d 64 62 2d 3e 61 4c se( nPat==db->aL
4d00: 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
4d10: 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c T_LIKE_PATTERN_L
4d20: 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 69 66 ENGTH]+1 );. if
4d30: 28 20 6e 50 61 74 20 3e 20 64 62 2d 3e 61 4c 69 ( nPat > db->aLi
4d40: 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
4d50: 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 _LIKE_PATTERN_LE
4d60: 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c NGTH] ){. sql
4d70: 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
4d80: 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b 45 r(context, "LIKE
4d90: 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e or GLOB pattern
4da0: 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20 2d too complex", -
4db0: 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 1);. return;.
4dc0: 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 42 }. assert( zB
4dd0: 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ==sqlite3_value_
4de0: 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b text(argv[0]) );
4df0: 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 64 69 /* Encoding di
4e00: 64 20 6e 6f 74 20 63 68 61 6e 67 65 20 2a 2f 0a d not change */.
4e10: 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29 . if( argc==3 )
4e20: 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 {. /* The esc
4e30: 61 70 65 20 63 68 61 72 61 63 74 65 72 20 73 74 ape character st
4e40: 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69 73 ring must consis
4e50: 74 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 55 54 t of a single UT
4e60: 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 F-8 character..
4e70: 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c ** Otherwise,
4e80: 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 return an error
4e90: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 6f 6e .. */. con
4ea0: 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
4eb0: 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69 74 65 33 *zEsc = sqlite3
4ec0: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
4ed0: 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 [2]);. if( zE
4ee0: 73 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a sc==0 ) return;.
4ef0: 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 55 if( sqlite3U
4f00: 74 66 38 43 68 61 72 4c 65 6e 28 28 63 68 61 72 tf8CharLen((char
4f10: 2a 29 7a 45 73 63 2c 20 2d 31 29 21 3d 31 20 29 *)zEsc, -1)!=1 )
4f20: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
4f30: 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e result_error(con
4f40: 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20 20 text, .
4f50: 20 22 45 53 43 41 50 45 20 65 78 70 72 65 73 73 "ESCAPE express
4f60: 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 20 73 69 ion must be a si
4f70: 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72 22 2c ngle character",
4f80: 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 -1);. retu
4f90: 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 73 rn;. }. es
4fa0: 63 61 70 65 20 3d 20 73 71 6c 69 74 65 33 55 74 cape = sqlite3Ut
4fb0: 66 38 52 65 61 64 28 7a 45 73 63 2c 20 26 7a 45 f8Read(zEsc, &zE
4fc0: 73 63 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a sc);. }. if( z
4fd0: 41 20 26 26 20 7a 42 20 29 7b 0a 20 20 20 20 73 A && zB ){. s
4fe0: 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 truct compareInf
4ff0: 6f 20 2a 70 49 6e 66 6f 20 3d 20 73 71 6c 69 74 o *pInfo = sqlit
5000: 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e e3_user_data(con
5010: 74 65 78 74 29 3b 0a 23 69 66 64 65 66 20 53 51 text);.#ifdef SQ
5020: 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 LITE_TEST. sq
5030: 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74 lite3_like_count
5040: 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 0a ++;.#endif. .
5050: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
5060: 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 lt_int(context,
5070: 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a patternCompare(z
5080: 42 2c 20 7a 41 2c 20 70 49 6e 66 6f 2c 20 65 73 B, zA, pInfo, es
5090: 63 61 70 65 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f cape));. }.}../
50a0: 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
50b0: 69 6f 6e 20 6f 66 20 74 68 65 20 4e 55 4c 4c 49 ion of the NULLI
50c0: 46 28 78 2c 79 29 20 66 75 6e 63 74 69 6f 6e 2e F(x,y) function.
50d0: 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 The result is
50e0: 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 the first.** arg
50f0: 75 6d 65 6e 74 20 69 66 20 74 68 65 20 61 72 67 ument if the arg
5100: 75 6d 65 6e 74 73 20 61 72 65 20 64 69 66 66 65 uments are diffe
5110: 72 65 6e 74 2e 20 20 54 68 65 20 72 65 73 75 6c rent. The resul
5120: 74 20 69 73 20 4e 55 4c 4c 20 69 66 20 74 68 65 t is NULL if the
5130: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 61 72 .** arguments ar
5140: 65 20 65 71 75 61 6c 20 74 6f 20 65 61 63 68 20 e equal to each
5150: 6f 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 other..*/.static
5160: 20 76 6f 69 64 20 6e 75 6c 6c 69 66 46 75 6e 63 void nullifFunc
5170: 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
5180: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
5190: 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 int NotUsed,. s
51a0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
51b0: 72 67 76 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 rgv.){. CollSeq
51c0: 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 *pColl = sqlite
51d0: 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 3GetFuncCollSeq(
51e0: 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 context);. UNUS
51f0: 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
5200: 55 73 65 64 29 3b 0a 20 20 69 66 28 20 73 71 6c Used);. if( sql
5210: 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 61 ite3MemCompare(a
5220: 72 67 76 5b 30 5d 2c 20 61 72 67 76 5b 31 5d 2c rgv[0], argv[1],
5230: 20 70 43 6f 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 pColl)!=0 ){.
5240: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
5250: 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 _value(context,
5260: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7d 0a 7d 0a argv[0]);. }.}.
5270: 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
5280: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
5290: 69 74 65 5f 76 65 72 73 69 6f 6e 28 29 20 66 75 ite_version() fu
52a0: 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 nction. The res
52b0: 75 6c 74 20 69 73 20 74 68 65 20 76 65 72 73 69 ult is the versi
52c0: 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c on.** of the SQL
52d0: 69 74 65 20 6c 69 62 72 61 72 79 20 74 68 61 74 ite library that
52e0: 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a is running..*/.
52f0: 73 74 61 74 69 63 20 76 6f 69 64 20 76 65 72 73 static void vers
5300: 69 6f 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 ionFunc(. sqlit
5310: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
5320: 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 ext,. int NotUs
5330: 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 ed,. sqlite3_va
5340: 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 lue **NotUsed2.)
5350: 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
5360: 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e ETER2(NotUsed, N
5370: 6f 74 55 73 65 64 32 29 3b 0a 20 20 2f 2a 20 49 otUsed2);. /* I
5380: 4d 50 3a 20 52 2d 34 38 36 39 39 2d 34 38 36 31 MP: R-48699-4861
5390: 37 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 7 This function
53a0: 69 73 20 61 6e 20 53 51 4c 20 77 72 61 70 70 65 is an SQL wrappe
53b0: 72 20 61 72 6f 75 6e 64 20 74 68 65 0a 20 20 2a r around the. *
53c0: 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 * sqlite3_libver
53d0: 73 69 6f 6e 28 29 20 43 2d 69 6e 74 65 72 66 61 sion() C-interfa
53e0: 63 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ce. */. sqlite3
53f0: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
5400: 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 6c 69 text, sqlite3_li
5410: 62 76 65 72 73 69 6f 6e 28 29 2c 20 2d 31 2c 20 bversion(), -1,
5420: 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a SQLITE_STATIC);.
5430: 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
5440: 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ntation of the s
5450: 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 qlite_source_id(
5460: 29 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 ) function. The
5470: 72 65 73 75 6c 74 20 69 73 20 61 20 73 74 72 69 result is a stri
5480: 6e 67 0a 2a 2a 20 74 68 61 74 20 69 64 65 6e 74 ng.** that ident
5490: 69 66 69 65 73 20 74 68 65 20 70 61 72 74 69 63 ifies the partic
54a0: 75 6c 61 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 ular version of
54b0: 74 68 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20 the source code
54c0: 75 73 65 64 20 74 6f 20 62 75 69 6c 64 0a 2a 2a used to build.**
54d0: 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 74 61 74 SQLite..*/.stat
54e0: 69 63 20 76 6f 69 64 20 73 6f 75 72 63 65 69 64 ic void sourceid
54f0: 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
5500: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
5510: 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c ,. int NotUsed,
5520: 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
5530: 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 **NotUsed2.){.
5540: 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
5550: 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 R2(NotUsed, NotU
5560: 73 65 64 32 29 3b 0a 20 20 2f 2a 20 49 4d 50 3a sed2);. /* IMP:
5570: 20 52 2d 32 34 34 37 30 2d 33 31 31 33 36 20 54 R-24470-31136 T
5580: 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
5590: 61 6e 20 53 51 4c 20 77 72 61 70 70 65 72 20 61 an SQL wrapper a
55a0: 72 6f 75 6e 64 20 74 68 65 0a 20 20 2a 2a 20 73 round the. ** s
55b0: 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 qlite3_sourceid(
55c0: 29 20 43 20 69 6e 74 65 72 66 61 63 65 2e 20 2a ) C interface. *
55d0: 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 /. sqlite3_resu
55e0: 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
55f0: 20 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 sqlite3_sourcei
5600: 64 28 29 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f d(), -1, SQLITE_
5610: 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a STATIC);.}../*.*
5620: 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
5630: 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 6c of the sqlite_l
5640: 6f 67 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 og() function.
5650: 54 68 69 73 20 69 73 20 61 20 77 72 61 70 70 65 This is a wrappe
5660: 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 73 71 6c 69 r around.** sqli
5670: 74 65 33 5f 6c 6f 67 28 29 2e 20 20 54 68 65 20 te3_log(). The
5680: 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 return value is
5690: 4e 55 4c 4c 2e 20 20 54 68 65 20 66 75 6e 63 74 NULL. The funct
56a0: 69 6f 6e 20 65 78 69 73 74 73 20 70 75 72 65 6c ion exists purel
56b0: 79 20 66 6f 72 0a 2a 2a 20 69 74 73 20 73 69 64 y for.** its sid
56c0: 65 2d 65 66 66 65 63 74 73 2e 0a 2a 2f 0a 73 74 e-effects..*/.st
56d0: 61 74 69 63 20 76 6f 69 64 20 65 72 72 6c 6f 67 atic void errlog
56e0: 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
56f0: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
5700: 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
5710: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
5720: 61 72 67 76 0a 29 7b 0a 20 20 55 4e 55 53 45 44 argv.){. UNUSED
5730: 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 _PARAMETER(argc)
5740: 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
5750: 45 54 45 52 28 63 6f 6e 74 65 78 74 29 3b 0a 20 ETER(context);.
5760: 20 73 71 6c 69 74 65 33 5f 6c 6f 67 28 73 71 6c sqlite3_log(sql
5770: 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 ite3_value_int(a
5780: 72 67 76 5b 30 5d 29 2c 20 22 25 73 22 2c 20 73 rgv[0]), "%s", s
5790: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
57a0: 74 28 61 72 67 76 5b 31 5d 29 29 3b 0a 7d 0a 0a t(argv[1]));.}..
57b0: 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
57c0: 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
57d0: 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e te_compileoption
57e0: 5f 75 73 65 64 28 29 20 66 75 6e 63 74 69 6f 6e _used() function
57f0: 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 ..** The result
5800: 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 is an integer th
5810: 61 74 20 69 64 65 6e 74 69 66 69 65 73 20 69 66 at identifies if
5820: 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 6f 70 the compiler op
5830: 74 69 6f 6e 0a 2a 2a 20 77 61 73 20 75 73 65 64 tion.** was used
5840: 20 74 6f 20 62 75 69 6c 64 20 53 51 4c 69 74 65 to build SQLite
5850: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c ..*/.#ifndef SQL
5860: 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 ITE_OMIT_COMPILE
5870: 4f 50 54 49 4f 4e 5f 44 49 41 47 53 0a 73 74 61 OPTION_DIAGS.sta
5880: 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 69 6c 65 tic void compile
5890: 6f 70 74 69 6f 6e 75 73 65 64 46 75 6e 63 28 0a optionusedFunc(.
58a0: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
58b0: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
58c0: 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
58d0: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
58e0: 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a {. const char *
58f0: 7a 4f 70 74 4e 61 6d 65 3b 0a 20 20 61 73 73 65 zOptName;. asse
5900: 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 rt( argc==1 );.
5910: 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
5920: 52 28 61 72 67 63 29 3b 0a 20 20 2f 2a 20 49 4d R(argc);. /* IM
5930: 50 3a 20 52 2d 33 39 35 36 34 2d 33 36 33 30 35 P: R-39564-36305
5940: 20 54 68 65 20 73 71 6c 69 74 65 5f 63 6f 6d 70 The sqlite_comp
5950: 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 28 29 ileoption_used()
5960: 20 53 51 4c 0a 20 20 2a 2a 20 66 75 6e 63 74 69 SQL. ** functi
5970: 6f 6e 20 69 73 20 61 20 77 72 61 70 70 65 72 20 on is a wrapper
5980: 61 72 6f 75 6e 64 20 74 68 65 20 73 71 6c 69 74 around the sqlit
5990: 65 33 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e e3_compileoption
59a0: 5f 75 73 65 64 28 29 20 43 2f 43 2b 2b 0a 20 20 _used() C/C++.
59b0: 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a ** function.. *
59c0: 2f 0a 20 20 69 66 28 20 28 7a 4f 70 74 4e 61 6d /. if( (zOptNam
59d0: 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a e = (const char*
59e0: 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
59f0: 65 78 74 28 61 72 67 76 5b 30 5d 29 29 21 3d 30 ext(argv[0]))!=0
5a00: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
5a10: 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 result_int(conte
5a20: 78 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 xt, sqlite3_comp
5a30: 69 6c 65 6f 70 74 69 6f 6e 5f 75 73 65 64 28 7a ileoption_used(z
5a40: 4f 70 74 4e 61 6d 65 29 29 3b 0a 20 20 7d 0a 7d OptName));. }.}
5a50: 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
5a60: 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 4f 50 E_OMIT_COMPILEOP
5a70: 54 49 4f 4e 5f 44 49 41 47 53 20 2a 2f 0a 0a 2f TION_DIAGS */../
5a80: 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
5a90: 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
5aa0: 65 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f e_compileoption_
5ab0: 67 65 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 get() function.
5ac0: 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 .** The result i
5ad0: 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 s a string that
5ae0: 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 63 identifies the c
5af0: 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6f 6e 73 20 ompiler options
5b00: 0a 2a 2a 20 75 73 65 64 20 74 6f 20 62 75 69 6c .** used to buil
5b10: 64 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 69 66 d SQLite..*/.#if
5b20: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
5b30: 5f 43 4f 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 _COMPILEOPTION_D
5b40: 49 41 47 53 0a 73 74 61 74 69 63 20 76 6f 69 64 IAGS.static void
5b50: 20 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 67 65 compileoptionge
5b60: 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 tFunc(. sqlite3
5b70: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
5b80: 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
5b90: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
5ba0: 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6e *argv.){. int n
5bb0: 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 ;. assert( argc
5bc0: 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f ==1 );. UNUSED_
5bd0: 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b PARAMETER(argc);
5be0: 0a 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 30 34 39 . /* IMP: R-049
5bf0: 32 32 2d 32 34 30 37 36 20 54 68 65 20 73 71 6c 22-24076 The sql
5c00: 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f ite_compileoptio
5c10: 6e 5f 67 65 74 28 29 20 53 51 4c 20 66 75 6e 63 n_get() SQL func
5c20: 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 61 20 77 tion. ** is a w
5c30: 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 rapper around th
5c40: 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c e sqlite3_compil
5c50: 65 6f 70 74 69 6f 6e 5f 67 65 74 28 29 20 43 2f eoption_get() C/
5c60: 43 2b 2b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 C++ function..
5c70: 2a 2f 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 */. n = sqlite3
5c80: 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b _value_int(argv[
5c90: 30 5d 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 0]);. sqlite3_r
5ca0: 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
5cb0: 78 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 xt, sqlite3_comp
5cc0: 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 28 6e 29 ileoption_get(n)
5cd0: 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 , -1, SQLITE_STA
5ce0: 54 49 43 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f TIC);.}.#endif /
5cf0: 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f * SQLITE_OMIT_CO
5d00: 4d 50 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 MPILEOPTION_DIAG
5d10: 53 20 2a 2f 0a 0a 2f 2a 20 41 72 72 61 79 20 66 S */../* Array f
5d20: 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 or converting fr
5d30: 6f 6d 20 68 61 6c 66 2d 62 79 74 65 73 20 28 6e om half-bytes (n
5d40: 79 62 62 6c 65 73 29 20 69 6e 74 6f 20 41 53 43 ybbles) into ASC
5d50: 49 49 20 68 65 78 0a 2a 2a 20 64 69 67 69 74 73 II hex.** digits
5d60: 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 . */.static cons
5d70: 74 20 63 68 61 72 20 68 65 78 64 69 67 69 74 73 t char hexdigits
5d80: 5b 5d 20 3d 20 7b 0a 20 20 27 30 27 2c 20 27 31 [] = {. '0', '1
5d90: 27 2c 20 27 32 27 2c 20 27 33 27 2c 20 27 34 27 ', '2', '3', '4'
5da0: 2c 20 27 35 27 2c 20 27 36 27 2c 20 27 37 27 2c , '5', '6', '7',
5db0: 0a 20 20 27 38 27 2c 20 27 39 27 2c 20 27 41 27 . '8', '9', 'A'
5dc0: 2c 20 27 42 27 2c 20 27 43 27 2c 20 27 44 27 2c , 'B', 'C', 'D',
5dd0: 20 27 45 27 2c 20 27 46 27 20 0a 7d 3b 0a 0a 2f 'E', 'F' .};../
5de0: 2a 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 *.** EXPERIMENTA
5df0: 4c 20 2d 20 54 68 69 73 20 69 73 20 6e 6f 74 20 L - This is not
5e00: 61 6e 20 6f 66 66 69 63 69 61 6c 20 66 75 6e 63 an official func
5e10: 74 69 6f 6e 2e 20 20 54 68 65 20 69 6e 74 65 72 tion. The inter
5e20: 66 61 63 65 20 6d 61 79 0a 2a 2a 20 63 68 61 6e face may.** chan
5e30: 67 65 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 ge. This functi
5e40: 6f 6e 20 6d 61 79 20 64 69 73 61 70 70 65 61 72 on may disappear
5e50: 2e 20 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 . Do not write
5e60: 63 6f 64 65 20 74 68 61 74 20 64 65 70 65 6e 64 code that depend
5e70: 73 0a 2a 2a 20 6f 6e 20 74 68 69 73 20 66 75 6e s.** on this fun
5e80: 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 ction..**.** Imp
5e90: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
5ea0: 68 65 20 51 55 4f 54 45 28 29 20 66 75 6e 63 74 he QUOTE() funct
5eb0: 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 ion. This funct
5ec0: 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 ion takes a sing
5ed0: 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 le.** argument.
5ee0: 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 If the argument
5ef0: 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 is numeric, the
5f00: 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 return value is
5f10: 20 74 68 65 20 73 61 6d 65 20 61 73 0a 2a 2a 20 the same as.**
5f20: 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 the argument. I
5f30: 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 f the argument i
5f40: 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 74 75 s NULL, the retu
5f50: 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 rn value is the
5f60: 73 74 72 69 6e 67 0a 2a 2a 20 22 4e 55 4c 4c 22 string.** "NULL"
5f70: 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 . Otherwise, th
5f80: 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 65 6e e argument is en
5f90: 63 6c 6f 73 65 64 20 69 6e 20 73 69 6e 67 6c 65 closed in single
5fa0: 20 71 75 6f 74 65 73 20 77 69 74 68 0a 2a 2a 20 quotes with.**
5fb0: 73 69 6e 67 6c 65 2d 71 75 6f 74 65 20 65 73 63 single-quote esc
5fc0: 61 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 apes..*/.static
5fd0: 76 6f 69 64 20 71 75 6f 74 65 46 75 6e 63 28 73 void quoteFunc(s
5fe0: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
5ff0: 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 context, int arg
6000: 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 c, sqlite3_value
6010: 20 2a 2a 61 72 67 76 29 7b 0a 20 20 61 73 73 65 **argv){. asse
6020: 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 rt( argc==1 );.
6030: 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
6040: 52 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 R(argc);. switc
6050: 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 h( sqlite3_value
6060: 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 _type(argv[0]) )
6070: 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
6080: 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 E_INTEGER:. c
6090: 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 ase SQLITE_FLOAT
60a0: 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
60b0: 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 3_result_value(c
60c0: 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 ontext, argv[0])
60d0: 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
60e0: 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 }. case SQ
60f0: 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 LITE_BLOB: {.
6100: 20 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d char *zText =
6110: 20 30 3b 0a 20 20 20 20 20 20 63 68 61 72 20 63 0;. char c
6120: 6f 6e 73 74 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 onst *zBlob = sq
6130: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
6140: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 (argv[0]);.
6150: 20 69 6e 74 20 6e 42 6c 6f 62 20 3d 20 73 71 6c int nBlob = sql
6160: 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
6170: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 (argv[0]);.
6180: 20 61 73 73 65 72 74 28 20 7a 42 6c 6f 62 3d 3d assert( zBlob==
6190: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c sqlite3_value_bl
61a0: 6f 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 2f ob(argv[0]) ); /
61b0: 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 * No encoding ch
61c0: 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 ange */. zT
61d0: 65 78 74 20 3d 20 28 63 68 61 72 20 2a 29 63 6f ext = (char *)co
61e0: 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 ntextMalloc(cont
61f0: 65 78 74 2c 20 28 32 2a 28 69 36 34 29 6e 42 6c ext, (2*(i64)nBl
6200: 6f 62 29 2b 34 29 3b 20 0a 20 20 20 20 20 20 69 ob)+4); . i
6210: 66 28 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 f( zText ){.
6220: 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 int i;.
6230: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 for(i=0; i<nB
6240: 6c 6f 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 lob; i++){.
6250: 20 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 zText[(i*2)
6260: 2b 32 5d 20 3d 20 68 65 78 64 69 67 69 74 73 5b +2] = hexdigits[
6270: 28 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 29 26 30 78 (zBlob[i]>>4)&0x
6280: 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 0F];. z
6290: 54 65 78 74 5b 28 69 2a 32 29 2b 33 5d 20 3d 20 Text[(i*2)+3] =
62a0: 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62 hexdigits[(zBlob
62b0: 5b 69 5d 29 26 30 78 30 46 5d 3b 0a 20 20 20 20 [i])&0x0F];.
62c0: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 54 }. zT
62d0: 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d ext[(nBlob*2)+2]
62e0: 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 = '\'';.
62f0: 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 zText[(nBlob*2)
6300: 2b 33 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 +3] = '\0';.
6310: 20 20 20 20 7a 54 65 78 74 5b 30 5d 20 3d 20 27 zText[0] = '
6320: 58 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 X';. zTex
6330: 74 5b 31 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 t[1] = '\'';.
6340: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
6350: 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
6360: 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 53 51 4c , zText, -1, SQL
6370: 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a ITE_TRANSIENT);.
6380: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
6390: 66 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20 20 free(zText);.
63a0: 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
63b0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
63c0: 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a SQLITE_TEXT: {.
63d0: 20 20 20 20 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 int i,j;.
63e0: 20 20 20 20 20 75 36 34 20 6e 3b 0a 20 20 20 20 u64 n;.
63f0: 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
6400: 20 63 68 61 72 20 2a 7a 41 72 67 20 3d 20 73 71 char *zArg = sq
6410: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
6420: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 (argv[0]);.
6430: 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 char *z;..
6440: 20 69 66 28 20 7a 41 72 67 3d 3d 30 20 29 20 72 if( zArg==0 ) r
6450: 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 66 6f 72 eturn;. for
6460: 28 69 3d 30 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b (i=0, n=0; zArg[
6470: 69 5d 3b 20 69 2b 2b 29 7b 20 69 66 28 20 7a 41 i]; i++){ if( zA
6480: 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b rg[i]=='\'' ) n+
6490: 2b 3b 20 7d 0a 20 20 20 20 20 20 7a 20 3d 20 63 +; }. z = c
64a0: 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e ontextMalloc(con
64b0: 74 65 78 74 2c 20 28 28 69 36 34 29 69 29 2b 28 text, ((i64)i)+(
64c0: 28 69 36 34 29 6e 29 2b 33 29 3b 0a 20 20 20 20 (i64)n)+3);.
64d0: 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 if( z ){.
64e0: 20 20 20 7a 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a z[0] = '\'';.
64f0: 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c for(i=0,
6500: 20 6a 3d 31 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 j=1; zArg[i]; i
6510: 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a ++){. z
6520: 5b 6a 2b 2b 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b [j++] = zArg[i];
6530: 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a . if( z
6540: 41 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a Arg[i]=='\'' ){.
6550: 20 20 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b z[j+
6560: 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 +] = '\'';.
6570: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
6580: 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 . z[j++]
6590: 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 = '\'';.
65a0: 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 z[j] = 0;.
65b0: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
65c0: 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a _text(context, z
65d0: 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 , j, sqlite3_fre
65e0: 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
65f0: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
6600: 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
6610: 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 assert( sqli
6620: 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 te3_value_type(a
6630: 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f rgv[0])==SQLITE_
6640: 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73 71 NULL );. sq
6650: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
6660: 74 28 63 6f 6e 74 65 78 74 2c 20 22 4e 55 4c 4c t(context, "NULL
6670: 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41 ", 4, SQLITE_STA
6680: 54 49 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 TIC);. brea
6690: 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a k;. }. }.}..
66a0: 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 78 28 29 20 /*.** The hex()
66b0: 66 75 6e 63 74 69 6f 6e 2e 20 20 49 6e 74 65 72 function. Inter
66c0: 70 72 65 74 20 74 68 65 20 61 72 67 75 6d 65 6e pret the argumen
66d0: 74 20 61 73 20 61 20 62 6c 6f 62 2e 20 20 52 65 t as a blob. Re
66e0: 74 75 72 6e 0a 2a 2a 20 61 20 68 65 78 61 64 65 turn.** a hexade
66f0: 63 69 6d 61 6c 20 72 65 6e 64 65 72 69 6e 67 20 cimal rendering
6700: 61 73 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 as text..*/.stat
6710: 69 63 20 76 6f 69 64 20 68 65 78 46 75 6e 63 28 ic void hexFunc(
6720: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
6730: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
6740: 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
6750: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
6760: 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 ){. int i, n;.
6770: 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
6780: 63 68 61 72 20 2a 70 42 6c 6f 62 3b 0a 20 20 63 char *pBlob;. c
6790: 68 61 72 20 2a 7a 48 65 78 2c 20 2a 7a 3b 0a 20 har *zHex, *z;.
67a0: 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 assert( argc==1
67b0: 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 );. UNUSED_PAR
67c0: 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 AMETER(argc);.
67d0: 70 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f pBlob = sqlite3_
67e0: 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b value_blob(argv[
67f0: 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 0]);. n = sqlit
6800: 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
6810: 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 rgv[0]);. asser
6820: 74 28 20 70 42 6c 6f 62 3d 3d 73 71 6c 69 74 65 t( pBlob==sqlite
6830: 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 3_value_blob(arg
6840: 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 v[0]) ); /* No
6850: 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 encoding change
6860: 2a 2f 0a 20 20 7a 20 3d 20 7a 48 65 78 20 3d 20 */. z = zHex =
6870: 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f contextMalloc(co
6880: 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2a ntext, ((i64)n)*
6890: 32 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 7a 48 2 + 1);. if( zH
68a0: 65 78 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d ex ){. for(i=
68b0: 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c 0; i<n; i++, pBl
68c0: 6f 62 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e 73 ob++){. uns
68d0: 69 67 6e 65 64 20 63 68 61 72 20 63 20 3d 20 2a igned char c = *
68e0: 70 42 6c 6f 62 3b 0a 20 20 20 20 20 20 2a 28 7a pBlob;. *(z
68f0: 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b ++) = hexdigits[
6900: 28 63 3e 3e 34 29 26 30 78 66 5d 3b 0a 20 20 20 (c>>4)&0xf];.
6910: 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 *(z++) = hexd
6920: 69 67 69 74 73 5b 63 26 30 78 66 5d 3b 0a 20 20 igits[c&0xf];.
6930: 20 20 7d 0a 20 20 20 20 2a 7a 20 3d 20 30 3b 0a }. *z = 0;.
6940: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
6950: 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
6960: 20 7a 48 65 78 2c 20 6e 2a 32 2c 20 73 71 6c 69 zHex, n*2, sqli
6970: 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d te3_free);. }.}
6980: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 7a 65 72 6f ../*.** The zero
6990: 62 6c 6f 62 28 4e 29 20 66 75 6e 63 74 69 6f 6e blob(N) function
69a0: 20 72 65 74 75 72 6e 73 20 61 20 7a 65 72 6f 2d returns a zero-
69b0: 66 69 6c 6c 65 64 20 62 6c 6f 62 20 6f 66 20 73 filled blob of s
69c0: 69 7a 65 20 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a ize N bytes..*/.
69d0: 73 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f static void zero
69e0: 62 6c 6f 62 46 75 6e 63 28 0a 20 20 73 71 6c 69 blobFunc(. sqli
69f0: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
6a00: 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
6a10: 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
6a20: 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 36 e **argv.){. i6
6a30: 34 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 4 n;. sqlite3 *
6a40: 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
6a50: 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
6a60: 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72 ontext);. asser
6a70: 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 t( argc==1 );.
6a80: 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
6a90: 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73 71 (argc);. n = sq
6aa0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 lite3_value_int6
6ab0: 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65 4(argv[0]);. te
6ac0: 73 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e 61 stcase( n==db->a
6ad0: 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
6ae0: 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 IT_LENGTH] );.
6af0: 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d testcase( n==db-
6b00: 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
6b10: 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 IMIT_LENGTH]+1 )
6b20: 3b 0a 20 20 69 66 28 20 6e 3e 64 62 2d 3e 61 4c ;. if( n>db->aL
6b30: 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
6b40: 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
6b50: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
6b60: 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e error_toobig(con
6b70: 74 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a text);. }else{.
6b80: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
6b90: 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e 74 lt_zeroblob(cont
6ba0: 65 78 74 2c 20 28 69 6e 74 29 6e 29 3b 20 2f 2a ext, (int)n); /*
6bb0: 20 49 4d 50 3a 20 52 2d 30 30 32 39 33 2d 36 34 IMP: R-00293-64
6bc0: 39 39 34 20 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 994 */. }.}../*
6bd0: 0a 2a 2a 20 54 68 65 20 72 65 70 6c 61 63 65 28 .** The replace(
6be0: 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 72 ) function. Thr
6bf0: 65 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 ee arguments are
6c00: 20 61 6c 6c 20 73 74 72 69 6e 67 73 3a 20 63 61 all strings: ca
6c10: 6c 6c 0a 2a 2a 20 74 68 65 6d 20 41 2c 20 42 2c ll.** them A, B,
6c20: 20 61 6e 64 20 43 2e 20 54 68 65 20 72 65 73 75 and C. The resu
6c30: 6c 74 20 69 73 20 61 6c 73 6f 20 61 20 73 74 72 lt is also a str
6c40: 69 6e 67 20 77 68 69 63 68 20 69 73 20 64 65 72 ing which is der
6c50: 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 ived.** from A b
6c60: 79 20 72 65 70 6c 61 63 69 6e 67 20 65 76 65 72 y replacing ever
6c70: 79 20 6f 63 63 75 72 61 6e 63 65 20 6f 66 20 42 y occurance of B
6c80: 20 77 69 74 68 20 43 2e 20 20 54 68 65 20 6d 61 with C. The ma
6c90: 74 63 68 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 tch.** must be e
6ca0: 78 61 63 74 2e 20 20 43 6f 6c 6c 61 74 69 6e 67 xact. Collating
6cb0: 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 20 6e sequences are n
6cc0: 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 ot used..*/.stat
6cd0: 69 63 20 76 6f 69 64 20 72 65 70 6c 61 63 65 46 ic void replaceF
6ce0: 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 unc(. sqlite3_c
6cf0: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
6d00: 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
6d10: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
6d20: 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 rgv.){. const u
6d30: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 nsigned char *zS
6d40: 74 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 tr; /* Th
6d50: 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 41 e input string A
6d60: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 */. const unsi
6d70: 67 6e 65 64 20 63 68 61 72 20 2a 7a 50 61 74 74 gned char *zPatt
6d80: 65 72 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 70 ern; /* The p
6d90: 61 74 74 65 72 6e 20 73 74 72 69 6e 67 20 42 20 attern string B
6da0: 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 */. const unsig
6db0: 6e 65 64 20 63 68 61 72 20 2a 7a 52 65 70 3b 20 ned char *zRep;
6dc0: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 /* The re
6dd0: 70 6c 61 63 65 6d 65 6e 74 20 73 74 72 69 6e 67 placement string
6de0: 20 43 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 C */. unsigned
6df0: 20 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 char *zOut;
6e00: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
6e10: 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 output */. int
6e20: 20 6e 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 nStr;
6e30: 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
6e40: 20 7a 53 74 72 20 2a 2f 0a 20 20 69 6e 74 20 6e zStr */. int n
6e50: 50 61 74 74 65 72 6e 3b 20 20 20 20 20 20 20 20 Pattern;
6e60: 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a /* Size of z
6e70: 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 Pattern */. int
6e80: 20 6e 52 65 70 3b 20 20 20 20 20 20 20 20 20 20 nRep;
6e90: 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
6ea0: 20 7a 52 65 70 20 2a 2f 0a 20 20 69 36 34 20 6e zRep */. i64 n
6eb0: 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 Out;
6ec0: 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 /* Maximum s
6ed0: 69 7a 65 20 6f 66 20 7a 4f 75 74 20 2a 2f 0a 20 ize of zOut */.
6ee0: 20 69 6e 74 20 6c 6f 6f 70 4c 69 6d 69 74 3b 20 int loopLimit;
6ef0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 /* Las
6f00: 74 20 7a 53 74 72 5b 5d 20 74 68 61 74 20 6d 69 t zStr[] that mi
6f10: 67 68 74 20 6d 61 74 63 68 20 7a 50 61 74 74 65 ght match zPatte
6f20: 72 6e 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c rn[] */. int i,
6f30: 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 j;
6f40: 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
6f50: 65 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ers */.. assert
6f60: 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 55 ( argc==3 );. U
6f70: 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
6f80: 61 72 67 63 29 3b 0a 20 20 7a 53 74 72 20 3d 20 argc);. zStr =
6f90: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
6fa0: 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 xt(argv[0]);. i
6fb0: 66 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65 74 f( zStr==0 ) ret
6fc0: 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73 71 urn;. nStr = sq
6fd0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
6fe0: 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 s(argv[0]);. as
6ff0: 73 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c 69 sert( zStr==sqli
7000: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
7010: 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e rgv[0]) ); /* N
7020: 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 o encoding chang
7030: 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e 20 e */. zPattern
7040: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
7050: 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 text(argv[1]);.
7060: 20 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d 30 if( zPattern==0
7070: 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
7080: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
7090: 70 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53 51 4c pe(argv[1])==SQL
70a0: 49 54 45 5f 4e 55 4c 4c 0a 20 20 20 20 20 20 20 ITE_NULL.
70b0: 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f || sqlite3_
70c0: 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c context_db_handl
70d0: 65 28 63 6f 6e 74 65 78 74 29 2d 3e 6d 61 6c 6c e(context)->mall
70e0: 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 ocFailed );.
70f0: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 return;. }. if
7100: 28 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d 3d 30 ( zPattern[0]==0
7110: 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
7120: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 sqlite3_value_ty
7130: 70 65 28 61 72 67 76 5b 31 5d 29 21 3d 53 51 4c pe(argv[1])!=SQL
7140: 49 54 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 ITE_NULL );.
7150: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 sqlite3_result_v
7160: 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 alue(context, ar
7170: 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72 65 74 75 gv[0]);. retu
7180: 72 6e 3b 0a 20 20 7d 0a 20 20 6e 50 61 74 74 65 rn;. }. nPatte
7190: 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c rn = sqlite3_val
71a0: 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d ue_bytes(argv[1]
71b0: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 50 61 );. assert( zPa
71c0: 74 74 65 72 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 ttern==sqlite3_v
71d0: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 alue_text(argv[1
71e0: 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 ]) ); /* No enc
71f0: 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a oding change */.
7200: 20 20 7a 52 65 70 20 3d 20 73 71 6c 69 74 65 33 zRep = sqlite3
7210: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
7220: 5b 32 5d 29 3b 0a 20 20 69 66 28 20 7a 52 65 70 [2]);. if( zRep
7230: 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
7240: 6e 52 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 nRep = sqlite3_v
7250: 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b alue_bytes(argv[
7260: 32 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 2]);. assert( z
7270: 52 65 70 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c Rep==sqlite3_val
7280: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 ue_text(argv[2])
7290: 20 29 3b 0a 20 20 6e 4f 75 74 20 3d 20 6e 53 74 );. nOut = nSt
72a0: 72 20 2b 20 31 3b 0a 20 20 61 73 73 65 72 74 28 r + 1;. assert(
72b0: 20 6e 4f 75 74 3c 53 51 4c 49 54 45 5f 4d 41 58 nOut<SQLITE_MAX
72c0: 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20 7a 4f 75 _LENGTH );. zOu
72d0: 74 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f t = contextMallo
72e0: 63 28 63 6f 6e 74 65 78 74 2c 20 28 69 36 34 29 c(context, (i64)
72f0: 6e 4f 75 74 29 3b 0a 20 20 69 66 28 20 7a 4f 75 nOut);. if( zOu
7300: 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 t==0 ){. retu
7310: 72 6e 3b 0a 20 20 7d 0a 20 20 6c 6f 6f 70 4c 69 rn;. }. loopLi
7320: 6d 69 74 20 3d 20 6e 53 74 72 20 2d 20 6e 50 61 mit = nStr - nPa
7330: 74 74 65 72 6e 3b 20 20 0a 20 20 66 6f 72 28 69 ttern; . for(i
7340: 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f 70 4c 69 6d =j=0; i<=loopLim
7350: 69 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 it; i++){. if
7360: 28 20 7a 53 74 72 5b 69 5d 21 3d 7a 50 61 74 74 ( zStr[i]!=zPatt
7370: 65 72 6e 5b 30 5d 20 7c 7c 20 6d 65 6d 63 6d 70 ern[0] || memcmp
7380: 28 26 7a 53 74 72 5b 69 5d 2c 20 7a 50 61 74 74 (&zStr[i], zPatt
7390: 65 72 6e 2c 20 6e 50 61 74 74 65 72 6e 29 20 29 ern, nPattern) )
73a0: 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 2b 2b {. zOut[j++
73b0: 5d 20 3d 20 7a 53 74 72 5b 69 5d 3b 0a 20 20 20 ] = zStr[i];.
73c0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 38 }else{. u8
73d0: 20 2a 7a 4f 6c 64 3b 0a 20 20 20 20 20 20 73 71 *zOld;. sq
73e0: 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 lite3 *db = sqli
73f0: 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 te3_context_db_h
7400: 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a andle(context);.
7410: 20 20 20 20 20 20 6e 4f 75 74 20 2b 3d 20 6e 52 nOut += nR
7420: 65 70 20 2d 20 6e 50 61 74 74 65 72 6e 3b 0a 20 ep - nPattern;.
7430: 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e testcase( n
7440: 4f 75 74 2d 31 3d 3d 64 62 2d 3e 61 4c 69 6d 69 Out-1==db->aLimi
7450: 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
7460: 45 4e 47 54 48 5d 20 29 3b 0a 20 20 20 20 20 20 ENGTH] );.
7470: 74 65 73 74 63 61 73 65 28 20 6e 4f 75 74 2d 32 testcase( nOut-2
7480: 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c ==db->aLimit[SQL
7490: 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 ITE_LIMIT_LENGTH
74a0: 5d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e ] );. if( n
74b0: 4f 75 74 2d 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 Out-1>db->aLimit
74c0: 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 [SQLITE_LIMIT_LE
74d0: 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20 20 NGTH] ){.
74e0: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
74f0: 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e error_toobig(con
7500: 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73 text);. s
7510: 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f 75 74 qlite3_free(zOut
7520: 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
7530: 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 n;. }.
7540: 20 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20 20 zOld = zOut;.
7550: 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 zOut = sqlit
7560: 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 74 2c e3_realloc(zOut,
7570: 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20 20 (int)nOut);.
7580: 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 if( zOut==0 )
7590: 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
75a0: 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 3_result_error_n
75b0: 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 omem(context);.
75c0: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
75d0: 72 65 65 28 7a 4f 6c 64 29 3b 0a 20 20 20 20 20 ree(zOld);.
75e0: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 return;.
75f0: 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 }. memcpy(
7600: 26 7a 4f 75 74 5b 6a 5d 2c 20 7a 52 65 70 2c 20 &zOut[j], zRep,
7610: 6e 52 65 70 29 3b 0a 20 20 20 20 20 20 6a 20 2b nRep);. j +
7620: 3d 20 6e 52 65 70 3b 0a 20 20 20 20 20 20 69 20 = nRep;. i
7630: 2b 3d 20 6e 50 61 74 74 65 72 6e 2d 31 3b 0a 20 += nPattern-1;.
7640: 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
7650: 74 28 20 6a 2b 6e 53 74 72 2d 69 2b 31 3d 3d 6e t( j+nStr-i+1==n
7660: 4f 75 74 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28 Out );. memcpy(
7670: 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53 74 72 5b &zOut[j], &zStr[
7680: 69 5d 2c 20 6e 53 74 72 2d 69 29 3b 0a 20 20 6a i], nStr-i);. j
7690: 20 2b 3d 20 6e 53 74 72 20 2d 20 69 3b 0a 20 20 += nStr - i;.
76a0: 61 73 73 65 72 74 28 20 6a 3c 3d 6e 4f 75 74 20 assert( j<=nOut
76b0: 29 3b 0a 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 30 );. zOut[j] = 0
76c0: 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 ;. sqlite3_resu
76d0: 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
76e0: 20 28 63 68 61 72 2a 29 7a 4f 75 74 2c 20 6a 2c (char*)zOut, j,
76f0: 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a sqlite3_free);.
7700: 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
7710: 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 54 ntation of the T
7720: 52 49 4d 28 29 2c 20 4c 54 52 49 4d 28 29 2c 20 RIM(), LTRIM(),
7730: 61 6e 64 20 52 54 52 49 4d 28 29 20 66 75 6e 63 and RTRIM() func
7740: 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 75 73 tions..** The us
7750: 65 72 64 61 74 61 20 69 73 20 30 78 31 20 66 6f erdata is 0x1 fo
7760: 72 20 6c 65 66 74 20 74 72 69 6d 2c 20 30 78 32 r left trim, 0x2
7770: 20 66 6f 72 20 72 69 67 68 74 20 74 72 69 6d 2c for right trim,
7780: 20 30 78 33 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 0x3 for both..*
7790: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72 /.static void tr
77a0: 69 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 imFunc(. sqlite
77b0: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
77c0: 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a xt,. int argc,.
77d0: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
77e0: 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 **argv.){. cons
77f0: 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
7800: 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a *zIn; /*
7810: 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f Input string */
7820: 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 . const unsigne
7830: 64 20 63 68 61 72 20 2a 7a 43 68 61 72 53 65 74 d char *zCharSet
7840: 3b 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 63 ; /* Set of c
7850: 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 72 69 haracters to tri
7860: 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 3b 20 m */. int nIn;
7870: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7880: 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
7890: 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 69 er of bytes in i
78a0: 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 66 6c nput */. int fl
78b0: 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
78c0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 /* 1
78d0: 3a 20 74 72 69 6d 6c 65 66 74 20 20 32 3a 20 74 : trimleft 2: t
78e0: 72 69 6d 72 69 67 68 74 20 20 33 3a 20 74 72 69 rimright 3: tri
78f0: 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 m */. int i;
7900: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7910: 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
7920: 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e counter */. un
7930: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4c 65 signed char *aLe
7940: 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 n = 0;
7950: 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 65 61 63 /* Length of eac
7960: 68 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a h character in z
7970: 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 75 6e 73 CharSet */. uns
7980: 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 61 7a 43 igned char **azC
7990: 68 61 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f har = 0; /
79a0: 2a 20 49 6e 64 69 76 69 64 75 61 6c 20 63 68 61 * Individual cha
79b0: 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68 61 72 racters in zChar
79c0: 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 Set */. int nCh
79d0: 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ar;
79e0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
79f0: 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
7a00: 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a rs in zCharSet *
7a10: 2f 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 /.. if( sqlite3
7a20: 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 _value_type(argv
7a30: 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c [0])==SQLITE_NUL
7a40: 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b L ){. return;
7a50: 0a 20 20 7d 0a 20 20 7a 49 6e 20 3d 20 73 71 6c . }. zIn = sql
7a60: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
7a70: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 argv[0]);. if(
7a80: 7a 49 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b zIn==0 ) return;
7a90: 0a 20 20 6e 49 6e 20 3d 20 73 71 6c 69 74 65 33 . nIn = sqlite3
7aa0: 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 _value_bytes(arg
7ab0: 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 v[0]);. assert(
7ac0: 20 7a 49 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 zIn==sqlite3_va
7ad0: 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d lue_text(argv[0]
7ae0: 29 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d ) );. if( argc=
7af0: 3d 31 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 =1 ){. static
7b00: 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
7b10: 63 68 61 72 20 6c 65 6e 4f 6e 65 5b 5d 20 3d 20 char lenOne[] =
7b20: 7b 20 31 20 7d 3b 0a 20 20 20 20 73 74 61 74 69 { 1 };. stati
7b30: 63 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 c unsigned char
7b40: 2a 20 63 6f 6e 73 74 20 61 7a 4f 6e 65 5b 5d 20 * const azOne[]
7b50: 3d 20 7b 20 28 75 38 2a 29 22 20 22 20 7d 3b 0a = { (u8*)" " };.
7b60: 20 20 20 20 6e 43 68 61 72 20 3d 20 31 3b 0a 20 nChar = 1;.
7b70: 20 20 20 61 4c 65 6e 20 3d 20 28 75 38 2a 29 6c aLen = (u8*)l
7b80: 65 6e 4f 6e 65 3b 0a 20 20 20 20 61 7a 43 68 61 enOne;. azCha
7b90: 72 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 r = (unsigned ch
7ba0: 61 72 20 2a 2a 29 61 7a 4f 6e 65 3b 0a 20 20 20 ar **)azOne;.
7bb0: 20 7a 43 68 61 72 53 65 74 20 3d 20 30 3b 0a 20 zCharSet = 0;.
7bc0: 20 7d 65 6c 73 65 20 69 66 28 20 28 7a 43 68 61 }else if( (zCha
7bd0: 72 53 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 rSet = sqlite3_v
7be0: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 alue_text(argv[1
7bf0: 5d 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ]))==0 ){. re
7c00: 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 turn;. }else{.
7c10: 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 const unsigne
7c20: 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 66 d char *z;. f
7c30: 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20 6e or(z=zCharSet, n
7c40: 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61 Char=0; *z; nCha
7c50: 72 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 r++){. SQLI
7c60: 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b TE_SKIP_UTF8(z);
7c70: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e . }. if( n
7c80: 43 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 20 20 Char>0 ){.
7c90: 61 7a 43 68 61 72 20 3d 20 63 6f 6e 74 65 78 74 azChar = context
7ca0: 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 Malloc(context,
7cb0: 28 28 69 36 34 29 6e 43 68 61 72 29 2a 28 73 69 ((i64)nChar)*(si
7cc0: 7a 65 6f 66 28 63 68 61 72 2a 29 2b 31 29 29 3b zeof(char*)+1));
7cd0: 0a 20 20 20 20 20 20 69 66 28 20 61 7a 43 68 61 . if( azCha
7ce0: 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 r==0 ){.
7cf0: 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a return;. }.
7d00: 20 20 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 6e aLen = (un
7d10: 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 61 7a signed char*)&az
7d20: 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 20 20 Char[nChar];.
7d30: 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65 for(z=zCharSe
7d40: 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 t, nChar=0; *z;
7d50: 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20 nChar++){.
7d60: 20 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 20 azChar[nChar]
7d70: 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
7d80: 20 2a 29 7a 3b 0a 20 20 20 20 20 20 20 20 53 51 *)z;. SQ
7d90: 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a LITE_SKIP_UTF8(z
7da0: 29 3b 0a 20 20 20 20 20 20 20 20 61 4c 65 6e 5b );. aLen[
7db0: 6e 43 68 61 72 5d 20 3d 20 28 75 38 29 28 7a 20 nChar] = (u8)(z
7dc0: 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 29 - azChar[nChar])
7dd0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
7de0: 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 72 3e }. if( nChar>
7df0: 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d 0 ){. flags =
7e00: 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 SQLITE_PTR_TO_I
7e10: 4e 54 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f NT(sqlite3_user_
7e20: 64 61 74 61 28 63 6f 6e 74 65 78 74 29 29 3b 0a data(context));.
7e30: 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 if( flags &
7e40: 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 1 ){. while
7e50: 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20 ( nIn>0 ){.
7e60: 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a int len = 0;.
7e70: 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b for(i=0;
7e80: 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a i<nChar; i++){.
7e90: 20 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 len =
7ea0: 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 aLen[i];.
7eb0: 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20 if( len<=nIn
7ec0: 26 26 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20 61 && memcmp(zIn, a
7ed0: 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d 3d zChar[i], len)==
7ee0: 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 0 ) break;.
7ef0: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
7f00: 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 i>=nChar ) brea
7f10: 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e 20 2b k;. zIn +
7f20: 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6e = len;. n
7f30: 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 In -= len;.
7f40: 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
7f50: 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a 20 20 flags & 2 ){.
7f60: 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30 while( nIn>0
7f70: 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
7f80: 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 len = 0;.
7f90: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 for(i=0; i<nCha
7fa0: 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 r; i++){.
7fb0: 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d len = aLen[i]
7fc0: 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
7fd0: 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 len<=nIn && memc
7fe0: 6d 70 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d mp(&zIn[nIn-len]
7ff0: 2c 61 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d ,azChar[i],len)=
8000: 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
8010: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
8020: 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 ( i>=nChar ) bre
8030: 61 6b 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 ak;. nIn
8040: 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a -= len;. }.
8050: 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 43 }. if( zC
8060: 68 61 72 53 65 74 20 29 7b 0a 20 20 20 20 20 20 harSet ){.
8070: 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43 sqlite3_free(azC
8080: 68 61 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a har);. }. }.
8090: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
80a0: 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 _text(context, (
80b0: 63 68 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20 char*)zIn, nIn,
80c0: 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
80d0: 29 3b 0a 7d 0a 0a 0a 2f 2a 20 49 4d 50 3a 20 52 );.}.../* IMP: R
80e0: 2d 32 35 33 36 31 2d 31 36 31 35 30 20 54 68 69 -25361-16150 Thi
80f0: 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6d s function is om
8100: 69 74 74 65 64 20 66 72 6f 6d 20 53 51 4c 69 74 itted from SQLit
8110: 65 20 62 79 20 64 65 66 61 75 6c 74 2e 20 49 74 e by default. It
8120: 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 .** is only avai
8130: 6c 61 62 6c 65 20 69 66 20 74 68 65 20 53 51 4c lable if the SQL
8140: 49 54 45 5f 53 4f 55 4e 44 45 58 20 63 6f 6d 70 ITE_SOUNDEX comp
8150: 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 ile-time option
8160: 69 73 20 75 73 65 64 0a 2a 2a 20 77 68 65 6e 20 is used.** when
8170: 53 51 4c 69 74 65 20 69 73 20 62 75 69 6c 74 2e SQLite is built.
8180: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
8190: 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a 20 E_SOUNDEX./*.**
81a0: 43 6f 6d 70 75 74 65 20 74 68 65 20 73 6f 75 6e Compute the soun
81b0: 64 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 dex encoding of
81c0: 61 20 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 49 4d a word..**.** IM
81d0: 50 3a 20 52 2d 35 39 37 38 32 2d 30 30 30 37 32 P: R-59782-00072
81e0: 20 54 68 65 20 73 6f 75 6e 64 65 78 28 58 29 20 The soundex(X)
81f0: 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
8200: 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 a string that i
8210: 73 20 74 68 65 0a 2a 2a 20 73 6f 75 6e 64 65 78 s the.** soundex
8220: 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 encoding of the
8230: 20 73 74 72 69 6e 67 20 58 2e 20 0a 2a 2f 0a 73 string X. .*/.s
8240: 74 61 74 69 63 20 76 6f 69 64 20 73 6f 75 6e 64 tatic void sound
8250: 65 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 exFunc(. sqlite
8260: 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
8270: 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a xt,. int argc,.
8280: 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
8290: 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 68 61 72 **argv.){. char
82a0: 20 7a 52 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 zResult[8];. c
82b0: 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 onst u8 *zIn;.
82c0: 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 61 74 int i, j;. stat
82d0: 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 ic const unsigne
82e0: 64 20 63 68 61 72 20 69 43 6f 64 65 5b 5d 20 3d d char iCode[] =
82f0: 20 7b 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 {. 0, 0, 0,
8300: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
8310: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
8320: 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 0, 0,. 0, 0,
8330: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
8340: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
8350: 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 , 0, 0, 0,. 0
8360: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
8370: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
8380: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 0, 0, 0, 0, 0,.
8390: 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0
83a0: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
83b0: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
83c0: 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 0,. 0, 0, 1,
83d0: 32 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2, 3, 0, 1, 2, 0
83e0: 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c , 0, 2, 2, 4, 5,
83f0: 20 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 5, 0,. 1, 2,
8400: 20 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 6, 2, 3, 0, 1,
8410: 30 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 0, 2, 0, 2, 0, 0
8420: 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 , 0, 0, 0,. 0
8430: 2c 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c , 0, 1, 2, 3, 0,
8440: 20 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 1, 2, 0, 0, 2,
8450: 32 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 2, 4, 5, 5, 0,.
8460: 20 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 1, 2, 6, 2, 3
8470: 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c , 0, 1, 0, 2, 0,
8480: 20 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 2, 0, 0, 0, 0,
8490: 30 2c 0a 20 20 7d 3b 0a 20 20 61 73 73 65 72 74 0,. };. assert
84a0: 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 7a ( argc==1 );. z
84b0: 49 6e 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 In = (u8*)sqlite
84c0: 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
84d0: 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e v[0]);. if( zIn
84e0: 3d 3d 30 20 29 20 7a 49 6e 20 3d 20 28 75 38 2a ==0 ) zIn = (u8*
84f0: 29 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 )"";. for(i=0;
8500: 7a 49 6e 5b 69 5d 20 26 26 20 21 73 71 6c 69 74 zIn[i] && !sqlit
8510: 65 33 49 73 61 6c 70 68 61 28 7a 49 6e 5b 69 5d e3Isalpha(zIn[i]
8520: 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 ); i++){}. if(
8530: 7a 49 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75 38 zIn[i] ){. u8
8540: 20 70 72 65 76 63 6f 64 65 20 3d 20 69 43 6f 64 prevcode = iCod
8550: 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a e[zIn[i]&0x7f];.
8560: 20 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20 3d zResult[0] =
8570: 20 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 sqlite3Toupper(
8580: 7a 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 zIn[i]);. for
8590: 28 6a 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e (j=1; j<4 && zIn
85a0: 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 [i]; i++){.
85b0: 20 69 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 int code = iCod
85c0: 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a e[zIn[i]&0x7f];.
85d0: 20 20 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 if( code>0
85e0: 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
85f0: 63 6f 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 code!=prevcode )
8600: 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 76 {. prev
8610: 63 6f 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 code = code;.
8620: 20 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a zResult[j
8630: 2b 2b 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 ++] = code + '0'
8640: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
8650: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
8660: 20 70 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 prevcode = 0;.
8670: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
8680: 20 77 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 while( j<4 ){.
8690: 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b zResult[j++
86a0: 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 ] = '0';. }.
86b0: 20 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 zResult[j] =
86c0: 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 0;. sqlite3_r
86d0: 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
86e0: 78 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 xt, zResult, 4,
86f0: 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
8700: 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
8710: 2f 2a 20 49 4d 50 3a 20 52 2d 36 34 38 39 34 2d /* IMP: R-64894-
8720: 35 30 33 32 31 20 54 68 65 20 73 74 72 69 6e 67 50321 The string
8730: 20 22 3f 30 30 30 22 20 69 73 20 72 65 74 75 72 "?000" is retur
8740: 6e 65 64 20 69 66 20 74 68 65 20 61 72 67 75 6d ned if the argum
8750: 65 6e 74 0a 20 20 20 20 2a 2a 20 69 73 20 4e 55 ent. ** is NU
8760: 4c 4c 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e LL or contains n
8770: 6f 20 41 53 43 49 49 20 61 6c 70 68 61 62 65 74 o ASCII alphabet
8780: 69 63 20 63 68 61 72 61 63 74 65 72 73 2e 20 2a ic characters. *
8790: 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 /. sqlite3_re
87a0: 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
87b0: 74 2c 20 22 3f 30 30 30 22 2c 20 34 2c 20 53 51 t, "?000", 4, SQ
87c0: 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 LITE_STATIC);.
87d0: 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }.}.#endif /* SQ
87e0: 4c 49 54 45 5f 53 4f 55 4e 44 45 58 20 2a 2f 0a LITE_SOUNDEX */.
87f0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
8800: 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
8810: 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66 75 6e 63 ION./*.** A func
8820: 74 69 6f 6e 20 74 68 61 74 20 6c 6f 61 64 73 20 tion that loads
8830: 61 20 73 68 61 72 65 64 2d 6c 69 62 72 61 72 79 a shared-library
8840: 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 65 6e 20 extension then
8850: 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f returns NULL..*/
8860: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 .static void loa
8870: 64 45 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e dExt(sqlite3_con
8880: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 text *context, i
8890: 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 nt argc, sqlite3
88a0: 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a _value **argv){.
88b0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
88c0: 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 ile = (const cha
88d0: 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 r *)sqlite3_valu
88e0: 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b e_text(argv[0]);
88f0: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
8900: 50 72 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33 20 Proc;. sqlite3
8910: 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f *db = sqlite3_co
8920: 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
8930: 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63 68 61 72 context);. char
8940: 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a *zErrMsg = 0;..
8950: 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b if( argc==2 ){
8960: 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 28 63 6f . zProc = (co
8970: 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 nst char *)sqlit
8980: 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
8990: 67 76 5b 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b gv[1]);. }else{
89a0: 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 30 3b 0a . zProc = 0;.
89b0: 20 20 7d 0a 20 20 69 66 28 20 7a 46 69 6c 65 20 }. if( zFile
89c0: 26 26 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f && sqlite3_load_
89d0: 65 78 74 65 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 extension(db, zF
89e0: 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 26 7a 45 72 ile, zProc, &zEr
89f0: 72 4d 73 67 29 20 29 7b 0a 20 20 20 20 73 71 6c rMsg) ){. sql
8a00: 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
8a10: 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 4d r(context, zErrM
8a20: 73 67 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c sg, -1);. sql
8a30: 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 4d 73 ite3_free(zErrMs
8a40: 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 g);. }.}.#endif
8a50: 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 .../*.** An inst
8a60: 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
8a70: 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
8a80: 68 6f 6c 64 73 20 74 68 65 20 63 6f 6e 74 65 78 holds the contex
8a90: 74 20 6f 66 20 61 0a 2a 2a 20 73 75 6d 28 29 20 t of a.** sum()
8aa0: 6f 72 20 61 76 67 28 29 20 61 67 67 72 65 67 61 or avg() aggrega
8ab0: 74 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 2e 0a te computation..
8ac0: 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
8ad0: 74 20 53 75 6d 43 74 78 20 53 75 6d 43 74 78 3b t SumCtx SumCtx;
8ae0: 0a 73 74 72 75 63 74 20 53 75 6d 43 74 78 20 7b .struct SumCtx {
8af0: 0a 20 20 64 6f 75 62 6c 65 20 72 53 75 6d 3b 20 . double rSum;
8b00: 20 20 20 20 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 /* Floating
8b10: 20 70 6f 69 6e 74 20 73 75 6d 20 2a 2f 0a 20 20 point sum */.
8b20: 69 36 34 20 69 53 75 6d 3b 20 20 20 20 20 20 20 i64 iSum;
8b30: 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 73 75 6d /* Integer sum
8b40: 20 2a 2f 20 20 20 0a 20 20 69 36 34 20 63 6e 74 */ . i64 cnt
8b50: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ; /* Nu
8b60: 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 mber of elements
8b70: 20 73 75 6d 6d 65 64 20 2a 2f 0a 20 20 75 38 20 summed */. u8
8b80: 6f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20 20 2f overflow; /
8b90: 2a 20 54 72 75 65 20 69 66 20 69 6e 74 65 67 65 * True if intege
8ba0: 72 20 6f 76 65 72 66 6c 6f 77 20 73 65 65 6e 20 r overflow seen
8bb0: 2a 2f 0a 20 20 75 38 20 61 70 70 72 6f 78 3b 20 */. u8 approx;
8bc0: 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
8bd0: 66 20 6e 6f 6e 2d 69 6e 74 65 67 65 72 20 76 61 f non-integer va
8be0: 6c 75 65 20 77 61 73 20 69 6e 70 75 74 20 74 6f lue was input to
8bf0: 20 74 68 65 20 73 75 6d 20 2a 2f 0a 7d 3b 0a 0a the sum */.};..
8c00: 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 /*.** Routines u
8c10: 73 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 sed to compute t
8c20: 68 65 20 73 75 6d 2c 20 61 76 65 72 61 67 65 2c he sum, average,
8c30: 20 61 6e 64 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a and total..**.*
8c40: 2a 20 54 68 65 20 53 55 4d 28 29 20 66 75 6e 63 * The SUM() func
8c50: 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73 20 74 68 65 tion follows the
8c60: 20 28 62 72 6f 6b 65 6e 29 20 53 51 4c 20 73 74 (broken) SQL st
8c70: 61 6e 64 61 72 64 20 77 68 69 63 68 20 6d 65 61 andard which mea
8c80: 6e 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 72 65 ns.** that it re
8c90: 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69 74 turns NULL if it
8ca0: 20 73 75 6d 73 20 6f 76 65 72 20 6e 6f 20 69 6e sums over no in
8cb0: 70 75 74 73 2e 20 20 54 4f 54 41 4c 20 72 65 74 puts. TOTAL ret
8cc0: 75 72 6e 73 0a 2a 2a 20 30 2e 30 20 69 6e 20 74 urns.** 0.0 in t
8cd0: 68 61 74 20 63 61 73 65 2e 20 20 49 6e 20 61 64 hat case. In ad
8ce0: 64 69 74 69 6f 6e 2c 20 54 4f 54 41 4c 20 61 6c dition, TOTAL al
8cf0: 77 61 79 73 20 72 65 74 75 72 6e 73 20 61 20 66 ways returns a f
8d00: 6c 6f 61 74 20 77 68 65 72 65 0a 2a 2a 20 53 55 loat where.** SU
8d10: 4d 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 61 M might return a
8d20: 6e 20 69 6e 74 65 67 65 72 20 69 66 20 69 74 20 n integer if it
8d30: 6e 65 76 65 72 20 65 6e 63 6f 75 6e 74 65 72 73 never encounters
8d40: 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e a floating poin
8d50: 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 54 4f 54 t.** value. TOT
8d60: 41 4c 20 6e 65 76 65 72 20 66 61 69 6c 73 2c 20 AL never fails,
8d70: 62 75 74 20 53 55 4d 20 6d 69 67 68 74 20 74 68 but SUM might th
8d80: 72 6f 75 67 68 20 61 6e 20 65 78 63 65 70 74 69 rough an excepti
8d90: 6f 6e 20 69 66 0a 2a 2a 20 69 74 20 6f 76 65 72 on if.** it over
8da0: 66 6c 6f 77 73 20 61 6e 20 69 6e 74 65 67 65 72 flows an integer
8db0: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
8dc0: 20 73 75 6d 53 74 65 70 28 73 71 6c 69 74 65 33 sumStep(sqlite3
8dd0: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
8de0: 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c t, int argc, sql
8df0: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 ite3_value **arg
8e00: 76 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b v){. SumCtx *p;
8e10: 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 61 . int type;. a
8e20: 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 ssert( argc==1 )
8e30: 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
8e40: 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 70 20 ETER(argc);. p
8e50: 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 = sqlite3_aggreg
8e60: 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 ate_context(cont
8e70: 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 ext, sizeof(*p))
8e80: 3b 0a 20 20 74 79 70 65 20 3d 20 73 71 6c 69 74 ;. type = sqlit
8e90: 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 e3_value_numeric
8ea0: 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a _type(argv[0]);.
8eb0: 20 20 69 66 28 20 70 20 26 26 20 74 79 70 65 21 if( p && type!
8ec0: 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a =SQLITE_NULL ){.
8ed0: 20 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 p->cnt++;.
8ee0: 20 20 69 66 28 20 74 79 70 65 3d 3d 53 51 4c 49 if( type==SQLI
8ef0: 54 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 TE_INTEGER ){.
8f00: 20 20 20 20 69 36 34 20 76 20 3d 20 73 71 6c 69 i64 v = sqli
8f10: 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 te3_value_int64(
8f20: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 argv[0]);.
8f30: 70 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b 0a 20 20 p->rSum += v;.
8f40: 20 20 20 20 69 66 28 20 28 70 2d 3e 61 70 70 72 if( (p->appr
8f50: 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f 77 29 3d ox|p->overflow)=
8f60: 3d 30 20 26 26 20 73 71 6c 69 74 65 33 41 64 64 =0 && sqlite3Add
8f70: 49 6e 74 36 34 28 26 70 2d 3e 69 53 75 6d 2c 20 Int64(&p->iSum,
8f80: 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d v) ){. p-
8f90: 3e 6f 76 65 72 66 6c 6f 77 20 3d 20 31 3b 0a 20 >overflow = 1;.
8fa0: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
8fb0: 7b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20 {. p->rSum
8fc0: 2b 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 += sqlite3_value
8fd0: 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 _double(argv[0])
8fe0: 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70 70 72 6f ;. p->appro
8ff0: 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d x = 1;. }. }
9000: 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 .}.static void s
9010: 75 6d 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 umFinalize(sqlit
9020: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
9030: 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a ext){. SumCtx *
9040: 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 p;. p = sqlite3
9050: 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 _aggregate_conte
9060: 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a xt(context, 0);.
9070: 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e if( p && p->cn
9080: 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 t>0 ){. if( p
9090: 2d 3e 6f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 ->overflow ){.
90a0: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
90b0: 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 lt_error(context
90c0: 2c 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c ,"integer overfl
90d0: 6f 77 22 2c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c ow",-1);. }el
90e0: 73 65 20 69 66 28 20 70 2d 3e 61 70 70 72 6f 78 se if( p->approx
90f0: 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
9100: 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 3_result_double(
9110: 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d context, p->rSum
9120: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
9130: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
9140: 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 lt_int64(context
9150: 2c 20 70 2d 3e 69 53 75 6d 29 3b 0a 20 20 20 20 , p->iSum);.
9160: 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 }. }.}.static v
9170: 6f 69 64 20 61 76 67 46 69 6e 61 6c 69 7a 65 28 oid avgFinalize(
9180: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
9190: 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d *context){. Sum
91a0: 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 Ctx *p;. p = sq
91b0: 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
91c0: 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c context(context,
91d0: 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 0);. if( p &&
91e0: 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 p->cnt>0 ){.
91f0: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 sqlite3_result_d
9200: 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 ouble(context, p
9210: 2d 3e 72 53 75 6d 2f 28 64 6f 75 62 6c 65 29 70 ->rSum/(double)p
9220: 2d 3e 63 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 ->cnt);. }.}.st
9230: 61 74 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 46 atic void totalF
9240: 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f inalize(sqlite3_
9250: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
9260: 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a ){. SumCtx *p;.
9270: 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 p = sqlite3_ag
9280: 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
9290: 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 2f context, 0);. /
92a0: 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 * (double)0 In c
92b0: 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d ase of SQLITE_OM
92c0: 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
92d0: 54 2e 2e 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 T... */. sqlite
92e0: 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 3_result_double(
92f0: 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d 3e context, p ? p->
9300: 72 53 75 6d 20 3a 20 28 64 6f 75 62 6c 65 29 30 rSum : (double)0
9310: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 );.}../*.** The
9320: 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
9330: 75 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 ure keeps track
9340: 6f 66 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 of state informa
9350: 74 69 6f 6e 20 66 6f 72 20 74 68 65 0a 2a 2a 20 tion for the.**
9360: 63 6f 75 6e 74 28 29 20 61 67 67 72 65 67 61 74 count() aggregat
9370: 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 74 e function..*/.t
9380: 79 70 65 64 65 66 20 73 74 72 75 63 74 20 43 6f ypedef struct Co
9390: 75 6e 74 43 74 78 20 43 6f 75 6e 74 43 74 78 3b untCtx CountCtx;
93a0: 0a 73 74 72 75 63 74 20 43 6f 75 6e 74 43 74 78 .struct CountCtx
93b0: 20 7b 0a 20 20 69 36 34 20 6e 3b 0a 7d 3b 0a 0a {. i64 n;.};..
93c0: 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 /*.** Routines t
93d0: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
93e0: 63 6f 75 6e 74 28 29 20 61 67 67 72 65 67 61 74 count() aggregat
93f0: 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 e function..*/.s
9400: 74 61 74 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 tatic void count
9410: 53 74 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e Step(sqlite3_con
9420: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 text *context, i
9430: 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 nt argc, sqlite3
9440: 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a _value **argv){.
9450: 20 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 CountCtx *p;.
9460: 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 p = sqlite3_agg
9470: 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 regate_context(c
9480: 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a ontext, sizeof(*
9490: 70 29 29 3b 0a 20 20 69 66 28 20 28 61 72 67 63 p));. if( (argc
94a0: 3d 3d 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55 ==0 || SQLITE_NU
94b0: 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 LL!=sqlite3_valu
94c0: 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 e_type(argv[0]))
94d0: 20 26 26 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e && p ){. p->
94e0: 6e 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 n++;. }..#ifnde
94f0: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 f SQLITE_OMIT_DE
9500: 50 52 45 43 41 54 45 44 0a 20 20 2f 2a 20 54 68 PRECATED. /* Th
9510: 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 e sqlite3_aggreg
9520: 61 74 65 5f 63 6f 75 6e 74 28 29 20 66 75 6e 63 ate_count() func
9530: 74 69 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74 tion is deprecat
9540: 65 64 2e 20 20 42 75 74 20 6a 75 73 74 20 74 6f ed. But just to
9550: 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 make. ** sure
9560: 69 74 20 73 74 69 6c 6c 20 6f 70 65 72 61 74 65 it still operate
9570: 73 20 63 6f 72 72 65 63 74 6c 79 2c 20 76 65 72 s correctly, ver
9580: 69 66 79 20 74 68 61 74 20 69 74 73 20 63 6f 75 ify that its cou
9590: 6e 74 20 61 67 72 65 65 73 20 77 69 74 68 20 6f nt agrees with o
95a0: 75 72 20 0a 20 20 2a 2a 20 69 6e 74 65 72 6e 61 ur . ** interna
95b0: 6c 20 63 6f 75 6e 74 20 77 68 65 6e 20 75 73 69 l count when usi
95c0: 6e 67 20 63 6f 75 6e 74 28 2a 29 20 61 6e 64 20 ng count(*) and
95d0: 77 68 65 6e 20 74 68 65 20 74 6f 74 61 6c 20 63 when the total c
95e0: 6f 75 6e 74 20 63 61 6e 20 62 65 0a 20 20 2a 2a ount can be. **
95f0: 20 65 78 70 72 65 73 73 65 64 20 61 73 20 61 20 expressed as a
9600: 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 20 32-bit integer.
9610: 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 */. assert( arg
9620: 63 3d 3d 31 20 7c 7c 20 70 3d 3d 30 20 7c 7c 20 c==1 || p==0 ||
9630: 70 2d 3e 6e 3e 30 78 37 66 66 66 66 66 66 66 0a p->n>0x7fffffff.
9640: 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e || p->
9650: 6e 3d 3d 73 71 6c 69 74 65 33 5f 61 67 67 72 65 n==sqlite3_aggre
9660: 67 61 74 65 5f 63 6f 75 6e 74 28 63 6f 6e 74 65 gate_count(conte
9670: 78 74 29 20 29 3b 0a 23 65 6e 64 69 66 0a 7d 20 xt) );.#endif.}
9680: 20 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 .static void c
9690: 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71 6c ountFinalize(sql
96a0: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
96b0: 6e 74 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74 43 ntext){. CountC
96c0: 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c tx *p;. p = sql
96d0: 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 ite3_aggregate_c
96e0: 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 ontext(context,
96f0: 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 0);. sqlite3_re
9700: 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 sult_int64(conte
9710: 78 74 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20 30 xt, p ? p->n : 0
9720: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 );.}../*.** Rout
9730: 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e ines to implemen
9740: 74 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 t min() and max(
9750: 29 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 ) aggregate func
9760: 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 tions..*/.static
9770: 20 76 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65 70 void minmaxStep
9780: 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
9790: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 ext *context, .
97a0: 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20 int NotUsed, .
97b0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
97c0: 2a 61 72 67 76 0a 29 7b 0a 20 20 4d 65 6d 20 2a *argv.){. Mem *
97d0: 70 41 72 67 20 20 3d 20 28 4d 65 6d 20 2a 29 61 pArg = (Mem *)a
97e0: 72 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d 20 2a 70 rgv[0];. Mem *p
97f0: 42 65 73 74 3b 0a 20 20 55 4e 55 53 45 44 5f 50 Best;. UNUSED_P
9800: 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
9810: 29 3b 0a 0a 20 20 70 42 65 73 74 20 3d 20 28 4d );.. pBest = (M
9820: 65 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 em *)sqlite3_agg
9830: 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 regate_context(c
9840: 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a ontext, sizeof(*
9850: 70 42 65 73 74 29 29 3b 0a 20 20 69 66 28 20 21 pBest));. if( !
9860: 70 42 65 73 74 20 29 20 72 65 74 75 72 6e 3b 0a pBest ) return;.
9870: 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 . if( sqlite3_v
9880: 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 alue_type(argv[0
9890: 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 ])==SQLITE_NULL
98a0: 29 7b 0a 20 20 20 20 69 66 28 20 70 42 65 73 74 ){. if( pBest
98b0: 2d 3e 66 6c 61 67 73 20 29 20 73 71 6c 69 74 65 ->flags ) sqlite
98c0: 33 53 6b 69 70 41 63 63 75 6d 75 6c 61 74 6f 72 3SkipAccumulator
98d0: 4c 6f 61 64 28 63 6f 6e 74 65 78 74 29 3b 0a 20 Load(context);.
98e0: 20 7d 65 6c 73 65 20 69 66 28 20 70 42 65 73 74 }else if( pBest
98f0: 2d 3e 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 69 ->flags ){. i
9900: 6e 74 20 6d 61 78 3b 0a 20 20 20 20 69 6e 74 20 nt max;. int
9910: 63 6d 70 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 cmp;. CollSeq
9920: 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 *pColl = sqlite
9930: 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 3GetFuncCollSeq(
9940: 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 2f 2a context);. /*
9950: 20 54 68 69 73 20 73 74 65 70 20 66 75 6e 63 74 This step funct
9960: 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 20 ion is used for
9970: 62 6f 74 68 20 74 68 65 20 6d 69 6e 28 29 20 61 both the min() a
9980: 6e 64 20 6d 61 78 28 29 20 61 67 67 72 65 67 61 nd max() aggrega
9990: 74 65 73 2c 0a 20 20 20 20 2a 2a 20 74 68 65 20 tes,. ** the
99a0: 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 only difference
99b0: 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 between the two
99c0: 62 65 69 6e 67 20 74 68 61 74 20 74 68 65 20 73 being that the s
99d0: 65 6e 73 65 20 6f 66 20 74 68 65 0a 20 20 20 20 ense of the.
99e0: 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 ** comparison is
99f0: 20 69 6e 76 65 72 74 65 64 2e 20 46 6f 72 20 74 inverted. For t
9a00: 68 65 20 6d 61 78 28 29 20 61 67 67 72 65 67 61 he max() aggrega
9a10: 74 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 te, the. ** s
9a20: 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 qlite3_user_data
9a30: 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 () function retu
9a40: 72 6e 73 20 28 76 6f 69 64 20 2a 29 2d 31 2e 20 rns (void *)-1.
9a50: 46 6f 72 20 6d 69 6e 28 29 20 69 74 0a 20 20 20 For min() it.
9a60: 20 2a 2a 20 72 65 74 75 72 6e 73 20 28 76 6f 69 ** returns (voi
9a70: 64 20 2a 29 64 62 2c 20 77 68 65 72 65 20 64 62 d *)db, where db
9a80: 20 69 73 20 74 68 65 20 73 71 6c 69 74 65 33 2a is the sqlite3*
9a90: 20 64 61 74 61 62 61 73 65 20 70 6f 69 6e 74 65 database pointe
9aa0: 72 2e 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 66 r.. ** Theref
9ab0: 6f 72 65 20 74 68 65 20 6e 65 78 74 20 73 74 61 ore the next sta
9ac0: 74 65 6d 65 6e 74 20 73 65 74 73 20 76 61 72 69 tement sets vari
9ad0: 61 62 6c 65 20 27 6d 61 78 27 20 74 6f 20 31 20 able 'max' to 1
9ae0: 66 6f 72 20 74 68 65 20 6d 61 78 28 29 0a 20 20 for the max().
9af0: 20 20 2a 2a 20 61 67 67 72 65 67 61 74 65 2c 20 ** aggregate,
9b00: 6f 72 20 30 20 66 6f 72 20 6d 69 6e 28 29 2e 0a or 0 for min()..
9b10: 20 20 20 20 2a 2f 0a 20 20 20 20 6d 61 78 20 3d */. max =
9b20: 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 sqlite3_user_da
9b30: 74 61 28 63 6f 6e 74 65 78 74 29 21 3d 30 3b 0a ta(context)!=0;.
9b40: 20 20 20 20 63 6d 70 20 3d 20 73 71 6c 69 74 65 cmp = sqlite
9b50: 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 42 65 73 3MemCompare(pBes
9b60: 74 2c 20 70 41 72 67 2c 20 70 43 6f 6c 6c 29 3b t, pArg, pColl);
9b70: 0a 20 20 20 20 69 66 28 20 28 6d 61 78 20 26 26 . if( (max &&
9b80: 20 63 6d 70 3c 30 29 20 7c 7c 20 28 21 6d 61 78 cmp<0) || (!max
9b90: 20 26 26 20 63 6d 70 3e 30 29 20 29 7b 0a 20 20 && cmp>0) ){.
9ba0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
9bb0: 65 6d 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41 emCopy(pBest, pA
9bc0: 72 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a rg);. }else{.
9bd0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 6b 69 sqlite3Ski
9be0: 70 41 63 63 75 6d 75 6c 61 74 6f 72 4c 6f 61 64 pAccumulatorLoad
9bf0: 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d (context);. }
9c00: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
9c10: 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 lite3VdbeMemCopy
9c20: 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 (pBest, pArg);.
9c30: 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}.static void
9c40: 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 minMaxFinalize(
9c50: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
9c60: 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c *context){. sql
9c70: 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 73 ite3_value *pRes
9c80: 3b 0a 20 20 70 52 65 73 20 3d 20 28 73 71 6c 69 ;. pRes = (sqli
9c90: 74 65 33 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69 te3_value *)sqli
9ca0: 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
9cb0: 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 ntext(context, 0
9cc0: 29 3b 0a 20 20 69 66 28 20 70 52 65 73 20 29 7b );. if( pRes ){
9cd0: 0a 20 20 20 20 69 66 28 20 70 52 65 73 2d 3e 66 . if( pRes->f
9ce0: 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 73 71 lags ){. sq
9cf0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c lite3_result_val
9d00: 75 65 28 63 6f 6e 74 65 78 74 2c 20 70 52 65 73 ue(context, pRes
9d10: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
9d20: 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
9d30: 73 65 28 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a se(pRes);. }.}.
9d40: 0a 2f 2a 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e ./*.** group_con
9d50: 63 61 74 28 45 58 50 52 2c 20 3f 53 45 50 41 52 cat(EXPR, ?SEPAR
9d60: 41 54 4f 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63 ATOR?).*/.static
9d70: 20 76 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 void groupConca
9d80: 74 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 tStep(. sqlite3
9d90: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
9da0: 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
9db0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
9dc0: 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 *argv.){. const
9dd0: 20 63 68 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53 char *zVal;. S
9de0: 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b trAccum *pAccum;
9df0: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
9e00: 53 65 70 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c Sep;. int nVal,
9e10: 20 6e 53 65 70 3b 0a 20 20 61 73 73 65 72 74 28 nSep;. assert(
9e20: 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 argc==1 || argc
9e30: 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c ==2 );. if( sql
9e40: 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
9e50: 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 argv[0])==SQLITE
9e60: 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a _NULL ) return;.
9e70: 20 20 70 41 63 63 75 6d 20 3d 20 28 53 74 72 41 pAccum = (StrA
9e80: 63 63 75 6d 2a 29 73 71 6c 69 74 65 33 5f 61 67 ccum*)sqlite3_ag
9e90: 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
9ea0: 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 context, sizeof(
9eb0: 2a 70 41 63 63 75 6d 29 29 3b 0a 0a 20 20 69 66 *pAccum));.. if
9ec0: 28 20 70 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 ( pAccum ){.
9ed0: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 sqlite3 *db = sq
9ee0: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
9ef0: 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 _handle(context)
9f00: 3b 0a 20 20 20 20 69 6e 74 20 66 69 72 73 74 54 ;. int firstT
9f10: 65 72 6d 20 3d 20 70 41 63 63 75 6d 2d 3e 75 73 erm = pAccum->us
9f20: 65 4d 61 6c 6c 6f 63 3d 3d 30 3b 0a 20 20 20 20 eMalloc==0;.
9f30: 70 41 63 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c 6f pAccum->useMallo
9f40: 63 20 3d 20 32 3b 0a 20 20 20 20 70 41 63 63 75 c = 2;. pAccu
9f50: 6d 2d 3e 6d 78 41 6c 6c 6f 63 20 3d 20 64 62 2d m->mxAlloc = db-
9f60: 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
9f70: 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 IMIT_LENGTH];.
9f80: 20 20 69 66 28 20 21 66 69 72 73 74 54 65 72 6d if( !firstTerm
9f90: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 72 ){. if( ar
9fa0: 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 gc==2 ){.
9fb0: 20 7a 53 65 70 20 3d 20 28 63 68 61 72 2a 29 73 zSep = (char*)s
9fc0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
9fd0: 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 t(argv[1]);.
9fe0: 20 20 20 20 6e 53 65 70 20 3d 20 73 71 6c 69 74 nSep = sqlit
9ff0: 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
a000: 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d rgv[1]);. }
a010: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 53 else{. zS
a020: 65 70 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20 ep = ",";.
a030: 20 20 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 20 nSep = 1;.
a040: 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
a050: 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
a060: 70 41 63 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 pAccum, zSep, nS
a070: 65 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a ep);. }. z
a080: 56 61 6c 20 3d 20 28 63 68 61 72 2a 29 73 71 6c Val = (char*)sql
a090: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
a0a0: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 56 argv[0]);. nV
a0b0: 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
a0c0: 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d ue_bytes(argv[0]
a0d0: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 );. sqlite3St
a0e0: 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 rAccumAppend(pAc
a0f0: 63 75 6d 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 cum, zVal, nVal)
a100: 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 ;. }.}.static v
a110: 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 oid groupConcatF
a120: 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f inalize(sqlite3_
a130: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
a140: 29 7b 0a 20 20 53 74 72 41 63 63 75 6d 20 2a 70 ){. StrAccum *p
a150: 41 63 63 75 6d 3b 0a 20 20 70 41 63 63 75 6d 20 Accum;. pAccum
a160: 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 = sqlite3_aggreg
a170: 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 ate_context(cont
a180: 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 ext, 0);. if( p
a190: 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 69 66 28 Accum ){. if(
a1a0: 20 70 41 63 63 75 6d 2d 3e 74 6f 6f 42 69 67 20 pAccum->tooBig
a1b0: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
a1c0: 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f _result_error_to
a1d0: 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 obig(context);.
a1e0: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 41 63 }else if( pAc
a1f0: 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 cum->mallocFaile
a200: 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 d ){. sqlit
a210: 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f e3_result_error_
a220: 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a nomem(context);.
a230: 20 20 20 20 7d 65 6c 73 65 7b 20 20 20 20 0a 20 }else{ .
a240: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
a250: 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
a260: 2c 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 , sqlite3StrAccu
a270: 6d 46 69 6e 69 73 68 28 70 41 63 63 75 6d 29 2c mFinish(pAccum),
a280: 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 -1, .
a290: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2a0: 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 sqlite3_free);.
a2b0: 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
a2c0: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
a2d0: 6f 65 73 20 70 65 72 2d 63 6f 6e 6e 65 63 74 69 oes per-connecti
a2e0: 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 on function regi
a2f0: 73 74 72 61 74 69 6f 6e 2e 20 20 4d 6f 73 74 0a stration. Most.
a300: 2a 2a 20 6f 66 20 74 68 65 20 62 75 69 6c 74 2d ** of the built-
a310: 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 61 62 6f in functions abo
a320: 76 65 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 ve are part of t
a330: 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 he global functi
a340: 6f 6e 20 73 65 74 2e 0a 2a 2a 20 54 68 69 73 20 on set..** This
a350: 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 64 65 61 routine only dea
a360: 6c 73 20 77 69 74 68 20 74 68 6f 73 65 20 74 68 ls with those th
a370: 61 74 20 61 72 65 20 6e 6f 74 20 67 6c 6f 62 61 at are not globa
a380: 6c 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 l..*/.void sqlit
a390: 65 33 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 e3RegisterBuilti
a3a0: 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 nFunctions(sqlit
a3b0: 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 e3 *db){. int r
a3c0: 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 c = sqlite3_over
a3d0: 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 load_function(db
a3e0: 2c 20 22 4d 41 54 43 48 22 2c 20 32 29 3b 0a 20 , "MATCH", 2);.
a3f0: 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
a400: 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d ITE_NOMEM || rc=
a410: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
a420: 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e if( rc==SQLITE_N
a430: 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 64 62 2d 3e OMEM ){. db->
a440: 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
a450: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 ;. }.}../*.** S
a460: 65 74 20 74 68 65 20 4c 49 4b 45 4f 50 54 20 66 et the LIKEOPT f
a470: 6c 61 67 20 6f 6e 20 74 68 65 20 32 2d 61 72 67 lag on the 2-arg
a480: 75 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e 20 77 ument function w
a490: 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 ith the given na
a4a0: 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f me..*/.static vo
a4b0: 69 64 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 id setLikeOptFla
a4c0: 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 g(sqlite3 *db, c
a4d0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
a4e0: 2c 20 75 38 20 66 6c 61 67 56 61 6c 29 7b 0a 20 , u8 flagVal){.
a4f0: 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a FuncDef *pDef;.
a500: 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 pDef = sqlite3
a510: 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c FindFunction(db,
a520: 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 zName, sqlite3S
a530: 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 0a trlen30(zName),.
a540: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a550: 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 2,
a560: 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b SQLITE_UTF8, 0);
a570: 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 44 . if( ALWAYS(pD
a580: 65 66 29 20 29 7b 0a 20 20 20 20 70 44 65 66 2d ef) ){. pDef-
a590: 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 56 61 6c >flags = flagVal
a5a0: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ;. }.}../*.** R
a5b0: 65 67 69 73 74 65 72 20 74 68 65 20 62 75 69 6c egister the buil
a5c0: 74 2d 69 6e 20 4c 49 4b 45 20 61 6e 64 20 47 4c t-in LIKE and GL
a5d0: 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 OB functions. T
a5e0: 68 65 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 he caseSensitive
a5f0: 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 64 65 .** parameter de
a600: 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65 72 termines whether
a610: 20 6f 72 20 6e 6f 74 20 74 68 65 20 4c 49 4b 45 or not the LIKE
a620: 20 6f 70 65 72 61 74 6f 72 20 69 73 20 63 61 73 operator is cas
a630: 65 0a 2a 2a 20 73 65 6e 73 69 74 69 76 65 2e 20 e.** sensitive.
a640: 20 47 4c 4f 42 20 69 73 20 61 6c 77 61 79 73 20 GLOB is always
a650: 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a case sensitive..
a660: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 52 */.void sqlite3R
a670: 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 egisterLikeFunct
a680: 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 ions(sqlite3 *db
a690: 2c 20 69 6e 74 20 63 61 73 65 53 65 6e 73 69 74 , int caseSensit
a6a0: 69 76 65 29 7b 0a 20 20 73 74 72 75 63 74 20 63 ive){. struct c
a6b0: 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 ompareInfo *pInf
a6c0: 6f 3b 0a 20 20 69 66 28 20 63 61 73 65 53 65 6e o;. if( caseSen
a6d0: 73 69 74 69 76 65 20 29 7b 0a 20 20 20 20 70 49 sitive ){. pI
a6e0: 6e 66 6f 20 3d 20 28 73 74 72 75 63 74 20 63 6f nfo = (struct co
a6f0: 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 mpareInfo*)&like
a700: 49 6e 66 6f 41 6c 74 3b 0a 20 20 7d 65 6c 73 65 InfoAlt;. }else
a710: 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 28 73 {. pInfo = (s
a720: 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 truct compareInf
a730: 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d o*)&likeInfoNorm
a740: 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 43 ;. }. sqlite3C
a750: 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 6c reateFunc(db, "l
a760: 69 6b 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f ike", 2, SQLITE_
a770: 55 54 46 38 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b UTF8, pInfo, lik
a780: 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 30 29 3b eFunc, 0, 0, 0);
a790: 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 . sqlite3Create
a7a0: 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c Func(db, "like",
a7b0: 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 3, SQLITE_UTF8,
a7c0: 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 pInfo, likeFunc
a7d0: 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 , 0, 0, 0);. sq
a7e0: 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 lite3CreateFunc(
a7f0: 64 62 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20 53 db, "glob", 2, S
a800: 51 4c 49 54 45 5f 55 54 46 38 2c 20 0a 20 20 20 QLITE_UTF8, .
a810: 20 20 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 (struct compa
a820: 72 65 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e 66 reInfo*)&globInf
a830: 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 o, likeFunc, 0,
a840: 30 2c 20 30 29 3b 0a 20 20 73 65 74 4c 69 6b 65 0, 0);. setLike
a850: 4f 70 74 46 6c 61 67 28 64 62 2c 20 22 67 6c 6f OptFlag(db, "glo
a860: 62 22 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f b", SQLITE_FUNC_
a870: 4c 49 4b 45 20 7c 20 53 51 4c 49 54 45 5f 46 55 LIKE | SQLITE_FU
a880: 4e 43 5f 43 41 53 45 29 3b 0a 20 20 73 65 74 4c NC_CASE);. setL
a890: 69 6b 65 4f 70 74 46 6c 61 67 28 64 62 2c 20 22 ikeOptFlag(db, "
a8a0: 6c 69 6b 65 22 2c 20 0a 20 20 20 20 20 20 63 61 like", . ca
a8b0: 73 65 53 65 6e 73 69 74 69 76 65 20 3f 20 28 53 seSensitive ? (S
a8c0: 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 QLITE_FUNC_LIKE
a8d0: 7c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 | SQLITE_FUNC_CA
a8e0: 53 45 29 20 3a 20 53 51 4c 49 54 45 5f 46 55 4e SE) : SQLITE_FUN
a8f0: 43 5f 4c 49 4b 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a C_LIKE);.}../*.*
a900: 2a 20 70 45 78 70 72 20 70 6f 69 6e 74 73 20 74 * pExpr points t
a910: 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 o an expression
a920: 77 68 69 63 68 20 69 6d 70 6c 65 6d 65 6e 74 73 which implements
a930: 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 66 a function. If
a940: 0a 2a 2a 20 69 74 20 69 73 20 61 70 70 72 6f 70 .** it is approp
a950: 72 69 61 74 65 20 74 6f 20 61 70 70 6c 79 20 74 riate to apply t
a960: 68 65 20 4c 49 4b 45 20 6f 70 74 69 6d 69 7a 61 he LIKE optimiza
a970: 74 69 6f 6e 20 74 6f 20 74 68 61 74 20 66 75 6e tion to that fun
a980: 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 73 65 ction.** then se
a990: 74 20 61 57 63 5b 30 5d 20 74 68 72 6f 75 67 68 t aWc[0] through
a9a0: 20 61 57 63 5b 32 5d 20 74 6f 20 74 68 65 20 77 aWc[2] to the w
a9b0: 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65 ildcard characte
a9c0: 72 73 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e rs and.** return
a9d0: 20 54 52 55 45 2e 20 20 49 66 20 74 68 65 20 66 TRUE. If the f
a9e0: 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 unction is not a
a9f0: 20 4c 49 4b 45 2d 73 74 79 6c 65 20 66 75 6e 63 LIKE-style func
aa00: 74 69 6f 6e 20 74 68 65 6e 0a 2a 2a 20 72 65 74 tion then.** ret
aa10: 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2f 0a 69 6e urn FALSE..*/.in
aa20: 74 20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 t sqlite3IsLikeF
aa30: 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 unction(sqlite3
aa40: 2a 64 62 2c 20 45 78 70 72 20 2a 70 45 78 70 72 *db, Expr *pExpr
aa50: 2c 20 69 6e 74 20 2a 70 49 73 4e 6f 63 61 73 65 , int *pIsNocase
aa60: 2c 20 63 68 61 72 20 2a 61 57 63 29 7b 0a 20 20 , char *aWc){.
aa70: 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 0a 20 FuncDef *pDef;.
aa80: 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d if( pExpr->op!=
aa90: 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 0a 20 20 20 TK_FUNCTION .
aaa0: 7c 7c 20 21 70 45 78 70 72 2d 3e 78 2e 70 4c 69 || !pExpr->x.pLi
aab0: 73 74 20 0a 20 20 20 7c 7c 20 70 45 78 70 72 2d st . || pExpr-
aac0: 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 >x.pList->nExpr!
aad0: 3d 32 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 =2. ){. retu
aae0: 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 rn 0;. }. asse
aaf0: 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
ab00: 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 erty(pExpr, EP_x
ab10: 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 70 IsSelect) );. p
ab20: 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e Def = sqlite3Fin
ab30: 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 45 dFunction(db, pE
ab40: 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 0a xpr->u.zToken, .
ab50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ab60: 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
ab70: 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 45 78 ite3Strlen30(pEx
ab80: 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0a 20 pr->u.zToken),.
ab90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aba0: 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 53 2, S
abb0: 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a QLITE_UTF8, 0);.
abc0: 20 20 69 66 28 20 4e 45 56 45 52 28 70 44 65 66 if( NEVER(pDef
abd0: 3d 3d 30 29 20 7c 7c 20 28 70 44 65 66 2d 3e 66 ==0) || (pDef->f
abe0: 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
abf0: 4e 43 5f 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 NC_LIKE)==0 ){.
ac00: 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
ac10: 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 65 6d 63 70 .. /* The memcp
ac20: 79 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61 73 y() statement as
ac30: 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 77 sumes that the w
ac40: 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65 ildcard characte
ac50: 72 73 20 61 72 65 0a 20 20 2a 2a 20 74 68 65 20 rs are. ** the
ac60: 66 69 72 73 74 20 74 68 72 65 65 20 73 74 61 74 first three stat
ac70: 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f ements in the co
ac80: 6d 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 mpareInfo struct
ac90: 75 72 65 2e 20 20 54 68 65 0a 20 20 2a 2a 20 61 ure. The. ** a
aca0: 73 73 65 72 74 73 28 29 20 74 68 61 74 20 66 6f sserts() that fo
acb0: 6c 6c 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 llow verify that
acc0: 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f assumption. */
acd0: 0a 20 20 6d 65 6d 63 70 79 28 61 57 63 2c 20 70 . memcpy(aWc, p
ace0: 44 65 66 2d 3e 70 55 73 65 72 44 61 74 61 2c 20 Def->pUserData,
acf0: 33 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 3);. assert( (c
ad00: 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c har*)&likeInfoAl
ad10: 74 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b t == (char*)&lik
ad20: 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 41 6c eInfoAlt.matchAl
ad30: 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26 l );. assert( &
ad40: 28 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 ((char*)&likeInf
ad50: 6f 41 6c 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61 oAlt)[1] == (cha
ad60: 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e r*)&likeInfoAlt.
ad70: 6d 61 74 63 68 4f 6e 65 20 29 3b 0a 20 20 61 73 matchOne );. as
ad80: 73 65 72 74 28 20 26 28 28 63 68 61 72 2a 29 26 sert( &((char*)&
ad90: 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 likeInfoAlt)[2]
ada0: 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 == (char*)&likeI
adb0: 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 53 65 74 20 nfoAlt.matchSet
adc0: 29 3b 0a 20 20 2a 70 49 73 4e 6f 63 61 73 65 20 );. *pIsNocase
add0: 3d 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 = (pDef->flags &
ade0: 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 SQLITE_FUNC_CAS
adf0: 45 29 3d 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 E)==0;. return
ae00: 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 1;.}../*.** All
ae10: 61 6c 6c 20 6f 66 20 74 68 65 20 46 75 6e 63 44 all of the FuncD
ae20: 65 66 20 73 74 72 75 63 74 75 72 65 73 20 69 6e ef structures in
ae30: 20 74 68 65 20 61 42 75 69 6c 74 69 6e 46 75 6e the aBuiltinFun
ae40: 63 5b 5d 20 61 72 72 61 79 20 61 62 6f 76 65 0a c[] array above.
ae50: 2a 2a 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c ** to the global
ae60: 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 68 20 74 function hash t
ae70: 61 62 6c 65 2e 20 20 54 68 69 73 20 6f 63 63 75 able. This occu
ae80: 72 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 rs at start-time
ae90: 20 28 61 73 0a 2a 2a 20 61 20 63 6f 6e 73 65 71 (as.** a conseq
aea0: 75 65 6e 63 65 20 6f 66 20 63 61 6c 6c 69 6e 67 uence of calling
aeb0: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
aec0: 69 7a 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 ize())..**.** Af
aed0: 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ter this routine
aee0: 20 72 75 6e 73 0a 2a 2f 0a 76 6f 69 64 20 73 71 runs.*/.void sq
aef0: 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f lite3RegisterGlo
af00: 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 balFunctions(voi
af10: 64 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 d){. /*. ** Th
af20: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 e following arra
af30: 79 20 68 6f 6c 64 73 20 46 75 6e 63 44 65 66 20 y holds FuncDef
af40: 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 61 structures for a
af50: 6c 6c 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 ll of the functi
af60: 6f 6e 73 0a 20 20 2a 2a 20 64 65 66 69 6e 65 64 ons. ** defined
af70: 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 20 in this file..
af80: 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61 72 72 **. ** The arr
af90: 61 79 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e ay cannot be con
afa0: 73 74 61 6e 74 20 73 69 6e 63 65 20 63 68 61 6e stant since chan
afb0: 67 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 ges are made to
afc0: 74 68 65 0a 20 20 2a 2a 20 46 75 6e 63 44 65 66 the. ** FuncDef
afd0: 2e 70 48 61 73 68 20 65 6c 65 6d 65 6e 74 73 20 .pHash elements
afe0: 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 at start-time.
aff0: 54 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 The elements of
b000: 74 68 69 73 20 61 72 72 61 79 0a 20 20 2a 2a 20 this array. **
b010: 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66 are read-only af
b020: 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ter initializati
b030: 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a on is complete..
b040: 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 53 51 */. static SQ
b050: 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 LITE_WSD FuncDef
b060: 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 aBuiltinFunc[]
b070: 3d 20 7b 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e = {. FUNCTION
b080: 28 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 (ltrim,
b090: 20 20 20 20 20 31 2c 20 31 2c 20 30 2c 20 74 72 1, 1, 0, tr
b0a0: 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 imFunc )
b0b0: 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c ,. FUNCTION(l
b0c0: 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 trim,
b0d0: 20 20 20 32 2c 20 31 2c 20 30 2c 20 74 72 69 6d 2, 1, 0, trim
b0e0: 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a Func ),.
b0f0: 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 FUNCTION(rtr
b100: 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 im,
b110: 20 31 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 1, 2, 0, trimFu
b120: 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 nc ),.
b130: 20 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d FUNCTION(rtrim
b140: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 , 2
b150: 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 , 2, 0, trimFunc
b160: 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 ),.
b170: 46 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 FUNCTION(trim,
b180: 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 1,
b190: 33 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 3, 0, trimFunc
b1a0: 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 ),. FU
b1b0: 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 NCTION(trim,
b1c0: 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 33 2c 2, 3,
b1d0: 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 0, trimFunc
b1e0: 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 ),. FUNC
b1f0: 54 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 TION(min,
b200: 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 31 -1, 0, 1
b210: 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20 20 20 , minmaxFunc
b220: 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
b230: 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 ON(min,
b240: 20 20 20 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 0, 0, 1,
b250: 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
b260: 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 ),. AGGREGAT
b270: 45 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 E(min,
b280: 20 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 6d 69 1, 0, 1, mi
b290: 6e 6d 61 78 53 74 65 70 2c 20 20 20 20 20 20 6d nmaxStep, m
b2a0: 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 29 2c inMaxFinalize ),
b2b0: 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 . FUNCTION(ma
b2c0: 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x,
b2d0: 20 2d 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 -1, 1, 1, minma
b2e0: 78 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 xFunc ),.
b2f0: 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c FUNCTION(max,
b300: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b310: 30 2c 20 31 2c 20 31 2c 20 30 20 20 20 20 20 20 0, 1, 1, 0
b320: 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 ),.
b330: 20 41 47 47 52 45 47 41 54 45 28 6d 61 78 2c 20 AGGREGATE(max,
b340: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 1,
b350: 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 1, 1, minmaxSte
b360: 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 p, minMaxFi
b370: 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20 46 55 nalize ),. FU
b380: 4e 43 54 49 4f 4e 32 28 74 79 70 65 6f 66 2c 20 NCTION2(typeof,
b390: 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 1, 0,
b3a0: 20 30 2c 20 74 79 70 65 6f 66 46 75 6e 63 2c 20 0, typeofFunc,
b3b0: 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 54 59 50 SQLITE_FUNC_TYP
b3c0: 45 4f 46 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 EOF),. FUNCTI
b3d0: 4f 4e 32 28 6c 65 6e 67 74 68 2c 20 20 20 20 20 ON2(length,
b3e0: 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 1, 0, 0,
b3f0: 6c 65 6e 67 74 68 46 75 6e 63 2c 20 20 53 51 4c lengthFunc, SQL
b400: 49 54 45 5f 46 55 4e 43 5f 4c 45 4e 47 54 48 29 ITE_FUNC_LENGTH)
b410: 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 ,. FUNCTION(s
b420: 75 62 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 ubstr,
b430: 20 20 20 32 2c 20 30 2c 20 30 2c 20 73 75 62 73 2, 0, 0, subs
b440: 74 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a trFunc ),.
b450: 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 FUNCTION(sub
b460: 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 str,
b470: 20 33 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72 3, 0, 0, substr
b480: 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 Func ),.
b490: 20 20 46 55 4e 43 54 49 4f 4e 28 61 62 73 2c 20 FUNCTION(abs,
b4a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
b4b0: 2c 20 30 2c 20 30 2c 20 61 62 73 46 75 6e 63 20 , 0, 0, absFunc
b4c0: 20 20 20 20 20 20 20 20 20 29 2c 0a 23 69 66 6e ),.#ifn
b4d0: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
b4e0: 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 FLOATING_POINT.
b4f0: 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e FUNCTION(roun
b500: 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
b510: 31 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 1, 0, 0, roundFu
b520: 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 nc ),.
b530: 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c FUNCTION(round,
b540: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 2,
b550: 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 0, 0, roundFunc
b560: 20 20 20 20 20 20 20 20 29 2c 0a 23 65 6e 64 69 ),.#endi
b570: 66 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 75 f. FUNCTION(u
b580: 70 70 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 pper,
b590: 20 20 20 31 2c 20 30 2c 20 30 2c 20 75 70 70 65 1, 0, 0, uppe
b5a0: 72 46 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a rFunc ),.
b5b0: 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 77 FUNCTION(low
b5c0: 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 er,
b5d0: 20 31 2c 20 30 2c 20 30 2c 20 6c 6f 77 65 72 46 1, 0, 0, lowerF
b5e0: 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 unc ),.
b5f0: 20 20 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 FUNCTION(coale
b600: 73 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 31 sce, 1
b610: 2c 20 30 2c 20 30 2c 20 30 20 20 20 20 20 20 20 , 0, 0, 0
b620: 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 ),.
b630: 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 FUNCTION(coalesc
b640: 65 2c 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 e, 0,
b650: 30 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20 20 0, 0, 0
b660: 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 ),. FU
b670: 4e 43 54 49 4f 4e 32 28 63 6f 61 6c 65 73 63 65 NCTION2(coalesce
b680: 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c , -1, 0,
b690: 20 30 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 2c 20 0, ifnullFunc,
b6a0: 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 SQLITE_FUNC_COA
b6b0: 4c 45 53 43 45 29 2c 0a 20 20 20 20 46 55 4e 43 LESCE),. FUNC
b6c0: 54 49 4f 4e 28 68 65 78 2c 20 20 20 20 20 20 20 TION(hex,
b6d0: 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 1, 0, 0
b6e0: 2c 20 68 65 78 46 75 6e 63 20 20 20 20 20 20 20 , hexFunc
b6f0: 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
b700: 4f 4e 32 28 69 66 6e 75 6c 6c 2c 20 20 20 20 20 ON2(ifnull,
b710: 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 2, 0, 0,
b720: 69 66 6e 75 6c 6c 46 75 6e 63 2c 20 20 53 51 4c ifnullFunc, SQL
b730: 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 43 ITE_FUNC_COALESC
b740: 45 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e E),. FUNCTION
b750: 28 72 61 6e 64 6f 6d 2c 20 20 20 20 20 20 20 20 (random,
b760: 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 72 61 0, 0, 0, ra
b770: 6e 64 6f 6d 46 75 6e 63 20 20 20 20 20 20 20 29 ndomFunc )
b780: 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 ,. FUNCTION(r
b790: 61 6e 64 6f 6d 62 6c 6f 62 2c 20 20 20 20 20 20 andomblob,
b7a0: 20 20 20 31 2c 20 30 2c 20 30 2c 20 72 61 6e 64 1, 0, 0, rand
b7b0: 6f 6d 42 6c 6f 62 20 20 20 20 20 20 20 29 2c 0a omBlob ),.
b7c0: 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6e 75 6c FUNCTION(nul
b7d0: 6c 69 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 lif,
b7e0: 20 32 2c 20 30 2c 20 31 2c 20 6e 75 6c 6c 69 66 2, 0, 1, nullif
b7f0: 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 Func ),.
b800: 20 20 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 FUNCTION(sqlit
b810: 65 5f 76 65 72 73 69 6f 6e 2c 20 20 20 20 20 30 e_version, 0
b820: 2c 20 30 2c 20 30 2c 20 76 65 72 73 69 6f 6e 46 , 0, 0, versionF
b830: 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 unc ),.
b840: 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f FUNCTION(sqlite_
b850: 73 6f 75 72 63 65 5f 69 64 2c 20 20 20 30 2c 20 source_id, 0,
b860: 30 2c 20 30 2c 20 73 6f 75 72 63 65 69 64 46 75 0, 0, sourceidFu
b870: 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 nc ),. FU
b880: 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 6c 6f NCTION(sqlite_lo
b890: 67 2c 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c g, 2, 0,
b8a0: 20 30 2c 20 65 72 72 6c 6f 67 46 75 6e 63 20 20 0, errlogFunc
b8b0: 20 20 20 20 20 29 2c 0a 23 69 66 6e 64 65 66 20 ),.#ifndef
b8c0: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 SQLITE_OMIT_COMP
b8d0: 49 4c 45 4f 50 54 49 4f 4e 5f 44 49 41 47 53 0a ILEOPTION_DIAGS.
b8e0: 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 71 6c FUNCTION(sql
b8f0: 69 74 65 5f 63 6f 6d 70 69 6c 65 6f 70 74 69 6f ite_compileoptio
b900: 6e 5f 75 73 65 64 2c 31 2c 20 30 2c 20 30 2c 20 n_used,1, 0, 0,
b910: 63 6f 6d 70 69 6c 65 6f 70 74 69 6f 6e 75 73 65 compileoptionuse
b920: 64 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 55 dFunc ),. FU
b930: 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 63 6f NCTION(sqlite_co
b940: 6d 70 69 6c 65 6f 70 74 69 6f 6e 5f 67 65 74 2c mpileoption_get,
b950: 20 31 2c 20 30 2c 20 30 2c 20 63 6f 6d 70 69 6c 1, 0, 0, compil
b960: 65 6f 70 74 69 6f 6e 67 65 74 46 75 6e 63 20 20 eoptiongetFunc
b970: 29 2c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c ),.#endif /* SQL
b980: 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 49 4c 45 ITE_OMIT_COMPILE
b990: 4f 50 54 49 4f 4e 5f 44 49 41 47 53 20 2a 2f 0a OPTION_DIAGS */.
b9a0: 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 71 75 6f FUNCTION(quo
b9b0: 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
b9c0: 20 31 2c 20 30 2c 20 30 2c 20 71 75 6f 74 65 46 1, 0, 0, quoteF
b9d0: 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 unc ),.
b9e0: 20 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73 74 5f FUNCTION(last_
b9f0: 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20 20 30 insert_rowid, 0
ba00: 2c 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69 6e 73 , 0, 0, last_ins
ba10: 65 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20 20 20 ert_rowid),.
ba20: 46 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67 65 73 FUNCTION(changes
ba30: 2c 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 , 0,
ba40: 30 2c 20 30 2c 20 63 68 61 6e 67 65 73 20 20 20 0, 0, changes
ba50: 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 ),. FU
ba60: 4e 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63 68 61 NCTION(total_cha
ba70: 6e 67 65 73 2c 20 20 20 20 20 20 30 2c 20 30 2c nges, 0, 0,
ba80: 20 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 0, total_change
ba90: 73 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 s ),. FUNC
baa0: 54 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20 20 20 TION(replace,
bab0: 20 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30 3, 0, 0
bac0: 2c 20 72 65 70 6c 61 63 65 46 75 6e 63 20 20 20 , replaceFunc
bad0: 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
bae0: 4f 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20 20 20 ON(zeroblob,
baf0: 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 1, 0, 0,
bb00: 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 20 20 zeroblobFunc
bb10: 20 29 2c 0a 20 20 23 69 66 64 65 66 20 53 51 4c ),. #ifdef SQL
bb20: 49 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20 20 20 ITE_SOUNDEX.
bb30: 46 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64 65 78 FUNCTION(soundex
bb40: 2c 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 , 1,
bb50: 30 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46 75 6e 0, 0, soundexFun
bb60: 63 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e 64 c ),. #end
bb70: 69 66 0a 20 20 23 69 66 6e 64 65 66 20 53 51 4c if. #ifndef SQL
bb80: 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 ITE_OMIT_LOAD_EX
bb90: 54 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55 4e 43 TENSION. FUNC
bba0: 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 TION(load_extens
bbb0: 69 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c 20 30 ion, 1, 0, 0
bbc0: 2c 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20 , loadExt
bbd0: 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 ),. FUNCTI
bbe0: 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f ON(load_extensio
bbf0: 6e 2c 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 n, 2, 0, 0,
bc00: 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20 20 20 loadExt
bc10: 20 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20 20 ),. #endif.
bc20: 20 41 47 47 52 45 47 41 54 45 28 73 75 6d 2c 20 AGGREGATE(sum,
bc30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 1,
bc40: 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 0, 0, sumStep,
bc50: 20 20 20 20 20 20 20 20 73 75 6d 46 69 6e 61 6c sumFinal
bc60: 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 ize ),. AG
bc70: 47 52 45 47 41 54 45 28 74 6f 74 61 6c 2c 20 20 GREGATE(total,
bc80: 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 1, 0,
bc90: 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 0, sumStep,
bca0: 20 20 20 20 20 74 6f 74 61 6c 46 69 6e 61 6c 69 totalFinali
bcb0: 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 ze ),. AGG
bcc0: 52 45 47 41 54 45 28 61 76 67 2c 20 20 20 20 20 REGATE(avg,
bcd0: 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
bce0: 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20 0, sumStep,
bcf0: 20 20 20 20 61 76 67 46 69 6e 61 6c 69 7a 65 20 avgFinalize
bd00: 20 20 20 29 2c 0a 20 2f 2a 20 41 47 47 52 45 47 ),. /* AGGREG
bd10: 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 ATE(count,
bd20: 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0,
bd30: 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 countStep,
bd40: 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 countFinalize
bd50: 29 2c 20 2a 2f 0a 20 20 20 20 7b 30 2c 53 51 4c ), */. {0,SQL
bd60: 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f ITE_UTF8,SQLITE_
bd70: 46 55 4e 43 5f 43 4f 55 4e 54 2c 30 2c 30 2c 30 FUNC_COUNT,0,0,0
bd80: 2c 63 6f 75 6e 74 53 74 65 70 2c 63 6f 75 6e 74 ,countStep,count
bd90: 46 69 6e 61 6c 69 7a 65 2c 22 63 6f 75 6e 74 22 Finalize,"count"
bda0: 2c 30 2c 30 7d 2c 0a 20 20 20 20 41 47 47 52 45 ,0,0},. AGGRE
bdb0: 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 GATE(count,
bdc0: 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 1, 0, 0,
bdd0: 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 countStep,
bde0: 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 countFinalize
bdf0: 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 ),. AGGREGAT
be00: 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 E(group_concat,
be10: 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 67 72 1, 0, 0, gr
be20: 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 oupConcatStep, g
be30: 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 roupConcatFinali
be40: 7a 65 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 ze),. AGGREGA
be50: 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c TE(group_concat,
be60: 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 67 2, 0, 0, g
be70: 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 roupConcatStep,
be80: 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c groupConcatFinal
be90: 69 7a 65 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b ize),. . LIK
bea0: 45 46 55 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26 EFUNC(glob, 2, &
beb0: 67 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45 globInfo, SQLITE
bec0: 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 _FUNC_LIKE|SQLIT
bed0: 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 E_FUNC_CASE),.
bee0: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41 #ifdef SQLITE_CA
bef0: 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b SE_SENSITIVE_LIK
bf00: 45 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c E. LIKEFUNC(l
bf10: 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 ike, 2, &likeInf
bf20: 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e oAlt, SQLITE_FUN
bf30: 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 C_LIKE|SQLITE_FU
bf40: 4e 43 5f 43 41 53 45 29 2c 0a 20 20 20 20 4c 49 NC_CASE),. LI
bf50: 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20 KEFUNC(like, 3,
bf60: 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 &likeInfoAlt, SQ
bf70: 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 LITE_FUNC_LIKE|S
bf80: 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 QLITE_FUNC_CASE)
bf90: 2c 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 4c 49 ,. #else. LI
bfa0: 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 KEFUNC(like, 2,
bfb0: 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53 &likeInfoNorm, S
bfc0: 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 QLITE_FUNC_LIKE)
bfd0: 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c ,. LIKEFUNC(l
bfe0: 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 ike, 3, &likeInf
bff0: 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 oNorm, SQLITE_FU
c000: 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64 NC_LIKE),. #end
c010: 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69 if. };.. int i
c020: 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 ;. FuncDefHash
c030: 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c *pHash = &GLOBAL
c040: 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 (FuncDefHash, sq
c050: 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 lite3GlobalFunct
c060: 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 ions);. FuncDef
c070: 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 *aFunc = (FuncD
c080: 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 ef*)&GLOBAL(Func
c090: 44 65 66 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e Def, aBuiltinFun
c0a0: 63 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 c);.. for(i=0;
c0b0: 69 3c 41 72 72 61 79 53 69 7a 65 28 61 42 75 69 i<ArraySize(aBui
c0c0: 6c 74 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b ltinFunc); i++){
c0d0: 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 . sqlite3Func
c0e0: 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c DefInsert(pHash,
c0f0: 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d &aFunc[i]);. }
c100: 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 . sqlite3Regist
c110: 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 erDateTimeFuncti
c120: 6f 6e 73 28 29 3b 0a 23 69 66 6e 64 65 66 20 53 ons();.#ifndef S
c130: 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 QLITE_OMIT_ALTER
c140: 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 41 TABLE. sqlite3A
c150: 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 29 3b lterFunctions();
c160: 0a 23 65 6e 64 69 66 0a 7d 0a .#endif.}.