blob: a0749cb22f3cf4d02d7969ddfedaede410cd0820 [file] [log] [blame]
drhb9bb7c12006-06-11 23:41:55 +00001/*
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**
danielk197739359dc2008-03-17 09:36:44 +000016** $Id: test8.c,v 1.61 2008/03/17 09:36:45 danielk1977 Exp $
drhb9bb7c12006-06-11 23:41:55 +000017*/
18#include "sqliteInt.h"
19#include "tcl.h"
drhb9bb7c12006-06-11 23:41:55 +000020#include <stdlib.h>
21#include <string.h>
22
danielk1977cc013f82006-06-24 06:36:11 +000023#ifndef SQLITE_OMIT_VIRTUALTABLE
24
danielk1977b7a7b9a2006-06-13 10:24:42 +000025typedef struct echo_vtab echo_vtab;
26typedef struct echo_cursor echo_cursor;
27
danielk1977c69cdfd2006-06-17 09:39:55 +000028/*
danielk1977780b1d92007-03-30 14:56:34 +000029** The test module defined in this file uses four global Tcl variables to
danielk1977c69cdfd2006-06-17 09:39:55 +000030** commicate with test-scripts:
31**
32** $::echo_module
33** $::echo_module_sync_fail
danielk19775017dc32006-06-24 09:34:22 +000034** $::echo_module_begin_fail
danielk1977780b1d92007-03-30 14:56:34 +000035** $::echo_module_cost
danielk1977cc013f82006-06-24 06:36:11 +000036**
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.
danielk1977c69cdfd2006-06-17 09:39:55 +000045*/
46
danielk19775fac9f82006-06-13 14:16:58 +000047/*
danielk1977d6e8dd02006-06-15 15:38:41 +000048** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:58 +000049**
danielk1977d6e8dd02006-06-15 15:38:41 +000050** 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**
danielk1977c69cdfd2006-06-17 09:39:55 +000055** Member variable aCol[] contains copies of the column names of the real
56** table.
danielk19775fac9f82006-06-13 14:16:58 +000057*/
danielk1977b7a7b9a2006-06-13 10:24:42 +000058struct echo_vtab {
59 sqlite3_vtab base;
danielk1977cc013f82006-06-24 06:36:11 +000060 Tcl_Interp *interp; /* Tcl interpreter containing debug variables */
61 sqlite3 *db; /* Database connection */
danielk19775fac9f82006-06-13 14:16:58 +000062
danielk1977182c4ba2007-06-27 15:53:34 +000063 int isPattern;
64 char *zThis; /* Name of the echo table */
danielk1977a4e76362006-06-14 06:31:28 +000065 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:48 +000066 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:28 +000067 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 */
danielk1977b7a7b9a2006-06-13 10:24:42 +000070};
71
72/* An echo cursor object */
73struct echo_cursor {
74 sqlite3_vtab_cursor base;
75 sqlite3_stmt *pStmt;
danielk1977b7a7b9a2006-06-13 10:24:42 +000076};
77
danielk1977cc013f82006-06-24 06:36:11 +000078/*
danielk1977182c4ba2007-06-27 15:53:34 +000079** 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*/
91static 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/*
danielk1977cc013f82006-06-24 06:36:11 +0000119** 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
drh17435752007-08-16 04:30:38 +0000124** set to point at sqlite3_malloc()'d space containing the array of
125** nCol column names. The caller is responsible for calling sqlite3_free
danielk1977cc013f82006-06-24 06:36:11 +0000126** on *paCol.
127*/
danielk19775fac9f82006-06-13 14:16:58 +0000128static int getColumnNames(
129 sqlite3 *db,
130 const char *zTab,
131 char ***paCol,
132 int *pnCol
133){
134 char **aCol = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000135 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000136 sqlite3_stmt *pStmt = 0;
137 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19 +0000138 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000139
danielk1977cc013f82006-06-24 06:36:11 +0000140 /* 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 */
drh17435752007-08-16 04:30:38 +0000144 zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000145 if( !zSql ){
146 rc = SQLITE_NOMEM;
147 goto out;
148 }
149 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000150 sqlite3_free(zSql);
danielk1977cc013f82006-06-24 06:36:11 +0000151
danielk19775fac9f82006-06-13 14:16:58 +0000152 if( rc==SQLITE_OK ){
153 int ii;
danielk1977cc013f82006-06-24 06:36:11 +0000154 int nBytes;
155 char *zSpace;
danielk19775fac9f82006-06-13 14:16:58 +0000156 nCol = sqlite3_column_count(pStmt);
danielk1977cc013f82006-06-24 06:36:11 +0000157
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++){
danielk1977a7a8e142008-02-13 18:25:27 +0000163 const char *zName = sqlite3_column_name(pStmt, ii);
164 if( !zName ){
165 rc = SQLITE_NOMEM;
166 goto out;
167 }
168 nBytes += strlen(zName)+1;
danielk1977cc013f82006-06-24 06:36:11 +0000169 }
danielk197731dad9d2007-08-16 11:36:15 +0000170 aCol = (char **)sqlite3MallocZero(nBytes);
danielk19775fac9f82006-06-13 14:16:58 +0000171 if( !aCol ){
172 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11 +0000173 goto out;
danielk19775fac9f82006-06-13 14:16:58 +0000174 }
danielk1977cc013f82006-06-24 06:36:11 +0000175
176 /* Copy the column names into the allocated space and set up the
177 ** pointers in the aCol[] array.
178 */
179 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58 +0000180 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11 +0000181 aCol[ii] = zSpace;
182 zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
183 zSpace++;
danielk19775fac9f82006-06-13 14:16:58 +0000184 }
danielk1977cc013f82006-06-24 06:36:11 +0000185 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58 +0000186 }
187
188 *paCol = aCol;
189 *pnCol = nCol;
190
danielk1977cc013f82006-06-24 06:36:11 +0000191out:
danielk19775fac9f82006-06-13 14:16:58 +0000192 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000193 return rc;
194}
195
danielk1977cc013f82006-06-24 06:36:11 +0000196/*
197** Parameter zTab is the name of a table in database db with nCol
198** columns. This function allocates an array of integers nCol in
199** size and populates it according to any implicit or explicit
200** indices on table zTab.
201**
202** If successful, SQLITE_OK is returned and *paIndex set to point
203** at the allocated array. Otherwise, an error code is returned.
204**
205** See comments associated with the member variable aIndex above
206** "struct echo_vtab" for details of the contents of the array.
207*/
208static int getIndexArray(
209 sqlite3 *db, /* Database connection */
210 const char *zTab, /* Name of table in database db */
211 int nCol,
212 int **paIndex
213){
danielk19775fac9f82006-06-13 14:16:58 +0000214 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000215 int *aIndex = 0;
216 int rc;
danielk1977cc013f82006-06-24 06:36:11 +0000217 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000218
danielk1977cc013f82006-06-24 06:36:11 +0000219 /* Allocate space for the index array */
danielk197731dad9d2007-08-16 11:36:15 +0000220 aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000221 if( !aIndex ){
222 rc = SQLITE_NOMEM;
223 goto get_index_array_out;
224 }
225
danielk1977cc013f82006-06-24 06:36:11 +0000226 /* Compile an sqlite pragma to loop through all indices on table zTab */
danielk19771e536952007-08-16 10:09:01 +0000227 zSql = sqlite3MPrintf(0, "PRAGMA index_list(%s)", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000228 if( !zSql ){
229 rc = SQLITE_NOMEM;
230 goto get_index_array_out;
231 }
232 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000233 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000234
danielk1977cc013f82006-06-24 06:36:11 +0000235 /* For each index, figure out the left-most column and set the
236 ** corresponding entry in aIndex[] to 1.
237 */
danielk19775fac9f82006-06-13 14:16:58 +0000238 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000239 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58 +0000240 sqlite3_stmt *pStmt2 = 0;
danielk19771e536952007-08-16 10:09:01 +0000241 zSql = sqlite3MPrintf(0, "PRAGMA index_info(%s)", zIdx);
danielk1977cc013f82006-06-24 06:36:11 +0000242 if( !zSql ){
243 rc = SQLITE_NOMEM;
244 goto get_index_array_out;
245 }
246 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
drh17435752007-08-16 04:30:38 +0000247 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000248 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
249 int cid = sqlite3_column_int(pStmt2, 1);
250 assert( cid>=0 && cid<nCol );
251 aIndex[cid] = 1;
252 }
danielk1977be718892006-06-23 08:05:19 +0000253 if( pStmt2 ){
254 rc = sqlite3_finalize(pStmt2);
255 }
danielk19775fac9f82006-06-13 14:16:58 +0000256 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000257 goto get_index_array_out;
258 }
259 }
260
danielk19775fac9f82006-06-13 14:16:58 +0000261
262get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000263 if( pStmt ){
264 int rc2 = sqlite3_finalize(pStmt);
265 if( rc==SQLITE_OK ){
266 rc = rc2;
267 }
268 }
danielk19775fac9f82006-06-13 14:16:58 +0000269 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000270 sqlite3_free(aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000271 aIndex = 0;
272 }
273 *paIndex = aIndex;
274 return rc;
275}
276
danielk197778efaba2006-06-12 06:09:17 +0000277/*
278** Global Tcl variable $echo_module is a list. This routine appends
279** the string element zArg to that list in interpreter interp.
280*/
drha967e882006-06-13 01:04:52 +0000281static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000282 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000283 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000284}
285
danielk19777e6ebfb2006-06-12 11:24:37 +0000286/*
287** This function is called from within the echo-modules xCreate and
288** xConnect methods. The argc and argv arguments are copies of those
289** passed to the calling method. This function is responsible for
290** calling sqlite3_declare_vtab() to declare the schema of the virtual
291** table being created or connected.
292**
293** If the constructor was passed just one argument, i.e.:
294**
295** CREATE TABLE t1 AS echo(t2);
296**
297** Then t2 is assumed to be the name of a *real* database table. The
298** schema of the virtual table is declared by passing a copy of the
299** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
300** Hence, the virtual table should have exactly the same column names and
301** types as the real table.
302*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000303static int echoDeclareVtab(
304 echo_vtab *pVtab,
danielk1977182c4ba2007-06-27 15:53:34 +0000305 sqlite3 *db
danielk1977b7a7b9a2006-06-13 10:24:42 +0000306){
danielk19777e6ebfb2006-06-12 11:24:37 +0000307 int rc = SQLITE_OK;
308
danielk1977182c4ba2007-06-27 15:53:34 +0000309 if( pVtab->zTableName ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000310 sqlite3_stmt *pStmt = 0;
danielk1977222a7572007-08-25 13:37:48 +0000311 rc = sqlite3_prepare(db,
danielk19777e6ebfb2006-06-12 11:24:37 +0000312 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
313 -1, &pStmt, 0);
danielk1977222a7572007-08-25 13:37:48 +0000314 if( rc==SQLITE_OK ){
315 sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
316 if( sqlite3_step(pStmt)==SQLITE_ROW ){
317 int rc2;
318 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
319 rc = sqlite3_declare_vtab(db, zCreateTable);
320 rc2 = sqlite3_finalize(pStmt);
321 if( rc==SQLITE_OK ){
322 rc = rc2;
323 }
324 } else {
325 rc = sqlite3_finalize(pStmt);
326 if( rc==SQLITE_OK ){
327 rc = SQLITE_ERROR;
328 }
329 }
danielk19777fa5dd12007-04-18 17:04:00 +0000330 if( rc==SQLITE_OK ){
danielk1977222a7572007-08-25 13:37:48 +0000331 rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
danielk19777fa5dd12007-04-18 17:04:00 +0000332 }
danielk1977222a7572007-08-25 13:37:48 +0000333 if( rc==SQLITE_OK ){
334 rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
danielk1977be718892006-06-23 08:05:19 +0000335 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000336 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000337 }
338
339 return rc;
340}
341
danielk1977cc013f82006-06-24 06:36:11 +0000342/*
343** This function frees all runtime structures associated with the virtual
344** table pVtab.
345*/
danielk1977a4e76362006-06-14 06:31:28 +0000346static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000347 echo_vtab *p = (echo_vtab*)pVtab;
drh17435752007-08-16 04:30:38 +0000348 sqlite3_free(p->aIndex);
349 sqlite3_free(p->aCol);
350 sqlite3_free(p->zThis);
351 sqlite3_free(p->zTableName);
352 sqlite3_free(p->zLogName);
353 sqlite3_free(p);
danielk1977a4e76362006-06-14 06:31:28 +0000354 return 0;
355}
356
danielk1977860b6cd2007-09-03 11:51:50 +0000357typedef struct EchoModule EchoModule;
358struct EchoModule {
359 Tcl_Interp *interp;
360};
361
danielk1977cc013f82006-06-24 06:36:11 +0000362/*
363** This function is called to do the work of the xConnect() method -
364** to allocate the required in-memory structures for a newly connected
365** virtual table.
366*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000367static int echoConstructor(
368 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000369 void *pAux,
drhe4102962006-09-11 00:34:22 +0000370 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000371 sqlite3_vtab **ppVtab,
372 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000373){
danielk1977a1644fd2007-08-29 12:31:25 +0000374 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000375 int i;
376 echo_vtab *pVtab;
377
danielk1977cc013f82006-06-24 06:36:11 +0000378 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk197731dad9d2007-08-16 11:36:15 +0000379 pVtab = sqlite3MallocZero( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000380 if( !pVtab ){
381 return SQLITE_NOMEM;
382 }
danielk1977860b6cd2007-09-03 11:51:50 +0000383 pVtab->interp = ((EchoModule *)pAux)->interp;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000384 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000385
danielk1977182c4ba2007-06-27 15:53:34 +0000386 /* Allocate echo_vtab.zThis */
danielk19771e536952007-08-16 10:09:01 +0000387 pVtab->zThis = sqlite3MPrintf(0, "%s", argv[2]);
danielk1977182c4ba2007-06-27 15:53:34 +0000388 if( !pVtab->zThis ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000389 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000390 return SQLITE_NOMEM;
391 }
392
danielk1977182c4ba2007-06-27 15:53:34 +0000393 /* Allocate echo_vtab.zTableName */
394 if( argc>3 ){
danielk19771e536952007-08-16 10:09:01 +0000395 pVtab->zTableName = sqlite3MPrintf(0, "%s", argv[3]);
danielk1977182c4ba2007-06-27 15:53:34 +0000396 dequoteString(pVtab->zTableName);
397 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
danielk19771e536952007-08-16 10:09:01 +0000398 char *z = sqlite3MPrintf(0, "%s%s", argv[2], &(pVtab->zTableName[1]));
drh17435752007-08-16 04:30:38 +0000399 sqlite3_free(pVtab->zTableName);
danielk1977182c4ba2007-06-27 15:53:34 +0000400 pVtab->zTableName = z;
401 pVtab->isPattern = 1;
402 }
403 if( !pVtab->zTableName ){
404 echoDestructor((sqlite3_vtab *)pVtab);
405 return SQLITE_NOMEM;
406 }
407 }
408
danielk1977cc013f82006-06-24 06:36:11 +0000409 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000410 for(i=0; i<argc; i++){
411 appendToEchoModule(pVtab->interp, argv[i]);
412 }
413
danielk1977cc013f82006-06-24 06:36:11 +0000414 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
415 ** structure. If an error occurs, delete the sqlite3_vtab structure and
416 ** return an error code.
417 */
danielk1977a1644fd2007-08-29 12:31:25 +0000418 rc = echoDeclareVtab(pVtab, db);
419 if( rc!=SQLITE_OK ){
danielk1977a4e76362006-06-14 06:31:28 +0000420 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977a1644fd2007-08-29 12:31:25 +0000421 return rc;
danielk1977a4e76362006-06-14 06:31:28 +0000422 }
423
danielk1977cc013f82006-06-24 06:36:11 +0000424 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000425 *ppVtab = &pVtab->base;
426 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000427}
drha967e882006-06-13 01:04:52 +0000428
danielk1977cc013f82006-06-24 06:36:11 +0000429/*
430** Echo virtual table module xCreate method.
431*/
drhb9bb7c12006-06-11 23:41:55 +0000432static int echoCreate(
433 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000434 void *pAux,
drhe4102962006-09-11 00:34:22 +0000435 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000436 sqlite3_vtab **ppVtab,
437 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000438){
danielk1977a298e902006-06-22 09:53:48 +0000439 int rc = SQLITE_OK;
danielk1977860b6cd2007-09-03 11:51:50 +0000440 appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000441 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000442
443 /* If there were two arguments passed to the module at the SQL level
444 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
445 ** the second argument is used as a table name. Attempt to create
446 ** such a table with a single column, "logmsg". This table will
447 ** be used to log calls to the xUpdate method. It will be deleted
448 ** when the virtual table is DROPed.
449 **
450 ** Note: The main point of this is to test that we can drop tables
451 ** from within an xDestroy method call.
452 */
danielk1977a298e902006-06-22 09:53:48 +0000453 if( rc==SQLITE_OK && argc==5 ){
454 char *zSql;
455 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
danielk19771e536952007-08-16 10:09:01 +0000456 pVtab->zLogName = sqlite3MPrintf(0, "%s", argv[4]);
457 zSql = sqlite3MPrintf(0, "CREATE TABLE %Q(logmsg)", pVtab->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000458 rc = sqlite3_exec(db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000459 sqlite3_free(zSql);
danielk1977cd2543b2007-09-03 15:03:20 +0000460 if( rc!=SQLITE_OK ){
461 *pzErr = sqlite3StrDup(sqlite3_errmsg(db));
462 }
463 }
464
465 if( *ppVtab && rc!=SQLITE_OK ){
466 echoDestructor(*ppVtab);
467 *ppVtab = 0;
danielk1977a298e902006-06-22 09:53:48 +0000468 }
danielk1977cc013f82006-06-24 06:36:11 +0000469
danielk1977a298e902006-06-22 09:53:48 +0000470 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000471}
danielk1977cc013f82006-06-24 06:36:11 +0000472
473/*
474** Echo virtual table module xConnect method.
475*/
drhb9bb7c12006-06-11 23:41:55 +0000476static int echoConnect(
477 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000478 void *pAux,
drhe4102962006-09-11 00:34:22 +0000479 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000480 sqlite3_vtab **ppVtab,
481 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000482){
danielk1977860b6cd2007-09-03 11:51:50 +0000483 appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000484 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000485}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000486
danielk1977cc013f82006-06-24 06:36:11 +0000487/*
488** Echo virtual table module xDisconnect method.
489*/
danielk19775fac9f82006-06-13 14:16:58 +0000490static int echoDisconnect(sqlite3_vtab *pVtab){
491 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
492 return echoDestructor(pVtab);
493}
danielk1977cc013f82006-06-24 06:36:11 +0000494
495/*
496** Echo virtual table module xDestroy method.
497*/
danielk19775fac9f82006-06-13 14:16:58 +0000498static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000499 int rc = SQLITE_OK;
500 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000501 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000502
503 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000504 if( p && p->zLogName ){
505 char *zSql;
danielk19771e536952007-08-16 10:09:01 +0000506 zSql = sqlite3MPrintf(0, "DROP TABLE %Q", p->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000507 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000508 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000509 }
danielk1977cc013f82006-06-24 06:36:11 +0000510
danielk1977a298e902006-06-22 09:53:48 +0000511 if( rc==SQLITE_OK ){
512 rc = echoDestructor(pVtab);
513 }
514 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000515}
516
danielk1977cc013f82006-06-24 06:36:11 +0000517/*
518** Echo virtual table module xOpen method.
519*/
danielk19775fac9f82006-06-13 14:16:58 +0000520static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000521 echo_cursor *pCur;
danielk197731dad9d2007-08-16 11:36:15 +0000522 pCur = sqlite3MallocZero(sizeof(echo_cursor));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000523 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000524 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000525}
526
danielk1977cc013f82006-06-24 06:36:11 +0000527/*
528** Echo virtual table module xClose method.
529*/
danielk19775fac9f82006-06-13 14:16:58 +0000530static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000531 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000532 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000533 sqlite3_stmt *pStmt = pCur->pStmt;
534 pCur->pStmt = 0;
drh17435752007-08-16 04:30:38 +0000535 sqlite3_free(pCur);
danielk1977be718892006-06-23 08:05:19 +0000536 rc = sqlite3_finalize(pStmt);
537 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000538}
539
danielk1977a298e902006-06-22 09:53:48 +0000540/*
541** Return non-zero if the cursor does not currently point to a valid record
542** (i.e if the scan has finished), or zero otherwise.
543*/
544static int echoEof(sqlite3_vtab_cursor *cur){
545 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
546}
547
danielk1977cc013f82006-06-24 06:36:11 +0000548/*
549** Echo virtual table module xNext method.
550*/
danielk19775fac9f82006-06-13 14:16:58 +0000551static int echoNext(sqlite3_vtab_cursor *cur){
danielk197731dad9d2007-08-16 11:36:15 +0000552 int rc = SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000553 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000554
danielk197731dad9d2007-08-16 11:36:15 +0000555 if( pCur->pStmt ){
556 rc = sqlite3_step(pCur->pStmt);
557 if( rc==SQLITE_ROW ){
558 rc = SQLITE_OK;
559 }else{
560 rc = sqlite3_finalize(pCur->pStmt);
561 pCur->pStmt = 0;
562 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000563 }
564
565 return rc;
566}
567
danielk1977cc013f82006-06-24 06:36:11 +0000568/*
569** Echo virtual table module xColumn method.
570*/
danielk19775fac9f82006-06-13 14:16:58 +0000571static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000572 int iCol = i + 1;
573 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk1977fbbe0052006-06-21 07:02:33 +0000574 if( !pStmt ){
575 sqlite3_result_null(ctx);
576 }else{
577 assert( sqlite3_data_count(pStmt)>iCol );
578 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
579 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000580 return SQLITE_OK;
581}
582
danielk1977cc013f82006-06-24 06:36:11 +0000583/*
584** Echo virtual table module xRowid method.
585*/
danielk19775fac9f82006-06-13 14:16:58 +0000586static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000587 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
588 *pRowid = sqlite3_column_int64(pStmt, 0);
589 return SQLITE_OK;
590}
591
danielk197798331532006-06-14 10:55:52 +0000592/*
593** Compute a simple hash of the null terminated string zString.
594**
595** This module uses only sqlite3_index_info.idxStr, not
596** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
597** in echoBestIndex(), idxNum is set to the corresponding hash value.
598** In echoFilter(), code assert()s that the supplied idxNum value is
599** indeed the hash of the supplied idxStr.
600*/
601static int hashString(const char *zString){
602 int val = 0;
603 int ii;
604 for(ii=0; zString[ii]; ii++){
605 val = (val << 3) + (int)zString[ii];
606 }
607 return val;
608}
609
danielk1977cc013f82006-06-24 06:36:11 +0000610/*
611** Echo virtual table module xFilter method.
612*/
danielk19775fac9f82006-06-13 14:16:58 +0000613static int echoFilter(
614 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000615 int idxNum, const char *idxStr,
616 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000617){
618 int rc;
drh4be8b512006-06-13 23:51:34 +0000619 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000620
621 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
622 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
623 sqlite3 *db = pVtab->db;
624
danielk1977cc013f82006-06-24 06:36:11 +0000625 /* Check that idxNum matches idxStr */
626 assert( idxNum==hashString(idxStr) );
627
628 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000629 appendToEchoModule(pVtab->interp, "xFilter");
630 appendToEchoModule(pVtab->interp, idxStr);
631 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000632 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000633 }
634
danielk19775fac9f82006-06-13 14:16:58 +0000635 sqlite3_finalize(pCur->pStmt);
636 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000637
638 /* Prepare the SQL statement created by echoBestIndex and bind the
639 ** runtime parameters passed to this function to it.
640 */
drh4be8b512006-06-13 23:51:34 +0000641 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000642 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000643 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk1977cc013f82006-06-24 06:36:11 +0000644 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000645 }
danielk1977cc013f82006-06-24 06:36:11 +0000646
647 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000648 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000649 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000650 }
651
danielk1977be718892006-06-23 08:05:19 +0000652 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000653}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000654
danielk1977cc013f82006-06-24 06:36:11 +0000655
656/*
657** A helper function used by echoUpdate() and echoBestIndex() for
658** manipulating strings in concert with the sqlite3_mprintf() function.
659**
660** Parameter pzStr points to a pointer to a string allocated with
661** sqlite3_mprintf. The second parameter, zAppend, points to another
662** string. The two strings are concatenated together and *pzStr
663** set to point at the result. The initial buffer pointed to by *pzStr
664** is deallocated via sqlite3_free().
665**
666** If the third argument, doFree, is true, then sqlite3_free() is
667** also called to free the buffer pointed to by zAppend.
668*/
danielk1977a1644fd2007-08-29 12:31:25 +0000669static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
danielk1977cc013f82006-06-24 06:36:11 +0000670 char *zIn = *pzStr;
danielk1977a1644fd2007-08-29 12:31:25 +0000671 if( !zAppend && doFree && *pRc==SQLITE_OK ){
672 *pRc = SQLITE_NOMEM;
673 }
674 if( *pRc!=SQLITE_OK ){
675 sqlite3_free(zIn);
676 zIn = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000677 }else{
danielk1977a1644fd2007-08-29 12:31:25 +0000678 if( zIn ){
679 char *zTemp = zIn;
680 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
681 sqlite3_free(zTemp);
682 }else{
683 zIn = sqlite3_mprintf("%s", zAppend);
684 }
685 if( !zIn ){
686 *pRc = SQLITE_NOMEM;
687 }
danielk1977cc013f82006-06-24 06:36:11 +0000688 }
689 *pzStr = zIn;
690 if( doFree ){
691 sqlite3_free(zAppend);
692 }
693}
694
drhb9bb7c12006-06-11 23:41:55 +0000695/*
danielk19775fac9f82006-06-13 14:16:58 +0000696** The echo module implements the subset of query constraints and sort
697** orders that may take advantage of SQLite indices on the underlying
698** real table. For example, if the real table is declared as:
699**
700** CREATE TABLE real(a, b, c);
701** CREATE INDEX real_index ON real(b);
702**
703** then the echo module handles WHERE or ORDER BY clauses that refer
704** to the column "b", but not "a" or "c". If a multi-column index is
drh85b623f2007-12-13 21:54:09 +0000705** present, only its left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000706**
707** This xBestIndex method encodes the proposed search strategy as
708** an SQL query on the real table underlying the virtual echo module
709** table and stores the query in sqlite3_index_info.idxStr. The SQL
710** statement is of the form:
711**
712** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
713**
714** where the <where-clause> and <order-by-clause> are determined
715** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000716*/
danielk19775fac9f82006-06-13 14:16:58 +0000717static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
718 int ii;
drh4be8b512006-06-13 23:51:34 +0000719 char *zQuery = 0;
720 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000721 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000722 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000723 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000724 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000725 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000726
727 int nRow;
728 int useIdx = 0;
729 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000730 int useCost = 0;
731 double cost;
danielk197774cdba42006-06-19 12:02:58 +0000732
danielk197739359dc2008-03-17 09:36:44 +0000733 int isIgnoreUsable = 0;
734 if( Tcl_GetVar(interp, "echo_module_ignore_usable", TCL_GLOBAL_ONLY) ){
735 isIgnoreUsable = 1;
736 }
737
danielk197774cdba42006-06-19 12:02:58 +0000738 /* Determine the number of rows in the table and store this value in local
739 ** variable nRow. The 'estimated-cost' of the scan will be the number of
740 ** rows in the table for a linear scan, or the log (base 2) of the
741 ** number of rows if the proposed scan uses an index.
742 */
danielk1977780b1d92007-03-30 14:56:34 +0000743 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
744 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
745 useCost = 1;
746 } else {
747 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000748 if( !zQuery ){
749 return SQLITE_NOMEM;
750 }
danielk1977780b1d92007-03-30 14:56:34 +0000751 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
752 sqlite3_free(zQuery);
753 if( rc!=SQLITE_OK ){
754 return rc;
755 }
756 sqlite3_step(pStmt);
757 nRow = sqlite3_column_int(pStmt, 0);
758 rc = sqlite3_finalize(pStmt);
759 if( rc!=SQLITE_OK ){
760 return rc;
761 }
danielk197774cdba42006-06-19 12:02:58 +0000762 }
danielk19775fac9f82006-06-13 14:16:58 +0000763
drh4be8b512006-06-13 23:51:34 +0000764 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000765 if( !zQuery ){
766 return SQLITE_NOMEM;
767 }
danielk19775fac9f82006-06-13 14:16:58 +0000768 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
769 const struct sqlite3_index_constraint *pConstraint;
770 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000771 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000772
773 pConstraint = &pIdxInfo->aConstraint[ii];
774 pUsage = &pIdxInfo->aConstraintUsage[ii];
775
danielk197739359dc2008-03-17 09:36:44 +0000776 if( !isIgnoreUsable && !pConstraint->usable ) continue;
777
drh383736b2006-10-08 18:56:57 +0000778 iCol = pConstraint->iColumn;
danielk19775fac9f82006-06-13 14:16:58 +0000779 if( pVtab->aIndex[iCol] ){
780 char *zCol = pVtab->aCol[iCol];
781 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000782 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000783 if( iCol<0 ){
784 zCol = "rowid";
785 }
danielk19775fac9f82006-06-13 14:16:58 +0000786 switch( pConstraint->op ){
787 case SQLITE_INDEX_CONSTRAINT_EQ:
788 zOp = "="; break;
789 case SQLITE_INDEX_CONSTRAINT_LT:
790 zOp = "<"; break;
791 case SQLITE_INDEX_CONSTRAINT_GT:
792 zOp = ">"; break;
793 case SQLITE_INDEX_CONSTRAINT_LE:
794 zOp = "<="; break;
795 case SQLITE_INDEX_CONSTRAINT_GE:
796 zOp = ">="; break;
797 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000798 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000799 }
drh1a90e092006-06-14 22:07:10 +0000800 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000801 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
802 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000803 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000804 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000805 }
danielk1977a1644fd2007-08-29 12:31:25 +0000806 string_concat(&zQuery, zNew, 1, &rc);
danielk1977cc013f82006-06-24 06:36:11 +0000807
drh4be8b512006-06-13 23:51:34 +0000808 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000809 pUsage->argvIndex = ++nArg;
810 pUsage->omit = 1;
811 }
812 }
danielk197747d08092006-06-14 07:41:31 +0000813
814 /* If there is only one term in the ORDER BY clause, and it is
815 ** on a column that this virtual table has an index for, then consume
816 ** the ORDER BY clause.
817 */
818 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000819 int iCol = pIdxInfo->aOrderBy->iColumn;
820 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000821 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000822 if( iCol<0 ){
823 zCol = "rowid";
824 }
danielk1977cc013f82006-06-24 06:36:11 +0000825 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
danielk1977a1644fd2007-08-29 12:31:25 +0000826 string_concat(&zQuery, zNew, 1, &rc);
danielk197747d08092006-06-14 07:41:31 +0000827 pIdxInfo->orderByConsumed = 1;
828 }
829
danielk19775fac9f82006-06-13 14:16:58 +0000830 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000831 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000832
danielk1977222a7572007-08-25 13:37:48 +0000833 if( !zQuery ){
danielk1977a1644fd2007-08-29 12:31:25 +0000834 return rc;
danielk1977222a7572007-08-25 13:37:48 +0000835 }
danielk197798331532006-06-14 10:55:52 +0000836 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000837 pIdxInfo->idxStr = zQuery;
838 pIdxInfo->needToFreeIdxStr = 1;
danielk1977780b1d92007-03-30 14:56:34 +0000839 if (useCost) {
840 pIdxInfo->estimatedCost = cost;
841 } else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000842 /* Approximation of log2(nRow). */
843 for( ii=0; ii<(sizeof(int)*8); ii++ ){
844 if( nRow & (1<<ii) ){
845 pIdxInfo->estimatedCost = (double)ii;
846 }
847 }
848 } else {
849 pIdxInfo->estimatedCost = (double)nRow;
850 }
851 return rc;
drha967e882006-06-13 01:04:52 +0000852}
853
danielk1977176f4d22006-06-15 10:41:15 +0000854/*
danielk1977cc013f82006-06-24 06:36:11 +0000855** The xUpdate method for echo module virtual tables.
856**
danielk1977176f4d22006-06-15 10:41:15 +0000857** apData[0] apData[1] apData[2..]
858**
859** INTEGER DELETE
860**
861** INTEGER NULL (nCol args) UPDATE (do not set rowid)
862** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
863**
864** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
865** NULL INTEGER (nCol args) INSERT (incl. rowid value)
866**
867*/
danielk19771f6eec52006-06-16 06:17:47 +0000868int echoUpdate(
869 sqlite3_vtab *tab,
870 int nData,
871 sqlite3_value **apData,
872 sqlite_int64 *pRowid
873){
danielk197726e41442006-06-14 15:16:35 +0000874 echo_vtab *pVtab = (echo_vtab *)tab;
875 sqlite3 *db = pVtab->db;
876 int rc = SQLITE_OK;
877
danielk1977176f4d22006-06-15 10:41:15 +0000878 sqlite3_stmt *pStmt;
879 char *z = 0; /* SQL statement to execute */
880 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
881 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
882 int i; /* Counter variable used by for loops */
883
danielk197726e41442006-06-14 15:16:35 +0000884 assert( nData==pVtab->nCol+2 || nData==1 );
885
drh5aec0422006-06-14 23:43:31 +0000886 /* If apData[0] is an integer and nData>1 then do an UPDATE */
887 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000888 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000889 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000890 if( !z ){
891 rc = SQLITE_NOMEM;
892 }
danielk1977176f4d22006-06-15 10:41:15 +0000893
894 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
895 bindArgZero = 1;
896
897 if( bindArgOne ){
danielk1977a1644fd2007-08-29 12:31:25 +0000898 string_concat(&z, " SET rowid=?1 ", 0, &rc);
drh5aec0422006-06-14 23:43:31 +0000899 zSep = ",";
900 }
901 for(i=2; i<nData; i++){
902 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000903 string_concat(&z, sqlite3_mprintf(
danielk1977a1644fd2007-08-29 12:31:25 +0000904 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000905 zSep = ",";
906 }
danielk1977a1644fd2007-08-29 12:31:25 +0000907 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000908 }
909
danielk1977176f4d22006-06-15 10:41:15 +0000910 /* If apData[0] is an integer and nData==1 then do a DELETE */
911 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
912 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000913 if( !z ){
914 rc = SQLITE_NOMEM;
915 }
danielk1977176f4d22006-06-15 10:41:15 +0000916 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000917 }
918
danielk1977176f4d22006-06-15 10:41:15 +0000919 /* If the first argument is NULL and there are more than two args, INSERT */
920 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000921 int ii;
922 char *zInsert = 0;
923 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000924
danielk19774b2688a2006-06-20 11:01:07 +0000925 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000926 if( !zInsert ){
927 rc = SQLITE_NOMEM;
928 }
danielk1977176f4d22006-06-15 10:41:15 +0000929 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
930 bindArgOne = 1;
931 zValues = sqlite3_mprintf("?");
danielk1977a1644fd2007-08-29 12:31:25 +0000932 string_concat(&zInsert, "rowid", 0, &rc);
danielk197726e41442006-06-14 15:16:35 +0000933 }
934
danielk1977176f4d22006-06-15 10:41:15 +0000935 assert((pVtab->nCol+2)==nData);
936 for(ii=2; ii<nData; ii++){
937 string_concat(&zInsert,
danielk1977a1644fd2007-08-29 12:31:25 +0000938 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
danielk1977176f4d22006-06-15 10:41:15 +0000939 string_concat(&zValues,
danielk1977a1644fd2007-08-29 12:31:25 +0000940 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
danielk197726e41442006-06-14 15:16:35 +0000941 }
942
danielk1977a1644fd2007-08-29 12:31:25 +0000943 string_concat(&z, zInsert, 1, &rc);
944 string_concat(&z, ") VALUES(", 0, &rc);
945 string_concat(&z, zValues, 1, &rc);
946 string_concat(&z, ")", 0, &rc);
danielk1977176f4d22006-06-15 10:41:15 +0000947 }
948
949 /* Anything else is an error */
950 else{
951 assert(0);
952 return SQLITE_ERROR;
953 }
954
danielk1977a1644fd2007-08-29 12:31:25 +0000955 if( rc==SQLITE_OK ){
956 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
957 }
danielk1977176f4d22006-06-15 10:41:15 +0000958 assert( rc!=SQLITE_OK || pStmt );
959 sqlite3_free(z);
960 if( rc==SQLITE_OK ) {
961 if( bindArgZero ){
962 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000963 }
danielk1977176f4d22006-06-15 10:41:15 +0000964 if( bindArgOne ){
965 sqlite3_bind_value(pStmt, 1, apData[1]);
966 }
danielk1977a7a8e142008-02-13 18:25:27 +0000967 for(i=2; i<nData && rc==SQLITE_OK; i++){
968 if( apData[i] ) rc = sqlite3_bind_value(pStmt, i, apData[i]);
danielk1977176f4d22006-06-15 10:41:15 +0000969 }
danielk1977a7a8e142008-02-13 18:25:27 +0000970 if( rc==SQLITE_OK ){
971 sqlite3_step(pStmt);
972 rc = sqlite3_finalize(pStmt);
973 }else{
974 sqlite3_finalize(pStmt);
975 }
danielk197726e41442006-06-14 15:16:35 +0000976 }
977
danielk19771f6eec52006-06-16 06:17:47 +0000978 if( pRowid && rc==SQLITE_OK ){
979 *pRowid = sqlite3_last_insert_rowid(db);
980 }
981
danielk197726e41442006-06-14 15:16:35 +0000982 return rc;
983}
984
drha967e882006-06-13 01:04:52 +0000985/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000986** xBegin, xSync, xCommit and xRollback callbacks for echo module
987** virtual tables. Do nothing other than add the name of the callback
988** to the $::echo_module Tcl variable.
989*/
990static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
991 char *z;
992 echo_vtab *pVtab = (echo_vtab *)tab;
993 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
994 appendToEchoModule(pVtab->interp, zCall);
995 appendToEchoModule(pVtab->interp, z);
996 sqlite3_free(z);
danielk1977a1644fd2007-08-29 12:31:25 +0000997 return (z?SQLITE_OK:SQLITE_NOMEM);
danielk1977c69cdfd2006-06-17 09:39:55 +0000998}
999static int echoBegin(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001000 int rc;
danielk19775017dc32006-06-24 09:34:22 +00001001 echo_vtab *pVtab = (echo_vtab *)tab;
1002 Tcl_Interp *interp = pVtab->interp;
1003 const char *zVal;
1004
danielk1977a1644fd2007-08-29 12:31:25 +00001005 rc = echoTransactionCall(tab, "xBegin");
danielk19775017dc32006-06-24 09:34:22 +00001006
danielk1977a1644fd2007-08-29 12:31:25 +00001007 if( rc==SQLITE_OK ){
1008 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
1009 ** and it is set to the name of the real table underlying this virtual
1010 ** echo module table, then cause this xSync operation to fail.
1011 */
1012 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
1013 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1014 rc = SQLITE_ERROR;
1015 }
danielk19775017dc32006-06-24 09:34:22 +00001016 }
danielk1977a1644fd2007-08-29 12:31:25 +00001017 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001018}
1019static int echoSync(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001020 int rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001021 echo_vtab *pVtab = (echo_vtab *)tab;
1022 Tcl_Interp *interp = pVtab->interp;
1023 const char *zVal;
1024
danielk1977a1644fd2007-08-29 12:31:25 +00001025 rc = echoTransactionCall(tab, "xSync");
danielk1977c69cdfd2006-06-17 09:39:55 +00001026
danielk1977a1644fd2007-08-29 12:31:25 +00001027 if( rc==SQLITE_OK ){
1028 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
1029 ** and it is set to the name of the real table underlying this virtual
1030 ** echo module table, then cause this xSync operation to fail.
1031 */
1032 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
1033 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1034 rc = -1;
1035 }
danielk1977c69cdfd2006-06-17 09:39:55 +00001036 }
danielk1977a1644fd2007-08-29 12:31:25 +00001037 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001038}
1039static int echoCommit(sqlite3_vtab *tab){
drh643167f2008-01-22 21:30:53 +00001040 int rc;
1041 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 1);
1042 rc = echoTransactionCall(tab, "xCommit");
1043 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0);
1044 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001045}
1046static int echoRollback(sqlite3_vtab *tab){
1047 return echoTransactionCall(tab, "xRollback");
1048}
1049
1050/*
drhe94b0c32006-07-08 18:09:15 +00001051** Implementation of "GLOB" function on the echo module. Pass
1052** all arguments to the ::echo_glob_overload procedure of TCL
1053** and return the result of that procedure as a string.
1054*/
1055static void overloadedGlobFunction(
1056 sqlite3_context *pContext,
1057 int nArg,
1058 sqlite3_value **apArg
1059){
1060 Tcl_Interp *interp = sqlite3_user_data(pContext);
1061 Tcl_DString str;
1062 int i;
1063 int rc;
1064 Tcl_DStringInit(&str);
1065 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
1066 for(i=0; i<nArg; i++){
1067 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
1068 }
1069 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
1070 Tcl_DStringFree(&str);
1071 if( rc ){
1072 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
1073 }else{
1074 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1075 -1, SQLITE_TRANSIENT);
1076 }
1077 Tcl_ResetResult(interp);
1078}
1079
1080/*
1081** This is the xFindFunction implementation for the echo module.
1082** SQLite calls this routine when the first argument of a function
1083** is a column of an echo virtual table. This routine can optionally
1084** override the implementation of that function. It will choose to
1085** do so if the function is named "glob", and a TCL command named
1086** ::echo_glob_overload exists.
1087*/
1088static int echoFindFunction(
1089 sqlite3_vtab *vtab,
1090 int nArg,
1091 const char *zFuncName,
1092 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1093 void **ppArg
1094){
1095 echo_vtab *pVtab = (echo_vtab *)vtab;
1096 Tcl_Interp *interp = pVtab->interp;
1097 Tcl_CmdInfo info;
1098 if( strcmp(zFuncName,"glob")!=0 ){
1099 return 0;
1100 }
1101 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1102 return 0;
1103 }
1104 *pxFunc = overloadedGlobFunction;
1105 *ppArg = interp;
1106 return 1;
1107}
1108
danielk1977182c4ba2007-06-27 15:53:34 +00001109static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1110 int rc = SQLITE_OK;
1111 echo_vtab *p = (echo_vtab *)vtab;
1112
1113 if( p->isPattern ){
1114 int nThis = strlen(p->zThis);
danielk19771e536952007-08-16 10:09:01 +00001115 char *zSql = sqlite3MPrintf(0, "ALTER TABLE %s RENAME TO %s%s",
danielk1977182c4ba2007-06-27 15:53:34 +00001116 p->zTableName, zNewName, &p->zTableName[nThis]
1117 );
1118 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +00001119 sqlite3_free(zSql);
danielk1977182c4ba2007-06-27 15:53:34 +00001120 }
1121
1122 return rc;
1123}
1124
drhe94b0c32006-07-08 18:09:15 +00001125/*
danielk1977cc013f82006-06-24 06:36:11 +00001126** A virtual table module that merely "echos" the contents of another
1127** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +00001128*/
1129static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +00001130 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +00001131 echoCreate,
1132 echoConnect,
drha967e882006-06-13 01:04:52 +00001133 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +00001134 echoDisconnect,
1135 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +00001136 echoOpen, /* xOpen - open a cursor */
1137 echoClose, /* xClose - close a cursor */
1138 echoFilter, /* xFilter - configure scan constraints */
1139 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +00001140 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +00001141 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +00001142 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +00001143 echoUpdate, /* xUpdate - write data */
1144 echoBegin, /* xBegin - begin transaction */
1145 echoSync, /* xSync - sync transaction */
1146 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +00001147 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +00001148 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:34 +00001149 echoRename, /* xRename - rename the table */
drhb9bb7c12006-06-11 23:41:55 +00001150};
1151
1152/*
1153** Decode a pointer to an sqlite3 object.
1154*/
1155static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
1156 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
1157 return TCL_OK;
1158}
1159
danielk1977860b6cd2007-09-03 11:51:50 +00001160static void moduleDestroy(void *p){
1161 sqlite3_free(p);
1162}
1163
drhb9bb7c12006-06-11 23:41:55 +00001164/*
1165** Register the echo virtual table module.
1166*/
1167static int register_echo_module(
1168 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1169 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1170 int objc, /* Number of arguments */
1171 Tcl_Obj *CONST objv[] /* Command arguments */
1172){
1173 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:50 +00001174 EchoModule *pMod;
drhb9bb7c12006-06-11 23:41:55 +00001175 if( objc!=2 ){
1176 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1177 return TCL_ERROR;
1178 }
1179 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977860b6cd2007-09-03 11:51:50 +00001180 pMod = sqlite3_malloc(sizeof(EchoModule));
1181 pMod->interp = interp;
1182 sqlite3_create_module_v2(db, "echo", &echoModule, (void*)pMod, moduleDestroy);
drhb9bb7c12006-06-11 23:41:55 +00001183 return TCL_OK;
1184}
1185
danielk19775017dc32006-06-24 09:34:22 +00001186/*
1187** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1188**
1189** sqlite3_declare_vtab DB SQL
1190*/
1191static int declare_vtab(
1192 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1193 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1194 int objc, /* Number of arguments */
1195 Tcl_Obj *CONST objv[] /* Command arguments */
1196){
1197 sqlite3 *db;
1198 int rc;
1199 if( objc!=3 ){
1200 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1201 return TCL_ERROR;
1202 }
1203 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1204 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1205 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001206 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001207 return TCL_ERROR;
1208 }
1209 return TCL_OK;
1210}
1211
danielk1977cc013f82006-06-24 06:36:11 +00001212#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001213
1214/*
1215** Register commands with the TCL interpreter.
1216*/
1217int Sqlitetest8_Init(Tcl_Interp *interp){
1218 static struct {
1219 char *zName;
1220 Tcl_ObjCmdProc *xProc;
1221 void *clientData;
1222 } aObjCmd[] = {
danielk1977cc013f82006-06-24 06:36:11 +00001223#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001224 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +00001225 { "sqlite3_declare_vtab", declare_vtab, 0 },
danielk1977cc013f82006-06-24 06:36:11 +00001226#endif
drhb9bb7c12006-06-11 23:41:55 +00001227 };
1228 int i;
1229 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1230 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1231 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1232 }
1233 return TCL_OK;
1234}