xref: /sqlite-3.40.0/src/vtab.c (revision 74e4352a)
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.39 2007/01/09 14:01:14 drh Exp $
15 */
16 #ifndef SQLITE_OMIT_VIRTUALTABLE
17 #include "sqliteInt.h"
18 
19 /*
20 ** External API function used to create a new virtual-table module.
21 */
22 int sqlite3_create_module(
23   sqlite3 *db,                    /* Database in which module is registered */
24   const char *zName,              /* Name assigned to this module */
25   const sqlite3_module *pModule,  /* The definition of the module */
26   void *pAux                      /* Context pointer for xCreate/xConnect */
27 ){
28   int nName = strlen(zName);
29   Module *pMod = (Module *)sqliteMallocRaw(sizeof(Module) + nName + 1);
30   if( pMod ){
31     char *zCopy = (char *)(&pMod[1]);
32     strcpy(zCopy, zName);
33     pMod->zName = zCopy;
34     pMod->pModule = pModule;
35     pMod->pAux = pAux;
36     pMod = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
37     sqliteFree(pMod);
38     sqlite3ResetInternalSchema(db, 0);
39   }
40   return sqlite3ApiExit(db, SQLITE_OK);
41 }
42 
43 /*
44 ** Lock the virtual table so that it cannot be disconnected.
45 ** Locks nest.  Every lock should have a corresponding unlock.
46 ** If an unlock is omitted, resources leaks will occur.
47 **
48 ** If a disconnect is attempted while a virtual table is locked,
49 ** the disconnect is deferred until all locks have been removed.
50 */
51 void sqlite3VtabLock(sqlite3_vtab *pVtab){
52   pVtab->nRef++;
53 }
54 
55 /*
56 ** Unlock a virtual table.  When the last lock is removed,
57 ** disconnect the virtual table.
58 */
59 void sqlite3VtabUnlock(sqlite3_vtab *pVtab){
60   pVtab->nRef--;
61   if( pVtab->nRef==0 ){
62     pVtab->pModule->xDisconnect(pVtab);
63   }
64 }
65 
66 /*
67 ** Clear any and all virtual-table information from the Table record.
68 ** This routine is called, for example, just before deleting the Table
69 ** record.
70 */
71 void sqlite3VtabClear(Table *p){
72   sqlite3_vtab *pVtab = p->pVtab;
73   if( pVtab ){
74     assert( p->pMod && p->pMod->pModule );
75     sqlite3VtabUnlock(pVtab);
76     p->pVtab = 0;
77   }
78   if( p->azModuleArg ){
79     int i;
80     for(i=0; i<p->nModuleArg; i++){
81       sqliteFree(p->azModuleArg[i]);
82     }
83     sqliteFree(p->azModuleArg);
84   }
85 }
86 
87 /*
88 ** Add a new module argument to pTable->azModuleArg[].
89 ** The string is not copied - the pointer is stored.  The
90 ** string will be freed automatically when the table is
91 ** deleted.
92 */
93 static void addModuleArgument(Table *pTable, char *zArg){
94   int i = pTable->nModuleArg++;
95   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
96   char **azModuleArg;
97   azModuleArg = sqliteRealloc(pTable->azModuleArg, nBytes);
98   if( azModuleArg==0 ){
99     int j;
100     for(j=0; j<i; j++){
101       sqliteFree(pTable->azModuleArg[j]);
102     }
103     sqliteFree(zArg);
104     sqliteFree(pTable->azModuleArg);
105     pTable->nModuleArg = 0;
106   }else{
107     azModuleArg[i] = zArg;
108     azModuleArg[i+1] = 0;
109   }
110   pTable->azModuleArg = azModuleArg;
111 }
112 
113 /*
114 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
115 ** statement.  The module name has been parsed, but the optional list
116 ** of parameters that follow the module name are still pending.
117 */
118 void sqlite3VtabBeginParse(
119   Parse *pParse,        /* Parsing context */
120   Token *pName1,        /* Name of new table, or database name */
121   Token *pName2,        /* Name of new table or NULL */
122   Token *pModuleName    /* Name of the module for the virtual table */
123 ){
124   int iDb;              /* The database the table is being created in */
125   Table *pTable;        /* The new virtual table */
126 
127   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
128   pTable = pParse->pNewTable;
129   if( pTable==0 || pParse->nErr ) return;
130   assert( 0==pTable->pIndex );
131 
132   iDb = sqlite3SchemaToIndex(pParse->db, pTable->pSchema);
133   assert( iDb>=0 );
134 
135   pTable->isVirtual = 1;
136   pTable->nModuleArg = 0;
137   addModuleArgument(pTable, sqlite3NameFromToken(pModuleName));
138   addModuleArgument(pTable, sqlite3StrDup(pParse->db->aDb[iDb].zName));
139   addModuleArgument(pTable, sqlite3StrDup(pTable->zName));
140   pParse->sNameToken.n = pModuleName->z + pModuleName->n - pName1->z;
141 
142 #ifndef SQLITE_OMIT_AUTHORIZATION
143   /* Creating a virtual table invokes the authorization callback twice.
144   ** The first invocation, to obtain permission to INSERT a row into the
145   ** sqlite_master table, has already been made by sqlite3StartTable().
146   ** The second call, to obtain permission to create the table, is made now.
147   */
148   if( pTable->azModuleArg ){
149     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
150             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
151   }
152 #endif
153 }
154 
155 /*
156 ** This routine takes the module argument that has been accumulating
157 ** in pParse->zArg[] and appends it to the list of arguments on the
158 ** virtual table currently under construction in pParse->pTable.
159 */
160 static void addArgumentToVtab(Parse *pParse){
161   if( pParse->sArg.z && pParse->pNewTable ){
162     const char *z = (const char*)pParse->sArg.z;
163     int n = pParse->sArg.n;
164     addModuleArgument(pParse->pNewTable, sqliteStrNDup(z, n));
165   }
166 }
167 
168 /*
169 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
170 ** has been completely parsed.
171 */
172 void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
173   Table *pTab;        /* The table being constructed */
174   sqlite3 *db;        /* The database connection */
175   char *zModule;      /* The module name of the table: USING modulename */
176   Module *pMod = 0;
177 
178   addArgumentToVtab(pParse);
179   pParse->sArg.z = 0;
180 
181   /* Lookup the module name. */
182   pTab = pParse->pNewTable;
183   if( pTab==0 ) return;
184   db = pParse->db;
185   if( pTab->nModuleArg<1 ) return;
186   zModule = pTab->azModuleArg[0];
187   pMod = (Module *)sqlite3HashFind(&db->aModule, zModule, strlen(zModule));
188   pTab->pMod = pMod;
189 
190   /* If the CREATE VIRTUAL TABLE statement is being entered for the
191   ** first time (in other words if the virtual table is actually being
192   ** created now instead of just being read out of sqlite_master) then
193   ** do additional initialization work and store the statement text
194   ** in the sqlite_master table.
195   */
196   if( !db->init.busy ){
197     char *zStmt;
198     char *zWhere;
199     int iDb;
200     Vdbe *v;
201 
202     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
203     if( pEnd ){
204       pParse->sNameToken.n = pEnd->z - pParse->sNameToken.z + pEnd->n;
205     }
206     zStmt = sqlite3MPrintf("CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
207 
208     /* A slot for the record has already been allocated in the
209     ** SQLITE_MASTER table.  We just need to update that slot with all
210     ** the information we've collected.
211     **
212     ** The top of the stack is the rootpage allocated by sqlite3StartTable().
213     ** This value is always 0 and is ignored, a virtual table does not have a
214     ** rootpage. The next entry on the stack is the rowid of the record
215     ** in the sqlite_master table.
216     */
217     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
218     sqlite3NestedParse(pParse,
219       "UPDATE %Q.%s "
220          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
221        "WHERE rowid=#1",
222       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
223       pTab->zName,
224       pTab->zName,
225       zStmt
226     );
227     sqliteFree(zStmt);
228     v = sqlite3GetVdbe(pParse);
229     sqlite3ChangeCookie(db, v, iDb);
230 
231     sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
232     zWhere = sqlite3MPrintf("name='%q'", pTab->zName);
233     sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 1, zWhere, P3_DYNAMIC);
234     sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1);
235   }
236 
237   /* If we are rereading the sqlite_master table create the in-memory
238   ** record of the table. If the module has already been registered,
239   ** also call the xConnect method here.
240   */
241   else {
242     Table *pOld;
243     Schema *pSchema = pTab->pSchema;
244     const char *zName = pTab->zName;
245     int nName = strlen(zName) + 1;
246     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
247     if( pOld ){
248       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
249       return;
250     }
251     pParse->pNewTable = 0;
252   }
253 }
254 
255 /*
256 ** The parser calls this routine when it sees the first token
257 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
258 */
259 void sqlite3VtabArgInit(Parse *pParse){
260   addArgumentToVtab(pParse);
261   pParse->sArg.z = 0;
262   pParse->sArg.n = 0;
263 }
264 
265 /*
266 ** The parser calls this routine for each token after the first token
267 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
268 */
269 void sqlite3VtabArgExtend(Parse *pParse, Token *p){
270   Token *pArg = &pParse->sArg;
271   if( pArg->z==0 ){
272     pArg->z = p->z;
273     pArg->n = p->n;
274   }else{
275     assert(pArg->z < p->z);
276     pArg->n = (p->z + p->n - pArg->z);
277   }
278 }
279 
280 /*
281 ** Invoke a virtual table constructor (either xCreate or xConnect). The
282 ** pointer to the function to invoke is passed as the fourth parameter
283 ** to this procedure.
284 */
285 static int vtabCallConstructor(
286   sqlite3 *db,
287   Table *pTab,
288   Module *pMod,
289   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
290   char **pzErr
291 ){
292   int rc;
293   int rc2;
294   sqlite3_vtab *pVtab;
295   const char *const*azArg = (const char *const*)pTab->azModuleArg;
296   int nArg = pTab->nModuleArg;
297   char *zErr = 0;
298   char *zModuleName = sqlite3MPrintf("%s", pTab->zName);
299 
300   assert( !db->pVTab );
301   assert( xConstruct );
302 
303   db->pVTab = pTab;
304   rc = sqlite3SafetyOff(db);
305   assert( rc==SQLITE_OK );
306   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pTab->pVtab, &zErr);
307   rc2 = sqlite3SafetyOn(db);
308   pVtab = pTab->pVtab;
309   if( rc==SQLITE_OK && pVtab ){
310     pVtab->pModule = pMod->pModule;
311     pVtab->nRef = 1;
312   }
313 
314   if( SQLITE_OK!=rc ){
315     if( zErr==0 ){
316       *pzErr = sqlite3MPrintf("vtable constructor failed: %s", zModuleName);
317     }else {
318       *pzErr = sqlite3MPrintf("%s", zErr);
319       sqlite3_free(zErr);
320     }
321   }else if( db->pVTab ){
322     const char *zFormat = "vtable constructor did not declare schema: %s";
323     *pzErr = sqlite3MPrintf(zFormat, pTab->zName);
324     rc = SQLITE_ERROR;
325   }
326   if( rc==SQLITE_OK ){
327     rc = rc2;
328   }
329   db->pVTab = 0;
330   sqliteFree(zModuleName);
331   return rc;
332 }
333 
334 /*
335 ** This function is invoked by the parser to call the xConnect() method
336 ** of the virtual table pTab. If an error occurs, an error code is returned
337 ** and an error left in pParse.
338 **
339 ** This call is a no-op if table pTab is not a virtual table.
340 */
341 int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
342   Module *pMod;
343   int rc = SQLITE_OK;
344 
345   if( !pTab || !pTab->isVirtual || pTab->pVtab ){
346     return SQLITE_OK;
347   }
348 
349   pMod = pTab->pMod;
350   if( !pMod ){
351     const char *zModule = pTab->azModuleArg[0];
352     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
353     rc = SQLITE_ERROR;
354   } else {
355     char *zErr = 0;
356     sqlite3 *db = pParse->db;
357     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
358     if( rc!=SQLITE_OK ){
359       sqlite3ErrorMsg(pParse, "%s", zErr);
360     }
361     sqliteFree(zErr);
362   }
363 
364   return rc;
365 }
366 
367 /*
368 ** Add the virtual table pVtab to the array sqlite3.aVTrans[].
369 */
370 static int addToVTrans(sqlite3 *db, sqlite3_vtab *pVtab){
371   const int ARRAY_INCR = 5;
372 
373   /* Grow the sqlite3.aVTrans array if required */
374   if( (db->nVTrans%ARRAY_INCR)==0 ){
375     sqlite3_vtab **aVTrans;
376     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
377     aVTrans = sqliteRealloc((void *)db->aVTrans, nBytes);
378     if( !aVTrans ){
379       return SQLITE_NOMEM;
380     }
381     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
382     db->aVTrans = aVTrans;
383   }
384 
385   /* Add pVtab to the end of sqlite3.aVTrans */
386   db->aVTrans[db->nVTrans++] = pVtab;
387   sqlite3VtabLock(pVtab);
388   return SQLITE_OK;
389 }
390 
391 /*
392 ** This function is invoked by the vdbe to call the xCreate method
393 ** of the virtual table named zTab in database iDb.
394 **
395 ** If an error occurs, *pzErr is set to point an an English language
396 ** description of the error and an SQLITE_XXX error code is returned.
397 ** In this case the caller must call sqliteFree() on *pzErr.
398 */
399 int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
400   int rc = SQLITE_OK;
401   Table *pTab;
402   Module *pMod;
403   const char *zModule;
404 
405   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
406   assert(pTab && pTab->isVirtual && !pTab->pVtab);
407   pMod = pTab->pMod;
408   zModule = pTab->azModuleArg[0];
409 
410   /* If the module has been registered and includes a Create method,
411   ** invoke it now. If the module has not been registered, return an
412   ** error. Otherwise, do nothing.
413   */
414   if( !pMod ){
415     *pzErr = sqlite3MPrintf("no such module: %s", zModule);
416     rc = SQLITE_ERROR;
417   }else{
418     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
419   }
420 
421   if( rc==SQLITE_OK && pTab->pVtab ){
422       rc = addToVTrans(db, pTab->pVtab);
423   }
424 
425   return rc;
426 }
427 
428 /*
429 ** This function is used to set the schema of a virtual table.  It is only
430 ** valid to call this function from within the xCreate() or xConnect() of a
431 ** virtual table module.
432 */
433 int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
434   Parse sParse;
435 
436   int rc = SQLITE_OK;
437   Table *pTab = db->pVTab;
438   char *zErr = 0;
439 
440   if( !pTab ){
441     sqlite3Error(db, SQLITE_MISUSE, 0);
442     return SQLITE_MISUSE;
443   }
444   assert(pTab->isVirtual && pTab->nCol==0 && pTab->aCol==0);
445 
446   memset(&sParse, 0, sizeof(Parse));
447   sParse.declareVtab = 1;
448   sParse.db = db;
449 
450   if(
451       SQLITE_OK == sqlite3RunParser(&sParse, zCreateTable, &zErr) &&
452       sParse.pNewTable &&
453       !sParse.pNewTable->pSelect &&
454       !sParse.pNewTable->isVirtual
455   ){
456     pTab->aCol = sParse.pNewTable->aCol;
457     pTab->nCol = sParse.pNewTable->nCol;
458     sParse.pNewTable->nCol = 0;
459     sParse.pNewTable->aCol = 0;
460   } else {
461     sqlite3Error(db, SQLITE_ERROR, zErr);
462     sqliteFree(zErr);
463     rc = SQLITE_ERROR;
464   }
465   sParse.declareVtab = 0;
466 
467   sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
468   sqlite3DeleteTable(0, sParse.pNewTable);
469   sParse.pNewTable = 0;
470   db->pVTab = 0;
471 
472   assert( (rc&0xff)==rc );
473   return rc;
474 }
475 
476 /*
477 ** This function is invoked by the vdbe to call the xDestroy method
478 ** of the virtual table named zTab in database iDb. This occurs
479 ** when a DROP TABLE is mentioned.
480 **
481 ** This call is a no-op if zTab is not a virtual table.
482 */
483 int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab)
484 {
485   int rc = SQLITE_OK;
486   Table *pTab;
487 
488   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
489   assert(pTab);
490   if( pTab->pVtab ){
491     int (*xDestroy)(sqlite3_vtab *pVTab) = pTab->pMod->pModule->xDestroy;
492     rc = sqlite3SafetyOff(db);
493     assert( rc==SQLITE_OK );
494     if( xDestroy ){
495       rc = xDestroy(pTab->pVtab);
496     }
497     sqlite3SafetyOn(db);
498     if( rc==SQLITE_OK ){
499       pTab->pVtab = 0;
500     }
501   }
502 
503   return rc;
504 }
505 
506 /*
507 ** This function invokes either the xRollback or xCommit method
508 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
509 ** called is identified by the second argument, "offset", which is
510 ** the offset of the method to call in the sqlite3_module structure.
511 **
512 ** The array is cleared after invoking the callbacks.
513 */
514 static void callFinaliser(sqlite3 *db, int offset){
515   int i;
516   for(i=0; i<db->nVTrans && db->aVTrans[i]; i++){
517     sqlite3_vtab *pVtab = db->aVTrans[i];
518     int (*x)(sqlite3_vtab *);
519     x = *(int (**)(sqlite3_vtab *))((char *)pVtab->pModule + offset);
520     if( x ) x(pVtab);
521     sqlite3VtabUnlock(pVtab);
522   }
523   sqliteFree(db->aVTrans);
524   db->nVTrans = 0;
525   db->aVTrans = 0;
526 }
527 
528 /*
529 ** If argument rc2 is not SQLITE_OK, then return it and do nothing.
530 ** Otherwise, invoke the xSync method of all virtual tables in the
531 ** sqlite3.aVTrans array. Return the error code for the first error
532 ** that occurs, or SQLITE_OK if all xSync operations are successful.
533 */
534 int sqlite3VtabSync(sqlite3 *db, int rc2){
535   int i;
536   int rc = SQLITE_OK;
537   int rcsafety;
538   sqlite3_vtab **aVTrans = db->aVTrans;
539   if( rc2!=SQLITE_OK ) return rc2;
540 
541   rc = sqlite3SafetyOff(db);
542   db->aVTrans = 0;
543   for(i=0; rc==SQLITE_OK && i<db->nVTrans && aVTrans[i]; i++){
544     sqlite3_vtab *pVtab = aVTrans[i];
545     int (*x)(sqlite3_vtab *);
546     x = pVtab->pModule->xSync;
547     if( x ){
548       rc = x(pVtab);
549     }
550   }
551   db->aVTrans = aVTrans;
552   rcsafety = sqlite3SafetyOn(db);
553 
554   if( rc==SQLITE_OK ){
555     rc = rcsafety;
556   }
557   return rc;
558 }
559 
560 /*
561 ** Invoke the xRollback method of all virtual tables in the
562 ** sqlite3.aVTrans array. Then clear the array itself.
563 */
564 int sqlite3VtabRollback(sqlite3 *db){
565   callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback));
566   return SQLITE_OK;
567 }
568 
569 /*
570 ** Invoke the xCommit method of all virtual tables in the
571 ** sqlite3.aVTrans array. Then clear the array itself.
572 */
573 int sqlite3VtabCommit(sqlite3 *db){
574   callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit));
575   return SQLITE_OK;
576 }
577 
578 /*
579 ** If the virtual table pVtab supports the transaction interface
580 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
581 ** not currently open, invoke the xBegin method now.
582 **
583 ** If the xBegin call is successful, place the sqlite3_vtab pointer
584 ** in the sqlite3.aVTrans array.
585 */
586 int sqlite3VtabBegin(sqlite3 *db, sqlite3_vtab *pVtab){
587   int rc = SQLITE_OK;
588   const sqlite3_module *pModule;
589 
590   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
591   ** than zero, then this function is being called from within a
592   ** virtual module xSync() callback. It is illegal to write to
593   ** virtual module tables in this case, so return SQLITE_LOCKED.
594   */
595   if( 0==db->aVTrans && db->nVTrans>0 ){
596     return SQLITE_LOCKED;
597   }
598   if( !pVtab ){
599     return SQLITE_OK;
600   }
601   pModule = pVtab->pModule;
602 
603   if( pModule->xBegin ){
604     int i;
605 
606 
607     /* If pVtab is already in the aVTrans array, return early */
608     for(i=0; (i<db->nVTrans) && 0!=db->aVTrans[i]; i++){
609       if( db->aVTrans[i]==pVtab ){
610         return SQLITE_OK;
611       }
612     }
613 
614     /* Invoke the xBegin method */
615     rc = pModule->xBegin(pVtab);
616     if( rc!=SQLITE_OK ){
617       return rc;
618     }
619 
620     rc = addToVTrans(db, pVtab);
621   }
622   return rc;
623 }
624 
625 /*
626 ** The first parameter (pDef) is a function implementation.  The
627 ** second parameter (pExpr) is the first argument to this function.
628 ** If pExpr is a column in a virtual table, then let the virtual
629 ** table implementation have an opportunity to overload the function.
630 **
631 ** This routine is used to allow virtual table implementations to
632 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
633 **
634 ** Return either the pDef argument (indicating no change) or a
635 ** new FuncDef structure that is marked as ephemeral using the
636 ** SQLITE_FUNC_EPHEM flag.
637 */
638 FuncDef *sqlite3VtabOverloadFunction(
639   FuncDef *pDef,  /* Function to possibly overload */
640   int nArg,       /* Number of arguments to the function */
641   Expr *pExpr     /* First argument to the function */
642 ){
643   Table *pTab;
644   sqlite3_vtab *pVtab;
645   sqlite3_module *pMod;
646   void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
647   void *pArg;
648   FuncDef *pNew;
649   int rc;
650   char *zLowerName;
651   unsigned char *z;
652 
653 
654   /* Check to see the left operand is a column in a virtual table */
655   if( pExpr==0 ) return pDef;
656   if( pExpr->op!=TK_COLUMN ) return pDef;
657   pTab = pExpr->pTab;
658   if( pTab==0 ) return pDef;
659   if( !pTab->isVirtual ) return pDef;
660   pVtab = pTab->pVtab;
661   assert( pVtab!=0 );
662   assert( pVtab->pModule!=0 );
663   pMod = (sqlite3_module *)pVtab->pModule;
664   if( pMod->xFindFunction==0 ) return pDef;
665 
666   /* Call the xFuncFunction method on the virtual table implementation
667   ** to see if the implementation wants to overload this function
668   */
669   zLowerName = sqlite3StrDup(pDef->zName);
670   for(z=(unsigned char*)zLowerName; *z; z++){
671     *z = sqlite3UpperToLower[*z];
672   }
673   rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
674   sqliteFree(zLowerName);
675   if( rc==0 ){
676     return pDef;
677   }
678 
679   /* Create a new ephemeral function definition for the overloaded
680   ** function */
681   pNew = sqliteMalloc( sizeof(*pNew) + strlen(pDef->zName) );
682   if( pNew==0 ){
683     return pDef;
684   }
685   *pNew = *pDef;
686   strcpy(pNew->zName, pDef->zName);
687   pNew->xFunc = xFunc;
688   pNew->pUserData = pArg;
689   pNew->flags |= SQLITE_FUNC_EPHEM;
690   return pNew;
691 }
692 
693 #endif /* SQLITE_OMIT_VIRTUALTABLE */
694