blob: 110aa656174f9debce8026f217e6bbee127ca15e [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.
drhb9bb7c12006-06-11 23:41:55 +000015*/
16#include "sqliteInt.h"
17#include "tcl.h"
drhb9bb7c12006-06-11 23:41:55 +000018#include <stdlib.h>
19#include <string.h>
20
danielk1977cc013f82006-06-24 06:36:11 +000021#ifndef SQLITE_OMIT_VIRTUALTABLE
22
danielk1977b7a7b9a2006-06-13 10:24:42 +000023typedef struct echo_vtab echo_vtab;
24typedef struct echo_cursor echo_cursor;
25
danielk1977c69cdfd2006-06-17 09:39:55 +000026/*
danielk1977780b1d92007-03-30 14:56:34 +000027** The test module defined in this file uses four global Tcl variables to
danielk1977c69cdfd2006-06-17 09:39:55 +000028** commicate with test-scripts:
29**
30** $::echo_module
31** $::echo_module_sync_fail
danielk19775017dc32006-06-24 09:34:22 +000032** $::echo_module_begin_fail
danielk1977780b1d92007-03-30 14:56:34 +000033** $::echo_module_cost
danielk1977cc013f82006-06-24 06:36:11 +000034**
35** The variable ::echo_module is a list. Each time one of the following
36** methods is called, one or more elements are appended to the list.
37** This is used for automated testing of virtual table modules.
38**
39** The ::echo_module_sync_fail variable is set by test scripts and read
40** by code in this file. If it is set to the name of a real table in the
41** the database, then all xSync operations on echo virtual tables that
42** use the named table as a backing store will fail.
danielk1977c69cdfd2006-06-17 09:39:55 +000043*/
44
danielk19773e3a84d2008-08-01 17:37:40 +000045/*
46** Errors can be provoked within the following echo virtual table methods:
47**
48** xBestIndex xOpen xFilter xNext
49** xColumn xRowid xUpdate xSync
danielk1977987a00e2008-08-01 17:51:47 +000050** xBegin xRename
danielk19773e3a84d2008-08-01 17:37:40 +000051**
52** This is done by setting the global tcl variable:
53**
54** echo_module_fail($method,$tbl)
55**
56** where $method is set to the name of the virtual table method to fail
57** (i.e. "xBestIndex") and $tbl is the name of the table being echoed (not
58** the name of the virtual table, the name of the underlying real table).
59*/
60
danielk19775fac9f82006-06-13 14:16:58 +000061/*
danielk1977d6e8dd02006-06-15 15:38:41 +000062** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:58 +000063**
danielk1977d6e8dd02006-06-15 15:38:41 +000064** echo.vtab.aIndex is an array of booleans. The nth entry is true if
65** the nth column of the real table is the left-most column of an index
66** (implicit or otherwise). In other words, if SQLite can optimize
67** a query like "SELECT * FROM real_table WHERE col = ?".
68**
danielk1977c69cdfd2006-06-17 09:39:55 +000069** Member variable aCol[] contains copies of the column names of the real
70** table.
danielk19775fac9f82006-06-13 14:16:58 +000071*/
danielk1977b7a7b9a2006-06-13 10:24:42 +000072struct echo_vtab {
73 sqlite3_vtab base;
danielk1977cc013f82006-06-24 06:36:11 +000074 Tcl_Interp *interp; /* Tcl interpreter containing debug variables */
75 sqlite3 *db; /* Database connection */
danielk19775fac9f82006-06-13 14:16:58 +000076
danielk1977182c4ba2007-06-27 15:53:34 +000077 int isPattern;
drhcd3dd9d2008-04-28 20:27:53 +000078 int inTransaction; /* True if within a transaction */
danielk1977182c4ba2007-06-27 15:53:34 +000079 char *zThis; /* Name of the echo table */
danielk1977a4e76362006-06-14 06:31:28 +000080 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:48 +000081 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:28 +000082 int nCol; /* Number of columns in the real table */
83 int *aIndex; /* Array of size nCol. True if column has an index */
84 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000085};
86
87/* An echo cursor object */
88struct echo_cursor {
89 sqlite3_vtab_cursor base;
90 sqlite3_stmt *pStmt;
danielk1977b7a7b9a2006-06-13 10:24:42 +000091};
92
danielk19773e3a84d2008-08-01 17:37:40 +000093static int simulateVtabError(echo_vtab *p, const char *zMethod){
94 const char *zErr;
95 char zVarname[128];
96 zVarname[127] = '\0';
shanef5eccb62008-08-31 00:29:08 +000097 sqlite3_snprintf(127, zVarname, "echo_module_fail(%s,%s)", zMethod, p->zTableName);
danielk19773e3a84d2008-08-01 17:37:40 +000098 zErr = Tcl_GetVar(p->interp, zVarname, TCL_GLOBAL_ONLY);
99 if( zErr ){
100 p->base.zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", zErr);
101 }
102 return (zErr!=0);
103}
104
danielk1977cc013f82006-06-24 06:36:11 +0000105/*
danielk1977182c4ba2007-06-27 15:53:34 +0000106** Convert an SQL-style quoted string into a normal string by removing
107** the quote characters. The conversion is done in-place. If the
108** input does not begin with a quote character, then this routine
109** is a no-op.
110**
111** Examples:
112**
113** "abc" becomes abc
114** 'xyz' becomes xyz
115** [pqr] becomes pqr
116** `mno` becomes mno
117*/
118static void dequoteString(char *z){
119 int quote;
120 int i, j;
121 if( z==0 ) return;
122 quote = z[0];
123 switch( quote ){
124 case '\'': break;
125 case '"': break;
126 case '`': break; /* For MySQL compatibility */
127 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
128 default: return;
129 }
130 for(i=1, j=0; z[i]; i++){
131 if( z[i]==quote ){
132 if( z[i+1]==quote ){
133 z[j++] = quote;
134 i++;
135 }else{
136 z[j++] = 0;
137 break;
138 }
139 }else{
140 z[j++] = z[i];
141 }
142 }
143}
144
145/*
danielk1977cc013f82006-06-24 06:36:11 +0000146** Retrieve the column names for the table named zTab via database
147** connection db. SQLITE_OK is returned on success, or an sqlite error
148** code otherwise.
149**
150** If successful, the number of columns is written to *pnCol. *paCol is
drh17435752007-08-16 04:30:38 +0000151** set to point at sqlite3_malloc()'d space containing the array of
152** nCol column names. The caller is responsible for calling sqlite3_free
danielk1977cc013f82006-06-24 06:36:11 +0000153** on *paCol.
154*/
danielk19775fac9f82006-06-13 14:16:58 +0000155static int getColumnNames(
156 sqlite3 *db,
157 const char *zTab,
158 char ***paCol,
159 int *pnCol
160){
161 char **aCol = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000162 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000163 sqlite3_stmt *pStmt = 0;
164 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19 +0000165 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000166
danielk1977cc013f82006-06-24 06:36:11 +0000167 /* Prepare the statement "SELECT * FROM <tbl>". The column names
168 ** of the result set of the compiled SELECT will be the same as
169 ** the column names of table <tbl>.
170 */
drh17435752007-08-16 04:30:38 +0000171 zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000172 if( !zSql ){
173 rc = SQLITE_NOMEM;
174 goto out;
175 }
176 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000177 sqlite3_free(zSql);
danielk1977cc013f82006-06-24 06:36:11 +0000178
danielk19775fac9f82006-06-13 14:16:58 +0000179 if( rc==SQLITE_OK ){
180 int ii;
danielk1977cc013f82006-06-24 06:36:11 +0000181 int nBytes;
182 char *zSpace;
danielk19775fac9f82006-06-13 14:16:58 +0000183 nCol = sqlite3_column_count(pStmt);
danielk1977cc013f82006-06-24 06:36:11 +0000184
185 /* Figure out how much space to allocate for the array of column names
186 ** (including space for the strings themselves). Then allocate it.
187 */
188 nBytes = sizeof(char *) * nCol;
189 for(ii=0; ii<nCol; ii++){
danielk1977a7a8e142008-02-13 18:25:27 +0000190 const char *zName = sqlite3_column_name(pStmt, ii);
191 if( !zName ){
192 rc = SQLITE_NOMEM;
193 goto out;
194 }
195 nBytes += strlen(zName)+1;
danielk1977cc013f82006-06-24 06:36:11 +0000196 }
danielk197731dad9d2007-08-16 11:36:15 +0000197 aCol = (char **)sqlite3MallocZero(nBytes);
danielk19775fac9f82006-06-13 14:16:58 +0000198 if( !aCol ){
199 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11 +0000200 goto out;
danielk19775fac9f82006-06-13 14:16:58 +0000201 }
danielk1977cc013f82006-06-24 06:36:11 +0000202
203 /* Copy the column names into the allocated space and set up the
204 ** pointers in the aCol[] array.
205 */
206 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58 +0000207 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11 +0000208 aCol[ii] = zSpace;
209 zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
210 zSpace++;
danielk19775fac9f82006-06-13 14:16:58 +0000211 }
danielk1977cc013f82006-06-24 06:36:11 +0000212 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58 +0000213 }
214
215 *paCol = aCol;
216 *pnCol = nCol;
217
danielk1977cc013f82006-06-24 06:36:11 +0000218out:
danielk19775fac9f82006-06-13 14:16:58 +0000219 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000220 return rc;
221}
222
danielk1977cc013f82006-06-24 06:36:11 +0000223/*
224** Parameter zTab is the name of a table in database db with nCol
225** columns. This function allocates an array of integers nCol in
226** size and populates it according to any implicit or explicit
227** indices on table zTab.
228**
229** If successful, SQLITE_OK is returned and *paIndex set to point
230** at the allocated array. Otherwise, an error code is returned.
231**
232** See comments associated with the member variable aIndex above
233** "struct echo_vtab" for details of the contents of the array.
234*/
235static int getIndexArray(
236 sqlite3 *db, /* Database connection */
237 const char *zTab, /* Name of table in database db */
238 int nCol,
239 int **paIndex
240){
danielk19775fac9f82006-06-13 14:16:58 +0000241 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000242 int *aIndex = 0;
243 int rc;
danielk1977cc013f82006-06-24 06:36:11 +0000244 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000245
danielk1977cc013f82006-06-24 06:36:11 +0000246 /* Allocate space for the index array */
danielk197731dad9d2007-08-16 11:36:15 +0000247 aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000248 if( !aIndex ){
249 rc = SQLITE_NOMEM;
250 goto get_index_array_out;
251 }
252
danielk1977cc013f82006-06-24 06:36:11 +0000253 /* Compile an sqlite pragma to loop through all indices on table zTab */
drhbc6160b2009-04-08 15:45:31 +0000254 zSql = sqlite3_mprintf("PRAGMA index_list(%s)", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000255 if( !zSql ){
256 rc = SQLITE_NOMEM;
257 goto get_index_array_out;
258 }
259 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000260 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000261
danielk1977cc013f82006-06-24 06:36:11 +0000262 /* For each index, figure out the left-most column and set the
263 ** corresponding entry in aIndex[] to 1.
264 */
danielk19775fac9f82006-06-13 14:16:58 +0000265 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000266 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58 +0000267 sqlite3_stmt *pStmt2 = 0;
drhbc6160b2009-04-08 15:45:31 +0000268 zSql = sqlite3_mprintf("PRAGMA index_info(%s)", zIdx);
danielk1977cc013f82006-06-24 06:36:11 +0000269 if( !zSql ){
270 rc = SQLITE_NOMEM;
271 goto get_index_array_out;
272 }
273 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
drh17435752007-08-16 04:30:38 +0000274 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000275 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
276 int cid = sqlite3_column_int(pStmt2, 1);
277 assert( cid>=0 && cid<nCol );
278 aIndex[cid] = 1;
279 }
danielk1977be718892006-06-23 08:05:19 +0000280 if( pStmt2 ){
281 rc = sqlite3_finalize(pStmt2);
282 }
danielk19775fac9f82006-06-13 14:16:58 +0000283 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000284 goto get_index_array_out;
285 }
286 }
287
danielk19775fac9f82006-06-13 14:16:58 +0000288
289get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000290 if( pStmt ){
291 int rc2 = sqlite3_finalize(pStmt);
292 if( rc==SQLITE_OK ){
293 rc = rc2;
294 }
295 }
danielk19775fac9f82006-06-13 14:16:58 +0000296 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000297 sqlite3_free(aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000298 aIndex = 0;
299 }
300 *paIndex = aIndex;
301 return rc;
302}
303
danielk197778efaba2006-06-12 06:09:17 +0000304/*
305** Global Tcl variable $echo_module is a list. This routine appends
306** the string element zArg to that list in interpreter interp.
307*/
drha967e882006-06-13 01:04:52 +0000308static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000309 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000310 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000311}
312
danielk19777e6ebfb2006-06-12 11:24:37 +0000313/*
314** This function is called from within the echo-modules xCreate and
315** xConnect methods. The argc and argv arguments are copies of those
316** passed to the calling method. This function is responsible for
317** calling sqlite3_declare_vtab() to declare the schema of the virtual
318** table being created or connected.
319**
320** If the constructor was passed just one argument, i.e.:
321**
322** CREATE TABLE t1 AS echo(t2);
323**
324** Then t2 is assumed to be the name of a *real* database table. The
325** schema of the virtual table is declared by passing a copy of the
326** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
327** Hence, the virtual table should have exactly the same column names and
328** types as the real table.
329*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000330static int echoDeclareVtab(
331 echo_vtab *pVtab,
danielk1977182c4ba2007-06-27 15:53:34 +0000332 sqlite3 *db
danielk1977b7a7b9a2006-06-13 10:24:42 +0000333){
danielk19777e6ebfb2006-06-12 11:24:37 +0000334 int rc = SQLITE_OK;
335
danielk1977182c4ba2007-06-27 15:53:34 +0000336 if( pVtab->zTableName ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000337 sqlite3_stmt *pStmt = 0;
danielk1977222a7572007-08-25 13:37:48 +0000338 rc = sqlite3_prepare(db,
danielk19777e6ebfb2006-06-12 11:24:37 +0000339 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
340 -1, &pStmt, 0);
danielk1977222a7572007-08-25 13:37:48 +0000341 if( rc==SQLITE_OK ){
342 sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
343 if( sqlite3_step(pStmt)==SQLITE_ROW ){
344 int rc2;
345 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
346 rc = sqlite3_declare_vtab(db, zCreateTable);
347 rc2 = sqlite3_finalize(pStmt);
348 if( rc==SQLITE_OK ){
349 rc = rc2;
350 }
351 } else {
352 rc = sqlite3_finalize(pStmt);
353 if( rc==SQLITE_OK ){
354 rc = SQLITE_ERROR;
355 }
356 }
danielk19777fa5dd12007-04-18 17:04:00 +0000357 if( rc==SQLITE_OK ){
danielk1977222a7572007-08-25 13:37:48 +0000358 rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
danielk19777fa5dd12007-04-18 17:04:00 +0000359 }
danielk1977222a7572007-08-25 13:37:48 +0000360 if( rc==SQLITE_OK ){
361 rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
danielk1977be718892006-06-23 08:05:19 +0000362 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000363 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000364 }
365
366 return rc;
367}
368
danielk1977cc013f82006-06-24 06:36:11 +0000369/*
370** This function frees all runtime structures associated with the virtual
371** table pVtab.
372*/
danielk1977a4e76362006-06-14 06:31:28 +0000373static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000374 echo_vtab *p = (echo_vtab*)pVtab;
drh17435752007-08-16 04:30:38 +0000375 sqlite3_free(p->aIndex);
376 sqlite3_free(p->aCol);
377 sqlite3_free(p->zThis);
378 sqlite3_free(p->zTableName);
379 sqlite3_free(p->zLogName);
380 sqlite3_free(p);
danielk1977a4e76362006-06-14 06:31:28 +0000381 return 0;
382}
383
danielk1977860b6cd2007-09-03 11:51:50 +0000384typedef struct EchoModule EchoModule;
385struct EchoModule {
386 Tcl_Interp *interp;
387};
388
danielk1977cc013f82006-06-24 06:36:11 +0000389/*
390** This function is called to do the work of the xConnect() method -
391** to allocate the required in-memory structures for a newly connected
392** virtual table.
393*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000394static int echoConstructor(
395 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000396 void *pAux,
drhe4102962006-09-11 00:34:22 +0000397 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000398 sqlite3_vtab **ppVtab,
399 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000400){
danielk1977a1644fd2007-08-29 12:31:25 +0000401 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000402 int i;
403 echo_vtab *pVtab;
404
danielk1977cc013f82006-06-24 06:36:11 +0000405 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk197731dad9d2007-08-16 11:36:15 +0000406 pVtab = sqlite3MallocZero( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000407 if( !pVtab ){
408 return SQLITE_NOMEM;
409 }
danielk1977860b6cd2007-09-03 11:51:50 +0000410 pVtab->interp = ((EchoModule *)pAux)->interp;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000411 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000412
danielk1977182c4ba2007-06-27 15:53:34 +0000413 /* Allocate echo_vtab.zThis */
drhbc6160b2009-04-08 15:45:31 +0000414 pVtab->zThis = sqlite3_mprintf("%s", argv[2]);
danielk1977182c4ba2007-06-27 15:53:34 +0000415 if( !pVtab->zThis ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000416 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000417 return SQLITE_NOMEM;
418 }
419
danielk1977182c4ba2007-06-27 15:53:34 +0000420 /* Allocate echo_vtab.zTableName */
421 if( argc>3 ){
drhbc6160b2009-04-08 15:45:31 +0000422 pVtab->zTableName = sqlite3_mprintf("%s", argv[3]);
danielk1977182c4ba2007-06-27 15:53:34 +0000423 dequoteString(pVtab->zTableName);
424 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
drhbc6160b2009-04-08 15:45:31 +0000425 char *z = sqlite3_mprintf("%s%s", argv[2], &(pVtab->zTableName[1]));
drh17435752007-08-16 04:30:38 +0000426 sqlite3_free(pVtab->zTableName);
danielk1977182c4ba2007-06-27 15:53:34 +0000427 pVtab->zTableName = z;
428 pVtab->isPattern = 1;
429 }
430 if( !pVtab->zTableName ){
431 echoDestructor((sqlite3_vtab *)pVtab);
432 return SQLITE_NOMEM;
433 }
434 }
435
danielk1977cc013f82006-06-24 06:36:11 +0000436 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000437 for(i=0; i<argc; i++){
438 appendToEchoModule(pVtab->interp, argv[i]);
439 }
440
danielk1977cc013f82006-06-24 06:36:11 +0000441 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
442 ** structure. If an error occurs, delete the sqlite3_vtab structure and
443 ** return an error code.
444 */
danielk1977a1644fd2007-08-29 12:31:25 +0000445 rc = echoDeclareVtab(pVtab, db);
446 if( rc!=SQLITE_OK ){
danielk1977a4e76362006-06-14 06:31:28 +0000447 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977a1644fd2007-08-29 12:31:25 +0000448 return rc;
danielk1977a4e76362006-06-14 06:31:28 +0000449 }
450
danielk1977cc013f82006-06-24 06:36:11 +0000451 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000452 *ppVtab = &pVtab->base;
453 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000454}
drha967e882006-06-13 01:04:52 +0000455
danielk1977cc013f82006-06-24 06:36:11 +0000456/*
457** Echo virtual table module xCreate method.
458*/
drhb9bb7c12006-06-11 23:41:55 +0000459static int echoCreate(
460 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000461 void *pAux,
drhe4102962006-09-11 00:34:22 +0000462 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000463 sqlite3_vtab **ppVtab,
464 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000465){
danielk1977a298e902006-06-22 09:53:48 +0000466 int rc = SQLITE_OK;
danielk1977860b6cd2007-09-03 11:51:50 +0000467 appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000468 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000469
470 /* If there were two arguments passed to the module at the SQL level
471 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
472 ** the second argument is used as a table name. Attempt to create
473 ** such a table with a single column, "logmsg". This table will
474 ** be used to log calls to the xUpdate method. It will be deleted
475 ** when the virtual table is DROPed.
476 **
477 ** Note: The main point of this is to test that we can drop tables
478 ** from within an xDestroy method call.
479 */
danielk1977a298e902006-06-22 09:53:48 +0000480 if( rc==SQLITE_OK && argc==5 ){
481 char *zSql;
482 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
drhbc6160b2009-04-08 15:45:31 +0000483 pVtab->zLogName = sqlite3_mprintf("%s", argv[4]);
484 zSql = sqlite3_mprintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000485 rc = sqlite3_exec(db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000486 sqlite3_free(zSql);
danielk1977cd2543b2007-09-03 15:03:20 +0000487 if( rc!=SQLITE_OK ){
drh633e6d52008-07-28 19:34:53 +0000488 *pzErr = sqlite3DbStrDup(0, sqlite3_errmsg(db));
danielk1977cd2543b2007-09-03 15:03:20 +0000489 }
490 }
491
492 if( *ppVtab && rc!=SQLITE_OK ){
493 echoDestructor(*ppVtab);
494 *ppVtab = 0;
danielk1977a298e902006-06-22 09:53:48 +0000495 }
danielk1977cc013f82006-06-24 06:36:11 +0000496
drhcd3dd9d2008-04-28 20:27:53 +0000497 if( rc==SQLITE_OK ){
498 (*(echo_vtab**)ppVtab)->inTransaction = 1;
499 }
500
danielk1977a298e902006-06-22 09:53:48 +0000501 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000502}
danielk1977cc013f82006-06-24 06:36:11 +0000503
504/*
505** Echo virtual table module xConnect method.
506*/
drhb9bb7c12006-06-11 23:41:55 +0000507static int echoConnect(
508 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000509 void *pAux,
drhe4102962006-09-11 00:34:22 +0000510 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000511 sqlite3_vtab **ppVtab,
512 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000513){
danielk1977860b6cd2007-09-03 11:51:50 +0000514 appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000515 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000516}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000517
danielk1977cc013f82006-06-24 06:36:11 +0000518/*
519** Echo virtual table module xDisconnect method.
520*/
danielk19775fac9f82006-06-13 14:16:58 +0000521static int echoDisconnect(sqlite3_vtab *pVtab){
522 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
523 return echoDestructor(pVtab);
524}
danielk1977cc013f82006-06-24 06:36:11 +0000525
526/*
527** Echo virtual table module xDestroy method.
528*/
danielk19775fac9f82006-06-13 14:16:58 +0000529static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000530 int rc = SQLITE_OK;
531 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000532 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000533
534 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000535 if( p && p->zLogName ){
536 char *zSql;
drhbc6160b2009-04-08 15:45:31 +0000537 zSql = sqlite3_mprintf("DROP TABLE %Q", p->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000538 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000539 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000540 }
danielk1977cc013f82006-06-24 06:36:11 +0000541
danielk1977a298e902006-06-22 09:53:48 +0000542 if( rc==SQLITE_OK ){
543 rc = echoDestructor(pVtab);
544 }
545 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000546}
547
danielk1977cc013f82006-06-24 06:36:11 +0000548/*
549** Echo virtual table module xOpen method.
550*/
danielk19775fac9f82006-06-13 14:16:58 +0000551static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000552 echo_cursor *pCur;
danielk19773e3a84d2008-08-01 17:37:40 +0000553 if( simulateVtabError((echo_vtab *)pVTab, "xOpen") ){
554 return SQLITE_ERROR;
555 }
danielk197731dad9d2007-08-16 11:36:15 +0000556 pCur = sqlite3MallocZero(sizeof(echo_cursor));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000557 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000558 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000559}
560
danielk1977cc013f82006-06-24 06:36:11 +0000561/*
562** Echo virtual table module xClose method.
563*/
danielk19775fac9f82006-06-13 14:16:58 +0000564static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000565 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000566 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000567 sqlite3_stmt *pStmt = pCur->pStmt;
568 pCur->pStmt = 0;
drh17435752007-08-16 04:30:38 +0000569 sqlite3_free(pCur);
danielk1977be718892006-06-23 08:05:19 +0000570 rc = sqlite3_finalize(pStmt);
571 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000572}
573
danielk1977a298e902006-06-22 09:53:48 +0000574/*
575** Return non-zero if the cursor does not currently point to a valid record
576** (i.e if the scan has finished), or zero otherwise.
577*/
578static int echoEof(sqlite3_vtab_cursor *cur){
579 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
580}
581
danielk1977cc013f82006-06-24 06:36:11 +0000582/*
583** Echo virtual table module xNext method.
584*/
danielk19775fac9f82006-06-13 14:16:58 +0000585static int echoNext(sqlite3_vtab_cursor *cur){
danielk197731dad9d2007-08-16 11:36:15 +0000586 int rc = SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000587 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000588
danielk19773e3a84d2008-08-01 17:37:40 +0000589 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xNext") ){
590 return SQLITE_ERROR;
591 }
592
danielk197731dad9d2007-08-16 11:36:15 +0000593 if( pCur->pStmt ){
594 rc = sqlite3_step(pCur->pStmt);
595 if( rc==SQLITE_ROW ){
596 rc = SQLITE_OK;
597 }else{
598 rc = sqlite3_finalize(pCur->pStmt);
599 pCur->pStmt = 0;
600 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000601 }
602
603 return rc;
604}
605
danielk1977cc013f82006-06-24 06:36:11 +0000606/*
607** Echo virtual table module xColumn method.
608*/
danielk19775fac9f82006-06-13 14:16:58 +0000609static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000610 int iCol = i + 1;
611 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40 +0000612
613 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xColumn") ){
614 return SQLITE_ERROR;
615 }
616
danielk1977fbbe0052006-06-21 07:02:33 +0000617 if( !pStmt ){
618 sqlite3_result_null(ctx);
619 }else{
620 assert( sqlite3_data_count(pStmt)>iCol );
621 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
622 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000623 return SQLITE_OK;
624}
625
danielk1977cc013f82006-06-24 06:36:11 +0000626/*
627** Echo virtual table module xRowid method.
628*/
danielk19775fac9f82006-06-13 14:16:58 +0000629static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000630 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40 +0000631
632 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xRowid") ){
633 return SQLITE_ERROR;
634 }
635
danielk1977b7a7b9a2006-06-13 10:24:42 +0000636 *pRowid = sqlite3_column_int64(pStmt, 0);
637 return SQLITE_OK;
638}
639
danielk197798331532006-06-14 10:55:52 +0000640/*
641** Compute a simple hash of the null terminated string zString.
642**
643** This module uses only sqlite3_index_info.idxStr, not
644** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
645** in echoBestIndex(), idxNum is set to the corresponding hash value.
646** In echoFilter(), code assert()s that the supplied idxNum value is
647** indeed the hash of the supplied idxStr.
648*/
649static int hashString(const char *zString){
650 int val = 0;
651 int ii;
652 for(ii=0; zString[ii]; ii++){
653 val = (val << 3) + (int)zString[ii];
654 }
655 return val;
656}
657
danielk1977cc013f82006-06-24 06:36:11 +0000658/*
659** Echo virtual table module xFilter method.
660*/
danielk19775fac9f82006-06-13 14:16:58 +0000661static int echoFilter(
662 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000663 int idxNum, const char *idxStr,
664 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000665){
666 int rc;
drh4be8b512006-06-13 23:51:34 +0000667 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000668
669 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
670 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
671 sqlite3 *db = pVtab->db;
672
danielk19773e3a84d2008-08-01 17:37:40 +0000673 if( simulateVtabError(pVtab, "xFilter") ){
674 return SQLITE_ERROR;
675 }
676
danielk1977cc013f82006-06-24 06:36:11 +0000677 /* Check that idxNum matches idxStr */
678 assert( idxNum==hashString(idxStr) );
679
680 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000681 appendToEchoModule(pVtab->interp, "xFilter");
682 appendToEchoModule(pVtab->interp, idxStr);
683 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000684 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000685 }
686
danielk19775fac9f82006-06-13 14:16:58 +0000687 sqlite3_finalize(pCur->pStmt);
688 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000689
690 /* Prepare the SQL statement created by echoBestIndex and bind the
691 ** runtime parameters passed to this function to it.
692 */
drh4be8b512006-06-13 23:51:34 +0000693 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000694 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000695 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk19776eacd282009-04-29 11:50:53 +0000696 rc = sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000697 }
danielk1977cc013f82006-06-24 06:36:11 +0000698
699 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000700 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000701 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000702 }
703
danielk1977be718892006-06-23 08:05:19 +0000704 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000705}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000706
danielk1977cc013f82006-06-24 06:36:11 +0000707
708/*
709** A helper function used by echoUpdate() and echoBestIndex() for
710** manipulating strings in concert with the sqlite3_mprintf() function.
711**
712** Parameter pzStr points to a pointer to a string allocated with
713** sqlite3_mprintf. The second parameter, zAppend, points to another
714** string. The two strings are concatenated together and *pzStr
715** set to point at the result. The initial buffer pointed to by *pzStr
716** is deallocated via sqlite3_free().
717**
718** If the third argument, doFree, is true, then sqlite3_free() is
719** also called to free the buffer pointed to by zAppend.
720*/
danielk1977a1644fd2007-08-29 12:31:25 +0000721static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
danielk1977cc013f82006-06-24 06:36:11 +0000722 char *zIn = *pzStr;
danielk1977a1644fd2007-08-29 12:31:25 +0000723 if( !zAppend && doFree && *pRc==SQLITE_OK ){
724 *pRc = SQLITE_NOMEM;
725 }
726 if( *pRc!=SQLITE_OK ){
727 sqlite3_free(zIn);
728 zIn = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000729 }else{
danielk1977a1644fd2007-08-29 12:31:25 +0000730 if( zIn ){
731 char *zTemp = zIn;
732 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
733 sqlite3_free(zTemp);
734 }else{
735 zIn = sqlite3_mprintf("%s", zAppend);
736 }
737 if( !zIn ){
738 *pRc = SQLITE_NOMEM;
739 }
danielk1977cc013f82006-06-24 06:36:11 +0000740 }
741 *pzStr = zIn;
742 if( doFree ){
743 sqlite3_free(zAppend);
744 }
745}
746
drhb9bb7c12006-06-11 23:41:55 +0000747/*
danielk19775fac9f82006-06-13 14:16:58 +0000748** The echo module implements the subset of query constraints and sort
749** orders that may take advantage of SQLite indices on the underlying
750** real table. For example, if the real table is declared as:
751**
752** CREATE TABLE real(a, b, c);
753** CREATE INDEX real_index ON real(b);
754**
755** then the echo module handles WHERE or ORDER BY clauses that refer
756** to the column "b", but not "a" or "c". If a multi-column index is
drh85b623f2007-12-13 21:54:09 +0000757** present, only its left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000758**
759** This xBestIndex method encodes the proposed search strategy as
760** an SQL query on the real table underlying the virtual echo module
761** table and stores the query in sqlite3_index_info.idxStr. The SQL
762** statement is of the form:
763**
764** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
765**
766** where the <where-clause> and <order-by-clause> are determined
767** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000768*/
danielk19775fac9f82006-06-13 14:16:58 +0000769static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
770 int ii;
drh4be8b512006-06-13 23:51:34 +0000771 char *zQuery = 0;
772 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000773 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000774 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000775 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000776 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000777 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000778
779 int nRow;
780 int useIdx = 0;
781 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000782 int useCost = 0;
783 double cost;
danielk197739359dc2008-03-17 09:36:44 +0000784 int isIgnoreUsable = 0;
785 if( Tcl_GetVar(interp, "echo_module_ignore_usable", TCL_GLOBAL_ONLY) ){
786 isIgnoreUsable = 1;
787 }
788
danielk19773e3a84d2008-08-01 17:37:40 +0000789 if( simulateVtabError(pVtab, "xBestIndex") ){
790 return SQLITE_ERROR;
791 }
792
danielk197774cdba42006-06-19 12:02:58 +0000793 /* Determine the number of rows in the table and store this value in local
794 ** variable nRow. The 'estimated-cost' of the scan will be the number of
795 ** rows in the table for a linear scan, or the log (base 2) of the
796 ** number of rows if the proposed scan uses an index.
797 */
danielk1977780b1d92007-03-30 14:56:34 +0000798 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
799 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
800 useCost = 1;
801 } else {
802 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000803 if( !zQuery ){
804 return SQLITE_NOMEM;
805 }
danielk1977780b1d92007-03-30 14:56:34 +0000806 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
807 sqlite3_free(zQuery);
808 if( rc!=SQLITE_OK ){
809 return rc;
810 }
811 sqlite3_step(pStmt);
812 nRow = sqlite3_column_int(pStmt, 0);
813 rc = sqlite3_finalize(pStmt);
814 if( rc!=SQLITE_OK ){
815 return rc;
816 }
danielk197774cdba42006-06-19 12:02:58 +0000817 }
danielk19775fac9f82006-06-13 14:16:58 +0000818
drh4be8b512006-06-13 23:51:34 +0000819 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000820 if( !zQuery ){
821 return SQLITE_NOMEM;
822 }
danielk19775fac9f82006-06-13 14:16:58 +0000823 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
824 const struct sqlite3_index_constraint *pConstraint;
825 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000826 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000827
828 pConstraint = &pIdxInfo->aConstraint[ii];
829 pUsage = &pIdxInfo->aConstraintUsage[ii];
830
danielk197739359dc2008-03-17 09:36:44 +0000831 if( !isIgnoreUsable && !pConstraint->usable ) continue;
832
drh383736b2006-10-08 18:56:57 +0000833 iCol = pConstraint->iColumn;
danielk197799e925d2008-06-16 14:19:57 +0000834 if( pVtab->aIndex[iCol] || iCol<0 ){
danielk19775fac9f82006-06-13 14:16:58 +0000835 char *zCol = pVtab->aCol[iCol];
836 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000837 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000838 if( iCol<0 ){
839 zCol = "rowid";
840 }
danielk19775fac9f82006-06-13 14:16:58 +0000841 switch( pConstraint->op ){
842 case SQLITE_INDEX_CONSTRAINT_EQ:
843 zOp = "="; break;
844 case SQLITE_INDEX_CONSTRAINT_LT:
845 zOp = "<"; break;
846 case SQLITE_INDEX_CONSTRAINT_GT:
847 zOp = ">"; break;
848 case SQLITE_INDEX_CONSTRAINT_LE:
849 zOp = "<="; break;
850 case SQLITE_INDEX_CONSTRAINT_GE:
851 zOp = ">="; break;
852 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000853 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000854 }
drh1a90e092006-06-14 22:07:10 +0000855 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000856 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
857 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000858 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000859 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000860 }
danielk1977a1644fd2007-08-29 12:31:25 +0000861 string_concat(&zQuery, zNew, 1, &rc);
danielk1977cc013f82006-06-24 06:36:11 +0000862
drh4be8b512006-06-13 23:51:34 +0000863 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000864 pUsage->argvIndex = ++nArg;
865 pUsage->omit = 1;
866 }
867 }
danielk197747d08092006-06-14 07:41:31 +0000868
869 /* If there is only one term in the ORDER BY clause, and it is
870 ** on a column that this virtual table has an index for, then consume
871 ** the ORDER BY clause.
872 */
873 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000874 int iCol = pIdxInfo->aOrderBy->iColumn;
875 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000876 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000877 if( iCol<0 ){
878 zCol = "rowid";
879 }
danielk1977cc013f82006-06-24 06:36:11 +0000880 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
danielk1977a1644fd2007-08-29 12:31:25 +0000881 string_concat(&zQuery, zNew, 1, &rc);
danielk197747d08092006-06-14 07:41:31 +0000882 pIdxInfo->orderByConsumed = 1;
883 }
884
danielk19775fac9f82006-06-13 14:16:58 +0000885 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000886 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000887
danielk1977222a7572007-08-25 13:37:48 +0000888 if( !zQuery ){
danielk1977a1644fd2007-08-29 12:31:25 +0000889 return rc;
danielk1977222a7572007-08-25 13:37:48 +0000890 }
danielk197798331532006-06-14 10:55:52 +0000891 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000892 pIdxInfo->idxStr = zQuery;
893 pIdxInfo->needToFreeIdxStr = 1;
danielk19771d461462009-04-21 09:02:45 +0000894 if( useCost ){
danielk1977780b1d92007-03-30 14:56:34 +0000895 pIdxInfo->estimatedCost = cost;
danielk19771d461462009-04-21 09:02:45 +0000896 }else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000897 /* Approximation of log2(nRow). */
898 for( ii=0; ii<(sizeof(int)*8); ii++ ){
899 if( nRow & (1<<ii) ){
900 pIdxInfo->estimatedCost = (double)ii;
901 }
902 }
danielk19771d461462009-04-21 09:02:45 +0000903 }else{
danielk197774cdba42006-06-19 12:02:58 +0000904 pIdxInfo->estimatedCost = (double)nRow;
905 }
906 return rc;
drha967e882006-06-13 01:04:52 +0000907}
908
danielk1977176f4d22006-06-15 10:41:15 +0000909/*
danielk1977cc013f82006-06-24 06:36:11 +0000910** The xUpdate method for echo module virtual tables.
911**
danielk1977176f4d22006-06-15 10:41:15 +0000912** apData[0] apData[1] apData[2..]
913**
914** INTEGER DELETE
915**
916** INTEGER NULL (nCol args) UPDATE (do not set rowid)
917** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
918**
919** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
920** NULL INTEGER (nCol args) INSERT (incl. rowid value)
921**
922*/
danielk19771f6eec52006-06-16 06:17:47 +0000923int echoUpdate(
924 sqlite3_vtab *tab,
925 int nData,
926 sqlite3_value **apData,
927 sqlite_int64 *pRowid
928){
danielk197726e41442006-06-14 15:16:35 +0000929 echo_vtab *pVtab = (echo_vtab *)tab;
930 sqlite3 *db = pVtab->db;
931 int rc = SQLITE_OK;
932
danielk1977176f4d22006-06-15 10:41:15 +0000933 sqlite3_stmt *pStmt;
934 char *z = 0; /* SQL statement to execute */
935 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
936 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
937 int i; /* Counter variable used by for loops */
938
danielk197726e41442006-06-14 15:16:35 +0000939 assert( nData==pVtab->nCol+2 || nData==1 );
940
drhcd3dd9d2008-04-28 20:27:53 +0000941 /* Ticket #3083 - make sure we always start a transaction prior to
942 ** making any changes to a virtual table */
943 assert( pVtab->inTransaction );
944
danielk19773e3a84d2008-08-01 17:37:40 +0000945 if( simulateVtabError(pVtab, "xUpdate") ){
946 return SQLITE_ERROR;
947 }
948
drh5aec0422006-06-14 23:43:31 +0000949 /* If apData[0] is an integer and nData>1 then do an UPDATE */
950 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000951 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000952 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000953 if( !z ){
954 rc = SQLITE_NOMEM;
955 }
danielk1977176f4d22006-06-15 10:41:15 +0000956
957 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
958 bindArgZero = 1;
959
960 if( bindArgOne ){
danielk1977a1644fd2007-08-29 12:31:25 +0000961 string_concat(&z, " SET rowid=?1 ", 0, &rc);
drh5aec0422006-06-14 23:43:31 +0000962 zSep = ",";
963 }
964 for(i=2; i<nData; i++){
965 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000966 string_concat(&z, sqlite3_mprintf(
danielk1977a1644fd2007-08-29 12:31:25 +0000967 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000968 zSep = ",";
969 }
danielk1977a1644fd2007-08-29 12:31:25 +0000970 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000971 }
972
danielk1977176f4d22006-06-15 10:41:15 +0000973 /* If apData[0] is an integer and nData==1 then do a DELETE */
974 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
975 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000976 if( !z ){
977 rc = SQLITE_NOMEM;
978 }
danielk1977176f4d22006-06-15 10:41:15 +0000979 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000980 }
981
danielk1977176f4d22006-06-15 10:41:15 +0000982 /* If the first argument is NULL and there are more than two args, INSERT */
983 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000984 int ii;
985 char *zInsert = 0;
986 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000987
danielk19774b2688a2006-06-20 11:01:07 +0000988 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000989 if( !zInsert ){
990 rc = SQLITE_NOMEM;
991 }
danielk1977176f4d22006-06-15 10:41:15 +0000992 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
993 bindArgOne = 1;
994 zValues = sqlite3_mprintf("?");
danielk1977a1644fd2007-08-29 12:31:25 +0000995 string_concat(&zInsert, "rowid", 0, &rc);
danielk197726e41442006-06-14 15:16:35 +0000996 }
997
danielk1977176f4d22006-06-15 10:41:15 +0000998 assert((pVtab->nCol+2)==nData);
999 for(ii=2; ii<nData; ii++){
1000 string_concat(&zInsert,
danielk1977a1644fd2007-08-29 12:31:25 +00001001 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
danielk1977176f4d22006-06-15 10:41:15 +00001002 string_concat(&zValues,
danielk1977a1644fd2007-08-29 12:31:25 +00001003 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
danielk197726e41442006-06-14 15:16:35 +00001004 }
1005
danielk1977a1644fd2007-08-29 12:31:25 +00001006 string_concat(&z, zInsert, 1, &rc);
1007 string_concat(&z, ") VALUES(", 0, &rc);
1008 string_concat(&z, zValues, 1, &rc);
1009 string_concat(&z, ")", 0, &rc);
danielk1977176f4d22006-06-15 10:41:15 +00001010 }
1011
1012 /* Anything else is an error */
1013 else{
1014 assert(0);
1015 return SQLITE_ERROR;
1016 }
1017
danielk1977a1644fd2007-08-29 12:31:25 +00001018 if( rc==SQLITE_OK ){
1019 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
1020 }
danielk1977176f4d22006-06-15 10:41:15 +00001021 assert( rc!=SQLITE_OK || pStmt );
1022 sqlite3_free(z);
1023 if( rc==SQLITE_OK ) {
1024 if( bindArgZero ){
1025 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +00001026 }
danielk1977176f4d22006-06-15 10:41:15 +00001027 if( bindArgOne ){
1028 sqlite3_bind_value(pStmt, 1, apData[1]);
1029 }
danielk1977a7a8e142008-02-13 18:25:27 +00001030 for(i=2; i<nData && rc==SQLITE_OK; i++){
1031 if( apData[i] ) rc = sqlite3_bind_value(pStmt, i, apData[i]);
danielk1977176f4d22006-06-15 10:41:15 +00001032 }
danielk1977a7a8e142008-02-13 18:25:27 +00001033 if( rc==SQLITE_OK ){
1034 sqlite3_step(pStmt);
1035 rc = sqlite3_finalize(pStmt);
1036 }else{
1037 sqlite3_finalize(pStmt);
1038 }
danielk197726e41442006-06-14 15:16:35 +00001039 }
1040
danielk19771f6eec52006-06-16 06:17:47 +00001041 if( pRowid && rc==SQLITE_OK ){
1042 *pRowid = sqlite3_last_insert_rowid(db);
1043 }
drh4dc754d2008-07-23 18:17:32 +00001044 if( rc!=SQLITE_OK ){
1045 tab->zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", sqlite3_errmsg(db));
1046 }
danielk19771f6eec52006-06-16 06:17:47 +00001047
danielk197726e41442006-06-14 15:16:35 +00001048 return rc;
1049}
1050
drha967e882006-06-13 01:04:52 +00001051/*
danielk1977c69cdfd2006-06-17 09:39:55 +00001052** xBegin, xSync, xCommit and xRollback callbacks for echo module
1053** virtual tables. Do nothing other than add the name of the callback
1054** to the $::echo_module Tcl variable.
1055*/
1056static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
1057 char *z;
1058 echo_vtab *pVtab = (echo_vtab *)tab;
1059 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
drh344c38e2008-05-05 13:23:04 +00001060 if( z==0 ) return SQLITE_NOMEM;
danielk1977c69cdfd2006-06-17 09:39:55 +00001061 appendToEchoModule(pVtab->interp, zCall);
1062 appendToEchoModule(pVtab->interp, z);
1063 sqlite3_free(z);
drh344c38e2008-05-05 13:23:04 +00001064 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +00001065}
1066static int echoBegin(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001067 int rc;
danielk19775017dc32006-06-24 09:34:22 +00001068 echo_vtab *pVtab = (echo_vtab *)tab;
1069 Tcl_Interp *interp = pVtab->interp;
1070 const char *zVal;
1071
drhcd3dd9d2008-04-28 20:27:53 +00001072 /* Ticket #3083 - do not start a transaction if we are already in
1073 ** a transaction */
1074 assert( !pVtab->inTransaction );
1075
danielk19773e3a84d2008-08-01 17:37:40 +00001076 if( simulateVtabError(pVtab, "xBegin") ){
1077 return SQLITE_ERROR;
1078 }
1079
danielk1977a1644fd2007-08-29 12:31:25 +00001080 rc = echoTransactionCall(tab, "xBegin");
danielk19775017dc32006-06-24 09:34:22 +00001081
danielk1977a1644fd2007-08-29 12:31:25 +00001082 if( rc==SQLITE_OK ){
1083 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
1084 ** and it is set to the name of the real table underlying this virtual
1085 ** echo module table, then cause this xSync operation to fail.
1086 */
1087 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
1088 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1089 rc = SQLITE_ERROR;
1090 }
danielk19775017dc32006-06-24 09:34:22 +00001091 }
drhcd3dd9d2008-04-28 20:27:53 +00001092 if( rc==SQLITE_OK ){
1093 pVtab->inTransaction = 1;
1094 }
danielk1977a1644fd2007-08-29 12:31:25 +00001095 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001096}
1097static int echoSync(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001098 int rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001099 echo_vtab *pVtab = (echo_vtab *)tab;
1100 Tcl_Interp *interp = pVtab->interp;
1101 const char *zVal;
1102
drhcd3dd9d2008-04-28 20:27:53 +00001103 /* Ticket #3083 - Only call xSync if we have previously started a
1104 ** transaction */
1105 assert( pVtab->inTransaction );
1106
danielk19773e3a84d2008-08-01 17:37:40 +00001107 if( simulateVtabError(pVtab, "xSync") ){
1108 return SQLITE_ERROR;
1109 }
1110
danielk1977a1644fd2007-08-29 12:31:25 +00001111 rc = echoTransactionCall(tab, "xSync");
danielk1977c69cdfd2006-06-17 09:39:55 +00001112
danielk1977a1644fd2007-08-29 12:31:25 +00001113 if( rc==SQLITE_OK ){
1114 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
1115 ** and it is set to the name of the real table underlying this virtual
1116 ** echo module table, then cause this xSync operation to fail.
1117 */
1118 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
1119 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1120 rc = -1;
1121 }
danielk1977c69cdfd2006-06-17 09:39:55 +00001122 }
danielk1977a1644fd2007-08-29 12:31:25 +00001123 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001124}
1125static int echoCommit(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:53 +00001126 echo_vtab *pVtab = (echo_vtab*)tab;
drh643167f2008-01-22 21:30:53 +00001127 int rc;
drhcd3dd9d2008-04-28 20:27:53 +00001128
1129 /* Ticket #3083 - Only call xCommit if we have previously started
1130 ** a transaction */
1131 assert( pVtab->inTransaction );
1132
danielk19773e3a84d2008-08-01 17:37:40 +00001133 if( simulateVtabError(pVtab, "xCommit") ){
1134 return SQLITE_ERROR;
1135 }
1136
danielk19772d1d86f2008-06-20 14:59:51 +00001137 sqlite3BeginBenignMalloc();
drh643167f2008-01-22 21:30:53 +00001138 rc = echoTransactionCall(tab, "xCommit");
danielk19772d1d86f2008-06-20 14:59:51 +00001139 sqlite3EndBenignMalloc();
drh344c38e2008-05-05 13:23:04 +00001140 pVtab->inTransaction = 0;
drh643167f2008-01-22 21:30:53 +00001141 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001142}
1143static int echoRollback(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:53 +00001144 int rc;
1145 echo_vtab *pVtab = (echo_vtab*)tab;
1146
1147 /* Ticket #3083 - Only call xRollback if we have previously started
1148 ** a transaction */
1149 assert( pVtab->inTransaction );
1150
1151 rc = echoTransactionCall(tab, "xRollback");
1152 pVtab->inTransaction = 0;
1153 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001154}
1155
1156/*
drhe94b0c32006-07-08 18:09:15 +00001157** Implementation of "GLOB" function on the echo module. Pass
1158** all arguments to the ::echo_glob_overload procedure of TCL
1159** and return the result of that procedure as a string.
1160*/
1161static void overloadedGlobFunction(
1162 sqlite3_context *pContext,
1163 int nArg,
1164 sqlite3_value **apArg
1165){
1166 Tcl_Interp *interp = sqlite3_user_data(pContext);
1167 Tcl_DString str;
1168 int i;
1169 int rc;
1170 Tcl_DStringInit(&str);
1171 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
1172 for(i=0; i<nArg; i++){
1173 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
1174 }
1175 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
1176 Tcl_DStringFree(&str);
1177 if( rc ){
1178 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
1179 }else{
1180 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1181 -1, SQLITE_TRANSIENT);
1182 }
1183 Tcl_ResetResult(interp);
1184}
1185
1186/*
1187** This is the xFindFunction implementation for the echo module.
1188** SQLite calls this routine when the first argument of a function
1189** is a column of an echo virtual table. This routine can optionally
1190** override the implementation of that function. It will choose to
1191** do so if the function is named "glob", and a TCL command named
1192** ::echo_glob_overload exists.
1193*/
1194static int echoFindFunction(
1195 sqlite3_vtab *vtab,
1196 int nArg,
1197 const char *zFuncName,
1198 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1199 void **ppArg
1200){
1201 echo_vtab *pVtab = (echo_vtab *)vtab;
1202 Tcl_Interp *interp = pVtab->interp;
1203 Tcl_CmdInfo info;
1204 if( strcmp(zFuncName,"glob")!=0 ){
1205 return 0;
1206 }
1207 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1208 return 0;
1209 }
1210 *pxFunc = overloadedGlobFunction;
1211 *ppArg = interp;
1212 return 1;
1213}
1214
danielk1977182c4ba2007-06-27 15:53:34 +00001215static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1216 int rc = SQLITE_OK;
1217 echo_vtab *p = (echo_vtab *)vtab;
1218
danielk1977987a00e2008-08-01 17:51:47 +00001219 if( simulateVtabError(p, "xRename") ){
1220 return SQLITE_ERROR;
1221 }
1222
danielk1977182c4ba2007-06-27 15:53:34 +00001223 if( p->isPattern ){
1224 int nThis = strlen(p->zThis);
drhbc6160b2009-04-08 15:45:31 +00001225 char *zSql = sqlite3_mprintf("ALTER TABLE %s RENAME TO %s%s",
danielk1977182c4ba2007-06-27 15:53:34 +00001226 p->zTableName, zNewName, &p->zTableName[nThis]
1227 );
1228 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +00001229 sqlite3_free(zSql);
danielk1977182c4ba2007-06-27 15:53:34 +00001230 }
1231
1232 return rc;
1233}
1234
drhe94b0c32006-07-08 18:09:15 +00001235/*
danielk1977cc013f82006-06-24 06:36:11 +00001236** A virtual table module that merely "echos" the contents of another
1237** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +00001238*/
1239static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +00001240 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +00001241 echoCreate,
1242 echoConnect,
drha967e882006-06-13 01:04:52 +00001243 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +00001244 echoDisconnect,
1245 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +00001246 echoOpen, /* xOpen - open a cursor */
1247 echoClose, /* xClose - close a cursor */
1248 echoFilter, /* xFilter - configure scan constraints */
1249 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +00001250 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +00001251 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +00001252 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +00001253 echoUpdate, /* xUpdate - write data */
1254 echoBegin, /* xBegin - begin transaction */
1255 echoSync, /* xSync - sync transaction */
1256 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +00001257 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +00001258 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:34 +00001259 echoRename, /* xRename - rename the table */
drhb9bb7c12006-06-11 23:41:55 +00001260};
1261
1262/*
1263** Decode a pointer to an sqlite3 object.
1264*/
drh24b58dd2008-07-07 14:50:14 +00001265extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
drhb9bb7c12006-06-11 23:41:55 +00001266
danielk1977860b6cd2007-09-03 11:51:50 +00001267static void moduleDestroy(void *p){
1268 sqlite3_free(p);
1269}
1270
drhb9bb7c12006-06-11 23:41:55 +00001271/*
1272** Register the echo virtual table module.
1273*/
1274static int register_echo_module(
1275 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1276 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1277 int objc, /* Number of arguments */
1278 Tcl_Obj *CONST objv[] /* Command arguments */
1279){
1280 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:50 +00001281 EchoModule *pMod;
drhb9bb7c12006-06-11 23:41:55 +00001282 if( objc!=2 ){
1283 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1284 return TCL_ERROR;
1285 }
1286 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977860b6cd2007-09-03 11:51:50 +00001287 pMod = sqlite3_malloc(sizeof(EchoModule));
1288 pMod->interp = interp;
1289 sqlite3_create_module_v2(db, "echo", &echoModule, (void*)pMod, moduleDestroy);
drhb9bb7c12006-06-11 23:41:55 +00001290 return TCL_OK;
1291}
1292
danielk19775017dc32006-06-24 09:34:22 +00001293/*
1294** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1295**
1296** sqlite3_declare_vtab DB SQL
1297*/
1298static int declare_vtab(
1299 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1300 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1301 int objc, /* Number of arguments */
1302 Tcl_Obj *CONST objv[] /* Command arguments */
1303){
1304 sqlite3 *db;
1305 int rc;
1306 if( objc!=3 ){
1307 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1308 return TCL_ERROR;
1309 }
1310 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1311 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1312 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001313 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001314 return TCL_ERROR;
1315 }
1316 return TCL_OK;
1317}
1318
danielk1977cc013f82006-06-24 06:36:11 +00001319#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001320
1321/*
1322** Register commands with the TCL interpreter.
1323*/
1324int Sqlitetest8_Init(Tcl_Interp *interp){
danielk19776e891622008-08-12 14:48:40 +00001325#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001326 static struct {
1327 char *zName;
1328 Tcl_ObjCmdProc *xProc;
1329 void *clientData;
1330 } aObjCmd[] = {
1331 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +00001332 { "sqlite3_declare_vtab", declare_vtab, 0 },
drhb9bb7c12006-06-11 23:41:55 +00001333 };
1334 int i;
1335 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1336 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1337 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1338 }
danielk19776e891622008-08-12 14:48:40 +00001339#endif
drhb9bb7c12006-06-11 23:41:55 +00001340 return TCL_OK;
1341}