blob: 3f75760ac46a7b06c32d16a229da30370742e962 [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**
drhc4a64fa2009-05-11 20:53:28 +000016** $Id: callback.c,v 1.40 2009/05/11 20:53:29 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*/
drhc4a64fa2009-05-11 20:53:28 +000026static void callCollNeeded(sqlite3 *db, const char *zName){
danielk19774dade032005-05-25 10:45:10 +000027 assert( !db->xCollNeeded || !db->xCollNeeded16 );
danielk19774dade032005-05-25 10:45:10 +000028 if( db->xCollNeeded ){
drhc4a64fa2009-05-11 20:53:28 +000029 char *zExternal = sqlite3DbStrDup(db, zName);
danielk19774dade032005-05-25 10:45:10 +000030 if( !zExternal ) return;
danielk197714db2662006-01-09 16:12:04 +000031 db->xCollNeeded(db->pCollNeededArg, db, (int)ENC(db), zExternal);
drh633e6d52008-07-28 19:34:53 +000032 sqlite3DbFree(db, zExternal);
danielk19774dade032005-05-25 10:45:10 +000033 }
34#ifndef SQLITE_OMIT_UTF16
35 if( db->xCollNeeded16 ){
36 char const *zExternal;
danielk19771e536952007-08-16 10:09:01 +000037 sqlite3_value *pTmp = sqlite3ValueNew(db);
drhc4a64fa2009-05-11 20:53:28 +000038 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
drhb21c8cd2007-08-21 19:33:56 +000039 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
drh26abcb12005-12-14 22:51:16 +000040 if( zExternal ){
danielk197714db2662006-01-09 16:12:04 +000041 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
drh26abcb12005-12-14 22:51:16 +000042 }
43 sqlite3ValueFree(pTmp);
danielk19774dade032005-05-25 10:45:10 +000044 }
45#endif
46}
47
48/*
49** This routine is called if the collation factory fails to deliver a
50** collation function in the best encoding but there may be other versions
51** of this collation function (for other text encodings) available. Use one
52** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
53** possible.
54*/
55static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
56 CollSeq *pColl2;
57 char *z = pColl->zName;
danielk19774dade032005-05-25 10:45:10 +000058 int i;
59 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
60 for(i=0; i<3; i++){
drhc4a64fa2009-05-11 20:53:28 +000061 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
danielk19774dade032005-05-25 10:45:10 +000062 if( pColl2->xCmp!=0 ){
63 memcpy(pColl, pColl2, sizeof(CollSeq));
danielk1977a9808b32007-05-07 09:32:45 +000064 pColl->xDel = 0; /* Do not copy the destructor */
danielk19774dade032005-05-25 10:45:10 +000065 return SQLITE_OK;
66 }
67 }
68 return SQLITE_ERROR;
69}
70
71/*
72** This function is responsible for invoking the collation factory callback
73** or substituting a collation sequence of a different encoding when the
74** requested collation sequence is not available in the database native
75** encoding.
76**
77** If it is not NULL, then pColl must point to the database native encoding
78** collation sequence with name zName, length nName.
79**
80** The return value is either the collation sequence to be used in database
81** db for collation type name zName, length nName, or NULL, if no collation
82** sequence can be found.
drhc4a64fa2009-05-11 20:53:28 +000083**
84** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
danielk19774dade032005-05-25 10:45:10 +000085*/
86CollSeq *sqlite3GetCollSeq(
drhc4a64fa2009-05-11 20:53:28 +000087 sqlite3* db, /* The database connection */
88 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
89 const char *zName /* Collating sequence name */
danielk19774dade032005-05-25 10:45:10 +000090){
91 CollSeq *p;
92
93 p = pColl;
94 if( !p ){
drhc4a64fa2009-05-11 20:53:28 +000095 p = sqlite3FindCollSeq(db, ENC(db), zName, 0);
danielk19774dade032005-05-25 10:45:10 +000096 }
97 if( !p || !p->xCmp ){
98 /* No collation sequence of this type for this encoding is registered.
99 ** Call the collation factory to see if it can supply us with one.
100 */
drhc4a64fa2009-05-11 20:53:28 +0000101 callCollNeeded(db, zName);
102 p = sqlite3FindCollSeq(db, ENC(db), zName, 0);
danielk19774dade032005-05-25 10:45:10 +0000103 }
104 if( p && !p->xCmp && synthCollSeq(db, p) ){
105 p = 0;
106 }
107 assert( !p || p->xCmp );
108 return p;
109}
110
111/*
112** This routine is called on a collation sequence before it is used to
113** check that it is defined. An undefined collation sequence exists when
114** a database is loaded that contains references to collation sequences
115** that have not been defined by sqlite3_create_collation() etc.
116**
117** If required, this routine calls the 'collation needed' callback to
118** request a definition of the collating sequence. If this doesn't work,
119** an equivalent collating sequence that uses a text encoding different
120** from the main database is substituted, if one is available.
121*/
122int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
123 if( pColl ){
124 const char *zName = pColl->zName;
drhc4a64fa2009-05-11 20:53:28 +0000125 CollSeq *p = sqlite3GetCollSeq(pParse->db, pColl, zName);
danielk19774dade032005-05-25 10:45:10 +0000126 if( !p ){
127 if( pParse->nErr==0 ){
128 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
129 }
130 pParse->nErr++;
131 return SQLITE_ERROR;
132 }
danielk1977b3bf5562006-01-10 17:58:23 +0000133 assert( p==pColl );
danielk19774dade032005-05-25 10:45:10 +0000134 }
135 return SQLITE_OK;
136}
137
138
139
140/*
danielk1977fd9a0a42005-05-24 12:01:00 +0000141** Locate and return an entry from the db.aCollSeq hash table. If the entry
142** specified by zName and nName is not found and parameter 'create' is
143** true, then create a new entry. Otherwise return NULL.
144**
145** Each pointer stored in the sqlite3.aCollSeq hash table contains an
146** array of three CollSeq structures. The first is the collation sequence
147** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
148**
149** Stored immediately after the three collation sequences is a copy of
150** the collation sequence name. A pointer to this string is stored in
151** each collation sequence structure.
152*/
drh55ef4d92005-08-14 01:20:37 +0000153static CollSeq *findCollSeqEntry(
drhc4a64fa2009-05-11 20:53:28 +0000154 sqlite3 *db, /* Database connection */
155 const char *zName, /* Name of the collating sequence */
156 int create /* Create a new entry if true */
danielk1977fd9a0a42005-05-24 12:01:00 +0000157){
158 CollSeq *pColl;
drhc4a64fa2009-05-11 20:53:28 +0000159 int nName = sqlite3Strlen30(zName);
danielk1977fd9a0a42005-05-24 12:01:00 +0000160 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
161
162 if( 0==pColl && create ){
drh17435752007-08-16 04:30:38 +0000163 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
danielk1977fd9a0a42005-05-24 12:01:00 +0000164 if( pColl ){
165 CollSeq *pDel = 0;
166 pColl[0].zName = (char*)&pColl[3];
167 pColl[0].enc = SQLITE_UTF8;
168 pColl[1].zName = (char*)&pColl[3];
169 pColl[1].enc = SQLITE_UTF16LE;
170 pColl[2].zName = (char*)&pColl[3];
171 pColl[2].enc = SQLITE_UTF16BE;
172 memcpy(pColl[0].zName, zName, nName);
173 pColl[0].zName[nName] = 0;
174 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
175
shanebe217792009-03-05 04:20:31 +0000176 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
danielk1977fd9a0a42005-05-24 12:01:00 +0000177 ** return the pColl pointer to be deleted (because it wasn't added
178 ** to the hash table).
179 */
drhf3a65f72007-08-22 20:18:21 +0000180 assert( pDel==0 || pDel==pColl );
181 if( pDel!=0 ){
182 db->mallocFailed = 1;
drh633e6d52008-07-28 19:34:53 +0000183 sqlite3DbFree(db, pDel);
drh91171cd2006-03-14 11:08:27 +0000184 pColl = 0;
185 }
danielk1977fd9a0a42005-05-24 12:01:00 +0000186 }
187 }
188 return pColl;
189}
190
191/*
192** Parameter zName points to a UTF-8 encoded string nName bytes long.
193** Return the CollSeq* pointer for the collation sequence named zName
194** for the encoding 'enc' from the database 'db'.
195**
196** If the entry specified is not found and 'create' is true, then create a
197** new entry. Otherwise return NULL.
drha34001c2007-02-02 12:44:37 +0000198**
199** A separate function sqlite3LocateCollSeq() is a wrapper around
200** this routine. sqlite3LocateCollSeq() invokes the collation factory
201** if necessary and generates an error message if the collating sequence
202** cannot be found.
drhc4a64fa2009-05-11 20:53:28 +0000203**
204** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
danielk1977fd9a0a42005-05-24 12:01:00 +0000205*/
206CollSeq *sqlite3FindCollSeq(
207 sqlite3 *db,
208 u8 enc,
209 const char *zName,
danielk1977fd9a0a42005-05-24 12:01:00 +0000210 int create
211){
danielk1977b3bf5562006-01-10 17:58:23 +0000212 CollSeq *pColl;
213 if( zName ){
drhc4a64fa2009-05-11 20:53:28 +0000214 pColl = findCollSeqEntry(db, zName, create);
danielk1977b3bf5562006-01-10 17:58:23 +0000215 }else{
216 pColl = db->pDfltColl;
217 }
danielk1977fd9a0a42005-05-24 12:01:00 +0000218 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
219 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
220 if( pColl ) pColl += enc-1;
221 return pColl;
222}
223
danielk19778c0a7912008-08-20 14:49:23 +0000224/* During the search for the best function definition, this procedure
225** is called to test how well the function passed as the first argument
226** matches the request for a function with nArg arguments in a system
227** that uses encoding enc. The value returned indicates how well the
228** request is matched. A higher value indicates a better match.
229**
drhdfbc3a82009-01-31 22:28:48 +0000230** The returned value is always between 0 and 6, as follows:
danielk19778c0a7912008-08-20 14:49:23 +0000231**
drhdfbc3a82009-01-31 22:28:48 +0000232** 0: Not a match, or if nArg<0 and the function is has no implementation.
danielk19778c0a7912008-08-20 14:49:23 +0000233** 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;
drhdfbc3a82009-01-31 22:28:48 +0000247 if( p->nArg==-1 || p->nArg==nArg
248 || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0))
249 ){
danielk19778c0a7912008-08-20 14:49:23 +0000250 match = 1;
251 if( p->nArg==nArg || nArg==-1 ){
252 match = 4;
253 }
254 if( enc==p->iPrefEnc ){
255 match += 2;
256 }
257 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
258 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
259 match += 1;
260 }
261 }
262 return match;
263}
264
danielk1977fd9a0a42005-05-24 12:01:00 +0000265/*
drh70a8ca32008-08-21 18:49:27 +0000266** Search a FuncDefHash for a function with the given name. Return
267** a pointer to the matching FuncDef if found, or 0 if there is no match.
268*/
269static FuncDef *functionSearch(
270 FuncDefHash *pHash, /* Hash table to search */
271 int h, /* Hash of the name */
272 const char *zFunc, /* Name of function */
273 int nFunc /* Number of bytes in zFunc */
274){
275 FuncDef *p;
276 for(p=pHash->a[h]; p; p=p->pHash){
277 if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
278 return p;
279 }
280 }
281 return 0;
282}
283
284/*
285** Insert a new FuncDef into a FuncDefHash hash table.
286*/
287void sqlite3FuncDefInsert(
288 FuncDefHash *pHash, /* The hash table into which to insert */
289 FuncDef *pDef /* The function definition to insert */
290){
291 FuncDef *pOther;
drhea678832008-12-10 19:26:22 +0000292 int nName = sqlite3Strlen30(pDef->zName);
drh70a8ca32008-08-21 18:49:27 +0000293 u8 c1 = (u8)pDef->zName[0];
294 int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
295 pOther = functionSearch(pHash, h, pDef->zName, nName);
296 if( pOther ){
297 pDef->pNext = pOther->pNext;
298 pOther->pNext = pDef;
299 }else{
300 pDef->pNext = 0;
301 pDef->pHash = pHash->a[h];
302 pHash->a[h] = pDef;
303 }
304}
305
306
307
308/*
danielk1977fd9a0a42005-05-24 12:01:00 +0000309** Locate a user function given a name, a number of arguments and a flag
310** indicating whether the function prefers UTF-16 over UTF-8. Return a
311** pointer to the FuncDef structure that defines that function, or return
312** NULL if the function does not exist.
313**
314** If the createFlag argument is true, then a new (blank) FuncDef
315** structure is created and liked into the "db" structure if a
316** no matching function previously existed. When createFlag is true
317** and the nArg parameter is -1, then only a function that accepts
318** any number of arguments will be returned.
319**
320** If createFlag is false and nArg is -1, then the first valid
321** function found is returned. A function is valid if either xFunc
322** or xStep is non-zero.
323**
324** If createFlag is false, then a function with the required name and
325** number of arguments may be returned even if the eTextRep flag does not
326** match that requested.
327*/
328FuncDef *sqlite3FindFunction(
329 sqlite3 *db, /* An open database */
330 const char *zName, /* Name of the function. Not null-terminated */
331 int nName, /* Number of characters in the name */
332 int nArg, /* Number of arguments. -1 means any number */
333 u8 enc, /* Preferred text encoding */
334 int createFlag /* Create new entry if true and does not otherwise exist */
335){
336 FuncDef *p; /* Iterator variable */
danielk1977fd9a0a42005-05-24 12:01:00 +0000337 FuncDef *pBest = 0; /* Best match found so far */
drh70a8ca32008-08-21 18:49:27 +0000338 int bestScore = 0; /* Score of best match */
339 int h; /* Hash value */
danielk1977fd9a0a42005-05-24 12:01:00 +0000340
341
342 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
343 if( nArg<-1 ) nArg = -1;
drh70a8ca32008-08-21 18:49:27 +0000344 h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
danielk1977fd9a0a42005-05-24 12:01:00 +0000345
drhe3602be2008-09-09 12:31:33 +0000346 /* First search for a match amongst the application-defined functions.
347 */
drh70a8ca32008-08-21 18:49:27 +0000348 p = functionSearch(&db->aFunc, h, zName, nName);
349 while( p ){
350 int score = matchQuality(p, nArg, enc);
351 if( score>bestScore ){
danielk19778c0a7912008-08-20 14:49:23 +0000352 pBest = p;
drh70a8ca32008-08-21 18:49:27 +0000353 bestScore = score;
danielk19778c0a7912008-08-20 14:49:23 +0000354 }
drh70a8ca32008-08-21 18:49:27 +0000355 p = p->pNext;
danielk19778c0a7912008-08-20 14:49:23 +0000356 }
danielk1977fd9a0a42005-05-24 12:01:00 +0000357
drhe3602be2008-09-09 12:31:33 +0000358 /* If no match is found, search the built-in functions.
359 **
360 ** Except, if createFlag is true, that means that we are trying to
361 ** install a new function. Whatever FuncDef structure is returned will
362 ** have fields overwritten with new information appropriate for the
363 ** new function. But the FuncDefs for built-in functions are read-only.
364 ** So we must not search for built-ins when creating a new function.
danielk19778c0a7912008-08-20 14:49:23 +0000365 */
366 if( !createFlag && !pBest ){
danielk1977075c23a2008-09-01 18:34:20 +0000367 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
368 p = functionSearch(pHash, h, zName, nName);
drh70a8ca32008-08-21 18:49:27 +0000369 while( p ){
370 int score = matchQuality(p, nArg, enc);
371 if( score>bestScore ){
372 pBest = p;
373 bestScore = score;
danielk1977fd9a0a42005-05-24 12:01:00 +0000374 }
drh70a8ca32008-08-21 18:49:27 +0000375 p = p->pNext;
danielk1977fd9a0a42005-05-24 12:01:00 +0000376 }
377 }
378
drhe3602be2008-09-09 12:31:33 +0000379 /* If the createFlag parameter is true and the search did not reveal an
danielk1977fd9a0a42005-05-24 12:01:00 +0000380 ** exact match for the name, number of arguments and encoding, then add a
381 ** new entry to the hash table and return it.
382 */
drhe3602be2008-09-09 12:31:33 +0000383 if( createFlag && (bestScore<6 || pBest->nArg!=nArg) &&
danielk19778c0a7912008-08-20 14:49:23 +0000384 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
385 pBest->zName = (char *)&pBest[1];
drh1bd10f82008-12-10 21:19:56 +0000386 pBest->nArg = (u16)nArg;
danielk1977fd9a0a42005-05-24 12:01:00 +0000387 pBest->iPrefEnc = enc;
388 memcpy(pBest->zName, zName, nName);
389 pBest->zName[nName] = 0;
drh70a8ca32008-08-21 18:49:27 +0000390 sqlite3FuncDefInsert(&db->aFunc, pBest);
danielk1977fd9a0a42005-05-24 12:01:00 +0000391 }
392
393 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
394 return pBest;
395 }
396 return 0;
397}
drh03b808a2006-03-13 15:06:05 +0000398
399/*
400** Free all resources held by the schema structure. The void* argument points
drh633e6d52008-07-28 19:34:53 +0000401** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
drh03b808a2006-03-13 15:06:05 +0000402** pointer itself, it just cleans up subsiduary resources (i.e. the contents
403** of the schema hash tables).
danielk19778cf6c552008-06-23 16:53:46 +0000404**
405** The Schema.cache_size variable is not cleared.
drh03b808a2006-03-13 15:06:05 +0000406*/
407void sqlite3SchemaFree(void *p){
408 Hash temp1;
409 Hash temp2;
410 HashElem *pElem;
411 Schema *pSchema = (Schema *)p;
412
413 temp1 = pSchema->tblHash;
414 temp2 = pSchema->trigHash;
drhe61922a2009-05-02 13:29:37 +0000415 sqlite3HashInit(&pSchema->trigHash);
drh03b808a2006-03-13 15:06:05 +0000416 sqlite3HashClear(&pSchema->idxHash);
417 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
drh633e6d52008-07-28 19:34:53 +0000418 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
drh03b808a2006-03-13 15:06:05 +0000419 }
420 sqlite3HashClear(&temp2);
drhe61922a2009-05-02 13:29:37 +0000421 sqlite3HashInit(&pSchema->tblHash);
drh03b808a2006-03-13 15:06:05 +0000422 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
423 Table *pTab = sqliteHashData(pElem);
drhd9da78a2009-03-24 15:08:09 +0000424 assert( pTab->dbMem==0 );
danielk1977a04a34f2007-04-16 15:06:25 +0000425 sqlite3DeleteTable(pTab);
drh03b808a2006-03-13 15:06:05 +0000426 }
427 sqlite3HashClear(&temp1);
428 pSchema->pSeqTab = 0;
429 pSchema->flags &= ~DB_SchemaLoaded;
430}
431
432/*
433** Find and return the schema associated with a BTree. Create
434** a new one if necessary.
435*/
drh17435752007-08-16 04:30:38 +0000436Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
drh03b808a2006-03-13 15:06:05 +0000437 Schema * p;
438 if( pBt ){
danielk1977a1644fd2007-08-29 12:31:25 +0000439 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree);
drh03b808a2006-03-13 15:06:05 +0000440 }else{
danielk1977a1644fd2007-08-29 12:31:25 +0000441 p = (Schema *)sqlite3MallocZero(sizeof(Schema));
drh03b808a2006-03-13 15:06:05 +0000442 }
danielk1977a1644fd2007-08-29 12:31:25 +0000443 if( !p ){
444 db->mallocFailed = 1;
445 }else if ( 0==p->file_format ){
drhe61922a2009-05-02 13:29:37 +0000446 sqlite3HashInit(&p->tblHash);
447 sqlite3HashInit(&p->idxHash);
448 sqlite3HashInit(&p->trigHash);
drhf012ea32006-05-24 12:43:26 +0000449 p->enc = SQLITE_UTF8;
drh03b808a2006-03-13 15:06:05 +0000450 }
451 return p;
452}