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