blob: d7d448b8d90f4827ca9b7beb781dba629dc4bd92 [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**
danielk19778c0a7912008-08-20 14:49:23 +000016** $Id: test8.c,v 1.74 2008/08/20 14:49:25 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
danielk19773e3a84d2008-08-01 17:37:40 +000047/*
48** Errors can be provoked within the following echo virtual table methods:
49**
50** xBestIndex xOpen xFilter xNext
51** xColumn xRowid xUpdate xSync
danielk1977987a00e2008-08-01 17:51:47 +000052** xBegin xRename
danielk19773e3a84d2008-08-01 17:37:40 +000053**
54** This is done by setting the global tcl variable:
55**
56** echo_module_fail($method,$tbl)
57**
58** where $method is set to the name of the virtual table method to fail
59** (i.e. "xBestIndex") and $tbl is the name of the table being echoed (not
60** the name of the virtual table, the name of the underlying real table).
61*/
62
danielk19775fac9f82006-06-13 14:16:58 +000063/*
danielk1977d6e8dd02006-06-15 15:38:41 +000064** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:58 +000065**
danielk1977d6e8dd02006-06-15 15:38:41 +000066** echo.vtab.aIndex is an array of booleans. The nth entry is true if
67** the nth column of the real table is the left-most column of an index
68** (implicit or otherwise). In other words, if SQLite can optimize
69** a query like "SELECT * FROM real_table WHERE col = ?".
70**
danielk1977c69cdfd2006-06-17 09:39:55 +000071** Member variable aCol[] contains copies of the column names of the real
72** table.
danielk19775fac9f82006-06-13 14:16:58 +000073*/
danielk1977b7a7b9a2006-06-13 10:24:42 +000074struct echo_vtab {
75 sqlite3_vtab base;
danielk1977cc013f82006-06-24 06:36:11 +000076 Tcl_Interp *interp; /* Tcl interpreter containing debug variables */
77 sqlite3 *db; /* Database connection */
danielk19775fac9f82006-06-13 14:16:58 +000078
danielk1977182c4ba2007-06-27 15:53:34 +000079 int isPattern;
drhcd3dd9d2008-04-28 20:27:53 +000080 int inTransaction; /* True if within a transaction */
danielk1977182c4ba2007-06-27 15:53:34 +000081 char *zThis; /* Name of the echo table */
danielk1977a4e76362006-06-14 06:31:28 +000082 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:48 +000083 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:28 +000084 int nCol; /* Number of columns in the real table */
85 int *aIndex; /* Array of size nCol. True if column has an index */
86 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000087};
88
89/* An echo cursor object */
90struct echo_cursor {
91 sqlite3_vtab_cursor base;
92 sqlite3_stmt *pStmt;
danielk1977b7a7b9a2006-06-13 10:24:42 +000093};
94
danielk19773e3a84d2008-08-01 17:37:40 +000095static int simulateVtabError(echo_vtab *p, const char *zMethod){
96 const char *zErr;
97 char zVarname[128];
98 zVarname[127] = '\0';
danielk19778c0a7912008-08-20 14:49:23 +000099 snprintf(zVarname, 127, "echo_module_fail(%s,%s)", zMethod, p->zTableName);
danielk19773e3a84d2008-08-01 17:37:40 +0000100 zErr = Tcl_GetVar(p->interp, zVarname, TCL_GLOBAL_ONLY);
101 if( zErr ){
102 p->base.zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", zErr);
103 }
104 return (zErr!=0);
105}
106
danielk1977cc013f82006-06-24 06:36:11 +0000107/*
danielk1977182c4ba2007-06-27 15:53:34 +0000108** Convert an SQL-style quoted string into a normal string by removing
109** the quote characters. The conversion is done in-place. If the
110** input does not begin with a quote character, then this routine
111** is a no-op.
112**
113** Examples:
114**
115** "abc" becomes abc
116** 'xyz' becomes xyz
117** [pqr] becomes pqr
118** `mno` becomes mno
119*/
120static void dequoteString(char *z){
121 int quote;
122 int i, j;
123 if( z==0 ) return;
124 quote = z[0];
125 switch( quote ){
126 case '\'': break;
127 case '"': break;
128 case '`': break; /* For MySQL compatibility */
129 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
130 default: return;
131 }
132 for(i=1, j=0; z[i]; i++){
133 if( z[i]==quote ){
134 if( z[i+1]==quote ){
135 z[j++] = quote;
136 i++;
137 }else{
138 z[j++] = 0;
139 break;
140 }
141 }else{
142 z[j++] = z[i];
143 }
144 }
145}
146
147/*
danielk1977cc013f82006-06-24 06:36:11 +0000148** Retrieve the column names for the table named zTab via database
149** connection db. SQLITE_OK is returned on success, or an sqlite error
150** code otherwise.
151**
152** If successful, the number of columns is written to *pnCol. *paCol is
drh17435752007-08-16 04:30:38 +0000153** set to point at sqlite3_malloc()'d space containing the array of
154** nCol column names. The caller is responsible for calling sqlite3_free
danielk1977cc013f82006-06-24 06:36:11 +0000155** on *paCol.
156*/
danielk19775fac9f82006-06-13 14:16:58 +0000157static int getColumnNames(
158 sqlite3 *db,
159 const char *zTab,
160 char ***paCol,
161 int *pnCol
162){
163 char **aCol = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000164 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000165 sqlite3_stmt *pStmt = 0;
166 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19 +0000167 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000168
danielk1977cc013f82006-06-24 06:36:11 +0000169 /* Prepare the statement "SELECT * FROM <tbl>". The column names
170 ** of the result set of the compiled SELECT will be the same as
171 ** the column names of table <tbl>.
172 */
drh17435752007-08-16 04:30:38 +0000173 zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000174 if( !zSql ){
175 rc = SQLITE_NOMEM;
176 goto out;
177 }
178 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000179 sqlite3_free(zSql);
danielk1977cc013f82006-06-24 06:36:11 +0000180
danielk19775fac9f82006-06-13 14:16:58 +0000181 if( rc==SQLITE_OK ){
182 int ii;
danielk1977cc013f82006-06-24 06:36:11 +0000183 int nBytes;
184 char *zSpace;
danielk19775fac9f82006-06-13 14:16:58 +0000185 nCol = sqlite3_column_count(pStmt);
danielk1977cc013f82006-06-24 06:36:11 +0000186
187 /* Figure out how much space to allocate for the array of column names
188 ** (including space for the strings themselves). Then allocate it.
189 */
190 nBytes = sizeof(char *) * nCol;
191 for(ii=0; ii<nCol; ii++){
danielk1977a7a8e142008-02-13 18:25:27 +0000192 const char *zName = sqlite3_column_name(pStmt, ii);
193 if( !zName ){
194 rc = SQLITE_NOMEM;
195 goto out;
196 }
197 nBytes += strlen(zName)+1;
danielk1977cc013f82006-06-24 06:36:11 +0000198 }
danielk197731dad9d2007-08-16 11:36:15 +0000199 aCol = (char **)sqlite3MallocZero(nBytes);
danielk19775fac9f82006-06-13 14:16:58 +0000200 if( !aCol ){
201 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11 +0000202 goto out;
danielk19775fac9f82006-06-13 14:16:58 +0000203 }
danielk1977cc013f82006-06-24 06:36:11 +0000204
205 /* Copy the column names into the allocated space and set up the
206 ** pointers in the aCol[] array.
207 */
208 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58 +0000209 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11 +0000210 aCol[ii] = zSpace;
211 zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
212 zSpace++;
danielk19775fac9f82006-06-13 14:16:58 +0000213 }
danielk1977cc013f82006-06-24 06:36:11 +0000214 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58 +0000215 }
216
217 *paCol = aCol;
218 *pnCol = nCol;
219
danielk1977cc013f82006-06-24 06:36:11 +0000220out:
danielk19775fac9f82006-06-13 14:16:58 +0000221 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000222 return rc;
223}
224
danielk1977cc013f82006-06-24 06:36:11 +0000225/*
226** Parameter zTab is the name of a table in database db with nCol
227** columns. This function allocates an array of integers nCol in
228** size and populates it according to any implicit or explicit
229** indices on table zTab.
230**
231** If successful, SQLITE_OK is returned and *paIndex set to point
232** at the allocated array. Otherwise, an error code is returned.
233**
234** See comments associated with the member variable aIndex above
235** "struct echo_vtab" for details of the contents of the array.
236*/
237static int getIndexArray(
238 sqlite3 *db, /* Database connection */
239 const char *zTab, /* Name of table in database db */
240 int nCol,
241 int **paIndex
242){
danielk19775fac9f82006-06-13 14:16:58 +0000243 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000244 int *aIndex = 0;
245 int rc;
danielk1977cc013f82006-06-24 06:36:11 +0000246 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000247
danielk1977cc013f82006-06-24 06:36:11 +0000248 /* Allocate space for the index array */
danielk197731dad9d2007-08-16 11:36:15 +0000249 aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000250 if( !aIndex ){
251 rc = SQLITE_NOMEM;
252 goto get_index_array_out;
253 }
254
danielk1977cc013f82006-06-24 06:36:11 +0000255 /* Compile an sqlite pragma to loop through all indices on table zTab */
danielk19771e536952007-08-16 10:09:01 +0000256 zSql = sqlite3MPrintf(0, "PRAGMA index_list(%s)", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000257 if( !zSql ){
258 rc = SQLITE_NOMEM;
259 goto get_index_array_out;
260 }
261 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000262 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000263
danielk1977cc013f82006-06-24 06:36:11 +0000264 /* For each index, figure out the left-most column and set the
265 ** corresponding entry in aIndex[] to 1.
266 */
danielk19775fac9f82006-06-13 14:16:58 +0000267 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000268 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58 +0000269 sqlite3_stmt *pStmt2 = 0;
danielk19771e536952007-08-16 10:09:01 +0000270 zSql = sqlite3MPrintf(0, "PRAGMA index_info(%s)", zIdx);
danielk1977cc013f82006-06-24 06:36:11 +0000271 if( !zSql ){
272 rc = SQLITE_NOMEM;
273 goto get_index_array_out;
274 }
275 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
drh17435752007-08-16 04:30:38 +0000276 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000277 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
278 int cid = sqlite3_column_int(pStmt2, 1);
279 assert( cid>=0 && cid<nCol );
280 aIndex[cid] = 1;
281 }
danielk1977be718892006-06-23 08:05:19 +0000282 if( pStmt2 ){
283 rc = sqlite3_finalize(pStmt2);
284 }
danielk19775fac9f82006-06-13 14:16:58 +0000285 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000286 goto get_index_array_out;
287 }
288 }
289
danielk19775fac9f82006-06-13 14:16:58 +0000290
291get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000292 if( pStmt ){
293 int rc2 = sqlite3_finalize(pStmt);
294 if( rc==SQLITE_OK ){
295 rc = rc2;
296 }
297 }
danielk19775fac9f82006-06-13 14:16:58 +0000298 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000299 sqlite3_free(aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000300 aIndex = 0;
301 }
302 *paIndex = aIndex;
303 return rc;
304}
305
danielk197778efaba2006-06-12 06:09:17 +0000306/*
307** Global Tcl variable $echo_module is a list. This routine appends
308** the string element zArg to that list in interpreter interp.
309*/
drha967e882006-06-13 01:04:52 +0000310static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000311 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000312 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000313}
314
danielk19777e6ebfb2006-06-12 11:24:37 +0000315/*
316** This function is called from within the echo-modules xCreate and
317** xConnect methods. The argc and argv arguments are copies of those
318** passed to the calling method. This function is responsible for
319** calling sqlite3_declare_vtab() to declare the schema of the virtual
320** table being created or connected.
321**
322** If the constructor was passed just one argument, i.e.:
323**
324** CREATE TABLE t1 AS echo(t2);
325**
326** Then t2 is assumed to be the name of a *real* database table. The
327** schema of the virtual table is declared by passing a copy of the
328** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
329** Hence, the virtual table should have exactly the same column names and
330** types as the real table.
331*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000332static int echoDeclareVtab(
333 echo_vtab *pVtab,
danielk1977182c4ba2007-06-27 15:53:34 +0000334 sqlite3 *db
danielk1977b7a7b9a2006-06-13 10:24:42 +0000335){
danielk19777e6ebfb2006-06-12 11:24:37 +0000336 int rc = SQLITE_OK;
337
danielk1977182c4ba2007-06-27 15:53:34 +0000338 if( pVtab->zTableName ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000339 sqlite3_stmt *pStmt = 0;
danielk1977222a7572007-08-25 13:37:48 +0000340 rc = sqlite3_prepare(db,
danielk19777e6ebfb2006-06-12 11:24:37 +0000341 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
342 -1, &pStmt, 0);
danielk1977222a7572007-08-25 13:37:48 +0000343 if( rc==SQLITE_OK ){
344 sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
345 if( sqlite3_step(pStmt)==SQLITE_ROW ){
346 int rc2;
347 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
348 rc = sqlite3_declare_vtab(db, zCreateTable);
349 rc2 = sqlite3_finalize(pStmt);
350 if( rc==SQLITE_OK ){
351 rc = rc2;
352 }
353 } else {
354 rc = sqlite3_finalize(pStmt);
355 if( rc==SQLITE_OK ){
356 rc = SQLITE_ERROR;
357 }
358 }
danielk19777fa5dd12007-04-18 17:04:00 +0000359 if( rc==SQLITE_OK ){
danielk1977222a7572007-08-25 13:37:48 +0000360 rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
danielk19777fa5dd12007-04-18 17:04:00 +0000361 }
danielk1977222a7572007-08-25 13:37:48 +0000362 if( rc==SQLITE_OK ){
363 rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
danielk1977be718892006-06-23 08:05:19 +0000364 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000365 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000366 }
367
368 return rc;
369}
370
danielk1977cc013f82006-06-24 06:36:11 +0000371/*
372** This function frees all runtime structures associated with the virtual
373** table pVtab.
374*/
danielk1977a4e76362006-06-14 06:31:28 +0000375static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000376 echo_vtab *p = (echo_vtab*)pVtab;
drh17435752007-08-16 04:30:38 +0000377 sqlite3_free(p->aIndex);
378 sqlite3_free(p->aCol);
379 sqlite3_free(p->zThis);
380 sqlite3_free(p->zTableName);
381 sqlite3_free(p->zLogName);
382 sqlite3_free(p);
danielk1977a4e76362006-06-14 06:31:28 +0000383 return 0;
384}
385
danielk1977860b6cd2007-09-03 11:51:50 +0000386typedef struct EchoModule EchoModule;
387struct EchoModule {
388 Tcl_Interp *interp;
389};
390
danielk1977cc013f82006-06-24 06:36:11 +0000391/*
392** This function is called to do the work of the xConnect() method -
393** to allocate the required in-memory structures for a newly connected
394** virtual table.
395*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000396static int echoConstructor(
397 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000398 void *pAux,
drhe4102962006-09-11 00:34:22 +0000399 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000400 sqlite3_vtab **ppVtab,
401 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000402){
danielk1977a1644fd2007-08-29 12:31:25 +0000403 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000404 int i;
405 echo_vtab *pVtab;
406
danielk1977cc013f82006-06-24 06:36:11 +0000407 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk197731dad9d2007-08-16 11:36:15 +0000408 pVtab = sqlite3MallocZero( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000409 if( !pVtab ){
410 return SQLITE_NOMEM;
411 }
danielk1977860b6cd2007-09-03 11:51:50 +0000412 pVtab->interp = ((EchoModule *)pAux)->interp;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000413 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000414
danielk1977182c4ba2007-06-27 15:53:34 +0000415 /* Allocate echo_vtab.zThis */
danielk19771e536952007-08-16 10:09:01 +0000416 pVtab->zThis = sqlite3MPrintf(0, "%s", argv[2]);
danielk1977182c4ba2007-06-27 15:53:34 +0000417 if( !pVtab->zThis ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000418 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000419 return SQLITE_NOMEM;
420 }
421
danielk1977182c4ba2007-06-27 15:53:34 +0000422 /* Allocate echo_vtab.zTableName */
423 if( argc>3 ){
danielk19771e536952007-08-16 10:09:01 +0000424 pVtab->zTableName = sqlite3MPrintf(0, "%s", argv[3]);
danielk1977182c4ba2007-06-27 15:53:34 +0000425 dequoteString(pVtab->zTableName);
426 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
danielk19771e536952007-08-16 10:09:01 +0000427 char *z = sqlite3MPrintf(0, "%s%s", argv[2], &(pVtab->zTableName[1]));
drh17435752007-08-16 04:30:38 +0000428 sqlite3_free(pVtab->zTableName);
danielk1977182c4ba2007-06-27 15:53:34 +0000429 pVtab->zTableName = z;
430 pVtab->isPattern = 1;
431 }
432 if( !pVtab->zTableName ){
433 echoDestructor((sqlite3_vtab *)pVtab);
434 return SQLITE_NOMEM;
435 }
436 }
437
danielk1977cc013f82006-06-24 06:36:11 +0000438 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000439 for(i=0; i<argc; i++){
440 appendToEchoModule(pVtab->interp, argv[i]);
441 }
442
danielk1977cc013f82006-06-24 06:36:11 +0000443 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
444 ** structure. If an error occurs, delete the sqlite3_vtab structure and
445 ** return an error code.
446 */
danielk1977a1644fd2007-08-29 12:31:25 +0000447 rc = echoDeclareVtab(pVtab, db);
448 if( rc!=SQLITE_OK ){
danielk1977a4e76362006-06-14 06:31:28 +0000449 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977a1644fd2007-08-29 12:31:25 +0000450 return rc;
danielk1977a4e76362006-06-14 06:31:28 +0000451 }
452
danielk1977cc013f82006-06-24 06:36:11 +0000453 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000454 *ppVtab = &pVtab->base;
455 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000456}
drha967e882006-06-13 01:04:52 +0000457
danielk1977cc013f82006-06-24 06:36:11 +0000458/*
459** Echo virtual table module xCreate method.
460*/
drhb9bb7c12006-06-11 23:41:55 +0000461static int echoCreate(
462 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000463 void *pAux,
drhe4102962006-09-11 00:34:22 +0000464 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000465 sqlite3_vtab **ppVtab,
466 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000467){
danielk1977a298e902006-06-22 09:53:48 +0000468 int rc = SQLITE_OK;
danielk1977860b6cd2007-09-03 11:51:50 +0000469 appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000470 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000471
472 /* If there were two arguments passed to the module at the SQL level
473 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
474 ** the second argument is used as a table name. Attempt to create
475 ** such a table with a single column, "logmsg". This table will
476 ** be used to log calls to the xUpdate method. It will be deleted
477 ** when the virtual table is DROPed.
478 **
479 ** Note: The main point of this is to test that we can drop tables
480 ** from within an xDestroy method call.
481 */
danielk1977a298e902006-06-22 09:53:48 +0000482 if( rc==SQLITE_OK && argc==5 ){
483 char *zSql;
484 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
danielk19771e536952007-08-16 10:09:01 +0000485 pVtab->zLogName = sqlite3MPrintf(0, "%s", argv[4]);
486 zSql = sqlite3MPrintf(0, "CREATE TABLE %Q(logmsg)", pVtab->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000487 rc = sqlite3_exec(db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000488 sqlite3_free(zSql);
danielk1977cd2543b2007-09-03 15:03:20 +0000489 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +0000490 *pzErr = sqlite3DbStrDup(0, sqlite3_errmsg(db));
danielk1977cd2543b2007-09-03 15:03:20 +0000491 }
492 }
493
494 if( *ppVtab && rc!=SQLITE_OK ){
495 echoDestructor(*ppVtab);
496 *ppVtab = 0;
danielk1977a298e902006-06-22 09:53:48 +0000497 }
danielk1977cc013f82006-06-24 06:36:11 +0000498
drhcd3dd9d2008-04-28 20:27:53 +0000499 if( rc==SQLITE_OK ){
500 (*(echo_vtab**)ppVtab)->inTransaction = 1;
501 }
502
danielk1977a298e902006-06-22 09:53:48 +0000503 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000504}
danielk1977cc013f82006-06-24 06:36:11 +0000505
506/*
507** Echo virtual table module xConnect method.
508*/
drhb9bb7c12006-06-11 23:41:55 +0000509static int echoConnect(
510 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000511 void *pAux,
drhe4102962006-09-11 00:34:22 +0000512 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000513 sqlite3_vtab **ppVtab,
514 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000515){
danielk1977860b6cd2007-09-03 11:51:50 +0000516 appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000517 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000518}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000519
danielk1977cc013f82006-06-24 06:36:11 +0000520/*
521** Echo virtual table module xDisconnect method.
522*/
danielk19775fac9f82006-06-13 14:16:58 +0000523static int echoDisconnect(sqlite3_vtab *pVtab){
524 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
525 return echoDestructor(pVtab);
526}
danielk1977cc013f82006-06-24 06:36:11 +0000527
528/*
529** Echo virtual table module xDestroy method.
530*/
danielk19775fac9f82006-06-13 14:16:58 +0000531static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000532 int rc = SQLITE_OK;
533 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000534 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000535
536 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000537 if( p && p->zLogName ){
538 char *zSql;
danielk19771e536952007-08-16 10:09:01 +0000539 zSql = sqlite3MPrintf(0, "DROP TABLE %Q", p->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000540 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000541 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000542 }
danielk1977cc013f82006-06-24 06:36:11 +0000543
danielk1977a298e902006-06-22 09:53:48 +0000544 if( rc==SQLITE_OK ){
545 rc = echoDestructor(pVtab);
546 }
547 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000548}
549
danielk1977cc013f82006-06-24 06:36:11 +0000550/*
551** Echo virtual table module xOpen method.
552*/
danielk19775fac9f82006-06-13 14:16:58 +0000553static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000554 echo_cursor *pCur;
danielk19773e3a84d2008-08-01 17:37:40 +0000555 if( simulateVtabError((echo_vtab *)pVTab, "xOpen") ){
556 return SQLITE_ERROR;
557 }
danielk197731dad9d2007-08-16 11:36:15 +0000558 pCur = sqlite3MallocZero(sizeof(echo_cursor));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000559 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000560 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000561}
562
danielk1977cc013f82006-06-24 06:36:11 +0000563/*
564** Echo virtual table module xClose method.
565*/
danielk19775fac9f82006-06-13 14:16:58 +0000566static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000567 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000568 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000569 sqlite3_stmt *pStmt = pCur->pStmt;
570 pCur->pStmt = 0;
drh17435752007-08-16 04:30:38 +0000571 sqlite3_free(pCur);
danielk1977be718892006-06-23 08:05:19 +0000572 rc = sqlite3_finalize(pStmt);
573 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000574}
575
danielk1977a298e902006-06-22 09:53:48 +0000576/*
577** Return non-zero if the cursor does not currently point to a valid record
578** (i.e if the scan has finished), or zero otherwise.
579*/
580static int echoEof(sqlite3_vtab_cursor *cur){
581 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
582}
583
danielk1977cc013f82006-06-24 06:36:11 +0000584/*
585** Echo virtual table module xNext method.
586*/
danielk19775fac9f82006-06-13 14:16:58 +0000587static int echoNext(sqlite3_vtab_cursor *cur){
danielk197731dad9d2007-08-16 11:36:15 +0000588 int rc = SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000589 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000590
danielk19773e3a84d2008-08-01 17:37:40 +0000591 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xNext") ){
592 return SQLITE_ERROR;
593 }
594
danielk197731dad9d2007-08-16 11:36:15 +0000595 if( pCur->pStmt ){
596 rc = sqlite3_step(pCur->pStmt);
597 if( rc==SQLITE_ROW ){
598 rc = SQLITE_OK;
599 }else{
600 rc = sqlite3_finalize(pCur->pStmt);
601 pCur->pStmt = 0;
602 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000603 }
604
605 return rc;
606}
607
danielk1977cc013f82006-06-24 06:36:11 +0000608/*
609** Echo virtual table module xColumn method.
610*/
danielk19775fac9f82006-06-13 14:16:58 +0000611static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000612 int iCol = i + 1;
613 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40 +0000614
615 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xColumn") ){
616 return SQLITE_ERROR;
617 }
618
danielk1977fbbe0052006-06-21 07:02:33 +0000619 if( !pStmt ){
620 sqlite3_result_null(ctx);
621 }else{
622 assert( sqlite3_data_count(pStmt)>iCol );
623 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
624 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000625 return SQLITE_OK;
626}
627
danielk1977cc013f82006-06-24 06:36:11 +0000628/*
629** Echo virtual table module xRowid method.
630*/
danielk19775fac9f82006-06-13 14:16:58 +0000631static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000632 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40 +0000633
634 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xRowid") ){
635 return SQLITE_ERROR;
636 }
637
danielk1977b7a7b9a2006-06-13 10:24:42 +0000638 *pRowid = sqlite3_column_int64(pStmt, 0);
639 return SQLITE_OK;
640}
641
danielk197798331532006-06-14 10:55:52 +0000642/*
643** Compute a simple hash of the null terminated string zString.
644**
645** This module uses only sqlite3_index_info.idxStr, not
646** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
647** in echoBestIndex(), idxNum is set to the corresponding hash value.
648** In echoFilter(), code assert()s that the supplied idxNum value is
649** indeed the hash of the supplied idxStr.
650*/
651static int hashString(const char *zString){
652 int val = 0;
653 int ii;
654 for(ii=0; zString[ii]; ii++){
655 val = (val << 3) + (int)zString[ii];
656 }
657 return val;
658}
659
danielk1977cc013f82006-06-24 06:36:11 +0000660/*
661** Echo virtual table module xFilter method.
662*/
danielk19775fac9f82006-06-13 14:16:58 +0000663static int echoFilter(
664 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000665 int idxNum, const char *idxStr,
666 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000667){
668 int rc;
drh4be8b512006-06-13 23:51:34 +0000669 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000670
671 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
672 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
673 sqlite3 *db = pVtab->db;
674
danielk19773e3a84d2008-08-01 17:37:40 +0000675 if( simulateVtabError(pVtab, "xFilter") ){
676 return SQLITE_ERROR;
677 }
678
danielk1977cc013f82006-06-24 06:36:11 +0000679 /* Check that idxNum matches idxStr */
680 assert( idxNum==hashString(idxStr) );
681
682 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000683 appendToEchoModule(pVtab->interp, "xFilter");
684 appendToEchoModule(pVtab->interp, idxStr);
685 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000686 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000687 }
688
danielk19775fac9f82006-06-13 14:16:58 +0000689 sqlite3_finalize(pCur->pStmt);
690 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000691
692 /* Prepare the SQL statement created by echoBestIndex and bind the
693 ** runtime parameters passed to this function to it.
694 */
drh4be8b512006-06-13 23:51:34 +0000695 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000696 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000697 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk1977cc013f82006-06-24 06:36:11 +0000698 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000699 }
danielk1977cc013f82006-06-24 06:36:11 +0000700
701 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000702 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000703 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000704 }
705
danielk1977be718892006-06-23 08:05:19 +0000706 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000707}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000708
danielk1977cc013f82006-06-24 06:36:11 +0000709
710/*
711** A helper function used by echoUpdate() and echoBestIndex() for
712** manipulating strings in concert with the sqlite3_mprintf() function.
713**
714** Parameter pzStr points to a pointer to a string allocated with
715** sqlite3_mprintf. The second parameter, zAppend, points to another
716** string. The two strings are concatenated together and *pzStr
717** set to point at the result. The initial buffer pointed to by *pzStr
718** is deallocated via sqlite3_free().
719**
720** If the third argument, doFree, is true, then sqlite3_free() is
721** also called to free the buffer pointed to by zAppend.
722*/
danielk1977a1644fd2007-08-29 12:31:25 +0000723static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
danielk1977cc013f82006-06-24 06:36:11 +0000724 char *zIn = *pzStr;
danielk1977a1644fd2007-08-29 12:31:25 +0000725 if( !zAppend && doFree && *pRc==SQLITE_OK ){
726 *pRc = SQLITE_NOMEM;
727 }
728 if( *pRc!=SQLITE_OK ){
729 sqlite3_free(zIn);
730 zIn = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000731 }else{
danielk1977a1644fd2007-08-29 12:31:25 +0000732 if( zIn ){
733 char *zTemp = zIn;
734 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
735 sqlite3_free(zTemp);
736 }else{
737 zIn = sqlite3_mprintf("%s", zAppend);
738 }
739 if( !zIn ){
740 *pRc = SQLITE_NOMEM;
741 }
danielk1977cc013f82006-06-24 06:36:11 +0000742 }
743 *pzStr = zIn;
744 if( doFree ){
745 sqlite3_free(zAppend);
746 }
747}
748
drhb9bb7c12006-06-11 23:41:55 +0000749/*
danielk19775fac9f82006-06-13 14:16:58 +0000750** The echo module implements the subset of query constraints and sort
751** orders that may take advantage of SQLite indices on the underlying
752** real table. For example, if the real table is declared as:
753**
754** CREATE TABLE real(a, b, c);
755** CREATE INDEX real_index ON real(b);
756**
757** then the echo module handles WHERE or ORDER BY clauses that refer
758** to the column "b", but not "a" or "c". If a multi-column index is
drh85b623f2007-12-13 21:54:09 +0000759** present, only its left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000760**
761** This xBestIndex method encodes the proposed search strategy as
762** an SQL query on the real table underlying the virtual echo module
763** table and stores the query in sqlite3_index_info.idxStr. The SQL
764** statement is of the form:
765**
766** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
767**
768** where the <where-clause> and <order-by-clause> are determined
769** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000770*/
danielk19775fac9f82006-06-13 14:16:58 +0000771static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
772 int ii;
drh4be8b512006-06-13 23:51:34 +0000773 char *zQuery = 0;
774 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000775 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000776 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000777 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000778 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000779 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000780
781 int nRow;
782 int useIdx = 0;
783 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000784 int useCost = 0;
785 double cost;
danielk197739359dc2008-03-17 09:36:44 +0000786 int isIgnoreUsable = 0;
787 if( Tcl_GetVar(interp, "echo_module_ignore_usable", TCL_GLOBAL_ONLY) ){
788 isIgnoreUsable = 1;
789 }
790
danielk19773e3a84d2008-08-01 17:37:40 +0000791 if( simulateVtabError(pVtab, "xBestIndex") ){
792 return SQLITE_ERROR;
793 }
794
danielk197774cdba42006-06-19 12:02:58 +0000795 /* Determine the number of rows in the table and store this value in local
796 ** variable nRow. The 'estimated-cost' of the scan will be the number of
797 ** rows in the table for a linear scan, or the log (base 2) of the
798 ** number of rows if the proposed scan uses an index.
799 */
danielk1977780b1d92007-03-30 14:56:34 +0000800 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
801 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
802 useCost = 1;
803 } else {
804 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000805 if( !zQuery ){
806 return SQLITE_NOMEM;
807 }
danielk1977780b1d92007-03-30 14:56:34 +0000808 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
809 sqlite3_free(zQuery);
810 if( rc!=SQLITE_OK ){
811 return rc;
812 }
813 sqlite3_step(pStmt);
814 nRow = sqlite3_column_int(pStmt, 0);
815 rc = sqlite3_finalize(pStmt);
816 if( rc!=SQLITE_OK ){
817 return rc;
818 }
danielk197774cdba42006-06-19 12:02:58 +0000819 }
danielk19775fac9f82006-06-13 14:16:58 +0000820
drh4be8b512006-06-13 23:51:34 +0000821 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000822 if( !zQuery ){
823 return SQLITE_NOMEM;
824 }
danielk19775fac9f82006-06-13 14:16:58 +0000825 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
826 const struct sqlite3_index_constraint *pConstraint;
827 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000828 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000829
830 pConstraint = &pIdxInfo->aConstraint[ii];
831 pUsage = &pIdxInfo->aConstraintUsage[ii];
832
danielk197739359dc2008-03-17 09:36:44 +0000833 if( !isIgnoreUsable && !pConstraint->usable ) continue;
834
drh383736b2006-10-08 18:56:57 +0000835 iCol = pConstraint->iColumn;
danielk197799e925d2008-06-16 14:19:57 +0000836 if( pVtab->aIndex[iCol] || iCol<0 ){
danielk19775fac9f82006-06-13 14:16:58 +0000837 char *zCol = pVtab->aCol[iCol];
838 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000839 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000840 if( iCol<0 ){
841 zCol = "rowid";
842 }
danielk19775fac9f82006-06-13 14:16:58 +0000843 switch( pConstraint->op ){
844 case SQLITE_INDEX_CONSTRAINT_EQ:
845 zOp = "="; break;
846 case SQLITE_INDEX_CONSTRAINT_LT:
847 zOp = "<"; break;
848 case SQLITE_INDEX_CONSTRAINT_GT:
849 zOp = ">"; break;
850 case SQLITE_INDEX_CONSTRAINT_LE:
851 zOp = "<="; break;
852 case SQLITE_INDEX_CONSTRAINT_GE:
853 zOp = ">="; break;
854 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000855 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000856 }
drh1a90e092006-06-14 22:07:10 +0000857 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000858 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
859 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000860 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000861 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000862 }
danielk1977a1644fd2007-08-29 12:31:25 +0000863 string_concat(&zQuery, zNew, 1, &rc);
danielk1977cc013f82006-06-24 06:36:11 +0000864
drh4be8b512006-06-13 23:51:34 +0000865 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000866 pUsage->argvIndex = ++nArg;
867 pUsage->omit = 1;
868 }
869 }
danielk197747d08092006-06-14 07:41:31 +0000870
871 /* If there is only one term in the ORDER BY clause, and it is
872 ** on a column that this virtual table has an index for, then consume
873 ** the ORDER BY clause.
874 */
875 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000876 int iCol = pIdxInfo->aOrderBy->iColumn;
877 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000878 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000879 if( iCol<0 ){
880 zCol = "rowid";
881 }
danielk1977cc013f82006-06-24 06:36:11 +0000882 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
danielk1977a1644fd2007-08-29 12:31:25 +0000883 string_concat(&zQuery, zNew, 1, &rc);
danielk197747d08092006-06-14 07:41:31 +0000884 pIdxInfo->orderByConsumed = 1;
885 }
886
danielk19775fac9f82006-06-13 14:16:58 +0000887 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000888 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000889
danielk1977222a7572007-08-25 13:37:48 +0000890 if( !zQuery ){
danielk1977a1644fd2007-08-29 12:31:25 +0000891 return rc;
danielk1977222a7572007-08-25 13:37:48 +0000892 }
danielk197798331532006-06-14 10:55:52 +0000893 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000894 pIdxInfo->idxStr = zQuery;
895 pIdxInfo->needToFreeIdxStr = 1;
danielk1977780b1d92007-03-30 14:56:34 +0000896 if (useCost) {
897 pIdxInfo->estimatedCost = cost;
898 } else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000899 /* Approximation of log2(nRow). */
900 for( ii=0; ii<(sizeof(int)*8); ii++ ){
901 if( nRow & (1<<ii) ){
902 pIdxInfo->estimatedCost = (double)ii;
903 }
904 }
905 } else {
906 pIdxInfo->estimatedCost = (double)nRow;
907 }
908 return rc;
drha967e882006-06-13 01:04:52 +0000909}
910
danielk1977176f4d22006-06-15 10:41:15 +0000911/*
danielk1977cc013f82006-06-24 06:36:11 +0000912** The xUpdate method for echo module virtual tables.
913**
danielk1977176f4d22006-06-15 10:41:15 +0000914** apData[0] apData[1] apData[2..]
915**
916** INTEGER DELETE
917**
918** INTEGER NULL (nCol args) UPDATE (do not set rowid)
919** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
920**
921** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
922** NULL INTEGER (nCol args) INSERT (incl. rowid value)
923**
924*/
danielk19771f6eec52006-06-16 06:17:47 +0000925int echoUpdate(
926 sqlite3_vtab *tab,
927 int nData,
928 sqlite3_value **apData,
929 sqlite_int64 *pRowid
930){
danielk197726e41442006-06-14 15:16:35 +0000931 echo_vtab *pVtab = (echo_vtab *)tab;
932 sqlite3 *db = pVtab->db;
933 int rc = SQLITE_OK;
934
danielk1977176f4d22006-06-15 10:41:15 +0000935 sqlite3_stmt *pStmt;
936 char *z = 0; /* SQL statement to execute */
937 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
938 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
939 int i; /* Counter variable used by for loops */
940
danielk197726e41442006-06-14 15:16:35 +0000941 assert( nData==pVtab->nCol+2 || nData==1 );
942
drhcd3dd9d2008-04-28 20:27:53 +0000943 /* Ticket #3083 - make sure we always start a transaction prior to
944 ** making any changes to a virtual table */
945 assert( pVtab->inTransaction );
946
danielk19773e3a84d2008-08-01 17:37:40 +0000947 if( simulateVtabError(pVtab, "xUpdate") ){
948 return SQLITE_ERROR;
949 }
950
drh5aec0422006-06-14 23:43:31 +0000951 /* If apData[0] is an integer and nData>1 then do an UPDATE */
952 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000953 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000954 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000955 if( !z ){
956 rc = SQLITE_NOMEM;
957 }
danielk1977176f4d22006-06-15 10:41:15 +0000958
959 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
960 bindArgZero = 1;
961
962 if( bindArgOne ){
danielk1977a1644fd2007-08-29 12:31:25 +0000963 string_concat(&z, " SET rowid=?1 ", 0, &rc);
drh5aec0422006-06-14 23:43:31 +0000964 zSep = ",";
965 }
966 for(i=2; i<nData; i++){
967 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000968 string_concat(&z, sqlite3_mprintf(
danielk1977a1644fd2007-08-29 12:31:25 +0000969 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000970 zSep = ",";
971 }
danielk1977a1644fd2007-08-29 12:31:25 +0000972 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000973 }
974
danielk1977176f4d22006-06-15 10:41:15 +0000975 /* If apData[0] is an integer and nData==1 then do a DELETE */
976 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
977 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000978 if( !z ){
979 rc = SQLITE_NOMEM;
980 }
danielk1977176f4d22006-06-15 10:41:15 +0000981 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000982 }
983
danielk1977176f4d22006-06-15 10:41:15 +0000984 /* If the first argument is NULL and there are more than two args, INSERT */
985 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000986 int ii;
987 char *zInsert = 0;
988 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000989
danielk19774b2688a2006-06-20 11:01:07 +0000990 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000991 if( !zInsert ){
992 rc = SQLITE_NOMEM;
993 }
danielk1977176f4d22006-06-15 10:41:15 +0000994 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
995 bindArgOne = 1;
996 zValues = sqlite3_mprintf("?");
danielk1977a1644fd2007-08-29 12:31:25 +0000997 string_concat(&zInsert, "rowid", 0, &rc);
danielk197726e41442006-06-14 15:16:35 +0000998 }
999
danielk1977176f4d22006-06-15 10:41:15 +00001000 assert((pVtab->nCol+2)==nData);
1001 for(ii=2; ii<nData; ii++){
1002 string_concat(&zInsert,
danielk1977a1644fd2007-08-29 12:31:25 +00001003 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
danielk1977176f4d22006-06-15 10:41:15 +00001004 string_concat(&zValues,
danielk1977a1644fd2007-08-29 12:31:25 +00001005 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
danielk197726e41442006-06-14 15:16:35 +00001006 }
1007
danielk1977a1644fd2007-08-29 12:31:25 +00001008 string_concat(&z, zInsert, 1, &rc);
1009 string_concat(&z, ") VALUES(", 0, &rc);
1010 string_concat(&z, zValues, 1, &rc);
1011 string_concat(&z, ")", 0, &rc);
danielk1977176f4d22006-06-15 10:41:15 +00001012 }
1013
1014 /* Anything else is an error */
1015 else{
1016 assert(0);
1017 return SQLITE_ERROR;
1018 }
1019
danielk1977a1644fd2007-08-29 12:31:25 +00001020 if( rc==SQLITE_OK ){
1021 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
1022 }
danielk1977176f4d22006-06-15 10:41:15 +00001023 assert( rc!=SQLITE_OK || pStmt );
1024 sqlite3_free(z);
1025 if( rc==SQLITE_OK ) {
1026 if( bindArgZero ){
1027 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +00001028 }
danielk1977176f4d22006-06-15 10:41:15 +00001029 if( bindArgOne ){
1030 sqlite3_bind_value(pStmt, 1, apData[1]);
1031 }
danielk1977a7a8e142008-02-13 18:25:27 +00001032 for(i=2; i<nData && rc==SQLITE_OK; i++){
1033 if( apData[i] ) rc = sqlite3_bind_value(pStmt, i, apData[i]);
danielk1977176f4d22006-06-15 10:41:15 +00001034 }
danielk1977a7a8e142008-02-13 18:25:27 +00001035 if( rc==SQLITE_OK ){
1036 sqlite3_step(pStmt);
1037 rc = sqlite3_finalize(pStmt);
1038 }else{
1039 sqlite3_finalize(pStmt);
1040 }
danielk197726e41442006-06-14 15:16:35 +00001041 }
1042
danielk19771f6eec52006-06-16 06:17:47 +00001043 if( pRowid && rc==SQLITE_OK ){
1044 *pRowid = sqlite3_last_insert_rowid(db);
1045 }
drh4dc754d2008-07-23 18:17:32 +00001046 if( rc!=SQLITE_OK ){
1047 tab->zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", sqlite3_errmsg(db));
1048 }
danielk19771f6eec52006-06-16 06:17:47 +00001049
danielk197726e41442006-06-14 15:16:35 +00001050 return rc;
1051}
1052
drha967e882006-06-13 01:04:52 +00001053/*
danielk1977c69cdfd2006-06-17 09:39:55 +00001054** xBegin, xSync, xCommit and xRollback callbacks for echo module
1055** virtual tables. Do nothing other than add the name of the callback
1056** to the $::echo_module Tcl variable.
1057*/
1058static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
1059 char *z;
1060 echo_vtab *pVtab = (echo_vtab *)tab;
1061 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
drh344c38e2008-05-05 13:23:04 +00001062 if( z==0 ) return SQLITE_NOMEM;
danielk1977c69cdfd2006-06-17 09:39:55 +00001063 appendToEchoModule(pVtab->interp, zCall);
1064 appendToEchoModule(pVtab->interp, z);
1065 sqlite3_free(z);
drh344c38e2008-05-05 13:23:04 +00001066 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +00001067}
1068static int echoBegin(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001069 int rc;
danielk19775017dc32006-06-24 09:34:22 +00001070 echo_vtab *pVtab = (echo_vtab *)tab;
1071 Tcl_Interp *interp = pVtab->interp;
1072 const char *zVal;
1073
drhcd3dd9d2008-04-28 20:27:53 +00001074 /* Ticket #3083 - do not start a transaction if we are already in
1075 ** a transaction */
1076 assert( !pVtab->inTransaction );
1077
danielk19773e3a84d2008-08-01 17:37:40 +00001078 if( simulateVtabError(pVtab, "xBegin") ){
1079 return SQLITE_ERROR;
1080 }
1081
danielk1977a1644fd2007-08-29 12:31:25 +00001082 rc = echoTransactionCall(tab, "xBegin");
danielk19775017dc32006-06-24 09:34:22 +00001083
danielk1977a1644fd2007-08-29 12:31:25 +00001084 if( rc==SQLITE_OK ){
1085 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
1086 ** and it is set to the name of the real table underlying this virtual
1087 ** echo module table, then cause this xSync operation to fail.
1088 */
1089 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
1090 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1091 rc = SQLITE_ERROR;
1092 }
danielk19775017dc32006-06-24 09:34:22 +00001093 }
drhcd3dd9d2008-04-28 20:27:53 +00001094 if( rc==SQLITE_OK ){
1095 pVtab->inTransaction = 1;
1096 }
danielk1977a1644fd2007-08-29 12:31:25 +00001097 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001098}
1099static int echoSync(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001100 int rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001101 echo_vtab *pVtab = (echo_vtab *)tab;
1102 Tcl_Interp *interp = pVtab->interp;
1103 const char *zVal;
1104
drhcd3dd9d2008-04-28 20:27:53 +00001105 /* Ticket #3083 - Only call xSync if we have previously started a
1106 ** transaction */
1107 assert( pVtab->inTransaction );
1108
danielk19773e3a84d2008-08-01 17:37:40 +00001109 if( simulateVtabError(pVtab, "xSync") ){
1110 return SQLITE_ERROR;
1111 }
1112
danielk1977a1644fd2007-08-29 12:31:25 +00001113 rc = echoTransactionCall(tab, "xSync");
danielk1977c69cdfd2006-06-17 09:39:55 +00001114
danielk1977a1644fd2007-08-29 12:31:25 +00001115 if( rc==SQLITE_OK ){
1116 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
1117 ** and it is set to the name of the real table underlying this virtual
1118 ** echo module table, then cause this xSync operation to fail.
1119 */
1120 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
1121 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1122 rc = -1;
1123 }
danielk1977c69cdfd2006-06-17 09:39:55 +00001124 }
danielk1977a1644fd2007-08-29 12:31:25 +00001125 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001126}
1127static int echoCommit(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:53 +00001128 echo_vtab *pVtab = (echo_vtab*)tab;
drh643167f2008-01-22 21:30:53 +00001129 int rc;
drhcd3dd9d2008-04-28 20:27:53 +00001130
1131 /* Ticket #3083 - Only call xCommit if we have previously started
1132 ** a transaction */
1133 assert( pVtab->inTransaction );
1134
danielk19773e3a84d2008-08-01 17:37:40 +00001135 if( simulateVtabError(pVtab, "xCommit") ){
1136 return SQLITE_ERROR;
1137 }
1138
danielk19772d1d86f2008-06-20 14:59:51 +00001139 sqlite3BeginBenignMalloc();
drh643167f2008-01-22 21:30:53 +00001140 rc = echoTransactionCall(tab, "xCommit");
danielk19772d1d86f2008-06-20 14:59:51 +00001141 sqlite3EndBenignMalloc();
drh344c38e2008-05-05 13:23:04 +00001142 pVtab->inTransaction = 0;
drh643167f2008-01-22 21:30:53 +00001143 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001144}
1145static int echoRollback(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:53 +00001146 int rc;
1147 echo_vtab *pVtab = (echo_vtab*)tab;
1148
1149 /* Ticket #3083 - Only call xRollback if we have previously started
1150 ** a transaction */
1151 assert( pVtab->inTransaction );
1152
1153 rc = echoTransactionCall(tab, "xRollback");
1154 pVtab->inTransaction = 0;
1155 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001156}
1157
1158/*
drhe94b0c32006-07-08 18:09:15 +00001159** Implementation of "GLOB" function on the echo module. Pass
1160** all arguments to the ::echo_glob_overload procedure of TCL
1161** and return the result of that procedure as a string.
1162*/
1163static void overloadedGlobFunction(
1164 sqlite3_context *pContext,
1165 int nArg,
1166 sqlite3_value **apArg
1167){
1168 Tcl_Interp *interp = sqlite3_user_data(pContext);
1169 Tcl_DString str;
1170 int i;
1171 int rc;
1172 Tcl_DStringInit(&str);
1173 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
1174 for(i=0; i<nArg; i++){
1175 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
1176 }
1177 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
1178 Tcl_DStringFree(&str);
1179 if( rc ){
1180 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
1181 }else{
1182 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1183 -1, SQLITE_TRANSIENT);
1184 }
1185 Tcl_ResetResult(interp);
1186}
1187
1188/*
1189** This is the xFindFunction implementation for the echo module.
1190** SQLite calls this routine when the first argument of a function
1191** is a column of an echo virtual table. This routine can optionally
1192** override the implementation of that function. It will choose to
1193** do so if the function is named "glob", and a TCL command named
1194** ::echo_glob_overload exists.
1195*/
1196static int echoFindFunction(
1197 sqlite3_vtab *vtab,
1198 int nArg,
1199 const char *zFuncName,
1200 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1201 void **ppArg
1202){
1203 echo_vtab *pVtab = (echo_vtab *)vtab;
1204 Tcl_Interp *interp = pVtab->interp;
1205 Tcl_CmdInfo info;
1206 if( strcmp(zFuncName,"glob")!=0 ){
1207 return 0;
1208 }
1209 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1210 return 0;
1211 }
1212 *pxFunc = overloadedGlobFunction;
1213 *ppArg = interp;
1214 return 1;
1215}
1216
danielk1977182c4ba2007-06-27 15:53:34 +00001217static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1218 int rc = SQLITE_OK;
1219 echo_vtab *p = (echo_vtab *)vtab;
1220
danielk1977987a00e2008-08-01 17:51:47 +00001221 if( simulateVtabError(p, "xRename") ){
1222 return SQLITE_ERROR;
1223 }
1224
danielk1977182c4ba2007-06-27 15:53:34 +00001225 if( p->isPattern ){
1226 int nThis = strlen(p->zThis);
danielk19771e536952007-08-16 10:09:01 +00001227 char *zSql = sqlite3MPrintf(0, "ALTER TABLE %s RENAME TO %s%s",
danielk1977182c4ba2007-06-27 15:53:34 +00001228 p->zTableName, zNewName, &p->zTableName[nThis]
1229 );
1230 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +00001231 sqlite3_free(zSql);
danielk1977182c4ba2007-06-27 15:53:34 +00001232 }
1233
1234 return rc;
1235}
1236
drhe94b0c32006-07-08 18:09:15 +00001237/*
danielk1977cc013f82006-06-24 06:36:11 +00001238** A virtual table module that merely "echos" the contents of another
1239** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +00001240*/
1241static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +00001242 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +00001243 echoCreate,
1244 echoConnect,
drha967e882006-06-13 01:04:52 +00001245 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +00001246 echoDisconnect,
1247 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +00001248 echoOpen, /* xOpen - open a cursor */
1249 echoClose, /* xClose - close a cursor */
1250 echoFilter, /* xFilter - configure scan constraints */
1251 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +00001252 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +00001253 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +00001254 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +00001255 echoUpdate, /* xUpdate - write data */
1256 echoBegin, /* xBegin - begin transaction */
1257 echoSync, /* xSync - sync transaction */
1258 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +00001259 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +00001260 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:34 +00001261 echoRename, /* xRename - rename the table */
drhb9bb7c12006-06-11 23:41:55 +00001262};
1263
1264/*
1265** Decode a pointer to an sqlite3 object.
1266*/
drh24b58dd2008-07-07 14:50:14 +00001267extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
drhb9bb7c12006-06-11 23:41:55 +00001268
danielk1977860b6cd2007-09-03 11:51:50 +00001269static void moduleDestroy(void *p){
1270 sqlite3_free(p);
1271}
1272
drhb9bb7c12006-06-11 23:41:55 +00001273/*
1274** Register the echo virtual table module.
1275*/
1276static int register_echo_module(
1277 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1278 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1279 int objc, /* Number of arguments */
1280 Tcl_Obj *CONST objv[] /* Command arguments */
1281){
1282 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:50 +00001283 EchoModule *pMod;
drhb9bb7c12006-06-11 23:41:55 +00001284 if( objc!=2 ){
1285 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1286 return TCL_ERROR;
1287 }
1288 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977860b6cd2007-09-03 11:51:50 +00001289 pMod = sqlite3_malloc(sizeof(EchoModule));
1290 pMod->interp = interp;
1291 sqlite3_create_module_v2(db, "echo", &echoModule, (void*)pMod, moduleDestroy);
drhb9bb7c12006-06-11 23:41:55 +00001292 return TCL_OK;
1293}
1294
danielk19775017dc32006-06-24 09:34:22 +00001295/*
1296** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1297**
1298** sqlite3_declare_vtab DB SQL
1299*/
1300static int declare_vtab(
1301 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1302 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1303 int objc, /* Number of arguments */
1304 Tcl_Obj *CONST objv[] /* Command arguments */
1305){
1306 sqlite3 *db;
1307 int rc;
1308 if( objc!=3 ){
1309 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1310 return TCL_ERROR;
1311 }
1312 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1313 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1314 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001315 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001316 return TCL_ERROR;
1317 }
1318 return TCL_OK;
1319}
1320
danielk1977cc013f82006-06-24 06:36:11 +00001321#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001322
1323/*
1324** Register commands with the TCL interpreter.
1325*/
1326int Sqlitetest8_Init(Tcl_Interp *interp){
danielk19776e891622008-08-12 14:48:40 +00001327#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001328 static struct {
1329 char *zName;
1330 Tcl_ObjCmdProc *xProc;
1331 void *clientData;
1332 } aObjCmd[] = {
1333 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +00001334 { "sqlite3_declare_vtab", declare_vtab, 0 },
drhb9bb7c12006-06-11 23:41:55 +00001335 };
1336 int i;
1337 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1338 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1339 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1340 }
danielk19776e891622008-08-12 14:48:40 +00001341#endif
drhb9bb7c12006-06-11 23:41:55 +00001342 return TCL_OK;
1343}