xref: /sqlite-3.40.0/src/vtab.c (revision 6695f47e)
1 /*
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 */
14 #ifndef SQLITE_OMIT_VIRTUALTABLE
15 #include "sqliteInt.h"
16 
17 /*
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 */
22 static 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 */
28 ){
29   int rc, nName;
30   Module *pMod;
31 
32   sqlite3_mutex_enter(db->mutex);
33   nName = sqlite3Strlen30(zName);
34   pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
35   if( pMod ){
36     Module *pDel;
37     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;
43     pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
44     if( pDel && pDel->xDestroy ){
45       pDel->xDestroy(pDel->pAux);
46     }
47     sqlite3DbFree(db, pDel);
48     if( pDel==pMod ){
49       db->mallocFailed = 1;
50     }
51     sqlite3ResetInternalSchema(db, 0);
52   }else if( xDestroy ){
53     xDestroy(pAux);
54   }
55   rc = sqlite3ApiExit(db, SQLITE_OK);
56   sqlite3_mutex_leave(db->mutex);
57   return rc;
58 }
59 
60 
61 /*
62 ** External API function used to create a new virtual-table module.
63 */
64 int sqlite3_create_module(
65   sqlite3 *db,                    /* Database in which module is registered */
66   const char *zName,              /* Name assigned to this module */
67   const sqlite3_module *pModule,  /* The definition of the module */
68   void *pAux                      /* Context pointer for xCreate/xConnect */
69 ){
70   return createModule(db, zName, pModule, pAux, 0);
71 }
72 
73 /*
74 ** External API function used to create a new virtual-table module.
75 */
76 int 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);
84 }
85 
86 /*
87 ** 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 */
94 void 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 */
104 VTable *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;
109 }
110 
111 /*
112 ** Decrement the ref-count on a virtual table object. When the ref-count
113 ** reaches zero, call the xDisconnect() method to delete the object.
114 */
115 void sqlite3VtabUnlock(VTable *pVTab){
116   sqlite3 *db = pVTab->db;
117 
118   assert( db );
119   assert( pVTab->nRef>0 );
120   assert( sqlite3SafetyCheckOk(db) );
121 
122   pVTab->nRef--;
123   if( pVTab->nRef==0 ){
124     sqlite3_vtab *p = pVTab->pVtab;
125     if( p ){
126 #ifdef SQLITE_DEBUG
127       if( pVTab->db->magic==SQLITE_MAGIC_BUSY ){
128         (void)sqlite3SafetyOff(db);
129         p->pModule->xDisconnect(p);
130         (void)sqlite3SafetyOn(db);
131       } else
132 #endif
133       {
134         p->pModule->xDisconnect(p);
135       }
136     }
137     sqlite3DbFree(db, pVTab);
138   }
139 }
140 
141 /*
142 ** Table p is a virtual table. This function moves all elements in the
143 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
144 ** database connections to be disconnected at the next opportunity.
145 ** Except, if argument db is not NULL, then the entry associated with
146 ** connection db is left in the p->pVTable list.
147 */
148 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
149   VTable *pRet = 0;
150   VTable *pVTable = p->pVTable;
151   p->pVTable = 0;
152 
153   /* Assert that the mutex (if any) associated with the BtShared database
154   ** that contains table p is held by the caller. See header comments
155   ** above function sqlite3VtabUnlockList() for an explanation of why
156   ** this makes it safe to access the sqlite3.pDisconnect list of any
157   ** database connection that may have an entry in the p->pVTable list.  */
158   assert( db==0 ||
159     sqlite3BtreeHoldsMutex(db->aDb[sqlite3SchemaToIndex(db, p->pSchema)].pBt)
160   );
161 
162   while( pVTable ){
163     sqlite3 *db2 = pVTable->db;
164     VTable *pNext = pVTable->pNext;
165     assert( db2 );
166     if( db2==db ){
167       pRet = pVTable;
168       p->pVTable = pRet;
169       pRet->pNext = 0;
170     }else{
171       pVTable->pNext = db2->pDisconnect;
172       db2->pDisconnect = pVTable;
173     }
174     pVTable = pNext;
175   }
176 
177   assert( !db || pRet );
178   return pRet;
179 }
180 
181 
182 /*
183 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
184 **
185 ** This function may only be called when the mutexes associated with all
186 ** shared b-tree databases opened using connection db are held by the
187 ** caller. This is done to protect the sqlite3.pDisconnect list. The
188 ** sqlite3.pDisconnect list is accessed only as follows:
189 **
190 **   1) By this function. In this case, all BtShared mutexes and the mutex
191 **      associated with the database handle itself must be held.
192 **
193 **   2) By function vtabDisconnectAll(), when it adds a VTable entry to
194 **      the sqlite3.pDisconnect list. In this case either the BtShared mutex
195 **      associated with the database the virtual table is stored in is held
196 **      or, if the virtual table is stored in a non-sharable database, then
197 **      the database handle mutex is held.
198 **
199 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
200 ** by multiple threads. It is thread-safe.
201 */
202 void sqlite3VtabUnlockList(sqlite3 *db){
203   VTable *p = db->pDisconnect;
204   db->pDisconnect = 0;
205 
206   assert( sqlite3BtreeHoldsAllMutexes(db) );
207   assert( sqlite3_mutex_held(db->mutex) );
208 
209   if( p ){
210     sqlite3ExpirePreparedStatements(db);
211     do {
212       VTable *pNext = p->pNext;
213       sqlite3VtabUnlock(p);
214       p = pNext;
215     }while( p );
216   }
217 }
218 
219 /*
220 ** Clear any and all virtual-table information from the Table record.
221 ** This routine is called, for example, just before deleting the Table
222 ** record.
223 **
224 ** Since it is a virtual-table, the Table structure contains a pointer
225 ** to the head of a linked list of VTable structures. Each VTable
226 ** structure is associated with a single sqlite3* user of the schema.
227 ** The reference count of the VTable structure associated with database
228 ** connection db is decremented immediately (which may lead to the
229 ** structure being xDisconnected and free). Any other VTable structures
230 ** in the list are moved to the sqlite3.pDisconnect list of the associated
231 ** database connection.
232 */
233 void sqlite3VtabClear(Table *p){
234   vtabDisconnectAll(0, p);
235   if( p->azModuleArg ){
236     int i;
237     for(i=0; i<p->nModuleArg; i++){
238       sqlite3DbFree(p->dbMem, p->azModuleArg[i]);
239     }
240     sqlite3DbFree(p->dbMem, p->azModuleArg);
241   }
242 }
243 
244 /*
245 ** Add a new module argument to pTable->azModuleArg[].
246 ** The string is not copied - the pointer is stored.  The
247 ** string will be freed automatically when the table is
248 ** deleted.
249 */
250 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
251   int i = pTable->nModuleArg++;
252   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
253   char **azModuleArg;
254   azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
255   if( azModuleArg==0 ){
256     int j;
257     for(j=0; j<i; j++){
258       sqlite3DbFree(db, pTable->azModuleArg[j]);
259     }
260     sqlite3DbFree(db, zArg);
261     sqlite3DbFree(db, pTable->azModuleArg);
262     pTable->nModuleArg = 0;
263   }else{
264     azModuleArg[i] = zArg;
265     azModuleArg[i+1] = 0;
266   }
267   pTable->azModuleArg = azModuleArg;
268 }
269 
270 /*
271 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
272 ** statement.  The module name has been parsed, but the optional list
273 ** of parameters that follow the module name are still pending.
274 */
275 void sqlite3VtabBeginParse(
276   Parse *pParse,        /* Parsing context */
277   Token *pName1,        /* Name of new table, or database name */
278   Token *pName2,        /* Name of new table or NULL */
279   Token *pModuleName    /* Name of the module for the virtual table */
280 ){
281   int iDb;              /* The database the table is being created in */
282   Table *pTable;        /* The new virtual table */
283   sqlite3 *db;          /* Database connection */
284 
285   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
286   pTable = pParse->pNewTable;
287   if( pTable==0 ) return;
288   assert( 0==pTable->pIndex );
289 
290   db = pParse->db;
291   iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
292   assert( iDb>=0 );
293 
294   pTable->tabFlags |= TF_Virtual;
295   pTable->nModuleArg = 0;
296   addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
297   addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
298   addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
299   pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
300 
301 #ifndef SQLITE_OMIT_AUTHORIZATION
302   /* Creating a virtual table invokes the authorization callback twice.
303   ** The first invocation, to obtain permission to INSERT a row into the
304   ** sqlite_master table, has already been made by sqlite3StartTable().
305   ** The second call, to obtain permission to create the table, is made now.
306   */
307   if( pTable->azModuleArg ){
308     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
309             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
310   }
311 #endif
312 }
313 
314 /*
315 ** This routine takes the module argument that has been accumulating
316 ** in pParse->zArg[] and appends it to the list of arguments on the
317 ** virtual table currently under construction in pParse->pTable.
318 */
319 static void addArgumentToVtab(Parse *pParse){
320   if( pParse->sArg.z && ALWAYS(pParse->pNewTable) ){
321     const char *z = (const char*)pParse->sArg.z;
322     int n = pParse->sArg.n;
323     sqlite3 *db = pParse->db;
324     addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
325   }
326 }
327 
328 /*
329 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
330 ** has been completely parsed.
331 */
332 void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
333   Table *pTab = pParse->pNewTable;  /* The table being constructed */
334   sqlite3 *db = pParse->db;         /* The database connection */
335 
336   if( pTab==0 ) return;
337   addArgumentToVtab(pParse);
338   pParse->sArg.z = 0;
339   if( pTab->nModuleArg<1 ) return;
340 
341   /* If the CREATE VIRTUAL TABLE statement is being entered for the
342   ** first time (in other words if the virtual table is actually being
343   ** created now instead of just being read out of sqlite_master) then
344   ** do additional initialization work and store the statement text
345   ** in the sqlite_master table.
346   */
347   if( !db->init.busy ){
348     char *zStmt;
349     char *zWhere;
350     int iDb;
351     Vdbe *v;
352 
353     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
354     if( pEnd ){
355       pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
356     }
357     zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
358 
359     /* A slot for the record has already been allocated in the
360     ** SQLITE_MASTER table.  We just need to update that slot with all
361     ** the information we've collected.
362     **
363     ** The VM register number pParse->regRowid holds the rowid of an
364     ** entry in the sqlite_master table tht was created for this vtab
365     ** by sqlite3StartTable().
366     */
367     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
368     sqlite3NestedParse(pParse,
369       "UPDATE %Q.%s "
370          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
371        "WHERE rowid=#%d",
372       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
373       pTab->zName,
374       pTab->zName,
375       zStmt,
376       pParse->regRowid
377     );
378     sqlite3DbFree(db, zStmt);
379     v = sqlite3GetVdbe(pParse);
380     sqlite3ChangeCookie(pParse, iDb);
381 
382     sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
383     zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName);
384     sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC);
385     sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
386                          pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
387   }
388 
389   /* If we are rereading the sqlite_master table create the in-memory
390   ** record of the table. The xConnect() method is not called until
391   ** the first time the virtual table is used in an SQL statement. This
392   ** allows a schema that contains virtual tables to be loaded before
393   ** the required virtual table implementations are registered.  */
394   else {
395     Table *pOld;
396     Schema *pSchema = pTab->pSchema;
397     const char *zName = pTab->zName;
398     int nName = sqlite3Strlen30(zName);
399     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
400     if( pOld ){
401       db->mallocFailed = 1;
402       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
403       return;
404     }
405     pSchema->db = pParse->db;
406     pParse->pNewTable = 0;
407   }
408 }
409 
410 /*
411 ** The parser calls this routine when it sees the first token
412 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
413 */
414 void sqlite3VtabArgInit(Parse *pParse){
415   addArgumentToVtab(pParse);
416   pParse->sArg.z = 0;
417   pParse->sArg.n = 0;
418 }
419 
420 /*
421 ** The parser calls this routine for each token after the first token
422 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
423 */
424 void sqlite3VtabArgExtend(Parse *pParse, Token *p){
425   Token *pArg = &pParse->sArg;
426   if( pArg->z==0 ){
427     pArg->z = p->z;
428     pArg->n = p->n;
429   }else{
430     assert(pArg->z < p->z);
431     pArg->n = (int)(&p->z[p->n] - pArg->z);
432   }
433 }
434 
435 /*
436 ** Invoke a virtual table constructor (either xCreate or xConnect). The
437 ** pointer to the function to invoke is passed as the fourth parameter
438 ** to this procedure.
439 */
440 static int vtabCallConstructor(
441   sqlite3 *db,
442   Table *pTab,
443   Module *pMod,
444   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
445   char **pzErr
446 ){
447   VTable *pVTable;
448   int rc;
449   const char *const*azArg = (const char *const*)pTab->azModuleArg;
450   int nArg = pTab->nModuleArg;
451   char *zErr = 0;
452   char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
453 
454   if( !zModuleName ){
455     return SQLITE_NOMEM;
456   }
457 
458   pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
459   if( !pVTable ){
460     sqlite3DbFree(db, zModuleName);
461     return SQLITE_NOMEM;
462   }
463   pVTable->db = db;
464   pVTable->pMod = pMod;
465 
466   assert( !db->pVTab );
467   assert( xConstruct );
468   db->pVTab = pTab;
469 
470   /* Invoke the virtual table constructor */
471   (void)sqlite3SafetyOff(db);
472   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
473   (void)sqlite3SafetyOn(db);
474   if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
475 
476   if( SQLITE_OK!=rc ){
477     if( zErr==0 ){
478       *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
479     }else {
480       *pzErr = sqlite3MPrintf(db, "%s", zErr);
481       sqlite3DbFree(db, zErr);
482     }
483     sqlite3DbFree(db, pVTable);
484   }else if( ALWAYS(pVTable->pVtab) ){
485     /* Justification of ALWAYS():  A correct vtab constructor must allocate
486     ** the sqlite3_vtab object if successful.  */
487     pVTable->pVtab->pModule = pMod->pModule;
488     pVTable->nRef = 1;
489     if( db->pVTab ){
490       const char *zFormat = "vtable constructor did not declare schema: %s";
491       *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
492       sqlite3VtabUnlock(pVTable);
493       rc = SQLITE_ERROR;
494     }else{
495       int iCol;
496       /* If everything went according to plan, link the new VTable structure
497       ** into the linked list headed by pTab->pVTable. Then loop through the
498       ** columns of the table to see if any of them contain the token "hidden".
499       ** If so, set the Column.isHidden flag and remove the token from
500       ** the type string.  */
501       pVTable->pNext = pTab->pVTable;
502       pTab->pVTable = pVTable;
503 
504       for(iCol=0; iCol<pTab->nCol; iCol++){
505         char *zType = pTab->aCol[iCol].zType;
506         int nType;
507         int i = 0;
508         if( !zType ) continue;
509         nType = sqlite3Strlen30(zType);
510         if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
511           for(i=0; i<nType; i++){
512             if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
513              && (zType[i+7]=='\0' || zType[i+7]==' ')
514             ){
515               i++;
516               break;
517             }
518           }
519         }
520         if( i<nType ){
521           int j;
522           int nDel = 6 + (zType[i+6] ? 1 : 0);
523           for(j=i; (j+nDel)<=nType; j++){
524             zType[j] = zType[j+nDel];
525           }
526           if( zType[i]=='\0' && i>0 ){
527             assert(zType[i-1]==' ');
528             zType[i-1] = '\0';
529           }
530           pTab->aCol[iCol].isHidden = 1;
531         }
532       }
533     }
534   }
535 
536   sqlite3DbFree(db, zModuleName);
537   db->pVTab = 0;
538   return rc;
539 }
540 
541 /*
542 ** This function is invoked by the parser to call the xConnect() method
543 ** of the virtual table pTab. If an error occurs, an error code is returned
544 ** and an error left in pParse.
545 **
546 ** This call is a no-op if table pTab is not a virtual table.
547 */
548 int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
549   sqlite3 *db = pParse->db;
550   const char *zMod;
551   Module *pMod;
552   int rc;
553 
554   assert( pTab );
555   if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
556     return SQLITE_OK;
557   }
558 
559   /* Locate the required virtual table module */
560   zMod = pTab->azModuleArg[0];
561   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
562 
563   if( !pMod ){
564     const char *zModule = pTab->azModuleArg[0];
565     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
566     rc = SQLITE_ERROR;
567   }else{
568     char *zErr = 0;
569     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
570     if( rc!=SQLITE_OK ){
571       sqlite3ErrorMsg(pParse, "%s", zErr);
572     }
573     sqlite3DbFree(db, zErr);
574   }
575 
576   return rc;
577 }
578 
579 /*
580 ** Add the virtual table pVTab to the array sqlite3.aVTrans[].
581 */
582 static int addToVTrans(sqlite3 *db, VTable *pVTab){
583   const int ARRAY_INCR = 5;
584 
585   /* Grow the sqlite3.aVTrans array if required */
586   if( (db->nVTrans%ARRAY_INCR)==0 ){
587     VTable **aVTrans;
588     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
589     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
590     if( !aVTrans ){
591       return SQLITE_NOMEM;
592     }
593     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
594     db->aVTrans = aVTrans;
595   }
596 
597   /* Add pVtab to the end of sqlite3.aVTrans */
598   db->aVTrans[db->nVTrans++] = pVTab;
599   sqlite3VtabLock(pVTab);
600   return SQLITE_OK;
601 }
602 
603 /*
604 ** This function is invoked by the vdbe to call the xCreate method
605 ** of the virtual table named zTab in database iDb.
606 **
607 ** If an error occurs, *pzErr is set to point an an English language
608 ** description of the error and an SQLITE_XXX error code is returned.
609 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
610 */
611 int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
612   int rc = SQLITE_OK;
613   Table *pTab;
614   Module *pMod;
615   const char *zMod;
616 
617   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
618   assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
619 
620   /* Locate the required virtual table module */
621   zMod = pTab->azModuleArg[0];
622   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
623 
624   /* If the module has been registered and includes a Create method,
625   ** invoke it now. If the module has not been registered, return an
626   ** error. Otherwise, do nothing.
627   */
628   if( !pMod ){
629     *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
630     rc = SQLITE_ERROR;
631   }else{
632     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
633   }
634 
635   /* Justification of ALWAYS():  The xConstructor method is required to
636   ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
637   if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
638       rc = addToVTrans(db, sqlite3GetVTable(db, pTab));
639   }
640 
641   return rc;
642 }
643 
644 /*
645 ** This function is used to set the schema of a virtual table.  It is only
646 ** valid to call this function from within the xCreate() or xConnect() of a
647 ** virtual table module.
648 */
649 int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
650   Parse *pParse;
651 
652   int rc = SQLITE_OK;
653   Table *pTab;
654   char *zErr = 0;
655 
656   sqlite3_mutex_enter(db->mutex);
657   pTab = db->pVTab;
658   if( !pTab ){
659     sqlite3Error(db, SQLITE_MISUSE, 0);
660     sqlite3_mutex_leave(db->mutex);
661     return SQLITE_MISUSE;
662   }
663   assert( (pTab->tabFlags & TF_Virtual)!=0 );
664 
665   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
666   if( pParse==0 ){
667     rc = SQLITE_NOMEM;
668   }else{
669     pParse->declareVtab = 1;
670     pParse->db = db;
671 
672     if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
673      && pParse->pNewTable
674      && !db->mallocFailed
675      && !pParse->pNewTable->pSelect
676      && (pParse->pNewTable->tabFlags & TF_Virtual)==0
677     ){
678       if( !pTab->aCol ){
679         pTab->aCol = pParse->pNewTable->aCol;
680         pTab->nCol = pParse->pNewTable->nCol;
681         pParse->pNewTable->nCol = 0;
682         pParse->pNewTable->aCol = 0;
683       }
684       db->pVTab = 0;
685     }else{
686       sqlite3Error(db, SQLITE_ERROR, zErr);
687       sqlite3DbFree(db, zErr);
688       rc = SQLITE_ERROR;
689     }
690     pParse->declareVtab = 0;
691 
692     if( pParse->pVdbe ){
693       sqlite3VdbeFinalize(pParse->pVdbe);
694     }
695     sqlite3DeleteTable(pParse->pNewTable);
696     sqlite3StackFree(db, pParse);
697   }
698 
699   assert( (rc&0xff)==rc );
700   rc = sqlite3ApiExit(db, rc);
701   sqlite3_mutex_leave(db->mutex);
702   return rc;
703 }
704 
705 /*
706 ** This function is invoked by the vdbe to call the xDestroy method
707 ** of the virtual table named zTab in database iDb. This occurs
708 ** when a DROP TABLE is mentioned.
709 **
710 ** This call is a no-op if zTab is not a virtual table.
711 */
712 int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
713   int rc = SQLITE_OK;
714   Table *pTab;
715 
716   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
717   if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
718     VTable *p = vtabDisconnectAll(db, pTab);
719 
720     rc = sqlite3SafetyOff(db);
721     assert( rc==SQLITE_OK );
722     rc = p->pMod->pModule->xDestroy(p->pVtab);
723     (void)sqlite3SafetyOn(db);
724 
725     /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
726     if( rc==SQLITE_OK ){
727       assert( pTab->pVTable==p && p->pNext==0 );
728       p->pVtab = 0;
729       pTab->pVTable = 0;
730       sqlite3VtabUnlock(p);
731     }
732   }
733 
734   return rc;
735 }
736 
737 /*
738 ** This function invokes either the xRollback or xCommit method
739 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
740 ** called is identified by the second argument, "offset", which is
741 ** the offset of the method to call in the sqlite3_module structure.
742 **
743 ** The array is cleared after invoking the callbacks.
744 */
745 static void callFinaliser(sqlite3 *db, int offset){
746   int i;
747   if( db->aVTrans ){
748     for(i=0; i<db->nVTrans; i++){
749       VTable *pVTab = db->aVTrans[i];
750       sqlite3_vtab *p = pVTab->pVtab;
751       if( p ){
752         int (*x)(sqlite3_vtab *);
753         x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
754         if( x ) x(p);
755       }
756       sqlite3VtabUnlock(pVTab);
757     }
758     sqlite3DbFree(db, db->aVTrans);
759     db->nVTrans = 0;
760     db->aVTrans = 0;
761   }
762 }
763 
764 /*
765 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
766 ** array. Return the error code for the first error that occurs, or
767 ** SQLITE_OK if all xSync operations are successful.
768 **
769 ** Set *pzErrmsg to point to a buffer that should be released using
770 ** sqlite3DbFree() containing an error message, if one is available.
771 */
772 int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
773   int i;
774   int rc = SQLITE_OK;
775   int rcsafety;
776   VTable **aVTrans = db->aVTrans;
777 
778   rc = sqlite3SafetyOff(db);
779   db->aVTrans = 0;
780   for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
781     int (*x)(sqlite3_vtab *);
782     sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
783     if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
784       rc = x(pVtab);
785       sqlite3DbFree(db, *pzErrmsg);
786       *pzErrmsg = pVtab->zErrMsg;
787       pVtab->zErrMsg = 0;
788     }
789   }
790   db->aVTrans = aVTrans;
791   rcsafety = sqlite3SafetyOn(db);
792 
793   if( rc==SQLITE_OK ){
794     rc = rcsafety;
795   }
796   return rc;
797 }
798 
799 /*
800 ** Invoke the xRollback method of all virtual tables in the
801 ** sqlite3.aVTrans array. Then clear the array itself.
802 */
803 int sqlite3VtabRollback(sqlite3 *db){
804   callFinaliser(db, offsetof(sqlite3_module,xRollback));
805   return SQLITE_OK;
806 }
807 
808 /*
809 ** Invoke the xCommit method of all virtual tables in the
810 ** sqlite3.aVTrans array. Then clear the array itself.
811 */
812 int sqlite3VtabCommit(sqlite3 *db){
813   callFinaliser(db, offsetof(sqlite3_module,xCommit));
814   return SQLITE_OK;
815 }
816 
817 /*
818 ** If the virtual table pVtab supports the transaction interface
819 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
820 ** not currently open, invoke the xBegin method now.
821 **
822 ** If the xBegin call is successful, place the sqlite3_vtab pointer
823 ** in the sqlite3.aVTrans array.
824 */
825 int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
826   int rc = SQLITE_OK;
827   const sqlite3_module *pModule;
828 
829   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
830   ** than zero, then this function is being called from within a
831   ** virtual module xSync() callback. It is illegal to write to
832   ** virtual module tables in this case, so return SQLITE_LOCKED.
833   */
834   if( sqlite3VtabInSync(db) ){
835     return SQLITE_LOCKED;
836   }
837   if( !pVTab ){
838     return SQLITE_OK;
839   }
840   pModule = pVTab->pVtab->pModule;
841 
842   if( pModule->xBegin ){
843     int i;
844 
845 
846     /* If pVtab is already in the aVTrans array, return early */
847     for(i=0; i<db->nVTrans; i++){
848       if( db->aVTrans[i]==pVTab ){
849         return SQLITE_OK;
850       }
851     }
852 
853     /* Invoke the xBegin method */
854     rc = pModule->xBegin(pVTab->pVtab);
855     if( rc==SQLITE_OK ){
856       rc = addToVTrans(db, pVTab);
857     }
858   }
859   return rc;
860 }
861 
862 /*
863 ** The first parameter (pDef) is a function implementation.  The
864 ** second parameter (pExpr) is the first argument to this function.
865 ** If pExpr is a column in a virtual table, then let the virtual
866 ** table implementation have an opportunity to overload the function.
867 **
868 ** This routine is used to allow virtual table implementations to
869 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
870 **
871 ** Return either the pDef argument (indicating no change) or a
872 ** new FuncDef structure that is marked as ephemeral using the
873 ** SQLITE_FUNC_EPHEM flag.
874 */
875 FuncDef *sqlite3VtabOverloadFunction(
876   sqlite3 *db,    /* Database connection for reporting malloc problems */
877   FuncDef *pDef,  /* Function to possibly overload */
878   int nArg,       /* Number of arguments to the function */
879   Expr *pExpr     /* First argument to the function */
880 ){
881   Table *pTab;
882   sqlite3_vtab *pVtab;
883   sqlite3_module *pMod;
884   void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
885   void *pArg = 0;
886   FuncDef *pNew;
887   int rc = 0;
888   char *zLowerName;
889   unsigned char *z;
890 
891 
892   /* Check to see the left operand is a column in a virtual table */
893   if( NEVER(pExpr==0) ) return pDef;
894   if( pExpr->op!=TK_COLUMN ) return pDef;
895   pTab = pExpr->pTab;
896   if( NEVER(pTab==0) ) return pDef;
897   if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
898   pVtab = sqlite3GetVTable(db, pTab)->pVtab;
899   assert( pVtab!=0 );
900   assert( pVtab->pModule!=0 );
901   pMod = (sqlite3_module *)pVtab->pModule;
902   if( pMod->xFindFunction==0 ) return pDef;
903 
904   /* Call the xFindFunction method on the virtual table implementation
905   ** to see if the implementation wants to overload this function
906   */
907   zLowerName = sqlite3DbStrDup(db, pDef->zName);
908   if( zLowerName ){
909     for(z=(unsigned char*)zLowerName; *z; z++){
910       *z = sqlite3UpperToLower[*z];
911     }
912     rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
913     sqlite3DbFree(db, zLowerName);
914   }
915   if( rc==0 ){
916     return pDef;
917   }
918 
919   /* Create a new ephemeral function definition for the overloaded
920   ** function */
921   pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
922                              + sqlite3Strlen30(pDef->zName) + 1);
923   if( pNew==0 ){
924     return pDef;
925   }
926   *pNew = *pDef;
927   pNew->zName = (char *)&pNew[1];
928   memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
929   pNew->xFunc = xFunc;
930   pNew->pUserData = pArg;
931   pNew->flags |= SQLITE_FUNC_EPHEM;
932   return pNew;
933 }
934 
935 /*
936 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
937 ** array so that an OP_VBegin will get generated for it.  Add pTab to the
938 ** array if it is missing.  If pTab is already in the array, this routine
939 ** is a no-op.
940 */
941 void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
942   Parse *pToplevel = sqlite3ParseToplevel(pParse);
943   int i, n;
944   Table **apVtabLock;
945 
946   assert( IsVirtual(pTab) );
947   for(i=0; i<pToplevel->nVtabLock; i++){
948     if( pTab==pToplevel->apVtabLock[i] ) return;
949   }
950   n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
951   apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
952   if( apVtabLock ){
953     pToplevel->apVtabLock = apVtabLock;
954     pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
955   }else{
956     pToplevel->db->mallocFailed = 1;
957   }
958 }
959 
960 #endif /* SQLITE_OMIT_VIRTUALTABLE */
961