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 2a 0a 2a file scope..**.*
0280: 2a 20 24 49 64 3a 20 66 75 6e 63 2e 63 2c 76 20 * $Id: func.c,v
0290: 31 2e 31 37 32 20 32 30 30 37 2f 30 38 2f 33 30 1.172 2007/08/30
02a0: 20 31 36 3a 33 30 3a 32 37 20 64 61 6e 69 65 6c 16:30:27 daniel
02b0: 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 k1977 Exp $.*/.#
02c0: 69 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 49 include "sqliteI
02d0: 6e 74 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 3c nt.h".#include <
02e0: 63 74 79 70 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 ctype.h>.#includ
02f0: 65 20 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e e <stdlib.h>.#in
0300: 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e 68 3e clude <assert.h>
0310: 0a 23 69 6e 63 6c 75 64 65 20 22 76 64 62 65 49 .#include "vdbeI
0320: 6e 74 2e 68 22 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 nt.h".../*.** Re
0330: 74 75 72 6e 20 74 68 65 20 63 6f 6c 6c 61 74 69 turn the collati
0340: 6e 67 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 6f ng function asso
0350: 63 69 61 74 65 64 20 77 69 74 68 20 61 20 66 75 ciated with a fu
0360: 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 nction..*/.stati
0370: 63 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 c CollSeq *sqlit
0380: 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 e3GetFuncCollSeq
0390: 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
03a0: 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 72 65 *context){. re
03b0: 74 75 72 6e 20 63 6f 6e 74 65 78 74 2d 3e 70 43 turn context->pC
03c0: 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d oll;.}../*.** Im
03d0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
03e0: 74 68 65 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 the non-aggregat
03f0: 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 e min() and max(
0400: 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73 ) functions.*/.s
0410: 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d 61 tatic void minma
0420: 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 xFunc(. sqlite3
0430: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
0440: 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
0450: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
0460: 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 *argv.){. int i
0470: 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20 20 ;. int mask;
0480: 20 2f 2a 20 30 20 66 6f 72 20 6d 69 6e 28 29 20 /* 0 for min()
0490: 6f 72 20 30 78 66 66 66 66 66 66 66 66 20 66 6f or 0xffffffff fo
04a0: 72 20 6d 61 78 28 29 20 2a 2f 0a 20 20 69 6e 74 r max() */. int
04b0: 20 69 42 65 73 74 3b 0a 20 20 43 6f 6c 6c 53 65 iBest;. CollSe
04c0: 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 69 66 28 q *pColl;.. if(
04d0: 20 61 72 67 63 3d 3d 30 20 29 20 72 65 74 75 72 argc==0 ) retur
04e0: 6e 3b 0a 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69 n;. mask = sqli
04f0: 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f te3_user_data(co
0500: 6e 74 65 78 74 29 3d 3d 30 20 3f 20 30 20 3a 20 ntext)==0 ? 0 :
0510: 2d 31 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 -1;. pColl = sq
0520: 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c lite3GetFuncColl
0530: 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 Seq(context);.
0540: 61 73 73 65 72 74 28 20 70 43 6f 6c 6c 20 29 3b assert( pColl );
0550: 0a 20 20 61 73 73 65 72 74 28 20 6d 61 73 6b 3d . assert( mask=
0560: 3d 2d 31 20 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 =-1 || mask==0 )
0570: 3b 0a 20 20 69 42 65 73 74 20 3d 20 30 3b 0a 20 ;. iBest = 0;.
0580: 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c if( sqlite3_val
0590: 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 ue_type(argv[0])
05a0: 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 ==SQLITE_NULL )
05b0: 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d return;. for(i=
05c0: 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 1; i<argc; i++){
05d0: 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
05e0: 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 _value_type(argv
05f0: 5b 69 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c [i])==SQLITE_NUL
0600: 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 L ) return;.
0610: 69 66 28 20 28 73 71 6c 69 74 65 33 4d 65 6d 43 if( (sqlite3MemC
0620: 6f 6d 70 61 72 65 28 61 72 67 76 5b 69 42 65 73 ompare(argv[iBes
0630: 74 5d 2c 20 61 72 67 76 5b 69 5d 2c 20 70 43 6f t], argv[i], pCo
0640: 6c 6c 29 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a ll)^mask)>=0 ){.
0650: 20 20 20 20 20 20 69 42 65 73 74 20 3d 20 69 3b iBest = i;
0660: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c . }. }. sql
0670: 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 ite3_result_valu
0680: 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b e(context, argv[
0690: 69 42 65 73 74 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a iBest]);.}../*.*
06a0: 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 * Return the typ
06b0: 65 20 6f 66 20 74 68 65 20 61 72 67 75 6d 65 6e e of the argumen
06c0: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
06d0: 64 20 74 79 70 65 6f 66 46 75 6e 63 28 0a 20 20 d typeofFunc(.
06e0: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
06f0: 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
0700: 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f argc,. sqlite3_
0710: 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a value **argv.){.
0720: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 const char *z
0730: 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 73 = 0;. switch( s
0740: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
0750: 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 e(argv[0]) ){.
0760: 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 case SQLITE_NU
0770: 4c 4c 3a 20 20 20 20 7a 20 3d 20 22 6e 75 6c 6c LL: z = "null
0780: 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 "; break;.
0790: 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 case SQLITE_INT
07a0: 45 47 45 52 3a 20 7a 20 3d 20 22 69 6e 74 65 67 EGER: z = "integ
07b0: 65 72 22 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 er"; break;.
07c0: 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 case SQLITE_TEXT
07d0: 3a 20 20 20 20 7a 20 3d 20 22 74 65 78 74 22 3b : z = "text";
07e0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 break;. c
07f0: 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 ase SQLITE_FLOAT
0800: 3a 20 20 20 7a 20 3d 20 22 72 65 61 6c 22 3b 20 : z = "real";
0810: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 break;. ca
0820: 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 se SQLITE_BLOB:
0830: 20 20 20 7a 20 3d 20 22 62 6c 6f 62 22 3b 20 20 z = "blob";
0840: 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 break;. }. s
0850: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
0860: 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d xt(context, z, -
0870: 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 1, SQLITE_STATIC
0880: 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 );.}.../*.** Imp
0890: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
08a0: 68 65 20 6c 65 6e 67 74 68 28 29 20 66 75 6e 63 he length() func
08b0: 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tion.*/.static v
08c0: 6f 69 64 20 6c 65 6e 67 74 68 46 75 6e 63 28 0a oid lengthFunc(.
08d0: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
08e0: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
08f0: 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
0900: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
0910: 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 {. int len;..
0920: 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 assert( argc==1
0930: 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c );. switch( sql
0940: 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
0950: 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 argv[0]) ){.
0960: 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 case SQLITE_BLOB
0970: 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 :. case SQLIT
0980: 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 E_INTEGER:. c
0990: 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 ase SQLITE_FLOAT
09a0: 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
09b0: 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 3_result_int(con
09c0: 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 text, sqlite3_va
09d0: 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 lue_bytes(argv[0
09e0: 5d 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ]));. break
09f0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
0a00: 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a SQLITE_TEXT: {.
0a10: 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 const unsi
0a20: 67 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d 20 73 gned char *z = s
0a30: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
0a40: 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 t(argv[0]);.
0a50: 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 if( z==0 ) ret
0a60: 75 72 6e 3b 0a 20 20 20 20 20 20 6c 65 6e 20 3d urn;. len =
0a70: 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 0;. while(
0a80: 20 2a 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6c *z ){. l
0a90: 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 53 51 en++;. SQ
0aa0: 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a LITE_SKIP_UTF8(z
0ab0: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
0ac0: 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
0ad0: 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 6c 65 6e int(context, len
0ae0: 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
0af0: 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
0b00: 74 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 t: {. sqlit
0b10: 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63 e3_result_null(c
0b20: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62 ontext);. b
0b30: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
0b40: 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
0b50: 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 ntation of the a
0b60: 62 73 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f bs() function.*/
0b70: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 62 73 .static void abs
0b80: 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e Func(sqlite3_con
0b90: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 text *context, i
0ba0: 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 nt argc, sqlite3
0bb0: 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a _value **argv){.
0bc0: 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d assert( argc==
0bd0: 31 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 1 );. switch( s
0be0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
0bf0: 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 e(argv[0]) ){.
0c00: 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e case SQLITE_IN
0c10: 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 TEGER: {. i
0c20: 36 34 20 69 56 61 6c 20 3d 20 73 71 6c 69 74 65 64 iVal = sqlite
0c30: 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 3_value_int64(ar
0c40: 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 gv[0]);. if
0c50: 28 20 69 56 61 6c 3c 30 20 29 7b 0a 20 20 20 20 ( iVal<0 ){.
0c60: 20 20 20 20 69 66 28 20 28 69 56 61 6c 3c 3c 31 if( (iVal<<1
0c70: 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
0c80: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
0c90: 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 _error(context,
0ca0: 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f "integer overflo
0cb0: 77 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 w", -1);.
0cc0: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 return;.
0cd0: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 56 61 }. iVa
0ce0: 6c 20 3d 20 2d 69 56 61 6c 3b 0a 20 20 20 20 20 l = -iVal;.
0cf0: 20 7d 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 } . sqlite
0d00: 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 3_result_int64(c
0d10: 6f 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b 0a 20 ontext, iVal);.
0d20: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
0d30: 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
0d40: 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 E_NULL: {.
0d50: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e sqlite3_result_n
0d60: 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 ull(context);.
0d70: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
0d80: 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
0d90: 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 56 61 double rVa
0da0: 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
0db0: 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d e_double(argv[0]
0dc0: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 56 61 );. if( rVa
0dd0: 6c 3c 30 20 29 20 72 56 61 6c 20 3d 20 2d 72 56 l<0 ) rVal = -rV
0de0: 61 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 al;. sqlite
0df0: 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 3_result_double(
0e00: 63 6f 6e 74 65 78 74 2c 20 72 56 61 6c 29 3b 0a context, rVal);.
0e10: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
0e20: 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
0e30: 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f Implementation o
0e40: 66 20 74 68 65 20 73 75 62 73 74 72 28 29 20 66 f the substr() f
0e50: 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 unction..**.** s
0e60: 75 62 73 74 72 28 78 2c 70 31 2c 70 32 29 20 20 ubstr(x,p1,p2)
0e70: 72 65 74 75 72 6e 73 20 70 32 20 63 68 61 72 61 returns p2 chara
0e80: 63 74 65 72 73 20 6f 66 20 78 5b 5d 20 62 65 67 cters of x[] beg
0e90: 69 6e 6e 69 6e 67 20 77 69 74 68 20 70 31 2e 0a inning with p1..
0ea0: 2a 2a 20 70 31 20 69 73 20 31 2d 69 6e 64 65 78 ** p1 is 1-index
0eb0: 65 64 2e 20 20 53 6f 20 73 75 62 73 74 72 28 78 ed. So substr(x
0ec0: 2c 31 2c 31 29 20 72 65 74 75 72 6e 73 20 74 68 ,1,1) returns th
0ed0: 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 e first characte
0ee0: 72 0a 2a 2a 20 6f 66 20 78 2e 20 20 49 66 20 78 r.** of x. If x
0ef0: 20 69 73 20 74 65 78 74 2c 20 74 68 65 6e 20 77 is text, then w
0f00: 65 20 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e 74 e actually count
0f10: 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 UTF-8 character
0f20: 73 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20 61 20 s..** If x is a
0f30: 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 63 6f blob, then we co
0f40: 75 6e 74 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a unt bytes..**.**
0f50: 20 49 66 20 70 31 20 69 73 20 6e 65 67 61 74 69 If p1 is negati
0f60: 76 65 2c 20 74 68 65 6e 20 77 65 20 62 65 67 69 ve, then we begi
0f70: 6e 20 61 62 73 28 70 31 29 20 66 72 6f 6d 20 74 n abs(p1) from t
0f80: 68 65 20 65 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a he end of x[]..*
0f90: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 /.static void su
0fa0: 62 73 74 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 bstrFunc(. sqli
0fb0: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
0fc0: 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
0fd0: 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
0fe0: 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f e **argv.){. co
0ff0: 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
1000: 72 20 2a 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e r *z;. const un
1010: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 32 3b signed char *z2;
1020: 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e . int len;. in
1030: 74 20 70 30 74 79 70 65 3b 0a 20 20 69 36 34 20 t p0type;. i64
1040: 70 31 2c 20 70 32 3b 0a 0a 20 20 61 73 73 65 72 p1, p2;.. asser
1050: 74 28 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 t( argc==3 );.
1060: 70 30 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 p0type = sqlite3
1070: 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 _value_type(argv
1080: 5b 30 5d 29 3b 0a 20 20 69 66 28 20 70 30 74 79 [0]);. if( p0ty
1090: 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 pe==SQLITE_BLOB
10a0: 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c ){. len = sql
10b0: 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
10c0: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 7a (argv[0]);. z
10d0: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
10e0: 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a _blob(argv[0]);.
10f0: 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 if( z==0 ) r
1100: 65 74 75 72 6e 3b 0a 20 20 20 20 61 73 73 65 72 eturn;. asser
1110: 74 28 20 6c 65 6e 3d 3d 73 71 6c 69 74 65 33 5f t( len==sqlite3_
1120: 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 value_bytes(argv
1130: 5b 30 5d 29 20 29 3b 0a 20 20 7d 65 6c 73 65 7b [0]) );. }else{
1140: 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 . z = sqlite3
1150: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
1160: 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d [0]);. if( z=
1170: 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 =0 ) return;.
1180: 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f len = 0;. fo
1190: 72 28 7a 32 3d 7a 3b 20 2a 7a 32 3b 20 6c 65 6e r(z2=z; *z2; len
11a0: 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 ++){. SQLIT
11b0: 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b E_SKIP_UTF8(z2);
11c0: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 31 20 . }. }. p1
11d0: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
11e0: 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 int(argv[1]);.
11f0: 70 32 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c p2 = sqlite3_val
1200: 75 65 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 3b ue_int(argv[2]);
1210: 0a 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 . if( p1<0 ){.
1220: 20 20 20 70 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 p1 += len;.
1230: 20 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 if( p1<0 ){.
1240: 20 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 p2 += p1;.
1250: 20 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 p1 = 0;.
1260: 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 31 }. }else if( p1
1270: 3e 30 20 29 7b 0a 20 20 20 20 70 31 2d 2d 3b 0a >0 ){. p1--;.
1280: 20 20 7d 0a 20 20 69 66 28 20 70 31 2b 70 32 3e }. if( p1+p2>
1290: 6c 65 6e 20 29 7b 0a 20 20 20 20 70 32 20 3d 20 len ){. p2 =
12a0: 6c 65 6e 2d 70 31 3b 0a 20 20 7d 0a 20 20 69 66 len-p1;. }. if
12b0: 28 20 70 30 74 79 70 65 21 3d 53 51 4c 49 54 45 ( p0type!=SQLITE
12c0: 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 77 68 69 _BLOB ){. whi
12d0: 6c 65 28 20 2a 7a 20 26 26 20 70 31 20 29 7b 0a le( *z && p1 ){.
12e0: 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 SQLITE_SKI
12f0: 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 P_UTF8(z);.
1300: 20 70 31 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 p1--;. }.
1310: 20 66 6f 72 28 7a 32 3d 7a 3b 20 2a 7a 32 20 26 for(z2=z; *z2 &
1320: 26 20 70 32 3b 20 70 32 2d 2d 29 7b 0a 20 20 20 & p2; p2--){.
1330: 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 SQLITE_SKIP_U
1340: 54 46 38 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20 TF8(z2);. }.
1350: 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
1360: 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
1370: 28 63 68 61 72 2a 29 7a 2c 20 7a 32 2d 7a 2c 20 (char*)z, z2-z,
1380: 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
1390: 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
13a0: 69 66 28 20 70 32 3c 30 20 29 20 70 32 20 3d 20 if( p2<0 ) p2 =
13b0: 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 0;. sqlite3_r
13c0: 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 esult_blob(conte
13d0: 78 74 2c 20 28 63 68 61 72 2a 29 26 7a 5b 70 31 xt, (char*)&z[p1
13e0: 5d 2c 20 70 32 2c 20 53 51 4c 49 54 45 5f 54 52 ], p2, SQLITE_TR
13f0: 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a ANSIENT);. }.}.
1400: 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
1410: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 6f 75 ation of the rou
1420: 6e 64 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f nd() function.*/
1430: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 75 .static void rou
1440: 6e 64 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 ndFunc(sqlite3_c
1450: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
1460: 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 int argc, sqlit
1470: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 e3_value **argv)
1480: 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 {. int n = 0;.
1490: 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 68 61 double r;. cha
14a0: 72 20 7a 42 75 66 5b 35 30 30 5d 3b 20 20 2f 2a r zBuf[500]; /*
14b0: 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
14c0: 20 25 66 20 72 65 70 72 65 73 65 6e 74 61 74 69 %f representati
14d0: 6f 6e 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 on of the larges
14e0: 74 20 64 6f 75 62 6c 65 20 2a 2f 0a 20 20 61 73 t double */. as
14f0: 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c sert( argc==1 ||
1500: 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66 argc==2 );. if
1510: 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 ( argc==2 ){.
1520: 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c if( SQLITE_NULL
1530: 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ==sqlite3_value_
1540: 74 79 70 65 28 61 72 67 76 5b 31 5d 29 20 29 20 type(argv[1]) )
1550: 72 65 74 75 72 6e 3b 0a 20 20 20 20 6e 20 3d 20 return;. n =
1560: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e sqlite3_value_in
1570: 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 t(argv[1]);.
1580: 69 66 28 20 6e 3e 33 30 20 29 20 6e 20 3d 20 33 if( n>30 ) n = 3
1590: 30 3b 0a 20 20 20 20 69 66 28 20 6e 3c 30 20 29 0;. if( n<0 )
15a0: 20 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 n = 0;. }. if
15b0: 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ( sqlite3_value_
15c0: 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 type(argv[0])==S
15d0: 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 QLITE_NULL ) ret
15e0: 75 72 6e 3b 0a 20 20 72 20 3d 20 73 71 6c 69 74 urn;. r = sqlit
15f0: 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 e3_value_double(
1600: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69 argv[0]);. sqli
1610: 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a te3_snprintf(siz
1620: 65 6f 66 28 7a 42 75 66 29 2c 7a 42 75 66 2c 22 eof(zBuf),zBuf,"
1630: 25 2e 2a 66 22 2c 6e 2c 72 29 3b 0a 20 20 73 71 %.*f",n,r);. sq
1640: 6c 69 74 65 33 41 74 6f 46 28 7a 42 75 66 2c 20 lite3AtoF(zBuf,
1650: 26 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 &r);. sqlite3_r
1660: 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e esult_double(con
1670: 74 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a text, r);.}../*.
1680: 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 ** Allocate nByt
1690: 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 e bytes of space
16a0: 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 6d using sqlite3_m
16b0: 61 6c 6c 6f 63 28 29 2e 20 49 66 20 74 68 65 0a alloc(). If the.
16c0: 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ** allocation fa
16d0: 69 6c 73 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 ils, call sqlite
16e0: 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 3_result_error_n
16f0: 6f 6d 65 6d 28 29 20 74 6f 20 6e 6f 74 69 66 79 omem() to notify
1700: 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
1710: 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6d 61 6c handle that mal
1720: 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64 loc() has failed
1730: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1740: 20 2a 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 *contextMalloc(
1750: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1760: 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 6e 42 *context, int nB
1770: 79 74 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 20 yte){. char *z
1780: 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 = sqlite3_malloc
1790: 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 21 (nByte);. if( !
17a0: 7a 20 26 26 20 6e 42 79 74 65 3e 30 20 29 7b 0a z && nByte>0 ){.
17b0: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
17c0: 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 lt_error_nomem(c
17d0: 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 ontext);. }. r
17e0: 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn z;.}../*.*
17f0: 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
1800: 20 6f 66 20 74 68 65 20 75 70 70 65 72 28 29 20 of the upper()
1810: 61 6e 64 20 6c 6f 77 65 72 28 29 20 53 51 4c 20 and lower() SQL
1820: 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 functions..*/.st
1830: 61 74 69 63 20 76 6f 69 64 20 75 70 70 65 72 46 atic void upperF
1840: 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 unc(sqlite3_cont
1850: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e ext *context, in
1860: 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f t argc, sqlite3_
1870: 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 value **argv){.
1880: 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e char *z1;. con
1890: 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69 st char *z2;. i
18a0: 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 61 nt i, n;. if( a
18b0: 72 67 63 3c 31 20 7c 7c 20 53 51 4c 49 54 45 5f rgc<1 || SQLITE_
18c0: 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 NULL==sqlite3_va
18d0: 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d lue_type(argv[0]
18e0: 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 32 ) ) return;. z2
18f0: 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 = (char*)sqlite
1900: 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
1910: 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c v[0]);. n = sql
1920: 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
1930: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 (argv[0]);. /*
1940: 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 Verify that the
1950: 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29 call to _bytes()
1960: 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69 does not invali
1970: 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 29 date the _text()
1980: 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73 pointer */. as
1990: 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a sert( z2==(char*
19a0: 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
19b0: 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a ext(argv[0]) );.
19c0: 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20 if( z2 ){.
19d0: 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c z1 = contextMall
19e0: 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 2b 31 29 oc(context, n+1)
19f0: 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29 7b 0a ;. if( z1 ){.
1a00: 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 31 2c memcpy(z1,
1a10: 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20 20 z2, n+1);.
1a20: 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b for(i=0; z1[i];
1a30: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a i++){. z
1a40: 31 5b 69 5d 20 3d 20 74 6f 75 70 70 65 72 28 7a 1[i] = toupper(z
1a50: 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 1[i]);. }.
1a60: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 sqlite3_res
1a70: 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
1a80: 2c 20 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65 , z1, -1, sqlite
1a90: 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 3_free);. }.
1aa0: 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}.static void
1ab0: 20 6c 6f 77 65 72 46 75 6e 63 28 73 71 6c 69 74 lowerFunc(sqlit
1ac0: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
1ad0: 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 ext, int argc, s
1ae0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
1af0: 72 67 76 29 7b 0a 20 20 63 68 61 72 20 2a 7a 31 rgv){. char *z1
1b00: 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
1b10: 7a 32 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a z2;. int i, n;.
1b20: 20 20 69 66 28 20 61 72 67 63 3c 31 20 7c 7c 20 if( argc<1 ||
1b30: 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c SQLITE_NULL==sql
1b40: 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
1b50: 61 72 67 76 5b 30 5d 29 20 29 20 72 65 74 75 72 argv[0]) ) retur
1b60: 6e 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61 72 2a n;. z2 = (char*
1b70: 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
1b80: 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 ext(argv[0]);.
1b90: 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 n = sqlite3_valu
1ba0: 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 e_bytes(argv[0])
1bb0: 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 ;. /* Verify th
1bc0: 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5f at the call to _
1bd0: 62 79 74 65 73 28 29 20 64 6f 65 73 20 6e 6f 74 bytes() does not
1be0: 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 invalidate the
1bf0: 5f 74 65 78 74 28 29 20 70 6f 69 6e 74 65 72 20 _text() pointer
1c00: 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 32 3d */. assert( z2=
1c10: 3d 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f =(char*)sqlite3_
1c20: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
1c30: 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 32 20 0]) );. if( z2
1c40: 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e 74 ){. z1 = cont
1c50: 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 extMalloc(contex
1c60: 74 2c 20 6e 2b 31 29 3b 0a 20 20 20 20 69 66 28 t, n+1);. if(
1c70: 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d z1 ){. mem
1c80: 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 cpy(z1, z2, n+1)
1c90: 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b ;. for(i=0;
1ca0: 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 z1[i]; i++){.
1cb0: 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 74 6f z1[i] = to
1cc0: 6c 6f 77 65 72 28 7a 31 5b 69 5d 29 3b 0a 20 20 lower(z1[i]);.
1cd0: 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
1ce0: 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
1cf0: 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 2d 31 2c context, z1, -1,
1d00: 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a sqlite3_free);.
1d10: 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
1d20: 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
1d30: 6e 20 6f 66 20 74 68 65 20 49 46 4e 55 4c 4c 28 n of the IFNULL(
1d40: 29 2c 20 4e 56 4c 28 29 2c 20 61 6e 64 20 43 4f ), NVL(), and CO
1d50: 41 4c 45 53 43 45 28 29 20 66 75 6e 63 74 69 6f ALESCE() functio
1d60: 6e 73 2e 20 20 0a 2a 2a 20 41 6c 6c 20 74 68 72 ns. .** All thr
1d70: 65 65 20 64 6f 20 74 68 65 20 73 61 6d 65 20 74 ee do the same t
1d80: 68 69 6e 67 2e 20 20 54 68 65 79 20 72 65 74 75 hing. They retu
1d90: 72 6e 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e rn the first non
1da0: 2d 4e 55 4c 4c 0a 2a 2a 20 61 72 67 75 6d 65 6e -NULL.** argumen
1db0: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
1dc0: 64 20 69 66 6e 75 6c 6c 46 75 6e 63 28 0a 20 20 d ifnullFunc(.
1dd0: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1de0: 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 *context,. int
1df0: 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f argc,. sqlite3_
1e00: 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a value **argv.){.
1e10: 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
1e20: 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 =0; i<argc; i++)
1e30: 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 {. if( SQLITE
1e40: 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76 _NULL!=sqlite3_v
1e50: 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69 alue_type(argv[i
1e60: 5d 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ]) ){. sqli
1e70: 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 te3_result_value
1e80: 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 (context, argv[i
1e90: 5d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b ]);. break;
1ea0: 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
1eb0: 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
1ec0: 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 28 29 2e 20 on of random().
1ed0: 20 52 65 74 75 72 6e 20 61 20 72 61 6e 64 6f 6d Return a random
1ee0: 20 69 6e 74 65 67 65 72 2e 20 20 0a 2a 2f 0a 73 integer. .*/.s
1ef0: 74 61 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f tatic void rando
1f00: 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 mFunc(. sqlite3
1f10: 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
1f20: 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
1f30: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1f40: 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 *argv.){. sqlit
1f50: 65 5f 69 6e 74 36 34 20 72 3b 0a 20 20 73 71 6c e_int64 r;. sql
1f60: 69 74 65 33 52 61 6e 64 6f 6d 6e 65 73 73 28 73 ite3Randomness(s
1f70: 69 7a 65 6f 66 28 72 29 2c 20 26 72 29 3b 0a 20 izeof(r), &r);.
1f80: 20 69 66 28 20 28 72 3c 3c 31 29 3d 3d 30 20 29 if( (r<<1)==0 )
1f90: 20 72 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 r = 0; /* Prev
1fa0: 65 6e 74 20 30 78 38 30 30 30 2e 2e 2e 2e 20 61 ent 0x8000.... a
1fb0: 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 6f 20 s the result so
1fc0: 74 68 61 74 20 77 65 20 2a 2f 0a 20 20 20 20 20 that we */.
1fd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fe0: 20 20 20 20 20 2f 2a 20 63 61 6e 20 61 6c 77 61 /* can alwa
1ff0: 79 73 20 64 6f 20 61 62 73 28 29 20 6f 66 20 74 ys do abs() of t
2000: 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 73 he result */. s
2010: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
2020: 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b t64(context, r);
2030: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d .}../*.** Implem
2040: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64 entation of rand
2050: 6f 6d 62 6c 6f 62 28 4e 29 2e 20 20 52 65 74 75 omblob(N). Retu
2060: 72 6e 20 61 20 72 61 6e 64 6f 6d 20 62 6c 6f 62 rn a random blob
2070: 0a 2a 2a 20 74 68 61 74 20 69 73 20 4e 20 62 79 .** that is N by
2080: 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 tes long..*/.sta
2090: 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 42 tic void randomB
20a0: 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 lob(. sqlite3_c
20b0: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c ontext *context,
20c0: 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 . int argc,. s
20d0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
20e0: 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a rgv.){. int n;.
20f0: 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
2100: 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 *p;. assert( ar
2110: 67 63 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 73 gc==1 );. n = s
2120: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
2130: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 (argv[0]);. if(
2140: 20 6e 3c 31 20 29 7b 0a 20 20 20 20 6e 20 3d 20 n<1 ){. n =
2150: 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 53 1;. }. if( n>S
2160: 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 QLITE_MAX_LENGTH
2170: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
2180: 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f result_error_too
2190: 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 big(context);.
21a0: 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 return;. }.
21b0: 70 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f p = contextMallo
21c0: 63 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b 0a 20 c(context, n);.
21d0: 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 if( p ){. sq
21e0: 6c 69 74 65 33 52 61 6e 64 6f 6d 6e 65 73 73 28 lite3Randomness(
21f0: 6e 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 n, p);. sqlit
2200: 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63 e3_result_blob(c
2210: 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 70 ontext, (char*)p
2220: 2c 20 6e 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 , n, sqlite3_fre
2230: 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a e);. }.}../*.**
2240: 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
2250: 6f 66 20 74 68 65 20 6c 61 73 74 5f 69 6e 73 65 of the last_inse
2260: 72 74 5f 72 6f 77 69 64 28 29 20 53 51 4c 20 66 rt_rowid() SQL f
2270: 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 unction. The re
2280: 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 73 turn.** value is
2290: 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 the same as the
22a0: 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e sqlite3_last_in
22b0: 73 65 72 74 5f 72 6f 77 69 64 28 29 20 41 50 49 sert_rowid() API
22c0: 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
22d0: 61 74 69 63 20 76 6f 69 64 20 6c 61 73 74 5f 69 atic void last_i
22e0: 6e 73 65 72 74 5f 72 6f 77 69 64 28 0a 20 20 73 nsert_rowid(. s
22f0: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
2300: 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 20 context, . int
2310: 61 72 67 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f arg, . sqlite3_
2320: 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a value **argv.){.
2330: 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
2340: 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
2350: 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 a(context);. sq
2360: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 lite3_result_int
2370: 36 34 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 64(context, sqli
2380: 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f te3_last_insert_
2390: 72 6f 77 69 64 28 64 62 29 29 3b 0a 7d 0a 0a 2f rowid(db));.}../
23a0: 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
23b0: 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 61 6e 67 ion of the chang
23c0: 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f es() SQL functio
23d0: 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 n. The return v
23e0: 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 73 alue is the.** s
23f0: 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69 74 ame as the sqlit
2400: 65 33 5f 63 68 61 6e 67 65 73 28 29 20 41 50 49 e3_changes() API
2410: 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
2420: 61 74 69 63 20 76 6f 69 64 20 63 68 61 6e 67 65 atic void change
2430: 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e s(. sqlite3_con
2440: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 text *context,.
2450: 20 69 6e 74 20 61 72 67 2c 0a 20 20 73 71 6c 69 int arg,. sqli
2460: 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
2470: 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 .){. sqlite3 *d
2480: 62 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72 b = sqlite3_user
2490: 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a _data(context);.
24a0: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
24b0: 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 _int(context, sq
24c0: 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 64 62 lite3_changes(db
24d0: 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 ));.}../*.** Imp
24e0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
24f0: 68 65 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 he total_changes
2500: 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e () SQL function.
2510: 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c The return val
2520: 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d ue is.** the sam
2530: 65 20 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 e as the sqlite3
2540: 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 _total_changes()
2550: 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a API function..*
2560: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f /.static void to
2570: 74 61 6c 5f 63 68 61 6e 67 65 73 28 0a 20 20 73 tal_changes(. s
2580: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
2590: 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
25a0: 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 rg,. sqlite3_va
25b0: 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
25c0: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 sqlite3 *db = sq
25d0: 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 lite3_user_data(
25e0: 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 context);. sqli
25f0: 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 te3_result_int(c
2600: 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f ontext, sqlite3_
2610: 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 64 62 total_changes(db
2620: 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 ));.}../*.** A s
2630: 74 72 75 63 74 75 72 65 20 64 65 66 69 6e 69 6e tructure definin
2640: 67 20 68 6f 77 20 74 6f 20 64 6f 20 47 4c 4f 42 g how to do GLOB
2650: 2d 73 74 79 6c 65 20 63 6f 6d 70 61 72 69 73 6f -style compariso
2660: 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 63 6f ns..*/.struct co
2670: 6d 70 61 72 65 49 6e 66 6f 20 7b 0a 20 20 75 38 mpareInfo {. u8
2680: 20 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 75 38 20 matchAll;. u8
2690: 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75 38 20 6d matchOne;. u8 m
26a0: 61 74 63 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f atchSet;. u8 no
26b0: 43 61 73 65 3b 0a 7d 3b 0a 0a 73 74 61 74 69 63 Case;.};..static
26c0: 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f const struct co
26d0: 6d 70 61 72 65 49 6e 66 6f 20 67 6c 6f 62 49 6e mpareInfo globIn
26e0: 66 6f 20 3d 20 7b 20 27 2a 27 2c 20 27 3f 27 2c fo = { '*', '?',
26f0: 20 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a 20 54 68 '[', 0 };./* Th
2700: 65 20 63 6f 72 72 65 63 74 20 53 51 4c 2d 39 32 e correct SQL-92
2710: 20 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72 behavior is for
2720: 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 the LIKE operat
2730: 6f 72 20 74 6f 20 69 67 6e 6f 72 65 0a 2a 2a 20 or to ignore.**
2740: 63 61 73 65 2e 20 20 54 68 75 73 20 20 27 61 27 case. Thus 'a'
2750: 20 4c 49 4b 45 20 27 41 27 20 77 6f 75 6c 64 20 LIKE 'A' would
2760: 62 65 20 74 72 75 65 2e 20 2a 2f 0a 73 74 61 74 be true. */.stat
2770: 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 ic const struct
2780: 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b 65 compareInfo like
2790: 49 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20 27 25 27 InfoNorm = { '%'
27a0: 2c 20 27 5f 27 2c 20 20 20 30 2c 20 31 20 7d 3b , '_', 0, 1 };
27b0: 0a 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 43 41 ./* If SQLITE_CA
27c0: 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b SE_SENSITIVE_LIK
27d0: 45 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 E is defined, th
27e0: 65 6e 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 en the LIKE oper
27f0: 61 74 6f 72 0a 2a 2a 20 69 73 20 63 61 73 65 20 ator.** is case
2800: 73 65 6e 73 69 74 69 76 65 20 63 61 75 73 69 6e sensitive causin
2810: 67 20 27 61 27 20 4c 49 4b 45 20 27 41 27 20 74 g 'a' LIKE 'A' t
2820: 6f 20 62 65 20 66 61 6c 73 65 20 2a 2f 0a 73 74 o be false */.st
2830: 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 atic const struc
2840: 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c 69 t compareInfo li
2850: 6b 65 49 6e 66 6f 41 6c 74 20 3d 20 7b 20 27 25 keInfoAlt = { '%
2860: 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 30 20 7d ', '_', 0, 0 }
2870: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 ;../*.** Compare
2880: 20 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69 6e two UTF-8 strin
2890: 67 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79 20 gs for equality
28a0: 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74 20 where the first
28b0: 73 74 72 69 6e 67 20 63 61 6e 0a 2a 2a 20 70 6f string can.** po
28c0: 74 65 6e 74 69 61 6c 6c 79 20 62 65 20 61 20 22 tentially be a "
28d0: 67 6c 6f 62 22 20 65 78 70 72 65 73 73 69 6f 6e glob" expression
28e0: 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 . Return true (
28f0: 31 29 20 69 66 20 74 68 65 79 0a 2a 2a 20 61 72 1) if they.** ar
2900: 65 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 66 e the same and f
2910: 61 6c 73 65 20 28 30 29 20 69 66 20 74 68 65 79 alse (0) if they
2920: 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 0a are different..
2930: 2a 2a 0a 2a 2a 20 47 6c 6f 62 62 69 6e 67 20 72 **.** Globbing r
2940: 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ules:.**.**
2950: 20 27 2a 27 20 20 20 20 20 20 20 4d 61 74 63 68 '*' Match
2960: 65 73 20 61 6e 79 20 73 65 71 75 65 6e 63 65 20 es any sequence
2970: 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 of zero or more
2980: 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a characters..**.*
2990: 2a 20 20 20 20 20 20 27 3f 27 20 20 20 20 20 20 * '?'
29a0: 20 4d 61 74 63 68 65 73 20 65 78 61 63 74 6c 79 Matches exactly
29b0: 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72 2e 0a one character..
29c0: 2a 2a 0a 2a 2a 20 20 20 20 20 5b 2e 2e 2e 5d 20 **.** [...]
29d0: 20 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 Matches one
29e0: 20 63 68 61 72 61 63 74 65 72 20 66 72 6f 6d 20 character from
29f0: 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 73 the enclosed lis
2a00: 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 t of.**
2a10: 20 20 20 20 20 20 20 63 68 61 72 61 63 74 65 72 character
2a20: 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 5e 2e s..**.** [^.
2a30: 2e 2e 5d 20 20 20 20 20 4d 61 74 63 68 65 73 20 ..] Matches
2a40: 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 6e 6f one character no
2a50: 74 20 69 6e 20 74 68 65 20 65 6e 63 6c 6f 73 65 t in the enclose
2a60: 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 d list..**.** Wi
2a70: 74 68 20 74 68 65 20 5b 2e 2e 2e 5d 20 61 6e 64 th the [...] and
2a80: 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68 69 6e 67 [^...] matching
2a90: 2c 20 61 20 27 5d 27 20 63 68 61 72 61 63 74 65 , a ']' characte
2aa0: 72 20 63 61 6e 20 62 65 20 69 6e 63 6c 75 64 65 r can be include
2ab0: 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69 73 74 d.** in the list
2ac0: 20 62 79 20 6d 61 6b 69 6e 67 20 69 74 20 74 68 by making it th
2ad0: 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 e first characte
2ae0: 72 20 61 66 74 65 72 20 27 5b 27 20 6f 72 20 27 r after '[' or '
2af0: 5e 27 2e 20 20 41 0a 2a 2a 20 72 61 6e 67 65 20 ^'. A.** range
2b00: 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 63 61 of characters ca
2b10: 6e 20 62 65 20 73 70 65 63 69 66 69 65 64 20 75 n be specified u
2b20: 73 69 6e 67 20 27 2d 27 2e 20 20 45 78 61 6d 70 sing '-'. Examp
2b30: 6c 65 3a 0a 2a 2a 20 22 5b 61 2d 7a 5d 22 20 6d le:.** "[a-z]" m
2b40: 61 74 63 68 65 73 20 61 6e 79 20 73 69 6e 67 6c atches any singl
2b50: 65 20 6c 6f 77 65 72 2d 63 61 73 65 20 6c 65 74 e lower-case let
2b60: 74 65 72 2e 20 20 54 6f 20 6d 61 74 63 68 20 61 ter. To match a
2b70: 20 27 2d 27 2c 20 6d 61 6b 65 0a 2a 2a 20 69 74 '-', make.** it
2b80: 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 the last charac
2b90: 74 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 2e ter in the list.
2ba0: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
2bb0: 69 6e 65 20 69 73 20 75 73 75 61 6c 6c 79 20 71 ine is usually q
2bc0: 75 69 63 6b 2c 20 62 75 74 20 63 61 6e 20 62 65 uick, but can be
2bd0: 20 4e 2a 2a 32 20 69 6e 20 74 68 65 20 77 6f 72 N**2 in the wor
2be0: 73 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 48 st case..**.** H
2bf0: 69 6e 74 73 3a 20 74 6f 20 6d 61 74 63 68 20 27 ints: to match '
2c00: 2a 27 20 6f 72 20 27 3f 27 2c 20 70 75 74 20 74 *' or '?', put t
2c10: 68 65 6d 20 69 6e 20 22 5b 5d 22 2e 20 20 4c 69 hem in "[]". Li
2c20: 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ke this:.**.**
2c30: 20 20 20 20 20 20 20 61 62 63 5b 2a 5d 78 79 7a abc[*]xyz
2c40: 20 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 Matches
2c50: 22 61 62 63 2a 78 79 7a 22 20 6f 6e 6c 79 0a 2a "abc*xyz" only.*
2c60: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 74 /.static int pat
2c70: 74 65 72 6e 43 6f 6d 70 61 72 65 28 0a 20 20 63 ternCompare(. c
2c80: 6f 6e 73 74 20 75 38 20 2a 7a 50 61 74 74 65 72 onst u8 *zPatter
2c90: 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n,
2ca0: 2f 2a 20 54 68 65 20 67 6c 6f 62 20 70 61 74 74 /* The glob patt
2cb0: 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 ern */. const u
2cc0: 38 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 20 8 *zString,
2cd0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
2ce0: 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70 61 string to compa
2cf0: 72 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 67 re against the g
2d00: 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 lob */. const s
2d10: 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 truct compareInf
2d20: 6f 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20 49 6e 66 o *pInfo, /* Inf
2d30: 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 68 ormation about h
2d40: 6f 77 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d ow to do the com
2d50: 70 61 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 pare */. const
2d60: 69 6e 74 20 65 73 63 20 20 20 20 20 20 20 20 20 int esc
2d70: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
2d80: 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74 e escape charact
2d90: 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 er */.){. int c
2da0: 2c 20 63 32 3b 0a 20 20 69 6e 74 20 69 6e 76 65 , c2;. int inve
2db0: 72 74 3b 0a 20 20 69 6e 74 20 73 65 65 6e 3b 0a rt;. int seen;.
2dc0: 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 20 3d 20 u8 matchOne =
2dd0: 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 4f 6e 65 3b pInfo->matchOne;
2de0: 0a 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 20 3d . u8 matchAll =
2df0: 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 41 6c 6c pInfo->matchAll
2e00: 3b 0a 20 20 75 38 20 6d 61 74 63 68 53 65 74 20 ;. u8 matchSet
2e10: 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 53 65 = pInfo->matchSe
2e20: 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65 20 3d t;. u8 noCase =
2e30: 20 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73 65 3b 20 pInfo->noCase;
2e40: 0a 20 20 69 6e 74 20 70 72 65 76 45 73 63 61 70 . int prevEscap
2e50: 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72 e = 0; /* Tr
2e60: 75 65 20 69 66 20 74 68 65 20 70 72 65 76 69 6f ue if the previo
2e70: 75 73 20 63 68 61 72 61 63 74 65 72 20 77 61 73 us character was
2e80: 20 27 65 73 63 61 70 65 27 20 2a 2f 0a 0a 20 20 'escape' */..
2e90: 77 68 69 6c 65 28 20 28 63 20 3d 20 73 71 6c 69 while( (c = sqli
2ea0: 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 te3Utf8Read(zPat
2eb0: 74 65 72 6e 2c 30 2c 26 7a 50 61 74 74 65 72 6e tern,0,&zPattern
2ec0: 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 ))!=0 ){. if(
2ed0: 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26 20 !prevEscape &&
2ee0: 63 3d 3d 6d 61 74 63 68 41 6c 6c 20 29 7b 0a 20 c==matchAll ){.
2ef0: 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 73 while( (c=s
2f00: 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
2f10: 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50 61 74 74 Pattern,0,&zPatt
2f20: 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63 68 41 6c ern)) == matchAl
2f30: 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l.
2f40: 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63 68 4f 6e || c == matchOn
2f50: 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 e ){. if(
2f60: 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 26 26 20 c==matchOne &&
2f70: 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 sqlite3Utf8Read(
2f80: 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 zString, 0, &zSt
2f90: 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 ring)==0 ){.
2fa0: 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
2fb0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2fc0: 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 }. if( c==0
2fd0: 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
2fe0: 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 rn 1;. }els
2ff0: 65 20 69 66 28 20 63 3d 3d 65 73 63 20 29 7b 0a e if( c==esc ){.
3000: 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 c = sqli
3010: 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 te3Utf8Read(zPat
3020: 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 tern, 0, &zPatte
3030: 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 rn);. if(
3040: 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 c==0 ){.
3050: 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
3060: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
3070: 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 53 se if( c==matchS
3080: 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 et ){. as
3090: 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b 20 sert( esc==0 );
30a0: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
30b0: 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b is GLOB, not LIK
30c0: 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 E */. ass
30d0: 65 72 74 28 20 6d 61 74 63 68 53 65 74 3c 30 78 ert( matchSet<0x
30e0: 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20 69 73 80 ); /* '[' is
30f0: 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 a single-byte c
3100: 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 haracter */.
3110: 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 74 72 while( *zStr
3120: 69 6e 67 20 26 26 20 70 61 74 74 65 72 6e 43 6f ing && patternCo
3130: 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e 5b mpare(&zPattern[
3140: 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66 -1],zString,pInf
3150: 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a 20 20 20 o,esc)==0 ){.
3160: 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b SQLITE_SK
3170: 49 50 5f 55 54 46 38 28 7a 53 74 72 69 6e 67 29 IP_UTF8(zString)
3180: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
3190: 20 20 20 20 72 65 74 75 72 6e 20 2a 7a 53 74 72 return *zStr
31a0: 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20 20 7d 0a ing!=0;. }.
31b0: 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 32 while( (c2
31c0: 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 = sqlite3Utf8Re
31d0: 61 64 28 7a 53 74 72 69 6e 67 2c 30 2c 26 7a 53 ad(zString,0,&zS
31e0: 74 72 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20 20 tring))!=0 ){.
31f0: 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73 65 if( noCase
3200: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 32 ){. c2
3210: 20 3d 20 63 32 3c 30 78 38 30 20 3f 20 73 71 6c = c2<0x80 ? sql
3220: 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 ite3UpperToLower
3230: 5b 63 32 5d 20 3a 20 63 32 3b 0a 20 20 20 20 20 [c2] : c2;.
3240: 20 20 20 20 20 63 20 3d 20 63 3c 30 78 38 30 20 c = c<0x80
3250: 3f 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f ? sqlite3UpperTo
3260: 4c 6f 77 65 72 5b 63 5d 20 3a 20 63 3b 0a 20 20 Lower[c] : c;.
3270: 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 while( c
3280: 32 20 21 3d 20 30 20 26 26 20 63 32 20 21 3d 20 2 != 0 && c2 !=
3290: 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 c ){.
32a0: 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 c2 = sqlite3Utf
32b0: 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 8Read(zString, 0
32c0: 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 , &zString);.
32d0: 20 20 20 20 20 20 20 20 20 69 66 28 20 63 32 3c if( c2<
32e0: 30 78 38 30 20 29 20 63 32 20 3d 20 73 71 6c 69 0x80 ) c2 = sqli
32f0: 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b te3UpperToLower[
3300: 63 32 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d c2];. }
3310: 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
3320: 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 while(
3330: 20 63 32 20 21 3d 20 30 20 26 26 20 63 32 20 21 c2 != 0 && c2 !
3340: 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 = c ){.
3350: 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 c2 = sqlite3U
3360: 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c tf8Read(zString,
3370: 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 0, &zString);.
3380: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
3390: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
33a0: 20 63 32 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 c2==0 ) return
33b0: 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 0;. if( p
33c0: 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 7a 50 atternCompare(zP
33d0: 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e 67 2c 70 attern,zString,p
33e0: 49 6e 66 6f 2c 65 73 63 29 20 29 20 72 65 74 75 Info,esc) ) retu
33f0: 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn 1;. }.
3400: 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
3410: 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65 }else if( !pre
3420: 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61 vEscape && c==ma
3430: 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20 20 tchOne ){.
3440: 69 66 28 20 73 71 6c 69 74 65 33 55 74 66 38 52 if( sqlite3Utf8R
3450: 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 ead(zString, 0,
3460: 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a &zString)==0 ){.
3470: 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 return 0
3480: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
3490: 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 lse if( c==match
34a0: 53 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 Set ){. int
34b0: 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 prior_c = 0;.
34c0: 20 20 20 20 61 73 73 65 72 74 28 20 65 73 63 3d assert( esc=
34d0: 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54 68 69 73 =0 ); /* This
34e0: 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20 66 6f 72 only occurs for
34f0: 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45 20 GLOB, not LIKE
3500: 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e 20 3d 20 */. seen =
3510: 30 3b 0a 20 20 20 20 20 20 69 6e 76 65 72 74 20 0;. invert
3520: 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d 20 73 = 0;. c = s
3530: 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
3540: 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 String, 0, &zStr
3550: 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ing);. if(
3560: 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b c==0 ) return 0;
3570: 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 . c2 = sqli
3580: 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 te3Utf8Read(zPat
3590: 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 tern, 0, &zPatte
35a0: 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 rn);. if( c
35b0: 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20 20 20 2=='^' ){.
35c0: 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a 20 20 invert = 1;.
35d0: 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 c2 = sqlit
35e0: 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 e3Utf8Read(zPatt
35f0: 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 72 ern, 0, &zPatter
3600: 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 n);. }.
3610: 20 20 69 66 28 20 63 32 3d 3d 27 5d 27 20 29 7b if( c2==']' ){
3620: 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d . if( c==
3630: 27 5d 27 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a ']' ) seen = 1;.
3640: 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c c2 = sql
3650: 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 ite3Utf8Read(zPa
3660: 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 ttern, 0, &zPatt
3670: 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ern);. }.
3680: 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 26 26 while( c2 &&
3690: 20 63 32 21 3d 27 5d 27 20 29 7b 0a 20 20 20 20 c2!=']' ){.
36a0: 20 20 20 20 69 66 28 20 63 32 3d 3d 27 2d 27 20 if( c2=='-'
36b0: 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d && zPattern[0]!=
36c0: 27 5d 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b ']' && zPattern[
36d0: 30 5d 21 3d 30 20 26 26 20 70 72 69 6f 72 5f 63 0]!=0 && prior_c
36e0: 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 >0 ){.
36f0: 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 c2 = sqlite3Utf8
3700: 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 20 30 Read(zPattern, 0
3710: 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 , &zPattern);.
3720: 20 20 20 20 20 20 20 20 69 66 28 20 63 3e 3d 70 if( c>=p
3730: 72 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63 32 20 rior_c && c<=c2
3740: 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 ) seen = 1;.
3750: 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 prior_c =
3760: 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0;. }else
3770: 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
3780: 63 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20 20 20 c==c2 ){.
3790: 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b 0a 20 seen = 1;.
37a0: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
37b0: 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d 20 63 prior_c = c
37c0: 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 2;. }.
37d0: 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 c2 = sqlite
37e0: 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 3Utf8Read(zPatte
37f0: 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65 72 6e rn, 0, &zPattern
3800: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
3810: 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20 28 73 if( c2==0 || (s
3820: 65 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d 3d 30 een ^ invert)==0
3830: 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
3840: 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn 0;. }.
3850: 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73 63 3d }else if( esc=
3860: 3d 63 20 26 26 20 21 70 72 65 76 45 73 63 61 70 =c && !prevEscap
3870: 65 20 29 7b 0a 20 20 20 20 20 20 70 72 65 76 45 e ){. prevE
3880: 73 63 61 70 65 20 3d 20 31 3b 0a 20 20 20 20 7d scape = 1;. }
3890: 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 32 20 3d else{. c2 =
38a0: 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
38b0: 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 (zString, 0, &zS
38c0: 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 tring);. if
38d0: 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20 ( noCase ){.
38e0: 20 20 20 20 63 20 3d 20 63 3c 30 78 38 30 20 3f c = c<0x80 ?
38f0: 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c sqlite3UpperToL
3900: 6f 77 65 72 5b 63 5d 20 3a 20 63 3b 0a 20 20 20 ower[c] : c;.
3910: 20 20 20 20 20 63 32 20 3d 20 63 32 3c 30 78 38 c2 = c2<0x8
3920: 30 20 3f 20 73 71 6c 69 74 65 33 55 70 70 65 72 0 ? sqlite3Upper
3930: 54 6f 4c 6f 77 65 72 5b 63 32 5d 20 3a 20 63 32 ToLower[c2] : c2
3940: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
3950: 69 66 28 20 63 21 3d 63 32 20 29 7b 0a 20 20 20 if( c!=c2 ){.
3960: 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
3970: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 72 65 }. pre
3980: 76 45 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 20 vEscape = 0;.
3990: 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
39a0: 2a 7a 53 74 72 69 6e 67 3d 3d 30 3b 0a 7d 0a 0a *zString==0;.}..
39b0: 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 /*.** Count the
39c0: 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
39d0: 74 68 61 74 20 74 68 65 20 4c 49 4b 45 20 6f 70 that the LIKE op
39e0: 65 72 61 74 6f 72 20 28 6f 72 20 47 4c 4f 42 20 erator (or GLOB
39f0: 77 68 69 63 68 20 69 73 0a 2a 2a 20 6a 75 73 74 which is.** just
3a00: 20 61 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20 a variation of
3a10: 4c 49 4b 45 29 20 67 65 74 73 20 63 61 6c 6c 65 LIKE) gets calle
3a20: 64 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 d. This is used
3a30: 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 for testing.**
3a40: 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 only..*/.#ifdef
3a50: 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20 SQLITE_TEST.int
3a60: 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 sqlite3_like_cou
3a70: 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
3a80: 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
3a90: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b ation of the lik
3aa0: 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e e() SQL function
3ab0: 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e . This function
3ac0: 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 implements.** t
3ad0: 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45 he build-in LIKE
3ae0: 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 operator. The
3af0: 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
3b00: 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 o the function i
3b10: 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e s the.** pattern
3b20: 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 and the second
3b30: 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
3b40: 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65 string. So, the
3b50: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a SQL statements:
3b60: 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c .**.** A L
3b70: 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69 IKE B.**.** is i
3b80: 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69 mplemented as li
3b90: 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a 20 54 ke(B,A)..**.** T
3ba0: 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74 69 6f his same functio
3bb0: 6e 20 28 77 69 74 68 20 61 20 64 69 66 66 65 72 n (with a differ
3bc0: 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 ent compareInfo
3bd0: 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d 70 75 structure) compu
3be0: 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f 42 20 tes.** the GLOB
3bf0: 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 operator..*/.sta
3c00: 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46 75 6e tic void likeFun
3c10: 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e c(. sqlite3_con
3c20: 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a text *context, .
3c30: 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 int argc, . s
3c40: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
3c50: 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 rgv.){. const u
3c60: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 nsigned char *zA
3c70: 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65 73 63 , *zB;. int esc
3c80: 61 70 65 20 3d 20 30 3b 0a 0a 20 20 7a 42 20 3d ape = 0;.. zB =
3c90: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
3ca0: 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 ext(argv[0]);.
3cb0: 7a 41 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c zA = sqlite3_val
3cc0: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 ue_text(argv[1])
3cd0: 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68 ;.. /* Limit th
3ce0: 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 e length of the
3cf0: 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 LIKE or GLOB pat
3d00: 74 65 72 6e 20 74 6f 20 61 76 6f 69 64 20 70 72 tern to avoid pr
3d10: 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20 64 oblems. ** of d
3d20: 65 65 70 20 72 65 63 75 72 73 69 6f 6e 20 61 6e eep recursion an
3d30: 64 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20 69 d N*N behavior i
3d40: 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 n patternCompare
3d50: 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 ().. */. if( s
3d60: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
3d70: 65 73 28 61 72 67 76 5b 30 5d 29 3e 53 51 4c 49 es(argv[0])>SQLI
3d80: 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 TE_MAX_LIKE_PATT
3d90: 45 52 4e 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20 ERN_LENGTH ){.
3da0: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
3db0: 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 _error(context,
3dc0: 22 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 "LIKE or GLOB pa
3dd0: 74 74 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 ttern too comple
3de0: 78 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 x", -1);. ret
3df0: 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 urn;. }. asser
3e00: 74 28 20 7a 42 3d 3d 73 71 6c 69 74 65 33 5f 76 t( zB==sqlite3_v
3e10: 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
3e20: 5d 29 20 29 3b 20 20 2f 2a 20 45 6e 63 6f 64 69 ]) ); /* Encodi
3e30: 6e 67 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67 ng did not chang
3e40: 65 20 2a 2f 0a 0a 20 20 69 66 28 20 61 72 67 63 e */.. if( argc
3e50: 3d 3d 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 ==3 ){. /* Th
3e60: 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74 e escape charact
3e70: 65 72 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 er string must c
3e80: 6f 6e 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 onsist of a sing
3e90: 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 le UTF-8 charact
3ea0: 65 72 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 er.. ** Other
3eb0: 77 69 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 wise, return an
3ec0: 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 error.. */.
3ed0: 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
3ee0: 20 63 68 61 72 20 2a 7a 45 73 63 20 3d 20 73 71 char *zEsc = sq
3ef0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
3f00: 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 (argv[2]);. i
3f10: 66 28 20 7a 45 73 63 3d 3d 30 20 29 20 72 65 74 f( zEsc==0 ) ret
3f20: 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 73 71 6c urn;. if( sql
3f30: 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 ite3Utf8CharLen(
3f40: 28 63 68 61 72 2a 29 7a 45 73 63 2c 20 2d 31 29 (char*)zEsc, -1)
3f50: 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c !=1 ){. sql
3f60: 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
3f70: 72 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 r(context, .
3f80: 20 20 20 20 20 20 22 45 53 43 41 50 45 20 65 78 "ESCAPE ex
3f90: 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 pression must be
3fa0: 20 61 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 a single charac
3fb0: 74 65 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 ter", -1);.
3fc0: 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
3fd0: 20 20 20 65 73 63 61 70 65 20 3d 20 73 71 6c 69 escape = sqli
3fe0: 74 65 33 55 74 66 38 52 65 61 64 28 7a 45 73 63 te3Utf8Read(zEsc
3ff0: 2c 20 30 2c 20 26 7a 45 73 63 29 3b 0a 20 20 7d , 0, &zEsc);. }
4000: 0a 20 20 69 66 28 20 7a 41 20 26 26 20 7a 42 20 . if( zA && zB
4010: 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 63 6f ){. struct co
4020: 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f mpareInfo *pInfo
4030: 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f = sqlite3_user_
4040: 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 23 data(context);.#
4050: 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
4060: 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 69 T. sqlite3_li
4070: 6b 65 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 ke_count++;.#end
4080: 69 66 0a 20 20 20 20 0a 20 20 20 20 73 71 6c 69 if. . sqli
4090: 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 te3_result_int(c
40a0: 6f 6e 74 65 78 74 2c 20 70 61 74 74 65 72 6e 43 ontext, patternC
40b0: 6f 6d 70 61 72 65 28 7a 42 2c 20 7a 41 2c 20 70 ompare(zB, zA, p
40c0: 49 6e 66 6f 2c 20 65 73 63 61 70 65 29 29 3b 0a Info, escape));.
40d0: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 }.}../*.** Imp
40e0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
40f0: 68 65 20 4e 55 4c 4c 49 46 28 78 2c 79 29 20 66 he NULLIF(x,y) f
4100: 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 unction. The re
4110: 73 75 6c 74 20 69 73 20 74 68 65 20 66 69 72 73 sult is the firs
4120: 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 66 t.** argument if
4130: 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 20 61 the arguments a
4140: 72 65 20 64 69 66 66 65 72 65 6e 74 2e 20 20 54 re different. T
4150: 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c he result is NUL
4160: 4c 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 L if the.** argu
4170: 6d 65 6e 74 73 20 61 72 65 20 65 71 75 61 6c 20 ments are equal
4180: 74 6f 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a to each other..*
4190: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 75 /.static void nu
41a0: 6c 6c 69 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 llifFunc(. sqli
41b0: 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
41c0: 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
41d0: 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
41e0: 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 43 6f e **argv.){. Co
41f0: 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 llSeq *pColl = s
4200: 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c qlite3GetFuncCol
4210: 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 lSeq(context);.
4220: 20 69 66 28 20 73 71 6c 69 74 65 33 4d 65 6d 43 if( sqlite3MemC
4230: 6f 6d 70 61 72 65 28 61 72 67 76 5b 30 5d 2c 20 ompare(argv[0],
4240: 61 72 67 76 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21 argv[1], pColl)!
4250: 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
4260: 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 3_result_value(c
4270: 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 ontext, argv[0])
4280: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
4290: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
42a0: 20 74 68 65 20 56 45 52 53 49 4f 4e 28 2a 29 20 the VERSION(*)
42b0: 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 function. The r
42c0: 65 73 75 6c 74 20 69 73 20 74 68 65 20 76 65 72 esult is the ver
42d0: 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 sion.** of the S
42e0: 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 74 68 QLite library th
42f0: 61 74 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a at is running..*
4300: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 65 /.static void ve
4310: 72 73 69 6f 6e 46 75 6e 63 28 0a 20 20 73 71 6c rsionFunc(. sql
4320: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
4330: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
4340: 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
4350: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 ue **argv.){. s
4360: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
4370: 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 xt(context, sqli
4380: 74 65 33 5f 76 65 72 73 69 6f 6e 2c 20 2d 31 2c te3_version, -1,
4390: 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b SQLITE_STATIC);
43a0: 0a 7d 0a 0a 2f 2a 20 41 72 72 61 79 20 66 6f 72 .}../* Array for
43b0: 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d converting from
43c0: 20 68 61 6c 66 2d 62 79 74 65 73 20 28 6e 79 62 half-bytes (nyb
43d0: 62 6c 65 73 29 20 69 6e 74 6f 20 41 53 43 49 49 bles) into ASCII
43e0: 20 68 65 78 0a 2a 2a 20 64 69 67 69 74 73 2e 20 hex.** digits.
43f0: 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
4400: 63 68 61 72 20 68 65 78 64 69 67 69 74 73 5b 5d char hexdigits[]
4410: 20 3d 20 7b 0a 20 20 27 30 27 2c 20 27 31 27 2c = {. '0', '1',
4420: 20 27 32 27 2c 20 27 33 27 2c 20 27 34 27 2c 20 '2', '3', '4',
4430: 27 35 27 2c 20 27 36 27 2c 20 27 37 27 2c 0a 20 '5', '6', '7',.
4440: 20 27 38 27 2c 20 27 39 27 2c 20 27 41 27 2c 20 '8', '9', 'A',
4450: 27 42 27 2c 20 27 43 27 2c 20 27 44 27 2c 20 27 'B', 'C', 'D', '
4460: 45 27 2c 20 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a E', 'F' .};../*.
4470: 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 ** EXPERIMENTAL
4480: 2d 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61 6e - This is not an
4490: 20 6f 66 66 69 63 69 61 6c 20 66 75 6e 63 74 69 official functi
44a0: 6f 6e 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 on. The interfa
44b0: 63 65 20 6d 61 79 0a 2a 2a 20 63 68 61 6e 67 65 ce may.** change
44c0: 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e . This function
44d0: 20 6d 61 79 20 64 69 73 61 70 70 65 61 72 2e 20 may disappear.
44e0: 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 63 6f Do not write co
44f0: 64 65 20 74 68 61 74 20 64 65 70 65 6e 64 73 0a de that depends.
4500: 2a 2a 20 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 ** on this funct
4510: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 ion..**.** Imple
4520: 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
4530: 20 51 55 4f 54 45 28 29 20 66 75 6e 63 74 69 6f QUOTE() functio
4540: 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f n. This functio
4550: 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 n takes a single
4560: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 .** argument. I
4570: 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 f the argument i
4580: 73 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 20 72 s numeric, the r
4590: 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 eturn value is t
45a0: 68 65 20 73 61 6d 65 20 61 73 0a 2a 2a 20 74 68 he same as.** th
45b0: 65 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 e argument. If
45c0: 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 the argument is
45d0: 4e 55 4c 4c 2c 20 74 68 65 20 72 65 74 75 72 6e NULL, the return
45e0: 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73 74 value is the st
45f0: 72 69 6e 67 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 ring.** "NULL".
4600: 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 Otherwise, the
4610: 61 72 67 75 6d 65 6e 74 20 69 73 20 65 6e 63 6c argument is encl
4620: 6f 73 65 64 20 69 6e 20 73 69 6e 67 6c 65 20 71 osed in single q
4630: 75 6f 74 65 73 20 77 69 74 68 0a 2a 2a 20 73 69 uotes with.** si
4640: 6e 67 6c 65 2d 71 75 6f 74 65 20 65 73 63 61 70 ngle-quote escap
4650: 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f es..*/.static vo
4660: 69 64 20 71 75 6f 74 65 46 75 6e 63 28 73 71 6c id quoteFunc(sql
4670: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
4680: 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c ntext, int argc,
4690: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
46a0: 2a 61 72 67 76 29 7b 0a 20 20 69 66 28 20 61 72 *argv){. if( ar
46b0: 67 63 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 gc<1 ) return;.
46c0: 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 switch( sqlite3
46d0: 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 _value_type(argv
46e0: 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 [0]) ){. case
46f0: 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0a SQLITE_NULL: {.
4700: 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 sqlite3_re
4710: 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 sult_text(contex
4720: 74 2c 20 22 4e 55 4c 4c 22 2c 20 34 2c 20 53 51 t, "NULL", 4, SQ
4730: 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 LITE_STATIC);.
4740: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
4750: 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
4760: 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61 _INTEGER:. ca
4770: 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a se SQLITE_FLOAT:
4780: 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 {. sqlite3
4790: 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f _result_value(co
47a0: 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b ntext, argv[0]);
47b0: 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
47c0: 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
47d0: 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 ITE_BLOB: {.
47e0: 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 char *zText =
47f0: 30 3b 0a 20 20 20 20 20 20 63 68 61 72 20 63 6f 0;. char co
4800: 6e 73 74 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c nst *zBlob = sql
4810: 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 ite3_value_blob(
4820: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 argv[0]);.
4830: 69 6e 74 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 int nBlob = sqli
4840: 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
4850: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 argv[0]);.
4860: 61 73 73 65 72 74 28 20 7a 42 6c 6f 62 3d 3d 73 assert( zBlob==s
4870: 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f qlite3_value_blo
4880: 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 2f 2a b(argv[0]) ); /*
4890: 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 No encoding cha
48a0: 6e 67 65 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 nge */.. if
48b0: 28 20 32 2a 6e 42 6c 6f 62 2b 34 3e 53 51 4c 49 ( 2*nBlob+4>SQLI
48c0: 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 7b TE_MAX_LENGTH ){
48d0: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
48e0: 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f _result_error_to
48f0: 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 obig(context);.
4900: 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 return;.
4910: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 }. zTe
4920: 78 74 20 3d 20 28 63 68 61 72 20 2a 29 63 6f 6e xt = (char *)con
4930: 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 textMalloc(conte
4940: 78 74 2c 20 28 32 2a 6e 42 6c 6f 62 29 2b 34 29 xt, (2*nBlob)+4)
4950: 3b 20 0a 20 20 20 20 20 20 69 66 28 20 7a 54 65 ; . if( zTe
4960: 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e xt ){. in
4970: 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 t i;. for
4980: 28 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62 3b 20 69 (i=0; i<nBlob; i
4990: 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a ++){. z
49a0: 54 65 78 74 5b 28 69 2a 32 29 2b 32 5d 20 3d 20 Text[(i*2)+2] =
49b0: 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62 hexdigits[(zBlob
49c0: 5b 69 5d 3e 3e 34 29 26 30 78 30 46 5d 3b 0a 20 [i]>>4)&0x0F];.
49d0: 20 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 zText[(
49e0: 69 2a 32 29 2b 33 5d 20 3d 20 68 65 78 64 69 67 i*2)+3] = hexdig
49f0: 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 29 26 30 its[(zBlob[i])&0
4a00: 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a x0F];. }.
4a10: 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 6e zText[(n
4a20: 42 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27 5c 27 Blob*2)+2] = '\'
4a30: 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 ';. zText
4a40: 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20 3d 20 [(nBlob*2)+3] =
4a50: 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 '\0';. zT
4a60: 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0a 20 20 ext[0] = 'X';.
4a70: 20 20 20 20 20 20 7a 54 65 78 74 5b 31 5d 20 3d zText[1] =
4a80: 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 73 '\'';. s
4a90: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
4aa0: 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 54 65 78 xt(context, zTex
4ab0: 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 t, -1, SQLITE_TR
4ac0: 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 ANSIENT);.
4ad0: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
4ae0: 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 Text);. }.
4af0: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
4b00: 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
4b10: 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 E_TEXT: {.
4b20: 69 6e 74 20 69 2c 6a 3b 0a 20 20 20 20 20 20 75 int i,j;. u
4b30: 36 34 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 64 n;. cons
4b40: 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
4b50: 2a 7a 41 72 67 20 3d 20 73 71 6c 69 74 65 33 5f *zArg = sqlite3_
4b60: 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b value_text(argv[
4b70: 30 5d 29 3b 0a 20 20 20 20 20 20 63 68 61 72 20 0]);. char
4b80: 2a 7a 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 7a *z;.. if( z
4b90: 41 72 67 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b Arg==0 ) return;
4ba0: 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 . for(i=0,
4bb0: 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b n=0; zArg[i]; i+
4bc0: 2b 29 7b 20 69 66 28 20 7a 41 72 67 5b 69 5d 3d +){ if( zArg[i]=
4bd0: 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d 0a 20 ='\'' ) n++; }.
4be0: 20 20 20 20 20 69 66 28 20 69 2b 6e 2b 33 3e 53 if( i+n+3>S
4bf0: 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 QLITE_MAX_LENGTH
4c00: 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
4c10: 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
4c20: 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 _toobig(context)
4c30: 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
4c40: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
4c50: 7a 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f z = contextMallo
4c60: 63 28 63 6f 6e 74 65 78 74 2c 20 69 2b 6e 2b 33 c(context, i+n+3
4c70: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 );. if( z )
4c80: 7b 0a 20 20 20 20 20 20 20 20 7a 5b 30 5d 20 3d {. z[0] =
4c90: 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 66 '\'';. f
4ca0: 6f 72 28 69 3d 30 2c 20 6a 3d 31 3b 20 7a 41 72 or(i=0, j=1; zAr
4cb0: 67 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 g[i]; i++){.
4cc0: 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a z[j++] = z
4cd0: 41 72 67 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 Arg[i];.
4ce0: 20 20 69 66 28 20 7a 41 72 67 5b 69 5d 3d 3d 27 if( zArg[i]=='
4cf0: 5c 27 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 \'' ){.
4d00: 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27 z[j++] = '\''
4d10: 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
4d20: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
4d30: 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 z[j++] = '\'';.
4d40: 20 20 20 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b z[j] = 0;
4d50: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
4d60: 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
4d70: 74 65 78 74 2c 20 7a 2c 20 6a 2c 20 73 71 6c 69 text, z, j, sqli
4d80: 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 20 te3_free);.
4d90: 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a }. }. }.}..
4da0: 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 78 28 29 20 /*.** The hex()
4db0: 66 75 6e 63 74 69 6f 6e 2e 20 20 49 6e 74 65 72 function. Inter
4dc0: 70 72 65 74 20 74 68 65 20 61 72 67 75 6d 65 6e pret the argumen
4dd0: 74 20 61 73 20 61 20 62 6c 6f 62 2e 20 20 52 65 t as a blob. Re
4de0: 74 75 72 6e 0a 2a 2a 20 61 20 68 65 78 61 64 65 turn.** a hexade
4df0: 63 69 6d 61 6c 20 72 65 6e 64 65 72 69 6e 67 20 cimal rendering
4e00: 61 73 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 as text..*/.stat
4e10: 69 63 20 76 6f 69 64 20 68 65 78 46 75 6e 63 28 ic void hexFunc(
4e20: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
4e30: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
4e40: 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
4e50: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
4e60: 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 ){. int i, n;.
4e70: 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
4e80: 63 68 61 72 20 2a 70 42 6c 6f 62 3b 0a 20 20 63 char *pBlob;. c
4e90: 68 61 72 20 2a 7a 48 65 78 2c 20 2a 7a 3b 0a 20 har *zHex, *z;.
4ea0: 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 assert( argc==1
4eb0: 20 29 3b 0a 20 20 70 42 6c 6f 62 20 3d 20 73 71 );. pBlob = sq
4ec0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 lite3_value_blob
4ed0: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d (argv[0]);. n =
4ee0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
4ef0: 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 ytes(argv[0]);.
4f00: 20 69 66 28 20 6e 2a 32 2b 31 3e 53 51 4c 49 54 if( n*2+1>SQLIT
4f10: 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 7b 0a E_MAX_LENGTH ){.
4f20: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
4f30: 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 lt_error_toobig(
4f40: 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 context);. re
4f50: 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 turn;. }. asse
4f60: 72 74 28 20 70 42 6c 6f 62 3d 3d 73 71 6c 69 74 rt( pBlob==sqlit
4f70: 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 e3_value_blob(ar
4f80: 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f gv[0]) ); /* No
4f90: 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 encoding change
4fa0: 20 2a 2f 0a 20 20 7a 20 3d 20 7a 48 65 78 20 3d */. z = zHex =
4fb0: 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 contextMalloc(c
4fc0: 6f 6e 74 65 78 74 2c 20 6e 2a 32 20 2b 20 31 29 ontext, n*2 + 1)
4fd0: 3b 0a 20 20 69 66 28 20 7a 48 65 78 20 29 7b 0a ;. if( zHex ){.
4fe0: 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e for(i=0; i<n
4ff0: 3b 20 69 2b 2b 2c 20 70 42 6c 6f 62 2b 2b 29 7b ; i++, pBlob++){
5000: 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
5010: 63 68 61 72 20 63 20 3d 20 2a 70 42 6c 6f 62 3b char c = *pBlob;
5020: 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 . *(z++) =
5030: 68 65 78 64 69 67 69 74 73 5b 28 63 3e 3e 34 29 hexdigits[(c>>4)
5040: 26 30 78 66 5d 3b 0a 20 20 20 20 20 20 2a 28 7a &0xf];. *(z
5050: 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b ++) = hexdigits[
5060: 63 26 30 78 66 5d 3b 0a 20 20 20 20 7d 0a 20 20 c&0xf];. }.
5070: 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 20 20 73 71 *z = 0;. sq
5080: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
5090: 74 28 63 6f 6e 74 65 78 74 2c 20 7a 48 65 78 2c t(context, zHex,
50a0: 20 6e 2a 32 2c 20 73 71 6c 69 74 65 33 5f 66 72 n*2, sqlite3_fr
50b0: 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ee);. }.}../*.*
50c0: 2a 20 54 68 65 20 7a 65 72 6f 62 6c 6f 62 28 4e * The zeroblob(N
50d0: 29 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 ) function retur
50e0: 6e 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 ns a zero-filled
50f0: 20 62 6c 6f 62 20 6f 66 20 73 69 7a 65 20 4e 20 blob of size N
5100: 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 bytes..*/.static
5110: 20 76 6f 69 64 20 7a 65 72 6f 62 6c 6f 62 46 75 void zeroblobFu
5120: 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
5130: 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
5140: 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 int argc,. sq
5150: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
5160: 67 76 0a 29 7b 0a 20 20 69 36 34 20 6e 3b 0a 20 gv.){. i64 n;.
5170: 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 assert( argc==1
5180: 20 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 );. n = sqlite
5190: 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 3_value_int64(ar
51a0: 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3e gv[0]);. if( n>
51b0: 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 SQLITE_MAX_LENGT
51c0: 48 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 H ){. sqlite3
51d0: 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f _result_error_to
51e0: 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 obig(context);.
51f0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
5200: 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 te3_result_zerob
5210: 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 6e 29 3b lob(context, n);
5220: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
5230: 65 20 72 65 70 6c 61 63 65 28 29 20 66 75 6e 63 e replace() func
5240: 74 69 6f 6e 2e 20 20 54 68 72 65 65 20 61 72 67 tion. Three arg
5250: 75 6d 65 6e 74 73 20 61 72 65 20 61 6c 6c 20 73 uments are all s
5260: 74 72 69 6e 67 73 3a 20 63 61 6c 6c 0a 2a 2a 20 trings: call.**
5270: 74 68 65 6d 20 41 2c 20 42 2c 20 61 6e 64 20 43 them A, B, and C
5280: 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 . The result is
5290: 61 6c 73 6f 20 61 20 73 74 72 69 6e 67 20 77 68 also a string wh
52a0: 69 63 68 20 69 73 20 64 65 72 69 76 65 64 0a 2a ich is derived.*
52b0: 2a 20 66 72 6f 6d 20 41 20 62 79 20 72 65 70 6c * from A by repl
52c0: 61 63 69 6e 67 20 65 76 65 72 79 20 6f 63 63 75 acing every occu
52d0: 72 61 6e 63 65 20 6f 66 20 42 20 77 69 74 68 20 rance of B with
52e0: 43 2e 20 20 54 68 65 20 6d 61 74 63 68 0a 2a 2a C. The match.**
52f0: 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 2e 20 must be exact.
5300: 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 Collating seque
5310: 6e 63 65 73 20 61 72 65 20 6e 6f 74 20 75 73 65 nces are not use
5320: 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
5330: 64 20 72 65 70 6c 61 63 65 46 75 6e 63 28 0a 20 d replaceFunc(.
5340: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
5350: 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
5360: 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 argc,. sqlite3
5370: 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
5380: 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 . const unsigne
5390: 64 20 63 68 61 72 20 2a 7a 53 74 72 3b 20 20 20 d char *zStr;
53a0: 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 /* The inpu
53b0: 74 20 73 74 72 69 6e 67 20 41 20 2a 2f 0a 20 20 t string A */.
53c0: 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
53d0: 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 3b 20 20 har *zPattern;
53e0: 20 20 2f 2a 20 54 68 65 20 70 61 74 74 65 72 6e /* The pattern
53f0: 20 73 74 72 69 6e 67 20 42 20 2a 2f 0a 20 20 63 string B */. c
5400: 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
5410: 61 72 20 2a 7a 52 65 70 3b 20 20 20 20 20 20 20 ar *zRep;
5420: 20 2f 2a 20 54 68 65 20 72 65 70 6c 61 63 65 6d /* The replacem
5430: 65 6e 74 20 73 74 72 69 6e 67 20 43 20 2a 2f 0a ent string C */.
5440: 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
5450: 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 *zOut;
5460: 20 20 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 /* The outpu
5470: 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74 72 3b t */. int nStr;
5480: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5490: 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 53 74 72 20 /* Size of zStr
54a0: 2a 2f 0a 20 20 69 6e 74 20 6e 50 61 74 74 65 72 */. int nPatter
54b0: 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a n; /*
54c0: 20 53 69 7a 65 20 6f 66 20 7a 50 61 74 74 65 72 Size of zPatter
54d0: 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 70 3b n */. int nRep;
54e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54f0: 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 52 65 70 20 /* Size of zRep
5500: 2a 2f 0a 20 20 69 36 34 20 6e 4f 75 74 3b 20 20 */. i64 nOut;
5510: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5520: 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 Maximum size of
5530: 20 7a 4f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6c zOut */. int l
5540: 6f 6f 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 oopLimit;
5550: 20 20 20 20 2f 2a 20 4c 61 73 74 20 7a 53 74 72 /* Last zStr
5560: 5b 5d 20 74 68 61 74 20 6d 69 67 68 74 20 6d 61 [] that might ma
5570: 74 63 68 20 7a 50 61 74 74 65 72 6e 5b 5d 20 2a tch zPattern[] *
5580: 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 /. int i, j;
5590: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
55a0: 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f Loop counters */
55b0: 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 .. assert( argc
55c0: 3d 3d 33 20 29 3b 0a 20 20 7a 53 74 72 20 3d 20 ==3 );. zStr =
55d0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
55e0: 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 xt(argv[0]);. i
55f0: 66 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65 74 f( zStr==0 ) ret
5600: 75 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73 71 urn;. nStr = sq
5610: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
5620: 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 s(argv[0]);. as
5630: 73 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c 69 sert( zStr==sqli
5640: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
5650: 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e rgv[0]) ); /* N
5660: 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 o encoding chang
5670: 65 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e 20 e */. zPattern
5680: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
5690: 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 text(argv[1]);.
56a0: 20 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d 30 if( zPattern==0
56b0: 20 7c 7c 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d || zPattern[0]=
56c0: 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e =0 ) return;. n
56d0: 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 Pattern = sqlite
56e0: 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 3_value_bytes(ar
56f0: 67 76 5b 31 5d 29 3b 0a 20 20 61 73 73 65 72 74 gv[1]);. assert
5700: 28 20 7a 50 61 74 74 65 72 6e 3d 3d 73 71 6c 69 ( zPattern==sqli
5710: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
5720: 72 67 76 5b 31 5d 29 20 29 3b 20 20 2f 2a 20 4e rgv[1]) ); /* N
5730: 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 o encoding chang
5740: 65 20 2a 2f 0a 20 20 7a 52 65 70 20 3d 20 73 71 e */. zRep = sq
5750: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
5760: 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69 66 28 (argv[2]);. if(
5770: 20 7a 52 65 70 3d 3d 30 20 29 20 72 65 74 75 72 zRep==0 ) retur
5780: 6e 3b 0a 20 20 6e 52 65 70 20 3d 20 73 71 6c 69 n;. nRep = sqli
5790: 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
57a0: 61 72 67 76 5b 32 5d 29 3b 0a 20 20 61 73 73 65 argv[2]);. asse
57b0: 72 74 28 20 7a 52 65 70 3d 3d 73 71 6c 69 74 65 rt( zRep==sqlite
57c0: 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
57d0: 76 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f 75 74 20 v[2]) );. nOut
57e0: 3d 20 6e 53 74 72 20 2b 20 31 3b 0a 20 20 61 73 = nStr + 1;. as
57f0: 73 65 72 74 28 20 6e 4f 75 74 3c 53 51 4c 49 54 sert( nOut<SQLIT
5800: 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a E_MAX_LENGTH );.
5810: 20 20 7a 4f 75 74 20 3d 20 63 6f 6e 74 65 78 74 zOut = context
5820: 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 Malloc(context,
5830: 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20 69 66 (int)nOut);. if
5840: 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 ( zOut==0 ){.
5850: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6c return;. }. l
5860: 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74 72 20 oopLimit = nStr
5870: 2d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0a 20 20 - nPattern; .
5880: 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f for(i=j=0; i<=lo
5890: 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20 opLimit; i++){.
58a0: 20 20 20 69 66 28 20 7a 53 74 72 5b 69 5d 21 3d if( zStr[i]!=
58b0: 7a 50 61 74 74 65 72 6e 5b 30 5d 20 7c 7c 20 6d zPattern[0] || m
58c0: 65 6d 63 6d 70 28 26 7a 53 74 72 5b 69 5d 2c 20 emcmp(&zStr[i],
58d0: 7a 50 61 74 74 65 72 6e 2c 20 6e 50 61 74 74 65 zPattern, nPatte
58e0: 72 6e 29 20 29 7b 0a 20 20 20 20 20 20 7a 4f 75 rn) ){. zOu
58f0: 74 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b 69 5d t[j++] = zStr[i]
5900: 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
5910: 20 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20 20 20 u8 *zOld;.
5920: 20 20 20 6e 4f 75 74 20 2b 3d 20 6e 52 65 70 20 nOut += nRep
5930: 2d 20 6e 50 61 74 74 65 72 6e 3b 0a 20 20 20 20 - nPattern;.
5940: 20 20 69 66 28 20 6e 4f 75 74 3e 3d 53 51 4c 49 if( nOut>=SQLI
5950: 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 7b TE_MAX_LENGTH ){
5960: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
5970: 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f _result_error_to
5980: 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 obig(context);.
5990: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 sqlite3_f
59a0: 72 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20 20 ree(zOut);.
59b0: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 return;.
59c0: 20 7d 0a 20 20 20 20 20 20 7a 4f 6c 64 20 3d 20 }. zOld =
59d0: 7a 4f 75 74 3b 0a 20 20 20 20 20 20 7a 4f 75 74 zOut;. zOut
59e0: 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c = sqlite3_reall
59f0: 6f 63 28 7a 4f 75 74 2c 20 28 69 6e 74 29 6e 4f oc(zOut, (int)nO
5a00: 75 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a ut);. if( z
5a10: 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Out==0 ){.
5a20: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
5a30: 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e _error_nomem(con
5a40: 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 73 text);. s
5a50: 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4f 6c 64 qlite3_free(zOld
5a60: 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
5a70: 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 n;. }.
5a80: 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d memcpy(&zOut[j]
5a90: 2c 20 7a 52 65 70 2c 20 6e 52 65 70 29 3b 0a 20 , zRep, nRep);.
5aa0: 20 20 20 20 20 6a 20 2b 3d 20 6e 52 65 70 3b 0a j += nRep;.
5ab0: 20 20 20 20 20 20 69 20 2b 3d 20 6e 50 61 74 74 i += nPatt
5ac0: 65 72 6e 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d ern-1;. }. }
5ad0: 0a 20 20 61 73 73 65 72 74 28 20 6a 2b 6e 53 74 . assert( j+nSt
5ae0: 72 2d 69 2b 31 3d 3d 6e 4f 75 74 20 29 3b 0a 20 r-i+1==nOut );.
5af0: 20 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d memcpy(&zOut[j]
5b00: 2c 20 26 7a 53 74 72 5b 69 5d 2c 20 6e 53 74 72 , &zStr[i], nStr
5b10: 2d 69 29 3b 0a 20 20 6a 20 2b 3d 20 6e 53 74 72 -i);. j += nStr
5b20: 20 2d 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 - i;. assert(
5b30: 6a 3c 3d 6e 4f 75 74 20 29 3b 0a 20 20 7a 4f 75 j<=nOut );. zOu
5b40: 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 t[j] = 0;. sqli
5b50: 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
5b60: 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 context, (char*)
5b70: 7a 4f 75 74 2c 20 6a 2c 20 73 71 6c 69 74 65 33 zOut, j, sqlite3
5b80: 5f 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _free);.}../*.**
5b90: 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
5ba0: 6f 66 20 74 68 65 20 54 52 49 4d 28 29 2c 20 4c of the TRIM(), L
5bb0: 54 52 49 4d 28 29 2c 20 61 6e 64 20 52 54 52 49 TRIM(), and RTRI
5bc0: 4d 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a M() functions..*
5bd0: 2a 20 54 68 65 20 75 73 65 72 64 61 74 61 20 69 * The userdata i
5be0: 73 20 30 78 31 20 66 6f 72 20 6c 65 66 74 20 74 s 0x1 for left t
5bf0: 72 69 6d 2c 20 30 78 32 20 66 6f 72 20 72 69 67 rim, 0x2 for rig
5c00: 68 74 20 74 72 69 6d 2c 20 30 78 33 20 66 6f 72 ht trim, 0x3 for
5c10: 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 both..*/.static
5c20: 20 76 6f 69 64 20 74 72 69 6d 46 75 6e 63 28 0a void trimFunc(.
5c30: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
5c40: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
5c50: 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
5c60: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
5c70: 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e {. const unsign
5c80: 65 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 ed char *zIn;
5c90: 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 /* Input s
5ca0: 74 72 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 tring */. const
5cb0: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
5cc0: 7a 43 68 61 72 53 65 74 3b 20 20 20 20 2f 2a 20 zCharSet; /*
5cd0: 53 65 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 Set of character
5ce0: 73 20 74 6f 20 74 72 69 6d 20 2a 2f 0a 20 20 69 s to trim */. i
5cf0: 6e 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 nt nIn;
5d00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5d10: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
5d20: 74 65 73 20 69 6e 20 69 6e 70 75 74 20 2a 2f 0a tes in input */.
5d30: 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 int flags;
5d40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5d50: 20 20 20 20 2f 2a 20 31 3a 20 74 72 69 6d 6c 65 /* 1: trimle
5d60: 66 74 20 20 32 3a 20 74 72 69 6d 72 69 67 68 74 ft 2: trimright
5d70: 20 20 33 3a 20 74 72 69 6d 20 2a 2f 0a 20 20 69 3: trim */. i
5d80: 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
5d90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5da0: 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
5db0: 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
5dc0: 68 61 72 20 2a 61 4c 65 6e 3b 20 20 20 20 20 20 har *aLen;
5dd0: 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 /* Lengt
5de0: 68 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63 h of each charac
5df0: 74 65 72 20 69 6e 20 7a 43 68 61 72 53 65 74 20 ter in zCharSet
5e00: 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 */. const unsig
5e10: 6e 65 64 20 63 68 61 72 20 2a 2a 61 7a 43 68 61 ned char **azCha
5e20: 72 3b 20 20 20 20 20 2f 2a 20 49 6e 64 69 76 69 r; /* Indivi
5e30: 64 75 61 6c 20 63 68 61 72 61 63 74 65 72 73 20 dual characters
5e40: 69 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 in zCharSet */.
5e50: 20 69 6e 74 20 6e 43 68 61 72 3b 20 20 20 20 20 int nChar;
5e60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e70: 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
5e80: 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 characters in zC
5e90: 68 61 72 53 65 74 20 2a 2f 0a 0a 20 20 69 66 28 harSet */.. if(
5ea0: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
5eb0: 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 ype(argv[0])==SQ
5ec0: 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 LITE_NULL ){.
5ed0: 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a return;. }. z
5ee0: 49 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c In = sqlite3_val
5ef0: 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
5f00: 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 ;. if( zIn==0 )
5f10: 20 72 65 74 75 72 6e 3b 0a 20 20 6e 49 6e 20 3d return;. nIn =
5f20: 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
5f30: 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 ytes(argv[0]);.
5f40: 20 61 73 73 65 72 74 28 20 7a 49 6e 3d 3d 73 71 assert( zIn==sq
5f50: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
5f60: 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 (argv[0]) );. i
5f70: 66 28 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 f( argc==1 ){.
5f80: 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 static const u
5f90: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 65 6e nsigned char len
5fa0: 4f 6e 65 5b 5d 20 3d 20 7b 20 31 20 7d 3b 0a 20 One[] = { 1 };.
5fb0: 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 static const
5fc0: 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 unsigned char *a
5fd0: 7a 4f 6e 65 5b 5d 20 3d 20 7b 20 28 75 38 2a 29 zOne[] = { (u8*)
5fe0: 22 20 22 20 7d 3b 0a 20 20 20 20 6e 43 68 61 72 " " };. nChar
5ff0: 20 3d 20 31 3b 0a 20 20 20 20 61 4c 65 6e 20 3d = 1;. aLen =
6000: 20 28 75 38 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 20 (u8*)lenOne;.
6010: 20 20 61 7a 43 68 61 72 20 3d 20 61 7a 4f 6e 65 azChar = azOne
6020: 3b 0a 20 20 20 20 7a 43 68 61 72 53 65 74 20 3d ;. zCharSet =
6030: 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0;. }else if(
6040: 28 7a 43 68 61 72 53 65 74 20 3d 20 73 71 6c 69 (zCharSet = sqli
6050: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
6060: 72 67 76 5b 31 5d 29 29 3d 3d 30 20 29 7b 0a 20 rgv[1]))==0 ){.
6070: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c return;. }el
6080: 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e se{. const un
6090: 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a signed char *z;.
60a0: 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 for(z=zCharS
60b0: 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b et, nChar=0; *z;
60c0: 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 nChar++){.
60d0: 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 SQLITE_SKIP_UTF
60e0: 38 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 8(z);. }.
60f0: 69 66 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a 20 if( nChar>0 ){.
6100: 20 20 20 20 20 61 7a 43 68 61 72 20 3d 20 63 6f azChar = co
6110: 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 ntextMalloc(cont
6120: 65 78 74 2c 20 6e 43 68 61 72 2a 28 73 69 7a 65 ext, nChar*(size
6130: 6f 66 28 63 68 61 72 2a 29 2b 31 29 29 3b 0a 20 of(char*)+1));.
6140: 20 20 20 20 20 69 66 28 20 61 7a 43 68 61 72 3d if( azChar=
6150: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
6160: 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 turn;. }.
6170: 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 6e 73 69 aLen = (unsi
6180: 67 6e 65 64 20 63 68 61 72 2a 29 26 61 7a 43 68 gned char*)&azCh
6190: 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 20 20 20 20 ar[nChar];.
61a0: 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c for(z=zCharSet,
61b0: 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 nChar=0; *z; nC
61c0: 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 har++){.
61d0: 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 20 3d 20 azChar[nChar] =
61e0: 7a 3b 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 z;. SQLIT
61f0: 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a E_SKIP_UTF8(z);.
6200: 20 20 20 20 20 20 20 20 61 4c 65 6e 5b 6e 43 68 aLen[nCh
6210: 61 72 5d 20 3d 20 7a 20 2d 20 61 7a 43 68 61 72 ar] = z - azChar
6220: 5b 6e 43 68 61 72 5d 3b 0a 20 20 20 20 20 20 7d [nChar];. }
6230: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
6240: 20 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 nChar>0 ){.
6250: 66 6c 61 67 73 20 3d 20 28 69 6e 74 29 73 71 6c flags = (int)sql
6260: 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 ite3_user_data(c
6270: 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 69 66 28 ontext);. if(
6280: 20 66 6c 61 67 73 20 26 20 31 20 29 7b 0a 20 20 flags & 1 ){.
6290: 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30 while( nIn>0
62a0: 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
62b0: 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 len;. for
62c0: 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20 69 (i=0; i<nChar; i
62d0: 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c ++){. l
62e0: 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20 20 en = aLen[i];.
62f0: 20 20 20 20 20 20 20 20 69 66 28 20 6d 65 6d 63 if( memc
6300: 6d 70 28 7a 49 6e 2c 20 61 7a 43 68 61 72 5b 69 mp(zIn, azChar[i
6310: 5d 2c 20 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 ], len)==0 ) bre
6320: 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
6330: 20 20 20 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 if( i>=nCh
6340: 61 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 ar ) break;.
6350: 20 20 20 20 7a 49 6e 20 2b 3d 20 6c 65 6e 3b 0a zIn += len;.
6360: 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d 20 6c nIn -= l
6370: 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 en;. }.
6380: 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 }. if( flags
6390: 26 20 32 20 29 7b 0a 20 20 20 20 20 20 77 68 69 & 2 ){. whi
63a0: 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 le( nIn>0 ){.
63b0: 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 int len;.
63c0: 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
63d0: 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 <nChar; i++){.
63e0: 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c len = aL
63f0: 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 en[i];.
6400: 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20 26 26 if( len<=nIn &&
6410: 20 6d 65 6d 63 6d 70 28 26 7a 49 6e 5b 6e 49 6e memcmp(&zIn[nIn
6420: 2d 6c 65 6e 5d 2c 61 7a 43 68 61 72 5b 69 5d 2c -len],azChar[i],
6430: 6c 65 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b len)==0 ) break;
6440: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
6450: 20 20 20 69 66 28 20 69 3e 3d 6e 43 68 61 72 20 if( i>=nChar
6460: 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ) break;.
6470: 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 nIn -= len;.
6480: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
6490: 66 28 20 7a 43 68 61 72 53 65 74 20 29 7b 0a 20 f( zCharSet ){.
64a0: 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
64b0: 65 28 61 7a 43 68 61 72 29 3b 0a 20 20 20 20 7d e(azChar);. }
64c0: 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 . }. sqlite3_r
64d0: 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
64e0: 78 74 2c 20 28 63 68 61 72 2a 29 7a 49 6e 2c 20 xt, (char*)zIn,
64f0: 6e 49 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e nIn, SQLITE_TRAN
6500: 53 49 45 4e 54 29 3b 0a 7d 0a 0a 23 69 66 64 65 SIENT);.}..#ifde
6510: 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 f SQLITE_SOUNDEX
6520: 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 ./*.** Compute t
6530: 68 65 20 73 6f 75 6e 64 65 78 20 65 6e 63 6f 64 he soundex encod
6540: 69 6e 67 20 6f 66 20 61 20 77 6f 72 64 2e 0a 2a ing of a word..*
6550: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6f /.static void so
6560: 75 6e 64 65 78 46 75 6e 63 28 0a 20 20 73 71 6c undexFunc(. sql
6570: 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f ite3_context *co
6580: 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 ntext,. int arg
6590: 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c c,. sqlite3_val
65a0: 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 ue **argv.){. c
65b0: 68 61 72 20 7a 52 65 73 75 6c 74 5b 38 5d 3b 0a har zResult[8];.
65c0: 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 3b const u8 *zIn;
65d0: 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 . int i, j;. s
65e0: 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 tatic const unsi
65f0: 67 6e 65 64 20 63 68 61 72 20 69 43 6f 64 65 5b gned char iCode[
6600: 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 30 2c 20 ] = {. 0, 0,
6610: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
6620: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
6630: 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 0, 0, 0,. 0,
6640: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
6650: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
6660: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 , 0, 0, 0, 0,.
6670: 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0, 0, 0, 0, 0,
6680: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
6690: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 0, 0, 0, 0, 0, 0
66a0: 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 ,. 0, 0, 0, 0
66b0: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c , 0, 0, 0, 0, 0,
66c0: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 0, 0, 0, 0,
66d0: 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 0, 0,. 0, 0,
66e0: 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 32 1, 2, 3, 0, 1, 2
66f0: 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2c , 0, 0, 2, 2, 4,
6700: 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20 31 2c 5, 5, 0,. 1,
6710: 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c 20 2, 6, 2, 3, 0,
6720: 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20 30 1, 0, 2, 0, 2, 0
6730: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 , 0, 0, 0, 0,.
6740: 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 33 2c 0, 0, 1, 2, 3,
6750: 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30 2c 20 0, 1, 2, 0, 0,
6760: 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2, 2, 4, 5, 5, 0
6770: 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c 20 32 ,. 1, 2, 6, 2
6780: 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2c , 3, 0, 1, 0, 2,
6790: 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2c 20 0, 2, 0, 0, 0,
67a0: 30 2c 20 30 2c 0a 20 20 7d 3b 0a 20 20 61 73 73 0, 0,. };. ass
67b0: 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a ert( argc==1 );.
67c0: 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 73 71 6c zIn = (u8*)sql
67d0: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
67e0: 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 argv[0]);. if(
67f0: 7a 49 6e 3d 3d 30 20 29 20 7a 49 6e 20 3d 20 28 zIn==0 ) zIn = (
6800: 75 38 2a 29 22 22 3b 0a 20 20 66 6f 72 28 69 3d u8*)"";. for(i=
6810: 30 3b 20 7a 49 6e 5b 69 5d 20 26 26 20 21 69 73 0; zIn[i] && !is
6820: 61 6c 70 68 61 28 7a 49 6e 5b 69 5d 29 3b 20 69 alpha(zIn[i]); i
6830: 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 7a 49 6e 5b ++){}. if( zIn[
6840: 69 5d 20 29 7b 0a 20 20 20 20 75 38 20 70 72 65 i] ){. u8 pre
6850: 76 63 6f 64 65 20 3d 20 69 43 6f 64 65 5b 7a 49 vcode = iCode[zI
6860: 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 20 20 20 n[i]&0x7f];.
6870: 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 74 6f 75 zResult[0] = tou
6880: 70 70 65 72 28 7a 49 6e 5b 69 5d 29 3b 0a 20 20 pper(zIn[i]);.
6890: 20 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c 34 20 26 for(j=1; j<4 &
68a0: 26 20 7a 49 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a & zIn[i]; i++){.
68b0: 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d int code =
68c0: 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 iCode[zIn[i]&0x
68d0: 37 66 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 63 7f];. if( c
68e0: 6f 64 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 ode>0 ){.
68f0: 20 69 66 28 20 63 6f 64 65 21 3d 70 72 65 76 63 if( code!=prevc
6900: 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ode ){.
6910: 20 70 72 65 76 63 6f 64 65 20 3d 20 63 6f 64 65 prevcode = code
6920: 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 52 65 73 ;. zRes
6930: 75 6c 74 5b 6a 2b 2b 5d 20 3d 20 63 6f 64 65 20 ult[j++] = code
6940: 2b 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 7d + '0';. }
6950: 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
6960: 20 20 20 20 20 20 70 72 65 76 63 6f 64 65 20 3d prevcode =
6970: 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
6980: 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 6a 3c 34 }. while( j<4
6990: 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 73 75 6c ){. zResul
69a0: 74 5b 6a 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 t[j++] = '0';.
69b0: 20 20 7d 0a 20 20 20 20 7a 52 65 73 75 6c 74 5b }. zResult[
69c0: 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 j] = 0;. sqli
69d0: 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 te3_result_text(
69e0: 63 6f 6e 74 65 78 74 2c 20 7a 52 65 73 75 6c 74 context, zResult
69f0: 2c 20 34 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e , 4, SQLITE_TRAN
6a00: 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b SIENT);. }else{
6a10: 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 . sqlite3_res
6a20: 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
6a30: 2c 20 22 3f 30 30 30 22 2c 20 34 2c 20 53 51 4c , "?000", 4, SQL
6a40: 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d ITE_STATIC);. }
6a50: 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
6a60: 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c ef SQLITE_OMIT_L
6a70: 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a OAD_EXTENSION./*
6a80: 0a 2a 2a 20 41 20 66 75 6e 63 74 69 6f 6e 20 74 .** A function t
6a90: 68 61 74 20 6c 6f 61 64 73 20 61 20 73 68 61 72 hat loads a shar
6aa0: 65 64 2d 6c 69 62 72 61 72 79 20 65 78 74 65 6e ed-library exten
6ab0: 73 69 6f 6e 20 74 68 65 6e 20 72 65 74 75 72 6e sion then return
6ac0: 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 s NULL..*/.stati
6ad0: 63 20 76 6f 69 64 20 6c 6f 61 64 45 78 74 28 73 c void loadExt(s
6ae0: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
6af0: 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 context, int arg
6b00: 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 c, sqlite3_value
6b10: 20 2a 2a 61 72 67 76 29 7b 0a 20 20 63 6f 6e 73 **argv){. cons
6b20: 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 20 3d 20 t char *zFile =
6b30: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 (const char *)sq
6b40: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
6b50: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 6f 6e (argv[0]);. con
6b60: 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 3b 0a st char *zProc;.
6b70: 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
6b80: 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
6b90: 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63 68 a(context);. ch
6ba0: 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b ar *zErrMsg = 0;
6bb0: 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 .. if( argc==2
6bc0: 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 28 ){. zProc = (
6bd0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c const char *)sql
6be0: 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
6bf0: 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 65 6c 73 argv[1]);. }els
6c00: 65 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20 30 e{. zProc = 0
6c10: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46 69 6c ;. }. if( zFil
6c20: 65 20 26 26 20 73 71 6c 69 74 65 33 5f 6c 6f 61 e && sqlite3_loa
6c30: 64 5f 65 78 74 65 6e 73 69 6f 6e 28 64 62 2c 20 d_extension(db,
6c40: 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 26 7a zFile, zProc, &z
6c50: 45 72 72 4d 73 67 29 20 29 7b 0a 20 20 20 20 73 ErrMsg) ){. s
6c60: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
6c70: 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 ror(context, zEr
6c80: 72 4d 73 67 2c 20 2d 31 29 3b 0a 20 20 20 20 73 rMsg, -1);. s
6c90: 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72 qlite3_free(zErr
6ca0: 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 Msg);. }.}.#end
6cb0: 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
6cc0: 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 E_TEST./*.** Thi
6cd0: 73 20 66 75 6e 63 74 69 6f 6e 20 67 65 6e 65 72 s function gener
6ce0: 61 74 65 73 20 61 20 73 74 72 69 6e 67 20 6f 66 ates a string of
6cf0: 20 72 61 6e 64 6f 6d 20 63 68 61 72 61 63 74 65 random characte
6d00: 72 73 2e 20 20 55 73 65 64 20 66 6f 72 0a 2a 2a rs. Used for.**
6d10: 20 67 65 6e 65 72 61 74 69 6e 67 20 74 65 73 74 generating test
6d20: 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 data..*/.static
6d30: 20 76 6f 69 64 20 72 61 6e 64 53 74 72 28 73 71 void randStr(sq
6d40: 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
6d50: 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 ontext, int argc
6d60: 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 , sqlite3_value
6d70: 2a 2a 61 72 67 76 29 7b 0a 20 20 73 74 61 74 69 **argv){. stati
6d80: 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
6d90: 20 63 68 61 72 20 7a 53 72 63 5b 5d 20 3d 20 0a char zSrc[] = .
6da0: 20 20 20 20 20 22 61 62 63 64 65 66 67 68 69 6a "abcdefghij
6db0: 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a klmnopqrstuvwxyz
6dc0: 22 0a 20 20 20 20 20 22 41 42 43 44 45 46 47 48 ". "ABCDEFGH
6dd0: 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 IJKLMNOPQRSTUVWX
6de0: 59 5a 22 0a 20 20 20 20 20 22 30 31 32 33 34 35 YZ". "012345
6df0: 36 37 38 39 22 0a 20 20 20 20 20 22 2e 2d 21 2c 6789". ".-!,
6e00: 3a 2a 5e 2b 3d 5f 7c 3f 2f 3c 3e 20 22 3b 0a 20 :*^+=_|?/<> ";.
6e10: 20 69 6e 74 20 69 4d 69 6e 2c 20 69 4d 61 78 2c int iMin, iMax,
6e20: 20 6e 2c 20 72 2c 20 69 3b 0a 20 20 75 6e 73 69 n, r, i;. unsi
6e30: 67 6e 65 64 20 63 68 61 72 20 7a 42 75 66 5b 31 gned char zBuf[1
6e40: 30 30 30 5d 3b 0a 20 20 69 66 28 20 61 72 67 63 000];. if( argc
6e50: 3e 3d 31 20 29 7b 0a 20 20 20 20 69 4d 69 6e 20 >=1 ){. iMin
6e60: 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
6e70: 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 int(argv[0]);.
6e80: 20 20 69 66 28 20 69 4d 69 6e 3c 30 20 29 20 69 if( iMin<0 ) i
6e90: 4d 69 6e 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 Min = 0;. if(
6ea0: 20 69 4d 69 6e 3e 3d 73 69 7a 65 6f 66 28 7a 42 iMin>=sizeof(zB
6eb0: 75 66 29 20 29 20 69 4d 69 6e 20 3d 20 73 69 7a uf) ) iMin = siz
6ec0: 65 6f 66 28 7a 42 75 66 29 2d 31 3b 0a 20 20 7d eof(zBuf)-1;. }
6ed0: 65 6c 73 65 7b 0a 20 20 20 20 69 4d 69 6e 20 3d else{. iMin =
6ee0: 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 61 72 1;. }. if( ar
6ef0: 67 63 3e 3d 32 20 29 7b 0a 20 20 20 20 69 4d 61 gc>=2 ){. iMa
6f00: 78 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 x = sqlite3_valu
6f10: 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b 0a e_int(argv[1]);.
6f20: 20 20 20 20 69 66 28 20 69 4d 61 78 3c 69 4d 69 if( iMax<iMi
6f30: 6e 20 29 20 69 4d 61 78 20 3d 20 69 4d 69 6e 3b n ) iMax = iMin;
6f40: 0a 20 20 20 20 69 66 28 20 69 4d 61 78 3e 3d 73 . if( iMax>=s
6f50: 69 7a 65 6f 66 28 7a 42 75 66 29 20 29 20 69 4d izeof(zBuf) ) iM
6f60: 61 78 20 3d 20 73 69 7a 65 6f 66 28 7a 42 75 66 ax = sizeof(zBuf
6f70: 29 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 )-1;. }else{.
6f80: 20 20 69 4d 61 78 20 3d 20 35 30 3b 0a 20 20 7d iMax = 50;. }
6f90: 0a 20 20 6e 20 3d 20 69 4d 69 6e 3b 0a 20 20 69 . n = iMin;. i
6fa0: 66 28 20 69 4d 61 78 3e 69 4d 69 6e 20 29 7b 0a f( iMax>iMin ){.
6fb0: 20 20 20 20 73 71 6c 69 74 65 33 52 61 6e 64 6f sqlite3Rando
6fc0: 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 29 2c mness(sizeof(r),
6fd0: 20 26 72 29 3b 0a 20 20 20 20 72 20 26 3d 20 30 &r);. r &= 0
6fe0: 78 37 66 66 66 66 66 66 66 3b 0a 20 20 20 20 6e x7fffffff;. n
6ff0: 20 2b 3d 20 72 25 28 69 4d 61 78 20 2b 20 31 20 += r%(iMax + 1
7000: 2d 20 69 4d 69 6e 29 3b 0a 20 20 7d 0a 20 20 61 - iMin);. }. a
7010: 73 73 65 72 74 28 20 6e 3c 73 69 7a 65 6f 66 28 ssert( n<sizeof(
7020: 7a 42 75 66 29 20 29 3b 0a 20 20 73 71 6c 69 74 zBuf) );. sqlit
7030: 65 33 52 61 6e 64 6f 6d 6e 65 73 73 28 6e 2c 20 e3Randomness(n,
7040: 7a 42 75 66 29 3b 0a 20 20 66 6f 72 28 69 3d 30 zBuf);. for(i=0
7050: 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 ; i<n; i++){.
7060: 20 7a 42 75 66 5b 69 5d 20 3d 20 7a 53 72 63 5b zBuf[i] = zSrc[
7070: 7a 42 75 66 5b 69 5d 25 28 73 69 7a 65 6f 66 28 zBuf[i]%(sizeof(
7080: 7a 53 72 63 29 2d 31 29 5d 3b 0a 20 20 7d 0a 20 zSrc)-1)];. }.
7090: 20 7a 42 75 66 5b 6e 5d 20 3d 20 30 3b 0a 20 20 zBuf[n] = 0;.
70a0: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
70b0: 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 ext(context, (ch
70c0: 61 72 2a 29 7a 42 75 66 2c 20 6e 2c 20 53 51 4c ar*)zBuf, n, SQL
70d0: 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a ITE_TRANSIENT);.
70e0: 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
70f0: 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 23 69 66 64 TE_TEST */..#ifd
7100: 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f ef SQLITE_TEST./
7110: 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
7120: 6e 67 20 74 77 6f 20 53 51 4c 20 66 75 6e 63 74 ng two SQL funct
7130: 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f ions are used to
7140: 20 74 65 73 74 20 72 65 74 75 72 6e 69 6e 67 20 test returning
7150: 61 20 74 65 78 74 0a 2a 2a 20 72 65 73 75 6c 74 a text.** result
7160: 20 77 69 74 68 20 61 20 64 65 73 74 72 75 63 74 with a destruct
7170: 6f 72 2e 20 46 75 6e 63 74 69 6f 6e 20 27 74 65 or. Function 'te
7180: 73 74 5f 64 65 73 74 72 75 63 74 6f 72 27 20 74 st_destructor' t
7190: 61 6b 65 73 20 6f 6e 65 20 61 72 67 75 6d 65 6e akes one argumen
71a0: 74 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 73 t.** and returns
71b0: 20 74 68 65 20 73 61 6d 65 20 61 72 67 75 6d 65 the same argume
71c0: 6e 74 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 nt interpreted a
71d0: 73 20 54 45 58 54 2e 20 41 20 64 65 73 74 72 75 s TEXT. A destru
71e0: 63 74 6f 72 20 69 73 0a 2a 2a 20 70 61 73 73 65 ctor is.** passe
71f0: 64 20 77 69 74 68 20 74 68 65 20 73 71 6c 69 74 d with the sqlit
7200: 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 29 e3_result_text()
7210: 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c call..**.** SQL
7220: 20 66 75 6e 63 74 69 6f 6e 20 27 74 65 73 74 5f function 'test_
7230: 64 65 73 74 72 75 63 74 6f 72 5f 63 6f 75 6e 74 destructor_count
7240: 27 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 ' returns the nu
7250: 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 mber of outstand
7260: 69 6e 67 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 ing .** allocati
7270: 6f 6e 73 20 6d 61 64 65 20 62 79 20 27 74 65 73 ons made by 'tes
7280: 74 5f 64 65 73 74 72 75 63 74 6f 72 27 3b 0a 2a t_destructor';.*
7290: 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 4e 6f *.** WARNING: No
72a0: 74 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f t threadsafe..*/
72b0: 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 73 74 .static int test
72c0: 5f 64 65 73 74 72 75 63 74 6f 72 5f 63 6f 75 6e _destructor_coun
72d0: 74 5f 76 61 72 20 3d 20 30 3b 0a 73 74 61 74 69 t_var = 0;.stati
72e0: 63 20 76 6f 69 64 20 64 65 73 74 72 75 63 74 6f c void destructo
72f0: 72 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 63 68 r(void *p){. ch
7300: 61 72 20 2a 7a 56 61 6c 20 3d 20 28 63 68 61 72 ar *zVal = (char
7310: 20 2a 29 70 3b 0a 20 20 61 73 73 65 72 74 28 7a *)p;. assert(z
7320: 56 61 6c 29 3b 0a 20 20 7a 56 61 6c 2d 2d 3b 0a Val);. zVal--;.
7330: 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
7340: 56 61 6c 29 3b 0a 20 20 74 65 73 74 5f 64 65 73 Val);. test_des
7350: 74 72 75 63 74 6f 72 5f 63 6f 75 6e 74 5f 76 61 tructor_count_va
7360: 72 2d 2d 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f r--;.}.static vo
7370: 69 64 20 74 65 73 74 5f 64 65 73 74 72 75 63 74 id test_destruct
7380: 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f or(. sqlite3_co
7390: 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 ntext *pCtx, .
73a0: 69 6e 74 20 6e 41 72 67 2c 0a 20 20 73 71 6c 69 int nArg,. sqli
73b0: 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
73c0: 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 56 61 6c .){. char *zVal
73d0: 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 73 ;. int len;. s
73e0: 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c qlite3 *db = sql
73f0: 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 70 ite3_user_data(p
7400: 43 74 78 29 3b 0a 20 0a 20 20 74 65 73 74 5f 64 Ctx);. . test_d
7410: 65 73 74 72 75 63 74 6f 72 5f 63 6f 75 6e 74 5f estructor_count_
7420: 76 61 72 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 var++;. assert(
7430: 20 6e 41 72 67 3d 3d 31 20 29 3b 0a 20 20 69 66 nArg==1 );. if
7440: 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ( sqlite3_value_
7450: 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 type(argv[0])==S
7460: 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 QLITE_NULL ) ret
7470: 75 72 6e 3b 0a 20 20 6c 65 6e 20 3d 20 73 71 6c urn;. len = sql
7480: 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 61 ite3ValueBytes(a
7490: 72 67 76 5b 30 5d 2c 20 45 4e 43 28 64 62 29 29 rgv[0], ENC(db))
74a0: 3b 20 0a 20 20 7a 56 61 6c 20 3d 20 63 6f 6e 74 ; . zVal = cont
74b0: 65 78 74 4d 61 6c 6c 6f 63 28 70 43 74 78 2c 20 extMalloc(pCtx,
74c0: 6c 65 6e 2b 33 29 3b 0a 20 20 69 66 28 20 21 7a len+3);. if( !z
74d0: 56 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 Val ){. retur
74e0: 6e 3b 0a 20 20 7d 0a 20 20 7a 56 61 6c 5b 6c 65 n;. }. zVal[le
74f0: 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 7a 56 61 6c n+1] = 0;. zVal
7500: 5b 6c 65 6e 2b 32 5d 20 3d 20 30 3b 0a 20 20 7a [len+2] = 0;. z
7510: 56 61 6c 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28 Val++;. memcpy(
7520: 7a 56 61 6c 2c 20 73 71 6c 69 74 65 33 56 61 6c zVal, sqlite3Val
7530: 75 65 54 65 78 74 28 61 72 67 76 5b 30 5d 2c 20 ueText(argv[0],
7540: 45 4e 43 28 64 62 29 29 2c 20 6c 65 6e 29 3b 0a ENC(db)), len);.
7550: 20 20 69 66 28 20 45 4e 43 28 64 62 29 3d 3d 53 if( ENC(db)==S
7560: 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 QLITE_UTF8 ){.
7570: 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
7580: 5f 74 65 78 74 28 70 43 74 78 2c 20 7a 56 61 6c _text(pCtx, zVal
7590: 2c 20 2d 31 2c 20 64 65 73 74 72 75 63 74 6f 72 , -1, destructor
75a0: 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
75b0: 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 7d E_OMIT_UTF16. }
75c0: 65 6c 73 65 20 69 66 28 20 45 4e 43 28 64 62 29 else if( ENC(db)
75d0: 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 ==SQLITE_UTF16LE
75e0: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
75f0: 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 28 result_text16le(
7600: 70 43 74 78 2c 20 7a 56 61 6c 2c 20 2d 31 2c 20 pCtx, zVal, -1,
7610: 64 65 73 74 72 75 63 74 6f 72 29 3b 0a 20 20 7d destructor);. }
7620: 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
7630: 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 3_result_text16b
7640: 65 28 70 43 74 78 2c 20 7a 56 61 6c 2c 20 2d 31 e(pCtx, zVal, -1
7650: 2c 20 64 65 73 74 72 75 63 74 6f 72 29 3b 0a 23 , destructor);.#
7660: 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
7670: 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 20 20 OMIT_UTF16 */.
7680: 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 }.}.static void
7690: 74 65 73 74 5f 64 65 73 74 72 75 63 74 6f 72 5f test_destructor_
76a0: 63 6f 75 6e 74 28 0a 20 20 73 71 6c 69 74 65 33 count(. sqlite3
76b0: 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
76c0: 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 73 . int nArg,. s
76d0: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
76e0: 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 rgv.){. sqlite3
76f0: 5f 72 65 73 75 6c 74 5f 69 6e 74 28 70 43 74 78 _result_int(pCtx
7700: 2c 20 74 65 73 74 5f 64 65 73 74 72 75 63 74 6f , test_destructo
7710: 72 5f 63 6f 75 6e 74 5f 76 61 72 29 3b 0a 7d 0a r_count_var);.}.
7720: 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
7730: 5f 54 45 53 54 20 2a 2f 0a 0a 23 69 66 64 65 66 _TEST */..#ifdef
7740: 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a SQLITE_TEST./*.
7750: 2a 2a 20 52 6f 75 74 69 6e 65 73 20 66 6f 72 20 ** Routines for
7760: 74 65 73 74 69 6e 67 20 74 68 65 20 73 71 6c 69 testing the sqli
7770: 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 te3_get_auxdata(
7780: 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 65 ) and sqlite3_se
7790: 74 5f 61 75 78 64 61 74 61 28 29 0a 2a 2a 20 69 t_auxdata().** i
77a0: 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
77b0: 54 68 65 20 74 65 73 74 5f 61 75 78 64 61 74 61 The test_auxdata
77c0: 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 () SQL function
77d0: 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 67 69 attempts to regi
77e0: 73 74 65 72 20 65 61 63 68 20 6f 66 20 69 74 73 ster each of its
77f0: 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 61 73 arguments.** as
7800: 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 2e auxiliary data.
7810: 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e If there are n
7820: 6f 20 70 72 69 6f 72 20 72 65 67 69 73 74 72 61 o prior registra
7830: 74 69 6f 6e 73 20 6f 66 20 61 75 78 20 64 61 74 tions of aux dat
7840: 61 20 66 6f 72 0a 2a 2a 20 74 68 61 74 20 61 72 a for.** that ar
7850: 67 75 6d 65 6e 74 20 28 6d 65 61 6e 69 6e 67 20 gument (meaning
7860: 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 the argument is
7870: 6e 6f 74 20 61 20 63 6f 6e 73 74 61 6e 74 20 6f not a constant o
7880: 72 20 74 68 69 73 20 69 73 20 69 74 73 20 66 69 r this is its fi
7890: 72 73 74 0a 2a 2a 20 63 61 6c 6c 29 20 74 68 65 rst.** call) the
78a0: 6e 20 74 68 65 20 72 65 73 75 6c 74 20 66 6f 72 n the result for
78b0: 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74 20 69 that argument i
78c0: 73 20 30 2e 20 20 49 66 20 74 68 65 72 65 20 69 s 0. If there i
78d0: 73 20 61 20 70 72 69 6f 72 0a 2a 2a 20 72 65 67 s a prior.** reg
78e0: 69 73 74 72 61 74 69 6f 6e 2c 20 74 68 65 20 72 istration, the r
78f0: 65 73 75 6c 74 20 66 6f 72 20 74 68 61 74 20 61 esult for that a
7900: 72 67 75 6d 65 6e 74 20 69 73 20 31 2e 20 20 54 rgument is 1. T
7910: 68 65 20 6f 76 65 72 61 6c 6c 20 72 65 73 75 6c he overall resul
7920: 74 0a 2a 2a 20 69 73 20 74 68 65 20 69 6e 64 69 t.** is the indi
7930: 76 69 64 75 61 6c 20 61 72 67 75 6d 65 6e 74 20 vidual argument
7940: 72 65 73 75 6c 74 73 20 73 65 70 61 72 61 74 65 results separate
7950: 64 20 62 79 20 73 70 61 63 65 73 2e 0a 2a 2f 0a d by spaces..*/.
7960: 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 static void free
7970: 5f 74 65 73 74 5f 61 75 78 64 61 74 61 28 76 6f _test_auxdata(vo
7980: 69 64 20 2a 70 29 20 7b 73 71 6c 69 74 65 33 5f id *p) {sqlite3_
7990: 66 72 65 65 28 70 29 3b 7d 0a 73 74 61 74 69 63 free(p);}.static
79a0: 20 76 6f 69 64 20 74 65 73 74 5f 61 75 78 64 61 void test_auxda
79b0: 74 61 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f ta(. sqlite3_co
79c0: 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 ntext *pCtx, .
79d0: 69 6e 74 20 6e 41 72 67 2c 0a 20 20 73 71 6c 69 int nArg,. sqli
79e0: 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
79f0: 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 .){. int i;. c
7a00: 68 61 72 20 2a 7a 52 65 74 20 3d 20 63 6f 6e 74 har *zRet = cont
7a10: 65 78 74 4d 61 6c 6c 6f 63 28 70 43 74 78 2c 20 extMalloc(pCtx,
7a20: 6e 41 72 67 2a 32 29 3b 0a 20 20 69 66 28 20 21 nArg*2);. if( !
7a30: 7a 52 65 74 20 29 20 72 65 74 75 72 6e 3b 0a 20 zRet ) return;.
7a40: 20 6d 65 6d 73 65 74 28 7a 52 65 74 2c 20 30 2c memset(zRet, 0,
7a50: 20 6e 41 72 67 2a 32 29 3b 0a 20 20 66 6f 72 28 nArg*2);. for(
7a60: 69 3d 30 3b 20 69 3c 6e 41 72 67 3b 20 69 2b 2b i=0; i<nArg; i++
7a70: 29 7b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 ){. char cons
7a80: 74 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 73 71 t *z = (char*)sq
7a90: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
7aa0: 28 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 69 (argv[i]);. i
7ab0: 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 63 68 f( z ){. ch
7ac0: 61 72 20 2a 7a 41 75 78 20 3d 20 73 71 6c 69 74 ar *zAux = sqlit
7ad0: 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 70 e3_get_auxdata(p
7ae0: 43 74 78 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 Ctx, i);. i
7af0: 66 28 20 7a 41 75 78 20 29 7b 0a 20 20 20 20 20 f( zAux ){.
7b00: 20 20 20 7a 52 65 74 5b 69 2a 32 5d 20 3d 20 27 zRet[i*2] = '
7b10: 31 27 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 1';. if(
7b20: 73 74 72 63 6d 70 28 7a 41 75 78 2c 20 7a 29 20 strcmp(zAux, z)
7b30: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 72 65 ){. fre
7b40: 65 5f 74 65 73 74 5f 61 75 78 64 61 74 61 28 28 e_test_auxdata((
7b50: 76 6f 69 64 20 2a 29 7a 52 65 74 29 3b 0a 20 20 void *)zRet);.
7b60: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
7b70: 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43 74 result_error(pCt
7b80: 78 2c 20 22 41 75 78 69 6c 61 72 79 20 64 61 74 x, "Auxilary dat
7b90: 61 20 63 6f 72 72 75 70 74 69 6f 6e 22 2c 20 2d a corruption", -
7ba0: 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 1);. re
7bb0: 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a turn;. }.
7bc0: 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
7bd0: 20 20 20 20 20 7a 52 65 74 5b 69 2a 32 5d 20 3d zRet[i*2] =
7be0: 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 7a 41 '0';. zA
7bf0: 75 78 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c ux = contextMall
7c00: 6f 63 28 70 43 74 78 2c 20 73 74 72 6c 65 6e 28 oc(pCtx, strlen(
7c10: 7a 29 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 69 z)+1);. i
7c20: 66 28 20 7a 41 75 78 20 29 7b 0a 20 20 20 20 20 f( zAux ){.
7c30: 20 20 20 20 20 73 74 72 63 70 79 28 7a 41 75 78 strcpy(zAux
7c40: 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , z);.
7c50: 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 sqlite3_set_auxd
7c60: 61 74 61 28 70 43 74 78 2c 20 69 2c 20 7a 41 75 ata(pCtx, i, zAu
7c70: 78 2c 20 66 72 65 65 5f 74 65 73 74 5f 61 75 78 x, free_test_aux
7c80: 64 61 74 61 29 3b 0a 20 20 20 20 20 20 20 20 7d data);. }
7c90: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a . }. z
7ca0: 52 65 74 5b 69 2a 32 2b 31 5d 20 3d 20 27 20 27 Ret[i*2+1] = ' '
7cb0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 ;. }. }. sq
7cc0: 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
7cd0: 74 28 70 43 74 78 2c 20 7a 52 65 74 2c 20 32 2a t(pCtx, zRet, 2*
7ce0: 6e 41 72 67 2d 31 2c 20 66 72 65 65 5f 74 65 73 nArg-1, free_tes
7cf0: 74 5f 61 75 78 64 61 74 61 29 3b 0a 7d 0a 23 65 t_auxdata);.}.#e
7d00: 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 ndif /* SQLITE_T
7d10: 45 53 54 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 EST */..#ifdef S
7d20: 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a QLITE_TEST./*.**
7d30: 20 41 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 A function to t
7d40: 65 73 74 20 65 72 72 6f 72 20 72 65 70 6f 72 74 est error report
7d50: 69 6e 67 20 66 72 6f 6d 20 75 73 65 72 20 66 75 ing from user fu
7d60: 6e 63 74 69 6f 6e 73 2e 20 54 68 69 73 20 66 75 nctions. This fu
7d70: 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e nction.** return
7d80: 73 20 61 20 63 6f 70 79 20 6f 66 20 69 74 27 73 s a copy of it's
7d90: 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 first argument
7da0: 61 73 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a as an error..*/.
7db0: 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 static void test
7dc0: 5f 65 72 72 6f 72 28 0a 20 20 73 71 6c 69 74 65 _error(. sqlite
7dd0: 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 3_context *pCtx,
7de0: 20 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 . int nArg,.
7df0: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
7e00: 61 72 67 76 0a 29 7b 0a 20 20 73 71 6c 69 74 65 argv.){. sqlite
7e10: 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 3_result_error(p
7e20: 43 74 78 2c 20 28 63 68 61 72 2a 29 73 71 6c 69 Ctx, (char*)sqli
7e30: 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
7e40: 72 67 76 5b 30 5d 29 2c 20 30 29 3b 0a 7d 0a 23 rgv[0]), 0);.}.#
7e50: 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
7e60: 54 45 53 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 TEST */../*.** A
7e70: 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
7e80: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
7e90: 63 74 75 72 65 20 68 6f 6c 64 73 20 74 68 65 20 cture holds the
7ea0: 63 6f 6e 74 65 78 74 20 6f 66 20 61 0a 2a 2a 20 context of a.**
7eb0: 73 75 6d 28 29 20 6f 72 20 61 76 67 28 29 20 61 sum() or avg() a
7ec0: 67 67 72 65 67 61 74 65 20 63 6f 6d 70 75 74 61 ggregate computa
7ed0: 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 tion..*/.typedef
7ee0: 20 73 74 72 75 63 74 20 53 75 6d 43 74 78 20 53 struct SumCtx S
7ef0: 75 6d 43 74 78 3b 0a 73 74 72 75 63 74 20 53 75 umCtx;.struct Su
7f00: 6d 43 74 78 20 7b 0a 20 20 64 6f 75 62 6c 65 20 mCtx {. double
7f10: 72 53 75 6d 3b 20 20 20 20 20 20 2f 2a 20 46 6c rSum; /* Fl
7f20: 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 6d oating point sum
7f30: 20 2a 2f 0a 20 20 69 36 34 20 69 53 75 6d 3b 20 */. i64 iSum;
7f40: 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
7f50: 65 72 20 73 75 6d 20 2a 2f 20 20 20 0a 20 20 69 er sum */ . i
7f60: 36 34 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 64 cnt;
7f70: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c /* Number of el
7f80: 65 6d 65 6e 74 73 20 73 75 6d 6d 65 64 20 2a 2f ements summed */
7f90: 0a 20 20 75 38 20 6f 76 65 72 66 6c 6f 77 3b 20 . u8 overflow;
7fa0: 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
7fb0: 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 integer overflow
7fc0: 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 61 70 seen */. u8 ap
7fd0: 70 72 6f 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 prox; /*
7fe0: 54 72 75 65 20 69 66 20 6e 6f 6e 2d 69 6e 74 65 True if non-inte
7ff0: 67 65 72 20 76 61 6c 75 65 20 77 61 73 20 69 6e ger value was in
8000: 70 75 74 20 74 6f 20 74 68 65 20 73 75 6d 20 2a put to the sum *
8010: 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 /.};../*.** Rout
8020: 69 6e 65 73 20 75 73 65 64 20 74 6f 20 63 6f 6d ines used to com
8030: 70 75 74 65 20 74 68 65 20 73 75 6d 2c 20 61 76 pute the sum, av
8040: 65 72 61 67 65 2c 20 61 6e 64 20 74 6f 74 61 6c erage, and total
8050: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 55 4d 28 ..**.** The SUM(
8060: 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c 6f ) function follo
8070: 77 73 20 74 68 65 20 28 62 72 6f 6b 65 6e 29 20 ws the (broken)
8080: 53 51 4c 20 73 74 61 6e 64 61 72 64 20 77 68 69 SQL standard whi
8090: 63 68 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 ch means.** that
80a0: 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
80b0: 20 69 66 20 69 74 20 73 75 6d 73 20 6f 76 65 72 if it sums over
80c0: 20 6e 6f 20 69 6e 70 75 74 73 2e 20 20 54 4f 54 no inputs. TOT
80d0: 41 4c 20 72 65 74 75 72 6e 73 0a 2a 2a 20 30 2e AL returns.** 0.
80e0: 30 20 69 6e 20 74 68 61 74 20 63 61 73 65 2e 20 0 in that case.
80f0: 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20 54 4f In addition, TO
8100: 54 41 4c 20 61 6c 77 61 79 73 20 72 65 74 75 72 TAL always retur
8110: 6e 73 20 61 20 66 6c 6f 61 74 20 77 68 65 72 65 ns a float where
8120: 0a 2a 2a 20 53 55 4d 20 6d 69 67 68 74 20 72 65 .** SUM might re
8130: 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 turn an integer
8140: 69 66 20 69 74 20 6e 65 76 65 72 20 65 6e 63 6f if it never enco
8150: 75 6e 74 65 72 73 20 61 20 66 6c 6f 61 74 69 6e unters a floatin
8160: 67 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 g point.** value
8170: 2e 20 20 54 4f 54 41 4c 20 6e 65 76 65 72 20 66 . TOTAL never f
8180: 61 69 6c 73 2c 20 62 75 74 20 53 55 4d 20 6d 69 ails, but SUM mi
8190: 67 68 74 20 74 68 72 6f 75 67 68 20 61 6e 20 65 ght through an e
81a0: 78 63 65 70 74 69 6f 6e 20 69 66 0a 2a 2a 20 69 xception if.** i
81b0: 74 20 6f 76 65 72 66 6c 6f 77 73 20 61 6e 20 69 t overflows an i
81c0: 6e 74 65 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 nteger..*/.stati
81d0: 63 20 76 6f 69 64 20 73 75 6d 53 74 65 70 28 73 c void sumStep(s
81e0: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
81f0: 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 context, int arg
8200: 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 c, sqlite3_value
8210: 20 2a 2a 61 72 67 76 29 7b 0a 20 20 53 75 6d 43 **argv){. SumC
8220: 74 78 20 2a 70 3b 0a 20 20 69 6e 74 20 74 79 70 tx *p;. int typ
8230: 65 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 e;. assert( arg
8240: 63 3d 3d 31 20 29 3b 0a 20 20 70 20 3d 20 73 71 c==1 );. p = sq
8250: 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
8260: 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c context(context,
8270: 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 sizeof(*p));.
8280: 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 type = sqlite3_v
8290: 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 alue_numeric_typ
82a0: 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 e(argv[0]);. if
82b0: 28 20 70 20 26 26 20 74 79 70 65 21 3d 53 51 4c ( p && type!=SQL
82c0: 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 ITE_NULL ){.
82d0: 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 p->cnt++;. if
82e0: 28 20 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 ( type==SQLITE_I
82f0: 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 NTEGER ){.
8300: 69 36 34 20 76 20 3d 20 73 71 6c 69 74 65 33 5f i64 v = sqlite3_
8310: 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 value_int64(argv
8320: 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 72 [0]);. p->r
8330: 53 75 6d 20 2b 3d 20 76 3b 0a 20 20 20 20 20 20 Sum += v;.
8340: 69 66 28 20 28 70 2d 3e 61 70 70 72 6f 78 7c 70 if( (p->approx|p
8350: 2d 3e 6f 76 65 72 66 6c 6f 77 29 3d 3d 30 20 29 ->overflow)==0 )
8360: 7b 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 4e {. i64 iN
8370: 65 77 53 75 6d 20 3d 20 70 2d 3e 69 53 75 6d 20 ewSum = p->iSum
8380: 2b 20 76 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 + v;. int
8390: 20 73 31 20 3d 20 70 2d 3e 69 53 75 6d 20 3e 3e s1 = p->iSum >>
83a0: 20 28 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d (sizeof(i64)*8-
83b0: 31 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 1);. int
83c0: 73 32 20 3d 20 76 20 20 20 20 20 20 20 3e 3e 20 s2 = v >>
83d0: 28 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 (sizeof(i64)*8-1
83e0: 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73 );. int s
83f0: 33 20 3d 20 69 4e 65 77 53 75 6d 20 3e 3e 20 28 3 = iNewSum >> (
8400: 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 sizeof(i64)*8-1)
8410: 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6f 76 65 ;. p->ove
8420: 72 66 6c 6f 77 20 3d 20 28 73 31 26 73 32 26 7e rflow = (s1&s2&~
8430: 73 33 29 20 7c 20 28 7e 73 31 26 7e 73 32 26 73 s3) | (~s1&~s2&s
8440: 33 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 3);. p->i
8450: 53 75 6d 20 3d 20 69 4e 65 77 53 75 6d 3b 0a 20 Sum = iNewSum;.
8460: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
8470: 7b 0a 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20 {. p->rSum
8480: 2b 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 += sqlite3_value
8490: 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 _double(argv[0])
84a0: 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70 70 72 6f ;. p->appro
84b0: 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d x = 1;. }. }
84c0: 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 .}.static void s
84d0: 75 6d 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 umFinalize(sqlit
84e0: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
84f0: 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a ext){. SumCtx *
8500: 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 p;. p = sqlite3
8510: 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 _aggregate_conte
8520: 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a xt(context, 0);.
8530: 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 63 6e if( p && p->cn
8540: 74 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 t>0 ){. if( p
8550: 2d 3e 6f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 ->overflow ){.
8560: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
8570: 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 lt_error(context
8580: 2c 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c ,"integer overfl
8590: 6f 77 22 2c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c ow",-1);. }el
85a0: 73 65 20 69 66 28 20 70 2d 3e 61 70 70 72 6f 78 se if( p->approx
85b0: 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
85c0: 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 3_result_double(
85d0: 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d context, p->rSum
85e0: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
85f0: 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
8600: 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 lt_int64(context
8610: 2c 20 70 2d 3e 69 53 75 6d 29 3b 0a 20 20 20 20 , p->iSum);.
8620: 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 }. }.}.static v
8630: 6f 69 64 20 61 76 67 46 69 6e 61 6c 69 7a 65 28 oid avgFinalize(
8640: 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
8650: 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d *context){. Sum
8660: 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 Ctx *p;. p = sq
8670: 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
8680: 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c context(context,
8690: 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 0);. if( p &&
86a0: 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 p->cnt>0 ){.
86b0: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 sqlite3_result_d
86c0: 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 ouble(context, p
86d0: 2d 3e 72 53 75 6d 2f 28 64 6f 75 62 6c 65 29 70 ->rSum/(double)p
86e0: 2d 3e 63 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 ->cnt);. }.}.st
86f0: 61 74 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 46 atic void totalF
8700: 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f inalize(sqlite3_
8710: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
8720: 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a ){. SumCtx *p;.
8730: 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 p = sqlite3_ag
8740: 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
8750: 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 73 context, 0);. s
8760: 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f qlite3_result_do
8770: 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 20 uble(context, p
8780: 3f 20 70 2d 3e 72 53 75 6d 20 3a 20 30 2e 30 29 ? p->rSum : 0.0)
8790: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 ;.}../*.** The f
87a0: 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
87b0: 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f re keeps track o
87c0: 66 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 f state informat
87d0: 69 6f 6e 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 ion for the.** c
87e0: 6f 75 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 ount() aggregate
87f0: 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 function..*/.ty
8800: 70 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 75 pedef struct Cou
8810: 6e 74 43 74 78 20 43 6f 75 6e 74 43 74 78 3b 0a ntCtx CountCtx;.
8820: 73 74 72 75 63 74 20 43 6f 75 6e 74 43 74 78 20 struct CountCtx
8830: 7b 0a 20 20 69 36 34 20 6e 3b 0a 7d 3b 0a 0a 2f {. i64 n;.};../
8840: 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f *.** Routines to
8850: 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63 implement the c
8860: 6f 75 6e 74 28 29 20 61 67 67 72 65 67 61 74 65 ount() aggregate
8870: 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
8880: 61 74 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 53 atic void countS
8890: 74 65 70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 tep(sqlite3_cont
88a0: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e ext *context, in
88b0: 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f t argc, sqlite3_
88c0: 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 value **argv){.
88d0: 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20 CountCtx *p;.
88e0: 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 p = sqlite3_aggr
88f0: 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f egate_context(co
8900: 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 ntext, sizeof(*p
8910: 29 29 3b 0a 20 20 69 66 28 20 28 61 72 67 63 3d ));. if( (argc=
8920: 3d 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4e 55 4c =0 || SQLITE_NUL
8930: 4c 21 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 L!=sqlite3_value
8940: 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 20 _type(argv[0]))
8950: 26 26 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e && p ){. p->n
8960: 2b 2b 3b 0a 20 20 7d 0a 7d 20 20 20 0a 73 74 61 ++;. }.} .sta
8970: 74 69 63 20 76 6f 69 64 20 63 6f 75 6e 74 46 69 tic void countFi
8980: 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 nalize(sqlite3_c
8990: 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 ontext *context)
89a0: 7b 0a 20 20 43 6f 75 6e 74 43 74 78 20 2a 70 3b {. CountCtx *p;
89b0: 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 . p = sqlite3_a
89c0: 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 ggregate_context
89d0: 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 (context, 0);.
89e0: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 sqlite3_result_i
89f0: 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 70 20 nt64(context, p
8a00: 3f 20 70 2d 3e 6e 20 3a 20 30 29 3b 0a 7d 0a 0a ? p->n : 0);.}..
8a10: 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 /*.** Routines t
8a20: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 69 6e 28 o implement min(
8a30: 29 20 61 6e 64 20 6d 61 78 28 29 20 61 67 67 72 ) and max() aggr
8a40: 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e egate functions.
8a50: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8a60: 6d 69 6e 6d 61 78 53 74 65 70 28 73 71 6c 69 74 minmaxStep(sqlit
8a70: 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
8a80: 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 ext, int argc, s
8a90: 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
8aa0: 72 67 76 29 7b 0a 20 20 4d 65 6d 20 2a 70 41 72 rgv){. Mem *pAr
8ab0: 67 20 20 3d 20 28 4d 65 6d 20 2a 29 61 72 67 76 g = (Mem *)argv
8ac0: 5b 30 5d 3b 0a 20 20 4d 65 6d 20 2a 70 42 65 73 [0];. Mem *pBes
8ad0: 74 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 t;.. if( sqlite
8ae0: 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
8af0: 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 v[0])==SQLITE_NU
8b00: 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 LL ) return;. p
8b10: 42 65 73 74 20 3d 20 28 4d 65 6d 20 2a 29 73 71 Best = (Mem *)sq
8b20: 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
8b30: 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c context(context,
8b40: 20 73 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 29 sizeof(*pBest))
8b50: 3b 0a 20 20 69 66 28 20 21 70 42 65 73 74 20 29 ;. if( !pBest )
8b60: 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 return;.. if(
8b70: 70 42 65 73 74 2d 3e 66 6c 61 67 73 20 29 7b 0a pBest->flags ){.
8b80: 20 20 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 20 int max;.
8b90: 20 69 6e 74 20 63 6d 70 3b 0a 20 20 20 20 43 6f int cmp;. Co
8ba0: 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 llSeq *pColl = s
8bb0: 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c qlite3GetFuncCol
8bc0: 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 lSeq(context);.
8bd0: 20 20 20 2f 2a 20 54 68 69 73 20 73 74 65 70 20 /* This step
8be0: 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 function is used
8bf0: 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20 6d 69 for both the mi
8c00: 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20 61 67 n() and max() ag
8c10: 67 72 65 67 61 74 65 73 2c 0a 20 20 20 20 2a 2a gregates,. **
8c20: 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 the only differ
8c30: 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 65 ence between the
8c40: 20 74 77 6f 20 62 65 69 6e 67 20 74 68 61 74 20 two being that
8c50: 74 68 65 20 73 65 6e 73 65 20 6f 66 20 74 68 65 the sense of the
8c60: 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 . ** comparis
8c70: 6f 6e 20 69 73 20 69 6e 76 65 72 74 65 64 2e 20 on is inverted.
8c80: 46 6f 72 20 74 68 65 20 6d 61 78 28 29 20 61 67 For the max() ag
8c90: 67 72 65 67 61 74 65 2c 20 74 68 65 0a 20 20 20 gregate, the.
8ca0: 20 2a 2a 20 73 71 6c 69 74 65 33 5f 75 73 65 72 ** sqlite3_user
8cb0: 5f 64 61 74 61 28 29 20 66 75 6e 63 74 69 6f 6e _data() function
8cc0: 20 72 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a returns (void *
8cd0: 29 2d 31 2e 20 46 6f 72 20 6d 69 6e 28 29 20 69 )-1. For min() i
8ce0: 74 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 73 t. ** returns
8cf0: 20 28 76 6f 69 64 20 2a 29 64 62 2c 20 77 68 65 (void *)db, whe
8d00: 72 65 20 64 62 20 69 73 20 74 68 65 20 73 71 6c re db is the sql
8d10: 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 70 ite3* database p
8d20: 6f 69 6e 74 65 72 2e 0a 20 20 20 20 2a 2a 20 54 ointer.. ** T
8d30: 68 65 72 65 66 6f 72 65 20 74 68 65 20 6e 65 78 herefore the nex
8d40: 74 20 73 74 61 74 65 6d 65 6e 74 20 73 65 74 73 t statement sets
8d50: 20 76 61 72 69 61 62 6c 65 20 27 6d 61 78 27 20 variable 'max'
8d60: 74 6f 20 31 20 66 6f 72 20 74 68 65 20 6d 61 78 to 1 for the max
8d70: 28 29 0a 20 20 20 20 2a 2a 20 61 67 67 72 65 67 (). ** aggreg
8d80: 61 74 65 2c 20 6f 72 20 30 20 66 6f 72 20 6d 69 ate, or 0 for mi
8d90: 6e 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 n().. */.
8da0: 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 max = sqlite3_us
8db0: 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 er_data(context)
8dc0: 21 3d 30 3b 0a 20 20 20 20 63 6d 70 20 3d 20 73 !=0;. cmp = s
8dd0: 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 qlite3MemCompare
8de0: 28 70 42 65 73 74 2c 20 70 41 72 67 2c 20 70 43 (pBest, pArg, pC
8df0: 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 6d oll);. if( (m
8e00: 61 78 20 26 26 20 63 6d 70 3c 30 29 20 7c 7c 20 ax && cmp<0) ||
8e10: 28 21 6d 61 78 20 26 26 20 63 6d 70 3e 30 29 20 (!max && cmp>0)
8e20: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
8e30: 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42 65 73 VdbeMemCopy(pBes
8e40: 74 2c 20 70 41 72 67 29 3b 0a 20 20 20 20 7d 0a t, pArg);. }.
8e50: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
8e60: 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 ite3VdbeMemCopy(
8e70: 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 pBest, pArg);.
8e80: 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 }.}.static void
8e90: 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28 73 minMaxFinalize(s
8ea0: 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
8eb0: 63 6f 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c 69 context){. sqli
8ec0: 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 73 3b te3_value *pRes;
8ed0: 0a 20 20 70 52 65 73 20 3d 20 28 73 71 6c 69 74 . pRes = (sqlit
8ee0: 65 33 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69 74 e3_value *)sqlit
8ef0: 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e e3_aggregate_con
8f00: 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 text(context, 0)
8f10: 3b 0a 20 20 69 66 28 20 70 52 65 73 20 29 7b 0a ;. if( pRes ){.
8f20: 20 20 20 20 69 66 28 20 70 52 65 73 2d 3e 66 6c if( pRes->fl
8f30: 61 67 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ags ){. sql
8f40: 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 ite3_result_valu
8f50: 65 28 63 6f 6e 74 65 78 74 2c 20 70 52 65 73 29 e(context, pRes)
8f60: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
8f70: 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
8f80: 65 28 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a 0a e(pRes);. }.}..
8f90: 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
8fa0: 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 tion registered
8fb0: 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 all of the above
8fc0: 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 C functions as
8fd0: 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 SQL.** functions
8fe0: 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 . This should b
8ff0: 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 e the only routi
9000: 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 ne in this file
9010: 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 6c with.** external
9020: 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 76 6f 69 linkage..*/.voi
9030: 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 d sqlite3Registe
9040: 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f 6e rBuiltinFunction
9050: 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a s(sqlite3 *db){.
9060: 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
9070: 74 72 75 63 74 20 7b 0a 20 20 20 20 20 63 68 61 truct {. cha
9080: 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 73 r *zName;. s
9090: 69 67 6e 65 64 20 63 68 61 72 20 6e 41 72 67 3b igned char nArg;
90a0: 0a 20 20 20 20 20 75 38 20 61 72 67 54 79 70 65 . u8 argType
90b0: 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 ; /* f
90c0: 66 3a 20 64 62 20 20 20 31 3a 20 30 2c 20 32 3a f: db 1: 0, 2:
90d0: 20 31 2c 20 33 3a 20 32 2c 2e 2e 2e 20 20 4e 3a 1, 3: 2,... N:
90e0: 20 20 4e 2d 31 2e 20 2a 2f 0a 20 20 20 20 20 75 N-1. */. u
90f0: 38 20 65 54 65 78 74 52 65 70 3b 20 20 20 20 20 8 eTextRep;
9100: 20 20 20 20 20 2f 2a 20 31 3a 20 55 54 46 2d 31 /* 1: UTF-1
9110: 36 2e 20 20 30 3a 20 55 54 46 2d 38 20 2a 2f 0a 6. 0: UTF-8 */.
9120: 20 20 20 20 20 75 38 20 6e 65 65 64 43 6f 6c 6c u8 needColl
9130: 53 65 71 3b 0a 20 20 20 20 20 76 6f 69 64 20 28 Seq;. void (
9140: 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f *xFunc)(sqlite3_
9150: 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c context*,int,sql
9160: 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a ite3_value **);.
9170: 20 20 7d 20 61 46 75 6e 63 73 5b 5d 20 3d 20 7b } aFuncs[] = {
9180: 0a 20 20 20 20 7b 20 22 6d 69 6e 22 2c 20 20 20 . { "min",
9190: 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 -1,
91a0: 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 0, SQLITE_UTF8,
91b0: 20 20 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 1, minmaxFunc
91c0: 20 7d 2c 0a 20 20 20 20 7b 20 22 6d 69 6e 22 2c },. { "min",
91d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
91e0: 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 0, 0, SQLITE_UTF
91f0: 38 2c 20 20 20 20 31 2c 20 30 20 20 20 20 20 20 8, 1, 0
9200: 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6d 61 },. { "ma
9210: 78 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 x",
9220: 20 20 2d 31 2c 20 31 2c 20 53 51 4c 49 54 45 5f -1, 1, SQLITE_
9230: 55 54 46 38 2c 20 20 20 20 31 2c 20 6d 69 6e 6d UTF8, 1, minm
9240: 61 78 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 axFunc },. {
9250: 22 6d 61 78 22 2c 20 20 20 20 20 20 20 20 20 20 "max",
9260: 20 20 20 20 20 20 30 2c 20 31 2c 20 53 51 4c 49 0, 1, SQLI
9270: 54 45 5f 55 54 46 38 2c 20 20 20 20 31 2c 20 30 TE_UTF8, 1, 0
9280: 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 },.
9290: 20 7b 20 22 74 79 70 65 6f 66 22 2c 20 20 20 20 { "typeof",
92a0: 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 1, 0, S
92b0: 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 QLITE_UTF8, 0
92c0: 2c 20 74 79 70 65 6f 66 46 75 6e 63 20 7d 2c 0a , typeofFunc },.
92d0: 20 20 20 20 7b 20 22 6c 65 6e 67 74 68 22 2c 20 { "length",
92e0: 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 1, 0
92f0: 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 , SQLITE_UTF8,
9300: 20 20 30 2c 20 6c 65 6e 67 74 68 46 75 6e 63 20 0, lengthFunc
9310: 7d 2c 0a 20 20 20 20 7b 20 22 73 75 62 73 74 72 },. { "substr
9320: 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 33 ", 3
9330: 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 , 0, SQLITE_UTF8
9340: 2c 20 20 20 20 30 2c 20 73 75 62 73 74 72 46 75 , 0, substrFu
9350: 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 61 62 73 nc },. { "abs
9360: 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ",
9370: 20 20 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 1, 0, SQLITE_U
9380: 54 46 38 2c 20 20 20 20 30 2c 20 61 62 73 46 75 TF8, 0, absFu
9390: 6e 63 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 nc },. { "
93a0: 72 6f 75 6e 64 22 2c 20 20 20 20 20 20 20 20 20 round",
93b0: 20 20 20 20 20 31 2c 20 30 2c 20 53 51 4c 49 54 1, 0, SQLIT
93c0: 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 72 6f E_UTF8, 0, ro
93d0: 75 6e 64 46 75 6e 63 20 20 7d 2c 0a 20 20 20 20 undFunc },.
93e0: 7b 20 22 72 6f 75 6e 64 22 2c 20 20 20 20 20 20 { "round",
93f0: 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 53 51 2, 0, SQ
9400: 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c LITE_UTF8, 0,
9410: 20 72 6f 75 6e 64 46 75 6e 63 20 20 7d 2c 0a 20 roundFunc },.
9420: 20 20 20 7b 20 22 75 70 70 65 72 22 2c 20 20 20 { "upper",
9430: 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 1, 0,
9440: 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 SQLITE_UTF8,
9450: 20 30 2c 20 75 70 70 65 72 46 75 6e 63 20 20 7d 0, upperFunc }
9460: 2c 0a 20 20 20 20 7b 20 22 6c 6f 77 65 72 22 2c ,. { "lower",
9470: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 1,
9480: 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 0, SQLITE_UTF8,
9490: 20 20 20 20 30 2c 20 6c 6f 77 65 72 46 75 6e 63 0, lowerFunc
94a0: 20 20 7d 2c 0a 20 20 20 20 7b 20 22 63 6f 61 6c },. { "coal
94b0: 65 73 63 65 22 2c 20 20 20 20 20 20 20 20 20 20 esce",
94c0: 2d 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 -1, 0, SQLITE_UT
94d0: 46 38 2c 20 20 20 20 30 2c 20 69 66 6e 75 6c 6c F8, 0, ifnull
94e0: 46 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 63 Func },. { "c
94f0: 6f 61 6c 65 73 63 65 22 2c 20 20 20 20 20 20 20 oalesce",
9500: 20 20 20 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 0, 0, SQLITE
9510: 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 30 20 20 _UTF8, 0, 0
9520: 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b },. {
9530: 20 22 63 6f 61 6c 65 73 63 65 22 2c 20 20 20 20 "coalesce",
9540: 20 20 20 20 20 20 20 31 2c 20 30 2c 20 53 51 4c 1, 0, SQL
9550: 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 ITE_UTF8, 0,
9560: 30 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 0 },.
9570: 20 20 7b 20 22 68 65 78 22 2c 20 20 20 20 20 20 { "hex",
9580: 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 1, 0,
9590: 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 SQLITE_UTF8,
95a0: 30 2c 20 68 65 78 46 75 6e 63 20 20 20 20 7d 2c 0, hexFunc },
95b0: 0a 20 20 20 20 7b 20 22 69 66 6e 75 6c 6c 22 2c . { "ifnull",
95c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 2,
95d0: 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 0, SQLITE_UTF8,
95e0: 20 20 20 31 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 1, ifnullFunc
95f0: 20 7d 2c 0a 20 20 20 20 7b 20 22 72 61 6e 64 6f },. { "rando
9600: 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 2d m", -
9610: 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 1, 0, SQLITE_UTF
9620: 38 2c 20 20 20 20 30 2c 20 72 61 6e 64 6f 6d 46 8, 0, randomF
9630: 75 6e 63 20 7d 2c 0a 20 20 20 20 7b 20 22 72 61 unc },. { "ra
9640: 6e 64 6f 6d 62 6c 6f 62 22 2c 20 20 20 20 20 20 ndomblob",
9650: 20 20 20 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 1, 0, SQLITE_
9660: 55 54 46 38 2c 20 20 20 20 30 2c 20 72 61 6e 64 UTF8, 0, rand
9670: 6f 6d 42 6c 6f 62 20 7d 2c 0a 20 20 20 20 7b 20 omBlob },. {
9680: 22 6e 75 6c 6c 69 66 22 2c 20 20 20 20 20 20 20 "nullif",
9690: 20 20 20 20 20 20 32 2c 20 30 2c 20 53 51 4c 49 2, 0, SQLI
96a0: 54 45 5f 55 54 46 38 2c 20 20 20 20 31 2c 20 6e TE_UTF8, 1, n
96b0: 75 6c 6c 69 66 46 75 6e 63 20 7d 2c 0a 20 20 20 ullifFunc },.
96c0: 20 7b 20 22 73 71 6c 69 74 65 5f 76 65 72 73 69 { "sqlite_versi
96d0: 6f 6e 22 2c 20 20 20 20 20 30 2c 20 30 2c 20 53 on", 0, 0, S
96e0: 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 QLITE_UTF8, 0
96f0: 2c 20 76 65 72 73 69 6f 6e 46 75 6e 63 7d 2c 0a , versionFunc},.
9700: 20 20 20 20 7b 20 22 71 75 6f 74 65 22 2c 20 20 { "quote",
9710: 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 1, 0
9720: 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 , SQLITE_UTF8,
9730: 20 20 30 2c 20 71 75 6f 74 65 46 75 6e 63 20 20 0, quoteFunc
9740: 7d 2c 0a 20 20 20 20 7b 20 22 6c 61 73 74 5f 69 },. { "last_i
9750: 6e 73 65 72 74 5f 72 6f 77 69 64 22 2c 20 20 30 nsert_rowid", 0
9760: 2c 20 30 78 66 66 2c 20 53 51 4c 49 54 45 5f 55 , 0xff, SQLITE_U
9770: 54 46 38 2c 20 30 2c 20 6c 61 73 74 5f 69 6e 73 TF8, 0, last_ins
9780: 65 72 74 5f 72 6f 77 69 64 20 7d 2c 0a 20 20 20 ert_rowid },.
9790: 20 7b 20 22 63 68 61 6e 67 65 73 22 2c 20 20 20 { "changes",
97a0: 20 20 20 20 20 20 20 20 20 30 2c 20 30 78 66 66 0, 0xff
97b0: 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 , SQLITE_UTF8, 0
97c0: 2c 20 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 , changes
97d0: 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 74 6f },. { "to
97e0: 74 61 6c 5f 63 68 61 6e 67 65 73 22 2c 20 20 20 tal_changes",
97f0: 20 20 20 30 2c 20 30 78 66 66 2c 20 53 51 4c 49 0, 0xff, SQLI
9800: 54 45 5f 55 54 46 38 2c 20 30 2c 20 74 6f 74 61 TE_UTF8, 0, tota
9810: 6c 5f 63 68 61 6e 67 65 73 20 20 20 20 20 7d 2c l_changes },
9820: 0a 20 20 20 20 7b 20 22 72 65 70 6c 61 63 65 22 . { "replace"
9830: 2c 20 20 20 20 20 20 20 20 20 20 20 20 33 2c 20 , 3,
9840: 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 0, SQLITE_UTF8,
9850: 20 20 20 30 2c 20 72 65 70 6c 61 63 65 46 75 6e 0, replaceFun
9860: 63 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b c },. {
9870: 20 22 6c 74 72 69 6d 22 2c 20 20 20 20 20 20 20 "ltrim",
9880: 20 20 20 20 20 20 20 31 2c 20 31 2c 20 53 51 4c 1, 1, SQL
9890: 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 ITE_UTF8, 0,
98a0: 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 trimFunc
98b0: 20 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 74 72 69 },. { "ltri
98c0: 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 m",
98d0: 20 32 2c 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 2, 1, SQLITE_UT
98e0: 46 38 2c 20 20 20 20 30 2c 20 74 72 69 6d 46 75 F8, 0, trimFu
98f0: 6e 63 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 nc },.
9900: 20 20 20 7b 20 22 72 74 72 69 6d 22 2c 20 20 20 { "rtrim",
9910: 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 32 2c 1, 2,
9920: 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 SQLITE_UTF8,
9930: 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 0, trimFunc
9940: 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 },. { "
9950: 72 74 72 69 6d 22 2c 20 20 20 20 20 20 20 20 20 rtrim",
9960: 20 20 20 20 20 32 2c 20 32 2c 20 53 51 4c 49 54 2, 2, SQLIT
9970: 45 5f 55 54 46 38 2c 20 20 20 20 30 2c 20 74 72 E_UTF8, 0, tr
9980: 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 20 imFunc
9990: 7d 2c 0a 20 20 20 20 7b 20 22 74 72 69 6d 22 2c },. { "trim",
99a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
99b0: 2c 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 , 3, SQLITE_UTF8
99c0: 2c 20 20 20 20 30 2c 20 74 72 69 6d 46 75 6e 63 , 0, trimFunc
99d0: 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 },.
99e0: 20 7b 20 22 74 72 69 6d 22 2c 20 20 20 20 20 20 { "trim",
99f0: 20 20 20 20 20 20 20 20 20 32 2c 20 33 2c 20 53 2, 3, S
9a00: 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 30 QLITE_UTF8, 0
9a10: 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 , trimFunc
9a20: 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 7a 65 },. { "ze
9a30: 72 6f 62 6c 6f 62 22 2c 20 20 20 20 20 20 20 20 roblob",
9a40: 20 20 20 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 1, 0, SQLITE_
9a50: 55 54 46 38 2c 20 20 20 20 30 2c 20 7a 65 72 6f UTF8, 0, zero
9a60: 62 6c 6f 62 46 75 6e 63 20 20 20 20 20 20 7d 2c blobFunc },
9a70: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 .#ifdef SQLITE_S
9a80: 4f 55 4e 44 45 58 0a 20 20 20 20 7b 20 22 73 6f OUNDEX. { "so
9a90: 75 6e 64 65 78 22 2c 20 20 20 20 20 20 20 20 20 undex",
9aa0: 20 20 20 31 2c 20 30 2c 20 53 51 4c 49 54 45 5f 1, 0, SQLITE_
9ab0: 55 54 46 38 2c 20 20 20 20 30 2c 20 73 6f 75 6e UTF8, 0, soun
9ac0: 64 65 78 46 75 6e 63 7d 2c 0a 23 65 6e 64 69 66 dexFunc},.#endif
9ad0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
9ae0: 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
9af0: 49 4f 4e 0a 20 20 20 20 7b 20 22 6c 6f 61 64 5f ION. { "load_
9b00: 65 78 74 65 6e 73 69 6f 6e 22 2c 20 20 20 20 20 extension",
9b10: 31 2c 20 30 78 66 66 2c 20 53 51 4c 49 54 45 5f 1, 0xff, SQLITE_
9b20: 55 54 46 38 2c 20 30 2c 20 6c 6f 61 64 45 78 74 UTF8, 0, loadExt
9b30: 20 7d 2c 0a 20 20 20 20 7b 20 22 6c 6f 61 64 5f },. { "load_
9b40: 65 78 74 65 6e 73 69 6f 6e 22 2c 20 20 20 20 20 extension",
9b50: 32 2c 20 30 78 66 66 2c 20 53 51 4c 49 54 45 5f 2, 0xff, SQLITE_
9b60: 55 54 46 38 2c 20 30 2c 20 6c 6f 61 64 45 78 74 UTF8, 0, loadExt
9b70: 20 7d 2c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 },.#endif.#ifde
9b80: 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 f SQLITE_TEST.
9b90: 20 20 7b 20 22 72 61 6e 64 73 74 72 22 2c 20 20 { "randstr",
9ba0: 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 2,
9bb0: 30 2c 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 0, SQLITE_UTF
9bc0: 38 2c 20 30 2c 20 72 61 6e 64 53 74 72 20 20 20 8, 0, randStr
9bd0: 20 7d 2c 0a 20 20 20 20 7b 20 22 74 65 73 74 5f },. { "test_
9be0: 64 65 73 74 72 75 63 74 6f 72 22 2c 20 20 20 20 destructor",
9bf0: 20 20 20 31 2c 20 30 78 66 66 2c 20 53 51 4c 49 1, 0xff, SQLI
9c00: 54 45 5f 55 54 46 38 2c 20 30 2c 20 74 65 73 74 TE_UTF8, 0, test
9c10: 5f 64 65 73 74 72 75 63 74 6f 72 7d 2c 0a 20 20 _destructor},.
9c20: 20 20 7b 20 22 74 65 73 74 5f 64 65 73 74 72 75 { "test_destru
9c30: 63 74 6f 72 5f 63 6f 75 6e 74 22 2c 20 30 2c 20 ctor_count", 0,
9c40: 30 2c 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 0, SQLITE_UTF
9c50: 38 2c 20 30 2c 20 74 65 73 74 5f 64 65 73 74 72 8, 0, test_destr
9c60: 75 63 74 6f 72 5f 63 6f 75 6e 74 7d 2c 0a 20 20 uctor_count},.
9c70: 20 20 7b 20 22 74 65 73 74 5f 61 75 78 64 61 74 { "test_auxdat
9c80: 61 22 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 a", -1,
9c90: 30 2c 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 0, SQLITE_UTF
9ca0: 38 2c 20 30 2c 20 74 65 73 74 5f 61 75 78 64 61 8, 0, test_auxda
9cb0: 74 61 7d 2c 0a 20 20 20 20 7b 20 22 74 65 73 74 ta},. { "test
9cc0: 5f 65 72 72 6f 72 22 2c 20 20 20 20 20 20 20 20 _error",
9cd0: 20 20 20 20 31 2c 20 30 2c 20 20 20 20 53 51 4c 1, 0, SQL
9ce0: 49 54 45 5f 55 54 46 38 2c 20 30 2c 20 74 65 73 ITE_UTF8, 0, tes
9cf0: 74 5f 65 72 72 6f 72 7d 2c 0a 23 65 6e 64 69 66 t_error},.#endif
9d00: 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 . };. static c
9d10: 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20 onst struct {.
9d20: 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 char *zName;.
9d30: 20 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 6e signed char n
9d40: 41 72 67 3b 0a 20 20 20 20 75 38 20 61 72 67 54 Arg;. u8 argT
9d50: 79 70 65 3b 0a 20 20 20 20 75 38 20 6e 65 65 64 ype;. u8 need
9d60: 43 6f 6c 6c 53 65 71 3b 0a 20 20 20 20 76 6f 69 CollSeq;. voi
9d70: 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 d (*xStep)(sqlit
9d80: 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c e3_context*,int,
9d90: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 sqlite3_value**)
9da0: 3b 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 46 69 ;. void (*xFi
9db0: 6e 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f nalize)(sqlite3_
9dc0: 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 7d 20 61 context*);. } a
9dd0: 41 67 67 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b Aggs[] = {. {
9de0: 20 22 6d 69 6e 22 2c 20 20 20 20 31 2c 20 30 2c "min", 1, 0,
9df0: 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 1, minmaxStep,
9e00: 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 minMaxFinalize
9e10: 20 7d 2c 0a 20 20 20 20 7b 20 22 6d 61 78 22 2c },. { "max",
9e20: 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 1, 1, 1, min
9e30: 6d 61 78 53 74 65 70 2c 20 20 20 6d 69 6e 4d 61 maxStep, minMa
9e40: 78 46 69 6e 61 6c 69 7a 65 20 7d 2c 0a 20 20 20 xFinalize },.
9e50: 20 7b 20 22 73 75 6d 22 2c 20 20 20 20 31 2c 20 { "sum", 1,
9e60: 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 0, 0, sumStep,
9e70: 20 20 20 20 73 75 6d 46 69 6e 61 6c 69 7a 65 20 sumFinalize
9e80: 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 74 6f 74 },. { "tot
9e90: 61 6c 22 2c 20 20 31 2c 20 30 2c 20 30 2c 20 73 al", 1, 0, 0, s
9ea0: 75 6d 53 74 65 70 2c 20 20 20 20 20 20 74 6f 74 umStep, tot
9eb0: 61 6c 46 69 6e 61 6c 69 7a 65 20 20 20 20 7d 2c alFinalize },
9ec0: 0a 20 20 20 20 7b 20 22 61 76 67 22 2c 20 20 20 . { "avg",
9ed0: 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 1, 0, 0, sumSte
9ee0: 70 2c 20 20 20 20 20 20 61 76 67 46 69 6e 61 6c p, avgFinal
9ef0: 69 7a 65 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 ize },. {
9f00: 22 63 6f 75 6e 74 22 2c 20 20 30 2c 20 30 2c 20 "count", 0, 0,
9f10: 30 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 0, countStep,
9f20: 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 countFinalize
9f30: 7d 2c 0a 20 20 20 20 7b 20 22 63 6f 75 6e 74 22 },. { "count"
9f40: 2c 20 20 31 2c 20 30 2c 20 30 2c 20 63 6f 75 6e , 1, 0, 0, coun
9f50: 74 53 74 65 70 2c 20 20 20 20 63 6f 75 6e 74 46 tStep, countF
9f60: 69 6e 61 6c 69 7a 65 20 20 7d 2c 0a 20 20 7d 3b inalize },. };
9f70: 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72 . int i;.. for
9f80: 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 (i=0; i<sizeof(a
9f90: 46 75 6e 63 73 29 2f 73 69 7a 65 6f 66 28 61 46 Funcs)/sizeof(aF
9fa0: 75 6e 63 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a uncs[0]); i++){.
9fb0: 20 20 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a void *pArg;.
9fc0: 20 20 20 20 75 38 20 61 72 67 54 79 70 65 20 3d u8 argType =
9fd0: 20 61 46 75 6e 63 73 5b 69 5d 2e 61 72 67 54 79 aFuncs[i].argTy
9fe0: 70 65 3b 0a 20 20 20 20 69 66 28 20 61 72 67 54 pe;. if( argT
9ff0: 79 70 65 3d 3d 30 78 66 66 20 29 7b 0a 20 20 20 ype==0xff ){.
a000: 20 20 20 70 41 72 67 20 3d 20 64 62 3b 0a 20 20 pArg = db;.
a010: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
a020: 41 72 67 20 3d 20 28 76 6f 69 64 2a 29 28 69 6e Arg = (void*)(in
a030: 74 29 61 72 67 54 79 70 65 3b 0a 20 20 20 20 7d t)argType;. }
a040: 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 . sqlite3Crea
a050: 74 65 46 75 6e 63 28 64 62 2c 20 61 46 75 6e 63 teFunc(db, aFunc
a060: 73 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 61 46 75 6e s[i].zName, aFun
a070: 63 73 5b 69 5d 2e 6e 41 72 67 2c 0a 20 20 20 20 cs[i].nArg,.
a080: 20 20 20 20 61 46 75 6e 63 73 5b 69 5d 2e 65 54 aFuncs[i].eT
a090: 65 78 74 52 65 70 2c 20 70 41 72 67 2c 20 61 46 extRep, pArg, aF
a0a0: 75 6e 63 73 5b 69 5d 2e 78 46 75 6e 63 2c 20 30 uncs[i].xFunc, 0
a0b0: 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 61 46 , 0);. if( aF
a0c0: 75 6e 63 73 5b 69 5d 2e 6e 65 65 64 43 6f 6c 6c uncs[i].needColl
a0d0: 53 65 71 20 29 7b 0a 20 20 20 20 20 20 46 75 6e Seq ){. Fun
a0e0: 63 44 65 66 20 2a 70 46 75 6e 63 20 3d 20 73 71 cDef *pFunc = sq
a0f0: 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f lite3FindFunctio
a100: 6e 28 64 62 2c 20 61 46 75 6e 63 73 5b 69 5d 2e n(db, aFuncs[i].
a110: 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 20 zName, .
a120: 20 20 73 74 72 6c 65 6e 28 61 46 75 6e 63 73 5b strlen(aFuncs[
a130: 69 5d 2e 7a 4e 61 6d 65 29 2c 20 61 46 75 6e 63 i].zName), aFunc
a140: 73 5b 69 5d 2e 6e 41 72 67 2c 20 61 46 75 6e 63 s[i].nArg, aFunc
a150: 73 5b 69 5d 2e 65 54 65 78 74 52 65 70 2c 20 30 s[i].eTextRep, 0
a160: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 46 75 );. if( pFu
a170: 6e 63 20 26 26 20 61 46 75 6e 63 73 5b 69 5d 2e nc && aFuncs[i].
a180: 6e 65 65 64 43 6f 6c 6c 53 65 71 20 29 7b 0a 20 needCollSeq ){.
a190: 20 20 20 20 20 20 20 70 46 75 6e 63 2d 3e 6e 65 pFunc->ne
a1a0: 65 64 43 6f 6c 6c 53 65 71 20 3d 20 31 3b 0a 20 edCollSeq = 1;.
a1b0: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
a1c0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a1d0: 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a OMIT_ALTERTABLE.
a1e0: 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 sqlite3AlterFu
a1f0: 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 23 65 6e nctions(db);.#en
a200: 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 dif.#ifndef SQLI
a210: 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45 52 0a 20 TE_OMIT_PARSER.
a220: 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 46 75 sqlite3AttachFu
a230: 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 23 65 6e nctions(db);.#en
a240: 64 69 66 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 dif. for(i=0; i
a250: 3c 73 69 7a 65 6f 66 28 61 41 67 67 73 29 2f 73 <sizeof(aAggs)/s
a260: 69 7a 65 6f 66 28 61 41 67 67 73 5b 30 5d 29 3b izeof(aAggs[0]);
a270: 20 69 2b 2b 29 7b 0a 20 20 20 20 76 6f 69 64 20 i++){. void
a280: 2a 70 41 72 67 20 3d 20 28 76 6f 69 64 2a 29 28 *pArg = (void*)(
a290: 69 6e 74 29 61 41 67 67 73 5b 69 5d 2e 61 72 67 int)aAggs[i].arg
a2a0: 54 79 70 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 Type;. sqlite
a2b0: 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 3CreateFunc(db,
a2c0: 61 41 67 67 73 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 aAggs[i].zName,
a2d0: 61 41 67 67 73 5b 69 5d 2e 6e 41 72 67 2c 20 53 aAggs[i].nArg, S
a2e0: 51 4c 49 54 45 5f 55 54 46 38 2c 20 0a 20 20 20 QLITE_UTF8, .
a2f0: 20 20 20 20 20 70 41 72 67 2c 20 30 2c 20 61 41 pArg, 0, aA
a300: 67 67 73 5b 69 5d 2e 78 53 74 65 70 2c 20 61 41 ggs[i].xStep, aA
a310: 67 67 73 5b 69 5d 2e 78 46 69 6e 61 6c 69 7a 65 ggs[i].xFinalize
a320: 29 3b 0a 20 20 20 20 69 66 28 20 61 41 67 67 73 );. if( aAggs
a330: 5b 69 5d 2e 6e 65 65 64 43 6f 6c 6c 53 65 71 20 [i].needCollSeq
a340: 29 7b 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 ){. FuncDef
a350: 20 2a 70 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 *pFunc = sqlite
a360: 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 20 64 3FindFunction( d
a370: 62 2c 20 61 41 67 67 73 5b 69 5d 2e 7a 4e 61 6d b, aAggs[i].zNam
a380: 65 2c 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 e,. str
a390: 6c 65 6e 28 61 41 67 67 73 5b 69 5d 2e 7a 4e 61 len(aAggs[i].zNa
a3a0: 6d 65 29 2c 20 61 41 67 67 73 5b 69 5d 2e 6e 41 me), aAggs[i].nA
a3b0: 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c rg, SQLITE_UTF8,
a3c0: 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 0);. if( p
a3d0: 46 75 6e 63 20 26 26 20 61 41 67 67 73 5b 69 5d Func && aAggs[i]
a3e0: 2e 6e 65 65 64 43 6f 6c 6c 53 65 71 20 29 7b 0a .needCollSeq ){.
a3f0: 20 20 20 20 20 20 20 20 70 46 75 6e 63 2d 3e 6e pFunc->n
a400: 65 65 64 43 6f 6c 6c 53 65 71 20 3d 20 31 3b 0a eedCollSeq = 1;.
a410: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
a420: 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 }. sqlite3Regis
a430: 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 terDateTimeFunct
a440: 69 6f 6e 73 28 64 62 29 3b 0a 20 20 69 66 28 20 ions(db);. if(
a450: 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 !db->mallocFaile
a460: 64 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 d ){. int rc
a470: 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f = sqlite3_overlo
a480: 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 ad_function(db,
a490: 22 4d 41 54 43 48 22 2c 20 32 29 3b 0a 20 20 20 "MATCH", 2);.
a4a0: 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
a4b0: 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d ITE_NOMEM || rc=
a4c0: 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
a4d0: 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
a4e0: 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 _NOMEM ){.
a4f0: 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
a500: 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 1;. }. }.
a510: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 #ifdef SQLITE_SS
a520: 45 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 E. (void)sqlite
a530: 33 53 73 65 46 75 6e 63 74 69 6f 6e 73 28 64 62 3SseFunctions(db
a540: 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 );.#endif.#ifdef
a550: 20 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e SQLITE_CASE_SEN
a560: 53 49 54 49 56 45 5f 4c 49 4b 45 0a 20 20 73 71 SITIVE_LIKE. sq
a570: 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b lite3RegisterLik
a580: 65 46 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20 31 eFunctions(db, 1
a590: 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 );.#else. sqlit
a5a0: 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 e3RegisterLikeFu
a5b0: 6e 63 74 69 6f 6e 73 28 64 62 2c 20 30 29 3b 0a nctions(db, 0);.
a5c0: 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
a5d0: 53 65 74 20 74 68 65 20 4c 49 4b 45 4f 50 54 20 Set the LIKEOPT
a5e0: 66 6c 61 67 20 6f 6e 20 74 68 65 20 32 2d 61 72 flag on the 2-ar
a5f0: 67 75 6d 65 6e 74 20 66 75 6e 63 74 69 6f 6e 20 gument function
a600: 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e with the given n
a610: 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ame..*/.static v
a620: 6f 69 64 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c oid setLikeOptFl
a630: 61 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ag(sqlite3 *db,
a640: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
a650: 65 2c 20 69 6e 74 20 66 6c 61 67 56 61 6c 29 7b e, int flagVal){
a660: 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 . FuncDef *pDef
a670: 3b 0a 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 ;. pDef = sqlit
a680: 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 e3FindFunction(d
a690: 62 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c 65 6e b, zName, strlen
a6a0: 28 7a 4e 61 6d 65 29 2c 20 32 2c 20 53 51 4c 49 (zName), 2, SQLI
a6b0: 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 TE_UTF8, 0);. i
a6c0: 66 28 20 70 44 65 66 20 29 7b 0a 20 20 20 20 70 f( pDef ){. p
a6d0: 44 65 66 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 Def->flags = fla
a6e0: 67 56 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a gVal;. }.}../*.
a6f0: 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20 ** Register the
a700: 62 75 69 6c 74 2d 69 6e 20 4c 49 4b 45 20 61 6e built-in LIKE an
a710: 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73 d GLOB functions
a720: 2e 20 20 54 68 65 20 63 61 73 65 53 65 6e 73 69 . The caseSensi
a730: 74 69 76 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 tive.** paramete
a740: 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 r determines whe
a750: 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
a760: 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 69 73 LIKE operator is
a770: 20 63 61 73 65 0a 2a 2a 20 73 65 6e 73 69 74 69 case.** sensiti
a780: 76 65 2e 20 20 47 4c 4f 42 20 69 73 20 61 6c 77 ve. GLOB is alw
a790: 61 79 73 20 63 61 73 65 20 73 65 6e 73 69 74 69 ays case sensiti
a7a0: 76 65 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 ve..*/.void sqli
a7b0: 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 te3RegisterLikeF
a7c0: 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 unctions(sqlite3
a7d0: 20 2a 64 62 2c 20 69 6e 74 20 63 61 73 65 53 65 *db, int caseSe
a7e0: 6e 73 69 74 69 76 65 29 7b 0a 20 20 73 74 72 75 nsitive){. stru
a7f0: 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a ct compareInfo *
a800: 70 49 6e 66 6f 3b 0a 20 20 69 66 28 20 63 61 73 pInfo;. if( cas
a810: 65 53 65 6e 73 69 74 69 76 65 20 29 7b 0a 20 20 eSensitive ){.
a820: 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 pInfo = (struc
a830: 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 t compareInfo*)&
a840: 6c 69 6b 65 49 6e 66 6f 41 6c 74 3b 0a 20 20 7d likeInfoAlt;. }
a850: 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 20 else{. pInfo
a860: 3d 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 = (struct compar
a870: 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f eInfo*)&likeInfo
a880: 4e 6f 72 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 Norm;. }. sqli
a890: 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 te3CreateFunc(db
a8a0: 2c 20 22 6c 69 6b 65 22 2c 20 32 2c 20 53 51 4c , "like", 2, SQL
a8b0: 49 54 45 5f 55 54 46 38 2c 20 70 49 6e 66 6f 2c ITE_UTF8, pInfo,
a8c0: 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 29 likeFunc, 0, 0)
a8d0: 3b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 ;. sqlite3Creat
a8e0: 65 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 eFunc(db, "like"
a8f0: 2c 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 , 3, SQLITE_UTF8
a900: 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e , pInfo, likeFun
a910: 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 c, 0, 0);. sqli
a920: 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 te3CreateFunc(db
a930: 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20 53 51 4c , "glob", 2, SQL
a940: 49 54 45 5f 55 54 46 38 2c 20 0a 20 20 20 20 20 ITE_UTF8, .
a950: 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 (struct compare
a960: 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c Info*)&globInfo,
a970: 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 30 29 3b likeFunc, 0,0);
a980: 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 . setLikeOptFla
a990: 67 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 53 51 g(db, "glob", SQ
a9a0: 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c LITE_FUNC_LIKE |
a9b0: 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 SQLITE_FUNC_CAS
a9c0: 45 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 E);. setLikeOpt
a9d0: 46 6c 61 67 28 64 62 2c 20 22 6c 69 6b 65 22 2c Flag(db, "like",
a9e0: 20 0a 20 20 20 20 20 20 63 61 73 65 53 65 6e 73 . caseSens
a9f0: 69 74 69 76 65 20 3f 20 28 53 51 4c 49 54 45 5f itive ? (SQLITE_
aa00: 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 FUNC_LIKE | SQLI
aa10: 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 20 3a 20 TE_FUNC_CASE) :
aa20: 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 SQLITE_FUNC_LIKE
aa30: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 );.}../*.** pExp
aa40: 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 r points to an e
aa50: 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 xpression which
aa60: 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e implements a fun
aa70: 63 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 69 74 ction. If.** it
aa80: 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 is appropriate
aa90: 74 6f 20 61 70 70 6c 79 20 74 68 65 20 4c 49 4b to apply the LIK
aaa0: 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 74 E optimization t
aab0: 6f 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 0a o that function.
aac0: 2a 2a 20 74 68 65 6e 20 73 65 74 20 61 57 63 5b ** then set aWc[
aad0: 30 5d 20 74 68 72 6f 75 67 68 20 61 57 63 5b 32 0] through aWc[2
aae0: 5d 20 74 6f 20 74 68 65 20 77 69 6c 64 63 61 72 ] to the wildcar
aaf0: 64 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 d characters and
ab00: 0a 2a 2a 20 72 65 74 75 72 6e 20 54 52 55 45 2e .** return TRUE.
ab10: 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f If the functio
ab20: 6e 20 69 73 20 6e 6f 74 20 61 20 4c 49 4b 45 2d n is not a LIKE-
ab30: 73 74 79 6c 65 20 66 75 6e 63 74 69 6f 6e 20 74 style function t
ab40: 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 46 41 hen.** return FA
ab50: 4c 53 45 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 LSE..*/.int sqli
ab60: 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f te3IsLikeFunctio
ab70: 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 n(sqlite3 *db, E
ab80: 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
ab90: 2a 70 49 73 4e 6f 63 61 73 65 2c 20 63 68 61 72 *pIsNocase, char
aba0: 20 2a 61 57 63 29 7b 0a 20 20 46 75 6e 63 44 65 *aWc){. FuncDe
abb0: 66 20 2a 70 44 65 66 3b 0a 20 20 69 66 28 20 70 f *pDef;. if( p
abc0: 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e Expr->op!=TK_FUN
abd0: 43 54 49 4f 4e 20 7c 7c 20 21 70 45 78 70 72 2d CTION || !pExpr-
abe0: 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 72 65 >pList ){. re
abf0: 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 turn 0;. }. if
ac00: 28 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e ( pExpr->pList->
ac10: 6e 45 78 70 72 21 3d 32 20 29 7b 0a 20 20 20 20 nExpr!=2 ){.
ac20: 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
ac30: 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 pDef = sqlite3Fi
ac40: 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 ndFunction(db, (
ac50: 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b char*)pExpr->tok
ac60: 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b en.z, pExpr->tok
ac70: 65 6e 2e 6e 2c 20 32 2c 0a 20 20 20 20 20 20 20 en.n, 2,.
ac80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ac90: 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 SQLITE_UTF
aca0: 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 44 65 8, 0);. if( pDe
acb0: 66 3d 3d 30 20 7c 7c 20 28 70 44 65 66 2d 3e 66 f==0 || (pDef->f
acc0: 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
acd0: 4e 43 5f 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a 20 NC_LIKE)==0 ){.
ace0: 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
acf0: 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 65 6d 63 70 .. /* The memcp
ad00: 79 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61 73 y() statement as
ad10: 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 77 sumes that the w
ad20: 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65 ildcard characte
ad30: 72 73 20 61 72 65 0a 20 20 2a 2a 20 74 68 65 20 rs are. ** the
ad40: 66 69 72 73 74 20 74 68 72 65 65 20 73 74 61 74 first three stat
ad50: 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f ements in the co
ad60: 6d 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74 mpareInfo struct
ad70: 75 72 65 2e 20 20 54 68 65 0a 20 20 2a 2a 20 61 ure. The. ** a
ad80: 73 73 65 72 74 73 28 29 20 74 68 61 74 20 66 6f sserts() that fo
ad90: 6c 6c 6f 77 20 76 65 72 69 66 79 20 74 68 61 74 llow verify that
ada0: 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f assumption. */
adb0: 0a 20 20 6d 65 6d 63 70 79 28 61 57 63 2c 20 70 . memcpy(aWc, p
adc0: 44 65 66 2d 3e 70 55 73 65 72 44 61 74 61 2c 20 Def->pUserData,
add0: 33 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 3);. assert( (c
ade0: 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c har*)&likeInfoAl
adf0: 74 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b t == (char*)&lik
ae00: 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 41 6c eInfoAlt.matchAl
ae10: 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26 l );. assert( &
ae20: 28 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 ((char*)&likeInf
ae30: 6f 41 6c 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61 oAlt)[1] == (cha
ae40: 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e r*)&likeInfoAlt.
ae50: 6d 61 74 63 68 4f 6e 65 20 29 3b 0a 20 20 61 73 matchOne );. as
ae60: 73 65 72 74 28 20 26 28 28 63 68 61 72 2a 29 26 sert( &((char*)&
ae70: 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20 likeInfoAlt)[2]
ae80: 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 == (char*)&likeI
ae90: 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 53 65 74 20 nfoAlt.matchSet
aea0: 29 3b 0a 20 20 2a 70 49 73 4e 6f 63 61 73 65 20 );. *pIsNocase
aeb0: 3d 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 = (pDef->flags &
aec0: 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 SQLITE_FUNC_CAS
aed0: 45 29 3d 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 E)==0;. return
aee0: 31 3b 0a 7d 0a 1;.}.