vtab.c 23 KB

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