blob: be6b98823ee0cf5a2400cea57c9ed210e023725e [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**
drh7c2d87c2006-09-02 14:16:59 +000014** $Id: vtab.c,v 1.30 2006/09/02 14:17:00 drh 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 ){
drh7c2d87c2006-09-02 14:16:59 +0000142 const char *z = (const char*)pParse->sArg.z;
danielk197733b39332006-06-24 08:51:05 +0000143 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;
drhb9bb7c12006-06-11 23:41:55 +0000181
182 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
183 if( pEnd ){
184 pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
185 }
186 zStmt = sqlite3MPrintf("CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
187
188 /* A slot for the record has already been allocated in the
189 ** SQLITE_MASTER table. We just need to update that slot with all
danielk197778efaba2006-06-12 06:09:17 +0000190 ** the information we've collected.
191 **
192 ** The top of the stack is the rootpage allocated by sqlite3StartTable().
193 ** This value is always 0 and is ignored, a virtual table does not have a
194 ** rootpage. The next entry on the stack is the rowid of the record
195 ** in the sqlite_master table.
drhb9bb7c12006-06-11 23:41:55 +0000196 */
197 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
198 sqlite3NestedParse(pParse,
199 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000200 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
201 "WHERE rowid=#1",
drhb9bb7c12006-06-11 23:41:55 +0000202 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
203 pTab->zName,
204 pTab->zName,
205 zStmt
206 );
207 sqliteFree(zStmt);
208 v = sqlite3GetVdbe(pParse);
drhb9bb7c12006-06-11 23:41:55 +0000209 sqlite3ChangeCookie(db, v, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000210
211 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
212 zWhere = sqlite3MPrintf("name='%q'", pTab->zName);
213 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
214 sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
drhb9bb7c12006-06-11 23:41:55 +0000215 }
216
danielk197778efaba2006-06-12 06:09:17 +0000217 /* If we are rereading the sqlite_master table create the in-memory
danielk1977c7d54102006-06-15 07:29:00 +0000218 ** record of the table. If the module has already been registered,
219 ** also call the xConnect method here.
drhb9bb7c12006-06-11 23:41:55 +0000220 */
danielk197778efaba2006-06-12 06:09:17 +0000221 else {
danielk197778efaba2006-06-12 06:09:17 +0000222 Table *pOld;
223 Schema *pSchema = pTab->pSchema;
224 const char *zName = pTab->zName;
225 int nName = strlen(zName) + 1;
226 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
227 if( pOld ){
228 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
229 return;
230 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000231 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000232 }
233}
234
235/*
236** The parser calls this routine when it sees the first token
237** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
238*/
239void sqlite3VtabArgInit(Parse *pParse){
240 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000241 pParse->sArg.z = 0;
242 pParse->sArg.n = 0;
drhb9bb7c12006-06-11 23:41:55 +0000243}
244
245/*
246** The parser calls this routine for each token after the first token
247** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
248*/
249void sqlite3VtabArgExtend(Parse *pParse, Token *p){
danielk197733b39332006-06-24 08:51:05 +0000250 Token *pArg = &pParse->sArg;
251 if( pArg->z==0 ){
252 pArg->z = p->z;
253 pArg->n = p->n;
254 }else{
255 assert(pArg->z < p->z);
256 pArg->n = (p->z + p->n - pArg->z);
drhb9bb7c12006-06-11 23:41:55 +0000257 }
drhb9bb7c12006-06-11 23:41:55 +0000258}
259
danielk197778efaba2006-06-12 06:09:17 +0000260/*
danielk19779da9d472006-06-14 06:58:15 +0000261** Invoke a virtual table constructor (either xCreate or xConnect). The
262** pointer to the function to invoke is passed as the fourth parameter
263** to this procedure.
264*/
265static int vtabCallConstructor(
266 sqlite3 *db,
267 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000268 Module *pMod,
danielk19779da9d472006-06-14 06:58:15 +0000269 int (*xConstruct)(sqlite3*, void *, int, char **, sqlite3_vtab **),
270 char **pzErr
271){
272 int rc;
273 int rc2;
274 char **azArg = pTab->azModuleArg;
275 int nArg = pTab->nModuleArg;
danielk1977be718892006-06-23 08:05:19 +0000276 char *zErr = sqlite3MPrintf("vtable constructor failed: %s", pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000277
278 assert( !db->pVTab );
279 assert( xConstruct );
280
281 db->pVTab = pTab;
282 rc = sqlite3SafetyOff(db);
283 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000284 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pTab->pVtab);
danielk19779da9d472006-06-14 06:58:15 +0000285 rc2 = sqlite3SafetyOn(db);
danielk1977be718892006-06-23 08:05:19 +0000286 if( rc==SQLITE_OK && pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000287 pTab->pVtab->pModule = pMod->pModule;
danielk1977be718892006-06-23 08:05:19 +0000288 pTab->pVtab->nRef = 1;
danielk19779da9d472006-06-14 06:58:15 +0000289 }
290
291 if( SQLITE_OK!=rc ){
danielk1977be718892006-06-23 08:05:19 +0000292 *pzErr = zErr;
293 zErr = 0;
danielk19779da9d472006-06-14 06:58:15 +0000294 } else if( db->pVTab ){
295 const char *zFormat = "vtable constructor did not declare schema: %s";
296 *pzErr = sqlite3MPrintf(zFormat, pTab->zName);
297 rc = SQLITE_ERROR;
298 }
299 if( rc==SQLITE_OK ){
300 rc = rc2;
301 }
302 db->pVTab = 0;
danielk1977be718892006-06-23 08:05:19 +0000303 sqliteFree(zErr);
danielk19779da9d472006-06-14 06:58:15 +0000304 return rc;
305}
306
307/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000308** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000309** of the virtual table pTab. If an error occurs, an error code is returned
310** and an error left in pParse.
311**
312** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000313*/
314int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000315 Module *pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000316 const char *zModule;
317 int rc = SQLITE_OK;
318
danielk1977fe3fcbe22006-06-12 12:08:45 +0000319 if( !pTab || !pTab->isVirtual || pTab->pVtab ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000320 return SQLITE_OK;
321 }
322
danielk1977d1ab1ba2006-06-15 04:28:13 +0000323 pMod = pTab->pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000324 zModule = pTab->azModuleArg[0];
danielk1977d1ab1ba2006-06-15 04:28:13 +0000325 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000326 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000327 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000328 rc = SQLITE_ERROR;
329 } else {
danielk19779da9d472006-06-14 06:58:15 +0000330 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000331 sqlite3 *db = pParse->db;
332 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000333 if( rc!=SQLITE_OK ){
334 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000335 }
danielk19779da9d472006-06-14 06:58:15 +0000336 sqliteFree(zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000337 }
338
339 return rc;
340}
341
danielk19779da9d472006-06-14 06:58:15 +0000342/*
danielk1977e7ff4032006-06-17 11:30:32 +0000343** Add the virtual table pVtab to the array sqlite3.aVTrans[].
344*/
danielk1977be718892006-06-23 08:05:19 +0000345static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
danielk1977e7ff4032006-06-17 11:30:32 +0000346 const int ARRAY_INCR = 5;
347
348 /* Grow the sqlite3.aVTrans array if required */
349 if( (db->nVTrans%ARRAY_INCR)==0 ){
350 sqlite3_vtab **aVTrans;
351 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
352 aVTrans = sqliteRealloc((void *)db->aVTrans, nBytes);
353 if( !aVTrans ){
354 return SQLITE_NOMEM;
355 }
356 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
357 db->aVTrans = aVTrans;
358 }
359
360 /* Add pVtab to the end of sqlite3.aVTrans */
361 db->aVTrans[db->nVTrans++] = pVtab;
danielk1977be718892006-06-23 08:05:19 +0000362 pVtab->nRef++;
danielk1977e7ff4032006-06-17 11:30:32 +0000363 return SQLITE_OK;
364}
365
366/*
danielk19779da9d472006-06-14 06:58:15 +0000367** This function is invoked by the vdbe to call the xCreate method
368** of the virtual table named zTab in database iDb.
369**
370** If an error occurs, *pzErr is set to point an an English language
371** description of the error and an SQLITE_XXX error code is returned.
372** In this case the caller must call sqliteFree() on *pzErr.
373*/
374int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
375 int rc = SQLITE_OK;
376 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000377 Module *pMod;
danielk19779da9d472006-06-14 06:58:15 +0000378 const char *zModule;
379
380 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
381 assert(pTab && pTab->isVirtual && !pTab->pVtab);
danielk1977d1ab1ba2006-06-15 04:28:13 +0000382 pMod = pTab->pMod;
danielk19779da9d472006-06-14 06:58:15 +0000383 zModule = pTab->azModuleArg[0];
384
385 /* If the module has been registered and includes a Create method,
386 ** invoke it now. If the module has not been registered, return an
387 ** error. Otherwise, do nothing.
388 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000389 if( !pMod ){
danielk19779da9d472006-06-14 06:58:15 +0000390 *pzErr = sqlite3MPrintf("no such module: %s", zModule);
391 rc = SQLITE_ERROR;
392 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000393 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000394 }
395
danielk1977e7ff4032006-06-17 11:30:32 +0000396 if( rc==SQLITE_OK && pTab->pVtab ){
danielk197720b1eaf2006-07-26 16:22:14 +0000397 rc = addToVTrans(db, pTab->pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000398 }
399
danielk19779da9d472006-06-14 06:58:15 +0000400 return rc;
401}
danielk1977be8a7832006-06-13 15:00:54 +0000402
403/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000404** This function is used to set the schema of a virtual table. It is only
405** valid to call this function from within the xCreate() or xConnect() of a
406** virtual table module.
407*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000408int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
409 Parse sParse;
410
411 int rc = SQLITE_OK;
412 Table *pTab = db->pVTab;
413 char *zErr = 0;
414
415 if( !pTab ){
416 sqlite3Error(db, SQLITE_MISUSE, 0);
417 return SQLITE_MISUSE;
418 }
419 assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
420
421 memset(&sParse, 0, sizeof(Parse));
422 sParse.declareVtab = 1;
423 sParse.db = db;
424
425 if(
426 SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
427 sParse.pNewTable &&
428 !sParse.pNewTable->pSelect &&
429 !sParse.pNewTable->isVirtual
430 ){
431 pTab->aCol = sParse.pNewTable->aCol;
432 pTab->nCol = sParse.pNewTable->nCol;
433 sParse.pNewTable->nCol = 0;
434 sParse.pNewTable->aCol = 0;
435 } else {
436 sqlite3Error(db, SQLITE_ERROR, zErr);
437 sqliteFree(zErr);
438 rc = SQLITE_ERROR;
439 }
440 sParse.declareVtab = 0;
441
442 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
443 sqlite3DeleteTable(0, sParse.pNewTable);
444 sParse.pNewTable = 0;
445 db->pVTab = 0;
446
447 return rc;
448}
449
450/*
danielk19779e39ce82006-06-12 16:01:21 +0000451** This function is invoked by the vdbe to call the xDestroy method
452** of the virtual table named zTab in database iDb. This occurs
453** when a DROP TABLE is mentioned.
454**
455** This call is a no-op if zTab is not a virtual table.
456*/
457int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
458{
459 int rc = SQLITE_OK;
460 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000461
462 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk19779e39ce82006-06-12 16:01:21 +0000463 assert(pTab);
464 if( pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000465 int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
danielk19779e39ce82006-06-12 16:01:21 +0000466 rc = sqlite3SafetyOff(db);
467 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000468 if( xDestroy ){
469 rc = xDestroy(pTab->pVtab);
470 }
danielk19779e39ce82006-06-12 16:01:21 +0000471 sqlite3SafetyOn(db);
472 if( rc==SQLITE_OK ){
473 pTab->pVtab = 0;
474 }
475 }
476
477 return rc;
478}
479
danielk1977f9e7dda2006-06-16 16:08:53 +0000480/*
danielk1977e7ff4032006-06-17 11:30:32 +0000481** This function invokes either the xRollback or xCommit method
482** of each of the virtual tables in the sqlite3.aVTrans array. The method
483** called is identified by the second argument, "offset", which is
484** the offset of the method to call in the sqlite3_module structure.
485**
486** The array is cleared after invoking the callbacks.
487*/
488static void callFinaliser(sqlite3 *db, int offset){
489 int i;
490 for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
491 sqlite3_vtab *pVtab = db->aVTrans[i];
492 int (*x)(sqlite3_vtab *);
493 x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
494 if( x ) x(pVtab);
danielk1977be718892006-06-23 08:05:19 +0000495 pVtab->nRef--;
496 if( pVtab->nRef==0 ){
497 pVtab->pModule->xDisconnect(pVtab);
498 }
danielk1977e7ff4032006-06-17 11:30:32 +0000499 }
500 sqliteFree(db->aVTrans);
501 db->nVTrans = 0;
502 db->aVTrans = 0;
503}
504
505/*
danielk19775bd270b2006-07-25 15:14:52 +0000506** If argument rc2 is not SQLITE_OK, then return it and do nothing.
danielk1977f9e7dda2006-06-16 16:08:53 +0000507** Otherwise, invoke the xSync method of all virtual tables in the
508** sqlite3.aVTrans array. Return the error code for the first error
509** that occurs, or SQLITE_OK if all xSync operations are successful.
510*/
danielk1977e7ff4032006-06-17 11:30:32 +0000511int sqlite3VtabSync(sqlite3 *db, int rc2){
512 int i;
513 int rc = SQLITE_OK;
danielk19775bd270b2006-07-25 15:14:52 +0000514 int rcsafety;
danielk197720b1eaf2006-07-26 16:22:14 +0000515 sqlite3_vtab **aVTrans = db->aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000516 if( rc2!=SQLITE_OK ) return rc2;
danielk197720b1eaf2006-07-26 16:22:14 +0000517
danielk19775bd270b2006-07-25 15:14:52 +0000518 rc = sqlite3SafetyOff(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000519 db->aVTrans = 0;
520 for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
521 sqlite3_vtab *pVtab = aVTrans[i];
danielk1977e7ff4032006-06-17 11:30:32 +0000522 int (*x)(sqlite3_vtab *);
523 x = pVtab->pModule->xSync;
524 if( x ){
525 rc = x(pVtab);
526 }
527 }
danielk197720b1eaf2006-07-26 16:22:14 +0000528 db->aVTrans = aVTrans;
danielk19775bd270b2006-07-25 15:14:52 +0000529 rcsafety = sqlite3SafetyOn(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000530
danielk19775bd270b2006-07-25 15:14:52 +0000531 if( rc==SQLITE_OK ){
532 rc = rcsafety;
533 }
danielk1977e7ff4032006-06-17 11:30:32 +0000534 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000535}
536
537/*
538** Invoke the xRollback method of all virtual tables in the
539** sqlite3.aVTrans array. Then clear the array itself.
540*/
541int sqlite3VtabRollback(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000542 callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback));
543 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000544}
545
546/*
547** Invoke the xCommit method of all virtual tables in the
548** sqlite3.aVTrans array. Then clear the array itself.
549*/
550int sqlite3VtabCommit(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000551 callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit));
552 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000553}
554
555/*
556** If the virtual table pVtab supports the transaction interface
557** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
558** not currently open, invoke the xBegin method now.
559**
560** If the xBegin call is successful, place the sqlite3_vtab pointer
561** in the sqlite3.aVTrans array.
562*/
563int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
564 int rc = SQLITE_OK;
danielk197720b1eaf2006-07-26 16:22:14 +0000565 const sqlite3_module *pModule;
566
567 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
568 ** than zero, then this function is being called from within a
569 ** virtual module xSync() callback. It is illegal to write to
570 ** virtual module tables in this case, so return SQLITE_LOCKED.
571 */
572 if( 0==db->aVTrans && db->nVTrans>0 ){
573 return SQLITE_LOCKED;
574 }
575 if( !pVtab ){
576 return SQLITE_OK;
577 }
578 pModule = pVtab->pModule;
579
danielk1977f9e7dda2006-06-16 16:08:53 +0000580 if( pModule->xBegin ){
581 int i;
582
danielk197720b1eaf2006-07-26 16:22:14 +0000583
danielk1977f9e7dda2006-06-16 16:08:53 +0000584 /* If pVtab is already in the aVTrans array, return early */
585 for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
586 if( db->aVTrans[i]==pVtab ){
587 return SQLITE_OK;
588 }
589 }
590
591 /* Invoke the xBegin method */
592 rc = pModule->xBegin(pVtab);
593 if( rc!=SQLITE_OK ){
594 return rc;
595 }
596
danielk1977e7ff4032006-06-17 11:30:32 +0000597 rc = addToVTrans(db, pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +0000598 }
599 return rc;
600}
601
drhb7f6f682006-07-08 17:06:43 +0000602/*
603** The first parameter (pDef) is a function implementation. The
604** second parameter (pExpr) is the first argument to this function.
605** If pExpr is a column in a virtual table, then let the virtual
606** table implementation have an opportunity to overload the function.
607**
608** This routine is used to allow virtual table implementations to
609** overload MATCH, LIKE, GLOB, and REGEXP operators.
610**
611** Return either the pDef argument (indicating no change) or a
612** new FuncDef structure that is marked as ephemeral using the
613** SQLITE_FUNC_EPHEM flag.
614*/
615FuncDef *sqlite3VtabOverloadFunction(
616 FuncDef *pDef, /* Function to possibly overload */
617 int nArg, /* Number of arguments to the function */
618 Expr *pExpr /* First argument to the function */
619){
620 Table *pTab;
621 sqlite3_vtab *pVtab;
622 sqlite3_module *pMod;
drhe94b0c32006-07-08 18:09:15 +0000623 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
drhb7f6f682006-07-08 17:06:43 +0000624 void *pArg;
drhb7f6f682006-07-08 17:06:43 +0000625 FuncDef *pNew;
626
627 /* Check to see the left operand is a column in a virtual table */
628 if( pExpr==0 ) return pDef;
629 if( pExpr->op!=TK_COLUMN ) return pDef;
630 pTab = pExpr->pTab;
631 if( pTab==0 ) return pDef;
632 if( !pTab->isVirtual ) return pDef;
633 pVtab = pTab->pVtab;
634 assert( pVtab!=0 );
635 assert( pVtab->pModule!=0 );
danielk19775bd270b2006-07-25 15:14:52 +0000636 pMod = (sqlite3_module *)pVtab->pModule;
drhb7f6f682006-07-08 17:06:43 +0000637 if( pMod->xFindFunction==0 ) return pDef;
638
639 /* Call the xFuncFunction method on the virtual table implementation
640 ** to see if the implementation wants to overload this function */
drhe94b0c32006-07-08 18:09:15 +0000641 if( pMod->xFindFunction(pVtab, nArg, pDef->zName, &xFunc, &pArg)==0 ){
drhb7f6f682006-07-08 17:06:43 +0000642 return pDef;
643 }
644
645 /* Create a new ephemeral function definition for the overloaded
646 ** function */
647 pNew = sqliteMalloc( sizeof(*pNew) + strlen(pDef->zName) );
648 if( pNew==0 ){
649 return pDef;
650 }
651 *pNew = *pDef;
652 strcpy(pNew->zName, pDef->zName);
653 pNew->xFunc = xFunc;
654 pNew->pUserData = pArg;
drhb7f6f682006-07-08 17:06:43 +0000655 pNew->flags |= SQLITE_FUNC_EPHEM;
656 return pNew;
657}
658
drhb9bb7c12006-06-11 23:41:55 +0000659#endif /* SQLITE_OMIT_VIRTUALTABLE */