xref: /sqlite-3.40.0/src/vtab.c (revision 50f79f56)
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 ** Before a virtual table xCreate() or xConnect() method is invoked, the
19 ** sqlite3.pVtabCtx member variable is set to point to an instance of
20 ** this struct allocated on the stack. It is used by the implementation of
21 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
22 ** are invoked only from within xCreate and xConnect methods.
23 */
24 struct VtabCtx {
25   Table *pTab;
26   VTable *pVTable;
27 };
28 
29 /*
30 ** The actual function that does the work of creating a new module.
31 ** This function implements the sqlite3_create_module() and
32 ** sqlite3_create_module_v2() interfaces.
33 */
34 static int createModule(
35   sqlite3 *db,                    /* Database in which module is registered */
36   const char *zName,              /* Name assigned to this module */
37   const sqlite3_module *pModule,  /* The definition of the module */
38   void *pAux,                     /* Context pointer for xCreate/xConnect */
39   void (*xDestroy)(void *)        /* Module destructor function */
40 ){
41   int rc, nName;
42   Module *pMod;
43 
44   sqlite3_mutex_enter(db->mutex);
45   nName = sqlite3Strlen30(zName);
46   pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
47   if( pMod ){
48     Module *pDel;
49     char *zCopy = (char *)(&pMod[1]);
50     memcpy(zCopy, zName, nName+1);
51     pMod->zName = zCopy;
52     pMod->pModule = pModule;
53     pMod->pAux = pAux;
54     pMod->xDestroy = xDestroy;
55     pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
56     if( pDel && pDel->xDestroy ){
57       sqlite3ResetInternalSchema(db, -1);
58       pDel->xDestroy(pDel->pAux);
59     }
60     sqlite3DbFree(db, pDel);
61     if( pDel==pMod ){
62       db->mallocFailed = 1;
63     }
64   }else if( xDestroy ){
65     xDestroy(pAux);
66   }
67   rc = sqlite3ApiExit(db, SQLITE_OK);
68   sqlite3_mutex_leave(db->mutex);
69   return rc;
70 }
71 
72 
73 /*
74 ** External API function used to create a new virtual-table module.
75 */
76 int sqlite3_create_module(
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 ){
82   return createModule(db, zName, pModule, pAux, 0);
83 }
84 
85 /*
86 ** External API function used to create a new virtual-table module.
87 */
88 int sqlite3_create_module_v2(
89   sqlite3 *db,                    /* Database in which module is registered */
90   const char *zName,              /* Name assigned to this module */
91   const sqlite3_module *pModule,  /* The definition of the module */
92   void *pAux,                     /* Context pointer for xCreate/xConnect */
93   void (*xDestroy)(void *)        /* Module destructor function */
94 ){
95   return createModule(db, zName, pModule, pAux, xDestroy);
96 }
97 
98 /*
99 ** Lock the virtual table so that it cannot be disconnected.
100 ** Locks nest.  Every lock should have a corresponding unlock.
101 ** If an unlock is omitted, resources leaks will occur.
102 **
103 ** If a disconnect is attempted while a virtual table is locked,
104 ** the disconnect is deferred until all locks have been removed.
105 */
106 void sqlite3VtabLock(VTable *pVTab){
107   pVTab->nRef++;
108 }
109 
110 
111 /*
112 ** pTab is a pointer to a Table structure representing a virtual-table.
113 ** Return a pointer to the VTable object used by connection db to access
114 ** this virtual-table, if one has been created, or NULL otherwise.
115 */
116 VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
117   VTable *pVtab;
118   assert( IsVirtual(pTab) );
119   for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
120   return pVtab;
121 }
122 
123 /*
124 ** Decrement the ref-count on a virtual table object. When the ref-count
125 ** reaches zero, call the xDisconnect() method to delete the object.
126 */
127 void sqlite3VtabUnlock(VTable *pVTab){
128   sqlite3 *db = pVTab->db;
129 
130   assert( db );
131   assert( pVTab->nRef>0 );
132   assert( sqlite3SafetyCheckOk(db) );
133 
134   pVTab->nRef--;
135   if( pVTab->nRef==0 ){
136     sqlite3_vtab *p = pVTab->pVtab;
137     if( p ){
138       p->pModule->xDisconnect(p);
139     }
140     sqlite3DbFree(db, pVTab);
141   }
142 }
143 
144 /*
145 ** Table p is a virtual table. This function moves all elements in the
146 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
147 ** database connections to be disconnected at the next opportunity.
148 ** Except, if argument db is not NULL, then the entry associated with
149 ** connection db is left in the p->pVTable list.
150 */
151 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
152   VTable *pRet = 0;
153   VTable *pVTable = p->pVTable;
154   p->pVTable = 0;
155 
156   /* Assert that the mutex (if any) associated with the BtShared database
157   ** that contains table p is held by the caller. See header comments
158   ** above function sqlite3VtabUnlockList() for an explanation of why
159   ** this makes it safe to access the sqlite3.pDisconnect list of any
160   ** database connection that may have an entry in the p->pVTable list.
161   */
162   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
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(sqlite3 *db, Table *p){
236   if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
237   if( p->azModuleArg ){
238     int i;
239     for(i=0; i<p->nModuleArg; i++){
240       sqlite3DbFree(db, p->azModuleArg[i]);
241     }
242     sqlite3DbFree(db, 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   int ifNotExists       /* No error if the table already exists */
283 ){
284   int iDb;              /* The database the table is being created in */
285   Table *pTable;        /* The new virtual table */
286   sqlite3 *db;          /* Database connection */
287 
288   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
289   pTable = pParse->pNewTable;
290   if( pTable==0 ) return;
291   assert( 0==pTable->pIndex );
292 
293   db = pParse->db;
294   iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
295   assert( iDb>=0 );
296 
297   pTable->tabFlags |= TF_Virtual;
298   pTable->nModuleArg = 0;
299   addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
300   addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
301   addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
302   pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
303 
304 #ifndef SQLITE_OMIT_AUTHORIZATION
305   /* Creating a virtual table invokes the authorization callback twice.
306   ** The first invocation, to obtain permission to INSERT a row into the
307   ** sqlite_master table, has already been made by sqlite3StartTable().
308   ** The second call, to obtain permission to create the table, is made now.
309   */
310   if( pTable->azModuleArg ){
311     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
312             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
313   }
314 #endif
315 }
316 
317 /*
318 ** This routine takes the module argument that has been accumulating
319 ** in pParse->zArg[] and appends it to the list of arguments on the
320 ** virtual table currently under construction in pParse->pTable.
321 */
322 static void addArgumentToVtab(Parse *pParse){
323   if( pParse->sArg.z && pParse->pNewTable ){
324     const char *z = (const char*)pParse->sArg.z;
325     int n = pParse->sArg.n;
326     sqlite3 *db = pParse->db;
327     addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
328   }
329 }
330 
331 /*
332 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
333 ** has been completely parsed.
334 */
335 void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
336   Table *pTab = pParse->pNewTable;  /* The table being constructed */
337   sqlite3 *db = pParse->db;         /* The database connection */
338 
339   if( pTab==0 ) return;
340   addArgumentToVtab(pParse);
341   pParse->sArg.z = 0;
342   if( pTab->nModuleArg<1 ) return;
343 
344   /* If the CREATE VIRTUAL TABLE statement is being entered for the
345   ** first time (in other words if the virtual table is actually being
346   ** created now instead of just being read out of sqlite_master) then
347   ** do additional initialization work and store the statement text
348   ** in the sqlite_master table.
349   */
350   if( !db->init.busy ){
351     char *zStmt;
352     char *zWhere;
353     int iDb;
354     Vdbe *v;
355 
356     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
357     if( pEnd ){
358       pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
359     }
360     zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
361 
362     /* A slot for the record has already been allocated in the
363     ** SQLITE_MASTER table.  We just need to update that slot with all
364     ** the information we've collected.
365     **
366     ** The VM register number pParse->regRowid holds the rowid of an
367     ** entry in the sqlite_master table tht was created for this vtab
368     ** by sqlite3StartTable().
369     */
370     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
371     sqlite3NestedParse(pParse,
372       "UPDATE %Q.%s "
373          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
374        "WHERE rowid=#%d",
375       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
376       pTab->zName,
377       pTab->zName,
378       zStmt,
379       pParse->regRowid
380     );
381     sqlite3DbFree(db, zStmt);
382     v = sqlite3GetVdbe(pParse);
383     sqlite3ChangeCookie(pParse, iDb);
384 
385     sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
386     zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
387     sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
388     sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
389                          pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
390   }
391 
392   /* If we are rereading the sqlite_master table create the in-memory
393   ** record of the table. The xConnect() method is not called until
394   ** the first time the virtual table is used in an SQL statement. This
395   ** allows a schema that contains virtual tables to be loaded before
396   ** the required virtual table implementations are registered.  */
397   else {
398     Table *pOld;
399     Schema *pSchema = pTab->pSchema;
400     const char *zName = pTab->zName;
401     int nName = sqlite3Strlen30(zName);
402     assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
403     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
404     if( pOld ){
405       db->mallocFailed = 1;
406       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
407       return;
408     }
409     pParse->pNewTable = 0;
410   }
411 }
412 
413 /*
414 ** The parser calls this routine when it sees the first token
415 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
416 */
417 void sqlite3VtabArgInit(Parse *pParse){
418   addArgumentToVtab(pParse);
419   pParse->sArg.z = 0;
420   pParse->sArg.n = 0;
421 }
422 
423 /*
424 ** The parser calls this routine for each token after the first token
425 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
426 */
427 void sqlite3VtabArgExtend(Parse *pParse, Token *p){
428   Token *pArg = &pParse->sArg;
429   if( pArg->z==0 ){
430     pArg->z = p->z;
431     pArg->n = p->n;
432   }else{
433     assert(pArg->z < p->z);
434     pArg->n = (int)(&p->z[p->n] - pArg->z);
435   }
436 }
437 
438 /*
439 ** Invoke a virtual table constructor (either xCreate or xConnect). The
440 ** pointer to the function to invoke is passed as the fourth parameter
441 ** to this procedure.
442 */
443 static int vtabCallConstructor(
444   sqlite3 *db,
445   Table *pTab,
446   Module *pMod,
447   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
448   char **pzErr
449 ){
450   VtabCtx sCtx;
451   VTable *pVTable;
452   int rc;
453   const char *const*azArg = (const char *const*)pTab->azModuleArg;
454   int nArg = pTab->nModuleArg;
455   char *zErr = 0;
456   char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
457 
458   if( !zModuleName ){
459     return SQLITE_NOMEM;
460   }
461 
462   pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
463   if( !pVTable ){
464     sqlite3DbFree(db, zModuleName);
465     return SQLITE_NOMEM;
466   }
467   pVTable->db = db;
468   pVTable->pMod = pMod;
469 
470   /* Invoke the virtual table constructor */
471   assert( &db->pVtabCtx );
472   assert( xConstruct );
473   sCtx.pTab = pTab;
474   sCtx.pVTable = pVTable;
475   db->pVtabCtx = &sCtx;
476   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
477   db->pVtabCtx = 0;
478   if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
479 
480   if( SQLITE_OK!=rc ){
481     if( zErr==0 ){
482       *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
483     }else {
484       *pzErr = sqlite3MPrintf(db, "%s", zErr);
485       sqlite3_free(zErr);
486     }
487     sqlite3DbFree(db, pVTable);
488   }else if( ALWAYS(pVTable->pVtab) ){
489     /* Justification of ALWAYS():  A correct vtab constructor must allocate
490     ** the sqlite3_vtab object if successful.  */
491     pVTable->pVtab->pModule = pMod->pModule;
492     pVTable->nRef = 1;
493     if( sCtx.pTab ){
494       const char *zFormat = "vtable constructor did not declare schema: %s";
495       *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
496       sqlite3VtabUnlock(pVTable);
497       rc = SQLITE_ERROR;
498     }else{
499       int iCol;
500       /* If everything went according to plan, link the new VTable structure
501       ** into the linked list headed by pTab->pVTable. Then loop through the
502       ** columns of the table to see if any of them contain the token "hidden".
503       ** If so, set the Column.isHidden flag and remove the token from
504       ** the type string.  */
505       pVTable->pNext = pTab->pVTable;
506       pTab->pVTable = pVTable;
507 
508       for(iCol=0; iCol<pTab->nCol; iCol++){
509         char *zType = pTab->aCol[iCol].zType;
510         int nType;
511         int i = 0;
512         if( !zType ) continue;
513         nType = sqlite3Strlen30(zType);
514         if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
515           for(i=0; i<nType; i++){
516             if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
517              && (zType[i+7]=='\0' || zType[i+7]==' ')
518             ){
519               i++;
520               break;
521             }
522           }
523         }
524         if( i<nType ){
525           int j;
526           int nDel = 6 + (zType[i+6] ? 1 : 0);
527           for(j=i; (j+nDel)<=nType; j++){
528             zType[j] = zType[j+nDel];
529           }
530           if( zType[i]=='\0' && i>0 ){
531             assert(zType[i-1]==' ');
532             zType[i-1] = '\0';
533           }
534           pTab->aCol[iCol].isHidden = 1;
535         }
536       }
537     }
538   }
539 
540   sqlite3DbFree(db, zModuleName);
541   return rc;
542 }
543 
544 /*
545 ** This function is invoked by the parser to call the xConnect() method
546 ** of the virtual table pTab. If an error occurs, an error code is returned
547 ** and an error left in pParse.
548 **
549 ** This call is a no-op if table pTab is not a virtual table.
550 */
551 int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
552   sqlite3 *db = pParse->db;
553   const char *zMod;
554   Module *pMod;
555   int rc;
556 
557   assert( pTab );
558   if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
559     return SQLITE_OK;
560   }
561 
562   /* Locate the required virtual table module */
563   zMod = pTab->azModuleArg[0];
564   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
565 
566   if( !pMod ){
567     const char *zModule = pTab->azModuleArg[0];
568     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
569     rc = SQLITE_ERROR;
570   }else{
571     char *zErr = 0;
572     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
573     if( rc!=SQLITE_OK ){
574       sqlite3ErrorMsg(pParse, "%s", zErr);
575     }
576     sqlite3DbFree(db, zErr);
577   }
578 
579   return rc;
580 }
581 /*
582 ** Grow the db->aVTrans[] array so that there is room for at least one
583 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
584 */
585 static int growVTrans(sqlite3 *db){
586   const int ARRAY_INCR = 5;
587 
588   /* Grow the sqlite3.aVTrans array if required */
589   if( (db->nVTrans%ARRAY_INCR)==0 ){
590     VTable **aVTrans;
591     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
592     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
593     if( !aVTrans ){
594       return SQLITE_NOMEM;
595     }
596     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
597     db->aVTrans = aVTrans;
598   }
599 
600   return SQLITE_OK;
601 }
602 
603 /*
604 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
605 ** have already been reserved using growVTrans().
606 */
607 static void addToVTrans(sqlite3 *db, VTable *pVTab){
608   /* Add pVtab to the end of sqlite3.aVTrans */
609   db->aVTrans[db->nVTrans++] = pVTab;
610   sqlite3VtabLock(pVTab);
611 }
612 
613 /*
614 ** This function is invoked by the vdbe to call the xCreate method
615 ** of the virtual table named zTab in database iDb.
616 **
617 ** If an error occurs, *pzErr is set to point an an English language
618 ** description of the error and an SQLITE_XXX error code is returned.
619 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
620 */
621 int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
622   int rc = SQLITE_OK;
623   Table *pTab;
624   Module *pMod;
625   const char *zMod;
626 
627   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
628   assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
629 
630   /* Locate the required virtual table module */
631   zMod = pTab->azModuleArg[0];
632   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
633 
634   /* If the module has been registered and includes a Create method,
635   ** invoke it now. If the module has not been registered, return an
636   ** error. Otherwise, do nothing.
637   */
638   if( !pMod ){
639     *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
640     rc = SQLITE_ERROR;
641   }else{
642     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
643   }
644 
645   /* Justification of ALWAYS():  The xConstructor method is required to
646   ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
647   if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
648     rc = growVTrans(db);
649     if( rc==SQLITE_OK ){
650       addToVTrans(db, sqlite3GetVTable(db, pTab));
651     }
652   }
653 
654   return rc;
655 }
656 
657 /*
658 ** This function is used to set the schema of a virtual table.  It is only
659 ** valid to call this function from within the xCreate() or xConnect() of a
660 ** virtual table module.
661 */
662 int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
663   Parse *pParse;
664 
665   int rc = SQLITE_OK;
666   Table *pTab;
667   char *zErr = 0;
668 
669   sqlite3_mutex_enter(db->mutex);
670   if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
671     sqlite3Error(db, SQLITE_MISUSE, 0);
672     sqlite3_mutex_leave(db->mutex);
673     return SQLITE_MISUSE_BKPT;
674   }
675   assert( (pTab->tabFlags & TF_Virtual)!=0 );
676 
677   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
678   if( pParse==0 ){
679     rc = SQLITE_NOMEM;
680   }else{
681     pParse->declareVtab = 1;
682     pParse->db = db;
683     pParse->nQueryLoop = 1;
684 
685     if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
686      && pParse->pNewTable
687      && !db->mallocFailed
688      && !pParse->pNewTable->pSelect
689      && (pParse->pNewTable->tabFlags & TF_Virtual)==0
690     ){
691       if( !pTab->aCol ){
692         pTab->aCol = pParse->pNewTable->aCol;
693         pTab->nCol = pParse->pNewTable->nCol;
694         pParse->pNewTable->nCol = 0;
695         pParse->pNewTable->aCol = 0;
696       }
697       db->pVtabCtx->pTab = 0;
698     }else{
699       sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
700       sqlite3DbFree(db, zErr);
701       rc = SQLITE_ERROR;
702     }
703     pParse->declareVtab = 0;
704 
705     if( pParse->pVdbe ){
706       sqlite3VdbeFinalize(pParse->pVdbe);
707     }
708     sqlite3DeleteTable(db, pParse->pNewTable);
709     sqlite3StackFree(db, pParse);
710   }
711 
712   assert( (rc&0xff)==rc );
713   rc = sqlite3ApiExit(db, rc);
714   sqlite3_mutex_leave(db->mutex);
715   return rc;
716 }
717 
718 /*
719 ** This function is invoked by the vdbe to call the xDestroy method
720 ** of the virtual table named zTab in database iDb. This occurs
721 ** when a DROP TABLE is mentioned.
722 **
723 ** This call is a no-op if zTab is not a virtual table.
724 */
725 int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
726   int rc = SQLITE_OK;
727   Table *pTab;
728 
729   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
730   if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
731     VTable *p = vtabDisconnectAll(db, pTab);
732 
733     assert( rc==SQLITE_OK );
734     rc = p->pMod->pModule->xDestroy(p->pVtab);
735 
736     /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
737     if( rc==SQLITE_OK ){
738       assert( pTab->pVTable==p && p->pNext==0 );
739       p->pVtab = 0;
740       pTab->pVTable = 0;
741       sqlite3VtabUnlock(p);
742     }
743   }
744 
745   return rc;
746 }
747 
748 /*
749 ** This function invokes either the xRollback or xCommit method
750 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
751 ** called is identified by the second argument, "offset", which is
752 ** the offset of the method to call in the sqlite3_module structure.
753 **
754 ** The array is cleared after invoking the callbacks.
755 */
756 static void callFinaliser(sqlite3 *db, int offset){
757   int i;
758   if( db->aVTrans ){
759     for(i=0; i<db->nVTrans; i++){
760       VTable *pVTab = db->aVTrans[i];
761       sqlite3_vtab *p = pVTab->pVtab;
762       if( p ){
763         int (*x)(sqlite3_vtab *);
764         x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
765         if( x ) x(p);
766       }
767       pVTab->iSavepoint = 0;
768       sqlite3VtabUnlock(pVTab);
769     }
770     sqlite3DbFree(db, db->aVTrans);
771     db->nVTrans = 0;
772     db->aVTrans = 0;
773   }
774 }
775 
776 /*
777 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
778 ** array. Return the error code for the first error that occurs, or
779 ** SQLITE_OK if all xSync operations are successful.
780 **
781 ** Set *pzErrmsg to point to a buffer that should be released using
782 ** sqlite3DbFree() containing an error message, if one is available.
783 */
784 int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
785   int i;
786   int rc = SQLITE_OK;
787   VTable **aVTrans = db->aVTrans;
788 
789   db->aVTrans = 0;
790   for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
791     int (*x)(sqlite3_vtab *);
792     sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
793     if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
794       rc = x(pVtab);
795       sqlite3DbFree(db, *pzErrmsg);
796       *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
797       sqlite3_free(pVtab->zErrMsg);
798     }
799   }
800   db->aVTrans = aVTrans;
801   return rc;
802 }
803 
804 /*
805 ** Invoke the xRollback method of all virtual tables in the
806 ** sqlite3.aVTrans array. Then clear the array itself.
807 */
808 int sqlite3VtabRollback(sqlite3 *db){
809   callFinaliser(db, offsetof(sqlite3_module,xRollback));
810   return SQLITE_OK;
811 }
812 
813 /*
814 ** Invoke the xCommit method of all virtual tables in the
815 ** sqlite3.aVTrans array. Then clear the array itself.
816 */
817 int sqlite3VtabCommit(sqlite3 *db){
818   callFinaliser(db, offsetof(sqlite3_module,xCommit));
819   return SQLITE_OK;
820 }
821 
822 /*
823 ** If the virtual table pVtab supports the transaction interface
824 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
825 ** not currently open, invoke the xBegin method now.
826 **
827 ** If the xBegin call is successful, place the sqlite3_vtab pointer
828 ** in the sqlite3.aVTrans array.
829 */
830 int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
831   int rc = SQLITE_OK;
832   const sqlite3_module *pModule;
833 
834   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
835   ** than zero, then this function is being called from within a
836   ** virtual module xSync() callback. It is illegal to write to
837   ** virtual module tables in this case, so return SQLITE_LOCKED.
838   */
839   if( sqlite3VtabInSync(db) ){
840     return SQLITE_LOCKED;
841   }
842   if( !pVTab ){
843     return SQLITE_OK;
844   }
845   pModule = pVTab->pVtab->pModule;
846 
847   if( pModule->xBegin ){
848     int i;
849 
850     /* If pVtab is already in the aVTrans array, return early */
851     for(i=0; i<db->nVTrans; i++){
852       if( db->aVTrans[i]==pVTab ){
853         return SQLITE_OK;
854       }
855     }
856 
857     /* Invoke the xBegin method. If successful, add the vtab to the
858     ** sqlite3.aVTrans[] array. */
859     rc = growVTrans(db);
860     if( rc==SQLITE_OK ){
861       rc = pModule->xBegin(pVTab->pVtab);
862       if( rc==SQLITE_OK ){
863         addToVTrans(db, pVTab);
864       }
865     }
866   }
867   return rc;
868 }
869 
870 /*
871 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
872 ** virtual tables that currently have an open transaction. Pass iSavepoint
873 ** as the second argument to the virtual table method invoked.
874 **
875 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
876 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
877 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
878 ** an open transaction is invoked.
879 **
880 ** If any virtual table method returns an error code other than SQLITE_OK,
881 ** processing is abandoned and the error returned to the caller of this
882 ** function immediately. If all calls to virtual table methods are successful,
883 ** SQLITE_OK is returned.
884 */
885 int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
886   int rc = SQLITE_OK;
887 
888   assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
889   assert( iSavepoint>=0 );
890   if( db->aVTrans ){
891     int i;
892     for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
893       VTable *pVTab = db->aVTrans[i];
894       const sqlite3_module *pMod = pVTab->pMod->pModule;
895       if( pVTab->pVtab && pMod->iVersion>=2 ){
896         int (*xMethod)(sqlite3_vtab *, int);
897         switch( op ){
898           case SAVEPOINT_BEGIN:
899             xMethod = pMod->xSavepoint;
900             pVTab->iSavepoint = iSavepoint+1;
901             break;
902           case SAVEPOINT_ROLLBACK:
903             xMethod = pMod->xRollbackTo;
904             break;
905           default:
906             xMethod = pMod->xRelease;
907             break;
908         }
909         if( xMethod && pVTab->iSavepoint>iSavepoint ){
910           rc = xMethod(pVTab->pVtab, iSavepoint);
911         }
912       }
913     }
914   }
915   return rc;
916 }
917 
918 /*
919 ** The first parameter (pDef) is a function implementation.  The
920 ** second parameter (pExpr) is the first argument to this function.
921 ** If pExpr is a column in a virtual table, then let the virtual
922 ** table implementation have an opportunity to overload the function.
923 **
924 ** This routine is used to allow virtual table implementations to
925 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
926 **
927 ** Return either the pDef argument (indicating no change) or a
928 ** new FuncDef structure that is marked as ephemeral using the
929 ** SQLITE_FUNC_EPHEM flag.
930 */
931 FuncDef *sqlite3VtabOverloadFunction(
932   sqlite3 *db,    /* Database connection for reporting malloc problems */
933   FuncDef *pDef,  /* Function to possibly overload */
934   int nArg,       /* Number of arguments to the function */
935   Expr *pExpr     /* First argument to the function */
936 ){
937   Table *pTab;
938   sqlite3_vtab *pVtab;
939   sqlite3_module *pMod;
940   void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
941   void *pArg = 0;
942   FuncDef *pNew;
943   int rc = 0;
944   char *zLowerName;
945   unsigned char *z;
946 
947 
948   /* Check to see the left operand is a column in a virtual table */
949   if( NEVER(pExpr==0) ) return pDef;
950   if( pExpr->op!=TK_COLUMN ) return pDef;
951   pTab = pExpr->pTab;
952   if( NEVER(pTab==0) ) return pDef;
953   if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
954   pVtab = sqlite3GetVTable(db, pTab)->pVtab;
955   assert( pVtab!=0 );
956   assert( pVtab->pModule!=0 );
957   pMod = (sqlite3_module *)pVtab->pModule;
958   if( pMod->xFindFunction==0 ) return pDef;
959 
960   /* Call the xFindFunction method on the virtual table implementation
961   ** to see if the implementation wants to overload this function
962   */
963   zLowerName = sqlite3DbStrDup(db, pDef->zName);
964   if( zLowerName ){
965     for(z=(unsigned char*)zLowerName; *z; z++){
966       *z = sqlite3UpperToLower[*z];
967     }
968     rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
969     sqlite3DbFree(db, zLowerName);
970   }
971   if( rc==0 ){
972     return pDef;
973   }
974 
975   /* Create a new ephemeral function definition for the overloaded
976   ** function */
977   pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
978                              + sqlite3Strlen30(pDef->zName) + 1);
979   if( pNew==0 ){
980     return pDef;
981   }
982   *pNew = *pDef;
983   pNew->zName = (char *)&pNew[1];
984   memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
985   pNew->xFunc = xFunc;
986   pNew->pUserData = pArg;
987   pNew->flags |= SQLITE_FUNC_EPHEM;
988   return pNew;
989 }
990 
991 /*
992 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
993 ** array so that an OP_VBegin will get generated for it.  Add pTab to the
994 ** array if it is missing.  If pTab is already in the array, this routine
995 ** is a no-op.
996 */
997 void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
998   Parse *pToplevel = sqlite3ParseToplevel(pParse);
999   int i, n;
1000   Table **apVtabLock;
1001 
1002   assert( IsVirtual(pTab) );
1003   for(i=0; i<pToplevel->nVtabLock; i++){
1004     if( pTab==pToplevel->apVtabLock[i] ) return;
1005   }
1006   n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
1007   apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
1008   if( apVtabLock ){
1009     pToplevel->apVtabLock = apVtabLock;
1010     pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
1011   }else{
1012     pToplevel->db->mallocFailed = 1;
1013   }
1014 }
1015 
1016 /*
1017 ** Return the ON CONFLICT resolution mode in effect for the virtual
1018 ** table update operation currently in progress.
1019 **
1020 ** The results of this routine are undefined unless it is called from
1021 ** within an xUpdate method.
1022 */
1023 int sqlite3_vtab_on_conflict(sqlite3 *db){
1024   static const unsigned char aMap[] = {
1025     SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
1026   };
1027   assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
1028   assert( OE_Ignore==4 && OE_Replace==5 );
1029   assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
1030   return (int)aMap[db->vtabOnConflict-1];
1031 }
1032 
1033 /*
1034 ** Call from within the xCreate() or xConnect() methods to provide
1035 ** the SQLite core with additional information about the behavior
1036 ** of the virtual table being implemented.
1037 */
1038 int sqlite3_vtab_config(sqlite3 *db, int op, ...){
1039   va_list ap;
1040   int rc = SQLITE_OK;
1041 
1042   sqlite3_mutex_enter(db->mutex);
1043 
1044   va_start(ap, op);
1045   switch( op ){
1046     case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
1047       VtabCtx *p = db->pVtabCtx;
1048       if( !p ){
1049         rc = SQLITE_MISUSE_BKPT;
1050       }else{
1051         assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
1052         p->pVTable->bConstraint = (u8)va_arg(ap, int);
1053       }
1054       break;
1055     }
1056     default:
1057       rc = SQLITE_MISUSE_BKPT;
1058       break;
1059   }
1060   va_end(ap);
1061 
1062   if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
1063   sqlite3_mutex_leave(db->mutex);
1064   return rc;
1065 }
1066 
1067 #endif /* SQLITE_OMIT_VIRTUALTABLE */
1068