0000: 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 /*.** 2005 May 2
0010: 33 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 3 .**.** The aut
0020: 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
0030: 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
0040: 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
0050: 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
0060: 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
0070: 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
0080: 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
0090: 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
00a0: 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
00b0: 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
00c0: 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
00d0: 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
00e0: 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
00f0: 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
0100: 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
0110: 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
0120: 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
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 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
0180: 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 e contains funct
0190: 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 61 63 63 ions used to acc
01a0: 65 73 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ess the internal
01b0: 20 68 61 73 68 20 74 61 62 6c 65 73 0a 2a 2a 20 hash tables.**
01c0: 6f 66 20 75 73 65 72 20 64 65 66 69 6e 65 64 20 of user defined
01d0: 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 63 6f functions and co
01e0: 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
01f0: 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63 61 s..**.** $Id: ca
0200: 6c 6c 62 61 63 6b 2e 63 2c 76 20 31 2e 37 20 32 llback.c,v 1.7 2
0210: 30 30 36 2f 30 31 2f 31 30 20 31 38 3a 34 30 3a 006/01/10 18:40:
0220: 33 37 20 72 6d 73 69 6d 70 73 6f 6e 20 45 78 70 37 rmsimpson Exp
0230: 20 24 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 $.*/..#include
0240: 22 73 71 6c 69 74 65 49 6e 74 2e 68 22 0a 0a 2f "sqliteInt.h"../
0250: 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 *.** Invoke the
0260: 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 65 'collation neede
0270: 64 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 72 d' callback to r
0280: 65 71 75 65 73 74 20 61 20 63 6f 6c 6c 61 74 69 equest a collati
0290: 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 69 on sequence.** i
02a0: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 n the database t
02b0: 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 ext encoding of
02c0: 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 name zName, leng
02d0: 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 20 49 66 20 th nName..** If
02e0: 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 the collation se
02f0: 71 75 65 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 quence.*/.static
0300: 20 76 6f 69 64 20 63 61 6c 6c 43 6f 6c 6c 4e 65 void callCollNe
0310: 65 64 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 eded(sqlite3 *db
0320: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e , const char *zN
0330: 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 7b ame, int nName){
0340: 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e . assert( !db->
0350: 78 43 6f 6c 6c 4e 65 65 64 65 64 20 7c 7c 20 21 xCollNeeded || !
0360: 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 db->xCollNeeded1
0370: 36 20 29 3b 0a 20 20 69 66 28 20 6e 4e 61 6d 65 6 );. if( nName
0380: 3c 30 20 29 20 6e 4e 61 6d 65 20 3d 20 73 74 72 <0 ) nName = str
0390: 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 len(zName);. if
03a0: 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 ( db->xCollNeede
03b0: 64 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a d ){. char *z
03c0: 45 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74 External = sqlit
03d0: 65 53 74 72 4e 44 75 70 28 7a 4e 61 6d 65 2c 20 eStrNDup(zName,
03e0: 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 nName);. if(
03f0: 21 7a 45 78 74 65 72 6e 61 6c 20 29 20 72 65 74 !zExternal ) ret
0400: 75 72 6e 3b 0a 20 20 20 20 64 62 2d 3e 78 43 6f urn;. db->xCo
0410: 6c 6c 4e 65 65 64 65 64 28 64 62 2d 3e 70 43 6f llNeeded(db->pCo
0420: 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 64 62 2c llNeededArg, db,
0430: 20 28 69 6e 74 29 45 4e 43 28 64 62 29 2c 20 7a (int)ENC(db), z
0440: 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20 73 External);. s
0450: 71 6c 69 74 65 46 72 65 65 28 7a 45 78 74 65 72 qliteFree(zExter
0460: 6e 61 6c 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 nal);. }.#ifnde
0470: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
0480: 46 31 36 0a 20 20 69 66 28 20 64 62 2d 3e 78 43 F16. if( db->xC
0490: 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29 7b 0a 20 ollNeeded16 ){.
04a0: 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a char const *z
04b0: 45 78 74 65 72 6e 61 6c 3b 0a 20 20 20 20 73 71 External;. sq
04c0: 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 54 6d lite3_value *pTm
04d0: 70 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 p = sqlite3Value
04e0: 4e 65 77 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 New();. sqlit
04f0: 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 54 e3ValueSetStr(pT
0500: 6d 70 2c 20 6e 4e 61 6d 65 2c 20 7a 4e 61 6d 65 mp, nName, zName
0510: 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
0520: 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
0530: 20 20 20 7a 45 78 74 65 72 6e 61 6c 20 3d 20 73 zExternal = s
0540: 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
0550: 70 54 6d 70 2c 20 53 51 4c 49 54 45 5f 55 54 46 pTmp, SQLITE_UTF
0560: 31 36 4e 41 54 49 56 45 29 3b 0a 20 20 20 20 69 16NATIVE);. i
0570: 66 28 20 7a 45 78 74 65 72 6e 61 6c 20 29 7b 0a f( zExternal ){.
0580: 20 20 20 20 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e db->xCollN
0590: 65 65 64 65 64 31 36 28 64 62 2d 3e 70 43 6f 6c eeded16(db->pCol
05a0: 6c 4e 65 65 64 65 64 41 72 67 2c 20 64 62 2c 20 lNeededArg, db,
05b0: 28 69 6e 74 29 45 4e 43 28 64 62 29 2c 20 7a 45 (int)ENC(db), zE
05c0: 78 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a xternal);. }.
05d0: 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 sqlite3Value
05e0: 46 72 65 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a Free(pTmp);. }.
05f0: 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
0600: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
0610: 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20 63 6f called if the co
0620: 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 llation factory
0630: 66 61 69 6c 73 20 74 6f 20 64 65 6c 69 76 65 72 fails to deliver
0640: 20 61 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 a.** collation
0650: 66 75 6e 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 function in the
0660: 62 65 73 74 20 65 6e 63 6f 64 69 6e 67 20 62 75 best encoding bu
0670: 74 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 6f t there may be o
0680: 74 68 65 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a ther versions.**
0690: 20 6f 66 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 of this collati
06a0: 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 on function (for
06b0: 20 6f 74 68 65 72 20 74 65 78 74 20 65 6e 63 6f other text enco
06c0: 64 69 6e 67 73 29 20 61 76 61 69 6c 61 62 6c 65 dings) available
06d0: 2e 20 55 73 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 . Use one.** of
06e0: 74 68 65 73 65 20 69 6e 73 74 65 61 64 20 69 66 these instead if
06f0: 20 74 68 65 79 20 65 78 69 73 74 2e 20 41 76 6f they exist. Avo
0700: 69 64 20 61 20 55 54 46 2d 38 20 3c 2d 3e 20 55 id a UTF-8 <-> U
0710: 54 46 2d 31 36 20 63 6f 6e 76 65 72 73 69 6f 6e TF-16 conversion
0720: 20 69 66 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 2e if.** possible.
0730: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
0740: 79 6e 74 68 43 6f 6c 6c 53 65 71 28 73 71 6c 69 ynthCollSeq(sqli
0750: 74 65 33 20 2a 64 62 2c 20 43 6f 6c 6c 53 65 71 te3 *db, CollSeq
0760: 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 43 6f 6c 6c *pColl){. Coll
0770: 53 65 71 20 2a 70 43 6f 6c 6c 32 3b 0a 20 20 63 Seq *pColl2;. c
0780: 68 61 72 20 2a 7a 20 3d 20 70 43 6f 6c 6c 2d 3e har *z = pColl->
0790: 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 20 3d zName;. int n =
07a0: 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 69 6e strlen(z);. in
07b0: 74 20 69 3b 0a 20 20 73 74 61 74 69 63 20 63 6f t i;. static co
07c0: 6e 73 74 20 75 38 20 61 45 6e 63 5b 5d 20 3d 20 nst u8 aEnc[] =
07d0: 7b 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 { SQLITE_UTF16BE
07e0: 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 , SQLITE_UTF16LE
07f0: 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 20 7d 3b , SQLITE_UTF8 };
0800: 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 33 3b . for(i=0; i<3;
0810: 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 6f 6c 6c i++){. pColl
0820: 32 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 2 = sqlite3FindC
0830: 6f 6c 6c 53 65 71 28 64 62 2c 20 61 45 6e 63 5b ollSeq(db, aEnc[
0840: 69 5d 2c 20 7a 2c 20 6e 2c 20 30 29 3b 0a 20 20 i], z, n, 0);.
0850: 20 20 69 66 28 20 70 43 6f 6c 6c 32 2d 3e 78 43 if( pColl2->xC
0860: 6d 70 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d mp!=0 ){. m
0870: 65 6d 63 70 79 28 70 43 6f 6c 6c 2c 20 70 43 6f emcpy(pColl, pCo
0880: 6c 6c 32 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 6c ll2, sizeof(Coll
0890: 53 65 71 29 29 3b 0a 20 20 20 20 20 20 72 65 74 Seq));. ret
08a0: 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
08b0: 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
08c0: 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
08d0: 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
08e0: 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e nction is respon
08f0: 73 69 62 6c 65 20 66 6f 72 20 69 6e 76 6f 6b 69 sible for invoki
0900: 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e ng the collation
0910: 20 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 factory callbac
0920: 6b 0a 2a 2a 20 6f 72 20 73 75 62 73 74 69 74 75 k.** or substitu
0930: 74 69 6e 67 20 61 20 63 6f 6c 6c 61 74 69 6f 6e ting a collation
0940: 20 73 65 71 75 65 6e 63 65 20 6f 66 20 61 20 64 sequence of a d
0950: 69 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e ifferent encodin
0960: 67 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 72 65 g when the.** re
0970: 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f quested collatio
0980: 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f n sequence is no
0990: 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 t available in t
09a0: 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69 he database nati
09b0: 76 65 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2e 0a ve.** encoding..
09c0: 2a 2a 20 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 ** .** If it is
09d0: 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 not NULL, then p
09e0: 43 6f 6c 6c 20 6d 75 73 74 20 70 6f 69 6e 74 20 Coll must point
09f0: 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
0a00: 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 native encoding
0a10: 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 .** collation se
0a20: 71 75 65 6e 63 65 20 77 69 74 68 20 6e 61 6d 65 quence with name
0a30: 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e zName, length n
0a40: 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 Name..**.** The
0a50: 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 return value is
0a60: 65 69 74 68 65 72 20 74 68 65 20 63 6f 6c 6c 61 either the colla
0a70: 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 74 6f tion sequence to
0a80: 20 62 65 20 75 73 65 64 20 69 6e 20 64 61 74 61 be used in data
0a90: 62 61 73 65 0a 2a 2a 20 64 62 20 66 6f 72 20 63 base.** db for c
0aa0: 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6e 61 ollation type na
0ab0: 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 me zName, length
0ac0: 20 6e 4e 61 6d 65 2c 20 6f 72 20 4e 55 4c 4c 2c nName, or NULL,
0ad0: 20 69 66 20 6e 6f 20 63 6f 6c 6c 61 74 69 6f 6e if no collation
0ae0: 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 63 61 6e .** sequence can
0af0: 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 43 6f be found..*/.Co
0b00: 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 llSeq *sqlite3Ge
0b10: 74 43 6f 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 tCollSeq(. sqli
0b20: 74 65 33 2a 20 64 62 2c 20 0a 20 20 43 6f 6c 6c te3* db, . Coll
0b30: 53 65 71 20 2a 70 43 6f 6c 6c 2c 20 0a 20 20 63 Seq *pColl, . c
0b40: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
0b50: 2c 20 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 0a 29 , . int nName.)
0b60: 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 3b 0a {. CollSeq *p;.
0b70: 0a 20 20 70 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 . p = pColl;.
0b80: 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 70 20 if( !p ){. p
0b90: 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c = sqlite3FindCol
0ba0: 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 lSeq(db, ENC(db)
0bb0: 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 , zName, nName,
0bc0: 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 0);. }. if( !p
0bd0: 20 7c 7c 20 21 70 2d 3e 78 43 6d 70 20 29 7b 0a || !p->xCmp ){.
0be0: 20 20 20 20 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 /* No collat
0bf0: 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20 ion sequence of
0c00: 74 68 69 73 20 74 79 70 65 20 66 6f 72 20 74 68 this type for th
0c10: 69 73 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 is encoding is r
0c20: 65 67 69 73 74 65 72 65 64 2e 0a 20 20 20 20 2a egistered.. *
0c30: 2a 20 43 61 6c 6c 20 74 68 65 20 63 6f 6c 6c 61 * Call the colla
0c40: 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 74 6f 20 tion factory to
0c50: 73 65 65 20 69 66 20 69 74 20 63 61 6e 20 73 75 see if it can su
0c60: 70 70 6c 79 20 75 73 20 77 69 74 68 20 6f 6e 65 pply us with one
0c70: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 6c .. */. cal
0c80: 6c 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2c 20 lCollNeeded(db,
0c90: 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 zName, nName);.
0ca0: 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 p = sqlite3Fi
0cb0: 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e ndCollSeq(db, EN
0cc0: 43 28 64 62 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e C(db), zName, nN
0cd0: 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 ame, 0);. }. i
0ce0: 66 28 20 70 20 26 26 20 21 70 2d 3e 78 43 6d 70 f( p && !p->xCmp
0cf0: 20 26 26 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 && synthCollSeq
0d00: 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 70 (db, p) ){. p
0d10: 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 = 0;. }. asse
0d20: 72 74 28 20 21 70 20 7c 7c 20 70 2d 3e 78 43 6d rt( !p || p->xCm
0d30: 70 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b p );. return p;
0d40: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
0d50: 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
0d60: 20 6f 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 on a collation
0d70: 73 65 71 75 65 6e 63 65 20 62 65 66 6f 72 65 20 sequence before
0d80: 69 74 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a it is used to.**
0d90: 20 63 68 65 63 6b 20 74 68 61 74 20 69 74 20 69 check that it i
0da0: 73 20 64 65 66 69 6e 65 64 2e 20 41 6e 20 75 6e s defined. An un
0db0: 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f defined collatio
0dc0: 6e 20 73 65 71 75 65 6e 63 65 20 65 78 69 73 74 n sequence exist
0dd0: 73 20 77 68 65 6e 0a 2a 2a 20 61 20 64 61 74 61 s when.** a data
0de0: 62 61 73 65 20 69 73 20 6c 6f 61 64 65 64 20 74 base is loaded t
0df0: 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 72 65 66 hat contains ref
0e00: 65 72 65 6e 63 65 73 20 74 6f 20 63 6f 6c 6c 61 erences to colla
0e10: 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 0a 2a tion sequences.*
0e20: 2a 20 74 68 61 74 20 68 61 76 65 20 6e 6f 74 20 * that have not
0e30: 62 65 65 6e 20 64 65 66 69 6e 65 64 20 62 79 20 been defined by
0e40: 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 sqlite3_create_c
0e50: 6f 6c 6c 61 74 69 6f 6e 28 29 20 65 74 63 2e 0a ollation() etc..
0e60: 2a 2a 0a 2a 2a 20 49 66 20 72 65 71 75 69 72 65 **.** If require
0e70: 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 d, this routine
0e80: 63 61 6c 6c 73 20 74 68 65 20 27 63 6f 6c 6c 61 calls the 'colla
0e90: 74 69 6f 6e 20 6e 65 65 64 65 64 27 20 63 61 6c tion needed' cal
0ea0: 6c 62 61 63 6b 20 74 6f 0a 2a 2a 20 72 65 71 75 lback to.** requ
0eb0: 65 73 74 20 61 20 64 65 66 69 6e 69 74 69 6f 6e est a definition
0ec0: 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e of the collatin
0ed0: 67 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20 74 g sequence. If t
0ee0: 68 69 73 20 64 6f 65 73 6e 27 74 20 77 6f 72 6b his doesn't work
0ef0: 2c 20 0a 2a 2a 20 61 6e 20 65 71 75 69 76 61 6c , .** an equival
0f00: 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 ent collating se
0f10: 71 75 65 6e 63 65 20 74 68 61 74 20 75 73 65 73 quence that uses
0f20: 20 61 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 a text encoding
0f30: 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 72 different.** fr
0f40: 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 om the main data
0f50: 62 61 73 65 20 69 73 20 73 75 62 73 74 69 74 75 base is substitu
0f60: 74 65 64 2c 20 69 66 20 6f 6e 65 20 69 73 20 61 ted, if one is a
0f70: 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 69 6e 74 vailable..*/.int
0f80: 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c sqlite3CheckCol
0f90: 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 lSeq(Parse *pPar
0fa0: 73 65 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f se, CollSeq *pCo
0fb0: 6c 6c 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c ll){. if( pColl
0fc0: 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 ){. const ch
0fd0: 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 43 6f 6c ar *zName = pCol
0fe0: 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 43 6f l->zName;. Co
0ff0: 6c 6c 53 65 71 20 2a 70 20 3d 20 73 71 6c 69 74 llSeq *p = sqlit
1000: 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 70 50 61 e3GetCollSeq(pPa
1010: 72 73 65 2d 3e 64 62 2c 20 70 43 6f 6c 6c 2c 20 rse->db, pColl,
1020: 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 20 20 zName, -1);.
1030: 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 if( !p ){.
1040: 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 if( pParse->nErr
1050: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
1060: 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1070: 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 Parse, "no such
1080: 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
1090: 63 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b ce: %s", zName);
10a0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 . }. p
10b0: 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 Parse->nErr++;.
10c0: 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
10d0: 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a TE_ERROR;. }.
10e0: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
10f0: 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a ITE_OK;.}..../*.
1100: 2a 2a 20 4c 6f 63 61 74 65 20 61 6e 64 20 72 65 ** Locate and re
1110: 74 75 72 6e 20 61 6e 20 65 6e 74 72 79 20 66 72 turn an entry fr
1120: 6f 6d 20 74 68 65 20 64 62 2e 61 43 6f 6c 6c 53 om the db.aCollS
1130: 65 71 20 68 61 73 68 20 74 61 62 6c 65 2e 20 49 eq hash table. I
1140: 66 20 74 68 65 20 65 6e 74 72 79 0a 2a 2a 20 73 f the entry.** s
1150: 70 65 63 69 66 69 65 64 20 62 79 20 7a 4e 61 6d pecified by zNam
1160: 65 20 61 6e 64 20 6e 4e 61 6d 65 20 69 73 20 6e e and nName is n
1170: 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 70 61 72 ot found and par
1180: 61 6d 65 74 65 72 20 27 63 72 65 61 74 65 27 20 ameter 'create'
1190: 69 73 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 6e is.** true, then
11a0: 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 65 6e create a new en
11b0: 74 72 79 2e 20 4f 74 68 65 72 77 69 73 65 20 72 try. Otherwise r
11c0: 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a eturn NULL..**.*
11d0: 2a 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 73 * Each pointer s
11e0: 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c tored in the sql
11f0: 69 74 65 33 2e 61 43 6f 6c 6c 53 65 71 20 68 61 ite3.aCollSeq ha
1200: 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e sh table contain
1210: 73 20 61 6e 0a 2a 2a 20 61 72 72 61 79 20 6f 66 s an.** array of
1220: 20 74 68 72 65 65 20 43 6f 6c 6c 53 65 71 20 73 three CollSeq s
1230: 74 72 75 63 74 75 72 65 73 2e 20 54 68 65 20 66 tructures. The f
1240: 69 72 73 74 20 69 73 20 74 68 65 20 63 6f 6c 6c irst is the coll
1250: 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a ation sequence.*
1260: 2a 20 70 72 65 66 66 65 72 72 65 64 20 66 6f 72 * prefferred for
1270: 20 55 54 46 2d 38 2c 20 74 68 65 20 73 65 63 6f UTF-8, the seco
1280: 6e 64 20 55 54 46 2d 31 36 6c 65 2c 20 61 6e 64 nd UTF-16le, and
1290: 20 74 68 65 20 74 68 69 72 64 20 55 54 46 2d 31 the third UTF-1
12a0: 36 62 65 2e 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 6be..**.** Store
12b0: 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 d immediately af
12c0: 74 65 72 20 74 68 65 20 74 68 72 65 65 20 63 6f ter the three co
12d0: 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
12e0: 73 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a s is a copy of.*
12f0: 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 * the collation
1300: 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 2e 20 41 sequence name. A
1310: 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 pointer to this
1320: 20 73 74 72 69 6e 67 20 69 73 20 73 74 6f 72 65 string is store
1330: 64 20 69 6e 0a 2a 2a 20 65 61 63 68 20 63 6f 6c d in.** each col
1340: 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
1350: 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 structure..*/.st
1360: 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 66 69 atic CollSeq *fi
1370: 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28 0a ndCollSeqEntry(.
1380: 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 sqlite3 *db,.
1390: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
13a0: 6d 65 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c me,. int nName,
13b0: 0a 20 20 69 6e 74 20 63 72 65 61 74 65 0a 29 7b . int create.){
13c0: 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c . CollSeq *pCol
13d0: 6c 3b 0a 20 20 69 66 28 20 6e 4e 61 6d 65 3c 30 l;. if( nName<0
13e0: 20 29 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 ) nName = strle
13f0: 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c n(zName);. pCol
1400: 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 l = sqlite3HashF
1410: 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 ind(&db->aCollSe
1420: 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 q, zName, nName)
1430: 3b 0a 0a 20 20 69 66 28 20 30 3d 3d 70 43 6f 6c ;.. if( 0==pCol
1440: 6c 20 26 26 20 63 72 65 61 74 65 20 29 7b 0a 20 l && create ){.
1450: 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 pColl = sqlit
1460: 65 4d 61 6c 6c 6f 63 28 20 33 2a 73 69 7a 65 6f eMalloc( 3*sizeo
1470: 66 28 2a 70 43 6f 6c 6c 29 20 2b 20 6e 4e 61 6d f(*pColl) + nNam
1480: 65 20 2b 20 31 20 29 3b 0a 20 20 20 20 69 66 28 e + 1 );. if(
1490: 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 pColl ){.
14a0: 43 6f 6c 6c 53 65 71 20 2a 70 44 65 6c 20 3d 20 CollSeq *pDel =
14b0: 30 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 0;. pColl[0
14c0: 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a ].zName = (char*
14d0: 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 )&pColl[3];.
14e0: 20 20 70 43 6f 6c 6c 5b 30 5d 2e 65 6e 63 20 3d pColl[0].enc =
14f0: 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 SQLITE_UTF8;.
1500: 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e 7a 4e 61 pColl[1].zNa
1510: 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f me = (char*)&pCo
1520: 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f ll[3];. pCo
1530: 6c 6c 5b 31 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 ll[1].enc = SQLI
1540: 54 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 TE_UTF16LE;.
1550: 20 20 70 43 6f 6c 6c 5b 32 5d 2e 7a 4e 61 6d 65 pColl[2].zName
1560: 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c = (char*)&pColl
1570: 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c [3];. pColl
1580: 5b 32 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 [2].enc = SQLITE
1590: 5f 55 54 46 31 36 42 45 3b 0a 20 20 20 20 20 20 _UTF16BE;.
15a0: 6d 65 6d 63 70 79 28 70 43 6f 6c 6c 5b 30 5d 2e memcpy(pColl[0].
15b0: 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e zName, zName, nN
15c0: 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c ame);. pCol
15d0: 6c 5b 30 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 l[0].zName[nName
15e0: 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 44 65 ] = 0;. pDe
15f0: 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 l = sqlite3HashI
1600: 6e 73 65 72 74 28 26 64 62 2d 3e 61 43 6f 6c 6c nsert(&db->aColl
1610: 53 65 71 2c 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e Seq, pColl[0].zN
1620: 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 43 6f 6c ame, nName, pCol
1630: 6c 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 l);.. /* If
1640: 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c a malloc() fail
1650: 75 72 65 20 6f 63 63 75 72 65 64 20 69 6e 20 73 ure occured in s
1660: 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 qlite3HashInsert
1670: 28 29 2c 20 69 74 20 77 69 6c 6c 20 0a 20 20 20 (), it will .
1680: 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 ** return the
1690: 20 70 43 6f 6c 6c 20 70 6f 69 6e 74 65 72 20 74 pColl pointer t
16a0: 6f 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 65 o be deleted (be
16b0: 63 61 75 73 65 20 69 74 20 77 61 73 6e 27 74 20 cause it wasn't
16c0: 61 64 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 added. ** t
16d0: 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 o the hash table
16e0: 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ).. */.
16f0: 20 20 61 73 73 65 72 74 28 20 21 70 44 65 6c 20 assert( !pDel
1700: 7c 7c 20 28 73 71 6c 69 74 65 33 54 68 72 65 61 || (sqlite3Threa
1710: 64 44 61 74 61 28 29 2d 3e 6d 61 6c 6c 6f 63 46 dData()->mallocF
1720: 61 69 6c 65 64 20 26 26 20 70 44 65 6c 3d 3d 70 ailed && pDel==p
1730: 43 6f 6c 6c 29 20 29 3b 0a 20 20 20 20 20 20 73 Coll) );. s
1740: 71 6c 69 74 65 46 72 65 65 28 70 44 65 6c 29 3b qliteFree(pDel);
1750: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
1760: 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a urn pColl;.}../*
1770: 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 4e .** Parameter zN
1780: 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 ame points to a
1790: 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 73 74 UTF-8 encoded st
17a0: 72 69 6e 67 20 6e 4e 61 6d 65 20 62 79 74 65 73 ring nName bytes
17b0: 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 75 72 6e long..** Return
17c0: 20 74 68 65 20 43 6f 6c 6c 53 65 71 2a 20 70 6f the CollSeq* po
17d0: 69 6e 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f inter for the co
17e0: 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
17f0: 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 0a 2a 2a 20 named zName.**
1800: 66 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 for the encoding
1810: 20 27 65 6e 63 27 20 66 72 6f 6d 20 74 68 65 20 'enc' from the
1820: 64 61 74 61 62 61 73 65 20 27 64 62 27 2e 0a 2a database 'db'..*
1830: 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 6e 74 72 *.** If the entr
1840: 79 20 73 70 65 63 69 66 69 65 64 20 69 73 20 6e y specified is n
1850: 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 27 63 72 ot found and 'cr
1860: 65 61 74 65 27 20 69 73 20 74 72 75 65 2c 20 74 eate' is true, t
1870: 68 65 6e 20 63 72 65 61 74 65 20 61 0a 2a 2a 20 hen create a.**
1880: 6e 65 77 20 65 6e 74 72 79 2e 20 20 4f 74 68 65 new entry. Othe
1890: 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c rwise return NUL
18a0: 4c 2e 0a 2a 2f 0a 43 6f 6c 6c 53 65 71 20 2a 73 L..*/.CollSeq *s
18b0: 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 qlite3FindCollSe
18c0: 71 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 q(. sqlite3 *db
18d0: 2c 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20 63 6f ,. u8 enc,. co
18e0: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
18f0: 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 0a 20 20 . int nName,.
1900: 69 6e 74 20 63 72 65 61 74 65 0a 29 7b 0a 20 20 int create.){.
1910: 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d CollSeq *pColl =
1920: 20 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 findCollSeqEntr
1930: 79 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 y(db, zName, nNa
1940: 6d 65 2c 20 63 72 65 61 74 65 29 3b 0a 20 20 61 me, create);. a
1950: 73 73 65 72 74 28 20 53 51 4c 49 54 45 5f 55 54 ssert( SQLITE_UT
1960: 46 38 3d 3d 31 20 26 26 20 53 51 4c 49 54 45 5f F8==1 && SQLITE_
1970: 55 54 46 31 36 4c 45 3d 3d 32 20 26 26 20 53 51 UTF16LE==2 && SQ
1980: 4c 49 54 45 5f 55 54 46 31 36 42 45 3d 3d 33 20 LITE_UTF16BE==3
1990: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 65 6e 63 );. assert( enc
19a0: 3e 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26 >=SQLITE_UTF8 &&
19b0: 20 65 6e 63 3c 3d 53 51 4c 49 54 45 5f 55 54 46 enc<=SQLITE_UTF
19c0: 31 36 42 45 20 29 3b 0a 20 20 69 66 28 20 70 43 16BE );. if( pC
19d0: 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b 3d 20 65 oll ) pColl += e
19e0: 6e 63 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 nc-1;. return p
19f0: 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c Coll;.}../*.** L
1a00: 6f 63 61 74 65 20 61 20 75 73 65 72 20 66 75 6e ocate a user fun
1a10: 63 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 6e 61 ction given a na
1a20: 6d 65 2c 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 me, a number of
1a30: 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 61 20 arguments and a
1a40: 66 6c 61 67 0a 2a 2a 20 69 6e 64 69 63 61 74 69 flag.** indicati
1a50: 6e 67 20 77 68 65 74 68 65 72 20 74 68 65 20 66 ng whether the f
1a60: 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 72 73 20 unction prefers
1a70: 55 54 46 2d 31 36 20 6f 76 65 72 20 55 54 46 2d UTF-16 over UTF-
1a80: 38 2e 20 20 52 65 74 75 72 6e 20 61 0a 2a 2a 20 8. Return a.**
1a90: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 pointer to the F
1aa0: 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65 uncDef structure
1ab0: 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 that defines th
1ac0: 61 74 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 at function, or
1ad0: 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 20 69 return.** NULL i
1ae0: 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 f the function d
1af0: 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a oes not exist..*
1b00: 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 72 65 61 *.** If the crea
1b10: 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 teFlag argument
1b20: 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61 20 is true, then a
1b30: 6e 65 77 20 28 62 6c 61 6e 6b 29 20 46 75 6e 63 new (blank) Func
1b40: 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 Def.** structure
1b50: 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 20 is created and
1b60: 6c 69 6b 65 64 20 69 6e 74 6f 20 74 68 65 20 22 liked into the "
1b70: 64 62 22 20 73 74 72 75 63 74 75 72 65 20 69 66 db" structure if
1b80: 20 61 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 69 6e a.** no matchin
1b90: 67 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 76 69 g function previ
1ba0: 6f 75 73 6c 79 20 65 78 69 73 74 65 64 2e 20 20 ously existed.
1bb0: 57 68 65 6e 20 63 72 65 61 74 65 46 6c 61 67 20 When createFlag
1bc0: 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 74 is true.** and t
1bd0: 68 65 20 6e 41 72 67 20 70 61 72 61 6d 65 74 65 he nArg paramete
1be0: 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 6f 6e r is -1, then on
1bf0: 6c 79 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 ly a function th
1c00: 61 74 20 61 63 63 65 70 74 73 0a 2a 2a 20 61 6e at accepts.** an
1c10: 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 y number of argu
1c20: 6d 65 6e 74 73 20 77 69 6c 6c 20 62 65 20 72 65 ments will be re
1c30: 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
1c40: 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 66 createFlag is f
1c50: 61 6c 73 65 20 61 6e 64 20 6e 41 72 67 20 69 73 alse and nArg is
1c60: 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 66 69 -1, then the fi
1c70: 72 73 74 20 76 61 6c 69 64 0a 2a 2a 20 66 75 6e rst valid.** fun
1c80: 63 74 69 6f 6e 20 66 6f 75 6e 64 20 69 73 20 72 ction found is r
1c90: 65 74 75 72 6e 65 64 2e 20 20 41 20 66 75 6e 63 eturned. A func
1ca0: 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 20 69 66 tion is valid if
1cb0: 20 65 69 74 68 65 72 20 78 46 75 6e 63 0a 2a 2a either xFunc.**
1cc0: 20 6f 72 20 78 53 74 65 70 20 69 73 20 6e 6f 6e or xStep is non
1cd0: 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 -zero..**.** If
1ce0: 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 66 61 createFlag is fa
1cf0: 6c 73 65 2c 20 74 68 65 6e 20 61 20 66 75 6e 63 lse, then a func
1d00: 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72 65 tion with the re
1d10: 71 75 69 72 65 64 20 6e 61 6d 65 20 61 6e 64 0a quired name and.
1d20: 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 ** number of arg
1d30: 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 20 72 65 uments may be re
1d40: 74 75 72 6e 65 64 20 65 76 65 6e 20 69 66 20 74 turned even if t
1d50: 68 65 20 65 54 65 78 74 52 65 70 20 66 6c 61 67 he eTextRep flag
1d60: 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 does not.** mat
1d70: 63 68 20 74 68 61 74 20 72 65 71 75 65 73 74 65 ch that requeste
1d80: 64 2e 0a 2a 2f 0a 46 75 6e 63 44 65 66 20 2a 73 d..*/.FuncDef *s
1d90: 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 qlite3FindFuncti
1da0: 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 on(. sqlite3 *d
1db0: 62 2c 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f b, /* An o
1dc0: 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a pen database */.
1dd0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
1de0: 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 ame, /* Name of
1df0: 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 4e the function. N
1e00: 6f 74 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 ot null-terminat
1e10: 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d ed */. int nNam
1e20: 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 e, /* Nu
1e30: 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 mber of characte
1e40: 72 73 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 2a rs in the name *
1e50: 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 /. int nArg,
1e60: 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1e70: 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 of arguments.
1e80: 2d 31 20 6d 65 61 6e 73 20 61 6e 79 20 6e 75 6d -1 means any num
1e90: 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c ber */. u8 enc,
1ea0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1eb0: 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 6e referred text en
1ec0: 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 coding */. int
1ed0: 63 72 65 61 74 65 46 6c 61 67 20 20 20 20 20 2f createFlag /
1ee0: 2a 20 43 72 65 61 74 65 20 6e 65 77 20 65 6e 74 * Create new ent
1ef0: 72 79 20 69 66 20 74 72 75 65 20 61 6e 64 20 64 ry if true and d
1f00: 6f 65 73 20 6e 6f 74 20 6f 74 68 65 72 77 69 73 oes not otherwis
1f10: 65 20 65 78 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 e exist */.){.
1f20: 46 75 6e 63 44 65 66 20 2a 70 3b 20 20 20 20 20 FuncDef *p;
1f30: 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 /* Iterator
1f40: 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 46 75 variable */. Fu
1f50: 6e 63 44 65 66 20 2a 70 46 69 72 73 74 3b 20 20 ncDef *pFirst;
1f60: 20 20 2f 2a 20 46 69 72 73 74 20 66 75 6e 63 74 /* First funct
1f70: 69 6f 6e 20 77 69 74 68 20 74 68 69 73 20 6e 61 ion with this na
1f80: 6d 65 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 me */. FuncDef
1f90: 2a 70 42 65 73 74 20 3d 20 30 3b 20 2f 2a 20 42 *pBest = 0; /* B
1fa0: 65 73 74 20 6d 61 74 63 68 20 66 6f 75 6e 64 20 est match found
1fb0: 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 so far */. int
1fc0: 62 65 73 74 6d 61 74 63 68 20 3d 20 30 3b 20 20 bestmatch = 0;
1fd0: 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 65 6e 63 ... assert( enc
1fe0: 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c ==SQLITE_UTF8 ||
1ff0: 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 enc==SQLITE_UTF
2000: 31 36 4c 45 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c 16LE || enc==SQL
2010: 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 ITE_UTF16BE );.
2020: 20 69 66 28 20 6e 41 72 67 3c 2d 31 20 29 20 6e if( nArg<-1 ) n
2030: 41 72 67 20 3d 20 2d 31 3b 0a 0a 20 20 70 46 69 Arg = -1;.. pFi
2040: 72 73 74 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 rst = (FuncDef*)
2050: 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 sqlite3HashFind(
2060: 26 64 62 2d 3e 61 46 75 6e 63 2c 20 7a 4e 61 6d &db->aFunc, zNam
2070: 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 e, nName);. for
2080: 28 70 3d 70 46 69 72 73 74 3b 20 70 3b 20 70 3d (p=pFirst; p; p=
2090: 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 2f p->pNext){. /
20a0: 2a 20 44 75 72 69 6e 67 20 74 68 65 20 73 65 61 * During the sea
20b0: 72 63 68 20 66 6f 72 20 74 68 65 20 62 65 73 74 rch for the best
20c0: 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 function defini
20d0: 74 69 6f 6e 2c 20 62 65 73 74 6d 61 74 63 68 20 tion, bestmatch
20e0: 69 73 20 73 65 74 0a 20 20 20 20 2a 2a 20 61 73 is set. ** as
20f0: 20 66 6f 6c 6c 6f 77 73 20 74 6f 20 69 6e 64 69 follows to indi
2100: 63 61 74 65 20 74 68 65 20 71 75 61 6c 69 74 79 cate the quality
2110: 20 6f 66 20 74 68 65 20 6d 61 74 63 68 20 77 69 of the match wi
2120: 74 68 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f th the definitio
2130: 6e 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 64 n. ** pointed
2140: 20 74 6f 20 62 79 20 70 42 65 73 74 3a 0a 20 20 to by pBest:.
2150: 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 30 3a 20 70 **. ** 0: p
2160: 42 65 73 74 20 69 73 20 4e 55 4c 4c 2e 20 4e 6f Best is NULL. No
2170: 20 6d 61 74 63 68 20 68 61 73 20 62 65 65 6e 20 match has been
2180: 66 6f 75 6e 64 2e 0a 20 20 20 20 2a 2a 20 31 3a found.. ** 1:
2190: 20 41 20 76 61 72 69 61 62 6c 65 20 61 72 67 75 A variable argu
21a0: 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 74 ments function t
21b0: 68 61 74 20 70 72 65 66 65 72 73 20 55 54 46 2d hat prefers UTF-
21c0: 38 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36 0a 8 when a UTF-16.
21d0: 20 20 20 20 2a 2a 20 20 20 20 65 6e 63 6f 64 69 ** encodi
21e0: 6e 67 20 69 73 20 72 65 71 75 65 73 74 65 64 2c ng is requested,
21f0: 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a or vice versa..
2200: 20 20 20 20 2a 2a 20 32 3a 20 41 20 76 61 72 69 ** 2: A vari
2210: 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66 able arguments f
2220: 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 65 unction that use
2230: 73 20 55 54 46 2d 31 36 42 45 20 77 68 65 6e 20 s UTF-16BE when
2240: 55 54 46 2d 31 36 4c 45 20 69 73 0a 20 20 20 20 UTF-16LE is.
2250: 2a 2a 20 20 20 20 72 65 71 75 65 73 74 65 64 2c ** requested,
2260: 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a or vice versa..
2270: 20 20 20 20 2a 2a 20 33 3a 20 41 20 76 61 72 69 ** 3: A vari
2280: 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66 able arguments f
2290: 75 6e 63 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 unction using th
22a0: 65 20 73 61 6d 65 20 74 65 78 74 20 65 6e 63 6f e same text enco
22b0: 64 69 6e 67 2e 0a 20 20 20 20 2a 2a 20 34 3a 20 ding.. ** 4:
22c0: 41 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 A function with
22d0: 74 68 65 20 65 78 61 63 74 20 6e 75 6d 62 65 72 the exact number
22e0: 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 72 65 of arguments re
22f0: 71 75 65 73 74 65 64 20 74 68 61 74 0a 20 20 20 quested that.
2300: 20 2a 2a 20 20 20 20 70 72 65 66 65 72 73 20 55 ** prefers U
2310: 54 46 2d 38 20 77 68 65 6e 20 61 20 55 54 46 2d TF-8 when a UTF-
2320: 31 36 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 16 encoding is r
2330: 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63 equested, or vic
2340: 65 20 76 65 72 73 61 2e 0a 20 20 20 20 2a 2a 20 e versa.. **
2350: 35 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77 69 5: A function wi
2360: 74 68 20 74 68 65 20 65 78 61 63 74 20 6e 75 6d th the exact num
2370: 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 ber of arguments
2380: 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74 0a requested that.
2390: 20 20 20 20 2a 2a 20 20 20 20 70 72 65 66 65 72 ** prefer
23a0: 73 20 55 54 46 2d 31 36 4c 45 20 77 68 65 6e 20 s UTF-16LE when
23b0: 55 54 46 2d 31 36 42 45 20 69 73 20 72 65 71 75 UTF-16BE is requ
23c0: 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 ested, or vice v
23d0: 65 72 73 61 2e 0a 20 20 20 20 2a 2a 20 36 3a 20 ersa.. ** 6:
23e0: 41 6e 20 65 78 61 63 74 20 6d 61 74 63 68 2e 0a An exact match..
23f0: 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 **. ** A
2400: 6c 61 72 67 65 72 20 76 61 6c 75 65 20 6f 66 20 larger value of
2410: 27 6d 61 74 63 68 71 75 61 6c 27 20 69 6e 64 69 'matchqual' indi
2420: 63 61 74 65 73 20 61 20 6d 6f 72 65 20 64 65 73 cates a more des
2430: 69 72 61 62 6c 65 20 6d 61 74 63 68 2e 0a 20 20 irable match..
2440: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e */. if( p->
2450: 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 70 2d 3e 6e nArg==-1 || p->n
2460: 41 72 67 3d 3d 6e 41 72 67 20 7c 7c 20 6e 41 72 Arg==nArg || nAr
2470: 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 69 g==-1 ){. i
2480: 6e 74 20 6d 61 74 63 68 20 3d 20 31 3b 20 20 20 nt match = 1;
2490: 20 20 20 20 20 20 20 2f 2a 20 51 75 61 6c 69 74 /* Qualit
24a0: 79 20 6f 66 20 74 68 69 73 20 6d 61 74 63 68 20 y of this match
24b0: 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e */. if( p->
24c0: 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c 20 6e 41 nArg==nArg || nA
24d0: 72 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 rg==-1 ){.
24e0: 20 20 6d 61 74 63 68 20 3d 20 34 3b 0a 20 20 20 match = 4;.
24f0: 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 }. if( e
2500: 6e 63 3d 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20 nc==p->iPrefEnc
2510: 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 74 63 68 ){. match
2520: 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 += 2;. }.
2530: 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 28 65 else if( (e
2540: 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 nc==SQLITE_UTF16
2550: 4c 45 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e LE && p->iPrefEn
2560: 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 c==SQLITE_UTF16B
2570: 45 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 E) ||.
2580: 20 20 20 20 20 28 65 6e 63 3d 3d 53 51 4c 49 54 (enc==SQLIT
2590: 45 5f 55 54 46 31 36 42 45 20 26 26 20 70 2d 3e E_UTF16BE && p->
25a0: 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c 49 54 45 iPrefEnc==SQLITE
25b0: 5f 55 54 46 31 36 4c 45 29 20 29 7b 0a 20 20 20 _UTF16LE) ){.
25c0: 20 20 20 20 20 6d 61 74 63 68 20 2b 3d 20 31 3b match += 1;
25d0: 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
25e0: 69 66 28 20 6d 61 74 63 68 3e 62 65 73 74 6d 61 if( match>bestma
25f0: 74 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 70 tch ){. p
2600: 42 65 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 Best = p;.
2610: 20 20 62 65 73 74 6d 61 74 63 68 20 3d 20 6d 61 bestmatch = ma
2620: 74 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 tch;. }.
2630: 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
2640: 74 68 65 20 63 72 65 61 74 65 46 6c 61 67 20 70 the createFlag p
2650: 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 arameter is true
2660: 2c 20 61 6e 64 20 74 68 65 20 73 65 61 63 68 20 , and the seach
2670: 64 69 64 20 6e 6f 74 20 72 65 76 65 61 6c 20 61 did not reveal a
2680: 6e 0a 20 20 2a 2a 20 65 78 61 63 74 20 6d 61 74 n. ** exact mat
2690: 63 68 20 66 6f 72 20 74 68 65 20 6e 61 6d 65 2c ch for the name,
26a0: 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d number of argum
26b0: 65 6e 74 73 20 61 6e 64 20 65 6e 63 6f 64 69 6e ents and encodin
26c0: 67 2c 20 74 68 65 6e 20 61 64 64 20 61 0a 20 20 g, then add a.
26d0: 2a 2a 20 6e 65 77 20 65 6e 74 72 79 20 74 6f 20 ** new entry to
26e0: 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 the hash table a
26f0: 6e 64 20 72 65 74 75 72 6e 20 69 74 2e 0a 20 20 nd return it..
2700: 2a 2f 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 */. if( createF
2710: 6c 61 67 20 26 26 20 62 65 73 74 6d 61 74 63 68 lag && bestmatch
2720: 3c 36 20 26 26 20 0a 20 20 20 20 20 20 28 70 42 <6 && . (pB
2730: 65 73 74 20 3d 20 73 71 6c 69 74 65 4d 61 6c 6c est = sqliteMall
2740: 6f 63 28 73 69 7a 65 6f 66 28 2a 70 42 65 73 74 oc(sizeof(*pBest
2750: 29 2b 6e 4e 61 6d 65 29 29 21 3d 30 20 29 7b 0a )+nName))!=0 ){.
2760: 20 20 20 20 70 42 65 73 74 2d 3e 6e 41 72 67 20 pBest->nArg
2770: 3d 20 6e 41 72 67 3b 0a 20 20 20 20 70 42 65 73 = nArg;. pBes
2780: 74 2d 3e 70 4e 65 78 74 20 3d 20 70 46 69 72 73 t->pNext = pFirs
2790: 74 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 69 50 t;. pBest->iP
27a0: 72 65 66 45 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 refEnc = enc;.
27b0: 20 20 6d 65 6d 63 70 79 28 70 42 65 73 74 2d 3e memcpy(pBest->
27c0: 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e zName, zName, nN
27d0: 61 6d 65 29 3b 0a 20 20 20 20 70 42 65 73 74 2d ame);. pBest-
27e0: 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 >zName[nName] =
27f0: 30 3b 0a 20 20 20 20 69 66 28 20 70 42 65 73 74 0;. if( pBest
2800: 3d 3d 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 ==sqlite3HashIns
2810: 65 72 74 28 26 64 62 2d 3e 61 46 75 6e 63 2c 70 ert(&db->aFunc,p
2820: 42 65 73 74 2d 3e 7a 4e 61 6d 65 2c 6e 4e 61 6d Best->zName,nNam
2830: 65 2c 28 76 6f 69 64 2a 29 70 42 65 73 74 29 20 e,(void*)pBest)
2840: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 46 ){. sqliteF
2850: 72 65 65 28 70 42 65 73 74 29 3b 0a 20 20 20 20 ree(pBest);.
2860: 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
2870: 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 42 65 }. }.. if( pBe
2880: 73 74 20 26 26 20 28 70 42 65 73 74 2d 3e 78 53 st && (pBest->xS
2890: 74 65 70 20 7c 7c 20 70 42 65 73 74 2d 3e 78 46 tep || pBest->xF
28a0: 75 6e 63 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 unc || createFla
28b0: 67 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e g) ){. return
28c0: 20 70 42 65 73 74 3b 0a 20 20 7d 0a 20 20 72 65 pBest;. }. re
28d0: 74 75 72 6e 20 30 3b 0a 7d 0a turn 0;.}.