blob: c3e2481a6316edd7890e42b3fc895d054b9a7eec [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**
drh4ac285a2006-09-15 07:28:50 +000014** $Id: vtab.c,v 1.36 2006/09/15 07:28:51 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/*
drh189d4af2006-09-02 20:57:52 +000044** Lock the virtual table so that it cannot be disconnected.
45** Locks nest. Every lock should have a corresponding unlock.
46** If an unlock is omitted, resources leaks will occur.
47**
48** If a disconnect is attempted while a virtual table is locked,
49** the disconnect is deferred until all locks have been removed.
50*/
51void sqlite3VtabLock(sqlite3_vtab *pVtab){
52 pVtab->nRef++;
53}
54
55/*
56** Unlock a virtual table. When the last lock is removed,
57** disconnect the virtual table.
58*/
59void sqlite3VtabUnlock(sqlite3_vtab *pVtab){
60 pVtab->nRef--;
61 if( pVtab->nRef==0 ){
62 pVtab->pModule->xDisconnect(pVtab);
63 }
64}
65
66/*
drhb9bb7c12006-06-11 23:41:55 +000067** Clear any and all virtual-table information from the Table record.
68** This routine is called, for example, just before deleting the Table
69** record.
70*/
71void sqlite3VtabClear(Table *p){
danielk1977be718892006-06-23 08:05:19 +000072 sqlite3_vtab *pVtab = p->pVtab;
73 if( pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +000074 assert( p->pMod && p->pMod->pModule );
drh189d4af2006-09-02 20:57:52 +000075 sqlite3VtabUnlock(pVtab);
danielk1977be718892006-06-23 08:05:19 +000076 p->pVtab = 0;
drhb9bb7c12006-06-11 23:41:55 +000077 }
78 if( p->azModuleArg ){
79 int i;
80 for(i=0; i<p->nModuleArg; i++){
81 sqliteFree(p->azModuleArg[i]);
82 }
83 sqliteFree(p->azModuleArg);
84 }
85}
86
87/*
88** Add a new module argument to pTable->azModuleArg[].
89** The string is not copied - the pointer is stored. The
90** string will be freed automatically when the table is
91** deleted.
92*/
93static void addModuleArgument(Table *pTable, char *zArg){
94 int i = pTable->nModuleArg++;
danielk1977b7a2f2e2006-06-23 11:34:54 +000095 int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
96 char **azModuleArg;
97 azModuleArg = sqliteRealloc(pTable->azModuleArg, nBytes);
98 if( azModuleArg==0 ){
99 int j;
100 for(j=0; j<i; j++){
101 sqliteFree(pTable->azModuleArg[j]);
102 }
drhb9bb7c12006-06-11 23:41:55 +0000103 sqliteFree(zArg);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000104 sqliteFree(pTable->azModuleArg);
105 pTable->nModuleArg = 0;
drhb9bb7c12006-06-11 23:41:55 +0000106 }else{
danielk1977b7a2f2e2006-06-23 11:34:54 +0000107 azModuleArg[i] = zArg;
108 azModuleArg[i+1] = 0;
drhb9bb7c12006-06-11 23:41:55 +0000109 }
danielk1977b7a2f2e2006-06-23 11:34:54 +0000110 pTable->azModuleArg = azModuleArg;
drhb9bb7c12006-06-11 23:41:55 +0000111}
112
113/*
114** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
115** statement. The module name has been parsed, but the optional list
116** of parameters that follow the module name are still pending.
117*/
118void sqlite3VtabBeginParse(
119 Parse *pParse, /* Parsing context */
120 Token *pName1, /* Name of new table, or database name */
121 Token *pName2, /* Name of new table or NULL */
122 Token *pModuleName /* Name of the module for the virtual table */
123){
danielk1977f1a381e2006-06-16 08:01:02 +0000124 int iDb; /* The database the table is being created in */
drhb9bb7c12006-06-11 23:41:55 +0000125 Table *pTable; /* The new virtual table */
126
danielk1977f1a381e2006-06-16 08:01:02 +0000127 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
drhb9bb7c12006-06-11 23:41:55 +0000128 pTable = pParse->pNewTable;
danielk1977f1a381e2006-06-16 08:01:02 +0000129 if( pTable==0 || pParse->nErr ) return;
130 assert( 0==pTable->pIndex );
131
danielk197770ba1642006-06-21 16:02:42 +0000132 iDb = sqlite3SchemaToIndex(pParse->db, pTable->pSchema);
133 assert( iDb>=0 );
134
drhb9bb7c12006-06-11 23:41:55 +0000135 pTable->isVirtual = 1;
136 pTable->nModuleArg = 0;
137 addModuleArgument(pTable, sqlite3NameFromToken(pModuleName));
danielk197770ba1642006-06-21 16:02:42 +0000138 addModuleArgument(pTable, sqlite3StrDup(pParse->db->aDb[iDb].zName));
139 addModuleArgument(pTable, sqlite3StrDup(pTable->zName));
drhb9bb7c12006-06-11 23:41:55 +0000140 pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
danielk1977f1a381e2006-06-16 08:01:02 +0000141
142#ifndef SQLITE_OMIT_AUTHORIZATION
143 /* Creating a virtual table invokes the authorization callback twice.
144 ** The first invocation, to obtain permission to INSERT a row into the
145 ** sqlite_master table, has already been made by sqlite3StartTable().
146 ** The second call, to obtain permission to create the table, is made now.
147 */
danielk1977be718892006-06-23 08:05:19 +0000148 if( pTable->azModuleArg ){
149 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
150 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
danielk1977f1a381e2006-06-16 08:01:02 +0000151 }
152#endif
drhb9bb7c12006-06-11 23:41:55 +0000153}
154
155/*
156** This routine takes the module argument that has been accumulating
157** in pParse->zArg[] and appends it to the list of arguments on the
158** virtual table currently under construction in pParse->pTable.
159*/
160static void addArgumentToVtab(Parse *pParse){
danielk197733b39332006-06-24 08:51:05 +0000161 if( pParse->sArg.z && pParse->pNewTable ){
drh7c2d87c2006-09-02 14:16:59 +0000162 const char *z = (const char*)pParse->sArg.z;
danielk197733b39332006-06-24 08:51:05 +0000163 int n = pParse->sArg.n;
164 addModuleArgument(pParse->pNewTable, sqliteStrNDup(z, n));
drhb9bb7c12006-06-11 23:41:55 +0000165 }
drhb9bb7c12006-06-11 23:41:55 +0000166}
167
168/*
169** The parser calls this routine after the CREATE VIRTUAL TABLE statement
170** has been completely parsed.
171*/
172void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
173 Table *pTab; /* The table being constructed */
174 sqlite3 *db; /* The database connection */
175 char *zModule; /* The module name of the table: USING modulename */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000176 Module *pMod = 0;
drhb9bb7c12006-06-11 23:41:55 +0000177
178 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000179 pParse->sArg.z = 0;
drhb9bb7c12006-06-11 23:41:55 +0000180
181 /* Lookup the module name. */
182 pTab = pParse->pNewTable;
183 if( pTab==0 ) return;
184 db = pParse->db;
185 if( pTab->nModuleArg<1 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000186 zModule = pTab->azModuleArg[0];
danielk1977d1ab1ba2006-06-15 04:28:13 +0000187 pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
188 pTab->pMod = pMod;
drhb9bb7c12006-06-11 23:41:55 +0000189
190 /* If the CREATE VIRTUAL TABLE statement is being entered for the
191 ** first time (in other words if the virtual table is actually being
192 ** created now instead of just being read out of sqlite_master) then
193 ** do additional initialization work and store the statement text
194 ** in the sqlite_master table.
195 */
196 if( !db->init.busy ){
197 char *zStmt;
danielk197778efaba2006-06-12 06:09:17 +0000198 char *zWhere;
drhb9bb7c12006-06-11 23:41:55 +0000199 int iDb;
200 Vdbe *v;
drhb9bb7c12006-06-11 23:41:55 +0000201
202 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
203 if( pEnd ){
204 pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
205 }
206 zStmt = sqlite3MPrintf("CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
207
208 /* A slot for the record has already been allocated in the
209 ** SQLITE_MASTER table. We just need to update that slot with all
danielk197778efaba2006-06-12 06:09:17 +0000210 ** the information we've collected.
211 **
212 ** The top of the stack is the rootpage allocated by sqlite3StartTable().
213 ** This value is always 0 and is ignored, a virtual table does not have a
214 ** rootpage. The next entry on the stack is the rowid of the record
215 ** in the sqlite_master table.
drhb9bb7c12006-06-11 23:41:55 +0000216 */
217 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
218 sqlite3NestedParse(pParse,
219 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000220 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
221 "WHERE rowid=#1",
drhb9bb7c12006-06-11 23:41:55 +0000222 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
223 pTab->zName,
224 pTab->zName,
225 zStmt
226 );
227 sqliteFree(zStmt);
228 v = sqlite3GetVdbe(pParse);
drhb9bb7c12006-06-11 23:41:55 +0000229 sqlite3ChangeCookie(db, v, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000230
231 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
232 zWhere = sqlite3MPrintf("name='%q'", pTab->zName);
233 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
234 sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
drhb9bb7c12006-06-11 23:41:55 +0000235 }
236
danielk197778efaba2006-06-12 06:09:17 +0000237 /* If we are rereading the sqlite_master table create the in-memory
danielk1977c7d54102006-06-15 07:29:00 +0000238 ** record of the table. If the module has already been registered,
239 ** also call the xConnect method here.
drhb9bb7c12006-06-11 23:41:55 +0000240 */
danielk197778efaba2006-06-12 06:09:17 +0000241 else {
danielk197778efaba2006-06-12 06:09:17 +0000242 Table *pOld;
243 Schema *pSchema = pTab->pSchema;
244 const char *zName = pTab->zName;
245 int nName = strlen(zName) + 1;
246 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
247 if( pOld ){
248 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
249 return;
250 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000251 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000252 }
253}
254
255/*
256** The parser calls this routine when it sees the first token
257** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
258*/
259void sqlite3VtabArgInit(Parse *pParse){
260 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000261 pParse->sArg.z = 0;
262 pParse->sArg.n = 0;
drhb9bb7c12006-06-11 23:41:55 +0000263}
264
265/*
266** The parser calls this routine for each token after the first token
267** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
268*/
269void sqlite3VtabArgExtend(Parse *pParse, Token *p){
danielk197733b39332006-06-24 08:51:05 +0000270 Token *pArg = &pParse->sArg;
271 if( pArg->z==0 ){
272 pArg->z = p->z;
273 pArg->n = p->n;
274 }else{
275 assert(pArg->z < p->z);
276 pArg->n = (p->z + p->n - pArg->z);
drhb9bb7c12006-06-11 23:41:55 +0000277 }
drhb9bb7c12006-06-11 23:41:55 +0000278}
279
danielk197778efaba2006-06-12 06:09:17 +0000280/*
danielk19779da9d472006-06-14 06:58:15 +0000281** Invoke a virtual table constructor (either xCreate or xConnect). The
282** pointer to the function to invoke is passed as the fourth parameter
283** to this procedure.
284*/
285static int vtabCallConstructor(
286 sqlite3 *db,
287 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000288 Module *pMod,
drhe4102962006-09-11 00:34:22 +0000289 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
danielk19779da9d472006-06-14 06:58:15 +0000290 char **pzErr
291){
292 int rc;
293 int rc2;
drhfe1368e2006-09-10 17:08:29 +0000294 sqlite3_vtab *pVtab;
drhe4102962006-09-11 00:34:22 +0000295 const char *const*azArg = (const char *const*)pTab->azModuleArg;
danielk19779da9d472006-06-14 06:58:15 +0000296 int nArg = pTab->nModuleArg;
drh4ca8aac2006-09-10 17:31:58 +0000297 char *zErr = 0;
drh235a8182006-09-13 19:21:28 +0000298 char *zModuleName = sqlite3MPrintf("%s", pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000299
300 assert( !db->pVTab );
301 assert( xConstruct );
302
303 db->pVTab = pTab;
304 rc = sqlite3SafetyOff(db);
305 assert( rc==SQLITE_OK );
drh4ca8aac2006-09-10 17:31:58 +0000306 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pTab->pVtab, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000307 rc2 = sqlite3SafetyOn(db);
drhfe1368e2006-09-10 17:08:29 +0000308 pVtab = pTab->pVtab;
309 if( rc==SQLITE_OK && pVtab ){
310 pVtab->pModule = pMod->pModule;
311 pVtab->nRef = 1;
danielk19779da9d472006-06-14 06:58:15 +0000312 }
313
314 if( SQLITE_OK!=rc ){
drh4ca8aac2006-09-10 17:31:58 +0000315 if( zErr==0 ){
drh235a8182006-09-13 19:21:28 +0000316 *pzErr = sqlite3MPrintf("vtable constructor failed: %s", zModuleName);
drh4ca8aac2006-09-10 17:31:58 +0000317 }else {
drhe4102962006-09-11 00:34:22 +0000318 *pzErr = sqlite3MPrintf("%s", zErr);
drh4ca8aac2006-09-10 17:31:58 +0000319 sqlite3_free(zErr);
drhfe1368e2006-09-10 17:08:29 +0000320 }
321 }else if( db->pVTab ){
danielk19779da9d472006-06-14 06:58:15 +0000322 const char *zFormat = "vtable constructor did not declare schema: %s";
323 *pzErr = sqlite3MPrintf(zFormat, pTab->zName);
324 rc = SQLITE_ERROR;
325 }
326 if( rc==SQLITE_OK ){
327 rc = rc2;
328 }
329 db->pVTab = 0;
drh235a8182006-09-13 19:21:28 +0000330 sqliteFree(zModuleName);
danielk19779da9d472006-06-14 06:58:15 +0000331 return rc;
332}
333
334/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000335** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000336** of the virtual table pTab. If an error occurs, an error code is returned
337** and an error left in pParse.
338**
339** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000340*/
341int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000342 Module *pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000343 const char *zModule;
344 int rc = SQLITE_OK;
345
danielk1977fe3fcbe22006-06-12 12:08:45 +0000346 if( !pTab || !pTab->isVirtual || pTab->pVtab ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000347 return SQLITE_OK;
348 }
349
danielk1977d1ab1ba2006-06-15 04:28:13 +0000350 pMod = pTab->pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000351 zModule = pTab->azModuleArg[0];
danielk1977d1ab1ba2006-06-15 04:28:13 +0000352 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000353 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000354 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000355 rc = SQLITE_ERROR;
356 } else {
danielk19779da9d472006-06-14 06:58:15 +0000357 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000358 sqlite3 *db = pParse->db;
359 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000360 if( rc!=SQLITE_OK ){
361 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000362 }
danielk19779da9d472006-06-14 06:58:15 +0000363 sqliteFree(zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000364 }
365
366 return rc;
367}
368
danielk19779da9d472006-06-14 06:58:15 +0000369/*
danielk1977e7ff4032006-06-17 11:30:32 +0000370** Add the virtual table pVtab to the array sqlite3.aVTrans[].
371*/
danielk1977be718892006-06-23 08:05:19 +0000372static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
danielk1977e7ff4032006-06-17 11:30:32 +0000373 const int ARRAY_INCR = 5;
374
375 /* Grow the sqlite3.aVTrans array if required */
376 if( (db->nVTrans%ARRAY_INCR)==0 ){
377 sqlite3_vtab **aVTrans;
378 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
379 aVTrans = sqliteRealloc((void *)db->aVTrans, nBytes);
380 if( !aVTrans ){
381 return SQLITE_NOMEM;
382 }
383 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
384 db->aVTrans = aVTrans;
385 }
386
387 /* Add pVtab to the end of sqlite3.aVTrans */
388 db->aVTrans[db->nVTrans++] = pVtab;
drh189d4af2006-09-02 20:57:52 +0000389 sqlite3VtabLock(pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000390 return SQLITE_OK;
391}
392
393/*
danielk19779da9d472006-06-14 06:58:15 +0000394** This function is invoked by the vdbe to call the xCreate method
395** of the virtual table named zTab in database iDb.
396**
397** If an error occurs, *pzErr is set to point an an English language
398** description of the error and an SQLITE_XXX error code is returned.
399** In this case the caller must call sqliteFree() on *pzErr.
400*/
401int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
402 int rc = SQLITE_OK;
403 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000404 Module *pMod;
danielk19779da9d472006-06-14 06:58:15 +0000405 const char *zModule;
406
407 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
408 assert(pTab && pTab->isVirtual && !pTab->pVtab);
danielk1977d1ab1ba2006-06-15 04:28:13 +0000409 pMod = pTab->pMod;
danielk19779da9d472006-06-14 06:58:15 +0000410 zModule = pTab->azModuleArg[0];
411
412 /* If the module has been registered and includes a Create method,
413 ** invoke it now. If the module has not been registered, return an
414 ** error. Otherwise, do nothing.
415 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000416 if( !pMod ){
danielk19779da9d472006-06-14 06:58:15 +0000417 *pzErr = sqlite3MPrintf("no such module: %s", zModule);
418 rc = SQLITE_ERROR;
419 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000420 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000421 }
422
danielk1977e7ff4032006-06-17 11:30:32 +0000423 if( rc==SQLITE_OK && pTab->pVtab ){
danielk197720b1eaf2006-07-26 16:22:14 +0000424 rc = addToVTrans(db, pTab->pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000425 }
426
danielk19779da9d472006-06-14 06:58:15 +0000427 return rc;
428}
danielk1977be8a7832006-06-13 15:00:54 +0000429
430/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000431** This function is used to set the schema of a virtual table. It is only
432** valid to call this function from within the xCreate() or xConnect() of a
433** virtual table module.
434*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000435int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
436 Parse sParse;
437
438 int rc = SQLITE_OK;
439 Table *pTab = db->pVTab;
440 char *zErr = 0;
441
442 if( !pTab ){
443 sqlite3Error(db, SQLITE_MISUSE, 0);
444 return SQLITE_MISUSE;
445 }
446 assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
447
448 memset(&sParse, 0, sizeof(Parse));
449 sParse.declareVtab = 1;
450 sParse.db = db;
451
452 if(
453 SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
454 sParse.pNewTable &&
455 !sParse.pNewTable->pSelect &&
456 !sParse.pNewTable->isVirtual
457 ){
458 pTab->aCol = sParse.pNewTable->aCol;
459 pTab->nCol = sParse.pNewTable->nCol;
460 sParse.pNewTable->nCol = 0;
461 sParse.pNewTable->aCol = 0;
462 } else {
463 sqlite3Error(db, SQLITE_ERROR, zErr);
464 sqliteFree(zErr);
465 rc = SQLITE_ERROR;
466 }
467 sParse.declareVtab = 0;
468
469 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
470 sqlite3DeleteTable(0, sParse.pNewTable);
471 sParse.pNewTable = 0;
472 db->pVTab = 0;
473
drh4ac285a2006-09-15 07:28:50 +0000474 assert( (rc&0xff)==rc );
danielk19777e6ebfb2006-06-12 11:24:37 +0000475 return rc;
476}
477
478/*
danielk19779e39ce82006-06-12 16:01:21 +0000479** This function is invoked by the vdbe to call the xDestroy method
480** of the virtual table named zTab in database iDb. This occurs
481** when a DROP TABLE is mentioned.
482**
483** This call is a no-op if zTab is not a virtual table.
484*/
485int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
486{
487 int rc = SQLITE_OK;
488 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000489
490 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk19779e39ce82006-06-12 16:01:21 +0000491 assert(pTab);
492 if( pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000493 int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
danielk19779e39ce82006-06-12 16:01:21 +0000494 rc = sqlite3SafetyOff(db);
495 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000496 if( xDestroy ){
497 rc = xDestroy(pTab->pVtab);
498 }
danielk19779e39ce82006-06-12 16:01:21 +0000499 sqlite3SafetyOn(db);
500 if( rc==SQLITE_OK ){
501 pTab->pVtab = 0;
502 }
503 }
504
505 return rc;
506}
507
danielk1977f9e7dda2006-06-16 16:08:53 +0000508/*
danielk1977e7ff4032006-06-17 11:30:32 +0000509** This function invokes either the xRollback or xCommit method
510** of each of the virtual tables in the sqlite3.aVTrans array. The method
511** called is identified by the second argument, "offset", which is
512** the offset of the method to call in the sqlite3_module structure.
513**
514** The array is cleared after invoking the callbacks.
515*/
516static void callFinaliser(sqlite3 *db, int offset){
517 int i;
518 for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
519 sqlite3_vtab *pVtab = db->aVTrans[i];
520 int (*x)(sqlite3_vtab *);
521 x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
522 if( x ) x(pVtab);
drh189d4af2006-09-02 20:57:52 +0000523 sqlite3VtabUnlock(pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000524 }
525 sqliteFree(db->aVTrans);
526 db->nVTrans = 0;
527 db->aVTrans = 0;
528}
529
530/*
danielk19775bd270b2006-07-25 15:14:52 +0000531** If argument rc2 is not SQLITE_OK, then return it and do nothing.
danielk1977f9e7dda2006-06-16 16:08:53 +0000532** Otherwise, invoke the xSync method of all virtual tables in the
533** sqlite3.aVTrans array. Return the error code for the first error
534** that occurs, or SQLITE_OK if all xSync operations are successful.
535*/
danielk1977e7ff4032006-06-17 11:30:32 +0000536int sqlite3VtabSync(sqlite3 *db, int rc2){
537 int i;
538 int rc = SQLITE_OK;
danielk19775bd270b2006-07-25 15:14:52 +0000539 int rcsafety;
danielk197720b1eaf2006-07-26 16:22:14 +0000540 sqlite3_vtab **aVTrans = db->aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000541 if( rc2!=SQLITE_OK ) return rc2;
danielk197720b1eaf2006-07-26 16:22:14 +0000542
danielk19775bd270b2006-07-25 15:14:52 +0000543 rc = sqlite3SafetyOff(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000544 db->aVTrans = 0;
545 for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
546 sqlite3_vtab *pVtab = aVTrans[i];
danielk1977e7ff4032006-06-17 11:30:32 +0000547 int (*x)(sqlite3_vtab *);
548 x = pVtab->pModule->xSync;
549 if( x ){
550 rc = x(pVtab);
551 }
552 }
danielk197720b1eaf2006-07-26 16:22:14 +0000553 db->aVTrans = aVTrans;
danielk19775bd270b2006-07-25 15:14:52 +0000554 rcsafety = sqlite3SafetyOn(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000555
danielk19775bd270b2006-07-25 15:14:52 +0000556 if( rc==SQLITE_OK ){
557 rc = rcsafety;
558 }
danielk1977e7ff4032006-06-17 11:30:32 +0000559 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000560}
561
562/*
563** Invoke the xRollback method of all virtual tables in the
564** sqlite3.aVTrans array. Then clear the array itself.
565*/
566int sqlite3VtabRollback(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000567 callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback));
568 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000569}
570
571/*
572** Invoke the xCommit method of all virtual tables in the
573** sqlite3.aVTrans array. Then clear the array itself.
574*/
575int sqlite3VtabCommit(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000576 callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit));
577 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000578}
579
580/*
581** If the virtual table pVtab supports the transaction interface
582** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
583** not currently open, invoke the xBegin method now.
584**
585** If the xBegin call is successful, place the sqlite3_vtab pointer
586** in the sqlite3.aVTrans array.
587*/
588int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
589 int rc = SQLITE_OK;
danielk197720b1eaf2006-07-26 16:22:14 +0000590 const sqlite3_module *pModule;
591
592 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
593 ** than zero, then this function is being called from within a
594 ** virtual module xSync() callback. It is illegal to write to
595 ** virtual module tables in this case, so return SQLITE_LOCKED.
596 */
597 if( 0==db->aVTrans && db->nVTrans>0 ){
598 return SQLITE_LOCKED;
599 }
600 if( !pVtab ){
601 return SQLITE_OK;
602 }
603 pModule = pVtab->pModule;
604
danielk1977f9e7dda2006-06-16 16:08:53 +0000605 if( pModule->xBegin ){
606 int i;
607
danielk197720b1eaf2006-07-26 16:22:14 +0000608
danielk1977f9e7dda2006-06-16 16:08:53 +0000609 /* If pVtab is already in the aVTrans array, return early */
610 for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
611 if( db->aVTrans[i]==pVtab ){
612 return SQLITE_OK;
613 }
614 }
615
616 /* Invoke the xBegin method */
617 rc = pModule->xBegin(pVtab);
618 if( rc!=SQLITE_OK ){
619 return rc;
620 }
621
danielk1977e7ff4032006-06-17 11:30:32 +0000622 rc = addToVTrans(db, pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +0000623 }
624 return rc;
625}
626
drhb7f6f682006-07-08 17:06:43 +0000627/*
628** The first parameter (pDef) is a function implementation. The
629** second parameter (pExpr) is the first argument to this function.
630** If pExpr is a column in a virtual table, then let the virtual
631** table implementation have an opportunity to overload the function.
632**
633** This routine is used to allow virtual table implementations to
634** overload MATCH, LIKE, GLOB, and REGEXP operators.
635**
636** Return either the pDef argument (indicating no change) or a
637** new FuncDef structure that is marked as ephemeral using the
638** SQLITE_FUNC_EPHEM flag.
639*/
640FuncDef *sqlite3VtabOverloadFunction(
641 FuncDef *pDef, /* Function to possibly overload */
642 int nArg, /* Number of arguments to the function */
643 Expr *pExpr /* First argument to the function */
644){
645 Table *pTab;
646 sqlite3_vtab *pVtab;
647 sqlite3_module *pMod;
drhe94b0c32006-07-08 18:09:15 +0000648 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
drhb7f6f682006-07-08 17:06:43 +0000649 void *pArg;
drhb7f6f682006-07-08 17:06:43 +0000650 FuncDef *pNew;
651
652 /* Check to see the left operand is a column in a virtual table */
653 if( pExpr==0 ) return pDef;
654 if( pExpr->op!=TK_COLUMN ) return pDef;
655 pTab = pExpr->pTab;
656 if( pTab==0 ) return pDef;
657 if( !pTab->isVirtual ) return pDef;
658 pVtab = pTab->pVtab;
659 assert( pVtab!=0 );
660 assert( pVtab->pModule!=0 );
danielk19775bd270b2006-07-25 15:14:52 +0000661 pMod = (sqlite3_module *)pVtab->pModule;
drhb7f6f682006-07-08 17:06:43 +0000662 if( pMod->xFindFunction==0 ) return pDef;
663
664 /* Call the xFuncFunction method on the virtual table implementation
665 ** to see if the implementation wants to overload this function */
drhe94b0c32006-07-08 18:09:15 +0000666 if( pMod->xFindFunction(pVtab, nArg, pDef->zName, &xFunc, &pArg)==0 ){
drhb7f6f682006-07-08 17:06:43 +0000667 return pDef;
668 }
669
670 /* Create a new ephemeral function definition for the overloaded
671 ** function */
672 pNew = sqliteMalloc( sizeof(*pNew) + strlen(pDef->zName) );
673 if( pNew==0 ){
674 return pDef;
675 }
676 *pNew = *pDef;
677 strcpy(pNew->zName, pDef->zName);
678 pNew->xFunc = xFunc;
679 pNew->pUserData = pArg;
drhb7f6f682006-07-08 17:06:43 +0000680 pNew->flags |= SQLITE_FUNC_EPHEM;
681 return pNew;
682}
683
drhb9bb7c12006-06-11 23:41:55 +0000684#endif /* SQLITE_OMIT_VIRTUALTABLE */