blob: 7a532346ed1bd8f22ec7c9577ce59dec1e01f654 [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"
mistachkin52b1dbb2016-07-28 14:37:04 +000017#if defined(INCLUDE_SQLITE_TCL_H)
18# include "sqlite_tcl.h"
19#else
20# include "tcl.h"
21#endif
drhb9bb7c12006-06-11 23:41:55 +000022#include <stdlib.h>
23#include <string.h>
24
danielk1977cc013f82006-06-24 06:36:11 +000025#ifndef SQLITE_OMIT_VIRTUALTABLE
26
danielk1977b7a7b9a2006-06-13 10:24:42 +000027typedef struct echo_vtab echo_vtab;
28typedef struct echo_cursor echo_cursor;
29
danielk1977c69cdfd2006-06-17 09:39:55 +000030/*
danielk1977780b1d92007-03-30 14:56:34 +000031** The test module defined in this file uses four global Tcl variables to
danielk1977c69cdfd2006-06-17 09:39:55 +000032** commicate with test-scripts:
33**
34** $::echo_module
35** $::echo_module_sync_fail
danielk19775017dc32006-06-24 09:34:22 +000036** $::echo_module_begin_fail
danielk1977780b1d92007-03-30 14:56:34 +000037** $::echo_module_cost
danielk1977cc013f82006-06-24 06:36:11 +000038**
39** The variable ::echo_module is a list. Each time one of the following
40** methods is called, one or more elements are appended to the list.
41** This is used for automated testing of virtual table modules.
42**
43** The ::echo_module_sync_fail variable is set by test scripts and read
44** by code in this file. If it is set to the name of a real table in the
45** the database, then all xSync operations on echo virtual tables that
46** use the named table as a backing store will fail.
danielk1977c69cdfd2006-06-17 09:39:55 +000047*/
48
danielk19773e3a84d2008-08-01 17:37:40 +000049/*
50** Errors can be provoked within the following echo virtual table methods:
51**
52** xBestIndex xOpen xFilter xNext
53** xColumn xRowid xUpdate xSync
danielk1977987a00e2008-08-01 17:51:47 +000054** xBegin xRename
danielk19773e3a84d2008-08-01 17:37:40 +000055**
56** This is done by setting the global tcl variable:
57**
58** echo_module_fail($method,$tbl)
59**
60** where $method is set to the name of the virtual table method to fail
61** (i.e. "xBestIndex") and $tbl is the name of the table being echoed (not
62** the name of the virtual table, the name of the underlying real table).
63*/
64
danielk19775fac9f82006-06-13 14:16:58 +000065/*
danielk1977d6e8dd02006-06-15 15:38:41 +000066** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:58 +000067**
danielk1977d6e8dd02006-06-15 15:38:41 +000068** echo.vtab.aIndex is an array of booleans. The nth entry is true if
69** the nth column of the real table is the left-most column of an index
70** (implicit or otherwise). In other words, if SQLite can optimize
71** a query like "SELECT * FROM real_table WHERE col = ?".
72**
danielk1977c69cdfd2006-06-17 09:39:55 +000073** Member variable aCol[] contains copies of the column names of the real
74** table.
danielk19775fac9f82006-06-13 14:16:58 +000075*/
danielk1977b7a7b9a2006-06-13 10:24:42 +000076struct echo_vtab {
77 sqlite3_vtab base;
danielk1977cc013f82006-06-24 06:36:11 +000078 Tcl_Interp *interp; /* Tcl interpreter containing debug variables */
79 sqlite3 *db; /* Database connection */
danielk19775fac9f82006-06-13 14:16:58 +000080
danielk1977182c4ba2007-06-27 15:53:34 +000081 int isPattern;
drhcd3dd9d2008-04-28 20:27:53 +000082 int inTransaction; /* True if within a transaction */
danielk1977182c4ba2007-06-27 15:53:34 +000083 char *zThis; /* Name of the echo table */
danielk1977a4e76362006-06-14 06:31:28 +000084 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:48 +000085 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:28 +000086 int nCol; /* Number of columns in the real table */
87 int *aIndex; /* Array of size nCol. True if column has an index */
88 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000089};
90
91/* An echo cursor object */
92struct echo_cursor {
93 sqlite3_vtab_cursor base;
94 sqlite3_stmt *pStmt;
danielk1977b7a7b9a2006-06-13 10:24:42 +000095};
96
danielk19773e3a84d2008-08-01 17:37:40 +000097static int simulateVtabError(echo_vtab *p, const char *zMethod){
98 const char *zErr;
99 char zVarname[128];
100 zVarname[127] = '\0';
shanef5eccb62008-08-31 00:29:08 +0000101 sqlite3_snprintf(127, zVarname, "echo_module_fail(%s,%s)", zMethod, p->zTableName);
danielk19773e3a84d2008-08-01 17:37:40 +0000102 zErr = Tcl_GetVar(p->interp, zVarname, TCL_GLOBAL_ONLY);
103 if( zErr ){
104 p->base.zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", zErr);
105 }
106 return (zErr!=0);
107}
108
danielk1977cc013f82006-06-24 06:36:11 +0000109/*
danielk1977182c4ba2007-06-27 15:53:34 +0000110** Convert an SQL-style quoted string into a normal string by removing
111** the quote characters. The conversion is done in-place. If the
112** input does not begin with a quote character, then this routine
113** is a no-op.
114**
115** Examples:
116**
117** "abc" becomes abc
118** 'xyz' becomes xyz
119** [pqr] becomes pqr
120** `mno` becomes mno
121*/
122static void dequoteString(char *z){
123 int quote;
124 int i, j;
125 if( z==0 ) return;
126 quote = z[0];
127 switch( quote ){
128 case '\'': break;
129 case '"': break;
130 case '`': break; /* For MySQL compatibility */
131 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
132 default: return;
133 }
134 for(i=1, j=0; z[i]; i++){
135 if( z[i]==quote ){
136 if( z[i+1]==quote ){
137 z[j++] = quote;
138 i++;
139 }else{
140 z[j++] = 0;
141 break;
142 }
143 }else{
144 z[j++] = z[i];
145 }
146 }
147}
148
149/*
danielk1977cc013f82006-06-24 06:36:11 +0000150** Retrieve the column names for the table named zTab via database
151** connection db. SQLITE_OK is returned on success, or an sqlite error
152** code otherwise.
153**
154** If successful, the number of columns is written to *pnCol. *paCol is
drh17435752007-08-16 04:30:38 +0000155** set to point at sqlite3_malloc()'d space containing the array of
156** nCol column names. The caller is responsible for calling sqlite3_free
danielk1977cc013f82006-06-24 06:36:11 +0000157** on *paCol.
158*/
danielk19775fac9f82006-06-13 14:16:58 +0000159static int getColumnNames(
160 sqlite3 *db,
161 const char *zTab,
162 char ***paCol,
163 int *pnCol
164){
165 char **aCol = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000166 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000167 sqlite3_stmt *pStmt = 0;
168 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19 +0000169 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000170
danielk1977cc013f82006-06-24 06:36:11 +0000171 /* Prepare the statement "SELECT * FROM <tbl>". The column names
172 ** of the result set of the compiled SELECT will be the same as
173 ** the column names of table <tbl>.
174 */
drh17435752007-08-16 04:30:38 +0000175 zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000176 if( !zSql ){
177 rc = SQLITE_NOMEM;
178 goto out;
179 }
180 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000181 sqlite3_free(zSql);
danielk1977cc013f82006-06-24 06:36:11 +0000182
danielk19775fac9f82006-06-13 14:16:58 +0000183 if( rc==SQLITE_OK ){
184 int ii;
danielk1977cc013f82006-06-24 06:36:11 +0000185 int nBytes;
186 char *zSpace;
danielk19775fac9f82006-06-13 14:16:58 +0000187 nCol = sqlite3_column_count(pStmt);
danielk1977cc013f82006-06-24 06:36:11 +0000188
189 /* Figure out how much space to allocate for the array of column names
190 ** (including space for the strings themselves). Then allocate it.
191 */
192 nBytes = sizeof(char *) * nCol;
193 for(ii=0; ii<nCol; ii++){
danielk1977a7a8e142008-02-13 18:25:27 +0000194 const char *zName = sqlite3_column_name(pStmt, ii);
195 if( !zName ){
196 rc = SQLITE_NOMEM;
197 goto out;
198 }
drh83cc1392012-04-19 18:04:28 +0000199 nBytes += (int)strlen(zName)+1;
danielk1977cc013f82006-06-24 06:36:11 +0000200 }
danielk197731dad9d2007-08-16 11:36:15 +0000201 aCol = (char **)sqlite3MallocZero(nBytes);
danielk19775fac9f82006-06-13 14:16:58 +0000202 if( !aCol ){
203 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11 +0000204 goto out;
danielk19775fac9f82006-06-13 14:16:58 +0000205 }
danielk1977cc013f82006-06-24 06:36:11 +0000206
207 /* Copy the column names into the allocated space and set up the
208 ** pointers in the aCol[] array.
209 */
210 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58 +0000211 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11 +0000212 aCol[ii] = zSpace;
drh65545b52015-01-19 00:35:53 +0000213 sqlite3_snprintf(nBytes, zSpace, "%s", sqlite3_column_name(pStmt,ii));
214 zSpace += (int)strlen(zSpace) + 1;
danielk19775fac9f82006-06-13 14:16:58 +0000215 }
danielk1977cc013f82006-06-24 06:36:11 +0000216 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58 +0000217 }
218
219 *paCol = aCol;
220 *pnCol = nCol;
221
danielk1977cc013f82006-06-24 06:36:11 +0000222out:
danielk19775fac9f82006-06-13 14:16:58 +0000223 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000224 return rc;
225}
226
danielk1977cc013f82006-06-24 06:36:11 +0000227/*
228** Parameter zTab is the name of a table in database db with nCol
229** columns. This function allocates an array of integers nCol in
230** size and populates it according to any implicit or explicit
231** indices on table zTab.
232**
233** If successful, SQLITE_OK is returned and *paIndex set to point
234** at the allocated array. Otherwise, an error code is returned.
235**
236** See comments associated with the member variable aIndex above
237** "struct echo_vtab" for details of the contents of the array.
238*/
239static int getIndexArray(
240 sqlite3 *db, /* Database connection */
241 const char *zTab, /* Name of table in database db */
242 int nCol,
243 int **paIndex
244){
danielk19775fac9f82006-06-13 14:16:58 +0000245 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000246 int *aIndex = 0;
247 int rc;
danielk1977cc013f82006-06-24 06:36:11 +0000248 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000249
danielk1977cc013f82006-06-24 06:36:11 +0000250 /* Allocate space for the index array */
danielk197731dad9d2007-08-16 11:36:15 +0000251 aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000252 if( !aIndex ){
253 rc = SQLITE_NOMEM;
254 goto get_index_array_out;
255 }
256
danielk1977cc013f82006-06-24 06:36:11 +0000257 /* Compile an sqlite pragma to loop through all indices on table zTab */
drhbc6160b2009-04-08 15:45:31 +0000258 zSql = sqlite3_mprintf("PRAGMA index_list(%s)", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000259 if( !zSql ){
260 rc = SQLITE_NOMEM;
261 goto get_index_array_out;
262 }
263 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000264 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000265
danielk1977cc013f82006-06-24 06:36:11 +0000266 /* For each index, figure out the left-most column and set the
267 ** corresponding entry in aIndex[] to 1.
268 */
danielk19775fac9f82006-06-13 14:16:58 +0000269 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000270 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58 +0000271 sqlite3_stmt *pStmt2 = 0;
drh6b169bd2013-10-05 20:18:25 +0000272 if( zIdx==0 ) continue;
drhbc6160b2009-04-08 15:45:31 +0000273 zSql = sqlite3_mprintf("PRAGMA index_info(%s)", zIdx);
danielk1977cc013f82006-06-24 06:36:11 +0000274 if( !zSql ){
275 rc = SQLITE_NOMEM;
276 goto get_index_array_out;
277 }
278 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
drh17435752007-08-16 04:30:38 +0000279 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000280 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
281 int cid = sqlite3_column_int(pStmt2, 1);
282 assert( cid>=0 && cid<nCol );
283 aIndex[cid] = 1;
284 }
danielk1977be718892006-06-23 08:05:19 +0000285 if( pStmt2 ){
286 rc = sqlite3_finalize(pStmt2);
287 }
danielk19775fac9f82006-06-13 14:16:58 +0000288 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000289 goto get_index_array_out;
290 }
291 }
292
danielk19775fac9f82006-06-13 14:16:58 +0000293
294get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000295 if( pStmt ){
296 int rc2 = sqlite3_finalize(pStmt);
297 if( rc==SQLITE_OK ){
298 rc = rc2;
299 }
300 }
danielk19775fac9f82006-06-13 14:16:58 +0000301 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000302 sqlite3_free(aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000303 aIndex = 0;
304 }
305 *paIndex = aIndex;
306 return rc;
307}
308
danielk197778efaba2006-06-12 06:09:17 +0000309/*
310** Global Tcl variable $echo_module is a list. This routine appends
311** the string element zArg to that list in interpreter interp.
312*/
drha967e882006-06-13 01:04:52 +0000313static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000314 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000315 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000316}
317
danielk19777e6ebfb2006-06-12 11:24:37 +0000318/*
319** This function is called from within the echo-modules xCreate and
320** xConnect methods. The argc and argv arguments are copies of those
321** passed to the calling method. This function is responsible for
322** calling sqlite3_declare_vtab() to declare the schema of the virtual
323** table being created or connected.
324**
325** If the constructor was passed just one argument, i.e.:
326**
327** CREATE TABLE t1 AS echo(t2);
328**
329** Then t2 is assumed to be the name of a *real* database table. The
330** schema of the virtual table is declared by passing a copy of the
331** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
332** Hence, the virtual table should have exactly the same column names and
333** types as the real table.
334*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000335static int echoDeclareVtab(
336 echo_vtab *pVtab,
danielk1977182c4ba2007-06-27 15:53:34 +0000337 sqlite3 *db
danielk1977b7a7b9a2006-06-13 10:24:42 +0000338){
danielk19777e6ebfb2006-06-12 11:24:37 +0000339 int rc = SQLITE_OK;
340
danielk1977182c4ba2007-06-27 15:53:34 +0000341 if( pVtab->zTableName ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000342 sqlite3_stmt *pStmt = 0;
danielk1977222a7572007-08-25 13:37:48 +0000343 rc = sqlite3_prepare(db,
drh1e32bed2020-06-19 13:33:53 +0000344 "SELECT sql FROM sqlite_schema WHERE type = 'table' AND name = ?",
danielk19777e6ebfb2006-06-12 11:24:37 +0000345 -1, &pStmt, 0);
danielk1977222a7572007-08-25 13:37:48 +0000346 if( rc==SQLITE_OK ){
347 sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
348 if( sqlite3_step(pStmt)==SQLITE_ROW ){
349 int rc2;
350 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
351 rc = sqlite3_declare_vtab(db, zCreateTable);
352 rc2 = sqlite3_finalize(pStmt);
353 if( rc==SQLITE_OK ){
354 rc = rc2;
355 }
356 } else {
357 rc = sqlite3_finalize(pStmt);
358 if( rc==SQLITE_OK ){
359 rc = SQLITE_ERROR;
360 }
361 }
danielk19777fa5dd12007-04-18 17:04:00 +0000362 if( rc==SQLITE_OK ){
danielk1977222a7572007-08-25 13:37:48 +0000363 rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
danielk19777fa5dd12007-04-18 17:04:00 +0000364 }
danielk1977222a7572007-08-25 13:37:48 +0000365 if( rc==SQLITE_OK ){
366 rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
danielk1977be718892006-06-23 08:05:19 +0000367 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000368 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000369 }
370
371 return rc;
372}
373
danielk1977cc013f82006-06-24 06:36:11 +0000374/*
375** This function frees all runtime structures associated with the virtual
376** table pVtab.
377*/
danielk1977a4e76362006-06-14 06:31:28 +0000378static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000379 echo_vtab *p = (echo_vtab*)pVtab;
drh17435752007-08-16 04:30:38 +0000380 sqlite3_free(p->aIndex);
381 sqlite3_free(p->aCol);
382 sqlite3_free(p->zThis);
383 sqlite3_free(p->zTableName);
384 sqlite3_free(p->zLogName);
385 sqlite3_free(p);
danielk1977a4e76362006-06-14 06:31:28 +0000386 return 0;
387}
388
danielk1977860b6cd2007-09-03 11:51:50 +0000389typedef struct EchoModule EchoModule;
390struct EchoModule {
391 Tcl_Interp *interp;
drhba39ca42021-05-17 13:11:24 +0000392 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:50 +0000393};
394
danielk1977cc013f82006-06-24 06:36:11 +0000395/*
396** This function is called to do the work of the xConnect() method -
397** to allocate the required in-memory structures for a newly connected
398** virtual table.
399*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000400static int echoConstructor(
401 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000402 void *pAux,
drhe4102962006-09-11 00:34:22 +0000403 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000404 sqlite3_vtab **ppVtab,
405 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000406){
danielk1977a1644fd2007-08-29 12:31:25 +0000407 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000408 int i;
409 echo_vtab *pVtab;
410
danielk1977cc013f82006-06-24 06:36:11 +0000411 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk197731dad9d2007-08-16 11:36:15 +0000412 pVtab = sqlite3MallocZero( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000413 if( !pVtab ){
414 return SQLITE_NOMEM;
415 }
danielk1977860b6cd2007-09-03 11:51:50 +0000416 pVtab->interp = ((EchoModule *)pAux)->interp;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000417 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000418
danielk1977182c4ba2007-06-27 15:53:34 +0000419 /* Allocate echo_vtab.zThis */
drhbc6160b2009-04-08 15:45:31 +0000420 pVtab->zThis = sqlite3_mprintf("%s", argv[2]);
danielk1977182c4ba2007-06-27 15:53:34 +0000421 if( !pVtab->zThis ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000422 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000423 return SQLITE_NOMEM;
424 }
425
danielk1977182c4ba2007-06-27 15:53:34 +0000426 /* Allocate echo_vtab.zTableName */
427 if( argc>3 ){
drhbc6160b2009-04-08 15:45:31 +0000428 pVtab->zTableName = sqlite3_mprintf("%s", argv[3]);
danielk1977182c4ba2007-06-27 15:53:34 +0000429 dequoteString(pVtab->zTableName);
430 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
drhbc6160b2009-04-08 15:45:31 +0000431 char *z = sqlite3_mprintf("%s%s", argv[2], &(pVtab->zTableName[1]));
drh17435752007-08-16 04:30:38 +0000432 sqlite3_free(pVtab->zTableName);
danielk1977182c4ba2007-06-27 15:53:34 +0000433 pVtab->zTableName = z;
434 pVtab->isPattern = 1;
435 }
436 if( !pVtab->zTableName ){
437 echoDestructor((sqlite3_vtab *)pVtab);
438 return SQLITE_NOMEM;
439 }
440 }
441
danielk1977cc013f82006-06-24 06:36:11 +0000442 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000443 for(i=0; i<argc; i++){
444 appendToEchoModule(pVtab->interp, argv[i]);
445 }
446
danielk1977cc013f82006-06-24 06:36:11 +0000447 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
448 ** structure. If an error occurs, delete the sqlite3_vtab structure and
449 ** return an error code.
450 */
danielk1977a1644fd2007-08-29 12:31:25 +0000451 rc = echoDeclareVtab(pVtab, db);
452 if( rc!=SQLITE_OK ){
danielk1977a4e76362006-06-14 06:31:28 +0000453 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977a1644fd2007-08-29 12:31:25 +0000454 return rc;
danielk1977a4e76362006-06-14 06:31:28 +0000455 }
456
danielk1977cc013f82006-06-24 06:36:11 +0000457 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000458 *ppVtab = &pVtab->base;
459 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000460}
drha967e882006-06-13 01:04:52 +0000461
danielk1977cc013f82006-06-24 06:36:11 +0000462/*
463** Echo virtual table module xCreate method.
464*/
drhb9bb7c12006-06-11 23:41:55 +0000465static int echoCreate(
466 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000467 void *pAux,
drhe4102962006-09-11 00:34:22 +0000468 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000469 sqlite3_vtab **ppVtab,
470 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000471){
danielk1977a298e902006-06-22 09:53:48 +0000472 int rc = SQLITE_OK;
danielk1977860b6cd2007-09-03 11:51:50 +0000473 appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000474 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000475
476 /* If there were two arguments passed to the module at the SQL level
477 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
478 ** the second argument is used as a table name. Attempt to create
479 ** such a table with a single column, "logmsg". This table will
480 ** be used to log calls to the xUpdate method. It will be deleted
481 ** when the virtual table is DROPed.
482 **
483 ** Note: The main point of this is to test that we can drop tables
484 ** from within an xDestroy method call.
485 */
danielk1977a298e902006-06-22 09:53:48 +0000486 if( rc==SQLITE_OK && argc==5 ){
487 char *zSql;
488 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
drhbc6160b2009-04-08 15:45:31 +0000489 pVtab->zLogName = sqlite3_mprintf("%s", argv[4]);
490 zSql = sqlite3_mprintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000491 rc = sqlite3_exec(db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000492 sqlite3_free(zSql);
danielk1977cd2543b2007-09-03 15:03:20 +0000493 if( rc!=SQLITE_OK ){
drhb9755982010-07-24 16:34:37 +0000494 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
danielk1977cd2543b2007-09-03 15:03:20 +0000495 }
496 }
497
498 if( *ppVtab && rc!=SQLITE_OK ){
499 echoDestructor(*ppVtab);
500 *ppVtab = 0;
danielk1977a298e902006-06-22 09:53:48 +0000501 }
danielk1977cc013f82006-06-24 06:36:11 +0000502
drhcd3dd9d2008-04-28 20:27:53 +0000503 if( rc==SQLITE_OK ){
504 (*(echo_vtab**)ppVtab)->inTransaction = 1;
505 }
506
danielk1977a298e902006-06-22 09:53:48 +0000507 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000508}
danielk1977cc013f82006-06-24 06:36:11 +0000509
510/*
511** Echo virtual table module xConnect method.
512*/
drhb9bb7c12006-06-11 23:41:55 +0000513static int echoConnect(
514 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000515 void *pAux,
drhe4102962006-09-11 00:34:22 +0000516 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000517 sqlite3_vtab **ppVtab,
518 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000519){
danielk1977860b6cd2007-09-03 11:51:50 +0000520 appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000521 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000522}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000523
danielk1977cc013f82006-06-24 06:36:11 +0000524/*
525** Echo virtual table module xDisconnect method.
526*/
danielk19775fac9f82006-06-13 14:16:58 +0000527static int echoDisconnect(sqlite3_vtab *pVtab){
528 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
529 return echoDestructor(pVtab);
530}
danielk1977cc013f82006-06-24 06:36:11 +0000531
532/*
533** Echo virtual table module xDestroy method.
534*/
danielk19775fac9f82006-06-13 14:16:58 +0000535static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000536 int rc = SQLITE_OK;
537 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000538 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000539
540 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000541 if( p && p->zLogName ){
542 char *zSql;
drhbc6160b2009-04-08 15:45:31 +0000543 zSql = sqlite3_mprintf("DROP TABLE %Q", p->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000544 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000545 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000546 }
danielk1977cc013f82006-06-24 06:36:11 +0000547
danielk1977a298e902006-06-22 09:53:48 +0000548 if( rc==SQLITE_OK ){
549 rc = echoDestructor(pVtab);
550 }
551 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000552}
553
danielk1977cc013f82006-06-24 06:36:11 +0000554/*
555** Echo virtual table module xOpen method.
556*/
danielk19775fac9f82006-06-13 14:16:58 +0000557static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000558 echo_cursor *pCur;
danielk19773e3a84d2008-08-01 17:37:40 +0000559 if( simulateVtabError((echo_vtab *)pVTab, "xOpen") ){
560 return SQLITE_ERROR;
561 }
danielk197731dad9d2007-08-16 11:36:15 +0000562 pCur = sqlite3MallocZero(sizeof(echo_cursor));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000563 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000564 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000565}
566
danielk1977cc013f82006-06-24 06:36:11 +0000567/*
568** Echo virtual table module xClose method.
569*/
danielk19775fac9f82006-06-13 14:16:58 +0000570static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000571 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000572 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000573 sqlite3_stmt *pStmt = pCur->pStmt;
574 pCur->pStmt = 0;
drh17435752007-08-16 04:30:38 +0000575 sqlite3_free(pCur);
danielk1977be718892006-06-23 08:05:19 +0000576 rc = sqlite3_finalize(pStmt);
577 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000578}
579
danielk1977a298e902006-06-22 09:53:48 +0000580/*
581** Return non-zero if the cursor does not currently point to a valid record
582** (i.e if the scan has finished), or zero otherwise.
583*/
584static int echoEof(sqlite3_vtab_cursor *cur){
585 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
586}
587
danielk1977cc013f82006-06-24 06:36:11 +0000588/*
589** Echo virtual table module xNext method.
590*/
danielk19775fac9f82006-06-13 14:16:58 +0000591static int echoNext(sqlite3_vtab_cursor *cur){
danielk197731dad9d2007-08-16 11:36:15 +0000592 int rc = SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000593 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000594
danielk19773e3a84d2008-08-01 17:37:40 +0000595 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xNext") ){
596 return SQLITE_ERROR;
597 }
598
danielk197731dad9d2007-08-16 11:36:15 +0000599 if( pCur->pStmt ){
600 rc = sqlite3_step(pCur->pStmt);
601 if( rc==SQLITE_ROW ){
602 rc = SQLITE_OK;
603 }else{
604 rc = sqlite3_finalize(pCur->pStmt);
605 pCur->pStmt = 0;
606 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000607 }
608
609 return rc;
610}
611
danielk1977cc013f82006-06-24 06:36:11 +0000612/*
613** Echo virtual table module xColumn method.
614*/
danielk19775fac9f82006-06-13 14:16:58 +0000615static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000616 int iCol = i + 1;
617 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40 +0000618
619 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xColumn") ){
620 return SQLITE_ERROR;
621 }
622
danielk1977fbbe0052006-06-21 07:02:33 +0000623 if( !pStmt ){
624 sqlite3_result_null(ctx);
625 }else{
626 assert( sqlite3_data_count(pStmt)>iCol );
627 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
628 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000629 return SQLITE_OK;
630}
631
danielk1977cc013f82006-06-24 06:36:11 +0000632/*
633** Echo virtual table module xRowid method.
634*/
danielk19775fac9f82006-06-13 14:16:58 +0000635static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000636 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40 +0000637
638 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xRowid") ){
639 return SQLITE_ERROR;
640 }
641
danielk1977b7a7b9a2006-06-13 10:24:42 +0000642 *pRowid = sqlite3_column_int64(pStmt, 0);
643 return SQLITE_OK;
644}
645
danielk197798331532006-06-14 10:55:52 +0000646/*
647** Compute a simple hash of the null terminated string zString.
648**
649** This module uses only sqlite3_index_info.idxStr, not
650** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
651** in echoBestIndex(), idxNum is set to the corresponding hash value.
652** In echoFilter(), code assert()s that the supplied idxNum value is
653** indeed the hash of the supplied idxStr.
654*/
655static int hashString(const char *zString){
drh4081d5d2015-01-01 23:02:01 +0000656 u32 val = 0;
danielk197798331532006-06-14 10:55:52 +0000657 int ii;
658 for(ii=0; zString[ii]; ii++){
659 val = (val << 3) + (int)zString[ii];
660 }
drh4081d5d2015-01-01 23:02:01 +0000661 return (int)(val&0x7fffffff);
danielk197798331532006-06-14 10:55:52 +0000662}
663
danielk1977cc013f82006-06-24 06:36:11 +0000664/*
665** Echo virtual table module xFilter method.
666*/
danielk19775fac9f82006-06-13 14:16:58 +0000667static int echoFilter(
668 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000669 int idxNum, const char *idxStr,
670 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000671){
672 int rc;
drh4be8b512006-06-13 23:51:34 +0000673 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000674
675 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
676 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
677 sqlite3 *db = pVtab->db;
678
danielk19773e3a84d2008-08-01 17:37:40 +0000679 if( simulateVtabError(pVtab, "xFilter") ){
680 return SQLITE_ERROR;
681 }
682
danielk1977cc013f82006-06-24 06:36:11 +0000683 /* Check that idxNum matches idxStr */
684 assert( idxNum==hashString(idxStr) );
685
686 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000687 appendToEchoModule(pVtab->interp, "xFilter");
688 appendToEchoModule(pVtab->interp, idxStr);
689 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000690 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000691 }
692
danielk19775fac9f82006-06-13 14:16:58 +0000693 sqlite3_finalize(pCur->pStmt);
694 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000695
696 /* Prepare the SQL statement created by echoBestIndex and bind the
697 ** runtime parameters passed to this function to it.
698 */
drh4be8b512006-06-13 23:51:34 +0000699 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000700 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000701 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk19776eacd282009-04-29 11:50:53 +0000702 rc = sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000703 }
danielk1977cc013f82006-06-24 06:36:11 +0000704
705 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000706 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000707 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000708 }
709
danielk1977be718892006-06-23 08:05:19 +0000710 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000711}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000712
danielk1977cc013f82006-06-24 06:36:11 +0000713
714/*
715** A helper function used by echoUpdate() and echoBestIndex() for
716** manipulating strings in concert with the sqlite3_mprintf() function.
717**
718** Parameter pzStr points to a pointer to a string allocated with
719** sqlite3_mprintf. The second parameter, zAppend, points to another
720** string. The two strings are concatenated together and *pzStr
721** set to point at the result. The initial buffer pointed to by *pzStr
722** is deallocated via sqlite3_free().
723**
724** If the third argument, doFree, is true, then sqlite3_free() is
725** also called to free the buffer pointed to by zAppend.
726*/
danielk1977a1644fd2007-08-29 12:31:25 +0000727static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
danielk1977cc013f82006-06-24 06:36:11 +0000728 char *zIn = *pzStr;
danielk1977a1644fd2007-08-29 12:31:25 +0000729 if( !zAppend && doFree && *pRc==SQLITE_OK ){
730 *pRc = SQLITE_NOMEM;
731 }
732 if( *pRc!=SQLITE_OK ){
733 sqlite3_free(zIn);
734 zIn = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000735 }else{
danielk1977a1644fd2007-08-29 12:31:25 +0000736 if( zIn ){
737 char *zTemp = zIn;
738 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
739 sqlite3_free(zTemp);
740 }else{
741 zIn = sqlite3_mprintf("%s", zAppend);
742 }
743 if( !zIn ){
744 *pRc = SQLITE_NOMEM;
745 }
danielk1977cc013f82006-06-24 06:36:11 +0000746 }
747 *pzStr = zIn;
748 if( doFree ){
749 sqlite3_free(zAppend);
750 }
751}
752
drhb9bb7c12006-06-11 23:41:55 +0000753/*
dan1acb5392015-11-26 19:33:41 +0000754** This function returns a pointer to an sqlite3_malloc()ed buffer
755** containing the select-list (the thing between keywords SELECT and FROM)
756** to query the underlying real table with for the scan described by
757** argument pIdxInfo.
758**
759** If the current SQLite version is earlier than 3.10.0, this is just "*"
760** (select all columns). Or, for version 3.10.0 and greater, the list of
761** columns identified by the pIdxInfo->colUsed mask.
762*/
763static char *echoSelectList(echo_vtab *pTab, sqlite3_index_info *pIdxInfo){
764 char *zRet = 0;
765 if( sqlite3_libversion_number()<3010000 ){
766 zRet = sqlite3_mprintf(", *");
767 }else{
768 int i;
769 for(i=0; i<pTab->nCol; i++){
770 if( pIdxInfo->colUsed & ((sqlite3_uint64)1 << (i>=63 ? 63 : i)) ){
771 zRet = sqlite3_mprintf("%z, %s", zRet, pTab->aCol[i]);
772 }else{
773 zRet = sqlite3_mprintf("%z, NULL", zRet);
774 }
775 if( !zRet ) break;
776 }
777 }
778 return zRet;
779}
780
781/*
danielk19775fac9f82006-06-13 14:16:58 +0000782** The echo module implements the subset of query constraints and sort
783** orders that may take advantage of SQLite indices on the underlying
784** real table. For example, if the real table is declared as:
785**
786** CREATE TABLE real(a, b, c);
787** CREATE INDEX real_index ON real(b);
788**
789** then the echo module handles WHERE or ORDER BY clauses that refer
790** to the column "b", but not "a" or "c". If a multi-column index is
drh85b623f2007-12-13 21:54:09 +0000791** present, only its left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000792**
793** This xBestIndex method encodes the proposed search strategy as
794** an SQL query on the real table underlying the virtual echo module
795** table and stores the query in sqlite3_index_info.idxStr. The SQL
796** statement is of the form:
797**
798** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
799**
800** where the <where-clause> and <order-by-clause> are determined
801** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000802*/
danielk19775fac9f82006-06-13 14:16:58 +0000803static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
804 int ii;
drh4be8b512006-06-13 23:51:34 +0000805 char *zQuery = 0;
dan1acb5392015-11-26 19:33:41 +0000806 char *zCol = 0;
drh4be8b512006-06-13 23:51:34 +0000807 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000808 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000809 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000810 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000811 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000812 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000813
mistachkin27b2f052015-01-12 19:49:46 +0000814 int nRow = 0;
danielk197774cdba42006-06-19 12:02:58 +0000815 int useIdx = 0;
816 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000817 int useCost = 0;
mistachkin27b2f052015-01-12 19:49:46 +0000818 double cost = 0;
danielk197739359dc2008-03-17 09:36:44 +0000819 int isIgnoreUsable = 0;
820 if( Tcl_GetVar(interp, "echo_module_ignore_usable", TCL_GLOBAL_ONLY) ){
821 isIgnoreUsable = 1;
822 }
823
danielk19773e3a84d2008-08-01 17:37:40 +0000824 if( simulateVtabError(pVtab, "xBestIndex") ){
825 return SQLITE_ERROR;
826 }
827
danielk197774cdba42006-06-19 12:02:58 +0000828 /* Determine the number of rows in the table and store this value in local
829 ** variable nRow. The 'estimated-cost' of the scan will be the number of
830 ** rows in the table for a linear scan, or the log (base 2) of the
831 ** number of rows if the proposed scan uses an index.
832 */
danielk1977780b1d92007-03-30 14:56:34 +0000833 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
834 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
835 useCost = 1;
836 } else {
837 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000838 if( !zQuery ){
839 return SQLITE_NOMEM;
840 }
danielk1977780b1d92007-03-30 14:56:34 +0000841 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
842 sqlite3_free(zQuery);
843 if( rc!=SQLITE_OK ){
844 return rc;
845 }
846 sqlite3_step(pStmt);
847 nRow = sqlite3_column_int(pStmt, 0);
848 rc = sqlite3_finalize(pStmt);
849 if( rc!=SQLITE_OK ){
850 return rc;
851 }
danielk197774cdba42006-06-19 12:02:58 +0000852 }
danielk19775fac9f82006-06-13 14:16:58 +0000853
dan1acb5392015-11-26 19:33:41 +0000854 zCol = echoSelectList(pVtab, pIdxInfo);
855 if( !zCol ) return SQLITE_NOMEM;
856 zQuery = sqlite3_mprintf("SELECT rowid%z FROM %Q", zCol, pVtab->zTableName);
857 if( !zQuery ) return SQLITE_NOMEM;
858
danielk19775fac9f82006-06-13 14:16:58 +0000859 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
860 const struct sqlite3_index_constraint *pConstraint;
861 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000862 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000863
864 pConstraint = &pIdxInfo->aConstraint[ii];
865 pUsage = &pIdxInfo->aConstraintUsage[ii];
866
danielk197739359dc2008-03-17 09:36:44 +0000867 if( !isIgnoreUsable && !pConstraint->usable ) continue;
868
drh383736b2006-10-08 18:56:57 +0000869 iCol = pConstraint->iColumn;
drh82ebc2a2012-03-20 03:10:51 +0000870 if( iCol<0 || pVtab->aIndex[iCol] ){
mistachkin8ccdef62015-12-16 22:06:52 +0000871 char *zNewCol = iCol>=0 ? pVtab->aCol[iCol] : "rowid";
danielk19775fac9f82006-06-13 14:16:58 +0000872 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000873 useIdx = 1;
danielk19775fac9f82006-06-13 14:16:58 +0000874 switch( pConstraint->op ){
875 case SQLITE_INDEX_CONSTRAINT_EQ:
876 zOp = "="; break;
877 case SQLITE_INDEX_CONSTRAINT_LT:
878 zOp = "<"; break;
879 case SQLITE_INDEX_CONSTRAINT_GT:
880 zOp = ">"; break;
881 case SQLITE_INDEX_CONSTRAINT_LE:
882 zOp = "<="; break;
883 case SQLITE_INDEX_CONSTRAINT_GE:
884 zOp = ">="; break;
885 case SQLITE_INDEX_CONSTRAINT_MATCH:
mistachkina9124d32015-11-24 01:17:01 +0000886 /* Purposely translate the MATCH operator into a LIKE, which
887 ** will be used by the next block of code to construct a new
888 ** query. It should also be noted here that the next block
889 ** of code requires the first letter of this operator to be
890 ** in upper-case to trigger the special MATCH handling (i.e.
891 ** wrapping the bound parameter with literal '%'s).
892 */
drh1a90e092006-06-14 22:07:10 +0000893 zOp = "LIKE"; break;
dan07bdba82015-11-23 21:09:54 +0000894 case SQLITE_INDEX_CONSTRAINT_LIKE:
895 zOp = "like"; break;
896 case SQLITE_INDEX_CONSTRAINT_GLOB:
897 zOp = "glob"; break;
898 case SQLITE_INDEX_CONSTRAINT_REGEXP:
899 zOp = "regexp"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000900 }
dand03024d2017-09-09 19:41:12 +0000901 if( zOp ){
902 if( zOp[0]=='L' ){
903 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
904 zSep, zNewCol);
905 } else {
906 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zNewCol, zOp);
907 }
908 string_concat(&zQuery, zNew, 1, &rc);
909 zSep = "AND";
910 pUsage->argvIndex = ++nArg;
911 pUsage->omit = 1;
drh1a90e092006-06-14 22:07:10 +0000912 }
danielk19775fac9f82006-06-13 14:16:58 +0000913 }
914 }
danielk197747d08092006-06-14 07:41:31 +0000915
916 /* If there is only one term in the ORDER BY clause, and it is
917 ** on a column that this virtual table has an index for, then consume
918 ** the ORDER BY clause.
919 */
drh82ebc2a2012-03-20 03:10:51 +0000920 if( pIdxInfo->nOrderBy==1 && (
921 pIdxInfo->aOrderBy->iColumn<0 ||
922 pVtab->aIndex[pIdxInfo->aOrderBy->iColumn]) ){
danielk197765fd59f2006-06-24 11:51:33 +0000923 int iCol = pIdxInfo->aOrderBy->iColumn;
mistachkin8ccdef62015-12-16 22:06:52 +0000924 char *zNewCol = iCol>=0 ? pVtab->aCol[iCol] : "rowid";
danielk197747d08092006-06-14 07:41:31 +0000925 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
mistachkin8ccdef62015-12-16 22:06:52 +0000926 zNew = sqlite3_mprintf(" ORDER BY %s %s", zNewCol, zDir);
danielk1977a1644fd2007-08-29 12:31:25 +0000927 string_concat(&zQuery, zNew, 1, &rc);
danielk197747d08092006-06-14 07:41:31 +0000928 pIdxInfo->orderByConsumed = 1;
929 }
930
danielk19775fac9f82006-06-13 14:16:58 +0000931 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000932 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000933
danielk1977222a7572007-08-25 13:37:48 +0000934 if( !zQuery ){
danielk1977a1644fd2007-08-29 12:31:25 +0000935 return rc;
danielk1977222a7572007-08-25 13:37:48 +0000936 }
danielk197798331532006-06-14 10:55:52 +0000937 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000938 pIdxInfo->idxStr = zQuery;
939 pIdxInfo->needToFreeIdxStr = 1;
danielk19771d461462009-04-21 09:02:45 +0000940 if( useCost ){
danielk1977780b1d92007-03-30 14:56:34 +0000941 pIdxInfo->estimatedCost = cost;
danielk19771d461462009-04-21 09:02:45 +0000942 }else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000943 /* Approximation of log2(nRow). */
drh693e6712014-01-24 22:58:00 +0000944 for( ii=0; ii<(sizeof(int)*8)-1; ii++ ){
danielk197774cdba42006-06-19 12:02:58 +0000945 if( nRow & (1<<ii) ){
946 pIdxInfo->estimatedCost = (double)ii;
947 }
948 }
danielk19771d461462009-04-21 09:02:45 +0000949 }else{
danielk197774cdba42006-06-19 12:02:58 +0000950 pIdxInfo->estimatedCost = (double)nRow;
951 }
952 return rc;
drha967e882006-06-13 01:04:52 +0000953}
954
danielk1977176f4d22006-06-15 10:41:15 +0000955/*
danielk1977cc013f82006-06-24 06:36:11 +0000956** The xUpdate method for echo module virtual tables.
957**
danielk1977176f4d22006-06-15 10:41:15 +0000958** apData[0] apData[1] apData[2..]
959**
960** INTEGER DELETE
961**
962** INTEGER NULL (nCol args) UPDATE (do not set rowid)
963** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
964**
965** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
966** NULL INTEGER (nCol args) INSERT (incl. rowid value)
967**
968*/
danielk19771f6eec52006-06-16 06:17:47 +0000969int echoUpdate(
970 sqlite3_vtab *tab,
971 int nData,
972 sqlite3_value **apData,
973 sqlite_int64 *pRowid
974){
danielk197726e41442006-06-14 15:16:35 +0000975 echo_vtab *pVtab = (echo_vtab *)tab;
976 sqlite3 *db = pVtab->db;
977 int rc = SQLITE_OK;
978
mistachkin27b2f052015-01-12 19:49:46 +0000979 sqlite3_stmt *pStmt = 0;
danielk1977176f4d22006-06-15 10:41:15 +0000980 char *z = 0; /* SQL statement to execute */
981 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
982 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
983 int i; /* Counter variable used by for loops */
984
danielk197726e41442006-06-14 15:16:35 +0000985 assert( nData==pVtab->nCol+2 || nData==1 );
986
drhcd3dd9d2008-04-28 20:27:53 +0000987 /* Ticket #3083 - make sure we always start a transaction prior to
988 ** making any changes to a virtual table */
989 assert( pVtab->inTransaction );
990
danielk19773e3a84d2008-08-01 17:37:40 +0000991 if( simulateVtabError(pVtab, "xUpdate") ){
992 return SQLITE_ERROR;
993 }
994
drh5aec0422006-06-14 23:43:31 +0000995 /* If apData[0] is an integer and nData>1 then do an UPDATE */
996 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000997 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000998 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000999 if( !z ){
1000 rc = SQLITE_NOMEM;
1001 }
danielk1977176f4d22006-06-15 10:41:15 +00001002
1003 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
1004 bindArgZero = 1;
1005
1006 if( bindArgOne ){
danielk1977a1644fd2007-08-29 12:31:25 +00001007 string_concat(&z, " SET rowid=?1 ", 0, &rc);
drh5aec0422006-06-14 23:43:31 +00001008 zSep = ",";
1009 }
1010 for(i=2; i<nData; i++){
1011 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +00001012 string_concat(&z, sqlite3_mprintf(
danielk1977a1644fd2007-08-29 12:31:25 +00001013 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
drh5aec0422006-06-14 23:43:31 +00001014 zSep = ",";
1015 }
danielk1977a1644fd2007-08-29 12:31:25 +00001016 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
drh5aec0422006-06-14 23:43:31 +00001017 }
1018
danielk1977176f4d22006-06-15 10:41:15 +00001019 /* If apData[0] is an integer and nData==1 then do a DELETE */
1020 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
1021 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +00001022 if( !z ){
1023 rc = SQLITE_NOMEM;
1024 }
danielk1977176f4d22006-06-15 10:41:15 +00001025 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +00001026 }
1027
danielk1977176f4d22006-06-15 10:41:15 +00001028 /* If the first argument is NULL and there are more than two args, INSERT */
1029 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +00001030 int ii;
1031 char *zInsert = 0;
1032 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +00001033
danielk19774b2688a2006-06-20 11:01:07 +00001034 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +00001035 if( !zInsert ){
1036 rc = SQLITE_NOMEM;
1037 }
danielk1977176f4d22006-06-15 10:41:15 +00001038 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
1039 bindArgOne = 1;
1040 zValues = sqlite3_mprintf("?");
danielk1977a1644fd2007-08-29 12:31:25 +00001041 string_concat(&zInsert, "rowid", 0, &rc);
danielk197726e41442006-06-14 15:16:35 +00001042 }
1043
danielk1977176f4d22006-06-15 10:41:15 +00001044 assert((pVtab->nCol+2)==nData);
1045 for(ii=2; ii<nData; ii++){
1046 string_concat(&zInsert,
danielk1977a1644fd2007-08-29 12:31:25 +00001047 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
danielk1977176f4d22006-06-15 10:41:15 +00001048 string_concat(&zValues,
danielk1977a1644fd2007-08-29 12:31:25 +00001049 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
danielk197726e41442006-06-14 15:16:35 +00001050 }
1051
danielk1977a1644fd2007-08-29 12:31:25 +00001052 string_concat(&z, zInsert, 1, &rc);
1053 string_concat(&z, ") VALUES(", 0, &rc);
1054 string_concat(&z, zValues, 1, &rc);
1055 string_concat(&z, ")", 0, &rc);
danielk1977176f4d22006-06-15 10:41:15 +00001056 }
1057
1058 /* Anything else is an error */
1059 else{
1060 assert(0);
1061 return SQLITE_ERROR;
1062 }
1063
danielk1977a1644fd2007-08-29 12:31:25 +00001064 if( rc==SQLITE_OK ){
1065 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
1066 }
danielk1977176f4d22006-06-15 10:41:15 +00001067 assert( rc!=SQLITE_OK || pStmt );
1068 sqlite3_free(z);
1069 if( rc==SQLITE_OK ) {
1070 if( bindArgZero ){
1071 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +00001072 }
danielk1977176f4d22006-06-15 10:41:15 +00001073 if( bindArgOne ){
1074 sqlite3_bind_value(pStmt, 1, apData[1]);
1075 }
danielk1977a7a8e142008-02-13 18:25:27 +00001076 for(i=2; i<nData && rc==SQLITE_OK; i++){
1077 if( apData[i] ) rc = sqlite3_bind_value(pStmt, i, apData[i]);
danielk1977176f4d22006-06-15 10:41:15 +00001078 }
danielk1977a7a8e142008-02-13 18:25:27 +00001079 if( rc==SQLITE_OK ){
1080 sqlite3_step(pStmt);
1081 rc = sqlite3_finalize(pStmt);
1082 }else{
1083 sqlite3_finalize(pStmt);
1084 }
danielk197726e41442006-06-14 15:16:35 +00001085 }
1086
danielk19771f6eec52006-06-16 06:17:47 +00001087 if( pRowid && rc==SQLITE_OK ){
1088 *pRowid = sqlite3_last_insert_rowid(db);
1089 }
drh4dc754d2008-07-23 18:17:32 +00001090 if( rc!=SQLITE_OK ){
1091 tab->zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", sqlite3_errmsg(db));
1092 }
danielk19771f6eec52006-06-16 06:17:47 +00001093
danielk197726e41442006-06-14 15:16:35 +00001094 return rc;
1095}
1096
drha967e882006-06-13 01:04:52 +00001097/*
danielk1977c69cdfd2006-06-17 09:39:55 +00001098** xBegin, xSync, xCommit and xRollback callbacks for echo module
1099** virtual tables. Do nothing other than add the name of the callback
1100** to the $::echo_module Tcl variable.
1101*/
1102static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
1103 char *z;
1104 echo_vtab *pVtab = (echo_vtab *)tab;
1105 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
drh344c38e2008-05-05 13:23:04 +00001106 if( z==0 ) return SQLITE_NOMEM;
danielk1977c69cdfd2006-06-17 09:39:55 +00001107 appendToEchoModule(pVtab->interp, zCall);
1108 appendToEchoModule(pVtab->interp, z);
1109 sqlite3_free(z);
drh344c38e2008-05-05 13:23:04 +00001110 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +00001111}
1112static int echoBegin(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001113 int rc;
danielk19775017dc32006-06-24 09:34:22 +00001114 echo_vtab *pVtab = (echo_vtab *)tab;
1115 Tcl_Interp *interp = pVtab->interp;
1116 const char *zVal;
1117
drhcd3dd9d2008-04-28 20:27:53 +00001118 /* Ticket #3083 - do not start a transaction if we are already in
1119 ** a transaction */
1120 assert( !pVtab->inTransaction );
1121
danielk19773e3a84d2008-08-01 17:37:40 +00001122 if( simulateVtabError(pVtab, "xBegin") ){
1123 return SQLITE_ERROR;
1124 }
1125
danielk1977a1644fd2007-08-29 12:31:25 +00001126 rc = echoTransactionCall(tab, "xBegin");
danielk19775017dc32006-06-24 09:34:22 +00001127
danielk1977a1644fd2007-08-29 12:31:25 +00001128 if( rc==SQLITE_OK ){
1129 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
1130 ** and it is set to the name of the real table underlying this virtual
1131 ** echo module table, then cause this xSync operation to fail.
1132 */
1133 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
1134 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1135 rc = SQLITE_ERROR;
1136 }
danielk19775017dc32006-06-24 09:34:22 +00001137 }
drhcd3dd9d2008-04-28 20:27:53 +00001138 if( rc==SQLITE_OK ){
1139 pVtab->inTransaction = 1;
1140 }
danielk1977a1644fd2007-08-29 12:31:25 +00001141 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001142}
1143static int echoSync(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001144 int rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001145 echo_vtab *pVtab = (echo_vtab *)tab;
1146 Tcl_Interp *interp = pVtab->interp;
1147 const char *zVal;
1148
drhcd3dd9d2008-04-28 20:27:53 +00001149 /* Ticket #3083 - Only call xSync if we have previously started a
1150 ** transaction */
1151 assert( pVtab->inTransaction );
1152
danielk19773e3a84d2008-08-01 17:37:40 +00001153 if( simulateVtabError(pVtab, "xSync") ){
1154 return SQLITE_ERROR;
1155 }
1156
danielk1977a1644fd2007-08-29 12:31:25 +00001157 rc = echoTransactionCall(tab, "xSync");
danielk1977c69cdfd2006-06-17 09:39:55 +00001158
danielk1977a1644fd2007-08-29 12:31:25 +00001159 if( rc==SQLITE_OK ){
1160 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
1161 ** and it is set to the name of the real table underlying this virtual
1162 ** echo module table, then cause this xSync operation to fail.
1163 */
1164 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
1165 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1166 rc = -1;
1167 }
danielk1977c69cdfd2006-06-17 09:39:55 +00001168 }
danielk1977a1644fd2007-08-29 12:31:25 +00001169 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001170}
1171static int echoCommit(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:53 +00001172 echo_vtab *pVtab = (echo_vtab*)tab;
drh643167f2008-01-22 21:30:53 +00001173 int rc;
drhcd3dd9d2008-04-28 20:27:53 +00001174
1175 /* Ticket #3083 - Only call xCommit if we have previously started
1176 ** a transaction */
1177 assert( pVtab->inTransaction );
1178
danielk19773e3a84d2008-08-01 17:37:40 +00001179 if( simulateVtabError(pVtab, "xCommit") ){
1180 return SQLITE_ERROR;
1181 }
1182
danielk19772d1d86f2008-06-20 14:59:51 +00001183 sqlite3BeginBenignMalloc();
drh643167f2008-01-22 21:30:53 +00001184 rc = echoTransactionCall(tab, "xCommit");
danielk19772d1d86f2008-06-20 14:59:51 +00001185 sqlite3EndBenignMalloc();
drh344c38e2008-05-05 13:23:04 +00001186 pVtab->inTransaction = 0;
drh643167f2008-01-22 21:30:53 +00001187 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001188}
1189static int echoRollback(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:53 +00001190 int rc;
1191 echo_vtab *pVtab = (echo_vtab*)tab;
1192
1193 /* Ticket #3083 - Only call xRollback if we have previously started
1194 ** a transaction */
1195 assert( pVtab->inTransaction );
1196
1197 rc = echoTransactionCall(tab, "xRollback");
1198 pVtab->inTransaction = 0;
1199 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001200}
1201
1202/*
drhe94b0c32006-07-08 18:09:15 +00001203** Implementation of "GLOB" function on the echo module. Pass
1204** all arguments to the ::echo_glob_overload procedure of TCL
1205** and return the result of that procedure as a string.
1206*/
1207static void overloadedGlobFunction(
1208 sqlite3_context *pContext,
1209 int nArg,
1210 sqlite3_value **apArg
1211){
1212 Tcl_Interp *interp = sqlite3_user_data(pContext);
1213 Tcl_DString str;
1214 int i;
1215 int rc;
1216 Tcl_DStringInit(&str);
1217 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
1218 for(i=0; i<nArg; i++){
1219 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
1220 }
1221 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
1222 Tcl_DStringFree(&str);
1223 if( rc ){
1224 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
1225 }else{
1226 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1227 -1, SQLITE_TRANSIENT);
1228 }
1229 Tcl_ResetResult(interp);
1230}
1231
1232/*
1233** This is the xFindFunction implementation for the echo module.
1234** SQLite calls this routine when the first argument of a function
1235** is a column of an echo virtual table. This routine can optionally
1236** override the implementation of that function. It will choose to
1237** do so if the function is named "glob", and a TCL command named
1238** ::echo_glob_overload exists.
1239*/
1240static int echoFindFunction(
1241 sqlite3_vtab *vtab,
1242 int nArg,
1243 const char *zFuncName,
1244 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1245 void **ppArg
1246){
1247 echo_vtab *pVtab = (echo_vtab *)vtab;
1248 Tcl_Interp *interp = pVtab->interp;
1249 Tcl_CmdInfo info;
1250 if( strcmp(zFuncName,"glob")!=0 ){
1251 return 0;
1252 }
1253 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1254 return 0;
1255 }
1256 *pxFunc = overloadedGlobFunction;
1257 *ppArg = interp;
1258 return 1;
1259}
1260
danielk1977182c4ba2007-06-27 15:53:34 +00001261static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1262 int rc = SQLITE_OK;
1263 echo_vtab *p = (echo_vtab *)vtab;
1264
danielk1977987a00e2008-08-01 17:51:47 +00001265 if( simulateVtabError(p, "xRename") ){
1266 return SQLITE_ERROR;
1267 }
1268
danielk1977182c4ba2007-06-27 15:53:34 +00001269 if( p->isPattern ){
drh83cc1392012-04-19 18:04:28 +00001270 int nThis = (int)strlen(p->zThis);
drhbc6160b2009-04-08 15:45:31 +00001271 char *zSql = sqlite3_mprintf("ALTER TABLE %s RENAME TO %s%s",
danielk1977182c4ba2007-06-27 15:53:34 +00001272 p->zTableName, zNewName, &p->zTableName[nThis]
1273 );
1274 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +00001275 sqlite3_free(zSql);
danielk1977182c4ba2007-06-27 15:53:34 +00001276 }
1277
1278 return rc;
1279}
1280
dan3b504df2011-10-29 15:29:43 +00001281static int echoSavepoint(sqlite3_vtab *pVTab, int iSavepoint){
1282 assert( pVTab );
1283 return SQLITE_OK;
1284}
1285
1286static int echoRelease(sqlite3_vtab *pVTab, int iSavepoint){
1287 assert( pVTab );
1288 return SQLITE_OK;
1289}
1290
1291static int echoRollbackTo(sqlite3_vtab *pVTab, int iSavepoint){
1292 assert( pVTab );
1293 return SQLITE_OK;
1294}
1295
drhe94b0c32006-07-08 18:09:15 +00001296/*
danielk1977cc013f82006-06-24 06:36:11 +00001297** A virtual table module that merely "echos" the contents of another
1298** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +00001299*/
1300static sqlite3_module echoModule = {
dan3b504df2011-10-29 15:29:43 +00001301 1, /* iVersion */
1302 echoCreate,
1303 echoConnect,
1304 echoBestIndex,
1305 echoDisconnect,
1306 echoDestroy,
1307 echoOpen, /* xOpen - open a cursor */
1308 echoClose, /* xClose - close a cursor */
1309 echoFilter, /* xFilter - configure scan constraints */
1310 echoNext, /* xNext - advance a cursor */
1311 echoEof, /* xEof */
1312 echoColumn, /* xColumn - read data */
1313 echoRowid, /* xRowid - read data */
1314 echoUpdate, /* xUpdate - write data */
1315 echoBegin, /* xBegin - begin transaction */
1316 echoSync, /* xSync - sync transaction */
1317 echoCommit, /* xCommit - commit transaction */
1318 echoRollback, /* xRollback - rollback transaction */
1319 echoFindFunction, /* xFindFunction - function overloading */
1320 echoRename /* xRename - rename the table */
1321};
1322
1323static sqlite3_module echoModuleV2 = {
1324 2, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +00001325 echoCreate,
1326 echoConnect,
drha967e882006-06-13 01:04:52 +00001327 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +00001328 echoDisconnect,
1329 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +00001330 echoOpen, /* xOpen - open a cursor */
1331 echoClose, /* xClose - close a cursor */
1332 echoFilter, /* xFilter - configure scan constraints */
1333 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +00001334 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +00001335 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +00001336 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +00001337 echoUpdate, /* xUpdate - write data */
1338 echoBegin, /* xBegin - begin transaction */
1339 echoSync, /* xSync - sync transaction */
1340 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +00001341 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +00001342 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:34 +00001343 echoRename, /* xRename - rename the table */
dan3b504df2011-10-29 15:29:43 +00001344 echoSavepoint,
1345 echoRelease,
1346 echoRollbackTo
drhb9bb7c12006-06-11 23:41:55 +00001347};
1348
1349/*
1350** Decode a pointer to an sqlite3 object.
1351*/
drh24b58dd2008-07-07 14:50:14 +00001352extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
mistachkine84d8d32013-04-29 03:09:10 +00001353extern const char *sqlite3ErrName(int);
drhb9bb7c12006-06-11 23:41:55 +00001354
danielk1977860b6cd2007-09-03 11:51:50 +00001355static void moduleDestroy(void *p){
drhba39ca42021-05-17 13:11:24 +00001356 EchoModule *pMod = (EchoModule*)p;
1357 sqlite3_create_function(pMod->db, "function_that_does_not_exist_0982ma98",
1358 SQLITE_ANY, 1, 0, 0, 0, 0);
danielk1977860b6cd2007-09-03 11:51:50 +00001359 sqlite3_free(p);
1360}
1361
drhb9bb7c12006-06-11 23:41:55 +00001362/*
1363** Register the echo virtual table module.
1364*/
mistachkin7617e4a2016-07-28 17:11:20 +00001365static int SQLITE_TCLAPI register_echo_module(
drhb9bb7c12006-06-11 23:41:55 +00001366 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1367 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1368 int objc, /* Number of arguments */
1369 Tcl_Obj *CONST objv[] /* Command arguments */
1370){
danca8b9ba2012-05-16 14:29:11 +00001371 int rc;
drhb9bb7c12006-06-11 23:41:55 +00001372 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:50 +00001373 EchoModule *pMod;
drhb9bb7c12006-06-11 23:41:55 +00001374 if( objc!=2 ){
1375 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1376 return TCL_ERROR;
1377 }
1378 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
dan3b504df2011-10-29 15:29:43 +00001379
1380 /* Virtual table module "echo" */
danielk1977860b6cd2007-09-03 11:51:50 +00001381 pMod = sqlite3_malloc(sizeof(EchoModule));
1382 pMod->interp = interp;
drhba39ca42021-05-17 13:11:24 +00001383 pMod->db = db;
danca8b9ba2012-05-16 14:29:11 +00001384 rc = sqlite3_create_module_v2(
1385 db, "echo", &echoModule, (void*)pMod, moduleDestroy
1386 );
dan3b504df2011-10-29 15:29:43 +00001387
1388 /* Virtual table module "echo_v2" */
danca8b9ba2012-05-16 14:29:11 +00001389 if( rc==SQLITE_OK ){
1390 pMod = sqlite3_malloc(sizeof(EchoModule));
1391 pMod->interp = interp;
drhba39ca42021-05-17 13:11:24 +00001392 pMod->db = db;
danca8b9ba2012-05-16 14:29:11 +00001393 rc = sqlite3_create_module_v2(db, "echo_v2",
1394 &echoModuleV2, (void*)pMod, moduleDestroy
1395 );
1396 }
1397
mistachkine84d8d32013-04-29 03:09:10 +00001398 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
drhb9bb7c12006-06-11 23:41:55 +00001399 return TCL_OK;
1400}
1401
danielk19775017dc32006-06-24 09:34:22 +00001402/*
1403** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1404**
1405** sqlite3_declare_vtab DB SQL
1406*/
mistachkin7617e4a2016-07-28 17:11:20 +00001407static int SQLITE_TCLAPI declare_vtab(
danielk19775017dc32006-06-24 09:34:22 +00001408 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1409 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1410 int objc, /* Number of arguments */
1411 Tcl_Obj *CONST objv[] /* Command arguments */
1412){
1413 sqlite3 *db;
1414 int rc;
1415 if( objc!=3 ){
1416 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1417 return TCL_ERROR;
1418 }
1419 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1420 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1421 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001422 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001423 return TCL_ERROR;
1424 }
1425 return TCL_OK;
1426}
1427
danielk1977cc013f82006-06-24 06:36:11 +00001428#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001429
1430/*
1431** Register commands with the TCL interpreter.
1432*/
1433int Sqlitetest8_Init(Tcl_Interp *interp){
danielk19776e891622008-08-12 14:48:40 +00001434#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001435 static struct {
1436 char *zName;
1437 Tcl_ObjCmdProc *xProc;
1438 void *clientData;
1439 } aObjCmd[] = {
dan2deb1652012-07-13 16:15:20 +00001440 { "register_echo_module", register_echo_module, 0 },
dan2deb1652012-07-13 16:15:20 +00001441 { "sqlite3_declare_vtab", declare_vtab, 0 },
drhb9bb7c12006-06-11 23:41:55 +00001442 };
1443 int i;
1444 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1445 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1446 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1447 }
danielk19776e891622008-08-12 14:48:40 +00001448#endif
drhb9bb7c12006-06-11 23:41:55 +00001449 return TCL_OK;
1450}