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