Back to index

lightning-sunbird  0.9+nobinonly
callback.c
Go to the documentation of this file.
00001 /*
00002 ** 2005 May 23 
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 **
00013 ** This file contains functions used to access the internal hash tables
00014 ** of user defined functions and collation sequences.
00015 **
00016 ** $Id: callback.c,v 1.2.2.4 2006/05/22 19:12:32 brettw%gmail.com Exp $
00017 */
00018 
00019 #include "sqliteInt.h"
00020 
00021 /*
00022 ** Invoke the 'collation needed' callback to request a collation sequence
00023 ** in the database text encoding of name zName, length nName.
00024 ** If the collation sequence
00025 */
00026 static void callCollNeeded(sqlite3 *db, const char *zName, int nName){
00027   assert( !db->xCollNeeded || !db->xCollNeeded16 );
00028   if( nName<0 ) nName = strlen(zName);
00029   if( db->xCollNeeded ){
00030     char *zExternal = sqliteStrNDup(zName, nName);
00031     if( !zExternal ) return;
00032     db->xCollNeeded(db->pCollNeededArg, db, (int)ENC(db), zExternal);
00033     sqliteFree(zExternal);
00034   }
00035 #ifndef SQLITE_OMIT_UTF16
00036   if( db->xCollNeeded16 ){
00037     char const *zExternal;
00038     sqlite3_value *pTmp = sqlite3ValueNew();
00039     sqlite3ValueSetStr(pTmp, nName, zName, SQLITE_UTF8, SQLITE_STATIC);
00040     zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
00041     if( zExternal ){
00042       db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
00043     }
00044     sqlite3ValueFree(pTmp);
00045   }
00046 #endif
00047 }
00048 
00049 /*
00050 ** This routine is called if the collation factory fails to deliver a
00051 ** collation function in the best encoding but there may be other versions
00052 ** of this collation function (for other text encodings) available. Use one
00053 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
00054 ** possible.
00055 */
00056 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
00057   CollSeq *pColl2;
00058   char *z = pColl->zName;
00059   int n = strlen(z);
00060   int i;
00061   static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
00062   for(i=0; i<3; i++){
00063     pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
00064     if( pColl2->xCmp!=0 ){
00065       memcpy(pColl, pColl2, sizeof(CollSeq));
00066       return SQLITE_OK;
00067     }
00068   }
00069   return SQLITE_ERROR;
00070 }
00071 
00072 /*
00073 ** This function is responsible for invoking the collation factory callback
00074 ** or substituting a collation sequence of a different encoding when the
00075 ** requested collation sequence is not available in the database native
00076 ** encoding.
00077 ** 
00078 ** If it is not NULL, then pColl must point to the database native encoding 
00079 ** collation sequence with name zName, length nName.
00080 **
00081 ** The return value is either the collation sequence to be used in database
00082 ** db for collation type name zName, length nName, or NULL, if no collation
00083 ** sequence can be found.
00084 */
00085 CollSeq *sqlite3GetCollSeq(
00086   sqlite3* db, 
00087   CollSeq *pColl, 
00088   const char *zName, 
00089   int nName
00090 ){
00091   CollSeq *p;
00092 
00093   p = pColl;
00094   if( !p ){
00095     p = sqlite3FindCollSeq(db, ENC(db), zName, nName, 0);
00096   }
00097   if( !p || !p->xCmp ){
00098     /* No collation sequence of this type for this encoding is registered.
00099     ** Call the collation factory to see if it can supply us with one.
00100     */
00101     callCollNeeded(db, zName, nName);
00102     p = sqlite3FindCollSeq(db, ENC(db), zName, nName, 0);
00103   }
00104   if( p && !p->xCmp && synthCollSeq(db, p) ){
00105     p = 0;
00106   }
00107   assert( !p || p->xCmp );
00108   return p;
00109 }
00110 
00111 /*
00112 ** This routine is called on a collation sequence before it is used to
00113 ** check that it is defined. An undefined collation sequence exists when
00114 ** a database is loaded that contains references to collation sequences
00115 ** that have not been defined by sqlite3_create_collation() etc.
00116 **
00117 ** If required, this routine calls the 'collation needed' callback to
00118 ** request a definition of the collating sequence. If this doesn't work, 
00119 ** an equivalent collating sequence that uses a text encoding different
00120 ** from the main database is substituted, if one is available.
00121 */
00122 int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
00123   if( pColl ){
00124     const char *zName = pColl->zName;
00125     CollSeq *p = sqlite3GetCollSeq(pParse->db, pColl, zName, -1);
00126     if( !p ){
00127       if( pParse->nErr==0 ){
00128         sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
00129       }
00130       pParse->nErr++;
00131       return SQLITE_ERROR;
00132     }
00133     assert( p==pColl );
00134   }
00135   return SQLITE_OK;
00136 }
00137 
00138 
00139 
00140 /*
00141 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
00142 ** specified by zName and nName is not found and parameter 'create' is
00143 ** true, then create a new entry. Otherwise return NULL.
00144 **
00145 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
00146 ** array of three CollSeq structures. The first is the collation sequence
00147 ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
00148 **
00149 ** Stored immediately after the three collation sequences is a copy of
00150 ** the collation sequence name. A pointer to this string is stored in
00151 ** each collation sequence structure.
00152 */
00153 static CollSeq *findCollSeqEntry(
00154   sqlite3 *db,
00155   const char *zName,
00156   int nName,
00157   int create
00158 ){
00159   CollSeq *pColl;
00160   if( nName<0 ) nName = strlen(zName);
00161   pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
00162 
00163   if( 0==pColl && create ){
00164     pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 );
00165     if( pColl ){
00166       CollSeq *pDel = 0;
00167       pColl[0].zName = (char*)&pColl[3];
00168       pColl[0].enc = SQLITE_UTF8;
00169       pColl[1].zName = (char*)&pColl[3];
00170       pColl[1].enc = SQLITE_UTF16LE;
00171       pColl[2].zName = (char*)&pColl[3];
00172       pColl[2].enc = SQLITE_UTF16BE;
00173       memcpy(pColl[0].zName, zName, nName);
00174       pColl[0].zName[nName] = 0;
00175       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
00176 
00177       /* If a malloc() failure occured in sqlite3HashInsert(), it will 
00178       ** return the pColl pointer to be deleted (because it wasn't added
00179       ** to the hash table).
00180       */
00181       assert( !pDel || (sqlite3MallocFailed() && pDel==pColl) );
00182       if( pDel ){
00183         sqliteFree(pDel);
00184         pColl = 0;
00185       }
00186     }
00187   }
00188   return pColl;
00189 }
00190 
00191 /*
00192 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
00193 ** Return the CollSeq* pointer for the collation sequence named zName
00194 ** for the encoding 'enc' from the database 'db'.
00195 **
00196 ** If the entry specified is not found and 'create' is true, then create a
00197 ** new entry.  Otherwise return NULL.
00198 */
00199 CollSeq *sqlite3FindCollSeq(
00200   sqlite3 *db,
00201   u8 enc,
00202   const char *zName,
00203   int nName,
00204   int create
00205 ){
00206   CollSeq *pColl;
00207   if( zName ){
00208     pColl = findCollSeqEntry(db, zName, nName, create);
00209   }else{
00210     pColl = db->pDfltColl;
00211   }
00212   assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
00213   assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
00214   if( pColl ) pColl += enc-1;
00215   return pColl;
00216 }
00217 
00218 /*
00219 ** Locate a user function given a name, a number of arguments and a flag
00220 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
00221 ** pointer to the FuncDef structure that defines that function, or return
00222 ** NULL if the function does not exist.
00223 **
00224 ** If the createFlag argument is true, then a new (blank) FuncDef
00225 ** structure is created and liked into the "db" structure if a
00226 ** no matching function previously existed.  When createFlag is true
00227 ** and the nArg parameter is -1, then only a function that accepts
00228 ** any number of arguments will be returned.
00229 **
00230 ** If createFlag is false and nArg is -1, then the first valid
00231 ** function found is returned.  A function is valid if either xFunc
00232 ** or xStep is non-zero.
00233 **
00234 ** If createFlag is false, then a function with the required name and
00235 ** number of arguments may be returned even if the eTextRep flag does not
00236 ** match that requested.
00237 */
00238 FuncDef *sqlite3FindFunction(
00239   sqlite3 *db,       /* An open database */
00240   const char *zName, /* Name of the function.  Not null-terminated */
00241   int nName,         /* Number of characters in the name */
00242   int nArg,          /* Number of arguments.  -1 means any number */
00243   u8 enc,            /* Preferred text encoding */
00244   int createFlag     /* Create new entry if true and does not otherwise exist */
00245 ){
00246   FuncDef *p;         /* Iterator variable */
00247   FuncDef *pFirst;    /* First function with this name */
00248   FuncDef *pBest = 0; /* Best match found so far */
00249   int bestmatch = 0;  
00250 
00251 
00252   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
00253   if( nArg<-1 ) nArg = -1;
00254 
00255   pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
00256   for(p=pFirst; p; p=p->pNext){
00257     /* During the search for the best function definition, bestmatch is set
00258     ** as follows to indicate the quality of the match with the definition
00259     ** pointed to by pBest:
00260     **
00261     ** 0: pBest is NULL. No match has been found.
00262     ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
00263     **    encoding is requested, or vice versa.
00264     ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
00265     **    requested, or vice versa.
00266     ** 3: A variable arguments function using the same text encoding.
00267     ** 4: A function with the exact number of arguments requested that
00268     **    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
00269     ** 5: A function with the exact number of arguments requested that
00270     **    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
00271     ** 6: An exact match.
00272     **
00273     ** A larger value of 'matchqual' indicates a more desirable match.
00274     */
00275     if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
00276       int match = 1;          /* Quality of this match */
00277       if( p->nArg==nArg || nArg==-1 ){
00278         match = 4;
00279       }
00280       if( enc==p->iPrefEnc ){
00281         match += 2;
00282       }
00283       else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
00284                (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
00285         match += 1;
00286       }
00287 
00288       if( match>bestmatch ){
00289         pBest = p;
00290         bestmatch = match;
00291       }
00292     }
00293   }
00294 
00295   /* If the createFlag parameter is true, and the seach did not reveal an
00296   ** exact match for the name, number of arguments and encoding, then add a
00297   ** new entry to the hash table and return it.
00298   */
00299   if( createFlag && bestmatch<6 && 
00300       (pBest = sqliteMalloc(sizeof(*pBest)+nName))!=0 ){
00301     pBest->nArg = nArg;
00302     pBest->pNext = pFirst;
00303     pBest->iPrefEnc = enc;
00304     memcpy(pBest->zName, zName, nName);
00305     pBest->zName[nName] = 0;
00306     if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
00307       sqliteFree(pBest);
00308       return 0;
00309     }
00310   }
00311 
00312   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
00313     return pBest;
00314   }
00315   return 0;
00316 }
00317 
00318 /*
00319 ** Free all resources held by the schema structure. The void* argument points
00320 ** at a Schema struct. This function does not call sqliteFree() on the 
00321 ** pointer itself, it just cleans up subsiduary resources (i.e. the contents
00322 ** of the schema hash tables).
00323 */
00324 void sqlite3SchemaFree(void *p){
00325   Hash temp1;
00326   Hash temp2;
00327   HashElem *pElem;
00328   Schema *pSchema = (Schema *)p;
00329 
00330   temp1 = pSchema->tblHash;
00331   temp2 = pSchema->trigHash;
00332   sqlite3HashInit(&pSchema->trigHash, SQLITE_HASH_STRING, 0);
00333   sqlite3HashClear(&pSchema->aFKey);
00334   sqlite3HashClear(&pSchema->idxHash);
00335   for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
00336     sqlite3DeleteTrigger((Trigger*)sqliteHashData(pElem));
00337   }
00338   sqlite3HashClear(&temp2);
00339   sqlite3HashInit(&pSchema->tblHash, SQLITE_HASH_STRING, 0);
00340   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
00341     Table *pTab = sqliteHashData(pElem);
00342     sqlite3DeleteTable(0, pTab);
00343   }
00344   sqlite3HashClear(&temp1);
00345   pSchema->pSeqTab = 0;
00346   pSchema->flags &= ~DB_SchemaLoaded;
00347 }
00348 
00349 /*
00350 ** Find and return the schema associated with a BTree.  Create
00351 ** a new one if necessary.
00352 */
00353 Schema *sqlite3SchemaGet(Btree *pBt){
00354   Schema * p;
00355   if( pBt ){
00356     p = (Schema *)sqlite3BtreeSchema(pBt,sizeof(Schema),sqlite3SchemaFree);
00357   }else{
00358     p = (Schema *)sqliteMalloc(sizeof(Schema));
00359   }
00360   if( p && 0==p->file_format ){
00361     sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0);
00362     sqlite3HashInit(&p->idxHash, SQLITE_HASH_STRING, 0);
00363     sqlite3HashInit(&p->trigHash, SQLITE_HASH_STRING, 0);
00364     sqlite3HashInit(&p->aFKey, SQLITE_HASH_STRING, 1);
00365   }
00366   return p;
00367 }