blob: faf6d520c4789007f0e89102f784f820db03661e [file] [log] [blame]
danielk1977fd9a0a42005-05-24 12:01:00 +00001/*
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:00 +000015*/
16
17#include "sqliteInt.h"
18
19/*
danielk19774dade032005-05-25 10:45:10 +000020** Invoke the 'collation needed' callback to request a collation sequence
drh9aeda792009-08-20 02:34:15 +000021** in the encoding enc of name zName, length nName.
danielk19774dade032005-05-25 10:45:10 +000022*/
drh9aeda792009-08-20 02:34:15 +000023static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
danielk19774dade032005-05-25 10:45:10 +000024 assert( !db->xCollNeeded || !db->xCollNeeded16 );
danielk19774dade032005-05-25 10:45:10 +000025 if( db->xCollNeeded ){
drhc4a64fa2009-05-11 20:53:28 +000026 char *zExternal = sqlite3DbStrDup(db, zName);
danielk19774dade032005-05-25 10:45:10 +000027 if( !zExternal ) return;
drh9aeda792009-08-20 02:34:15 +000028 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
drh633e6d52008-07-28 19:34:53 +000029 sqlite3DbFree(db, zExternal);
danielk19774dade032005-05-25 10:45:10 +000030 }
31#ifndef SQLITE_OMIT_UTF16
32 if( db->xCollNeeded16 ){
33 char const *zExternal;
danielk19771e536952007-08-16 10:09:01 +000034 sqlite3_value *pTmp = sqlite3ValueNew(db);
drhc4a64fa2009-05-11 20:53:28 +000035 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
drhb21c8cd2007-08-21 19:33:56 +000036 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
drh26abcb12005-12-14 22:51:16 +000037 if( zExternal ){
danielk197714db2662006-01-09 16:12:04 +000038 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
drh26abcb12005-12-14 22:51:16 +000039 }
40 sqlite3ValueFree(pTmp);
danielk19774dade032005-05-25 10:45:10 +000041 }
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:10 +000055 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:28 +000058 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
danielk19774dade032005-05-25 10:45:10 +000059 if( pColl2->xCmp!=0 ){
60 memcpy(pColl, pColl2, sizeof(CollSeq));
danielk1977a9808b32007-05-07 09:32:45 +000061 pColl->xDel = 0; /* Do not copy the destructor */
danielk19774dade032005-05-25 10:45:10 +000062 return SQLITE_OK;
63 }
64 }
65 return SQLITE_ERROR;
66}
67
68/*
69** This function is responsible for invoking the collation factory callback
70** or substituting a collation sequence of a different encoding when the
drh9aeda792009-08-20 02:34:15 +000071** requested collation sequence is not available in the desired encoding.
danielk19774dade032005-05-25 10:45:10 +000072**
73** If it is not NULL, then pColl must point to the database native encoding
74** collation sequence with name zName, length nName.
75**
76** The return value is either the collation sequence to be used in database
77** db for collation type name zName, length nName, or NULL, if no collation
drh79e72a52012-10-05 14:43:40 +000078** sequence can be found. If no collation is found, leave an error message.
drhc4a64fa2009-05-11 20:53:28 +000079**
80** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
danielk19774dade032005-05-25 10:45:10 +000081*/
82CollSeq *sqlite3GetCollSeq(
drh79e72a52012-10-05 14:43:40 +000083 Parse *pParse, /* Parsing context */
shanecea72b22009-09-07 04:38:36 +000084 u8 enc, /* The desired encoding for the collating sequence */
drhc4a64fa2009-05-11 20:53:28 +000085 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
86 const char *zName /* Collating sequence name */
danielk19774dade032005-05-25 10:45:10 +000087){
88 CollSeq *p;
drh79e72a52012-10-05 14:43:40 +000089 sqlite3 *db = pParse->db;
danielk19774dade032005-05-25 10:45:10 +000090
91 p = pColl;
92 if( !p ){
drh9aeda792009-08-20 02:34:15 +000093 p = sqlite3FindCollSeq(db, enc, zName, 0);
danielk19774dade032005-05-25 10:45:10 +000094 }
95 if( !p || !p->xCmp ){
96 /* No collation sequence of this type for this encoding is registered.
97 ** Call the collation factory to see if it can supply us with one.
98 */
drh9aeda792009-08-20 02:34:15 +000099 callCollNeeded(db, enc, zName);
100 p = sqlite3FindCollSeq(db, enc, zName, 0);
danielk19774dade032005-05-25 10:45:10 +0000101 }
102 if( p && !p->xCmp && synthCollSeq(db, p) ){
103 p = 0;
104 }
105 assert( !p || p->xCmp );
drh79e72a52012-10-05 14:43:40 +0000106 if( p==0 ){
107 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
drh7e8515d2017-12-08 19:37:04 +0000108 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
drh79e72a52012-10-05 14:43:40 +0000109 }
danielk19774dade032005-05-25 10:45:10 +0000110 return p;
111}
112
113/*
114** This routine is called on a collation sequence before it is used to
115** check that it is defined. An undefined collation sequence exists when
116** a database is loaded that contains references to collation sequences
117** that have not been defined by sqlite3_create_collation() etc.
118**
119** If required, this routine calls the 'collation needed' callback to
120** request a definition of the collating sequence. If this doesn't work,
121** an equivalent collating sequence that uses a text encoding different
122** from the main database is substituted, if one is available.
123*/
124int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
drh666e8662017-07-06 02:49:26 +0000125 if( pColl && pColl->xCmp==0 ){
danielk19774dade032005-05-25 10:45:10 +0000126 const char *zName = pColl->zName;
drh9aeda792009-08-20 02:34:15 +0000127 sqlite3 *db = pParse->db;
drh79e72a52012-10-05 14:43:40 +0000128 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
danielk19774dade032005-05-25 10:45:10 +0000129 if( !p ){
danielk19774dade032005-05-25 10:45:10 +0000130 return SQLITE_ERROR;
131 }
danielk1977b3bf5562006-01-10 17:58:23 +0000132 assert( p==pColl );
danielk19774dade032005-05-25 10:45:10 +0000133 }
134 return SQLITE_OK;
135}
136
137
138
139/*
danielk1977fd9a0a42005-05-24 12:01:00 +0000140** Locate and return an entry from the db.aCollSeq hash table. If the entry
141** specified by zName and nName is not found and parameter 'create' is
142** true, then create a new entry. Otherwise return NULL.
143**
144** Each pointer stored in the sqlite3.aCollSeq hash table contains an
145** array of three CollSeq structures. The first is the collation sequence
peter.d.reid60ec9142014-09-06 16:39:46 +0000146** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
danielk1977fd9a0a42005-05-24 12:01:00 +0000147**
148** Stored immediately after the three collation sequences is a copy of
149** the collation sequence name. A pointer to this string is stored in
150** each collation sequence structure.
151*/
drh55ef4d92005-08-14 01:20:37 +0000152static CollSeq *findCollSeqEntry(
drhc4a64fa2009-05-11 20:53:28 +0000153 sqlite3 *db, /* Database connection */
154 const char *zName, /* Name of the collating sequence */
155 int create /* Create a new entry if true */
danielk1977fd9a0a42005-05-24 12:01:00 +0000156){
157 CollSeq *pColl;
drhacbcb7e2014-08-21 20:26:37 +0000158 pColl = sqlite3HashFind(&db->aCollSeq, zName);
danielk1977fd9a0a42005-05-24 12:01:00 +0000159
160 if( 0==pColl && create ){
drh3b02e0f2017-07-06 03:06:20 +0000161 int nName = sqlite3Strlen30(zName) + 1;
162 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
danielk1977fd9a0a42005-05-24 12:01:00 +0000163 if( pColl ){
164 CollSeq *pDel = 0;
165 pColl[0].zName = (char*)&pColl[3];
166 pColl[0].enc = SQLITE_UTF8;
167 pColl[1].zName = (char*)&pColl[3];
168 pColl[1].enc = SQLITE_UTF16LE;
169 pColl[2].zName = (char*)&pColl[3];
170 pColl[2].enc = SQLITE_UTF16BE;
171 memcpy(pColl[0].zName, zName, nName);
drhacbcb7e2014-08-21 20:26:37 +0000172 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
danielk1977fd9a0a42005-05-24 12:01:00 +0000173
shanebe217792009-03-05 04:20:31 +0000174 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
danielk1977fd9a0a42005-05-24 12:01:00 +0000175 ** return the pColl pointer to be deleted (because it wasn't added
176 ** to the hash table).
177 */
drhf3a65f72007-08-22 20:18:21 +0000178 assert( pDel==0 || pDel==pColl );
179 if( pDel!=0 ){
drh4a642b62016-02-05 01:55:27 +0000180 sqlite3OomFault(db);
drh633e6d52008-07-28 19:34:53 +0000181 sqlite3DbFree(db, pDel);
drh91171cd2006-03-14 11:08:27 +0000182 pColl = 0;
183 }
danielk1977fd9a0a42005-05-24 12:01:00 +0000184 }
185 }
186 return pColl;
187}
188
189/*
190** Parameter zName points to a UTF-8 encoded string nName bytes long.
191** Return the CollSeq* pointer for the collation sequence named zName
192** for the encoding 'enc' from the database 'db'.
193**
194** If the entry specified is not found and 'create' is true, then create a
195** new entry. Otherwise return NULL.
drha34001c2007-02-02 12:44:37 +0000196**
197** A separate function sqlite3LocateCollSeq() is a wrapper around
198** this routine. sqlite3LocateCollSeq() invokes the collation factory
199** if necessary and generates an error message if the collating sequence
200** cannot be found.
drhc4a64fa2009-05-11 20:53:28 +0000201**
202** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
danielk1977fd9a0a42005-05-24 12:01:00 +0000203*/
204CollSeq *sqlite3FindCollSeq(
205 sqlite3 *db,
206 u8 enc,
207 const char *zName,
danielk1977fd9a0a42005-05-24 12:01:00 +0000208 int create
209){
danielk1977b3bf5562006-01-10 17:58:23 +0000210 CollSeq *pColl;
211 if( zName ){
drhc4a64fa2009-05-11 20:53:28 +0000212 pColl = findCollSeqEntry(db, zName, create);
danielk1977b3bf5562006-01-10 17:58:23 +0000213 }else{
214 pColl = db->pDfltColl;
215 }
danielk1977fd9a0a42005-05-24 12:01:00 +0000216 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
217 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
218 if( pColl ) pColl += enc-1;
219 return pColl;
220}
221
danielk19778c0a7912008-08-20 14:49:23 +0000222/* During the search for the best function definition, this procedure
223** is called to test how well the function passed as the first argument
224** matches the request for a function with nArg arguments in a system
225** that uses encoding enc. The value returned indicates how well the
226** request is matched. A higher value indicates a better match.
227**
drh89d5d6a2012-04-07 00:09:21 +0000228** If nArg is -1 that means to only return a match (non-zero) if p->nArg
229** is also -1. In other words, we are searching for a function that
230** takes a variable number of arguments.
231**
232** If nArg is -2 that means that we are searching for any function
233** regardless of the number of arguments it uses, so return a positive
234** match score for any
235**
drhdfbc3a82009-01-31 22:28:48 +0000236** The returned value is always between 0 and 6, as follows:
danielk19778c0a7912008-08-20 14:49:23 +0000237**
drh89d5d6a2012-04-07 00:09:21 +0000238** 0: Not a match.
239** 1: UTF8/16 conversion required and function takes any number of arguments.
240** 2: UTF16 byte order change required and function takes any number of args.
241** 3: encoding matches and function takes any number of arguments
242** 4: UTF8/16 conversion required - argument count matches exactly
243** 5: UTF16 byte order conversion required - argument count matches exactly
244** 6: Perfect match: encoding and argument count match exactly.
danielk19778c0a7912008-08-20 14:49:23 +0000245**
drh2d801512016-01-14 22:19:58 +0000246** If nArg==(-2) then any function with a non-null xSFunc is
247** a perfect match and any function with xSFunc NULL is
drh89d5d6a2012-04-07 00:09:21 +0000248** a non-match.
danielk19778c0a7912008-08-20 14:49:23 +0000249*/
drh89d5d6a2012-04-07 00:09:21 +0000250#define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
251static int matchQuality(
252 FuncDef *p, /* The function we are evaluating for match quality */
253 int nArg, /* Desired number of arguments. (-1)==any */
254 u8 enc /* Desired text encoding */
255){
256 int match;
257
258 /* nArg of -2 is a special case */
drh2d801512016-01-14 22:19:58 +0000259 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
drh89d5d6a2012-04-07 00:09:21 +0000260
261 /* Wrong number of arguments means "no match" */
262 if( p->nArg!=nArg && p->nArg>=0 ) return 0;
263
264 /* Give a better score to a function with a specific number of arguments
265 ** than to function that accepts any number of arguments. */
266 if( p->nArg==nArg ){
267 match = 4;
268 }else{
danielk19778c0a7912008-08-20 14:49:23 +0000269 match = 1;
danielk19778c0a7912008-08-20 14:49:23 +0000270 }
drh89d5d6a2012-04-07 00:09:21 +0000271
272 /* Bonus points if the text encoding matches */
drhd36e1042013-09-06 13:10:12 +0000273 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
drh89d5d6a2012-04-07 00:09:21 +0000274 match += 2; /* Exact encoding match */
drhd36e1042013-09-06 13:10:12 +0000275 }else if( (enc & p->funcFlags & 2)!=0 ){
drh89d5d6a2012-04-07 00:09:21 +0000276 match += 1; /* Both are UTF16, but with different byte orders */
277 }
278
danielk19778c0a7912008-08-20 14:49:23 +0000279 return match;
280}
281
danielk1977fd9a0a42005-05-24 12:01:00 +0000282/*
drh70a8ca32008-08-21 18:49:27 +0000283** Search a FuncDefHash for a function with the given name. Return
284** a pointer to the matching FuncDef if found, or 0 if there is no match.
285*/
286static FuncDef *functionSearch(
drh70a8ca32008-08-21 18:49:27 +0000287 int h, /* Hash of the name */
drh80738d92016-02-15 00:34:16 +0000288 const char *zFunc /* Name of function */
drh70a8ca32008-08-21 18:49:27 +0000289){
290 FuncDef *p;
drh80738d92016-02-15 00:34:16 +0000291 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
292 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
drh70a8ca32008-08-21 18:49:27 +0000293 return p;
294 }
295 }
296 return 0;
297}
mistachkin8bee11a2018-10-29 17:53:23 +0000298#ifdef SQLITE_ENABLE_NORMALIZE
299FuncDef *sqlite3FunctionSearchN(
300 int h, /* Hash of the name */
301 const char *zFunc, /* Name of function */
302 int nFunc /* Length of the name */
303){
304 FuncDef *p;
305 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
306 if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 ){
307 return p;
308 }
309 }
310 return 0;
311}
312#endif /* SQLITE_ENABLE_NORMALIZE */
drh70a8ca32008-08-21 18:49:27 +0000313
314/*
315** Insert a new FuncDef into a FuncDefHash hash table.
316*/
drh80738d92016-02-15 00:34:16 +0000317void sqlite3InsertBuiltinFuncs(
318 FuncDef *aDef, /* List of global functions to be inserted */
319 int nDef /* Length of the apDef[] list */
drh70a8ca32008-08-21 18:49:27 +0000320){
drh80738d92016-02-15 00:34:16 +0000321 int i;
322 for(i=0; i<nDef; i++){
323 FuncDef *pOther;
324 const char *zName = aDef[i].zName;
325 int nName = sqlite3Strlen30(zName);
mistachkin8bee11a2018-10-29 17:53:23 +0000326 int h = SQLITE_FUNC_HASH(zName[0], nName);
drhebaaa672017-07-06 13:23:26 +0000327 assert( zName[0]>='a' && zName[0]<='z' );
drh80738d92016-02-15 00:34:16 +0000328 pOther = functionSearch(h, zName);
329 if( pOther ){
330 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
331 aDef[i].pNext = pOther->pNext;
332 pOther->pNext = &aDef[i];
333 }else{
334 aDef[i].pNext = 0;
335 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
336 sqlite3BuiltinFunctions.a[h] = &aDef[i];
337 }
drh70a8ca32008-08-21 18:49:27 +0000338 }
339}
340
341
342
343/*
danielk1977fd9a0a42005-05-24 12:01:00 +0000344** Locate a user function given a name, a number of arguments and a flag
345** indicating whether the function prefers UTF-16 over UTF-8. Return a
346** pointer to the FuncDef structure that defines that function, or return
347** NULL if the function does not exist.
348**
349** If the createFlag argument is true, then a new (blank) FuncDef
350** structure is created and liked into the "db" structure if a
drh89d5d6a2012-04-07 00:09:21 +0000351** no matching function previously existed.
danielk1977fd9a0a42005-05-24 12:01:00 +0000352**
drh89d5d6a2012-04-07 00:09:21 +0000353** If nArg is -2, then the first valid function found is returned. A
drh2d801512016-01-14 22:19:58 +0000354** function is valid if xSFunc is non-zero. The nArg==(-2)
drh89d5d6a2012-04-07 00:09:21 +0000355** case is used to see if zName is a valid function name for some number
356** of arguments. If nArg is -2, then createFlag must be 0.
danielk1977fd9a0a42005-05-24 12:01:00 +0000357**
358** If createFlag is false, then a function with the required name and
359** number of arguments may be returned even if the eTextRep flag does not
360** match that requested.
361*/
362FuncDef *sqlite3FindFunction(
363 sqlite3 *db, /* An open database */
drh80738d92016-02-15 00:34:16 +0000364 const char *zName, /* Name of the function. zero-terminated */
danielk1977fd9a0a42005-05-24 12:01:00 +0000365 int nArg, /* Number of arguments. -1 means any number */
366 u8 enc, /* Preferred text encoding */
drh89d5d6a2012-04-07 00:09:21 +0000367 u8 createFlag /* Create new entry if true and does not otherwise exist */
danielk1977fd9a0a42005-05-24 12:01:00 +0000368){
369 FuncDef *p; /* Iterator variable */
danielk1977fd9a0a42005-05-24 12:01:00 +0000370 FuncDef *pBest = 0; /* Best match found so far */
drh70a8ca32008-08-21 18:49:27 +0000371 int bestScore = 0; /* Score of best match */
372 int h; /* Hash value */
drh80738d92016-02-15 00:34:16 +0000373 int nName; /* Length of the name */
danielk1977fd9a0a42005-05-24 12:01:00 +0000374
drh89d5d6a2012-04-07 00:09:21 +0000375 assert( nArg>=(-2) );
376 assert( nArg>=(-1) || createFlag==0 );
drh80738d92016-02-15 00:34:16 +0000377 nName = sqlite3Strlen30(zName);
danielk1977fd9a0a42005-05-24 12:01:00 +0000378
drhe3602be2008-09-09 12:31:33 +0000379 /* First search for a match amongst the application-defined functions.
380 */
drh80738d92016-02-15 00:34:16 +0000381 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
drh70a8ca32008-08-21 18:49:27 +0000382 while( p ){
383 int score = matchQuality(p, nArg, enc);
384 if( score>bestScore ){
danielk19778c0a7912008-08-20 14:49:23 +0000385 pBest = p;
drh70a8ca32008-08-21 18:49:27 +0000386 bestScore = score;
danielk19778c0a7912008-08-20 14:49:23 +0000387 }
drh70a8ca32008-08-21 18:49:27 +0000388 p = p->pNext;
danielk19778c0a7912008-08-20 14:49:23 +0000389 }
danielk1977fd9a0a42005-05-24 12:01:00 +0000390
drhe3602be2008-09-09 12:31:33 +0000391 /* If no match is found, search the built-in functions.
392 **
drh8257aa82017-07-26 19:59:13 +0000393 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
drh545f5872010-04-24 14:02:59 +0000394 ** functions even if a prior app-defined function was found. And give
395 ** priority to built-in functions.
396 **
drhe3602be2008-09-09 12:31:33 +0000397 ** Except, if createFlag is true, that means that we are trying to
drh6c5cecb2010-09-16 19:49:22 +0000398 ** install a new function. Whatever FuncDef structure is returned it will
drhe3602be2008-09-09 12:31:33 +0000399 ** have fields overwritten with new information appropriate for the
400 ** new function. But the FuncDefs for built-in functions are read-only.
401 ** So we must not search for built-ins when creating a new function.
danielk19778c0a7912008-08-20 14:49:23 +0000402 */
drh8257aa82017-07-26 19:59:13 +0000403 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
drh545f5872010-04-24 14:02:59 +0000404 bestScore = 0;
mistachkin8bee11a2018-10-29 17:53:23 +0000405 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
drh80738d92016-02-15 00:34:16 +0000406 p = functionSearch(h, zName);
drh70a8ca32008-08-21 18:49:27 +0000407 while( p ){
408 int score = matchQuality(p, nArg, enc);
409 if( score>bestScore ){
410 pBest = p;
411 bestScore = score;
danielk1977fd9a0a42005-05-24 12:01:00 +0000412 }
drh70a8ca32008-08-21 18:49:27 +0000413 p = p->pNext;
danielk1977fd9a0a42005-05-24 12:01:00 +0000414 }
415 }
416
drhe3602be2008-09-09 12:31:33 +0000417 /* If the createFlag parameter is true and the search did not reveal an
danielk1977fd9a0a42005-05-24 12:01:00 +0000418 ** exact match for the name, number of arguments and encoding, then add a
419 ** new entry to the hash table and return it.
420 */
drh89d5d6a2012-04-07 00:09:21 +0000421 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
danielk19778c0a7912008-08-20 14:49:23 +0000422 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
drh80738d92016-02-15 00:34:16 +0000423 FuncDef *pOther;
drh92011842018-05-26 16:00:26 +0000424 u8 *z;
drh6ad224e2016-02-24 19:57:11 +0000425 pBest->zName = (const char*)&pBest[1];
drh1bd10f82008-12-10 21:19:56 +0000426 pBest->nArg = (u16)nArg;
drhd36e1042013-09-06 13:10:12 +0000427 pBest->funcFlags = enc;
drh6ad224e2016-02-24 19:57:11 +0000428 memcpy((char*)&pBest[1], zName, nName+1);
drh92011842018-05-26 16:00:26 +0000429 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
drh80738d92016-02-15 00:34:16 +0000430 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
431 if( pOther==pBest ){
432 sqlite3DbFree(db, pBest);
433 sqlite3OomFault(db);
434 return 0;
435 }else{
436 pBest->pNext = pOther;
437 }
danielk1977fd9a0a42005-05-24 12:01:00 +0000438 }
439
drh2d801512016-01-14 22:19:58 +0000440 if( pBest && (pBest->xSFunc || createFlag) ){
danielk1977fd9a0a42005-05-24 12:01:00 +0000441 return pBest;
442 }
443 return 0;
444}
drh03b808a2006-03-13 15:06:05 +0000445
446/*
447** Free all resources held by the schema structure. The void* argument points
drh633e6d52008-07-28 19:34:53 +0000448** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
drhb6ee6602011-04-04 13:40:53 +0000449** pointer itself, it just cleans up subsidiary resources (i.e. the contents
drh03b808a2006-03-13 15:06:05 +0000450** of the schema hash tables).
danielk19778cf6c552008-06-23 16:53:46 +0000451**
452** The Schema.cache_size variable is not cleared.
drh03b808a2006-03-13 15:06:05 +0000453*/
drhb6ee6602011-04-04 13:40:53 +0000454void sqlite3SchemaClear(void *p){
drh03b808a2006-03-13 15:06:05 +0000455 Hash temp1;
456 Hash temp2;
457 HashElem *pElem;
458 Schema *pSchema = (Schema *)p;
459
460 temp1 = pSchema->tblHash;
461 temp2 = pSchema->trigHash;
drhe61922a2009-05-02 13:29:37 +0000462 sqlite3HashInit(&pSchema->trigHash);
drh03b808a2006-03-13 15:06:05 +0000463 sqlite3HashClear(&pSchema->idxHash);
464 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
drh633e6d52008-07-28 19:34:53 +0000465 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
drh03b808a2006-03-13 15:06:05 +0000466 }
467 sqlite3HashClear(&temp2);
drhe61922a2009-05-02 13:29:37 +0000468 sqlite3HashInit(&pSchema->tblHash);
drh03b808a2006-03-13 15:06:05 +0000469 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
470 Table *pTab = sqliteHashData(pElem);
dan1feeaed2010-07-23 15:41:47 +0000471 sqlite3DeleteTable(0, pTab);
drh03b808a2006-03-13 15:06:05 +0000472 }
473 sqlite3HashClear(&temp1);
dan1da40a32009-09-19 17:00:31 +0000474 sqlite3HashClear(&pSchema->fkeyHash);
drh03b808a2006-03-13 15:06:05 +0000475 pSchema->pSeqTab = 0;
drh2c5e35f2014-08-05 11:04:21 +0000476 if( pSchema->schemaFlags & DB_SchemaLoaded ){
drhc2a75552011-03-18 21:55:46 +0000477 pSchema->iGeneration++;
drhc2a75552011-03-18 21:55:46 +0000478 }
drhdc6b41e2017-08-17 02:26:35 +0000479 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
drh03b808a2006-03-13 15:06:05 +0000480}
481
482/*
483** Find and return the schema associated with a BTree. Create
484** a new one if necessary.
485*/
drh17435752007-08-16 04:30:38 +0000486Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
drh03b808a2006-03-13 15:06:05 +0000487 Schema * p;
488 if( pBt ){
drhb6ee6602011-04-04 13:40:53 +0000489 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
drh03b808a2006-03-13 15:06:05 +0000490 }else{
drhb9755982010-07-24 16:34:37 +0000491 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
drh03b808a2006-03-13 15:06:05 +0000492 }
danielk1977a1644fd2007-08-29 12:31:25 +0000493 if( !p ){
drh4a642b62016-02-05 01:55:27 +0000494 sqlite3OomFault(db);
danielk1977a1644fd2007-08-29 12:31:25 +0000495 }else if ( 0==p->file_format ){
drhe61922a2009-05-02 13:29:37 +0000496 sqlite3HashInit(&p->tblHash);
497 sqlite3HashInit(&p->idxHash);
498 sqlite3HashInit(&p->trigHash);
dan1da40a32009-09-19 17:00:31 +0000499 sqlite3HashInit(&p->fkeyHash);
drhf012ea32006-05-24 12:43:26 +0000500 p->enc = SQLITE_UTF8;
drh03b808a2006-03-13 15:06:05 +0000501 }
502 return p;
503}