blob: 9b8b545fa68bc5196ff64be59b60a1f11f3efe0d [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 }
drh83cc1392012-04-19 18:04:28 +0000195 nBytes += (int)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;
drh6b169bd2013-10-05 20:18:25 +0000268 if( zIdx==0 ) continue;
drhbc6160b2009-04-08 15:45:31 +0000269 zSql = sqlite3_mprintf("PRAGMA index_info(%s)", zIdx);
danielk1977cc013f82006-06-24 06:36:11 +0000270 if( !zSql ){
271 rc = SQLITE_NOMEM;
272 goto get_index_array_out;
273 }
274 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
drh17435752007-08-16 04:30:38 +0000275 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000276 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
277 int cid = sqlite3_column_int(pStmt2, 1);
278 assert( cid>=0 && cid<nCol );
279 aIndex[cid] = 1;
280 }
danielk1977be718892006-06-23 08:05:19 +0000281 if( pStmt2 ){
282 rc = sqlite3_finalize(pStmt2);
283 }
danielk19775fac9f82006-06-13 14:16:58 +0000284 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000285 goto get_index_array_out;
286 }
287 }
288
danielk19775fac9f82006-06-13 14:16:58 +0000289
290get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000291 if( pStmt ){
292 int rc2 = sqlite3_finalize(pStmt);
293 if( rc==SQLITE_OK ){
294 rc = rc2;
295 }
296 }
danielk19775fac9f82006-06-13 14:16:58 +0000297 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000298 sqlite3_free(aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000299 aIndex = 0;
300 }
301 *paIndex = aIndex;
302 return rc;
303}
304
danielk197778efaba2006-06-12 06:09:17 +0000305/*
306** Global Tcl variable $echo_module is a list. This routine appends
307** the string element zArg to that list in interpreter interp.
308*/
drha967e882006-06-13 01:04:52 +0000309static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000310 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000311 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000312}
313
danielk19777e6ebfb2006-06-12 11:24:37 +0000314/*
315** This function is called from within the echo-modules xCreate and
316** xConnect methods. The argc and argv arguments are copies of those
317** passed to the calling method. This function is responsible for
318** calling sqlite3_declare_vtab() to declare the schema of the virtual
319** table being created or connected.
320**
321** If the constructor was passed just one argument, i.e.:
322**
323** CREATE TABLE t1 AS echo(t2);
324**
325** Then t2 is assumed to be the name of a *real* database table. The
326** schema of the virtual table is declared by passing a copy of the
327** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
328** Hence, the virtual table should have exactly the same column names and
329** types as the real table.
330*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000331static int echoDeclareVtab(
332 echo_vtab *pVtab,
danielk1977182c4ba2007-06-27 15:53:34 +0000333 sqlite3 *db
danielk1977b7a7b9a2006-06-13 10:24:42 +0000334){
danielk19777e6ebfb2006-06-12 11:24:37 +0000335 int rc = SQLITE_OK;
336
danielk1977182c4ba2007-06-27 15:53:34 +0000337 if( pVtab->zTableName ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000338 sqlite3_stmt *pStmt = 0;
danielk1977222a7572007-08-25 13:37:48 +0000339 rc = sqlite3_prepare(db,
danielk19777e6ebfb2006-06-12 11:24:37 +0000340 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
341 -1, &pStmt, 0);
danielk1977222a7572007-08-25 13:37:48 +0000342 if( rc==SQLITE_OK ){
343 sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
344 if( sqlite3_step(pStmt)==SQLITE_ROW ){
345 int rc2;
346 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
347 rc = sqlite3_declare_vtab(db, zCreateTable);
348 rc2 = sqlite3_finalize(pStmt);
349 if( rc==SQLITE_OK ){
350 rc = rc2;
351 }
352 } else {
353 rc = sqlite3_finalize(pStmt);
354 if( rc==SQLITE_OK ){
355 rc = SQLITE_ERROR;
356 }
357 }
danielk19777fa5dd12007-04-18 17:04:00 +0000358 if( rc==SQLITE_OK ){
danielk1977222a7572007-08-25 13:37:48 +0000359 rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
danielk19777fa5dd12007-04-18 17:04:00 +0000360 }
danielk1977222a7572007-08-25 13:37:48 +0000361 if( rc==SQLITE_OK ){
362 rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
danielk1977be718892006-06-23 08:05:19 +0000363 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000364 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000365 }
366
367 return rc;
368}
369
danielk1977cc013f82006-06-24 06:36:11 +0000370/*
371** This function frees all runtime structures associated with the virtual
372** table pVtab.
373*/
danielk1977a4e76362006-06-14 06:31:28 +0000374static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000375 echo_vtab *p = (echo_vtab*)pVtab;
drh17435752007-08-16 04:30:38 +0000376 sqlite3_free(p->aIndex);
377 sqlite3_free(p->aCol);
378 sqlite3_free(p->zThis);
379 sqlite3_free(p->zTableName);
380 sqlite3_free(p->zLogName);
381 sqlite3_free(p);
danielk1977a4e76362006-06-14 06:31:28 +0000382 return 0;
383}
384
danielk1977860b6cd2007-09-03 11:51:50 +0000385typedef struct EchoModule EchoModule;
386struct EchoModule {
387 Tcl_Interp *interp;
388};
389
danielk1977cc013f82006-06-24 06:36:11 +0000390/*
391** This function is called to do the work of the xConnect() method -
392** to allocate the required in-memory structures for a newly connected
393** virtual table.
394*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000395static int echoConstructor(
396 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000397 void *pAux,
drhe4102962006-09-11 00:34:22 +0000398 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000399 sqlite3_vtab **ppVtab,
400 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000401){
danielk1977a1644fd2007-08-29 12:31:25 +0000402 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000403 int i;
404 echo_vtab *pVtab;
405
danielk1977cc013f82006-06-24 06:36:11 +0000406 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk197731dad9d2007-08-16 11:36:15 +0000407 pVtab = sqlite3MallocZero( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000408 if( !pVtab ){
409 return SQLITE_NOMEM;
410 }
danielk1977860b6cd2007-09-03 11:51:50 +0000411 pVtab->interp = ((EchoModule *)pAux)->interp;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000412 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000413
danielk1977182c4ba2007-06-27 15:53:34 +0000414 /* Allocate echo_vtab.zThis */
drhbc6160b2009-04-08 15:45:31 +0000415 pVtab->zThis = sqlite3_mprintf("%s", argv[2]);
danielk1977182c4ba2007-06-27 15:53:34 +0000416 if( !pVtab->zThis ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000417 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000418 return SQLITE_NOMEM;
419 }
420
danielk1977182c4ba2007-06-27 15:53:34 +0000421 /* Allocate echo_vtab.zTableName */
422 if( argc>3 ){
drhbc6160b2009-04-08 15:45:31 +0000423 pVtab->zTableName = sqlite3_mprintf("%s", argv[3]);
danielk1977182c4ba2007-06-27 15:53:34 +0000424 dequoteString(pVtab->zTableName);
425 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
drhbc6160b2009-04-08 15:45:31 +0000426 char *z = sqlite3_mprintf("%s%s", argv[2], &(pVtab->zTableName[1]));
drh17435752007-08-16 04:30:38 +0000427 sqlite3_free(pVtab->zTableName);
danielk1977182c4ba2007-06-27 15:53:34 +0000428 pVtab->zTableName = z;
429 pVtab->isPattern = 1;
430 }
431 if( !pVtab->zTableName ){
432 echoDestructor((sqlite3_vtab *)pVtab);
433 return SQLITE_NOMEM;
434 }
435 }
436
danielk1977cc013f82006-06-24 06:36:11 +0000437 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000438 for(i=0; i<argc; i++){
439 appendToEchoModule(pVtab->interp, argv[i]);
440 }
441
danielk1977cc013f82006-06-24 06:36:11 +0000442 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
443 ** structure. If an error occurs, delete the sqlite3_vtab structure and
444 ** return an error code.
445 */
danielk1977a1644fd2007-08-29 12:31:25 +0000446 rc = echoDeclareVtab(pVtab, db);
447 if( rc!=SQLITE_OK ){
danielk1977a4e76362006-06-14 06:31:28 +0000448 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977a1644fd2007-08-29 12:31:25 +0000449 return rc;
danielk1977a4e76362006-06-14 06:31:28 +0000450 }
451
danielk1977cc013f82006-06-24 06:36:11 +0000452 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000453 *ppVtab = &pVtab->base;
454 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000455}
drha967e882006-06-13 01:04:52 +0000456
danielk1977cc013f82006-06-24 06:36:11 +0000457/*
458** Echo virtual table module xCreate method.
459*/
drhb9bb7c12006-06-11 23:41:55 +0000460static int echoCreate(
461 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000462 void *pAux,
drhe4102962006-09-11 00:34:22 +0000463 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000464 sqlite3_vtab **ppVtab,
465 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000466){
danielk1977a298e902006-06-22 09:53:48 +0000467 int rc = SQLITE_OK;
danielk1977860b6cd2007-09-03 11:51:50 +0000468 appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000469 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000470
471 /* If there were two arguments passed to the module at the SQL level
472 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
473 ** the second argument is used as a table name. Attempt to create
474 ** such a table with a single column, "logmsg". This table will
475 ** be used to log calls to the xUpdate method. It will be deleted
476 ** when the virtual table is DROPed.
477 **
478 ** Note: The main point of this is to test that we can drop tables
479 ** from within an xDestroy method call.
480 */
danielk1977a298e902006-06-22 09:53:48 +0000481 if( rc==SQLITE_OK && argc==5 ){
482 char *zSql;
483 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
drhbc6160b2009-04-08 15:45:31 +0000484 pVtab->zLogName = sqlite3_mprintf("%s", argv[4]);
485 zSql = sqlite3_mprintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000486 rc = sqlite3_exec(db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000487 sqlite3_free(zSql);
danielk1977cd2543b2007-09-03 15:03:20 +0000488 if( rc!=SQLITE_OK ){
drhb9755982010-07-24 16:34:37 +0000489 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
danielk1977cd2543b2007-09-03 15:03:20 +0000490 }
491 }
492
493 if( *ppVtab && rc!=SQLITE_OK ){
494 echoDestructor(*ppVtab);
495 *ppVtab = 0;
danielk1977a298e902006-06-22 09:53:48 +0000496 }
danielk1977cc013f82006-06-24 06:36:11 +0000497
drhcd3dd9d2008-04-28 20:27:53 +0000498 if( rc==SQLITE_OK ){
499 (*(echo_vtab**)ppVtab)->inTransaction = 1;
500 }
501
danielk1977a298e902006-06-22 09:53:48 +0000502 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000503}
danielk1977cc013f82006-06-24 06:36:11 +0000504
505/*
506** Echo virtual table module xConnect method.
507*/
drhb9bb7c12006-06-11 23:41:55 +0000508static int echoConnect(
509 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000510 void *pAux,
drhe4102962006-09-11 00:34:22 +0000511 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000512 sqlite3_vtab **ppVtab,
513 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000514){
danielk1977860b6cd2007-09-03 11:51:50 +0000515 appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000516 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000517}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000518
danielk1977cc013f82006-06-24 06:36:11 +0000519/*
520** Echo virtual table module xDisconnect method.
521*/
danielk19775fac9f82006-06-13 14:16:58 +0000522static int echoDisconnect(sqlite3_vtab *pVtab){
523 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
524 return echoDestructor(pVtab);
525}
danielk1977cc013f82006-06-24 06:36:11 +0000526
527/*
528** Echo virtual table module xDestroy method.
529*/
danielk19775fac9f82006-06-13 14:16:58 +0000530static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000531 int rc = SQLITE_OK;
532 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000533 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000534
535 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000536 if( p && p->zLogName ){
537 char *zSql;
drhbc6160b2009-04-08 15:45:31 +0000538 zSql = sqlite3_mprintf("DROP TABLE %Q", p->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000539 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000540 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000541 }
danielk1977cc013f82006-06-24 06:36:11 +0000542
danielk1977a298e902006-06-22 09:53:48 +0000543 if( rc==SQLITE_OK ){
544 rc = echoDestructor(pVtab);
545 }
546 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000547}
548
danielk1977cc013f82006-06-24 06:36:11 +0000549/*
550** Echo virtual table module xOpen method.
551*/
danielk19775fac9f82006-06-13 14:16:58 +0000552static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000553 echo_cursor *pCur;
danielk19773e3a84d2008-08-01 17:37:40 +0000554 if( simulateVtabError((echo_vtab *)pVTab, "xOpen") ){
555 return SQLITE_ERROR;
556 }
danielk197731dad9d2007-08-16 11:36:15 +0000557 pCur = sqlite3MallocZero(sizeof(echo_cursor));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000558 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000559 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000560}
561
danielk1977cc013f82006-06-24 06:36:11 +0000562/*
563** Echo virtual table module xClose method.
564*/
danielk19775fac9f82006-06-13 14:16:58 +0000565static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000566 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000567 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000568 sqlite3_stmt *pStmt = pCur->pStmt;
569 pCur->pStmt = 0;
drh17435752007-08-16 04:30:38 +0000570 sqlite3_free(pCur);
danielk1977be718892006-06-23 08:05:19 +0000571 rc = sqlite3_finalize(pStmt);
572 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000573}
574
danielk1977a298e902006-06-22 09:53:48 +0000575/*
576** Return non-zero if the cursor does not currently point to a valid record
577** (i.e if the scan has finished), or zero otherwise.
578*/
579static int echoEof(sqlite3_vtab_cursor *cur){
580 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
581}
582
danielk1977cc013f82006-06-24 06:36:11 +0000583/*
584** Echo virtual table module xNext method.
585*/
danielk19775fac9f82006-06-13 14:16:58 +0000586static int echoNext(sqlite3_vtab_cursor *cur){
danielk197731dad9d2007-08-16 11:36:15 +0000587 int rc = SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000588 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000589
danielk19773e3a84d2008-08-01 17:37:40 +0000590 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xNext") ){
591 return SQLITE_ERROR;
592 }
593
danielk197731dad9d2007-08-16 11:36:15 +0000594 if( pCur->pStmt ){
595 rc = sqlite3_step(pCur->pStmt);
596 if( rc==SQLITE_ROW ){
597 rc = SQLITE_OK;
598 }else{
599 rc = sqlite3_finalize(pCur->pStmt);
600 pCur->pStmt = 0;
601 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000602 }
603
604 return rc;
605}
606
danielk1977cc013f82006-06-24 06:36:11 +0000607/*
608** Echo virtual table module xColumn method.
609*/
danielk19775fac9f82006-06-13 14:16:58 +0000610static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000611 int iCol = i + 1;
612 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40 +0000613
614 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xColumn") ){
615 return SQLITE_ERROR;
616 }
617
danielk1977fbbe0052006-06-21 07:02:33 +0000618 if( !pStmt ){
619 sqlite3_result_null(ctx);
620 }else{
621 assert( sqlite3_data_count(pStmt)>iCol );
622 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
623 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000624 return SQLITE_OK;
625}
626
danielk1977cc013f82006-06-24 06:36:11 +0000627/*
628** Echo virtual table module xRowid method.
629*/
danielk19775fac9f82006-06-13 14:16:58 +0000630static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000631 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40 +0000632
633 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xRowid") ){
634 return SQLITE_ERROR;
635 }
636
danielk1977b7a7b9a2006-06-13 10:24:42 +0000637 *pRowid = sqlite3_column_int64(pStmt, 0);
638 return SQLITE_OK;
639}
640
danielk197798331532006-06-14 10:55:52 +0000641/*
642** Compute a simple hash of the null terminated string zString.
643**
644** This module uses only sqlite3_index_info.idxStr, not
645** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
646** in echoBestIndex(), idxNum is set to the corresponding hash value.
647** In echoFilter(), code assert()s that the supplied idxNum value is
648** indeed the hash of the supplied idxStr.
649*/
650static int hashString(const char *zString){
651 int val = 0;
652 int ii;
653 for(ii=0; zString[ii]; ii++){
654 val = (val << 3) + (int)zString[ii];
655 }
656 return val;
657}
658
danielk1977cc013f82006-06-24 06:36:11 +0000659/*
660** Echo virtual table module xFilter method.
661*/
danielk19775fac9f82006-06-13 14:16:58 +0000662static int echoFilter(
663 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000664 int idxNum, const char *idxStr,
665 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000666){
667 int rc;
drh4be8b512006-06-13 23:51:34 +0000668 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000669
670 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
671 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
672 sqlite3 *db = pVtab->db;
673
danielk19773e3a84d2008-08-01 17:37:40 +0000674 if( simulateVtabError(pVtab, "xFilter") ){
675 return SQLITE_ERROR;
676 }
677
danielk1977cc013f82006-06-24 06:36:11 +0000678 /* Check that idxNum matches idxStr */
679 assert( idxNum==hashString(idxStr) );
680
681 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000682 appendToEchoModule(pVtab->interp, "xFilter");
683 appendToEchoModule(pVtab->interp, idxStr);
684 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000685 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000686 }
687
danielk19775fac9f82006-06-13 14:16:58 +0000688 sqlite3_finalize(pCur->pStmt);
689 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000690
691 /* Prepare the SQL statement created by echoBestIndex and bind the
692 ** runtime parameters passed to this function to it.
693 */
drh4be8b512006-06-13 23:51:34 +0000694 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000695 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000696 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk19776eacd282009-04-29 11:50:53 +0000697 rc = sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000698 }
danielk1977cc013f82006-06-24 06:36:11 +0000699
700 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000701 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000702 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000703 }
704
danielk1977be718892006-06-23 08:05:19 +0000705 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000706}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000707
danielk1977cc013f82006-06-24 06:36:11 +0000708
709/*
710** A helper function used by echoUpdate() and echoBestIndex() for
711** manipulating strings in concert with the sqlite3_mprintf() function.
712**
713** Parameter pzStr points to a pointer to a string allocated with
714** sqlite3_mprintf. The second parameter, zAppend, points to another
715** string. The two strings are concatenated together and *pzStr
716** set to point at the result. The initial buffer pointed to by *pzStr
717** is deallocated via sqlite3_free().
718**
719** If the third argument, doFree, is true, then sqlite3_free() is
720** also called to free the buffer pointed to by zAppend.
721*/
danielk1977a1644fd2007-08-29 12:31:25 +0000722static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
danielk1977cc013f82006-06-24 06:36:11 +0000723 char *zIn = *pzStr;
danielk1977a1644fd2007-08-29 12:31:25 +0000724 if( !zAppend && doFree && *pRc==SQLITE_OK ){
725 *pRc = SQLITE_NOMEM;
726 }
727 if( *pRc!=SQLITE_OK ){
728 sqlite3_free(zIn);
729 zIn = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000730 }else{
danielk1977a1644fd2007-08-29 12:31:25 +0000731 if( zIn ){
732 char *zTemp = zIn;
733 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
734 sqlite3_free(zTemp);
735 }else{
736 zIn = sqlite3_mprintf("%s", zAppend);
737 }
738 if( !zIn ){
739 *pRc = SQLITE_NOMEM;
740 }
danielk1977cc013f82006-06-24 06:36:11 +0000741 }
742 *pzStr = zIn;
743 if( doFree ){
744 sqlite3_free(zAppend);
745 }
746}
747
drhb9bb7c12006-06-11 23:41:55 +0000748/*
danielk19775fac9f82006-06-13 14:16:58 +0000749** The echo module implements the subset of query constraints and sort
750** orders that may take advantage of SQLite indices on the underlying
751** real table. For example, if the real table is declared as:
752**
753** CREATE TABLE real(a, b, c);
754** CREATE INDEX real_index ON real(b);
755**
756** then the echo module handles WHERE or ORDER BY clauses that refer
757** to the column "b", but not "a" or "c". If a multi-column index is
drh85b623f2007-12-13 21:54:09 +0000758** present, only its left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000759**
760** This xBestIndex method encodes the proposed search strategy as
761** an SQL query on the real table underlying the virtual echo module
762** table and stores the query in sqlite3_index_info.idxStr. The SQL
763** statement is of the form:
764**
765** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
766**
767** where the <where-clause> and <order-by-clause> are determined
768** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000769*/
danielk19775fac9f82006-06-13 14:16:58 +0000770static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
771 int ii;
drh4be8b512006-06-13 23:51:34 +0000772 char *zQuery = 0;
773 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000774 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000775 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000776 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000777 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000778 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000779
780 int nRow;
781 int useIdx = 0;
782 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000783 int useCost = 0;
784 double cost;
danielk197739359dc2008-03-17 09:36:44 +0000785 int isIgnoreUsable = 0;
786 if( Tcl_GetVar(interp, "echo_module_ignore_usable", TCL_GLOBAL_ONLY) ){
787 isIgnoreUsable = 1;
788 }
789
danielk19773e3a84d2008-08-01 17:37:40 +0000790 if( simulateVtabError(pVtab, "xBestIndex") ){
791 return SQLITE_ERROR;
792 }
793
danielk197774cdba42006-06-19 12:02:58 +0000794 /* Determine the number of rows in the table and store this value in local
795 ** variable nRow. The 'estimated-cost' of the scan will be the number of
796 ** rows in the table for a linear scan, or the log (base 2) of the
797 ** number of rows if the proposed scan uses an index.
798 */
danielk1977780b1d92007-03-30 14:56:34 +0000799 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
800 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
801 useCost = 1;
802 } else {
803 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000804 if( !zQuery ){
805 return SQLITE_NOMEM;
806 }
danielk1977780b1d92007-03-30 14:56:34 +0000807 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
808 sqlite3_free(zQuery);
809 if( rc!=SQLITE_OK ){
810 return rc;
811 }
812 sqlite3_step(pStmt);
813 nRow = sqlite3_column_int(pStmt, 0);
814 rc = sqlite3_finalize(pStmt);
815 if( rc!=SQLITE_OK ){
816 return rc;
817 }
danielk197774cdba42006-06-19 12:02:58 +0000818 }
danielk19775fac9f82006-06-13 14:16:58 +0000819
drh4be8b512006-06-13 23:51:34 +0000820 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000821 if( !zQuery ){
822 return SQLITE_NOMEM;
823 }
danielk19775fac9f82006-06-13 14:16:58 +0000824 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
825 const struct sqlite3_index_constraint *pConstraint;
826 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000827 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000828
829 pConstraint = &pIdxInfo->aConstraint[ii];
830 pUsage = &pIdxInfo->aConstraintUsage[ii];
831
danielk197739359dc2008-03-17 09:36:44 +0000832 if( !isIgnoreUsable && !pConstraint->usable ) continue;
833
drh383736b2006-10-08 18:56:57 +0000834 iCol = pConstraint->iColumn;
drh82ebc2a2012-03-20 03:10:51 +0000835 if( iCol<0 || pVtab->aIndex[iCol] ){
836 char *zCol = iCol>=0 ? pVtab->aCol[iCol] : "rowid";
danielk19775fac9f82006-06-13 14:16:58 +0000837 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000838 useIdx = 1;
danielk19775fac9f82006-06-13 14:16:58 +0000839 switch( pConstraint->op ){
840 case SQLITE_INDEX_CONSTRAINT_EQ:
841 zOp = "="; break;
842 case SQLITE_INDEX_CONSTRAINT_LT:
843 zOp = "<"; break;
844 case SQLITE_INDEX_CONSTRAINT_GT:
845 zOp = ">"; break;
846 case SQLITE_INDEX_CONSTRAINT_LE:
847 zOp = "<="; break;
848 case SQLITE_INDEX_CONSTRAINT_GE:
849 zOp = ">="; break;
850 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000851 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000852 }
drh1a90e092006-06-14 22:07:10 +0000853 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000854 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
855 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000856 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000857 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000858 }
danielk1977a1644fd2007-08-29 12:31:25 +0000859 string_concat(&zQuery, zNew, 1, &rc);
danielk1977cc013f82006-06-24 06:36:11 +0000860
drh4be8b512006-06-13 23:51:34 +0000861 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000862 pUsage->argvIndex = ++nArg;
863 pUsage->omit = 1;
864 }
865 }
danielk197747d08092006-06-14 07:41:31 +0000866
867 /* If there is only one term in the ORDER BY clause, and it is
868 ** on a column that this virtual table has an index for, then consume
869 ** the ORDER BY clause.
870 */
drh82ebc2a2012-03-20 03:10:51 +0000871 if( pIdxInfo->nOrderBy==1 && (
872 pIdxInfo->aOrderBy->iColumn<0 ||
873 pVtab->aIndex[pIdxInfo->aOrderBy->iColumn]) ){
danielk197765fd59f2006-06-24 11:51:33 +0000874 int iCol = pIdxInfo->aOrderBy->iColumn;
drh82ebc2a2012-03-20 03:10:51 +0000875 char *zCol = iCol>=0 ? pVtab->aCol[iCol] : "rowid";
danielk197747d08092006-06-14 07:41:31 +0000876 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk1977cc013f82006-06-24 06:36:11 +0000877 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
danielk1977a1644fd2007-08-29 12:31:25 +0000878 string_concat(&zQuery, zNew, 1, &rc);
danielk197747d08092006-06-14 07:41:31 +0000879 pIdxInfo->orderByConsumed = 1;
880 }
881
danielk19775fac9f82006-06-13 14:16:58 +0000882 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000883 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000884
danielk1977222a7572007-08-25 13:37:48 +0000885 if( !zQuery ){
danielk1977a1644fd2007-08-29 12:31:25 +0000886 return rc;
danielk1977222a7572007-08-25 13:37:48 +0000887 }
danielk197798331532006-06-14 10:55:52 +0000888 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000889 pIdxInfo->idxStr = zQuery;
890 pIdxInfo->needToFreeIdxStr = 1;
danielk19771d461462009-04-21 09:02:45 +0000891 if( useCost ){
danielk1977780b1d92007-03-30 14:56:34 +0000892 pIdxInfo->estimatedCost = cost;
danielk19771d461462009-04-21 09:02:45 +0000893 }else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000894 /* Approximation of log2(nRow). */
895 for( ii=0; ii<(sizeof(int)*8); ii++ ){
896 if( nRow & (1<<ii) ){
897 pIdxInfo->estimatedCost = (double)ii;
898 }
899 }
danielk19771d461462009-04-21 09:02:45 +0000900 }else{
danielk197774cdba42006-06-19 12:02:58 +0000901 pIdxInfo->estimatedCost = (double)nRow;
902 }
903 return rc;
drha967e882006-06-13 01:04:52 +0000904}
905
danielk1977176f4d22006-06-15 10:41:15 +0000906/*
danielk1977cc013f82006-06-24 06:36:11 +0000907** The xUpdate method for echo module virtual tables.
908**
danielk1977176f4d22006-06-15 10:41:15 +0000909** apData[0] apData[1] apData[2..]
910**
911** INTEGER DELETE
912**
913** INTEGER NULL (nCol args) UPDATE (do not set rowid)
914** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
915**
916** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
917** NULL INTEGER (nCol args) INSERT (incl. rowid value)
918**
919*/
danielk19771f6eec52006-06-16 06:17:47 +0000920int echoUpdate(
921 sqlite3_vtab *tab,
922 int nData,
923 sqlite3_value **apData,
924 sqlite_int64 *pRowid
925){
danielk197726e41442006-06-14 15:16:35 +0000926 echo_vtab *pVtab = (echo_vtab *)tab;
927 sqlite3 *db = pVtab->db;
928 int rc = SQLITE_OK;
929
danielk1977176f4d22006-06-15 10:41:15 +0000930 sqlite3_stmt *pStmt;
931 char *z = 0; /* SQL statement to execute */
932 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
933 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
934 int i; /* Counter variable used by for loops */
935
danielk197726e41442006-06-14 15:16:35 +0000936 assert( nData==pVtab->nCol+2 || nData==1 );
937
drhcd3dd9d2008-04-28 20:27:53 +0000938 /* Ticket #3083 - make sure we always start a transaction prior to
939 ** making any changes to a virtual table */
940 assert( pVtab->inTransaction );
941
danielk19773e3a84d2008-08-01 17:37:40 +0000942 if( simulateVtabError(pVtab, "xUpdate") ){
943 return SQLITE_ERROR;
944 }
945
drh5aec0422006-06-14 23:43:31 +0000946 /* If apData[0] is an integer and nData>1 then do an UPDATE */
947 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000948 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000949 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000950 if( !z ){
951 rc = SQLITE_NOMEM;
952 }
danielk1977176f4d22006-06-15 10:41:15 +0000953
954 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
955 bindArgZero = 1;
956
957 if( bindArgOne ){
danielk1977a1644fd2007-08-29 12:31:25 +0000958 string_concat(&z, " SET rowid=?1 ", 0, &rc);
drh5aec0422006-06-14 23:43:31 +0000959 zSep = ",";
960 }
961 for(i=2; i<nData; i++){
962 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000963 string_concat(&z, sqlite3_mprintf(
danielk1977a1644fd2007-08-29 12:31:25 +0000964 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000965 zSep = ",";
966 }
danielk1977a1644fd2007-08-29 12:31:25 +0000967 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000968 }
969
danielk1977176f4d22006-06-15 10:41:15 +0000970 /* If apData[0] is an integer and nData==1 then do a DELETE */
971 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
972 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000973 if( !z ){
974 rc = SQLITE_NOMEM;
975 }
danielk1977176f4d22006-06-15 10:41:15 +0000976 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000977 }
978
danielk1977176f4d22006-06-15 10:41:15 +0000979 /* If the first argument is NULL and there are more than two args, INSERT */
980 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000981 int ii;
982 char *zInsert = 0;
983 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000984
danielk19774b2688a2006-06-20 11:01:07 +0000985 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000986 if( !zInsert ){
987 rc = SQLITE_NOMEM;
988 }
danielk1977176f4d22006-06-15 10:41:15 +0000989 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
990 bindArgOne = 1;
991 zValues = sqlite3_mprintf("?");
danielk1977a1644fd2007-08-29 12:31:25 +0000992 string_concat(&zInsert, "rowid", 0, &rc);
danielk197726e41442006-06-14 15:16:35 +0000993 }
994
danielk1977176f4d22006-06-15 10:41:15 +0000995 assert((pVtab->nCol+2)==nData);
996 for(ii=2; ii<nData; ii++){
997 string_concat(&zInsert,
danielk1977a1644fd2007-08-29 12:31:25 +0000998 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
danielk1977176f4d22006-06-15 10:41:15 +0000999 string_concat(&zValues,
danielk1977a1644fd2007-08-29 12:31:25 +00001000 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
danielk197726e41442006-06-14 15:16:35 +00001001 }
1002
danielk1977a1644fd2007-08-29 12:31:25 +00001003 string_concat(&z, zInsert, 1, &rc);
1004 string_concat(&z, ") VALUES(", 0, &rc);
1005 string_concat(&z, zValues, 1, &rc);
1006 string_concat(&z, ")", 0, &rc);
danielk1977176f4d22006-06-15 10:41:15 +00001007 }
1008
1009 /* Anything else is an error */
1010 else{
1011 assert(0);
1012 return SQLITE_ERROR;
1013 }
1014
danielk1977a1644fd2007-08-29 12:31:25 +00001015 if( rc==SQLITE_OK ){
1016 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
1017 }
danielk1977176f4d22006-06-15 10:41:15 +00001018 assert( rc!=SQLITE_OK || pStmt );
1019 sqlite3_free(z);
1020 if( rc==SQLITE_OK ) {
1021 if( bindArgZero ){
1022 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +00001023 }
danielk1977176f4d22006-06-15 10:41:15 +00001024 if( bindArgOne ){
1025 sqlite3_bind_value(pStmt, 1, apData[1]);
1026 }
danielk1977a7a8e142008-02-13 18:25:27 +00001027 for(i=2; i<nData && rc==SQLITE_OK; i++){
1028 if( apData[i] ) rc = sqlite3_bind_value(pStmt, i, apData[i]);
danielk1977176f4d22006-06-15 10:41:15 +00001029 }
danielk1977a7a8e142008-02-13 18:25:27 +00001030 if( rc==SQLITE_OK ){
1031 sqlite3_step(pStmt);
1032 rc = sqlite3_finalize(pStmt);
1033 }else{
1034 sqlite3_finalize(pStmt);
1035 }
danielk197726e41442006-06-14 15:16:35 +00001036 }
1037
danielk19771f6eec52006-06-16 06:17:47 +00001038 if( pRowid && rc==SQLITE_OK ){
1039 *pRowid = sqlite3_last_insert_rowid(db);
1040 }
drh4dc754d2008-07-23 18:17:32 +00001041 if( rc!=SQLITE_OK ){
1042 tab->zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", sqlite3_errmsg(db));
1043 }
danielk19771f6eec52006-06-16 06:17:47 +00001044
danielk197726e41442006-06-14 15:16:35 +00001045 return rc;
1046}
1047
drha967e882006-06-13 01:04:52 +00001048/*
danielk1977c69cdfd2006-06-17 09:39:55 +00001049** xBegin, xSync, xCommit and xRollback callbacks for echo module
1050** virtual tables. Do nothing other than add the name of the callback
1051** to the $::echo_module Tcl variable.
1052*/
1053static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
1054 char *z;
1055 echo_vtab *pVtab = (echo_vtab *)tab;
1056 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
drh344c38e2008-05-05 13:23:04 +00001057 if( z==0 ) return SQLITE_NOMEM;
danielk1977c69cdfd2006-06-17 09:39:55 +00001058 appendToEchoModule(pVtab->interp, zCall);
1059 appendToEchoModule(pVtab->interp, z);
1060 sqlite3_free(z);
drh344c38e2008-05-05 13:23:04 +00001061 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +00001062}
1063static int echoBegin(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001064 int rc;
danielk19775017dc32006-06-24 09:34:22 +00001065 echo_vtab *pVtab = (echo_vtab *)tab;
1066 Tcl_Interp *interp = pVtab->interp;
1067 const char *zVal;
1068
drhcd3dd9d2008-04-28 20:27:53 +00001069 /* Ticket #3083 - do not start a transaction if we are already in
1070 ** a transaction */
1071 assert( !pVtab->inTransaction );
1072
danielk19773e3a84d2008-08-01 17:37:40 +00001073 if( simulateVtabError(pVtab, "xBegin") ){
1074 return SQLITE_ERROR;
1075 }
1076
danielk1977a1644fd2007-08-29 12:31:25 +00001077 rc = echoTransactionCall(tab, "xBegin");
danielk19775017dc32006-06-24 09:34:22 +00001078
danielk1977a1644fd2007-08-29 12:31:25 +00001079 if( rc==SQLITE_OK ){
1080 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
1081 ** and it is set to the name of the real table underlying this virtual
1082 ** echo module table, then cause this xSync operation to fail.
1083 */
1084 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
1085 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1086 rc = SQLITE_ERROR;
1087 }
danielk19775017dc32006-06-24 09:34:22 +00001088 }
drhcd3dd9d2008-04-28 20:27:53 +00001089 if( rc==SQLITE_OK ){
1090 pVtab->inTransaction = 1;
1091 }
danielk1977a1644fd2007-08-29 12:31:25 +00001092 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001093}
1094static int echoSync(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001095 int rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001096 echo_vtab *pVtab = (echo_vtab *)tab;
1097 Tcl_Interp *interp = pVtab->interp;
1098 const char *zVal;
1099
drhcd3dd9d2008-04-28 20:27:53 +00001100 /* Ticket #3083 - Only call xSync if we have previously started a
1101 ** transaction */
1102 assert( pVtab->inTransaction );
1103
danielk19773e3a84d2008-08-01 17:37:40 +00001104 if( simulateVtabError(pVtab, "xSync") ){
1105 return SQLITE_ERROR;
1106 }
1107
danielk1977a1644fd2007-08-29 12:31:25 +00001108 rc = echoTransactionCall(tab, "xSync");
danielk1977c69cdfd2006-06-17 09:39:55 +00001109
danielk1977a1644fd2007-08-29 12:31:25 +00001110 if( rc==SQLITE_OK ){
1111 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
1112 ** and it is set to the name of the real table underlying this virtual
1113 ** echo module table, then cause this xSync operation to fail.
1114 */
1115 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
1116 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1117 rc = -1;
1118 }
danielk1977c69cdfd2006-06-17 09:39:55 +00001119 }
danielk1977a1644fd2007-08-29 12:31:25 +00001120 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001121}
1122static int echoCommit(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:53 +00001123 echo_vtab *pVtab = (echo_vtab*)tab;
drh643167f2008-01-22 21:30:53 +00001124 int rc;
drhcd3dd9d2008-04-28 20:27:53 +00001125
1126 /* Ticket #3083 - Only call xCommit if we have previously started
1127 ** a transaction */
1128 assert( pVtab->inTransaction );
1129
danielk19773e3a84d2008-08-01 17:37:40 +00001130 if( simulateVtabError(pVtab, "xCommit") ){
1131 return SQLITE_ERROR;
1132 }
1133
danielk19772d1d86f2008-06-20 14:59:51 +00001134 sqlite3BeginBenignMalloc();
drh643167f2008-01-22 21:30:53 +00001135 rc = echoTransactionCall(tab, "xCommit");
danielk19772d1d86f2008-06-20 14:59:51 +00001136 sqlite3EndBenignMalloc();
drh344c38e2008-05-05 13:23:04 +00001137 pVtab->inTransaction = 0;
drh643167f2008-01-22 21:30:53 +00001138 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001139}
1140static int echoRollback(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:53 +00001141 int rc;
1142 echo_vtab *pVtab = (echo_vtab*)tab;
1143
1144 /* Ticket #3083 - Only call xRollback if we have previously started
1145 ** a transaction */
1146 assert( pVtab->inTransaction );
1147
1148 rc = echoTransactionCall(tab, "xRollback");
1149 pVtab->inTransaction = 0;
1150 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001151}
1152
1153/*
drhe94b0c32006-07-08 18:09:15 +00001154** Implementation of "GLOB" function on the echo module. Pass
1155** all arguments to the ::echo_glob_overload procedure of TCL
1156** and return the result of that procedure as a string.
1157*/
1158static void overloadedGlobFunction(
1159 sqlite3_context *pContext,
1160 int nArg,
1161 sqlite3_value **apArg
1162){
1163 Tcl_Interp *interp = sqlite3_user_data(pContext);
1164 Tcl_DString str;
1165 int i;
1166 int rc;
1167 Tcl_DStringInit(&str);
1168 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
1169 for(i=0; i<nArg; i++){
1170 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
1171 }
1172 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
1173 Tcl_DStringFree(&str);
1174 if( rc ){
1175 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
1176 }else{
1177 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1178 -1, SQLITE_TRANSIENT);
1179 }
1180 Tcl_ResetResult(interp);
1181}
1182
1183/*
1184** This is the xFindFunction implementation for the echo module.
1185** SQLite calls this routine when the first argument of a function
1186** is a column of an echo virtual table. This routine can optionally
1187** override the implementation of that function. It will choose to
1188** do so if the function is named "glob", and a TCL command named
1189** ::echo_glob_overload exists.
1190*/
1191static int echoFindFunction(
1192 sqlite3_vtab *vtab,
1193 int nArg,
1194 const char *zFuncName,
1195 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1196 void **ppArg
1197){
1198 echo_vtab *pVtab = (echo_vtab *)vtab;
1199 Tcl_Interp *interp = pVtab->interp;
1200 Tcl_CmdInfo info;
1201 if( strcmp(zFuncName,"glob")!=0 ){
1202 return 0;
1203 }
1204 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1205 return 0;
1206 }
1207 *pxFunc = overloadedGlobFunction;
1208 *ppArg = interp;
1209 return 1;
1210}
1211
danielk1977182c4ba2007-06-27 15:53:34 +00001212static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1213 int rc = SQLITE_OK;
1214 echo_vtab *p = (echo_vtab *)vtab;
1215
danielk1977987a00e2008-08-01 17:51:47 +00001216 if( simulateVtabError(p, "xRename") ){
1217 return SQLITE_ERROR;
1218 }
1219
danielk1977182c4ba2007-06-27 15:53:34 +00001220 if( p->isPattern ){
drh83cc1392012-04-19 18:04:28 +00001221 int nThis = (int)strlen(p->zThis);
drhbc6160b2009-04-08 15:45:31 +00001222 char *zSql = sqlite3_mprintf("ALTER TABLE %s RENAME TO %s%s",
danielk1977182c4ba2007-06-27 15:53:34 +00001223 p->zTableName, zNewName, &p->zTableName[nThis]
1224 );
1225 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +00001226 sqlite3_free(zSql);
danielk1977182c4ba2007-06-27 15:53:34 +00001227 }
1228
1229 return rc;
1230}
1231
dan3b504df2011-10-29 15:29:43 +00001232static int echoSavepoint(sqlite3_vtab *pVTab, int iSavepoint){
1233 assert( pVTab );
1234 return SQLITE_OK;
1235}
1236
1237static int echoRelease(sqlite3_vtab *pVTab, int iSavepoint){
1238 assert( pVTab );
1239 return SQLITE_OK;
1240}
1241
1242static int echoRollbackTo(sqlite3_vtab *pVTab, int iSavepoint){
1243 assert( pVTab );
1244 return SQLITE_OK;
1245}
1246
drhe94b0c32006-07-08 18:09:15 +00001247/*
danielk1977cc013f82006-06-24 06:36:11 +00001248** A virtual table module that merely "echos" the contents of another
1249** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +00001250*/
1251static sqlite3_module echoModule = {
dan3b504df2011-10-29 15:29:43 +00001252 1, /* iVersion */
1253 echoCreate,
1254 echoConnect,
1255 echoBestIndex,
1256 echoDisconnect,
1257 echoDestroy,
1258 echoOpen, /* xOpen - open a cursor */
1259 echoClose, /* xClose - close a cursor */
1260 echoFilter, /* xFilter - configure scan constraints */
1261 echoNext, /* xNext - advance a cursor */
1262 echoEof, /* xEof */
1263 echoColumn, /* xColumn - read data */
1264 echoRowid, /* xRowid - read data */
1265 echoUpdate, /* xUpdate - write data */
1266 echoBegin, /* xBegin - begin transaction */
1267 echoSync, /* xSync - sync transaction */
1268 echoCommit, /* xCommit - commit transaction */
1269 echoRollback, /* xRollback - rollback transaction */
1270 echoFindFunction, /* xFindFunction - function overloading */
1271 echoRename /* xRename - rename the table */
1272};
1273
1274static sqlite3_module echoModuleV2 = {
1275 2, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +00001276 echoCreate,
1277 echoConnect,
drha967e882006-06-13 01:04:52 +00001278 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +00001279 echoDisconnect,
1280 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +00001281 echoOpen, /* xOpen - open a cursor */
1282 echoClose, /* xClose - close a cursor */
1283 echoFilter, /* xFilter - configure scan constraints */
1284 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +00001285 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +00001286 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +00001287 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +00001288 echoUpdate, /* xUpdate - write data */
1289 echoBegin, /* xBegin - begin transaction */
1290 echoSync, /* xSync - sync transaction */
1291 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +00001292 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +00001293 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:34 +00001294 echoRename, /* xRename - rename the table */
dan3b504df2011-10-29 15:29:43 +00001295 echoSavepoint,
1296 echoRelease,
1297 echoRollbackTo
drhb9bb7c12006-06-11 23:41:55 +00001298};
1299
1300/*
1301** Decode a pointer to an sqlite3 object.
1302*/
drh24b58dd2008-07-07 14:50:14 +00001303extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
mistachkine84d8d32013-04-29 03:09:10 +00001304extern const char *sqlite3ErrName(int);
drhb9bb7c12006-06-11 23:41:55 +00001305
danielk1977860b6cd2007-09-03 11:51:50 +00001306static void moduleDestroy(void *p){
1307 sqlite3_free(p);
1308}
1309
drhb9bb7c12006-06-11 23:41:55 +00001310/*
1311** Register the echo virtual table module.
1312*/
1313static int register_echo_module(
1314 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1315 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1316 int objc, /* Number of arguments */
1317 Tcl_Obj *CONST objv[] /* Command arguments */
1318){
danca8b9ba2012-05-16 14:29:11 +00001319 int rc;
drhb9bb7c12006-06-11 23:41:55 +00001320 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:50 +00001321 EchoModule *pMod;
drhb9bb7c12006-06-11 23:41:55 +00001322 if( objc!=2 ){
1323 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1324 return TCL_ERROR;
1325 }
1326 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
dan3b504df2011-10-29 15:29:43 +00001327
1328 /* Virtual table module "echo" */
danielk1977860b6cd2007-09-03 11:51:50 +00001329 pMod = sqlite3_malloc(sizeof(EchoModule));
1330 pMod->interp = interp;
danca8b9ba2012-05-16 14:29:11 +00001331 rc = sqlite3_create_module_v2(
1332 db, "echo", &echoModule, (void*)pMod, moduleDestroy
1333 );
dan3b504df2011-10-29 15:29:43 +00001334
1335 /* Virtual table module "echo_v2" */
danca8b9ba2012-05-16 14:29:11 +00001336 if( rc==SQLITE_OK ){
1337 pMod = sqlite3_malloc(sizeof(EchoModule));
1338 pMod->interp = interp;
1339 rc = sqlite3_create_module_v2(db, "echo_v2",
1340 &echoModuleV2, (void*)pMod, moduleDestroy
1341 );
1342 }
1343
mistachkine84d8d32013-04-29 03:09:10 +00001344 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
drhb9bb7c12006-06-11 23:41:55 +00001345 return TCL_OK;
1346}
1347
danielk19775017dc32006-06-24 09:34:22 +00001348/*
1349** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1350**
1351** sqlite3_declare_vtab DB SQL
1352*/
1353static int declare_vtab(
1354 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1355 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1356 int objc, /* Number of arguments */
1357 Tcl_Obj *CONST objv[] /* Command arguments */
1358){
1359 sqlite3 *db;
1360 int rc;
1361 if( objc!=3 ){
1362 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1363 return TCL_ERROR;
1364 }
1365 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1366 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1367 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001368 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001369 return TCL_ERROR;
1370 }
1371 return TCL_OK;
1372}
1373
danielk1977cc013f82006-06-24 06:36:11 +00001374#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001375
1376/*
1377** Register commands with the TCL interpreter.
1378*/
1379int Sqlitetest8_Init(Tcl_Interp *interp){
danielk19776e891622008-08-12 14:48:40 +00001380#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001381 static struct {
1382 char *zName;
1383 Tcl_ObjCmdProc *xProc;
1384 void *clientData;
1385 } aObjCmd[] = {
dan2deb1652012-07-13 16:15:20 +00001386 { "register_echo_module", register_echo_module, 0 },
dan2deb1652012-07-13 16:15:20 +00001387 { "sqlite3_declare_vtab", declare_vtab, 0 },
drhb9bb7c12006-06-11 23:41:55 +00001388 };
1389 int i;
1390 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1391 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1392 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1393 }
danielk19776e891622008-08-12 14:48:40 +00001394#endif
drhb9bb7c12006-06-11 23:41:55 +00001395 return TCL_OK;
1396}