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