blob: 1989391e227f73a899af85899c17c7268f0582a1 [file] [log] [blame]
drhb9bb7c12006-06-11 23:41:55 +00001/*
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.
drhb9bb7c12006-06-11 23:41:55 +000013*/
14#ifndef SQLITE_OMIT_VIRTUALTABLE
15#include "sqliteInt.h"
16
drh63842412009-04-11 16:27:19 +000017/*
danb061d052011-04-25 18:49:57 +000018** Before a virtual table xCreate() or xConnect() method is invoked, the
19** sqlite3.pVtabCtx member variable is set to point to an instance of
20** this struct allocated on the stack. It is used by the implementation of
21** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
22** are invoked only from within xCreate and xConnect methods.
23*/
24struct VtabCtx {
drh81028a42012-05-15 18:28:27 +000025 VTable *pVTable; /* The virtual table being constructed */
26 Table *pTab; /* The Table object to which the virtual table belongs */
dan75395cc2015-04-10 07:55:07 +000027 VtabCtx *pPrior; /* Parent context (if any) */
28 int bDeclared; /* True after sqlite3_declare_vtab() is called */
danb061d052011-04-25 18:49:57 +000029};
30
31/*
drh63842412009-04-11 16:27:19 +000032** The actual function that does the work of creating a new module.
33** This function implements the sqlite3_create_module() and
34** sqlite3_create_module_v2() interfaces.
35*/
danielk1977832a58a2007-06-22 15:21:15 +000036static int createModule(
37 sqlite3 *db, /* Database in which module is registered */
38 const char *zName, /* Name assigned to this module */
39 const sqlite3_module *pModule, /* The definition of the module */
40 void *pAux, /* Context pointer for xCreate/xConnect */
41 void (*xDestroy)(void *) /* Module destructor function */
danielk1977595a5232009-07-24 17:58:53 +000042){
danca8b9ba2012-05-16 14:29:11 +000043 int rc = SQLITE_OK;
44 int nName;
drh27641702007-08-22 02:56:42 +000045
46 sqlite3_mutex_enter(db->mutex);
drhea678832008-12-10 19:26:22 +000047 nName = sqlite3Strlen30(zName);
drhacbcb7e2014-08-21 20:26:37 +000048 if( sqlite3HashFind(&db->aModule, zName) ){
danca8b9ba2012-05-16 14:29:11 +000049 rc = SQLITE_MISUSE_BKPT;
50 }else{
51 Module *pMod;
52 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
53 if( pMod ){
54 Module *pDel;
55 char *zCopy = (char *)(&pMod[1]);
56 memcpy(zCopy, zName, nName+1);
57 pMod->zName = zCopy;
58 pMod->pModule = pModule;
59 pMod->pAux = pAux;
60 pMod->xDestroy = xDestroy;
drhacbcb7e2014-08-21 20:26:37 +000061 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
danca8b9ba2012-05-16 14:29:11 +000062 assert( pDel==0 || pDel==pMod );
63 if( pDel ){
64 db->mallocFailed = 1;
65 sqlite3DbFree(db, pDel);
66 }
danielk1977832a58a2007-06-22 15:21:15 +000067 }
danielk1977832a58a2007-06-22 15:21:15 +000068 }
danca8b9ba2012-05-16 14:29:11 +000069 rc = sqlite3ApiExit(db, rc);
70 if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
71
drh27641702007-08-22 02:56:42 +000072 sqlite3_mutex_leave(db->mutex);
73 return rc;
danielk1977832a58a2007-06-22 15:21:15 +000074}
75
76
drhb9bb7c12006-06-11 23:41:55 +000077/*
78** External API function used to create a new virtual-table module.
79*/
80int sqlite3_create_module(
81 sqlite3 *db, /* Database in which module is registered */
82 const char *zName, /* Name assigned to this module */
danielk1977d1ab1ba2006-06-15 04:28:13 +000083 const sqlite3_module *pModule, /* The definition of the module */
84 void *pAux /* Context pointer for xCreate/xConnect */
drhb9bb7c12006-06-11 23:41:55 +000085){
drh9ca95732014-10-24 00:35:58 +000086#ifdef SQLITE_ENABLE_API_ARMOR
87 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
88#endif
danielk1977832a58a2007-06-22 15:21:15 +000089 return createModule(db, zName, pModule, pAux, 0);
90}
91
92/*
93** External API function used to create a new virtual-table module.
94*/
95int sqlite3_create_module_v2(
96 sqlite3 *db, /* Database in which module is registered */
97 const char *zName, /* Name assigned to this module */
98 const sqlite3_module *pModule, /* The definition of the module */
99 void *pAux, /* Context pointer for xCreate/xConnect */
100 void (*xDestroy)(void *) /* Module destructor function */
101){
drh9ca95732014-10-24 00:35:58 +0000102#ifdef SQLITE_ENABLE_API_ARMOR
103 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
104#endif
danielk1977832a58a2007-06-22 15:21:15 +0000105 return createModule(db, zName, pModule, pAux, xDestroy);
drhb9bb7c12006-06-11 23:41:55 +0000106}
107
drhb9bb7c12006-06-11 23:41:55 +0000108/*
drh189d4af2006-09-02 20:57:52 +0000109** Lock the virtual table so that it cannot be disconnected.
110** Locks nest. Every lock should have a corresponding unlock.
111** If an unlock is omitted, resources leaks will occur.
112**
113** If a disconnect is attempted while a virtual table is locked,
114** the disconnect is deferred until all locks have been removed.
115*/
danielk1977595a5232009-07-24 17:58:53 +0000116void sqlite3VtabLock(VTable *pVTab){
117 pVTab->nRef++;
118}
119
120
121/*
122** pTab is a pointer to a Table structure representing a virtual-table.
123** Return a pointer to the VTable object used by connection db to access
124** this virtual-table, if one has been created, or NULL otherwise.
125*/
126VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
127 VTable *pVtab;
128 assert( IsVirtual(pTab) );
129 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
130 return pVtab;
drh189d4af2006-09-02 20:57:52 +0000131}
132
133/*
danielk1977595a5232009-07-24 17:58:53 +0000134** Decrement the ref-count on a virtual table object. When the ref-count
135** reaches zero, call the xDisconnect() method to delete the object.
drh189d4af2006-09-02 20:57:52 +0000136*/
danielk1977595a5232009-07-24 17:58:53 +0000137void sqlite3VtabUnlock(VTable *pVTab){
138 sqlite3 *db = pVTab->db;
139
140 assert( db );
141 assert( pVTab->nRef>0 );
drh04f2e342012-08-27 14:39:47 +0000142 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
danielk1977595a5232009-07-24 17:58:53 +0000143
144 pVTab->nRef--;
145 if( pVTab->nRef==0 ){
146 sqlite3_vtab *p = pVTab->pVtab;
147 if( p ){
drh9978c972010-02-23 17:36:32 +0000148 p->pModule->xDisconnect(p);
danielk1977a04a34f2007-04-16 15:06:25 +0000149 }
danielk1977595a5232009-07-24 17:58:53 +0000150 sqlite3DbFree(db, pVTab);
151 }
152}
153
154/*
155** Table p is a virtual table. This function moves all elements in the
156** p->pVTable list to the sqlite3.pDisconnect lists of their associated
157** database connections to be disconnected at the next opportunity.
158** Except, if argument db is not NULL, then the entry associated with
159** connection db is left in the p->pVTable list.
160*/
161static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
162 VTable *pRet = 0;
163 VTable *pVTable = p->pVTable;
164 p->pVTable = 0;
165
166 /* Assert that the mutex (if any) associated with the BtShared database
167 ** that contains table p is held by the caller. See header comments
168 ** above function sqlite3VtabUnlockList() for an explanation of why
169 ** this makes it safe to access the sqlite3.pDisconnect list of any
drh9bbc2e22011-04-04 20:40:22 +0000170 ** database connection that may have an entry in the p->pVTable list.
171 */
172 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
danielk1977595a5232009-07-24 17:58:53 +0000173
174 while( pVTable ){
175 sqlite3 *db2 = pVTable->db;
176 VTable *pNext = pVTable->pNext;
177 assert( db2 );
178 if( db2==db ){
179 pRet = pVTable;
180 p->pVTable = pRet;
181 pRet->pNext = 0;
182 }else{
183 pVTable->pNext = db2->pDisconnect;
184 db2->pDisconnect = pVTable;
185 }
186 pVTable = pNext;
187 }
188
189 assert( !db || pRet );
190 return pRet;
191}
192
danbba02a92012-05-15 17:15:34 +0000193/*
194** Table *p is a virtual table. This function removes the VTable object
195** for table *p associated with database connection db from the linked
196** list in p->pVTab. It also decrements the VTable ref count. This is
197** used when closing database connection db to free all of its VTable
198** objects without disturbing the rest of the Schema object (which may
199** be being used by other shared-cache connections).
200*/
201void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
202 VTable **ppVTab;
203
204 assert( IsVirtual(p) );
205 assert( sqlite3BtreeHoldsAllMutexes(db) );
206 assert( sqlite3_mutex_held(db->mutex) );
207
208 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
209 if( (*ppVTab)->db==db ){
210 VTable *pVTab = *ppVTab;
211 *ppVTab = pVTab->pNext;
212 sqlite3VtabUnlock(pVTab);
213 break;
214 }
215 }
216}
217
danielk1977595a5232009-07-24 17:58:53 +0000218
219/*
220** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
221**
222** This function may only be called when the mutexes associated with all
223** shared b-tree databases opened using connection db are held by the
224** caller. This is done to protect the sqlite3.pDisconnect list. The
225** sqlite3.pDisconnect list is accessed only as follows:
226**
227** 1) By this function. In this case, all BtShared mutexes and the mutex
228** associated with the database handle itself must be held.
229**
230** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
231** the sqlite3.pDisconnect list. In this case either the BtShared mutex
232** associated with the database the virtual table is stored in is held
233** or, if the virtual table is stored in a non-sharable database, then
234** the database handle mutex is held.
235**
236** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
237** by multiple threads. It is thread-safe.
238*/
239void sqlite3VtabUnlockList(sqlite3 *db){
240 VTable *p = db->pDisconnect;
241 db->pDisconnect = 0;
242
243 assert( sqlite3BtreeHoldsAllMutexes(db) );
244 assert( sqlite3_mutex_held(db->mutex) );
245
246 if( p ){
247 sqlite3ExpirePreparedStatements(db);
248 do {
249 VTable *pNext = p->pNext;
250 sqlite3VtabUnlock(p);
251 p = pNext;
252 }while( p );
drh189d4af2006-09-02 20:57:52 +0000253 }
254}
255
256/*
drhb9bb7c12006-06-11 23:41:55 +0000257** Clear any and all virtual-table information from the Table record.
258** This routine is called, for example, just before deleting the Table
259** record.
danielk1977595a5232009-07-24 17:58:53 +0000260**
261** Since it is a virtual-table, the Table structure contains a pointer
262** to the head of a linked list of VTable structures. Each VTable
263** structure is associated with a single sqlite3* user of the schema.
264** The reference count of the VTable structure associated with database
265** connection db is decremented immediately (which may lead to the
266** structure being xDisconnected and free). Any other VTable structures
267** in the list are moved to the sqlite3.pDisconnect list of the associated
268** database connection.
drhb9bb7c12006-06-11 23:41:55 +0000269*/
dan1feeaed2010-07-23 15:41:47 +0000270void sqlite3VtabClear(sqlite3 *db, Table *p){
dand46def72010-07-24 11:28:28 +0000271 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
drhb9bb7c12006-06-11 23:41:55 +0000272 if( p->azModuleArg ){
273 int i;
274 for(i=0; i<p->nModuleArg; i++){
drh5a558262012-10-09 14:36:47 +0000275 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
drhb9bb7c12006-06-11 23:41:55 +0000276 }
dan1feeaed2010-07-23 15:41:47 +0000277 sqlite3DbFree(db, p->azModuleArg);
drhb9bb7c12006-06-11 23:41:55 +0000278 }
279}
280
281/*
282** Add a new module argument to pTable->azModuleArg[].
283** The string is not copied - the pointer is stored. The
284** string will be freed automatically when the table is
285** deleted.
286*/
drh17435752007-08-16 04:30:38 +0000287static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
drhb9bb7c12006-06-11 23:41:55 +0000288 int i = pTable->nModuleArg++;
danielk1977b7a2f2e2006-06-23 11:34:54 +0000289 int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
290 char **azModuleArg;
danielk197726783a52007-08-29 14:06:22 +0000291 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000292 if( azModuleArg==0 ){
293 int j;
294 for(j=0; j<i; j++){
drh633e6d52008-07-28 19:34:53 +0000295 sqlite3DbFree(db, pTable->azModuleArg[j]);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000296 }
drh633e6d52008-07-28 19:34:53 +0000297 sqlite3DbFree(db, zArg);
298 sqlite3DbFree(db, pTable->azModuleArg);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000299 pTable->nModuleArg = 0;
drhb9bb7c12006-06-11 23:41:55 +0000300 }else{
danielk1977b7a2f2e2006-06-23 11:34:54 +0000301 azModuleArg[i] = zArg;
302 azModuleArg[i+1] = 0;
drhb9bb7c12006-06-11 23:41:55 +0000303 }
danielk1977b7a2f2e2006-06-23 11:34:54 +0000304 pTable->azModuleArg = azModuleArg;
drhb9bb7c12006-06-11 23:41:55 +0000305}
306
307/*
308** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
309** statement. The module name has been parsed, but the optional list
310** of parameters that follow the module name are still pending.
311*/
312void sqlite3VtabBeginParse(
313 Parse *pParse, /* Parsing context */
314 Token *pName1, /* Name of new table, or database name */
315 Token *pName2, /* Name of new table or NULL */
drhb421b892012-01-28 19:41:53 +0000316 Token *pModuleName, /* Name of the module for the virtual table */
317 int ifNotExists /* No error if the table already exists */
drhb9bb7c12006-06-11 23:41:55 +0000318){
danielk1977f1a381e2006-06-16 08:01:02 +0000319 int iDb; /* The database the table is being created in */
drhb9bb7c12006-06-11 23:41:55 +0000320 Table *pTable; /* The new virtual table */
drh17435752007-08-16 04:30:38 +0000321 sqlite3 *db; /* Database connection */
drhb9bb7c12006-06-11 23:41:55 +0000322
drhb421b892012-01-28 19:41:53 +0000323 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
drhb9bb7c12006-06-11 23:41:55 +0000324 pTable = pParse->pNewTable;
drh748ce212009-05-20 20:10:47 +0000325 if( pTable==0 ) return;
danielk1977f1a381e2006-06-16 08:01:02 +0000326 assert( 0==pTable->pIndex );
327
drh17435752007-08-16 04:30:38 +0000328 db = pParse->db;
329 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
danielk197770ba1642006-06-21 16:02:42 +0000330 assert( iDb>=0 );
331
drh7d10d5a2008-08-20 16:35:10 +0000332 pTable->tabFlags |= TF_Virtual;
drhb9bb7c12006-06-11 23:41:55 +0000333 pTable->nModuleArg = 0;
drh17435752007-08-16 04:30:38 +0000334 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
dan41fb5cd2012-10-04 19:33:00 +0000335 addModuleArgument(db, pTable, 0);
drh17435752007-08-16 04:30:38 +0000336 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
dand89b8342014-11-27 11:36:36 +0000337 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
338 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
339 );
340 pParse->sNameToken.n = (int)(
341 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
342 );
danielk1977f1a381e2006-06-16 08:01:02 +0000343
344#ifndef SQLITE_OMIT_AUTHORIZATION
345 /* Creating a virtual table invokes the authorization callback twice.
346 ** The first invocation, to obtain permission to INSERT a row into the
347 ** sqlite_master table, has already been made by sqlite3StartTable().
348 ** The second call, to obtain permission to create the table, is made now.
349 */
danielk1977be718892006-06-23 08:05:19 +0000350 if( pTable->azModuleArg ){
351 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
352 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
danielk1977f1a381e2006-06-16 08:01:02 +0000353 }
354#endif
drhb9bb7c12006-06-11 23:41:55 +0000355}
356
357/*
358** This routine takes the module argument that has been accumulating
359** in pParse->zArg[] and appends it to the list of arguments on the
360** virtual table currently under construction in pParse->pTable.
361*/
362static void addArgumentToVtab(Parse *pParse){
drhb421b892012-01-28 19:41:53 +0000363 if( pParse->sArg.z && pParse->pNewTable ){
drh7c2d87c2006-09-02 14:16:59 +0000364 const char *z = (const char*)pParse->sArg.z;
danielk197733b39332006-06-24 08:51:05 +0000365 int n = pParse->sArg.n;
drh17435752007-08-16 04:30:38 +0000366 sqlite3 *db = pParse->db;
367 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
drhb9bb7c12006-06-11 23:41:55 +0000368 }
drhb9bb7c12006-06-11 23:41:55 +0000369}
370
371/*
372** The parser calls this routine after the CREATE VIRTUAL TABLE statement
373** has been completely parsed.
374*/
375void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
danielk1977595a5232009-07-24 17:58:53 +0000376 Table *pTab = pParse->pNewTable; /* The table being constructed */
377 sqlite3 *db = pParse->db; /* The database connection */
drhb9bb7c12006-06-11 23:41:55 +0000378
danielk1977595a5232009-07-24 17:58:53 +0000379 if( pTab==0 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000380 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000381 pParse->sArg.z = 0;
drhb9bb7c12006-06-11 23:41:55 +0000382 if( pTab->nModuleArg<1 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000383
384 /* If the CREATE VIRTUAL TABLE statement is being entered for the
385 ** first time (in other words if the virtual table is actually being
386 ** created now instead of just being read out of sqlite_master) then
387 ** do additional initialization work and store the statement text
388 ** in the sqlite_master table.
389 */
390 if( !db->init.busy ){
391 char *zStmt;
danielk197778efaba2006-06-12 06:09:17 +0000392 char *zWhere;
drhb9bb7c12006-06-11 23:41:55 +0000393 int iDb;
dan73779452015-03-19 18:56:17 +0000394 int iReg;
drhb9bb7c12006-06-11 23:41:55 +0000395 Vdbe *v;
drhb9bb7c12006-06-11 23:41:55 +0000396
397 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
398 if( pEnd ){
drhb27b7f52008-12-10 18:03:45 +0000399 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
drhb9bb7c12006-06-11 23:41:55 +0000400 }
danielk19771e536952007-08-16 10:09:01 +0000401 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
drhb9bb7c12006-06-11 23:41:55 +0000402
403 /* A slot for the record has already been allocated in the
404 ** SQLITE_MASTER table. We just need to update that slot with all
danielk197778efaba2006-06-12 06:09:17 +0000405 ** the information we've collected.
406 **
drhb7654112008-01-12 12:48:07 +0000407 ** The VM register number pParse->regRowid holds the rowid of an
408 ** entry in the sqlite_master table tht was created for this vtab
409 ** by sqlite3StartTable().
drhb9bb7c12006-06-11 23:41:55 +0000410 */
411 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
412 sqlite3NestedParse(pParse,
413 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000414 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
drhb7654112008-01-12 12:48:07 +0000415 "WHERE rowid=#%d",
drhb9bb7c12006-06-11 23:41:55 +0000416 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
417 pTab->zName,
418 pTab->zName,
drhb7654112008-01-12 12:48:07 +0000419 zStmt,
420 pParse->regRowid
drhb9bb7c12006-06-11 23:41:55 +0000421 );
drh633e6d52008-07-28 19:34:53 +0000422 sqlite3DbFree(db, zStmt);
drhb9bb7c12006-06-11 23:41:55 +0000423 v = sqlite3GetVdbe(pParse);
drh9cbf3422008-01-17 16:22:13 +0000424 sqlite3ChangeCookie(pParse, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000425
drh66a51672008-01-03 00:01:23 +0000426 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
dan39f1bcb2010-09-29 07:16:46 +0000427 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
drh5d9c9da2011-06-03 20:11:17 +0000428 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
dan73779452015-03-19 18:56:17 +0000429
430 iReg = ++pParse->nMem;
431 sqlite3VdbeAddOp4(v, OP_String8, 0, iReg, 0, pTab->zName, 0);
432 sqlite3VdbeAddOp2(v, OP_VCreate, iDb, iReg);
drhb9bb7c12006-06-11 23:41:55 +0000433 }
434
danielk197778efaba2006-06-12 06:09:17 +0000435 /* If we are rereading the sqlite_master table create the in-memory
danielk1977595a5232009-07-24 17:58:53 +0000436 ** record of the table. The xConnect() method is not called until
437 ** the first time the virtual table is used in an SQL statement. This
438 ** allows a schema that contains virtual tables to be loaded before
439 ** the required virtual table implementations are registered. */
danielk197778efaba2006-06-12 06:09:17 +0000440 else {
danielk197778efaba2006-06-12 06:09:17 +0000441 Table *pOld;
442 Schema *pSchema = pTab->pSchema;
443 const char *zName = pTab->zName;
drh21206082011-04-04 18:22:02 +0000444 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
drhacbcb7e2014-08-21 20:26:37 +0000445 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
danielk197778efaba2006-06-12 06:09:17 +0000446 if( pOld ){
danielk1977a1644fd2007-08-29 12:31:25 +0000447 db->mallocFailed = 1;
danielk197778efaba2006-06-12 06:09:17 +0000448 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
449 return;
450 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000451 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000452 }
453}
454
455/*
456** The parser calls this routine when it sees the first token
457** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
458*/
459void sqlite3VtabArgInit(Parse *pParse){
460 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000461 pParse->sArg.z = 0;
462 pParse->sArg.n = 0;
drhb9bb7c12006-06-11 23:41:55 +0000463}
464
465/*
466** The parser calls this routine for each token after the first token
467** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
468*/
469void sqlite3VtabArgExtend(Parse *pParse, Token *p){
danielk197733b39332006-06-24 08:51:05 +0000470 Token *pArg = &pParse->sArg;
471 if( pArg->z==0 ){
472 pArg->z = p->z;
473 pArg->n = p->n;
474 }else{
drh542a1762015-04-19 23:11:10 +0000475 assert(pArg->z <= p->z);
drhb27b7f52008-12-10 18:03:45 +0000476 pArg->n = (int)(&p->z[p->n] - pArg->z);
drhb9bb7c12006-06-11 23:41:55 +0000477 }
drhb9bb7c12006-06-11 23:41:55 +0000478}
479
danielk197778efaba2006-06-12 06:09:17 +0000480/*
danielk19779da9d472006-06-14 06:58:15 +0000481** Invoke a virtual table constructor (either xCreate or xConnect). The
482** pointer to the function to invoke is passed as the fourth parameter
483** to this procedure.
484*/
485static int vtabCallConstructor(
486 sqlite3 *db,
487 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000488 Module *pMod,
drhe4102962006-09-11 00:34:22 +0000489 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
danielk19779da9d472006-06-14 06:58:15 +0000490 char **pzErr
491){
dan75395cc2015-04-10 07:55:07 +0000492 VtabCtx sCtx;
danielk1977595a5232009-07-24 17:58:53 +0000493 VTable *pVTable;
danielk19779da9d472006-06-14 06:58:15 +0000494 int rc;
drhe4102962006-09-11 00:34:22 +0000495 const char *const*azArg = (const char *const*)pTab->azModuleArg;
danielk19779da9d472006-06-14 06:58:15 +0000496 int nArg = pTab->nModuleArg;
drh4ca8aac2006-09-10 17:31:58 +0000497 char *zErr = 0;
dan75395cc2015-04-10 07:55:07 +0000498 char *zModuleName;
dan41fb5cd2012-10-04 19:33:00 +0000499 int iDb;
dan75395cc2015-04-10 07:55:07 +0000500 VtabCtx *pCtx;
danielk19779da9d472006-06-14 06:58:15 +0000501
dan75395cc2015-04-10 07:55:07 +0000502 /* Check that the virtual-table is not already being initialized */
503 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
504 if( pCtx->pTab==pTab ){
505 *pzErr = sqlite3MPrintf(db,
506 "vtable constructor called recursively: %s", pTab->zName
507 );
508 return SQLITE_LOCKED;
509 }
510 }
511
512 zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
danielk197701256832007-04-18 14:24:32 +0000513 if( !zModuleName ){
514 return SQLITE_NOMEM;
515 }
516
danielk1977595a5232009-07-24 17:58:53 +0000517 pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
518 if( !pVTable ){
519 sqlite3DbFree(db, zModuleName);
520 return SQLITE_NOMEM;
521 }
522 pVTable->db = db;
523 pVTable->pMod = pMod;
524
dan41fb5cd2012-10-04 19:33:00 +0000525 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
526 pTab->azModuleArg[1] = db->aDb[iDb].zName;
527
danielk1977595a5232009-07-24 17:58:53 +0000528 /* Invoke the virtual table constructor */
danb061d052011-04-25 18:49:57 +0000529 assert( &db->pVtabCtx );
530 assert( xConstruct );
531 sCtx.pTab = pTab;
532 sCtx.pVTable = pVTable;
dan75395cc2015-04-10 07:55:07 +0000533 sCtx.pPrior = db->pVtabCtx;
534 sCtx.bDeclared = 0;
danb061d052011-04-25 18:49:57 +0000535 db->pVtabCtx = &sCtx;
danielk1977595a5232009-07-24 17:58:53 +0000536 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
dan75395cc2015-04-10 07:55:07 +0000537 db->pVtabCtx = sCtx.pPrior;
drh55f04f22009-05-11 23:37:59 +0000538 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
dan75395cc2015-04-10 07:55:07 +0000539 assert( sCtx.pTab==pTab );
danielk19779da9d472006-06-14 06:58:15 +0000540
541 if( SQLITE_OK!=rc ){
drh4ca8aac2006-09-10 17:31:58 +0000542 if( zErr==0 ){
danielk19771e536952007-08-16 10:09:01 +0000543 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
drh4ca8aac2006-09-10 17:31:58 +0000544 }else {
danielk19771e536952007-08-16 10:09:01 +0000545 *pzErr = sqlite3MPrintf(db, "%s", zErr);
drhb9755982010-07-24 16:34:37 +0000546 sqlite3_free(zErr);
drhfe1368e2006-09-10 17:08:29 +0000547 }
danielk1977595a5232009-07-24 17:58:53 +0000548 sqlite3DbFree(db, pVTable);
549 }else if( ALWAYS(pVTable->pVtab) ){
550 /* Justification of ALWAYS(): A correct vtab constructor must allocate
551 ** the sqlite3_vtab object if successful. */
drh5604cc02014-10-14 20:25:43 +0000552 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
danielk1977595a5232009-07-24 17:58:53 +0000553 pVTable->pVtab->pModule = pMod->pModule;
554 pVTable->nRef = 1;
dan75395cc2015-04-10 07:55:07 +0000555 if( sCtx.bDeclared==0 ){
danielk1977595a5232009-07-24 17:58:53 +0000556 const char *zFormat = "vtable constructor did not declare schema: %s";
557 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
558 sqlite3VtabUnlock(pVTable);
559 rc = SQLITE_ERROR;
560 }else{
561 int iCol;
drha21f78b2015-04-19 18:32:43 +0000562 u8 oooHidden = 0;
danielk1977595a5232009-07-24 17:58:53 +0000563 /* If everything went according to plan, link the new VTable structure
564 ** into the linked list headed by pTab->pVTable. Then loop through the
565 ** columns of the table to see if any of them contain the token "hidden".
drha371ace2012-09-13 14:22:47 +0000566 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
danielk1977595a5232009-07-24 17:58:53 +0000567 ** the type string. */
568 pVTable->pNext = pTab->pVTable;
569 pTab->pVTable = pVTable;
danielk1977034ca142007-06-26 10:38:54 +0000570
danielk1977595a5232009-07-24 17:58:53 +0000571 for(iCol=0; iCol<pTab->nCol; iCol++){
572 char *zType = pTab->aCol[iCol].zType;
573 int nType;
574 int i = 0;
drha21f78b2015-04-19 18:32:43 +0000575 if( !zType ){
576 pTab->tabFlags |= oooHidden;
577 continue;
578 }
danielk1977595a5232009-07-24 17:58:53 +0000579 nType = sqlite3Strlen30(zType);
580 if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
581 for(i=0; i<nType; i++){
582 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
583 && (zType[i+7]=='\0' || zType[i+7]==' ')
584 ){
585 i++;
586 break;
587 }
danielk1977034ca142007-06-26 10:38:54 +0000588 }
589 }
danielk1977595a5232009-07-24 17:58:53 +0000590 if( i<nType ){
591 int j;
592 int nDel = 6 + (zType[i+6] ? 1 : 0);
593 for(j=i; (j+nDel)<=nType; j++){
594 zType[j] = zType[j+nDel];
595 }
596 if( zType[i]=='\0' && i>0 ){
597 assert(zType[i-1]==' ');
598 zType[i-1] = '\0';
599 }
drha371ace2012-09-13 14:22:47 +0000600 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
drha21f78b2015-04-19 18:32:43 +0000601 oooHidden = TF_OOOHidden;
602 }else{
603 pTab->tabFlags |= oooHidden;
danielk1977034ca142007-06-26 10:38:54 +0000604 }
danielk1977034ca142007-06-26 10:38:54 +0000605 }
606 }
607 }
danielk1977595a5232009-07-24 17:58:53 +0000608
609 sqlite3DbFree(db, zModuleName);
danielk19779da9d472006-06-14 06:58:15 +0000610 return rc;
611}
612
613/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000614** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000615** of the virtual table pTab. If an error occurs, an error code is returned
616** and an error left in pParse.
617**
618** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000619*/
620int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977595a5232009-07-24 17:58:53 +0000621 sqlite3 *db = pParse->db;
622 const char *zMod;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000623 Module *pMod;
danielk1977595a5232009-07-24 17:58:53 +0000624 int rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000625
drh748ce212009-05-20 20:10:47 +0000626 assert( pTab );
danielk1977595a5232009-07-24 17:58:53 +0000627 if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000628 return SQLITE_OK;
629 }
630
danielk1977595a5232009-07-24 17:58:53 +0000631 /* Locate the required virtual table module */
632 zMod = pTab->azModuleArg[0];
drhacbcb7e2014-08-21 20:26:37 +0000633 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
danielk1977595a5232009-07-24 17:58:53 +0000634
danielk1977d1ab1ba2006-06-15 04:28:13 +0000635 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000636 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000637 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000638 rc = SQLITE_ERROR;
danielk1977595a5232009-07-24 17:58:53 +0000639 }else{
danielk19779da9d472006-06-14 06:58:15 +0000640 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000641 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000642 if( rc!=SQLITE_OK ){
643 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000644 }
drh633e6d52008-07-28 19:34:53 +0000645 sqlite3DbFree(db, zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000646 }
647
648 return rc;
649}
danielk19779da9d472006-06-14 06:58:15 +0000650/*
dan2cac2072011-05-25 18:46:22 +0000651** Grow the db->aVTrans[] array so that there is room for at least one
652** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
danielk1977e7ff4032006-06-17 11:30:32 +0000653*/
dan2cac2072011-05-25 18:46:22 +0000654static int growVTrans(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000655 const int ARRAY_INCR = 5;
656
657 /* Grow the sqlite3.aVTrans array if required */
658 if( (db->nVTrans%ARRAY_INCR)==0 ){
danielk1977595a5232009-07-24 17:58:53 +0000659 VTable **aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000660 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
danielk197726783a52007-08-29 14:06:22 +0000661 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
danielk1977e7ff4032006-06-17 11:30:32 +0000662 if( !aVTrans ){
663 return SQLITE_NOMEM;
664 }
665 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
666 db->aVTrans = aVTrans;
667 }
668
dan2cac2072011-05-25 18:46:22 +0000669 return SQLITE_OK;
670}
671
672/*
673** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
674** have already been reserved using growVTrans().
675*/
676static void addToVTrans(sqlite3 *db, VTable *pVTab){
danielk1977e7ff4032006-06-17 11:30:32 +0000677 /* Add pVtab to the end of sqlite3.aVTrans */
danielk1977595a5232009-07-24 17:58:53 +0000678 db->aVTrans[db->nVTrans++] = pVTab;
679 sqlite3VtabLock(pVTab);
danielk1977e7ff4032006-06-17 11:30:32 +0000680}
681
682/*
danielk19779da9d472006-06-14 06:58:15 +0000683** This function is invoked by the vdbe to call the xCreate method
684** of the virtual table named zTab in database iDb.
685**
686** If an error occurs, *pzErr is set to point an an English language
687** description of the error and an SQLITE_XXX error code is returned.
drh633e6d52008-07-28 19:34:53 +0000688** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
danielk19779da9d472006-06-14 06:58:15 +0000689*/
690int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
691 int rc = SQLITE_OK;
692 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000693 Module *pMod;
danielk1977595a5232009-07-24 17:58:53 +0000694 const char *zMod;
danielk19779da9d472006-06-14 06:58:15 +0000695
696 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk1977595a5232009-07-24 17:58:53 +0000697 assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
698
699 /* Locate the required virtual table module */
700 zMod = pTab->azModuleArg[0];
drhacbcb7e2014-08-21 20:26:37 +0000701 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
danielk19779da9d472006-06-14 06:58:15 +0000702
703 /* If the module has been registered and includes a Create method,
704 ** invoke it now. If the module has not been registered, return an
705 ** error. Otherwise, do nothing.
706 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000707 if( !pMod ){
danielk1977595a5232009-07-24 17:58:53 +0000708 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
danielk19779da9d472006-06-14 06:58:15 +0000709 rc = SQLITE_ERROR;
710 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000711 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000712 }
713
drh748ce212009-05-20 20:10:47 +0000714 /* Justification of ALWAYS(): The xConstructor method is required to
715 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
danielk1977595a5232009-07-24 17:58:53 +0000716 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
dan2cac2072011-05-25 18:46:22 +0000717 rc = growVTrans(db);
718 if( rc==SQLITE_OK ){
719 addToVTrans(db, sqlite3GetVTable(db, pTab));
720 }
danielk1977e7ff4032006-06-17 11:30:32 +0000721 }
722
danielk19779da9d472006-06-14 06:58:15 +0000723 return rc;
724}
danielk1977be8a7832006-06-13 15:00:54 +0000725
726/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000727** This function is used to set the schema of a virtual table. It is only
728** valid to call this function from within the xCreate() or xConnect() of a
729** virtual table module.
730*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000731int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
dan4e235362015-04-10 16:05:33 +0000732 VtabCtx *pCtx;
drhe98c9042009-06-02 21:31:38 +0000733 Parse *pParse;
danielk19777e6ebfb2006-06-12 11:24:37 +0000734 int rc = SQLITE_OK;
drh27641702007-08-22 02:56:42 +0000735 Table *pTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000736 char *zErr = 0;
737
drh9ca95732014-10-24 00:35:58 +0000738#ifdef SQLITE_ENABLE_API_ARMOR
drh96c707a2015-02-13 16:36:14 +0000739 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
740 return SQLITE_MISUSE_BKPT;
741 }
drh9ca95732014-10-24 00:35:58 +0000742#endif
drh27641702007-08-22 02:56:42 +0000743 sqlite3_mutex_enter(db->mutex);
dan4e235362015-04-10 16:05:33 +0000744 pCtx = db->pVtabCtx;
dan75395cc2015-04-10 07:55:07 +0000745 if( !pCtx || pCtx->bDeclared ){
drh13f40da2014-08-22 18:00:11 +0000746 sqlite3Error(db, SQLITE_MISUSE);
drh27641702007-08-22 02:56:42 +0000747 sqlite3_mutex_leave(db->mutex);
drh413c3d32010-02-23 20:11:56 +0000748 return SQLITE_MISUSE_BKPT;
danielk19777e6ebfb2006-06-12 11:24:37 +0000749 }
dan75395cc2015-04-10 07:55:07 +0000750 pTab = pCtx->pTab;
danielk1977595a5232009-07-24 17:58:53 +0000751 assert( (pTab->tabFlags & TF_Virtual)!=0 );
danielk19777e6ebfb2006-06-12 11:24:37 +0000752
drhe98c9042009-06-02 21:31:38 +0000753 pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
754 if( pParse==0 ){
755 rc = SQLITE_NOMEM;
756 }else{
757 pParse->declareVtab = 1;
758 pParse->db = db;
drh8b307fb2010-04-06 15:57:05 +0000759 pParse->nQueryLoop = 1;
drhe98c9042009-06-02 21:31:38 +0000760
dan84db21e2009-12-08 19:05:53 +0000761 if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
dan84db21e2009-12-08 19:05:53 +0000762 && pParse->pNewTable
drh0e3c5122009-12-08 22:16:15 +0000763 && !db->mallocFailed
dan84db21e2009-12-08 19:05:53 +0000764 && !pParse->pNewTable->pSelect
765 && (pParse->pNewTable->tabFlags & TF_Virtual)==0
drhe98c9042009-06-02 21:31:38 +0000766 ){
danielk1977595a5232009-07-24 17:58:53 +0000767 if( !pTab->aCol ){
768 pTab->aCol = pParse->pNewTable->aCol;
769 pTab->nCol = pParse->pNewTable->nCol;
770 pParse->pNewTable->nCol = 0;
771 pParse->pNewTable->aCol = 0;
772 }
dan75395cc2015-04-10 07:55:07 +0000773 pCtx->bDeclared = 1;
dan84db21e2009-12-08 19:05:53 +0000774 }else{
drh13f40da2014-08-22 18:00:11 +0000775 sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
dan78f9b732010-08-11 11:59:37 +0000776 sqlite3DbFree(db, zErr);
drhe98c9042009-06-02 21:31:38 +0000777 rc = SQLITE_ERROR;
778 }
779 pParse->declareVtab = 0;
780
781 if( pParse->pVdbe ){
782 sqlite3VdbeFinalize(pParse->pVdbe);
783 }
dan1feeaed2010-07-23 15:41:47 +0000784 sqlite3DeleteTable(db, pParse->pNewTable);
drhf30a9692013-11-15 01:10:18 +0000785 sqlite3ParserReset(pParse);
drhe98c9042009-06-02 21:31:38 +0000786 sqlite3StackFree(db, pParse);
danielk19777e6ebfb2006-06-12 11:24:37 +0000787 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000788
drh4ac285a2006-09-15 07:28:50 +0000789 assert( (rc&0xff)==rc );
drh27641702007-08-22 02:56:42 +0000790 rc = sqlite3ApiExit(db, rc);
791 sqlite3_mutex_leave(db->mutex);
792 return rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000793}
794
795/*
danielk19779e39ce82006-06-12 16:01:21 +0000796** This function is invoked by the vdbe to call the xDestroy method
797** of the virtual table named zTab in database iDb. This occurs
798** when a DROP TABLE is mentioned.
799**
800** This call is a no-op if zTab is not a virtual table.
801*/
drh748ce212009-05-20 20:10:47 +0000802int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
danielk19779e39ce82006-06-12 16:01:21 +0000803 int rc = SQLITE_OK;
804 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000805
806 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk1977595a5232009-07-24 17:58:53 +0000807 if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
drha68d6282015-03-24 13:32:53 +0000808 VTable *p;
809 for(p=pTab->pVTable; p; p=p->pNext){
810 assert( p->pVtab );
811 if( p->pVtab->nRef>0 ){
812 return SQLITE_LOCKED;
813 }
814 }
815 p = vtabDisconnectAll(db, pTab);
danielk1977595a5232009-07-24 17:58:53 +0000816 rc = p->pMod->pModule->xDestroy(p->pVtab);
danielk1977595a5232009-07-24 17:58:53 +0000817 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
danielk19779e39ce82006-06-12 16:01:21 +0000818 if( rc==SQLITE_OK ){
danielk1977595a5232009-07-24 17:58:53 +0000819 assert( pTab->pVTable==p && p->pNext==0 );
820 p->pVtab = 0;
821 pTab->pVTable = 0;
822 sqlite3VtabUnlock(p);
danielk19779e39ce82006-06-12 16:01:21 +0000823 }
824 }
825
826 return rc;
827}
828
danielk1977f9e7dda2006-06-16 16:08:53 +0000829/*
danielk1977e7ff4032006-06-17 11:30:32 +0000830** This function invokes either the xRollback or xCommit method
831** of each of the virtual tables in the sqlite3.aVTrans array. The method
832** called is identified by the second argument, "offset", which is
833** the offset of the method to call in the sqlite3_module structure.
834**
835** The array is cleared after invoking the callbacks.
836*/
drh7209c692008-04-27 18:40:11 +0000837static void callFinaliser(sqlite3 *db, int offset){
danielk1977e7ff4032006-06-17 11:30:32 +0000838 int i;
danielk19770b83fa82007-04-19 14:48:37 +0000839 if( db->aVTrans ){
drh748ce212009-05-20 20:10:47 +0000840 for(i=0; i<db->nVTrans; i++){
danielk1977595a5232009-07-24 17:58:53 +0000841 VTable *pVTab = db->aVTrans[i];
842 sqlite3_vtab *p = pVTab->pVtab;
843 if( p ){
844 int (*x)(sqlite3_vtab *);
845 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
846 if( x ) x(p);
847 }
drh346506f2011-05-25 01:16:42 +0000848 pVTab->iSavepoint = 0;
danielk1977595a5232009-07-24 17:58:53 +0000849 sqlite3VtabUnlock(pVTab);
danielk19770b83fa82007-04-19 14:48:37 +0000850 }
drh633e6d52008-07-28 19:34:53 +0000851 sqlite3DbFree(db, db->aVTrans);
danielk19770b83fa82007-04-19 14:48:37 +0000852 db->nVTrans = 0;
853 db->aVTrans = 0;
danielk1977e7ff4032006-06-17 11:30:32 +0000854 }
danielk1977e7ff4032006-06-17 11:30:32 +0000855}
856
857/*
danielk19773e3a84d2008-08-01 17:37:40 +0000858** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
859** array. Return the error code for the first error that occurs, or
860** SQLITE_OK if all xSync operations are successful.
861**
dan016f7812013-08-21 17:35:48 +0000862** If an error message is available, leave it in p->zErrMsg.
danielk1977f9e7dda2006-06-16 16:08:53 +0000863*/
dan016f7812013-08-21 17:35:48 +0000864int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
danielk1977e7ff4032006-06-17 11:30:32 +0000865 int i;
866 int rc = SQLITE_OK;
danielk1977595a5232009-07-24 17:58:53 +0000867 VTable **aVTrans = db->aVTrans;
danielk197720b1eaf2006-07-26 16:22:14 +0000868
danielk197720b1eaf2006-07-26 16:22:14 +0000869 db->aVTrans = 0;
drh748ce212009-05-20 20:10:47 +0000870 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
danielk1977e7ff4032006-06-17 11:30:32 +0000871 int (*x)(sqlite3_vtab *);
danielk1977595a5232009-07-24 17:58:53 +0000872 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
873 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
danielk1977e7ff4032006-06-17 11:30:32 +0000874 rc = x(pVtab);
dan016f7812013-08-21 17:35:48 +0000875 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000876 }
877 }
danielk197720b1eaf2006-07-26 16:22:14 +0000878 db->aVTrans = aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000879 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000880}
881
882/*
883** Invoke the xRollback method of all virtual tables in the
884** sqlite3.aVTrans array. Then clear the array itself.
885*/
886int sqlite3VtabRollback(sqlite3 *db){
drh7209c692008-04-27 18:40:11 +0000887 callFinaliser(db, offsetof(sqlite3_module,xRollback));
danielk1977e7ff4032006-06-17 11:30:32 +0000888 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000889}
890
891/*
892** Invoke the xCommit method of all virtual tables in the
893** sqlite3.aVTrans array. Then clear the array itself.
894*/
895int sqlite3VtabCommit(sqlite3 *db){
drh7209c692008-04-27 18:40:11 +0000896 callFinaliser(db, offsetof(sqlite3_module,xCommit));
danielk1977e7ff4032006-06-17 11:30:32 +0000897 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000898}
899
900/*
901** If the virtual table pVtab supports the transaction interface
902** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
903** not currently open, invoke the xBegin method now.
904**
905** If the xBegin call is successful, place the sqlite3_vtab pointer
906** in the sqlite3.aVTrans array.
907*/
danielk1977595a5232009-07-24 17:58:53 +0000908int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
danielk1977f9e7dda2006-06-16 16:08:53 +0000909 int rc = SQLITE_OK;
danielk197720b1eaf2006-07-26 16:22:14 +0000910 const sqlite3_module *pModule;
911
912 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
913 ** than zero, then this function is being called from within a
914 ** virtual module xSync() callback. It is illegal to write to
drh748ce212009-05-20 20:10:47 +0000915 ** virtual module tables in this case, so return SQLITE_LOCKED.
danielk197720b1eaf2006-07-26 16:22:14 +0000916 */
danielk1977093e0f62008-11-13 18:00:14 +0000917 if( sqlite3VtabInSync(db) ){
danielk197720b1eaf2006-07-26 16:22:14 +0000918 return SQLITE_LOCKED;
919 }
danielk1977595a5232009-07-24 17:58:53 +0000920 if( !pVTab ){
danielk197720b1eaf2006-07-26 16:22:14 +0000921 return SQLITE_OK;
922 }
danielk1977595a5232009-07-24 17:58:53 +0000923 pModule = pVTab->pVtab->pModule;
danielk197720b1eaf2006-07-26 16:22:14 +0000924
danielk1977f9e7dda2006-06-16 16:08:53 +0000925 if( pModule->xBegin ){
926 int i;
927
928 /* If pVtab is already in the aVTrans array, return early */
drh748ce212009-05-20 20:10:47 +0000929 for(i=0; i<db->nVTrans; i++){
danielk1977595a5232009-07-24 17:58:53 +0000930 if( db->aVTrans[i]==pVTab ){
danielk1977f9e7dda2006-06-16 16:08:53 +0000931 return SQLITE_OK;
932 }
933 }
934
dan2cac2072011-05-25 18:46:22 +0000935 /* Invoke the xBegin method. If successful, add the vtab to the
936 ** sqlite3.aVTrans[] array. */
937 rc = growVTrans(db);
danielk19773e3a84d2008-08-01 17:37:40 +0000938 if( rc==SQLITE_OK ){
dan2cac2072011-05-25 18:46:22 +0000939 rc = pModule->xBegin(pVTab->pVtab);
940 if( rc==SQLITE_OK ){
941 addToVTrans(db, pVTab);
942 }
danielk1977f9e7dda2006-06-16 16:08:53 +0000943 }
danielk1977f9e7dda2006-06-16 16:08:53 +0000944 }
945 return rc;
946}
947
dand9495cd2011-04-27 12:08:04 +0000948/*
949** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
950** virtual tables that currently have an open transaction. Pass iSavepoint
951** as the second argument to the virtual table method invoked.
952**
953** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
954** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
955** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
956** an open transaction is invoked.
957**
958** If any virtual table method returns an error code other than SQLITE_OK,
959** processing is abandoned and the error returned to the caller of this
960** function immediately. If all calls to virtual table methods are successful,
961** SQLITE_OK is returned.
962*/
dana311b802011-04-26 19:21:34 +0000963int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
dana311b802011-04-26 19:21:34 +0000964 int rc = SQLITE_OK;
965
966 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
danea8562e2015-04-18 16:25:54 +0000967 assert( iSavepoint>=-1 );
dand9495cd2011-04-27 12:08:04 +0000968 if( db->aVTrans ){
969 int i;
970 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
drhe4855222011-05-24 15:36:01 +0000971 VTable *pVTab = db->aVTrans[i];
972 const sqlite3_module *pMod = pVTab->pMod->pModule;
dan341cade2011-10-29 11:43:04 +0000973 if( pVTab->pVtab && pMod->iVersion>=2 ){
dand9495cd2011-04-27 12:08:04 +0000974 int (*xMethod)(sqlite3_vtab *, int);
975 switch( op ){
976 case SAVEPOINT_BEGIN:
977 xMethod = pMod->xSavepoint;
drh346506f2011-05-25 01:16:42 +0000978 pVTab->iSavepoint = iSavepoint+1;
dand9495cd2011-04-27 12:08:04 +0000979 break;
980 case SAVEPOINT_ROLLBACK:
981 xMethod = pMod->xRollbackTo;
982 break;
983 default:
984 xMethod = pMod->xRelease;
985 break;
986 }
drh346506f2011-05-25 01:16:42 +0000987 if( xMethod && pVTab->iSavepoint>iSavepoint ){
dan341cade2011-10-29 11:43:04 +0000988 rc = xMethod(pVTab->pVtab, iSavepoint);
drh346506f2011-05-25 01:16:42 +0000989 }
dana311b802011-04-26 19:21:34 +0000990 }
991 }
992 }
993 return rc;
994}
995
drhb7f6f682006-07-08 17:06:43 +0000996/*
997** The first parameter (pDef) is a function implementation. The
998** second parameter (pExpr) is the first argument to this function.
999** If pExpr is a column in a virtual table, then let the virtual
1000** table implementation have an opportunity to overload the function.
1001**
1002** This routine is used to allow virtual table implementations to
1003** overload MATCH, LIKE, GLOB, and REGEXP operators.
1004**
1005** Return either the pDef argument (indicating no change) or a
1006** new FuncDef structure that is marked as ephemeral using the
1007** SQLITE_FUNC_EPHEM flag.
1008*/
1009FuncDef *sqlite3VtabOverloadFunction(
drh17435752007-08-16 04:30:38 +00001010 sqlite3 *db, /* Database connection for reporting malloc problems */
drhb7f6f682006-07-08 17:06:43 +00001011 FuncDef *pDef, /* Function to possibly overload */
1012 int nArg, /* Number of arguments to the function */
1013 Expr *pExpr /* First argument to the function */
1014){
1015 Table *pTab;
1016 sqlite3_vtab *pVtab;
1017 sqlite3_module *pMod;
drhdc5ea5c2008-12-10 17:19:59 +00001018 void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
1019 void *pArg = 0;
drhb7f6f682006-07-08 17:06:43 +00001020 FuncDef *pNew;
drh777b17a2007-09-20 10:02:54 +00001021 int rc = 0;
drha70034d2006-09-18 20:24:02 +00001022 char *zLowerName;
1023 unsigned char *z;
1024
drhb7f6f682006-07-08 17:06:43 +00001025
1026 /* Check to see the left operand is a column in a virtual table */
drh748ce212009-05-20 20:10:47 +00001027 if( NEVER(pExpr==0) ) return pDef;
drhb7f6f682006-07-08 17:06:43 +00001028 if( pExpr->op!=TK_COLUMN ) return pDef;
1029 pTab = pExpr->pTab;
drh748ce212009-05-20 20:10:47 +00001030 if( NEVER(pTab==0) ) return pDef;
drh7d10d5a2008-08-20 16:35:10 +00001031 if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
danielk1977595a5232009-07-24 17:58:53 +00001032 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
drhb7f6f682006-07-08 17:06:43 +00001033 assert( pVtab!=0 );
1034 assert( pVtab->pModule!=0 );
danielk19775bd270b2006-07-25 15:14:52 +00001035 pMod = (sqlite3_module *)pVtab->pModule;
drhb7f6f682006-07-08 17:06:43 +00001036 if( pMod->xFindFunction==0 ) return pDef;
1037
rseb72e88d2007-09-20 11:32:18 +00001038 /* Call the xFindFunction method on the virtual table implementation
drha70034d2006-09-18 20:24:02 +00001039 ** to see if the implementation wants to overload this function
1040 */
drh17435752007-08-16 04:30:38 +00001041 zLowerName = sqlite3DbStrDup(db, pDef->zName);
1042 if( zLowerName ){
1043 for(z=(unsigned char*)zLowerName; *z; z++){
1044 *z = sqlite3UpperToLower[*z];
1045 }
1046 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
drh633e6d52008-07-28 19:34:53 +00001047 sqlite3DbFree(db, zLowerName);
drha70034d2006-09-18 20:24:02 +00001048 }
drha70034d2006-09-18 20:24:02 +00001049 if( rc==0 ){
drhb7f6f682006-07-08 17:06:43 +00001050 return pDef;
1051 }
1052
1053 /* Create a new ephemeral function definition for the overloaded
1054 ** function */
drhea678832008-12-10 19:26:22 +00001055 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
danielk1977e25a50b2009-07-01 18:04:20 +00001056 + sqlite3Strlen30(pDef->zName) + 1);
drhb7f6f682006-07-08 17:06:43 +00001057 if( pNew==0 ){
1058 return pDef;
1059 }
1060 *pNew = *pDef;
danielk19778c0a7912008-08-20 14:49:23 +00001061 pNew->zName = (char *)&pNew[1];
drhea678832008-12-10 19:26:22 +00001062 memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
drhb7f6f682006-07-08 17:06:43 +00001063 pNew->xFunc = xFunc;
1064 pNew->pUserData = pArg;
drhd36e1042013-09-06 13:10:12 +00001065 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
drhb7f6f682006-07-08 17:06:43 +00001066 return pNew;
1067}
1068
drh4f3dd152008-04-28 18:46:43 +00001069/*
1070** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
1071** array so that an OP_VBegin will get generated for it. Add pTab to the
1072** array if it is missing. If pTab is already in the array, this routine
1073** is a no-op.
1074*/
1075void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
dan65a7cd12009-09-01 12:16:01 +00001076 Parse *pToplevel = sqlite3ParseToplevel(pParse);
drh4f3dd152008-04-28 18:46:43 +00001077 int i, n;
drh748ce212009-05-20 20:10:47 +00001078 Table **apVtabLock;
1079
drh4f3dd152008-04-28 18:46:43 +00001080 assert( IsVirtual(pTab) );
dan65a7cd12009-09-01 12:16:01 +00001081 for(i=0; i<pToplevel->nVtabLock; i++){
1082 if( pTab==pToplevel->apVtabLock[i] ) return;
drh4f3dd152008-04-28 18:46:43 +00001083 }
dan65a7cd12009-09-01 12:16:01 +00001084 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
drhf3cdcdc2015-04-29 16:50:28 +00001085 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
drh748ce212009-05-20 20:10:47 +00001086 if( apVtabLock ){
dan65a7cd12009-09-01 12:16:01 +00001087 pToplevel->apVtabLock = apVtabLock;
1088 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
drh344c38e2008-05-05 13:23:04 +00001089 }else{
dan65a7cd12009-09-01 12:16:01 +00001090 pToplevel->db->mallocFailed = 1;
drh4f3dd152008-04-28 18:46:43 +00001091 }
1092}
1093
drhef45bb72011-05-05 15:39:50 +00001094/*
1095** Return the ON CONFLICT resolution mode in effect for the virtual
1096** table update operation currently in progress.
1097**
1098** The results of this routine are undefined unless it is called from
1099** within an xUpdate method.
1100*/
danb061d052011-04-25 18:49:57 +00001101int sqlite3_vtab_on_conflict(sqlite3 *db){
drhef45bb72011-05-05 15:39:50 +00001102 static const unsigned char aMap[] = {
drh87f67bf2011-05-05 17:41:58 +00001103 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
danb061d052011-04-25 18:49:57 +00001104 };
drh9ca95732014-10-24 00:35:58 +00001105#ifdef SQLITE_ENABLE_API_ARMOR
1106 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
1107#endif
danb061d052011-04-25 18:49:57 +00001108 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
1109 assert( OE_Ignore==4 && OE_Replace==5 );
1110 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
drhef45bb72011-05-05 15:39:50 +00001111 return (int)aMap[db->vtabOnConflict-1];
danb061d052011-04-25 18:49:57 +00001112}
1113
drhef45bb72011-05-05 15:39:50 +00001114/*
1115** Call from within the xCreate() or xConnect() methods to provide
1116** the SQLite core with additional information about the behavior
1117** of the virtual table being implemented.
1118*/
danb061d052011-04-25 18:49:57 +00001119int sqlite3_vtab_config(sqlite3 *db, int op, ...){
1120 va_list ap;
1121 int rc = SQLITE_OK;
1122
drh9ca95732014-10-24 00:35:58 +00001123#ifdef SQLITE_ENABLE_API_ARMOR
1124 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
1125#endif
danb061d052011-04-25 18:49:57 +00001126 sqlite3_mutex_enter(db->mutex);
danb061d052011-04-25 18:49:57 +00001127 va_start(ap, op);
1128 switch( op ){
1129 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
1130 VtabCtx *p = db->pVtabCtx;
1131 if( !p ){
1132 rc = SQLITE_MISUSE_BKPT;
1133 }else{
drh367e84d2011-05-05 23:07:43 +00001134 assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
danb061d052011-04-25 18:49:57 +00001135 p->pVTable->bConstraint = (u8)va_arg(ap, int);
1136 }
1137 break;
1138 }
1139 default:
1140 rc = SQLITE_MISUSE_BKPT;
1141 break;
1142 }
1143 va_end(ap);
1144
drh13f40da2014-08-22 18:00:11 +00001145 if( rc!=SQLITE_OK ) sqlite3Error(db, rc);
danb061d052011-04-25 18:49:57 +00001146 sqlite3_mutex_leave(db->mutex);
1147 return rc;
1148}
1149
drhb9bb7c12006-06-11 23:41:55 +00001150#endif /* SQLITE_OMIT_VIRTUALTABLE */