vtab.c

Go to the documentation of this file.
00001 /*
00002 ** 2006 June 10
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 ** This file contains code used to help implement virtual tables.
00013 **
00014 ** $Id: vtab.c,v 1.76 2008/08/20 16:35:10 drh Exp $
00015 */
00016 #ifndef SQLITE_OMIT_VIRTUALTABLE
00017 #include "sqliteInt.h"
00018 
00019 static int createModule(
00020   sqlite3 *db,                    /* Database in which module is registered */
00021   const char *zName,              /* Name assigned to this module */
00022   const sqlite3_module *pModule,  /* The definition of the module */
00023   void *pAux,                     /* Context pointer for xCreate/xConnect */
00024   void (*xDestroy)(void *)        /* Module destructor function */
00025 ) {
00026   int rc, nName;
00027   Module *pMod;
00028 
00029   sqlite3_mutex_enter(db->mutex);
00030   nName = strlen(zName);
00031   pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
00032   if( pMod ){
00033     Module *pDel;
00034     char *zCopy = (char *)(&pMod[1]);
00035     memcpy(zCopy, zName, nName+1);
00036     pMod->zName = zCopy;
00037     pMod->pModule = pModule;
00038     pMod->pAux = pAux;
00039     pMod->xDestroy = xDestroy;
00040     pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
00041     if( pDel && pDel->xDestroy ){
00042       pDel->xDestroy(pDel->pAux);
00043     }
00044     sqlite3DbFree(db, pDel);
00045     if( pDel==pMod ){
00046       db->mallocFailed = 1;
00047     }
00048     sqlite3ResetInternalSchema(db, 0);
00049   }
00050   rc = sqlite3ApiExit(db, SQLITE_OK);
00051   sqlite3_mutex_leave(db->mutex);
00052   return rc;
00053 }
00054 
00055 
00056 /*
00057 ** External API function used to create a new virtual-table module.
00058 */
00059 int sqlite3_create_module(
00060   sqlite3 *db,                    /* Database in which module is registered */
00061   const char *zName,              /* Name assigned to this module */
00062   const sqlite3_module *pModule,  /* The definition of the module */
00063   void *pAux                      /* Context pointer for xCreate/xConnect */
00064 ){
00065   return createModule(db, zName, pModule, pAux, 0);
00066 }
00067 
00068 /*
00069 ** External API function used to create a new virtual-table module.
00070 */
00071 int sqlite3_create_module_v2(
00072   sqlite3 *db,                    /* Database in which module is registered */
00073   const char *zName,              /* Name assigned to this module */
00074   const sqlite3_module *pModule,  /* The definition of the module */
00075   void *pAux,                     /* Context pointer for xCreate/xConnect */
00076   void (*xDestroy)(void *)        /* Module destructor function */
00077 ){
00078   return createModule(db, zName, pModule, pAux, xDestroy);
00079 }
00080 
00081 /*
00082 ** Lock the virtual table so that it cannot be disconnected.
00083 ** Locks nest.  Every lock should have a corresponding unlock.
00084 ** If an unlock is omitted, resources leaks will occur.  
00085 **
00086 ** If a disconnect is attempted while a virtual table is locked,
00087 ** the disconnect is deferred until all locks have been removed.
00088 */
00089 void sqlite3VtabLock(sqlite3_vtab *pVtab){
00090   pVtab->nRef++;
00091 }
00092 
00093 /*
00094 ** Unlock a virtual table.  When the last lock is removed,
00095 ** disconnect the virtual table.
00096 */
00097 void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
00098   pVtab->nRef--;
00099   assert(db);
00100   assert( sqlite3SafetyCheckOk(db) );
00101   if( pVtab->nRef==0 ){
00102     if( db->magic==SQLITE_MAGIC_BUSY ){
00103       (void)sqlite3SafetyOff(db);
00104       pVtab->pModule->xDisconnect(pVtab);
00105       (void)sqlite3SafetyOn(db);
00106     } else {
00107       pVtab->pModule->xDisconnect(pVtab);
00108     }
00109   }
00110 }
00111 
00112 /*
00113 ** Clear any and all virtual-table information from the Table record.
00114 ** This routine is called, for example, just before deleting the Table
00115 ** record.
00116 */
00117 void sqlite3VtabClear(Table *p){
00118   sqlite3_vtab *pVtab = p->pVtab;
00119   sqlite3 *db = p->db;
00120   if( pVtab ){
00121     assert( p->pMod && p->pMod->pModule );
00122     sqlite3VtabUnlock(db, pVtab);
00123     p->pVtab = 0;
00124   }
00125   if( p->azModuleArg ){
00126     int i;
00127     for(i=0; i<p->nModuleArg; i++){
00128       sqlite3DbFree(db, p->azModuleArg[i]);
00129     }
00130     sqlite3DbFree(db, p->azModuleArg);
00131   }
00132 }
00133 
00134 /*
00135 ** Add a new module argument to pTable->azModuleArg[].
00136 ** The string is not copied - the pointer is stored.  The
00137 ** string will be freed automatically when the table is
00138 ** deleted.
00139 */
00140 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
00141   int i = pTable->nModuleArg++;
00142   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
00143   char **azModuleArg;
00144   azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
00145   if( azModuleArg==0 ){
00146     int j;
00147     for(j=0; j<i; j++){
00148       sqlite3DbFree(db, pTable->azModuleArg[j]);
00149     }
00150     sqlite3DbFree(db, zArg);
00151     sqlite3DbFree(db, pTable->azModuleArg);
00152     pTable->nModuleArg = 0;
00153   }else{
00154     azModuleArg[i] = zArg;
00155     azModuleArg[i+1] = 0;
00156   }
00157   pTable->azModuleArg = azModuleArg;
00158 }
00159 
00160 /*
00161 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
00162 ** statement.  The module name has been parsed, but the optional list
00163 ** of parameters that follow the module name are still pending.
00164 */
00165 void sqlite3VtabBeginParse(
00166   Parse *pParse,        /* Parsing context */
00167   Token *pName1,        /* Name of new table, or database name */
00168   Token *pName2,        /* Name of new table or NULL */
00169   Token *pModuleName    /* Name of the module for the virtual table */
00170 ){
00171   int iDb;              /* The database the table is being created in */
00172   Table *pTable;        /* The new virtual table */
00173   sqlite3 *db;          /* Database connection */
00174 
00175   if( pParse->db->flags & SQLITE_SharedCache ){
00176     sqlite3ErrorMsg(pParse, "Cannot use virtual tables in shared-cache mode");
00177     return;
00178   }
00179 
00180   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
00181   pTable = pParse->pNewTable;
00182   if( pTable==0 || pParse->nErr ) return;
00183   assert( 0==pTable->pIndex );
00184 
00185   db = pParse->db;
00186   iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
00187   assert( iDb>=0 );
00188 
00189   pTable->tabFlags |= TF_Virtual;
00190   pTable->nModuleArg = 0;
00191   addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
00192   addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
00193   addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
00194   pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
00195 
00196 #ifndef SQLITE_OMIT_AUTHORIZATION
00197   /* Creating a virtual table invokes the authorization callback twice.
00198   ** The first invocation, to obtain permission to INSERT a row into the
00199   ** sqlite_master table, has already been made by sqlite3StartTable().
00200   ** The second call, to obtain permission to create the table, is made now.
00201   */
00202   if( pTable->azModuleArg ){
00203     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
00204             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
00205   }
00206 #endif
00207 }
00208 
00209 /*
00210 ** This routine takes the module argument that has been accumulating
00211 ** in pParse->zArg[] and appends it to the list of arguments on the
00212 ** virtual table currently under construction in pParse->pTable.
00213 */
00214 static void addArgumentToVtab(Parse *pParse){
00215   if( pParse->sArg.z && pParse->pNewTable ){
00216     const char *z = (const char*)pParse->sArg.z;
00217     int n = pParse->sArg.n;
00218     sqlite3 *db = pParse->db;
00219     addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
00220   }
00221 }
00222 
00223 /*
00224 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
00225 ** has been completely parsed.
00226 */
00227 void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
00228   Table *pTab;        /* The table being constructed */
00229   sqlite3 *db;        /* The database connection */
00230   char *zModule;      /* The module name of the table: USING modulename */
00231   Module *pMod = 0;
00232 
00233   addArgumentToVtab(pParse);
00234   pParse->sArg.z = 0;
00235 
00236   /* Lookup the module name. */
00237   pTab = pParse->pNewTable;
00238   if( pTab==0 ) return;
00239   db = pParse->db;
00240   if( pTab->nModuleArg<1 ) return;
00241   zModule = pTab->azModuleArg[0];
00242   pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
00243   pTab->pMod = pMod;
00244   
00245   /* If the CREATE VIRTUAL TABLE statement is being entered for the
00246   ** first time (in other words if the virtual table is actually being
00247   ** created now instead of just being read out of sqlite_master) then
00248   ** do additional initialization work and store the statement text
00249   ** in the sqlite_master table.
00250   */
00251   if( !db->init.busy ){
00252     char *zStmt;
00253     char *zWhere;
00254     int iDb;
00255     Vdbe *v;
00256 
00257     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
00258     if( pEnd ){
00259       pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
00260     }
00261     zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
00262 
00263     /* A slot for the record has already been allocated in the 
00264     ** SQLITE_MASTER table.  We just need to update that slot with all
00265     ** the information we've collected.  
00266     **
00267     ** The VM register number pParse->regRowid holds the rowid of an
00268     ** entry in the sqlite_master table tht was created for this vtab
00269     ** by sqlite3StartTable().
00270     */
00271     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
00272     sqlite3NestedParse(pParse,
00273       "UPDATE %Q.%s "
00274          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
00275        "WHERE rowid=#%d",
00276       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
00277       pTab->zName,
00278       pTab->zName,
00279       zStmt,
00280       pParse->regRowid
00281     );
00282     sqlite3DbFree(db, zStmt);
00283     v = sqlite3GetVdbe(pParse);
00284     sqlite3ChangeCookie(pParse, iDb);
00285 
00286     sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
00287     zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
00288     sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC);
00289     sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, 
00290                          pTab->zName, strlen(pTab->zName) + 1);
00291   }
00292 
00293   /* If we are rereading the sqlite_master table create the in-memory
00294   ** record of the table. If the module has already been registered,
00295   ** also call the xConnect method here.
00296   */
00297   else {
00298     Table *pOld;
00299     Schema *pSchema = pTab->pSchema;
00300     const char *zName = pTab->zName;
00301     int nName = strlen(zName) + 1;
00302     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
00303     if( pOld ){
00304       db->mallocFailed = 1;
00305       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
00306       return;
00307     }
00308     pSchema->db = pParse->db;
00309     pParse->pNewTable = 0;
00310   }
00311 }
00312 
00313 /*
00314 ** The parser calls this routine when it sees the first token
00315 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
00316 */
00317 void sqlite3VtabArgInit(Parse *pParse){
00318   addArgumentToVtab(pParse);
00319   pParse->sArg.z = 0;
00320   pParse->sArg.n = 0;
00321 }
00322 
00323 /*
00324 ** The parser calls this routine for each token after the first token
00325 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
00326 */
00327 void sqlite3VtabArgExtend(Parse *pParse, Token *p){
00328   Token *pArg = &pParse->sArg;
00329   if( pArg->z==0 ){
00330     pArg->z = p->z;
00331     pArg->n = p->n;
00332   }else{
00333     assert(pArg->z < p->z);
00334     pArg->n = (p->z + p->n - pArg->z);
00335   }
00336 }
00337 
00338 /*
00339 ** Invoke a virtual table constructor (either xCreate or xConnect). The
00340 ** pointer to the function to invoke is passed as the fourth parameter
00341 ** to this procedure.
00342 */
00343 static int vtabCallConstructor(
00344   sqlite3 *db, 
00345   Table *pTab,
00346   Module *pMod,
00347   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
00348   char **pzErr
00349 ){
00350   int rc;
00351   int rc2;
00352   sqlite3_vtab *pVtab = 0;
00353   const char *const*azArg = (const char *const*)pTab->azModuleArg;
00354   int nArg = pTab->nModuleArg;
00355   char *zErr = 0;
00356   char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
00357 
00358   if( !zModuleName ){
00359     return SQLITE_NOMEM;
00360   }
00361 
00362   assert( !db->pVTab );
00363   assert( xConstruct );
00364 
00365   db->pVTab = pTab;
00366   rc = sqlite3SafetyOff(db);
00367   assert( rc==SQLITE_OK );
00368   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVtab, &zErr);
00369   rc2 = sqlite3SafetyOn(db);
00370   if( rc==SQLITE_OK && pVtab ){
00371     pVtab->pModule = pMod->pModule;
00372     pVtab->nRef = 1;
00373     pTab->pVtab = pVtab;
00374   }
00375 
00376   if( SQLITE_OK!=rc ){
00377     if( zErr==0 ){
00378       *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
00379     }else {
00380       *pzErr = sqlite3MPrintf(db, "%s", zErr);
00381       sqlite3DbFree(db, zErr);
00382     }
00383   }else if( db->pVTab ){
00384     const char *zFormat = "vtable constructor did not declare schema: %s";
00385     *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
00386     rc = SQLITE_ERROR;
00387   } 
00388   if( rc==SQLITE_OK ){
00389     rc = rc2;
00390   }
00391   db->pVTab = 0;
00392   sqlite3DbFree(db, zModuleName);
00393 
00394   /* If everything went according to plan, loop through the columns
00395   ** of the table to see if any of them contain the token "hidden".
00396   ** If so, set the Column.isHidden flag and remove the token from
00397   ** the type string.
00398   */
00399   if( rc==SQLITE_OK ){
00400     int iCol;
00401     for(iCol=0; iCol<pTab->nCol; iCol++){
00402       char *zType = pTab->aCol[iCol].zType;
00403       int nType;
00404       int i = 0;
00405       if( !zType ) continue;
00406       nType = strlen(zType);
00407       if( sqlite3StrNICmp("hidden", zType, 6) || (zType[6] && zType[6]!=' ') ){
00408         for(i=0; i<nType; i++){
00409           if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
00410            && (zType[i+7]=='\0' || zType[i+7]==' ')
00411           ){
00412             i++;
00413             break;
00414           }
00415         }
00416       }
00417       if( i<nType ){
00418         int j;
00419         int nDel = 6 + (zType[i+6] ? 1 : 0);
00420         for(j=i; (j+nDel)<=nType; j++){
00421           zType[j] = zType[j+nDel];
00422         }
00423         if( zType[i]=='\0' && i>0 ){
00424           assert(zType[i-1]==' ');
00425           zType[i-1] = '\0';
00426         }
00427         pTab->aCol[iCol].isHidden = 1;
00428       }
00429     }
00430   }
00431   return rc;
00432 }
00433 
00434 /*
00435 ** This function is invoked by the parser to call the xConnect() method
00436 ** of the virtual table pTab. If an error occurs, an error code is returned 
00437 ** and an error left in pParse.
00438 **
00439 ** This call is a no-op if table pTab is not a virtual table.
00440 */
00441 int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
00442   Module *pMod;
00443   int rc = SQLITE_OK;
00444 
00445   if( !pTab || (pTab->tabFlags & TF_Virtual)==0 || pTab->pVtab ){
00446     return SQLITE_OK;
00447   }
00448 
00449   pMod = pTab->pMod;
00450   if( !pMod ){
00451     const char *zModule = pTab->azModuleArg[0];
00452     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
00453     rc = SQLITE_ERROR;
00454   } else {
00455     char *zErr = 0;
00456     sqlite3 *db = pParse->db;
00457     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
00458     if( rc!=SQLITE_OK ){
00459       sqlite3ErrorMsg(pParse, "%s", zErr);
00460     }
00461     sqlite3DbFree(db, zErr);
00462   }
00463 
00464   return rc;
00465 }
00466 
00467 /*
00468 ** Add the virtual table pVtab to the array sqlite3.aVTrans[].
00469 */
00470 static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
00471   const int ARRAY_INCR = 5;
00472 
00473   /* Grow the sqlite3.aVTrans array if required */
00474   if( (db->nVTrans%ARRAY_INCR)==0 ){
00475     sqlite3_vtab **aVTrans;
00476     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
00477     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
00478     if( !aVTrans ){
00479       return SQLITE_NOMEM;
00480     }
00481     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
00482     db->aVTrans = aVTrans;
00483   }
00484 
00485   /* Add pVtab to the end of sqlite3.aVTrans */
00486   db->aVTrans[db->nVTrans++] = pVtab;
00487   sqlite3VtabLock(pVtab);
00488   return SQLITE_OK;
00489 }
00490 
00491 /*
00492 ** This function is invoked by the vdbe to call the xCreate method
00493 ** of the virtual table named zTab in database iDb. 
00494 **
00495 ** If an error occurs, *pzErr is set to point an an English language
00496 ** description of the error and an SQLITE_XXX error code is returned.
00497 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
00498 */
00499 int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
00500   int rc = SQLITE_OK;
00501   Table *pTab;
00502   Module *pMod;
00503   const char *zModule;
00504 
00505   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
00506   assert(pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVtab);
00507   pMod = pTab->pMod;
00508   zModule = pTab->azModuleArg[0];
00509 
00510   /* If the module has been registered and includes a Create method, 
00511   ** invoke it now. If the module has not been registered, return an 
00512   ** error. Otherwise, do nothing.
00513   */
00514   if( !pMod ){
00515     *pzErr = sqlite3MPrintf(db, "no such module: %s", zModule);
00516     rc = SQLITE_ERROR;
00517   }else{
00518     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
00519   }
00520 
00521   if( rc==SQLITE_OK && pTab->pVtab ){
00522       rc = addToVTrans(db, pTab->pVtab);
00523   }
00524 
00525   return rc;
00526 }
00527 
00528 /*
00529 ** This function is used to set the schema of a virtual table.  It is only
00530 ** valid to call this function from within the xCreate() or xConnect() of a
00531 ** virtual table module.
00532 */
00533 int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
00534   Parse sParse;
00535 
00536   int rc = SQLITE_OK;
00537   Table *pTab;
00538   char *zErr = 0;
00539 
00540   sqlite3_mutex_enter(db->mutex);
00541   pTab = db->pVTab;
00542   if( !pTab ){
00543     sqlite3Error(db, SQLITE_MISUSE, 0);
00544     sqlite3_mutex_leave(db->mutex);
00545     return SQLITE_MISUSE;
00546   }
00547   assert((pTab->tabFlags & TF_Virtual)!=0 && pTab->nCol==0 && pTab->aCol==0);
00548 
00549   memset(&sParse, 0, sizeof(Parse));
00550   sParse.declareVtab = 1;
00551   sParse.db = db;
00552 
00553   if( 
00554       SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) && 
00555       sParse.pNewTable && 
00556       !sParse.pNewTable->pSelect && 
00557       (sParse.pNewTable->tabFlags & TF_Virtual)==0
00558   ){
00559     pTab->aCol = sParse.pNewTable->aCol;
00560     pTab->nCol = sParse.pNewTable->nCol;
00561     sParse.pNewTable->nCol = 0;
00562     sParse.pNewTable->aCol = 0;
00563     db->pVTab = 0;
00564   } else {
00565     sqlite3Error(db, SQLITE_ERROR, zErr);
00566     sqlite3DbFree(db, zErr);
00567     rc = SQLITE_ERROR;
00568   }
00569   sParse.declareVtab = 0;
00570 
00571   sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
00572   sqlite3DeleteTable(sParse.pNewTable);
00573   sParse.pNewTable = 0;
00574 
00575   assert( (rc&0xff)==rc );
00576   rc = sqlite3ApiExit(db, rc);
00577   sqlite3_mutex_leave(db->mutex);
00578   return rc;
00579 }
00580 
00581 /*
00582 ** This function is invoked by the vdbe to call the xDestroy method
00583 ** of the virtual table named zTab in database iDb. This occurs
00584 ** when a DROP TABLE is mentioned.
00585 **
00586 ** This call is a no-op if zTab is not a virtual table.
00587 */
00588 int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
00589 {
00590   int rc = SQLITE_OK;
00591   Table *pTab;
00592 
00593   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
00594   assert(pTab);
00595   if( pTab->pVtab ){
00596     int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
00597     rc = sqlite3SafetyOff(db);
00598     assert( rc==SQLITE_OK );
00599     if( xDestroy ){
00600       rc = xDestroy(pTab->pVtab);
00601     }
00602     (void)sqlite3SafetyOn(db);
00603     if( rc==SQLITE_OK ){
00604       int i;
00605       for(i=0; i<db->nVTrans; i++){
00606         if( db->aVTrans[i]==pTab->pVtab ){
00607           db->aVTrans[i] = db->aVTrans[--db->nVTrans];
00608           break;
00609         }
00610       }
00611       pTab->pVtab = 0;
00612     }
00613   }
00614 
00615   return rc;
00616 }
00617 
00618 /*
00619 ** This function invokes either the xRollback or xCommit method
00620 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
00621 ** called is identified by the second argument, "offset", which is
00622 ** the offset of the method to call in the sqlite3_module structure.
00623 **
00624 ** The array is cleared after invoking the callbacks. 
00625 */
00626 static void callFinaliser(sqlite3 *db, int offset){
00627   int i;
00628   if( db->aVTrans ){
00629     for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
00630       sqlite3_vtab *pVtab = db->aVTrans[i];
00631       int (*x)(sqlite3_vtab *);
00632       x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
00633       if( x ) x(pVtab);
00634       sqlite3VtabUnlock(db, pVtab);
00635     }
00636     sqlite3DbFree(db, db->aVTrans);
00637     db->nVTrans = 0;
00638     db->aVTrans = 0;
00639   }
00640 }
00641 
00642 /*
00643 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
00644 ** array. Return the error code for the first error that occurs, or
00645 ** SQLITE_OK if all xSync operations are successful.
00646 **
00647 ** Set *pzErrmsg to point to a buffer that should be released using 
00648 ** sqlite3DbFree() containing an error message, if one is available.
00649 */
00650 int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
00651   int i;
00652   int rc = SQLITE_OK;
00653   int rcsafety;
00654   sqlite3_vtab **aVTrans = db->aVTrans;
00655 
00656   rc = sqlite3SafetyOff(db);
00657   db->aVTrans = 0;
00658   for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
00659     sqlite3_vtab *pVtab = aVTrans[i];
00660     int (*x)(sqlite3_vtab *);
00661     x = pVtab->pModule->xSync;
00662     if( x ){
00663       rc = x(pVtab);
00664       sqlite3DbFree(db, *pzErrmsg);
00665       *pzErrmsg = pVtab->zErrMsg;
00666       pVtab->zErrMsg = 0;
00667     }
00668   }
00669   db->aVTrans = aVTrans;
00670   rcsafety = sqlite3SafetyOn(db);
00671 
00672   if( rc==SQLITE_OK ){
00673     rc = rcsafety;
00674   }
00675   return rc;
00676 }
00677 
00678 /*
00679 ** Invoke the xRollback method of all virtual tables in the 
00680 ** sqlite3.aVTrans array. Then clear the array itself.
00681 */
00682 int sqlite3VtabRollback(sqlite3 *db){
00683   callFinaliser(db, offsetof(sqlite3_module,xRollback));
00684   return SQLITE_OK;
00685 }
00686 
00687 /*
00688 ** Invoke the xCommit method of all virtual tables in the 
00689 ** sqlite3.aVTrans array. Then clear the array itself.
00690 */
00691 int sqlite3VtabCommit(sqlite3 *db){
00692   callFinaliser(db, offsetof(sqlite3_module,xCommit));
00693   return SQLITE_OK;
00694 }
00695 
00696 /*
00697 ** If the virtual table pVtab supports the transaction interface
00698 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
00699 ** not currently open, invoke the xBegin method now.
00700 **
00701 ** If the xBegin call is successful, place the sqlite3_vtab pointer
00702 ** in the sqlite3.aVTrans array.
00703 */
00704 int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
00705   int rc = SQLITE_OK;
00706   const sqlite3_module *pModule;
00707 
00708   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
00709   ** than zero, then this function is being called from within a
00710   ** virtual module xSync() callback. It is illegal to write to 
00711   ** virtual module tables in this case, so return SQLITE_LOCKED.
00712   */
00713   if( 0==db->aVTrans && db->nVTrans>0 ){
00714     return SQLITE_LOCKED;
00715   }
00716   if( !pVtab ){
00717     return SQLITE_OK;
00718   } 
00719   pModule = pVtab->pModule;
00720 
00721   if( pModule->xBegin ){
00722     int i;
00723 
00724 
00725     /* If pVtab is already in the aVTrans array, return early */
00726     for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
00727       if( db->aVTrans[i]==pVtab ){
00728         return SQLITE_OK;
00729       }
00730     }
00731 
00732     /* Invoke the xBegin method */
00733     rc = pModule->xBegin(pVtab);
00734     if( rc==SQLITE_OK ){
00735       rc = addToVTrans(db, pVtab);
00736     }
00737   }
00738   return rc;
00739 }
00740 
00741 /*
00742 ** The first parameter (pDef) is a function implementation.  The
00743 ** second parameter (pExpr) is the first argument to this function.
00744 ** If pExpr is a column in a virtual table, then let the virtual
00745 ** table implementation have an opportunity to overload the function.
00746 **
00747 ** This routine is used to allow virtual table implementations to
00748 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
00749 **
00750 ** Return either the pDef argument (indicating no change) or a 
00751 ** new FuncDef structure that is marked as ephemeral using the
00752 ** SQLITE_FUNC_EPHEM flag.
00753 */
00754 FuncDef *sqlite3VtabOverloadFunction(
00755   sqlite3 *db,    /* Database connection for reporting malloc problems */
00756   FuncDef *pDef,  /* Function to possibly overload */
00757   int nArg,       /* Number of arguments to the function */
00758   Expr *pExpr     /* First argument to the function */
00759 ){
00760   Table *pTab;
00761   sqlite3_vtab *pVtab;
00762   sqlite3_module *pMod;
00763   void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
00764   void *pArg;
00765   FuncDef *pNew;
00766   int rc = 0;
00767   char *zLowerName;
00768   unsigned char *z;
00769 
00770 
00771   /* Check to see the left operand is a column in a virtual table */
00772   if( pExpr==0 ) return pDef;
00773   if( pExpr->op!=TK_COLUMN ) return pDef;
00774   pTab = pExpr->pTab;
00775   if( pTab==0 ) return pDef;
00776   if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
00777   pVtab = pTab->pVtab;
00778   assert( pVtab!=0 );
00779   assert( pVtab->pModule!=0 );
00780   pMod = (sqlite3_module *)pVtab->pModule;
00781   if( pMod->xFindFunction==0 ) return pDef;
00782  
00783   /* Call the xFindFunction method on the virtual table implementation
00784   ** to see if the implementation wants to overload this function 
00785   */
00786   zLowerName = sqlite3DbStrDup(db, pDef->zName);
00787   if( zLowerName ){
00788     for(z=(unsigned char*)zLowerName; *z; z++){
00789       *z = sqlite3UpperToLower[*z];
00790     }
00791     rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
00792     sqlite3DbFree(db, zLowerName);
00793     if( pVtab->zErrMsg ){
00794       sqlite3Error(db, rc, "%s", pVtab->zErrMsg);
00795       sqlite3DbFree(db, pVtab->zErrMsg);
00796       pVtab->zErrMsg = 0;
00797     }
00798   }
00799   if( rc==0 ){
00800     return pDef;
00801   }
00802 
00803   /* Create a new ephemeral function definition for the overloaded
00804   ** function */
00805   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) );
00806   if( pNew==0 ){
00807     return pDef;
00808   }
00809   *pNew = *pDef;
00810   pNew->zName = (char *)&pNew[1];
00811   memcpy(pNew->zName, pDef->zName, strlen(pDef->zName)+1);
00812   pNew->xFunc = xFunc;
00813   pNew->pUserData = pArg;
00814   pNew->flags |= SQLITE_FUNC_EPHEM;
00815   return pNew;
00816 }
00817 
00818 /*
00819 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
00820 ** array so that an OP_VBegin will get generated for it.  Add pTab to the
00821 ** array if it is missing.  If pTab is already in the array, this routine
00822 ** is a no-op.
00823 */
00824 void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
00825   int i, n;
00826   assert( IsVirtual(pTab) );
00827   for(i=0; i<pParse->nVtabLock; i++){
00828     if( pTab==pParse->apVtabLock[i] ) return;
00829   }
00830   n = (pParse->nVtabLock+1)*sizeof(pParse->apVtabLock[0]);
00831   pParse->apVtabLock = sqlite3_realloc(pParse->apVtabLock, n);
00832   if( pParse->apVtabLock ){
00833     pParse->apVtabLock[pParse->nVtabLock++] = pTab;
00834   }else{
00835     pParse->db->mallocFailed = 1;
00836   }
00837 }
00838 
00839 #endif /* SQLITE_OMIT_VIRTUALTABLE */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1