Fix test script literal.test so that it works with SQLITE_OMIT_ALTER_TABLE builds.
[sqlite.git] / src / callback.c
blobc36d51a4ecf31e65a2a8f93cc576ea786c00b2de
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.
11 *************************************************************************
13 ** This file contains functions used to access the internal hash tables
14 ** of user defined functions and collation sequences.
17 #include "sqliteInt.h"
20 ** Invoke the 'collation needed' callback to request a collation sequence
21 ** in the encoding enc of name zName, length nName.
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);
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);
40 sqlite3ValueFree(pTmp);
42 #endif
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.
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;
65 return SQLITE_ERROR;
69 ** 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.
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.
79 int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
80 if( pColl && pColl->xCmp==0 ){
81 const char *zName = pColl->zName;
82 sqlite3 *db = pParse->db;
83 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
84 if( !p ){
85 return SQLITE_ERROR;
87 assert( p==pColl );
89 return SQLITE_OK;
95 ** 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.
99 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
100 ** array of three CollSeq structures. The first is the collation sequence
101 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
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.
107 static CollSeq *findCollSeqEntry(
108 sqlite3 *db, /* Database connection */
109 const char *zName, /* Name of the collating sequence */
110 int create /* Create a new entry if true */
112 CollSeq *pColl;
113 pColl = sqlite3HashFind(&db->aCollSeq, zName);
115 if( 0==pColl && create ){
116 int nName = sqlite3Strlen30(zName) + 1;
117 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
118 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);
127 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
129 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
130 ** return the pColl pointer to be deleted (because it wasn't added
131 ** to the hash table).
133 assert( pDel==0 || pDel==pColl );
134 if( pDel!=0 ){
135 sqlite3OomFault(db);
136 sqlite3DbFree(db, pDel);
137 pColl = 0;
141 return pColl;
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'.
149 ** If the entry specified is not found and 'create' is true, then create a
150 ** new entry. Otherwise return NULL.
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.
157 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
159 CollSeq *sqlite3FindCollSeq(
160 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 */
165 CollSeq *pColl;
166 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
167 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
168 if( zName ){
169 pColl = findCollSeqEntry(db, zName, create);
170 if( pColl ) pColl += enc-1;
171 }else{
172 pColl = db->pDfltColl;
174 return pColl;
178 ** Change the text encoding for a database connection. This means that
179 ** the pDfltColl must change as well.
181 void 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.
187 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
188 sqlite3ExpirePreparedStatements(db, 1);
192 ** 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.
196 ** If it is not NULL, then pColl must point to the database native encoding
197 ** collation sequence with name zName, length nName.
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.
203 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
205 CollSeq *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 */
211 CollSeq *p;
212 sqlite3 *db = pParse->db;
214 p = pColl;
215 if( !p ){
216 p = sqlite3FindCollSeq(db, enc, zName, 0);
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.
222 callCollNeeded(db, enc, zName);
223 p = sqlite3FindCollSeq(db, enc, zName, 0);
225 if( p && !p->xCmp && synthCollSeq(db, p) ){
226 p = 0;
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;
233 return p;
237 ** This function returns the collation sequence for database native text
238 ** encoding identified by the string zName.
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.
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.
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.
254 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
256 CollSeq *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;
262 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
263 if( !initbusy && (!pColl || !pColl->xCmp) ){
264 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
267 return pColl;
270 /* 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.
276 ** 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.
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
284 ** The returned value is always between 0 and 6, as follows:
286 ** 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.
294 ** If nArg==(-2) then any function with a non-null xSFunc is
295 ** a perfect match and any function with xSFunc NULL is
296 ** a non-match.
298 #define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
299 static 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 */
304 int match;
305 assert( p->nArg>=-1 );
307 /* Wrong number of arguments means "no match" */
308 if( p->nArg!=nArg ){
309 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
310 if( p->nArg>=0 ) return 0;
313 /* Give a better score to a function with a specific number of arguments
314 ** than to function that accepts any number of arguments. */
315 if( p->nArg==nArg ){
316 match = 4;
317 }else{
318 match = 1;
321 /* Bonus points if the text encoding matches */
322 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
323 match += 2; /* Exact encoding match */
324 }else if( (enc & p->funcFlags & 2)!=0 ){
325 match += 1; /* Both are UTF16, but with different byte orders */
328 return match;
332 ** Search a FuncDefHash for a function with the given name. Return
333 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
335 FuncDef *sqlite3FunctionSearch(
336 int h, /* Hash of the name */
337 const char *zFunc /* Name of function */
339 FuncDef *p;
340 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
341 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
342 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
343 return p;
346 return 0;
350 ** Insert a new FuncDef into a FuncDefHash hash table.
352 void sqlite3InsertBuiltinFuncs(
353 FuncDef *aDef, /* List of global functions to be inserted */
354 int nDef /* Length of the apDef[] list */
356 int i;
357 for(i=0; i<nDef; i++){
358 FuncDef *pOther;
359 const char *zName = aDef[i].zName;
360 int nName = sqlite3Strlen30(zName);
361 int h = SQLITE_FUNC_HASH(zName[0], nName);
362 assert( aDef[i].funcFlags & SQLITE_FUNC_BUILTIN );
363 pOther = sqlite3FunctionSearch(h, zName);
364 if( pOther ){
365 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
366 aDef[i].pNext = pOther->pNext;
367 pOther->pNext = &aDef[i];
368 }else{
369 aDef[i].pNext = 0;
370 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
371 sqlite3BuiltinFunctions.a[h] = &aDef[i];
379 ** Locate a user function given a name, a number of arguments and a flag
380 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
381 ** pointer to the FuncDef structure that defines that function, or return
382 ** NULL if the function does not exist.
384 ** If the createFlag argument is true, then a new (blank) FuncDef
385 ** structure is created and liked into the "db" structure if a
386 ** no matching function previously existed.
388 ** If nArg is -2, then the first valid function found is returned. A
389 ** function is valid if xSFunc is non-zero. The nArg==(-2)
390 ** case is used to see if zName is a valid function name for some number
391 ** of arguments. If nArg is -2, then createFlag must be 0.
393 ** If createFlag is false, then a function with the required name and
394 ** number of arguments may be returned even if the eTextRep flag does not
395 ** match that requested.
397 FuncDef *sqlite3FindFunction(
398 sqlite3 *db, /* An open database */
399 const char *zName, /* Name of the function. zero-terminated */
400 int nArg, /* Number of arguments. -1 means any number */
401 u8 enc, /* Preferred text encoding */
402 u8 createFlag /* Create new entry if true and does not otherwise exist */
404 FuncDef *p; /* Iterator variable */
405 FuncDef *pBest = 0; /* Best match found so far */
406 int bestScore = 0; /* Score of best match */
407 int h; /* Hash value */
408 int nName; /* Length of the name */
410 assert( nArg>=(-2) );
411 assert( nArg>=(-1) || createFlag==0 );
412 nName = sqlite3Strlen30(zName);
414 /* First search for a match amongst the application-defined functions.
416 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
417 while( p ){
418 int score = matchQuality(p, nArg, enc);
419 if( score>bestScore ){
420 pBest = p;
421 bestScore = score;
423 p = p->pNext;
426 /* If no match is found, search the built-in functions.
428 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
429 ** functions even if a prior app-defined function was found. And give
430 ** priority to built-in functions.
432 ** Except, if createFlag is true, that means that we are trying to
433 ** install a new function. Whatever FuncDef structure is returned it will
434 ** have fields overwritten with new information appropriate for the
435 ** new function. But the FuncDefs for built-in functions are read-only.
436 ** So we must not search for built-ins when creating a new function.
438 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
439 bestScore = 0;
440 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
441 p = sqlite3FunctionSearch(h, zName);
442 while( p ){
443 int score = matchQuality(p, nArg, enc);
444 if( score>bestScore ){
445 pBest = p;
446 bestScore = score;
448 p = p->pNext;
452 /* If the createFlag parameter is true and the search did not reveal an
453 ** exact match for the name, number of arguments and encoding, then add a
454 ** new entry to the hash table and return it.
456 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
457 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
458 FuncDef *pOther;
459 u8 *z;
460 pBest->zName = (const char*)&pBest[1];
461 pBest->nArg = (u16)nArg;
462 pBest->funcFlags = enc;
463 memcpy((char*)&pBest[1], zName, nName+1);
464 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
465 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
466 if( pOther==pBest ){
467 sqlite3DbFree(db, pBest);
468 sqlite3OomFault(db);
469 return 0;
470 }else{
471 pBest->pNext = pOther;
475 if( pBest && (pBest->xSFunc || createFlag) ){
476 return pBest;
478 return 0;
482 ** Free all resources held by the schema structure. The void* argument points
483 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
484 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
485 ** of the schema hash tables).
487 ** The Schema.cache_size variable is not cleared.
489 void sqlite3SchemaClear(void *p){
490 Hash temp1;
491 Hash temp2;
492 HashElem *pElem;
493 Schema *pSchema = (Schema *)p;
494 sqlite3 xdb;
496 memset(&xdb, 0, sizeof(xdb));
497 temp1 = pSchema->tblHash;
498 temp2 = pSchema->trigHash;
499 sqlite3HashInit(&pSchema->trigHash);
500 sqlite3HashClear(&pSchema->idxHash);
501 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
502 sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem));
504 sqlite3HashClear(&temp2);
505 sqlite3HashInit(&pSchema->tblHash);
506 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
507 Table *pTab = sqliteHashData(pElem);
508 sqlite3DeleteTable(&xdb, pTab);
510 sqlite3HashClear(&temp1);
511 sqlite3HashClear(&pSchema->fkeyHash);
512 pSchema->pSeqTab = 0;
513 if( pSchema->schemaFlags & DB_SchemaLoaded ){
514 pSchema->iGeneration++;
516 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
520 ** Find and return the schema associated with a BTree. Create
521 ** a new one if necessary.
523 Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
524 Schema * p;
525 if( pBt ){
526 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
527 }else{
528 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
530 if( !p ){
531 sqlite3OomFault(db);
532 }else if ( 0==p->file_format ){
533 sqlite3HashInit(&p->tblHash);
534 sqlite3HashInit(&p->idxHash);
535 sqlite3HashInit(&p->trigHash);
536 sqlite3HashInit(&p->fkeyHash);
537 p->enc = SQLITE_UTF8;
539 return p;