blob: d25836b4357cda3c124f217024bc07e45ec30f87 [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.
15**
drh70a8ca32008-08-21 18:49:27 +000016** $Id: callback.c,v 1.28 2008/08/21 18:49:28 drh Exp $
danielk1977fd9a0a42005-05-24 12:01:00 +000017*/
18
19#include "sqliteInt.h"
20
21/*
danielk19774dade032005-05-25 10:45:10 +000022** Invoke the 'collation needed' callback to request a collation sequence
23** in the database text encoding of name zName, length nName.
24** If the collation sequence
25*/
26static void callCollNeeded(sqlite3 *db, const char *zName, int nName){
27 assert( !db->xCollNeeded || !db->xCollNeeded16 );
drh0a687d12008-07-08 14:52:07 +000028 if( nName<0 ) nName = sqlite3Strlen(db, zName);
danielk19774dade032005-05-25 10:45:10 +000029 if( db->xCollNeeded ){
drh17435752007-08-16 04:30:38 +000030 char *zExternal = sqlite3DbStrNDup(db, zName, nName);
danielk19774dade032005-05-25 10:45:10 +000031 if( !zExternal ) return;
danielk197714db2662006-01-09 16:12:04 +000032 db->xCollNeeded(db->pCollNeededArg, db, (int)ENC(db), zExternal);
drh633e6d52008-07-28 19:34:53 +000033 sqlite3DbFree(db, zExternal);
danielk19774dade032005-05-25 10:45:10 +000034 }
35#ifndef SQLITE_OMIT_UTF16
36 if( db->xCollNeeded16 ){
37 char const *zExternal;
danielk19771e536952007-08-16 10:09:01 +000038 sqlite3_value *pTmp = sqlite3ValueNew(db);
drhb21c8cd2007-08-21 19:33:56 +000039 sqlite3ValueSetStr(pTmp, nName, zName, SQLITE_UTF8, SQLITE_STATIC);
40 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
drh26abcb12005-12-14 22:51:16 +000041 if( zExternal ){
danielk197714db2662006-01-09 16:12:04 +000042 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
drh26abcb12005-12-14 22:51:16 +000043 }
44 sqlite3ValueFree(pTmp);
danielk19774dade032005-05-25 10:45:10 +000045 }
46#endif
47}
48
49/*
50** This routine is called if the collation factory fails to deliver a
51** collation function in the best encoding but there may be other versions
52** of this collation function (for other text encodings) available. Use one
53** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
54** possible.
55*/
56static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
57 CollSeq *pColl2;
58 char *z = pColl->zName;
59 int n = strlen(z);
60 int i;
61 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
62 for(i=0; i<3; i++){
63 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
64 if( pColl2->xCmp!=0 ){
65 memcpy(pColl, pColl2, sizeof(CollSeq));
danielk1977a9808b32007-05-07 09:32:45 +000066 pColl->xDel = 0; /* Do not copy the destructor */
danielk19774dade032005-05-25 10:45:10 +000067 return SQLITE_OK;
68 }
69 }
70 return SQLITE_ERROR;
71}
72
73/*
74** This function is responsible for invoking the collation factory callback
75** or substituting a collation sequence of a different encoding when the
76** requested collation sequence is not available in the database native
77** encoding.
78**
79** If it is not NULL, then pColl must point to the database native encoding
80** collation sequence with name zName, length nName.
81**
82** The return value is either the collation sequence to be used in database
83** db for collation type name zName, length nName, or NULL, if no collation
84** sequence can be found.
85*/
86CollSeq *sqlite3GetCollSeq(
87 sqlite3* db,
88 CollSeq *pColl,
89 const char *zName,
90 int nName
91){
92 CollSeq *p;
93
94 p = pColl;
95 if( !p ){
danielk197714db2662006-01-09 16:12:04 +000096 p = sqlite3FindCollSeq(db, ENC(db), zName, nName, 0);
danielk19774dade032005-05-25 10:45:10 +000097 }
98 if( !p || !p->xCmp ){
99 /* No collation sequence of this type for this encoding is registered.
100 ** Call the collation factory to see if it can supply us with one.
101 */
102 callCollNeeded(db, zName, nName);
danielk197714db2662006-01-09 16:12:04 +0000103 p = sqlite3FindCollSeq(db, ENC(db), zName, nName, 0);
danielk19774dade032005-05-25 10:45:10 +0000104 }
105 if( p && !p->xCmp && synthCollSeq(db, p) ){
106 p = 0;
107 }
108 assert( !p || p->xCmp );
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*/
123int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
124 if( pColl ){
125 const char *zName = pColl->zName;
126 CollSeq *p = sqlite3GetCollSeq(pParse->db, pColl, zName, -1);
127 if( !p ){
128 if( pParse->nErr==0 ){
129 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
130 }
131 pParse->nErr++;
132 return SQLITE_ERROR;
133 }
danielk1977b3bf5562006-01-10 17:58:23 +0000134 assert( p==pColl );
danielk19774dade032005-05-25 10:45:10 +0000135 }
136 return SQLITE_OK;
137}
138
139
140
141/*
danielk1977fd9a0a42005-05-24 12:01:00 +0000142** Locate and return an entry from the db.aCollSeq hash table. If the entry
143** specified by zName and nName is not found and parameter 'create' is
144** true, then create a new entry. Otherwise return NULL.
145**
146** Each pointer stored in the sqlite3.aCollSeq hash table contains an
147** array of three CollSeq structures. The first is the collation sequence
148** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
149**
150** Stored immediately after the three collation sequences is a copy of
151** the collation sequence name. A pointer to this string is stored in
152** each collation sequence structure.
153*/
drh55ef4d92005-08-14 01:20:37 +0000154static CollSeq *findCollSeqEntry(
danielk1977fd9a0a42005-05-24 12:01:00 +0000155 sqlite3 *db,
156 const char *zName,
157 int nName,
158 int create
159){
160 CollSeq *pColl;
drh0a687d12008-07-08 14:52:07 +0000161 if( nName<0 ) nName = sqlite3Strlen(db, zName);
danielk1977fd9a0a42005-05-24 12:01:00 +0000162 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
163
164 if( 0==pColl && create ){
drh17435752007-08-16 04:30:38 +0000165 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
danielk1977fd9a0a42005-05-24 12:01:00 +0000166 if( pColl ){
167 CollSeq *pDel = 0;
168 pColl[0].zName = (char*)&pColl[3];
169 pColl[0].enc = SQLITE_UTF8;
170 pColl[1].zName = (char*)&pColl[3];
171 pColl[1].enc = SQLITE_UTF16LE;
172 pColl[2].zName = (char*)&pColl[3];
173 pColl[2].enc = SQLITE_UTF16BE;
174 memcpy(pColl[0].zName, zName, nName);
175 pColl[0].zName[nName] = 0;
176 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
177
178 /* If a malloc() failure occured in sqlite3HashInsert(), it will
179 ** return the pColl pointer to be deleted (because it wasn't added
180 ** to the hash table).
181 */
drhf3a65f72007-08-22 20:18:21 +0000182 assert( pDel==0 || pDel==pColl );
183 if( pDel!=0 ){
184 db->mallocFailed = 1;
drh633e6d52008-07-28 19:34:53 +0000185 sqlite3DbFree(db, pDel);
drh91171cd2006-03-14 11:08:27 +0000186 pColl = 0;
187 }
danielk1977fd9a0a42005-05-24 12:01:00 +0000188 }
189 }
190 return pColl;
191}
192
193/*
194** Parameter zName points to a UTF-8 encoded string nName bytes long.
195** Return the CollSeq* pointer for the collation sequence named zName
196** for the encoding 'enc' from the database 'db'.
197**
198** If the entry specified is not found and 'create' is true, then create a
199** new entry. Otherwise return NULL.
drha34001c2007-02-02 12:44:37 +0000200**
201** A separate function sqlite3LocateCollSeq() is a wrapper around
202** this routine. sqlite3LocateCollSeq() invokes the collation factory
203** if necessary and generates an error message if the collating sequence
204** cannot be found.
danielk1977fd9a0a42005-05-24 12:01:00 +0000205*/
206CollSeq *sqlite3FindCollSeq(
207 sqlite3 *db,
208 u8 enc,
209 const char *zName,
210 int nName,
211 int create
212){
danielk1977b3bf5562006-01-10 17:58:23 +0000213 CollSeq *pColl;
214 if( zName ){
215 pColl = findCollSeqEntry(db, zName, nName, create);
216 }else{
217 pColl = db->pDfltColl;
218 }
danielk1977fd9a0a42005-05-24 12:01:00 +0000219 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
220 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
221 if( pColl ) pColl += enc-1;
222 return pColl;
223}
224
danielk19778c0a7912008-08-20 14:49:23 +0000225/* During the search for the best function definition, this procedure
226** is called to test how well the function passed as the first argument
227** matches the request for a function with nArg arguments in a system
228** that uses encoding enc. The value returned indicates how well the
229** request is matched. A higher value indicates a better match.
230**
231** The returned value is always between 1 and 6, as follows:
232**
233** 1: A variable arguments function that prefers UTF-8 when a UTF-16
234** encoding is requested, or vice versa.
235** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
236** requested, or vice versa.
237** 3: A variable arguments function using the same text encoding.
238** 4: A function with the exact number of arguments requested that
239** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
240** 5: A function with the exact number of arguments requested that
241** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
242** 6: An exact match.
243**
244*/
245static int matchQuality(FuncDef *p, int nArg, u8 enc){
246 int match = 0;
247 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
248 match = 1;
249 if( p->nArg==nArg || nArg==-1 ){
250 match = 4;
251 }
252 if( enc==p->iPrefEnc ){
253 match += 2;
254 }
255 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
256 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
257 match += 1;
258 }
259 }
260 return match;
261}
262
danielk1977fd9a0a42005-05-24 12:01:00 +0000263/*
drh70a8ca32008-08-21 18:49:27 +0000264** Search a FuncDefHash for a function with the given name. Return
265** a pointer to the matching FuncDef if found, or 0 if there is no match.
266*/
267static FuncDef *functionSearch(
268 FuncDefHash *pHash, /* Hash table to search */
269 int h, /* Hash of the name */
270 const char *zFunc, /* Name of function */
271 int nFunc /* Number of bytes in zFunc */
272){
273 FuncDef *p;
274 for(p=pHash->a[h]; p; p=p->pHash){
275 if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
276 return p;
277 }
278 }
279 return 0;
280}
281
282/*
283** Insert a new FuncDef into a FuncDefHash hash table.
284*/
285void sqlite3FuncDefInsert(
286 FuncDefHash *pHash, /* The hash table into which to insert */
287 FuncDef *pDef /* The function definition to insert */
288){
289 FuncDef *pOther;
290 int nName = strlen(pDef->zName);
291 u8 c1 = (u8)pDef->zName[0];
292 int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
293 pOther = functionSearch(pHash, h, pDef->zName, nName);
294 if( pOther ){
295 pDef->pNext = pOther->pNext;
296 pOther->pNext = pDef;
297 }else{
298 pDef->pNext = 0;
299 pDef->pHash = pHash->a[h];
300 pHash->a[h] = pDef;
301 }
302}
303
304
305
306/*
danielk1977fd9a0a42005-05-24 12:01:00 +0000307** Locate a user function given a name, a number of arguments and a flag
308** indicating whether the function prefers UTF-16 over UTF-8. Return a
309** pointer to the FuncDef structure that defines that function, or return
310** NULL if the function does not exist.
311**
312** If the createFlag argument is true, then a new (blank) FuncDef
313** structure is created and liked into the "db" structure if a
314** no matching function previously existed. When createFlag is true
315** and the nArg parameter is -1, then only a function that accepts
316** any number of arguments will be returned.
317**
318** If createFlag is false and nArg is -1, then the first valid
319** function found is returned. A function is valid if either xFunc
320** or xStep is non-zero.
321**
322** If createFlag is false, then a function with the required name and
323** number of arguments may be returned even if the eTextRep flag does not
324** match that requested.
325*/
326FuncDef *sqlite3FindFunction(
327 sqlite3 *db, /* An open database */
328 const char *zName, /* Name of the function. Not null-terminated */
329 int nName, /* Number of characters in the name */
330 int nArg, /* Number of arguments. -1 means any number */
331 u8 enc, /* Preferred text encoding */
332 int createFlag /* Create new entry if true and does not otherwise exist */
333){
334 FuncDef *p; /* Iterator variable */
danielk1977fd9a0a42005-05-24 12:01:00 +0000335 FuncDef *pBest = 0; /* Best match found so far */
drh70a8ca32008-08-21 18:49:27 +0000336 int bestScore = 0; /* Score of best match */
337 int h; /* Hash value */
danielk1977fd9a0a42005-05-24 12:01:00 +0000338
339
340 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
341 if( nArg<-1 ) nArg = -1;
drh70a8ca32008-08-21 18:49:27 +0000342 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
danielk1977fd9a0a42005-05-24 12:01:00 +0000343
drh70a8ca32008-08-21 18:49:27 +0000344
345 p = functionSearch(&db->aFunc, h, zName, nName);
346 while( p ){
347 int score = matchQuality(p, nArg, enc);
348 if( score>bestScore ){
danielk19778c0a7912008-08-20 14:49:23 +0000349 pBest = p;
drh70a8ca32008-08-21 18:49:27 +0000350 bestScore = score;
danielk19778c0a7912008-08-20 14:49:23 +0000351 }
drh70a8ca32008-08-21 18:49:27 +0000352 p = p->pNext;
danielk19778c0a7912008-08-20 14:49:23 +0000353 }
danielk1977fd9a0a42005-05-24 12:01:00 +0000354
danielk19778c0a7912008-08-20 14:49:23 +0000355 /* If the createFlag parameter is false and no match was found amongst
356 ** the custom functions stored in sqlite3.aFunc, try to find a built-in
357 ** function to use.
358 */
359 if( !createFlag && !pBest ){
drh70a8ca32008-08-21 18:49:27 +0000360 p = functionSearch(&sqlite3FuncBuiltins, h, zName, nName);
361 while( p ){
362 int score = matchQuality(p, nArg, enc);
363 if( score>bestScore ){
364 pBest = p;
365 bestScore = score;
danielk1977fd9a0a42005-05-24 12:01:00 +0000366 }
drh70a8ca32008-08-21 18:49:27 +0000367 p = p->pNext;
danielk1977fd9a0a42005-05-24 12:01:00 +0000368 }
369 }
370
371 /* If the createFlag parameter is true, and the seach did not reveal an
372 ** exact match for the name, number of arguments and encoding, then add a
373 ** new entry to the hash table and return it.
374 */
drh70a8ca32008-08-21 18:49:27 +0000375 if( createFlag && bestScore<6 &&
danielk19778c0a7912008-08-20 14:49:23 +0000376 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
377 pBest->zName = (char *)&pBest[1];
danielk1977fd9a0a42005-05-24 12:01:00 +0000378 pBest->nArg = nArg;
danielk1977fd9a0a42005-05-24 12:01:00 +0000379 pBest->iPrefEnc = enc;
380 memcpy(pBest->zName, zName, nName);
381 pBest->zName[nName] = 0;
drh70a8ca32008-08-21 18:49:27 +0000382 sqlite3FuncDefInsert(&db->aFunc, pBest);
danielk1977fd9a0a42005-05-24 12:01:00 +0000383 }
384
385 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
386 return pBest;
387 }
388 return 0;
389}
drh03b808a2006-03-13 15:06:05 +0000390
391/*
392** Free all resources held by the schema structure. The void* argument points
drh633e6d52008-07-28 19:34:53 +0000393** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
drh03b808a2006-03-13 15:06:05 +0000394** pointer itself, it just cleans up subsiduary resources (i.e. the contents
395** of the schema hash tables).
danielk19778cf6c552008-06-23 16:53:46 +0000396**
397** The Schema.cache_size variable is not cleared.
drh03b808a2006-03-13 15:06:05 +0000398*/
399void sqlite3SchemaFree(void *p){
400 Hash temp1;
401 Hash temp2;
402 HashElem *pElem;
403 Schema *pSchema = (Schema *)p;
404
405 temp1 = pSchema->tblHash;
406 temp2 = pSchema->trigHash;
407 sqlite3HashInit(&pSchema->trigHash, SQLITE_HASH_STRING, 0);
408 sqlite3HashClear(&pSchema->aFKey);
409 sqlite3HashClear(&pSchema->idxHash);
410 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
drh633e6d52008-07-28 19:34:53 +0000411 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
drh03b808a2006-03-13 15:06:05 +0000412 }
413 sqlite3HashClear(&temp2);
414 sqlite3HashInit(&pSchema->tblHash, SQLITE_HASH_STRING, 0);
415 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
416 Table *pTab = sqliteHashData(pElem);
danielk1977a04a34f2007-04-16 15:06:25 +0000417 sqlite3DeleteTable(pTab);
drh03b808a2006-03-13 15:06:05 +0000418 }
419 sqlite3HashClear(&temp1);
420 pSchema->pSeqTab = 0;
421 pSchema->flags &= ~DB_SchemaLoaded;
422}
423
424/*
425** Find and return the schema associated with a BTree. Create
426** a new one if necessary.
427*/
drh17435752007-08-16 04:30:38 +0000428Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
drh03b808a2006-03-13 15:06:05 +0000429 Schema * p;
430 if( pBt ){
danielk1977a1644fd2007-08-29 12:31:25 +0000431 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree);
drh03b808a2006-03-13 15:06:05 +0000432 }else{
danielk1977a1644fd2007-08-29 12:31:25 +0000433 p = (Schema *)sqlite3MallocZero(sizeof(Schema));
drh03b808a2006-03-13 15:06:05 +0000434 }
danielk1977a1644fd2007-08-29 12:31:25 +0000435 if( !p ){
436 db->mallocFailed = 1;
437 }else if ( 0==p->file_format ){
drh03b808a2006-03-13 15:06:05 +0000438 sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0);
439 sqlite3HashInit(&p->idxHash, SQLITE_HASH_STRING, 0);
440 sqlite3HashInit(&p->trigHash, SQLITE_HASH_STRING, 0);
441 sqlite3HashInit(&p->aFKey, SQLITE_HASH_STRING, 1);
drhf012ea32006-05-24 12:43:26 +0000442 p->enc = SQLITE_UTF8;
drh03b808a2006-03-13 15:06:05 +0000443 }
444 return p;
445}