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