blob: 5402695e80e3a612249b0b90ebfc1574065b30e0 [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**
drh9cbf3422008-01-17 16:22:13 +000014** $Id: vtab.c,v 1.62 2008/01/17 16:22:15 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;
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;
danielk197726783a52007-08-29 14:06:22 +0000139 azModuleArg = sqlite3DbRealloc(db, 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;
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 **
drhb7654112008-01-12 12:48:07 +0000262 ** The VM register number pParse->regRowid holds the rowid of an
263 ** entry in the sqlite_master table tht was created for this vtab
264 ** by sqlite3StartTable().
drhb9bb7c12006-06-11 23:41:55 +0000265 */
266 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
267 sqlite3NestedParse(pParse,
268 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000269 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
drhb7654112008-01-12 12:48:07 +0000270 "WHERE rowid=#%d",
drhb9bb7c12006-06-11 23:41:55 +0000271 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
272 pTab->zName,
273 pTab->zName,
drhb7654112008-01-12 12:48:07 +0000274 zStmt,
275 pParse->regRowid
drhb9bb7c12006-06-11 23:41:55 +0000276 );
drh17435752007-08-16 04:30:38 +0000277 sqlite3_free(zStmt);
drhb9bb7c12006-06-11 23:41:55 +0000278 v = sqlite3GetVdbe(pParse);
drh9cbf3422008-01-17 16:22:13 +0000279 sqlite3ChangeCookie(pParse, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000280
drh66a51672008-01-03 00:01:23 +0000281 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
danielk19771e536952007-08-16 10:09:01 +0000282 zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
drh66a51672008-01-03 00:01:23 +0000283 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC);
284 sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
285 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;
danielk19775bccfc92007-09-04 15:38:57 +0000347 sqlite3_vtab *pVtab = 0;
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 );
danielk19775bccfc92007-09-04 15:38:57 +0000363 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVtab, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000364 rc2 = sqlite3SafetyOn(db);
drhfe1368e2006-09-10 17:08:29 +0000365 if( rc==SQLITE_OK && pVtab ){
366 pVtab->pModule = pMod->pModule;
367 pVtab->nRef = 1;
danielk19775bccfc92007-09-04 15:38:57 +0000368 pTab->pVtab = pVtab;
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);
danielk197726783a52007-08-29 14:06:22 +0000472 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
danielk1977e7ff4032006-06-17 11:30:32 +0000473 if( !aVTrans ){
474 return SQLITE_NOMEM;
475 }
476 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
477 db->aVTrans = aVTrans;
478 }
479
480 /* Add pVtab to the end of sqlite3.aVTrans */
481 db->aVTrans[db->nVTrans++] = pVtab;
drh189d4af2006-09-02 20:57:52 +0000482 sqlite3VtabLock(pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000483 return SQLITE_OK;
484}
485
486/*
danielk19779da9d472006-06-14 06:58:15 +0000487** This function is invoked by the vdbe to call the xCreate method
488** of the virtual table named zTab in database iDb.
489**
490** If an error occurs, *pzErr is set to point an an English language
491** description of the error and an SQLITE_XXX error code is returned.
drh17435752007-08-16 04:30:38 +0000492** In this case the caller must call sqlite3_free() on *pzErr.
danielk19779da9d472006-06-14 06:58:15 +0000493*/
494int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
495 int rc = SQLITE_OK;
496 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000497 Module *pMod;
danielk19779da9d472006-06-14 06:58:15 +0000498 const char *zModule;
499
500 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
501 assert(pTab && pTab->isVirtual && !pTab->pVtab);
danielk1977d1ab1ba2006-06-15 04:28:13 +0000502 pMod = pTab->pMod;
danielk19779da9d472006-06-14 06:58:15 +0000503 zModule = pTab->azModuleArg[0];
504
505 /* If the module has been registered and includes a Create method,
506 ** invoke it now. If the module has not been registered, return an
507 ** error. Otherwise, do nothing.
508 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000509 if( !pMod ){
danielk19771e536952007-08-16 10:09:01 +0000510 *pzErr = sqlite3MPrintf(db, "no such module: %s", zModule);
danielk19779da9d472006-06-14 06:58:15 +0000511 rc = SQLITE_ERROR;
512 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000513 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000514 }
515
danielk1977e7ff4032006-06-17 11:30:32 +0000516 if( rc==SQLITE_OK && pTab->pVtab ){
danielk197720b1eaf2006-07-26 16:22:14 +0000517 rc = addToVTrans(db, pTab->pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000518 }
519
danielk19779da9d472006-06-14 06:58:15 +0000520 return rc;
521}
danielk1977be8a7832006-06-13 15:00:54 +0000522
523/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000524** This function is used to set the schema of a virtual table. It is only
525** valid to call this function from within the xCreate() or xConnect() of a
526** virtual table module.
527*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000528int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
529 Parse sParse;
530
531 int rc = SQLITE_OK;
drh27641702007-08-22 02:56:42 +0000532 Table *pTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000533 char *zErr = 0;
534
drh27641702007-08-22 02:56:42 +0000535 sqlite3_mutex_enter(db->mutex);
536 pTab = db->pVTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000537 if( !pTab ){
538 sqlite3Error(db, SQLITE_MISUSE, 0);
drh27641702007-08-22 02:56:42 +0000539 sqlite3_mutex_leave(db->mutex);
danielk19777e6ebfb2006-06-12 11:24:37 +0000540 return SQLITE_MISUSE;
541 }
542 assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
543
544 memset(&sParse, 0, sizeof(Parse));
545 sParse.declareVtab = 1;
546 sParse.db = db;
547
548 if(
549 SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
550 sParse.pNewTable &&
551 !sParse.pNewTable->pSelect &&
552 !sParse.pNewTable->isVirtual
553 ){
554 pTab->aCol = sParse.pNewTable->aCol;
555 pTab->nCol = sParse.pNewTable->nCol;
556 sParse.pNewTable->nCol = 0;
557 sParse.pNewTable->aCol = 0;
danielk197701256832007-04-18 14:24:32 +0000558 db->pVTab = 0;
danielk19777e6ebfb2006-06-12 11:24:37 +0000559 } else {
560 sqlite3Error(db, SQLITE_ERROR, zErr);
drh17435752007-08-16 04:30:38 +0000561 sqlite3_free(zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000562 rc = SQLITE_ERROR;
563 }
564 sParse.declareVtab = 0;
565
566 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk1977a04a34f2007-04-16 15:06:25 +0000567 sqlite3DeleteTable(sParse.pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000568 sParse.pNewTable = 0;
danielk19777e6ebfb2006-06-12 11:24:37 +0000569
drh4ac285a2006-09-15 07:28:50 +0000570 assert( (rc&0xff)==rc );
drh27641702007-08-22 02:56:42 +0000571 rc = sqlite3ApiExit(db, rc);
572 sqlite3_mutex_leave(db->mutex);
573 return rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000574}
575
576/*
danielk19779e39ce82006-06-12 16:01:21 +0000577** This function is invoked by the vdbe to call the xDestroy method
578** of the virtual table named zTab in database iDb. This occurs
579** when a DROP TABLE is mentioned.
580**
581** This call is a no-op if zTab is not a virtual table.
582*/
583int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
584{
585 int rc = SQLITE_OK;
586 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000587
588 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk19779e39ce82006-06-12 16:01:21 +0000589 assert(pTab);
590 if( pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000591 int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
danielk19779e39ce82006-06-12 16:01:21 +0000592 rc = sqlite3SafetyOff(db);
593 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000594 if( xDestroy ){
595 rc = xDestroy(pTab->pVtab);
596 }
danielk19779e39ce82006-06-12 16:01:21 +0000597 sqlite3SafetyOn(db);
598 if( rc==SQLITE_OK ){
599 pTab->pVtab = 0;
600 }
601 }
602
603 return rc;
604}
605
danielk1977f9e7dda2006-06-16 16:08:53 +0000606/*
danielk1977e7ff4032006-06-17 11:30:32 +0000607** This function invokes either the xRollback or xCommit method
608** of each of the virtual tables in the sqlite3.aVTrans array. The method
609** called is identified by the second argument, "offset", which is
610** the offset of the method to call in the sqlite3_module structure.
611**
612** The array is cleared after invoking the callbacks.
613*/
614static void callFinaliser(sqlite3 *db, int offset){
615 int i;
danielk19770b83fa82007-04-19 14:48:37 +0000616 if( db->aVTrans ){
617 for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
618 sqlite3_vtab *pVtab = db->aVTrans[i];
619 int (*x)(sqlite3_vtab *);
620 x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
621 if( x ) x(pVtab);
622 sqlite3VtabUnlock(db, pVtab);
623 }
drh17435752007-08-16 04:30:38 +0000624 sqlite3_free(db->aVTrans);
danielk19770b83fa82007-04-19 14:48:37 +0000625 db->nVTrans = 0;
626 db->aVTrans = 0;
danielk1977e7ff4032006-06-17 11:30:32 +0000627 }
danielk1977e7ff4032006-06-17 11:30:32 +0000628}
629
630/*
danielk19775bd270b2006-07-25 15:14:52 +0000631** If argument rc2 is not SQLITE_OK, then return it and do nothing.
danielk1977f9e7dda2006-06-16 16:08:53 +0000632** Otherwise, invoke the xSync method of all virtual tables in the
633** sqlite3.aVTrans array. Return the error code for the first error
634** that occurs, or SQLITE_OK if all xSync operations are successful.
635*/
danielk1977e7ff4032006-06-17 11:30:32 +0000636int sqlite3VtabSync(sqlite3 *db, int rc2){
637 int i;
638 int rc = SQLITE_OK;
danielk19775bd270b2006-07-25 15:14:52 +0000639 int rcsafety;
danielk197720b1eaf2006-07-26 16:22:14 +0000640 sqlite3_vtab **aVTrans = db->aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000641 if( rc2!=SQLITE_OK ) return rc2;
danielk197720b1eaf2006-07-26 16:22:14 +0000642
danielk19775bd270b2006-07-25 15:14:52 +0000643 rc = sqlite3SafetyOff(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000644 db->aVTrans = 0;
645 for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
646 sqlite3_vtab *pVtab = aVTrans[i];
danielk1977e7ff4032006-06-17 11:30:32 +0000647 int (*x)(sqlite3_vtab *);
648 x = pVtab->pModule->xSync;
649 if( x ){
650 rc = x(pVtab);
651 }
652 }
danielk197720b1eaf2006-07-26 16:22:14 +0000653 db->aVTrans = aVTrans;
danielk19775bd270b2006-07-25 15:14:52 +0000654 rcsafety = sqlite3SafetyOn(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000655
danielk19775bd270b2006-07-25 15:14:52 +0000656 if( rc==SQLITE_OK ){
657 rc = rcsafety;
658 }
danielk1977e7ff4032006-06-17 11:30:32 +0000659 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000660}
661
662/*
663** Invoke the xRollback method of all virtual tables in the
664** sqlite3.aVTrans array. Then clear the array itself.
665*/
666int sqlite3VtabRollback(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000667 callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback));
668 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000669}
670
671/*
672** Invoke the xCommit method of all virtual tables in the
673** sqlite3.aVTrans array. Then clear the array itself.
674*/
675int sqlite3VtabCommit(sqlite3 *db){
danielk1977e7ff4032006-06-17 11:30:32 +0000676 callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit));
677 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000678}
679
680/*
681** If the virtual table pVtab supports the transaction interface
682** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
683** not currently open, invoke the xBegin method now.
684**
685** If the xBegin call is successful, place the sqlite3_vtab pointer
686** in the sqlite3.aVTrans array.
687*/
688int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
689 int rc = SQLITE_OK;
danielk197720b1eaf2006-07-26 16:22:14 +0000690 const sqlite3_module *pModule;
691
692 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
693 ** than zero, then this function is being called from within a
694 ** virtual module xSync() callback. It is illegal to write to
695 ** virtual module tables in this case, so return SQLITE_LOCKED.
696 */
697 if( 0==db->aVTrans && db->nVTrans>0 ){
698 return SQLITE_LOCKED;
699 }
700 if( !pVtab ){
701 return SQLITE_OK;
702 }
703 pModule = pVtab->pModule;
704
danielk1977f9e7dda2006-06-16 16:08:53 +0000705 if( pModule->xBegin ){
706 int i;
707
danielk197720b1eaf2006-07-26 16:22:14 +0000708
danielk1977f9e7dda2006-06-16 16:08:53 +0000709 /* If pVtab is already in the aVTrans array, return early */
710 for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
711 if( db->aVTrans[i]==pVtab ){
712 return SQLITE_OK;
713 }
714 }
715
716 /* Invoke the xBegin method */
717 rc = pModule->xBegin(pVtab);
718 if( rc!=SQLITE_OK ){
719 return rc;
720 }
721
danielk1977e7ff4032006-06-17 11:30:32 +0000722 rc = addToVTrans(db, pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +0000723 }
724 return rc;
725}
726
drhb7f6f682006-07-08 17:06:43 +0000727/*
728** The first parameter (pDef) is a function implementation. The
729** second parameter (pExpr) is the first argument to this function.
730** If pExpr is a column in a virtual table, then let the virtual
731** table implementation have an opportunity to overload the function.
732**
733** This routine is used to allow virtual table implementations to
734** overload MATCH, LIKE, GLOB, and REGEXP operators.
735**
736** Return either the pDef argument (indicating no change) or a
737** new FuncDef structure that is marked as ephemeral using the
738** SQLITE_FUNC_EPHEM flag.
739*/
740FuncDef *sqlite3VtabOverloadFunction(
drh17435752007-08-16 04:30:38 +0000741 sqlite3 *db, /* Database connection for reporting malloc problems */
drhb7f6f682006-07-08 17:06:43 +0000742 FuncDef *pDef, /* Function to possibly overload */
743 int nArg, /* Number of arguments to the function */
744 Expr *pExpr /* First argument to the function */
745){
746 Table *pTab;
747 sqlite3_vtab *pVtab;
748 sqlite3_module *pMod;
drhe94b0c32006-07-08 18:09:15 +0000749 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
drhb7f6f682006-07-08 17:06:43 +0000750 void *pArg;
drhb7f6f682006-07-08 17:06:43 +0000751 FuncDef *pNew;
drh777b17a2007-09-20 10:02:54 +0000752 int rc = 0;
drha70034d2006-09-18 20:24:02 +0000753 char *zLowerName;
754 unsigned char *z;
755
drhb7f6f682006-07-08 17:06:43 +0000756
757 /* Check to see the left operand is a column in a virtual table */
758 if( pExpr==0 ) return pDef;
759 if( pExpr->op!=TK_COLUMN ) return pDef;
760 pTab = pExpr->pTab;
761 if( pTab==0 ) return pDef;
762 if( !pTab->isVirtual ) return pDef;
763 pVtab = pTab->pVtab;
764 assert( pVtab!=0 );
765 assert( pVtab->pModule!=0 );
danielk19775bd270b2006-07-25 15:14:52 +0000766 pMod = (sqlite3_module *)pVtab->pModule;
drhb7f6f682006-07-08 17:06:43 +0000767 if( pMod->xFindFunction==0 ) return pDef;
768
rseb72e88d2007-09-20 11:32:18 +0000769 /* Call the xFindFunction method on the virtual table implementation
drha70034d2006-09-18 20:24:02 +0000770 ** to see if the implementation wants to overload this function
771 */
drh17435752007-08-16 04:30:38 +0000772 zLowerName = sqlite3DbStrDup(db, pDef->zName);
773 if( zLowerName ){
774 for(z=(unsigned char*)zLowerName; *z; z++){
775 *z = sqlite3UpperToLower[*z];
776 }
777 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
778 sqlite3_free(zLowerName);
drha70034d2006-09-18 20:24:02 +0000779 }
drha70034d2006-09-18 20:24:02 +0000780 if( rc==0 ){
drhb7f6f682006-07-08 17:06:43 +0000781 return pDef;
782 }
783
784 /* Create a new ephemeral function definition for the overloaded
785 ** function */
drh17435752007-08-16 04:30:38 +0000786 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) );
drhb7f6f682006-07-08 17:06:43 +0000787 if( pNew==0 ){
788 return pDef;
789 }
790 *pNew = *pDef;
drh5bb3eb92007-05-04 13:15:55 +0000791 memcpy(pNew->zName, pDef->zName, strlen(pDef->zName)+1);
drhb7f6f682006-07-08 17:06:43 +0000792 pNew->xFunc = xFunc;
793 pNew->pUserData = pArg;
drhb7f6f682006-07-08 17:06:43 +0000794 pNew->flags |= SQLITE_FUNC_EPHEM;
795 return pNew;
796}
797
drhb9bb7c12006-06-11 23:41:55 +0000798#endif /* SQLITE_OMIT_VIRTUALTABLE */