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