Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 8a13f5d55613d745d95ebe10128cf0ae154ccfff [file] [log] [blame]
drhb9bb7c12006-06-11 23:41:551/*
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:5515*/
16#include "sqliteInt.h"
drh064b6812024-07-30 15:49:0217#include "tclsqlite.h"
drhb9bb7c12006-06-11 23:41:5518#include <stdlib.h>
19#include <string.h>
20
danielk1977cc013f82006-06-24 06:36:1121#ifndef SQLITE_OMIT_VIRTUALTABLE
22
danielk1977b7a7b9a2006-06-13 10:24:4223typedef struct echo_vtab echo_vtab;
24typedef struct echo_cursor echo_cursor;
25
danielk1977c69cdfd2006-06-17 09:39:5526/*
danielk1977780b1d92007-03-30 14:56:3427** The test module defined in this file uses four global Tcl variables to
larrybrbc917382023-06-07 08:40:3128** communicate with test-scripts:
danielk1977c69cdfd2006-06-17 09:39:5529**
30** $::echo_module
31** $::echo_module_sync_fail
danielk19775017dc32006-06-24 09:34:2232** $::echo_module_begin_fail
danielk1977780b1d92007-03-30 14:56:3433** $::echo_module_cost
danielk1977cc013f82006-06-24 06:36:1134**
35** The variable ::echo_module is a list. Each time one of the following
36** methods is called, one or more elements are appended to the list.
37** This is used for automated testing of virtual table modules.
38**
39** The ::echo_module_sync_fail variable is set by test scripts and read
40** by code in this file. If it is set to the name of a real table in the
41** the database, then all xSync operations on echo virtual tables that
42** use the named table as a backing store will fail.
danielk1977c69cdfd2006-06-17 09:39:5543*/
44
danielk19773e3a84d2008-08-01 17:37:4045/*
46** Errors can be provoked within the following echo virtual table methods:
47**
48** xBestIndex xOpen xFilter xNext
49** xColumn xRowid xUpdate xSync
danielk1977987a00e2008-08-01 17:51:4750** xBegin xRename
danielk19773e3a84d2008-08-01 17:37:4051**
52** This is done by setting the global tcl variable:
53**
54** echo_module_fail($method,$tbl)
55**
56** where $method is set to the name of the virtual table method to fail
57** (i.e. "xBestIndex") and $tbl is the name of the table being echoed (not
58** the name of the virtual table, the name of the underlying real table).
59*/
60
danielk19775fac9f82006-06-13 14:16:5861/*
danielk1977d6e8dd02006-06-15 15:38:4162** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:5863**
danielk1977d6e8dd02006-06-15 15:38:4164** echo.vtab.aIndex is an array of booleans. The nth entry is true if
65** the nth column of the real table is the left-most column of an index
66** (implicit or otherwise). In other words, if SQLite can optimize
67** a query like "SELECT * FROM real_table WHERE col = ?".
68**
danielk1977c69cdfd2006-06-17 09:39:5569** Member variable aCol[] contains copies of the column names of the real
70** table.
danielk19775fac9f82006-06-13 14:16:5871*/
danielk1977b7a7b9a2006-06-13 10:24:4272struct echo_vtab {
73 sqlite3_vtab base;
danielk1977cc013f82006-06-24 06:36:1174 Tcl_Interp *interp; /* Tcl interpreter containing debug variables */
75 sqlite3 *db; /* Database connection */
danielk19775fac9f82006-06-13 14:16:5876
danielk1977182c4ba2007-06-27 15:53:3477 int isPattern;
drhcd3dd9d2008-04-28 20:27:5378 int inTransaction; /* True if within a transaction */
danielk1977182c4ba2007-06-27 15:53:3479 char *zThis; /* Name of the echo table */
danielk1977a4e76362006-06-14 06:31:2880 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:4881 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:2882 int nCol; /* Number of columns in the real table */
83 int *aIndex; /* Array of size nCol. True if column has an index */
84 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:4285};
86
87/* An echo cursor object */
88struct echo_cursor {
89 sqlite3_vtab_cursor base;
90 sqlite3_stmt *pStmt;
danielk1977b7a7b9a2006-06-13 10:24:4291};
92
danielk19773e3a84d2008-08-01 17:37:4093static int simulateVtabError(echo_vtab *p, const char *zMethod){
94 const char *zErr;
95 char zVarname[128];
96 zVarname[127] = '\0';
shanef5eccb62008-08-31 00:29:0897 sqlite3_snprintf(127, zVarname, "echo_module_fail(%s,%s)", zMethod, p->zTableName);
danielk19773e3a84d2008-08-01 17:37:4098 zErr = Tcl_GetVar(p->interp, zVarname, TCL_GLOBAL_ONLY);
99 if( zErr ){
100 p->base.zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", zErr);
101 }
102 return (zErr!=0);
103}
104
danielk1977cc013f82006-06-24 06:36:11105/*
danielk1977182c4ba2007-06-27 15:53:34106** Convert an SQL-style quoted string into a normal string by removing
107** the quote characters. The conversion is done in-place. If the
108** input does not begin with a quote character, then this routine
109** is a no-op.
110**
111** Examples:
112**
113** "abc" becomes abc
114** 'xyz' becomes xyz
115** [pqr] becomes pqr
116** `mno` becomes mno
117*/
118static void dequoteString(char *z){
119 int quote;
120 int i, j;
121 if( z==0 ) return;
122 quote = z[0];
123 switch( quote ){
124 case '\'': break;
125 case '"': break;
126 case '`': break; /* For MySQL compatibility */
127 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
128 default: return;
129 }
130 for(i=1, j=0; z[i]; i++){
131 if( z[i]==quote ){
132 if( z[i+1]==quote ){
133 z[j++] = quote;
134 i++;
135 }else{
136 z[j++] = 0;
137 break;
138 }
139 }else{
140 z[j++] = z[i];
141 }
142 }
143}
144
145/*
danielk1977cc013f82006-06-24 06:36:11146** Retrieve the column names for the table named zTab via database
147** connection db. SQLITE_OK is returned on success, or an sqlite error
148** code otherwise.
149**
150** If successful, the number of columns is written to *pnCol. *paCol is
drh17435752007-08-16 04:30:38151** set to point at sqlite3_malloc()'d space containing the array of
152** nCol column names. The caller is responsible for calling sqlite3_free
danielk1977cc013f82006-06-24 06:36:11153** on *paCol.
154*/
danielk19775fac9f82006-06-13 14:16:58155static int getColumnNames(
156 sqlite3 *db,
157 const char *zTab,
158 char ***paCol,
159 int *pnCol
160){
161 char **aCol = 0;
danielk1977cc013f82006-06-24 06:36:11162 char *zSql;
danielk19775fac9f82006-06-13 14:16:58163 sqlite3_stmt *pStmt = 0;
164 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19165 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58166
danielk1977cc013f82006-06-24 06:36:11167 /* Prepare the statement "SELECT * FROM <tbl>". The column names
168 ** of the result set of the compiled SELECT will be the same as
169 ** the column names of table <tbl>.
170 */
drh17435752007-08-16 04:30:38171 zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab);
danielk1977cc013f82006-06-24 06:36:11172 if( !zSql ){
173 rc = SQLITE_NOMEM;
174 goto out;
175 }
176 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38177 sqlite3_free(zSql);
danielk1977cc013f82006-06-24 06:36:11178
danielk19775fac9f82006-06-13 14:16:58179 if( rc==SQLITE_OK ){
180 int ii;
danielk1977cc013f82006-06-24 06:36:11181 int nBytes;
182 char *zSpace;
danielk19775fac9f82006-06-13 14:16:58183 nCol = sqlite3_column_count(pStmt);
danielk1977cc013f82006-06-24 06:36:11184
185 /* Figure out how much space to allocate for the array of column names
186 ** (including space for the strings themselves). Then allocate it.
187 */
188 nBytes = sizeof(char *) * nCol;
189 for(ii=0; ii<nCol; ii++){
danielk1977a7a8e142008-02-13 18:25:27190 const char *zName = sqlite3_column_name(pStmt, ii);
191 if( !zName ){
192 rc = SQLITE_NOMEM;
193 goto out;
194 }
drh83cc1392012-04-19 18:04:28195 nBytes += (int)strlen(zName)+1;
danielk1977cc013f82006-06-24 06:36:11196 }
danielk197731dad9d2007-08-16 11:36:15197 aCol = (char **)sqlite3MallocZero(nBytes);
danielk19775fac9f82006-06-13 14:16:58198 if( !aCol ){
199 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11200 goto out;
danielk19775fac9f82006-06-13 14:16:58201 }
danielk1977cc013f82006-06-24 06:36:11202
203 /* Copy the column names into the allocated space and set up the
204 ** pointers in the aCol[] array.
205 */
206 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58207 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11208 aCol[ii] = zSpace;
drh65545b52015-01-19 00:35:53209 sqlite3_snprintf(nBytes, zSpace, "%s", sqlite3_column_name(pStmt,ii));
210 zSpace += (int)strlen(zSpace) + 1;
danielk19775fac9f82006-06-13 14:16:58211 }
danielk1977cc013f82006-06-24 06:36:11212 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58213 }
214
215 *paCol = aCol;
216 *pnCol = nCol;
217
danielk1977cc013f82006-06-24 06:36:11218out:
danielk19775fac9f82006-06-13 14:16:58219 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58220 return rc;
221}
222
danielk1977cc013f82006-06-24 06:36:11223/*
224** Parameter zTab is the name of a table in database db with nCol
225** columns. This function allocates an array of integers nCol in
226** size and populates it according to any implicit or explicit
227** indices on table zTab.
228**
229** If successful, SQLITE_OK is returned and *paIndex set to point
230** at the allocated array. Otherwise, an error code is returned.
231**
232** See comments associated with the member variable aIndex above
233** "struct echo_vtab" for details of the contents of the array.
234*/
235static int getIndexArray(
236 sqlite3 *db, /* Database connection */
237 const char *zTab, /* Name of table in database db */
238 int nCol,
239 int **paIndex
240){
danielk19775fac9f82006-06-13 14:16:58241 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58242 int *aIndex = 0;
243 int rc;
danielk1977cc013f82006-06-24 06:36:11244 char *zSql;
danielk19775fac9f82006-06-13 14:16:58245
danielk1977cc013f82006-06-24 06:36:11246 /* Allocate space for the index array */
danielk197731dad9d2007-08-16 11:36:15247 aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
danielk19775fac9f82006-06-13 14:16:58248 if( !aIndex ){
249 rc = SQLITE_NOMEM;
250 goto get_index_array_out;
251 }
252
danielk1977cc013f82006-06-24 06:36:11253 /* Compile an sqlite pragma to loop through all indices on table zTab */
drhbc6160b2009-04-08 15:45:31254 zSql = sqlite3_mprintf("PRAGMA index_list(%s)", zTab);
danielk1977cc013f82006-06-24 06:36:11255 if( !zSql ){
256 rc = SQLITE_NOMEM;
257 goto get_index_array_out;
258 }
259 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38260 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58261
danielk1977cc013f82006-06-24 06:36:11262 /* For each index, figure out the left-most column and set the
263 ** corresponding entry in aIndex[] to 1.
264 */
danielk19775fac9f82006-06-13 14:16:58265 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33266 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58267 sqlite3_stmt *pStmt2 = 0;
drh6b169bd2013-10-05 20:18:25268 if( zIdx==0 ) continue;
drhbc6160b2009-04-08 15:45:31269 zSql = sqlite3_mprintf("PRAGMA index_info(%s)", zIdx);
danielk1977cc013f82006-06-24 06:36:11270 if( !zSql ){
271 rc = SQLITE_NOMEM;
272 goto get_index_array_out;
273 }
274 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
drh17435752007-08-16 04:30:38275 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58276 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
277 int cid = sqlite3_column_int(pStmt2, 1);
278 assert( cid>=0 && cid<nCol );
279 aIndex[cid] = 1;
280 }
danielk1977be718892006-06-23 08:05:19281 if( pStmt2 ){
282 rc = sqlite3_finalize(pStmt2);
283 }
danielk19775fac9f82006-06-13 14:16:58284 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58285 goto get_index_array_out;
286 }
287 }
288
danielk19775fac9f82006-06-13 14:16:58289
290get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11291 if( pStmt ){
292 int rc2 = sqlite3_finalize(pStmt);
293 if( rc==SQLITE_OK ){
294 rc = rc2;
295 }
296 }
danielk19775fac9f82006-06-13 14:16:58297 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38298 sqlite3_free(aIndex);
danielk19775fac9f82006-06-13 14:16:58299 aIndex = 0;
300 }
301 *paIndex = aIndex;
302 return rc;
303}
304
danielk197778efaba2006-06-12 06:09:17305/*
306** Global Tcl variable $echo_module is a list. This routine appends
307** the string element zArg to that list in interpreter interp.
308*/
drha967e882006-06-13 01:04:52309static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17310 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58311 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17312}
313
danielk19777e6ebfb2006-06-12 11:24:37314/*
315** This function is called from within the echo-modules xCreate and
316** xConnect methods. The argc and argv arguments are copies of those
317** passed to the calling method. This function is responsible for
318** calling sqlite3_declare_vtab() to declare the schema of the virtual
319** table being created or connected.
320**
321** If the constructor was passed just one argument, i.e.:
322**
323** CREATE TABLE t1 AS echo(t2);
324**
325** Then t2 is assumed to be the name of a *real* database table. The
326** schema of the virtual table is declared by passing a copy of the
327** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
328** Hence, the virtual table should have exactly the same column names and
329** types as the real table.
330*/
danielk1977b7a7b9a2006-06-13 10:24:42331static int echoDeclareVtab(
332 echo_vtab *pVtab,
danielk1977182c4ba2007-06-27 15:53:34333 sqlite3 *db
danielk1977b7a7b9a2006-06-13 10:24:42334){
danielk19777e6ebfb2006-06-12 11:24:37335 int rc = SQLITE_OK;
336
danielk1977182c4ba2007-06-27 15:53:34337 if( pVtab->zTableName ){
danielk19777e6ebfb2006-06-12 11:24:37338 sqlite3_stmt *pStmt = 0;
danielk1977222a7572007-08-25 13:37:48339 rc = sqlite3_prepare(db,
drh1e32bed2020-06-19 13:33:53340 "SELECT sql FROM sqlite_schema WHERE type = 'table' AND name = ?",
danielk19777e6ebfb2006-06-12 11:24:37341 -1, &pStmt, 0);
danielk1977222a7572007-08-25 13:37:48342 if( rc==SQLITE_OK ){
343 sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
344 if( sqlite3_step(pStmt)==SQLITE_ROW ){
345 int rc2;
346 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
347 rc = sqlite3_declare_vtab(db, zCreateTable);
348 rc2 = sqlite3_finalize(pStmt);
349 if( rc==SQLITE_OK ){
350 rc = rc2;
351 }
352 } else {
353 rc = sqlite3_finalize(pStmt);
354 if( rc==SQLITE_OK ){
355 rc = SQLITE_ERROR;
356 }
357 }
danielk19777fa5dd12007-04-18 17:04:00358 if( rc==SQLITE_OK ){
danielk1977222a7572007-08-25 13:37:48359 rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
danielk19777fa5dd12007-04-18 17:04:00360 }
danielk1977222a7572007-08-25 13:37:48361 if( rc==SQLITE_OK ){
362 rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
danielk1977be718892006-06-23 08:05:19363 }
danielk19777e6ebfb2006-06-12 11:24:37364 }
danielk19777e6ebfb2006-06-12 11:24:37365 }
366
367 return rc;
368}
369
danielk1977cc013f82006-06-24 06:36:11370/*
371** This function frees all runtime structures associated with the virtual
372** table pVtab.
373*/
danielk1977a4e76362006-06-14 06:31:28374static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28375 echo_vtab *p = (echo_vtab*)pVtab;
drh17435752007-08-16 04:30:38376 sqlite3_free(p->aIndex);
377 sqlite3_free(p->aCol);
378 sqlite3_free(p->zThis);
379 sqlite3_free(p->zTableName);
380 sqlite3_free(p->zLogName);
381 sqlite3_free(p);
danielk1977a4e76362006-06-14 06:31:28382 return 0;
383}
384
danielk1977860b6cd2007-09-03 11:51:50385typedef struct EchoModule EchoModule;
386struct EchoModule {
387 Tcl_Interp *interp;
drhba39ca42021-05-17 13:11:24388 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:50389};
390
danielk1977cc013f82006-06-24 06:36:11391/*
392** This function is called to do the work of the xConnect() method -
393** to allocate the required in-memory structures for a newly connected
394** virtual table.
395*/
danielk1977b7a7b9a2006-06-13 10:24:42396static int echoConstructor(
397 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15398 void *pAux,
drhe4102962006-09-11 00:34:22399 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58400 sqlite3_vtab **ppVtab,
401 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42402){
danielk1977a1644fd2007-08-29 12:31:25403 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42404 int i;
405 echo_vtab *pVtab;
406
danielk1977cc013f82006-06-24 06:36:11407 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk197731dad9d2007-08-16 11:36:15408 pVtab = sqlite3MallocZero( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19409 if( !pVtab ){
410 return SQLITE_NOMEM;
411 }
danielk1977860b6cd2007-09-03 11:51:50412 pVtab->interp = ((EchoModule *)pAux)->interp;
danielk1977b7a7b9a2006-06-13 10:24:42413 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11414
danielk1977182c4ba2007-06-27 15:53:34415 /* Allocate echo_vtab.zThis */
drhbc6160b2009-04-08 15:45:31416 pVtab->zThis = sqlite3_mprintf("%s", argv[2]);
danielk1977182c4ba2007-06-27 15:53:34417 if( !pVtab->zThis ){
danielk1977b7a2f2e2006-06-23 11:34:54418 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19419 return SQLITE_NOMEM;
420 }
421
danielk1977182c4ba2007-06-27 15:53:34422 /* Allocate echo_vtab.zTableName */
423 if( argc>3 ){
drhbc6160b2009-04-08 15:45:31424 pVtab->zTableName = sqlite3_mprintf("%s", argv[3]);
danielk1977182c4ba2007-06-27 15:53:34425 dequoteString(pVtab->zTableName);
426 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
drhbc6160b2009-04-08 15:45:31427 char *z = sqlite3_mprintf("%s%s", argv[2], &(pVtab->zTableName[1]));
drh17435752007-08-16 04:30:38428 sqlite3_free(pVtab->zTableName);
danielk1977182c4ba2007-06-27 15:53:34429 pVtab->zTableName = z;
430 pVtab->isPattern = 1;
431 }
432 if( !pVtab->zTableName ){
433 echoDestructor((sqlite3_vtab *)pVtab);
434 return SQLITE_NOMEM;
435 }
436 }
437
danielk1977cc013f82006-06-24 06:36:11438 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42439 for(i=0; i<argc; i++){
440 appendToEchoModule(pVtab->interp, argv[i]);
441 }
442
danielk1977cc013f82006-06-24 06:36:11443 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
444 ** structure. If an error occurs, delete the sqlite3_vtab structure and
445 ** return an error code.
446 */
danielk1977a1644fd2007-08-29 12:31:25447 rc = echoDeclareVtab(pVtab, db);
448 if( rc!=SQLITE_OK ){
danielk1977a4e76362006-06-14 06:31:28449 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977a1644fd2007-08-29 12:31:25450 return rc;
danielk1977a4e76362006-06-14 06:31:28451 }
452
danielk1977cc013f82006-06-24 06:36:11453 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28454 *ppVtab = &pVtab->base;
455 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42456}
drha967e882006-06-13 01:04:52457
danielk1977cc013f82006-06-24 06:36:11458/*
459** Echo virtual table module xCreate method.
460*/
drhb9bb7c12006-06-11 23:41:55461static int echoCreate(
462 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15463 void *pAux,
drhe4102962006-09-11 00:34:22464 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58465 sqlite3_vtab **ppVtab,
466 char **pzErr
drhb9bb7c12006-06-11 23:41:55467){
danielk1977a298e902006-06-22 09:53:48468 int rc = SQLITE_OK;
danielk1977860b6cd2007-09-03 11:51:50469 appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
drh4ca8aac2006-09-10 17:31:58470 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11471
472 /* If there were two arguments passed to the module at the SQL level
473 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
474 ** the second argument is used as a table name. Attempt to create
475 ** such a table with a single column, "logmsg". This table will
476 ** be used to log calls to the xUpdate method. It will be deleted
477 ** when the virtual table is DROPed.
478 **
479 ** Note: The main point of this is to test that we can drop tables
480 ** from within an xDestroy method call.
481 */
danielk1977a298e902006-06-22 09:53:48482 if( rc==SQLITE_OK && argc==5 ){
483 char *zSql;
484 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
drhbc6160b2009-04-08 15:45:31485 pVtab->zLogName = sqlite3_mprintf("%s", argv[4]);
486 zSql = sqlite3_mprintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
danielk1977a298e902006-06-22 09:53:48487 rc = sqlite3_exec(db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38488 sqlite3_free(zSql);
danielk1977cd2543b2007-09-03 15:03:20489 if( rc!=SQLITE_OK ){
drhb9755982010-07-24 16:34:37490 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
danielk1977cd2543b2007-09-03 15:03:20491 }
492 }
493
494 if( *ppVtab && rc!=SQLITE_OK ){
495 echoDestructor(*ppVtab);
496 *ppVtab = 0;
danielk1977a298e902006-06-22 09:53:48497 }
danielk1977cc013f82006-06-24 06:36:11498
drhcd3dd9d2008-04-28 20:27:53499 if( rc==SQLITE_OK ){
500 (*(echo_vtab**)ppVtab)->inTransaction = 1;
501 }
502
danielk1977a298e902006-06-22 09:53:48503 return rc;
drhb9bb7c12006-06-11 23:41:55504}
danielk1977cc013f82006-06-24 06:36:11505
506/*
507** Echo virtual table module xConnect method.
508*/
drhb9bb7c12006-06-11 23:41:55509static int echoConnect(
510 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15511 void *pAux,
drhe4102962006-09-11 00:34:22512 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58513 sqlite3_vtab **ppVtab,
514 char **pzErr
drhb9bb7c12006-06-11 23:41:55515){
danielk1977860b6cd2007-09-03 11:51:50516 appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
drh4ca8aac2006-09-10 17:31:58517 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55518}
danielk1977b7a7b9a2006-06-13 10:24:42519
danielk1977cc013f82006-06-24 06:36:11520/*
521** Echo virtual table module xDisconnect method.
522*/
danielk19775fac9f82006-06-13 14:16:58523static int echoDisconnect(sqlite3_vtab *pVtab){
524 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
525 return echoDestructor(pVtab);
526}
danielk1977cc013f82006-06-24 06:36:11527
528/*
529** Echo virtual table module xDestroy method.
530*/
danielk19775fac9f82006-06-13 14:16:58531static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48532 int rc = SQLITE_OK;
533 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58534 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11535
536 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48537 if( p && p->zLogName ){
538 char *zSql;
drhbc6160b2009-04-08 15:45:31539 zSql = sqlite3_mprintf("DROP TABLE %Q", p->zLogName);
danielk1977a298e902006-06-22 09:53:48540 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38541 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48542 }
danielk1977cc013f82006-06-24 06:36:11543
danielk1977a298e902006-06-22 09:53:48544 if( rc==SQLITE_OK ){
545 rc = echoDestructor(pVtab);
546 }
547 return rc;
danielk19775fac9f82006-06-13 14:16:58548}
549
danielk1977cc013f82006-06-24 06:36:11550/*
551** Echo virtual table module xOpen method.
552*/
danielk19775fac9f82006-06-13 14:16:58553static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42554 echo_cursor *pCur;
danielk19773e3a84d2008-08-01 17:37:40555 if( simulateVtabError((echo_vtab *)pVTab, "xOpen") ){
556 return SQLITE_ERROR;
557 }
danielk197731dad9d2007-08-16 11:36:15558 pCur = sqlite3MallocZero(sizeof(echo_cursor));
danielk1977b7a7b9a2006-06-13 10:24:42559 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19560 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42561}
562
danielk1977cc013f82006-06-24 06:36:11563/*
564** Echo virtual table module xClose method.
565*/
danielk19775fac9f82006-06-13 14:16:58566static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19567 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42568 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19569 sqlite3_stmt *pStmt = pCur->pStmt;
570 pCur->pStmt = 0;
drh17435752007-08-16 04:30:38571 sqlite3_free(pCur);
danielk1977be718892006-06-23 08:05:19572 rc = sqlite3_finalize(pStmt);
573 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42574}
575
danielk1977a298e902006-06-22 09:53:48576/*
577** Return non-zero if the cursor does not currently point to a valid record
578** (i.e if the scan has finished), or zero otherwise.
579*/
580static int echoEof(sqlite3_vtab_cursor *cur){
581 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
582}
583
danielk1977cc013f82006-06-24 06:36:11584/*
585** Echo virtual table module xNext method.
586*/
danielk19775fac9f82006-06-13 14:16:58587static int echoNext(sqlite3_vtab_cursor *cur){
danielk197731dad9d2007-08-16 11:36:15588 int rc = SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42589 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42590
danielk19773e3a84d2008-08-01 17:37:40591 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xNext") ){
592 return SQLITE_ERROR;
593 }
594
danielk197731dad9d2007-08-16 11:36:15595 if( pCur->pStmt ){
596 rc = sqlite3_step(pCur->pStmt);
597 if( rc==SQLITE_ROW ){
598 rc = SQLITE_OK;
599 }else{
600 rc = sqlite3_finalize(pCur->pStmt);
601 pCur->pStmt = 0;
602 }
danielk1977b7a7b9a2006-06-13 10:24:42603 }
604
605 return rc;
606}
607
danielk1977cc013f82006-06-24 06:36:11608/*
609** Echo virtual table module xColumn method.
610*/
danielk19775fac9f82006-06-13 14:16:58611static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42612 int iCol = i + 1;
613 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40614
615 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xColumn") ){
616 return SQLITE_ERROR;
617 }
618
danielk1977fbbe0052006-06-21 07:02:33619 if( !pStmt ){
620 sqlite3_result_null(ctx);
621 }else{
622 assert( sqlite3_data_count(pStmt)>iCol );
623 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
624 }
danielk1977b7a7b9a2006-06-13 10:24:42625 return SQLITE_OK;
626}
627
danielk1977cc013f82006-06-24 06:36:11628/*
629** Echo virtual table module xRowid method.
630*/
danielk19775fac9f82006-06-13 14:16:58631static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42632 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19773e3a84d2008-08-01 17:37:40633
634 if( simulateVtabError((echo_vtab *)(cur->pVtab), "xRowid") ){
635 return SQLITE_ERROR;
636 }
637
danielk1977b7a7b9a2006-06-13 10:24:42638 *pRowid = sqlite3_column_int64(pStmt, 0);
639 return SQLITE_OK;
640}
641
danielk197798331532006-06-14 10:55:52642/*
643** Compute a simple hash of the null terminated string zString.
644**
645** This module uses only sqlite3_index_info.idxStr, not
646** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
647** in echoBestIndex(), idxNum is set to the corresponding hash value.
648** In echoFilter(), code assert()s that the supplied idxNum value is
649** indeed the hash of the supplied idxStr.
650*/
651static int hashString(const char *zString){
drh4081d5d2015-01-01 23:02:01652 u32 val = 0;
danielk197798331532006-06-14 10:55:52653 int ii;
654 for(ii=0; zString[ii]; ii++){
655 val = (val << 3) + (int)zString[ii];
656 }
drh4081d5d2015-01-01 23:02:01657 return (int)(val&0x7fffffff);
danielk197798331532006-06-14 10:55:52658}
659
danielk1977cc013f82006-06-24 06:36:11660/*
661** Echo virtual table module xFilter method.
662*/
danielk19775fac9f82006-06-13 14:16:58663static int echoFilter(
664 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34665 int idxNum, const char *idxStr,
666 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58667){
668 int rc;
drh4be8b512006-06-13 23:51:34669 int i;
danielk19775fac9f82006-06-13 14:16:58670
671 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
672 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
673 sqlite3 *db = pVtab->db;
674
danielk19773e3a84d2008-08-01 17:37:40675 if( simulateVtabError(pVtab, "xFilter") ){
676 return SQLITE_ERROR;
677 }
678
danielk1977cc013f82006-06-24 06:36:11679 /* Check that idxNum matches idxStr */
680 assert( idxNum==hashString(idxStr) );
681
682 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19683 appendToEchoModule(pVtab->interp, "xFilter");
684 appendToEchoModule(pVtab->interp, idxStr);
685 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33686 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19687 }
688
danielk19775fac9f82006-06-13 14:16:58689 sqlite3_finalize(pCur->pStmt);
690 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11691
692 /* Prepare the SQL statement created by echoBestIndex and bind the
693 ** runtime parameters passed to this function to it.
694 */
drh4be8b512006-06-13 23:51:34695 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33696 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07697 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk19776eacd282009-04-29 11:50:53698 rc = sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34699 }
danielk1977cc013f82006-06-24 06:36:11700
701 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58702 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19703 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58704 }
705
danielk1977be718892006-06-23 08:05:19706 return rc;
danielk19775fac9f82006-06-13 14:16:58707}
danielk1977b7a7b9a2006-06-13 10:24:42708
danielk1977cc013f82006-06-24 06:36:11709
710/*
711** A helper function used by echoUpdate() and echoBestIndex() for
712** manipulating strings in concert with the sqlite3_mprintf() function.
713**
714** Parameter pzStr points to a pointer to a string allocated with
715** sqlite3_mprintf. The second parameter, zAppend, points to another
716** string. The two strings are concatenated together and *pzStr
717** set to point at the result. The initial buffer pointed to by *pzStr
718** is deallocated via sqlite3_free().
719**
720** If the third argument, doFree, is true, then sqlite3_free() is
721** also called to free the buffer pointed to by zAppend.
722*/
danielk1977a1644fd2007-08-29 12:31:25723static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
danielk1977cc013f82006-06-24 06:36:11724 char *zIn = *pzStr;
danielk1977a1644fd2007-08-29 12:31:25725 if( !zAppend && doFree && *pRc==SQLITE_OK ){
726 *pRc = SQLITE_NOMEM;
727 }
728 if( *pRc!=SQLITE_OK ){
729 sqlite3_free(zIn);
730 zIn = 0;
danielk1977cc013f82006-06-24 06:36:11731 }else{
danielk1977a1644fd2007-08-29 12:31:25732 if( zIn ){
733 char *zTemp = zIn;
734 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
735 sqlite3_free(zTemp);
736 }else{
737 zIn = sqlite3_mprintf("%s", zAppend);
738 }
739 if( !zIn ){
740 *pRc = SQLITE_NOMEM;
741 }
danielk1977cc013f82006-06-24 06:36:11742 }
743 *pzStr = zIn;
744 if( doFree ){
745 sqlite3_free(zAppend);
746 }
747}
748
drhb9bb7c12006-06-11 23:41:55749/*
dan1acb5392015-11-26 19:33:41750** This function returns a pointer to an sqlite3_malloc()ed buffer
751** containing the select-list (the thing between keywords SELECT and FROM)
752** to query the underlying real table with for the scan described by
753** argument pIdxInfo.
754**
755** If the current SQLite version is earlier than 3.10.0, this is just "*"
756** (select all columns). Or, for version 3.10.0 and greater, the list of
757** columns identified by the pIdxInfo->colUsed mask.
758*/
759static char *echoSelectList(echo_vtab *pTab, sqlite3_index_info *pIdxInfo){
760 char *zRet = 0;
761 if( sqlite3_libversion_number()<3010000 ){
762 zRet = sqlite3_mprintf(", *");
763 }else{
764 int i;
765 for(i=0; i<pTab->nCol; i++){
766 if( pIdxInfo->colUsed & ((sqlite3_uint64)1 << (i>=63 ? 63 : i)) ){
767 zRet = sqlite3_mprintf("%z, %s", zRet, pTab->aCol[i]);
768 }else{
769 zRet = sqlite3_mprintf("%z, NULL", zRet);
770 }
771 if( !zRet ) break;
772 }
773 }
774 return zRet;
775}
776
777/*
danielk19775fac9f82006-06-13 14:16:58778** The echo module implements the subset of query constraints and sort
779** orders that may take advantage of SQLite indices on the underlying
780** real table. For example, if the real table is declared as:
781**
782** CREATE TABLE real(a, b, c);
783** CREATE INDEX real_index ON real(b);
784**
785** then the echo module handles WHERE or ORDER BY clauses that refer
786** to the column "b", but not "a" or "c". If a multi-column index is
drh85b623f2007-12-13 21:54:09787** present, only its left most column is considered.
danielk1977cc013f82006-06-24 06:36:11788**
789** This xBestIndex method encodes the proposed search strategy as
790** an SQL query on the real table underlying the virtual echo module
791** table and stores the query in sqlite3_index_info.idxStr. The SQL
792** statement is of the form:
793**
794** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
795**
796** where the <where-clause> and <order-by-clause> are determined
797** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52798*/
danielk19775fac9f82006-06-13 14:16:58799static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
800 int ii;
drh4be8b512006-06-13 23:51:34801 char *zQuery = 0;
dan1acb5392015-11-26 19:33:41802 char *zCol = 0;
drh4be8b512006-06-13 23:51:34803 char *zNew;
danielk19775fac9f82006-06-13 14:16:58804 int nArg = 0;
drh4be8b512006-06-13 23:51:34805 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58806 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58807 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34808 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58809
mistachkin27b2f052015-01-12 19:49:46810 int nRow = 0;
danielk197774cdba42006-06-19 12:02:58811 int useIdx = 0;
812 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34813 int useCost = 0;
mistachkin27b2f052015-01-12 19:49:46814 double cost = 0;
danielk197739359dc2008-03-17 09:36:44815 int isIgnoreUsable = 0;
816 if( Tcl_GetVar(interp, "echo_module_ignore_usable", TCL_GLOBAL_ONLY) ){
817 isIgnoreUsable = 1;
818 }
819
danielk19773e3a84d2008-08-01 17:37:40820 if( simulateVtabError(pVtab, "xBestIndex") ){
821 return SQLITE_ERROR;
822 }
823
danielk197774cdba42006-06-19 12:02:58824 /* Determine the number of rows in the table and store this value in local
825 ** variable nRow. The 'estimated-cost' of the scan will be the number of
826 ** rows in the table for a linear scan, or the log (base 2) of the
827 ** number of rows if the proposed scan uses an index.
828 */
danielk1977780b1d92007-03-30 14:56:34829 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
830 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
831 useCost = 1;
832 } else {
833 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25834 if( !zQuery ){
835 return SQLITE_NOMEM;
836 }
danielk1977780b1d92007-03-30 14:56:34837 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
838 sqlite3_free(zQuery);
839 if( rc!=SQLITE_OK ){
840 return rc;
841 }
842 sqlite3_step(pStmt);
843 nRow = sqlite3_column_int(pStmt, 0);
844 rc = sqlite3_finalize(pStmt);
845 if( rc!=SQLITE_OK ){
846 return rc;
847 }
danielk197774cdba42006-06-19 12:02:58848 }
danielk19775fac9f82006-06-13 14:16:58849
dan1acb5392015-11-26 19:33:41850 zCol = echoSelectList(pVtab, pIdxInfo);
851 if( !zCol ) return SQLITE_NOMEM;
852 zQuery = sqlite3_mprintf("SELECT rowid%z FROM %Q", zCol, pVtab->zTableName);
853 if( !zQuery ) return SQLITE_NOMEM;
854
danielk19775fac9f82006-06-13 14:16:58855 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
856 const struct sqlite3_index_constraint *pConstraint;
857 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57858 int iCol;
danielk19775fac9f82006-06-13 14:16:58859
860 pConstraint = &pIdxInfo->aConstraint[ii];
861 pUsage = &pIdxInfo->aConstraintUsage[ii];
862
danielk197739359dc2008-03-17 09:36:44863 if( !isIgnoreUsable && !pConstraint->usable ) continue;
864
drh383736b2006-10-08 18:56:57865 iCol = pConstraint->iColumn;
drh82ebc2a2012-03-20 03:10:51866 if( iCol<0 || pVtab->aIndex[iCol] ){
mistachkin8ccdef62015-12-16 22:06:52867 char *zNewCol = iCol>=0 ? pVtab->aCol[iCol] : "rowid";
danielk19775fac9f82006-06-13 14:16:58868 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58869 useIdx = 1;
danielk19775fac9f82006-06-13 14:16:58870 switch( pConstraint->op ){
871 case SQLITE_INDEX_CONSTRAINT_EQ:
872 zOp = "="; break;
873 case SQLITE_INDEX_CONSTRAINT_LT:
874 zOp = "<"; break;
875 case SQLITE_INDEX_CONSTRAINT_GT:
876 zOp = ">"; break;
877 case SQLITE_INDEX_CONSTRAINT_LE:
878 zOp = "<="; break;
879 case SQLITE_INDEX_CONSTRAINT_GE:
880 zOp = ">="; break;
881 case SQLITE_INDEX_CONSTRAINT_MATCH:
mistachkina9124d32015-11-24 01:17:01882 /* Purposely translate the MATCH operator into a LIKE, which
883 ** will be used by the next block of code to construct a new
884 ** query. It should also be noted here that the next block
885 ** of code requires the first letter of this operator to be
886 ** in upper-case to trigger the special MATCH handling (i.e.
887 ** wrapping the bound parameter with literal '%'s).
888 */
drh1a90e092006-06-14 22:07:10889 zOp = "LIKE"; break;
dan07bdba82015-11-23 21:09:54890 case SQLITE_INDEX_CONSTRAINT_LIKE:
891 zOp = "like"; break;
892 case SQLITE_INDEX_CONSTRAINT_GLOB:
893 zOp = "glob"; break;
894 case SQLITE_INDEX_CONSTRAINT_REGEXP:
895 zOp = "regexp"; break;
danielk19775fac9f82006-06-13 14:16:58896 }
dand03024d2017-09-09 19:41:12897 if( zOp ){
898 if( zOp[0]=='L' ){
899 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
900 zSep, zNewCol);
901 } else {
902 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zNewCol, zOp);
903 }
904 string_concat(&zQuery, zNew, 1, &rc);
905 zSep = "AND";
906 pUsage->argvIndex = ++nArg;
907 pUsage->omit = 1;
drh1a90e092006-06-14 22:07:10908 }
danielk19775fac9f82006-06-13 14:16:58909 }
910 }
danielk197747d08092006-06-14 07:41:31911
912 /* If there is only one term in the ORDER BY clause, and it is
913 ** on a column that this virtual table has an index for, then consume
914 ** the ORDER BY clause.
915 */
drh82ebc2a2012-03-20 03:10:51916 if( pIdxInfo->nOrderBy==1 && (
917 pIdxInfo->aOrderBy->iColumn<0 ||
918 pVtab->aIndex[pIdxInfo->aOrderBy->iColumn]) ){
danielk197765fd59f2006-06-24 11:51:33919 int iCol = pIdxInfo->aOrderBy->iColumn;
mistachkin8ccdef62015-12-16 22:06:52920 char *zNewCol = iCol>=0 ? pVtab->aCol[iCol] : "rowid";
danielk197747d08092006-06-14 07:41:31921 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
mistachkin8ccdef62015-12-16 22:06:52922 zNew = sqlite3_mprintf(" ORDER BY %s %s", zNewCol, zDir);
danielk1977a1644fd2007-08-29 12:31:25923 string_concat(&zQuery, zNew, 1, &rc);
danielk197747d08092006-06-14 07:41:31924 pIdxInfo->orderByConsumed = 1;
925 }
926
danielk19775fac9f82006-06-13 14:16:58927 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34928 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58929
danielk1977222a7572007-08-25 13:37:48930 if( !zQuery ){
danielk1977a1644fd2007-08-29 12:31:25931 return rc;
danielk1977222a7572007-08-25 13:37:48932 }
danielk197798331532006-06-14 10:55:52933 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34934 pIdxInfo->idxStr = zQuery;
935 pIdxInfo->needToFreeIdxStr = 1;
danielk19771d461462009-04-21 09:02:45936 if( useCost ){
danielk1977780b1d92007-03-30 14:56:34937 pIdxInfo->estimatedCost = cost;
danielk19771d461462009-04-21 09:02:45938 }else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58939 /* Approximation of log2(nRow). */
drh693e6712014-01-24 22:58:00940 for( ii=0; ii<(sizeof(int)*8)-1; ii++ ){
danielk197774cdba42006-06-19 12:02:58941 if( nRow & (1<<ii) ){
942 pIdxInfo->estimatedCost = (double)ii;
943 }
944 }
danielk19771d461462009-04-21 09:02:45945 }else{
danielk197774cdba42006-06-19 12:02:58946 pIdxInfo->estimatedCost = (double)nRow;
947 }
948 return rc;
drha967e882006-06-13 01:04:52949}
950
danielk1977176f4d22006-06-15 10:41:15951/*
danielk1977cc013f82006-06-24 06:36:11952** The xUpdate method for echo module virtual tables.
953**
danielk1977176f4d22006-06-15 10:41:15954** apData[0] apData[1] apData[2..]
955**
956** INTEGER DELETE
957**
958** INTEGER NULL (nCol args) UPDATE (do not set rowid)
959** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
960**
961** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
962** NULL INTEGER (nCol args) INSERT (incl. rowid value)
963**
964*/
danielk19771f6eec52006-06-16 06:17:47965int echoUpdate(
966 sqlite3_vtab *tab,
967 int nData,
968 sqlite3_value **apData,
969 sqlite_int64 *pRowid
970){
danielk197726e41442006-06-14 15:16:35971 echo_vtab *pVtab = (echo_vtab *)tab;
972 sqlite3 *db = pVtab->db;
973 int rc = SQLITE_OK;
974
mistachkin27b2f052015-01-12 19:49:46975 sqlite3_stmt *pStmt = 0;
danielk1977176f4d22006-06-15 10:41:15976 char *z = 0; /* SQL statement to execute */
977 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
978 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
979 int i; /* Counter variable used by for loops */
980
danielk197726e41442006-06-14 15:16:35981 assert( nData==pVtab->nCol+2 || nData==1 );
982
drhcd3dd9d2008-04-28 20:27:53983 /* Ticket #3083 - make sure we always start a transaction prior to
984 ** making any changes to a virtual table */
985 assert( pVtab->inTransaction );
986
danielk19773e3a84d2008-08-01 17:37:40987 if( simulateVtabError(pVtab, "xUpdate") ){
988 return SQLITE_ERROR;
989 }
990
drh5aec0422006-06-14 23:43:31991 /* If apData[0] is an integer and nData>1 then do an UPDATE */
992 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31993 char *zSep = " SET";
drh383736b2006-10-08 18:56:57994 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25995 if( !z ){
996 rc = SQLITE_NOMEM;
997 }
danielk1977176f4d22006-06-15 10:41:15998
999 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
1000 bindArgZero = 1;
1001
1002 if( bindArgOne ){
danielk1977a1644fd2007-08-29 12:31:251003 string_concat(&z, " SET rowid=?1 ", 0, &rc);
drh5aec0422006-06-14 23:43:311004 zSep = ",";
1005 }
1006 for(i=2; i<nData; i++){
1007 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:151008 string_concat(&z, sqlite3_mprintf(
danielk1977a1644fd2007-08-29 12:31:251009 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
drh5aec0422006-06-14 23:43:311010 zSep = ",";
1011 }
danielk1977a1644fd2007-08-29 12:31:251012 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
drh5aec0422006-06-14 23:43:311013 }
1014
danielk1977176f4d22006-06-15 10:41:151015 /* If apData[0] is an integer and nData==1 then do a DELETE */
1016 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
1017 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:251018 if( !z ){
1019 rc = SQLITE_NOMEM;
1020 }
danielk1977176f4d22006-06-15 10:41:151021 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:351022 }
1023
danielk1977176f4d22006-06-15 10:41:151024 /* If the first argument is NULL and there are more than two args, INSERT */
1025 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:351026 int ii;
1027 char *zInsert = 0;
1028 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:471029
danielk19774b2688a2006-06-20 11:01:071030 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:251031 if( !zInsert ){
1032 rc = SQLITE_NOMEM;
1033 }
danielk1977176f4d22006-06-15 10:41:151034 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
1035 bindArgOne = 1;
1036 zValues = sqlite3_mprintf("?");
danielk1977a1644fd2007-08-29 12:31:251037 string_concat(&zInsert, "rowid", 0, &rc);
danielk197726e41442006-06-14 15:16:351038 }
1039
danielk1977176f4d22006-06-15 10:41:151040 assert((pVtab->nCol+2)==nData);
1041 for(ii=2; ii<nData; ii++){
1042 string_concat(&zInsert,
danielk1977a1644fd2007-08-29 12:31:251043 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
danielk1977176f4d22006-06-15 10:41:151044 string_concat(&zValues,
danielk1977a1644fd2007-08-29 12:31:251045 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
danielk197726e41442006-06-14 15:16:351046 }
1047
danielk1977a1644fd2007-08-29 12:31:251048 string_concat(&z, zInsert, 1, &rc);
1049 string_concat(&z, ") VALUES(", 0, &rc);
1050 string_concat(&z, zValues, 1, &rc);
1051 string_concat(&z, ")", 0, &rc);
danielk1977176f4d22006-06-15 10:41:151052 }
1053
1054 /* Anything else is an error */
1055 else{
1056 assert(0);
1057 return SQLITE_ERROR;
1058 }
1059
danielk1977a1644fd2007-08-29 12:31:251060 if( rc==SQLITE_OK ){
1061 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
1062 }
danielk1977176f4d22006-06-15 10:41:151063 assert( rc!=SQLITE_OK || pStmt );
1064 sqlite3_free(z);
1065 if( rc==SQLITE_OK ) {
1066 if( bindArgZero ){
1067 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:351068 }
danielk1977176f4d22006-06-15 10:41:151069 if( bindArgOne ){
1070 sqlite3_bind_value(pStmt, 1, apData[1]);
1071 }
danielk1977a7a8e142008-02-13 18:25:271072 for(i=2; i<nData && rc==SQLITE_OK; i++){
1073 if( apData[i] ) rc = sqlite3_bind_value(pStmt, i, apData[i]);
danielk1977176f4d22006-06-15 10:41:151074 }
danielk1977a7a8e142008-02-13 18:25:271075 if( rc==SQLITE_OK ){
1076 sqlite3_step(pStmt);
1077 rc = sqlite3_finalize(pStmt);
1078 }else{
1079 sqlite3_finalize(pStmt);
1080 }
danielk197726e41442006-06-14 15:16:351081 }
1082
danielk19771f6eec52006-06-16 06:17:471083 if( pRowid && rc==SQLITE_OK ){
1084 *pRowid = sqlite3_last_insert_rowid(db);
1085 }
drh4dc754d2008-07-23 18:17:321086 if( rc!=SQLITE_OK ){
1087 tab->zErrMsg = sqlite3_mprintf("echo-vtab-error: %s", sqlite3_errmsg(db));
1088 }
danielk19771f6eec52006-06-16 06:17:471089
danielk197726e41442006-06-14 15:16:351090 return rc;
1091}
1092
drha967e882006-06-13 01:04:521093/*
danielk1977c69cdfd2006-06-17 09:39:551094** xBegin, xSync, xCommit and xRollback callbacks for echo module
1095** virtual tables. Do nothing other than add the name of the callback
1096** to the $::echo_module Tcl variable.
1097*/
1098static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
1099 char *z;
1100 echo_vtab *pVtab = (echo_vtab *)tab;
1101 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
drh344c38e2008-05-05 13:23:041102 if( z==0 ) return SQLITE_NOMEM;
danielk1977c69cdfd2006-06-17 09:39:551103 appendToEchoModule(pVtab->interp, zCall);
1104 appendToEchoModule(pVtab->interp, z);
1105 sqlite3_free(z);
drh344c38e2008-05-05 13:23:041106 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:551107}
1108static int echoBegin(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:251109 int rc;
danielk19775017dc32006-06-24 09:34:221110 echo_vtab *pVtab = (echo_vtab *)tab;
1111 Tcl_Interp *interp = pVtab->interp;
1112 const char *zVal;
1113
drhcd3dd9d2008-04-28 20:27:531114 /* Ticket #3083 - do not start a transaction if we are already in
1115 ** a transaction */
1116 assert( !pVtab->inTransaction );
1117
danielk19773e3a84d2008-08-01 17:37:401118 if( simulateVtabError(pVtab, "xBegin") ){
1119 return SQLITE_ERROR;
1120 }
1121
danielk1977a1644fd2007-08-29 12:31:251122 rc = echoTransactionCall(tab, "xBegin");
danielk19775017dc32006-06-24 09:34:221123
danielk1977a1644fd2007-08-29 12:31:251124 if( rc==SQLITE_OK ){
1125 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
1126 ** and it is set to the name of the real table underlying this virtual
1127 ** echo module table, then cause this xSync operation to fail.
1128 */
1129 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
1130 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1131 rc = SQLITE_ERROR;
1132 }
danielk19775017dc32006-06-24 09:34:221133 }
drhcd3dd9d2008-04-28 20:27:531134 if( rc==SQLITE_OK ){
1135 pVtab->inTransaction = 1;
1136 }
danielk1977a1644fd2007-08-29 12:31:251137 return rc;
danielk1977c69cdfd2006-06-17 09:39:551138}
1139static int echoSync(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:251140 int rc;
danielk1977c69cdfd2006-06-17 09:39:551141 echo_vtab *pVtab = (echo_vtab *)tab;
1142 Tcl_Interp *interp = pVtab->interp;
1143 const char *zVal;
1144
drhcd3dd9d2008-04-28 20:27:531145 /* Ticket #3083 - Only call xSync if we have previously started a
1146 ** transaction */
1147 assert( pVtab->inTransaction );
1148
danielk19773e3a84d2008-08-01 17:37:401149 if( simulateVtabError(pVtab, "xSync") ){
1150 return SQLITE_ERROR;
1151 }
1152
danielk1977a1644fd2007-08-29 12:31:251153 rc = echoTransactionCall(tab, "xSync");
danielk1977c69cdfd2006-06-17 09:39:551154
danielk1977a1644fd2007-08-29 12:31:251155 if( rc==SQLITE_OK ){
1156 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
1157 ** and it is set to the name of the real table underlying this virtual
1158 ** echo module table, then cause this xSync operation to fail.
1159 */
1160 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
1161 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1162 rc = -1;
1163 }
danielk1977c69cdfd2006-06-17 09:39:551164 }
danielk1977a1644fd2007-08-29 12:31:251165 return rc;
danielk1977c69cdfd2006-06-17 09:39:551166}
1167static int echoCommit(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:531168 echo_vtab *pVtab = (echo_vtab*)tab;
drh643167f2008-01-22 21:30:531169 int rc;
drhcd3dd9d2008-04-28 20:27:531170
1171 /* Ticket #3083 - Only call xCommit if we have previously started
1172 ** a transaction */
1173 assert( pVtab->inTransaction );
1174
danielk19773e3a84d2008-08-01 17:37:401175 if( simulateVtabError(pVtab, "xCommit") ){
1176 return SQLITE_ERROR;
1177 }
1178
danielk19772d1d86f2008-06-20 14:59:511179 sqlite3BeginBenignMalloc();
drh643167f2008-01-22 21:30:531180 rc = echoTransactionCall(tab, "xCommit");
danielk19772d1d86f2008-06-20 14:59:511181 sqlite3EndBenignMalloc();
drh344c38e2008-05-05 13:23:041182 pVtab->inTransaction = 0;
drh643167f2008-01-22 21:30:531183 return rc;
danielk1977c69cdfd2006-06-17 09:39:551184}
1185static int echoRollback(sqlite3_vtab *tab){
drhcd3dd9d2008-04-28 20:27:531186 int rc;
1187 echo_vtab *pVtab = (echo_vtab*)tab;
1188
1189 /* Ticket #3083 - Only call xRollback if we have previously started
1190 ** a transaction */
1191 assert( pVtab->inTransaction );
1192
1193 rc = echoTransactionCall(tab, "xRollback");
1194 pVtab->inTransaction = 0;
1195 return rc;
danielk1977c69cdfd2006-06-17 09:39:551196}
1197
1198/*
drhe94b0c32006-07-08 18:09:151199** Implementation of "GLOB" function on the echo module. Pass
1200** all arguments to the ::echo_glob_overload procedure of TCL
1201** and return the result of that procedure as a string.
1202*/
1203static void overloadedGlobFunction(
1204 sqlite3_context *pContext,
1205 int nArg,
1206 sqlite3_value **apArg
1207){
1208 Tcl_Interp *interp = sqlite3_user_data(pContext);
1209 Tcl_DString str;
1210 int i;
1211 int rc;
1212 Tcl_DStringInit(&str);
1213 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
1214 for(i=0; i<nArg; i++){
1215 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
1216 }
1217 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
1218 Tcl_DStringFree(&str);
1219 if( rc ){
1220 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
1221 }else{
1222 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1223 -1, SQLITE_TRANSIENT);
1224 }
1225 Tcl_ResetResult(interp);
1226}
1227
1228/*
1229** This is the xFindFunction implementation for the echo module.
1230** SQLite calls this routine when the first argument of a function
1231** is a column of an echo virtual table. This routine can optionally
1232** override the implementation of that function. It will choose to
1233** do so if the function is named "glob", and a TCL command named
1234** ::echo_glob_overload exists.
1235*/
1236static int echoFindFunction(
1237 sqlite3_vtab *vtab,
1238 int nArg,
1239 const char *zFuncName,
1240 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1241 void **ppArg
1242){
1243 echo_vtab *pVtab = (echo_vtab *)vtab;
1244 Tcl_Interp *interp = pVtab->interp;
1245 Tcl_CmdInfo info;
1246 if( strcmp(zFuncName,"glob")!=0 ){
1247 return 0;
1248 }
1249 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1250 return 0;
1251 }
1252 *pxFunc = overloadedGlobFunction;
1253 *ppArg = interp;
1254 return 1;
1255}
1256
danielk1977182c4ba2007-06-27 15:53:341257static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1258 int rc = SQLITE_OK;
1259 echo_vtab *p = (echo_vtab *)vtab;
1260
danielk1977987a00e2008-08-01 17:51:471261 if( simulateVtabError(p, "xRename") ){
1262 return SQLITE_ERROR;
1263 }
1264
danielk1977182c4ba2007-06-27 15:53:341265 if( p->isPattern ){
drh83cc1392012-04-19 18:04:281266 int nThis = (int)strlen(p->zThis);
drhbc6160b2009-04-08 15:45:311267 char *zSql = sqlite3_mprintf("ALTER TABLE %s RENAME TO %s%s",
danielk1977182c4ba2007-06-27 15:53:341268 p->zTableName, zNewName, &p->zTableName[nThis]
1269 );
1270 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:381271 sqlite3_free(zSql);
danielk1977182c4ba2007-06-27 15:53:341272 }
1273
1274 return rc;
1275}
1276
dan3b504df2011-10-29 15:29:431277static int echoSavepoint(sqlite3_vtab *pVTab, int iSavepoint){
1278 assert( pVTab );
1279 return SQLITE_OK;
1280}
1281
1282static int echoRelease(sqlite3_vtab *pVTab, int iSavepoint){
1283 assert( pVTab );
1284 return SQLITE_OK;
1285}
1286
1287static int echoRollbackTo(sqlite3_vtab *pVTab, int iSavepoint){
1288 assert( pVTab );
1289 return SQLITE_OK;
1290}
1291
drhe94b0c32006-07-08 18:09:151292/*
danielk1977cc013f82006-06-24 06:36:111293** A virtual table module that merely "echos" the contents of another
1294** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:551295*/
1296static sqlite3_module echoModule = {
dan3b504df2011-10-29 15:29:431297 1, /* iVersion */
1298 echoCreate,
1299 echoConnect,
1300 echoBestIndex,
1301 echoDisconnect,
1302 echoDestroy,
1303 echoOpen, /* xOpen - open a cursor */
1304 echoClose, /* xClose - close a cursor */
1305 echoFilter, /* xFilter - configure scan constraints */
1306 echoNext, /* xNext - advance a cursor */
1307 echoEof, /* xEof */
1308 echoColumn, /* xColumn - read data */
1309 echoRowid, /* xRowid - read data */
1310 echoUpdate, /* xUpdate - write data */
1311 echoBegin, /* xBegin - begin transaction */
1312 echoSync, /* xSync - sync transaction */
1313 echoCommit, /* xCommit - commit transaction */
1314 echoRollback, /* xRollback - rollback transaction */
1315 echoFindFunction, /* xFindFunction - function overloading */
drh19358872023-10-06 12:51:051316 echoRename, /* xRename - rename the table */
1317 0, /* xSavepoint */
1318 0, /* xRelease */
1319 0, /* xRollbackTo */
1320 0, /* xShadowName */
1321 0 /* xIntegrity */
dan3b504df2011-10-29 15:29:431322};
1323
1324static sqlite3_module echoModuleV2 = {
1325 2, /* iVersion */
drhb9bb7c12006-06-11 23:41:551326 echoCreate,
1327 echoConnect,
drha967e882006-06-13 01:04:521328 echoBestIndex,
drhb9bb7c12006-06-11 23:41:551329 echoDisconnect,
1330 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:421331 echoOpen, /* xOpen - open a cursor */
1332 echoClose, /* xClose - close a cursor */
1333 echoFilter, /* xFilter - configure scan constraints */
1334 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:481335 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:421336 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:351337 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:551338 echoUpdate, /* xUpdate - write data */
1339 echoBegin, /* xBegin - begin transaction */
1340 echoSync, /* xSync - sync transaction */
1341 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:431342 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:151343 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:341344 echoRename, /* xRename - rename the table */
dan3b504df2011-10-29 15:29:431345 echoSavepoint,
1346 echoRelease,
drh19358872023-10-06 12:51:051347 echoRollbackTo,
1348 0, /* xShadowName */
1349 0 /* xIntegrity */
drhb9bb7c12006-06-11 23:41:551350};
1351
1352/*
1353** Decode a pointer to an sqlite3 object.
1354*/
drh24b58dd2008-07-07 14:50:141355extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
mistachkine84d8d32013-04-29 03:09:101356extern const char *sqlite3ErrName(int);
drhb9bb7c12006-06-11 23:41:551357
danielk1977860b6cd2007-09-03 11:51:501358static void moduleDestroy(void *p){
drhba39ca42021-05-17 13:11:241359 EchoModule *pMod = (EchoModule*)p;
1360 sqlite3_create_function(pMod->db, "function_that_does_not_exist_0982ma98",
1361 SQLITE_ANY, 1, 0, 0, 0, 0);
danielk1977860b6cd2007-09-03 11:51:501362 sqlite3_free(p);
1363}
1364
drhb9bb7c12006-06-11 23:41:551365/*
1366** Register the echo virtual table module.
1367*/
mistachkin7617e4a2016-07-28 17:11:201368static int SQLITE_TCLAPI register_echo_module(
drhb9bb7c12006-06-11 23:41:551369 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1370 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1371 int objc, /* Number of arguments */
1372 Tcl_Obj *CONST objv[] /* Command arguments */
1373){
danca8b9ba2012-05-16 14:29:111374 int rc;
drhb9bb7c12006-06-11 23:41:551375 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:501376 EchoModule *pMod;
drhb9bb7c12006-06-11 23:41:551377 if( objc!=2 ){
1378 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1379 return TCL_ERROR;
1380 }
1381 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
dan3b504df2011-10-29 15:29:431382
1383 /* Virtual table module "echo" */
danielk1977860b6cd2007-09-03 11:51:501384 pMod = sqlite3_malloc(sizeof(EchoModule));
1385 pMod->interp = interp;
drhba39ca42021-05-17 13:11:241386 pMod->db = db;
danca8b9ba2012-05-16 14:29:111387 rc = sqlite3_create_module_v2(
1388 db, "echo", &echoModule, (void*)pMod, moduleDestroy
1389 );
dan3b504df2011-10-29 15:29:431390
1391 /* Virtual table module "echo_v2" */
danca8b9ba2012-05-16 14:29:111392 if( rc==SQLITE_OK ){
1393 pMod = sqlite3_malloc(sizeof(EchoModule));
1394 pMod->interp = interp;
drhba39ca42021-05-17 13:11:241395 pMod->db = db;
danca8b9ba2012-05-16 14:29:111396 rc = sqlite3_create_module_v2(db, "echo_v2",
1397 &echoModuleV2, (void*)pMod, moduleDestroy
1398 );
1399 }
1400
mistachkine84d8d32013-04-29 03:09:101401 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
drhb9bb7c12006-06-11 23:41:551402 return TCL_OK;
1403}
1404
danielk19775017dc32006-06-24 09:34:221405/*
1406** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1407**
1408** sqlite3_declare_vtab DB SQL
1409*/
mistachkin7617e4a2016-07-28 17:11:201410static int SQLITE_TCLAPI declare_vtab(
danielk19775017dc32006-06-24 09:34:221411 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1412 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1413 int objc, /* Number of arguments */
1414 Tcl_Obj *CONST objv[] /* Command arguments */
1415){
1416 sqlite3 *db;
1417 int rc;
1418 if( objc!=3 ){
1419 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1420 return TCL_ERROR;
1421 }
1422 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1423 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1424 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:331425 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:221426 return TCL_ERROR;
1427 }
1428 return TCL_OK;
1429}
1430
danielk1977cc013f82006-06-24 06:36:111431#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:551432
1433/*
1434** Register commands with the TCL interpreter.
1435*/
1436int Sqlitetest8_Init(Tcl_Interp *interp){
danielk19776e891622008-08-12 14:48:401437#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:551438 static struct {
1439 char *zName;
1440 Tcl_ObjCmdProc *xProc;
1441 void *clientData;
1442 } aObjCmd[] = {
dan2deb1652012-07-13 16:15:201443 { "register_echo_module", register_echo_module, 0 },
dan2deb1652012-07-13 16:15:201444 { "sqlite3_declare_vtab", declare_vtab, 0 },
drhb9bb7c12006-06-11 23:41:551445 };
1446 int i;
1447 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1448 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1449 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1450 }
danielk19776e891622008-08-12 14:48:401451#endif
drhb9bb7c12006-06-11 23:41:551452 return TCL_OK;
1453}