Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/vtab.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / vtab.c` 1405 lines ยท 44515 bytes Raw Blame History 1 /* 2 ** 2006 June 10 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file contains code used to help implement virtual tables. 13 */ 14 #ifndef SQLITE_OMIT_VIRTUALTABLE 15 #include "sqliteInt.h" 16 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 VtabCtx *pPrior; /* Parent context (if any) */ 28 int bDeclared; /* True after sqlite3_declare_vtab() is called */ 29 }; 30 31 /* 32 ** Construct and install a Module object for a virtual table. When this 33 ** routine is called, it is guaranteed that all appropriate locks are held 34 ** and the module is not already part of the connection. 35 ** 36 ** If there already exists a module with zName, replace it with the new one. 37 ** If pModule==0, then delete the module zName if it exists. 38 */ 39 Module *sqlite3VtabCreateModule( 40 sqlite3 *db, /* Database in which module is registered */ 41 const char *zName, /* Name assigned to this module */ 42 const sqlite3_module *pModule, /* The definition of the module */ 43 void *pAux, /* Context pointer for xCreate/xConnect */ 44 void (*xDestroy)(void *) /* Module destructor function */ 45 ){ 46 Module *pMod; 47 Module *pDel; 48 char *zCopy; 49 if( pModule==0 ){ 50 zCopy = (char*)zName; 51 pMod = 0; 52 }else{ 53 int nName = sqlite3Strlen30(zName); 54 pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1); 55 if( pMod==0 ){ 56 sqlite3OomFault(db); 57 return 0; 58 } 59 zCopy = (char *)(&pMod[1]); 60 memcpy(zCopy, zName, nName+1); 61 pMod->zName = zCopy; 62 pMod->pModule = pModule; 63 pMod->pAux = pAux; 64 pMod->xDestroy = xDestroy; 65 pMod->pEpoTab = 0; 66 pMod->nRefModule = 1; 67 } 68 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod); 69 if( pDel ){ 70 if( pDel==pMod ){ 71 sqlite3OomFault(db); 72 sqlite3DbFree(db, pDel); 73 pMod = 0; 74 }else{ 75 sqlite3VtabEponymousTableClear(db, pDel); 76 sqlite3VtabModuleUnref(db, pDel); 77 } 78 } 79 return pMod; 80 } 81 82 /* 83 ** The actual function that does the work of creating a new module. 84 ** This function implements the sqlite3_create_module() and 85 ** sqlite3_create_module_v2() interfaces. 86 */ 87 static int createModule( 88 sqlite3 *db, /* Database in which module is registered */ 89 const char *zName, /* Name assigned to this module */ 90 const sqlite3_module *pModule, /* The definition of the module */ 91 void *pAux, /* Context pointer for xCreate/xConnect */ 92 void (*xDestroy)(void *) /* Module destructor function */ 93 ){ 94 int rc = SQLITE_OK; 95 96 sqlite3_mutex_enter(db->mutex); 97 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy); 98 rc = sqlite3ApiExit(db, rc); 99 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux); 100 sqlite3_mutex_leave(db->mutex); 101 return rc; 102 } 103 104 105 /* 106 ** External API function used to create a new virtual-table module. 107 */ 108 int sqlite3_create_module( 109 sqlite3 *db, /* Database in which module is registered */ 110 const char *zName, /* Name assigned to this module */ 111 const sqlite3_module *pModule, /* The definition of the module */ 112 void *pAux /* Context pointer for xCreate/xConnect */ 113 ){ 114 #ifdef SQLITE_ENABLE_API_ARMOR 115 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; 116 #endif 117 return createModule(db, zName, pModule, pAux, 0); 118 } 119 120 /* 121 ** External API function used to create a new virtual-table module. 122 */ 123 int sqlite3_create_module_v2( 124 sqlite3 *db, /* Database in which module is registered */ 125 const char *zName, /* Name assigned to this module */ 126 const sqlite3_module *pModule, /* The definition of the module */ 127 void *pAux, /* Context pointer for xCreate/xConnect */ 128 void (*xDestroy)(void *) /* Module destructor function */ 129 ){ 130 #ifdef SQLITE_ENABLE_API_ARMOR 131 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; 132 #endif 133 return createModule(db, zName, pModule, pAux, xDestroy); 134 } 135 136 /* 137 ** External API to drop all virtual-table modules, except those named 138 ** on the azNames list. 139 */ 140 int sqlite3_drop_modules(sqlite3 *db, const char** azNames){ 141 HashElem *pThis, *pNext; 142 #ifdef SQLITE_ENABLE_API_ARMOR 143 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 144 #endif 145 sqlite3_mutex_enter(db->mutex); 146 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){ 147 Module *pMod = (Module*)sqliteHashData(pThis); 148 pNext = sqliteHashNext(pThis); 149 if( azNames ){ 150 int ii; 151 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){} 152 if( azNames[ii]!=0 ) continue; 153 } 154 createModule(db, pMod->zName, 0, 0, 0); 155 } 156 sqlite3_mutex_leave(db->mutex); 157 return SQLITE_OK; 158 } 159 160 /* 161 ** Decrement the reference count on a Module object. Destroy the 162 ** module when the reference count reaches zero. 163 */ 164 void sqlite3VtabModuleUnref(sqlite3 *db, Module *pMod){ 165 assert( pMod->nRefModule>0 ); 166 pMod->nRefModule--; 167 if( pMod->nRefModule==0 ){ 168 if( pMod->xDestroy ){ 169 pMod->xDestroy(pMod->pAux); 170 } 171 assert( pMod->pEpoTab==0 ); 172 sqlite3DbFree(db, pMod); 173 } 174 } 175 176 /* 177 ** Lock the virtual table so that it cannot be disconnected. 178 ** Locks nest. Every lock should have a corresponding unlock. 179 ** If an unlock is omitted, resources leaks will occur. 180 ** 181 ** If a disconnect is attempted while a virtual table is locked, 182 ** the disconnect is deferred until all locks have been removed. 183 */ 184 void sqlite3VtabLock(VTable *pVTab){ 185 pVTab->nRef++; 186 } 187 188 189 /* 190 ** pTab is a pointer to a Table structure representing a virtual-table. 191 ** Return a pointer to the VTable object used by connection db to access 192 ** this virtual-table, if one has been created, or NULL otherwise. 193 */ 194 VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){ 195 VTable *pVtab; 196 assert( IsVirtual(pTab) ); 197 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext); 198 return pVtab; 199 } 200 201 /* 202 ** Decrement the ref-count on a virtual table object. When the ref-count 203 ** reaches zero, call the xDisconnect() method to delete the object. 204 */ 205 void sqlite3VtabUnlock(VTable *pVTab){ 206 sqlite3 *db = pVTab->db; 207 208 assert( db ); 209 assert( pVTab->nRef>0 ); 210 assert( db->eOpenState==SQLITE_STATE_OPEN 211 || db->eOpenState==SQLITE_STATE_ZOMBIE ); 212 213 pVTab->nRef--; 214 if( pVTab->nRef==0 ){ 215 sqlite3_vtab *p = pVTab->pVtab; 216 if( p ){ 217 p->pModule->xDisconnect(p); 218 } 219 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod); 220 sqlite3DbFree(db, pVTab); 221 } 222 } 223 224 /* 225 ** Table p is a virtual table. This function moves all elements in the 226 ** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated 227 ** database connections to be disconnected at the next opportunity. 228 ** Except, if argument db is not NULL, then the entry associated with 229 ** connection db is left in the p->u.vtab.p list. 230 */ 231 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){ 232 VTable *pRet = 0; 233 VTable *pVTable; 234 235 assert( IsVirtual(p) ); 236 pVTable = p->u.vtab.p; 237 p->u.vtab.p = 0; 238 239 /* Assert that the mutex (if any) associated with the BtShared database 240 ** that contains table p is held by the caller. See header comments 241 ** above function sqlite3VtabUnlockList() for an explanation of why 242 ** this makes it safe to access the sqlite3.pDisconnect list of any 243 ** database connection that may have an entry in the p->u.vtab.p list. 244 */ 245 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) ); 246 247 while( pVTable ){ 248 sqlite3 *db2 = pVTable->db; 249 VTable *pNext = pVTable->pNext; 250 assert( db2 ); 251 if( db2==db ){ 252 pRet = pVTable; 253 p->u.vtab.p = pRet; 254 pRet->pNext = 0; 255 }else{ 256 pVTable->pNext = db2->pDisconnect; 257 db2->pDisconnect = pVTable; 258 } 259 pVTable = pNext; 260 } 261 262 assert( !db || pRet ); 263 return pRet; 264 } 265 266 /* 267 ** Table *p is a virtual table. This function removes the VTable object 268 ** for table *p associated with database connection db from the linked 269 ** list in p->pVTab. It also decrements the VTable ref count. This is 270 ** used when closing database connection db to free all of its VTable 271 ** objects without disturbing the rest of the Schema object (which may 272 ** be being used by other shared-cache connections). 273 */ 274 void sqlite3VtabDisconnect(sqlite3 *db, Table *p){ 275 VTable **ppVTab; 276 277 assert( IsVirtual(p) ); 278 assert( sqlite3BtreeHoldsAllMutexes(db) ); 279 assert( sqlite3_mutex_held(db->mutex) ); 280 281 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){ 282 if( (*ppVTab)->db==db ){ 283 VTable *pVTab = *ppVTab; 284 *ppVTab = pVTab->pNext; 285 sqlite3VtabUnlock(pVTab); 286 break; 287 } 288 } 289 } 290 291 292 /* 293 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list. 294 ** 295 ** This function may only be called when the mutexes associated with all 296 ** shared b-tree databases opened using connection db are held by the 297 ** caller. This is done to protect the sqlite3.pDisconnect list. The 298 ** sqlite3.pDisconnect list is accessed only as follows: 299 ** 300 ** 1) By this function. In this case, all BtShared mutexes and the mutex 301 ** associated with the database handle itself must be held. 302 ** 303 ** 2) By function vtabDisconnectAll(), when it adds a VTable entry to 304 ** the sqlite3.pDisconnect list. In this case either the BtShared mutex 305 ** associated with the database the virtual table is stored in is held 306 ** or, if the virtual table is stored in a non-sharable database, then 307 ** the database handle mutex is held. 308 ** 309 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously 310 ** by multiple threads. It is thread-safe. 311 */ 312 void sqlite3VtabUnlockList(sqlite3 *db){ 313 VTable *p = db->pDisconnect; 314 315 assert( sqlite3BtreeHoldsAllMutexes(db) ); 316 assert( sqlite3_mutex_held(db->mutex) ); 317 318 if( p ){ 319 db->pDisconnect = 0; 320 do { 321 VTable *pNext = p->pNext; 322 sqlite3VtabUnlock(p); 323 p = pNext; 324 }while( p ); 325 } 326 } 327 328 /* 329 ** Clear any and all virtual-table information from the Table record. 330 ** This routine is called, for example, just before deleting the Table 331 ** record. 332 ** 333 ** Since it is a virtual-table, the Table structure contains a pointer 334 ** to the head of a linked list of VTable structures. Each VTable 335 ** structure is associated with a single sqlite3* user of the schema. 336 ** The reference count of the VTable structure associated with database 337 ** connection db is decremented immediately (which may lead to the 338 ** structure being xDisconnected and free). Any other VTable structures 339 ** in the list are moved to the sqlite3.pDisconnect list of the associated 340 ** database connection. 341 */ 342 void sqlite3VtabClear(sqlite3 *db, Table *p){ 343 assert( IsVirtual(p) ); 344 assert( db!=0 ); 345 if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); 346 if( p->u.vtab.azArg ){ 347 int i; 348 for(i=0; iu.vtab.nArg; i++){ 349 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]); 350 } 351 sqlite3DbFree(db, p->u.vtab.azArg); 352 } 353 } 354 355 /* 356 ** Add a new module argument to pTable->u.vtab.azArg[]. 357 ** The string is not copied - the pointer is stored. The 358 ** string will be freed automatically when the table is 359 ** deleted. 360 */ 361 static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){ 362 sqlite3_int64 nBytes; 363 char **azModuleArg; 364 sqlite3 *db = pParse->db; 365 366 assert( IsVirtual(pTable) ); 367 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg); 368 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){ 369 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName); 370 } 371 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes); 372 if( azModuleArg==0 ){ 373 sqlite3DbFree(db, zArg); 374 }else{ 375 int i = pTable->u.vtab.nArg++; 376 azModuleArg[i] = zArg; 377 azModuleArg[i+1] = 0; 378 pTable->u.vtab.azArg = azModuleArg; 379 } 380 } 381 382 /* 383 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE 384 ** statement. The module name has been parsed, but the optional list 385 ** of parameters that follow the module name are still pending. 386 */ 387 void sqlite3VtabBeginParse( 388 Parse *pParse, /* Parsing context */ 389 Token *pName1, /* Name of new table, or database name */ 390 Token *pName2, /* Name of new table or NULL */ 391 Token *pModuleName, /* Name of the module for the virtual table */ 392 int ifNotExists /* No error if the table already exists */ 393 ){ 394 Table *pTable; /* The new virtual table */ 395 sqlite3 *db; /* Database connection */ 396 397 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists); 398 pTable = pParse->pNewTable; 399 if( pTable==0 ) return; 400 assert( 0==pTable->pIndex ); 401 pTable->eTabType = TABTYP_VTAB; 402 403 db = pParse->db; 404 405 assert( pTable->u.vtab.nArg==0 ); 406 addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName)); 407 addModuleArgument(pParse, pTable, 0); 408 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName)); 409 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0) 410 || (pParse->sNameToken.z==pName1->z && pName2->z==0) 411 ); 412 pParse->sNameToken.n = (int)( 413 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z 414 ); 415 416 #ifndef SQLITE_OMIT_AUTHORIZATION 417 /* Creating a virtual table invokes the authorization callback twice. 418 ** The first invocation, to obtain permission to INSERT a row into the 419 ** sqlite_schema table, has already been made by sqlite3StartTable(). 420 ** The second call, to obtain permission to create the table, is made now. 421 */ 422 if( pTable->u.vtab.azArg ){ 423 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema); 424 assert( iDb>=0 ); /* The database the table is being created in */ 425 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 426 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName); 427 } 428 #endif 429 } 430 431 /* 432 ** This routine takes the module argument that has been accumulating 433 ** in pParse->zArg[] and appends it to the list of arguments on the 434 ** virtual table currently under construction in pParse->pTable. 435 */ 436 static void addArgumentToVtab(Parse *pParse){ 437 if( pParse->sArg.z && pParse->pNewTable ){ 438 const char *z = (const char*)pParse->sArg.z; 439 int n = pParse->sArg.n; 440 sqlite3 *db = pParse->db; 441 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); 442 } 443 } 444 445 /* 446 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement 447 ** has been completely parsed. 448 */ 449 void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ 450 Table *pTab = pParse->pNewTable; /* The table being constructed */ 451 sqlite3 *db = pParse->db; /* The database connection */ 452 453 if( pTab==0 ) return; 454 assert( IsVirtual(pTab) ); 455 addArgumentToVtab(pParse); 456 pParse->sArg.z = 0; 457 if( pTab->u.vtab.nArg<1 ) return; 458 459 /* If the CREATE VIRTUAL TABLE statement is being entered for the 460 ** first time (in other words if the virtual table is actually being 461 ** created now instead of just being read out of sqlite_schema) then 462 ** do additional initialization work and store the statement text 463 ** in the sqlite_schema table. 464 */ 465 if( !db->init.busy ){ 466 char *zStmt; 467 char *zWhere; 468 int iDb; 469 int iReg; 470 Vdbe *v; 471 472 sqlite3MayAbort(pParse); 473 474 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ 475 if( pEnd ){ 476 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n; 477 } 478 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken); 479 480 /* A slot for the record has already been allocated in the 481 ** schema table. We just need to update that slot with all 482 ** the information we've collected. 483 ** 484 ** The VM register number pParse->u1.cr.regRowid holds the rowid of an 485 ** entry in the sqlite_schema table that was created for this vtab 486 ** by sqlite3StartTable(). 487 */ 488 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 489 assert( pParse->isCreate ); 490 sqlite3NestedParse(pParse, 491 "UPDATE %Q." LEGACY_SCHEMA_TABLE " " 492 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q " 493 "WHERE rowid=#%d", 494 db->aDb[iDb].zDbSName, 495 pTab->zName, 496 pTab->zName, 497 zStmt, 498 pParse->u1.cr.regRowid 499 ); 500 v = sqlite3GetVdbe(pParse); 501 sqlite3ChangeCookie(pParse, iDb); 502 503 sqlite3VdbeAddOp0(v, OP_Expire); 504 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt); 505 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere, 0); 506 sqlite3DbFree(db, zStmt); 507 508 iReg = ++pParse->nMem; 509 sqlite3VdbeLoadString(v, iReg, pTab->zName); 510 sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg); 511 }else{ 512 /* If we are rereading the sqlite_schema table create the in-memory 513 ** record of the table. */ 514 Table *pOld; 515 Schema *pSchema = pTab->pSchema; 516 const char *zName = pTab->zName; 517 assert( zName!=0 ); 518 sqlite3MarkAllShadowTablesOf(db, pTab); 519 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab); 520 if( pOld ){ 521 sqlite3OomFault(db); 522 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ 523 return; 524 } 525 pParse->pNewTable = 0; 526 } 527 } 528 529 /* 530 ** The parser calls this routine when it sees the first token 531 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement. 532 */ 533 void sqlite3VtabArgInit(Parse *pParse){ 534 addArgumentToVtab(pParse); 535 pParse->sArg.z = 0; 536 pParse->sArg.n = 0; 537 } 538 539 /* 540 ** The parser calls this routine for each token after the first token 541 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement. 542 */ 543 void sqlite3VtabArgExtend(Parse *pParse, Token *p){ 544 Token *pArg = &pParse->sArg; 545 if( pArg->z==0 ){ 546 pArg->z = p->z; 547 pArg->n = p->n; 548 }else{ 549 assert(pArg->z <= p->z); 550 pArg->n = (int)(&p->z[p->n] - pArg->z); 551 } 552 } 553 554 /* 555 ** Invoke a virtual table constructor (either xCreate or xConnect). The 556 ** pointer to the function to invoke is passed as the fourth parameter 557 ** to this procedure. 558 */ 559 static int vtabCallConstructor( 560 sqlite3 *db, 561 Table *pTab, 562 Module *pMod, 563 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), 564 char **pzErr 565 ){ 566 VtabCtx sCtx; 567 VTable *pVTable; 568 int rc; 569 const char *const*azArg; 570 int nArg = pTab->u.vtab.nArg; 571 char *zErr = 0; 572 char *zModuleName; 573 int iDb; 574 VtabCtx *pCtx; 575 576 assert( IsVirtual(pTab) ); 577 azArg = (const char *const*)pTab->u.vtab.azArg; 578 579 /* Check that the virtual-table is not already being initialized */ 580 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){ 581 if( pCtx->pTab==pTab ){ 582 *pzErr = sqlite3MPrintf(db, 583 "vtable constructor called recursively: %s", pTab->zName 584 ); 585 return SQLITE_LOCKED; 586 } 587 } 588 589 zModuleName = sqlite3DbStrDup(db, pTab->zName); 590 if( !zModuleName ){ 591 return SQLITE_NOMEM_BKPT; 592 } 593 594 pVTable = sqlite3MallocZero(sizeof(VTable)); 595 if( !pVTable ){ 596 sqlite3OomFault(db); 597 sqlite3DbFree(db, zModuleName); 598 return SQLITE_NOMEM_BKPT; 599 } 600 pVTable->db = db; 601 pVTable->pMod = pMod; 602 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal; 603 604 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 605 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName; 606 607 /* Invoke the virtual table constructor */ 608 assert( &db->pVtabCtx ); 609 assert( xConstruct ); 610 sCtx.pTab = pTab; 611 sCtx.pVTable = pVTable; 612 sCtx.pPrior = db->pVtabCtx; 613 sCtx.bDeclared = 0; 614 db->pVtabCtx = &sCtx; 615 pTab->nTabRef++; 616 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); 617 assert( pTab!=0 ); 618 assert( pTab->nTabRef>1 || rc!=SQLITE_OK ); 619 sqlite3DeleteTable(db, pTab); 620 db->pVtabCtx = sCtx.pPrior; 621 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); 622 assert( sCtx.pTab==pTab ); 623 624 if( SQLITE_OK!=rc ){ 625 if( zErr==0 ){ 626 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); 627 }else { 628 *pzErr = sqlite3MPrintf(db, "%s", zErr); 629 sqlite3_free(zErr); 630 } 631 sqlite3DbFree(db, pVTable); 632 }else if( ALWAYS(pVTable->pVtab) ){ 633 /* Justification of ALWAYS(): A correct vtab constructor must allocate 634 ** the sqlite3_vtab object if successful. */ 635 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0])); 636 pVTable->pVtab->pModule = pMod->pModule; 637 pMod->nRefModule++; 638 pVTable->nRef = 1; 639 if( sCtx.bDeclared==0 ){ 640 const char *zFormat = "vtable constructor did not declare schema: %s"; 641 *pzErr = sqlite3MPrintf(db, zFormat, zModuleName); 642 sqlite3VtabUnlock(pVTable); 643 rc = SQLITE_ERROR; 644 }else{ 645 int iCol; 646 u16 oooHidden = 0; 647 /* If everything went according to plan, link the new VTable structure 648 ** into the linked list headed by pTab->u.vtab.p. Then loop through the 649 ** columns of the table to see if any of them contain the token "hidden". 650 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from 651 ** the type string. */ 652 pVTable->pNext = pTab->u.vtab.p; 653 pTab->u.vtab.p = pVTable; 654 655 for(iCol=0; iColnCol; iCol++){ 656 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], ""); 657 int nType; 658 int i = 0; 659 nType = sqlite3Strlen30(zType); 660 for(i=0; i0 ){ 675 assert(zType[i-1]==' '); 676 zType[i-1] = '\0'; 677 } 678 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN; 679 pTab->tabFlags |= TF_HasHidden; 680 oooHidden = TF_OOOHidden; 681 }else{ 682 pTab->tabFlags |= oooHidden; 683 } 684 } 685 } 686 } 687 688 sqlite3DbFree(db, zModuleName); 689 return rc; 690 } 691 692 /* 693 ** This function is invoked by the parser to call the xConnect() method 694 ** of the virtual table pTab. If an error occurs, an error code is returned 695 ** and an error left in pParse. 696 ** 697 ** This call is a no-op if table pTab is not a virtual table. 698 */ 699 int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ 700 sqlite3 *db = pParse->db; 701 const char *zMod; 702 Module *pMod; 703 int rc; 704 705 assert( pTab ); 706 assert( IsVirtual(pTab) ); 707 if( sqlite3GetVTable(db, pTab) ){ 708 return SQLITE_OK; 709 } 710 711 /* Locate the required virtual table module */ 712 zMod = pTab->u.vtab.azArg[0]; 713 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod); 714 715 if( !pMod ){ 716 const char *zModule = pTab->u.vtab.azArg[0]; 717 sqlite3ErrorMsg(pParse, "no such module: %s", zModule); 718 rc = SQLITE_ERROR; 719 }else{ 720 char *zErr = 0; 721 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr); 722 if( rc!=SQLITE_OK ){ 723 sqlite3ErrorMsg(pParse, "%s", zErr); 724 pParse->rc = rc; 725 }else{ 726 sqlite3MarkAllShadowTablesOf(db, pTab); 727 } 728 sqlite3DbFree(db, zErr); 729 } 730 731 return rc; 732 } 733 /* 734 ** Grow the db->aVTrans[] array so that there is room for at least one 735 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise. 736 */ 737 static int growVTrans(sqlite3 *db){ 738 const int ARRAY_INCR = 5; 739 740 /* Grow the sqlite3.aVTrans array if required */ 741 if( (db->nVTrans%ARRAY_INCR)==0 ){ 742 VTable **aVTrans; 743 sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)* 744 ((sqlite3_int64)db->nVTrans + ARRAY_INCR); 745 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes); 746 if( !aVTrans ){ 747 return SQLITE_NOMEM_BKPT; 748 } 749 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); 750 db->aVTrans = aVTrans; 751 } 752 753 return SQLITE_OK; 754 } 755 756 /* 757 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should 758 ** have already been reserved using growVTrans(). 759 */ 760 static void addToVTrans(sqlite3 *db, VTable *pVTab){ 761 /* Add pVtab to the end of sqlite3.aVTrans */ 762 db->aVTrans[db->nVTrans++] = pVTab; 763 sqlite3VtabLock(pVTab); 764 } 765 766 /* 767 ** This function is invoked by the vdbe to call the xCreate method 768 ** of the virtual table named zTab in database iDb. 769 ** 770 ** If an error occurs, *pzErr is set to point to an English language 771 ** description of the error and an SQLITE_XXX error code is returned. 772 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr. 773 */ 774 int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ 775 int rc = SQLITE_OK; 776 Table *pTab; 777 Module *pMod; 778 const char *zMod; 779 780 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName); 781 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p ); 782 783 /* Locate the required virtual table module */ 784 zMod = pTab->u.vtab.azArg[0]; 785 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod); 786 787 /* If the module has been registered and includes a Create method, 788 ** invoke it now. If the module has not been registered, return an 789 ** error. Otherwise, do nothing. 790 */ 791 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){ 792 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod); 793 rc = SQLITE_ERROR; 794 }else{ 795 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); 796 } 797 798 /* Justification of ALWAYS(): The xConstructor method is required to 799 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */ 800 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){ 801 rc = growVTrans(db); 802 if( rc==SQLITE_OK ){ 803 VTable *pVTab = sqlite3GetVTable(db, pTab); 804 const sqlite3_module *pModule = pMod->pModule; 805 int iSvpt = db->nStatement + db->nSavepoint; 806 addToVTrans(db, pVTab); 807 /* As in sqlite3VtabBegin(): open the current savepoint on the new 808 ** table, so that a ROLLBACK TO that undoes the CREATE also invokes 809 ** its xRollbackTo method. */ 810 if( iSvpt && pModule->iVersion>=2 && pModule->xSavepoint ){ 811 pVTab->iSavepoint = iSvpt; 812 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1); 813 } 814 } 815 } 816 817 return rc; 818 } 819 820 /* 821 ** This function is used to set the schema of a virtual table. It is only 822 ** valid to call this function from within the xCreate() or xConnect() of a 823 ** virtual table module. 824 */ 825 int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ 826 VtabCtx *pCtx; 827 int rc = SQLITE_OK; 828 Table *pTab; 829 Parse sParse; 830 int initBusy; 831 int i; 832 const unsigned char *z; 833 static const u8 aKeyword[] = { TK_CREATE, TK_TABLE, 0 }; 834 835 #ifdef SQLITE_ENABLE_API_ARMOR 836 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){ 837 return SQLITE_MISUSE_BKPT; 838 } 839 #endif 840 841 /* Verify that the first two keywords in the CREATE TABLE statement 842 ** really are "CREATE" and "TABLE". If this is not the case, then 843 ** sqlite3_declare_vtab() is being misused. 844 */ 845 z = (const unsigned char*)zCreateTable; 846 for(i=0; aKeyword[i]; i++){ 847 int tokenType = 0; 848 do{ 849 z += sqlite3GetToken(z, &tokenType); 850 }while( tokenType==TK_SPACE || tokenType==TK_COMMENT ); 851 if( tokenType!=aKeyword[i] ){ 852 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "syntax error"); 853 return SQLITE_ERROR; 854 } 855 } 856 857 sqlite3_mutex_enter(db->mutex); 858 pCtx = db->pVtabCtx; 859 if( !pCtx || pCtx->bDeclared ){ 860 sqlite3Error(db, SQLITE_MISUSE_BKPT); 861 sqlite3_mutex_leave(db->mutex); 862 return SQLITE_MISUSE_BKPT; 863 } 864 865 pTab = pCtx->pTab; 866 assert( IsVirtual(pTab) ); 867 868 sqlite3ParseObjectInit(&sParse, db); 869 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB; 870 sParse.disableTriggers = 1; 871 /* We should never be able to reach this point while loading the 872 ** schema. Nevertheless, defend against that (turn off db->init.busy) 873 ** in case a bug arises. */ 874 assert( db->init.busy==0 ); 875 initBusy = db->init.busy; 876 db->init.busy = 0; 877 sParse.nQueryLoop = 1; 878 if( SQLITE_OK==sqlite3RunParser(&sParse, zCreateTable) ){ 879 assert( sParse.pNewTable!=0 ); 880 assert( !db->mallocFailed ); 881 assert( IsOrdinaryTable(sParse.pNewTable) ); 882 assert( sParse.zErrMsg==0 ); 883 if( !pTab->aCol ){ 884 Table *pNew = sParse.pNewTable; 885 Index *pIdx; 886 pTab->aCol = pNew->aCol; 887 assert( IsOrdinaryTable(pNew) ); 888 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList); 889 pTab->nNVCol = pTab->nCol = pNew->nCol; 890 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid); 891 pNew->nCol = 0; 892 pNew->aCol = 0; 893 assert( pTab->pIndex==0 ); 894 assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 ); 895 if( !HasRowid(pNew) 896 && pCtx->pVTable->pMod->pModule->xUpdate!=0 897 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1 898 ){ 899 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0) 900 ** or else must have a single-column PRIMARY KEY */ 901 rc = SQLITE_ERROR; 902 } 903 pIdx = pNew->pIndex; 904 if( pIdx ){ 905 assert( pIdx->pNext==0 ); 906 pTab->pIndex = pIdx; 907 pNew->pIndex = 0; 908 pIdx->pTable = pTab; 909 } 910 } 911 pCtx->bDeclared = 1; 912 }else{ 913 sqlite3ErrorWithMsg(db, SQLITE_ERROR, 914 (sParse.zErrMsg ? "%s" : 0), sParse.zErrMsg); 915 sqlite3DbFree(db, sParse.zErrMsg); 916 rc = SQLITE_ERROR; 917 } 918 sParse.eParseMode = PARSE_MODE_NORMAL; 919 920 if( sParse.pVdbe ){ 921 sqlite3VdbeFinalize(sParse.pVdbe); 922 } 923 sqlite3DeleteTable(db, sParse.pNewTable); 924 sqlite3ParseObjectReset(&sParse); 925 db->init.busy = initBusy; 926 927 assert( (rc&0xff)==rc ); 928 rc = sqlite3ApiExit(db, rc); 929 sqlite3_mutex_leave(db->mutex); 930 return rc; 931 } 932 933 /* 934 ** This function is invoked by the vdbe to call the xDestroy method 935 ** of the virtual table named zTab in database iDb. This occurs 936 ** when a DROP TABLE is mentioned. 937 ** 938 ** This call is a no-op if zTab is not a virtual table. 939 */ 940 int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){ 941 int rc = SQLITE_OK; 942 Table *pTab; 943 944 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName); 945 if( ALWAYS(pTab!=0) 946 && ALWAYS(IsVirtual(pTab)) 947 && ALWAYS(pTab->u.vtab.p!=0) 948 ){ 949 VTable *p; 950 int (*xDestroy)(sqlite3_vtab *); 951 for(p=pTab->u.vtab.p; p; p=p->pNext){ 952 assert( p->pVtab ); 953 if( p->pVtab->nRef>0 ){ 954 return SQLITE_LOCKED; 955 } 956 } 957 p = vtabDisconnectAll(db, pTab); 958 xDestroy = p->pMod->pModule->xDestroy; 959 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect; 960 assert( xDestroy!=0 ); 961 pTab->nTabRef++; 962 rc = xDestroy(p->pVtab); 963 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ 964 if( rc==SQLITE_OK ){ 965 assert( pTab->u.vtab.p==p && p->pNext==0 ); 966 p->pVtab = 0; 967 pTab->u.vtab.p = 0; 968 sqlite3VtabUnlock(p); 969 } 970 sqlite3DeleteTable(db, pTab); 971 } 972 973 return rc; 974 } 975 976 /* 977 ** This function invokes either the xRollback or xCommit method 978 ** of each of the virtual tables in the sqlite3.aVTrans array. The method 979 ** called is identified by the second argument, "offset", which is 980 ** the offset of the method to call in the sqlite3_module structure. 981 ** 982 ** The array is cleared after invoking the callbacks. 983 */ 984 static void callFinaliser(sqlite3 *db, int offset){ 985 int i; 986 if( db->aVTrans ){ 987 VTable **aVTrans = db->aVTrans; 988 db->aVTrans = 0; 989 for(i=0; inVTrans; i++){ 990 VTable *pVTab = aVTrans[i]; 991 sqlite3_vtab *p = pVTab->pVtab; 992 if( p ){ 993 int (*x)(sqlite3_vtab *); 994 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset); 995 if( x ) x(p); 996 } 997 pVTab->iSavepoint = 0; 998 sqlite3VtabUnlock(pVTab); 999 } 1000 sqlite3DbFree(db, aVTrans); 1001 db->nVTrans = 0; 1002 } 1003 } 1004 1005 /* 1006 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans 1007 ** array. Return the error code for the first error that occurs, or 1008 ** SQLITE_OK if all xSync operations are successful. 1009 ** 1010 ** If an error message is available, leave it in p->zErrMsg. 1011 */ 1012 int sqlite3VtabSync(sqlite3 *db, Vdbe *p){ 1013 int i; 1014 int rc = SQLITE_OK; 1015 VTable **aVTrans = db->aVTrans; 1016 1017 db->aVTrans = 0; 1018 for(i=0; rc==SQLITE_OK && inVTrans; i++){ 1019 int (*x)(sqlite3_vtab *); 1020 sqlite3_vtab *pVtab = aVTrans[i]->pVtab; 1021 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){ 1022 rc = x(pVtab); 1023 sqlite3VtabImportErrmsg(p, pVtab); 1024 } 1025 } 1026 db->aVTrans = aVTrans; 1027 return rc; 1028 } 1029 1030 /* 1031 ** Invoke the xRollback method of all virtual tables in the 1032 ** sqlite3.aVTrans array. Then clear the array itself. 1033 */ 1034 int sqlite3VtabRollback(sqlite3 *db){ 1035 callFinaliser(db, offsetof(sqlite3_module,xRollback)); 1036 return SQLITE_OK; 1037 } 1038 1039 /* 1040 ** Invoke the xCommit method of all virtual tables in the 1041 ** sqlite3.aVTrans array. Then clear the array itself. 1042 */ 1043 int sqlite3VtabCommit(sqlite3 *db){ 1044 callFinaliser(db, offsetof(sqlite3_module,xCommit)); 1045 return SQLITE_OK; 1046 } 1047 1048 /* 1049 ** If the virtual table pVtab supports the transaction interface 1050 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is 1051 ** not currently open, invoke the xBegin method now. 1052 ** 1053 ** If the xBegin call is successful, place the sqlite3_vtab pointer 1054 ** in the sqlite3.aVTrans array. 1055 */ 1056 int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){ 1057 int rc = SQLITE_OK; 1058 const sqlite3_module *pModule; 1059 1060 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater 1061 ** than zero, then this function is being called from within a 1062 ** virtual module xSync() callback. It is illegal to write to 1063 ** virtual module tables in this case, so return SQLITE_LOCKED. 1064 */ 1065 if( sqlite3VtabInSync(db) ){ 1066 return SQLITE_LOCKED; 1067 } 1068 if( !pVTab ){ 1069 return SQLITE_OK; 1070 } 1071 pModule = pVTab->pVtab->pModule; 1072 1073 if( pModule->xBegin ){ 1074 int i; 1075 1076 /* If pVtab is already in the aVTrans array, return early */ 1077 for(i=0; inVTrans; i++){ 1078 if( db->aVTrans[i]==pVTab ){ 1079 return SQLITE_OK; 1080 } 1081 } 1082 1083 /* Invoke the xBegin method. If successful, add the vtab to the 1084 ** sqlite3.aVTrans[] array. */ 1085 rc = growVTrans(db); 1086 if( rc==SQLITE_OK ){ 1087 rc = pModule->xBegin(pVTab->pVtab); 1088 if( rc==SQLITE_OK ){ 1089 int iSvpt = db->nStatement + db->nSavepoint; 1090 addToVTrans(db, pVTab); 1091 if( iSvpt && pModule->xSavepoint ){ 1092 pVTab->iSavepoint = iSvpt; 1093 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1); 1094 } 1095 } 1096 } 1097 } 1098 return rc; 1099 } 1100 1101 /* 1102 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all 1103 ** virtual tables that currently have an open transaction. Pass iSavepoint 1104 ** as the second argument to the virtual table method invoked. 1105 ** 1106 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is 1107 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is 1108 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with 1109 ** an open transaction is invoked. 1110 ** 1111 ** If any virtual table method returns an error code other than SQLITE_OK, 1112 ** processing is abandoned and the error returned to the caller of this 1113 ** function immediately. If all calls to virtual table methods are successful, 1114 ** SQLITE_OK is returned. 1115 */ 1116 int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){ 1117 int rc = SQLITE_OK; 1118 1119 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN ); 1120 assert( iSavepoint>=-1 ); 1121 if( db->aVTrans ){ 1122 int i; 1123 for(i=0; rc==SQLITE_OK && inVTrans; i++){ 1124 VTable *pVTab = db->aVTrans[i]; 1125 const sqlite3_module *pMod = pVTab->pMod->pModule; 1126 if( pVTab->pVtab && pMod->iVersion>=2 ){ 1127 int (*xMethod)(sqlite3_vtab *, int); 1128 sqlite3VtabLock(pVTab); 1129 switch( op ){ 1130 case SAVEPOINT_BEGIN: 1131 xMethod = pMod->xSavepoint; 1132 pVTab->iSavepoint = iSavepoint+1; 1133 break; 1134 case SAVEPOINT_ROLLBACK: 1135 xMethod = pMod->xRollbackTo; 1136 break; 1137 default: 1138 xMethod = pMod->xRelease; 1139 break; 1140 } 1141 if( xMethod && pVTab->iSavepoint>iSavepoint ){ 1142 u64 savedFlags = (db->flags & SQLITE_Defensive); 1143 db->flags &= ~(u64)SQLITE_Defensive; 1144 rc = xMethod(pVTab->pVtab, iSavepoint); 1145 db->flags |= savedFlags; 1146 } 1147 sqlite3VtabUnlock(pVTab); 1148 } 1149 } 1150 } 1151 return rc; 1152 } 1153 1154 /* 1155 ** The first parameter (pDef) is a function implementation. The 1156 ** second parameter (pExpr) is the first argument to this function. 1157 ** If pExpr is a column in a virtual table, then let the virtual 1158 ** table implementation have an opportunity to overload the function. 1159 ** 1160 ** This routine is used to allow virtual table implementations to 1161 ** overload MATCH, LIKE, GLOB, and REGEXP operators. 1162 ** 1163 ** Return either the pDef argument (indicating no change) or a 1164 ** new FuncDef structure that is marked as ephemeral using the 1165 ** SQLITE_FUNC_EPHEM flag. 1166 */ 1167 FuncDef *sqlite3VtabOverloadFunction( 1168 sqlite3 *db, /* Database connection for reporting malloc problems */ 1169 FuncDef *pDef, /* Function to possibly overload */ 1170 int nArg, /* Number of arguments to the function */ 1171 Expr *pExpr /* First argument to the function */ 1172 ){ 1173 Table *pTab; 1174 sqlite3_vtab *pVtab; 1175 sqlite3_module *pMod; 1176 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0; 1177 void *pArg = 0; 1178 FuncDef *pNew; 1179 int rc = 0; 1180 1181 /* Check to see the left operand is a column in a virtual table */ 1182 if( NEVER(pExpr==0) ) return pDef; 1183 if( pExpr->op!=TK_COLUMN ) return pDef; 1184 assert( ExprUseYTab(pExpr) ); 1185 pTab = pExpr->y.pTab; 1186 if( NEVER(pTab==0) ) return pDef; 1187 if( !IsVirtual(pTab) ) return pDef; 1188 pVtab = sqlite3GetVTable(db, pTab)->pVtab; 1189 assert( pVtab!=0 ); 1190 assert( pVtab->pModule!=0 ); 1191 pMod = (sqlite3_module *)pVtab->pModule; 1192 if( pMod->xFindFunction==0 ) return pDef; 1193 1194 /* Call the xFindFunction method on the virtual table implementation 1195 ** to see if the implementation wants to overload this function. 1196 ** 1197 ** Though undocumented, we have historically always invoked xFindFunction 1198 ** with an all lower-case function name. Continue in this tradition to 1199 ** avoid any chance of an incompatibility. 1200 */ 1201 #ifdef SQLITE_DEBUG 1202 { 1203 int i; 1204 for(i=0; pDef->zName[i]; i++){ 1205 unsigned char x = (unsigned char)pDef->zName[i]; 1206 assert( x==sqlite3UpperToLower[x] ); 1207 } 1208 } 1209 #endif 1210 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg); 1211 if( rc==0 ){ 1212 return pDef; 1213 } 1214 1215 /* Create a new ephemeral function definition for the overloaded 1216 ** function */ 1217 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) 1218 + sqlite3Strlen30(pDef->zName) + 1); 1219 if( pNew==0 ){ 1220 return pDef; 1221 } 1222 *pNew = *pDef; 1223 pNew->zName = (const char*)&pNew[1]; 1224 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1); 1225 pNew->xSFunc = xSFunc; 1226 pNew->pUserData = pArg; 1227 pNew->funcFlags |= SQLITE_FUNC_EPHEM; 1228 return pNew; 1229 } 1230 1231 /* 1232 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[] 1233 ** array so that an OP_VBegin will get generated for it. Add pTab to the 1234 ** array if it is missing. If pTab is already in the array, this routine 1235 ** is a no-op. 1236 */ 1237 void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ 1238 Parse *pToplevel = sqlite3ParseToplevel(pParse); 1239 int i, n; 1240 Table **apVtabLock; 1241 1242 assert( IsVirtual(pTab) ); 1243 for(i=0; inVtabLock; i++){ 1244 if( pTab==pToplevel->apVtabLock[i] ) return; 1245 } 1246 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); 1247 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n); 1248 if( apVtabLock ){ 1249 pToplevel->apVtabLock = apVtabLock; 1250 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; 1251 }else{ 1252 sqlite3OomFault(pToplevel->db); 1253 } 1254 } 1255 1256 /* 1257 ** Check to see if virtual table module pMod can be have an eponymous 1258 ** virtual table instance. If it can, create one if one does not already 1259 ** exist. Return non-zero if either the eponymous virtual table instance 1260 ** exists when this routine returns or if an attempt to create it failed 1261 ** and an error message was left in pParse. 1262 ** 1263 ** An eponymous virtual table instance is one that is named after its 1264 ** module, and more importantly, does not require a CREATE VIRTUAL TABLE 1265 ** statement in order to come into existence. Eponymous virtual table 1266 ** instances always exist. They cannot be DROP-ed. 1267 ** 1268 ** Any virtual table module for which xConnect and xCreate are the same 1269 ** method can have an eponymous virtual table instance. 1270 */ 1271 int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){ 1272 const sqlite3_module *pModule = pMod->pModule; 1273 Table *pTab; 1274 char *zErr = 0; 1275 int rc; 1276 sqlite3 *db = pParse->db; 1277 if( pMod->pEpoTab ) return 1; 1278 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0; 1279 pTab = sqlite3DbMallocZero(db, sizeof(Table)); 1280 if( pTab==0 ) return 0; 1281 pTab->zName = sqlite3DbStrDup(db, pMod->zName); 1282 if( pTab->zName==0 ){ 1283 sqlite3DbFree(db, pTab); 1284 return 0; 1285 } 1286 pMod->pEpoTab = pTab; 1287 pTab->nTabRef = 1; 1288 pTab->eTabType = TABTYP_VTAB; 1289 pTab->pSchema = db->aDb[0].pSchema; 1290 assert( pTab->u.vtab.nArg==0 ); 1291 pTab->iPKey = -1; 1292 pTab->tabFlags |= TF_Eponymous; 1293 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName)); 1294 addModuleArgument(pParse, pTab, 0); 1295 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName)); 1296 db->nSchemaLock++; 1297 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr); 1298 db->nSchemaLock--; 1299 if( rc ){ 1300 sqlite3ErrorMsg(pParse, "%s", zErr); 1301 pParse->rc = rc; 1302 sqlite3DbFree(db, zErr); 1303 sqlite3VtabEponymousTableClear(db, pMod); 1304 } 1305 return 1; 1306 } 1307 1308 /* 1309 ** Erase the eponymous virtual table instance associated with 1310 ** virtual table module pMod, if it exists. 1311 */ 1312 void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){ 1313 Table *pTab = pMod->pEpoTab; 1314 if( pTab!=0 ){ 1315 /* Mark the table as Ephemeral prior to deleting it, so that the 1316 ** sqlite3DeleteTable() routine will know that it is not stored in 1317 ** the schema. */ 1318 pTab->tabFlags |= TF_Ephemeral; 1319 sqlite3DeleteTable(db, pTab); 1320 pMod->pEpoTab = 0; 1321 } 1322 } 1323 1324 /* 1325 ** Erase all eponymous virtual table instances associated with database 1326 ** handle db. 1327 */ 1328 void sqlite3VtabEponymousTableClearAll(sqlite3 *db){ 1329 HashElem *pElem; 1330 for(pElem=sqliteHashFirst(&db->aModule); pElem; pElem=sqliteHashNext(pElem)){ 1331 sqlite3VtabEponymousTableClear(db, (Module*)sqliteHashData(pElem)); 1332 } 1333 } 1334 1335 /* 1336 ** Return the ON CONFLICT resolution mode in effect for the virtual 1337 ** table update operation currently in progress. 1338 ** 1339 ** The results of this routine are undefined unless it is called from 1340 ** within an xUpdate method. 1341 */ 1342 int sqlite3_vtab_on_conflict(sqlite3 *db){ 1343 static const unsigned char aMap[] = { 1344 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE 1345 }; 1346 #ifdef SQLITE_ENABLE_API_ARMOR 1347 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 1348 #endif 1349 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 ); 1350 assert( OE_Ignore==4 && OE_Replace==5 ); 1351 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 ); 1352 return (int)aMap[db->vtabOnConflict-1]; 1353 } 1354 1355 /* 1356 ** Call from within the xCreate() or xConnect() methods to provide 1357 ** the SQLite core with additional information about the behavior 1358 ** of the virtual table being implemented. 1359 */ 1360 int sqlite3_vtab_config(sqlite3 *db, int op, ...){ 1361 va_list ap; 1362 int rc = SQLITE_OK; 1363 VtabCtx *p; 1364 1365 #ifdef SQLITE_ENABLE_API_ARMOR 1366 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 1367 #endif 1368 sqlite3_mutex_enter(db->mutex); 1369 p = db->pVtabCtx; 1370 if( !p ){ 1371 rc = SQLITE_MISUSE_BKPT; 1372 }else{ 1373 assert( p->pTab==0 || IsVirtual(p->pTab) ); 1374 va_start(ap, op); 1375 switch( op ){ 1376 case SQLITE_VTAB_CONSTRAINT_SUPPORT: { 1377 p->pVTable->bConstraint = (u8)va_arg(ap, int); 1378 break; 1379 } 1380 case SQLITE_VTAB_INNOCUOUS: { 1381 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low; 1382 break; 1383 } 1384 case SQLITE_VTAB_DIRECTONLY: { 1385 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High; 1386 break; 1387 } 1388 case SQLITE_VTAB_USES_ALL_SCHEMAS: { 1389 p->pVTable->bAllSchemas = 1; 1390 break; 1391 } 1392 default: { 1393 rc = SQLITE_MISUSE_BKPT; 1394 break; 1395 } 1396 } 1397 va_end(ap); 1398 } 1399 1400 if( rc!=SQLITE_OK ) sqlite3Error(db, rc); 1401 sqlite3_mutex_leave(db->mutex); 1402 return rc; 1403 } 1404 1405 #endif /* SQLITE_OMIT_VIRTUALTABLE */