| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2006 June 10 | 2 ** 2006 June 10 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file contains code used to help implement virtual tables. | 12 ** This file contains code used to help implement virtual tables. |
| 13 */ | 13 */ |
| 14 #ifndef SQLITE_OMIT_VIRTUALTABLE | 14 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 15 #include "sqliteInt.h" | 15 #include "sqliteInt.h" |
| 16 | 16 |
| 17 /* | 17 /* |
| 18 ** Before a virtual table xCreate() or xConnect() method is invoked, the |
| 19 ** sqlite3.pVtabCtx member variable is set to point to an instance of |
| 20 ** this struct allocated on the stack. It is used by the implementation of |
| 21 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which |
| 22 ** are invoked only from within xCreate and xConnect methods. |
| 23 */ |
| 24 struct VtabCtx { |
| 25 VTable *pVTable; /* The virtual table being constructed */ |
| 26 Table *pTab; /* The Table object to which the virtual table belongs */ |
| 27 }; |
| 28 |
| 29 /* |
| 18 ** The actual function that does the work of creating a new module. | 30 ** The actual function that does the work of creating a new module. |
| 19 ** This function implements the sqlite3_create_module() and | 31 ** This function implements the sqlite3_create_module() and |
| 20 ** sqlite3_create_module_v2() interfaces. | 32 ** sqlite3_create_module_v2() interfaces. |
| 21 */ | 33 */ |
| 22 static int createModule( | 34 static int createModule( |
| 23 sqlite3 *db, /* Database in which module is registered */ | 35 sqlite3 *db, /* Database in which module is registered */ |
| 24 const char *zName, /* Name assigned to this module */ | 36 const char *zName, /* Name assigned to this module */ |
| 25 const sqlite3_module *pModule, /* The definition of the module */ | 37 const sqlite3_module *pModule, /* The definition of the module */ |
| 26 void *pAux, /* Context pointer for xCreate/xConnect */ | 38 void *pAux, /* Context pointer for xCreate/xConnect */ |
| 27 void (*xDestroy)(void *) /* Module destructor function */ | 39 void (*xDestroy)(void *) /* Module destructor function */ |
| 28 ){ | 40 ){ |
| 29 int rc, nName; | 41 int rc = SQLITE_OK; |
| 30 Module *pMod; | 42 int nName; |
| 31 | 43 |
| 32 sqlite3_mutex_enter(db->mutex); | 44 sqlite3_mutex_enter(db->mutex); |
| 33 nName = sqlite3Strlen30(zName); | 45 nName = sqlite3Strlen30(zName); |
| 34 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1); | 46 if( sqlite3HashFind(&db->aModule, zName) ){ |
| 35 if( pMod ){ | 47 rc = SQLITE_MISUSE_BKPT; |
| 36 Module *pDel; | 48 }else{ |
| 37 char *zCopy = (char *)(&pMod[1]); | 49 Module *pMod; |
| 38 memcpy(zCopy, zName, nName+1); | 50 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1); |
| 39 pMod->zName = zCopy; | 51 if( pMod ){ |
| 40 pMod->pModule = pModule; | 52 Module *pDel; |
| 41 pMod->pAux = pAux; | 53 char *zCopy = (char *)(&pMod[1]); |
| 42 pMod->xDestroy = xDestroy; | 54 memcpy(zCopy, zName, nName+1); |
| 43 pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod); | 55 pMod->zName = zCopy; |
| 44 if( pDel && pDel->xDestroy ){ | 56 pMod->pModule = pModule; |
| 45 pDel->xDestroy(pDel->pAux); | 57 pMod->pAux = pAux; |
| 58 pMod->xDestroy = xDestroy; |
| 59 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod); |
| 60 assert( pDel==0 || pDel==pMod ); |
| 61 if( pDel ){ |
| 62 db->mallocFailed = 1; |
| 63 sqlite3DbFree(db, pDel); |
| 64 } |
| 46 } | 65 } |
| 47 sqlite3DbFree(db, pDel); | |
| 48 if( pDel==pMod ){ | |
| 49 db->mallocFailed = 1; | |
| 50 } | |
| 51 sqlite3ResetInternalSchema(db, -1); | |
| 52 }else if( xDestroy ){ | |
| 53 xDestroy(pAux); | |
| 54 } | 66 } |
| 55 rc = sqlite3ApiExit(db, SQLITE_OK); | 67 rc = sqlite3ApiExit(db, rc); |
| 68 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux); |
| 69 |
| 56 sqlite3_mutex_leave(db->mutex); | 70 sqlite3_mutex_leave(db->mutex); |
| 57 return rc; | 71 return rc; |
| 58 } | 72 } |
| 59 | 73 |
| 60 | 74 |
| 61 /* | 75 /* |
| 62 ** External API function used to create a new virtual-table module. | 76 ** External API function used to create a new virtual-table module. |
| 63 */ | 77 */ |
| 64 int sqlite3_create_module( | 78 int sqlite3_create_module( |
| 65 sqlite3 *db, /* Database in which module is registered */ | 79 sqlite3 *db, /* Database in which module is registered */ |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 124 |
| 111 /* | 125 /* |
| 112 ** Decrement the ref-count on a virtual table object. When the ref-count | 126 ** Decrement the ref-count on a virtual table object. When the ref-count |
| 113 ** reaches zero, call the xDisconnect() method to delete the object. | 127 ** reaches zero, call the xDisconnect() method to delete the object. |
| 114 */ | 128 */ |
| 115 void sqlite3VtabUnlock(VTable *pVTab){ | 129 void sqlite3VtabUnlock(VTable *pVTab){ |
| 116 sqlite3 *db = pVTab->db; | 130 sqlite3 *db = pVTab->db; |
| 117 | 131 |
| 118 assert( db ); | 132 assert( db ); |
| 119 assert( pVTab->nRef>0 ); | 133 assert( pVTab->nRef>0 ); |
| 120 assert( sqlite3SafetyCheckOk(db) ); | 134 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE ); |
| 121 | 135 |
| 122 pVTab->nRef--; | 136 pVTab->nRef--; |
| 123 if( pVTab->nRef==0 ){ | 137 if( pVTab->nRef==0 ){ |
| 124 sqlite3_vtab *p = pVTab->pVtab; | 138 sqlite3_vtab *p = pVTab->pVtab; |
| 125 if( p ){ | 139 if( p ){ |
| 126 p->pModule->xDisconnect(p); | 140 p->pModule->xDisconnect(p); |
| 127 } | 141 } |
| 128 sqlite3DbFree(db, pVTab); | 142 sqlite3DbFree(db, pVTab); |
| 129 } | 143 } |
| 130 } | 144 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 161 pVTable->pNext = db2->pDisconnect; | 175 pVTable->pNext = db2->pDisconnect; |
| 162 db2->pDisconnect = pVTable; | 176 db2->pDisconnect = pVTable; |
| 163 } | 177 } |
| 164 pVTable = pNext; | 178 pVTable = pNext; |
| 165 } | 179 } |
| 166 | 180 |
| 167 assert( !db || pRet ); | 181 assert( !db || pRet ); |
| 168 return pRet; | 182 return pRet; |
| 169 } | 183 } |
| 170 | 184 |
| 185 /* |
| 186 ** Table *p is a virtual table. This function removes the VTable object |
| 187 ** for table *p associated with database connection db from the linked |
| 188 ** list in p->pVTab. It also decrements the VTable ref count. This is |
| 189 ** used when closing database connection db to free all of its VTable |
| 190 ** objects without disturbing the rest of the Schema object (which may |
| 191 ** be being used by other shared-cache connections). |
| 192 */ |
| 193 void sqlite3VtabDisconnect(sqlite3 *db, Table *p){ |
| 194 VTable **ppVTab; |
| 195 |
| 196 assert( IsVirtual(p) ); |
| 197 assert( sqlite3BtreeHoldsAllMutexes(db) ); |
| 198 assert( sqlite3_mutex_held(db->mutex) ); |
| 199 |
| 200 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){ |
| 201 if( (*ppVTab)->db==db ){ |
| 202 VTable *pVTab = *ppVTab; |
| 203 *ppVTab = pVTab->pNext; |
| 204 sqlite3VtabUnlock(pVTab); |
| 205 break; |
| 206 } |
| 207 } |
| 208 } |
| 209 |
| 171 | 210 |
| 172 /* | 211 /* |
| 173 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list. | 212 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list. |
| 174 ** | 213 ** |
| 175 ** This function may only be called when the mutexes associated with all | 214 ** This function may only be called when the mutexes associated with all |
| 176 ** shared b-tree databases opened using connection db are held by the | 215 ** shared b-tree databases opened using connection db are held by the |
| 177 ** caller. This is done to protect the sqlite3.pDisconnect list. The | 216 ** caller. This is done to protect the sqlite3.pDisconnect list. The |
| 178 ** sqlite3.pDisconnect list is accessed only as follows: | 217 ** sqlite3.pDisconnect list is accessed only as follows: |
| 179 ** | 218 ** |
| 180 ** 1) By this function. In this case, all BtShared mutexes and the mutex | 219 ** 1) By this function. In this case, all BtShared mutexes and the mutex |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 ** connection db is decremented immediately (which may lead to the | 257 ** connection db is decremented immediately (which may lead to the |
| 219 ** structure being xDisconnected and free). Any other VTable structures | 258 ** structure being xDisconnected and free). Any other VTable structures |
| 220 ** in the list are moved to the sqlite3.pDisconnect list of the associated | 259 ** in the list are moved to the sqlite3.pDisconnect list of the associated |
| 221 ** database connection. | 260 ** database connection. |
| 222 */ | 261 */ |
| 223 void sqlite3VtabClear(sqlite3 *db, Table *p){ | 262 void sqlite3VtabClear(sqlite3 *db, Table *p){ |
| 224 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); | 263 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); |
| 225 if( p->azModuleArg ){ | 264 if( p->azModuleArg ){ |
| 226 int i; | 265 int i; |
| 227 for(i=0; i<p->nModuleArg; i++){ | 266 for(i=0; i<p->nModuleArg; i++){ |
| 228 sqlite3DbFree(db, p->azModuleArg[i]); | 267 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]); |
| 229 } | 268 } |
| 230 sqlite3DbFree(db, p->azModuleArg); | 269 sqlite3DbFree(db, p->azModuleArg); |
| 231 } | 270 } |
| 232 } | 271 } |
| 233 | 272 |
| 234 /* | 273 /* |
| 235 ** Add a new module argument to pTable->azModuleArg[]. | 274 ** Add a new module argument to pTable->azModuleArg[]. |
| 236 ** The string is not copied - the pointer is stored. The | 275 ** The string is not copied - the pointer is stored. The |
| 237 ** string will be freed automatically when the table is | 276 ** string will be freed automatically when the table is |
| 238 ** deleted. | 277 ** deleted. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 259 | 298 |
| 260 /* | 299 /* |
| 261 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE | 300 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE |
| 262 ** statement. The module name has been parsed, but the optional list | 301 ** statement. The module name has been parsed, but the optional list |
| 263 ** of parameters that follow the module name are still pending. | 302 ** of parameters that follow the module name are still pending. |
| 264 */ | 303 */ |
| 265 void sqlite3VtabBeginParse( | 304 void sqlite3VtabBeginParse( |
| 266 Parse *pParse, /* Parsing context */ | 305 Parse *pParse, /* Parsing context */ |
| 267 Token *pName1, /* Name of new table, or database name */ | 306 Token *pName1, /* Name of new table, or database name */ |
| 268 Token *pName2, /* Name of new table or NULL */ | 307 Token *pName2, /* Name of new table or NULL */ |
| 269 Token *pModuleName /* Name of the module for the virtual table */ | 308 Token *pModuleName, /* Name of the module for the virtual table */ |
| 309 int ifNotExists /* No error if the table already exists */ |
| 270 ){ | 310 ){ |
| 271 int iDb; /* The database the table is being created in */ | 311 int iDb; /* The database the table is being created in */ |
| 272 Table *pTable; /* The new virtual table */ | 312 Table *pTable; /* The new virtual table */ |
| 273 sqlite3 *db; /* Database connection */ | 313 sqlite3 *db; /* Database connection */ |
| 274 | 314 |
| 275 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0); | 315 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists); |
| 276 pTable = pParse->pNewTable; | 316 pTable = pParse->pNewTable; |
| 277 if( pTable==0 ) return; | 317 if( pTable==0 ) return; |
| 278 assert( 0==pTable->pIndex ); | 318 assert( 0==pTable->pIndex ); |
| 279 | 319 |
| 280 db = pParse->db; | 320 db = pParse->db; |
| 281 iDb = sqlite3SchemaToIndex(db, pTable->pSchema); | 321 iDb = sqlite3SchemaToIndex(db, pTable->pSchema); |
| 282 assert( iDb>=0 ); | 322 assert( iDb>=0 ); |
| 283 | 323 |
| 284 pTable->tabFlags |= TF_Virtual; | 324 pTable->tabFlags |= TF_Virtual; |
| 285 pTable->nModuleArg = 0; | 325 pTable->nModuleArg = 0; |
| 286 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); | 326 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); |
| 287 addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName)); | 327 addModuleArgument(db, pTable, 0); |
| 288 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); | 328 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); |
| 289 pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z); | 329 pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z); |
| 290 | 330 |
| 291 #ifndef SQLITE_OMIT_AUTHORIZATION | 331 #ifndef SQLITE_OMIT_AUTHORIZATION |
| 292 /* Creating a virtual table invokes the authorization callback twice. | 332 /* Creating a virtual table invokes the authorization callback twice. |
| 293 ** The first invocation, to obtain permission to INSERT a row into the | 333 ** The first invocation, to obtain permission to INSERT a row into the |
| 294 ** sqlite_master table, has already been made by sqlite3StartTable(). | 334 ** sqlite_master table, has already been made by sqlite3StartTable(). |
| 295 ** The second call, to obtain permission to create the table, is made now. | 335 ** The second call, to obtain permission to create the table, is made now. |
| 296 */ | 336 */ |
| 297 if( pTable->azModuleArg ){ | 337 if( pTable->azModuleArg ){ |
| 298 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, | 338 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, |
| 299 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName); | 339 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName); |
| 300 } | 340 } |
| 301 #endif | 341 #endif |
| 302 } | 342 } |
| 303 | 343 |
| 304 /* | 344 /* |
| 305 ** This routine takes the module argument that has been accumulating | 345 ** This routine takes the module argument that has been accumulating |
| 306 ** in pParse->zArg[] and appends it to the list of arguments on the | 346 ** in pParse->zArg[] and appends it to the list of arguments on the |
| 307 ** virtual table currently under construction in pParse->pTable. | 347 ** virtual table currently under construction in pParse->pTable. |
| 308 */ | 348 */ |
| 309 static void addArgumentToVtab(Parse *pParse){ | 349 static void addArgumentToVtab(Parse *pParse){ |
| 310 if( pParse->sArg.z && ALWAYS(pParse->pNewTable) ){ | 350 if( pParse->sArg.z && pParse->pNewTable ){ |
| 311 const char *z = (const char*)pParse->sArg.z; | 351 const char *z = (const char*)pParse->sArg.z; |
| 312 int n = pParse->sArg.n; | 352 int n = pParse->sArg.n; |
| 313 sqlite3 *db = pParse->db; | 353 sqlite3 *db = pParse->db; |
| 314 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); | 354 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); |
| 315 } | 355 } |
| 316 } | 356 } |
| 317 | 357 |
| 318 /* | 358 /* |
| 319 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement | 359 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement |
| 320 ** has been completely parsed. | 360 ** has been completely parsed. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 pTab->zName, | 404 pTab->zName, |
| 365 zStmt, | 405 zStmt, |
| 366 pParse->regRowid | 406 pParse->regRowid |
| 367 ); | 407 ); |
| 368 sqlite3DbFree(db, zStmt); | 408 sqlite3DbFree(db, zStmt); |
| 369 v = sqlite3GetVdbe(pParse); | 409 v = sqlite3GetVdbe(pParse); |
| 370 sqlite3ChangeCookie(pParse, iDb); | 410 sqlite3ChangeCookie(pParse, iDb); |
| 371 | 411 |
| 372 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); | 412 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); |
| 373 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); | 413 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); |
| 374 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC); | 414 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere); |
| 375 sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, | 415 sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, |
| 376 pTab->zName, sqlite3Strlen30(pTab->zName) + 1); | 416 pTab->zName, sqlite3Strlen30(pTab->zName) + 1); |
| 377 } | 417 } |
| 378 | 418 |
| 379 /* If we are rereading the sqlite_master table create the in-memory | 419 /* If we are rereading the sqlite_master table create the in-memory |
| 380 ** record of the table. The xConnect() method is not called until | 420 ** record of the table. The xConnect() method is not called until |
| 381 ** the first time the virtual table is used in an SQL statement. This | 421 ** the first time the virtual table is used in an SQL statement. This |
| 382 ** allows a schema that contains virtual tables to be loaded before | 422 ** allows a schema that contains virtual tables to be loaded before |
| 383 ** the required virtual table implementations are registered. */ | 423 ** the required virtual table implementations are registered. */ |
| 384 else { | 424 else { |
| 385 Table *pOld; | 425 Table *pOld; |
| 386 Schema *pSchema = pTab->pSchema; | 426 Schema *pSchema = pTab->pSchema; |
| 387 const char *zName = pTab->zName; | 427 const char *zName = pTab->zName; |
| 388 int nName = sqlite3Strlen30(zName); | |
| 389 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) ); | 428 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) ); |
| 390 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab); | 429 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab); |
| 391 if( pOld ){ | 430 if( pOld ){ |
| 392 db->mallocFailed = 1; | 431 db->mallocFailed = 1; |
| 393 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ | 432 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ |
| 394 return; | 433 return; |
| 395 } | 434 } |
| 396 pParse->pNewTable = 0; | 435 pParse->pNewTable = 0; |
| 397 } | 436 } |
| 398 } | 437 } |
| 399 | 438 |
| 400 /* | 439 /* |
| (...skipping 26 matching lines...) Expand all Loading... |
| 427 ** pointer to the function to invoke is passed as the fourth parameter | 466 ** pointer to the function to invoke is passed as the fourth parameter |
| 428 ** to this procedure. | 467 ** to this procedure. |
| 429 */ | 468 */ |
| 430 static int vtabCallConstructor( | 469 static int vtabCallConstructor( |
| 431 sqlite3 *db, | 470 sqlite3 *db, |
| 432 Table *pTab, | 471 Table *pTab, |
| 433 Module *pMod, | 472 Module *pMod, |
| 434 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), | 473 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), |
| 435 char **pzErr | 474 char **pzErr |
| 436 ){ | 475 ){ |
| 476 VtabCtx sCtx, *pPriorCtx; |
| 437 VTable *pVTable; | 477 VTable *pVTable; |
| 438 int rc; | 478 int rc; |
| 439 const char *const*azArg = (const char *const*)pTab->azModuleArg; | 479 const char *const*azArg = (const char *const*)pTab->azModuleArg; |
| 440 int nArg = pTab->nModuleArg; | 480 int nArg = pTab->nModuleArg; |
| 441 char *zErr = 0; | 481 char *zErr = 0; |
| 442 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); | 482 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); |
| 483 int iDb; |
| 443 | 484 |
| 444 if( !zModuleName ){ | 485 if( !zModuleName ){ |
| 445 return SQLITE_NOMEM; | 486 return SQLITE_NOMEM; |
| 446 } | 487 } |
| 447 | 488 |
| 448 pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); | 489 pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); |
| 449 if( !pVTable ){ | 490 if( !pVTable ){ |
| 450 sqlite3DbFree(db, zModuleName); | 491 sqlite3DbFree(db, zModuleName); |
| 451 return SQLITE_NOMEM; | 492 return SQLITE_NOMEM; |
| 452 } | 493 } |
| 453 pVTable->db = db; | 494 pVTable->db = db; |
| 454 pVTable->pMod = pMod; | 495 pVTable->pMod = pMod; |
| 455 | 496 |
| 456 assert( !db->pVTab ); | 497 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 457 assert( xConstruct ); | 498 pTab->azModuleArg[1] = db->aDb[iDb].zName; |
| 458 db->pVTab = pTab; | |
| 459 | 499 |
| 460 /* Invoke the virtual table constructor */ | 500 /* Invoke the virtual table constructor */ |
| 501 assert( &db->pVtabCtx ); |
| 502 assert( xConstruct ); |
| 503 sCtx.pTab = pTab; |
| 504 sCtx.pVTable = pVTable; |
| 505 pPriorCtx = db->pVtabCtx; |
| 506 db->pVtabCtx = &sCtx; |
| 461 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); | 507 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); |
| 508 db->pVtabCtx = pPriorCtx; |
| 462 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; | 509 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; |
| 463 | 510 |
| 464 if( SQLITE_OK!=rc ){ | 511 if( SQLITE_OK!=rc ){ |
| 465 if( zErr==0 ){ | 512 if( zErr==0 ){ |
| 466 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); | 513 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); |
| 467 }else { | 514 }else { |
| 468 *pzErr = sqlite3MPrintf(db, "%s", zErr); | 515 *pzErr = sqlite3MPrintf(db, "%s", zErr); |
| 469 sqlite3_free(zErr); | 516 sqlite3_free(zErr); |
| 470 } | 517 } |
| 471 sqlite3DbFree(db, pVTable); | 518 sqlite3DbFree(db, pVTable); |
| 472 }else if( ALWAYS(pVTable->pVtab) ){ | 519 }else if( ALWAYS(pVTable->pVtab) ){ |
| 473 /* Justification of ALWAYS(): A correct vtab constructor must allocate | 520 /* Justification of ALWAYS(): A correct vtab constructor must allocate |
| 474 ** the sqlite3_vtab object if successful. */ | 521 ** the sqlite3_vtab object if successful. */ |
| 522 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0])); |
| 475 pVTable->pVtab->pModule = pMod->pModule; | 523 pVTable->pVtab->pModule = pMod->pModule; |
| 476 pVTable->nRef = 1; | 524 pVTable->nRef = 1; |
| 477 if( db->pVTab ){ | 525 if( sCtx.pTab ){ |
| 478 const char *zFormat = "vtable constructor did not declare schema: %s"; | 526 const char *zFormat = "vtable constructor did not declare schema: %s"; |
| 479 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); | 527 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); |
| 480 sqlite3VtabUnlock(pVTable); | 528 sqlite3VtabUnlock(pVTable); |
| 481 rc = SQLITE_ERROR; | 529 rc = SQLITE_ERROR; |
| 482 }else{ | 530 }else{ |
| 483 int iCol; | 531 int iCol; |
| 484 /* If everything went according to plan, link the new VTable structure | 532 /* If everything went according to plan, link the new VTable structure |
| 485 ** into the linked list headed by pTab->pVTable. Then loop through the | 533 ** into the linked list headed by pTab->pVTable. Then loop through the |
| 486 ** columns of the table to see if any of them contain the token "hidden". | 534 ** columns of the table to see if any of them contain the token "hidden". |
| 487 ** If so, set the Column.isHidden flag and remove the token from | 535 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from |
| 488 ** the type string. */ | 536 ** the type string. */ |
| 489 pVTable->pNext = pTab->pVTable; | 537 pVTable->pNext = pTab->pVTable; |
| 490 pTab->pVTable = pVTable; | 538 pTab->pVTable = pVTable; |
| 491 | 539 |
| 492 for(iCol=0; iCol<pTab->nCol; iCol++){ | 540 for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 493 char *zType = pTab->aCol[iCol].zType; | 541 char *zType = pTab->aCol[iCol].zType; |
| 494 int nType; | 542 int nType; |
| 495 int i = 0; | 543 int i = 0; |
| 496 if( !zType ) continue; | 544 if( !zType ) continue; |
| 497 nType = sqlite3Strlen30(zType); | 545 nType = sqlite3Strlen30(zType); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 508 if( i<nType ){ | 556 if( i<nType ){ |
| 509 int j; | 557 int j; |
| 510 int nDel = 6 + (zType[i+6] ? 1 : 0); | 558 int nDel = 6 + (zType[i+6] ? 1 : 0); |
| 511 for(j=i; (j+nDel)<=nType; j++){ | 559 for(j=i; (j+nDel)<=nType; j++){ |
| 512 zType[j] = zType[j+nDel]; | 560 zType[j] = zType[j+nDel]; |
| 513 } | 561 } |
| 514 if( zType[i]=='\0' && i>0 ){ | 562 if( zType[i]=='\0' && i>0 ){ |
| 515 assert(zType[i-1]==' '); | 563 assert(zType[i-1]==' '); |
| 516 zType[i-1] = '\0'; | 564 zType[i-1] = '\0'; |
| 517 } | 565 } |
| 518 pTab->aCol[iCol].isHidden = 1; | 566 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN; |
| 519 } | 567 } |
| 520 } | 568 } |
| 521 } | 569 } |
| 522 } | 570 } |
| 523 | 571 |
| 524 sqlite3DbFree(db, zModuleName); | 572 sqlite3DbFree(db, zModuleName); |
| 525 db->pVTab = 0; | |
| 526 return rc; | 573 return rc; |
| 527 } | 574 } |
| 528 | 575 |
| 529 /* | 576 /* |
| 530 ** This function is invoked by the parser to call the xConnect() method | 577 ** This function is invoked by the parser to call the xConnect() method |
| 531 ** of the virtual table pTab. If an error occurs, an error code is returned | 578 ** of the virtual table pTab. If an error occurs, an error code is returned |
| 532 ** and an error left in pParse. | 579 ** and an error left in pParse. |
| 533 ** | 580 ** |
| 534 ** This call is a no-op if table pTab is not a virtual table. | 581 ** This call is a no-op if table pTab is not a virtual table. |
| 535 */ | 582 */ |
| 536 int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ | 583 int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ |
| 537 sqlite3 *db = pParse->db; | 584 sqlite3 *db = pParse->db; |
| 538 const char *zMod; | 585 const char *zMod; |
| 539 Module *pMod; | 586 Module *pMod; |
| 540 int rc; | 587 int rc; |
| 541 | 588 |
| 542 assert( pTab ); | 589 assert( pTab ); |
| 543 if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){ | 590 if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){ |
| 544 return SQLITE_OK; | 591 return SQLITE_OK; |
| 545 } | 592 } |
| 546 | 593 |
| 547 /* Locate the required virtual table module */ | 594 /* Locate the required virtual table module */ |
| 548 zMod = pTab->azModuleArg[0]; | 595 zMod = pTab->azModuleArg[0]; |
| 549 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod)); | 596 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod); |
| 550 | 597 |
| 551 if( !pMod ){ | 598 if( !pMod ){ |
| 552 const char *zModule = pTab->azModuleArg[0]; | 599 const char *zModule = pTab->azModuleArg[0]; |
| 553 sqlite3ErrorMsg(pParse, "no such module: %s", zModule); | 600 sqlite3ErrorMsg(pParse, "no such module: %s", zModule); |
| 554 rc = SQLITE_ERROR; | 601 rc = SQLITE_ERROR; |
| 555 }else{ | 602 }else{ |
| 556 char *zErr = 0; | 603 char *zErr = 0; |
| 557 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr); | 604 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr); |
| 558 if( rc!=SQLITE_OK ){ | 605 if( rc!=SQLITE_OK ){ |
| 559 sqlite3ErrorMsg(pParse, "%s", zErr); | 606 sqlite3ErrorMsg(pParse, "%s", zErr); |
| 560 } | 607 } |
| 561 sqlite3DbFree(db, zErr); | 608 sqlite3DbFree(db, zErr); |
| 562 } | 609 } |
| 563 | 610 |
| 564 return rc; | 611 return rc; |
| 565 } | 612 } |
| 566 | |
| 567 /* | 613 /* |
| 568 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. | 614 ** Grow the db->aVTrans[] array so that there is room for at least one |
| 615 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise. |
| 569 */ | 616 */ |
| 570 static int addToVTrans(sqlite3 *db, VTable *pVTab){ | 617 static int growVTrans(sqlite3 *db){ |
| 571 const int ARRAY_INCR = 5; | 618 const int ARRAY_INCR = 5; |
| 572 | 619 |
| 573 /* Grow the sqlite3.aVTrans array if required */ | 620 /* Grow the sqlite3.aVTrans array if required */ |
| 574 if( (db->nVTrans%ARRAY_INCR)==0 ){ | 621 if( (db->nVTrans%ARRAY_INCR)==0 ){ |
| 575 VTable **aVTrans; | 622 VTable **aVTrans; |
| 576 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR); | 623 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR); |
| 577 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes); | 624 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes); |
| 578 if( !aVTrans ){ | 625 if( !aVTrans ){ |
| 579 return SQLITE_NOMEM; | 626 return SQLITE_NOMEM; |
| 580 } | 627 } |
| 581 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); | 628 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); |
| 582 db->aVTrans = aVTrans; | 629 db->aVTrans = aVTrans; |
| 583 } | 630 } |
| 584 | 631 |
| 585 /* Add pVtab to the end of sqlite3.aVTrans */ | |
| 586 db->aVTrans[db->nVTrans++] = pVTab; | |
| 587 sqlite3VtabLock(pVTab); | |
| 588 return SQLITE_OK; | 632 return SQLITE_OK; |
| 589 } | 633 } |
| 590 | 634 |
| 591 /* | 635 /* |
| 636 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should |
| 637 ** have already been reserved using growVTrans(). |
| 638 */ |
| 639 static void addToVTrans(sqlite3 *db, VTable *pVTab){ |
| 640 /* Add pVtab to the end of sqlite3.aVTrans */ |
| 641 db->aVTrans[db->nVTrans++] = pVTab; |
| 642 sqlite3VtabLock(pVTab); |
| 643 } |
| 644 |
| 645 /* |
| 592 ** This function is invoked by the vdbe to call the xCreate method | 646 ** This function is invoked by the vdbe to call the xCreate method |
| 593 ** of the virtual table named zTab in database iDb. | 647 ** of the virtual table named zTab in database iDb. |
| 594 ** | 648 ** |
| 595 ** If an error occurs, *pzErr is set to point an an English language | 649 ** If an error occurs, *pzErr is set to point an an English language |
| 596 ** description of the error and an SQLITE_XXX error code is returned. | 650 ** description of the error and an SQLITE_XXX error code is returned. |
| 597 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr. | 651 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr. |
| 598 */ | 652 */ |
| 599 int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ | 653 int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ |
| 600 int rc = SQLITE_OK; | 654 int rc = SQLITE_OK; |
| 601 Table *pTab; | 655 Table *pTab; |
| 602 Module *pMod; | 656 Module *pMod; |
| 603 const char *zMod; | 657 const char *zMod; |
| 604 | 658 |
| 605 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); | 659 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); |
| 606 assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable ); | 660 assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable ); |
| 607 | 661 |
| 608 /* Locate the required virtual table module */ | 662 /* Locate the required virtual table module */ |
| 609 zMod = pTab->azModuleArg[0]; | 663 zMod = pTab->azModuleArg[0]; |
| 610 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod)); | 664 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod); |
| 611 | 665 |
| 612 /* If the module has been registered and includes a Create method, | 666 /* If the module has been registered and includes a Create method, |
| 613 ** invoke it now. If the module has not been registered, return an | 667 ** invoke it now. If the module has not been registered, return an |
| 614 ** error. Otherwise, do nothing. | 668 ** error. Otherwise, do nothing. |
| 615 */ | 669 */ |
| 616 if( !pMod ){ | 670 if( !pMod ){ |
| 617 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod); | 671 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod); |
| 618 rc = SQLITE_ERROR; | 672 rc = SQLITE_ERROR; |
| 619 }else{ | 673 }else{ |
| 620 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); | 674 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); |
| 621 } | 675 } |
| 622 | 676 |
| 623 /* Justification of ALWAYS(): The xConstructor method is required to | 677 /* Justification of ALWAYS(): The xConstructor method is required to |
| 624 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */ | 678 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */ |
| 625 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){ | 679 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){ |
| 626 rc = addToVTrans(db, sqlite3GetVTable(db, pTab)); | 680 rc = growVTrans(db); |
| 681 if( rc==SQLITE_OK ){ |
| 682 addToVTrans(db, sqlite3GetVTable(db, pTab)); |
| 683 } |
| 627 } | 684 } |
| 628 | 685 |
| 629 return rc; | 686 return rc; |
| 630 } | 687 } |
| 631 | 688 |
| 632 /* | 689 /* |
| 633 ** This function is used to set the schema of a virtual table. It is only | 690 ** This function is used to set the schema of a virtual table. It is only |
| 634 ** valid to call this function from within the xCreate() or xConnect() of a | 691 ** valid to call this function from within the xCreate() or xConnect() of a |
| 635 ** virtual table module. | 692 ** virtual table module. |
| 636 */ | 693 */ |
| 637 int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ | 694 int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ |
| 638 Parse *pParse; | 695 Parse *pParse; |
| 639 | 696 |
| 640 int rc = SQLITE_OK; | 697 int rc = SQLITE_OK; |
| 641 Table *pTab; | 698 Table *pTab; |
| 642 char *zErr = 0; | 699 char *zErr = 0; |
| 643 | 700 |
| 644 sqlite3_mutex_enter(db->mutex); | 701 sqlite3_mutex_enter(db->mutex); |
| 645 pTab = db->pVTab; | 702 if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){ |
| 646 if( !pTab ){ | 703 sqlite3Error(db, SQLITE_MISUSE); |
| 647 sqlite3Error(db, SQLITE_MISUSE, 0); | |
| 648 sqlite3_mutex_leave(db->mutex); | 704 sqlite3_mutex_leave(db->mutex); |
| 649 return SQLITE_MISUSE_BKPT; | 705 return SQLITE_MISUSE_BKPT; |
| 650 } | 706 } |
| 651 assert( (pTab->tabFlags & TF_Virtual)!=0 ); | 707 assert( (pTab->tabFlags & TF_Virtual)!=0 ); |
| 652 | 708 |
| 653 pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); | 709 pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); |
| 654 if( pParse==0 ){ | 710 if( pParse==0 ){ |
| 655 rc = SQLITE_NOMEM; | 711 rc = SQLITE_NOMEM; |
| 656 }else{ | 712 }else{ |
| 657 pParse->declareVtab = 1; | 713 pParse->declareVtab = 1; |
| 658 pParse->db = db; | 714 pParse->db = db; |
| 659 pParse->nQueryLoop = 1; | 715 pParse->nQueryLoop = 1; |
| 660 | 716 |
| 661 if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) | 717 if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) |
| 662 && pParse->pNewTable | 718 && pParse->pNewTable |
| 663 && !db->mallocFailed | 719 && !db->mallocFailed |
| 664 && !pParse->pNewTable->pSelect | 720 && !pParse->pNewTable->pSelect |
| 665 && (pParse->pNewTable->tabFlags & TF_Virtual)==0 | 721 && (pParse->pNewTable->tabFlags & TF_Virtual)==0 |
| 666 ){ | 722 ){ |
| 667 if( !pTab->aCol ){ | 723 if( !pTab->aCol ){ |
| 668 pTab->aCol = pParse->pNewTable->aCol; | 724 pTab->aCol = pParse->pNewTable->aCol; |
| 669 pTab->nCol = pParse->pNewTable->nCol; | 725 pTab->nCol = pParse->pNewTable->nCol; |
| 670 pParse->pNewTable->nCol = 0; | 726 pParse->pNewTable->nCol = 0; |
| 671 pParse->pNewTable->aCol = 0; | 727 pParse->pNewTable->aCol = 0; |
| 672 } | 728 } |
| 673 db->pVTab = 0; | 729 db->pVtabCtx->pTab = 0; |
| 674 }else{ | 730 }else{ |
| 675 sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr); | 731 sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr); |
| 676 sqlite3DbFree(db, zErr); | 732 sqlite3DbFree(db, zErr); |
| 677 rc = SQLITE_ERROR; | 733 rc = SQLITE_ERROR; |
| 678 } | 734 } |
| 679 pParse->declareVtab = 0; | 735 pParse->declareVtab = 0; |
| 680 | 736 |
| 681 if( pParse->pVdbe ){ | 737 if( pParse->pVdbe ){ |
| 682 sqlite3VdbeFinalize(pParse->pVdbe); | 738 sqlite3VdbeFinalize(pParse->pVdbe); |
| 683 } | 739 } |
| 684 sqlite3DeleteTable(db, pParse->pNewTable); | 740 sqlite3DeleteTable(db, pParse->pNewTable); |
| 741 sqlite3ParserReset(pParse); |
| 685 sqlite3StackFree(db, pParse); | 742 sqlite3StackFree(db, pParse); |
| 686 } | 743 } |
| 687 | 744 |
| 688 assert( (rc&0xff)==rc ); | 745 assert( (rc&0xff)==rc ); |
| 689 rc = sqlite3ApiExit(db, rc); | 746 rc = sqlite3ApiExit(db, rc); |
| 690 sqlite3_mutex_leave(db->mutex); | 747 sqlite3_mutex_leave(db->mutex); |
| 691 return rc; | 748 return rc; |
| 692 } | 749 } |
| 693 | 750 |
| 694 /* | 751 /* |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 int i; | 790 int i; |
| 734 if( db->aVTrans ){ | 791 if( db->aVTrans ){ |
| 735 for(i=0; i<db->nVTrans; i++){ | 792 for(i=0; i<db->nVTrans; i++){ |
| 736 VTable *pVTab = db->aVTrans[i]; | 793 VTable *pVTab = db->aVTrans[i]; |
| 737 sqlite3_vtab *p = pVTab->pVtab; | 794 sqlite3_vtab *p = pVTab->pVtab; |
| 738 if( p ){ | 795 if( p ){ |
| 739 int (*x)(sqlite3_vtab *); | 796 int (*x)(sqlite3_vtab *); |
| 740 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset); | 797 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset); |
| 741 if( x ) x(p); | 798 if( x ) x(p); |
| 742 } | 799 } |
| 800 pVTab->iSavepoint = 0; |
| 743 sqlite3VtabUnlock(pVTab); | 801 sqlite3VtabUnlock(pVTab); |
| 744 } | 802 } |
| 745 sqlite3DbFree(db, db->aVTrans); | 803 sqlite3DbFree(db, db->aVTrans); |
| 746 db->nVTrans = 0; | 804 db->nVTrans = 0; |
| 747 db->aVTrans = 0; | 805 db->aVTrans = 0; |
| 748 } | 806 } |
| 749 } | 807 } |
| 750 | 808 |
| 751 /* | 809 /* |
| 752 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans | 810 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans |
| 753 ** array. Return the error code for the first error that occurs, or | 811 ** array. Return the error code for the first error that occurs, or |
| 754 ** SQLITE_OK if all xSync operations are successful. | 812 ** SQLITE_OK if all xSync operations are successful. |
| 755 ** | 813 ** |
| 756 ** Set *pzErrmsg to point to a buffer that should be released using | 814 ** If an error message is available, leave it in p->zErrMsg. |
| 757 ** sqlite3DbFree() containing an error message, if one is available. | |
| 758 */ | 815 */ |
| 759 int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){ | 816 int sqlite3VtabSync(sqlite3 *db, Vdbe *p){ |
| 760 int i; | 817 int i; |
| 761 int rc = SQLITE_OK; | 818 int rc = SQLITE_OK; |
| 762 VTable **aVTrans = db->aVTrans; | 819 VTable **aVTrans = db->aVTrans; |
| 763 | 820 |
| 764 db->aVTrans = 0; | 821 db->aVTrans = 0; |
| 765 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){ | 822 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){ |
| 766 int (*x)(sqlite3_vtab *); | 823 int (*x)(sqlite3_vtab *); |
| 767 sqlite3_vtab *pVtab = aVTrans[i]->pVtab; | 824 sqlite3_vtab *pVtab = aVTrans[i]->pVtab; |
| 768 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){ | 825 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){ |
| 769 rc = x(pVtab); | 826 rc = x(pVtab); |
| 770 sqlite3DbFree(db, *pzErrmsg); | 827 sqlite3VtabImportErrmsg(p, pVtab); |
| 771 *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg); | |
| 772 sqlite3_free(pVtab->zErrMsg); | |
| 773 } | 828 } |
| 774 } | 829 } |
| 775 db->aVTrans = aVTrans; | 830 db->aVTrans = aVTrans; |
| 776 return rc; | 831 return rc; |
| 777 } | 832 } |
| 778 | 833 |
| 779 /* | 834 /* |
| 780 ** Invoke the xRollback method of all virtual tables in the | 835 ** Invoke the xRollback method of all virtual tables in the |
| 781 ** sqlite3.aVTrans array. Then clear the array itself. | 836 ** sqlite3.aVTrans array. Then clear the array itself. |
| 782 */ | 837 */ |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 815 return SQLITE_LOCKED; | 870 return SQLITE_LOCKED; |
| 816 } | 871 } |
| 817 if( !pVTab ){ | 872 if( !pVTab ){ |
| 818 return SQLITE_OK; | 873 return SQLITE_OK; |
| 819 } | 874 } |
| 820 pModule = pVTab->pVtab->pModule; | 875 pModule = pVTab->pVtab->pModule; |
| 821 | 876 |
| 822 if( pModule->xBegin ){ | 877 if( pModule->xBegin ){ |
| 823 int i; | 878 int i; |
| 824 | 879 |
| 825 | |
| 826 /* If pVtab is already in the aVTrans array, return early */ | 880 /* If pVtab is already in the aVTrans array, return early */ |
| 827 for(i=0; i<db->nVTrans; i++){ | 881 for(i=0; i<db->nVTrans; i++){ |
| 828 if( db->aVTrans[i]==pVTab ){ | 882 if( db->aVTrans[i]==pVTab ){ |
| 829 return SQLITE_OK; | 883 return SQLITE_OK; |
| 830 } | 884 } |
| 831 } | 885 } |
| 832 | 886 |
| 833 /* Invoke the xBegin method */ | 887 /* Invoke the xBegin method. If successful, add the vtab to the |
| 834 rc = pModule->xBegin(pVTab->pVtab); | 888 ** sqlite3.aVTrans[] array. */ |
| 889 rc = growVTrans(db); |
| 835 if( rc==SQLITE_OK ){ | 890 if( rc==SQLITE_OK ){ |
| 836 rc = addToVTrans(db, pVTab); | 891 rc = pModule->xBegin(pVTab->pVtab); |
| 892 if( rc==SQLITE_OK ){ |
| 893 addToVTrans(db, pVTab); |
| 894 } |
| 837 } | 895 } |
| 838 } | 896 } |
| 839 return rc; | 897 return rc; |
| 898 } |
| 899 |
| 900 /* |
| 901 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all |
| 902 ** virtual tables that currently have an open transaction. Pass iSavepoint |
| 903 ** as the second argument to the virtual table method invoked. |
| 904 ** |
| 905 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is |
| 906 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is |
| 907 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with |
| 908 ** an open transaction is invoked. |
| 909 ** |
| 910 ** If any virtual table method returns an error code other than SQLITE_OK, |
| 911 ** processing is abandoned and the error returned to the caller of this |
| 912 ** function immediately. If all calls to virtual table methods are successful, |
| 913 ** SQLITE_OK is returned. |
| 914 */ |
| 915 int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){ |
| 916 int rc = SQLITE_OK; |
| 917 |
| 918 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN ); |
| 919 assert( iSavepoint>=0 ); |
| 920 if( db->aVTrans ){ |
| 921 int i; |
| 922 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){ |
| 923 VTable *pVTab = db->aVTrans[i]; |
| 924 const sqlite3_module *pMod = pVTab->pMod->pModule; |
| 925 if( pVTab->pVtab && pMod->iVersion>=2 ){ |
| 926 int (*xMethod)(sqlite3_vtab *, int); |
| 927 switch( op ){ |
| 928 case SAVEPOINT_BEGIN: |
| 929 xMethod = pMod->xSavepoint; |
| 930 pVTab->iSavepoint = iSavepoint+1; |
| 931 break; |
| 932 case SAVEPOINT_ROLLBACK: |
| 933 xMethod = pMod->xRollbackTo; |
| 934 break; |
| 935 default: |
| 936 xMethod = pMod->xRelease; |
| 937 break; |
| 938 } |
| 939 if( xMethod && pVTab->iSavepoint>iSavepoint ){ |
| 940 rc = xMethod(pVTab->pVtab, iSavepoint); |
| 941 } |
| 942 } |
| 943 } |
| 944 } |
| 945 return rc; |
| 840 } | 946 } |
| 841 | 947 |
| 842 /* | 948 /* |
| 843 ** The first parameter (pDef) is a function implementation. The | 949 ** The first parameter (pDef) is a function implementation. The |
| 844 ** second parameter (pExpr) is the first argument to this function. | 950 ** second parameter (pExpr) is the first argument to this function. |
| 845 ** If pExpr is a column in a virtual table, then let the virtual | 951 ** If pExpr is a column in a virtual table, then let the virtual |
| 846 ** table implementation have an opportunity to overload the function. | 952 ** table implementation have an opportunity to overload the function. |
| 847 ** | 953 ** |
| 848 ** This routine is used to allow virtual table implementations to | 954 ** This routine is used to allow virtual table implementations to |
| 849 ** overload MATCH, LIKE, GLOB, and REGEXP operators. | 955 ** overload MATCH, LIKE, GLOB, and REGEXP operators. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 901 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) | 1007 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) |
| 902 + sqlite3Strlen30(pDef->zName) + 1); | 1008 + sqlite3Strlen30(pDef->zName) + 1); |
| 903 if( pNew==0 ){ | 1009 if( pNew==0 ){ |
| 904 return pDef; | 1010 return pDef; |
| 905 } | 1011 } |
| 906 *pNew = *pDef; | 1012 *pNew = *pDef; |
| 907 pNew->zName = (char *)&pNew[1]; | 1013 pNew->zName = (char *)&pNew[1]; |
| 908 memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1); | 1014 memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1); |
| 909 pNew->xFunc = xFunc; | 1015 pNew->xFunc = xFunc; |
| 910 pNew->pUserData = pArg; | 1016 pNew->pUserData = pArg; |
| 911 pNew->flags |= SQLITE_FUNC_EPHEM; | 1017 pNew->funcFlags |= SQLITE_FUNC_EPHEM; |
| 912 return pNew; | 1018 return pNew; |
| 913 } | 1019 } |
| 914 | 1020 |
| 915 /* | 1021 /* |
| 916 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[] | 1022 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[] |
| 917 ** array so that an OP_VBegin will get generated for it. Add pTab to the | 1023 ** array so that an OP_VBegin will get generated for it. Add pTab to the |
| 918 ** array if it is missing. If pTab is already in the array, this routine | 1024 ** array if it is missing. If pTab is already in the array, this routine |
| 919 ** is a no-op. | 1025 ** is a no-op. |
| 920 */ | 1026 */ |
| 921 void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ | 1027 void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ |
| 922 Parse *pToplevel = sqlite3ParseToplevel(pParse); | 1028 Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 923 int i, n; | 1029 int i, n; |
| 924 Table **apVtabLock; | 1030 Table **apVtabLock; |
| 925 | 1031 |
| 926 assert( IsVirtual(pTab) ); | 1032 assert( IsVirtual(pTab) ); |
| 927 for(i=0; i<pToplevel->nVtabLock; i++){ | 1033 for(i=0; i<pToplevel->nVtabLock; i++){ |
| 928 if( pTab==pToplevel->apVtabLock[i] ) return; | 1034 if( pTab==pToplevel->apVtabLock[i] ) return; |
| 929 } | 1035 } |
| 930 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); | 1036 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); |
| 931 apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n); | 1037 apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n); |
| 932 if( apVtabLock ){ | 1038 if( apVtabLock ){ |
| 933 pToplevel->apVtabLock = apVtabLock; | 1039 pToplevel->apVtabLock = apVtabLock; |
| 934 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; | 1040 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; |
| 935 }else{ | 1041 }else{ |
| 936 pToplevel->db->mallocFailed = 1; | 1042 pToplevel->db->mallocFailed = 1; |
| 937 } | 1043 } |
| 938 } | 1044 } |
| 939 | 1045 |
| 1046 /* |
| 1047 ** Return the ON CONFLICT resolution mode in effect for the virtual |
| 1048 ** table update operation currently in progress. |
| 1049 ** |
| 1050 ** The results of this routine are undefined unless it is called from |
| 1051 ** within an xUpdate method. |
| 1052 */ |
| 1053 int sqlite3_vtab_on_conflict(sqlite3 *db){ |
| 1054 static const unsigned char aMap[] = { |
| 1055 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE |
| 1056 }; |
| 1057 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); |
| 1058 assert( OE_Ignore==4 && OE_Replace==5 ); |
| 1059 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); |
| 1060 return (int)aMap[db->vtabOnConflict-1]; |
| 1061 } |
| 1062 |
| 1063 /* |
| 1064 ** Call from within the xCreate() or xConnect() methods to provide |
| 1065 ** the SQLite core with additional information about the behavior |
| 1066 ** of the virtual table being implemented. |
| 1067 */ |
| 1068 int sqlite3_vtab_config(sqlite3 *db, int op, ...){ |
| 1069 va_list ap; |
| 1070 int rc = SQLITE_OK; |
| 1071 |
| 1072 sqlite3_mutex_enter(db->mutex); |
| 1073 |
| 1074 va_start(ap, op); |
| 1075 switch( op ){ |
| 1076 case SQLITE_VTAB_CONSTRAINT_SUPPORT: { |
| 1077 VtabCtx *p = db->pVtabCtx; |
| 1078 if( !p ){ |
| 1079 rc = SQLITE_MISUSE_BKPT; |
| 1080 }else{ |
| 1081 assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 ); |
| 1082 p->pVTable->bConstraint = (u8)va_arg(ap, int); |
| 1083 } |
| 1084 break; |
| 1085 } |
| 1086 default: |
| 1087 rc = SQLITE_MISUSE_BKPT; |
| 1088 break; |
| 1089 } |
| 1090 va_end(ap); |
| 1091 |
| 1092 if( rc!=SQLITE_OK ) sqlite3Error(db, rc); |
| 1093 sqlite3_mutex_leave(db->mutex); |
| 1094 return rc; |
| 1095 } |
| 1096 |
| 940 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | 1097 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| OLD | NEW |