xref: /sqlite-3.40.0/src/test8.c (revision 9f8a4b43)
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 ** Code for testing the virtual table interfaces.  This code
13 ** is not included in the SQLite library.  It is used for automated
14 ** testing of the SQLite library.
15 **
16 ** $Id: test8.c,v 1.48 2007/07/20 00:35:59 drh Exp $
17 */
18 #include "sqliteInt.h"
19 #include "tcl.h"
20 #include "os.h"
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #ifndef SQLITE_OMIT_VIRTUALTABLE
25 
26 typedef struct echo_vtab echo_vtab;
27 typedef struct echo_cursor echo_cursor;
28 
29 /*
30 ** The test module defined in this file uses four global Tcl variables to
31 ** commicate with test-scripts:
32 **
33 **     $::echo_module
34 **     $::echo_module_sync_fail
35 **     $::echo_module_begin_fail
36 **     $::echo_module_cost
37 **
38 ** The variable ::echo_module is a list. Each time one of the following
39 ** methods is called, one or more elements are appended to the list.
40 ** This is used for automated testing of virtual table modules.
41 **
42 ** The ::echo_module_sync_fail variable is set by test scripts and read
43 ** by code in this file. If it is set to the name of a real table in the
44 ** the database, then all xSync operations on echo virtual tables that
45 ** use the named table as a backing store will fail.
46 */
47 
48 /*
49 ** An echo virtual-table object.
50 **
51 ** echo.vtab.aIndex is an array of booleans. The nth entry is true if
52 ** the nth column of the real table is the left-most column of an index
53 ** (implicit or otherwise). In other words, if SQLite can optimize
54 ** a query like "SELECT * FROM real_table WHERE col = ?".
55 **
56 ** Member variable aCol[] contains copies of the column names of the real
57 ** table.
58 */
59 struct echo_vtab {
60   sqlite3_vtab base;
61   Tcl_Interp *interp;     /* Tcl interpreter containing debug variables */
62   sqlite3 *db;            /* Database connection */
63 
64   int isPattern;
65   char *zThis;            /* Name of the echo table */
66   char *zTableName;       /* Name of the real table */
67   char *zLogName;         /* Name of the log table */
68   int nCol;               /* Number of columns in the real table */
69   int *aIndex;            /* Array of size nCol. True if column has an index */
70   char **aCol;            /* Array of size nCol. Column names */
71 };
72 
73 /* An echo cursor object */
74 struct echo_cursor {
75   sqlite3_vtab_cursor base;
76   sqlite3_stmt *pStmt;
77 };
78 
79 /*
80 ** Convert an SQL-style quoted string into a normal string by removing
81 ** the quote characters.  The conversion is done in-place.  If the
82 ** input does not begin with a quote character, then this routine
83 ** is a no-op.
84 **
85 ** Examples:
86 **
87 **     "abc"   becomes   abc
88 **     'xyz'   becomes   xyz
89 **     [pqr]   becomes   pqr
90 **     `mno`   becomes   mno
91 */
92 static void dequoteString(char *z){
93   int quote;
94   int i, j;
95   if( z==0 ) return;
96   quote = z[0];
97   switch( quote ){
98     case '\'':  break;
99     case '"':   break;
100     case '`':   break;                /* For MySQL compatibility */
101     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
102     default:    return;
103   }
104   for(i=1, j=0; z[i]; i++){
105     if( z[i]==quote ){
106       if( z[i+1]==quote ){
107         z[j++] = quote;
108         i++;
109       }else{
110         z[j++] = 0;
111         break;
112       }
113     }else{
114       z[j++] = z[i];
115     }
116   }
117 }
118 
119 /*
120 ** Retrieve the column names for the table named zTab via database
121 ** connection db. SQLITE_OK is returned on success, or an sqlite error
122 ** code otherwise.
123 **
124 ** If successful, the number of columns is written to *pnCol. *paCol is
125 ** set to point at sqliteMalloc()'d space containing the array of
126 ** nCol column names. The caller is responsible for calling sqliteFree
127 ** on *paCol.
128 */
129 static int getColumnNames(
130   sqlite3 *db,
131   const char *zTab,
132   char ***paCol,
133   int *pnCol
134 ){
135   char **aCol = 0;
136   char *zSql;
137   sqlite3_stmt *pStmt = 0;
138   int rc = SQLITE_OK;
139   int nCol = 0;
140 
141   /* Prepare the statement "SELECT * FROM <tbl>". The column names
142   ** of the result set of the compiled SELECT will be the same as
143   ** the column names of table <tbl>.
144   */
145   zSql = sqlite3MPrintf("SELECT * FROM %Q", zTab);
146   if( !zSql ){
147     rc = SQLITE_NOMEM;
148     goto out;
149   }
150   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
151   sqliteFree(zSql);
152 
153   if( rc==SQLITE_OK ){
154     int ii;
155     int nBytes;
156     char *zSpace;
157     nCol = sqlite3_column_count(pStmt);
158 
159     /* Figure out how much space to allocate for the array of column names
160     ** (including space for the strings themselves). Then allocate it.
161     */
162     nBytes = sizeof(char *) * nCol;
163     for(ii=0; ii<nCol; ii++){
164       nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1);
165     }
166     aCol = (char **)sqliteMalloc(nBytes);
167     if( !aCol ){
168       rc = SQLITE_NOMEM;
169       goto out;
170     }
171 
172     /* Copy the column names into the allocated space and set up the
173     ** pointers in the aCol[] array.
174     */
175     zSpace = (char *)(&aCol[nCol]);
176     for(ii=0; ii<nCol; ii++){
177       aCol[ii] = zSpace;
178       zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
179       zSpace++;
180     }
181     assert( (zSpace-nBytes)==(char *)aCol );
182   }
183 
184   *paCol = aCol;
185   *pnCol = nCol;
186 
187 out:
188   sqlite3_finalize(pStmt);
189   return rc;
190 }
191 
192 /*
193 ** Parameter zTab is the name of a table in database db with nCol
194 ** columns. This function allocates an array of integers nCol in
195 ** size and populates it according to any implicit or explicit
196 ** indices on table zTab.
197 **
198 ** If successful, SQLITE_OK is returned and *paIndex set to point
199 ** at the allocated array. Otherwise, an error code is returned.
200 **
201 ** See comments associated with the member variable aIndex above
202 ** "struct echo_vtab" for details of the contents of the array.
203 */
204 static int getIndexArray(
205   sqlite3 *db,             /* Database connection */
206   const char *zTab,        /* Name of table in database db */
207   int nCol,
208   int **paIndex
209 ){
210   sqlite3_stmt *pStmt = 0;
211   int *aIndex = 0;
212   int rc;
213   char *zSql;
214 
215   /* Allocate space for the index array */
216   aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
217   if( !aIndex ){
218     rc = SQLITE_NOMEM;
219     goto get_index_array_out;
220   }
221 
222   /* Compile an sqlite pragma to loop through all indices on table zTab */
223   zSql = sqlite3MPrintf("PRAGMA index_list(%s)", zTab);
224   if( !zSql ){
225     rc = SQLITE_NOMEM;
226     goto get_index_array_out;
227   }
228   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
229   sqliteFree(zSql);
230 
231   /* For each index, figure out the left-most column and set the
232   ** corresponding entry in aIndex[] to 1.
233   */
234   while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
235     const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
236     sqlite3_stmt *pStmt2 = 0;
237     zSql = sqlite3MPrintf("PRAGMA index_info(%s)", zIdx);
238     if( !zSql ){
239       rc = SQLITE_NOMEM;
240       goto get_index_array_out;
241     }
242     rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
243     sqliteFree(zSql);
244     if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
245       int cid = sqlite3_column_int(pStmt2, 1);
246       assert( cid>=0 && cid<nCol );
247       aIndex[cid] = 1;
248     }
249     if( pStmt2 ){
250       rc = sqlite3_finalize(pStmt2);
251     }
252     if( rc!=SQLITE_OK ){
253       goto get_index_array_out;
254     }
255   }
256 
257 
258 get_index_array_out:
259   if( pStmt ){
260     int rc2 = sqlite3_finalize(pStmt);
261     if( rc==SQLITE_OK ){
262       rc = rc2;
263     }
264   }
265   if( rc!=SQLITE_OK ){
266     sqliteFree(aIndex);
267     aIndex = 0;
268   }
269   *paIndex = aIndex;
270   return rc;
271 }
272 
273 /*
274 ** Global Tcl variable $echo_module is a list. This routine appends
275 ** the string element zArg to that list in interpreter interp.
276 */
277 static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
278   int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
279   Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
280 }
281 
282 /*
283 ** This function is called from within the echo-modules xCreate and
284 ** xConnect methods. The argc and argv arguments are copies of those
285 ** passed to the calling method. This function is responsible for
286 ** calling sqlite3_declare_vtab() to declare the schema of the virtual
287 ** table being created or connected.
288 **
289 ** If the constructor was passed just one argument, i.e.:
290 **
291 **   CREATE TABLE t1 AS echo(t2);
292 **
293 ** Then t2 is assumed to be the name of a *real* database table. The
294 ** schema of the virtual table is declared by passing a copy of the
295 ** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
296 ** Hence, the virtual table should have exactly the same column names and
297 ** types as the real table.
298 */
299 static int echoDeclareVtab(
300   echo_vtab *pVtab,
301   sqlite3 *db
302 ){
303   int rc = SQLITE_OK;
304 
305   if( pVtab->zTableName ){
306     sqlite3_stmt *pStmt = 0;
307     sqlite3_prepare(db,
308         "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
309         -1, &pStmt, 0);
310     sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
311     if( sqlite3_step(pStmt)==SQLITE_ROW ){
312       int rc2;
313       const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
314       rc = sqlite3_declare_vtab(db, zCreateTable);
315       rc2 = sqlite3_finalize(pStmt);
316       if( rc==SQLITE_OK ){
317         rc = rc2;
318       }
319     } else {
320       rc = sqlite3_finalize(pStmt);
321       if( rc==SQLITE_OK ){
322         rc = SQLITE_ERROR;
323       }
324     }
325     if( rc==SQLITE_OK ){
326       rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
327     }
328     if( rc==SQLITE_OK ){
329       rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
330     }
331   }
332 
333   return rc;
334 }
335 
336 /*
337 ** This function frees all runtime structures associated with the virtual
338 ** table pVtab.
339 */
340 static int echoDestructor(sqlite3_vtab *pVtab){
341   echo_vtab *p = (echo_vtab*)pVtab;
342   sqliteFree(p->aIndex);
343   sqliteFree(p->aCol);
344   sqliteFree(p->zThis);
345   sqliteFree(p->zTableName);
346   sqliteFree(p->zLogName);
347   sqliteFree(p);
348   return 0;
349 }
350 
351 /*
352 ** This function is called to do the work of the xConnect() method -
353 ** to allocate the required in-memory structures for a newly connected
354 ** virtual table.
355 */
356 static int echoConstructor(
357   sqlite3 *db,
358   void *pAux,
359   int argc, const char *const*argv,
360   sqlite3_vtab **ppVtab,
361   char **pzErr
362 ){
363   int i;
364   echo_vtab *pVtab;
365 
366   /* Allocate the sqlite3_vtab/echo_vtab structure itself */
367   pVtab = sqliteMalloc( sizeof(*pVtab) );
368   if( !pVtab ){
369     return SQLITE_NOMEM;
370   }
371   pVtab->interp = (Tcl_Interp *)pAux;
372   pVtab->db = db;
373 
374   /* Allocate echo_vtab.zThis */
375   pVtab->zThis = sqlite3MPrintf("%s", argv[2]);
376   if( !pVtab->zThis ){
377     echoDestructor((sqlite3_vtab *)pVtab);
378     return SQLITE_NOMEM;
379   }
380 
381   /* Allocate echo_vtab.zTableName */
382   if( argc>3 ){
383     pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
384     dequoteString(pVtab->zTableName);
385     if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
386       char *z = sqlite3MPrintf("%s%s", argv[2], &(pVtab->zTableName[1]));
387       sqliteFree(pVtab->zTableName);
388       pVtab->zTableName = z;
389       pVtab->isPattern = 1;
390     }
391     if( !pVtab->zTableName ){
392       echoDestructor((sqlite3_vtab *)pVtab);
393       return SQLITE_NOMEM;
394     }
395   }
396 
397   /* Log the arguments to this function to Tcl var ::echo_module */
398   for(i=0; i<argc; i++){
399     appendToEchoModule(pVtab->interp, argv[i]);
400   }
401 
402   /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
403   ** structure. If an error occurs, delete the sqlite3_vtab structure and
404   ** return an error code.
405   */
406   if( echoDeclareVtab(pVtab, db) ){
407     echoDestructor((sqlite3_vtab *)pVtab);
408     return SQLITE_ERROR;
409   }
410 
411   /* Success. Set *ppVtab and return */
412   *ppVtab = &pVtab->base;
413   return SQLITE_OK;
414 }
415 
416 /*
417 ** Echo virtual table module xCreate method.
418 */
419 static int echoCreate(
420   sqlite3 *db,
421   void *pAux,
422   int argc, const char *const*argv,
423   sqlite3_vtab **ppVtab,
424   char **pzErr
425 ){
426   int rc = SQLITE_OK;
427   appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
428   rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
429 
430   /* If there were two arguments passed to the module at the SQL level
431   ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
432   ** the second argument is used as a table name. Attempt to create
433   ** such a table with a single column, "logmsg". This table will
434   ** be used to log calls to the xUpdate method. It will be deleted
435   ** when the virtual table is DROPed.
436   **
437   ** Note: The main point of this is to test that we can drop tables
438   ** from within an xDestroy method call.
439   */
440   if( rc==SQLITE_OK && argc==5 ){
441     char *zSql;
442     echo_vtab *pVtab = *(echo_vtab **)ppVtab;
443     pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
444     zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
445     rc = sqlite3_exec(db, zSql, 0, 0, 0);
446     sqliteFree(zSql);
447   }
448 
449   return rc;
450 }
451 
452 /*
453 ** Echo virtual table module xConnect method.
454 */
455 static int echoConnect(
456   sqlite3 *db,
457   void *pAux,
458   int argc, const char *const*argv,
459   sqlite3_vtab **ppVtab,
460   char **pzErr
461 ){
462   appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
463   return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
464 }
465 
466 /*
467 ** Echo virtual table module xDisconnect method.
468 */
469 static int echoDisconnect(sqlite3_vtab *pVtab){
470   appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
471   return echoDestructor(pVtab);
472 }
473 
474 /*
475 ** Echo virtual table module xDestroy method.
476 */
477 static int echoDestroy(sqlite3_vtab *pVtab){
478   int rc = SQLITE_OK;
479   echo_vtab *p = (echo_vtab *)pVtab;
480   appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
481 
482   /* Drop the "log" table, if one exists (see echoCreate() for details) */
483   if( p && p->zLogName ){
484     char *zSql;
485     zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName);
486     rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
487     sqliteFree(zSql);
488   }
489 
490   if( rc==SQLITE_OK ){
491     rc = echoDestructor(pVtab);
492   }
493   return rc;
494 }
495 
496 /*
497 ** Echo virtual table module xOpen method.
498 */
499 static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
500   echo_cursor *pCur;
501   pCur = sqliteMalloc(sizeof(echo_cursor));
502   *ppCursor = (sqlite3_vtab_cursor *)pCur;
503   return (pCur ? SQLITE_OK : SQLITE_NOMEM);
504 }
505 
506 /*
507 ** Echo virtual table module xClose method.
508 */
509 static int echoClose(sqlite3_vtab_cursor *cur){
510   int rc;
511   echo_cursor *pCur = (echo_cursor *)cur;
512   sqlite3_stmt *pStmt = pCur->pStmt;
513   pCur->pStmt = 0;
514   sqliteFree(pCur);
515   rc = sqlite3_finalize(pStmt);
516   return rc;
517 }
518 
519 /*
520 ** Return non-zero if the cursor does not currently point to a valid record
521 ** (i.e if the scan has finished), or zero otherwise.
522 */
523 static int echoEof(sqlite3_vtab_cursor *cur){
524   return (((echo_cursor *)cur)->pStmt ? 0 : 1);
525 }
526 
527 /*
528 ** Echo virtual table module xNext method.
529 */
530 static int echoNext(sqlite3_vtab_cursor *cur){
531   int rc;
532   echo_cursor *pCur = (echo_cursor *)cur;
533   rc = sqlite3_step(pCur->pStmt);
534 
535   if( rc==SQLITE_ROW ){
536     rc = SQLITE_OK;
537   }else{
538     rc = sqlite3_finalize(pCur->pStmt);
539     pCur->pStmt = 0;
540   }
541 
542   return rc;
543 }
544 
545 /*
546 ** Echo virtual table module xColumn method.
547 */
548 static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
549   int iCol = i + 1;
550   sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
551   if( !pStmt ){
552     sqlite3_result_null(ctx);
553   }else{
554     assert( sqlite3_data_count(pStmt)>iCol );
555     sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
556   }
557   return SQLITE_OK;
558 }
559 
560 /*
561 ** Echo virtual table module xRowid method.
562 */
563 static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
564   sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
565   *pRowid = sqlite3_column_int64(pStmt, 0);
566   return SQLITE_OK;
567 }
568 
569 /*
570 ** Compute a simple hash of the null terminated string zString.
571 **
572 ** This module uses only sqlite3_index_info.idxStr, not
573 ** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
574 ** in echoBestIndex(), idxNum is set to the corresponding hash value.
575 ** In echoFilter(), code assert()s that the supplied idxNum value is
576 ** indeed the hash of the supplied idxStr.
577 */
578 static int hashString(const char *zString){
579   int val = 0;
580   int ii;
581   for(ii=0; zString[ii]; ii++){
582     val = (val << 3) + (int)zString[ii];
583   }
584   return val;
585 }
586 
587 /*
588 ** Echo virtual table module xFilter method.
589 */
590 static int echoFilter(
591   sqlite3_vtab_cursor *pVtabCursor,
592   int idxNum, const char *idxStr,
593   int argc, sqlite3_value **argv
594 ){
595   int rc;
596   int i;
597 
598   echo_cursor *pCur = (echo_cursor *)pVtabCursor;
599   echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
600   sqlite3 *db = pVtab->db;
601 
602   /* Check that idxNum matches idxStr */
603   assert( idxNum==hashString(idxStr) );
604 
605   /* Log arguments to the ::echo_module Tcl variable */
606   appendToEchoModule(pVtab->interp, "xFilter");
607   appendToEchoModule(pVtab->interp, idxStr);
608   for(i=0; i<argc; i++){
609     appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
610   }
611 
612   sqlite3_finalize(pCur->pStmt);
613   pCur->pStmt = 0;
614 
615   /* Prepare the SQL statement created by echoBestIndex and bind the
616   ** runtime parameters passed to this function to it.
617   */
618   rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
619   assert( pCur->pStmt || rc!=SQLITE_OK );
620   for(i=0; rc==SQLITE_OK && i<argc; i++){
621     sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
622   }
623 
624   /* If everything was successful, advance to the first row of the scan */
625   if( rc==SQLITE_OK ){
626     rc = echoNext(pVtabCursor);
627   }
628 
629   return rc;
630 }
631 
632 
633 /*
634 ** A helper function used by echoUpdate() and echoBestIndex() for
635 ** manipulating strings in concert with the sqlite3_mprintf() function.
636 **
637 ** Parameter pzStr points to a pointer to a string allocated with
638 ** sqlite3_mprintf. The second parameter, zAppend, points to another
639 ** string. The two strings are concatenated together and *pzStr
640 ** set to point at the result. The initial buffer pointed to by *pzStr
641 ** is deallocated via sqlite3_free().
642 **
643 ** If the third argument, doFree, is true, then sqlite3_free() is
644 ** also called to free the buffer pointed to by zAppend.
645 */
646 static void string_concat(char **pzStr, char *zAppend, int doFree){
647   char *zIn = *pzStr;
648   if( zIn ){
649     char *zTemp = zIn;
650     zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
651     sqlite3_free(zTemp);
652   }else{
653     zIn = sqlite3_mprintf("%s", zAppend);
654   }
655   *pzStr = zIn;
656   if( doFree ){
657     sqlite3_free(zAppend);
658   }
659 }
660 
661 /*
662 ** The echo module implements the subset of query constraints and sort
663 ** orders that may take advantage of SQLite indices on the underlying
664 ** real table. For example, if the real table is declared as:
665 **
666 **     CREATE TABLE real(a, b, c);
667 **     CREATE INDEX real_index ON real(b);
668 **
669 ** then the echo module handles WHERE or ORDER BY clauses that refer
670 ** to the column "b", but not "a" or "c". If a multi-column index is
671 ** present, only it's left most column is considered.
672 **
673 ** This xBestIndex method encodes the proposed search strategy as
674 ** an SQL query on the real table underlying the virtual echo module
675 ** table and stores the query in sqlite3_index_info.idxStr. The SQL
676 ** statement is of the form:
677 **
678 **   SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
679 **
680 ** where the <where-clause> and <order-by-clause> are determined
681 ** by the contents of the structure pointed to by the pIdxInfo argument.
682 */
683 static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
684   int ii;
685   char *zQuery = 0;
686   char *zNew;
687   int nArg = 0;
688   const char *zSep = "WHERE";
689   echo_vtab *pVtab = (echo_vtab *)tab;
690   sqlite3_stmt *pStmt = 0;
691   Tcl_Interp *interp = pVtab->interp;
692 
693   int nRow;
694   int useIdx = 0;
695   int rc = SQLITE_OK;
696   int useCost = 0;
697   double cost;
698 
699   /* Determine the number of rows in the table and store this value in local
700   ** variable nRow. The 'estimated-cost' of the scan will be the number of
701   ** rows in the table for a linear scan, or the log (base 2) of the
702   ** number of rows if the proposed scan uses an index.
703   */
704   if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
705     cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
706     useCost = 1;
707   } else {
708     zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
709     rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
710     sqlite3_free(zQuery);
711     if( rc!=SQLITE_OK ){
712       return rc;
713     }
714     sqlite3_step(pStmt);
715     nRow = sqlite3_column_int(pStmt, 0);
716     rc = sqlite3_finalize(pStmt);
717     if( rc!=SQLITE_OK ){
718       return rc;
719     }
720   }
721 
722   zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
723   for(ii=0; ii<pIdxInfo->nConstraint; ii++){
724     const struct sqlite3_index_constraint *pConstraint;
725     struct sqlite3_index_constraint_usage *pUsage;
726     int iCol;
727 
728     pConstraint = &pIdxInfo->aConstraint[ii];
729     pUsage = &pIdxInfo->aConstraintUsage[ii];
730 
731     iCol = pConstraint->iColumn;
732     if( pVtab->aIndex[iCol] ){
733       char *zCol = pVtab->aCol[iCol];
734       char *zOp = 0;
735       useIdx = 1;
736       if( iCol<0 ){
737         zCol = "rowid";
738       }
739       switch( pConstraint->op ){
740         case SQLITE_INDEX_CONSTRAINT_EQ:
741           zOp = "="; break;
742         case SQLITE_INDEX_CONSTRAINT_LT:
743           zOp = "<"; break;
744         case SQLITE_INDEX_CONSTRAINT_GT:
745           zOp = ">"; break;
746         case SQLITE_INDEX_CONSTRAINT_LE:
747           zOp = "<="; break;
748         case SQLITE_INDEX_CONSTRAINT_GE:
749           zOp = ">="; break;
750         case SQLITE_INDEX_CONSTRAINT_MATCH:
751           zOp = "LIKE"; break;
752       }
753       if( zOp[0]=='L' ){
754         zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
755                                zSep, zCol);
756       } else {
757         zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
758       }
759       string_concat(&zQuery, zNew, 1);
760 
761       zSep = "AND";
762       pUsage->argvIndex = ++nArg;
763       pUsage->omit = 1;
764     }
765   }
766 
767   /* If there is only one term in the ORDER BY clause, and it is
768   ** on a column that this virtual table has an index for, then consume
769   ** the ORDER BY clause.
770   */
771   if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
772     int iCol = pIdxInfo->aOrderBy->iColumn;
773     char *zCol = pVtab->aCol[iCol];
774     char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
775     if( iCol<0 ){
776       zCol = "rowid";
777     }
778     zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
779     string_concat(&zQuery, zNew, 1);
780     pIdxInfo->orderByConsumed = 1;
781   }
782 
783   appendToEchoModule(pVtab->interp, "xBestIndex");;
784   appendToEchoModule(pVtab->interp, zQuery);
785 
786   pIdxInfo->idxNum = hashString(zQuery);
787   pIdxInfo->idxStr = zQuery;
788   pIdxInfo->needToFreeIdxStr = 1;
789   if (useCost) {
790     pIdxInfo->estimatedCost = cost;
791   } else if( useIdx ){
792     /* Approximation of log2(nRow). */
793     for( ii=0; ii<(sizeof(int)*8); ii++ ){
794       if( nRow & (1<<ii) ){
795         pIdxInfo->estimatedCost = (double)ii;
796       }
797     }
798   } else {
799     pIdxInfo->estimatedCost = (double)nRow;
800   }
801   return rc;
802 }
803 
804 /*
805 ** The xUpdate method for echo module virtual tables.
806 **
807 **    apData[0]  apData[1]  apData[2..]
808 **
809 **    INTEGER                              DELETE
810 **
811 **    INTEGER    NULL       (nCol args)    UPDATE (do not set rowid)
812 **    INTEGER    INTEGER    (nCol args)    UPDATE (with SET rowid = <arg1>)
813 **
814 **    NULL       NULL       (nCol args)    INSERT INTO (automatic rowid value)
815 **    NULL       INTEGER    (nCol args)    INSERT (incl. rowid value)
816 **
817 */
818 int echoUpdate(
819   sqlite3_vtab *tab,
820   int nData,
821   sqlite3_value **apData,
822   sqlite_int64 *pRowid
823 ){
824   echo_vtab *pVtab = (echo_vtab *)tab;
825   sqlite3 *db = pVtab->db;
826   int rc = SQLITE_OK;
827 
828   sqlite3_stmt *pStmt;
829   char *z = 0;               /* SQL statement to execute */
830   int bindArgZero = 0;       /* True to bind apData[0] to sql var no. nData */
831   int bindArgOne = 0;        /* True to bind apData[1] to sql var no. 1 */
832   int i;                     /* Counter variable used by for loops */
833 
834   assert( nData==pVtab->nCol+2 || nData==1 );
835 
836   /* If apData[0] is an integer and nData>1 then do an UPDATE */
837   if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
838     char *zSep = " SET";
839     z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
840 
841     bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
842     bindArgZero = 1;
843 
844     if( bindArgOne ){
845        string_concat(&z, " SET rowid=?1 ", 0);
846        zSep = ",";
847     }
848     for(i=2; i<nData; i++){
849       if( apData[i]==0 ) continue;
850       string_concat(&z, sqlite3_mprintf(
851           "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
852       zSep = ",";
853     }
854     string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
855   }
856 
857   /* If apData[0] is an integer and nData==1 then do a DELETE */
858   else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
859     z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
860     bindArgZero = 1;
861   }
862 
863   /* If the first argument is NULL and there are more than two args, INSERT */
864   else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
865     int ii;
866     char *zInsert = 0;
867     char *zValues = 0;
868 
869     zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
870     if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
871       bindArgOne = 1;
872       zValues = sqlite3_mprintf("?");
873       string_concat(&zInsert, "rowid", 0);
874     }
875 
876     assert((pVtab->nCol+2)==nData);
877     for(ii=2; ii<nData; ii++){
878       string_concat(&zInsert,
879           sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
880       string_concat(&zValues,
881           sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
882     }
883 
884     string_concat(&z, zInsert, 1);
885     string_concat(&z, ") VALUES(", 0);
886     string_concat(&z, zValues, 1);
887     string_concat(&z, ")", 0);
888   }
889 
890   /* Anything else is an error */
891   else{
892     assert(0);
893     return SQLITE_ERROR;
894   }
895 
896   rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
897   assert( rc!=SQLITE_OK || pStmt );
898   sqlite3_free(z);
899   if( rc==SQLITE_OK ) {
900     if( bindArgZero ){
901       sqlite3_bind_value(pStmt, nData, apData[0]);
902     }
903     if( bindArgOne ){
904       sqlite3_bind_value(pStmt, 1, apData[1]);
905     }
906     for(i=2; i<nData; i++){
907       if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
908     }
909     sqlite3_step(pStmt);
910     rc = sqlite3_finalize(pStmt);
911   }
912 
913   if( pRowid && rc==SQLITE_OK ){
914     *pRowid = sqlite3_last_insert_rowid(db);
915   }
916 
917   return rc;
918 }
919 
920 /*
921 ** xBegin, xSync, xCommit and xRollback callbacks for echo module
922 ** virtual tables. Do nothing other than add the name of the callback
923 ** to the $::echo_module Tcl variable.
924 */
925 static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
926   char *z;
927   echo_vtab *pVtab = (echo_vtab *)tab;
928   z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
929   appendToEchoModule(pVtab->interp, zCall);
930   appendToEchoModule(pVtab->interp, z);
931   sqlite3_free(z);
932   return SQLITE_OK;
933 }
934 static int echoBegin(sqlite3_vtab *tab){
935   echo_vtab *pVtab = (echo_vtab *)tab;
936   Tcl_Interp *interp = pVtab->interp;
937   const char *zVal;
938 
939   echoTransactionCall(tab, "xBegin");
940 
941   /* Check if the $::echo_module_begin_fail variable is defined. If it is,
942   ** and it is set to the name of the real table underlying this virtual
943   ** echo module table, then cause this xSync operation to fail.
944   */
945   zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
946   if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
947     return SQLITE_ERROR;
948   }
949   return SQLITE_OK;
950 }
951 static int echoSync(sqlite3_vtab *tab){
952   echo_vtab *pVtab = (echo_vtab *)tab;
953   Tcl_Interp *interp = pVtab->interp;
954   const char *zVal;
955 
956   echoTransactionCall(tab, "xSync");
957 
958   /* Check if the $::echo_module_sync_fail variable is defined. If it is,
959   ** and it is set to the name of the real table underlying this virtual
960   ** echo module table, then cause this xSync operation to fail.
961   */
962   zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
963   if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
964     return -1;
965   }
966   return SQLITE_OK;
967 }
968 static int echoCommit(sqlite3_vtab *tab){
969   return echoTransactionCall(tab, "xCommit");
970 }
971 static int echoRollback(sqlite3_vtab *tab){
972   return echoTransactionCall(tab, "xRollback");
973 }
974 
975 /*
976 ** Implementation of "GLOB" function on the echo module.  Pass
977 ** all arguments to the ::echo_glob_overload procedure of TCL
978 ** and return the result of that procedure as a string.
979 */
980 static void overloadedGlobFunction(
981   sqlite3_context *pContext,
982   int nArg,
983   sqlite3_value **apArg
984 ){
985   Tcl_Interp *interp = sqlite3_user_data(pContext);
986   Tcl_DString str;
987   int i;
988   int rc;
989   Tcl_DStringInit(&str);
990   Tcl_DStringAppendElement(&str, "::echo_glob_overload");
991   for(i=0; i<nArg; i++){
992     Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
993   }
994   rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
995   Tcl_DStringFree(&str);
996   if( rc ){
997     sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
998   }else{
999     sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1000                         -1, SQLITE_TRANSIENT);
1001   }
1002   Tcl_ResetResult(interp);
1003 }
1004 
1005 /*
1006 ** This is the xFindFunction implementation for the echo module.
1007 ** SQLite calls this routine when the first argument of a function
1008 ** is a column of an echo virtual table.  This routine can optionally
1009 ** override the implementation of that function.  It will choose to
1010 ** do so if the function is named "glob", and a TCL command named
1011 ** ::echo_glob_overload exists.
1012 */
1013 static int echoFindFunction(
1014   sqlite3_vtab *vtab,
1015   int nArg,
1016   const char *zFuncName,
1017   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1018   void **ppArg
1019 ){
1020   echo_vtab *pVtab = (echo_vtab *)vtab;
1021   Tcl_Interp *interp = pVtab->interp;
1022   Tcl_CmdInfo info;
1023   if( strcmp(zFuncName,"glob")!=0 ){
1024     return 0;
1025   }
1026   if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1027     return 0;
1028   }
1029   *pxFunc = overloadedGlobFunction;
1030   *ppArg = interp;
1031   return 1;
1032 }
1033 
1034 static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1035   int rc = SQLITE_OK;
1036   echo_vtab *p = (echo_vtab *)vtab;
1037 
1038   if( p->isPattern ){
1039     int nThis = strlen(p->zThis);
1040     char *zSql = sqlite3MPrintf("ALTER TABLE %s RENAME TO %s%s",
1041         p->zTableName, zNewName, &p->zTableName[nThis]
1042     );
1043     rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
1044     sqliteFree(zSql);
1045   }
1046 
1047   return rc;
1048 }
1049 
1050 /*
1051 ** A virtual table module that merely "echos" the contents of another
1052 ** table (like an SQL VIEW).
1053 */
1054 static sqlite3_module echoModule = {
1055   0,                         /* iVersion */
1056   echoCreate,
1057   echoConnect,
1058   echoBestIndex,
1059   echoDisconnect,
1060   echoDestroy,
1061   echoOpen,                  /* xOpen - open a cursor */
1062   echoClose,                 /* xClose - close a cursor */
1063   echoFilter,                /* xFilter - configure scan constraints */
1064   echoNext,                  /* xNext - advance a cursor */
1065   echoEof,                   /* xEof */
1066   echoColumn,                /* xColumn - read data */
1067   echoRowid,                 /* xRowid - read data */
1068   echoUpdate,                /* xUpdate - write data */
1069   echoBegin,                 /* xBegin - begin transaction */
1070   echoSync,                  /* xSync - sync transaction */
1071   echoCommit,                /* xCommit - commit transaction */
1072   echoRollback,              /* xRollback - rollback transaction */
1073   echoFindFunction,          /* xFindFunction - function overloading */
1074   echoRename,                /* xRename - rename the table */
1075 };
1076 
1077 /*
1078 ** Decode a pointer to an sqlite3 object.
1079 */
1080 static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
1081   *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
1082   return TCL_OK;
1083 }
1084 
1085 /*
1086 ** Register the echo virtual table module.
1087 */
1088 static int register_echo_module(
1089   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1090   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1091   int objc,              /* Number of arguments */
1092   Tcl_Obj *CONST objv[]  /* Command arguments */
1093 ){
1094   sqlite3 *db;
1095   if( objc!=2 ){
1096     Tcl_WrongNumArgs(interp, 1, objv, "DB");
1097     return TCL_ERROR;
1098   }
1099   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1100   sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
1101   return TCL_OK;
1102 }
1103 
1104 /*
1105 ** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1106 **
1107 ** sqlite3_declare_vtab DB SQL
1108 */
1109 static int declare_vtab(
1110   ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1111   Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
1112   int objc,              /* Number of arguments */
1113   Tcl_Obj *CONST objv[]  /* Command arguments */
1114 ){
1115   sqlite3 *db;
1116   int rc;
1117   if( objc!=3 ){
1118     Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1119     return TCL_ERROR;
1120   }
1121   if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1122   rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1123   if( rc!=SQLITE_OK ){
1124     Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
1125     return TCL_ERROR;
1126   }
1127   return TCL_OK;
1128 }
1129 
1130 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
1131 
1132 /*
1133 ** Register commands with the TCL interpreter.
1134 */
1135 int Sqlitetest8_Init(Tcl_Interp *interp){
1136   static struct {
1137      char *zName;
1138      Tcl_ObjCmdProc *xProc;
1139      void *clientData;
1140   } aObjCmd[] = {
1141 #ifndef SQLITE_OMIT_VIRTUALTABLE
1142      { "register_echo_module",   register_echo_module, 0 },
1143      { "sqlite3_declare_vtab",   declare_vtab, 0 },
1144 #endif
1145   };
1146   int i;
1147   for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1148     Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1149         aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1150   }
1151   return TCL_OK;
1152 }
1153