blob: e460ee59e34bd55faafb17a5d23d31cb70e7cd42 [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.
drhb9bb7c12006-06-11 23:41:55 +000013*/
14#ifndef SQLITE_OMIT_VIRTUALTABLE
15#include "sqliteInt.h"
16
drh63842412009-04-11 16:27:19 +000017/*
18** The actual function that does the work of creating a new module.
19** This function implements the sqlite3_create_module() and
20** sqlite3_create_module_v2() interfaces.
21*/
danielk1977832a58a2007-06-22 15:21:15 +000022static int createModule(
23 sqlite3 *db, /* Database in which module is registered */
24 const char *zName, /* Name assigned to this module */
25 const sqlite3_module *pModule, /* The definition of the module */
26 void *pAux, /* Context pointer for xCreate/xConnect */
27 void (*xDestroy)(void *) /* Module destructor function */
danielk1977595a5232009-07-24 17:58:53 +000028){
drh27641702007-08-22 02:56:42 +000029 int rc, nName;
drh153c62c2007-08-24 03:51:33 +000030 Module *pMod;
drh27641702007-08-22 02:56:42 +000031
32 sqlite3_mutex_enter(db->mutex);
drhea678832008-12-10 19:26:22 +000033 nName = sqlite3Strlen30(zName);
drh153c62c2007-08-24 03:51:33 +000034 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
danielk1977832a58a2007-06-22 15:21:15 +000035 if( pMod ){
danielk197727a430c2008-06-23 17:44:18 +000036 Module *pDel;
danielk1977832a58a2007-06-22 15:21:15 +000037 char *zCopy = (char *)(&pMod[1]);
38 memcpy(zCopy, zName, nName+1);
39 pMod->zName = zCopy;
40 pMod->pModule = pModule;
41 pMod->pAux = pAux;
42 pMod->xDestroy = xDestroy;
danielk197727a430c2008-06-23 17:44:18 +000043 pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
44 if( pDel && pDel->xDestroy ){
45 pDel->xDestroy(pDel->pAux);
danielk1977832a58a2007-06-22 15:21:15 +000046 }
drh633e6d52008-07-28 19:34:53 +000047 sqlite3DbFree(db, pDel);
danielk197727a430c2008-06-23 17:44:18 +000048 if( pDel==pMod ){
49 db->mallocFailed = 1;
50 }
danielk1977832a58a2007-06-22 15:21:15 +000051 sqlite3ResetInternalSchema(db, 0);
danielk1977777da082008-11-13 19:12:34 +000052 }else if( xDestroy ){
53 xDestroy(pAux);
danielk1977832a58a2007-06-22 15:21:15 +000054 }
drh27641702007-08-22 02:56:42 +000055 rc = sqlite3ApiExit(db, SQLITE_OK);
56 sqlite3_mutex_leave(db->mutex);
57 return rc;
danielk1977832a58a2007-06-22 15:21:15 +000058}
59
60
drhb9bb7c12006-06-11 23:41:55 +000061/*
62** External API function used to create a new virtual-table module.
63*/
64int sqlite3_create_module(
65 sqlite3 *db, /* Database in which module is registered */
66 const char *zName, /* Name assigned to this module */
danielk1977d1ab1ba2006-06-15 04:28:13 +000067 const sqlite3_module *pModule, /* The definition of the module */
68 void *pAux /* Context pointer for xCreate/xConnect */
drhb9bb7c12006-06-11 23:41:55 +000069){
danielk1977832a58a2007-06-22 15:21:15 +000070 return createModule(db, zName, pModule, pAux, 0);
71}
72
73/*
74** External API function used to create a new virtual-table module.
75*/
76int sqlite3_create_module_v2(
77 sqlite3 *db, /* Database in which module is registered */
78 const char *zName, /* Name assigned to this module */
79 const sqlite3_module *pModule, /* The definition of the module */
80 void *pAux, /* Context pointer for xCreate/xConnect */
81 void (*xDestroy)(void *) /* Module destructor function */
82){
83 return createModule(db, zName, pModule, pAux, xDestroy);
drhb9bb7c12006-06-11 23:41:55 +000084}
85
drhb9bb7c12006-06-11 23:41:55 +000086/*
drh189d4af2006-09-02 20:57:52 +000087** Lock the virtual table so that it cannot be disconnected.
88** Locks nest. Every lock should have a corresponding unlock.
89** If an unlock is omitted, resources leaks will occur.
90**
91** If a disconnect is attempted while a virtual table is locked,
92** the disconnect is deferred until all locks have been removed.
93*/
danielk1977595a5232009-07-24 17:58:53 +000094void sqlite3VtabLock(VTable *pVTab){
95 pVTab->nRef++;
96}
97
98
99/*
100** pTab is a pointer to a Table structure representing a virtual-table.
101** Return a pointer to the VTable object used by connection db to access
102** this virtual-table, if one has been created, or NULL otherwise.
103*/
104VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
105 VTable *pVtab;
106 assert( IsVirtual(pTab) );
107 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
108 return pVtab;
drh189d4af2006-09-02 20:57:52 +0000109}
110
111/*
danielk1977595a5232009-07-24 17:58:53 +0000112** Decrement the ref-count on a virtual table object. When the ref-count
113** reaches zero, call the xDisconnect() method to delete the object.
drh189d4af2006-09-02 20:57:52 +0000114*/
danielk1977595a5232009-07-24 17:58:53 +0000115void sqlite3VtabUnlock(VTable *pVTab){
116 sqlite3 *db = pVTab->db;
117
118 assert( db );
119 assert( pVTab->nRef>0 );
drh7e8b8482008-01-23 03:03:05 +0000120 assert( sqlite3SafetyCheckOk(db) );
danielk1977595a5232009-07-24 17:58:53 +0000121
122 pVTab->nRef--;
123 if( pVTab->nRef==0 ){
124 sqlite3_vtab *p = pVTab->pVtab;
125 if( p ){
drh9978c972010-02-23 17:36:32 +0000126 p->pModule->xDisconnect(p);
danielk1977a04a34f2007-04-16 15:06:25 +0000127 }
danielk1977595a5232009-07-24 17:58:53 +0000128 sqlite3DbFree(db, pVTab);
129 }
130}
131
132/*
133** Table p is a virtual table. This function moves all elements in the
134** p->pVTable list to the sqlite3.pDisconnect lists of their associated
135** database connections to be disconnected at the next opportunity.
136** Except, if argument db is not NULL, then the entry associated with
137** connection db is left in the p->pVTable list.
138*/
139static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
140 VTable *pRet = 0;
141 VTable *pVTable = p->pVTable;
142 p->pVTable = 0;
143
144 /* Assert that the mutex (if any) associated with the BtShared database
145 ** that contains table p is held by the caller. See header comments
146 ** above function sqlite3VtabUnlockList() for an explanation of why
147 ** this makes it safe to access the sqlite3.pDisconnect list of any
148 ** database connection that may have an entry in the p->pVTable list. */
149 assert( db==0 ||
150 sqlite3BtreeHoldsMutex(db->aDb[sqlite3SchemaToIndex(db, p->pSchema)].pBt)
151 );
152
153 while( pVTable ){
154 sqlite3 *db2 = pVTable->db;
155 VTable *pNext = pVTable->pNext;
156 assert( db2 );
157 if( db2==db ){
158 pRet = pVTable;
159 p->pVTable = pRet;
160 pRet->pNext = 0;
161 }else{
162 pVTable->pNext = db2->pDisconnect;
163 db2->pDisconnect = pVTable;
164 }
165 pVTable = pNext;
166 }
167
168 assert( !db || pRet );
169 return pRet;
170}
171
172
173/*
174** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
175**
176** This function may only be called when the mutexes associated with all
177** shared b-tree databases opened using connection db are held by the
178** caller. This is done to protect the sqlite3.pDisconnect list. The
179** sqlite3.pDisconnect list is accessed only as follows:
180**
181** 1) By this function. In this case, all BtShared mutexes and the mutex
182** associated with the database handle itself must be held.
183**
184** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
185** the sqlite3.pDisconnect list. In this case either the BtShared mutex
186** associated with the database the virtual table is stored in is held
187** or, if the virtual table is stored in a non-sharable database, then
188** the database handle mutex is held.
189**
190** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
191** by multiple threads. It is thread-safe.
192*/
193void sqlite3VtabUnlockList(sqlite3 *db){
194 VTable *p = db->pDisconnect;
195 db->pDisconnect = 0;
196
197 assert( sqlite3BtreeHoldsAllMutexes(db) );
198 assert( sqlite3_mutex_held(db->mutex) );
199
200 if( p ){
201 sqlite3ExpirePreparedStatements(db);
202 do {
203 VTable *pNext = p->pNext;
204 sqlite3VtabUnlock(p);
205 p = pNext;
206 }while( p );
drh189d4af2006-09-02 20:57:52 +0000207 }
208}
209
210/*
drhb9bb7c12006-06-11 23:41:55 +0000211** Clear any and all virtual-table information from the Table record.
212** This routine is called, for example, just before deleting the Table
213** record.
danielk1977595a5232009-07-24 17:58:53 +0000214**
215** Since it is a virtual-table, the Table structure contains a pointer
216** to the head of a linked list of VTable structures. Each VTable
217** structure is associated with a single sqlite3* user of the schema.
218** The reference count of the VTable structure associated with database
219** connection db is decremented immediately (which may lead to the
220** structure being xDisconnected and free). Any other VTable structures
221** in the list are moved to the sqlite3.pDisconnect list of the associated
222** database connection.
drhb9bb7c12006-06-11 23:41:55 +0000223*/
dan1feeaed2010-07-23 15:41:47 +0000224void sqlite3VtabClear(sqlite3 *db, Table *p){
dand46def72010-07-24 11:28:28 +0000225 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
drhb9bb7c12006-06-11 23:41:55 +0000226 if( p->azModuleArg ){
227 int i;
228 for(i=0; i<p->nModuleArg; i++){
dan1feeaed2010-07-23 15:41:47 +0000229 sqlite3DbFree(db, p->azModuleArg[i]);
drhb9bb7c12006-06-11 23:41:55 +0000230 }
dan1feeaed2010-07-23 15:41:47 +0000231 sqlite3DbFree(db, p->azModuleArg);
drhb9bb7c12006-06-11 23:41:55 +0000232 }
233}
234
235/*
236** Add a new module argument to pTable->azModuleArg[].
237** The string is not copied - the pointer is stored. The
238** string will be freed automatically when the table is
239** deleted.
240*/
drh17435752007-08-16 04:30:38 +0000241static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
drhb9bb7c12006-06-11 23:41:55 +0000242 int i = pTable->nModuleArg++;
danielk1977b7a2f2e2006-06-23 11:34:54 +0000243 int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
244 char **azModuleArg;
danielk197726783a52007-08-29 14:06:22 +0000245 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000246 if( azModuleArg==0 ){
247 int j;
248 for(j=0; j<i; j++){
drh633e6d52008-07-28 19:34:53 +0000249 sqlite3DbFree(db, pTable->azModuleArg[j]);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000250 }
drh633e6d52008-07-28 19:34:53 +0000251 sqlite3DbFree(db, zArg);
252 sqlite3DbFree(db, pTable->azModuleArg);
danielk1977b7a2f2e2006-06-23 11:34:54 +0000253 pTable->nModuleArg = 0;
drhb9bb7c12006-06-11 23:41:55 +0000254 }else{
danielk1977b7a2f2e2006-06-23 11:34:54 +0000255 azModuleArg[i] = zArg;
256 azModuleArg[i+1] = 0;
drhb9bb7c12006-06-11 23:41:55 +0000257 }
danielk1977b7a2f2e2006-06-23 11:34:54 +0000258 pTable->azModuleArg = azModuleArg;
drhb9bb7c12006-06-11 23:41:55 +0000259}
260
261/*
262** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
263** statement. The module name has been parsed, but the optional list
264** of parameters that follow the module name are still pending.
265*/
266void sqlite3VtabBeginParse(
267 Parse *pParse, /* Parsing context */
268 Token *pName1, /* Name of new table, or database name */
269 Token *pName2, /* Name of new table or NULL */
270 Token *pModuleName /* Name of the module for the virtual table */
271){
danielk1977f1a381e2006-06-16 08:01:02 +0000272 int iDb; /* The database the table is being created in */
drhb9bb7c12006-06-11 23:41:55 +0000273 Table *pTable; /* The new virtual table */
drh17435752007-08-16 04:30:38 +0000274 sqlite3 *db; /* Database connection */
drhb9bb7c12006-06-11 23:41:55 +0000275
danielk1977f1a381e2006-06-16 08:01:02 +0000276 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
drhb9bb7c12006-06-11 23:41:55 +0000277 pTable = pParse->pNewTable;
drh748ce212009-05-20 20:10:47 +0000278 if( pTable==0 ) return;
danielk1977f1a381e2006-06-16 08:01:02 +0000279 assert( 0==pTable->pIndex );
280
drh17435752007-08-16 04:30:38 +0000281 db = pParse->db;
282 iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
danielk197770ba1642006-06-21 16:02:42 +0000283 assert( iDb>=0 );
284
drh7d10d5a2008-08-20 16:35:10 +0000285 pTable->tabFlags |= TF_Virtual;
drhb9bb7c12006-06-11 23:41:55 +0000286 pTable->nModuleArg = 0;
drh17435752007-08-16 04:30:38 +0000287 addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
288 addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
289 addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
drhb27b7f52008-12-10 18:03:45 +0000290 pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
danielk1977f1a381e2006-06-16 08:01:02 +0000291
292#ifndef SQLITE_OMIT_AUTHORIZATION
293 /* Creating a virtual table invokes the authorization callback twice.
294 ** The first invocation, to obtain permission to INSERT a row into the
295 ** sqlite_master table, has already been made by sqlite3StartTable().
296 ** The second call, to obtain permission to create the table, is made now.
297 */
danielk1977be718892006-06-23 08:05:19 +0000298 if( pTable->azModuleArg ){
299 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
300 pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
danielk1977f1a381e2006-06-16 08:01:02 +0000301 }
302#endif
drhb9bb7c12006-06-11 23:41:55 +0000303}
304
305/*
306** This routine takes the module argument that has been accumulating
307** in pParse->zArg[] and appends it to the list of arguments on the
308** virtual table currently under construction in pParse->pTable.
309*/
310static void addArgumentToVtab(Parse *pParse){
drh748ce212009-05-20 20:10:47 +0000311 if( pParse->sArg.z && ALWAYS(pParse->pNewTable) ){
drh7c2d87c2006-09-02 14:16:59 +0000312 const char *z = (const char*)pParse->sArg.z;
danielk197733b39332006-06-24 08:51:05 +0000313 int n = pParse->sArg.n;
drh17435752007-08-16 04:30:38 +0000314 sqlite3 *db = pParse->db;
315 addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
drhb9bb7c12006-06-11 23:41:55 +0000316 }
drhb9bb7c12006-06-11 23:41:55 +0000317}
318
319/*
320** The parser calls this routine after the CREATE VIRTUAL TABLE statement
321** has been completely parsed.
322*/
323void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
danielk1977595a5232009-07-24 17:58:53 +0000324 Table *pTab = pParse->pNewTable; /* The table being constructed */
325 sqlite3 *db = pParse->db; /* The database connection */
drhb9bb7c12006-06-11 23:41:55 +0000326
danielk1977595a5232009-07-24 17:58:53 +0000327 if( pTab==0 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000328 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000329 pParse->sArg.z = 0;
drhb9bb7c12006-06-11 23:41:55 +0000330 if( pTab->nModuleArg<1 ) return;
drhb9bb7c12006-06-11 23:41:55 +0000331
332 /* If the CREATE VIRTUAL TABLE statement is being entered for the
333 ** first time (in other words if the virtual table is actually being
334 ** created now instead of just being read out of sqlite_master) then
335 ** do additional initialization work and store the statement text
336 ** in the sqlite_master table.
337 */
338 if( !db->init.busy ){
339 char *zStmt;
danielk197778efaba2006-06-12 06:09:17 +0000340 char *zWhere;
drhb9bb7c12006-06-11 23:41:55 +0000341 int iDb;
342 Vdbe *v;
drhb9bb7c12006-06-11 23:41:55 +0000343
344 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
345 if( pEnd ){
drhb27b7f52008-12-10 18:03:45 +0000346 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
drhb9bb7c12006-06-11 23:41:55 +0000347 }
danielk19771e536952007-08-16 10:09:01 +0000348 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
drhb9bb7c12006-06-11 23:41:55 +0000349
350 /* A slot for the record has already been allocated in the
351 ** SQLITE_MASTER table. We just need to update that slot with all
danielk197778efaba2006-06-12 06:09:17 +0000352 ** the information we've collected.
353 **
drhb7654112008-01-12 12:48:07 +0000354 ** The VM register number pParse->regRowid holds the rowid of an
355 ** entry in the sqlite_master table tht was created for this vtab
356 ** by sqlite3StartTable().
drhb9bb7c12006-06-11 23:41:55 +0000357 */
358 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
359 sqlite3NestedParse(pParse,
360 "UPDATE %Q.%s "
danielk197778efaba2006-06-12 06:09:17 +0000361 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
drhb7654112008-01-12 12:48:07 +0000362 "WHERE rowid=#%d",
drhb9bb7c12006-06-11 23:41:55 +0000363 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
364 pTab->zName,
365 pTab->zName,
drhb7654112008-01-12 12:48:07 +0000366 zStmt,
367 pParse->regRowid
drhb9bb7c12006-06-11 23:41:55 +0000368 );
drh633e6d52008-07-28 19:34:53 +0000369 sqlite3DbFree(db, zStmt);
drhb9bb7c12006-06-11 23:41:55 +0000370 v = sqlite3GetVdbe(pParse);
drh9cbf3422008-01-17 16:22:13 +0000371 sqlite3ChangeCookie(pParse, iDb);
danielk197778efaba2006-06-12 06:09:17 +0000372
drh66a51672008-01-03 00:01:23 +0000373 sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
dan39f1bcb2010-09-29 07:16:46 +0000374 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
drh66a51672008-01-03 00:01:23 +0000375 sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC);
376 sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
drhea678832008-12-10 19:26:22 +0000377 pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
drhb9bb7c12006-06-11 23:41:55 +0000378 }
379
danielk197778efaba2006-06-12 06:09:17 +0000380 /* If we are rereading the sqlite_master table create the in-memory
danielk1977595a5232009-07-24 17:58:53 +0000381 ** record of the table. The xConnect() method is not called until
382 ** the first time the virtual table is used in an SQL statement. This
383 ** allows a schema that contains virtual tables to be loaded before
384 ** the required virtual table implementations are registered. */
danielk197778efaba2006-06-12 06:09:17 +0000385 else {
danielk197778efaba2006-06-12 06:09:17 +0000386 Table *pOld;
387 Schema *pSchema = pTab->pSchema;
388 const char *zName = pTab->zName;
drha83ccca2009-04-28 13:01:09 +0000389 int nName = sqlite3Strlen30(zName);
danielk197778efaba2006-06-12 06:09:17 +0000390 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
391 if( pOld ){
danielk1977a1644fd2007-08-29 12:31:25 +0000392 db->mallocFailed = 1;
danielk197778efaba2006-06-12 06:09:17 +0000393 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
394 return;
395 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000396 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000397 }
398}
399
400/*
401** The parser calls this routine when it sees the first token
402** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
403*/
404void sqlite3VtabArgInit(Parse *pParse){
405 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000406 pParse->sArg.z = 0;
407 pParse->sArg.n = 0;
drhb9bb7c12006-06-11 23:41:55 +0000408}
409
410/*
411** The parser calls this routine for each token after the first token
412** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
413*/
414void sqlite3VtabArgExtend(Parse *pParse, Token *p){
danielk197733b39332006-06-24 08:51:05 +0000415 Token *pArg = &pParse->sArg;
416 if( pArg->z==0 ){
417 pArg->z = p->z;
418 pArg->n = p->n;
419 }else{
420 assert(pArg->z < p->z);
drhb27b7f52008-12-10 18:03:45 +0000421 pArg->n = (int)(&p->z[p->n] - pArg->z);
drhb9bb7c12006-06-11 23:41:55 +0000422 }
drhb9bb7c12006-06-11 23:41:55 +0000423}
424
danielk197778efaba2006-06-12 06:09:17 +0000425/*
danielk19779da9d472006-06-14 06:58:15 +0000426** Invoke a virtual table constructor (either xCreate or xConnect). The
427** pointer to the function to invoke is passed as the fourth parameter
428** to this procedure.
429*/
430static int vtabCallConstructor(
431 sqlite3 *db,
432 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000433 Module *pMod,
drhe4102962006-09-11 00:34:22 +0000434 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
danielk19779da9d472006-06-14 06:58:15 +0000435 char **pzErr
436){
danielk1977595a5232009-07-24 17:58:53 +0000437 VTable *pVTable;
danielk19779da9d472006-06-14 06:58:15 +0000438 int rc;
drhe4102962006-09-11 00:34:22 +0000439 const char *const*azArg = (const char *const*)pTab->azModuleArg;
danielk19779da9d472006-06-14 06:58:15 +0000440 int nArg = pTab->nModuleArg;
drh4ca8aac2006-09-10 17:31:58 +0000441 char *zErr = 0;
danielk19771e536952007-08-16 10:09:01 +0000442 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000443
danielk197701256832007-04-18 14:24:32 +0000444 if( !zModuleName ){
445 return SQLITE_NOMEM;
446 }
447
danielk1977595a5232009-07-24 17:58:53 +0000448 pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
449 if( !pVTable ){
450 sqlite3DbFree(db, zModuleName);
451 return SQLITE_NOMEM;
452 }
453 pVTable->db = db;
454 pVTable->pMod = pMod;
455
danielk19779da9d472006-06-14 06:58:15 +0000456 assert( !db->pVTab );
457 assert( xConstruct );
danielk19779da9d472006-06-14 06:58:15 +0000458 db->pVTab = pTab;
danielk1977595a5232009-07-24 17:58:53 +0000459
460 /* Invoke the virtual table constructor */
danielk1977595a5232009-07-24 17:58:53 +0000461 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
drh55f04f22009-05-11 23:37:59 +0000462 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
danielk19779da9d472006-06-14 06:58:15 +0000463
464 if( SQLITE_OK!=rc ){
drh4ca8aac2006-09-10 17:31:58 +0000465 if( zErr==0 ){
danielk19771e536952007-08-16 10:09:01 +0000466 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
drh4ca8aac2006-09-10 17:31:58 +0000467 }else {
danielk19771e536952007-08-16 10:09:01 +0000468 *pzErr = sqlite3MPrintf(db, "%s", zErr);
drhb9755982010-07-24 16:34:37 +0000469 sqlite3_free(zErr);
drhfe1368e2006-09-10 17:08:29 +0000470 }
danielk1977595a5232009-07-24 17:58:53 +0000471 sqlite3DbFree(db, pVTable);
472 }else if( ALWAYS(pVTable->pVtab) ){
473 /* Justification of ALWAYS(): A correct vtab constructor must allocate
474 ** the sqlite3_vtab object if successful. */
475 pVTable->pVtab->pModule = pMod->pModule;
476 pVTable->nRef = 1;
477 if( db->pVTab ){
478 const char *zFormat = "vtable constructor did not declare schema: %s";
479 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
480 sqlite3VtabUnlock(pVTable);
481 rc = SQLITE_ERROR;
482 }else{
483 int iCol;
484 /* If everything went according to plan, link the new VTable structure
485 ** into the linked list headed by pTab->pVTable. Then loop through the
486 ** columns of the table to see if any of them contain the token "hidden".
487 ** If so, set the Column.isHidden flag and remove the token from
488 ** the type string. */
489 pVTable->pNext = pTab->pVTable;
490 pTab->pVTable = pVTable;
danielk1977034ca142007-06-26 10:38:54 +0000491
danielk1977595a5232009-07-24 17:58:53 +0000492 for(iCol=0; iCol<pTab->nCol; iCol++){
493 char *zType = pTab->aCol[iCol].zType;
494 int nType;
495 int i = 0;
496 if( !zType ) continue;
497 nType = sqlite3Strlen30(zType);
498 if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
499 for(i=0; i<nType; i++){
500 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
501 && (zType[i+7]=='\0' || zType[i+7]==' ')
502 ){
503 i++;
504 break;
505 }
danielk1977034ca142007-06-26 10:38:54 +0000506 }
507 }
danielk1977595a5232009-07-24 17:58:53 +0000508 if( i<nType ){
509 int j;
510 int nDel = 6 + (zType[i+6] ? 1 : 0);
511 for(j=i; (j+nDel)<=nType; j++){
512 zType[j] = zType[j+nDel];
513 }
514 if( zType[i]=='\0' && i>0 ){
515 assert(zType[i-1]==' ');
516 zType[i-1] = '\0';
517 }
518 pTab->aCol[iCol].isHidden = 1;
danielk1977034ca142007-06-26 10:38:54 +0000519 }
danielk1977034ca142007-06-26 10:38:54 +0000520 }
521 }
522 }
danielk1977595a5232009-07-24 17:58:53 +0000523
524 sqlite3DbFree(db, zModuleName);
525 db->pVTab = 0;
danielk19779da9d472006-06-14 06:58:15 +0000526 return rc;
527}
528
529/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000530** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000531** of the virtual table pTab. If an error occurs, an error code is returned
532** and an error left in pParse.
533**
534** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000535*/
536int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977595a5232009-07-24 17:58:53 +0000537 sqlite3 *db = pParse->db;
538 const char *zMod;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000539 Module *pMod;
danielk1977595a5232009-07-24 17:58:53 +0000540 int rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000541
drh748ce212009-05-20 20:10:47 +0000542 assert( pTab );
danielk1977595a5232009-07-24 17:58:53 +0000543 if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000544 return SQLITE_OK;
545 }
546
danielk1977595a5232009-07-24 17:58:53 +0000547 /* Locate the required virtual table module */
548 zMod = pTab->azModuleArg[0];
549 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
550
danielk1977d1ab1ba2006-06-15 04:28:13 +0000551 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000552 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000553 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000554 rc = SQLITE_ERROR;
danielk1977595a5232009-07-24 17:58:53 +0000555 }else{
danielk19779da9d472006-06-14 06:58:15 +0000556 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000557 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000558 if( rc!=SQLITE_OK ){
559 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000560 }
drh633e6d52008-07-28 19:34:53 +0000561 sqlite3DbFree(db, zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000562 }
563
564 return rc;
565}
566
danielk19779da9d472006-06-14 06:58:15 +0000567/*
danielk1977595a5232009-07-24 17:58:53 +0000568** Add the virtual table pVTab to the array sqlite3.aVTrans[].
danielk1977e7ff4032006-06-17 11:30:32 +0000569*/
danielk1977595a5232009-07-24 17:58:53 +0000570static int addToVTrans(sqlite3 *db, VTable *pVTab){
danielk1977e7ff4032006-06-17 11:30:32 +0000571 const int ARRAY_INCR = 5;
572
573 /* Grow the sqlite3.aVTrans array if required */
574 if( (db->nVTrans%ARRAY_INCR)==0 ){
danielk1977595a5232009-07-24 17:58:53 +0000575 VTable **aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000576 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
danielk197726783a52007-08-29 14:06:22 +0000577 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
danielk1977e7ff4032006-06-17 11:30:32 +0000578 if( !aVTrans ){
579 return SQLITE_NOMEM;
580 }
581 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
582 db->aVTrans = aVTrans;
583 }
584
585 /* Add pVtab to the end of sqlite3.aVTrans */
danielk1977595a5232009-07-24 17:58:53 +0000586 db->aVTrans[db->nVTrans++] = pVTab;
587 sqlite3VtabLock(pVTab);
danielk1977e7ff4032006-06-17 11:30:32 +0000588 return SQLITE_OK;
589}
590
591/*
danielk19779da9d472006-06-14 06:58:15 +0000592** This function is invoked by the vdbe to call the xCreate method
593** of the virtual table named zTab in database iDb.
594**
595** If an error occurs, *pzErr is set to point an an English language
596** description of the error and an SQLITE_XXX error code is returned.
drh633e6d52008-07-28 19:34:53 +0000597** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
danielk19779da9d472006-06-14 06:58:15 +0000598*/
599int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
600 int rc = SQLITE_OK;
601 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000602 Module *pMod;
danielk1977595a5232009-07-24 17:58:53 +0000603 const char *zMod;
danielk19779da9d472006-06-14 06:58:15 +0000604
605 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk1977595a5232009-07-24 17:58:53 +0000606 assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
607
608 /* Locate the required virtual table module */
609 zMod = pTab->azModuleArg[0];
610 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
danielk19779da9d472006-06-14 06:58:15 +0000611
612 /* If the module has been registered and includes a Create method,
613 ** invoke it now. If the module has not been registered, return an
614 ** error. Otherwise, do nothing.
615 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000616 if( !pMod ){
danielk1977595a5232009-07-24 17:58:53 +0000617 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
danielk19779da9d472006-06-14 06:58:15 +0000618 rc = SQLITE_ERROR;
619 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000620 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000621 }
622
drh748ce212009-05-20 20:10:47 +0000623 /* Justification of ALWAYS(): The xConstructor method is required to
624 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
danielk1977595a5232009-07-24 17:58:53 +0000625 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
626 rc = addToVTrans(db, sqlite3GetVTable(db, pTab));
danielk1977e7ff4032006-06-17 11:30:32 +0000627 }
628
danielk19779da9d472006-06-14 06:58:15 +0000629 return rc;
630}
danielk1977be8a7832006-06-13 15:00:54 +0000631
632/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000633** This function is used to set the schema of a virtual table. It is only
634** valid to call this function from within the xCreate() or xConnect() of a
635** virtual table module.
636*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000637int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
drhe98c9042009-06-02 21:31:38 +0000638 Parse *pParse;
danielk19777e6ebfb2006-06-12 11:24:37 +0000639
640 int rc = SQLITE_OK;
drh27641702007-08-22 02:56:42 +0000641 Table *pTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000642 char *zErr = 0;
643
drh27641702007-08-22 02:56:42 +0000644 sqlite3_mutex_enter(db->mutex);
645 pTab = db->pVTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000646 if( !pTab ){
647 sqlite3Error(db, SQLITE_MISUSE, 0);
drh27641702007-08-22 02:56:42 +0000648 sqlite3_mutex_leave(db->mutex);
drh413c3d32010-02-23 20:11:56 +0000649 return SQLITE_MISUSE_BKPT;
danielk19777e6ebfb2006-06-12 11:24:37 +0000650 }
danielk1977595a5232009-07-24 17:58:53 +0000651 assert( (pTab->tabFlags & TF_Virtual)!=0 );
danielk19777e6ebfb2006-06-12 11:24:37 +0000652
drhe98c9042009-06-02 21:31:38 +0000653 pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
654 if( pParse==0 ){
655 rc = SQLITE_NOMEM;
656 }else{
657 pParse->declareVtab = 1;
658 pParse->db = db;
drh8b307fb2010-04-06 15:57:05 +0000659 pParse->nQueryLoop = 1;
drhe98c9042009-06-02 21:31:38 +0000660
dan84db21e2009-12-08 19:05:53 +0000661 if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
dan84db21e2009-12-08 19:05:53 +0000662 && pParse->pNewTable
drh0e3c5122009-12-08 22:16:15 +0000663 && !db->mallocFailed
dan84db21e2009-12-08 19:05:53 +0000664 && !pParse->pNewTable->pSelect
665 && (pParse->pNewTable->tabFlags & TF_Virtual)==0
drhe98c9042009-06-02 21:31:38 +0000666 ){
danielk1977595a5232009-07-24 17:58:53 +0000667 if( !pTab->aCol ){
668 pTab->aCol = pParse->pNewTable->aCol;
669 pTab->nCol = pParse->pNewTable->nCol;
670 pParse->pNewTable->nCol = 0;
671 pParse->pNewTable->aCol = 0;
672 }
drhe98c9042009-06-02 21:31:38 +0000673 db->pVTab = 0;
dan84db21e2009-12-08 19:05:53 +0000674 }else{
dan06b5db02010-10-21 15:12:44 +0000675 sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
dan78f9b732010-08-11 11:59:37 +0000676 sqlite3DbFree(db, zErr);
drhe98c9042009-06-02 21:31:38 +0000677 rc = SQLITE_ERROR;
678 }
679 pParse->declareVtab = 0;
680
681 if( pParse->pVdbe ){
682 sqlite3VdbeFinalize(pParse->pVdbe);
683 }
dan1feeaed2010-07-23 15:41:47 +0000684 sqlite3DeleteTable(db, pParse->pNewTable);
drhe98c9042009-06-02 21:31:38 +0000685 sqlite3StackFree(db, pParse);
danielk19777e6ebfb2006-06-12 11:24:37 +0000686 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000687
drh4ac285a2006-09-15 07:28:50 +0000688 assert( (rc&0xff)==rc );
drh27641702007-08-22 02:56:42 +0000689 rc = sqlite3ApiExit(db, rc);
690 sqlite3_mutex_leave(db->mutex);
691 return rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000692}
693
694/*
danielk19779e39ce82006-06-12 16:01:21 +0000695** This function is invoked by the vdbe to call the xDestroy method
696** of the virtual table named zTab in database iDb. This occurs
697** when a DROP TABLE is mentioned.
698**
699** This call is a no-op if zTab is not a virtual table.
700*/
drh748ce212009-05-20 20:10:47 +0000701int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
danielk19779e39ce82006-06-12 16:01:21 +0000702 int rc = SQLITE_OK;
703 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000704
705 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk1977595a5232009-07-24 17:58:53 +0000706 if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
707 VTable *p = vtabDisconnectAll(db, pTab);
708
danielk19779e39ce82006-06-12 16:01:21 +0000709 assert( rc==SQLITE_OK );
danielk1977595a5232009-07-24 17:58:53 +0000710 rc = p->pMod->pModule->xDestroy(p->pVtab);
danielk1977595a5232009-07-24 17:58:53 +0000711
712 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
danielk19779e39ce82006-06-12 16:01:21 +0000713 if( rc==SQLITE_OK ){
danielk1977595a5232009-07-24 17:58:53 +0000714 assert( pTab->pVTable==p && p->pNext==0 );
715 p->pVtab = 0;
716 pTab->pVTable = 0;
717 sqlite3VtabUnlock(p);
danielk19779e39ce82006-06-12 16:01:21 +0000718 }
719 }
720
721 return rc;
722}
723
danielk1977f9e7dda2006-06-16 16:08:53 +0000724/*
danielk1977e7ff4032006-06-17 11:30:32 +0000725** This function invokes either the xRollback or xCommit method
726** of each of the virtual tables in the sqlite3.aVTrans array. The method
727** called is identified by the second argument, "offset", which is
728** the offset of the method to call in the sqlite3_module structure.
729**
730** The array is cleared after invoking the callbacks.
731*/
drh7209c692008-04-27 18:40:11 +0000732static void callFinaliser(sqlite3 *db, int offset){
danielk1977e7ff4032006-06-17 11:30:32 +0000733 int i;
danielk19770b83fa82007-04-19 14:48:37 +0000734 if( db->aVTrans ){
drh748ce212009-05-20 20:10:47 +0000735 for(i=0; i<db->nVTrans; i++){
danielk1977595a5232009-07-24 17:58:53 +0000736 VTable *pVTab = db->aVTrans[i];
737 sqlite3_vtab *p = pVTab->pVtab;
738 if( p ){
739 int (*x)(sqlite3_vtab *);
740 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
741 if( x ) x(p);
742 }
743 sqlite3VtabUnlock(pVTab);
danielk19770b83fa82007-04-19 14:48:37 +0000744 }
drh633e6d52008-07-28 19:34:53 +0000745 sqlite3DbFree(db, db->aVTrans);
danielk19770b83fa82007-04-19 14:48:37 +0000746 db->nVTrans = 0;
747 db->aVTrans = 0;
danielk1977e7ff4032006-06-17 11:30:32 +0000748 }
danielk1977e7ff4032006-06-17 11:30:32 +0000749}
750
751/*
danielk19773e3a84d2008-08-01 17:37:40 +0000752** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
753** array. Return the error code for the first error that occurs, or
754** SQLITE_OK if all xSync operations are successful.
755**
756** Set *pzErrmsg to point to a buffer that should be released using
757** sqlite3DbFree() containing an error message, if one is available.
danielk1977f9e7dda2006-06-16 16:08:53 +0000758*/
danielk19773e3a84d2008-08-01 17:37:40 +0000759int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
danielk1977e7ff4032006-06-17 11:30:32 +0000760 int i;
761 int rc = SQLITE_OK;
danielk1977595a5232009-07-24 17:58:53 +0000762 VTable **aVTrans = db->aVTrans;
danielk197720b1eaf2006-07-26 16:22:14 +0000763
danielk197720b1eaf2006-07-26 16:22:14 +0000764 db->aVTrans = 0;
drh748ce212009-05-20 20:10:47 +0000765 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
danielk1977e7ff4032006-06-17 11:30:32 +0000766 int (*x)(sqlite3_vtab *);
danielk1977595a5232009-07-24 17:58:53 +0000767 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
768 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
danielk1977e7ff4032006-06-17 11:30:32 +0000769 rc = x(pVtab);
danielk19773e3a84d2008-08-01 17:37:40 +0000770 sqlite3DbFree(db, *pzErrmsg);
drhb9755982010-07-24 16:34:37 +0000771 *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
772 sqlite3_free(pVtab->zErrMsg);
danielk1977e7ff4032006-06-17 11:30:32 +0000773 }
774 }
danielk197720b1eaf2006-07-26 16:22:14 +0000775 db->aVTrans = aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000776 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000777}
778
779/*
780** Invoke the xRollback method of all virtual tables in the
781** sqlite3.aVTrans array. Then clear the array itself.
782*/
783int sqlite3VtabRollback(sqlite3 *db){
drh7209c692008-04-27 18:40:11 +0000784 callFinaliser(db, offsetof(sqlite3_module,xRollback));
danielk1977e7ff4032006-06-17 11:30:32 +0000785 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000786}
787
788/*
789** Invoke the xCommit method of all virtual tables in the
790** sqlite3.aVTrans array. Then clear the array itself.
791*/
792int sqlite3VtabCommit(sqlite3 *db){
drh7209c692008-04-27 18:40:11 +0000793 callFinaliser(db, offsetof(sqlite3_module,xCommit));
danielk1977e7ff4032006-06-17 11:30:32 +0000794 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000795}
796
797/*
798** If the virtual table pVtab supports the transaction interface
799** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
800** not currently open, invoke the xBegin method now.
801**
802** If the xBegin call is successful, place the sqlite3_vtab pointer
803** in the sqlite3.aVTrans array.
804*/
danielk1977595a5232009-07-24 17:58:53 +0000805int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
danielk1977f9e7dda2006-06-16 16:08:53 +0000806 int rc = SQLITE_OK;
danielk197720b1eaf2006-07-26 16:22:14 +0000807 const sqlite3_module *pModule;
808
809 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
810 ** than zero, then this function is being called from within a
811 ** virtual module xSync() callback. It is illegal to write to
drh748ce212009-05-20 20:10:47 +0000812 ** virtual module tables in this case, so return SQLITE_LOCKED.
danielk197720b1eaf2006-07-26 16:22:14 +0000813 */
danielk1977093e0f62008-11-13 18:00:14 +0000814 if( sqlite3VtabInSync(db) ){
danielk197720b1eaf2006-07-26 16:22:14 +0000815 return SQLITE_LOCKED;
816 }
danielk1977595a5232009-07-24 17:58:53 +0000817 if( !pVTab ){
danielk197720b1eaf2006-07-26 16:22:14 +0000818 return SQLITE_OK;
819 }
danielk1977595a5232009-07-24 17:58:53 +0000820 pModule = pVTab->pVtab->pModule;
danielk197720b1eaf2006-07-26 16:22:14 +0000821
danielk1977f9e7dda2006-06-16 16:08:53 +0000822 if( pModule->xBegin ){
823 int i;
824
danielk197720b1eaf2006-07-26 16:22:14 +0000825
danielk1977f9e7dda2006-06-16 16:08:53 +0000826 /* If pVtab is already in the aVTrans array, return early */
drh748ce212009-05-20 20:10:47 +0000827 for(i=0; i<db->nVTrans; i++){
danielk1977595a5232009-07-24 17:58:53 +0000828 if( db->aVTrans[i]==pVTab ){
danielk1977f9e7dda2006-06-16 16:08:53 +0000829 return SQLITE_OK;
830 }
831 }
832
833 /* Invoke the xBegin method */
danielk1977595a5232009-07-24 17:58:53 +0000834 rc = pModule->xBegin(pVTab->pVtab);
danielk19773e3a84d2008-08-01 17:37:40 +0000835 if( rc==SQLITE_OK ){
danielk1977595a5232009-07-24 17:58:53 +0000836 rc = addToVTrans(db, pVTab);
danielk1977f9e7dda2006-06-16 16:08:53 +0000837 }
danielk1977f9e7dda2006-06-16 16:08:53 +0000838 }
839 return rc;
840}
841
drhb7f6f682006-07-08 17:06:43 +0000842/*
843** The first parameter (pDef) is a function implementation. The
844** second parameter (pExpr) is the first argument to this function.
845** If pExpr is a column in a virtual table, then let the virtual
846** table implementation have an opportunity to overload the function.
847**
848** This routine is used to allow virtual table implementations to
849** overload MATCH, LIKE, GLOB, and REGEXP operators.
850**
851** Return either the pDef argument (indicating no change) or a
852** new FuncDef structure that is marked as ephemeral using the
853** SQLITE_FUNC_EPHEM flag.
854*/
855FuncDef *sqlite3VtabOverloadFunction(
drh17435752007-08-16 04:30:38 +0000856 sqlite3 *db, /* Database connection for reporting malloc problems */
drhb7f6f682006-07-08 17:06:43 +0000857 FuncDef *pDef, /* Function to possibly overload */
858 int nArg, /* Number of arguments to the function */
859 Expr *pExpr /* First argument to the function */
860){
861 Table *pTab;
862 sqlite3_vtab *pVtab;
863 sqlite3_module *pMod;
drhdc5ea5c2008-12-10 17:19:59 +0000864 void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
865 void *pArg = 0;
drhb7f6f682006-07-08 17:06:43 +0000866 FuncDef *pNew;
drh777b17a2007-09-20 10:02:54 +0000867 int rc = 0;
drha70034d2006-09-18 20:24:02 +0000868 char *zLowerName;
869 unsigned char *z;
870
drhb7f6f682006-07-08 17:06:43 +0000871
872 /* Check to see the left operand is a column in a virtual table */
drh748ce212009-05-20 20:10:47 +0000873 if( NEVER(pExpr==0) ) return pDef;
drhb7f6f682006-07-08 17:06:43 +0000874 if( pExpr->op!=TK_COLUMN ) return pDef;
875 pTab = pExpr->pTab;
drh748ce212009-05-20 20:10:47 +0000876 if( NEVER(pTab==0) ) return pDef;
drh7d10d5a2008-08-20 16:35:10 +0000877 if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
danielk1977595a5232009-07-24 17:58:53 +0000878 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
drhb7f6f682006-07-08 17:06:43 +0000879 assert( pVtab!=0 );
880 assert( pVtab->pModule!=0 );
danielk19775bd270b2006-07-25 15:14:52 +0000881 pMod = (sqlite3_module *)pVtab->pModule;
drhb7f6f682006-07-08 17:06:43 +0000882 if( pMod->xFindFunction==0 ) return pDef;
883
rseb72e88d2007-09-20 11:32:18 +0000884 /* Call the xFindFunction method on the virtual table implementation
drha70034d2006-09-18 20:24:02 +0000885 ** to see if the implementation wants to overload this function
886 */
drh17435752007-08-16 04:30:38 +0000887 zLowerName = sqlite3DbStrDup(db, pDef->zName);
888 if( zLowerName ){
889 for(z=(unsigned char*)zLowerName; *z; z++){
890 *z = sqlite3UpperToLower[*z];
891 }
892 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
drh633e6d52008-07-28 19:34:53 +0000893 sqlite3DbFree(db, zLowerName);
drha70034d2006-09-18 20:24:02 +0000894 }
drha70034d2006-09-18 20:24:02 +0000895 if( rc==0 ){
drhb7f6f682006-07-08 17:06:43 +0000896 return pDef;
897 }
898
899 /* Create a new ephemeral function definition for the overloaded
900 ** function */
drhea678832008-12-10 19:26:22 +0000901 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
danielk1977e25a50b2009-07-01 18:04:20 +0000902 + sqlite3Strlen30(pDef->zName) + 1);
drhb7f6f682006-07-08 17:06:43 +0000903 if( pNew==0 ){
904 return pDef;
905 }
906 *pNew = *pDef;
danielk19778c0a7912008-08-20 14:49:23 +0000907 pNew->zName = (char *)&pNew[1];
drhea678832008-12-10 19:26:22 +0000908 memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
drhb7f6f682006-07-08 17:06:43 +0000909 pNew->xFunc = xFunc;
910 pNew->pUserData = pArg;
drhb7f6f682006-07-08 17:06:43 +0000911 pNew->flags |= SQLITE_FUNC_EPHEM;
912 return pNew;
913}
914
drh4f3dd152008-04-28 18:46:43 +0000915/*
916** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
917** array so that an OP_VBegin will get generated for it. Add pTab to the
918** array if it is missing. If pTab is already in the array, this routine
919** is a no-op.
920*/
921void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
dan65a7cd12009-09-01 12:16:01 +0000922 Parse *pToplevel = sqlite3ParseToplevel(pParse);
drh4f3dd152008-04-28 18:46:43 +0000923 int i, n;
drh748ce212009-05-20 20:10:47 +0000924 Table **apVtabLock;
925
drh4f3dd152008-04-28 18:46:43 +0000926 assert( IsVirtual(pTab) );
dan65a7cd12009-09-01 12:16:01 +0000927 for(i=0; i<pToplevel->nVtabLock; i++){
928 if( pTab==pToplevel->apVtabLock[i] ) return;
drh4f3dd152008-04-28 18:46:43 +0000929 }
dan65a7cd12009-09-01 12:16:01 +0000930 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
931 apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
drh748ce212009-05-20 20:10:47 +0000932 if( apVtabLock ){
dan65a7cd12009-09-01 12:16:01 +0000933 pToplevel->apVtabLock = apVtabLock;
934 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
drh344c38e2008-05-05 13:23:04 +0000935 }else{
dan65a7cd12009-09-01 12:16:01 +0000936 pToplevel->db->mallocFailed = 1;
drh4f3dd152008-04-28 18:46:43 +0000937 }
938}
939
drhb9bb7c12006-06-11 23:41:55 +0000940#endif /* SQLITE_OMIT_VIRTUALTABLE */