blob: 7559bdefadecb7b237ee14611f43e0c1122e0795 [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**
danielk1977a1644fd2007-08-29 12:31:25 +000014** $Id: vtab.c,v 1.55 2007/08/29 12:31:29 danielk1977 Exp $
drhb9bb7c12006-06-11 23:41:55 +000015*/
16#ifndef SQLITE_OMIT_VIRTUALTABLE
17#include "sqliteInt.h"
18
danielk1977832a58a2007-06-22 15:21:15 +000019static int createModule(
20 sqlite3 *db, /* Database in which module is registered */
21 const char *zName, /* Name assigned to this module */
22 const sqlite3_module *pModule, /* The definition of the module */
23 void *pAux, /* Context pointer for xCreate/xConnect */
24 void (*xDestroy)(void *) /* Module destructor function */
25) {
drh27641702007-08-22 02:56:42 +000026 int rc, nName;
drh153c62c2007-08-24 03:51:33 +000027 Module *pMod;
drh27641702007-08-22 02:56:42 +000028
29 sqlite3_mutex_enter(db->mutex);
30 nName = strlen(zName);
drh153c62c2007-08-24 03:51:33 +000031 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
danielk1977832a58a2007-06-22 15:21:15 +000032 if( pMod ){
33 char *zCopy = (char *)(&pMod[1]);
34 memcpy(zCopy, zName, nName+1);
35 pMod->zName = zCopy;
36 pMod->pModule = pModule;
37 pMod->pAux = pAux;
38 pMod->xDestroy = xDestroy;
39 pMod = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
40 if( pMod && pMod->xDestroy ){
41 pMod->xDestroy(pMod->pAux);
42 }
drh17435752007-08-16 04:30:38 +000043 sqlite3_free(pMod);
danielk1977832a58a2007-06-22 15:21:15 +000044 sqlite3ResetInternalSchema(db, 0);
45 }
drh27641702007-08-22 02:56:42 +000046 rc = sqlite3ApiExit(db, SQLITE_OK);
47 sqlite3_mutex_leave(db->mutex);
48 return rc;
danielk1977832a58a2007-06-22 15:21:15 +000049}
50
51
drhb9bb7c12006-06-11 23:41:55 +000052/*
53** External API function used to create a new virtual-table module.
54*/
55int sqlite3_create_module(
56 sqlite3 *db, /* Database in which module is registered */
57 const char *zName, /* Name assigned to this module */
danielk1977d1ab1ba2006-06-15 04:28:13 +000058 const sqlite3_module *pModule, /* The definition of the module */
59 void *pAux /* Context pointer for xCreate/xConnect */
drhb9bb7c12006-06-11 23:41:55 +000060){
danielk1977832a58a2007-06-22 15:21:15 +000061 return createModule(db, zName, pModule, pAux, 0);
62}
63
64/*
65** External API function used to create a new virtual-table module.
66*/
67int sqlite3_create_module_v2(
68 sqlite3 *db, /* Database in which module is registered */
69 const char *zName, /* Name assigned to this module */
70 const sqlite3_module *pModule, /* The definition of the module */
71 void *pAux, /* Context pointer for xCreate/xConnect */
72 void (*xDestroy)(void *) /* Module destructor function */
73){
74 return createModule(db, zName, pModule, pAux, xDestroy);
drhb9bb7c12006-06-11 23:41:55 +000075}
76
drhb9bb7c12006-06-11 23:41:55 +000077/*
drh189d4af2006-09-02 20:57:52 +000078** Lock the virtual table so that it cannot be disconnected.
79** Locks nest. Every lock should have a corresponding unlock.
80** If an unlock is omitted, resources leaks will occur.
81**
82** If a disconnect is attempted while a virtual table is locked,
83** the disconnect is deferred until all locks have been removed.
84*/
85void sqlite3VtabLock(sqlite3_vtab *pVtab){
86 pVtab->nRef++;
87}
88
89/*
90** Unlock a virtual table. When the last lock is removed,
91** disconnect the virtual table.
92*/
danielk1977a04a34f2007-04-16 15:06:25 +000093void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
drh189d4af2006-09-02 20:57:52 +000094 pVtab->nRef--;
danielk1977a04a34f2007-04-16 15:06:25 +000095 assert(db);
96 assert(!sqlite3SafetyCheck(db));
drh189d4af2006-09-02 20:57:52 +000097 if( pVtab->nRef==0 ){
danielk1977a04a34f2007-04-16 15:06:25 +000098 if( db->magic==SQLITE_MAGIC_BUSY ){
99 sqlite3SafetyOff(db);
100 pVtab->pModule->xDisconnect(pVtab);
101 sqlite3SafetyOn(db);
102 } else {
103 pVtab->pModule->xDisconnect(pVtab);
104 }
drh189d4af2006-09-02 20:57:52 +0000105 }
106}
107
108/*
drhb9bb7c12006-06-11 23:41:55 +0000109** Clear any and all virtual-table information from the Table record.
110** This routine is called, for example, just before deleting the Table
111** record.
112*/
113void sqlite3VtabClear(Table *p){
danielk1977be718892006-06-23 08:05:19 +0000114 sqlite3_vtab *pVtab = p->pVtab;
115 if( pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000116 assert( p->pMod && p->pMod->pModule );
danielk1977a04a34f2007-04-16 15:06:25 +0000117 sqlite3VtabUnlock(p->pSchema->db, pVtab);
danielk1977be718892006-06-23 08:05:19 +0000118 p->pVtab = 0;
drhb9bb7c12006-06-11 23:41:55 +0000119 }
120 if( p->azModuleArg ){
121 int i;
122 for(i=0; i<p->nModuleArg; i++){
drh17435752007-08-16 04:30:38 +0000123 sqlite3_free(p->azModuleArg[i]);
drhb9bb7c12006-06-11 23:41:55 +0000124 }
drh17435752007-08-16 04:30:38 +0000125 sqlite3_free(p->azModuleArg);
drhb9bb7c12006-06-11 23:41:55 +0000126 }
127}
128
129/*
130** Add a new module argument to pTable->azModuleArg[].
131** The string is not copied - the pointer is stored. The
132** string will be freed automatically when the table is
133** deleted.
134*/
drh17435752007-08-16 04:30:38 +0000135static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
drhb9bb7c12006-06-11 23:41:55 +0000136 int i = pTable->nModuleArg++;
danielk1977b7a2f2e2006-06-23 11:34:54 +0000137 int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
138 char **azModuleArg;
drh17435752007-08-16 04:30:38 +0000139 azModuleArg = sqlite3_realloc(pTable->azModuleArg, nBytes);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000140 if( azModuleArg==0 ){
141 int j;
142 for(j=0; j<i; j++){
drh17435752007-08-16 04:30:38 +0000143 sqlite3_free(pTable->azModuleArg[j]);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000144 }
drh17435752007-08-16 04:30:38 +0000145 sqlite3_free(zArg);
146 sqlite3_free(pTable->azModuleArg);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000147 pTable->nModuleArg = 0;
drh17435752007-08-16 04:30:38 +0000148 db->mallocFailed = 1;
drhb9bb7c12006-06-11 23:41:55 +0000149 }else{
danielk1977b7a2f2e2006-06-23 11:34:54 +0000150 azModuleArg[i] = zArg;
151 azModuleArg[i+1] = 0;
drhb9bb7c12006-06-11 23:41:55 +0000152 }
danielk1977b7a2f2e2006-06-23 11:34:54 +0000153 pTable->azModuleArg = azModuleArg;
drhb9bb7c12006-06-11 23:41:55 +0000154}
155
156/*
157** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
158** statement. The module name has been parsed, but the optional list
159** of parameters that follow the module name are still pending.
160*/
161void sqlite3VtabBeginParse(
162 Parse *pParse, /* Parsing context */
163 Token *pName1, /* Name of new table, or database name */
164 Token *pName2, /* Name of new table or NULL */
165 Token *pModuleName /* Name of the module for the virtual table */
166){
danielk1977f1a381e2006-06-16 08:01:02 +0000167 int iDb; /* The database the table is being created in */
drhb9bb7c12006-06-11 23:41:55 +0000168 Table *pTable; /* The new virtual table */
drh17435752007-08-16 04:30:38 +0000169 sqlite3 *db; /* Database connection */
drhb9bb7c12006-06-11 23:41:55 +0000170
drh4a50aac2007-08-23 02:47:53 +0000171 if( pParse->db->flags & SQLITE_SharedCache ){
danielk1977113e5452007-04-16 15:49:41 +0000172 sqlite3ErrorMsg(pParse, "Cannot use virtual tables in shared-cache mode");
173 return;
174 }
175
danielk1977f1a381e2006-06-16 08:01:02 +0000176 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
drhb9bb7c12006-06-11 23:41:55 +0000177 pTable = pParse->pNewTable;
danielk1977f1a381e2006-06-16 08:01:02 +0000178 if( pTable==0 || pParse->nErr ) return;
179 assert( 0==pTable->pIndex );
180
drh17435752007-08-16 04:30:38 +0000181 db = pParse->db;
182 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
danielk197770ba1642006-06-21 16:02:42 +0000183 assert( iDb>=0 );
184
drhb9bb7c12006-06-11 23:41:55 +0000185 pTable->isVirtual = 1;
186 pTable->nModuleArg = 0;
drh17435752007-08-16 04:30:38 +0000187 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
188 addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
189 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
drhb9bb7c12006-06-11 23:41:55 +0000190 pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
danielk1977f1a381e2006-06-16 08:01:02 +0000191
192#ifndef SQLITE_OMIT_AUTHORIZATION
193 /* Creating a virtual table invokes the authorization callback twice.
194 ** The first invocation, to obtain permission to INSERT a row into the
195 ** sqlite_master table, has already been made by sqlite3StartTable().
196 ** The second call, to obtain permission to create the table, is made now.
197 */
danielk1977be718892006-06-23 08:05:19 +0000198 if( pTable->azModuleArg ){
199 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
200 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
danielk1977f1a381e2006-06-16 08:01:02 +0000201 }
202#endif
drhb9bb7c12006-06-11 23:41:55 +0000203}
204
205/*
206** This routine takes the module argument that has been accumulating
207** in pParse->zArg[] and appends it to the list of arguments on the
208** virtual table currently under construction in pParse->pTable.
209*/
210static void addArgumentToVtab(Parse *pParse){
danielk197733b39332006-06-24 08:51:05 +0000211 if( pParse->sArg.z && pParse->pNewTable ){
drh7c2d87c2006-09-02 14:16:59 +0000212 const char *z = (const char*)pParse->sArg.z;
danielk197733b39332006-06-24 08:51:05 +0000213 int n = pParse->sArg.n;
drh17435752007-08-16 04:30:38 +0000214 sqlite3 *db = pParse->db;
215 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
drhb9bb7c12006-06-11 23:41:55 +0000216 }
drhb9bb7c12006-06-11 23:41:55 +0000217}
218
219/*
220** The parser calls this routine after the CREATE VIRTUAL TABLE statement
221** has been completely parsed.
222*/
223void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
224 Table *pTab; /* The table being constructed */
225 sqlite3 *db; /* The database connection */
226 char *zModule; /* The module name of the table: USING modulename */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000227 Module *pMod = 0;
drhb9bb7c12006-06-11 23:41:55 +0000228
229 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000230 pParse->sArg.z = 0;
drhb9bb7c12006-06-11 23:41:55 +0000231
232 /* Lookup the module name. */
233 pTab = pParse->pNewTable;
234 if( pTab==0 ) return;
235 db = pParse->db;
236 if( pTab->nModuleArg<1 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000237 zModule = pTab->azModuleArg[0];
danielk1977d1ab1ba2006-06-15 04:28:13 +0000238 pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
239 pTab->pMod = pMod;
drhb9bb7c12006-06-11 23:41:55 +0000240
241 /* If the CREATE VIRTUAL TABLE statement is being entered for the
242 ** first time (in other words if the virtual table is actually being
243 ** created now instead of just being read out of sqlite_master) then
244 ** do additional initialization work and store the statement text
245 ** in the sqlite_master table.
246 */
247 if( !db->init.busy ){
248 char *zStmt;
danielk197778efaba2006-06-12 06:09:17 +0000249 char *zWhere;
drhb9bb7c12006-06-11 23:41:55 +0000250 int iDb;
251 Vdbe *v;
drhb9bb7c12006-06-11 23:41:55 +0000252
253 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
254 if( pEnd ){
255 pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
256 }
danielk19771e536952007-08-16 10:09:01 +0000257 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
drhb9bb7c12006-06-11 23:41:55 +0000258
259 /* A slot for the record has already been allocated in the
260 ** SQLITE_MASTER table. We just need to update that slot with all
danielk197778efaba2006-06-12 06:09:17 +0000261 ** the information we've collected.
262 **
263 ** The top of the stack is the rootpage allocated by sqlite3StartTable().
264 ** This value is always 0 and is ignored, a virtual table does not have a
265 ** rootpage. The next entry on the stack is the rowid of the record
266 ** in the sqlite_master table.
drhb9bb7c12006-06-11 23:41:55 +0000267 */
268 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
269 sqlite3NestedParse(pParse,
270 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000271 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
272 "WHERE rowid=#1",
drhb9bb7c12006-06-11 23:41:55 +0000273 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
274 pTab->zName,
275 pTab->zName,
276 zStmt
277 );
drh17435752007-08-16 04:30:38 +0000278 sqlite3_free(zStmt);
drhb9bb7c12006-06-11 23:41:55 +0000279 v = sqlite3GetVdbe(pParse);
drhb9bb7c12006-06-11 23:41:55 +0000280 sqlite3ChangeCookie(db, v, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000281
282 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
danielk19771e536952007-08-16 10:09:01 +0000283 zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
drh3c23a882007-01-09 14:01:13 +0000284 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 1, zWhere, P3_DYNAMIC);
danielk197778efaba2006-06-12 06:09:17 +0000285 sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
drhb9bb7c12006-06-11 23:41:55 +0000286 }
287
danielk197778efaba2006-06-12 06:09:17 +0000288 /* If we are rereading the sqlite_master table create the in-memory
danielk1977c7d54102006-06-15 07:29:00 +0000289 ** record of the table. If the module has already been registered,
290 ** also call the xConnect method here.
drhb9bb7c12006-06-11 23:41:55 +0000291 */
danielk197778efaba2006-06-12 06:09:17 +0000292 else {
danielk197778efaba2006-06-12 06:09:17 +0000293 Table *pOld;
294 Schema *pSchema = pTab->pSchema;
295 const char *zName = pTab->zName;
296 int nName = strlen(zName) + 1;
297 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
298 if( pOld ){
danielk1977a1644fd2007-08-29 12:31:25 +0000299 db->mallocFailed = 1;
danielk197778efaba2006-06-12 06:09:17 +0000300 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
301 return;
302 }
danielk1977a04a34f2007-04-16 15:06:25 +0000303 pSchema->db = pParse->db;
danielk19777e6ebfb2006-06-12 11:24:37 +0000304 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000305 }
306}
307
308/*
309** The parser calls this routine when it sees the first token
310** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
311*/
312void sqlite3VtabArgInit(Parse *pParse){
313 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000314 pParse->sArg.z = 0;
315 pParse->sArg.n = 0;
drhb9bb7c12006-06-11 23:41:55 +0000316}
317
318/*
319** The parser calls this routine for each token after the first token
320** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
321*/
322void sqlite3VtabArgExtend(Parse *pParse, Token *p){
danielk197733b39332006-06-24 08:51:05 +0000323 Token *pArg = &pParse->sArg;
324 if( pArg->z==0 ){
325 pArg->z = p->z;
326 pArg->n = p->n;
327 }else{
328 assert(pArg->z < p->z);
329 pArg->n = (p->z + p->n - pArg->z);
drhb9bb7c12006-06-11 23:41:55 +0000330 }
drhb9bb7c12006-06-11 23:41:55 +0000331}
332
danielk197778efaba2006-06-12 06:09:17 +0000333/*
danielk19779da9d472006-06-14 06:58:15 +0000334** Invoke a virtual table constructor (either xCreate or xConnect). The
335** pointer to the function to invoke is passed as the fourth parameter
336** to this procedure.
337*/
338static int vtabCallConstructor(
339 sqlite3 *db,
340 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000341 Module *pMod,
drhe4102962006-09-11 00:34:22 +0000342 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
danielk19779da9d472006-06-14 06:58:15 +0000343 char **pzErr
344){
345 int rc;
346 int rc2;
drhfe1368e2006-09-10 17:08:29 +0000347 sqlite3_vtab *pVtab;
drhe4102962006-09-11 00:34:22 +0000348 const char *const*azArg = (const char *const*)pTab->azModuleArg;
danielk19779da9d472006-06-14 06:58:15 +0000349 int nArg = pTab->nModuleArg;
drh4ca8aac2006-09-10 17:31:58 +0000350 char *zErr = 0;
danielk19771e536952007-08-16 10:09:01 +0000351 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000352
danielk197701256832007-04-18 14:24:32 +0000353 if( !zModuleName ){
354 return SQLITE_NOMEM;
355 }
356
danielk19779da9d472006-06-14 06:58:15 +0000357 assert( !db->pVTab );
358 assert( xConstruct );
359
360 db->pVTab = pTab;
361 rc = sqlite3SafetyOff(db);
362 assert( rc==SQLITE_OK );
drh4ca8aac2006-09-10 17:31:58 +0000363 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pTab->pVtab, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000364 rc2 = sqlite3SafetyOn(db);
drhfe1368e2006-09-10 17:08:29 +0000365 pVtab = pTab->pVtab;
366 if( rc==SQLITE_OK && pVtab ){
367 pVtab->pModule = pMod->pModule;
368 pVtab->nRef = 1;
danielk19779da9d472006-06-14 06:58:15 +0000369 }
370
371 if( SQLITE_OK!=rc ){
drh4ca8aac2006-09-10 17:31:58 +0000372 if( zErr==0 ){
danielk19771e536952007-08-16 10:09:01 +0000373 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
drh4ca8aac2006-09-10 17:31:58 +0000374 }else {
danielk19771e536952007-08-16 10:09:01 +0000375 *pzErr = sqlite3MPrintf(db, "%s", zErr);
drh4ca8aac2006-09-10 17:31:58 +0000376 sqlite3_free(zErr);
drhfe1368e2006-09-10 17:08:29 +0000377 }
378 }else if( db->pVTab ){
danielk19779da9d472006-06-14 06:58:15 +0000379 const char *zFormat = "vtable constructor did not declare schema: %s";
danielk19771e536952007-08-16 10:09:01 +0000380 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000381 rc = SQLITE_ERROR;
382 }
383 if( rc==SQLITE_OK ){
384 rc = rc2;
385 }
386 db->pVTab = 0;
drh17435752007-08-16 04:30:38 +0000387 sqlite3_free(zModuleName);
danielk1977034ca142007-06-26 10:38:54 +0000388
389 /* If everything went according to plan, loop through the columns
390 ** of the table to see if any of them contain the token "hidden".
391 ** If so, set the Column.isHidden flag and remove the token from
392 ** the type string.
393 */
394 if( rc==SQLITE_OK ){
395 int iCol;
396 for(iCol=0; iCol<pTab->nCol; iCol++){
397 char *zType = pTab->aCol[iCol].zType;
398 int nType;
399 int i = 0;
400 if( !zType ) continue;
401 nType = strlen(zType);
402 if( sqlite3StrNICmp("hidden", zType, 6) || (zType[6] && zType[6]!=' ') ){
403 for(i=0; i<nType; i++){
404 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
405 && (zType[i+7]=='\0' || zType[i+7]==' ')
406 ){
407 i++;
408 break;
409 }
410 }
411 }
412 if( i<nType ){
413 int j;
414 int nDel = 6 + (zType[i+6] ? 1 : 0);
415 for(j=i; (j+nDel)<=nType; j++){
416 zType[j] = zType[j+nDel];
417 }
418 if( zType[i]=='\0' && i>0 ){
419 assert(zType[i-1]==' ');
420 zType[i-1] = '\0';
421 }
422 pTab->aCol[iCol].isHidden = 1;
423 }
424 }
425 }
danielk19779da9d472006-06-14 06:58:15 +0000426 return rc;
427}
428
429/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000430** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000431** of the virtual table pTab. If an error occurs, an error code is returned
432** and an error left in pParse.
433**
434** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000435*/
436int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000437 Module *pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000438 int rc = SQLITE_OK;
439
danielk1977fe3fcbe22006-06-12 12:08:45 +0000440 if( !pTab || !pTab->isVirtual || pTab->pVtab ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000441 return SQLITE_OK;
442 }
443
danielk1977d1ab1ba2006-06-15 04:28:13 +0000444 pMod = pTab->pMod;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000445 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000446 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000447 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000448 rc = SQLITE_ERROR;
449 } else {
danielk19779da9d472006-06-14 06:58:15 +0000450 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000451 sqlite3 *db = pParse->db;
452 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000453 if( rc!=SQLITE_OK ){
454 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000455 }
drh17435752007-08-16 04:30:38 +0000456 sqlite3_free(zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000457 }
458
459 return rc;
460}
461
danielk19779da9d472006-06-14 06:58:15 +0000462/*
danielk1977e7ff4032006-06-17 11:30:32 +0000463** Add the virtual table pVtab to the array sqlite3.aVTrans[].
464*/
danielk1977be718892006-06-23 08:05:19 +0000465static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
danielk1977e7ff4032006-06-17 11:30:32 +0000466 const int ARRAY_INCR = 5;
467
468 /* Grow the sqlite3.aVTrans array if required */
469 if( (db->nVTrans%ARRAY_INCR)==0 ){
470 sqlite3_vtab **aVTrans;
471 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
drh17435752007-08-16 04:30:38 +0000472 aVTrans = sqlite3_realloc((void *)db->aVTrans, nBytes);
danielk1977e7ff4032006-06-17 11:30:32 +0000473 if( !aVTrans ){
drh17435752007-08-16 04:30:38 +0000474 db->mallocFailed = 1;
danielk1977e7ff4032006-06-17 11:30:32 +0000475 return SQLITE_NOMEM;
476 }
477 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
478 db->aVTrans = aVTrans;
479 }
480
481 /* Add pVtab to the end of sqlite3.aVTrans */
482 db->aVTrans[db->nVTrans++] = pVtab;
drh189d4af2006-09-02 20:57:52 +0000483 sqlite3VtabLock(pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000484 return SQLITE_OK;
485}
486
487/*
danielk19779da9d472006-06-14 06:58:15 +0000488** This function is invoked by the vdbe to call the xCreate method
489** of the virtual table named zTab in database iDb.
490**
491** If an error occurs, *pzErr is set to point an an English language
492** description of the error and an SQLITE_XXX error code is returned.
drh17435752007-08-16 04:30:38 +0000493** In this case the caller must call sqlite3_free() on *pzErr.
danielk19779da9d472006-06-14 06:58:15 +0000494*/
495int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
496 int rc = SQLITE_OK;
497 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000498 Module *pMod;
danielk19779da9d472006-06-14 06:58:15 +0000499 const char *zModule;
500
501 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
502 assert(pTab && pTab->isVirtual && !pTab->pVtab);
danielk1977d1ab1ba2006-06-15 04:28:13 +0000503 pMod = pTab->pMod;
danielk19779da9d472006-06-14 06:58:15 +0000504 zModule = pTab->azModuleArg[0];
505
506 /* If the module has been registered and includes a Create method,
507 ** invoke it now. If the module has not been registered, return an
508 ** error. Otherwise, do nothing.
509 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000510 if( !pMod ){
danielk19771e536952007-08-16 10:09:01 +0000511 *pzErr = sqlite3MPrintf(db, "no such module: %s", zModule);
danielk19779da9d472006-06-14 06:58:15 +0000512 rc = SQLITE_ERROR;
513 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000514 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000515 }
516
danielk1977e7ff4032006-06-17 11:30:32 +0000517 if( rc==SQLITE_OK && pTab->pVtab ){
danielk197720b1eaf2006-07-26 16:22:14 +0000518 rc = addToVTrans(db, pTab->pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000519 }
520
danielk19779da9d472006-06-14 06:58:15 +0000521 return rc;
522}
danielk1977be8a7832006-06-13 15:00:54 +0000523
524/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000525** This function is used to set the schema of a virtual table. It is only
526** valid to call this function from within the xCreate() or xConnect() of a
527** virtual table module.
528*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000529int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
530 Parse sParse;
531
532 int rc = SQLITE_OK;
drh27641702007-08-22 02:56:42 +0000533 Table *pTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000534 char *zErr = 0;
535
drh27641702007-08-22 02:56:42 +0000536 sqlite3_mutex_enter(db->mutex);
537 pTab = db->pVTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000538 if( !pTab ){
539 sqlite3Error(db, SQLITE_MISUSE, 0);
drh27641702007-08-22 02:56:42 +0000540 sqlite3_mutex_leave(db->mutex);
danielk19777e6ebfb2006-06-12 11:24:37 +0000541 return SQLITE_MISUSE;
542 }
543 assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
544
545 memset(&sParse, 0, sizeof(Parse));
546 sParse.declareVtab = 1;
547 sParse.db = db;
548
549 if(
550 SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
551 sParse.pNewTable &&
552 !sParse.pNewTable->pSelect &&
553 !sParse.pNewTable->isVirtual
554 ){
555 pTab->aCol = sParse.pNewTable->aCol;
556 pTab->nCol = sParse.pNewTable->nCol;
557 sParse.pNewTable->nCol = 0;
558 sParse.pNewTable->aCol = 0;
danielk197701256832007-04-18 14:24:32 +0000559 db->pVTab = 0;
danielk19777e6ebfb2006-06-12 11:24:37 +0000560 } else {
561 sqlite3Error(db, SQLITE_ERROR, zErr);
drh17435752007-08-16 04:30:38 +0000562 sqlite3_free(zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000563 rc = SQLITE_ERROR;
564 }
565 sParse.declareVtab = 0;
566
567 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk1977a04a34f2007-04-16 15:06:25 +0000568 sqlite3DeleteTable(sParse.pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000569 sParse.pNewTable = 0;
danielk19777e6ebfb2006-06-12 11:24:37 +0000570
drh4ac285a2006-09-15 07:28:50 +0000571 assert( (rc&0xff)==rc );
drh27641702007-08-22 02:56:42 +0000572 rc = sqlite3ApiExit(db, rc);
573 sqlite3_mutex_leave(db->mutex);
574 return rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000575}
576
577/*
danielk19779e39ce82006-06-12 16:01:21 +0000578** This function is invoked by the vdbe to call the xDestroy method
579** of the virtual table named zTab in database iDb. This occurs
580** when a DROP TABLE is mentioned.
581**
582** This call is a no-op if zTab is not a virtual table.
583*/
584int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
585{
586 int rc = SQLITE_OK;
587 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000588
589 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk19779e39ce82006-06-12 16:01:21 +0000590 assert(pTab);
591 if( pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000592 int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
danielk19779e39ce82006-06-12 16:01:21 +0000593 rc = sqlite3SafetyOff(db);
594 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000595 if( xDestroy ){
596 rc = xDestroy(pTab->pVtab);
597 }
danielk19779e39ce82006-06-12 16:01:21 +0000598 sqlite3SafetyOn(db);
599 if( rc==SQLITE_OK ){
600 pTab->pVtab = 0;
601 }
602 }
603
604 return rc;
605}
606
danielk1977f9e7dda2006-06-16 16:08:53 +0000607/*
danielk1977e7ff4032006-06-17 11:30:32 +0000608** This function invokes either the xRollback or xCommit method
609** of each of the virtual tables in the sqlite3.aVTrans array. The method
610** called is identified by the second argument, "offset", which is
611** the offset of the method to call in the sqlite3_module structure.
612**
613** The array is cleared after invoking the callbacks.
614*/
615static void callFinaliser(sqlite3 *db, int offset){
616 int i;
danielk19770b83fa82007-04-19 14:48:37 +0000617 if( db->aVTrans ){
618 for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
619 sqlite3_vtab *pVtab = db->aVTrans[i];
620 int (*x)(sqlite3_vtab *);
621 x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
622 if( x ) x(pVtab);
623 sqlite3VtabUnlock(db, pVtab);
624 }
drh17435752007-08-16 04:30:38 +0000625 sqlite3_free(db->aVTrans);
danielk19770b83fa82007-04-19 14:48:37 +0000626 db->nVTrans = 0;
627 db->aVTrans = 0;
danielk1977e7ff4032006-06-17 11:30:32 +0000628 }
danielk1977e7ff4032006-06-17 11:30:32 +0000629}
630
631/*
danielk19775bd270b2006-07-25 15:14:52 +0000632** If argument rc2 is not SQLITE_OK, then return it and do nothing.
danielk1977f9e7dda2006-06-16 16:08:53 +0000633** Otherwise, invoke the xSync method of all virtual tables in the
634** sqlite3.aVTrans array. Return the error code for the first error
635** that occurs, or SQLITE_OK if all xSync operations are successful.
636*/
danielk1977e7ff4032006-06-17 11:30:32 +0000637int sqlite3VtabSync(sqlite3 *db, int rc2){
638 int i;
639 int rc = SQLITE_OK;
danielk19775bd270b2006-07-25 15:14:52 +0000640 int rcsafety;
danielk197720b1eaf2006-07-26 16:22:14 +0000641 sqlite3_vtab **aVTrans = db->aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000642 if( rc2!=SQLITE_OK ) return rc2;
danielk197720b1eaf2006-07-26 16:22:14 +0000643
danielk19775bd270b2006-07-25 15:14:52 +0000644 rc = sqlite3SafetyOff(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000645 db->aVTrans = 0;
646 for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
647 sqlite3_vtab *pVtab = aVTrans[i];
danielk1977e7ff4032006-06-17 11:30:32 +0000648 int (*x)(sqlite3_vtab *);
649 x = pVtab->pModule->xSync;
650 if( x ){
651 rc = x(pVtab);
652 }
653 }
danielk197720b1eaf2006-07-26 16:22:14 +0000654 db->aVTrans = aVTrans;
danielk19775bd270b2006-07-25 15:14:52 +0000655 rcsafety = sqlite3SafetyOn(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000656
danielk19775bd270b2006-07-25 15:14:52 +0000657 if( rc==SQLITE_OK ){
658 rc = rcsafety;
659 }
danielk1977e7ff4032006-06-17 11:30:32 +0000660 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000661}
662
663/*
664** Invoke the xRollback method of all virtual tables in the
665** sqlite3.aVTrans array. Then clear the array itself.
666*/
667int sqlite3VtabRollback(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000668 callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback));
669 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000670}
671
672/*
673** Invoke the xCommit method of all virtual tables in the
674** sqlite3.aVTrans array. Then clear the array itself.
675*/
676int sqlite3VtabCommit(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000677 callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit));
678 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000679}
680
681/*
682** If the virtual table pVtab supports the transaction interface
683** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
684** not currently open, invoke the xBegin method now.
685**
686** If the xBegin call is successful, place the sqlite3_vtab pointer
687** in the sqlite3.aVTrans array.
688*/
689int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
690 int rc = SQLITE_OK;
danielk197720b1eaf2006-07-26 16:22:14 +0000691 const sqlite3_module *pModule;
692
693 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
694 ** than zero, then this function is being called from within a
695 ** virtual module xSync() callback. It is illegal to write to
696 ** virtual module tables in this case, so return SQLITE_LOCKED.
697 */
698 if( 0==db->aVTrans && db->nVTrans>0 ){
699 return SQLITE_LOCKED;
700 }
701 if( !pVtab ){
702 return SQLITE_OK;
703 }
704 pModule = pVtab->pModule;
705
danielk1977f9e7dda2006-06-16 16:08:53 +0000706 if( pModule->xBegin ){
707 int i;
708
danielk197720b1eaf2006-07-26 16:22:14 +0000709
danielk1977f9e7dda2006-06-16 16:08:53 +0000710 /* If pVtab is already in the aVTrans array, return early */
711 for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
712 if( db->aVTrans[i]==pVtab ){
713 return SQLITE_OK;
714 }
715 }
716
717 /* Invoke the xBegin method */
718 rc = pModule->xBegin(pVtab);
719 if( rc!=SQLITE_OK ){
720 return rc;
721 }
722
danielk1977e7ff4032006-06-17 11:30:32 +0000723 rc = addToVTrans(db, pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +0000724 }
725 return rc;
726}
727
drhb7f6f682006-07-08 17:06:43 +0000728/*
729** The first parameter (pDef) is a function implementation. The
730** second parameter (pExpr) is the first argument to this function.
731** If pExpr is a column in a virtual table, then let the virtual
732** table implementation have an opportunity to overload the function.
733**
734** This routine is used to allow virtual table implementations to
735** overload MATCH, LIKE, GLOB, and REGEXP operators.
736**
737** Return either the pDef argument (indicating no change) or a
738** new FuncDef structure that is marked as ephemeral using the
739** SQLITE_FUNC_EPHEM flag.
740*/
741FuncDef *sqlite3VtabOverloadFunction(
drh17435752007-08-16 04:30:38 +0000742 sqlite3 *db, /* Database connection for reporting malloc problems */
drhb7f6f682006-07-08 17:06:43 +0000743 FuncDef *pDef, /* Function to possibly overload */
744 int nArg, /* Number of arguments to the function */
745 Expr *pExpr /* First argument to the function */
746){
747 Table *pTab;
748 sqlite3_vtab *pVtab;
749 sqlite3_module *pMod;
drhe94b0c32006-07-08 18:09:15 +0000750 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
drhb7f6f682006-07-08 17:06:43 +0000751 void *pArg;
drhb7f6f682006-07-08 17:06:43 +0000752 FuncDef *pNew;
drha70034d2006-09-18 20:24:02 +0000753 int rc;
754 char *zLowerName;
755 unsigned char *z;
756
drhb7f6f682006-07-08 17:06:43 +0000757
758 /* Check to see the left operand is a column in a virtual table */
759 if( pExpr==0 ) return pDef;
760 if( pExpr->op!=TK_COLUMN ) return pDef;
761 pTab = pExpr->pTab;
762 if( pTab==0 ) return pDef;
763 if( !pTab->isVirtual ) return pDef;
764 pVtab = pTab->pVtab;
765 assert( pVtab!=0 );
766 assert( pVtab->pModule!=0 );
danielk19775bd270b2006-07-25 15:14:52 +0000767 pMod = (sqlite3_module *)pVtab->pModule;
drhb7f6f682006-07-08 17:06:43 +0000768 if( pMod->xFindFunction==0 ) return pDef;
769
770 /* Call the xFuncFunction method on the virtual table implementation
drha70034d2006-09-18 20:24:02 +0000771 ** to see if the implementation wants to overload this function
772 */
drh17435752007-08-16 04:30:38 +0000773 zLowerName = sqlite3DbStrDup(db, pDef->zName);
774 if( zLowerName ){
775 for(z=(unsigned char*)zLowerName; *z; z++){
776 *z = sqlite3UpperToLower[*z];
777 }
778 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
779 sqlite3_free(zLowerName);
drha70034d2006-09-18 20:24:02 +0000780 }
drha70034d2006-09-18 20:24:02 +0000781 if( rc==0 ){
drhb7f6f682006-07-08 17:06:43 +0000782 return pDef;
783 }
784
785 /* Create a new ephemeral function definition for the overloaded
786 ** function */
drh17435752007-08-16 04:30:38 +0000787 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) );
drhb7f6f682006-07-08 17:06:43 +0000788 if( pNew==0 ){
789 return pDef;
790 }
791 *pNew = *pDef;
drh5bb3eb92007-05-04 13:15:55 +0000792 memcpy(pNew->zName, pDef->zName, strlen(pDef->zName)+1);
drhb7f6f682006-07-08 17:06:43 +0000793 pNew->xFunc = xFunc;
794 pNew->pUserData = pArg;
drhb7f6f682006-07-08 17:06:43 +0000795 pNew->flags |= SQLITE_FUNC_EPHEM;
796 return pNew;
797}
798
drhb9bb7c12006-06-11 23:41:55 +0000799#endif /* SQLITE_OMIT_VIRTUALTABLE */