ds@1952: /* ds@1952: ** 2005 May 23 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** ds@1952: ** This file contains functions used to access the internal hash tables ds@1952: ** of user defined functions and collation sequences. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: ds@1952: #include "sqliteInt.h" ds@1952: ds@1952: /* ds@1952: ** Invoke the 'collation needed' callback to request a collation sequence ds@1952: ** in the database text encoding of name zName, length nName. ds@1952: ** If the collation sequence ds@1952: */ ds@1952: static void callCollNeeded(sqlite3 *db, const char *zName, int nName){ ds@1952: assert( !db->xCollNeeded || !db->xCollNeeded16 ); ds@1952: if( nName<0 ) nName = strlen(zName); ds@1952: if( db->xCollNeeded ){ ds@1952: char *zExternal = sqlite3DbStrNDup(db, zName, nName); ds@1952: if( !zExternal ) return; ds@1952: db->xCollNeeded(db->pCollNeededArg, db, (int)ENC(db), zExternal); ds@1952: sqlite3_free(zExternal); ds@1952: } ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: if( db->xCollNeeded16 ){ ds@1952: char const *zExternal; ds@1952: sqlite3_value *pTmp = sqlite3ValueNew(db); ds@1952: sqlite3ValueSetStr(pTmp, nName, zName, SQLITE_UTF8, SQLITE_STATIC); ds@1952: zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE); ds@1952: if( zExternal ){ ds@1952: db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal); ds@1952: } ds@1952: sqlite3ValueFree(pTmp); ds@1952: } ds@1952: #endif ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine is called if the collation factory fails to deliver a ds@1952: ** collation function in the best encoding but there may be other versions ds@1952: ** of this collation function (for other text encodings) available. Use one ds@1952: ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if ds@1952: ** possible. ds@1952: */ ds@1952: static int synthCollSeq(sqlite3 *db, CollSeq *pColl){ ds@1952: CollSeq *pColl2; ds@1952: char *z = pColl->zName; ds@1952: int n = strlen(z); ds@1952: int i; ds@1952: static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 }; ds@1952: for(i=0; i<3; i++){ ds@1952: pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0); ds@1952: if( pColl2->xCmp!=0 ){ ds@1952: memcpy(pColl, pColl2, sizeof(CollSeq)); ds@1952: pColl->xDel = 0; /* Do not copy the destructor */ ds@1952: return SQLITE_OK; ds@1952: } ds@1952: } ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This function is responsible for invoking the collation factory callback ds@1952: ** or substituting a collation sequence of a different encoding when the ds@1952: ** requested collation sequence is not available in the database native ds@1952: ** encoding. ds@1952: ** ds@1952: ** If it is not NULL, then pColl must point to the database native encoding ds@1952: ** collation sequence with name zName, length nName. ds@1952: ** ds@1952: ** The return value is either the collation sequence to be used in database ds@1952: ** db for collation type name zName, length nName, or NULL, if no collation ds@1952: ** sequence can be found. ds@1952: */ ds@1952: CollSeq *sqlite3GetCollSeq( ds@1952: sqlite3* db, ds@1952: CollSeq *pColl, ds@1952: const char *zName, ds@1952: int nName ds@1952: ){ ds@1952: CollSeq *p; ds@1952: ds@1952: p = pColl; ds@1952: if( !p ){ ds@1952: p = sqlite3FindCollSeq(db, ENC(db), zName, nName, 0); ds@1952: } ds@1952: if( !p || !p->xCmp ){ ds@1952: /* No collation sequence of this type for this encoding is registered. ds@1952: ** Call the collation factory to see if it can supply us with one. ds@1952: */ ds@1952: callCollNeeded(db, zName, nName); ds@1952: p = sqlite3FindCollSeq(db, ENC(db), zName, nName, 0); ds@1952: } ds@1952: if( p && !p->xCmp && synthCollSeq(db, p) ){ ds@1952: p = 0; ds@1952: } ds@1952: assert( !p || p->xCmp ); ds@1952: return p; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine is called on a collation sequence before it is used to ds@1952: ** check that it is defined. An undefined collation sequence exists when ds@1952: ** a database is loaded that contains references to collation sequences ds@1952: ** that have not been defined by sqlite3_create_collation() etc. ds@1952: ** ds@1952: ** If required, this routine calls the 'collation needed' callback to ds@1952: ** request a definition of the collating sequence. If this doesn't work, ds@1952: ** an equivalent collating sequence that uses a text encoding different ds@1952: ** from the main database is substituted, if one is available. ds@1952: */ ds@1952: int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){ ds@1952: if( pColl ){ ds@1952: const char *zName = pColl->zName; ds@1952: CollSeq *p = sqlite3GetCollSeq(pParse->db, pColl, zName, -1); ds@1952: if( !p ){ ds@1952: if( pParse->nErr==0 ){ ds@1952: sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName); ds@1952: } ds@1952: pParse->nErr++; ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: assert( p==pColl ); ds@1952: } ds@1952: return SQLITE_OK; ds@1952: } ds@1952: ds@1952: ds@1952: ds@1952: /* ds@1952: ** Locate and return an entry from the db.aCollSeq hash table. If the entry ds@1952: ** specified by zName and nName is not found and parameter 'create' is ds@1952: ** true, then create a new entry. Otherwise return NULL. ds@1952: ** ds@1952: ** Each pointer stored in the sqlite3.aCollSeq hash table contains an ds@1952: ** array of three CollSeq structures. The first is the collation sequence ds@1952: ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be. ds@1952: ** ds@1952: ** Stored immediately after the three collation sequences is a copy of ds@1952: ** the collation sequence name. A pointer to this string is stored in ds@1952: ** each collation sequence structure. ds@1952: */ ds@1952: static CollSeq *findCollSeqEntry( ds@1952: sqlite3 *db, ds@1952: const char *zName, ds@1952: int nName, ds@1952: int create ds@1952: ){ ds@1952: CollSeq *pColl; ds@1952: if( nName<0 ) nName = strlen(zName); ds@1952: pColl = sqlite3HashFind(&db->aCollSeq, zName, nName); ds@1952: ds@1952: if( 0==pColl && create ){ ds@1952: pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 ); ds@1952: if( pColl ){ ds@1952: CollSeq *pDel = 0; ds@1952: pColl[0].zName = (char*)&pColl[3]; ds@1952: pColl[0].enc = SQLITE_UTF8; ds@1952: pColl[1].zName = (char*)&pColl[3]; ds@1952: pColl[1].enc = SQLITE_UTF16LE; ds@1952: pColl[2].zName = (char*)&pColl[3]; ds@1952: pColl[2].enc = SQLITE_UTF16BE; ds@1952: memcpy(pColl[0].zName, zName, nName); ds@1952: pColl[0].zName[nName] = 0; ds@1952: pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl); ds@1952: ds@1952: /* If a malloc() failure occured in sqlite3HashInsert(), it will ds@1952: ** return the pColl pointer to be deleted (because it wasn't added ds@1952: ** to the hash table). ds@1952: */ ds@1952: assert( pDel==0 || pDel==pColl ); ds@1952: if( pDel!=0 ){ ds@1952: db->mallocFailed = 1; ds@1952: sqlite3_free(pDel); ds@1952: pColl = 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: return pColl; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Parameter zName points to a UTF-8 encoded string nName bytes long. ds@1952: ** Return the CollSeq* pointer for the collation sequence named zName ds@1952: ** for the encoding 'enc' from the database 'db'. ds@1952: ** ds@1952: ** If the entry specified is not found and 'create' is true, then create a ds@1952: ** new entry. Otherwise return NULL. ds@1952: ** ds@1952: ** A separate function sqlite3LocateCollSeq() is a wrapper around ds@1952: ** this routine. sqlite3LocateCollSeq() invokes the collation factory ds@1952: ** if necessary and generates an error message if the collating sequence ds@1952: ** cannot be found. ds@1952: */ ds@1952: CollSeq *sqlite3FindCollSeq( ds@1952: sqlite3 *db, ds@1952: u8 enc, ds@1952: const char *zName, ds@1952: int nName, ds@1952: int create ds@1952: ){ ds@1952: CollSeq *pColl; ds@1952: if( zName ){ ds@1952: pColl = findCollSeqEntry(db, zName, nName, create); ds@1952: }else{ ds@1952: pColl = db->pDfltColl; ds@1952: } ds@1952: assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); ds@1952: assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE ); ds@1952: if( pColl ) pColl += enc-1; ds@1952: return pColl; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Locate a user function given a name, a number of arguments and a flag ds@1952: ** indicating whether the function prefers UTF-16 over UTF-8. Return a ds@1952: ** pointer to the FuncDef structure that defines that function, or return ds@1952: ** NULL if the function does not exist. ds@1952: ** ds@1952: ** If the createFlag argument is true, then a new (blank) FuncDef ds@1952: ** structure is created and liked into the "db" structure if a ds@1952: ** no matching function previously existed. When createFlag is true ds@1952: ** and the nArg parameter is -1, then only a function that accepts ds@1952: ** any number of arguments will be returned. ds@1952: ** ds@1952: ** If createFlag is false and nArg is -1, then the first valid ds@1952: ** function found is returned. A function is valid if either xFunc ds@1952: ** or xStep is non-zero. ds@1952: ** ds@1952: ** If createFlag is false, then a function with the required name and ds@1952: ** number of arguments may be returned even if the eTextRep flag does not ds@1952: ** match that requested. ds@1952: */ ds@1952: FuncDef *sqlite3FindFunction( ds@1952: sqlite3 *db, /* An open database */ ds@1952: const char *zName, /* Name of the function. Not null-terminated */ ds@1952: int nName, /* Number of characters in the name */ ds@1952: int nArg, /* Number of arguments. -1 means any number */ ds@1952: u8 enc, /* Preferred text encoding */ ds@1952: int createFlag /* Create new entry if true and does not otherwise exist */ ds@1952: ){ ds@1952: FuncDef *p; /* Iterator variable */ ds@1952: FuncDef *pFirst; /* First function with this name */ ds@1952: FuncDef *pBest = 0; /* Best match found so far */ ds@1952: int bestmatch = 0; ds@1952: ds@1952: ds@1952: assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); ds@1952: if( nArg<-1 ) nArg = -1; ds@1952: ds@1952: pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName); ds@1952: for(p=pFirst; p; p=p->pNext){ ds@1952: /* During the search for the best function definition, bestmatch is set ds@1952: ** as follows to indicate the quality of the match with the definition ds@1952: ** pointed to by pBest: ds@1952: ** ds@1952: ** 0: pBest is NULL. No match has been found. ds@1952: ** 1: A variable arguments function that prefers UTF-8 when a UTF-16 ds@1952: ** encoding is requested, or vice versa. ds@1952: ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is ds@1952: ** requested, or vice versa. ds@1952: ** 3: A variable arguments function using the same text encoding. ds@1952: ** 4: A function with the exact number of arguments requested that ds@1952: ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa. ds@1952: ** 5: A function with the exact number of arguments requested that ds@1952: ** prefers UTF-16LE when UTF-16BE is requested, or vice versa. ds@1952: ** 6: An exact match. ds@1952: ** ds@1952: ** A larger value of 'matchqual' indicates a more desirable match. ds@1952: */ ds@1952: if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){ ds@1952: int match = 1; /* Quality of this match */ ds@1952: if( p->nArg==nArg || nArg==-1 ){ ds@1952: match = 4; ds@1952: } ds@1952: if( enc==p->iPrefEnc ){ ds@1952: match += 2; ds@1952: } ds@1952: else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) || ds@1952: (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){ ds@1952: match += 1; ds@1952: } ds@1952: ds@1952: if( match>bestmatch ){ ds@1952: pBest = p; ds@1952: bestmatch = match; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* If the createFlag parameter is true, and the seach did not reveal an ds@1952: ** exact match for the name, number of arguments and encoding, then add a ds@1952: ** new entry to the hash table and return it. ds@1952: */ ds@1952: if( createFlag && bestmatch<6 && ds@1952: (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName))!=0 ){ ds@1952: pBest->nArg = nArg; ds@1952: pBest->pNext = pFirst; ds@1952: pBest->iPrefEnc = enc; ds@1952: memcpy(pBest->zName, zName, nName); ds@1952: pBest->zName[nName] = 0; ds@1952: if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){ ds@1952: db->mallocFailed = 1; ds@1952: sqlite3_free(pBest); ds@1952: return 0; ds@1952: } ds@1952: } ds@1952: ds@1952: if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){ ds@1952: return pBest; ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Free all resources held by the schema structure. The void* argument points ds@1952: ** at a Schema struct. This function does not call sqlite3_free() on the ds@1952: ** pointer itself, it just cleans up subsiduary resources (i.e. the contents ds@1952: ** of the schema hash tables). ds@1952: */ ds@1952: void sqlite3SchemaFree(void *p){ ds@1952: Hash temp1; ds@1952: Hash temp2; ds@1952: HashElem *pElem; ds@1952: Schema *pSchema = (Schema *)p; ds@1952: ds@1952: temp1 = pSchema->tblHash; ds@1952: temp2 = pSchema->trigHash; ds@1952: sqlite3HashInit(&pSchema->trigHash, SQLITE_HASH_STRING, 0); ds@1952: sqlite3HashClear(&pSchema->aFKey); ds@1952: sqlite3HashClear(&pSchema->idxHash); ds@1952: for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ ds@1952: sqlite3DeleteTrigger((Trigger*)sqliteHashData(pElem)); ds@1952: } ds@1952: sqlite3HashClear(&temp2); ds@1952: sqlite3HashInit(&pSchema->tblHash, SQLITE_HASH_STRING, 0); ds@1952: for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ ds@1952: Table *pTab = sqliteHashData(pElem); ds@1952: sqlite3DeleteTable(pTab); ds@1952: } ds@1952: sqlite3HashClear(&temp1); ds@1952: pSchema->pSeqTab = 0; ds@1952: pSchema->flags &= ~DB_SchemaLoaded; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Find and return the schema associated with a BTree. Create ds@1952: ** a new one if necessary. ds@1952: */ ds@1952: Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ ds@1952: Schema * p; ds@1952: if( pBt ){ ds@1952: p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree); ds@1952: }else{ ds@1952: p = (Schema *)sqlite3MallocZero(sizeof(Schema)); ds@1952: } ds@1952: if( !p ){ ds@1952: db->mallocFailed = 1; ds@1952: }else if ( 0==p->file_format ){ ds@1952: sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0); ds@1952: sqlite3HashInit(&p->idxHash, SQLITE_HASH_STRING, 0); ds@1952: sqlite3HashInit(&p->trigHash, SQLITE_HASH_STRING, 0); ds@1952: sqlite3HashInit(&p->aFKey, SQLITE_HASH_STRING, 1); ds@1952: p->enc = SQLITE_UTF8; ds@1952: } ds@1952: return p; ds@1952: }