vtab.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. #ifndef SQLITE_OMIT_VIRTUALTABLE
  15. #include "sqliteInt.h"
  16. /*
  17. ** Before a virtual table xCreate() or xConnect() method is invoked, the
  18. ** sqlite3.pVtabCtx member variable is set to point to an instance of
  19. ** this struct allocated on the stack. It is used by the implementation of
  20. ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
  21. ** are invoked only from within xCreate and xConnect methods.
  22. */
  23. struct VtabCtx {
  24. VTable *pVTable; /* The virtual table being constructed */
  25. Table *pTab; /* The Table object to which the virtual table belongs */
  26. };
  27. /*
  28. ** The actual function that does the work of creating a new module.
  29. ** This function implements the sqlite3_create_module() and
  30. ** sqlite3_create_module_v2() interfaces.
  31. */
  32. static int createModule(
  33. sqlite3 *db, /* Database in which module is registered */
  34. const char *zName, /* Name assigned to this module */
  35. const sqlite3_module *pModule, /* The definition of the module */
  36. void *pAux, /* Context pointer for xCreate/xConnect */
  37. void (*xDestroy)(void *) /* Module destructor function */
  38. ){
  39. int rc = SQLITE_OK;
  40. int nName;
  41. sqlite3_mutex_enter(db->mutex);
  42. nName = sqlite3Strlen30(zName);
  43. if( sqlite3HashFind(&db->aModule, zName, nName) ){
  44. rc = SQLITE_MISUSE_BKPT;
  45. }else{
  46. Module *pMod;
  47. pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
  48. if( pMod ){
  49. Module *pDel;
  50. char *zCopy = (char *)(&pMod[1]);
  51. memcpy(zCopy, zName, nName+1);
  52. pMod->zName = zCopy;
  53. pMod->pModule = pModule;
  54. pMod->pAux = pAux;
  55. pMod->xDestroy = xDestroy;
  56. pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,nName,(void*)pMod);
  57. assert( pDel==0 || pDel==pMod );
  58. if( pDel ){
  59. db->mallocFailed = 1;
  60. sqlite3DbFree(db, pDel);
  61. }
  62. }
  63. }
  64. rc = sqlite3ApiExit(db, rc);
  65. if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
  66. sqlite3_mutex_leave(db->mutex);
  67. return rc;
  68. }
  69. /*
  70. ** External API function used to create a new virtual-table module.
  71. */
  72. int sqlite3_create_module(
  73. sqlite3 *db, /* Database in which module is registered */
  74. const char *zName, /* Name assigned to this module */
  75. const sqlite3_module *pModule, /* The definition of the module */
  76. void *pAux /* Context pointer for xCreate/xConnect */
  77. ){
  78. return createModule(db, zName, pModule, pAux, 0);
  79. }
  80. /*
  81. ** External API function used to create a new virtual-table module.
  82. */
  83. int sqlite3_create_module_v2(
  84. sqlite3 *db, /* Database in which module is registered */
  85. const char *zName, /* Name assigned to this module */
  86. const sqlite3_module *pModule, /* The definition of the module */
  87. void *pAux, /* Context pointer for xCreate/xConnect */
  88. void (*xDestroy)(void *) /* Module destructor function */
  89. ){
  90. return createModule(db, zName, pModule, pAux, xDestroy);
  91. }
  92. /*
  93. ** Lock the virtual table so that it cannot be disconnected.
  94. ** Locks nest. Every lock should have a corresponding unlock.
  95. ** If an unlock is omitted, resources leaks will occur.
  96. **
  97. ** If a disconnect is attempted while a virtual table is locked,
  98. ** the disconnect is deferred until all locks have been removed.
  99. */
  100. void sqlite3VtabLock(VTable *pVTab){
  101. pVTab->nRef++;
  102. }
  103. /*
  104. ** pTab is a pointer to a Table structure representing a virtual-table.
  105. ** Return a pointer to the VTable object used by connection db to access
  106. ** this virtual-table, if one has been created, or NULL otherwise.
  107. */
  108. VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
  109. VTable *pVtab;
  110. assert( IsVirtual(pTab) );
  111. for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
  112. return pVtab;
  113. }
  114. /*
  115. ** Decrement the ref-count on a virtual table object. When the ref-count
  116. ** reaches zero, call the xDisconnect() method to delete the object.
  117. */
  118. void sqlite3VtabUnlock(VTable *pVTab){
  119. sqlite3 *db = pVTab->db;
  120. assert( db );
  121. assert( pVTab->nRef>0 );
  122. assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
  123. pVTab->nRef--;
  124. if( pVTab->nRef==0 ){
  125. sqlite3_vtab *p = pVTab->pVtab;
  126. if( p ){
  127. p->pModule->xDisconnect(p);
  128. }
  129. sqlite3DbFree(db, pVTab);
  130. }
  131. }
  132. /*
  133. ** Table p is a virtual table. This function moves all elements in the
  134. ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
  135. ** database connections to be disconnected at the next opportunity.
  136. ** Except, if argument db is not NULL, then the entry associated with
  137. ** connection db is left in the p->pVTable list.
  138. */
  139. static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
  140. VTable *pRet = 0;
  141. VTable *pVTable = p->pVTable;
  142. p->pVTable = 0;
  143. /* Assert that the mutex (if any) associated with the BtShared database
  144. ** that contains table p is held by the caller. See header comments
  145. ** above function sqlite3VtabUnlockList() for an explanation of why
  146. ** this makes it safe to access the sqlite3.pDisconnect list of any
  147. ** database connection that may have an entry in the p->pVTable list.
  148. */
  149. assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
  150. while( pVTable ){
  151. sqlite3 *db2 = pVTable->db;
  152. VTable *pNext = pVTable->pNext;
  153. assert( db2 );
  154. if( db2==db ){
  155. pRet = pVTable;
  156. p->pVTable = pRet;
  157. pRet->pNext = 0;
  158. }else{
  159. pVTable->pNext = db2->pDisconnect;
  160. db2->pDisconnect = pVTable;
  161. }
  162. pVTable = pNext;
  163. }
  164. assert( !db || pRet );
  165. return pRet;
  166. }
  167. /*
  168. ** Table *p is a virtual table. This function removes the VTable object
  169. ** for table *p associated with database connection db from the linked
  170. ** list in p->pVTab. It also decrements the VTable ref count. This is
  171. ** used when closing database connection db to free all of its VTable
  172. ** objects without disturbing the rest of the Schema object (which may
  173. ** be being used by other shared-cache connections).
  174. */
  175. void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
  176. VTable **ppVTab;
  177. assert( IsVirtual(p) );
  178. assert( sqlite3BtreeHoldsAllMutexes(db) );
  179. assert( sqlite3_mutex_held(db->mutex) );
  180. for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
  181. if( (*ppVTab)->db==db ){
  182. VTable *pVTab = *ppVTab;
  183. *ppVTab = pVTab->pNext;
  184. sqlite3VtabUnlock(pVTab);
  185. break;
  186. }
  187. }
  188. }
  189. /*
  190. ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
  191. **
  192. ** This function may only be called when the mutexes associated with all
  193. ** shared b-tree databases opened using connection db are held by the
  194. ** caller. This is done to protect the sqlite3.pDisconnect list. The
  195. ** sqlite3.pDisconnect list is accessed only as follows:
  196. **
  197. ** 1) By this function. In this case, all BtShared mutexes and the mutex
  198. ** associated with the database handle itself must be held.
  199. **
  200. ** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
  201. ** the sqlite3.pDisconnect list. In this case either the BtShared mutex
  202. ** associated with the database the virtual table is stored in is held
  203. ** or, if the virtual table is stored in a non-sharable database, then
  204. ** the database handle mutex is held.
  205. **
  206. ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
  207. ** by multiple threads. It is thread-safe.
  208. */
  209. void sqlite3VtabUnlockList(sqlite3 *db){
  210. VTable *p = db->pDisconnect;
  211. db->pDisconnect = 0;
  212. assert( sqlite3BtreeHoldsAllMutexes(db) );
  213. assert( sqlite3_mutex_held(db->mutex) );
  214. if( p ){
  215. sqlite3ExpirePreparedStatements(db);
  216. do {
  217. VTable *pNext = p->pNext;
  218. sqlite3VtabUnlock(p);
  219. p = pNext;
  220. }while( p );
  221. }
  222. }
  223. /*
  224. ** Clear any and all virtual-table information from the Table record.
  225. ** This routine is called, for example, just before deleting the Table
  226. ** record.
  227. **
  228. ** Since it is a virtual-table, the Table structure contains a pointer
  229. ** to the head of a linked list of VTable structures. Each VTable
  230. ** structure is associated with a single sqlite3* user of the schema.
  231. ** The reference count of the VTable structure associated with database
  232. ** connection db is decremented immediately (which may lead to the
  233. ** structure being xDisconnected and free). Any other VTable structures
  234. ** in the list are moved to the sqlite3.pDisconnect list of the associated
  235. ** database connection.
  236. */
  237. void sqlite3VtabClear(sqlite3 *db, Table *p){
  238. if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
  239. if( p->azModuleArg ){
  240. int i;
  241. for(i=0; i<p->nModuleArg; i++){
  242. if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
  243. }
  244. sqlite3DbFree(db, p->azModuleArg);
  245. }
  246. }
  247. /*
  248. ** Add a new module argument to pTable->azModuleArg[].
  249. ** The string is not copied - the pointer is stored. The
  250. ** string will be freed automatically when the table is
  251. ** deleted.
  252. */
  253. static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
  254. int i = pTable->nModuleArg++;
  255. int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
  256. char **azModuleArg;
  257. azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
  258. if( azModuleArg==0 ){
  259. int j;
  260. for(j=0; j<i; j++){
  261. sqlite3DbFree(db, pTable->azModuleArg[j]);
  262. }
  263. sqlite3DbFree(db, zArg);
  264. sqlite3DbFree(db, pTable->azModuleArg);
  265. pTable->nModuleArg = 0;
  266. }else{
  267. azModuleArg[i] = zArg;
  268. azModuleArg[i+1] = 0;
  269. }
  270. pTable->azModuleArg = azModuleArg;
  271. }
  272. /*
  273. ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
  274. ** statement. The module name has been parsed, but the optional list
  275. ** of parameters that follow the module name are still pending.
  276. */
  277. void sqlite3VtabBeginParse(
  278. Parse *pParse, /* Parsing context */
  279. Token *pName1, /* Name of new table, or database name */
  280. Token *pName2, /* Name of new table or NULL */
  281. Token *pModuleName, /* Name of the module for the virtual table */
  282. int ifNotExists /* No error if the table already exists */
  283. ){
  284. int iDb; /* The database the table is being created in */
  285. Table *pTable; /* The new virtual table */
  286. sqlite3 *db; /* Database connection */
  287. sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
  288. pTable = pParse->pNewTable;
  289. if( pTable==0 ) return;
  290. assert( 0==pTable->pIndex );
  291. db = pParse->db;
  292. iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
  293. assert( iDb>=0 );
  294. pTable->tabFlags |= TF_Virtual;
  295. pTable->nModuleArg = 0;
  296. addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
  297. addModuleArgument(db, pTable, 0);
  298. addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
  299. pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
  300. #ifndef SQLITE_OMIT_AUTHORIZATION
  301. /* Creating a virtual table invokes the authorization callback twice.
  302. ** The first invocation, to obtain permission to INSERT a row into the
  303. ** sqlite_master table, has already been made by sqlite3StartTable().
  304. ** The second call, to obtain permission to create the table, is made now.
  305. */
  306. if( pTable->azModuleArg ){
  307. sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
  308. pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
  309. }
  310. #endif
  311. }
  312. /*
  313. ** This routine takes the module argument that has been accumulating
  314. ** in pParse->zArg[] and appends it to the list of arguments on the
  315. ** virtual table currently under construction in pParse->pTable.
  316. */
  317. static void addArgumentToVtab(Parse *pParse){
  318. if( pParse->sArg.z && pParse->pNewTable ){
  319. const char *z = (const char*)pParse->sArg.z;
  320. int n = pParse->sArg.n;
  321. sqlite3 *db = pParse->db;
  322. addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
  323. }
  324. }
  325. /*
  326. ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
  327. ** has been completely parsed.
  328. */
  329. void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
  330. Table *pTab = pParse->pNewTable; /* The table being constructed */
  331. sqlite3 *db = pParse->db; /* The database connection */
  332. if( pTab==0 ) return;
  333. addArgumentToVtab(pParse);
  334. pParse->sArg.z = 0;
  335. if( pTab->nModuleArg<1 ) return;
  336. /* If the CREATE VIRTUAL TABLE statement is being entered for the
  337. ** first time (in other words if the virtual table is actually being
  338. ** created now instead of just being read out of sqlite_master) then
  339. ** do additional initialization work and store the statement text
  340. ** in the sqlite_master table.
  341. */
  342. if( !db->init.busy ){
  343. char *zStmt;
  344. char *zWhere;
  345. int iDb;
  346. Vdbe *v;
  347. /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
  348. if( pEnd ){
  349. pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
  350. }
  351. zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
  352. /* A slot for the record has already been allocated in the
  353. ** SQLITE_MASTER table. We just need to update that slot with all
  354. ** the information we've collected.
  355. **
  356. ** The VM register number pParse->regRowid holds the rowid of an
  357. ** entry in the sqlite_master table tht was created for this vtab
  358. ** by sqlite3StartTable().
  359. */
  360. iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  361. sqlite3NestedParse(pParse,
  362. "UPDATE %Q.%s "
  363. "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
  364. "WHERE rowid=#%d",
  365. db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
  366. pTab->zName,
  367. pTab->zName,
  368. zStmt,
  369. pParse->regRowid
  370. );
  371. sqlite3DbFree(db, zStmt);
  372. v = sqlite3GetVdbe(pParse);
  373. sqlite3ChangeCookie(pParse, iDb);
  374. sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
  375. zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
  376. sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
  377. sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
  378. pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
  379. }
  380. /* If we are rereading the sqlite_master table create the in-memory
  381. ** record of the table. The xConnect() method is not called until
  382. ** the first time the virtual table is used in an SQL statement. This
  383. ** allows a schema that contains virtual tables to be loaded before
  384. ** the required virtual table implementations are registered. */
  385. else {
  386. Table *pOld;
  387. Schema *pSchema = pTab->pSchema;
  388. const char *zName = pTab->zName;
  389. int nName = sqlite3Strlen30(zName);
  390. assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
  391. pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
  392. if( pOld ){
  393. db->mallocFailed = 1;
  394. assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
  395. return;
  396. }
  397. pParse->pNewTable = 0;
  398. }
  399. }
  400. /*
  401. ** The parser calls this routine when it sees the first token
  402. ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
  403. */
  404. void sqlite3VtabArgInit(Parse *pParse){
  405. addArgumentToVtab(pParse);
  406. pParse->sArg.z = 0;
  407. pParse->sArg.n = 0;
  408. }
  409. /*
  410. ** The parser calls this routine for each token after the first token
  411. ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
  412. */
  413. void sqlite3VtabArgExtend(Parse *pParse, Token *p){
  414. Token *pArg = &pParse->sArg;
  415. if( pArg->z==0 ){
  416. pArg->z = p->z;
  417. pArg->n = p->n;
  418. }else{
  419. assert(pArg->z < p->z);
  420. pArg->n = (int)(&p->z[p->n] - pArg->z);
  421. }
  422. }
  423. /*
  424. ** Invoke a virtual table constructor (either xCreate or xConnect). The
  425. ** pointer to the function to invoke is passed as the fourth parameter
  426. ** to this procedure.
  427. */
  428. static int vtabCallConstructor(
  429. sqlite3 *db,
  430. Table *pTab,
  431. Module *pMod,
  432. int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
  433. char **pzErr
  434. ){
  435. VtabCtx sCtx, *pPriorCtx;
  436. VTable *pVTable;
  437. int rc;
  438. const char *const*azArg = (const char *const*)pTab->azModuleArg;
  439. int nArg = pTab->nModuleArg;
  440. char *zErr = 0;
  441. char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
  442. int iDb;
  443. if( !zModuleName ){
  444. return SQLITE_NOMEM;
  445. }
  446. pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
  447. if( !pVTable ){
  448. sqlite3DbFree(db, zModuleName);
  449. return SQLITE_NOMEM;
  450. }
  451. pVTable->db = db;
  452. pVTable->pMod = pMod;
  453. iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  454. pTab->azModuleArg[1] = db->aDb[iDb].zName;
  455. /* Invoke the virtual table constructor */
  456. assert( &db->pVtabCtx );
  457. assert( xConstruct );
  458. sCtx.pTab = pTab;
  459. sCtx.pVTable = pVTable;
  460. pPriorCtx = db->pVtabCtx;
  461. db->pVtabCtx = &sCtx;
  462. rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
  463. db->pVtabCtx = pPriorCtx;
  464. if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
  465. if( SQLITE_OK!=rc ){
  466. if( zErr==0 ){
  467. *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
  468. }else {
  469. *pzErr = sqlite3MPrintf(db, "%s", zErr);
  470. sqlite3_free(zErr);
  471. }
  472. sqlite3DbFree(db, pVTable);
  473. }else if( ALWAYS(pVTable->pVtab) ){
  474. /* Justification of ALWAYS(): A correct vtab constructor must allocate
  475. ** the sqlite3_vtab object if successful. */
  476. pVTable->pVtab->pModule = pMod->pModule;
  477. pVTable->nRef = 1;
  478. if( sCtx.pTab ){
  479. const char *zFormat = "vtable constructor did not declare schema: %s";
  480. *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
  481. sqlite3VtabUnlock(pVTable);
  482. rc = SQLITE_ERROR;
  483. }else{
  484. int iCol;
  485. /* If everything went according to plan, link the new VTable structure
  486. ** into the linked list headed by pTab->pVTable. Then loop through the
  487. ** columns of the table to see if any of them contain the token "hidden".
  488. ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
  489. ** the type string. */
  490. pVTable->pNext = pTab->pVTable;
  491. pTab->pVTable = pVTable;
  492. for(iCol=0; iCol<pTab->nCol; iCol++){
  493. char *zType = pTab->aCol[iCol].zType;
  494. int nType;
  495. int i = 0;
  496. if( !zType ) continue;
  497. nType = sqlite3Strlen30(zType);
  498. if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
  499. for(i=0; i<nType; i++){
  500. if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
  501. && (zType[i+7]=='\0' || zType[i+7]==' ')
  502. ){
  503. i++;
  504. break;
  505. }
  506. }
  507. }
  508. if( i<nType ){
  509. int j;
  510. int nDel = 6 + (zType[i+6] ? 1 : 0);
  511. for(j=i; (j+nDel)<=nType; j++){
  512. zType[j] = zType[j+nDel];
  513. }
  514. if( zType[i]=='\0' && i>0 ){
  515. assert(zType[i-1]==' ');
  516. zType[i-1] = '\0';
  517. }
  518. pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
  519. }
  520. }
  521. }
  522. }
  523. sqlite3DbFree(db, zModuleName);
  524. return rc;
  525. }
  526. /*
  527. ** This function is invoked by the parser to call the xConnect() method
  528. ** of the virtual table pTab. If an error occurs, an error code is returned
  529. ** and an error left in pParse.
  530. **
  531. ** This call is a no-op if table pTab is not a virtual table.
  532. */
  533. int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
  534. sqlite3 *db = pParse->db;
  535. const char *zMod;
  536. Module *pMod;
  537. int rc;
  538. assert( pTab );
  539. if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
  540. return SQLITE_OK;
  541. }
  542. /* Locate the required virtual table module */
  543. zMod = pTab->azModuleArg[0];
  544. pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
  545. if( !pMod ){
  546. const char *zModule = pTab->azModuleArg[0];
  547. sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
  548. rc = SQLITE_ERROR;
  549. }else{
  550. char *zErr = 0;
  551. rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
  552. if( rc!=SQLITE_OK ){
  553. sqlite3ErrorMsg(pParse, "%s", zErr);
  554. }
  555. sqlite3DbFree(db, zErr);
  556. }
  557. return rc;
  558. }
  559. /*
  560. ** Grow the db->aVTrans[] array so that there is room for at least one
  561. ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
  562. */
  563. static int growVTrans(sqlite3 *db){
  564. const int ARRAY_INCR = 5;
  565. /* Grow the sqlite3.aVTrans array if required */
  566. if( (db->nVTrans%ARRAY_INCR)==0 ){
  567. VTable **aVTrans;
  568. int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
  569. aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
  570. if( !aVTrans ){
  571. return SQLITE_NOMEM;
  572. }
  573. memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
  574. db->aVTrans = aVTrans;
  575. }
  576. return SQLITE_OK;
  577. }
  578. /*
  579. ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
  580. ** have already been reserved using growVTrans().
  581. */
  582. static void addToVTrans(sqlite3 *db, VTable *pVTab){
  583. /* Add pVtab to the end of sqlite3.aVTrans */
  584. db->aVTrans[db->nVTrans++] = pVTab;
  585. sqlite3VtabLock(pVTab);
  586. }
  587. /*
  588. ** This function is invoked by the vdbe to call the xCreate method
  589. ** of the virtual table named zTab in database iDb.
  590. **
  591. ** If an error occurs, *pzErr is set to point an an English language
  592. ** description of the error and an SQLITE_XXX error code is returned.
  593. ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
  594. */
  595. int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
  596. int rc = SQLITE_OK;
  597. Table *pTab;
  598. Module *pMod;
  599. const char *zMod;
  600. pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
  601. assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
  602. /* Locate the required virtual table module */
  603. zMod = pTab->azModuleArg[0];
  604. pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
  605. /* If the module has been registered and includes a Create method,
  606. ** invoke it now. If the module has not been registered, return an
  607. ** error. Otherwise, do nothing.
  608. */
  609. if( !pMod ){
  610. *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
  611. rc = SQLITE_ERROR;
  612. }else{
  613. rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
  614. }
  615. /* Justification of ALWAYS(): The xConstructor method is required to
  616. ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
  617. if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
  618. rc = growVTrans(db);
  619. if( rc==SQLITE_OK ){
  620. addToVTrans(db, sqlite3GetVTable(db, pTab));
  621. }
  622. }
  623. return rc;
  624. }
  625. /*
  626. ** This function is used to set the schema of a virtual table. It is only
  627. ** valid to call this function from within the xCreate() or xConnect() of a
  628. ** virtual table module.
  629. */
  630. int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
  631. Parse *pParse;
  632. int rc = SQLITE_OK;
  633. Table *pTab;
  634. char *zErr = 0;
  635. sqlite3_mutex_enter(db->mutex);
  636. if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
  637. sqlite3Error(db, SQLITE_MISUSE, 0);
  638. sqlite3_mutex_leave(db->mutex);
  639. return SQLITE_MISUSE_BKPT;
  640. }
  641. assert( (pTab->tabFlags & TF_Virtual)!=0 );
  642. pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
  643. if( pParse==0 ){
  644. rc = SQLITE_NOMEM;
  645. }else{
  646. pParse->declareVtab = 1;
  647. pParse->db = db;
  648. pParse->nQueryLoop = 1;
  649. if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
  650. && pParse->pNewTable
  651. && !db->mallocFailed
  652. && !pParse->pNewTable->pSelect
  653. && (pParse->pNewTable->tabFlags & TF_Virtual)==0
  654. ){
  655. if( !pTab->aCol ){
  656. pTab->aCol = pParse->pNewTable->aCol;
  657. pTab->nCol = pParse->pNewTable->nCol;
  658. pParse->pNewTable->nCol = 0;
  659. pParse->pNewTable->aCol = 0;
  660. }
  661. db->pVtabCtx->pTab = 0;
  662. }else{
  663. sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
  664. sqlite3DbFree(db, zErr);
  665. rc = SQLITE_ERROR;
  666. }
  667. pParse->declareVtab = 0;
  668. if( pParse->pVdbe ){
  669. sqlite3VdbeFinalize(pParse->pVdbe);
  670. }
  671. sqlite3DeleteTable(db, pParse->pNewTable);
  672. sqlite3StackFree(db, pParse);
  673. }
  674. assert( (rc&0xff)==rc );
  675. rc = sqlite3ApiExit(db, rc);
  676. sqlite3_mutex_leave(db->mutex);
  677. return rc;
  678. }
  679. /*
  680. ** This function is invoked by the vdbe to call the xDestroy method
  681. ** of the virtual table named zTab in database iDb. This occurs
  682. ** when a DROP TABLE is mentioned.
  683. **
  684. ** This call is a no-op if zTab is not a virtual table.
  685. */
  686. int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
  687. int rc = SQLITE_OK;
  688. Table *pTab;
  689. pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
  690. if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
  691. VTable *p = vtabDisconnectAll(db, pTab);
  692. assert( rc==SQLITE_OK );
  693. rc = p->pMod->pModule->xDestroy(p->pVtab);
  694. /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
  695. if( rc==SQLITE_OK ){
  696. assert( pTab->pVTable==p && p->pNext==0 );
  697. p->pVtab = 0;
  698. pTab->pVTable = 0;
  699. sqlite3VtabUnlock(p);
  700. }
  701. }
  702. return rc;
  703. }
  704. /*
  705. ** This function invokes either the xRollback or xCommit method
  706. ** of each of the virtual tables in the sqlite3.aVTrans array. The method
  707. ** called is identified by the second argument, "offset", which is
  708. ** the offset of the method to call in the sqlite3_module structure.
  709. **
  710. ** The array is cleared after invoking the callbacks.
  711. */
  712. static void callFinaliser(sqlite3 *db, int offset){
  713. int i;
  714. if( db->aVTrans ){
  715. for(i=0; i<db->nVTrans; i++){
  716. VTable *pVTab = db->aVTrans[i];
  717. sqlite3_vtab *p = pVTab->pVtab;
  718. if( p ){
  719. int (*x)(sqlite3_vtab *);
  720. x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
  721. if( x ) x(p);
  722. }
  723. pVTab->iSavepoint = 0;
  724. sqlite3VtabUnlock(pVTab);
  725. }
  726. sqlite3DbFree(db, db->aVTrans);
  727. db->nVTrans = 0;
  728. db->aVTrans = 0;
  729. }
  730. }
  731. /*
  732. ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
  733. ** array. Return the error code for the first error that occurs, or
  734. ** SQLITE_OK if all xSync operations are successful.
  735. **
  736. ** If an error message is available, leave it in p->zErrMsg.
  737. */
  738. int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
  739. int i;
  740. int rc = SQLITE_OK;
  741. VTable **aVTrans = db->aVTrans;
  742. db->aVTrans = 0;
  743. for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
  744. int (*x)(sqlite3_vtab *);
  745. sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
  746. if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
  747. rc = x(pVtab);
  748. sqlite3VtabImportErrmsg(p, pVtab);
  749. }
  750. }
  751. db->aVTrans = aVTrans;
  752. return rc;
  753. }
  754. /*
  755. ** Invoke the xRollback method of all virtual tables in the
  756. ** sqlite3.aVTrans array. Then clear the array itself.
  757. */
  758. int sqlite3VtabRollback(sqlite3 *db){
  759. callFinaliser(db, offsetof(sqlite3_module,xRollback));
  760. return SQLITE_OK;
  761. }
  762. /*
  763. ** Invoke the xCommit method of all virtual tables in the
  764. ** sqlite3.aVTrans array. Then clear the array itself.
  765. */
  766. int sqlite3VtabCommit(sqlite3 *db){
  767. callFinaliser(db, offsetof(sqlite3_module,xCommit));
  768. return SQLITE_OK;
  769. }
  770. /*
  771. ** If the virtual table pVtab supports the transaction interface
  772. ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
  773. ** not currently open, invoke the xBegin method now.
  774. **
  775. ** If the xBegin call is successful, place the sqlite3_vtab pointer
  776. ** in the sqlite3.aVTrans array.
  777. */
  778. int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
  779. int rc = SQLITE_OK;
  780. const sqlite3_module *pModule;
  781. /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
  782. ** than zero, then this function is being called from within a
  783. ** virtual module xSync() callback. It is illegal to write to
  784. ** virtual module tables in this case, so return SQLITE_LOCKED.
  785. */
  786. if( sqlite3VtabInSync(db) ){
  787. return SQLITE_LOCKED;
  788. }
  789. if( !pVTab ){
  790. return SQLITE_OK;
  791. }
  792. pModule = pVTab->pVtab->pModule;
  793. if( pModule->xBegin ){
  794. int i;
  795. /* If pVtab is already in the aVTrans array, return early */
  796. for(i=0; i<db->nVTrans; i++){
  797. if( db->aVTrans[i]==pVTab ){
  798. return SQLITE_OK;
  799. }
  800. }
  801. /* Invoke the xBegin method. If successful, add the vtab to the
  802. ** sqlite3.aVTrans[] array. */
  803. rc = growVTrans(db);
  804. if( rc==SQLITE_OK ){
  805. rc = pModule->xBegin(pVTab->pVtab);
  806. if( rc==SQLITE_OK ){
  807. addToVTrans(db, pVTab);
  808. }
  809. }
  810. }
  811. return rc;
  812. }
  813. /*
  814. ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
  815. ** virtual tables that currently have an open transaction. Pass iSavepoint
  816. ** as the second argument to the virtual table method invoked.
  817. **
  818. ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
  819. ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
  820. ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
  821. ** an open transaction is invoked.
  822. **
  823. ** If any virtual table method returns an error code other than SQLITE_OK,
  824. ** processing is abandoned and the error returned to the caller of this
  825. ** function immediately. If all calls to virtual table methods are successful,
  826. ** SQLITE_OK is returned.
  827. */
  828. int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
  829. int rc = SQLITE_OK;
  830. assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
  831. assert( iSavepoint>=0 );
  832. if( db->aVTrans ){
  833. int i;
  834. for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
  835. VTable *pVTab = db->aVTrans[i];
  836. const sqlite3_module *pMod = pVTab->pMod->pModule;
  837. if( pVTab->pVtab && pMod->iVersion>=2 ){
  838. int (*xMethod)(sqlite3_vtab *, int);
  839. switch( op ){
  840. case SAVEPOINT_BEGIN:
  841. xMethod = pMod->xSavepoint;
  842. pVTab->iSavepoint = iSavepoint+1;
  843. break;
  844. case SAVEPOINT_ROLLBACK:
  845. xMethod = pMod->xRollbackTo;
  846. break;
  847. default:
  848. xMethod = pMod->xRelease;
  849. break;
  850. }
  851. if( xMethod && pVTab->iSavepoint>iSavepoint ){
  852. rc = xMethod(pVTab->pVtab, iSavepoint);
  853. }
  854. }
  855. }
  856. }
  857. return rc;
  858. }
  859. /*
  860. ** The first parameter (pDef) is a function implementation. The
  861. ** second parameter (pExpr) is the first argument to this function.
  862. ** If pExpr is a column in a virtual table, then let the virtual
  863. ** table implementation have an opportunity to overload the function.
  864. **
  865. ** This routine is used to allow virtual table implementations to
  866. ** overload MATCH, LIKE, GLOB, and REGEXP operators.
  867. **
  868. ** Return either the pDef argument (indicating no change) or a
  869. ** new FuncDef structure that is marked as ephemeral using the
  870. ** SQLITE_FUNC_EPHEM flag.
  871. */
  872. FuncDef *sqlite3VtabOverloadFunction(
  873. sqlite3 *db, /* Database connection for reporting malloc problems */
  874. FuncDef *pDef, /* Function to possibly overload */
  875. int nArg, /* Number of arguments to the function */
  876. Expr *pExpr /* First argument to the function */
  877. ){
  878. Table *pTab;
  879. sqlite3_vtab *pVtab;
  880. sqlite3_module *pMod;
  881. void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
  882. void *pArg = 0;
  883. FuncDef *pNew;
  884. int rc = 0;
  885. char *zLowerName;
  886. unsigned char *z;
  887. /* Check to see the left operand is a column in a virtual table */
  888. if( NEVER(pExpr==0) ) return pDef;
  889. if( pExpr->op!=TK_COLUMN ) return pDef;
  890. pTab = pExpr->pTab;
  891. if( NEVER(pTab==0) ) return pDef;
  892. if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
  893. pVtab = sqlite3GetVTable(db, pTab)->pVtab;
  894. assert( pVtab!=0 );
  895. assert( pVtab->pModule!=0 );
  896. pMod = (sqlite3_module *)pVtab->pModule;
  897. if( pMod->xFindFunction==0 ) return pDef;
  898. /* Call the xFindFunction method on the virtual table implementation
  899. ** to see if the implementation wants to overload this function
  900. */
  901. zLowerName = sqlite3DbStrDup(db, pDef->zName);
  902. if( zLowerName ){
  903. for(z=(unsigned char*)zLowerName; *z; z++){
  904. *z = sqlite3UpperToLower[*z];
  905. }
  906. rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
  907. sqlite3DbFree(db, zLowerName);
  908. }
  909. if( rc==0 ){
  910. return pDef;
  911. }
  912. /* Create a new ephemeral function definition for the overloaded
  913. ** function */
  914. pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
  915. + sqlite3Strlen30(pDef->zName) + 1);
  916. if( pNew==0 ){
  917. return pDef;
  918. }
  919. *pNew = *pDef;
  920. pNew->zName = (char *)&pNew[1];
  921. memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
  922. pNew->xFunc = xFunc;
  923. pNew->pUserData = pArg;
  924. pNew->funcFlags |= SQLITE_FUNC_EPHEM;
  925. return pNew;
  926. }
  927. /*
  928. ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
  929. ** array so that an OP_VBegin will get generated for it. Add pTab to the
  930. ** array if it is missing. If pTab is already in the array, this routine
  931. ** is a no-op.
  932. */
  933. void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
  934. Parse *pToplevel = sqlite3ParseToplevel(pParse);
  935. int i, n;
  936. Table **apVtabLock;
  937. assert( IsVirtual(pTab) );
  938. for(i=0; i<pToplevel->nVtabLock; i++){
  939. if( pTab==pToplevel->apVtabLock[i] ) return;
  940. }
  941. n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
  942. apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
  943. if( apVtabLock ){
  944. pToplevel->apVtabLock = apVtabLock;
  945. pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
  946. }else{
  947. pToplevel->db->mallocFailed = 1;
  948. }
  949. }
  950. /*
  951. ** Return the ON CONFLICT resolution mode in effect for the virtual
  952. ** table update operation currently in progress.
  953. **
  954. ** The results of this routine are undefined unless it is called from
  955. ** within an xUpdate method.
  956. */
  957. int sqlite3_vtab_on_conflict(sqlite3 *db){
  958. static const unsigned char aMap[] = {
  959. SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
  960. };
  961. assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
  962. assert( OE_Ignore==4 && OE_Replace==5 );
  963. assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
  964. return (int)aMap[db->vtabOnConflict-1];
  965. }
  966. /*
  967. ** Call from within the xCreate() or xConnect() methods to provide
  968. ** the SQLite core with additional information about the behavior
  969. ** of the virtual table being implemented.
  970. */
  971. int sqlite3_vtab_config(sqlite3 *db, int op, ...){
  972. va_list ap;
  973. int rc = SQLITE_OK;
  974. sqlite3_mutex_enter(db->mutex);
  975. va_start(ap, op);
  976. switch( op ){
  977. case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
  978. VtabCtx *p = db->pVtabCtx;
  979. if( !p ){
  980. rc = SQLITE_MISUSE_BKPT;
  981. }else{
  982. assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
  983. p->pVTable->bConstraint = (u8)va_arg(ap, int);
  984. }
  985. break;
  986. }
  987. default:
  988. rc = SQLITE_MISUSE_BKPT;
  989. break;
  990. }
  991. va_end(ap);
  992. if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
  993. sqlite3_mutex_leave(db->mutex);
  994. return rc;
  995. }
  996. #endif /* SQLITE_OMIT_VIRTUALTABLE */