000001 /*
000002 ** 2005 May 23
000003 **
000004 ** The author disclaims copyright to this source code. In place of
000005 ** a legal notice, here is a blessing:
000006 **
000007 ** May you do good and not evil.
000008 ** May you find forgiveness for yourself and forgive others.
000009 ** May you share freely, never taking more than you give.
000010 **
000011 *************************************************************************
000012 **
000013 ** This file contains functions used to access the internal hash tables
000014 ** of user defined functions and collation sequences.
000015 */
000016
000017 #include "sqliteInt.h"
000018
000019 /*
000020 ** Invoke the 'collation needed' callback to request a collation sequence
000021 ** in the encoding enc of name zName, length nName.
000022 */
000023 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
000024 assert( !db->xCollNeeded || !db->xCollNeeded16 );
000025 if( db->xCollNeeded ){
000026 char *zExternal = sqlite3DbStrDup(db, zName);
000027 if( !zExternal ) return;
000028 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
000029 sqlite3DbFree(db, zExternal);
000030 }
000031 #ifndef SQLITE_OMIT_UTF16
000032 if( db->xCollNeeded16 ){
000033 char const *zExternal;
000034 sqlite3_value *pTmp = sqlite3ValueNew(db);
000035 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
000036 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
000037 if( zExternal ){
000038 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
000039 }
000040 sqlite3ValueFree(pTmp);
000041 }
000042 #endif
000043 }
000044
000045 /*
000046 ** This routine is called if the collation factory fails to deliver a
000047 ** collation function in the best encoding but there may be other versions
000048 ** of this collation function (for other text encodings) available. Use one
000049 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
000050 ** possible.
000051 */
000052 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
000053 CollSeq *pColl2;
000054 char *z = pColl->zName;
000055 int i;
000056 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
000057 for(i=0; i<3; i++){
000058 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
000059 if( pColl2->xCmp!=0 ){
000060 memcpy(pColl, pColl2, sizeof(CollSeq));
000061 pColl->xDel = 0; /* Do not copy the destructor */
000062 return SQLITE_OK;
000063 }
000064 }
000065 return SQLITE_ERROR;
000066 }
000067
000068 /*
000069 ** This routine is called on a collation sequence before it is used to
000070 ** check that it is defined. An undefined collation sequence exists when
000071 ** a database is loaded that contains references to collation sequences
000072 ** that have not been defined by sqlite3_create_collation() etc.
000073 **
000074 ** If required, this routine calls the 'collation needed' callback to
000075 ** request a definition of the collating sequence. If this doesn't work,
000076 ** an equivalent collating sequence that uses a text encoding different
000077 ** from the main database is substituted, if one is available.
000078 */
000079 int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
000080 if( pColl && pColl->xCmp==0 ){
000081 const char *zName = pColl->zName;
000082 sqlite3 *db = pParse->db;
000083 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
000084 if( !p ){
000085 return SQLITE_ERROR;
000086 }
000087 assert( p==pColl );
000088 }
000089 return SQLITE_OK;
000090 }
000091
000092
000093
000094 /*
000095 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
000096 ** specified by zName and nName is not found and parameter 'create' is
000097 ** true, then create a new entry. Otherwise return NULL.
000098 **
000099 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
000100 ** array of three CollSeq structures. The first is the collation sequence
000101 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
000102 **
000103 ** Stored immediately after the three collation sequences is a copy of
000104 ** the collation sequence name. A pointer to this string is stored in
000105 ** each collation sequence structure.
000106 */
000107 static CollSeq *findCollSeqEntry(
000108 sqlite3 *db, /* Database connection */
000109 const char *zName, /* Name of the collating sequence */
000110 int create /* Create a new entry if true */
000111 ){
000112 CollSeq *pColl;
000113 pColl = sqlite3HashFind(&db->aCollSeq, zName);
000114
000115 if( 0==pColl && create ){
000116 int nName = sqlite3Strlen30(zName) + 1;
000117 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
000118 if( pColl ){
000119 CollSeq *pDel = 0;
000120 pColl[0].zName = (char*)&pColl[3];
000121 pColl[0].enc = SQLITE_UTF8;
000122 pColl[1].zName = (char*)&pColl[3];
000123 pColl[1].enc = SQLITE_UTF16LE;
000124 pColl[2].zName = (char*)&pColl[3];
000125 pColl[2].enc = SQLITE_UTF16BE;
000126 memcpy(pColl[0].zName, zName, nName);
000127 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
000128
000129 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
000130 ** return the pColl pointer to be deleted (because it wasn't added
000131 ** to the hash table).
000132 */
000133 assert( pDel==0 || pDel==pColl );
000134 if( pDel!=0 ){
000135 sqlite3OomFault(db);
000136 sqlite3DbFree(db, pDel);
000137 pColl = 0;
000138 }
000139 }
000140 }
000141 return pColl;
000142 }
000143
000144 /*
000145 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
000146 ** Return the CollSeq* pointer for the collation sequence named zName
000147 ** for the encoding 'enc' from the database 'db'.
000148 **
000149 ** If the entry specified is not found and 'create' is true, then create a
000150 ** new entry. Otherwise return NULL.
000151 **
000152 ** A separate function sqlite3LocateCollSeq() is a wrapper around
000153 ** this routine. sqlite3LocateCollSeq() invokes the collation factory
000154 ** if necessary and generates an error message if the collating sequence
000155 ** cannot be found.
000156 **
000157 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
000158 */
000159 CollSeq *sqlite3FindCollSeq(
000160 sqlite3 *db, /* Database connection to search */
000161 u8 enc, /* Desired text encoding */
000162 const char *zName, /* Name of the collating sequence. Might be NULL */
000163 int create /* True to create CollSeq if doesn't already exist */
000164 ){
000165 CollSeq *pColl;
000166 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
000167 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
000168 if( zName ){
000169 pColl = findCollSeqEntry(db, zName, create);
000170 if( pColl ) pColl += enc-1;
000171 }else{
000172 pColl = db->pDfltColl;
000173 }
000174 return pColl;
000175 }
000176
000177 /*
000178 ** Change the text encoding for a database connection. This means that
000179 ** the pDfltColl must change as well.
000180 */
000181 void sqlite3SetTextEncoding(sqlite3 *db, u8 enc){
000182 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
000183 db->enc = enc;
000184 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
000185 ** strings is BINARY.
000186 */
000187 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
000188 sqlite3ExpirePreparedStatements(db, 1);
000189 }
000190
000191 /*
000192 ** This function is responsible for invoking the collation factory callback
000193 ** or substituting a collation sequence of a different encoding when the
000194 ** requested collation sequence is not available in the desired encoding.
000195 **
000196 ** If it is not NULL, then pColl must point to the database native encoding
000197 ** collation sequence with name zName, length nName.
000198 **
000199 ** The return value is either the collation sequence to be used in database
000200 ** db for collation type name zName, length nName, or NULL, if no collation
000201 ** sequence can be found. If no collation is found, leave an error message.
000202 **
000203 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
000204 */
000205 CollSeq *sqlite3GetCollSeq(
000206 Parse *pParse, /* Parsing context */
000207 u8 enc, /* The desired encoding for the collating sequence */
000208 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
000209 const char *zName /* Collating sequence name */
000210 ){
000211 CollSeq *p;
000212 sqlite3 *db = pParse->db;
000213
000214 p = pColl;
000215 if( !p ){
000216 p = sqlite3FindCollSeq(db, enc, zName, 0);
000217 }
000218 if( !p || !p->xCmp ){
000219 /* No collation sequence of this type for this encoding is registered.
000220 ** Call the collation factory to see if it can supply us with one.
000221 */
000222 callCollNeeded(db, enc, zName);
000223 p = sqlite3FindCollSeq(db, enc, zName, 0);
000224 }
000225 if( p && !p->xCmp && synthCollSeq(db, p) ){
000226 p = 0;
000227 }
000228 assert( !p || p->xCmp );
000229 if( p==0 ){
000230 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
000231 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
000232 }
000233 return p;
000234 }
000235
000236 /*
000237 ** This function returns the collation sequence for database native text
000238 ** encoding identified by the string zName.
000239 **
000240 ** If the requested collation sequence is not available, or not available
000241 ** in the database native encoding, the collation factory is invoked to
000242 ** request it. If the collation factory does not supply such a sequence,
000243 ** and the sequence is available in another text encoding, then that is
000244 ** returned instead.
000245 **
000246 ** If no versions of the requested collations sequence are available, or
000247 ** another error occurs, NULL is returned and an error message written into
000248 ** pParse.
000249 **
000250 ** This routine is a wrapper around sqlite3FindCollSeq(). This routine
000251 ** invokes the collation factory if the named collation cannot be found
000252 ** and generates an error message.
000253 **
000254 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
000255 */
000256 CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
000257 sqlite3 *db = pParse->db;
000258 u8 enc = ENC(db);
000259 u8 initbusy = db->init.busy;
000260 CollSeq *pColl;
000261
000262 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
000263 if( !initbusy && (!pColl || !pColl->xCmp) ){
000264 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
000265 }
000266
000267 return pColl;
000268 }
000269
000270 /* During the search for the best function definition, this procedure
000271 ** is called to test how well the function passed as the first argument
000272 ** matches the request for a function with nArg arguments in a system
000273 ** that uses encoding enc. The value returned indicates how well the
000274 ** request is matched. A higher value indicates a better match.
000275 **
000276 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
000277 ** is also -1. In other words, we are searching for a function that
000278 ** takes a variable number of arguments.
000279 **
000280 ** If nArg is -2 that means that we are searching for any function
000281 ** regardless of the number of arguments it uses, so return a positive
000282 ** match score for any
000283 **
000284 ** The returned value is always between 0 and 6, as follows:
000285 **
000286 ** 0: Not a match.
000287 ** 1: UTF8/16 conversion required and function takes any number of arguments.
000288 ** 2: UTF16 byte order change required and function takes any number of args.
000289 ** 3: encoding matches and function takes any number of arguments
000290 ** 4: UTF8/16 conversion required - argument count matches exactly
000291 ** 5: UTF16 byte order conversion required - argument count matches exactly
000292 ** 6: Perfect match: encoding and argument count match exactly.
000293 **
000294 ** If nArg==(-2) then any function with a non-null xSFunc is
000295 ** a perfect match and any function with xSFunc NULL is
000296 ** a non-match.
000297 */
000298 #define FUNC_PERFECT_MATCH 6 /* The score for a perfect match */
000299 static int matchQuality(
000300 FuncDef *p, /* The function we are evaluating for match quality */
000301 int nArg, /* Desired number of arguments. (-1)==any */
000302 u8 enc /* Desired text encoding */
000303 ){
000304 int match;
000305 assert( p->nArg>=(-4) && p->nArg!=(-2) );
000306 assert( nArg>=(-2) );
000307
000308 /* Wrong number of arguments means "no match" */
000309 if( p->nArg!=nArg ){
000310 if( nArg==(-2) ) return p->xSFunc==0 ? 0 : FUNC_PERFECT_MATCH;
000311 if( p->nArg>=0 ) return 0;
000312 /* Special p->nArg values available to built-in functions only:
000313 ** -3 1 or more arguments required
000314 ** -4 2 or more arguments required
000315 */
000316 if( p->nArg<(-2) && nArg<(-2-p->nArg) ) return 0;
000317 }
000318
000319 /* Give a better score to a function with a specific number of arguments
000320 ** than to function that accepts any number of arguments. */
000321 if( p->nArg==nArg ){
000322 match = 4;
000323 }else{
000324 match = 1;
000325 }
000326
000327 /* Bonus points if the text encoding matches */
000328 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
000329 match += 2; /* Exact encoding match */
000330 }else if( (enc & p->funcFlags & 2)!=0 ){
000331 match += 1; /* Both are UTF16, but with different byte orders */
000332 }
000333
000334 return match;
000335 }
000336
000337 /*
000338 ** Search a FuncDefHash for a function with the given name. Return
000339 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
000340 */
000341 FuncDef *sqlite3FunctionSearch(
000342 int h, /* Hash of the name */
000343 const char *zFunc /* Name of function */
000344 ){
000345 FuncDef *p;
000346 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
000347 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
000348 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
000349 return p;
000350 }
000351 }
000352 return 0;
000353 }
000354
000355 /*
000356 ** Insert a new FuncDef into a FuncDefHash hash table.
000357 */
000358 void sqlite3InsertBuiltinFuncs(
000359 FuncDef *aDef, /* List of global functions to be inserted */
000360 int nDef /* Length of the apDef[] list */
000361 ){
000362 int i;
000363 for(i=0; i<nDef; i++){
000364 FuncDef *pOther;
000365 const char *zName = aDef[i].zName;
000366 int nName = sqlite3Strlen30(zName);
000367 int h = SQLITE_FUNC_HASH(zName[0], nName);
000368 assert( aDef[i].funcFlags & SQLITE_FUNC_BUILTIN );
000369 pOther = sqlite3FunctionSearch(h, zName);
000370 if( pOther ){
000371 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
000372 aDef[i].pNext = pOther->pNext;
000373 pOther->pNext = &aDef[i];
000374 }else{
000375 aDef[i].pNext = 0;
000376 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
000377 sqlite3BuiltinFunctions.a[h] = &aDef[i];
000378 }
000379 }
000380 }
000381
000382
000383
000384 /*
000385 ** Locate a user function given a name, a number of arguments and a flag
000386 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
000387 ** pointer to the FuncDef structure that defines that function, or return
000388 ** NULL if the function does not exist.
000389 **
000390 ** If the createFlag argument is true, then a new (blank) FuncDef
000391 ** structure is created and liked into the "db" structure if a
000392 ** no matching function previously existed.
000393 **
000394 ** If nArg is -2, then the first valid function found is returned. A
000395 ** function is valid if xSFunc is non-zero. The nArg==(-2)
000396 ** case is used to see if zName is a valid function name for some number
000397 ** of arguments. If nArg is -2, then createFlag must be 0.
000398 **
000399 ** If createFlag is false, then a function with the required name and
000400 ** number of arguments may be returned even if the eTextRep flag does not
000401 ** match that requested.
000402 */
000403 FuncDef *sqlite3FindFunction(
000404 sqlite3 *db, /* An open database */
000405 const char *zName, /* Name of the function. zero-terminated */
000406 int nArg, /* Number of arguments. -1 means any number */
000407 u8 enc, /* Preferred text encoding */
000408 u8 createFlag /* Create new entry if true and does not otherwise exist */
000409 ){
000410 FuncDef *p; /* Iterator variable */
000411 FuncDef *pBest = 0; /* Best match found so far */
000412 int bestScore = 0; /* Score of best match */
000413 int h; /* Hash value */
000414 int nName; /* Length of the name */
000415
000416 assert( nArg>=(-2) );
000417 assert( nArg>=(-1) || createFlag==0 );
000418 nName = sqlite3Strlen30(zName);
000419
000420 /* First search for a match amongst the application-defined functions.
000421 */
000422 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
000423 while( p ){
000424 int score = matchQuality(p, nArg, enc);
000425 if( score>bestScore ){
000426 pBest = p;
000427 bestScore = score;
000428 }
000429 p = p->pNext;
000430 }
000431
000432 /* If no match is found, search the built-in functions.
000433 **
000434 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
000435 ** functions even if a prior app-defined function was found. And give
000436 ** priority to built-in functions.
000437 **
000438 ** Except, if createFlag is true, that means that we are trying to
000439 ** install a new function. Whatever FuncDef structure is returned it will
000440 ** have fields overwritten with new information appropriate for the
000441 ** new function. But the FuncDefs for built-in functions are read-only.
000442 ** So we must not search for built-ins when creating a new function.
000443 */
000444 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
000445 bestScore = 0;
000446 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName);
000447 p = sqlite3FunctionSearch(h, zName);
000448 while( p ){
000449 int score = matchQuality(p, nArg, enc);
000450 if( score>bestScore ){
000451 pBest = p;
000452 bestScore = score;
000453 }
000454 p = p->pNext;
000455 }
000456 }
000457
000458 /* If the createFlag parameter is true and the search did not reveal an
000459 ** exact match for the name, number of arguments and encoding, then add a
000460 ** new entry to the hash table and return it.
000461 */
000462 if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
000463 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
000464 FuncDef *pOther;
000465 u8 *z;
000466 pBest->zName = (const char*)&pBest[1];
000467 pBest->nArg = (u16)nArg;
000468 pBest->funcFlags = enc;
000469 memcpy((char*)&pBest[1], zName, nName+1);
000470 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
000471 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
000472 if( pOther==pBest ){
000473 sqlite3DbFree(db, pBest);
000474 sqlite3OomFault(db);
000475 return 0;
000476 }else{
000477 pBest->pNext = pOther;
000478 }
000479 }
000480
000481 if( pBest && (pBest->xSFunc || createFlag) ){
000482 return pBest;
000483 }
000484 return 0;
000485 }
000486
000487 /*
000488 ** Free all resources held by the schema structure. The void* argument points
000489 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
000490 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
000491 ** of the schema hash tables).
000492 **
000493 ** The Schema.cache_size variable is not cleared.
000494 */
000495 void sqlite3SchemaClear(void *p){
000496 Hash temp1;
000497 Hash temp2;
000498 HashElem *pElem;
000499 Schema *pSchema = (Schema *)p;
000500 sqlite3 xdb;
000501
000502 memset(&xdb, 0, sizeof(xdb));
000503 temp1 = pSchema->tblHash;
000504 temp2 = pSchema->trigHash;
000505 sqlite3HashInit(&pSchema->trigHash);
000506 sqlite3HashClear(&pSchema->idxHash);
000507 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
000508 sqlite3DeleteTrigger(&xdb, (Trigger*)sqliteHashData(pElem));
000509 }
000510 sqlite3HashClear(&temp2);
000511 sqlite3HashInit(&pSchema->tblHash);
000512 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
000513 Table *pTab = sqliteHashData(pElem);
000514 sqlite3DeleteTable(&xdb, pTab);
000515 }
000516 sqlite3HashClear(&temp1);
000517 sqlite3HashClear(&pSchema->fkeyHash);
000518 pSchema->pSeqTab = 0;
000519 if( pSchema->schemaFlags & DB_SchemaLoaded ){
000520 pSchema->iGeneration++;
000521 }
000522 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
000523 }
000524
000525 /*
000526 ** Find and return the schema associated with a BTree. Create
000527 ** a new one if necessary.
000528 */
000529 Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
000530 Schema * p;
000531 if( pBt ){
000532 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
000533 }else{
000534 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
000535 }
000536 if( !p ){
000537 sqlite3OomFault(db);
000538 }else if ( 0==p->file_format ){
000539 sqlite3HashInit(&p->tblHash);
000540 sqlite3HashInit(&p->idxHash);
000541 sqlite3HashInit(&p->trigHash);
000542 sqlite3HashInit(&p->fkeyHash);
000543 p->enc = SQLITE_UTF8;
000544 }
000545 return p;
000546 }