blob: 24e922e8dd4f33b39a0a937491bab9c04e455e36 [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*/
224void sqlite3VtabClear(Table *p){
danielk1977595a5232009-07-24 17:58:53 +0000225 vtabDisconnectAll(0, p);
drhb9bb7c12006-06-11 23:41:55 +0000226 if( p->azModuleArg ){
227 int i;
228 for(i=0; i<p->nModuleArg; i++){
danielk1977595a5232009-07-24 17:58:53 +0000229 sqlite3DbFree(p->dbMem, p->azModuleArg[i]);
drhb9bb7c12006-06-11 23:41:55 +0000230 }
danielk1977595a5232009-07-24 17:58:53 +0000231 sqlite3DbFree(p->dbMem, 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);
danielk19771e536952007-08-16 10:09:01 +0000374 zWhere = sqlite3MPrintf(db, "name='%q'", 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 }
danielk1977a04a34f2007-04-16 15:06:25 +0000396 pSchema->db = pParse->db;
danielk19777e6ebfb2006-06-12 11:24:37 +0000397 pParse->pNewTable = 0;
drhb9bb7c12006-06-11 23:41:55 +0000398 }
399}
400
401/*
402** The parser calls this routine when it sees the first token
403** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
404*/
405void sqlite3VtabArgInit(Parse *pParse){
406 addArgumentToVtab(pParse);
danielk197733b39332006-06-24 08:51:05 +0000407 pParse->sArg.z = 0;
408 pParse->sArg.n = 0;
drhb9bb7c12006-06-11 23:41:55 +0000409}
410
411/*
412** The parser calls this routine for each token after the first token
413** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
414*/
415void sqlite3VtabArgExtend(Parse *pParse, Token *p){
danielk197733b39332006-06-24 08:51:05 +0000416 Token *pArg = &pParse->sArg;
417 if( pArg->z==0 ){
418 pArg->z = p->z;
419 pArg->n = p->n;
420 }else{
421 assert(pArg->z < p->z);
drhb27b7f52008-12-10 18:03:45 +0000422 pArg->n = (int)(&p->z[p->n] - pArg->z);
drhb9bb7c12006-06-11 23:41:55 +0000423 }
drhb9bb7c12006-06-11 23:41:55 +0000424}
425
danielk197778efaba2006-06-12 06:09:17 +0000426/*
danielk19779da9d472006-06-14 06:58:15 +0000427** Invoke a virtual table constructor (either xCreate or xConnect). The
428** pointer to the function to invoke is passed as the fourth parameter
429** to this procedure.
430*/
431static int vtabCallConstructor(
432 sqlite3 *db,
433 Table *pTab,
danielk1977d1ab1ba2006-06-15 04:28:13 +0000434 Module *pMod,
drhe4102962006-09-11 00:34:22 +0000435 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
danielk19779da9d472006-06-14 06:58:15 +0000436 char **pzErr
437){
danielk1977595a5232009-07-24 17:58:53 +0000438 VTable *pVTable;
danielk19779da9d472006-06-14 06:58:15 +0000439 int rc;
drhe4102962006-09-11 00:34:22 +0000440 const char *const*azArg = (const char *const*)pTab->azModuleArg;
danielk19779da9d472006-06-14 06:58:15 +0000441 int nArg = pTab->nModuleArg;
drh4ca8aac2006-09-10 17:31:58 +0000442 char *zErr = 0;
danielk19771e536952007-08-16 10:09:01 +0000443 char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
danielk19779da9d472006-06-14 06:58:15 +0000444
danielk197701256832007-04-18 14:24:32 +0000445 if( !zModuleName ){
446 return SQLITE_NOMEM;
447 }
448
danielk1977595a5232009-07-24 17:58:53 +0000449 pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
450 if( !pVTable ){
451 sqlite3DbFree(db, zModuleName);
452 return SQLITE_NOMEM;
453 }
454 pVTable->db = db;
455 pVTable->pMod = pMod;
456
danielk19779da9d472006-06-14 06:58:15 +0000457 assert( !db->pVTab );
458 assert( xConstruct );
danielk19779da9d472006-06-14 06:58:15 +0000459 db->pVTab = pTab;
danielk1977595a5232009-07-24 17:58:53 +0000460
461 /* Invoke the virtual table constructor */
danielk1977595a5232009-07-24 17:58:53 +0000462 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
drh55f04f22009-05-11 23:37:59 +0000463 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
danielk19779da9d472006-06-14 06:58:15 +0000464
465 if( SQLITE_OK!=rc ){
drh4ca8aac2006-09-10 17:31:58 +0000466 if( zErr==0 ){
danielk19771e536952007-08-16 10:09:01 +0000467 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
drh4ca8aac2006-09-10 17:31:58 +0000468 }else {
danielk19771e536952007-08-16 10:09:01 +0000469 *pzErr = sqlite3MPrintf(db, "%s", zErr);
drh633e6d52008-07-28 19:34:53 +0000470 sqlite3DbFree(db, zErr);
drhfe1368e2006-09-10 17:08:29 +0000471 }
danielk1977595a5232009-07-24 17:58:53 +0000472 sqlite3DbFree(db, pVTable);
473 }else if( ALWAYS(pVTable->pVtab) ){
474 /* Justification of ALWAYS(): A correct vtab constructor must allocate
475 ** the sqlite3_vtab object if successful. */
476 pVTable->pVtab->pModule = pMod->pModule;
477 pVTable->nRef = 1;
478 if( db->pVTab ){
479 const char *zFormat = "vtable constructor did not declare schema: %s";
480 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
481 sqlite3VtabUnlock(pVTable);
482 rc = SQLITE_ERROR;
483 }else{
484 int iCol;
485 /* If everything went according to plan, link the new VTable structure
486 ** into the linked list headed by pTab->pVTable. Then loop through the
487 ** columns of the table to see if any of them contain the token "hidden".
488 ** If so, set the Column.isHidden flag and remove the token from
489 ** the type string. */
490 pVTable->pNext = pTab->pVTable;
491 pTab->pVTable = pVTable;
danielk1977034ca142007-06-26 10:38:54 +0000492
danielk1977595a5232009-07-24 17:58:53 +0000493 for(iCol=0; iCol<pTab->nCol; iCol++){
494 char *zType = pTab->aCol[iCol].zType;
495 int nType;
496 int i = 0;
497 if( !zType ) continue;
498 nType = sqlite3Strlen30(zType);
499 if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
500 for(i=0; i<nType; i++){
501 if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
502 && (zType[i+7]=='\0' || zType[i+7]==' ')
503 ){
504 i++;
505 break;
506 }
danielk1977034ca142007-06-26 10:38:54 +0000507 }
508 }
danielk1977595a5232009-07-24 17:58:53 +0000509 if( i<nType ){
510 int j;
511 int nDel = 6 + (zType[i+6] ? 1 : 0);
512 for(j=i; (j+nDel)<=nType; j++){
513 zType[j] = zType[j+nDel];
514 }
515 if( zType[i]=='\0' && i>0 ){
516 assert(zType[i-1]==' ');
517 zType[i-1] = '\0';
518 }
519 pTab->aCol[iCol].isHidden = 1;
danielk1977034ca142007-06-26 10:38:54 +0000520 }
danielk1977034ca142007-06-26 10:38:54 +0000521 }
522 }
523 }
danielk1977595a5232009-07-24 17:58:53 +0000524
525 sqlite3DbFree(db, zModuleName);
526 db->pVTab = 0;
danielk19779da9d472006-06-14 06:58:15 +0000527 return rc;
528}
529
530/*
danielk19777e6ebfb2006-06-12 11:24:37 +0000531** This function is invoked by the parser to call the xConnect() method
danielk1977fe3fcbe22006-06-12 12:08:45 +0000532** of the virtual table pTab. If an error occurs, an error code is returned
533** and an error left in pParse.
534**
535** This call is a no-op if table pTab is not a virtual table.
danielk19777e6ebfb2006-06-12 11:24:37 +0000536*/
537int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
danielk1977595a5232009-07-24 17:58:53 +0000538 sqlite3 *db = pParse->db;
539 const char *zMod;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000540 Module *pMod;
danielk1977595a5232009-07-24 17:58:53 +0000541 int rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000542
drh748ce212009-05-20 20:10:47 +0000543 assert( pTab );
danielk1977595a5232009-07-24 17:58:53 +0000544 if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000545 return SQLITE_OK;
546 }
547
danielk1977595a5232009-07-24 17:58:53 +0000548 /* Locate the required virtual table module */
549 zMod = pTab->azModuleArg[0];
550 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
551
danielk1977d1ab1ba2006-06-15 04:28:13 +0000552 if( !pMod ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000553 const char *zModule = pTab->azModuleArg[0];
danielk1977a4e76362006-06-14 06:31:28 +0000554 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
danielk19777e6ebfb2006-06-12 11:24:37 +0000555 rc = SQLITE_ERROR;
danielk1977595a5232009-07-24 17:58:53 +0000556 }else{
danielk19779da9d472006-06-14 06:58:15 +0000557 char *zErr = 0;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000558 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
danielk19779da9d472006-06-14 06:58:15 +0000559 if( rc!=SQLITE_OK ){
560 sqlite3ErrorMsg(pParse, "%s", zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000561 }
drh633e6d52008-07-28 19:34:53 +0000562 sqlite3DbFree(db, zErr);
danielk19777e6ebfb2006-06-12 11:24:37 +0000563 }
564
565 return rc;
566}
567
danielk19779da9d472006-06-14 06:58:15 +0000568/*
danielk1977595a5232009-07-24 17:58:53 +0000569** Add the virtual table pVTab to the array sqlite3.aVTrans[].
danielk1977e7ff4032006-06-17 11:30:32 +0000570*/
danielk1977595a5232009-07-24 17:58:53 +0000571static int addToVTrans(sqlite3 *db, VTable *pVTab){
danielk1977e7ff4032006-06-17 11:30:32 +0000572 const int ARRAY_INCR = 5;
573
574 /* Grow the sqlite3.aVTrans array if required */
575 if( (db->nVTrans%ARRAY_INCR)==0 ){
danielk1977595a5232009-07-24 17:58:53 +0000576 VTable **aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000577 int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
danielk197726783a52007-08-29 14:06:22 +0000578 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
danielk1977e7ff4032006-06-17 11:30:32 +0000579 if( !aVTrans ){
580 return SQLITE_NOMEM;
581 }
582 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
583 db->aVTrans = aVTrans;
584 }
585
586 /* Add pVtab to the end of sqlite3.aVTrans */
danielk1977595a5232009-07-24 17:58:53 +0000587 db->aVTrans[db->nVTrans++] = pVTab;
588 sqlite3VtabLock(pVTab);
danielk1977e7ff4032006-06-17 11:30:32 +0000589 return SQLITE_OK;
590}
591
592/*
danielk19779da9d472006-06-14 06:58:15 +0000593** This function is invoked by the vdbe to call the xCreate method
594** of the virtual table named zTab in database iDb.
595**
596** If an error occurs, *pzErr is set to point an an English language
597** description of the error and an SQLITE_XXX error code is returned.
drh633e6d52008-07-28 19:34:53 +0000598** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
danielk19779da9d472006-06-14 06:58:15 +0000599*/
600int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
601 int rc = SQLITE_OK;
602 Table *pTab;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000603 Module *pMod;
danielk1977595a5232009-07-24 17:58:53 +0000604 const char *zMod;
danielk19779da9d472006-06-14 06:58:15 +0000605
606 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk1977595a5232009-07-24 17:58:53 +0000607 assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
608
609 /* Locate the required virtual table module */
610 zMod = pTab->azModuleArg[0];
611 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
danielk19779da9d472006-06-14 06:58:15 +0000612
613 /* If the module has been registered and includes a Create method,
614 ** invoke it now. If the module has not been registered, return an
615 ** error. Otherwise, do nothing.
616 */
danielk1977d1ab1ba2006-06-15 04:28:13 +0000617 if( !pMod ){
danielk1977595a5232009-07-24 17:58:53 +0000618 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
danielk19779da9d472006-06-14 06:58:15 +0000619 rc = SQLITE_ERROR;
620 }else{
danielk1977d1ab1ba2006-06-15 04:28:13 +0000621 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
danielk19779da9d472006-06-14 06:58:15 +0000622 }
623
drh748ce212009-05-20 20:10:47 +0000624 /* Justification of ALWAYS(): The xConstructor method is required to
625 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
danielk1977595a5232009-07-24 17:58:53 +0000626 if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
627 rc = addToVTrans(db, sqlite3GetVTable(db, pTab));
danielk1977e7ff4032006-06-17 11:30:32 +0000628 }
629
danielk19779da9d472006-06-14 06:58:15 +0000630 return rc;
631}
danielk1977be8a7832006-06-13 15:00:54 +0000632
633/*
danielk1977fe3fcbe22006-06-12 12:08:45 +0000634** This function is used to set the schema of a virtual table. It is only
635** valid to call this function from within the xCreate() or xConnect() of a
636** virtual table module.
637*/
danielk19777e6ebfb2006-06-12 11:24:37 +0000638int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
drhe98c9042009-06-02 21:31:38 +0000639 Parse *pParse;
danielk19777e6ebfb2006-06-12 11:24:37 +0000640
641 int rc = SQLITE_OK;
drh27641702007-08-22 02:56:42 +0000642 Table *pTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000643 char *zErr = 0;
644
drh27641702007-08-22 02:56:42 +0000645 sqlite3_mutex_enter(db->mutex);
646 pTab = db->pVTab;
danielk19777e6ebfb2006-06-12 11:24:37 +0000647 if( !pTab ){
648 sqlite3Error(db, SQLITE_MISUSE, 0);
drh27641702007-08-22 02:56:42 +0000649 sqlite3_mutex_leave(db->mutex);
drh413c3d32010-02-23 20:11:56 +0000650 return SQLITE_MISUSE_BKPT;
danielk19777e6ebfb2006-06-12 11:24:37 +0000651 }
danielk1977595a5232009-07-24 17:58:53 +0000652 assert( (pTab->tabFlags & TF_Virtual)!=0 );
danielk19777e6ebfb2006-06-12 11:24:37 +0000653
drhe98c9042009-06-02 21:31:38 +0000654 pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
655 if( pParse==0 ){
656 rc = SQLITE_NOMEM;
657 }else{
658 pParse->declareVtab = 1;
659 pParse->db = db;
drh8b307fb2010-04-06 15:57:05 +0000660 pParse->nQueryLoop = 1;
drhe98c9042009-06-02 21:31:38 +0000661
dan84db21e2009-12-08 19:05:53 +0000662 if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
dan84db21e2009-12-08 19:05:53 +0000663 && pParse->pNewTable
drh0e3c5122009-12-08 22:16:15 +0000664 && !db->mallocFailed
dan84db21e2009-12-08 19:05:53 +0000665 && !pParse->pNewTable->pSelect
666 && (pParse->pNewTable->tabFlags & TF_Virtual)==0
drhe98c9042009-06-02 21:31:38 +0000667 ){
danielk1977595a5232009-07-24 17:58:53 +0000668 if( !pTab->aCol ){
669 pTab->aCol = pParse->pNewTable->aCol;
670 pTab->nCol = pParse->pNewTable->nCol;
671 pParse->pNewTable->nCol = 0;
672 pParse->pNewTable->aCol = 0;
673 }
drhe98c9042009-06-02 21:31:38 +0000674 db->pVTab = 0;
dan84db21e2009-12-08 19:05:53 +0000675 }else{
drhe98c9042009-06-02 21:31:38 +0000676 sqlite3Error(db, SQLITE_ERROR, zErr);
677 sqlite3DbFree(db, zErr);
678 rc = SQLITE_ERROR;
679 }
680 pParse->declareVtab = 0;
681
682 if( pParse->pVdbe ){
683 sqlite3VdbeFinalize(pParse->pVdbe);
684 }
685 sqlite3DeleteTable(pParse->pNewTable);
686 sqlite3StackFree(db, pParse);
danielk19777e6ebfb2006-06-12 11:24:37 +0000687 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000688
drh4ac285a2006-09-15 07:28:50 +0000689 assert( (rc&0xff)==rc );
drh27641702007-08-22 02:56:42 +0000690 rc = sqlite3ApiExit(db, rc);
691 sqlite3_mutex_leave(db->mutex);
692 return rc;
danielk19777e6ebfb2006-06-12 11:24:37 +0000693}
694
695/*
danielk19779e39ce82006-06-12 16:01:21 +0000696** This function is invoked by the vdbe to call the xDestroy method
697** of the virtual table named zTab in database iDb. This occurs
698** when a DROP TABLE is mentioned.
699**
700** This call is a no-op if zTab is not a virtual table.
701*/
drh748ce212009-05-20 20:10:47 +0000702int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
danielk19779e39ce82006-06-12 16:01:21 +0000703 int rc = SQLITE_OK;
704 Table *pTab;
danielk19779e39ce82006-06-12 16:01:21 +0000705
706 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
danielk1977595a5232009-07-24 17:58:53 +0000707 if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
708 VTable *p = vtabDisconnectAll(db, pTab);
709
danielk19779e39ce82006-06-12 16:01:21 +0000710 assert( rc==SQLITE_OK );
danielk1977595a5232009-07-24 17:58:53 +0000711 rc = p->pMod->pModule->xDestroy(p->pVtab);
danielk1977595a5232009-07-24 17:58:53 +0000712
713 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
danielk19779e39ce82006-06-12 16:01:21 +0000714 if( rc==SQLITE_OK ){
danielk1977595a5232009-07-24 17:58:53 +0000715 assert( pTab->pVTable==p && p->pNext==0 );
716 p->pVtab = 0;
717 pTab->pVTable = 0;
718 sqlite3VtabUnlock(p);
danielk19779e39ce82006-06-12 16:01:21 +0000719 }
720 }
721
722 return rc;
723}
724
danielk1977f9e7dda2006-06-16 16:08:53 +0000725/*
danielk1977e7ff4032006-06-17 11:30:32 +0000726** This function invokes either the xRollback or xCommit method
727** of each of the virtual tables in the sqlite3.aVTrans array. The method
728** called is identified by the second argument, "offset", which is
729** the offset of the method to call in the sqlite3_module structure.
730**
731** The array is cleared after invoking the callbacks.
732*/
drh7209c692008-04-27 18:40:11 +0000733static void callFinaliser(sqlite3 *db, int offset){
danielk1977e7ff4032006-06-17 11:30:32 +0000734 int i;
danielk19770b83fa82007-04-19 14:48:37 +0000735 if( db->aVTrans ){
drh748ce212009-05-20 20:10:47 +0000736 for(i=0; i<db->nVTrans; i++){
danielk1977595a5232009-07-24 17:58:53 +0000737 VTable *pVTab = db->aVTrans[i];
738 sqlite3_vtab *p = pVTab->pVtab;
739 if( p ){
740 int (*x)(sqlite3_vtab *);
741 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
742 if( x ) x(p);
743 }
744 sqlite3VtabUnlock(pVTab);
danielk19770b83fa82007-04-19 14:48:37 +0000745 }
drh633e6d52008-07-28 19:34:53 +0000746 sqlite3DbFree(db, db->aVTrans);
danielk19770b83fa82007-04-19 14:48:37 +0000747 db->nVTrans = 0;
748 db->aVTrans = 0;
danielk1977e7ff4032006-06-17 11:30:32 +0000749 }
danielk1977e7ff4032006-06-17 11:30:32 +0000750}
751
752/*
danielk19773e3a84d2008-08-01 17:37:40 +0000753** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
754** array. Return the error code for the first error that occurs, or
755** SQLITE_OK if all xSync operations are successful.
756**
757** Set *pzErrmsg to point to a buffer that should be released using
758** sqlite3DbFree() containing an error message, if one is available.
danielk1977f9e7dda2006-06-16 16:08:53 +0000759*/
danielk19773e3a84d2008-08-01 17:37:40 +0000760int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
danielk1977e7ff4032006-06-17 11:30:32 +0000761 int i;
762 int rc = SQLITE_OK;
danielk1977595a5232009-07-24 17:58:53 +0000763 VTable **aVTrans = db->aVTrans;
danielk197720b1eaf2006-07-26 16:22:14 +0000764
danielk197720b1eaf2006-07-26 16:22:14 +0000765 db->aVTrans = 0;
drh748ce212009-05-20 20:10:47 +0000766 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
danielk1977e7ff4032006-06-17 11:30:32 +0000767 int (*x)(sqlite3_vtab *);
danielk1977595a5232009-07-24 17:58:53 +0000768 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
769 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
danielk1977e7ff4032006-06-17 11:30:32 +0000770 rc = x(pVtab);
danielk19773e3a84d2008-08-01 17:37:40 +0000771 sqlite3DbFree(db, *pzErrmsg);
772 *pzErrmsg = pVtab->zErrMsg;
773 pVtab->zErrMsg = 0;
danielk1977e7ff4032006-06-17 11:30:32 +0000774 }
775 }
danielk197720b1eaf2006-07-26 16:22:14 +0000776 db->aVTrans = aVTrans;
danielk1977e7ff4032006-06-17 11:30:32 +0000777 return rc;
danielk1977f9e7dda2006-06-16 16:08:53 +0000778}
779
780/*
781** Invoke the xRollback method of all virtual tables in the
782** sqlite3.aVTrans array. Then clear the array itself.
783*/
784int sqlite3VtabRollback(sqlite3 *db){
drh7209c692008-04-27 18:40:11 +0000785 callFinaliser(db, offsetof(sqlite3_module,xRollback));
danielk1977e7ff4032006-06-17 11:30:32 +0000786 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000787}
788
789/*
790** Invoke the xCommit method of all virtual tables in the
791** sqlite3.aVTrans array. Then clear the array itself.
792*/
793int sqlite3VtabCommit(sqlite3 *db){
drh7209c692008-04-27 18:40:11 +0000794 callFinaliser(db, offsetof(sqlite3_module,xCommit));
danielk1977e7ff4032006-06-17 11:30:32 +0000795 return SQLITE_OK;
danielk1977f9e7dda2006-06-16 16:08:53 +0000796}
797
798/*
799** If the virtual table pVtab supports the transaction interface
800** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
801** not currently open, invoke the xBegin method now.
802**
803** If the xBegin call is successful, place the sqlite3_vtab pointer
804** in the sqlite3.aVTrans array.
805*/
danielk1977595a5232009-07-24 17:58:53 +0000806int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
danielk1977f9e7dda2006-06-16 16:08:53 +0000807 int rc = SQLITE_OK;
danielk197720b1eaf2006-07-26 16:22:14 +0000808 const sqlite3_module *pModule;
809
810 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
811 ** than zero, then this function is being called from within a
812 ** virtual module xSync() callback. It is illegal to write to
drh748ce212009-05-20 20:10:47 +0000813 ** virtual module tables in this case, so return SQLITE_LOCKED.
danielk197720b1eaf2006-07-26 16:22:14 +0000814 */
danielk1977093e0f62008-11-13 18:00:14 +0000815 if( sqlite3VtabInSync(db) ){
danielk197720b1eaf2006-07-26 16:22:14 +0000816 return SQLITE_LOCKED;
817 }
danielk1977595a5232009-07-24 17:58:53 +0000818 if( !pVTab ){
danielk197720b1eaf2006-07-26 16:22:14 +0000819 return SQLITE_OK;
820 }
danielk1977595a5232009-07-24 17:58:53 +0000821 pModule = pVTab->pVtab->pModule;
danielk197720b1eaf2006-07-26 16:22:14 +0000822
danielk1977f9e7dda2006-06-16 16:08:53 +0000823 if( pModule->xBegin ){
824 int i;
825
danielk197720b1eaf2006-07-26 16:22:14 +0000826
danielk1977f9e7dda2006-06-16 16:08:53 +0000827 /* If pVtab is already in the aVTrans array, return early */
drh748ce212009-05-20 20:10:47 +0000828 for(i=0; i<db->nVTrans; i++){
danielk1977595a5232009-07-24 17:58:53 +0000829 if( db->aVTrans[i]==pVTab ){
danielk1977f9e7dda2006-06-16 16:08:53 +0000830 return SQLITE_OK;
831 }
832 }
833
834 /* Invoke the xBegin method */
danielk1977595a5232009-07-24 17:58:53 +0000835 rc = pModule->xBegin(pVTab->pVtab);
danielk19773e3a84d2008-08-01 17:37:40 +0000836 if( rc==SQLITE_OK ){
danielk1977595a5232009-07-24 17:58:53 +0000837 rc = addToVTrans(db, pVTab);
danielk1977f9e7dda2006-06-16 16:08:53 +0000838 }
danielk1977f9e7dda2006-06-16 16:08:53 +0000839 }
840 return rc;
841}
842
drhb7f6f682006-07-08 17:06:43 +0000843/*
844** The first parameter (pDef) is a function implementation. The
845** second parameter (pExpr) is the first argument to this function.
846** If pExpr is a column in a virtual table, then let the virtual
847** table implementation have an opportunity to overload the function.
848**
849** This routine is used to allow virtual table implementations to
850** overload MATCH, LIKE, GLOB, and REGEXP operators.
851**
852** Return either the pDef argument (indicating no change) or a
853** new FuncDef structure that is marked as ephemeral using the
854** SQLITE_FUNC_EPHEM flag.
855*/
856FuncDef *sqlite3VtabOverloadFunction(
drh17435752007-08-16 04:30:38 +0000857 sqlite3 *db, /* Database connection for reporting malloc problems */
drhb7f6f682006-07-08 17:06:43 +0000858 FuncDef *pDef, /* Function to possibly overload */
859 int nArg, /* Number of arguments to the function */
860 Expr *pExpr /* First argument to the function */
861){
862 Table *pTab;
863 sqlite3_vtab *pVtab;
864 sqlite3_module *pMod;
drhdc5ea5c2008-12-10 17:19:59 +0000865 void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
866 void *pArg = 0;
drhb7f6f682006-07-08 17:06:43 +0000867 FuncDef *pNew;
drh777b17a2007-09-20 10:02:54 +0000868 int rc = 0;
drha70034d2006-09-18 20:24:02 +0000869 char *zLowerName;
870 unsigned char *z;
871
drhb7f6f682006-07-08 17:06:43 +0000872
873 /* Check to see the left operand is a column in a virtual table */
drh748ce212009-05-20 20:10:47 +0000874 if( NEVER(pExpr==0) ) return pDef;
drhb7f6f682006-07-08 17:06:43 +0000875 if( pExpr->op!=TK_COLUMN ) return pDef;
876 pTab = pExpr->pTab;
drh748ce212009-05-20 20:10:47 +0000877 if( NEVER(pTab==0) ) return pDef;
drh7d10d5a2008-08-20 16:35:10 +0000878 if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
danielk1977595a5232009-07-24 17:58:53 +0000879 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
drhb7f6f682006-07-08 17:06:43 +0000880 assert( pVtab!=0 );
881 assert( pVtab->pModule!=0 );
danielk19775bd270b2006-07-25 15:14:52 +0000882 pMod = (sqlite3_module *)pVtab->pModule;
drhb7f6f682006-07-08 17:06:43 +0000883 if( pMod->xFindFunction==0 ) return pDef;
884
rseb72e88d2007-09-20 11:32:18 +0000885 /* Call the xFindFunction method on the virtual table implementation
drha70034d2006-09-18 20:24:02 +0000886 ** to see if the implementation wants to overload this function
887 */
drh17435752007-08-16 04:30:38 +0000888 zLowerName = sqlite3DbStrDup(db, pDef->zName);
889 if( zLowerName ){
890 for(z=(unsigned char*)zLowerName; *z; z++){
891 *z = sqlite3UpperToLower[*z];
892 }
893 rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
drh633e6d52008-07-28 19:34:53 +0000894 sqlite3DbFree(db, zLowerName);
drha70034d2006-09-18 20:24:02 +0000895 }
drha70034d2006-09-18 20:24:02 +0000896 if( rc==0 ){
drhb7f6f682006-07-08 17:06:43 +0000897 return pDef;
898 }
899
900 /* Create a new ephemeral function definition for the overloaded
901 ** function */
drhea678832008-12-10 19:26:22 +0000902 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
danielk1977e25a50b2009-07-01 18:04:20 +0000903 + sqlite3Strlen30(pDef->zName) + 1);
drhb7f6f682006-07-08 17:06:43 +0000904 if( pNew==0 ){
905 return pDef;
906 }
907 *pNew = *pDef;
danielk19778c0a7912008-08-20 14:49:23 +0000908 pNew->zName = (char *)&pNew[1];
drhea678832008-12-10 19:26:22 +0000909 memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
drhb7f6f682006-07-08 17:06:43 +0000910 pNew->xFunc = xFunc;
911 pNew->pUserData = pArg;
drhb7f6f682006-07-08 17:06:43 +0000912 pNew->flags |= SQLITE_FUNC_EPHEM;
913 return pNew;
914}
915
drh4f3dd152008-04-28 18:46:43 +0000916/*
917** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
918** array so that an OP_VBegin will get generated for it. Add pTab to the
919** array if it is missing. If pTab is already in the array, this routine
920** is a no-op.
921*/
922void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
dan65a7cd12009-09-01 12:16:01 +0000923 Parse *pToplevel = sqlite3ParseToplevel(pParse);
drh4f3dd152008-04-28 18:46:43 +0000924 int i, n;
drh748ce212009-05-20 20:10:47 +0000925 Table **apVtabLock;
926
drh4f3dd152008-04-28 18:46:43 +0000927 assert( IsVirtual(pTab) );
dan65a7cd12009-09-01 12:16:01 +0000928 for(i=0; i<pToplevel->nVtabLock; i++){
929 if( pTab==pToplevel->apVtabLock[i] ) return;
drh4f3dd152008-04-28 18:46:43 +0000930 }
dan65a7cd12009-09-01 12:16:01 +0000931 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
932 apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
drh748ce212009-05-20 20:10:47 +0000933 if( apVtabLock ){
dan65a7cd12009-09-01 12:16:01 +0000934 pToplevel->apVtabLock = apVtabLock;
935 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
drh344c38e2008-05-05 13:23:04 +0000936 }else{
dan65a7cd12009-09-01 12:16:01 +0000937 pToplevel->db->mallocFailed = 1;
drh4f3dd152008-04-28 18:46:43 +0000938 }
939}
940
drhb9bb7c12006-06-11 23:41:55 +0000941#endif /* SQLITE_OMIT_VIRTUALTABLE */