blob: daab504e4ea683a917f2abb6da262dea5237830f [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,
danielk19777e6ebfb2006-06-12 11:24:37 +0000344 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
345 -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;
392};
393
danielk1977cc013f82006-06-24 06:36:11 +0000394/*
395** This function is called to do the work of the xConnect() method -
396** to allocate the required in-memory structures for a newly connected
397** virtual table.
398*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000399static int echoConstructor(
400 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000401 void *pAux,
drhe4102962006-09-11 00:34:22 +0000402 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000403 sqlite3_vtab **ppVtab,
404 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000405){
danielk1977a1644fd2007-08-29 12:31:25 +0000406 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000407 int i;
408 echo_vtab *pVtab;
409
danielk1977cc013f82006-06-24 06:36:11 +0000410 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk197731dad9d2007-08-16 11:36:15 +0000411 pVtab = sqlite3MallocZero( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000412 if( !pVtab ){
413 return SQLITE_NOMEM;
414 }
danielk1977860b6cd2007-09-03 11:51:50 +0000415 pVtab->interp = ((EchoModule *)pAux)->interp;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000416 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000417
danielk1977182c4ba2007-06-27 15:53:34 +0000418 /* Allocate echo_vtab.zThis */
drhbc6160b2009-04-08 15:45:31 +0000419 pVtab->zThis = sqlite3_mprintf("%s", argv[2]);
danielk1977182c4ba2007-06-27 15:53:34 +0000420 if( !pVtab->zThis ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000421 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000422 return SQLITE_NOMEM;
423 }
424
danielk1977182c4ba2007-06-27 15:53:34 +0000425 /* Allocate echo_vtab.zTableName */
426 if( argc>3 ){
drhbc6160b2009-04-08 15:45:31 +0000427 pVtab->zTableName = sqlite3_mprintf("%s", argv[3]);
danielk1977182c4ba2007-06-27 15:53:34 +0000428 dequoteString(pVtab->zTableName);
429 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
drhbc6160b2009-04-08 15:45:31 +0000430 char *z = sqlite3_mprintf("%s%s", argv[2], &(pVtab->zTableName[1]));
drh17435752007-08-16 04:30:38 +0000431 sqlite3_free(pVtab->zTableName);
danielk1977182c4ba2007-06-27 15:53:34 +0000432 pVtab->zTableName = z;
433 pVtab->isPattern = 1;
434 }
435 if( !pVtab->zTableName ){
436 echoDestructor((sqlite3_vtab *)pVtab);
437 return SQLITE_NOMEM;
438 }
439 }
440
danielk1977cc013f82006-06-24 06:36:11 +0000441 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000442 for(i=0; i<argc; i++){
443 appendToEchoModule(pVtab->interp, argv[i]);
444 }
445
danielk1977cc013f82006-06-24 06:36:11 +0000446 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
447 ** structure. If an error occurs, delete the sqlite3_vtab structure and
448 ** return an error code.
449 */
danielk1977a1644fd2007-08-29 12:31:25 +0000450 rc = echoDeclareVtab(pVtab, db);
451 if( rc!=SQLITE_OK ){
danielk1977a4e76362006-06-14 06:31:28 +0000452 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977a1644fd2007-08-29 12:31:25 +0000453 return rc;
danielk1977a4e76362006-06-14 06:31:28 +0000454 }
455
danielk1977cc013f82006-06-24 06:36:11 +0000456 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000457 *ppVtab = &pVtab->base;
458 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000459}
drha967e882006-06-13 01:04:52 +0000460
danielk1977cc013f82006-06-24 06:36:11 +0000461/*
462** Echo virtual table module xCreate method.
463*/
drhb9bb7c12006-06-11 23:41:55 +0000464static int echoCreate(
465 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000466 void *pAux,
drhe4102962006-09-11 00:34:22 +0000467 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000468 sqlite3_vtab **ppVtab,
469 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000470){
danielk1977a298e902006-06-22 09:53:48 +0000471 int rc = SQLITE_OK;
danielk1977860b6cd2007-09-03 11:51:50 +0000472 appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000473 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000474
475 /* If there were two arguments passed to the module at the SQL level
476 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
477 ** the second argument is used as a table name. Attempt to create
478 ** such a table with a single column, "logmsg". This table will
479 ** be used to log calls to the xUpdate method. It will be deleted
480 ** when the virtual table is DROPed.
481 **
482 ** Note: The main point of this is to test that we can drop tables
483 ** from within an xDestroy method call.
484 */
danielk1977a298e902006-06-22 09:53:48 +0000485 if( rc==SQLITE_OK && argc==5 ){
486 char *zSql;
487 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
drhbc6160b2009-04-08 15:45:31 +0000488 pVtab->zLogName = sqlite3_mprintf("%s", argv[4]);
489 zSql = sqlite3_mprintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000490 rc = sqlite3_exec(db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000491 sqlite3_free(zSql);
danielk1977cd2543b2007-09-03 15:03:20 +0000492 if( rc!=SQLITE_OK ){
drhb9755982010-07-24 16:34:37 +0000493 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
danielk1977cd2543b2007-09-03 15:03:20 +0000494 }
495 }
496
497 if( *ppVtab && rc!=SQLITE_OK ){
498 echoDestructor(*ppVtab);
499 *ppVtab = 0;
danielk1977a298e902006-06-22 09:53:48 +0000500 }
danielk1977cc013f82006-06-24 06:36:11 +0000501
drhcd3dd9d2008-04-28 20:27:53 +0000502 if( rc==SQLITE_OK ){
503 (*(echo_vtab**)ppVtab)->inTransaction = 1;
504 }
505
danielk1977a298e902006-06-22 09:53:48 +0000506 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000507}
danielk1977cc013f82006-06-24 06:36:11 +0000508
509/*
510** Echo virtual table module xConnect method.
511*/
drhb9bb7c12006-06-11 23:41:55 +0000512static int echoConnect(
513 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000514 void *pAux,
drhe4102962006-09-11 00:34:22 +0000515 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000516 sqlite3_vtab **ppVtab,
517 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000518){
danielk1977860b6cd2007-09-03 11:51:50 +0000519 appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000520 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000521}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000522
danielk1977cc013f82006-06-24 06:36:11 +0000523/*
524** Echo virtual table module xDisconnect method.
525*/
danielk19775fac9f82006-06-13 14:16:58 +0000526static int echoDisconnect(sqlite3_vtab *pVtab){
527 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
528 return echoDestructor(pVtab);
529}
danielk1977cc013f82006-06-24 06:36:11 +0000530
531/*
532** Echo virtual table module xDestroy method.
533*/
danielk19775fac9f82006-06-13 14:16:58 +0000534static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000535 int rc = SQLITE_OK;
536 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000537 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000538
539 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000540 if( p && p->zLogName ){
541 char *zSql;
drhbc6160b2009-04-08 15:45:31 +0000542 zSql = sqlite3_mprintf("DROP TABLE %Q", p->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000543 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000544 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000545 }
danielk1977cc013f82006-06-24 06:36:11 +0000546
danielk1977a298e902006-06-22 09:53:48 +0000547 if( rc==SQLITE_OK ){
548 rc = echoDestructor(pVtab);
549 }
550 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000551}
552
danielk1977cc013f82006-06-24 06:36:11 +0000553/*
554** Echo virtual table module xOpen method.
555*/
danielk19775fac9f82006-06-13 14:16:58 +0000556static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000557 echo_cursor *pCur;
danielk19773e3a84d2008-08-01 17:37:40 +0000558 if( simulateVtabError((echo_vtab *)pVTab, "xOpen") ){
559 return SQLITE_ERROR;
560 }
danielk197731dad9d2007-08-16 11:36:15 +0000561 pCur = sqlite3MallocZero(sizeof(echo_cursor));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000562 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000563 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000564}
565
danielk1977cc013f82006-06-24 06:36:11 +0000566/*
567** Echo virtual table module xClose method.
568*/
danielk19775fac9f82006-06-13 14:16:58 +0000569static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000570 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000571 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000572 sqlite3_stmt *pStmt = pCur->pStmt;
573 pCur->pStmt = 0;
drh17435752007-08-16 04:30:38 +0000574 sqlite3_free(pCur);
danielk1977be718892006-06-23 08:05:19 +0000575 rc = sqlite3_finalize(pStmt);
576 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000577}
578
danielk1977a298e902006-06-22 09:53:48 +0000579/*
580** Return non-zero if the cursor does not currently point to a valid record
581** (i.e if the scan has finished), or zero otherwise.
582*/
583static int echoEof(sqlite3_vtab_cursor *cur){
584 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
585}
586
danielk1977cc013f82006-06-24 06:36:11 +0000587/*
588** Echo virtual table module xNext method.
589*/
danielk19775fac9f82006-06-13 14:16:58 +0000590static int echoNext(sqlite3_vtab_cursor *cur){
danielk197731dad9d2007-08-16 11:36:15 +0000591 int rc = SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000592 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000593
danielk19773e3a84d2008-08-01 17:37:40 +0000594 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xNext") ){
595 return SQLITE_ERROR;
596 }
597
danielk197731dad9d2007-08-16 11:36:15 +0000598 if( pCur->pStmt ){
599 rc = sqlite3_step(pCur->pStmt);
600 if( rc==SQLITE_ROW ){
601 rc = SQLITE_OK;
602 }else{
603 rc = sqlite3_finalize(pCur->pStmt);
604 pCur->pStmt = 0;
605 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000606 }
607
608 return rc;
609}
610
danielk1977cc013f82006-06-24 06:36:11 +0000611/*
612** Echo virtual table module xColumn method.
613*/
danielk19775fac9f82006-06-13 14:16:58 +0000614static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000615 int iCol = i + 1;
616 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40 +0000617
618 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xColumn") ){
619 return SQLITE_ERROR;
620 }
621
danielk1977fbbe0052006-06-21 07:02:33 +0000622 if( !pStmt ){
623 sqlite3_result_null(ctx);
624 }else{
625 assert( sqlite3_data_count(pStmt)>iCol );
626 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
627 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000628 return SQLITE_OK;
629}
630
danielk1977cc013f82006-06-24 06:36:11 +0000631/*
632** Echo virtual table module xRowid method.
633*/
danielk19775fac9f82006-06-13 14:16:58 +0000634static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000635 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40 +0000636
637 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xRowid") ){
638 return SQLITE_ERROR;
639 }
640
danielk1977b7a7b9a2006-06-13 10:24:42 +0000641 *pRowid = sqlite3_column_int64(pStmt, 0);
642 return SQLITE_OK;
643}
644
danielk197798331532006-06-14 10:55:52 +0000645/*
646** Compute a simple hash of the null terminated string zString.
647**
648** This module uses only sqlite3_index_info.idxStr, not
649** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
650** in echoBestIndex(), idxNum is set to the corresponding hash value.
651** In echoFilter(), code assert()s that the supplied idxNum value is
652** indeed the hash of the supplied idxStr.
653*/
654static int hashString(const char *zString){
drh4081d5d2015-01-01 23:02:01 +0000655 u32 val = 0;
danielk197798331532006-06-14 10:55:52 +0000656 int ii;
657 for(ii=0; zString[ii]; ii++){
658 val = (val << 3) + (int)zString[ii];
659 }
drh4081d5d2015-01-01 23:02:01 +0000660 return (int)(val&0x7fffffff);
danielk197798331532006-06-14 10:55:52 +0000661}
662
danielk1977cc013f82006-06-24 06:36:11 +0000663/*
664** Echo virtual table module xFilter method.
665*/
danielk19775fac9f82006-06-13 14:16:58 +0000666static int echoFilter(
667 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000668 int idxNum, const char *idxStr,
669 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000670){
671 int rc;
drh4be8b512006-06-13 23:51:34 +0000672 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000673
674 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
675 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
676 sqlite3 *db = pVtab->db;
677
danielk19773e3a84d2008-08-01 17:37:40 +0000678 if( simulateVtabError(pVtab, "xFilter") ){
679 return SQLITE_ERROR;
680 }
681
danielk1977cc013f82006-06-24 06:36:11 +0000682 /* Check that idxNum matches idxStr */
683 assert( idxNum==hashString(idxStr) );
684
685 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000686 appendToEchoModule(pVtab->interp, "xFilter");
687 appendToEchoModule(pVtab->interp, idxStr);
688 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000689 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000690 }
691
danielk19775fac9f82006-06-13 14:16:58 +0000692 sqlite3_finalize(pCur->pStmt);
693 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000694
695 /* Prepare the SQL statement created by echoBestIndex and bind the
696 ** runtime parameters passed to this function to it.
697 */
drh4be8b512006-06-13 23:51:34 +0000698 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000699 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000700 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk19776eacd282009-04-29 11:50:53 +0000701 rc = sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000702 }
danielk1977cc013f82006-06-24 06:36:11 +0000703
704 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000705 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000706 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000707 }
708
danielk1977be718892006-06-23 08:05:19 +0000709 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000710}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000711
danielk1977cc013f82006-06-24 06:36:11 +0000712
713/*
714** A helper function used by echoUpdate() and echoBestIndex() for
715** manipulating strings in concert with the sqlite3_mprintf() function.
716**
717** Parameter pzStr points to a pointer to a string allocated with
718** sqlite3_mprintf. The second parameter, zAppend, points to another
719** string. The two strings are concatenated together and *pzStr
720** set to point at the result. The initial buffer pointed to by *pzStr
721** is deallocated via sqlite3_free().
722**
723** If the third argument, doFree, is true, then sqlite3_free() is
724** also called to free the buffer pointed to by zAppend.
725*/
danielk1977a1644fd2007-08-29 12:31:25 +0000726static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
danielk1977cc013f82006-06-24 06:36:11 +0000727 char *zIn = *pzStr;
danielk1977a1644fd2007-08-29 12:31:25 +0000728 if( !zAppend && doFree && *pRc==SQLITE_OK ){
729 *pRc = SQLITE_NOMEM;
730 }
731 if( *pRc!=SQLITE_OK ){
732 sqlite3_free(zIn);
733 zIn = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000734 }else{
danielk1977a1644fd2007-08-29 12:31:25 +0000735 if( zIn ){
736 char *zTemp = zIn;
737 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
738 sqlite3_free(zTemp);
739 }else{
740 zIn = sqlite3_mprintf("%s", zAppend);
741 }
742 if( !zIn ){
743 *pRc = SQLITE_NOMEM;
744 }
danielk1977cc013f82006-06-24 06:36:11 +0000745 }
746 *pzStr = zIn;
747 if( doFree ){
748 sqlite3_free(zAppend);
749 }
750}
751
drhb9bb7c12006-06-11 23:41:55 +0000752/*
dan1acb5392015-11-26 19:33:41 +0000753** This function returns a pointer to an sqlite3_malloc()ed buffer
754** containing the select-list (the thing between keywords SELECT and FROM)
755** to query the underlying real table with for the scan described by
756** argument pIdxInfo.
757**
758** If the current SQLite version is earlier than 3.10.0, this is just "*"
759** (select all columns). Or, for version 3.10.0 and greater, the list of
760** columns identified by the pIdxInfo->colUsed mask.
761*/
762static char *echoSelectList(echo_vtab *pTab, sqlite3_index_info *pIdxInfo){
763 char *zRet = 0;
764 if( sqlite3_libversion_number()<3010000 ){
765 zRet = sqlite3_mprintf(", *");
766 }else{
767 int i;
768 for(i=0; i<pTab->nCol; i++){
769 if( pIdxInfo->colUsed & ((sqlite3_uint64)1 << (i>=63 ? 63 : i)) ){
770 zRet = sqlite3_mprintf("%z, %s", zRet, pTab->aCol[i]);
771 }else{
772 zRet = sqlite3_mprintf("%z, NULL", zRet);
773 }
774 if( !zRet ) break;
775 }
776 }
777 return zRet;
778}
779
780/*
danielk19775fac9f82006-06-13 14:16:58 +0000781** The echo module implements the subset of query constraints and sort
782** orders that may take advantage of SQLite indices on the underlying
783** real table. For example, if the real table is declared as:
784**
785** CREATE TABLE real(a, b, c);
786** CREATE INDEX real_index ON real(b);
787**
788** then the echo module handles WHERE or ORDER BY clauses that refer
789** to the column "b", but not "a" or "c". If a multi-column index is
drh85b623f2007-12-13 21:54:09 +0000790** present, only its left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000791**
792** This xBestIndex method encodes the proposed search strategy as
793** an SQL query on the real table underlying the virtual echo module
794** table and stores the query in sqlite3_index_info.idxStr. The SQL
795** statement is of the form:
796**
797** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
798**
799** where the <where-clause> and <order-by-clause> are determined
800** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000801*/
danielk19775fac9f82006-06-13 14:16:58 +0000802static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
803 int ii;
drh4be8b512006-06-13 23:51:34 +0000804 char *zQuery = 0;
dan1acb5392015-11-26 19:33:41 +0000805 char *zCol = 0;
drh4be8b512006-06-13 23:51:34 +0000806 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000807 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000808 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000809 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000810 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000811 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000812
mistachkin27b2f052015-01-12 19:49:46 +0000813 int nRow = 0;
danielk197774cdba42006-06-19 12:02:58 +0000814 int useIdx = 0;
815 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000816 int useCost = 0;
mistachkin27b2f052015-01-12 19:49:46 +0000817 double cost = 0;
danielk197739359dc2008-03-17 09:36:44 +0000818 int isIgnoreUsable = 0;
819 if( Tcl_GetVar(interp, "echo_module_ignore_usable", TCL_GLOBAL_ONLY) ){
820 isIgnoreUsable = 1;
821 }
822
danielk19773e3a84d2008-08-01 17:37:40 +0000823 if( simulateVtabError(pVtab, "xBestIndex") ){
824 return SQLITE_ERROR;
825 }
826
danielk197774cdba42006-06-19 12:02:58 +0000827 /* Determine the number of rows in the table and store this value in local
828 ** variable nRow. The 'estimated-cost' of the scan will be the number of
829 ** rows in the table for a linear scan, or the log (base 2) of the
830 ** number of rows if the proposed scan uses an index.
831 */
danielk1977780b1d92007-03-30 14:56:34 +0000832 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
833 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
834 useCost = 1;
835 } else {
836 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000837 if( !zQuery ){
838 return SQLITE_NOMEM;
839 }
danielk1977780b1d92007-03-30 14:56:34 +0000840 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
841 sqlite3_free(zQuery);
842 if( rc!=SQLITE_OK ){
843 return rc;
844 }
845 sqlite3_step(pStmt);
846 nRow = sqlite3_column_int(pStmt, 0);
847 rc = sqlite3_finalize(pStmt);
848 if( rc!=SQLITE_OK ){
849 return rc;
850 }
danielk197774cdba42006-06-19 12:02:58 +0000851 }
danielk19775fac9f82006-06-13 14:16:58 +0000852
dan1acb5392015-11-26 19:33:41 +0000853 zCol = echoSelectList(pVtab, pIdxInfo);
854 if( !zCol ) return SQLITE_NOMEM;
855 zQuery = sqlite3_mprintf("SELECT rowid%z FROM %Q", zCol, pVtab->zTableName);
856 if( !zQuery ) return SQLITE_NOMEM;
857
danielk19775fac9f82006-06-13 14:16:58 +0000858 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
859 const struct sqlite3_index_constraint *pConstraint;
860 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000861 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000862
863 pConstraint = &pIdxInfo->aConstraint[ii];
864 pUsage = &pIdxInfo->aConstraintUsage[ii];
865
danielk197739359dc2008-03-17 09:36:44 +0000866 if( !isIgnoreUsable && !pConstraint->usable ) continue;
867
drh383736b2006-10-08 18:56:57 +0000868 iCol = pConstraint->iColumn;
drh82ebc2a2012-03-20 03:10:51 +0000869 if( iCol<0 || pVtab->aIndex[iCol] ){
mistachkin8ccdef62015-12-16 22:06:52 +0000870 char *zNewCol = iCol>=0 ? pVtab->aCol[iCol] : "rowid";
danielk19775fac9f82006-06-13 14:16:58 +0000871 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000872 useIdx = 1;
danielk19775fac9f82006-06-13 14:16:58 +0000873 switch( pConstraint->op ){
874 case SQLITE_INDEX_CONSTRAINT_EQ:
875 zOp = "="; break;
876 case SQLITE_INDEX_CONSTRAINT_LT:
877 zOp = "<"; break;
878 case SQLITE_INDEX_CONSTRAINT_GT:
879 zOp = ">"; break;
880 case SQLITE_INDEX_CONSTRAINT_LE:
881 zOp = "<="; break;
882 case SQLITE_INDEX_CONSTRAINT_GE:
883 zOp = ">="; break;
884 case SQLITE_INDEX_CONSTRAINT_MATCH:
mistachkina9124d32015-11-24 01:17:01 +0000885 /* Purposely translate the MATCH operator into a LIKE, which
886 ** will be used by the next block of code to construct a new
887 ** query. It should also be noted here that the next block
888 ** of code requires the first letter of this operator to be
889 ** in upper-case to trigger the special MATCH handling (i.e.
890 ** wrapping the bound parameter with literal '%'s).
891 */
drh1a90e092006-06-14 22:07:10 +0000892 zOp = "LIKE"; break;
dan07bdba82015-11-23 21:09:54 +0000893 case SQLITE_INDEX_CONSTRAINT_LIKE:
894 zOp = "like"; break;
895 case SQLITE_INDEX_CONSTRAINT_GLOB:
896 zOp = "glob"; break;
897 case SQLITE_INDEX_CONSTRAINT_REGEXP:
898 zOp = "regexp"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000899 }
drh1a90e092006-06-14 22:07:10 +0000900 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000901 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
mistachkin8ccdef62015-12-16 22:06:52 +0000902 zSep, zNewCol);
drh1a90e092006-06-14 22:07:10 +0000903 } else {
mistachkin8ccdef62015-12-16 22:06:52 +0000904 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zNewCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000905 }
danielk1977a1644fd2007-08-29 12:31:25 +0000906 string_concat(&zQuery, zNew, 1, &rc);
danielk1977cc013f82006-06-24 06:36:11 +0000907
drh4be8b512006-06-13 23:51:34 +0000908 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000909 pUsage->argvIndex = ++nArg;
910 pUsage->omit = 1;
911 }
912 }
danielk197747d08092006-06-14 07:41:31 +0000913
914 /* If there is only one term in the ORDER BY clause, and it is
915 ** on a column that this virtual table has an index for, then consume
916 ** the ORDER BY clause.
917 */
drh82ebc2a2012-03-20 03:10:51 +0000918 if( pIdxInfo->nOrderBy==1 && (
919 pIdxInfo->aOrderBy->iColumn<0 ||
920 pVtab->aIndex[pIdxInfo->aOrderBy->iColumn]) ){
danielk197765fd59f2006-06-24 11:51:33 +0000921 int iCol = pIdxInfo->aOrderBy->iColumn;
mistachkin8ccdef62015-12-16 22:06:52 +0000922 char *zNewCol = iCol>=0 ? pVtab->aCol[iCol] : "rowid";
danielk197747d08092006-06-14 07:41:31 +0000923 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
mistachkin8ccdef62015-12-16 22:06:52 +0000924 zNew = sqlite3_mprintf(" ORDER BY %s %s", zNewCol, zDir);
danielk1977a1644fd2007-08-29 12:31:25 +0000925 string_concat(&zQuery, zNew, 1, &rc);
danielk197747d08092006-06-14 07:41:31 +0000926 pIdxInfo->orderByConsumed = 1;
927 }
928
danielk19775fac9f82006-06-13 14:16:58 +0000929 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000930 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000931
danielk1977222a7572007-08-25 13:37:48 +0000932 if( !zQuery ){
danielk1977a1644fd2007-08-29 12:31:25 +0000933 return rc;
danielk1977222a7572007-08-25 13:37:48 +0000934 }
danielk197798331532006-06-14 10:55:52 +0000935 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000936 pIdxInfo->idxStr = zQuery;
937 pIdxInfo->needToFreeIdxStr = 1;
danielk19771d461462009-04-21 09:02:45 +0000938 if( useCost ){
danielk1977780b1d92007-03-30 14:56:34 +0000939 pIdxInfo->estimatedCost = cost;
danielk19771d461462009-04-21 09:02:45 +0000940 }else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000941 /* Approximation of log2(nRow). */
drh693e6712014-01-24 22:58:00 +0000942 for( ii=0; ii<(sizeof(int)*8)-1; ii++ ){
danielk197774cdba42006-06-19 12:02:58 +0000943 if( nRow & (1<<ii) ){
944 pIdxInfo->estimatedCost = (double)ii;
945 }
946 }
danielk19771d461462009-04-21 09:02:45 +0000947 }else{
danielk197774cdba42006-06-19 12:02:58 +0000948 pIdxInfo->estimatedCost = (double)nRow;
949 }
950 return rc;
drha967e882006-06-13 01:04:52 +0000951}
952
danielk1977176f4d22006-06-15 10:41:15 +0000953/*
danielk1977cc013f82006-06-24 06:36:11 +0000954** The xUpdate method for echo module virtual tables.
955**
danielk1977176f4d22006-06-15 10:41:15 +0000956** apData[0] apData[1] apData[2..]
957**
958** INTEGER DELETE
959**
960** INTEGER NULL (nCol args) UPDATE (do not set rowid)
961** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
962**
963** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
964** NULL INTEGER (nCol args) INSERT (incl. rowid value)
965**
966*/
danielk19771f6eec52006-06-16 06:17:47 +0000967int echoUpdate(
968 sqlite3_vtab *tab,
969 int nData,
970 sqlite3_value **apData,
971 sqlite_int64 *pRowid
972){
danielk197726e41442006-06-14 15:16:35 +0000973 echo_vtab *pVtab = (echo_vtab *)tab;
974 sqlite3 *db = pVtab->db;
975 int rc = SQLITE_OK;
976
mistachkin27b2f052015-01-12 19:49:46 +0000977 sqlite3_stmt *pStmt = 0;
danielk1977176f4d22006-06-15 10:41:15 +0000978 char *z = 0; /* SQL statement to execute */
979 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
980 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
981 int i; /* Counter variable used by for loops */
982
danielk197726e41442006-06-14 15:16:35 +0000983 assert( nData==pVtab->nCol+2 || nData==1 );
984
drhcd3dd9d2008-04-28 20:27:53 +0000985 /* Ticket #3083 - make sure we always start a transaction prior to
986 ** making any changes to a virtual table */
987 assert( pVtab->inTransaction );
988
danielk19773e3a84d2008-08-01 17:37:40 +0000989 if( simulateVtabError(pVtab, "xUpdate") ){
990 return SQLITE_ERROR;
991 }
992
drh5aec0422006-06-14 23:43:31 +0000993 /* If apData[0] is an integer and nData>1 then do an UPDATE */
994 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000995 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000996 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000997 if( !z ){
998 rc = SQLITE_NOMEM;
999 }
danielk1977176f4d22006-06-15 10:41:15 +00001000
1001 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
1002 bindArgZero = 1;
1003
1004 if( bindArgOne ){
danielk1977a1644fd2007-08-29 12:31:25 +00001005 string_concat(&z, " SET rowid=?1 ", 0, &rc);
drh5aec0422006-06-14 23:43:31 +00001006 zSep = ",";
1007 }
1008 for(i=2; i<nData; i++){
1009 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +00001010 string_concat(&z, sqlite3_mprintf(
danielk1977a1644fd2007-08-29 12:31:25 +00001011 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
drh5aec0422006-06-14 23:43:31 +00001012 zSep = ",";
1013 }
danielk1977a1644fd2007-08-29 12:31:25 +00001014 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
drh5aec0422006-06-14 23:43:31 +00001015 }
1016
danielk1977176f4d22006-06-15 10:41:15 +00001017 /* If apData[0] is an integer and nData==1 then do a DELETE */
1018 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
1019 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +00001020 if( !z ){
1021 rc = SQLITE_NOMEM;
1022 }
danielk1977176f4d22006-06-15 10:41:15 +00001023 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +00001024 }
1025
danielk1977176f4d22006-06-15 10:41:15 +00001026 /* If the first argument is NULL and there are more than two args, INSERT */
1027 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +00001028 int ii;
1029 char *zInsert = 0;
1030 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +00001031
danielk19774b2688a2006-06-20 11:01:07 +00001032 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +00001033 if( !zInsert ){
1034 rc = SQLITE_NOMEM;
1035 }
danielk1977176f4d22006-06-15 10:41:15 +00001036 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
1037 bindArgOne = 1;
1038 zValues = sqlite3_mprintf("?");
danielk1977a1644fd2007-08-29 12:31:25 +00001039 string_concat(&zInsert, "rowid", 0, &rc);
danielk197726e41442006-06-14 15:16:35 +00001040 }
1041
danielk1977176f4d22006-06-15 10:41:15 +00001042 assert((pVtab->nCol+2)==nData);
1043 for(ii=2; ii<nData; ii++){
1044 string_concat(&zInsert,
danielk1977a1644fd2007-08-29 12:31:25 +00001045 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
danielk1977176f4d22006-06-15 10:41:15 +00001046 string_concat(&zValues,
danielk1977a1644fd2007-08-29 12:31:25 +00001047 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
danielk197726e41442006-06-14 15:16:35 +00001048 }
1049
danielk1977a1644fd2007-08-29 12:31:25 +00001050 string_concat(&z, zInsert, 1, &rc);
1051 string_concat(&z, ") VALUES(", 0, &rc);
1052 string_concat(&z, zValues, 1, &rc);
1053 string_concat(&z, ")", 0, &rc);
danielk1977176f4d22006-06-15 10:41:15 +00001054 }
1055
1056 /* Anything else is an error */
1057 else{
1058 assert(0);
1059 return SQLITE_ERROR;
1060 }
1061
danielk1977a1644fd2007-08-29 12:31:25 +00001062 if( rc==SQLITE_OK ){
1063 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
1064 }
danielk1977176f4d22006-06-15 10:41:15 +00001065 assert( rc!=SQLITE_OK || pStmt );
1066 sqlite3_free(z);
1067 if( rc==SQLITE_OK ) {
1068 if( bindArgZero ){
1069 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +00001070 }
danielk1977176f4d22006-06-15 10:41:15 +00001071 if( bindArgOne ){
1072 sqlite3_bind_value(pStmt, 1, apData[1]);
1073 }
danielk1977a7a8e142008-02-13 18:25:27 +00001074 for(i=2; i<nData && rc==SQLITE_OK; i++){
1075 if( apData[i] ) rc = sqlite3_bind_value(pStmt, i, apData[i]);
danielk1977176f4d22006-06-15 10:41:15 +00001076 }
danielk1977a7a8e142008-02-13 18:25:27 +00001077 if( rc==SQLITE_OK ){
1078 sqlite3_step(pStmt);
1079 rc = sqlite3_finalize(pStmt);
1080 }else{
1081 sqlite3_finalize(pStmt);
1082 }
danielk197726e41442006-06-14 15:16:35 +00001083 }
1084
danielk19771f6eec52006-06-16 06:17:47 +00001085 if( pRowid && rc==SQLITE_OK ){
1086 *pRowid = sqlite3_last_insert_rowid(db);
1087 }
drh4dc754d2008-07-23 18:17:32 +00001088 if( rc!=SQLITE_OK ){
1089 tab->zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", sqlite3_errmsg(db));
1090 }
danielk19771f6eec52006-06-16 06:17:47 +00001091
danielk197726e41442006-06-14 15:16:35 +00001092 return rc;
1093}
1094
drha967e882006-06-13 01:04:52 +00001095/*
danielk1977c69cdfd2006-06-17 09:39:55 +00001096** xBegin, xSync, xCommit and xRollback callbacks for echo module
1097** virtual tables. Do nothing other than add the name of the callback
1098** to the $::echo_module Tcl variable.
1099*/
1100static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
1101 char *z;
1102 echo_vtab *pVtab = (echo_vtab *)tab;
1103 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
drh344c38e2008-05-05 13:23:04 +00001104 if( z==0 ) return SQLITE_NOMEM;
danielk1977c69cdfd2006-06-17 09:39:55 +00001105 appendToEchoModule(pVtab->interp, zCall);
1106 appendToEchoModule(pVtab->interp, z);
1107 sqlite3_free(z);
drh344c38e2008-05-05 13:23:04 +00001108 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +00001109}
1110static int echoBegin(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001111 int rc;
danielk19775017dc32006-06-24 09:34:22 +00001112 echo_vtab *pVtab = (echo_vtab *)tab;
1113 Tcl_Interp *interp = pVtab->interp;
1114 const char *zVal;
1115
drhcd3dd9d2008-04-28 20:27:53 +00001116 /* Ticket #3083 - do not start a transaction if we are already in
1117 ** a transaction */
1118 assert( !pVtab->inTransaction );
1119
danielk19773e3a84d2008-08-01 17:37:40 +00001120 if( simulateVtabError(pVtab, "xBegin") ){
1121 return SQLITE_ERROR;
1122 }
1123
danielk1977a1644fd2007-08-29 12:31:25 +00001124 rc = echoTransactionCall(tab, "xBegin");
danielk19775017dc32006-06-24 09:34:22 +00001125
danielk1977a1644fd2007-08-29 12:31:25 +00001126 if( rc==SQLITE_OK ){
1127 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
1128 ** and it is set to the name of the real table underlying this virtual
1129 ** echo module table, then cause this xSync operation to fail.
1130 */
1131 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
1132 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1133 rc = SQLITE_ERROR;
1134 }
danielk19775017dc32006-06-24 09:34:22 +00001135 }
drhcd3dd9d2008-04-28 20:27:53 +00001136 if( rc==SQLITE_OK ){
1137 pVtab->inTransaction = 1;
1138 }
danielk1977a1644fd2007-08-29 12:31:25 +00001139 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001140}
1141static int echoSync(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001142 int rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001143 echo_vtab *pVtab = (echo_vtab *)tab;
1144 Tcl_Interp *interp = pVtab->interp;
1145 const char *zVal;
1146
drhcd3dd9d2008-04-28 20:27:53 +00001147 /* Ticket #3083 - Only call xSync if we have previously started a
1148 ** transaction */
1149 assert( pVtab->inTransaction );
1150
danielk19773e3a84d2008-08-01 17:37:40 +00001151 if( simulateVtabError(pVtab, "xSync") ){
1152 return SQLITE_ERROR;
1153 }
1154
danielk1977a1644fd2007-08-29 12:31:25 +00001155 rc = echoTransactionCall(tab, "xSync");
danielk1977c69cdfd2006-06-17 09:39:55 +00001156
danielk1977a1644fd2007-08-29 12:31:25 +00001157 if( rc==SQLITE_OK ){
1158 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
1159 ** and it is set to the name of the real table underlying this virtual
1160 ** echo module table, then cause this xSync operation to fail.
1161 */
1162 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
1163 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1164 rc = -1;
1165 }
danielk1977c69cdfd2006-06-17 09:39:55 +00001166 }
danielk1977a1644fd2007-08-29 12:31:25 +00001167 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001168}
1169static int echoCommit(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:53 +00001170 echo_vtab *pVtab = (echo_vtab*)tab;
drh643167f2008-01-22 21:30:53 +00001171 int rc;
drhcd3dd9d2008-04-28 20:27:53 +00001172
1173 /* Ticket #3083 - Only call xCommit if we have previously started
1174 ** a transaction */
1175 assert( pVtab->inTransaction );
1176
danielk19773e3a84d2008-08-01 17:37:40 +00001177 if( simulateVtabError(pVtab, "xCommit") ){
1178 return SQLITE_ERROR;
1179 }
1180
danielk19772d1d86f2008-06-20 14:59:51 +00001181 sqlite3BeginBenignMalloc();
drh643167f2008-01-22 21:30:53 +00001182 rc = echoTransactionCall(tab, "xCommit");
danielk19772d1d86f2008-06-20 14:59:51 +00001183 sqlite3EndBenignMalloc();
drh344c38e2008-05-05 13:23:04 +00001184 pVtab->inTransaction = 0;
drh643167f2008-01-22 21:30:53 +00001185 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001186}
1187static int echoRollback(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:53 +00001188 int rc;
1189 echo_vtab *pVtab = (echo_vtab*)tab;
1190
1191 /* Ticket #3083 - Only call xRollback if we have previously started
1192 ** a transaction */
1193 assert( pVtab->inTransaction );
1194
1195 rc = echoTransactionCall(tab, "xRollback");
1196 pVtab->inTransaction = 0;
1197 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001198}
1199
1200/*
drhe94b0c32006-07-08 18:09:15 +00001201** Implementation of "GLOB" function on the echo module. Pass
1202** all arguments to the ::echo_glob_overload procedure of TCL
1203** and return the result of that procedure as a string.
1204*/
1205static void overloadedGlobFunction(
1206 sqlite3_context *pContext,
1207 int nArg,
1208 sqlite3_value **apArg
1209){
1210 Tcl_Interp *interp = sqlite3_user_data(pContext);
1211 Tcl_DString str;
1212 int i;
1213 int rc;
1214 Tcl_DStringInit(&str);
1215 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
1216 for(i=0; i<nArg; i++){
1217 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
1218 }
1219 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
1220 Tcl_DStringFree(&str);
1221 if( rc ){
1222 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
1223 }else{
1224 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1225 -1, SQLITE_TRANSIENT);
1226 }
1227 Tcl_ResetResult(interp);
1228}
1229
1230/*
1231** This is the xFindFunction implementation for the echo module.
1232** SQLite calls this routine when the first argument of a function
1233** is a column of an echo virtual table. This routine can optionally
1234** override the implementation of that function. It will choose to
1235** do so if the function is named "glob", and a TCL command named
1236** ::echo_glob_overload exists.
1237*/
1238static int echoFindFunction(
1239 sqlite3_vtab *vtab,
1240 int nArg,
1241 const char *zFuncName,
1242 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1243 void **ppArg
1244){
1245 echo_vtab *pVtab = (echo_vtab *)vtab;
1246 Tcl_Interp *interp = pVtab->interp;
1247 Tcl_CmdInfo info;
1248 if( strcmp(zFuncName,"glob")!=0 ){
1249 return 0;
1250 }
1251 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1252 return 0;
1253 }
1254 *pxFunc = overloadedGlobFunction;
1255 *ppArg = interp;
1256 return 1;
1257}
1258
danielk1977182c4ba2007-06-27 15:53:34 +00001259static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1260 int rc = SQLITE_OK;
1261 echo_vtab *p = (echo_vtab *)vtab;
1262
danielk1977987a00e2008-08-01 17:51:47 +00001263 if( simulateVtabError(p, "xRename") ){
1264 return SQLITE_ERROR;
1265 }
1266
danielk1977182c4ba2007-06-27 15:53:34 +00001267 if( p->isPattern ){
drh83cc1392012-04-19 18:04:28 +00001268 int nThis = (int)strlen(p->zThis);
drhbc6160b2009-04-08 15:45:31 +00001269 char *zSql = sqlite3_mprintf("ALTER TABLE %s RENAME TO %s%s",
danielk1977182c4ba2007-06-27 15:53:34 +00001270 p->zTableName, zNewName, &p->zTableName[nThis]
1271 );
1272 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +00001273 sqlite3_free(zSql);
danielk1977182c4ba2007-06-27 15:53:34 +00001274 }
1275
1276 return rc;
1277}
1278
dan3b504df2011-10-29 15:29:43 +00001279static int echoSavepoint(sqlite3_vtab *pVTab, int iSavepoint){
1280 assert( pVTab );
1281 return SQLITE_OK;
1282}
1283
1284static int echoRelease(sqlite3_vtab *pVTab, int iSavepoint){
1285 assert( pVTab );
1286 return SQLITE_OK;
1287}
1288
1289static int echoRollbackTo(sqlite3_vtab *pVTab, int iSavepoint){
1290 assert( pVTab );
1291 return SQLITE_OK;
1292}
1293
drhe94b0c32006-07-08 18:09:15 +00001294/*
danielk1977cc013f82006-06-24 06:36:11 +00001295** A virtual table module that merely "echos" the contents of another
1296** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +00001297*/
1298static sqlite3_module echoModule = {
dan3b504df2011-10-29 15:29:43 +00001299 1, /* iVersion */
1300 echoCreate,
1301 echoConnect,
1302 echoBestIndex,
1303 echoDisconnect,
1304 echoDestroy,
1305 echoOpen, /* xOpen - open a cursor */
1306 echoClose, /* xClose - close a cursor */
1307 echoFilter, /* xFilter - configure scan constraints */
1308 echoNext, /* xNext - advance a cursor */
1309 echoEof, /* xEof */
1310 echoColumn, /* xColumn - read data */
1311 echoRowid, /* xRowid - read data */
1312 echoUpdate, /* xUpdate - write data */
1313 echoBegin, /* xBegin - begin transaction */
1314 echoSync, /* xSync - sync transaction */
1315 echoCommit, /* xCommit - commit transaction */
1316 echoRollback, /* xRollback - rollback transaction */
1317 echoFindFunction, /* xFindFunction - function overloading */
1318 echoRename /* xRename - rename the table */
1319};
1320
1321static sqlite3_module echoModuleV2 = {
1322 2, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +00001323 echoCreate,
1324 echoConnect,
drha967e882006-06-13 01:04:52 +00001325 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +00001326 echoDisconnect,
1327 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +00001328 echoOpen, /* xOpen - open a cursor */
1329 echoClose, /* xClose - close a cursor */
1330 echoFilter, /* xFilter - configure scan constraints */
1331 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +00001332 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +00001333 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +00001334 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +00001335 echoUpdate, /* xUpdate - write data */
1336 echoBegin, /* xBegin - begin transaction */
1337 echoSync, /* xSync - sync transaction */
1338 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +00001339 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +00001340 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:34 +00001341 echoRename, /* xRename - rename the table */
dan3b504df2011-10-29 15:29:43 +00001342 echoSavepoint,
1343 echoRelease,
1344 echoRollbackTo
drhb9bb7c12006-06-11 23:41:55 +00001345};
1346
1347/*
1348** Decode a pointer to an sqlite3 object.
1349*/
drh24b58dd2008-07-07 14:50:14 +00001350extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
mistachkine84d8d32013-04-29 03:09:10 +00001351extern const char *sqlite3ErrName(int);
drhb9bb7c12006-06-11 23:41:55 +00001352
danielk1977860b6cd2007-09-03 11:51:50 +00001353static void moduleDestroy(void *p){
1354 sqlite3_free(p);
1355}
1356
drhb9bb7c12006-06-11 23:41:55 +00001357/*
1358** Register the echo virtual table module.
1359*/
mistachkin7617e4a2016-07-28 17:11:20 +00001360static int SQLITE_TCLAPI register_echo_module(
drhb9bb7c12006-06-11 23:41:55 +00001361 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1362 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1363 int objc, /* Number of arguments */
1364 Tcl_Obj *CONST objv[] /* Command arguments */
1365){
danca8b9ba2012-05-16 14:29:11 +00001366 int rc;
drhb9bb7c12006-06-11 23:41:55 +00001367 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:50 +00001368 EchoModule *pMod;
drhb9bb7c12006-06-11 23:41:55 +00001369 if( objc!=2 ){
1370 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1371 return TCL_ERROR;
1372 }
1373 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
dan3b504df2011-10-29 15:29:43 +00001374
1375 /* Virtual table module "echo" */
danielk1977860b6cd2007-09-03 11:51:50 +00001376 pMod = sqlite3_malloc(sizeof(EchoModule));
1377 pMod->interp = interp;
danca8b9ba2012-05-16 14:29:11 +00001378 rc = sqlite3_create_module_v2(
1379 db, "echo", &echoModule, (void*)pMod, moduleDestroy
1380 );
dan3b504df2011-10-29 15:29:43 +00001381
1382 /* Virtual table module "echo_v2" */
danca8b9ba2012-05-16 14:29:11 +00001383 if( rc==SQLITE_OK ){
1384 pMod = sqlite3_malloc(sizeof(EchoModule));
1385 pMod->interp = interp;
1386 rc = sqlite3_create_module_v2(db, "echo_v2",
1387 &echoModuleV2, (void*)pMod, moduleDestroy
1388 );
1389 }
1390
mistachkine84d8d32013-04-29 03:09:10 +00001391 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
drhb9bb7c12006-06-11 23:41:55 +00001392 return TCL_OK;
1393}
1394
danielk19775017dc32006-06-24 09:34:22 +00001395/*
1396** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1397**
1398** sqlite3_declare_vtab DB SQL
1399*/
mistachkin7617e4a2016-07-28 17:11:20 +00001400static int SQLITE_TCLAPI declare_vtab(
danielk19775017dc32006-06-24 09:34:22 +00001401 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1402 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1403 int objc, /* Number of arguments */
1404 Tcl_Obj *CONST objv[] /* Command arguments */
1405){
1406 sqlite3 *db;
1407 int rc;
1408 if( objc!=3 ){
1409 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1410 return TCL_ERROR;
1411 }
1412 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1413 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1414 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001415 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001416 return TCL_ERROR;
1417 }
1418 return TCL_OK;
1419}
1420
danielk1977cc013f82006-06-24 06:36:11 +00001421#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001422
1423/*
1424** Register commands with the TCL interpreter.
1425*/
1426int Sqlitetest8_Init(Tcl_Interp *interp){
danielk19776e891622008-08-12 14:48:40 +00001427#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001428 static struct {
1429 char *zName;
1430 Tcl_ObjCmdProc *xProc;
1431 void *clientData;
1432 } aObjCmd[] = {
dan2deb1652012-07-13 16:15:20 +00001433 { "register_echo_module", register_echo_module, 0 },
dan2deb1652012-07-13 16:15:20 +00001434 { "sqlite3_declare_vtab", declare_vtab, 0 },
drhb9bb7c12006-06-11 23:41:55 +00001435 };
1436 int i;
1437 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1438 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1439 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1440 }
danielk19776e891622008-08-12 14:48:40 +00001441#endif
drhb9bb7c12006-06-11 23:41:55 +00001442 return TCL_OK;
1443}