1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/vtab.c Sun Jul 29 01:15:31 2007 +0000
1.3 @@ -0,0 +1,781 @@
1.4 +/*
1.5 +** 2006 June 10
1.6 +**
1.7 +** The author disclaims copyright to this source code. In place of
1.8 +** a legal notice, here is a blessing:
1.9 +**
1.10 +** May you do good and not evil.
1.11 +** May you find forgiveness for yourself and forgive others.
1.12 +** May you share freely, never taking more than you give.
1.13 +**
1.14 +*************************************************************************
1.15 +** This file contains code used to help implement virtual tables.
1.16 +**
1.17 +** $Id$
1.18 +*/
1.19 +#ifndef SQLITE_OMIT_VIRTUALTABLE
1.20 +#include "sqliteInt.h"
1.21 +
1.22 +static int createModule(
1.23 + sqlite3 *db, /* Database in which module is registered */
1.24 + const char *zName, /* Name assigned to this module */
1.25 + const sqlite3_module *pModule, /* The definition of the module */
1.26 + void *pAux, /* Context pointer for xCreate/xConnect */
1.27 + void (*xDestroy)(void *) /* Module destructor function */
1.28 +) {
1.29 + int nName = strlen(zName);
1.30 + Module *pMod = (Module *)sqliteMallocRaw(sizeof(Module) + nName + 1);
1.31 + if( pMod ){
1.32 + char *zCopy = (char *)(&pMod[1]);
1.33 + memcpy(zCopy, zName, nName+1);
1.34 + pMod->zName = zCopy;
1.35 + pMod->pModule = pModule;
1.36 + pMod->pAux = pAux;
1.37 + pMod->xDestroy = xDestroy;
1.38 + pMod = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
1.39 + if( pMod && pMod->xDestroy ){
1.40 + pMod->xDestroy(pMod->pAux);
1.41 + }
1.42 + sqliteFree(pMod);
1.43 + sqlite3ResetInternalSchema(db, 0);
1.44 + }
1.45 + return sqlite3ApiExit(db, SQLITE_OK);
1.46 +}
1.47 +
1.48 +
1.49 +/*
1.50 +** External API function used to create a new virtual-table module.
1.51 +*/
1.52 +int sqlite3_create_module(
1.53 + sqlite3 *db, /* Database in which module is registered */
1.54 + const char *zName, /* Name assigned to this module */
1.55 + const sqlite3_module *pModule, /* The definition of the module */
1.56 + void *pAux /* Context pointer for xCreate/xConnect */
1.57 +){
1.58 + return createModule(db, zName, pModule, pAux, 0);
1.59 +}
1.60 +
1.61 +/*
1.62 +** External API function used to create a new virtual-table module.
1.63 +*/
1.64 +int sqlite3_create_module_v2(
1.65 + sqlite3 *db, /* Database in which module is registered */
1.66 + const char *zName, /* Name assigned to this module */
1.67 + const sqlite3_module *pModule, /* The definition of the module */
1.68 + void *pAux, /* Context pointer for xCreate/xConnect */
1.69 + void (*xDestroy)(void *) /* Module destructor function */
1.70 +){
1.71 + return createModule(db, zName, pModule, pAux, xDestroy);
1.72 +}
1.73 +
1.74 +/*
1.75 +** Lock the virtual table so that it cannot be disconnected.
1.76 +** Locks nest. Every lock should have a corresponding unlock.
1.77 +** If an unlock is omitted, resources leaks will occur.
1.78 +**
1.79 +** If a disconnect is attempted while a virtual table is locked,
1.80 +** the disconnect is deferred until all locks have been removed.
1.81 +*/
1.82 +void sqlite3VtabLock(sqlite3_vtab *pVtab){
1.83 + pVtab->nRef++;
1.84 +}
1.85 +
1.86 +/*
1.87 +** Unlock a virtual table. When the last lock is removed,
1.88 +** disconnect the virtual table.
1.89 +*/
1.90 +void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
1.91 + pVtab->nRef--;
1.92 + assert(db);
1.93 + assert(!sqlite3SafetyCheck(db));
1.94 + if( pVtab->nRef==0 ){
1.95 + if( db->magic==SQLITE_MAGIC_BUSY ){
1.96 + sqlite3SafetyOff(db);
1.97 + pVtab->pModule->xDisconnect(pVtab);
1.98 + sqlite3SafetyOn(db);
1.99 + } else {
1.100 + pVtab->pModule->xDisconnect(pVtab);
1.101 + }
1.102 + }
1.103 +}
1.104 +
1.105 +/*
1.106 +** Clear any and all virtual-table information from the Table record.
1.107 +** This routine is called, for example, just before deleting the Table
1.108 +** record.
1.109 +*/
1.110 +void sqlite3VtabClear(Table *p){
1.111 + sqlite3_vtab *pVtab = p->pVtab;
1.112 + if( pVtab ){
1.113 + assert( p->pMod && p->pMod->pModule );
1.114 + sqlite3VtabUnlock(p->pSchema->db, pVtab);
1.115 + p->pVtab = 0;
1.116 + }
1.117 + if( p->azModuleArg ){
1.118 + int i;
1.119 + for(i=0; i<p->nModuleArg; i++){
1.120 + sqliteFree(p->azModuleArg[i]);
1.121 + }
1.122 + sqliteFree(p->azModuleArg);
1.123 + }
1.124 +}
1.125 +
1.126 +/*
1.127 +** Add a new module argument to pTable->azModuleArg[].
1.128 +** The string is not copied - the pointer is stored. The
1.129 +** string will be freed automatically when the table is
1.130 +** deleted.
1.131 +*/
1.132 +static void addModuleArgument(Table *pTable, char *zArg){
1.133 + int i = pTable->nModuleArg++;
1.134 + int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
1.135 + char **azModuleArg;
1.136 + azModuleArg = sqliteRealloc(pTable->azModuleArg, nBytes);
1.137 + if( azModuleArg==0 ){
1.138 + int j;
1.139 + for(j=0; j<i; j++){
1.140 + sqliteFree(pTable->azModuleArg[j]);
1.141 + }
1.142 + sqliteFree(zArg);
1.143 + sqliteFree(pTable->azModuleArg);
1.144 + pTable->nModuleArg = 0;
1.145 + }else{
1.146 + azModuleArg[i] = zArg;
1.147 + azModuleArg[i+1] = 0;
1.148 + }
1.149 + pTable->azModuleArg = azModuleArg;
1.150 +}
1.151 +
1.152 +/*
1.153 +** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
1.154 +** statement. The module name has been parsed, but the optional list
1.155 +** of parameters that follow the module name are still pending.
1.156 +*/
1.157 +void sqlite3VtabBeginParse(
1.158 + Parse *pParse, /* Parsing context */
1.159 + Token *pName1, /* Name of new table, or database name */
1.160 + Token *pName2, /* Name of new table or NULL */
1.161 + Token *pModuleName /* Name of the module for the virtual table */
1.162 +){
1.163 + int iDb; /* The database the table is being created in */
1.164 + Table *pTable; /* The new virtual table */
1.165 +
1.166 +#ifndef SQLITE_OMIT_SHARED_CACHE
1.167 + if( sqlite3ThreadDataReadOnly()->useSharedData ){
1.168 + sqlite3ErrorMsg(pParse, "Cannot use virtual tables in shared-cache mode");
1.169 + return;
1.170 + }
1.171 +#endif
1.172 +
1.173 + sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
1.174 + pTable = pParse->pNewTable;
1.175 + if( pTable==0 || pParse->nErr ) return;
1.176 + assert( 0==pTable->pIndex );
1.177 +
1.178 + iDb = sqlite3SchemaToIndex(pParse->db, pTable->pSchema);
1.179 + assert( iDb>=0 );
1.180 +
1.181 + pTable->isVirtual = 1;
1.182 + pTable->nModuleArg = 0;
1.183 + addModuleArgument(pTable, sqlite3NameFromToken(pModuleName));
1.184 + addModuleArgument(pTable, sqlite3StrDup(pParse->db->aDb[iDb].zName));
1.185 + addModuleArgument(pTable, sqlite3StrDup(pTable->zName));
1.186 + pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
1.187 +
1.188 +#ifndef SQLITE_OMIT_AUTHORIZATION
1.189 + /* Creating a virtual table invokes the authorization callback twice.
1.190 + ** The first invocation, to obtain permission to INSERT a row into the
1.191 + ** sqlite_master table, has already been made by sqlite3StartTable().
1.192 + ** The second call, to obtain permission to create the table, is made now.
1.193 + */
1.194 + if( pTable->azModuleArg ){
1.195 + sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
1.196 + pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
1.197 + }
1.198 +#endif
1.199 +}
1.200 +
1.201 +/*
1.202 +** This routine takes the module argument that has been accumulating
1.203 +** in pParse->zArg[] and appends it to the list of arguments on the
1.204 +** virtual table currently under construction in pParse->pTable.
1.205 +*/
1.206 +static void addArgumentToVtab(Parse *pParse){
1.207 + if( pParse->sArg.z && pParse->pNewTable ){
1.208 + const char *z = (const char*)pParse->sArg.z;
1.209 + int n = pParse->sArg.n;
1.210 + addModuleArgument(pParse->pNewTable, sqliteStrNDup(z, n));
1.211 + }
1.212 +}
1.213 +
1.214 +/*
1.215 +** The parser calls this routine after the CREATE VIRTUAL TABLE statement
1.216 +** has been completely parsed.
1.217 +*/
1.218 +void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
1.219 + Table *pTab; /* The table being constructed */
1.220 + sqlite3 *db; /* The database connection */
1.221 + char *zModule; /* The module name of the table: USING modulename */
1.222 + Module *pMod = 0;
1.223 +
1.224 + addArgumentToVtab(pParse);
1.225 + pParse->sArg.z = 0;
1.226 +
1.227 + /* Lookup the module name. */
1.228 + pTab = pParse->pNewTable;
1.229 + if( pTab==0 ) return;
1.230 + db = pParse->db;
1.231 + if( pTab->nModuleArg<1 ) return;
1.232 + zModule = pTab->azModuleArg[0];
1.233 + pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
1.234 + pTab->pMod = pMod;
1.235 +
1.236 + /* If the CREATE VIRTUAL TABLE statement is being entered for the
1.237 + ** first time (in other words if the virtual table is actually being
1.238 + ** created now instead of just being read out of sqlite_master) then
1.239 + ** do additional initialization work and store the statement text
1.240 + ** in the sqlite_master table.
1.241 + */
1.242 + if( !db->init.busy ){
1.243 + char *zStmt;
1.244 + char *zWhere;
1.245 + int iDb;
1.246 + Vdbe *v;
1.247 +
1.248 + /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
1.249 + if( pEnd ){
1.250 + pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
1.251 + }
1.252 + zStmt = sqlite3MPrintf("CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
1.253 +
1.254 + /* A slot for the record has already been allocated in the
1.255 + ** SQLITE_MASTER table. We just need to update that slot with all
1.256 + ** the information we've collected.
1.257 + **
1.258 + ** The top of the stack is the rootpage allocated by sqlite3StartTable().
1.259 + ** This value is always 0 and is ignored, a virtual table does not have a
1.260 + ** rootpage. The next entry on the stack is the rowid of the record
1.261 + ** in the sqlite_master table.
1.262 + */
1.263 + iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1.264 + sqlite3NestedParse(pParse,
1.265 + "UPDATE %Q.%s "
1.266 + "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
1.267 + "WHERE rowid=#1",
1.268 + db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
1.269 + pTab->zName,
1.270 + pTab->zName,
1.271 + zStmt
1.272 + );
1.273 + sqliteFree(zStmt);
1.274 + v = sqlite3GetVdbe(pParse);
1.275 + sqlite3ChangeCookie(db, v, iDb);
1.276 +
1.277 + sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
1.278 + zWhere = sqlite3MPrintf("name='%q'", pTab->zName);
1.279 + sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 1, zWhere, P3_DYNAMIC);
1.280 + sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
1.281 + }
1.282 +
1.283 + /* If we are rereading the sqlite_master table create the in-memory
1.284 + ** record of the table. If the module has already been registered,
1.285 + ** also call the xConnect method here.
1.286 + */
1.287 + else {
1.288 + Table *pOld;
1.289 + Schema *pSchema = pTab->pSchema;
1.290 + const char *zName = pTab->zName;
1.291 + int nName = strlen(zName) + 1;
1.292 + pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
1.293 + if( pOld ){
1.294 + assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
1.295 + return;
1.296 + }
1.297 + pSchema->db = pParse->db;
1.298 + pParse->pNewTable = 0;
1.299 + }
1.300 +}
1.301 +
1.302 +/*
1.303 +** The parser calls this routine when it sees the first token
1.304 +** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
1.305 +*/
1.306 +void sqlite3VtabArgInit(Parse *pParse){
1.307 + addArgumentToVtab(pParse);
1.308 + pParse->sArg.z = 0;
1.309 + pParse->sArg.n = 0;
1.310 +}
1.311 +
1.312 +/*
1.313 +** The parser calls this routine for each token after the first token
1.314 +** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
1.315 +*/
1.316 +void sqlite3VtabArgExtend(Parse *pParse, Token *p){
1.317 + Token *pArg = &pParse->sArg;
1.318 + if( pArg->z==0 ){
1.319 + pArg->z = p->z;
1.320 + pArg->n = p->n;
1.321 + }else{
1.322 + assert(pArg->z < p->z);
1.323 + pArg->n = (p->z + p->n - pArg->z);
1.324 + }
1.325 +}
1.326 +
1.327 +/*
1.328 +** Invoke a virtual table constructor (either xCreate or xConnect). The
1.329 +** pointer to the function to invoke is passed as the fourth parameter
1.330 +** to this procedure.
1.331 +*/
1.332 +static int vtabCallConstructor(
1.333 + sqlite3 *db,
1.334 + Table *pTab,
1.335 + Module *pMod,
1.336 + int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
1.337 + char **pzErr
1.338 +){
1.339 + int rc;
1.340 + int rc2;
1.341 + sqlite3_vtab *pVtab;
1.342 + const char *const*azArg = (const char *const*)pTab->azModuleArg;
1.343 + int nArg = pTab->nModuleArg;
1.344 + char *zErr = 0;
1.345 + char *zModuleName = sqlite3MPrintf("%s", pTab->zName);
1.346 +
1.347 + if( !zModuleName ){
1.348 + return SQLITE_NOMEM;
1.349 + }
1.350 +
1.351 + assert( !db->pVTab );
1.352 + assert( xConstruct );
1.353 +
1.354 + db->pVTab = pTab;
1.355 + rc = sqlite3SafetyOff(db);
1.356 + assert( rc==SQLITE_OK );
1.357 + rc = xConstruct(db, pMod->pAux, nArg, azArg, &pTab->pVtab, &zErr);
1.358 + rc2 = sqlite3SafetyOn(db);
1.359 + pVtab = pTab->pVtab;
1.360 + if( rc==SQLITE_OK && pVtab ){
1.361 + pVtab->pModule = pMod->pModule;
1.362 + pVtab->nRef = 1;
1.363 + }
1.364 +
1.365 + if( SQLITE_OK!=rc ){
1.366 + if( zErr==0 ){
1.367 + *pzErr = sqlite3MPrintf("vtable constructor failed: %s", zModuleName);
1.368 + }else {
1.369 + *pzErr = sqlite3MPrintf("%s", zErr);
1.370 + sqlite3_free(zErr);
1.371 + }
1.372 + }else if( db->pVTab ){
1.373 + const char *zFormat = "vtable constructor did not declare schema: %s";
1.374 + *pzErr = sqlite3MPrintf(zFormat, pTab->zName);
1.375 + rc = SQLITE_ERROR;
1.376 + }
1.377 + if( rc==SQLITE_OK ){
1.378 + rc = rc2;
1.379 + }
1.380 + db->pVTab = 0;
1.381 + sqliteFree(zModuleName);
1.382 +
1.383 + /* If everything went according to plan, loop through the columns
1.384 + ** of the table to see if any of them contain the token "hidden".
1.385 + ** If so, set the Column.isHidden flag and remove the token from
1.386 + ** the type string.
1.387 + */
1.388 + if( rc==SQLITE_OK ){
1.389 + int iCol;
1.390 + for(iCol=0; iCol<pTab->nCol; iCol++){
1.391 + char *zType = pTab->aCol[iCol].zType;
1.392 + int nType;
1.393 + int i = 0;
1.394 + if( !zType ) continue;
1.395 + nType = strlen(zType);
1.396 + if( sqlite3StrNICmp("hidden", zType, 6) || (zType[6] && zType[6]!=' ') ){
1.397 + for(i=0; i<nType; i++){
1.398 + if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
1.399 + && (zType[i+7]=='\0' || zType[i+7]==' ')
1.400 + ){
1.401 + i++;
1.402 + break;
1.403 + }
1.404 + }
1.405 + }
1.406 + if( i<nType ){
1.407 + int j;
1.408 + int nDel = 6 + (zType[i+6] ? 1 : 0);
1.409 + for(j=i; (j+nDel)<=nType; j++){
1.410 + zType[j] = zType[j+nDel];
1.411 + }
1.412 + if( zType[i]=='\0' && i>0 ){
1.413 + assert(zType[i-1]==' ');
1.414 + zType[i-1] = '\0';
1.415 + }
1.416 + pTab->aCol[iCol].isHidden = 1;
1.417 + }
1.418 + }
1.419 + }
1.420 + return rc;
1.421 +}
1.422 +
1.423 +/*
1.424 +** This function is invoked by the parser to call the xConnect() method
1.425 +** of the virtual table pTab. If an error occurs, an error code is returned
1.426 +** and an error left in pParse.
1.427 +**
1.428 +** This call is a no-op if table pTab is not a virtual table.
1.429 +*/
1.430 +int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
1.431 + Module *pMod;
1.432 + int rc = SQLITE_OK;
1.433 +
1.434 + if( !pTab || !pTab->isVirtual || pTab->pVtab ){
1.435 + return SQLITE_OK;
1.436 + }
1.437 +
1.438 + pMod = pTab->pMod;
1.439 + if( !pMod ){
1.440 + const char *zModule = pTab->azModuleArg[0];
1.441 + sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
1.442 + rc = SQLITE_ERROR;
1.443 + } else {
1.444 + char *zErr = 0;
1.445 + sqlite3 *db = pParse->db;
1.446 + rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
1.447 + if( rc!=SQLITE_OK ){
1.448 + sqlite3ErrorMsg(pParse, "%s", zErr);
1.449 + }
1.450 + sqliteFree(zErr);
1.451 + }
1.452 +
1.453 + return rc;
1.454 +}
1.455 +
1.456 +/*
1.457 +** Add the virtual table pVtab to the array sqlite3.aVTrans[].
1.458 +*/
1.459 +static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
1.460 + const int ARRAY_INCR = 5;
1.461 +
1.462 + /* Grow the sqlite3.aVTrans array if required */
1.463 + if( (db->nVTrans%ARRAY_INCR)==0 ){
1.464 + sqlite3_vtab **aVTrans;
1.465 + int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
1.466 + aVTrans = sqliteRealloc((void *)db->aVTrans, nBytes);
1.467 + if( !aVTrans ){
1.468 + return SQLITE_NOMEM;
1.469 + }
1.470 + memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
1.471 + db->aVTrans = aVTrans;
1.472 + }
1.473 +
1.474 + /* Add pVtab to the end of sqlite3.aVTrans */
1.475 + db->aVTrans[db->nVTrans++] = pVtab;
1.476 + sqlite3VtabLock(pVtab);
1.477 + return SQLITE_OK;
1.478 +}
1.479 +
1.480 +/*
1.481 +** This function is invoked by the vdbe to call the xCreate method
1.482 +** of the virtual table named zTab in database iDb.
1.483 +**
1.484 +** If an error occurs, *pzErr is set to point an an English language
1.485 +** description of the error and an SQLITE_XXX error code is returned.
1.486 +** In this case the caller must call sqliteFree() on *pzErr.
1.487 +*/
1.488 +int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
1.489 + int rc = SQLITE_OK;
1.490 + Table *pTab;
1.491 + Module *pMod;
1.492 + const char *zModule;
1.493 +
1.494 + pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
1.495 + assert(pTab && pTab->isVirtual && !pTab->pVtab);
1.496 + pMod = pTab->pMod;
1.497 + zModule = pTab->azModuleArg[0];
1.498 +
1.499 + /* If the module has been registered and includes a Create method,
1.500 + ** invoke it now. If the module has not been registered, return an
1.501 + ** error. Otherwise, do nothing.
1.502 + */
1.503 + if( !pMod ){
1.504 + *pzErr = sqlite3MPrintf("no such module: %s", zModule);
1.505 + rc = SQLITE_ERROR;
1.506 + }else{
1.507 + rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
1.508 + }
1.509 +
1.510 + if( rc==SQLITE_OK && pTab->pVtab ){
1.511 + rc = addToVTrans(db, pTab->pVtab);
1.512 + }
1.513 +
1.514 + return rc;
1.515 +}
1.516 +
1.517 +/*
1.518 +** This function is used to set the schema of a virtual table. It is only
1.519 +** valid to call this function from within the xCreate() or xConnect() of a
1.520 +** virtual table module.
1.521 +*/
1.522 +int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
1.523 + Parse sParse;
1.524 +
1.525 + int rc = SQLITE_OK;
1.526 + Table *pTab = db->pVTab;
1.527 + char *zErr = 0;
1.528 +
1.529 + if( !pTab ){
1.530 + sqlite3Error(db, SQLITE_MISUSE, 0);
1.531 + return SQLITE_MISUSE;
1.532 + }
1.533 + assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
1.534 +
1.535 + memset(&sParse, 0, sizeof(Parse));
1.536 + sParse.declareVtab = 1;
1.537 + sParse.db = db;
1.538 +
1.539 + if(
1.540 + SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
1.541 + sParse.pNewTable &&
1.542 + !sParse.pNewTable->pSelect &&
1.543 + !sParse.pNewTable->isVirtual
1.544 + ){
1.545 + pTab->aCol = sParse.pNewTable->aCol;
1.546 + pTab->nCol = sParse.pNewTable->nCol;
1.547 + sParse.pNewTable->nCol = 0;
1.548 + sParse.pNewTable->aCol = 0;
1.549 + db->pVTab = 0;
1.550 + } else {
1.551 + sqlite3Error(db, SQLITE_ERROR, zErr);
1.552 + sqliteFree(zErr);
1.553 + rc = SQLITE_ERROR;
1.554 + }
1.555 + sParse.declareVtab = 0;
1.556 +
1.557 + sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
1.558 + sqlite3DeleteTable(sParse.pNewTable);
1.559 + sParse.pNewTable = 0;
1.560 +
1.561 + assert( (rc&0xff)==rc );
1.562 + return sqlite3ApiExit(db, rc);
1.563 +}
1.564 +
1.565 +/*
1.566 +** This function is invoked by the vdbe to call the xDestroy method
1.567 +** of the virtual table named zTab in database iDb. This occurs
1.568 +** when a DROP TABLE is mentioned.
1.569 +**
1.570 +** This call is a no-op if zTab is not a virtual table.
1.571 +*/
1.572 +int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
1.573 +{
1.574 + int rc = SQLITE_OK;
1.575 + Table *pTab;
1.576 +
1.577 + pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
1.578 + assert(pTab);
1.579 + if( pTab->pVtab ){
1.580 + int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
1.581 + rc = sqlite3SafetyOff(db);
1.582 + assert( rc==SQLITE_OK );
1.583 + if( xDestroy ){
1.584 + rc = xDestroy(pTab->pVtab);
1.585 + }
1.586 + sqlite3SafetyOn(db);
1.587 + if( rc==SQLITE_OK ){
1.588 + pTab->pVtab = 0;
1.589 + }
1.590 + }
1.591 +
1.592 + return rc;
1.593 +}
1.594 +
1.595 +/*
1.596 +** This function invokes either the xRollback or xCommit method
1.597 +** of each of the virtual tables in the sqlite3.aVTrans array. The method
1.598 +** called is identified by the second argument, "offset", which is
1.599 +** the offset of the method to call in the sqlite3_module structure.
1.600 +**
1.601 +** The array is cleared after invoking the callbacks.
1.602 +*/
1.603 +static void callFinaliser(sqlite3 *db, int offset){
1.604 + int i;
1.605 + if( db->aVTrans ){
1.606 + for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
1.607 + sqlite3_vtab *pVtab = db->aVTrans[i];
1.608 + int (*x)(sqlite3_vtab *);
1.609 + x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
1.610 + if( x ) x(pVtab);
1.611 + sqlite3VtabUnlock(db, pVtab);
1.612 + }
1.613 + sqliteFree(db->aVTrans);
1.614 + db->nVTrans = 0;
1.615 + db->aVTrans = 0;
1.616 + }
1.617 +}
1.618 +
1.619 +/*
1.620 +** If argument rc2 is not SQLITE_OK, then return it and do nothing.
1.621 +** Otherwise, invoke the xSync method of all virtual tables in the
1.622 +** sqlite3.aVTrans array. Return the error code for the first error
1.623 +** that occurs, or SQLITE_OK if all xSync operations are successful.
1.624 +*/
1.625 +int sqlite3VtabSync(sqlite3 *db, int rc2){
1.626 + int i;
1.627 + int rc = SQLITE_OK;
1.628 + int rcsafety;
1.629 + sqlite3_vtab **aVTrans = db->aVTrans;
1.630 + if( rc2!=SQLITE_OK ) return rc2;
1.631 +
1.632 + rc = sqlite3SafetyOff(db);
1.633 + db->aVTrans = 0;
1.634 + for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
1.635 + sqlite3_vtab *pVtab = aVTrans[i];
1.636 + int (*x)(sqlite3_vtab *);
1.637 + x = pVtab->pModule->xSync;
1.638 + if( x ){
1.639 + rc = x(pVtab);
1.640 + }
1.641 + }
1.642 + db->aVTrans = aVTrans;
1.643 + rcsafety = sqlite3SafetyOn(db);
1.644 +
1.645 + if( rc==SQLITE_OK ){
1.646 + rc = rcsafety;
1.647 + }
1.648 + return rc;
1.649 +}
1.650 +
1.651 +/*
1.652 +** Invoke the xRollback method of all virtual tables in the
1.653 +** sqlite3.aVTrans array. Then clear the array itself.
1.654 +*/
1.655 +int sqlite3VtabRollback(sqlite3 *db){
1.656 + callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback));
1.657 + return SQLITE_OK;
1.658 +}
1.659 +
1.660 +/*
1.661 +** Invoke the xCommit method of all virtual tables in the
1.662 +** sqlite3.aVTrans array. Then clear the array itself.
1.663 +*/
1.664 +int sqlite3VtabCommit(sqlite3 *db){
1.665 + callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit));
1.666 + return SQLITE_OK;
1.667 +}
1.668 +
1.669 +/*
1.670 +** If the virtual table pVtab supports the transaction interface
1.671 +** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
1.672 +** not currently open, invoke the xBegin method now.
1.673 +**
1.674 +** If the xBegin call is successful, place the sqlite3_vtab pointer
1.675 +** in the sqlite3.aVTrans array.
1.676 +*/
1.677 +int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
1.678 + int rc = SQLITE_OK;
1.679 + const sqlite3_module *pModule;
1.680 +
1.681 + /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
1.682 + ** than zero, then this function is being called from within a
1.683 + ** virtual module xSync() callback. It is illegal to write to
1.684 + ** virtual module tables in this case, so return SQLITE_LOCKED.
1.685 + */
1.686 + if( 0==db->aVTrans && db->nVTrans>0 ){
1.687 + return SQLITE_LOCKED;
1.688 + }
1.689 + if( !pVtab ){
1.690 + return SQLITE_OK;
1.691 + }
1.692 + pModule = pVtab->pModule;
1.693 +
1.694 + if( pModule->xBegin ){
1.695 + int i;
1.696 +
1.697 +
1.698 + /* If pVtab is already in the aVTrans array, return early */
1.699 + for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
1.700 + if( db->aVTrans[i]==pVtab ){
1.701 + return SQLITE_OK;
1.702 + }
1.703 + }
1.704 +
1.705 + /* Invoke the xBegin method */
1.706 + rc = pModule->xBegin(pVtab);
1.707 + if( rc!=SQLITE_OK ){
1.708 + return rc;
1.709 + }
1.710 +
1.711 + rc = addToVTrans(db, pVtab);
1.712 + }
1.713 + return rc;
1.714 +}
1.715 +
1.716 +/*
1.717 +** The first parameter (pDef) is a function implementation. The
1.718 +** second parameter (pExpr) is the first argument to this function.
1.719 +** If pExpr is a column in a virtual table, then let the virtual
1.720 +** table implementation have an opportunity to overload the function.
1.721 +**
1.722 +** This routine is used to allow virtual table implementations to
1.723 +** overload MATCH, LIKE, GLOB, and REGEXP operators.
1.724 +**
1.725 +** Return either the pDef argument (indicating no change) or a
1.726 +** new FuncDef structure that is marked as ephemeral using the
1.727 +** SQLITE_FUNC_EPHEM flag.
1.728 +*/
1.729 +FuncDef *sqlite3VtabOverloadFunction(
1.730 + FuncDef *pDef, /* Function to possibly overload */
1.731 + int nArg, /* Number of arguments to the function */
1.732 + Expr *pExpr /* First argument to the function */
1.733 +){
1.734 + Table *pTab;
1.735 + sqlite3_vtab *pVtab;
1.736 + sqlite3_module *pMod;
1.737 + void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
1.738 + void *pArg;
1.739 + FuncDef *pNew;
1.740 + int rc;
1.741 + char *zLowerName;
1.742 + unsigned char *z;
1.743 +
1.744 +
1.745 + /* Check to see the left operand is a column in a virtual table */
1.746 + if( pExpr==0 ) return pDef;
1.747 + if( pExpr->op!=TK_COLUMN ) return pDef;
1.748 + pTab = pExpr->pTab;
1.749 + if( pTab==0 ) return pDef;
1.750 + if( !pTab->isVirtual ) return pDef;
1.751 + pVtab = pTab->pVtab;
1.752 + assert( pVtab!=0 );
1.753 + assert( pVtab->pModule!=0 );
1.754 + pMod = (sqlite3_module *)pVtab->pModule;
1.755 + if( pMod->xFindFunction==0 ) return pDef;
1.756 +
1.757 + /* Call the xFuncFunction method on the virtual table implementation
1.758 + ** to see if the implementation wants to overload this function
1.759 + */
1.760 + zLowerName = sqlite3StrDup(pDef->zName);
1.761 + for(z=(unsigned char*)zLowerName; *z; z++){
1.762 + *z = sqlite3UpperToLower[*z];
1.763 + }
1.764 + rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
1.765 + sqliteFree(zLowerName);
1.766 + if( rc==0 ){
1.767 + return pDef;
1.768 + }
1.769 +
1.770 + /* Create a new ephemeral function definition for the overloaded
1.771 + ** function */
1.772 + pNew = sqliteMalloc( sizeof(*pNew) + strlen(pDef->zName) );
1.773 + if( pNew==0 ){
1.774 + return pDef;
1.775 + }
1.776 + *pNew = *pDef;
1.777 + memcpy(pNew->zName, pDef->zName, strlen(pDef->zName)+1);
1.778 + pNew->xFunc = xFunc;
1.779 + pNew->pUserData = pArg;
1.780 + pNew->flags |= SQLITE_FUNC_EPHEM;
1.781 + return pNew;
1.782 +}
1.783 +
1.784 +#endif /* SQLITE_OMIT_VIRTUALTABLE */