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