blob: 09793aef7524a5fc033386aaec022a5f3c7d5434 [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**
drh4a50aac2007-08-23 02:47:53 +000014** $Id: vtab.c,v 1.53 2007/08/23 02:47:53 drh 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;
27
28 sqlite3_mutex_enter(db->mutex);
29 nName = strlen(zName);
drh17435752007-08-16 04:30:38 +000030 Module *pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
danielk1977832a58a2007-06-22 15:21:15 +000031 if( pMod ){
32 char *zCopy = (char *)(&pMod[1]);
33 memcpy(zCopy, zName, nName+1);
34 pMod->zName = zCopy;
35 pMod->pModule = pModule;
36 pMod->pAux = pAux;
37 pMod->xDestroy = xDestroy;
38 pMod = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
39 if( pMod && pMod->xDestroy ){
40 pMod->xDestroy(pMod->pAux);
41 }
drh17435752007-08-16 04:30:38 +000042 sqlite3_free(pMod);
danielk1977832a58a2007-06-22 15:21:15 +000043 sqlite3ResetInternalSchema(db, 0);
44 }
drh27641702007-08-22 02:56:42 +000045 rc = sqlite3ApiExit(db, SQLITE_OK);
46 sqlite3_mutex_leave(db->mutex);
47 return rc;
danielk1977832a58a2007-06-22 15:21:15 +000048}
49
50
drhb9bb7c12006-06-11 23:41:55 +000051/*
52** External API function used to create a new virtual-table module.
53*/
54int sqlite3_create_module(
55 sqlite3 *db, /* Database in which module is registered */
56 const char *zName, /* Name assigned to this module */
danielk1977d1ab1ba2006-06-15 04:28:13 +000057 const sqlite3_module *pModule, /* The definition of the module */
58 void *pAux /* Context pointer for xCreate/xConnect */
drhb9bb7c12006-06-11 23:41:55 +000059){
danielk1977832a58a2007-06-22 15:21:15 +000060 return createModule(db, zName, pModule, pAux, 0);
61}
62
63/*
64** External API function used to create a new virtual-table module.
65*/
66int sqlite3_create_module_v2(
67 sqlite3 *db, /* Database in which module is registered */
68 const char *zName, /* Name assigned to this module */
69 const sqlite3_module *pModule, /* The definition of the module */
70 void *pAux, /* Context pointer for xCreate/xConnect */
71 void (*xDestroy)(void *) /* Module destructor function */
72){
73 return createModule(db, zName, pModule, pAux, xDestroy);
drhb9bb7c12006-06-11 23:41:55 +000074}
75
drhb9bb7c12006-06-11 23:41:55 +000076/*
drh189d4af2006-09-02 20:57:52 +000077** Lock the virtual table so that it cannot be disconnected.
78** Locks nest. Every lock should have a corresponding unlock.
79** If an unlock is omitted, resources leaks will occur.
80**
81** If a disconnect is attempted while a virtual table is locked,
82** the disconnect is deferred until all locks have been removed.
83*/
84void sqlite3VtabLock(sqlite3_vtab *pVtab){
85 pVtab->nRef++;
86}
87
88/*
89** Unlock a virtual table. When the last lock is removed,
90** disconnect the virtual table.
91*/
danielk1977a04a34f2007-04-16 15:06:25 +000092void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
drh189d4af2006-09-02 20:57:52 +000093 pVtab->nRef--;
danielk1977a04a34f2007-04-16 15:06:25 +000094 assert(db);
95 assert(!sqlite3SafetyCheck(db));
drh189d4af2006-09-02 20:57:52 +000096 if( pVtab->nRef==0 ){
danielk1977a04a34f2007-04-16 15:06:25 +000097 if( db->magic==SQLITE_MAGIC_BUSY ){
98 sqlite3SafetyOff(db);
99 pVtab->pModule->xDisconnect(pVtab);
100 sqlite3SafetyOn(db);
101 } else {
102 pVtab->pModule->xDisconnect(pVtab);
103 }
drh189d4af2006-09-02 20:57:52 +0000104 }
105}
106
107/*
drhb9bb7c12006-06-11 23:41:55 +0000108** Clear any and all virtual-table information from the Table record.
109** This routine is called, for example, just before deleting the Table
110** record.
111*/
112void sqlite3VtabClear(Table *p){
danielk1977be718892006-06-23 08:05:19 +0000113 sqlite3_vtab *pVtab = p->pVtab;
114 if( pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000115 assert( p->pMod && p->pMod->pModule );
danielk1977a04a34f2007-04-16 15:06:25 +0000116 sqlite3VtabUnlock(p->pSchema->db, pVtab);
danielk1977be718892006-06-23 08:05:19 +0000117 p->pVtab = 0;
drhb9bb7c12006-06-11 23:41:55 +0000118 }
119 if( p->azModuleArg ){
120 int i;
121 for(i=0; i<p->nModuleArg; i++){
drh17435752007-08-16 04:30:38 +0000122 sqlite3_free(p->azModuleArg[i]);
drhb9bb7c12006-06-11 23:41:55 +0000123 }
drh17435752007-08-16 04:30:38 +0000124 sqlite3_free(p->azModuleArg);
drhb9bb7c12006-06-11 23:41:55 +0000125 }
126}
127
128/*
129** Add a new module argument to pTable->azModuleArg[].
130** The string is not copied - the pointer is stored. The
131** string will be freed automatically when the table is
132** deleted.
133*/
drh17435752007-08-16 04:30:38 +0000134static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
drhb9bb7c12006-06-11 23:41:55 +0000135 int i = pTable->nModuleArg++;
danielk1977b7a2f2e2006-06-23 11:34:54 +0000136 int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
137 char **azModuleArg;
drh17435752007-08-16 04:30:38 +0000138 azModuleArg = sqlite3_realloc(pTable->azModuleArg, nBytes);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000139 if( azModuleArg==0 ){
140 int j;
141 for(j=0; j<i; j++){
drh17435752007-08-16 04:30:38 +0000142 sqlite3_free(pTable->azModuleArg[j]);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000143 }
drh17435752007-08-16 04:30:38 +0000144 sqlite3_free(zArg);
145 sqlite3_free(pTable->azModuleArg);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000146 pTable->nModuleArg = 0;
drh17435752007-08-16 04:30:38 +0000147 db->mallocFailed = 1;
drhb9bb7c12006-06-11 23:41:55 +0000148 }else{
danielk1977b7a2f2e2006-06-23 11:34:54 +0000149 azModuleArg[i] = zArg;
150 azModuleArg[i+1] = 0;
drhb9bb7c12006-06-11 23:41:55 +0000151 }
danielk1977b7a2f2e2006-06-23 11:34:54 +0000152 pTable->azModuleArg = azModuleArg;
drhb9bb7c12006-06-11 23:41:55 +0000153}
154
155/*
156** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
157** statement. The module name has been parsed, but the optional list
158** of parameters that follow the module name are still pending.
159*/
160void sqlite3VtabBeginParse(
161 Parse *pParse, /* Parsing context */
162 Token *pName1, /* Name of new table, or database name */
163 Token *pName2, /* Name of new table or NULL */
164 Token *pModuleName /* Name of the module for the virtual table */
165){
danielk1977f1a381e2006-06-16 08:01:02 +0000166 int iDb; /* The database the table is being created in */
drhb9bb7c12006-06-11 23:41:55 +0000167 Table *pTable; /* The new virtual table */
drh17435752007-08-16 04:30:38 +0000168 sqlite3 *db; /* Database connection */
drhb9bb7c12006-06-11 23:41:55 +0000169
drh4a50aac2007-08-23 02:47:53 +0000170 if( pParse->db->flags & SQLITE_SharedCache ){
danielk1977113e5452007-04-16 15:49:41 +0000171 sqlite3ErrorMsg(pParse, "Cannot use virtual tables in shared-cache mode");
172 return;
173 }
174
danielk1977f1a381e2006-06-16 08:01:02 +0000175 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
drhb9bb7c12006-06-11 23:41:55 +0000176 pTable = pParse->pNewTable;
danielk1977f1a381e2006-06-16 08:01:02 +0000177 if( pTable==0 || pParse->nErr ) return;
178 assert( 0==pTable->pIndex );
179
drh17435752007-08-16 04:30:38 +0000180 db = pParse->db;
181 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
danielk197770ba1642006-06-21 16:02:42 +0000182 assert( iDb>=0 );
183
drhb9bb7c12006-06-11 23:41:55 +0000184 pTable->isVirtual = 1;
185 pTable->nModuleArg = 0;
drh17435752007-08-16 04:30:38 +0000186 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
187 addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
188 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
drhb9bb7c12006-06-11 23:41:55 +0000189 pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
danielk1977f1a381e2006-06-16 08:01:02 +0000190
191#ifndef SQLITE_OMIT_AUTHORIZATION
192 /* Creating a virtual table invokes the authorization callback twice.
193 ** The first invocation, to obtain permission to INSERT a row into the
194 ** sqlite_master table, has already been made by sqlite3StartTable().
195 ** The second call, to obtain permission to create the table, is made now.
196 */
danielk1977be718892006-06-23 08:05:19 +0000197 if( pTable->azModuleArg ){
198 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
199 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
danielk1977f1a381e2006-06-16 08:01:02 +0000200 }
201#endif
drhb9bb7c12006-06-11 23:41:55 +0000202}
203
204/*
205** This routine takes the module argument that has been accumulating
206** in pParse->zArg[] and appends it to the list of arguments on the
207** virtual table currently under construction in pParse->pTable.
208*/
209static void addArgumentToVtab(Parse *pParse){
danielk197733b39332006-06-24 08:51:05 +0000210 if( pParse->sArg.z && pParse->pNewTable ){
drh7c2d87c2006-09-02 14:16:59 +0000211 const char *z = (const char*)pParse->sArg.z;
danielk197733b39332006-06-24 08:51:05 +0000212 int n = pParse->sArg.n;
drh17435752007-08-16 04:30:38 +0000213 sqlite3 *db = pParse->db;
214 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
drhb9bb7c12006-06-11 23:41:55 +0000215 }
drhb9bb7c12006-06-11 23:41:55 +0000216}
217
218/*
219** The parser calls this routine after the CREATE VIRTUAL TABLE statement
220** has been completely parsed.
221*/
222void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
223 Table *pTab; /* The table being constructed */
224 sqlite3 *db; /* The database connection */
225 char *zModule; /* The module name of the table: USING modulename */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000226 Module *pMod = 0;
drhb9bb7c12006-06-11 23:41:55 +0000227
228 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000229 pParse->sArg.z = 0;
drhb9bb7c12006-06-11 23:41:55 +0000230
231 /* Lookup the module name. */
232 pTab = pParse->pNewTable;
233 if( pTab==0 ) return;
234 db = pParse->db;
235 if( pTab->nModuleArg<1 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000236 zModule = pTab->azModuleArg[0];
danielk1977d1ab1ba2006-06-15 04:28:13 +0000237 pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
238 pTab->pMod = pMod;
drhb9bb7c12006-06-11 23:41:55 +0000239
240 /* If the CREATE VIRTUAL TABLE statement is being entered for the
241 ** first time (in other words if the virtual table is actually being
242 ** created now instead of just being read out of sqlite_master) then
243 ** do additional initialization work and store the statement text
244 ** in the sqlite_master table.
245 */
246 if( !db->init.busy ){
247 char *zStmt;
danielk197778efaba2006-06-12 06:09:17 +0000248 char *zWhere;
drhb9bb7c12006-06-11 23:41:55 +0000249 int iDb;
250 Vdbe *v;
drhb9bb7c12006-06-11 23:41:55 +0000251
252 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
253 if( pEnd ){
254 pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
255 }
danielk19771e536952007-08-16 10:09:01 +0000256 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
drhb9bb7c12006-06-11 23:41:55 +0000257
258 /* A slot for the record has already been allocated in the
259 ** SQLITE_MASTER table. We just need to update that slot with all
danielk197778efaba2006-06-12 06:09:17 +0000260 ** the information we've collected.
261 **
262 ** The top of the stack is the rootpage allocated by sqlite3StartTable().
263 ** This value is always 0 and is ignored, a virtual table does not have a
264 ** rootpage. The next entry on the stack is the rowid of the record
265 ** in the sqlite_master table.
drhb9bb7c12006-06-11 23:41:55 +0000266 */
267 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
268 sqlite3NestedParse(pParse,
269 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000270 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
271 "WHERE rowid=#1",
drhb9bb7c12006-06-11 23:41:55 +0000272 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
273 pTab->zName,
274 pTab->zName,
275 zStmt
276 );
drh17435752007-08-16 04:30:38 +0000277 sqlite3_free(zStmt);
drhb9bb7c12006-06-11 23:41:55 +0000278 v = sqlite3GetVdbe(pParse);
drhb9bb7c12006-06-11 23:41:55 +0000279 sqlite3ChangeCookie(db, v, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000280
281 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
danielk19771e536952007-08-16 10:09:01 +0000282 zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
drh3c23a882007-01-09 14:01:13 +0000283 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 1, zWhere, P3_DYNAMIC);
danielk197778efaba2006-06-12 06:09:17 +0000284 sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
drhb9bb7c12006-06-11 23:41:55 +0000285 }
286
danielk197778efaba2006-06-12 06:09:17 +0000287 /* If we are rereading the sqlite_master table create the in-memory
danielk1977c7d54102006-06-15 07:29:00 +0000288 ** record of the table. If the module has already been registered,
289 ** also call the xConnect method here.
drhb9bb7c12006-06-11 23:41:55 +0000290 */
danielk197778efaba2006-06-12 06:09:17 +0000291 else {
danielk197778efaba2006-06-12 06:09:17 +0000292 Table *pOld;
293 Schema *pSchema = pTab->pSchema;
294 const char *zName = pTab->zName;
295 int nName = strlen(zName) + 1;
296 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
297 if( pOld ){
298 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
299 return;
300 }
danielk1977a04a34f2007-04-16 15:06:25 +0000301 pSchema->db = pParse->db;
danielk19777e6ebfb2006-06-12 11:24:37 +0000302 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000303 }
304}
305
306/*
307** The parser calls this routine when it sees the first token
308** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
309*/
310void sqlite3VtabArgInit(Parse *pParse){
311 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000312 pParse->sArg.z = 0;
313 pParse->sArg.n = 0;
drhb9bb7c12006-06-11 23:41:55 +0000314}
315
316/*
317** The parser calls this routine for each token after the first token
318** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
319*/
320void sqlite3VtabArgExtend(Parse *pParse, Token *p){
danielk197733b39332006-06-24 08:51:05 +0000321 Token *pArg = &pParse->sArg;
322 if( pArg->z==0 ){
323 pArg->z = p->z;
324 pArg->n = p->n;
325 }else{
326 assert(pArg->z < p->z);
327 pArg->n = (p->z + p->n - pArg->z);
drhb9bb7c12006-06-11 23:41:55 +0000328 }
drhb9bb7c12006-06-11 23:41:55 +0000329}
330
danielk197778efaba2006-06-12 06:09:17 +0000331/*
danielk19779da9d472006-06-14 06:58:15 +0000332** Invoke a virtual table constructor (either xCreate or xConnect). The
333** pointer to the function to invoke is passed as the fourth parameter
334** to this procedure.
335*/
336static int vtabCallConstructor(
337 sqlite3 *db,
338 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000339 Module *pMod,
drhe4102962006-09-11 00:34:22 +0000340 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
danielk19779da9d472006-06-14 06:58:15 +0000341 char **pzErr
342){
343 int rc;
344 int rc2;
drhfe1368e2006-09-10 17:08:29 +0000345 sqlite3_vtab *pVtab;
drhe4102962006-09-11 00:34:22 +0000346 const char *const*azArg = (const char *const*)pTab->azModuleArg;
danielk19779da9d472006-06-14 06:58:15 +0000347 int nArg = pTab->nModuleArg;
drh4ca8aac2006-09-10 17:31:58 +0000348 char *zErr = 0;
danielk19771e536952007-08-16 10:09:01 +0000349 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000350
danielk197701256832007-04-18 14:24:32 +0000351 if( !zModuleName ){
352 return SQLITE_NOMEM;
353 }
354
danielk19779da9d472006-06-14 06:58:15 +0000355 assert( !db->pVTab );
356 assert( xConstruct );
357
358 db->pVTab = pTab;
359 rc = sqlite3SafetyOff(db);
360 assert( rc==SQLITE_OK );
drh4ca8aac2006-09-10 17:31:58 +0000361 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pTab->pVtab, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000362 rc2 = sqlite3SafetyOn(db);
drhfe1368e2006-09-10 17:08:29 +0000363 pVtab = pTab->pVtab;
364 if( rc==SQLITE_OK && pVtab ){
365 pVtab->pModule = pMod->pModule;
366 pVtab->nRef = 1;
danielk19779da9d472006-06-14 06:58:15 +0000367 }
368
369 if( SQLITE_OK!=rc ){
drh4ca8aac2006-09-10 17:31:58 +0000370 if( zErr==0 ){
danielk19771e536952007-08-16 10:09:01 +0000371 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
drh4ca8aac2006-09-10 17:31:58 +0000372 }else {
danielk19771e536952007-08-16 10:09:01 +0000373 *pzErr = sqlite3MPrintf(db, "%s", zErr);
drh4ca8aac2006-09-10 17:31:58 +0000374 sqlite3_free(zErr);
drhfe1368e2006-09-10 17:08:29 +0000375 }
376 }else if( db->pVTab ){
danielk19779da9d472006-06-14 06:58:15 +0000377 const char *zFormat = "vtable constructor did not declare schema: %s";
danielk19771e536952007-08-16 10:09:01 +0000378 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000379 rc = SQLITE_ERROR;
380 }
381 if( rc==SQLITE_OK ){
382 rc = rc2;
383 }
384 db->pVTab = 0;
drh17435752007-08-16 04:30:38 +0000385 sqlite3_free(zModuleName);
danielk1977034ca142007-06-26 10:38:54 +0000386
387 /* If everything went according to plan, loop through the columns
388 ** of the table to see if any of them contain the token "hidden".
389 ** If so, set the Column.isHidden flag and remove the token from
390 ** the type string.
391 */
392 if( rc==SQLITE_OK ){
393 int iCol;
394 for(iCol=0; iCol<pTab->nCol; iCol++){
395 char *zType = pTab->aCol[iCol].zType;
396 int nType;
397 int i = 0;
398 if( !zType ) continue;
399 nType = strlen(zType);
400 if( sqlite3StrNICmp("hidden", zType, 6) || (zType[6] && zType[6]!=' ') ){
401 for(i=0; i<nType; i++){
402 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
403 && (zType[i+7]=='\0' || zType[i+7]==' ')
404 ){
405 i++;
406 break;
407 }
408 }
409 }
410 if( i<nType ){
411 int j;
412 int nDel = 6 + (zType[i+6] ? 1 : 0);
413 for(j=i; (j+nDel)<=nType; j++){
414 zType[j] = zType[j+nDel];
415 }
416 if( zType[i]=='\0' && i>0 ){
417 assert(zType[i-1]==' ');
418 zType[i-1] = '\0';
419 }
420 pTab->aCol[iCol].isHidden = 1;
421 }
422 }
423 }
danielk19779da9d472006-06-14 06:58:15 +0000424 return rc;
425}
426
427/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000428** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000429** of the virtual table pTab. If an error occurs, an error code is returned
430** and an error left in pParse.
431**
432** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000433*/
434int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000435 Module *pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000436 int rc = SQLITE_OK;
437
danielk1977fe3fcbe22006-06-12 12:08:45 +0000438 if( !pTab || !pTab->isVirtual || pTab->pVtab ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000439 return SQLITE_OK;
440 }
441
danielk1977d1ab1ba2006-06-15 04:28:13 +0000442 pMod = pTab->pMod;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000443 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000444 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000445 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000446 rc = SQLITE_ERROR;
447 } else {
danielk19779da9d472006-06-14 06:58:15 +0000448 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000449 sqlite3 *db = pParse->db;
450 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000451 if( rc!=SQLITE_OK ){
452 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000453 }
drh17435752007-08-16 04:30:38 +0000454 sqlite3_free(zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000455 }
456
457 return rc;
458}
459
danielk19779da9d472006-06-14 06:58:15 +0000460/*
danielk1977e7ff4032006-06-17 11:30:32 +0000461** Add the virtual table pVtab to the array sqlite3.aVTrans[].
462*/
danielk1977be718892006-06-23 08:05:19 +0000463static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
danielk1977e7ff4032006-06-17 11:30:32 +0000464 const int ARRAY_INCR = 5;
465
466 /* Grow the sqlite3.aVTrans array if required */
467 if( (db->nVTrans%ARRAY_INCR)==0 ){
468 sqlite3_vtab **aVTrans;
469 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
drh17435752007-08-16 04:30:38 +0000470 aVTrans = sqlite3_realloc((void *)db->aVTrans, nBytes);
danielk1977e7ff4032006-06-17 11:30:32 +0000471 if( !aVTrans ){
drh17435752007-08-16 04:30:38 +0000472 db->mallocFailed = 1;
danielk1977e7ff4032006-06-17 11:30:32 +0000473 return SQLITE_NOMEM;
474 }
475 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
476 db->aVTrans = aVTrans;
477 }
478
479 /* Add pVtab to the end of sqlite3.aVTrans */
480 db->aVTrans[db->nVTrans++] = pVtab;
drh189d4af2006-09-02 20:57:52 +0000481 sqlite3VtabLock(pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000482 return SQLITE_OK;
483}
484
485/*
danielk19779da9d472006-06-14 06:58:15 +0000486** This function is invoked by the vdbe to call the xCreate method
487** of the virtual table named zTab in database iDb.
488**
489** If an error occurs, *pzErr is set to point an an English language
490** description of the error and an SQLITE_XXX error code is returned.
drh17435752007-08-16 04:30:38 +0000491** In this case the caller must call sqlite3_free() on *pzErr.
danielk19779da9d472006-06-14 06:58:15 +0000492*/
493int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
494 int rc = SQLITE_OK;
495 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000496 Module *pMod;
danielk19779da9d472006-06-14 06:58:15 +0000497 const char *zModule;
498
499 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
500 assert(pTab && pTab->isVirtual && !pTab->pVtab);
danielk1977d1ab1ba2006-06-15 04:28:13 +0000501 pMod = pTab->pMod;
danielk19779da9d472006-06-14 06:58:15 +0000502 zModule = pTab->azModuleArg[0];
503
504 /* If the module has been registered and includes a Create method,
505 ** invoke it now. If the module has not been registered, return an
506 ** error. Otherwise, do nothing.
507 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000508 if( !pMod ){
danielk19771e536952007-08-16 10:09:01 +0000509 *pzErr = sqlite3MPrintf(db, "no such module: %s", zModule);
danielk19779da9d472006-06-14 06:58:15 +0000510 rc = SQLITE_ERROR;
511 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000512 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000513 }
514
danielk1977e7ff4032006-06-17 11:30:32 +0000515 if( rc==SQLITE_OK && pTab->pVtab ){
danielk197720b1eaf2006-07-26 16:22:14 +0000516 rc = addToVTrans(db, pTab->pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000517 }
518
danielk19779da9d472006-06-14 06:58:15 +0000519 return rc;
520}
danielk1977be8a7832006-06-13 15:00:54 +0000521
522/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000523** This function is used to set the schema of a virtual table. It is only
524** valid to call this function from within the xCreate() or xConnect() of a
525** virtual table module.
526*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000527int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
528 Parse sParse;
529
530 int rc = SQLITE_OK;
drh27641702007-08-22 02:56:42 +0000531 Table *pTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000532 char *zErr = 0;
533
drh27641702007-08-22 02:56:42 +0000534 sqlite3_mutex_enter(db->mutex);
535 pTab = db->pVTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000536 if( !pTab ){
537 sqlite3Error(db, SQLITE_MISUSE, 0);
drh27641702007-08-22 02:56:42 +0000538 sqlite3_mutex_leave(db->mutex);
danielk19777e6ebfb2006-06-12 11:24:37 +0000539 return SQLITE_MISUSE;
540 }
541 assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
542
543 memset(&sParse, 0, sizeof(Parse));
544 sParse.declareVtab = 1;
545 sParse.db = db;
546
547 if(
548 SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
549 sParse.pNewTable &&
550 !sParse.pNewTable->pSelect &&
551 !sParse.pNewTable->isVirtual
552 ){
553 pTab->aCol = sParse.pNewTable->aCol;
554 pTab->nCol = sParse.pNewTable->nCol;
555 sParse.pNewTable->nCol = 0;
556 sParse.pNewTable->aCol = 0;
danielk197701256832007-04-18 14:24:32 +0000557 db->pVTab = 0;
danielk19777e6ebfb2006-06-12 11:24:37 +0000558 } else {
559 sqlite3Error(db, SQLITE_ERROR, zErr);
drh17435752007-08-16 04:30:38 +0000560 sqlite3_free(zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000561 rc = SQLITE_ERROR;
562 }
563 sParse.declareVtab = 0;
564
565 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk1977a04a34f2007-04-16 15:06:25 +0000566 sqlite3DeleteTable(sParse.pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000567 sParse.pNewTable = 0;
danielk19777e6ebfb2006-06-12 11:24:37 +0000568
drh4ac285a2006-09-15 07:28:50 +0000569 assert( (rc&0xff)==rc );
drh27641702007-08-22 02:56:42 +0000570 rc = sqlite3ApiExit(db, rc);
571 sqlite3_mutex_leave(db->mutex);
572 return rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000573}
574
575/*
danielk19779e39ce82006-06-12 16:01:21 +0000576** This function is invoked by the vdbe to call the xDestroy method
577** of the virtual table named zTab in database iDb. This occurs
578** when a DROP TABLE is mentioned.
579**
580** This call is a no-op if zTab is not a virtual table.
581*/
582int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
583{
584 int rc = SQLITE_OK;
585 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000586
587 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk19779e39ce82006-06-12 16:01:21 +0000588 assert(pTab);
589 if( pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000590 int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
danielk19779e39ce82006-06-12 16:01:21 +0000591 rc = sqlite3SafetyOff(db);
592 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000593 if( xDestroy ){
594 rc = xDestroy(pTab->pVtab);
595 }
danielk19779e39ce82006-06-12 16:01:21 +0000596 sqlite3SafetyOn(db);
597 if( rc==SQLITE_OK ){
598 pTab->pVtab = 0;
599 }
600 }
601
602 return rc;
603}
604
danielk1977f9e7dda2006-06-16 16:08:53 +0000605/*
danielk1977e7ff4032006-06-17 11:30:32 +0000606** This function invokes either the xRollback or xCommit method
607** of each of the virtual tables in the sqlite3.aVTrans array. The method
608** called is identified by the second argument, "offset", which is
609** the offset of the method to call in the sqlite3_module structure.
610**
611** The array is cleared after invoking the callbacks.
612*/
613static void callFinaliser(sqlite3 *db, int offset){
614 int i;
danielk19770b83fa82007-04-19 14:48:37 +0000615 if( db->aVTrans ){
616 for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
617 sqlite3_vtab *pVtab = db->aVTrans[i];
618 int (*x)(sqlite3_vtab *);
619 x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
620 if( x ) x(pVtab);
621 sqlite3VtabUnlock(db, pVtab);
622 }
drh17435752007-08-16 04:30:38 +0000623 sqlite3_free(db->aVTrans);
danielk19770b83fa82007-04-19 14:48:37 +0000624 db->nVTrans = 0;
625 db->aVTrans = 0;
danielk1977e7ff4032006-06-17 11:30:32 +0000626 }
danielk1977e7ff4032006-06-17 11:30:32 +0000627}
628
629/*
danielk19775bd270b2006-07-25 15:14:52 +0000630** If argument rc2 is not SQLITE_OK, then return it and do nothing.
danielk1977f9e7dda2006-06-16 16:08:53 +0000631** Otherwise, invoke the xSync method of all virtual tables in the
632** sqlite3.aVTrans array. Return the error code for the first error
633** that occurs, or SQLITE_OK if all xSync operations are successful.
634*/
danielk1977e7ff4032006-06-17 11:30:32 +0000635int sqlite3VtabSync(sqlite3 *db, int rc2){
636 int i;
637 int rc = SQLITE_OK;
danielk19775bd270b2006-07-25 15:14:52 +0000638 int rcsafety;
danielk197720b1eaf2006-07-26 16:22:14 +0000639 sqlite3_vtab **aVTrans = db->aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000640 if( rc2!=SQLITE_OK ) return rc2;
danielk197720b1eaf2006-07-26 16:22:14 +0000641
danielk19775bd270b2006-07-25 15:14:52 +0000642 rc = sqlite3SafetyOff(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000643 db->aVTrans = 0;
644 for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
645 sqlite3_vtab *pVtab = aVTrans[i];
danielk1977e7ff4032006-06-17 11:30:32 +0000646 int (*x)(sqlite3_vtab *);
647 x = pVtab->pModule->xSync;
648 if( x ){
649 rc = x(pVtab);
650 }
651 }
danielk197720b1eaf2006-07-26 16:22:14 +0000652 db->aVTrans = aVTrans;
danielk19775bd270b2006-07-25 15:14:52 +0000653 rcsafety = sqlite3SafetyOn(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000654
danielk19775bd270b2006-07-25 15:14:52 +0000655 if( rc==SQLITE_OK ){
656 rc = rcsafety;
657 }
danielk1977e7ff4032006-06-17 11:30:32 +0000658 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000659}
660
661/*
662** Invoke the xRollback method of all virtual tables in the
663** sqlite3.aVTrans array. Then clear the array itself.
664*/
665int sqlite3VtabRollback(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000666 callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback));
667 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000668}
669
670/*
671** Invoke the xCommit method of all virtual tables in the
672** sqlite3.aVTrans array. Then clear the array itself.
673*/
674int sqlite3VtabCommit(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000675 callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit));
676 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000677}
678
679/*
680** If the virtual table pVtab supports the transaction interface
681** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
682** not currently open, invoke the xBegin method now.
683**
684** If the xBegin call is successful, place the sqlite3_vtab pointer
685** in the sqlite3.aVTrans array.
686*/
687int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
688 int rc = SQLITE_OK;
danielk197720b1eaf2006-07-26 16:22:14 +0000689 const sqlite3_module *pModule;
690
691 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
692 ** than zero, then this function is being called from within a
693 ** virtual module xSync() callback. It is illegal to write to
694 ** virtual module tables in this case, so return SQLITE_LOCKED.
695 */
696 if( 0==db->aVTrans && db->nVTrans>0 ){
697 return SQLITE_LOCKED;
698 }
699 if( !pVtab ){
700 return SQLITE_OK;
701 }
702 pModule = pVtab->pModule;
703
danielk1977f9e7dda2006-06-16 16:08:53 +0000704 if( pModule->xBegin ){
705 int i;
706
danielk197720b1eaf2006-07-26 16:22:14 +0000707
danielk1977f9e7dda2006-06-16 16:08:53 +0000708 /* If pVtab is already in the aVTrans array, return early */
709 for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
710 if( db->aVTrans[i]==pVtab ){
711 return SQLITE_OK;
712 }
713 }
714
715 /* Invoke the xBegin method */
716 rc = pModule->xBegin(pVtab);
717 if( rc!=SQLITE_OK ){
718 return rc;
719 }
720
danielk1977e7ff4032006-06-17 11:30:32 +0000721 rc = addToVTrans(db, pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +0000722 }
723 return rc;
724}
725
drhb7f6f682006-07-08 17:06:43 +0000726/*
727** The first parameter (pDef) is a function implementation. The
728** second parameter (pExpr) is the first argument to this function.
729** If pExpr is a column in a virtual table, then let the virtual
730** table implementation have an opportunity to overload the function.
731**
732** This routine is used to allow virtual table implementations to
733** overload MATCH, LIKE, GLOB, and REGEXP operators.
734**
735** Return either the pDef argument (indicating no change) or a
736** new FuncDef structure that is marked as ephemeral using the
737** SQLITE_FUNC_EPHEM flag.
738*/
739FuncDef *sqlite3VtabOverloadFunction(
drh17435752007-08-16 04:30:38 +0000740 sqlite3 *db, /* Database connection for reporting malloc problems */
drhb7f6f682006-07-08 17:06:43 +0000741 FuncDef *pDef, /* Function to possibly overload */
742 int nArg, /* Number of arguments to the function */
743 Expr *pExpr /* First argument to the function */
744){
745 Table *pTab;
746 sqlite3_vtab *pVtab;
747 sqlite3_module *pMod;
drhe94b0c32006-07-08 18:09:15 +0000748 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
drhb7f6f682006-07-08 17:06:43 +0000749 void *pArg;
drhb7f6f682006-07-08 17:06:43 +0000750 FuncDef *pNew;
drha70034d2006-09-18 20:24:02 +0000751 int rc;
752 char *zLowerName;
753 unsigned char *z;
754
drhb7f6f682006-07-08 17:06:43 +0000755
756 /* Check to see the left operand is a column in a virtual table */
757 if( pExpr==0 ) return pDef;
758 if( pExpr->op!=TK_COLUMN ) return pDef;
759 pTab = pExpr->pTab;
760 if( pTab==0 ) return pDef;
761 if( !pTab->isVirtual ) return pDef;
762 pVtab = pTab->pVtab;
763 assert( pVtab!=0 );
764 assert( pVtab->pModule!=0 );
danielk19775bd270b2006-07-25 15:14:52 +0000765 pMod = (sqlite3_module *)pVtab->pModule;
drhb7f6f682006-07-08 17:06:43 +0000766 if( pMod->xFindFunction==0 ) return pDef;
767
768 /* Call the xFuncFunction method on the virtual table implementation
drha70034d2006-09-18 20:24:02 +0000769 ** to see if the implementation wants to overload this function
770 */
drh17435752007-08-16 04:30:38 +0000771 zLowerName = sqlite3DbStrDup(db, pDef->zName);
772 if( zLowerName ){
773 for(z=(unsigned char*)zLowerName; *z; z++){
774 *z = sqlite3UpperToLower[*z];
775 }
776 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
777 sqlite3_free(zLowerName);
drha70034d2006-09-18 20:24:02 +0000778 }
drha70034d2006-09-18 20:24:02 +0000779 if( rc==0 ){
drhb7f6f682006-07-08 17:06:43 +0000780 return pDef;
781 }
782
783 /* Create a new ephemeral function definition for the overloaded
784 ** function */
drh17435752007-08-16 04:30:38 +0000785 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) );
drhb7f6f682006-07-08 17:06:43 +0000786 if( pNew==0 ){
787 return pDef;
788 }
789 *pNew = *pDef;
drh5bb3eb92007-05-04 13:15:55 +0000790 memcpy(pNew->zName, pDef->zName, strlen(pDef->zName)+1);
drhb7f6f682006-07-08 17:06:43 +0000791 pNew->xFunc = xFunc;
792 pNew->pUserData = pArg;
drhb7f6f682006-07-08 17:06:43 +0000793 pNew->flags |= SQLITE_FUNC_EPHEM;
794 return pNew;
795}
796
drhb9bb7c12006-06-11 23:41:55 +0000797#endif /* SQLITE_OMIT_VIRTUALTABLE */