blob: d18f6b3c2065798098a10f4349a146b690293c09 [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**
danielk1977777da082008-11-13 19:12:34 +000014** $Id: vtab.c,v 1.78 2008/11/13 19:12:36 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 ){
danielk197727a430c2008-06-23 17:44:18 +000033 Module *pDel;
danielk1977832a58a2007-06-22 15:21:15 +000034 char *zCopy = (char *)(&pMod[1]);
35 memcpy(zCopy, zName, nName+1);
36 pMod->zName = zCopy;
37 pMod->pModule = pModule;
38 pMod->pAux = pAux;
39 pMod->xDestroy = xDestroy;
danielk197727a430c2008-06-23 17:44:18 +000040 pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
41 if( pDel && pDel->xDestroy ){
42 pDel->xDestroy(pDel->pAux);
danielk1977832a58a2007-06-22 15:21:15 +000043 }
drh633e6d52008-07-28 19:34:53 +000044 sqlite3DbFree(db, pDel);
danielk197727a430c2008-06-23 17:44:18 +000045 if( pDel==pMod ){
46 db->mallocFailed = 1;
47 }
danielk1977832a58a2007-06-22 15:21:15 +000048 sqlite3ResetInternalSchema(db, 0);
danielk1977777da082008-11-13 19:12:34 +000049 }else if( xDestroy ){
50 xDestroy(pAux);
danielk1977832a58a2007-06-22 15:21:15 +000051 }
drh27641702007-08-22 02:56:42 +000052 rc = sqlite3ApiExit(db, SQLITE_OK);
53 sqlite3_mutex_leave(db->mutex);
54 return rc;
danielk1977832a58a2007-06-22 15:21:15 +000055}
56
57
drhb9bb7c12006-06-11 23:41:55 +000058/*
59** External API function used to create a new virtual-table module.
60*/
61int sqlite3_create_module(
62 sqlite3 *db, /* Database in which module is registered */
63 const char *zName, /* Name assigned to this module */
danielk1977d1ab1ba2006-06-15 04:28:13 +000064 const sqlite3_module *pModule, /* The definition of the module */
65 void *pAux /* Context pointer for xCreate/xConnect */
drhb9bb7c12006-06-11 23:41:55 +000066){
danielk1977832a58a2007-06-22 15:21:15 +000067 return createModule(db, zName, pModule, pAux, 0);
68}
69
70/*
71** External API function used to create a new virtual-table module.
72*/
73int sqlite3_create_module_v2(
74 sqlite3 *db, /* Database in which module is registered */
75 const char *zName, /* Name assigned to this module */
76 const sqlite3_module *pModule, /* The definition of the module */
77 void *pAux, /* Context pointer for xCreate/xConnect */
78 void (*xDestroy)(void *) /* Module destructor function */
79){
80 return createModule(db, zName, pModule, pAux, xDestroy);
drhb9bb7c12006-06-11 23:41:55 +000081}
82
drhb9bb7c12006-06-11 23:41:55 +000083/*
drh189d4af2006-09-02 20:57:52 +000084** Lock the virtual table so that it cannot be disconnected.
85** Locks nest. Every lock should have a corresponding unlock.
86** If an unlock is omitted, resources leaks will occur.
87**
88** If a disconnect is attempted while a virtual table is locked,
89** the disconnect is deferred until all locks have been removed.
90*/
91void sqlite3VtabLock(sqlite3_vtab *pVtab){
92 pVtab->nRef++;
93}
94
95/*
96** Unlock a virtual table. When the last lock is removed,
97** disconnect the virtual table.
98*/
danielk1977a04a34f2007-04-16 15:06:25 +000099void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
drh189d4af2006-09-02 20:57:52 +0000100 pVtab->nRef--;
danielk1977a04a34f2007-04-16 15:06:25 +0000101 assert(db);
drh7e8b8482008-01-23 03:03:05 +0000102 assert( sqlite3SafetyCheckOk(db) );
drh189d4af2006-09-02 20:57:52 +0000103 if( pVtab->nRef==0 ){
danielk1977a04a34f2007-04-16 15:06:25 +0000104 if( db->magic==SQLITE_MAGIC_BUSY ){
drh7e8b8482008-01-23 03:03:05 +0000105 (void)sqlite3SafetyOff(db);
danielk1977a04a34f2007-04-16 15:06:25 +0000106 pVtab->pModule->xDisconnect(pVtab);
drh7e8b8482008-01-23 03:03:05 +0000107 (void)sqlite3SafetyOn(db);
danielk1977a04a34f2007-04-16 15:06:25 +0000108 } else {
109 pVtab->pModule->xDisconnect(pVtab);
110 }
drh189d4af2006-09-02 20:57:52 +0000111 }
112}
113
114/*
drhb9bb7c12006-06-11 23:41:55 +0000115** Clear any and all virtual-table information from the Table record.
116** This routine is called, for example, just before deleting the Table
117** record.
118*/
119void sqlite3VtabClear(Table *p){
danielk1977be718892006-06-23 08:05:19 +0000120 sqlite3_vtab *pVtab = p->pVtab;
drh633e6d52008-07-28 19:34:53 +0000121 sqlite3 *db = p->db;
danielk1977be718892006-06-23 08:05:19 +0000122 if( pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000123 assert( p->pMod && p->pMod->pModule );
drh633e6d52008-07-28 19:34:53 +0000124 sqlite3VtabUnlock(db, pVtab);
danielk1977be718892006-06-23 08:05:19 +0000125 p->pVtab = 0;
drhb9bb7c12006-06-11 23:41:55 +0000126 }
127 if( p->azModuleArg ){
128 int i;
129 for(i=0; i<p->nModuleArg; i++){
drh633e6d52008-07-28 19:34:53 +0000130 sqlite3DbFree(db, p->azModuleArg[i]);
drhb9bb7c12006-06-11 23:41:55 +0000131 }
drh633e6d52008-07-28 19:34:53 +0000132 sqlite3DbFree(db, p->azModuleArg);
drhb9bb7c12006-06-11 23:41:55 +0000133 }
134}
135
136/*
137** Add a new module argument to pTable->azModuleArg[].
138** The string is not copied - the pointer is stored. The
139** string will be freed automatically when the table is
140** deleted.
141*/
drh17435752007-08-16 04:30:38 +0000142static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
drhb9bb7c12006-06-11 23:41:55 +0000143 int i = pTable->nModuleArg++;
danielk1977b7a2f2e2006-06-23 11:34:54 +0000144 int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
145 char **azModuleArg;
danielk197726783a52007-08-29 14:06:22 +0000146 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000147 if( azModuleArg==0 ){
148 int j;
149 for(j=0; j<i; j++){
drh633e6d52008-07-28 19:34:53 +0000150 sqlite3DbFree(db, pTable->azModuleArg[j]);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000151 }
drh633e6d52008-07-28 19:34:53 +0000152 sqlite3DbFree(db, zArg);
153 sqlite3DbFree(db, pTable->azModuleArg);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000154 pTable->nModuleArg = 0;
drhb9bb7c12006-06-11 23:41:55 +0000155 }else{
danielk1977b7a2f2e2006-06-23 11:34:54 +0000156 azModuleArg[i] = zArg;
157 azModuleArg[i+1] = 0;
drhb9bb7c12006-06-11 23:41:55 +0000158 }
danielk1977b7a2f2e2006-06-23 11:34:54 +0000159 pTable->azModuleArg = azModuleArg;
drhb9bb7c12006-06-11 23:41:55 +0000160}
161
162/*
163** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
164** statement. The module name has been parsed, but the optional list
165** of parameters that follow the module name are still pending.
166*/
167void sqlite3VtabBeginParse(
168 Parse *pParse, /* Parsing context */
169 Token *pName1, /* Name of new table, or database name */
170 Token *pName2, /* Name of new table or NULL */
171 Token *pModuleName /* Name of the module for the virtual table */
172){
danielk1977f1a381e2006-06-16 08:01:02 +0000173 int iDb; /* The database the table is being created in */
drhb9bb7c12006-06-11 23:41:55 +0000174 Table *pTable; /* The new virtual table */
drh17435752007-08-16 04:30:38 +0000175 sqlite3 *db; /* Database connection */
drhb9bb7c12006-06-11 23:41:55 +0000176
drh4a50aac2007-08-23 02:47:53 +0000177 if( pParse->db->flags & SQLITE_SharedCache ){
danielk1977113e5452007-04-16 15:49:41 +0000178 sqlite3ErrorMsg(pParse, "Cannot use virtual tables in shared-cache mode");
179 return;
180 }
181
danielk1977f1a381e2006-06-16 08:01:02 +0000182 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
drhb9bb7c12006-06-11 23:41:55 +0000183 pTable = pParse->pNewTable;
danielk1977f1a381e2006-06-16 08:01:02 +0000184 if( pTable==0 || pParse->nErr ) return;
185 assert( 0==pTable->pIndex );
186
drh17435752007-08-16 04:30:38 +0000187 db = pParse->db;
188 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
danielk197770ba1642006-06-21 16:02:42 +0000189 assert( iDb>=0 );
190
drh7d10d5a2008-08-20 16:35:10 +0000191 pTable->tabFlags |= TF_Virtual;
drhb9bb7c12006-06-11 23:41:55 +0000192 pTable->nModuleArg = 0;
drh17435752007-08-16 04:30:38 +0000193 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
194 addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
195 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
drhb9bb7c12006-06-11 23:41:55 +0000196 pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
danielk1977f1a381e2006-06-16 08:01:02 +0000197
198#ifndef SQLITE_OMIT_AUTHORIZATION
199 /* Creating a virtual table invokes the authorization callback twice.
200 ** The first invocation, to obtain permission to INSERT a row into the
201 ** sqlite_master table, has already been made by sqlite3StartTable().
202 ** The second call, to obtain permission to create the table, is made now.
203 */
danielk1977be718892006-06-23 08:05:19 +0000204 if( pTable->azModuleArg ){
205 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
206 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
danielk1977f1a381e2006-06-16 08:01:02 +0000207 }
208#endif
drhb9bb7c12006-06-11 23:41:55 +0000209}
210
211/*
212** This routine takes the module argument that has been accumulating
213** in pParse->zArg[] and appends it to the list of arguments on the
214** virtual table currently under construction in pParse->pTable.
215*/
216static void addArgumentToVtab(Parse *pParse){
danielk197733b39332006-06-24 08:51:05 +0000217 if( pParse->sArg.z && pParse->pNewTable ){
drh7c2d87c2006-09-02 14:16:59 +0000218 const char *z = (const char*)pParse->sArg.z;
danielk197733b39332006-06-24 08:51:05 +0000219 int n = pParse->sArg.n;
drh17435752007-08-16 04:30:38 +0000220 sqlite3 *db = pParse->db;
221 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
drhb9bb7c12006-06-11 23:41:55 +0000222 }
drhb9bb7c12006-06-11 23:41:55 +0000223}
224
225/*
226** The parser calls this routine after the CREATE VIRTUAL TABLE statement
227** has been completely parsed.
228*/
229void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
230 Table *pTab; /* The table being constructed */
231 sqlite3 *db; /* The database connection */
232 char *zModule; /* The module name of the table: USING modulename */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000233 Module *pMod = 0;
drhb9bb7c12006-06-11 23:41:55 +0000234
235 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000236 pParse->sArg.z = 0;
drhb9bb7c12006-06-11 23:41:55 +0000237
238 /* Lookup the module name. */
239 pTab = pParse->pNewTable;
240 if( pTab==0 ) return;
241 db = pParse->db;
242 if( pTab->nModuleArg<1 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000243 zModule = pTab->azModuleArg[0];
danielk1977d1ab1ba2006-06-15 04:28:13 +0000244 pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
245 pTab->pMod = pMod;
drhb9bb7c12006-06-11 23:41:55 +0000246
247 /* If the CREATE VIRTUAL TABLE statement is being entered for the
248 ** first time (in other words if the virtual table is actually being
249 ** created now instead of just being read out of sqlite_master) then
250 ** do additional initialization work and store the statement text
251 ** in the sqlite_master table.
252 */
253 if( !db->init.busy ){
254 char *zStmt;
danielk197778efaba2006-06-12 06:09:17 +0000255 char *zWhere;
drhb9bb7c12006-06-11 23:41:55 +0000256 int iDb;
257 Vdbe *v;
drhb9bb7c12006-06-11 23:41:55 +0000258
259 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
260 if( pEnd ){
261 pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
262 }
danielk19771e536952007-08-16 10:09:01 +0000263 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
drhb9bb7c12006-06-11 23:41:55 +0000264
265 /* A slot for the record has already been allocated in the
266 ** SQLITE_MASTER table. We just need to update that slot with all
danielk197778efaba2006-06-12 06:09:17 +0000267 ** the information we've collected.
268 **
drhb7654112008-01-12 12:48:07 +0000269 ** The VM register number pParse->regRowid holds the rowid of an
270 ** entry in the sqlite_master table tht was created for this vtab
271 ** by sqlite3StartTable().
drhb9bb7c12006-06-11 23:41:55 +0000272 */
273 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
274 sqlite3NestedParse(pParse,
275 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000276 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
drhb7654112008-01-12 12:48:07 +0000277 "WHERE rowid=#%d",
drhb9bb7c12006-06-11 23:41:55 +0000278 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
279 pTab->zName,
280 pTab->zName,
drhb7654112008-01-12 12:48:07 +0000281 zStmt,
282 pParse->regRowid
drhb9bb7c12006-06-11 23:41:55 +0000283 );
drh633e6d52008-07-28 19:34:53 +0000284 sqlite3DbFree(db, zStmt);
drhb9bb7c12006-06-11 23:41:55 +0000285 v = sqlite3GetVdbe(pParse);
drh9cbf3422008-01-17 16:22:13 +0000286 sqlite3ChangeCookie(pParse, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000287
drh66a51672008-01-03 00:01:23 +0000288 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
danielk19771e536952007-08-16 10:09:01 +0000289 zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
drh66a51672008-01-03 00:01:23 +0000290 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC);
291 sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
292 pTab->zName, strlen(pTab->zName) + 1);
drhb9bb7c12006-06-11 23:41:55 +0000293 }
294
danielk197778efaba2006-06-12 06:09:17 +0000295 /* If we are rereading the sqlite_master table create the in-memory
danielk1977c7d54102006-06-15 07:29:00 +0000296 ** record of the table. If the module has already been registered,
297 ** also call the xConnect method here.
drhb9bb7c12006-06-11 23:41:55 +0000298 */
danielk197778efaba2006-06-12 06:09:17 +0000299 else {
danielk197778efaba2006-06-12 06:09:17 +0000300 Table *pOld;
301 Schema *pSchema = pTab->pSchema;
302 const char *zName = pTab->zName;
303 int nName = strlen(zName) + 1;
304 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
305 if( pOld ){
danielk1977a1644fd2007-08-29 12:31:25 +0000306 db->mallocFailed = 1;
danielk197778efaba2006-06-12 06:09:17 +0000307 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
308 return;
309 }
danielk1977a04a34f2007-04-16 15:06:25 +0000310 pSchema->db = pParse->db;
danielk19777e6ebfb2006-06-12 11:24:37 +0000311 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000312 }
313}
314
315/*
316** The parser calls this routine when it sees the first token
317** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
318*/
319void sqlite3VtabArgInit(Parse *pParse){
320 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000321 pParse->sArg.z = 0;
322 pParse->sArg.n = 0;
drhb9bb7c12006-06-11 23:41:55 +0000323}
324
325/*
326** The parser calls this routine for each token after the first token
327** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
328*/
329void sqlite3VtabArgExtend(Parse *pParse, Token *p){
danielk197733b39332006-06-24 08:51:05 +0000330 Token *pArg = &pParse->sArg;
331 if( pArg->z==0 ){
332 pArg->z = p->z;
333 pArg->n = p->n;
334 }else{
335 assert(pArg->z < p->z);
336 pArg->n = (p->z + p->n - pArg->z);
drhb9bb7c12006-06-11 23:41:55 +0000337 }
drhb9bb7c12006-06-11 23:41:55 +0000338}
339
danielk197778efaba2006-06-12 06:09:17 +0000340/*
danielk19779da9d472006-06-14 06:58:15 +0000341** Invoke a virtual table constructor (either xCreate or xConnect). The
342** pointer to the function to invoke is passed as the fourth parameter
343** to this procedure.
344*/
345static int vtabCallConstructor(
346 sqlite3 *db,
347 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000348 Module *pMod,
drhe4102962006-09-11 00:34:22 +0000349 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
danielk19779da9d472006-06-14 06:58:15 +0000350 char **pzErr
351){
352 int rc;
353 int rc2;
danielk19775bccfc92007-09-04 15:38:57 +0000354 sqlite3_vtab *pVtab = 0;
drhe4102962006-09-11 00:34:22 +0000355 const char *const*azArg = (const char *const*)pTab->azModuleArg;
danielk19779da9d472006-06-14 06:58:15 +0000356 int nArg = pTab->nModuleArg;
drh4ca8aac2006-09-10 17:31:58 +0000357 char *zErr = 0;
danielk19771e536952007-08-16 10:09:01 +0000358 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000359
danielk197701256832007-04-18 14:24:32 +0000360 if( !zModuleName ){
361 return SQLITE_NOMEM;
362 }
363
danielk19779da9d472006-06-14 06:58:15 +0000364 assert( !db->pVTab );
365 assert( xConstruct );
366
367 db->pVTab = pTab;
368 rc = sqlite3SafetyOff(db);
369 assert( rc==SQLITE_OK );
danielk19775bccfc92007-09-04 15:38:57 +0000370 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVtab, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000371 rc2 = sqlite3SafetyOn(db);
drhfe1368e2006-09-10 17:08:29 +0000372 if( rc==SQLITE_OK && pVtab ){
373 pVtab->pModule = pMod->pModule;
374 pVtab->nRef = 1;
danielk19775bccfc92007-09-04 15:38:57 +0000375 pTab->pVtab = pVtab;
danielk19779da9d472006-06-14 06:58:15 +0000376 }
377
378 if( SQLITE_OK!=rc ){
drh4ca8aac2006-09-10 17:31:58 +0000379 if( zErr==0 ){
danielk19771e536952007-08-16 10:09:01 +0000380 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
drh4ca8aac2006-09-10 17:31:58 +0000381 }else {
danielk19771e536952007-08-16 10:09:01 +0000382 *pzErr = sqlite3MPrintf(db, "%s", zErr);
drh633e6d52008-07-28 19:34:53 +0000383 sqlite3DbFree(db, zErr);
drhfe1368e2006-09-10 17:08:29 +0000384 }
385 }else if( db->pVTab ){
danielk19779da9d472006-06-14 06:58:15 +0000386 const char *zFormat = "vtable constructor did not declare schema: %s";
danielk19771e536952007-08-16 10:09:01 +0000387 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000388 rc = SQLITE_ERROR;
389 }
390 if( rc==SQLITE_OK ){
391 rc = rc2;
392 }
393 db->pVTab = 0;
drh633e6d52008-07-28 19:34:53 +0000394 sqlite3DbFree(db, zModuleName);
danielk1977034ca142007-06-26 10:38:54 +0000395
396 /* If everything went according to plan, loop through the columns
397 ** of the table to see if any of them contain the token "hidden".
398 ** If so, set the Column.isHidden flag and remove the token from
399 ** the type string.
400 */
401 if( rc==SQLITE_OK ){
402 int iCol;
403 for(iCol=0; iCol<pTab->nCol; iCol++){
404 char *zType = pTab->aCol[iCol].zType;
405 int nType;
406 int i = 0;
407 if( !zType ) continue;
408 nType = strlen(zType);
409 if( sqlite3StrNICmp("hidden", zType, 6) || (zType[6] && zType[6]!=' ') ){
410 for(i=0; i<nType; i++){
411 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
412 && (zType[i+7]=='\0' || zType[i+7]==' ')
413 ){
414 i++;
415 break;
416 }
417 }
418 }
419 if( i<nType ){
420 int j;
421 int nDel = 6 + (zType[i+6] ? 1 : 0);
422 for(j=i; (j+nDel)<=nType; j++){
423 zType[j] = zType[j+nDel];
424 }
425 if( zType[i]=='\0' && i>0 ){
426 assert(zType[i-1]==' ');
427 zType[i-1] = '\0';
428 }
429 pTab->aCol[iCol].isHidden = 1;
430 }
431 }
432 }
danielk19779da9d472006-06-14 06:58:15 +0000433 return rc;
434}
435
436/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000437** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000438** of the virtual table pTab. If an error occurs, an error code is returned
439** and an error left in pParse.
440**
441** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000442*/
443int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000444 Module *pMod;
danielk19777e6ebfb2006-06-12 11:24:37 +0000445 int rc = SQLITE_OK;
446
drh7d10d5a2008-08-20 16:35:10 +0000447 if( !pTab || (pTab->tabFlags & TF_Virtual)==0 || pTab->pVtab ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000448 return SQLITE_OK;
449 }
450
danielk1977d1ab1ba2006-06-15 04:28:13 +0000451 pMod = pTab->pMod;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000452 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000453 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000454 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000455 rc = SQLITE_ERROR;
456 } else {
danielk19779da9d472006-06-14 06:58:15 +0000457 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000458 sqlite3 *db = pParse->db;
459 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000460 if( rc!=SQLITE_OK ){
461 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000462 }
drh633e6d52008-07-28 19:34:53 +0000463 sqlite3DbFree(db, zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000464 }
465
466 return rc;
467}
468
danielk19779da9d472006-06-14 06:58:15 +0000469/*
danielk1977e7ff4032006-06-17 11:30:32 +0000470** Add the virtual table pVtab to the array sqlite3.aVTrans[].
471*/
danielk1977be718892006-06-23 08:05:19 +0000472static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
danielk1977e7ff4032006-06-17 11:30:32 +0000473 const int ARRAY_INCR = 5;
474
475 /* Grow the sqlite3.aVTrans array if required */
476 if( (db->nVTrans%ARRAY_INCR)==0 ){
477 sqlite3_vtab **aVTrans;
478 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
danielk197726783a52007-08-29 14:06:22 +0000479 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
danielk1977e7ff4032006-06-17 11:30:32 +0000480 if( !aVTrans ){
481 return SQLITE_NOMEM;
482 }
483 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
484 db->aVTrans = aVTrans;
485 }
486
487 /* Add pVtab to the end of sqlite3.aVTrans */
488 db->aVTrans[db->nVTrans++] = pVtab;
drh189d4af2006-09-02 20:57:52 +0000489 sqlite3VtabLock(pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000490 return SQLITE_OK;
491}
492
493/*
danielk19779da9d472006-06-14 06:58:15 +0000494** This function is invoked by the vdbe to call the xCreate method
495** of the virtual table named zTab in database iDb.
496**
497** If an error occurs, *pzErr is set to point an an English language
498** description of the error and an SQLITE_XXX error code is returned.
drh633e6d52008-07-28 19:34:53 +0000499** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
danielk19779da9d472006-06-14 06:58:15 +0000500*/
501int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
502 int rc = SQLITE_OK;
503 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000504 Module *pMod;
danielk19779da9d472006-06-14 06:58:15 +0000505 const char *zModule;
506
507 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
drh7d10d5a2008-08-20 16:35:10 +0000508 assert(pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVtab);
danielk1977d1ab1ba2006-06-15 04:28:13 +0000509 pMod = pTab->pMod;
danielk19779da9d472006-06-14 06:58:15 +0000510 zModule = pTab->azModuleArg[0];
511
512 /* If the module has been registered and includes a Create method,
513 ** invoke it now. If the module has not been registered, return an
514 ** error. Otherwise, do nothing.
515 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000516 if( !pMod ){
danielk19771e536952007-08-16 10:09:01 +0000517 *pzErr = sqlite3MPrintf(db, "no such module: %s", zModule);
danielk19779da9d472006-06-14 06:58:15 +0000518 rc = SQLITE_ERROR;
519 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000520 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000521 }
522
danielk1977e7ff4032006-06-17 11:30:32 +0000523 if( rc==SQLITE_OK && pTab->pVtab ){
danielk197720b1eaf2006-07-26 16:22:14 +0000524 rc = addToVTrans(db, pTab->pVtab);
danielk1977e7ff4032006-06-17 11:30:32 +0000525 }
526
danielk19779da9d472006-06-14 06:58:15 +0000527 return rc;
528}
danielk1977be8a7832006-06-13 15:00:54 +0000529
530/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000531** This function is used to set the schema of a virtual table. It is only
532** valid to call this function from within the xCreate() or xConnect() of a
533** virtual table module.
534*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000535int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
536 Parse sParse;
537
538 int rc = SQLITE_OK;
drh27641702007-08-22 02:56:42 +0000539 Table *pTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000540 char *zErr = 0;
541
drh27641702007-08-22 02:56:42 +0000542 sqlite3_mutex_enter(db->mutex);
543 pTab = db->pVTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000544 if( !pTab ){
545 sqlite3Error(db, SQLITE_MISUSE, 0);
drh27641702007-08-22 02:56:42 +0000546 sqlite3_mutex_leave(db->mutex);
danielk19777e6ebfb2006-06-12 11:24:37 +0000547 return SQLITE_MISUSE;
548 }
drh7d10d5a2008-08-20 16:35:10 +0000549 assert((pTab->tabFlags & TF_Virtual)!=0 && pTab->nCol==0 && pTab->aCol==0);
danielk19777e6ebfb2006-06-12 11:24:37 +0000550
551 memset(&sParse, 0, sizeof(Parse));
552 sParse.declareVtab = 1;
553 sParse.db = db;
554
555 if(
556 SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
557 sParse.pNewTable &&
558 !sParse.pNewTable->pSelect &&
drh7d10d5a2008-08-20 16:35:10 +0000559 (sParse.pNewTable->tabFlags & TF_Virtual)==0
danielk19777e6ebfb2006-06-12 11:24:37 +0000560 ){
561 pTab->aCol = sParse.pNewTable->aCol;
562 pTab->nCol = sParse.pNewTable->nCol;
563 sParse.pNewTable->nCol = 0;
564 sParse.pNewTable->aCol = 0;
danielk197701256832007-04-18 14:24:32 +0000565 db->pVTab = 0;
danielk19777e6ebfb2006-06-12 11:24:37 +0000566 } else {
567 sqlite3Error(db, SQLITE_ERROR, zErr);
drh633e6d52008-07-28 19:34:53 +0000568 sqlite3DbFree(db, zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000569 rc = SQLITE_ERROR;
570 }
571 sParse.declareVtab = 0;
572
573 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk1977a04a34f2007-04-16 15:06:25 +0000574 sqlite3DeleteTable(sParse.pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000575 sParse.pNewTable = 0;
danielk19777e6ebfb2006-06-12 11:24:37 +0000576
drh4ac285a2006-09-15 07:28:50 +0000577 assert( (rc&0xff)==rc );
drh27641702007-08-22 02:56:42 +0000578 rc = sqlite3ApiExit(db, rc);
579 sqlite3_mutex_leave(db->mutex);
580 return rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000581}
582
583/*
danielk19779e39ce82006-06-12 16:01:21 +0000584** This function is invoked by the vdbe to call the xDestroy method
585** of the virtual table named zTab in database iDb. This occurs
586** when a DROP TABLE is mentioned.
587**
588** This call is a no-op if zTab is not a virtual table.
589*/
590int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
591{
592 int rc = SQLITE_OK;
593 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000594
595 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk19779e39ce82006-06-12 16:01:21 +0000596 assert(pTab);
597 if( pTab->pVtab ){
danielk1977d1ab1ba2006-06-15 04:28:13 +0000598 int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
danielk19779e39ce82006-06-12 16:01:21 +0000599 rc = sqlite3SafetyOff(db);
600 assert( rc==SQLITE_OK );
danielk1977d1ab1ba2006-06-15 04:28:13 +0000601 if( xDestroy ){
602 rc = xDestroy(pTab->pVtab);
603 }
drh7e8b8482008-01-23 03:03:05 +0000604 (void)sqlite3SafetyOn(db);
danielk19779e39ce82006-06-12 16:01:21 +0000605 if( rc==SQLITE_OK ){
drh665850f2008-04-10 18:35:21 +0000606 int i;
607 for(i=0; i<db->nVTrans; i++){
608 if( db->aVTrans[i]==pTab->pVtab ){
609 db->aVTrans[i] = db->aVTrans[--db->nVTrans];
610 break;
611 }
612 }
danielk19779e39ce82006-06-12 16:01:21 +0000613 pTab->pVtab = 0;
614 }
615 }
616
617 return rc;
618}
619
danielk1977f9e7dda2006-06-16 16:08:53 +0000620/*
danielk1977e7ff4032006-06-17 11:30:32 +0000621** This function invokes either the xRollback or xCommit method
622** of each of the virtual tables in the sqlite3.aVTrans array. The method
623** called is identified by the second argument, "offset", which is
624** the offset of the method to call in the sqlite3_module structure.
625**
626** The array is cleared after invoking the callbacks.
627*/
drh7209c692008-04-27 18:40:11 +0000628static void callFinaliser(sqlite3 *db, int offset){
danielk1977e7ff4032006-06-17 11:30:32 +0000629 int i;
danielk19770b83fa82007-04-19 14:48:37 +0000630 if( db->aVTrans ){
631 for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
632 sqlite3_vtab *pVtab = db->aVTrans[i];
633 int (*x)(sqlite3_vtab *);
634 x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
635 if( x ) x(pVtab);
636 sqlite3VtabUnlock(db, pVtab);
637 }
drh633e6d52008-07-28 19:34:53 +0000638 sqlite3DbFree(db, db->aVTrans);
danielk19770b83fa82007-04-19 14:48:37 +0000639 db->nVTrans = 0;
640 db->aVTrans = 0;
danielk1977e7ff4032006-06-17 11:30:32 +0000641 }
danielk1977e7ff4032006-06-17 11:30:32 +0000642}
643
644/*
danielk19773e3a84d2008-08-01 17:37:40 +0000645** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
646** array. Return the error code for the first error that occurs, or
647** SQLITE_OK if all xSync operations are successful.
648**
649** Set *pzErrmsg to point to a buffer that should be released using
650** sqlite3DbFree() containing an error message, if one is available.
danielk1977f9e7dda2006-06-16 16:08:53 +0000651*/
danielk19773e3a84d2008-08-01 17:37:40 +0000652int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
danielk1977e7ff4032006-06-17 11:30:32 +0000653 int i;
654 int rc = SQLITE_OK;
danielk19775bd270b2006-07-25 15:14:52 +0000655 int rcsafety;
danielk197720b1eaf2006-07-26 16:22:14 +0000656 sqlite3_vtab **aVTrans = db->aVTrans;
danielk197720b1eaf2006-07-26 16:22:14 +0000657
danielk19775bd270b2006-07-25 15:14:52 +0000658 rc = sqlite3SafetyOff(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000659 db->aVTrans = 0;
660 for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
661 sqlite3_vtab *pVtab = aVTrans[i];
danielk1977e7ff4032006-06-17 11:30:32 +0000662 int (*x)(sqlite3_vtab *);
663 x = pVtab->pModule->xSync;
664 if( x ){
665 rc = x(pVtab);
danielk19773e3a84d2008-08-01 17:37:40 +0000666 sqlite3DbFree(db, *pzErrmsg);
667 *pzErrmsg = pVtab->zErrMsg;
668 pVtab->zErrMsg = 0;
danielk1977e7ff4032006-06-17 11:30:32 +0000669 }
670 }
danielk197720b1eaf2006-07-26 16:22:14 +0000671 db->aVTrans = aVTrans;
danielk19775bd270b2006-07-25 15:14:52 +0000672 rcsafety = sqlite3SafetyOn(db);
danielk197720b1eaf2006-07-26 16:22:14 +0000673
danielk19775bd270b2006-07-25 15:14:52 +0000674 if( rc==SQLITE_OK ){
675 rc = rcsafety;
676 }
danielk1977e7ff4032006-06-17 11:30:32 +0000677 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000678}
679
680/*
681** Invoke the xRollback method of all virtual tables in the
682** sqlite3.aVTrans array. Then clear the array itself.
683*/
684int sqlite3VtabRollback(sqlite3 *db){
drh7209c692008-04-27 18:40:11 +0000685 callFinaliser(db, offsetof(sqlite3_module,xRollback));
danielk1977e7ff4032006-06-17 11:30:32 +0000686 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000687}
688
689/*
690** Invoke the xCommit method of all virtual tables in the
691** sqlite3.aVTrans array. Then clear the array itself.
692*/
693int sqlite3VtabCommit(sqlite3 *db){
drh7209c692008-04-27 18:40:11 +0000694 callFinaliser(db, offsetof(sqlite3_module,xCommit));
danielk1977e7ff4032006-06-17 11:30:32 +0000695 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000696}
697
698/*
699** If the virtual table pVtab supports the transaction interface
700** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
701** not currently open, invoke the xBegin method now.
702**
703** If the xBegin call is successful, place the sqlite3_vtab pointer
704** in the sqlite3.aVTrans array.
705*/
706int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
707 int rc = SQLITE_OK;
danielk197720b1eaf2006-07-26 16:22:14 +0000708 const sqlite3_module *pModule;
709
710 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
711 ** than zero, then this function is being called from within a
712 ** virtual module xSync() callback. It is illegal to write to
713 ** virtual module tables in this case, so return SQLITE_LOCKED.
714 */
danielk1977093e0f62008-11-13 18:00:14 +0000715 if( sqlite3VtabInSync(db) ){
danielk197720b1eaf2006-07-26 16:22:14 +0000716 return SQLITE_LOCKED;
717 }
718 if( !pVtab ){
719 return SQLITE_OK;
720 }
721 pModule = pVtab->pModule;
722
danielk1977f9e7dda2006-06-16 16:08:53 +0000723 if( pModule->xBegin ){
724 int i;
725
danielk197720b1eaf2006-07-26 16:22:14 +0000726
danielk1977f9e7dda2006-06-16 16:08:53 +0000727 /* If pVtab is already in the aVTrans array, return early */
728 for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
729 if( db->aVTrans[i]==pVtab ){
730 return SQLITE_OK;
731 }
732 }
733
734 /* Invoke the xBegin method */
735 rc = pModule->xBegin(pVtab);
danielk19773e3a84d2008-08-01 17:37:40 +0000736 if( rc==SQLITE_OK ){
737 rc = addToVTrans(db, pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +0000738 }
danielk1977f9e7dda2006-06-16 16:08:53 +0000739 }
740 return rc;
741}
742
drhb7f6f682006-07-08 17:06:43 +0000743/*
744** The first parameter (pDef) is a function implementation. The
745** second parameter (pExpr) is the first argument to this function.
746** If pExpr is a column in a virtual table, then let the virtual
747** table implementation have an opportunity to overload the function.
748**
749** This routine is used to allow virtual table implementations to
750** overload MATCH, LIKE, GLOB, and REGEXP operators.
751**
752** Return either the pDef argument (indicating no change) or a
753** new FuncDef structure that is marked as ephemeral using the
754** SQLITE_FUNC_EPHEM flag.
755*/
756FuncDef *sqlite3VtabOverloadFunction(
drh17435752007-08-16 04:30:38 +0000757 sqlite3 *db, /* Database connection for reporting malloc problems */
drhb7f6f682006-07-08 17:06:43 +0000758 FuncDef *pDef, /* Function to possibly overload */
759 int nArg, /* Number of arguments to the function */
760 Expr *pExpr /* First argument to the function */
761){
762 Table *pTab;
763 sqlite3_vtab *pVtab;
764 sqlite3_module *pMod;
drhe94b0c32006-07-08 18:09:15 +0000765 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
drhb7f6f682006-07-08 17:06:43 +0000766 void *pArg;
drhb7f6f682006-07-08 17:06:43 +0000767 FuncDef *pNew;
drh777b17a2007-09-20 10:02:54 +0000768 int rc = 0;
drha70034d2006-09-18 20:24:02 +0000769 char *zLowerName;
770 unsigned char *z;
771
drhb7f6f682006-07-08 17:06:43 +0000772
773 /* Check to see the left operand is a column in a virtual table */
774 if( pExpr==0 ) return pDef;
775 if( pExpr->op!=TK_COLUMN ) return pDef;
776 pTab = pExpr->pTab;
777 if( pTab==0 ) return pDef;
drh7d10d5a2008-08-20 16:35:10 +0000778 if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
drhb7f6f682006-07-08 17:06:43 +0000779 pVtab = pTab->pVtab;
780 assert( pVtab!=0 );
781 assert( pVtab->pModule!=0 );
danielk19775bd270b2006-07-25 15:14:52 +0000782 pMod = (sqlite3_module *)pVtab->pModule;
drhb7f6f682006-07-08 17:06:43 +0000783 if( pMod->xFindFunction==0 ) return pDef;
784
rseb72e88d2007-09-20 11:32:18 +0000785 /* Call the xFindFunction method on the virtual table implementation
drha70034d2006-09-18 20:24:02 +0000786 ** to see if the implementation wants to overload this function
787 */
drh17435752007-08-16 04:30:38 +0000788 zLowerName = sqlite3DbStrDup(db, pDef->zName);
789 if( zLowerName ){
790 for(z=(unsigned char*)zLowerName; *z; z++){
791 *z = sqlite3UpperToLower[*z];
792 }
793 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
drh633e6d52008-07-28 19:34:53 +0000794 sqlite3DbFree(db, zLowerName);
drh701bb3b2008-08-02 03:50:39 +0000795 if( pVtab->zErrMsg ){
796 sqlite3Error(db, rc, "%s", pVtab->zErrMsg);
797 sqlite3DbFree(db, pVtab->zErrMsg);
798 pVtab->zErrMsg = 0;
799 }
drha70034d2006-09-18 20:24:02 +0000800 }
drha70034d2006-09-18 20:24:02 +0000801 if( rc==0 ){
drhb7f6f682006-07-08 17:06:43 +0000802 return pDef;
803 }
804
805 /* Create a new ephemeral function definition for the overloaded
806 ** function */
drh17435752007-08-16 04:30:38 +0000807 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + strlen(pDef->zName) );
drhb7f6f682006-07-08 17:06:43 +0000808 if( pNew==0 ){
809 return pDef;
810 }
811 *pNew = *pDef;
danielk19778c0a7912008-08-20 14:49:23 +0000812 pNew->zName = (char *)&pNew[1];
drh5bb3eb92007-05-04 13:15:55 +0000813 memcpy(pNew->zName, pDef->zName, strlen(pDef->zName)+1);
drhb7f6f682006-07-08 17:06:43 +0000814 pNew->xFunc = xFunc;
815 pNew->pUserData = pArg;
drhb7f6f682006-07-08 17:06:43 +0000816 pNew->flags |= SQLITE_FUNC_EPHEM;
817 return pNew;
818}
819
drh4f3dd152008-04-28 18:46:43 +0000820/*
821** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
822** array so that an OP_VBegin will get generated for it. Add pTab to the
823** array if it is missing. If pTab is already in the array, this routine
824** is a no-op.
825*/
826void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
827 int i, n;
828 assert( IsVirtual(pTab) );
829 for(i=0; i<pParse->nVtabLock; i++){
830 if( pTab==pParse->apVtabLock[i] ) return;
831 }
832 n = (pParse->nVtabLock+1)*sizeof(pParse->apVtabLock[0]);
833 pParse->apVtabLock = sqlite3_realloc(pParse->apVtabLock, n);
834 if( pParse->apVtabLock ){
835 pParse->apVtabLock[pParse->nVtabLock++] = pTab;
drh344c38e2008-05-05 13:23:04 +0000836 }else{
837 pParse->db->mallocFailed = 1;
drh4f3dd152008-04-28 18:46:43 +0000838 }
839}
840
drhb9bb7c12006-06-11 23:41:55 +0000841#endif /* SQLITE_OMIT_VIRTUALTABLE */