blob: c561f7198f866590f59885fc696b3e2ba9da5ba7 [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 {
25 Table *pTab;
26 VTable *pVTable;
27};
28
29/*
drh63842412009-04-11 16:27:19 +000030** The actual function that does the work of creating a new module.
31** This function implements the sqlite3_create_module() and
32** sqlite3_create_module_v2() interfaces.
33*/
danielk1977832a58a2007-06-22 15:21:15 +000034static int createModule(
35 sqlite3 *db, /* Database in which module is registered */
36 const char *zName, /* Name assigned to this module */
37 const sqlite3_module *pModule, /* The definition of the module */
38 void *pAux, /* Context pointer for xCreate/xConnect */
39 void (*xDestroy)(void *) /* Module destructor function */
danielk1977595a5232009-07-24 17:58:53 +000040){
drh27641702007-08-22 02:56:42 +000041 int rc, nName;
drh153c62c2007-08-24 03:51:33 +000042 Module *pMod;
drh27641702007-08-22 02:56:42 +000043
44 sqlite3_mutex_enter(db->mutex);
drhea678832008-12-10 19:26:22 +000045 nName = sqlite3Strlen30(zName);
drh153c62c2007-08-24 03:51:33 +000046 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
danielk1977832a58a2007-06-22 15:21:15 +000047 if( pMod ){
danielk197727a430c2008-06-23 17:44:18 +000048 Module *pDel;
danielk1977832a58a2007-06-22 15:21:15 +000049 char *zCopy = (char *)(&pMod[1]);
50 memcpy(zCopy, zName, nName+1);
51 pMod->zName = zCopy;
52 pMod->pModule = pModule;
53 pMod->pAux = pAux;
54 pMod->xDestroy = xDestroy;
danielk197727a430c2008-06-23 17:44:18 +000055 pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
56 if( pDel && pDel->xDestroy ){
drh7af72cf2011-05-05 13:54:28 +000057 sqlite3ResetInternalSchema(db, -1);
danielk197727a430c2008-06-23 17:44:18 +000058 pDel->xDestroy(pDel->pAux);
danielk1977832a58a2007-06-22 15:21:15 +000059 }
drh633e6d52008-07-28 19:34:53 +000060 sqlite3DbFree(db, pDel);
danielk197727a430c2008-06-23 17:44:18 +000061 if( pDel==pMod ){
62 db->mallocFailed = 1;
63 }
danielk1977777da082008-11-13 19:12:34 +000064 }else if( xDestroy ){
65 xDestroy(pAux);
danielk1977832a58a2007-06-22 15:21:15 +000066 }
drh27641702007-08-22 02:56:42 +000067 rc = sqlite3ApiExit(db, SQLITE_OK);
68 sqlite3_mutex_leave(db->mutex);
69 return rc;
danielk1977832a58a2007-06-22 15:21:15 +000070}
71
72
drhb9bb7c12006-06-11 23:41:55 +000073/*
74** External API function used to create a new virtual-table module.
75*/
76int sqlite3_create_module(
77 sqlite3 *db, /* Database in which module is registered */
78 const char *zName, /* Name assigned to this module */
danielk1977d1ab1ba2006-06-15 04:28:13 +000079 const sqlite3_module *pModule, /* The definition of the module */
80 void *pAux /* Context pointer for xCreate/xConnect */
drhb9bb7c12006-06-11 23:41:55 +000081){
danielk1977832a58a2007-06-22 15:21:15 +000082 return createModule(db, zName, pModule, pAux, 0);
83}
84
85/*
86** External API function used to create a new virtual-table module.
87*/
88int sqlite3_create_module_v2(
89 sqlite3 *db, /* Database in which module is registered */
90 const char *zName, /* Name assigned to this module */
91 const sqlite3_module *pModule, /* The definition of the module */
92 void *pAux, /* Context pointer for xCreate/xConnect */
93 void (*xDestroy)(void *) /* Module destructor function */
94){
95 return createModule(db, zName, pModule, pAux, xDestroy);
drhb9bb7c12006-06-11 23:41:55 +000096}
97
drhb9bb7c12006-06-11 23:41:55 +000098/*
drh189d4af2006-09-02 20:57:52 +000099** Lock the virtual table so that it cannot be disconnected.
100** Locks nest. Every lock should have a corresponding unlock.
101** If an unlock is omitted, resources leaks will occur.
102**
103** If a disconnect is attempted while a virtual table is locked,
104** the disconnect is deferred until all locks have been removed.
105*/
danielk1977595a5232009-07-24 17:58:53 +0000106void sqlite3VtabLock(VTable *pVTab){
107 pVTab->nRef++;
108}
109
110
111/*
112** pTab is a pointer to a Table structure representing a virtual-table.
113** Return a pointer to the VTable object used by connection db to access
114** this virtual-table, if one has been created, or NULL otherwise.
115*/
116VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
117 VTable *pVtab;
118 assert( IsVirtual(pTab) );
119 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
120 return pVtab;
drh189d4af2006-09-02 20:57:52 +0000121}
122
123/*
danielk1977595a5232009-07-24 17:58:53 +0000124** Decrement the ref-count on a virtual table object. When the ref-count
125** reaches zero, call the xDisconnect() method to delete the object.
drh189d4af2006-09-02 20:57:52 +0000126*/
danielk1977595a5232009-07-24 17:58:53 +0000127void sqlite3VtabUnlock(VTable *pVTab){
128 sqlite3 *db = pVTab->db;
129
130 assert( db );
131 assert( pVTab->nRef>0 );
drh7e8b8482008-01-23 03:03:05 +0000132 assert( sqlite3SafetyCheckOk(db) );
danielk1977595a5232009-07-24 17:58:53 +0000133
134 pVTab->nRef--;
135 if( pVTab->nRef==0 ){
136 sqlite3_vtab *p = pVTab->pVtab;
137 if( p ){
drh9978c972010-02-23 17:36:32 +0000138 p->pModule->xDisconnect(p);
danielk1977a04a34f2007-04-16 15:06:25 +0000139 }
danielk1977595a5232009-07-24 17:58:53 +0000140 sqlite3DbFree(db, pVTab);
141 }
142}
143
144/*
145** Table p is a virtual table. This function moves all elements in the
146** p->pVTable list to the sqlite3.pDisconnect lists of their associated
147** database connections to be disconnected at the next opportunity.
148** Except, if argument db is not NULL, then the entry associated with
149** connection db is left in the p->pVTable list.
150*/
151static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
152 VTable *pRet = 0;
153 VTable *pVTable = p->pVTable;
154 p->pVTable = 0;
155
156 /* Assert that the mutex (if any) associated with the BtShared database
157 ** that contains table p is held by the caller. See header comments
158 ** above function sqlite3VtabUnlockList() for an explanation of why
159 ** this makes it safe to access the sqlite3.pDisconnect list of any
drh9bbc2e22011-04-04 20:40:22 +0000160 ** database connection that may have an entry in the p->pVTable list.
161 */
162 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
danielk1977595a5232009-07-24 17:58:53 +0000163
164 while( pVTable ){
165 sqlite3 *db2 = pVTable->db;
166 VTable *pNext = pVTable->pNext;
167 assert( db2 );
168 if( db2==db ){
169 pRet = pVTable;
170 p->pVTable = pRet;
171 pRet->pNext = 0;
172 }else{
173 pVTable->pNext = db2->pDisconnect;
174 db2->pDisconnect = pVTable;
175 }
176 pVTable = pNext;
177 }
178
179 assert( !db || pRet );
180 return pRet;
181}
182
183
184/*
185** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
186**
187** This function may only be called when the mutexes associated with all
188** shared b-tree databases opened using connection db are held by the
189** caller. This is done to protect the sqlite3.pDisconnect list. The
190** sqlite3.pDisconnect list is accessed only as follows:
191**
192** 1) By this function. In this case, all BtShared mutexes and the mutex
193** associated with the database handle itself must be held.
194**
195** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
196** the sqlite3.pDisconnect list. In this case either the BtShared mutex
197** associated with the database the virtual table is stored in is held
198** or, if the virtual table is stored in a non-sharable database, then
199** the database handle mutex is held.
200**
201** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
202** by multiple threads. It is thread-safe.
203*/
204void sqlite3VtabUnlockList(sqlite3 *db){
205 VTable *p = db->pDisconnect;
206 db->pDisconnect = 0;
207
208 assert( sqlite3BtreeHoldsAllMutexes(db) );
209 assert( sqlite3_mutex_held(db->mutex) );
210
211 if( p ){
212 sqlite3ExpirePreparedStatements(db);
213 do {
214 VTable *pNext = p->pNext;
215 sqlite3VtabUnlock(p);
216 p = pNext;
217 }while( p );
drh189d4af2006-09-02 20:57:52 +0000218 }
219}
220
221/*
drhb9bb7c12006-06-11 23:41:55 +0000222** Clear any and all virtual-table information from the Table record.
223** This routine is called, for example, just before deleting the Table
224** record.
danielk1977595a5232009-07-24 17:58:53 +0000225**
226** Since it is a virtual-table, the Table structure contains a pointer
227** to the head of a linked list of VTable structures. Each VTable
228** structure is associated with a single sqlite3* user of the schema.
229** The reference count of the VTable structure associated with database
230** connection db is decremented immediately (which may lead to the
231** structure being xDisconnected and free). Any other VTable structures
232** in the list are moved to the sqlite3.pDisconnect list of the associated
233** database connection.
drhb9bb7c12006-06-11 23:41:55 +0000234*/
dan1feeaed2010-07-23 15:41:47 +0000235void sqlite3VtabClear(sqlite3 *db, Table *p){
dand46def72010-07-24 11:28:28 +0000236 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
drhb9bb7c12006-06-11 23:41:55 +0000237 if( p->azModuleArg ){
238 int i;
239 for(i=0; i<p->nModuleArg; i++){
dan1feeaed2010-07-23 15:41:47 +0000240 sqlite3DbFree(db, p->azModuleArg[i]);
drhb9bb7c12006-06-11 23:41:55 +0000241 }
dan1feeaed2010-07-23 15:41:47 +0000242 sqlite3DbFree(db, p->azModuleArg);
drhb9bb7c12006-06-11 23:41:55 +0000243 }
244}
245
246/*
247** Add a new module argument to pTable->azModuleArg[].
248** The string is not copied - the pointer is stored. The
249** string will be freed automatically when the table is
250** deleted.
251*/
drh17435752007-08-16 04:30:38 +0000252static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
drhb9bb7c12006-06-11 23:41:55 +0000253 int i = pTable->nModuleArg++;
danielk1977b7a2f2e2006-06-23 11:34:54 +0000254 int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
255 char **azModuleArg;
danielk197726783a52007-08-29 14:06:22 +0000256 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000257 if( azModuleArg==0 ){
258 int j;
259 for(j=0; j<i; j++){
drh633e6d52008-07-28 19:34:53 +0000260 sqlite3DbFree(db, pTable->azModuleArg[j]);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000261 }
drh633e6d52008-07-28 19:34:53 +0000262 sqlite3DbFree(db, zArg);
263 sqlite3DbFree(db, pTable->azModuleArg);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000264 pTable->nModuleArg = 0;
drhb9bb7c12006-06-11 23:41:55 +0000265 }else{
danielk1977b7a2f2e2006-06-23 11:34:54 +0000266 azModuleArg[i] = zArg;
267 azModuleArg[i+1] = 0;
drhb9bb7c12006-06-11 23:41:55 +0000268 }
danielk1977b7a2f2e2006-06-23 11:34:54 +0000269 pTable->azModuleArg = azModuleArg;
drhb9bb7c12006-06-11 23:41:55 +0000270}
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*/
277void 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 */
drhb421b892012-01-28 19:41:53 +0000281 Token *pModuleName, /* Name of the module for the virtual table */
282 int ifNotExists /* No error if the table already exists */
drhb9bb7c12006-06-11 23:41:55 +0000283){
danielk1977f1a381e2006-06-16 08:01:02 +0000284 int iDb; /* The database the table is being created in */
drhb9bb7c12006-06-11 23:41:55 +0000285 Table *pTable; /* The new virtual table */
drh17435752007-08-16 04:30:38 +0000286 sqlite3 *db; /* Database connection */
drhb9bb7c12006-06-11 23:41:55 +0000287
drhb421b892012-01-28 19:41:53 +0000288 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
drhb9bb7c12006-06-11 23:41:55 +0000289 pTable = pParse->pNewTable;
drh748ce212009-05-20 20:10:47 +0000290 if( pTable==0 ) return;
danielk1977f1a381e2006-06-16 08:01:02 +0000291 assert( 0==pTable->pIndex );
292
drh17435752007-08-16 04:30:38 +0000293 db = pParse->db;
294 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
danielk197770ba1642006-06-21 16:02:42 +0000295 assert( iDb>=0 );
296
drh7d10d5a2008-08-20 16:35:10 +0000297 pTable->tabFlags |= TF_Virtual;
drhb9bb7c12006-06-11 23:41:55 +0000298 pTable->nModuleArg = 0;
drh17435752007-08-16 04:30:38 +0000299 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
300 addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
301 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
drhb27b7f52008-12-10 18:03:45 +0000302 pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
danielk1977f1a381e2006-06-16 08:01:02 +0000303
304#ifndef SQLITE_OMIT_AUTHORIZATION
305 /* Creating a virtual table invokes the authorization callback twice.
306 ** The first invocation, to obtain permission to INSERT a row into the
307 ** sqlite_master table, has already been made by sqlite3StartTable().
308 ** The second call, to obtain permission to create the table, is made now.
309 */
danielk1977be718892006-06-23 08:05:19 +0000310 if( pTable->azModuleArg ){
311 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
312 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
danielk1977f1a381e2006-06-16 08:01:02 +0000313 }
314#endif
drhb9bb7c12006-06-11 23:41:55 +0000315}
316
317/*
318** This routine takes the module argument that has been accumulating
319** in pParse->zArg[] and appends it to the list of arguments on the
320** virtual table currently under construction in pParse->pTable.
321*/
322static void addArgumentToVtab(Parse *pParse){
drhb421b892012-01-28 19:41:53 +0000323 if( pParse->sArg.z && pParse->pNewTable ){
drh7c2d87c2006-09-02 14:16:59 +0000324 const char *z = (const char*)pParse->sArg.z;
danielk197733b39332006-06-24 08:51:05 +0000325 int n = pParse->sArg.n;
drh17435752007-08-16 04:30:38 +0000326 sqlite3 *db = pParse->db;
327 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
drhb9bb7c12006-06-11 23:41:55 +0000328 }
drhb9bb7c12006-06-11 23:41:55 +0000329}
330
331/*
332** The parser calls this routine after the CREATE VIRTUAL TABLE statement
333** has been completely parsed.
334*/
335void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
danielk1977595a5232009-07-24 17:58:53 +0000336 Table *pTab = pParse->pNewTable; /* The table being constructed */
337 sqlite3 *db = pParse->db; /* The database connection */
drhb9bb7c12006-06-11 23:41:55 +0000338
danielk1977595a5232009-07-24 17:58:53 +0000339 if( pTab==0 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000340 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000341 pParse->sArg.z = 0;
drhb9bb7c12006-06-11 23:41:55 +0000342 if( pTab->nModuleArg<1 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000343
344 /* If the CREATE VIRTUAL TABLE statement is being entered for the
345 ** first time (in other words if the virtual table is actually being
346 ** created now instead of just being read out of sqlite_master) then
347 ** do additional initialization work and store the statement text
348 ** in the sqlite_master table.
349 */
350 if( !db->init.busy ){
351 char *zStmt;
danielk197778efaba2006-06-12 06:09:17 +0000352 char *zWhere;
drhb9bb7c12006-06-11 23:41:55 +0000353 int iDb;
354 Vdbe *v;
drhb9bb7c12006-06-11 23:41:55 +0000355
356 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
357 if( pEnd ){
drhb27b7f52008-12-10 18:03:45 +0000358 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
drhb9bb7c12006-06-11 23:41:55 +0000359 }
danielk19771e536952007-08-16 10:09:01 +0000360 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
drhb9bb7c12006-06-11 23:41:55 +0000361
362 /* A slot for the record has already been allocated in the
363 ** SQLITE_MASTER table. We just need to update that slot with all
danielk197778efaba2006-06-12 06:09:17 +0000364 ** the information we've collected.
365 **
drhb7654112008-01-12 12:48:07 +0000366 ** The VM register number pParse->regRowid holds the rowid of an
367 ** entry in the sqlite_master table tht was created for this vtab
368 ** by sqlite3StartTable().
drhb9bb7c12006-06-11 23:41:55 +0000369 */
370 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
371 sqlite3NestedParse(pParse,
372 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000373 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
drhb7654112008-01-12 12:48:07 +0000374 "WHERE rowid=#%d",
drhb9bb7c12006-06-11 23:41:55 +0000375 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
376 pTab->zName,
377 pTab->zName,
drhb7654112008-01-12 12:48:07 +0000378 zStmt,
379 pParse->regRowid
drhb9bb7c12006-06-11 23:41:55 +0000380 );
drh633e6d52008-07-28 19:34:53 +0000381 sqlite3DbFree(db, zStmt);
drhb9bb7c12006-06-11 23:41:55 +0000382 v = sqlite3GetVdbe(pParse);
drh9cbf3422008-01-17 16:22:13 +0000383 sqlite3ChangeCookie(pParse, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000384
drh66a51672008-01-03 00:01:23 +0000385 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
dan39f1bcb2010-09-29 07:16:46 +0000386 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
drh5d9c9da2011-06-03 20:11:17 +0000387 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
drh66a51672008-01-03 00:01:23 +0000388 sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
drhea678832008-12-10 19:26:22 +0000389 pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
drhb9bb7c12006-06-11 23:41:55 +0000390 }
391
danielk197778efaba2006-06-12 06:09:17 +0000392 /* If we are rereading the sqlite_master table create the in-memory
danielk1977595a5232009-07-24 17:58:53 +0000393 ** record of the table. The xConnect() method is not called until
394 ** the first time the virtual table is used in an SQL statement. This
395 ** allows a schema that contains virtual tables to be loaded before
396 ** the required virtual table implementations are registered. */
danielk197778efaba2006-06-12 06:09:17 +0000397 else {
danielk197778efaba2006-06-12 06:09:17 +0000398 Table *pOld;
399 Schema *pSchema = pTab->pSchema;
400 const char *zName = pTab->zName;
drha83ccca2009-04-28 13:01:09 +0000401 int nName = sqlite3Strlen30(zName);
drh21206082011-04-04 18:22:02 +0000402 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
danielk197778efaba2006-06-12 06:09:17 +0000403 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
404 if( pOld ){
danielk1977a1644fd2007-08-29 12:31:25 +0000405 db->mallocFailed = 1;
danielk197778efaba2006-06-12 06:09:17 +0000406 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
407 return;
408 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000409 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000410 }
411}
412
413/*
414** The parser calls this routine when it sees the first token
415** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
416*/
417void sqlite3VtabArgInit(Parse *pParse){
418 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000419 pParse->sArg.z = 0;
420 pParse->sArg.n = 0;
drhb9bb7c12006-06-11 23:41:55 +0000421}
422
423/*
424** The parser calls this routine for each token after the first token
425** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
426*/
427void sqlite3VtabArgExtend(Parse *pParse, Token *p){
danielk197733b39332006-06-24 08:51:05 +0000428 Token *pArg = &pParse->sArg;
429 if( pArg->z==0 ){
430 pArg->z = p->z;
431 pArg->n = p->n;
432 }else{
433 assert(pArg->z < p->z);
drhb27b7f52008-12-10 18:03:45 +0000434 pArg->n = (int)(&p->z[p->n] - pArg->z);
drhb9bb7c12006-06-11 23:41:55 +0000435 }
drhb9bb7c12006-06-11 23:41:55 +0000436}
437
danielk197778efaba2006-06-12 06:09:17 +0000438/*
danielk19779da9d472006-06-14 06:58:15 +0000439** Invoke a virtual table constructor (either xCreate or xConnect). The
440** pointer to the function to invoke is passed as the fourth parameter
441** to this procedure.
442*/
443static int vtabCallConstructor(
444 sqlite3 *db,
445 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000446 Module *pMod,
drhe4102962006-09-11 00:34:22 +0000447 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
danielk19779da9d472006-06-14 06:58:15 +0000448 char **pzErr
449){
drh92505812012-04-26 22:47:20 +0000450 VtabCtx sCtx, *pPriorCtx;
danielk1977595a5232009-07-24 17:58:53 +0000451 VTable *pVTable;
danielk19779da9d472006-06-14 06:58:15 +0000452 int rc;
drhe4102962006-09-11 00:34:22 +0000453 const char *const*azArg = (const char *const*)pTab->azModuleArg;
danielk19779da9d472006-06-14 06:58:15 +0000454 int nArg = pTab->nModuleArg;
drh4ca8aac2006-09-10 17:31:58 +0000455 char *zErr = 0;
danielk19771e536952007-08-16 10:09:01 +0000456 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000457
danielk197701256832007-04-18 14:24:32 +0000458 if( !zModuleName ){
459 return SQLITE_NOMEM;
460 }
461
danielk1977595a5232009-07-24 17:58:53 +0000462 pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
463 if( !pVTable ){
464 sqlite3DbFree(db, zModuleName);
465 return SQLITE_NOMEM;
466 }
467 pVTable->db = db;
468 pVTable->pMod = pMod;
469
danielk1977595a5232009-07-24 17:58:53 +0000470 /* Invoke the virtual table constructor */
danb061d052011-04-25 18:49:57 +0000471 assert( &db->pVtabCtx );
472 assert( xConstruct );
473 sCtx.pTab = pTab;
474 sCtx.pVTable = pVTable;
drh92505812012-04-26 22:47:20 +0000475 pPriorCtx = db->pVtabCtx;
danb061d052011-04-25 18:49:57 +0000476 db->pVtabCtx = &sCtx;
danielk1977595a5232009-07-24 17:58:53 +0000477 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
drh92505812012-04-26 22:47:20 +0000478 db->pVtabCtx = pPriorCtx;
drh55f04f22009-05-11 23:37:59 +0000479 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
danielk19779da9d472006-06-14 06:58:15 +0000480
481 if( SQLITE_OK!=rc ){
drh4ca8aac2006-09-10 17:31:58 +0000482 if( zErr==0 ){
danielk19771e536952007-08-16 10:09:01 +0000483 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
drh4ca8aac2006-09-10 17:31:58 +0000484 }else {
danielk19771e536952007-08-16 10:09:01 +0000485 *pzErr = sqlite3MPrintf(db, "%s", zErr);
drhb9755982010-07-24 16:34:37 +0000486 sqlite3_free(zErr);
drhfe1368e2006-09-10 17:08:29 +0000487 }
danielk1977595a5232009-07-24 17:58:53 +0000488 sqlite3DbFree(db, pVTable);
489 }else if( ALWAYS(pVTable->pVtab) ){
490 /* Justification of ALWAYS(): A correct vtab constructor must allocate
491 ** the sqlite3_vtab object if successful. */
492 pVTable->pVtab->pModule = pMod->pModule;
493 pVTable->nRef = 1;
danb061d052011-04-25 18:49:57 +0000494 if( sCtx.pTab ){
danielk1977595a5232009-07-24 17:58:53 +0000495 const char *zFormat = "vtable constructor did not declare schema: %s";
496 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
497 sqlite3VtabUnlock(pVTable);
498 rc = SQLITE_ERROR;
499 }else{
500 int iCol;
501 /* If everything went according to plan, link the new VTable structure
502 ** into the linked list headed by pTab->pVTable. Then loop through the
503 ** columns of the table to see if any of them contain the token "hidden".
504 ** If so, set the Column.isHidden flag and remove the token from
505 ** the type string. */
506 pVTable->pNext = pTab->pVTable;
507 pTab->pVTable = pVTable;
danielk1977034ca142007-06-26 10:38:54 +0000508
danielk1977595a5232009-07-24 17:58:53 +0000509 for(iCol=0; iCol<pTab->nCol; iCol++){
510 char *zType = pTab->aCol[iCol].zType;
511 int nType;
512 int i = 0;
513 if( !zType ) continue;
514 nType = sqlite3Strlen30(zType);
515 if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
516 for(i=0; i<nType; i++){
517 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
518 && (zType[i+7]=='\0' || zType[i+7]==' ')
519 ){
520 i++;
521 break;
522 }
danielk1977034ca142007-06-26 10:38:54 +0000523 }
524 }
danielk1977595a5232009-07-24 17:58:53 +0000525 if( i<nType ){
526 int j;
527 int nDel = 6 + (zType[i+6] ? 1 : 0);
528 for(j=i; (j+nDel)<=nType; j++){
529 zType[j] = zType[j+nDel];
530 }
531 if( zType[i]=='\0' && i>0 ){
532 assert(zType[i-1]==' ');
533 zType[i-1] = '\0';
534 }
535 pTab->aCol[iCol].isHidden = 1;
danielk1977034ca142007-06-26 10:38:54 +0000536 }
danielk1977034ca142007-06-26 10:38:54 +0000537 }
538 }
539 }
danielk1977595a5232009-07-24 17:58:53 +0000540
541 sqlite3DbFree(db, zModuleName);
danielk19779da9d472006-06-14 06:58:15 +0000542 return rc;
543}
544
545/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000546** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000547** of the virtual table pTab. If an error occurs, an error code is returned
548** and an error left in pParse.
549**
550** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000551*/
552int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977595a5232009-07-24 17:58:53 +0000553 sqlite3 *db = pParse->db;
554 const char *zMod;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000555 Module *pMod;
danielk1977595a5232009-07-24 17:58:53 +0000556 int rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000557
drh748ce212009-05-20 20:10:47 +0000558 assert( pTab );
danielk1977595a5232009-07-24 17:58:53 +0000559 if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000560 return SQLITE_OK;
561 }
562
danielk1977595a5232009-07-24 17:58:53 +0000563 /* Locate the required virtual table module */
564 zMod = pTab->azModuleArg[0];
565 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
566
danielk1977d1ab1ba2006-06-15 04:28:13 +0000567 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000568 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000569 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000570 rc = SQLITE_ERROR;
danielk1977595a5232009-07-24 17:58:53 +0000571 }else{
danielk19779da9d472006-06-14 06:58:15 +0000572 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000573 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000574 if( rc!=SQLITE_OK ){
575 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000576 }
drh633e6d52008-07-28 19:34:53 +0000577 sqlite3DbFree(db, zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000578 }
579
580 return rc;
581}
danielk19779da9d472006-06-14 06:58:15 +0000582/*
dan2cac2072011-05-25 18:46:22 +0000583** Grow the db->aVTrans[] array so that there is room for at least one
584** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
danielk1977e7ff4032006-06-17 11:30:32 +0000585*/
dan2cac2072011-05-25 18:46:22 +0000586static int growVTrans(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000587 const int ARRAY_INCR = 5;
588
589 /* Grow the sqlite3.aVTrans array if required */
590 if( (db->nVTrans%ARRAY_INCR)==0 ){
danielk1977595a5232009-07-24 17:58:53 +0000591 VTable **aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000592 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
danielk197726783a52007-08-29 14:06:22 +0000593 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
danielk1977e7ff4032006-06-17 11:30:32 +0000594 if( !aVTrans ){
595 return SQLITE_NOMEM;
596 }
597 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
598 db->aVTrans = aVTrans;
599 }
600
dan2cac2072011-05-25 18:46:22 +0000601 return SQLITE_OK;
602}
603
604/*
605** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
606** have already been reserved using growVTrans().
607*/
608static void addToVTrans(sqlite3 *db, VTable *pVTab){
danielk1977e7ff4032006-06-17 11:30:32 +0000609 /* Add pVtab to the end of sqlite3.aVTrans */
danielk1977595a5232009-07-24 17:58:53 +0000610 db->aVTrans[db->nVTrans++] = pVTab;
611 sqlite3VtabLock(pVTab);
danielk1977e7ff4032006-06-17 11:30:32 +0000612}
613
614/*
danielk19779da9d472006-06-14 06:58:15 +0000615** This function is invoked by the vdbe to call the xCreate method
616** of the virtual table named zTab in database iDb.
617**
618** If an error occurs, *pzErr is set to point an an English language
619** description of the error and an SQLITE_XXX error code is returned.
drh633e6d52008-07-28 19:34:53 +0000620** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
danielk19779da9d472006-06-14 06:58:15 +0000621*/
622int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
623 int rc = SQLITE_OK;
624 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000625 Module *pMod;
danielk1977595a5232009-07-24 17:58:53 +0000626 const char *zMod;
danielk19779da9d472006-06-14 06:58:15 +0000627
628 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk1977595a5232009-07-24 17:58:53 +0000629 assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
630
631 /* Locate the required virtual table module */
632 zMod = pTab->azModuleArg[0];
633 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
danielk19779da9d472006-06-14 06:58:15 +0000634
635 /* If the module has been registered and includes a Create method,
636 ** invoke it now. If the module has not been registered, return an
637 ** error. Otherwise, do nothing.
638 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000639 if( !pMod ){
danielk1977595a5232009-07-24 17:58:53 +0000640 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
danielk19779da9d472006-06-14 06:58:15 +0000641 rc = SQLITE_ERROR;
642 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000643 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000644 }
645
drh748ce212009-05-20 20:10:47 +0000646 /* Justification of ALWAYS(): The xConstructor method is required to
647 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
danielk1977595a5232009-07-24 17:58:53 +0000648 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
dan2cac2072011-05-25 18:46:22 +0000649 rc = growVTrans(db);
650 if( rc==SQLITE_OK ){
651 addToVTrans(db, sqlite3GetVTable(db, pTab));
652 }
danielk1977e7ff4032006-06-17 11:30:32 +0000653 }
654
danielk19779da9d472006-06-14 06:58:15 +0000655 return rc;
656}
danielk1977be8a7832006-06-13 15:00:54 +0000657
658/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000659** This function is used to set the schema of a virtual table. It is only
660** valid to call this function from within the xCreate() or xConnect() of a
661** virtual table module.
662*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000663int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
drhe98c9042009-06-02 21:31:38 +0000664 Parse *pParse;
danielk19777e6ebfb2006-06-12 11:24:37 +0000665
666 int rc = SQLITE_OK;
drh27641702007-08-22 02:56:42 +0000667 Table *pTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000668 char *zErr = 0;
669
drh27641702007-08-22 02:56:42 +0000670 sqlite3_mutex_enter(db->mutex);
dand9495cd2011-04-27 12:08:04 +0000671 if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000672 sqlite3Error(db, SQLITE_MISUSE, 0);
drh27641702007-08-22 02:56:42 +0000673 sqlite3_mutex_leave(db->mutex);
drh413c3d32010-02-23 20:11:56 +0000674 return SQLITE_MISUSE_BKPT;
danielk19777e6ebfb2006-06-12 11:24:37 +0000675 }
danielk1977595a5232009-07-24 17:58:53 +0000676 assert( (pTab->tabFlags & TF_Virtual)!=0 );
danielk19777e6ebfb2006-06-12 11:24:37 +0000677
drhe98c9042009-06-02 21:31:38 +0000678 pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
679 if( pParse==0 ){
680 rc = SQLITE_NOMEM;
681 }else{
682 pParse->declareVtab = 1;
683 pParse->db = db;
drh8b307fb2010-04-06 15:57:05 +0000684 pParse->nQueryLoop = 1;
drhe98c9042009-06-02 21:31:38 +0000685
dan84db21e2009-12-08 19:05:53 +0000686 if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
dan84db21e2009-12-08 19:05:53 +0000687 && pParse->pNewTable
drh0e3c5122009-12-08 22:16:15 +0000688 && !db->mallocFailed
dan84db21e2009-12-08 19:05:53 +0000689 && !pParse->pNewTable->pSelect
690 && (pParse->pNewTable->tabFlags & TF_Virtual)==0
drhe98c9042009-06-02 21:31:38 +0000691 ){
danielk1977595a5232009-07-24 17:58:53 +0000692 if( !pTab->aCol ){
693 pTab->aCol = pParse->pNewTable->aCol;
694 pTab->nCol = pParse->pNewTable->nCol;
695 pParse->pNewTable->nCol = 0;
696 pParse->pNewTable->aCol = 0;
697 }
danb061d052011-04-25 18:49:57 +0000698 db->pVtabCtx->pTab = 0;
dan84db21e2009-12-08 19:05:53 +0000699 }else{
dan06b5db02010-10-21 15:12:44 +0000700 sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
dan78f9b732010-08-11 11:59:37 +0000701 sqlite3DbFree(db, zErr);
drhe98c9042009-06-02 21:31:38 +0000702 rc = SQLITE_ERROR;
703 }
704 pParse->declareVtab = 0;
705
706 if( pParse->pVdbe ){
707 sqlite3VdbeFinalize(pParse->pVdbe);
708 }
dan1feeaed2010-07-23 15:41:47 +0000709 sqlite3DeleteTable(db, pParse->pNewTable);
drhe98c9042009-06-02 21:31:38 +0000710 sqlite3StackFree(db, pParse);
danielk19777e6ebfb2006-06-12 11:24:37 +0000711 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000712
drh4ac285a2006-09-15 07:28:50 +0000713 assert( (rc&0xff)==rc );
drh27641702007-08-22 02:56:42 +0000714 rc = sqlite3ApiExit(db, rc);
715 sqlite3_mutex_leave(db->mutex);
716 return rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000717}
718
719/*
danielk19779e39ce82006-06-12 16:01:21 +0000720** This function is invoked by the vdbe to call the xDestroy method
721** of the virtual table named zTab in database iDb. This occurs
722** when a DROP TABLE is mentioned.
723**
724** This call is a no-op if zTab is not a virtual table.
725*/
drh748ce212009-05-20 20:10:47 +0000726int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
danielk19779e39ce82006-06-12 16:01:21 +0000727 int rc = SQLITE_OK;
728 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000729
730 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk1977595a5232009-07-24 17:58:53 +0000731 if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
732 VTable *p = vtabDisconnectAll(db, pTab);
733
danielk19779e39ce82006-06-12 16:01:21 +0000734 assert( rc==SQLITE_OK );
danielk1977595a5232009-07-24 17:58:53 +0000735 rc = p->pMod->pModule->xDestroy(p->pVtab);
danielk1977595a5232009-07-24 17:58:53 +0000736
737 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
danielk19779e39ce82006-06-12 16:01:21 +0000738 if( rc==SQLITE_OK ){
danielk1977595a5232009-07-24 17:58:53 +0000739 assert( pTab->pVTable==p && p->pNext==0 );
740 p->pVtab = 0;
741 pTab->pVTable = 0;
742 sqlite3VtabUnlock(p);
danielk19779e39ce82006-06-12 16:01:21 +0000743 }
744 }
745
746 return rc;
747}
748
danielk1977f9e7dda2006-06-16 16:08:53 +0000749/*
danielk1977e7ff4032006-06-17 11:30:32 +0000750** This function invokes either the xRollback or xCommit method
751** of each of the virtual tables in the sqlite3.aVTrans array. The method
752** called is identified by the second argument, "offset", which is
753** the offset of the method to call in the sqlite3_module structure.
754**
755** The array is cleared after invoking the callbacks.
756*/
drh7209c692008-04-27 18:40:11 +0000757static void callFinaliser(sqlite3 *db, int offset){
danielk1977e7ff4032006-06-17 11:30:32 +0000758 int i;
danielk19770b83fa82007-04-19 14:48:37 +0000759 if( db->aVTrans ){
drh748ce212009-05-20 20:10:47 +0000760 for(i=0; i<db->nVTrans; i++){
danielk1977595a5232009-07-24 17:58:53 +0000761 VTable *pVTab = db->aVTrans[i];
762 sqlite3_vtab *p = pVTab->pVtab;
763 if( p ){
764 int (*x)(sqlite3_vtab *);
765 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
766 if( x ) x(p);
767 }
drh346506f2011-05-25 01:16:42 +0000768 pVTab->iSavepoint = 0;
danielk1977595a5232009-07-24 17:58:53 +0000769 sqlite3VtabUnlock(pVTab);
danielk19770b83fa82007-04-19 14:48:37 +0000770 }
drh633e6d52008-07-28 19:34:53 +0000771 sqlite3DbFree(db, db->aVTrans);
danielk19770b83fa82007-04-19 14:48:37 +0000772 db->nVTrans = 0;
773 db->aVTrans = 0;
danielk1977e7ff4032006-06-17 11:30:32 +0000774 }
danielk1977e7ff4032006-06-17 11:30:32 +0000775}
776
777/*
danielk19773e3a84d2008-08-01 17:37:40 +0000778** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
779** array. Return the error code for the first error that occurs, or
780** SQLITE_OK if all xSync operations are successful.
781**
782** Set *pzErrmsg to point to a buffer that should be released using
783** sqlite3DbFree() containing an error message, if one is available.
danielk1977f9e7dda2006-06-16 16:08:53 +0000784*/
danielk19773e3a84d2008-08-01 17:37:40 +0000785int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
danielk1977e7ff4032006-06-17 11:30:32 +0000786 int i;
787 int rc = SQLITE_OK;
danielk1977595a5232009-07-24 17:58:53 +0000788 VTable **aVTrans = db->aVTrans;
danielk197720b1eaf2006-07-26 16:22:14 +0000789
danielk197720b1eaf2006-07-26 16:22:14 +0000790 db->aVTrans = 0;
drh748ce212009-05-20 20:10:47 +0000791 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
danielk1977e7ff4032006-06-17 11:30:32 +0000792 int (*x)(sqlite3_vtab *);
danielk1977595a5232009-07-24 17:58:53 +0000793 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
794 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
danielk1977e7ff4032006-06-17 11:30:32 +0000795 rc = x(pVtab);
danielk19773e3a84d2008-08-01 17:37:40 +0000796 sqlite3DbFree(db, *pzErrmsg);
drhb9755982010-07-24 16:34:37 +0000797 *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
798 sqlite3_free(pVtab->zErrMsg);
danielk1977e7ff4032006-06-17 11:30:32 +0000799 }
800 }
danielk197720b1eaf2006-07-26 16:22:14 +0000801 db->aVTrans = aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000802 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000803}
804
805/*
806** Invoke the xRollback method of all virtual tables in the
807** sqlite3.aVTrans array. Then clear the array itself.
808*/
809int sqlite3VtabRollback(sqlite3 *db){
drh7209c692008-04-27 18:40:11 +0000810 callFinaliser(db, offsetof(sqlite3_module,xRollback));
danielk1977e7ff4032006-06-17 11:30:32 +0000811 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000812}
813
814/*
815** Invoke the xCommit method of all virtual tables in the
816** sqlite3.aVTrans array. Then clear the array itself.
817*/
818int sqlite3VtabCommit(sqlite3 *db){
drh7209c692008-04-27 18:40:11 +0000819 callFinaliser(db, offsetof(sqlite3_module,xCommit));
danielk1977e7ff4032006-06-17 11:30:32 +0000820 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000821}
822
823/*
824** If the virtual table pVtab supports the transaction interface
825** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
826** not currently open, invoke the xBegin method now.
827**
828** If the xBegin call is successful, place the sqlite3_vtab pointer
829** in the sqlite3.aVTrans array.
830*/
danielk1977595a5232009-07-24 17:58:53 +0000831int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
danielk1977f9e7dda2006-06-16 16:08:53 +0000832 int rc = SQLITE_OK;
danielk197720b1eaf2006-07-26 16:22:14 +0000833 const sqlite3_module *pModule;
834
835 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
836 ** than zero, then this function is being called from within a
837 ** virtual module xSync() callback. It is illegal to write to
drh748ce212009-05-20 20:10:47 +0000838 ** virtual module tables in this case, so return SQLITE_LOCKED.
danielk197720b1eaf2006-07-26 16:22:14 +0000839 */
danielk1977093e0f62008-11-13 18:00:14 +0000840 if( sqlite3VtabInSync(db) ){
danielk197720b1eaf2006-07-26 16:22:14 +0000841 return SQLITE_LOCKED;
842 }
danielk1977595a5232009-07-24 17:58:53 +0000843 if( !pVTab ){
danielk197720b1eaf2006-07-26 16:22:14 +0000844 return SQLITE_OK;
845 }
danielk1977595a5232009-07-24 17:58:53 +0000846 pModule = pVTab->pVtab->pModule;
danielk197720b1eaf2006-07-26 16:22:14 +0000847
danielk1977f9e7dda2006-06-16 16:08:53 +0000848 if( pModule->xBegin ){
849 int i;
850
851 /* If pVtab is already in the aVTrans array, return early */
drh748ce212009-05-20 20:10:47 +0000852 for(i=0; i<db->nVTrans; i++){
danielk1977595a5232009-07-24 17:58:53 +0000853 if( db->aVTrans[i]==pVTab ){
danielk1977f9e7dda2006-06-16 16:08:53 +0000854 return SQLITE_OK;
855 }
856 }
857
dan2cac2072011-05-25 18:46:22 +0000858 /* Invoke the xBegin method. If successful, add the vtab to the
859 ** sqlite3.aVTrans[] array. */
860 rc = growVTrans(db);
danielk19773e3a84d2008-08-01 17:37:40 +0000861 if( rc==SQLITE_OK ){
dan2cac2072011-05-25 18:46:22 +0000862 rc = pModule->xBegin(pVTab->pVtab);
863 if( rc==SQLITE_OK ){
864 addToVTrans(db, pVTab);
865 }
danielk1977f9e7dda2006-06-16 16:08:53 +0000866 }
danielk1977f9e7dda2006-06-16 16:08:53 +0000867 }
868 return rc;
869}
870
dand9495cd2011-04-27 12:08:04 +0000871/*
872** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
873** virtual tables that currently have an open transaction. Pass iSavepoint
874** as the second argument to the virtual table method invoked.
875**
876** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
877** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
878** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
879** an open transaction is invoked.
880**
881** If any virtual table method returns an error code other than SQLITE_OK,
882** processing is abandoned and the error returned to the caller of this
883** function immediately. If all calls to virtual table methods are successful,
884** SQLITE_OK is returned.
885*/
dana311b802011-04-26 19:21:34 +0000886int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
dana311b802011-04-26 19:21:34 +0000887 int rc = SQLITE_OK;
888
889 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
drh346506f2011-05-25 01:16:42 +0000890 assert( iSavepoint>=0 );
dand9495cd2011-04-27 12:08:04 +0000891 if( db->aVTrans ){
892 int i;
893 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
drhe4855222011-05-24 15:36:01 +0000894 VTable *pVTab = db->aVTrans[i];
895 const sqlite3_module *pMod = pVTab->pMod->pModule;
dan341cade2011-10-29 11:43:04 +0000896 if( pVTab->pVtab && pMod->iVersion>=2 ){
dand9495cd2011-04-27 12:08:04 +0000897 int (*xMethod)(sqlite3_vtab *, int);
898 switch( op ){
899 case SAVEPOINT_BEGIN:
900 xMethod = pMod->xSavepoint;
drh346506f2011-05-25 01:16:42 +0000901 pVTab->iSavepoint = iSavepoint+1;
dand9495cd2011-04-27 12:08:04 +0000902 break;
903 case SAVEPOINT_ROLLBACK:
904 xMethod = pMod->xRollbackTo;
905 break;
906 default:
907 xMethod = pMod->xRelease;
908 break;
909 }
drh346506f2011-05-25 01:16:42 +0000910 if( xMethod && pVTab->iSavepoint>iSavepoint ){
dan341cade2011-10-29 11:43:04 +0000911 rc = xMethod(pVTab->pVtab, iSavepoint);
drh346506f2011-05-25 01:16:42 +0000912 }
dana311b802011-04-26 19:21:34 +0000913 }
914 }
915 }
916 return rc;
917}
918
drhb7f6f682006-07-08 17:06:43 +0000919/*
920** The first parameter (pDef) is a function implementation. The
921** second parameter (pExpr) is the first argument to this function.
922** If pExpr is a column in a virtual table, then let the virtual
923** table implementation have an opportunity to overload the function.
924**
925** This routine is used to allow virtual table implementations to
926** overload MATCH, LIKE, GLOB, and REGEXP operators.
927**
928** Return either the pDef argument (indicating no change) or a
929** new FuncDef structure that is marked as ephemeral using the
930** SQLITE_FUNC_EPHEM flag.
931*/
932FuncDef *sqlite3VtabOverloadFunction(
drh17435752007-08-16 04:30:38 +0000933 sqlite3 *db, /* Database connection for reporting malloc problems */
drhb7f6f682006-07-08 17:06:43 +0000934 FuncDef *pDef, /* Function to possibly overload */
935 int nArg, /* Number of arguments to the function */
936 Expr *pExpr /* First argument to the function */
937){
938 Table *pTab;
939 sqlite3_vtab *pVtab;
940 sqlite3_module *pMod;
drhdc5ea5c2008-12-10 17:19:59 +0000941 void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
942 void *pArg = 0;
drhb7f6f682006-07-08 17:06:43 +0000943 FuncDef *pNew;
drh777b17a2007-09-20 10:02:54 +0000944 int rc = 0;
drha70034d2006-09-18 20:24:02 +0000945 char *zLowerName;
946 unsigned char *z;
947
drhb7f6f682006-07-08 17:06:43 +0000948
949 /* Check to see the left operand is a column in a virtual table */
drh748ce212009-05-20 20:10:47 +0000950 if( NEVER(pExpr==0) ) return pDef;
drhb7f6f682006-07-08 17:06:43 +0000951 if( pExpr->op!=TK_COLUMN ) return pDef;
952 pTab = pExpr->pTab;
drh748ce212009-05-20 20:10:47 +0000953 if( NEVER(pTab==0) ) return pDef;
drh7d10d5a2008-08-20 16:35:10 +0000954 if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
danielk1977595a5232009-07-24 17:58:53 +0000955 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
drhb7f6f682006-07-08 17:06:43 +0000956 assert( pVtab!=0 );
957 assert( pVtab->pModule!=0 );
danielk19775bd270b2006-07-25 15:14:52 +0000958 pMod = (sqlite3_module *)pVtab->pModule;
drhb7f6f682006-07-08 17:06:43 +0000959 if( pMod->xFindFunction==0 ) return pDef;
960
rseb72e88d2007-09-20 11:32:18 +0000961 /* Call the xFindFunction method on the virtual table implementation
drha70034d2006-09-18 20:24:02 +0000962 ** to see if the implementation wants to overload this function
963 */
drh17435752007-08-16 04:30:38 +0000964 zLowerName = sqlite3DbStrDup(db, pDef->zName);
965 if( zLowerName ){
966 for(z=(unsigned char*)zLowerName; *z; z++){
967 *z = sqlite3UpperToLower[*z];
968 }
969 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
drh633e6d52008-07-28 19:34:53 +0000970 sqlite3DbFree(db, zLowerName);
drha70034d2006-09-18 20:24:02 +0000971 }
drha70034d2006-09-18 20:24:02 +0000972 if( rc==0 ){
drhb7f6f682006-07-08 17:06:43 +0000973 return pDef;
974 }
975
976 /* Create a new ephemeral function definition for the overloaded
977 ** function */
drhea678832008-12-10 19:26:22 +0000978 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
danielk1977e25a50b2009-07-01 18:04:20 +0000979 + sqlite3Strlen30(pDef->zName) + 1);
drhb7f6f682006-07-08 17:06:43 +0000980 if( pNew==0 ){
981 return pDef;
982 }
983 *pNew = *pDef;
danielk19778c0a7912008-08-20 14:49:23 +0000984 pNew->zName = (char *)&pNew[1];
drhea678832008-12-10 19:26:22 +0000985 memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
drhb7f6f682006-07-08 17:06:43 +0000986 pNew->xFunc = xFunc;
987 pNew->pUserData = pArg;
drhb7f6f682006-07-08 17:06:43 +0000988 pNew->flags |= SQLITE_FUNC_EPHEM;
989 return pNew;
990}
991
drh4f3dd152008-04-28 18:46:43 +0000992/*
993** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
994** array so that an OP_VBegin will get generated for it. Add pTab to the
995** array if it is missing. If pTab is already in the array, this routine
996** is a no-op.
997*/
998void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
dan65a7cd12009-09-01 12:16:01 +0000999 Parse *pToplevel = sqlite3ParseToplevel(pParse);
drh4f3dd152008-04-28 18:46:43 +00001000 int i, n;
drh748ce212009-05-20 20:10:47 +00001001 Table **apVtabLock;
1002
drh4f3dd152008-04-28 18:46:43 +00001003 assert( IsVirtual(pTab) );
dan65a7cd12009-09-01 12:16:01 +00001004 for(i=0; i<pToplevel->nVtabLock; i++){
1005 if( pTab==pToplevel->apVtabLock[i] ) return;
drh4f3dd152008-04-28 18:46:43 +00001006 }
dan65a7cd12009-09-01 12:16:01 +00001007 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
1008 apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
drh748ce212009-05-20 20:10:47 +00001009 if( apVtabLock ){
dan65a7cd12009-09-01 12:16:01 +00001010 pToplevel->apVtabLock = apVtabLock;
1011 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
drh344c38e2008-05-05 13:23:04 +00001012 }else{
dan65a7cd12009-09-01 12:16:01 +00001013 pToplevel->db->mallocFailed = 1;
drh4f3dd152008-04-28 18:46:43 +00001014 }
1015}
1016
drhef45bb72011-05-05 15:39:50 +00001017/*
1018** Return the ON CONFLICT resolution mode in effect for the virtual
1019** table update operation currently in progress.
1020**
1021** The results of this routine are undefined unless it is called from
1022** within an xUpdate method.
1023*/
danb061d052011-04-25 18:49:57 +00001024int sqlite3_vtab_on_conflict(sqlite3 *db){
drhef45bb72011-05-05 15:39:50 +00001025 static const unsigned char aMap[] = {
drh87f67bf2011-05-05 17:41:58 +00001026 SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
danb061d052011-04-25 18:49:57 +00001027 };
1028 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
1029 assert( OE_Ignore==4 && OE_Replace==5 );
1030 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
drhef45bb72011-05-05 15:39:50 +00001031 return (int)aMap[db->vtabOnConflict-1];
danb061d052011-04-25 18:49:57 +00001032}
1033
drhef45bb72011-05-05 15:39:50 +00001034/*
1035** Call from within the xCreate() or xConnect() methods to provide
1036** the SQLite core with additional information about the behavior
1037** of the virtual table being implemented.
1038*/
danb061d052011-04-25 18:49:57 +00001039int sqlite3_vtab_config(sqlite3 *db, int op, ...){
1040 va_list ap;
1041 int rc = SQLITE_OK;
1042
1043 sqlite3_mutex_enter(db->mutex);
1044
1045 va_start(ap, op);
1046 switch( op ){
1047 case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
1048 VtabCtx *p = db->pVtabCtx;
1049 if( !p ){
1050 rc = SQLITE_MISUSE_BKPT;
1051 }else{
drh367e84d2011-05-05 23:07:43 +00001052 assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
danb061d052011-04-25 18:49:57 +00001053 p->pVTable->bConstraint = (u8)va_arg(ap, int);
1054 }
1055 break;
1056 }
1057 default:
1058 rc = SQLITE_MISUSE_BKPT;
1059 break;
1060 }
1061 va_end(ap);
1062
1063 if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
1064 sqlite3_mutex_leave(db->mutex);
1065 return rc;
1066}
1067
drhb9bb7c12006-06-11 23:41:55 +00001068#endif /* SQLITE_OMIT_VIRTUALTABLE */