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