blob: e5ebe244aa3e5e4c80b22119573819a9eee76d0f [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.
13**
danielk197733b39332006-06-24 08:51:05 +000014** $Id: vtab.c,v 1.23 2006/06/24 08:51:05 danielk1977 Exp $
drhb9bb7c12006-06-11 23:41:55 +000015*/
16#ifndef SQLITE_OMIT_VIRTUALTABLE
17#include "sqliteInt.h"
18
19/*
20** External API function used to create a new virtual-table module.
21*/
22int sqlite3_create_module(
23 sqlite3 *db, /* Database in which module is registered */
24 const char *zName, /* Name assigned to this module */
danielk1977d1ab1ba2006-06-15 04:28:13 +000025 const sqlite3_module *pModule, /* The definition of the module */
26 void *pAux /* Context pointer for xCreate/xConnect */
drhb9bb7c12006-06-11 23:41:55 +000027){
danielk1977d1ab1ba2006-06-15 04:28:13 +000028 int nName = strlen(zName);
29 Module *pMod = (Module *)sqliteMallocRaw(sizeof(Module) + nName + 1);
30 if( pMod ){
31 char *zCopy = (char *)(&pMod[1]);
32 strcpy(zCopy, zName);
33 pMod->zName = zCopy;
34 pMod->pModule = pModule;
35 pMod->pAux = pAux;
36 pMod = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
37 sqliteFree(pMod);
38 sqlite3ResetInternalSchema(db, 0);
39 }
40 return sqlite3ApiExit(db, SQLITE_OK);
drhb9bb7c12006-06-11 23:41:55 +000041}
42
drhb9bb7c12006-06-11 23:41:55 +000043/*
44** Clear any and all virtual-table information from the Table record.
45** This routine is called, for example, just before deleting the Table
46** record.
47*/
48void sqlite3VtabClear(Table *p){
danielk1977be718892006-06-23 08:05:19 +000049 sqlite3_vtab *pVtab = p->pVtab;
50 if( pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +000051 assert( p->pMod && p->pMod->pModule );
danielk1977be718892006-06-23 08:05:19 +000052 pVtab->nRef--;
53 if( pVtab->nRef==0 ){
54 pVtab->pModule->xDisconnect(pVtab);
55 }
56 p->pVtab = 0;
drhb9bb7c12006-06-11 23:41:55 +000057 }
58 if( p->azModuleArg ){
59 int i;
60 for(i=0; i<p->nModuleArg; i++){
61 sqliteFree(p->azModuleArg[i]);
62 }
63 sqliteFree(p->azModuleArg);
64 }
65}
66
67/*
68** Add a new module argument to pTable->azModuleArg[].
69** The string is not copied - the pointer is stored. The
70** string will be freed automatically when the table is
71** deleted.
72*/
73static void addModuleArgument(Table *pTable, char *zArg){
74 int i = pTable->nModuleArg++;
danielk1977b7a2f2e2006-06-23 11:34:54 +000075 int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
76 char **azModuleArg;
77 azModuleArg = sqliteRealloc(pTable->azModuleArg, nBytes);
78 if( azModuleArg==0 ){
79 int j;
80 for(j=0; j<i; j++){
81 sqliteFree(pTable->azModuleArg[j]);
82 }
drhb9bb7c12006-06-11 23:41:55 +000083 sqliteFree(zArg);
danielk1977b7a2f2e2006-06-23 11:34:54 +000084 sqliteFree(pTable->azModuleArg);
85 pTable->nModuleArg = 0;
drhb9bb7c12006-06-11 23:41:55 +000086 }else{
danielk1977b7a2f2e2006-06-23 11:34:54 +000087 azModuleArg[i] = zArg;
88 azModuleArg[i+1] = 0;
drhb9bb7c12006-06-11 23:41:55 +000089 }
danielk1977b7a2f2e2006-06-23 11:34:54 +000090 pTable->azModuleArg = azModuleArg;
drhb9bb7c12006-06-11 23:41:55 +000091}
92
93/*
94** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
95** statement. The module name has been parsed, but the optional list
96** of parameters that follow the module name are still pending.
97*/
98void sqlite3VtabBeginParse(
99 Parse *pParse, /* Parsing context */
100 Token *pName1, /* Name of new table, or database name */
101 Token *pName2, /* Name of new table or NULL */
102 Token *pModuleName /* Name of the module for the virtual table */
103){
danielk1977f1a381e2006-06-16 08:01:02 +0000104 int iDb; /* The database the table is being created in */
drhb9bb7c12006-06-11 23:41:55 +0000105 Table *pTable; /* The new virtual table */
106
danielk1977f1a381e2006-06-16 08:01:02 +0000107 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
drhb9bb7c12006-06-11 23:41:55 +0000108 pTable = pParse->pNewTable;
danielk1977f1a381e2006-06-16 08:01:02 +0000109 if( pTable==0 || pParse->nErr ) return;
110 assert( 0==pTable->pIndex );
111
danielk197770ba1642006-06-21 16:02:42 +0000112 iDb = sqlite3SchemaToIndex(pParse->db, pTable->pSchema);
113 assert( iDb>=0 );
114
drhb9bb7c12006-06-11 23:41:55 +0000115 pTable->isVirtual = 1;
116 pTable->nModuleArg = 0;
117 addModuleArgument(pTable, sqlite3NameFromToken(pModuleName));
danielk197770ba1642006-06-21 16:02:42 +0000118 addModuleArgument(pTable, sqlite3StrDup(pParse->db->aDb[iDb].zName));
119 addModuleArgument(pTable, sqlite3StrDup(pTable->zName));
drhb9bb7c12006-06-11 23:41:55 +0000120 pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
danielk1977f1a381e2006-06-16 08:01:02 +0000121
122#ifndef SQLITE_OMIT_AUTHORIZATION
123 /* Creating a virtual table invokes the authorization callback twice.
124 ** The first invocation, to obtain permission to INSERT a row into the
125 ** sqlite_master table, has already been made by sqlite3StartTable().
126 ** The second call, to obtain permission to create the table, is made now.
127 */
danielk1977be718892006-06-23 08:05:19 +0000128 if( pTable->azModuleArg ){
129 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
130 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
danielk1977f1a381e2006-06-16 08:01:02 +0000131 }
132#endif
drhb9bb7c12006-06-11 23:41:55 +0000133}
134
135/*
136** This routine takes the module argument that has been accumulating
137** in pParse->zArg[] and appends it to the list of arguments on the
138** virtual table currently under construction in pParse->pTable.
139*/
140static void addArgumentToVtab(Parse *pParse){
danielk197733b39332006-06-24 08:51:05 +0000141 if( pParse->sArg.z && pParse->pNewTable ){
142 char *z = pParse->sArg.z;
143 int n = pParse->sArg.n;
144 addModuleArgument(pParse->pNewTable, sqliteStrNDup(z, n));
drhb9bb7c12006-06-11 23:41:55 +0000145 }
drhb9bb7c12006-06-11 23:41:55 +0000146}
147
148/*
149** The parser calls this routine after the CREATE VIRTUAL TABLE statement
150** has been completely parsed.
151*/
152void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
153 Table *pTab; /* The table being constructed */
154 sqlite3 *db; /* The database connection */
155 char *zModule; /* The module name of the table: USING modulename */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000156 Module *pMod = 0;
drhb9bb7c12006-06-11 23:41:55 +0000157
158 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000159 pParse->sArg.z = 0;
drhb9bb7c12006-06-11 23:41:55 +0000160
161 /* Lookup the module name. */
162 pTab = pParse->pNewTable;
163 if( pTab==0 ) return;
164 db = pParse->db;
165 if( pTab->nModuleArg<1 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000166 zModule = pTab->azModuleArg[0];
danielk1977d1ab1ba2006-06-15 04:28:13 +0000167 pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
168 pTab->pMod = pMod;
drhb9bb7c12006-06-11 23:41:55 +0000169
170 /* If the CREATE VIRTUAL TABLE statement is being entered for the
171 ** first time (in other words if the virtual table is actually being
172 ** created now instead of just being read out of sqlite_master) then
173 ** do additional initialization work and store the statement text
174 ** in the sqlite_master table.
175 */
176 if( !db->init.busy ){
177 char *zStmt;
danielk197778efaba2006-06-12 06:09:17 +0000178 char *zWhere;
drhb9bb7c12006-06-11 23:41:55 +0000179 int iDb;
180 Vdbe *v;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000181 if( !pMod ){
danielk1977a4e76362006-06-14 06:31:28 +0000182 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
drhb9bb7c12006-06-11 23:41:55 +0000183 }
184
185 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
186 if( pEnd ){
187 pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
188 }
189 zStmt = sqlite3MPrintf("CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
190
191 /* A slot for the record has already been allocated in the
192 ** SQLITE_MASTER table. We just need to update that slot with all
danielk197778efaba2006-06-12 06:09:17 +0000193 ** the information we've collected.
194 **
195 ** The top of the stack is the rootpage allocated by sqlite3StartTable().
196 ** This value is always 0 and is ignored, a virtual table does not have a
197 ** rootpage. The next entry on the stack is the rowid of the record
198 ** in the sqlite_master table.
drhb9bb7c12006-06-11 23:41:55 +0000199 */
200 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
201 sqlite3NestedParse(pParse,
202 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000203 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
204 "WHERE rowid=#1",
drhb9bb7c12006-06-11 23:41:55 +0000205 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
206 pTab->zName,
207 pTab->zName,
208 zStmt
209 );
210 sqliteFree(zStmt);
211 v = sqlite3GetVdbe(pParse);
drhb9bb7c12006-06-11 23:41:55 +0000212 sqlite3ChangeCookie(db, v, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000213
214 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
215 zWhere = sqlite3MPrintf("name='%q'", pTab->zName);
216 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
217 sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
drhb9bb7c12006-06-11 23:41:55 +0000218 }
219
danielk197778efaba2006-06-12 06:09:17 +0000220 /* If we are rereading the sqlite_master table create the in-memory
danielk1977c7d54102006-06-15 07:29:00 +0000221 ** record of the table. If the module has already been registered,
222 ** also call the xConnect method here.
drhb9bb7c12006-06-11 23:41:55 +0000223 */
danielk197778efaba2006-06-12 06:09:17 +0000224 else {
danielk197778efaba2006-06-12 06:09:17 +0000225 Table *pOld;
226 Schema *pSchema = pTab->pSchema;
227 const char *zName = pTab->zName;
228 int nName = strlen(zName) + 1;
229 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
230 if( pOld ){
231 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
232 return;
233 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000234 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000235 }
236}
237
238/*
239** The parser calls this routine when it sees the first token
240** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
241*/
242void sqlite3VtabArgInit(Parse *pParse){
243 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000244 pParse->sArg.z = 0;
245 pParse->sArg.n = 0;
drhb9bb7c12006-06-11 23:41:55 +0000246}
247
248/*
249** The parser calls this routine for each token after the first token
250** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
251*/
252void sqlite3VtabArgExtend(Parse *pParse, Token *p){
danielk197733b39332006-06-24 08:51:05 +0000253 Token *pArg = &pParse->sArg;
254 if( pArg->z==0 ){
255 pArg->z = p->z;
256 pArg->n = p->n;
257 }else{
258 assert(pArg->z < p->z);
259 pArg->n = (p->z + p->n - pArg->z);
drhb9bb7c12006-06-11 23:41:55 +0000260 }
drhb9bb7c12006-06-11 23:41:55 +0000261}
262
danielk197778efaba2006-06-12 06:09:17 +0000263/*
danielk19779da9d472006-06-14 06:58:15 +0000264** Invoke a virtual table constructor (either xCreate or xConnect). The
265** pointer to the function to invoke is passed as the fourth parameter
266** to this procedure.
267*/
268static int vtabCallConstructor(
269 sqlite3 *db,
270 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000271 Module *pMod,
danielk19779da9d472006-06-14 06:58:15 +0000272 int (*xConstruct)(sqlite3*, void *, int, char **, sqlite3_vtab **),
273 char **pzErr
274){
275 int rc;
276 int rc2;
277 char **azArg = pTab->azModuleArg;
278 int nArg = pTab->nModuleArg;
danielk1977be718892006-06-23 08:05:19 +0000279 char *zErr = sqlite3MPrintf("vtable constructor failed: %s", pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000280
281 assert( !db->pVTab );
282 assert( xConstruct );
283
284 db->pVTab = pTab;
285 rc = sqlite3SafetyOff(db);
286 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000287 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pTab->pVtab);
danielk19779da9d472006-06-14 06:58:15 +0000288 rc2 = sqlite3SafetyOn(db);
danielk1977be718892006-06-23 08:05:19 +0000289 if( rc==SQLITE_OK && pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000290 pTab->pVtab->pModule = pMod->pModule;
danielk1977be718892006-06-23 08:05:19 +0000291 pTab->pVtab->nRef = 1;
danielk19779da9d472006-06-14 06:58:15 +0000292 }
293
294 if( SQLITE_OK!=rc ){
danielk1977be718892006-06-23 08:05:19 +0000295 *pzErr = zErr;
296 zErr = 0;
danielk19779da9d472006-06-14 06:58:15 +0000297 } else if( db->pVTab ){
298 const char *zFormat = "vtable constructor did not declare schema: %s";
299 *pzErr = sqlite3MPrintf(zFormat, pTab->zName);
300 rc = SQLITE_ERROR;
301 }
302 if( rc==SQLITE_OK ){
303 rc = rc2;
304 }
305 db->pVTab = 0;
danielk1977be718892006-06-23 08:05:19 +0000306 sqliteFree(zErr);
danielk19779da9d472006-06-14 06:58:15 +0000307 return rc;
308}
309
310/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000311** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000312** of the virtual table pTab. If an error occurs, an error code is returned
313** and an error left in pParse.
314**
315** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000316*/
317int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000318 Module *pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000319 const char *zModule;
320 int rc = SQLITE_OK;
321
danielk1977fe3fcbe22006-06-12 12:08:45 +0000322 if( !pTab || !pTab->isVirtual || pTab->pVtab ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000323 return SQLITE_OK;
324 }
325
danielk1977d1ab1ba2006-06-15 04:28:13 +0000326 pMod = pTab->pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000327 zModule = pTab->azModuleArg[0];
danielk1977d1ab1ba2006-06-15 04:28:13 +0000328 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000329 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000330 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000331 rc = SQLITE_ERROR;
332 } else {
danielk19779da9d472006-06-14 06:58:15 +0000333 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000334 sqlite3 *db = pParse->db;
335 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000336 if( rc!=SQLITE_OK ){
337 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000338 }
danielk19779da9d472006-06-14 06:58:15 +0000339 sqliteFree(zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000340 }
341
342 return rc;
343}
344
danielk19779da9d472006-06-14 06:58:15 +0000345/*
danielk1977e7ff4032006-06-17 11:30:32 +0000346** Add the virtual table pVtab to the array sqlite3.aVTrans[].
347*/
danielk1977be718892006-06-23 08:05:19 +0000348static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
danielk1977e7ff4032006-06-17 11:30:32 +0000349 const int ARRAY_INCR = 5;
350
351 /* Grow the sqlite3.aVTrans array if required */
352 if( (db->nVTrans%ARRAY_INCR)==0 ){
353 sqlite3_vtab **aVTrans;
354 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
355 aVTrans = sqliteRealloc((void *)db->aVTrans, nBytes);
356 if( !aVTrans ){
357 return SQLITE_NOMEM;
358 }
359 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
360 db->aVTrans = aVTrans;
361 }
362
363 /* Add pVtab to the end of sqlite3.aVTrans */
364 db->aVTrans[db->nVTrans++] = pVtab;
danielk1977be718892006-06-23 08:05:19 +0000365 pVtab->nRef++;
danielk1977e7ff4032006-06-17 11:30:32 +0000366 return SQLITE_OK;
367}
368
369/*
danielk19779da9d472006-06-14 06:58:15 +0000370** This function is invoked by the vdbe to call the xCreate method
371** of the virtual table named zTab in database iDb.
372**
373** If an error occurs, *pzErr is set to point an an English language
374** description of the error and an SQLITE_XXX error code is returned.
375** In this case the caller must call sqliteFree() on *pzErr.
376*/
377int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
378 int rc = SQLITE_OK;
379 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000380 Module *pMod;
danielk19779da9d472006-06-14 06:58:15 +0000381 const char *zModule;
382
383 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
384 assert(pTab && pTab->isVirtual && !pTab->pVtab);
danielk1977d1ab1ba2006-06-15 04:28:13 +0000385 pMod = pTab->pMod;
danielk19779da9d472006-06-14 06:58:15 +0000386 zModule = pTab->azModuleArg[0];
387
388 /* If the module has been registered and includes a Create method,
389 ** invoke it now. If the module has not been registered, return an
390 ** error. Otherwise, do nothing.
391 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000392 if( !pMod ){
danielk19779da9d472006-06-14 06:58:15 +0000393 *pzErr = sqlite3MPrintf("no such module: %s", zModule);
394 rc = SQLITE_ERROR;
395 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000396 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000397 }
398
danielk1977e7ff4032006-06-17 11:30:32 +0000399 if( rc==SQLITE_OK && pTab->pVtab ){
400 rc = addToVTrans(db, pTab->pVtab);
401 }
402
danielk19779da9d472006-06-14 06:58:15 +0000403 return rc;
404}
danielk1977be8a7832006-06-13 15:00:54 +0000405
406/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000407** This function is used to set the schema of a virtual table. It is only
408** valid to call this function from within the xCreate() or xConnect() of a
409** virtual table module.
410*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000411int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
412 Parse sParse;
413
414 int rc = SQLITE_OK;
415 Table *pTab = db->pVTab;
416 char *zErr = 0;
417
418 if( !pTab ){
419 sqlite3Error(db, SQLITE_MISUSE, 0);
420 return SQLITE_MISUSE;
421 }
422 assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
423
424 memset(&sParse, 0, sizeof(Parse));
425 sParse.declareVtab = 1;
426 sParse.db = db;
427
428 if(
429 SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
430 sParse.pNewTable &&
431 !sParse.pNewTable->pSelect &&
432 !sParse.pNewTable->isVirtual
433 ){
434 pTab->aCol = sParse.pNewTable->aCol;
435 pTab->nCol = sParse.pNewTable->nCol;
436 sParse.pNewTable->nCol = 0;
437 sParse.pNewTable->aCol = 0;
438 } else {
439 sqlite3Error(db, SQLITE_ERROR, zErr);
440 sqliteFree(zErr);
441 rc = SQLITE_ERROR;
442 }
443 sParse.declareVtab = 0;
444
445 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
446 sqlite3DeleteTable(0, sParse.pNewTable);
447 sParse.pNewTable = 0;
448 db->pVTab = 0;
449
450 return rc;
451}
452
453/*
danielk19779e39ce82006-06-12 16:01:21 +0000454** This function is invoked by the vdbe to call the xDestroy method
455** of the virtual table named zTab in database iDb. This occurs
456** when a DROP TABLE is mentioned.
457**
458** This call is a no-op if zTab is not a virtual table.
459*/
460int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
461{
462 int rc = SQLITE_OK;
463 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000464
465 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk19779e39ce82006-06-12 16:01:21 +0000466 assert(pTab);
467 if( pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000468 int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
danielk19779e39ce82006-06-12 16:01:21 +0000469 rc = sqlite3SafetyOff(db);
470 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000471 if( xDestroy ){
472 rc = xDestroy(pTab->pVtab);
473 }
danielk19779e39ce82006-06-12 16:01:21 +0000474 sqlite3SafetyOn(db);
475 if( rc==SQLITE_OK ){
476 pTab->pVtab = 0;
477 }
478 }
479
480 return rc;
481}
482
danielk1977f9e7dda2006-06-16 16:08:53 +0000483/*
danielk1977e7ff4032006-06-17 11:30:32 +0000484** This function invokes either the xRollback or xCommit method
485** of each of the virtual tables in the sqlite3.aVTrans array. The method
486** called is identified by the second argument, "offset", which is
487** the offset of the method to call in the sqlite3_module structure.
488**
489** The array is cleared after invoking the callbacks.
490*/
491static void callFinaliser(sqlite3 *db, int offset){
492 int i;
493 for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
494 sqlite3_vtab *pVtab = db->aVTrans[i];
495 int (*x)(sqlite3_vtab *);
496 x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
497 if( x ) x(pVtab);
danielk1977be718892006-06-23 08:05:19 +0000498 pVtab->nRef--;
499 if( pVtab->nRef==0 ){
500 pVtab->pModule->xDisconnect(pVtab);
501 }
danielk1977e7ff4032006-06-17 11:30:32 +0000502 }
503 sqliteFree(db->aVTrans);
504 db->nVTrans = 0;
505 db->aVTrans = 0;
506}
507
508/*
danielk1977f9e7dda2006-06-16 16:08:53 +0000509** If argument rc is not SQLITE_OK, then return it and do nothing.
510** Otherwise, invoke the xSync method of all virtual tables in the
511** sqlite3.aVTrans array. Return the error code for the first error
512** that occurs, or SQLITE_OK if all xSync operations are successful.
513*/
danielk1977e7ff4032006-06-17 11:30:32 +0000514int sqlite3VtabSync(sqlite3 *db, int rc2){
515 int i;
516 int rc = SQLITE_OK;
517 if( rc2!=SQLITE_OK ) return rc2;
518 for(i=0; rc==SQLITE_OK && i<db->nVTrans && db->aVTrans[i]; i++){
519 sqlite3_vtab *pVtab = db->aVTrans[i];
520 int (*x)(sqlite3_vtab *);
521 x = pVtab->pModule->xSync;
522 if( x ){
523 rc = x(pVtab);
524 }
525 }
526 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000527}
528
529/*
530** Invoke the xRollback method of all virtual tables in the
531** sqlite3.aVTrans array. Then clear the array itself.
532*/
533int sqlite3VtabRollback(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000534 callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback));
535 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000536}
537
538/*
539** Invoke the xCommit method of all virtual tables in the
540** sqlite3.aVTrans array. Then clear the array itself.
541*/
542int sqlite3VtabCommit(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000543 callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit));
544 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000545}
546
547/*
548** If the virtual table pVtab supports the transaction interface
549** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
550** not currently open, invoke the xBegin method now.
551**
552** If the xBegin call is successful, place the sqlite3_vtab pointer
553** in the sqlite3.aVTrans array.
554*/
555int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
556 int rc = SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000557 const sqlite3_module *pModule = pVtab->pModule;
558 if( pModule->xBegin ){
559 int i;
560
561 /* If pVtab is already in the aVTrans array, return early */
562 for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
563 if( db->aVTrans[i]==pVtab ){
564 return SQLITE_OK;
565 }
566 }
567
568 /* Invoke the xBegin method */
569 rc = pModule->xBegin(pVtab);
570 if( rc!=SQLITE_OK ){
571 return rc;
572 }
573
danielk1977e7ff4032006-06-17 11:30:32 +0000574 rc = addToVTrans(db, pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +0000575 }
576 return rc;
577}
578
drhb9bb7c12006-06-11 23:41:55 +0000579#endif /* SQLITE_OMIT_VIRTUALTABLE */