Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 6fe21a295621ecdcf4b5e45fc72bbda0a718353e [file] [log] [blame]
danielk1977fd9a0a42005-05-24 12:01:001/*
2** 2005 May 23
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file contains functions used to access the internal hash tables
14** of user defined functions and collation sequences.
danielk1977fd9a0a42005-05-24 12:01:0015*/
16
17#include "sqliteInt.h"
18
19/*
danielk19774dade032005-05-25 10:45:1020** Invoke the 'collation needed' callback to request a collation sequence
drh9aeda792009-08-20 02:34:1521** in the encoding enc of name zName, length nName.
danielk19774dade032005-05-25 10:45:1022*/
drh9aeda792009-08-20 02:34:1523static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
danielk19774dade032005-05-25 10:45:1024 assert( !db->xCollNeeded || !db->xCollNeeded16 );
danielk19774dade032005-05-25 10:45:1025 if( db->xCollNeeded ){
drhc4a64fa2009-05-11 20:53:2826 char *zExternal = sqlite3DbStrDup(db, zName);
danielk19774dade032005-05-25 10:45:1027 if( !zExternal ) return;
drh9aeda792009-08-20 02:34:1528 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
drh633e6d52008-07-28 19:34:5329 sqlite3DbFree(db, zExternal);
danielk19774dade032005-05-25 10:45:1030 }
31#ifndef SQLITE_OMIT_UTF16
32 if( db->xCollNeeded16 ){
33 char const *zExternal;
danielk19771e536952007-08-16 10:09:0134 sqlite3_value *pTmp = sqlite3ValueNew(db);
drhc4a64fa2009-05-11 20:53:2835 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
drhb21c8cd2007-08-21 19:33:5636 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
drh26abcb12005-12-14 22:51:1637 if( zExternal ){
danielk197714db2662006-01-09 16:12:0438 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
drh26abcb12005-12-14 22:51:1639 }
40 sqlite3ValueFree(pTmp);
danielk19774dade032005-05-25 10:45:1041 }
42#endif
43}
44
45/*
46** This routine is called if the collation factory fails to deliver a
47** collation function in the best encoding but there may be other versions
48** of this collation function (for other text encodings) available. Use one
49** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
50** possible.
51*/
52static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
53 CollSeq *pColl2;
54 char *z = pColl->zName;
danielk19774dade032005-05-25 10:45:1055 int i;
56 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
57 for(i=0; i<3; i++){
drhc4a64fa2009-05-11 20:53:2858 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
danielk19774dade032005-05-25 10:45:1059 if( pColl2->xCmp!=0 ){
60 memcpy(pColl, pColl2, sizeof(CollSeq));
danielk1977a9808b32007-05-07 09:32:4561 pColl->xDel = 0; /* Do not copy the destructor */
danielk19774dade032005-05-25 10:45:1062 return SQLITE_OK;
63 }
64 }
65 return SQLITE_ERROR;
66}
67
68/*
danielk19774dade032005-05-25 10:45:1069** This routine is called on a collation sequence before it is used to
70** check that it is defined. An undefined collation sequence exists when
71** a database is loaded that contains references to collation sequences
72** that have not been defined by sqlite3_create_collation() etc.
73**
74** If required, this routine calls the 'collation needed' callback to
75** request a definition of the collating sequence. If this doesn't work,
76** an equivalent collating sequence that uses a text encoding different
77** from the main database is substituted, if one is available.
78*/
79int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
drh666e8662017-07-06 02:49:2680 if( pColl && pColl->xCmp==0 ){
danielk19774dade032005-05-25 10:45:1081 const char *zName = pColl->zName;
drh9aeda792009-08-20 02:34:1582 sqlite3 *db = pParse->db;
drh79e72a52012-10-05 14:43:4083 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
danielk19774dade032005-05-25 10:45:1084 if( !p ){
danielk19774dade032005-05-25 10:45:1085 return SQLITE_ERROR;
86 }
danielk1977b3bf5562006-01-10 17:58:2387 assert( p==pColl );
danielk19774dade032005-05-25 10:45:1088 }
89 return SQLITE_OK;
90}
91
92
93
94/*
danielk1977fd9a0a42005-05-24 12:01:0095** Locate and return an entry from the db.aCollSeq hash table. If the entry
96** specified by zName and nName is not found and parameter 'create' is
97** true, then create a new entry. Otherwise return NULL.
98**
99** Each pointer stored in the sqlite3.aCollSeq hash table contains an
100** array of three CollSeq structures. The first is the collation sequence
peter.d.reid60ec9142014-09-06 16:39:46101** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
danielk1977fd9a0a42005-05-24 12:01:00102**
103** Stored immediately after the three collation sequences is a copy of
104** the collation sequence name. A pointer to this string is stored in
105** each collation sequence structure.
106*/
drh55ef4d92005-08-14 01:20:37107static CollSeq *findCollSeqEntry(
drhc4a64fa2009-05-11 20:53:28108 sqlite3 *db, /* Database connection */
109 const char *zName, /* Name of the collating sequence */
110 int create /* Create a new entry if true */
danielk1977fd9a0a42005-05-24 12:01:00111){
112 CollSeq *pColl;
drhacbcb7e2014-08-21 20:26:37113 pColl = sqlite3HashFind(&db->aCollSeq, zName);
danielk1977fd9a0a42005-05-24 12:01:00114
115 if( 0==pColl && create ){
drh3b02e0f2017-07-06 03:06:20116 int nName = sqlite3Strlen30(zName) + 1;
117 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
danielk1977fd9a0a42005-05-24 12:01:00118 if( pColl ){
119 CollSeq *pDel = 0;
120 pColl[0].zName = (char*)&pColl[3];
121 pColl[0].enc = SQLITE_UTF8;
122 pColl[1].zName = (char*)&pColl[3];
123 pColl[1].enc = SQLITE_UTF16LE;
124 pColl[2].zName = (char*)&pColl[3];
125 pColl[2].enc = SQLITE_UTF16BE;
126 memcpy(pColl[0].zName, zName, nName);
drhacbcb7e2014-08-21 20:26:37127 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
danielk1977fd9a0a42005-05-24 12:01:00128
shanebe217792009-03-05 04:20:31129 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
danielk1977fd9a0a42005-05-24 12:01:00130 ** return the pColl pointer to be deleted (because it wasn't added
131 ** to the hash table).
132 */
drhf3a65f72007-08-22 20:18:21133 assert( pDel==0 || pDel==pColl );
134 if( pDel!=0 ){
drh4a642b62016-02-05 01:55:27135 sqlite3OomFault(db);
drh633e6d52008-07-28 19:34:53136 sqlite3DbFree(db, pDel);
drh91171cd2006-03-14 11:08:27137 pColl = 0;
138 }
danielk1977fd9a0a42005-05-24 12:01:00139 }
140 }
141 return pColl;
142}
143
144/*
145** Parameter zName points to a UTF-8 encoded string nName bytes long.
146** Return the CollSeq* pointer for the collation sequence named zName
147** for the encoding 'enc' from the database 'db'.
148**
149** If the entry specified is not found and 'create' is true, then create a
150** new entry. Otherwise return NULL.
drha34001c2007-02-02 12:44:37151**
152** A separate function sqlite3LocateCollSeq() is a wrapper around
153** this routine. sqlite3LocateCollSeq() invokes the collation factory
154** if necessary and generates an error message if the collating sequence
155** cannot be found.
drhc4a64fa2009-05-11 20:53:28156**
157** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
danielk1977fd9a0a42005-05-24 12:01:00158*/
159CollSeq *sqlite3FindCollSeq(
drha5d09112019-10-22 01:00:16160 sqlite3 *db, /* Database connection to search */
161 u8 enc, /* Desired text encoding */
162 const char *zName, /* Name of the collating sequence. Might be NULL */
163 int create /* True to create CollSeq if doesn't already exist */
danielk1977fd9a0a42005-05-24 12:01:00164){
danielk1977b3bf5562006-01-10 17:58:23165 CollSeq *pColl;
drh42a630b2020-03-05 16:13:24166 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
167 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
danielk1977b3bf5562006-01-10 17:58:23168 if( zName ){
drhc4a64fa2009-05-11 20:53:28169 pColl = findCollSeqEntry(db, zName, create);
drh42a630b2020-03-05 16:13:24170 if( pColl ) pColl += enc-1;
danielk1977b3bf5562006-01-10 17:58:23171 }else{
172 pColl = db->pDfltColl;
173 }
danielk1977fd9a0a42005-05-24 12:01:00174 return pColl;
175}
176
drha5d09112019-10-22 01:00:16177/*
drh42a630b2020-03-05 16:13:24178** Change the text encoding for a database connection. This means that
179** the pDfltColl must change as well.
180*/
181void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
182 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
183 db->enc = enc;
184 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
185 ** strings is BINARY.
186 */
187 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
drh6b429dc2023-05-12 10:52:12188 sqlite3ExpirePreparedStatements(db, 1);
drh42a630b2020-03-05 16:13:24189}
190
191/*
drha5d09112019-10-22 01:00:16192** This function is responsible for invoking the collation factory callback
193** or substituting a collation sequence of a different encoding when the
194** requested collation sequence is not available in the desired encoding.
195**
196** If it is not NULL, then pColl must point to the database native encoding
197** collation sequence with name zName, length nName.
198**
199** The return value is either the collation sequence to be used in database
200** db for collation type name zName, length nName, or NULL, if no collation
201** sequence can be found. If no collation is found, leave an error message.
202**
203** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
204*/
205CollSeq *sqlite3GetCollSeq(
206 Parse *pParse, /* Parsing context */
207 u8 enc, /* The desired encoding for the collating sequence */
208 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
209 const char *zName /* Collating sequence name */
210){
211 CollSeq *p;
212 sqlite3 *db = pParse->db;
213
214 p = pColl;
215 if( !p ){
216 p = sqlite3FindCollSeq(db, enc, zName, 0);
217 }
218 if( !p || !p->xCmp ){
219 /* No collation sequence of this type for this encoding is registered.
220 ** Call the collation factory to see if it can supply us with one.
221 */
222 callCollNeeded(db, enc, zName);
223 p = sqlite3FindCollSeq(db, enc, zName, 0);
224 }
225 if( p && !p->xCmp && synthCollSeq(db, p) ){
226 p = 0;
227 }
228 assert( !p || p->xCmp );
229 if( p==0 ){
230 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
231 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
232 }
233 return p;
234}
235
236/*
237** This function returns the collation sequence for database native text
238** encoding identified by the string zName.
239**
240** If the requested collation sequence is not available, or not available
241** in the database native encoding, the collation factory is invoked to
242** request it. If the collation factory does not supply such a sequence,
243** and the sequence is available in another text encoding, then that is
244** returned instead.
245**
246** If no versions of the requested collations sequence are available, or
247** another error occurs, NULL is returned and an error message written into
248** pParse.
249**
250** This routine is a wrapper around sqlite3FindCollSeq(). This routine
251** invokes the collation factory if the named collation cannot be found
252** and generates an error message.
253**
254** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
255*/
256CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
257 sqlite3 *db = pParse->db;
258 u8 enc = ENC(db);
259 u8 initbusy = db->init.busy;
260 CollSeq *pColl;
261
262 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
263 if( !initbusy && (!pColl || !pColl->xCmp) ){
264 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
265 }
266
267 return pColl;
268}
269
danielk19778c0a7912008-08-20 14:49:23270/* During the search for the best function definition, this procedure
271** is called to test how well the function passed as the first argument
272** matches the request for a function with nArg arguments in a system
273** that uses encoding enc. The value returned indicates how well the
274** request is matched. A higher value indicates a better match.
275**
drh89d5d6a2012-04-07 00:09:21276** If nArg is -1 that means to only return a match (non-zero) if p->nArg
277** is also -1. In other words, we are searching for a function that
278** takes a variable number of arguments.
279**
280** If nArg is -2 that means that we are searching for any function
281** regardless of the number of arguments it uses, so return a positive
282** match score for any
283**
drhdfbc3a82009-01-31 22:28:48284** The returned value is always between 0 and 6, as follows:
danielk19778c0a7912008-08-20 14:49:23285**
drh89d5d6a2012-04-07 00:09:21286** 0: Not a match.
287** 1: UTF8/16 conversion required and function takes any number of arguments.
288** 2: UTF16 byte order change required and function takes any number of args.
289** 3: encoding matches and function takes any number of arguments
290** 4: UTF8/16 conversion required - argument count matches exactly
291** 5: UTF16 byte order conversion required - argument count matches exactly
292** 6: Perfect match: encoding and argument count match exactly.
danielk19778c0a7912008-08-20 14:49:23293**
drh2d801512016-01-14 22:19:58294** If nArg==(-2) then any function with a non-null xSFunc is
295** a perfect match and any function with xSFunc NULL is
drh89d5d6a2012-04-07 00:09:21296** a non-match.
danielk19778c0a7912008-08-20 14:49:23297*/
drh89d5d6a2012-04-07 00:09:21298#define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
299static int matchQuality(
300 FuncDef *p, /* The function we are evaluating for match quality */
301 int nArg, /* Desired number of arguments. (-1)==any */
302 u8 enc /* Desired text encoding */
303){
304 int match;
drh2e899cc2025-01-21 15:12:00305 assert( p->nArg>=(-4) && p->nArg!=(-2) );
306 assert( nArg>=(-2) );
drh89d5d6a2012-04-07 00:09:21307
308 /* Wrong number of arguments means "no match" */
drh2eeca202020-01-08 20:37:45309 if( p->nArg!=nArg ){
drh2e899cc2025-01-21 15:12:00310 if( nArg==(-2) ) return p->xSFunc==0 ? 0 : FUNC_PERFECT_MATCH;
drh2eeca202020-01-08 20:37:45311 if( p->nArg>=0 ) return 0;
drh2e899cc2025-01-21 15:12:00312 /* Special p->nArg values available to built-in functions only:
313 ** -3 1 or more arguments required
314 ** -4 2 or more arguments required
315 */
316 if( p->nArg<(-2) && nArg<(-2-p->nArg) ) return 0;
drh2eeca202020-01-08 20:37:45317 }
drh89d5d6a2012-04-07 00:09:21318
319 /* Give a better score to a function with a specific number of arguments
320 ** than to function that accepts any number of arguments. */
321 if( p->nArg==nArg ){
322 match = 4;
323 }else{
danielk19778c0a7912008-08-20 14:49:23324 match = 1;
danielk19778c0a7912008-08-20 14:49:23325 }
drh89d5d6a2012-04-07 00:09:21326
327 /* Bonus points if the text encoding matches */
drhd36e1042013-09-06 13:10:12328 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
drh89d5d6a2012-04-07 00:09:21329 match += 2; /* Exact encoding match */
drhd36e1042013-09-06 13:10:12330 }else if( (enc & p->funcFlags & 2)!=0 ){
drh89d5d6a2012-04-07 00:09:21331 match += 1; /* Both are UTF16, but with different byte orders */
332 }
333
danielk19778c0a7912008-08-20 14:49:23334 return match;
335}
336
danielk1977fd9a0a42005-05-24 12:01:00337/*
drh70a8ca32008-08-21 18:49:27338** Search a FuncDefHash for a function with the given name. Return
339** a pointer to the matching FuncDef if found, or 0 if there is no match.
340*/
drh19efd0d2018-12-05 17:48:57341FuncDef *sqlite3FunctionSearch(
drh70a8ca32008-08-21 18:49:27342 int h, /* Hash of the name */
drh80738d92016-02-15 00:34:16343 const char *zFunc /* Name of function */
drh70a8ca32008-08-21 18:49:27344){
345 FuncDef *p;
drh80738d92016-02-15 00:34:16346 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
drhf9751072021-10-07 13:40:29347 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
drh80738d92016-02-15 00:34:16348 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
drh70a8ca32008-08-21 18:49:27349 return p;
350 }
351 }
352 return 0;
353}
354
355/*
356** Insert a new FuncDef into a FuncDefHash hash table.
357*/
drh80738d92016-02-15 00:34:16358void sqlite3InsertBuiltinFuncs(
359 FuncDef *aDef, /* List of global functions to be inserted */
360 int nDef /* Length of the apDef[] list */
drh70a8ca32008-08-21 18:49:27361){
drh80738d92016-02-15 00:34:16362 int i;
363 for(i=0; i<nDef; i++){
364 FuncDef *pOther;
365 const char *zName = aDef[i].zName;
366 int nName = sqlite3Strlen30(zName);
mistachkin8bee11a2018-10-29 17:53:23367 int h = SQLITE_FUNC_HASH(zName[0], nName);
drhf9751072021-10-07 13:40:29368 assert( aDef[i].funcFlags & SQLITE_FUNC_BUILTIN );
drh19efd0d2018-12-05 17:48:57369 pOther = sqlite3FunctionSearch(h, zName);
drh80738d92016-02-15 00:34:16370 if( pOther ){
371 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
372 aDef[i].pNext = pOther->pNext;
373 pOther->pNext = &aDef[i];
374 }else{
375 aDef[i].pNext = 0;
376 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
377 sqlite3BuiltinFunctions.a[h] = &aDef[i];
378 }
drh70a8ca32008-08-21 18:49:27379 }
380}
381
382
383
384/*
danielk1977fd9a0a42005-05-24 12:01:00385** Locate a user function given a name, a number of arguments and a flag
386** indicating whether the function prefers UTF-16 over UTF-8. Return a
387** pointer to the FuncDef structure that defines that function, or return
388** NULL if the function does not exist.
389**
390** If the createFlag argument is true, then a new (blank) FuncDef
391** structure is created and liked into the "db" structure if a
drh89d5d6a2012-04-07 00:09:21392** no matching function previously existed.
danielk1977fd9a0a42005-05-24 12:01:00393**
drh89d5d6a2012-04-07 00:09:21394** If nArg is -2, then the first valid function found is returned. A
drh2d801512016-01-14 22:19:58395** function is valid if xSFunc is non-zero. The nArg==(-2)
drh89d5d6a2012-04-07 00:09:21396** case is used to see if zName is a valid function name for some number
397** of arguments. If nArg is -2, then createFlag must be 0.
danielk1977fd9a0a42005-05-24 12:01:00398**
399** If createFlag is false, then a function with the required name and
400** number of arguments may be returned even if the eTextRep flag does not
401** match that requested.
402*/
403FuncDef *sqlite3FindFunction(
404 sqlite3 *db, /* An open database */
drh80738d92016-02-15 00:34:16405 const char *zName, /* Name of the function. zero-terminated */
danielk1977fd9a0a42005-05-24 12:01:00406 int nArg, /* Number of arguments. -1 means any number */
407 u8 enc, /* Preferred text encoding */
drh89d5d6a2012-04-07 00:09:21408 u8 createFlag /* Create new entry if true and does not otherwise exist */
danielk1977fd9a0a42005-05-24 12:01:00409){
410 FuncDef *p; /* Iterator variable */
danielk1977fd9a0a42005-05-24 12:01:00411 FuncDef *pBest = 0; /* Best match found so far */
drh70a8ca32008-08-21 18:49:27412 int bestScore = 0; /* Score of best match */
413 int h; /* Hash value */
drh80738d92016-02-15 00:34:16414 int nName; /* Length of the name */
danielk1977fd9a0a42005-05-24 12:01:00415
drh89d5d6a2012-04-07 00:09:21416 assert( nArg>=(-2) );
417 assert( nArg>=(-1) || createFlag==0 );
drh80738d92016-02-15 00:34:16418 nName = sqlite3Strlen30(zName);
danielk1977fd9a0a42005-05-24 12:01:00419
drhe3602be2008-09-09 12:31:33420 /* First search for a match amongst the application-defined functions.
421 */
drh80738d92016-02-15 00:34:16422 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
drh70a8ca32008-08-21 18:49:27423 while( p ){
424 int score = matchQuality(p, nArg, enc);
425 if( score>bestScore ){
danielk19778c0a7912008-08-20 14:49:23426 pBest = p;
drh70a8ca32008-08-21 18:49:27427 bestScore = score;
danielk19778c0a7912008-08-20 14:49:23428 }
drh70a8ca32008-08-21 18:49:27429 p = p->pNext;
danielk19778c0a7912008-08-20 14:49:23430 }
danielk1977fd9a0a42005-05-24 12:01:00431
drhe3602be2008-09-09 12:31:33432 /* If no match is found, search the built-in functions.
433 **
drh8257aa82017-07-26 19:59:13434 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
drh545f5872010-04-24 14:02:59435 ** functions even if a prior app-defined function was found. And give
436 ** priority to built-in functions.
437 **
drhe3602be2008-09-09 12:31:33438 ** Except, if createFlag is true, that means that we are trying to
drh6c5cecb2010-09-16 19:49:22439 ** install a new function. Whatever FuncDef structure is returned it will
drhe3602be2008-09-09 12:31:33440 ** have fields overwritten with new information appropriate for the
441 ** new function. But the FuncDefs for built-in functions are read-only.
442 ** So we must not search for built-ins when creating a new function.
danielk19778c0a7912008-08-20 14:49:23443 */
drh8257aa82017-07-26 19:59:13444 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
drh545f5872010-04-24 14:02:59445 bestScore = 0;
mistachkin8bee11a2018-10-29 17:53:23446 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
drh19efd0d2018-12-05 17:48:57447 p = sqlite3FunctionSearch(h, zName);
drh70a8ca32008-08-21 18:49:27448 while( p ){
449 int score = matchQuality(p, nArg, enc);
450 if( score>bestScore ){
451 pBest = p;
452 bestScore = score;
danielk1977fd9a0a42005-05-24 12:01:00453 }
drh70a8ca32008-08-21 18:49:27454 p = p->pNext;
danielk1977fd9a0a42005-05-24 12:01:00455 }
456 }
457
drhe3602be2008-09-09 12:31:33458 /* If the createFlag parameter is true and the search did not reveal an
danielk1977fd9a0a42005-05-24 12:01:00459 ** exact match for the name, number of arguments and encoding, then add a
460 ** new entry to the hash table and return it.
461 */
drh89d5d6a2012-04-07 00:09:21462 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
danielk19778c0a7912008-08-20 14:49:23463 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
drh80738d92016-02-15 00:34:16464 FuncDef *pOther;
drh92011842018-05-26 16:00:26465 u8 *z;
drh6ad224e2016-02-24 19:57:11466 pBest->zName = (const char*)&pBest[1];
drh1bd10f82008-12-10 21:19:56467 pBest->nArg = (u16)nArg;
drhd36e1042013-09-06 13:10:12468 pBest->funcFlags = enc;
drh6ad224e2016-02-24 19:57:11469 memcpy((char*)&pBest[1], zName, nName+1);
drh92011842018-05-26 16:00:26470 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
drh80738d92016-02-15 00:34:16471 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
472 if( pOther==pBest ){
473 sqlite3DbFree(db, pBest);
474 sqlite3OomFault(db);
475 return 0;
476 }else{
477 pBest->pNext = pOther;
478 }
danielk1977fd9a0a42005-05-24 12:01:00479 }
480
drh2d801512016-01-14 22:19:58481 if( pBest && (pBest->xSFunc || createFlag) ){
danielk1977fd9a0a42005-05-24 12:01:00482 return pBest;
483 }
484 return 0;
485}
drh03b808a2006-03-13 15:06:05486
487/*
488** Free all resources held by the schema structure. The void* argument points
drh633e6d52008-07-28 19:34:53489** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
drhb6ee6602011-04-04 13:40:53490** pointer itself, it just cleans up subsidiary resources (i.e. the contents
drh03b808a2006-03-13 15:06:05491** of the schema hash tables).
danielk19778cf6c552008-06-23 16:53:46492**
493** The Schema.cache_size variable is not cleared.
drh03b808a2006-03-13 15:06:05494*/
drhb6ee6602011-04-04 13:40:53495void sqlite3SchemaClear(void *p){
drh03b808a2006-03-13 15:06:05496 Hash temp1;
497 Hash temp2;
498 HashElem *pElem;
499 Schema *pSchema = (Schema *)p;
drh41ce47c2022-08-22 02:00:26500 sqlite3 xdb;
drh03b808a2006-03-13 15:06:05501
drh41ce47c2022-08-22 02:00:26502 memset(&xdb, 0, sizeof(xdb));
drh03b808a2006-03-13 15:06:05503 temp1 = pSchema->tblHash;
504 temp2 = pSchema->trigHash;
drhe61922a2009-05-02 13:29:37505 sqlite3HashInit(&pSchema->trigHash);
drh03b808a2006-03-13 15:06:05506 sqlite3HashClear(&pSchema->idxHash);
507 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
drh41ce47c2022-08-22 02:00:26508 sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem));
drh03b808a2006-03-13 15:06:05509 }
510 sqlite3HashClear(&temp2);
drhe61922a2009-05-02 13:29:37511 sqlite3HashInit(&pSchema->tblHash);
drh03b808a2006-03-13 15:06:05512 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
513 Table *pTab = sqliteHashData(pElem);
drh41ce47c2022-08-22 02:00:26514 sqlite3DeleteTable(&xdb, pTab);
drh03b808a2006-03-13 15:06:05515 }
516 sqlite3HashClear(&temp1);
dan1da40a32009-09-19 17:00:31517 sqlite3HashClear(&pSchema->fkeyHash);
drh03b808a2006-03-13 15:06:05518 pSchema->pSeqTab = 0;
drh2c5e35f2014-08-05 11:04:21519 if( pSchema->schemaFlags & DB_SchemaLoaded ){
drhc2a75552011-03-18 21:55:46520 pSchema->iGeneration++;
drhc2a75552011-03-18 21:55:46521 }
drhdc6b41e2017-08-17 02:26:35522 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
drh03b808a2006-03-13 15:06:05523}
524
525/*
526** Find and return the schema associated with a BTree. Create
527** a new one if necessary.
528*/
drh17435752007-08-16 04:30:38529Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
drh03b808a2006-03-13 15:06:05530 Schema * p;
531 if( pBt ){
drhb6ee6602011-04-04 13:40:53532 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
drh03b808a2006-03-13 15:06:05533 }else{
drhb9755982010-07-24 16:34:37534 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
drh03b808a2006-03-13 15:06:05535 }
danielk1977a1644fd2007-08-29 12:31:25536 if( !p ){
drh4a642b62016-02-05 01:55:27537 sqlite3OomFault(db);
danielk1977a1644fd2007-08-29 12:31:25538 }else if ( 0==p->file_format ){
drhe61922a2009-05-02 13:29:37539 sqlite3HashInit(&p->tblHash);
540 sqlite3HashInit(&p->idxHash);
541 sqlite3HashInit(&p->trigHash);
dan1da40a32009-09-19 17:00:31542 sqlite3HashInit(&p->fkeyHash);
drhf012ea32006-05-24 12:43:26543 p->enc = SQLITE_UTF8;
drh03b808a2006-03-13 15:06:05544 }
545 return p;
546}