xref: /sqlite-3.40.0/src/callback.c (revision bd41d566)
1 /*
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.
15 */
16 
17 #include "sqliteInt.h"
18 
19 /*
20 ** Invoke the 'collation needed' callback to request a collation sequence
21 ** in the encoding enc of name zName, length nName.
22 */
23 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
24   assert( !db->xCollNeeded || !db->xCollNeeded16 );
25   if( db->xCollNeeded ){
26     char *zExternal = sqlite3DbStrDup(db, zName);
27     if( !zExternal ) return;
28     db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
29     sqlite3DbFree(db, zExternal);
30   }
31 #ifndef SQLITE_OMIT_UTF16
32   if( db->xCollNeeded16 ){
33     char const *zExternal;
34     sqlite3_value *pTmp = sqlite3ValueNew(db);
35     sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
36     zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
37     if( zExternal ){
38       db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
39     }
40     sqlite3ValueFree(pTmp);
41   }
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 */
52 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
53   CollSeq *pColl2;
54   char *z = pColl->zName;
55   int i;
56   static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
57   for(i=0; i<3; i++){
58     pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
59     if( pColl2->xCmp!=0 ){
60       memcpy(pColl, pColl2, sizeof(CollSeq));
61       pColl->xDel = 0;         /* Do not copy the destructor */
62       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
71 ** requested collation sequence is not available in the desired encoding.
72 **
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
78 ** sequence can be found.  If no collation is found, leave an error message.
79 **
80 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
81 */
82 CollSeq *sqlite3GetCollSeq(
83   Parse *pParse,        /* Parsing context */
84   u8 enc,               /* The desired encoding for the collating sequence */
85   CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
86   const char *zName     /* Collating sequence name */
87 ){
88   CollSeq *p;
89   sqlite3 *db = pParse->db;
90 
91   p = pColl;
92   if( !p ){
93     p = sqlite3FindCollSeq(db, enc, zName, 0);
94   }
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     */
99     callCollNeeded(db, enc, zName);
100     p = sqlite3FindCollSeq(db, enc, zName, 0);
101   }
102   if( p && !p->xCmp && synthCollSeq(db, p) ){
103     p = 0;
104   }
105   assert( !p || p->xCmp );
106   if( p==0 ){
107     sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
108   }
109   return p;
110 }
111 
112 /*
113 ** This routine is called on a collation sequence before it is used to
114 ** check that it is defined. An undefined collation sequence exists when
115 ** a database is loaded that contains references to collation sequences
116 ** that have not been defined by sqlite3_create_collation() etc.
117 **
118 ** If required, this routine calls the 'collation needed' callback to
119 ** request a definition of the collating sequence. If this doesn't work,
120 ** an equivalent collating sequence that uses a text encoding different
121 ** from the main database is substituted, if one is available.
122 */
123 int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
124   if( pColl ){
125     const char *zName = pColl->zName;
126     sqlite3 *db = pParse->db;
127     CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
128     if( !p ){
129       return SQLITE_ERROR;
130     }
131     assert( p==pColl );
132   }
133   return SQLITE_OK;
134 }
135 
136 
137 
138 /*
139 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
140 ** specified by zName and nName is not found and parameter 'create' is
141 ** true, then create a new entry. Otherwise return NULL.
142 **
143 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
144 ** array of three CollSeq structures. The first is the collation sequence
145 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
146 **
147 ** Stored immediately after the three collation sequences is a copy of
148 ** the collation sequence name. A pointer to this string is stored in
149 ** each collation sequence structure.
150 */
151 static CollSeq *findCollSeqEntry(
152   sqlite3 *db,          /* Database connection */
153   const char *zName,    /* Name of the collating sequence */
154   int create            /* Create a new entry if true */
155 ){
156   CollSeq *pColl;
157   pColl = sqlite3HashFind(&db->aCollSeq, zName);
158 
159   if( 0==pColl && create ){
160     int nName = sqlite3Strlen30(zName);
161     pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1);
162     if( pColl ){
163       CollSeq *pDel = 0;
164       pColl[0].zName = (char*)&pColl[3];
165       pColl[0].enc = SQLITE_UTF8;
166       pColl[1].zName = (char*)&pColl[3];
167       pColl[1].enc = SQLITE_UTF16LE;
168       pColl[2].zName = (char*)&pColl[3];
169       pColl[2].enc = SQLITE_UTF16BE;
170       memcpy(pColl[0].zName, zName, nName);
171       pColl[0].zName[nName] = 0;
172       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
173 
174       /* If a malloc() failure occurred in sqlite3HashInsert(), it will
175       ** return the pColl pointer to be deleted (because it wasn't added
176       ** to the hash table).
177       */
178       assert( pDel==0 || pDel==pColl );
179       if( pDel!=0 ){
180         db->mallocFailed = 1;
181         sqlite3DbFree(db, pDel);
182         pColl = 0;
183       }
184     }
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.
196 **
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.
201 **
202 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
203 */
204 CollSeq *sqlite3FindCollSeq(
205   sqlite3 *db,
206   u8 enc,
207   const char *zName,
208   int create
209 ){
210   CollSeq *pColl;
211   if( zName ){
212     pColl = findCollSeqEntry(db, zName, create);
213   }else{
214     pColl = db->pDfltColl;
215   }
216   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 
222 /* 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 **
228 ** 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 **
236 ** The returned value is always between 0 and 6, as follows:
237 **
238 ** 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.
245 **
246 ** If nArg==(-2) then any function with a non-null xStep or xFunc is
247 ** a perfect match and any function with both xStep and xFunc NULL is
248 ** a non-match.
249 */
250 #define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
251 static 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 */
259   if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
260 
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{
269     match = 1;
270   }
271 
272   /* Bonus points if the text encoding matches */
273   if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
274     match += 2;  /* Exact encoding match */
275   }else if( (enc & p->funcFlags & 2)!=0 ){
276     match += 1;  /* Both are UTF16, but with different byte orders */
277   }
278 
279   return match;
280 }
281 
282 /*
283 ** 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 */
286 static FuncDef *functionSearch(
287   FuncDefHash *pHash,  /* Hash table to search */
288   int h,               /* Hash of the name */
289   const char *zFunc,   /* Name of function */
290   int nFunc            /* Number of bytes in zFunc */
291 ){
292   FuncDef *p;
293   for(p=pHash->a[h]; p; p=p->pHash){
294     if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
295       return p;
296     }
297   }
298   return 0;
299 }
300 
301 /*
302 ** Insert a new FuncDef into a FuncDefHash hash table.
303 */
304 void sqlite3FuncDefInsert(
305   FuncDefHash *pHash,  /* The hash table into which to insert */
306   FuncDef *pDef        /* The function definition to insert */
307 ){
308   FuncDef *pOther;
309   int nName = sqlite3Strlen30(pDef->zName);
310   u8 c1 = (u8)pDef->zName[0];
311   int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
312   pOther = functionSearch(pHash, h, pDef->zName, nName);
313   if( pOther ){
314     assert( pOther!=pDef && pOther->pNext!=pDef );
315     pDef->pNext = pOther->pNext;
316     pOther->pNext = pDef;
317   }else{
318     pDef->pNext = 0;
319     pDef->pHash = pHash->a[h];
320     pHash->a[h] = pDef;
321   }
322 }
323 
324 
325 
326 /*
327 ** Locate a user function given a name, a number of arguments and a flag
328 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
329 ** pointer to the FuncDef structure that defines that function, or return
330 ** NULL if the function does not exist.
331 **
332 ** If the createFlag argument is true, then a new (blank) FuncDef
333 ** structure is created and liked into the "db" structure if a
334 ** no matching function previously existed.
335 **
336 ** If nArg is -2, then the first valid function found is returned.  A
337 ** function is valid if either xFunc or xStep is non-zero.  The nArg==(-2)
338 ** case is used to see if zName is a valid function name for some number
339 ** of arguments.  If nArg is -2, then createFlag must be 0.
340 **
341 ** If createFlag is false, then a function with the required name and
342 ** number of arguments may be returned even if the eTextRep flag does not
343 ** match that requested.
344 */
345 FuncDef *sqlite3FindFunction(
346   sqlite3 *db,       /* An open database */
347   const char *zName, /* Name of the function.  Not null-terminated */
348   int nName,         /* Number of characters in the name */
349   int nArg,          /* Number of arguments.  -1 means any number */
350   u8 enc,            /* Preferred text encoding */
351   u8 createFlag      /* Create new entry if true and does not otherwise exist */
352 ){
353   FuncDef *p;         /* Iterator variable */
354   FuncDef *pBest = 0; /* Best match found so far */
355   int bestScore = 0;  /* Score of best match */
356   int h;              /* Hash value */
357 
358   assert( nArg>=(-2) );
359   assert( nArg>=(-1) || createFlag==0 );
360   h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
361 
362   /* First search for a match amongst the application-defined functions.
363   */
364   p = functionSearch(&db->aFunc, h, zName, nName);
365   while( p ){
366     int score = matchQuality(p, nArg, enc);
367     if( score>bestScore ){
368       pBest = p;
369       bestScore = score;
370     }
371     p = p->pNext;
372   }
373 
374   /* If no match is found, search the built-in functions.
375   **
376   ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
377   ** functions even if a prior app-defined function was found.  And give
378   ** priority to built-in functions.
379   **
380   ** Except, if createFlag is true, that means that we are trying to
381   ** install a new function.  Whatever FuncDef structure is returned it will
382   ** have fields overwritten with new information appropriate for the
383   ** new function.  But the FuncDefs for built-in functions are read-only.
384   ** So we must not search for built-ins when creating a new function.
385   */
386   if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
387     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
388     bestScore = 0;
389     p = functionSearch(pHash, h, zName, nName);
390     while( p ){
391       int score = matchQuality(p, nArg, enc);
392       if( score>bestScore ){
393         pBest = p;
394         bestScore = score;
395       }
396       p = p->pNext;
397     }
398   }
399 
400   /* If the createFlag parameter is true and the search did not reveal an
401   ** exact match for the name, number of arguments and encoding, then add a
402   ** new entry to the hash table and return it.
403   */
404   if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
405       (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
406     pBest->zName = (char *)&pBest[1];
407     pBest->nArg = (u16)nArg;
408     pBest->funcFlags = enc;
409     memcpy(pBest->zName, zName, nName);
410     pBest->zName[nName] = 0;
411     sqlite3FuncDefInsert(&db->aFunc, pBest);
412   }
413 
414   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
415     return pBest;
416   }
417   return 0;
418 }
419 
420 /*
421 ** Free all resources held by the schema structure. The void* argument points
422 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
423 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
424 ** of the schema hash tables).
425 **
426 ** The Schema.cache_size variable is not cleared.
427 */
428 void sqlite3SchemaClear(void *p){
429   Hash temp1;
430   Hash temp2;
431   HashElem *pElem;
432   Schema *pSchema = (Schema *)p;
433 
434   temp1 = pSchema->tblHash;
435   temp2 = pSchema->trigHash;
436   sqlite3HashInit(&pSchema->trigHash);
437   sqlite3HashClear(&pSchema->idxHash);
438   for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
439     sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
440   }
441   sqlite3HashClear(&temp2);
442   sqlite3HashInit(&pSchema->tblHash);
443   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
444     Table *pTab = sqliteHashData(pElem);
445     sqlite3DeleteTable(0, pTab);
446   }
447   sqlite3HashClear(&temp1);
448   sqlite3HashClear(&pSchema->fkeyHash);
449   pSchema->pSeqTab = 0;
450   if( pSchema->schemaFlags & DB_SchemaLoaded ){
451     pSchema->iGeneration++;
452     pSchema->schemaFlags &= ~DB_SchemaLoaded;
453   }
454 }
455 
456 /*
457 ** Find and return the schema associated with a BTree.  Create
458 ** a new one if necessary.
459 */
460 Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
461   Schema * p;
462   if( pBt ){
463     p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
464   }else{
465     p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
466   }
467   if( !p ){
468     db->mallocFailed = 1;
469   }else if ( 0==p->file_format ){
470     sqlite3HashInit(&p->tblHash);
471     sqlite3HashInit(&p->idxHash);
472     sqlite3HashInit(&p->trigHash);
473     sqlite3HashInit(&p->fkeyHash);
474     p->enc = SQLITE_UTF8;
475   }
476   return p;
477 }
478