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