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 ** $Id: vtab.c,v 1.66 2008/04/10 18:35:22 drh Exp $ 15 */ 16 #ifndef SQLITE_OMIT_VIRTUALTABLE 17 #include "sqliteInt.h" 18 19 static int createModule( 20 sqlite3 *db, /* Database in which module is registered */ 21 const char *zName, /* Name assigned to this module */ 22 const sqlite3_module *pModule, /* The definition of the module */ 23 void *pAux, /* Context pointer for xCreate/xConnect */ 24 void (*xDestroy)(void *) /* Module destructor function */ 25 ) { 26 int rc, nName; 27 Module *pMod; 28 29 sqlite3_mutex_enter(db->mutex); 30 nName = strlen(zName); 31 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1); 32 if( pMod ){ 33 char *zCopy = (char *)(&pMod[1]); 34 memcpy(zCopy, zName, nName+1); 35 pMod->zName = zCopy; 36 pMod->pModule = pModule; 37 pMod->pAux = pAux; 38 pMod->xDestroy = xDestroy; 39 pMod = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod); 40 if( pMod && pMod->xDestroy ){ 41 pMod->xDestroy(pMod->pAux); 42 } 43 sqlite3_free(pMod); 44 sqlite3ResetInternalSchema(db, 0); 45 } 46 rc = sqlite3ApiExit(db, SQLITE_OK); 47 sqlite3_mutex_leave(db->mutex); 48 return rc; 49 } 50 51 52 /* 53 ** External API function used to create a new virtual-table module. 54 */ 55 int sqlite3_create_module( 56 sqlite3 *db, /* Database in which module is registered */ 57 const char *zName, /* Name assigned to this module */ 58 const sqlite3_module *pModule, /* The definition of the module */ 59 void *pAux /* Context pointer for xCreate/xConnect */ 60 ){ 61 return createModule(db, zName, pModule, pAux, 0); 62 } 63 64 /* 65 ** External API function used to create a new virtual-table module. 66 */ 67 int sqlite3_create_module_v2( 68 sqlite3 *db, /* Database in which module is registered */ 69 const char *zName, /* Name assigned to this module */ 70 const sqlite3_module *pModule, /* The definition of the module */ 71 void *pAux, /* Context pointer for xCreate/xConnect */ 72 void (*xDestroy)(void *) /* Module destructor function */ 73 ){ 74 return createModule(db, zName, pModule, pAux, xDestroy); 75 } 76 77 /* 78 ** Lock the virtual table so that it cannot be disconnected. 79 ** Locks nest. Every lock should have a corresponding unlock. 80 ** If an unlock is omitted, resources leaks will occur. 81 ** 82 ** If a disconnect is attempted while a virtual table is locked, 83 ** the disconnect is deferred until all locks have been removed. 84 */ 85 void sqlite3VtabLock(sqlite3_vtab *pVtab){ 86 pVtab->nRef++; 87 } 88 89 /* 90 ** Unlock a virtual table. When the last lock is removed, 91 ** disconnect the virtual table. 92 */ 93 void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){ 94 pVtab->nRef--; 95 assert(db); 96 assert( sqlite3SafetyCheckOk(db) ); 97 if( pVtab->nRef==0 ){ 98 if( db->magic==SQLITE_MAGIC_BUSY ){ 99 (void)sqlite3SafetyOff(db); 100 pVtab->pModule->xDisconnect(pVtab); 101 (void)sqlite3SafetyOn(db); 102 } else { 103 pVtab->pModule->xDisconnect(pVtab); 104 } 105 } 106 } 107 108 /* 109 ** Clear any and all virtual-table information from the Table record. 110 ** This routine is called, for example, just before deleting the Table 111 ** record. 112 */ 113 void sqlite3VtabClear(Table *p){ 114 sqlite3_vtab *pVtab = p->pVtab; 115 if( pVtab ){ 116 assert( p->pMod && p->pMod->pModule ); 117 sqlite3VtabUnlock(p->pSchema->db, pVtab); 118 p->pVtab = 0; 119 } 120 if( p->azModuleArg ){ 121 int i; 122 for(i=0; i<p->nModuleArg; i++){ 123 sqlite3_free(p->azModuleArg[i]); 124 } 125 sqlite3_free(p->azModuleArg); 126 } 127 } 128 129 /* 130 ** Add a new module argument to pTable->azModuleArg[]. 131 ** The string is not copied - the pointer is stored. The 132 ** string will be freed automatically when the table is 133 ** deleted. 134 */ 135 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){ 136 int i = pTable->nModuleArg++; 137 int nBytes = sizeof(char *)*(1+pTable->nModuleArg); 138 char **azModuleArg; 139 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes); 140 if( azModuleArg==0 ){ 141 int j; 142 for(j=0; j<i; j++){ 143 sqlite3_free(pTable->azModuleArg[j]); 144 } 145 sqlite3_free(zArg); 146 sqlite3_free(pTable->azModuleArg); 147 pTable->nModuleArg = 0; 148 }else{ 149 azModuleArg[i] = zArg; 150 azModuleArg[i+1] = 0; 151 } 152 pTable->azModuleArg = azModuleArg; 153 } 154 155 /* 156 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE 157 ** statement. The module name has been parsed, but the optional list 158 ** of parameters that follow the module name are still pending. 159 */ 160 void sqlite3VtabBeginParse( 161 Parse *pParse, /* Parsing context */ 162 Token *pName1, /* Name of new table, or database name */ 163 Token *pName2, /* Name of new table or NULL */ 164 Token *pModuleName /* Name of the module for the virtual table */ 165 ){ 166 int iDb; /* The database the table is being created in */ 167 Table *pTable; /* The new virtual table */ 168 sqlite3 *db; /* Database connection */ 169 170 if( pParse->db->flags & SQLITE_SharedCache ){ 171 sqlite3ErrorMsg(pParse, "Cannot use virtual tables in shared-cache mode"); 172 return; 173 } 174 175 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0); 176 pTable = pParse->pNewTable; 177 if( pTable==0 || pParse->nErr ) return; 178 assert( 0==pTable->pIndex ); 179 180 db = pParse->db; 181 iDb = sqlite3SchemaToIndex(db, pTable->pSchema); 182 assert( iDb>=0 ); 183 184 pTable->isVirtual = 1; 185 pTable->nModuleArg = 0; 186 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); 187 addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName)); 188 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); 189 pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z; 190 191 #ifndef SQLITE_OMIT_AUTHORIZATION 192 /* Creating a virtual table invokes the authorization callback twice. 193 ** The first invocation, to obtain permission to INSERT a row into the 194 ** sqlite_master table, has already been made by sqlite3StartTable(). 195 ** The second call, to obtain permission to create the table, is made now. 196 */ 197 if( pTable->azModuleArg ){ 198 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 199 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName); 200 } 201 #endif 202 } 203 204 /* 205 ** This routine takes the module argument that has been accumulating 206 ** in pParse->zArg[] and appends it to the list of arguments on the 207 ** virtual table currently under construction in pParse->pTable. 208 */ 209 static void addArgumentToVtab(Parse *pParse){ 210 if( pParse->sArg.z && pParse->pNewTable ){ 211 const char *z = (const char*)pParse->sArg.z; 212 int n = pParse->sArg.n; 213 sqlite3 *db = pParse->db; 214 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); 215 } 216 } 217 218 /* 219 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement 220 ** has been completely parsed. 221 */ 222 void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ 223 Table *pTab; /* The table being constructed */ 224 sqlite3 *db; /* The database connection */ 225 char *zModule; /* The module name of the table: USING modulename */ 226 Module *pMod = 0; 227 228 addArgumentToVtab(pParse); 229 pParse->sArg.z = 0; 230 231 /* Lookup the module name. */ 232 pTab = pParse->pNewTable; 233 if( pTab==0 ) return; 234 db = pParse->db; 235 if( pTab->nModuleArg<1 ) return; 236 zModule = pTab->azModuleArg[0]; 237 pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule)); 238 pTab->pMod = pMod; 239 240 /* If the CREATE VIRTUAL TABLE statement is being entered for the 241 ** first time (in other words if the virtual table is actually being 242 ** created now instead of just being read out of sqlite_master) then 243 ** do additional initialization work and store the statement text 244 ** in the sqlite_master table. 245 */ 246 if( !db->init.busy ){ 247 char *zStmt; 248 char *zWhere; 249 int iDb; 250 Vdbe *v; 251 252 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ 253 if( pEnd ){ 254 pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n; 255 } 256 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken); 257 258 /* A slot for the record has already been allocated in the 259 ** SQLITE_MASTER table. We just need to update that slot with all 260 ** the information we've collected. 261 ** 262 ** The VM register number pParse->regRowid holds the rowid of an 263 ** entry in the sqlite_master table tht was created for this vtab 264 ** by sqlite3StartTable(). 265 */ 266 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 267 sqlite3NestedParse(pParse, 268 "UPDATE %Q.%s " 269 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q " 270 "WHERE rowid=#%d", 271 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), 272 pTab->zName, 273 pTab->zName, 274 zStmt, 275 pParse->regRowid 276 ); 277 sqlite3_free(zStmt); 278 v = sqlite3GetVdbe(pParse); 279 sqlite3ChangeCookie(pParse, iDb); 280 281 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); 282 zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName); 283 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC); 284 sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, 285 pTab->zName, strlen(pTab->zName) + 1); 286 } 287 288 /* If we are rereading the sqlite_master table create the in-memory 289 ** record of the table. If the module has already been registered, 290 ** also call the xConnect method here. 291 */ 292 else { 293 Table *pOld; 294 Schema *pSchema = pTab->pSchema; 295 const char *zName = pTab->zName; 296 int nName = strlen(zName) + 1; 297 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab); 298 if( pOld ){ 299 db->mallocFailed = 1; 300 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ 301 return; 302 } 303 pSchema->db = pParse->db; 304 pParse->pNewTable = 0; 305 } 306 } 307 308 /* 309 ** The parser calls this routine when it sees the first token 310 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement. 311 */ 312 void sqlite3VtabArgInit(Parse *pParse){ 313 addArgumentToVtab(pParse); 314 pParse->sArg.z = 0; 315 pParse->sArg.n = 0; 316 } 317 318 /* 319 ** The parser calls this routine for each token after the first token 320 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement. 321 */ 322 void sqlite3VtabArgExtend(Parse *pParse, Token *p){ 323 Token *pArg = &pParse->sArg; 324 if( pArg->z==0 ){ 325 pArg->z = p->z; 326 pArg->n = p->n; 327 }else{ 328 assert(pArg->z < p->z); 329 pArg->n = (p->z + p->n - pArg->z); 330 } 331 } 332 333 /* 334 ** Invoke a virtual table constructor (either xCreate or xConnect). The 335 ** pointer to the function to invoke is passed as the fourth parameter 336 ** to this procedure. 337 */ 338 static int vtabCallConstructor( 339 sqlite3 *db, 340 Table *pTab, 341 Module *pMod, 342 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), 343 char **pzErr 344 ){ 345 int rc; 346 int rc2; 347 sqlite3_vtab *pVtab = 0; 348 const char *const*azArg = (const char *const*)pTab->azModuleArg; 349 int nArg = pTab->nModuleArg; 350 char *zErr = 0; 351 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); 352 353 if( !zModuleName ){ 354 return SQLITE_NOMEM; 355 } 356 357 assert( !db->pVTab ); 358 assert( xConstruct ); 359 360 db->pVTab = pTab; 361 rc = sqlite3SafetyOff(db); 362 assert( rc==SQLITE_OK ); 363 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVtab, &zErr); 364 rc2 = sqlite3SafetyOn(db); 365 if( rc==SQLITE_OK && pVtab ){ 366 pVtab->pModule = pMod->pModule; 367 pVtab->nRef = 1; 368 pTab->pVtab = pVtab; 369 } 370 371 if( SQLITE_OK!=rc ){ 372 if( zErr==0 ){ 373 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); 374 }else { 375 *pzErr = sqlite3MPrintf(db, "%s", zErr); 376 sqlite3_free(zErr); 377 } 378 }else if( db->pVTab ){ 379 const char *zFormat = "vtable constructor did not declare schema: %s"; 380 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); 381 rc = SQLITE_ERROR; 382 } 383 if( rc==SQLITE_OK ){ 384 rc = rc2; 385 } 386 db->pVTab = 0; 387 sqlite3_free(zModuleName); 388 389 /* If everything went according to plan, loop through the columns 390 ** of the table to see if any of them contain the token "hidden". 391 ** If so, set the Column.isHidden flag and remove the token from 392 ** the type string. 393 */ 394 if( rc==SQLITE_OK ){ 395 int iCol; 396 for(iCol=0; iCol<pTab->nCol; iCol++){ 397 char *zType = pTab->aCol[iCol].zType; 398 int nType; 399 int i = 0; 400 if( !zType ) continue; 401 nType = strlen(zType); 402 if( sqlite3StrNICmp("hidden", zType, 6) || (zType[6] && zType[6]!=' ') ){ 403 for(i=0; i<nType; i++){ 404 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7)) 405 && (zType[i+7]=='\0' || zType[i+7]==' ') 406 ){ 407 i++; 408 break; 409 } 410 } 411 } 412 if( i<nType ){ 413 int j; 414 int nDel = 6 + (zType[i+6] ? 1 : 0); 415 for(j=i; (j+nDel)<=nType; j++){ 416 zType[j] = zType[j+nDel]; 417 } 418 if( zType[i]=='\0' && i>0 ){ 419 assert(zType[i-1]==' '); 420 zType[i-1] = '\0'; 421 } 422 pTab->aCol[iCol].isHidden = 1; 423 } 424 } 425 } 426 return rc; 427 } 428 429 /* 430 ** This function is invoked by the parser to call the xConnect() method 431 ** of the virtual table pTab. If an error occurs, an error code is returned 432 ** and an error left in pParse. 433 ** 434 ** This call is a no-op if table pTab is not a virtual table. 435 */ 436 int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ 437 Module *pMod; 438 int rc = SQLITE_OK; 439 440 if( !pTab || !pTab->isVirtual || pTab->pVtab ){ 441 return SQLITE_OK; 442 } 443 444 pMod = pTab->pMod; 445 if( !pMod ){ 446 const char *zModule = pTab->azModuleArg[0]; 447 sqlite3ErrorMsg(pParse, "no such module: %s", zModule); 448 rc = SQLITE_ERROR; 449 } else { 450 char *zErr = 0; 451 sqlite3 *db = pParse->db; 452 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr); 453 if( rc!=SQLITE_OK ){ 454 sqlite3ErrorMsg(pParse, "%s", zErr); 455 } 456 sqlite3_free(zErr); 457 } 458 459 return rc; 460 } 461 462 /* 463 ** Add the virtual table pVtab to the array sqlite3.aVTrans[]. 464 */ 465 static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){ 466 const int ARRAY_INCR = 5; 467 468 /* Grow the sqlite3.aVTrans array if required */ 469 if( (db->nVTrans%ARRAY_INCR)==0 ){ 470 sqlite3_vtab **aVTrans; 471 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR); 472 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes); 473 if( !aVTrans ){ 474 return SQLITE_NOMEM; 475 } 476 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); 477 db->aVTrans = aVTrans; 478 } 479 480 /* Add pVtab to the end of sqlite3.aVTrans */ 481 db->aVTrans[db->nVTrans++] = pVtab; 482 sqlite3VtabLock(pVtab); 483 return SQLITE_OK; 484 } 485 486 /* 487 ** This function is invoked by the vdbe to call the xCreate method 488 ** of the virtual table named zTab in database iDb. 489 ** 490 ** If an error occurs, *pzErr is set to point an an English language 491 ** description of the error and an SQLITE_XXX error code is returned. 492 ** In this case the caller must call sqlite3_free() on *pzErr. 493 */ 494 int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ 495 int rc = SQLITE_OK; 496 Table *pTab; 497 Module *pMod; 498 const char *zModule; 499 500 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); 501 assert(pTab && pTab->isVirtual && !pTab->pVtab); 502 pMod = pTab->pMod; 503 zModule = pTab->azModuleArg[0]; 504 505 /* If the module has been registered and includes a Create method, 506 ** invoke it now. If the module has not been registered, return an 507 ** error. Otherwise, do nothing. 508 */ 509 if( !pMod ){ 510 *pzErr = sqlite3MPrintf(db, "no such module: %s", zModule); 511 rc = SQLITE_ERROR; 512 }else{ 513 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); 514 } 515 516 if( rc==SQLITE_OK && pTab->pVtab ){ 517 rc = addToVTrans(db, pTab->pVtab); 518 } 519 520 return rc; 521 } 522 523 /* 524 ** This function is used to set the schema of a virtual table. It is only 525 ** valid to call this function from within the xCreate() or xConnect() of a 526 ** virtual table module. 527 */ 528 int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ 529 Parse sParse; 530 531 int rc = SQLITE_OK; 532 Table *pTab; 533 char *zErr = 0; 534 535 sqlite3_mutex_enter(db->mutex); 536 pTab = db->pVTab; 537 if( !pTab ){ 538 sqlite3Error(db, SQLITE_MISUSE, 0); 539 sqlite3_mutex_leave(db->mutex); 540 return SQLITE_MISUSE; 541 } 542 assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0); 543 544 memset(&sParse, 0, sizeof(Parse)); 545 sParse.declareVtab = 1; 546 sParse.db = db; 547 548 if( 549 SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) && 550 sParse.pNewTable && 551 !sParse.pNewTable->pSelect && 552 !sParse.pNewTable->isVirtual 553 ){ 554 pTab->aCol = sParse.pNewTable->aCol; 555 pTab->nCol = sParse.pNewTable->nCol; 556 sParse.pNewTable->nCol = 0; 557 sParse.pNewTable->aCol = 0; 558 db->pVTab = 0; 559 } else { 560 sqlite3Error(db, SQLITE_ERROR, zErr); 561 sqlite3_free(zErr); 562 rc = SQLITE_ERROR; 563 } 564 sParse.declareVtab = 0; 565 566 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); 567 sqlite3DeleteTable(sParse.pNewTable); 568 sParse.pNewTable = 0; 569 570 assert( (rc&0xff)==rc ); 571 rc = sqlite3ApiExit(db, rc); 572 sqlite3_mutex_leave(db->mutex); 573 return rc; 574 } 575 576 /* 577 ** This function is invoked by the vdbe to call the xDestroy method 578 ** of the virtual table named zTab in database iDb. This occurs 579 ** when a DROP TABLE is mentioned. 580 ** 581 ** This call is a no-op if zTab is not a virtual table. 582 */ 583 int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab) 584 { 585 int rc = SQLITE_OK; 586 Table *pTab; 587 588 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); 589 assert(pTab); 590 if( pTab->pVtab ){ 591 int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy; 592 rc = sqlite3SafetyOff(db); 593 assert( rc==SQLITE_OK ); 594 if( xDestroy ){ 595 rc = xDestroy(pTab->pVtab); 596 } 597 (void)sqlite3SafetyOn(db); 598 if( rc==SQLITE_OK ){ 599 int i; 600 for(i=0; i<db->nVTrans; i++){ 601 if( db->aVTrans[i]==pTab->pVtab ){ 602 db->aVTrans[i] = db->aVTrans[--db->nVTrans]; 603 break; 604 } 605 } 606 pTab->pVtab = 0; 607 } 608 } 609 610 return rc; 611 } 612 613 /* 614 ** This function invokes either the xRollback or xCommit method 615 ** of each of the virtual tables in the sqlite3.aVTrans array. The method 616 ** called is identified by the second argument, "offset", which is 617 ** the offset of the method to call in the sqlite3_module structure. 618 ** 619 ** The array is cleared after invoking the callbacks. 620 */ 621 static void callFinaliser(sqlite3 *db, sqlite3_intptr_t offset){ 622 int i; 623 if( db->aVTrans ){ 624 for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){ 625 sqlite3_vtab *pVtab = db->aVTrans[i]; 626 int (*x)(sqlite3_vtab *); 627 x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset); 628 if( x ) x(pVtab); 629 sqlite3VtabUnlock(db, pVtab); 630 } 631 sqlite3_free(db->aVTrans); 632 db->nVTrans = 0; 633 db->aVTrans = 0; 634 } 635 } 636 637 /* 638 ** If argument rc2 is not SQLITE_OK, then return it and do nothing. 639 ** Otherwise, invoke the xSync method of all virtual tables in the 640 ** sqlite3.aVTrans array. Return the error code for the first error 641 ** that occurs, or SQLITE_OK if all xSync operations are successful. 642 */ 643 int sqlite3VtabSync(sqlite3 *db, int rc2){ 644 int i; 645 int rc = SQLITE_OK; 646 int rcsafety; 647 sqlite3_vtab **aVTrans = db->aVTrans; 648 if( rc2!=SQLITE_OK ) return rc2; 649 650 rc = sqlite3SafetyOff(db); 651 db->aVTrans = 0; 652 for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){ 653 sqlite3_vtab *pVtab = aVTrans[i]; 654 int (*x)(sqlite3_vtab *); 655 x = pVtab->pModule->xSync; 656 if( x ){ 657 rc = x(pVtab); 658 } 659 } 660 db->aVTrans = aVTrans; 661 rcsafety = sqlite3SafetyOn(db); 662 663 if( rc==SQLITE_OK ){ 664 rc = rcsafety; 665 } 666 return rc; 667 } 668 669 /* 670 ** Invoke the xRollback method of all virtual tables in the 671 ** sqlite3.aVTrans array. Then clear the array itself. 672 */ 673 int sqlite3VtabRollback(sqlite3 *db){ 674 callFinaliser(db, (sqlite3_intptr_t)(&((sqlite3_module *)0)->xRollback)); 675 return SQLITE_OK; 676 } 677 678 /* 679 ** Invoke the xCommit method of all virtual tables in the 680 ** sqlite3.aVTrans array. Then clear the array itself. 681 */ 682 int sqlite3VtabCommit(sqlite3 *db){ 683 callFinaliser(db, (sqlite3_intptr_t)(&((sqlite3_module *)0)->xCommit)); 684 return SQLITE_OK; 685 } 686 687 /* 688 ** If the virtual table pVtab supports the transaction interface 689 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is 690 ** not currently open, invoke the xBegin method now. 691 ** 692 ** If the xBegin call is successful, place the sqlite3_vtab pointer 693 ** in the sqlite3.aVTrans array. 694 */ 695 int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){ 696 int rc = SQLITE_OK; 697 const sqlite3_module *pModule; 698 699 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater 700 ** than zero, then this function is being called from within a 701 ** virtual module xSync() callback. It is illegal to write to 702 ** virtual module tables in this case, so return SQLITE_LOCKED. 703 */ 704 if( 0==db->aVTrans && db->nVTrans>0 ){ 705 return SQLITE_LOCKED; 706 } 707 if( !pVtab ){ 708 return SQLITE_OK; 709 } 710 pModule = pVtab->pModule; 711 712 if( pModule->xBegin ){ 713 int i; 714 715 716 /* If pVtab is already in the aVTrans array, return early */ 717 for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){ 718 if( db->aVTrans[i]==pVtab ){ 719 return SQLITE_OK; 720 } 721 } 722 723 /* Invoke the xBegin method */ 724 rc = pModule->xBegin(pVtab); 725 if( rc!=SQLITE_OK ){ 726 return rc; 727 } 728 729 rc = addToVTrans(db, pVtab); 730 } 731 return rc; 732 } 733 734 /* 735 ** The first parameter (pDef) is a function implementation. The 736 ** second parameter (pExpr) is the first argument to this function. 737 ** If pExpr is a column in a virtual table, then let the virtual 738 ** table implementation have an opportunity to overload the function. 739 ** 740 ** This routine is used to allow virtual table implementations to 741 ** overload MATCH, LIKE, GLOB, and REGEXP operators. 742 ** 743 ** Return either the pDef argument (indicating no change) or a 744 ** new FuncDef structure that is marked as ephemeral using the 745 ** SQLITE_FUNC_EPHEM flag. 746 */ 747 FuncDef *sqlite3VtabOverloadFunction( 748 sqlite3 *db, /* Database connection for reporting malloc problems */ 749 FuncDef *pDef, /* Function to possibly overload */ 750 int nArg, /* Number of arguments to the function */ 751 Expr *pExpr /* First argument to the function */ 752 ){ 753 Table *pTab; 754 sqlite3_vtab *pVtab; 755 sqlite3_module *pMod; 756 void (*xFunc)(sqlite3_context*,int,sqlite3_value**); 757 void *pArg; 758 FuncDef *pNew; 759 int rc = 0; 760 char *zLowerName; 761 unsigned char *z; 762 763 764 /* Check to see the left operand is a column in a virtual table */ 765 if( pExpr==0 ) return pDef; 766 if( pExpr->op!=TK_COLUMN ) return pDef; 767 pTab = pExpr->pTab; 768 if( pTab==0 ) return pDef; 769 if( !pTab->isVirtual ) return pDef; 770 pVtab = pTab->pVtab; 771 assert( pVtab!=0 ); 772 assert( pVtab->pModule!=0 ); 773 pMod = (sqlite3_module *)pVtab->pModule; 774 if( pMod->xFindFunction==0 ) return pDef; 775 776 /* Call the xFindFunction method on the virtual table implementation 777 ** to see if the implementation wants to overload this function 778 */ 779 zLowerName = sqlite3DbStrDup(db, pDef->zName); 780 if( zLowerName ){ 781 for(z=(unsigned char*)zLowerName; *z; z++){ 782 *z = sqlite3UpperToLower[*z]; 783 } 784 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg); 785 sqlite3_free(zLowerName); 786 } 787 if( rc==0 ){ 788 return pDef; 789 } 790 791 /* Create a new ephemeral function definition for the overloaded 792 ** function */ 793 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) ); 794 if( pNew==0 ){ 795 return pDef; 796 } 797 *pNew = *pDef; 798 memcpy(pNew->zName, pDef->zName, strlen(pDef->zName)+1); 799 pNew->xFunc = xFunc; 800 pNew->pUserData = pArg; 801 pNew->flags |= SQLITE_FUNC_EPHEM; 802 return pNew; 803 } 804 805 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 806