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