blob: d95612269a97637652e18d22be4010b0f51a3a42 [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.
15**
danielk1977860b6cd2007-09-03 11:51:50 +000016** $Id: test8.c,v 1.56 2007/09/03 11:51:50 danielk1977 Exp $
drhb9bb7c12006-06-11 23:41:55 +000017*/
18#include "sqliteInt.h"
19#include "tcl.h"
drhb9bb7c12006-06-11 23:41:55 +000020#include <stdlib.h>
21#include <string.h>
22
danielk1977cc013f82006-06-24 06:36:11 +000023#ifndef SQLITE_OMIT_VIRTUALTABLE
24
danielk1977b7a7b9a2006-06-13 10:24:42 +000025typedef struct echo_vtab echo_vtab;
26typedef struct echo_cursor echo_cursor;
27
danielk1977c69cdfd2006-06-17 09:39:55 +000028/*
danielk1977780b1d92007-03-30 14:56:34 +000029** The test module defined in this file uses four global Tcl variables to
danielk1977c69cdfd2006-06-17 09:39:55 +000030** commicate with test-scripts:
31**
32** $::echo_module
33** $::echo_module_sync_fail
danielk19775017dc32006-06-24 09:34:22 +000034** $::echo_module_begin_fail
danielk1977780b1d92007-03-30 14:56:34 +000035** $::echo_module_cost
danielk1977cc013f82006-06-24 06:36:11 +000036**
37** The variable ::echo_module is a list. Each time one of the following
38** methods is called, one or more elements are appended to the list.
39** This is used for automated testing of virtual table modules.
40**
41** The ::echo_module_sync_fail variable is set by test scripts and read
42** by code in this file. If it is set to the name of a real table in the
43** the database, then all xSync operations on echo virtual tables that
44** use the named table as a backing store will fail.
danielk1977c69cdfd2006-06-17 09:39:55 +000045*/
46
danielk19775fac9f82006-06-13 14:16:58 +000047/*
danielk1977d6e8dd02006-06-15 15:38:41 +000048** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:58 +000049**
danielk1977d6e8dd02006-06-15 15:38:41 +000050** echo.vtab.aIndex is an array of booleans. The nth entry is true if
51** the nth column of the real table is the left-most column of an index
52** (implicit or otherwise). In other words, if SQLite can optimize
53** a query like "SELECT * FROM real_table WHERE col = ?".
54**
danielk1977c69cdfd2006-06-17 09:39:55 +000055** Member variable aCol[] contains copies of the column names of the real
56** table.
danielk19775fac9f82006-06-13 14:16:58 +000057*/
danielk1977b7a7b9a2006-06-13 10:24:42 +000058struct echo_vtab {
59 sqlite3_vtab base;
danielk1977cc013f82006-06-24 06:36:11 +000060 Tcl_Interp *interp; /* Tcl interpreter containing debug variables */
61 sqlite3 *db; /* Database connection */
danielk19775fac9f82006-06-13 14:16:58 +000062
danielk1977182c4ba2007-06-27 15:53:34 +000063 int isPattern;
64 char *zThis; /* Name of the echo table */
danielk1977a4e76362006-06-14 06:31:28 +000065 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:48 +000066 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:28 +000067 int nCol; /* Number of columns in the real table */
68 int *aIndex; /* Array of size nCol. True if column has an index */
69 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000070};
71
72/* An echo cursor object */
73struct echo_cursor {
74 sqlite3_vtab_cursor base;
75 sqlite3_stmt *pStmt;
danielk1977b7a7b9a2006-06-13 10:24:42 +000076};
77
danielk1977cc013f82006-06-24 06:36:11 +000078/*
danielk1977182c4ba2007-06-27 15:53:34 +000079** Convert an SQL-style quoted string into a normal string by removing
80** the quote characters. The conversion is done in-place. If the
81** input does not begin with a quote character, then this routine
82** is a no-op.
83**
84** Examples:
85**
86** "abc" becomes abc
87** 'xyz' becomes xyz
88** [pqr] becomes pqr
89** `mno` becomes mno
90*/
91static void dequoteString(char *z){
92 int quote;
93 int i, j;
94 if( z==0 ) return;
95 quote = z[0];
96 switch( quote ){
97 case '\'': break;
98 case '"': break;
99 case '`': break; /* For MySQL compatibility */
100 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
101 default: return;
102 }
103 for(i=1, j=0; z[i]; i++){
104 if( z[i]==quote ){
105 if( z[i+1]==quote ){
106 z[j++] = quote;
107 i++;
108 }else{
109 z[j++] = 0;
110 break;
111 }
112 }else{
113 z[j++] = z[i];
114 }
115 }
116}
117
118/*
danielk1977cc013f82006-06-24 06:36:11 +0000119** Retrieve the column names for the table named zTab via database
120** connection db. SQLITE_OK is returned on success, or an sqlite error
121** code otherwise.
122**
123** If successful, the number of columns is written to *pnCol. *paCol is
drh17435752007-08-16 04:30:38 +0000124** set to point at sqlite3_malloc()'d space containing the array of
125** nCol column names. The caller is responsible for calling sqlite3_free
danielk1977cc013f82006-06-24 06:36:11 +0000126** on *paCol.
127*/
danielk19775fac9f82006-06-13 14:16:58 +0000128static int getColumnNames(
129 sqlite3 *db,
130 const char *zTab,
131 char ***paCol,
132 int *pnCol
133){
134 char **aCol = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000135 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000136 sqlite3_stmt *pStmt = 0;
137 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19 +0000138 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000139
danielk1977cc013f82006-06-24 06:36:11 +0000140 /* Prepare the statement "SELECT * FROM <tbl>". The column names
141 ** of the result set of the compiled SELECT will be the same as
142 ** the column names of table <tbl>.
143 */
drh17435752007-08-16 04:30:38 +0000144 zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000145 if( !zSql ){
146 rc = SQLITE_NOMEM;
147 goto out;
148 }
149 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000150 sqlite3_free(zSql);
danielk1977cc013f82006-06-24 06:36:11 +0000151
danielk19775fac9f82006-06-13 14:16:58 +0000152 if( rc==SQLITE_OK ){
153 int ii;
danielk1977cc013f82006-06-24 06:36:11 +0000154 int nBytes;
155 char *zSpace;
danielk19775fac9f82006-06-13 14:16:58 +0000156 nCol = sqlite3_column_count(pStmt);
danielk1977cc013f82006-06-24 06:36:11 +0000157
158 /* Figure out how much space to allocate for the array of column names
159 ** (including space for the strings themselves). Then allocate it.
160 */
161 nBytes = sizeof(char *) * nCol;
162 for(ii=0; ii<nCol; ii++){
163 nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1);
164 }
danielk197731dad9d2007-08-16 11:36:15 +0000165 aCol = (char **)sqlite3MallocZero(nBytes);
danielk19775fac9f82006-06-13 14:16:58 +0000166 if( !aCol ){
167 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11 +0000168 goto out;
danielk19775fac9f82006-06-13 14:16:58 +0000169 }
danielk1977cc013f82006-06-24 06:36:11 +0000170
171 /* Copy the column names into the allocated space and set up the
172 ** pointers in the aCol[] array.
173 */
174 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58 +0000175 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11 +0000176 aCol[ii] = zSpace;
177 zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
178 zSpace++;
danielk19775fac9f82006-06-13 14:16:58 +0000179 }
danielk1977cc013f82006-06-24 06:36:11 +0000180 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58 +0000181 }
182
183 *paCol = aCol;
184 *pnCol = nCol;
185
danielk1977cc013f82006-06-24 06:36:11 +0000186out:
danielk19775fac9f82006-06-13 14:16:58 +0000187 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000188 return rc;
189}
190
danielk1977cc013f82006-06-24 06:36:11 +0000191/*
192** Parameter zTab is the name of a table in database db with nCol
193** columns. This function allocates an array of integers nCol in
194** size and populates it according to any implicit or explicit
195** indices on table zTab.
196**
197** If successful, SQLITE_OK is returned and *paIndex set to point
198** at the allocated array. Otherwise, an error code is returned.
199**
200** See comments associated with the member variable aIndex above
201** "struct echo_vtab" for details of the contents of the array.
202*/
203static int getIndexArray(
204 sqlite3 *db, /* Database connection */
205 const char *zTab, /* Name of table in database db */
206 int nCol,
207 int **paIndex
208){
danielk19775fac9f82006-06-13 14:16:58 +0000209 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000210 int *aIndex = 0;
211 int rc;
danielk1977cc013f82006-06-24 06:36:11 +0000212 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000213
danielk1977cc013f82006-06-24 06:36:11 +0000214 /* Allocate space for the index array */
danielk197731dad9d2007-08-16 11:36:15 +0000215 aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000216 if( !aIndex ){
217 rc = SQLITE_NOMEM;
218 goto get_index_array_out;
219 }
220
danielk1977cc013f82006-06-24 06:36:11 +0000221 /* Compile an sqlite pragma to loop through all indices on table zTab */
danielk19771e536952007-08-16 10:09:01 +0000222 zSql = sqlite3MPrintf(0, "PRAGMA index_list(%s)", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000223 if( !zSql ){
224 rc = SQLITE_NOMEM;
225 goto get_index_array_out;
226 }
227 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000228 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000229
danielk1977cc013f82006-06-24 06:36:11 +0000230 /* For each index, figure out the left-most column and set the
231 ** corresponding entry in aIndex[] to 1.
232 */
danielk19775fac9f82006-06-13 14:16:58 +0000233 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000234 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58 +0000235 sqlite3_stmt *pStmt2 = 0;
danielk19771e536952007-08-16 10:09:01 +0000236 zSql = sqlite3MPrintf(0, "PRAGMA index_info(%s)", zIdx);
danielk1977cc013f82006-06-24 06:36:11 +0000237 if( !zSql ){
238 rc = SQLITE_NOMEM;
239 goto get_index_array_out;
240 }
241 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
drh17435752007-08-16 04:30:38 +0000242 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000243 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
244 int cid = sqlite3_column_int(pStmt2, 1);
245 assert( cid>=0 && cid<nCol );
246 aIndex[cid] = 1;
247 }
danielk1977be718892006-06-23 08:05:19 +0000248 if( pStmt2 ){
249 rc = sqlite3_finalize(pStmt2);
250 }
danielk19775fac9f82006-06-13 14:16:58 +0000251 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000252 goto get_index_array_out;
253 }
254 }
255
danielk19775fac9f82006-06-13 14:16:58 +0000256
257get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000258 if( pStmt ){
259 int rc2 = sqlite3_finalize(pStmt);
260 if( rc==SQLITE_OK ){
261 rc = rc2;
262 }
263 }
danielk19775fac9f82006-06-13 14:16:58 +0000264 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000265 sqlite3_free(aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000266 aIndex = 0;
267 }
268 *paIndex = aIndex;
269 return rc;
270}
271
danielk197778efaba2006-06-12 06:09:17 +0000272/*
273** Global Tcl variable $echo_module is a list. This routine appends
274** the string element zArg to that list in interpreter interp.
275*/
drha967e882006-06-13 01:04:52 +0000276static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000277 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000278 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000279}
280
danielk19777e6ebfb2006-06-12 11:24:37 +0000281/*
282** This function is called from within the echo-modules xCreate and
283** xConnect methods. The argc and argv arguments are copies of those
284** passed to the calling method. This function is responsible for
285** calling sqlite3_declare_vtab() to declare the schema of the virtual
286** table being created or connected.
287**
288** If the constructor was passed just one argument, i.e.:
289**
290** CREATE TABLE t1 AS echo(t2);
291**
292** Then t2 is assumed to be the name of a *real* database table. The
293** schema of the virtual table is declared by passing a copy of the
294** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
295** Hence, the virtual table should have exactly the same column names and
296** types as the real table.
297*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000298static int echoDeclareVtab(
299 echo_vtab *pVtab,
danielk1977182c4ba2007-06-27 15:53:34 +0000300 sqlite3 *db
danielk1977b7a7b9a2006-06-13 10:24:42 +0000301){
danielk19777e6ebfb2006-06-12 11:24:37 +0000302 int rc = SQLITE_OK;
303
danielk1977182c4ba2007-06-27 15:53:34 +0000304 if( pVtab->zTableName ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000305 sqlite3_stmt *pStmt = 0;
danielk1977222a7572007-08-25 13:37:48 +0000306 rc = sqlite3_prepare(db,
danielk19777e6ebfb2006-06-12 11:24:37 +0000307 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
308 -1, &pStmt, 0);
danielk1977222a7572007-08-25 13:37:48 +0000309 if( rc==SQLITE_OK ){
310 sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
311 if( sqlite3_step(pStmt)==SQLITE_ROW ){
312 int rc2;
313 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
314 rc = sqlite3_declare_vtab(db, zCreateTable);
315 rc2 = sqlite3_finalize(pStmt);
316 if( rc==SQLITE_OK ){
317 rc = rc2;
318 }
319 } else {
320 rc = sqlite3_finalize(pStmt);
321 if( rc==SQLITE_OK ){
322 rc = SQLITE_ERROR;
323 }
324 }
danielk19777fa5dd12007-04-18 17:04:00 +0000325 if( rc==SQLITE_OK ){
danielk1977222a7572007-08-25 13:37:48 +0000326 rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
danielk19777fa5dd12007-04-18 17:04:00 +0000327 }
danielk1977222a7572007-08-25 13:37:48 +0000328 if( rc==SQLITE_OK ){
329 rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
danielk1977be718892006-06-23 08:05:19 +0000330 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000331 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000332 }
333
334 return rc;
335}
336
danielk1977cc013f82006-06-24 06:36:11 +0000337/*
338** This function frees all runtime structures associated with the virtual
339** table pVtab.
340*/
danielk1977a4e76362006-06-14 06:31:28 +0000341static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000342 echo_vtab *p = (echo_vtab*)pVtab;
drh17435752007-08-16 04:30:38 +0000343 sqlite3_free(p->aIndex);
344 sqlite3_free(p->aCol);
345 sqlite3_free(p->zThis);
346 sqlite3_free(p->zTableName);
347 sqlite3_free(p->zLogName);
348 sqlite3_free(p);
danielk1977a4e76362006-06-14 06:31:28 +0000349 return 0;
350}
351
danielk1977860b6cd2007-09-03 11:51:50 +0000352typedef struct EchoModule EchoModule;
353struct EchoModule {
354 Tcl_Interp *interp;
355};
356
danielk1977cc013f82006-06-24 06:36:11 +0000357/*
358** This function is called to do the work of the xConnect() method -
359** to allocate the required in-memory structures for a newly connected
360** virtual table.
361*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000362static int echoConstructor(
363 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000364 void *pAux,
drhe4102962006-09-11 00:34:22 +0000365 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000366 sqlite3_vtab **ppVtab,
367 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000368){
danielk1977a1644fd2007-08-29 12:31:25 +0000369 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000370 int i;
371 echo_vtab *pVtab;
372
danielk1977cc013f82006-06-24 06:36:11 +0000373 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk197731dad9d2007-08-16 11:36:15 +0000374 pVtab = sqlite3MallocZero( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000375 if( !pVtab ){
376 return SQLITE_NOMEM;
377 }
danielk1977860b6cd2007-09-03 11:51:50 +0000378 pVtab->interp = ((EchoModule *)pAux)->interp;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000379 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000380
danielk1977182c4ba2007-06-27 15:53:34 +0000381 /* Allocate echo_vtab.zThis */
danielk19771e536952007-08-16 10:09:01 +0000382 pVtab->zThis = sqlite3MPrintf(0, "%s", argv[2]);
danielk1977182c4ba2007-06-27 15:53:34 +0000383 if( !pVtab->zThis ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000384 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000385 return SQLITE_NOMEM;
386 }
387
danielk1977182c4ba2007-06-27 15:53:34 +0000388 /* Allocate echo_vtab.zTableName */
389 if( argc>3 ){
danielk19771e536952007-08-16 10:09:01 +0000390 pVtab->zTableName = sqlite3MPrintf(0, "%s", argv[3]);
danielk1977182c4ba2007-06-27 15:53:34 +0000391 dequoteString(pVtab->zTableName);
392 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
danielk19771e536952007-08-16 10:09:01 +0000393 char *z = sqlite3MPrintf(0, "%s%s", argv[2], &(pVtab->zTableName[1]));
drh17435752007-08-16 04:30:38 +0000394 sqlite3_free(pVtab->zTableName);
danielk1977182c4ba2007-06-27 15:53:34 +0000395 pVtab->zTableName = z;
396 pVtab->isPattern = 1;
397 }
398 if( !pVtab->zTableName ){
399 echoDestructor((sqlite3_vtab *)pVtab);
400 return SQLITE_NOMEM;
401 }
402 }
403
danielk1977cc013f82006-06-24 06:36:11 +0000404 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000405 for(i=0; i<argc; i++){
406 appendToEchoModule(pVtab->interp, argv[i]);
407 }
408
danielk1977cc013f82006-06-24 06:36:11 +0000409 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
410 ** structure. If an error occurs, delete the sqlite3_vtab structure and
411 ** return an error code.
412 */
danielk1977a1644fd2007-08-29 12:31:25 +0000413 rc = echoDeclareVtab(pVtab, db);
414 if( rc!=SQLITE_OK ){
danielk1977a4e76362006-06-14 06:31:28 +0000415 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977a1644fd2007-08-29 12:31:25 +0000416 return rc;
danielk1977a4e76362006-06-14 06:31:28 +0000417 }
418
danielk1977cc013f82006-06-24 06:36:11 +0000419 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000420 *ppVtab = &pVtab->base;
421 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000422}
drha967e882006-06-13 01:04:52 +0000423
danielk1977cc013f82006-06-24 06:36:11 +0000424/*
425** Echo virtual table module xCreate method.
426*/
drhb9bb7c12006-06-11 23:41:55 +0000427static int echoCreate(
428 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000429 void *pAux,
drhe4102962006-09-11 00:34:22 +0000430 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000431 sqlite3_vtab **ppVtab,
432 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000433){
danielk1977a298e902006-06-22 09:53:48 +0000434 int rc = SQLITE_OK;
danielk1977860b6cd2007-09-03 11:51:50 +0000435 appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000436 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000437
438 /* If there were two arguments passed to the module at the SQL level
439 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
440 ** the second argument is used as a table name. Attempt to create
441 ** such a table with a single column, "logmsg". This table will
442 ** be used to log calls to the xUpdate method. It will be deleted
443 ** when the virtual table is DROPed.
444 **
445 ** Note: The main point of this is to test that we can drop tables
446 ** from within an xDestroy method call.
447 */
danielk1977a298e902006-06-22 09:53:48 +0000448 if( rc==SQLITE_OK && argc==5 ){
449 char *zSql;
450 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
danielk19771e536952007-08-16 10:09:01 +0000451 pVtab->zLogName = sqlite3MPrintf(0, "%s", argv[4]);
452 zSql = sqlite3MPrintf(0, "CREATE TABLE %Q(logmsg)", pVtab->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000453 rc = sqlite3_exec(db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000454 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000455 }
danielk1977cc013f82006-06-24 06:36:11 +0000456
danielk1977a298e902006-06-22 09:53:48 +0000457 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000458}
danielk1977cc013f82006-06-24 06:36:11 +0000459
460/*
461** Echo virtual table module xConnect method.
462*/
drhb9bb7c12006-06-11 23:41:55 +0000463static int echoConnect(
464 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000465 void *pAux,
drhe4102962006-09-11 00:34:22 +0000466 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000467 sqlite3_vtab **ppVtab,
468 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000469){
danielk1977860b6cd2007-09-03 11:51:50 +0000470 appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000471 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000472}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000473
danielk1977cc013f82006-06-24 06:36:11 +0000474/*
475** Echo virtual table module xDisconnect method.
476*/
danielk19775fac9f82006-06-13 14:16:58 +0000477static int echoDisconnect(sqlite3_vtab *pVtab){
478 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
479 return echoDestructor(pVtab);
480}
danielk1977cc013f82006-06-24 06:36:11 +0000481
482/*
483** Echo virtual table module xDestroy method.
484*/
danielk19775fac9f82006-06-13 14:16:58 +0000485static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000486 int rc = SQLITE_OK;
487 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000488 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000489
490 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000491 if( p && p->zLogName ){
492 char *zSql;
danielk19771e536952007-08-16 10:09:01 +0000493 zSql = sqlite3MPrintf(0, "DROP TABLE %Q", p->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000494 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000495 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000496 }
danielk1977cc013f82006-06-24 06:36:11 +0000497
danielk1977a298e902006-06-22 09:53:48 +0000498 if( rc==SQLITE_OK ){
499 rc = echoDestructor(pVtab);
500 }
501 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000502}
503
danielk1977cc013f82006-06-24 06:36:11 +0000504/*
505** Echo virtual table module xOpen method.
506*/
danielk19775fac9f82006-06-13 14:16:58 +0000507static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000508 echo_cursor *pCur;
danielk197731dad9d2007-08-16 11:36:15 +0000509 pCur = sqlite3MallocZero(sizeof(echo_cursor));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000510 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000511 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000512}
513
danielk1977cc013f82006-06-24 06:36:11 +0000514/*
515** Echo virtual table module xClose method.
516*/
danielk19775fac9f82006-06-13 14:16:58 +0000517static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000518 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000519 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000520 sqlite3_stmt *pStmt = pCur->pStmt;
521 pCur->pStmt = 0;
drh17435752007-08-16 04:30:38 +0000522 sqlite3_free(pCur);
danielk1977be718892006-06-23 08:05:19 +0000523 rc = sqlite3_finalize(pStmt);
524 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000525}
526
danielk1977a298e902006-06-22 09:53:48 +0000527/*
528** Return non-zero if the cursor does not currently point to a valid record
529** (i.e if the scan has finished), or zero otherwise.
530*/
531static int echoEof(sqlite3_vtab_cursor *cur){
532 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
533}
534
danielk1977cc013f82006-06-24 06:36:11 +0000535/*
536** Echo virtual table module xNext method.
537*/
danielk19775fac9f82006-06-13 14:16:58 +0000538static int echoNext(sqlite3_vtab_cursor *cur){
danielk197731dad9d2007-08-16 11:36:15 +0000539 int rc = SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000540 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000541
danielk197731dad9d2007-08-16 11:36:15 +0000542 if( pCur->pStmt ){
543 rc = sqlite3_step(pCur->pStmt);
544 if( rc==SQLITE_ROW ){
545 rc = SQLITE_OK;
546 }else{
547 rc = sqlite3_finalize(pCur->pStmt);
548 pCur->pStmt = 0;
549 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000550 }
551
552 return rc;
553}
554
danielk1977cc013f82006-06-24 06:36:11 +0000555/*
556** Echo virtual table module xColumn method.
557*/
danielk19775fac9f82006-06-13 14:16:58 +0000558static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000559 int iCol = i + 1;
560 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk1977fbbe0052006-06-21 07:02:33 +0000561 if( !pStmt ){
562 sqlite3_result_null(ctx);
563 }else{
564 assert( sqlite3_data_count(pStmt)>iCol );
565 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
566 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000567 return SQLITE_OK;
568}
569
danielk1977cc013f82006-06-24 06:36:11 +0000570/*
571** Echo virtual table module xRowid method.
572*/
danielk19775fac9f82006-06-13 14:16:58 +0000573static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000574 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
575 *pRowid = sqlite3_column_int64(pStmt, 0);
576 return SQLITE_OK;
577}
578
danielk197798331532006-06-14 10:55:52 +0000579/*
580** Compute a simple hash of the null terminated string zString.
581**
582** This module uses only sqlite3_index_info.idxStr, not
583** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
584** in echoBestIndex(), idxNum is set to the corresponding hash value.
585** In echoFilter(), code assert()s that the supplied idxNum value is
586** indeed the hash of the supplied idxStr.
587*/
588static int hashString(const char *zString){
589 int val = 0;
590 int ii;
591 for(ii=0; zString[ii]; ii++){
592 val = (val << 3) + (int)zString[ii];
593 }
594 return val;
595}
596
danielk1977cc013f82006-06-24 06:36:11 +0000597/*
598** Echo virtual table module xFilter method.
599*/
danielk19775fac9f82006-06-13 14:16:58 +0000600static int echoFilter(
601 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000602 int idxNum, const char *idxStr,
603 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000604){
605 int rc;
drh4be8b512006-06-13 23:51:34 +0000606 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000607
608 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
609 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
610 sqlite3 *db = pVtab->db;
611
danielk1977cc013f82006-06-24 06:36:11 +0000612 /* Check that idxNum matches idxStr */
613 assert( idxNum==hashString(idxStr) );
614
615 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000616 appendToEchoModule(pVtab->interp, "xFilter");
617 appendToEchoModule(pVtab->interp, idxStr);
618 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000619 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000620 }
621
danielk19775fac9f82006-06-13 14:16:58 +0000622 sqlite3_finalize(pCur->pStmt);
623 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000624
625 /* Prepare the SQL statement created by echoBestIndex and bind the
626 ** runtime parameters passed to this function to it.
627 */
drh4be8b512006-06-13 23:51:34 +0000628 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000629 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000630 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk1977cc013f82006-06-24 06:36:11 +0000631 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000632 }
danielk1977cc013f82006-06-24 06:36:11 +0000633
634 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000635 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000636 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000637 }
638
danielk1977be718892006-06-23 08:05:19 +0000639 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000640}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000641
danielk1977cc013f82006-06-24 06:36:11 +0000642
643/*
644** A helper function used by echoUpdate() and echoBestIndex() for
645** manipulating strings in concert with the sqlite3_mprintf() function.
646**
647** Parameter pzStr points to a pointer to a string allocated with
648** sqlite3_mprintf. The second parameter, zAppend, points to another
649** string. The two strings are concatenated together and *pzStr
650** set to point at the result. The initial buffer pointed to by *pzStr
651** is deallocated via sqlite3_free().
652**
653** If the third argument, doFree, is true, then sqlite3_free() is
654** also called to free the buffer pointed to by zAppend.
655*/
danielk1977a1644fd2007-08-29 12:31:25 +0000656static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
danielk1977cc013f82006-06-24 06:36:11 +0000657 char *zIn = *pzStr;
danielk1977a1644fd2007-08-29 12:31:25 +0000658 if( !zAppend && doFree && *pRc==SQLITE_OK ){
659 *pRc = SQLITE_NOMEM;
660 }
661 if( *pRc!=SQLITE_OK ){
662 sqlite3_free(zIn);
663 zIn = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000664 }else{
danielk1977a1644fd2007-08-29 12:31:25 +0000665 if( zIn ){
666 char *zTemp = zIn;
667 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
668 sqlite3_free(zTemp);
669 }else{
670 zIn = sqlite3_mprintf("%s", zAppend);
671 }
672 if( !zIn ){
673 *pRc = SQLITE_NOMEM;
674 }
danielk1977cc013f82006-06-24 06:36:11 +0000675 }
676 *pzStr = zIn;
677 if( doFree ){
678 sqlite3_free(zAppend);
679 }
680}
681
drhb9bb7c12006-06-11 23:41:55 +0000682/*
danielk19775fac9f82006-06-13 14:16:58 +0000683** The echo module implements the subset of query constraints and sort
684** orders that may take advantage of SQLite indices on the underlying
685** real table. For example, if the real table is declared as:
686**
687** CREATE TABLE real(a, b, c);
688** CREATE INDEX real_index ON real(b);
689**
690** then the echo module handles WHERE or ORDER BY clauses that refer
691** to the column "b", but not "a" or "c". If a multi-column index is
692** present, only it's left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000693**
694** This xBestIndex method encodes the proposed search strategy as
695** an SQL query on the real table underlying the virtual echo module
696** table and stores the query in sqlite3_index_info.idxStr. The SQL
697** statement is of the form:
698**
699** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
700**
701** where the <where-clause> and <order-by-clause> are determined
702** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000703*/
danielk19775fac9f82006-06-13 14:16:58 +0000704static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
705 int ii;
drh4be8b512006-06-13 23:51:34 +0000706 char *zQuery = 0;
707 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000708 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000709 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000710 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000711 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000712 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000713
714 int nRow;
715 int useIdx = 0;
716 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000717 int useCost = 0;
718 double cost;
danielk197774cdba42006-06-19 12:02:58 +0000719
720 /* Determine the number of rows in the table and store this value in local
721 ** variable nRow. The 'estimated-cost' of the scan will be the number of
722 ** rows in the table for a linear scan, or the log (base 2) of the
723 ** number of rows if the proposed scan uses an index.
724 */
danielk1977780b1d92007-03-30 14:56:34 +0000725 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
726 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
727 useCost = 1;
728 } else {
729 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000730 if( !zQuery ){
731 return SQLITE_NOMEM;
732 }
danielk1977780b1d92007-03-30 14:56:34 +0000733 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
734 sqlite3_free(zQuery);
735 if( rc!=SQLITE_OK ){
736 return rc;
737 }
738 sqlite3_step(pStmt);
739 nRow = sqlite3_column_int(pStmt, 0);
740 rc = sqlite3_finalize(pStmt);
741 if( rc!=SQLITE_OK ){
742 return rc;
743 }
danielk197774cdba42006-06-19 12:02:58 +0000744 }
danielk19775fac9f82006-06-13 14:16:58 +0000745
drh4be8b512006-06-13 23:51:34 +0000746 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000747 if( !zQuery ){
748 return SQLITE_NOMEM;
749 }
danielk19775fac9f82006-06-13 14:16:58 +0000750 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
751 const struct sqlite3_index_constraint *pConstraint;
752 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000753 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000754
755 pConstraint = &pIdxInfo->aConstraint[ii];
756 pUsage = &pIdxInfo->aConstraintUsage[ii];
757
drh383736b2006-10-08 18:56:57 +0000758 iCol = pConstraint->iColumn;
danielk19775fac9f82006-06-13 14:16:58 +0000759 if( pVtab->aIndex[iCol] ){
760 char *zCol = pVtab->aCol[iCol];
761 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000762 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000763 if( iCol<0 ){
764 zCol = "rowid";
765 }
danielk19775fac9f82006-06-13 14:16:58 +0000766 switch( pConstraint->op ){
767 case SQLITE_INDEX_CONSTRAINT_EQ:
768 zOp = "="; break;
769 case SQLITE_INDEX_CONSTRAINT_LT:
770 zOp = "<"; break;
771 case SQLITE_INDEX_CONSTRAINT_GT:
772 zOp = ">"; break;
773 case SQLITE_INDEX_CONSTRAINT_LE:
774 zOp = "<="; break;
775 case SQLITE_INDEX_CONSTRAINT_GE:
776 zOp = ">="; break;
777 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000778 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000779 }
drh1a90e092006-06-14 22:07:10 +0000780 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000781 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
782 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000783 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000784 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000785 }
danielk1977a1644fd2007-08-29 12:31:25 +0000786 string_concat(&zQuery, zNew, 1, &rc);
danielk1977cc013f82006-06-24 06:36:11 +0000787
drh4be8b512006-06-13 23:51:34 +0000788 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000789 pUsage->argvIndex = ++nArg;
790 pUsage->omit = 1;
791 }
792 }
danielk197747d08092006-06-14 07:41:31 +0000793
794 /* If there is only one term in the ORDER BY clause, and it is
795 ** on a column that this virtual table has an index for, then consume
796 ** the ORDER BY clause.
797 */
798 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000799 int iCol = pIdxInfo->aOrderBy->iColumn;
800 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000801 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000802 if( iCol<0 ){
803 zCol = "rowid";
804 }
danielk1977cc013f82006-06-24 06:36:11 +0000805 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
danielk1977a1644fd2007-08-29 12:31:25 +0000806 string_concat(&zQuery, zNew, 1, &rc);
danielk197747d08092006-06-14 07:41:31 +0000807 pIdxInfo->orderByConsumed = 1;
808 }
809
danielk19775fac9f82006-06-13 14:16:58 +0000810 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000811 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000812
danielk1977222a7572007-08-25 13:37:48 +0000813 if( !zQuery ){
danielk1977a1644fd2007-08-29 12:31:25 +0000814 return rc;
danielk1977222a7572007-08-25 13:37:48 +0000815 }
danielk197798331532006-06-14 10:55:52 +0000816 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000817 pIdxInfo->idxStr = zQuery;
818 pIdxInfo->needToFreeIdxStr = 1;
danielk1977780b1d92007-03-30 14:56:34 +0000819 if (useCost) {
820 pIdxInfo->estimatedCost = cost;
821 } else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000822 /* Approximation of log2(nRow). */
823 for( ii=0; ii<(sizeof(int)*8); ii++ ){
824 if( nRow & (1<<ii) ){
825 pIdxInfo->estimatedCost = (double)ii;
826 }
827 }
828 } else {
829 pIdxInfo->estimatedCost = (double)nRow;
830 }
831 return rc;
drha967e882006-06-13 01:04:52 +0000832}
833
danielk1977176f4d22006-06-15 10:41:15 +0000834/*
danielk1977cc013f82006-06-24 06:36:11 +0000835** The xUpdate method for echo module virtual tables.
836**
danielk1977176f4d22006-06-15 10:41:15 +0000837** apData[0] apData[1] apData[2..]
838**
839** INTEGER DELETE
840**
841** INTEGER NULL (nCol args) UPDATE (do not set rowid)
842** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
843**
844** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
845** NULL INTEGER (nCol args) INSERT (incl. rowid value)
846**
847*/
danielk19771f6eec52006-06-16 06:17:47 +0000848int echoUpdate(
849 sqlite3_vtab *tab,
850 int nData,
851 sqlite3_value **apData,
852 sqlite_int64 *pRowid
853){
danielk197726e41442006-06-14 15:16:35 +0000854 echo_vtab *pVtab = (echo_vtab *)tab;
855 sqlite3 *db = pVtab->db;
856 int rc = SQLITE_OK;
857
danielk1977176f4d22006-06-15 10:41:15 +0000858 sqlite3_stmt *pStmt;
859 char *z = 0; /* SQL statement to execute */
860 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
861 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
862 int i; /* Counter variable used by for loops */
863
danielk197726e41442006-06-14 15:16:35 +0000864 assert( nData==pVtab->nCol+2 || nData==1 );
865
drh5aec0422006-06-14 23:43:31 +0000866 /* If apData[0] is an integer and nData>1 then do an UPDATE */
867 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000868 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000869 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000870 if( !z ){
871 rc = SQLITE_NOMEM;
872 }
danielk1977176f4d22006-06-15 10:41:15 +0000873
874 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
875 bindArgZero = 1;
876
877 if( bindArgOne ){
danielk1977a1644fd2007-08-29 12:31:25 +0000878 string_concat(&z, " SET rowid=?1 ", 0, &rc);
drh5aec0422006-06-14 23:43:31 +0000879 zSep = ",";
880 }
881 for(i=2; i<nData; i++){
882 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000883 string_concat(&z, sqlite3_mprintf(
danielk1977a1644fd2007-08-29 12:31:25 +0000884 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000885 zSep = ",";
886 }
danielk1977a1644fd2007-08-29 12:31:25 +0000887 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000888 }
889
danielk1977176f4d22006-06-15 10:41:15 +0000890 /* If apData[0] is an integer and nData==1 then do a DELETE */
891 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
892 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000893 if( !z ){
894 rc = SQLITE_NOMEM;
895 }
danielk1977176f4d22006-06-15 10:41:15 +0000896 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000897 }
898
danielk1977176f4d22006-06-15 10:41:15 +0000899 /* If the first argument is NULL and there are more than two args, INSERT */
900 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000901 int ii;
902 char *zInsert = 0;
903 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000904
danielk19774b2688a2006-06-20 11:01:07 +0000905 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000906 if( !zInsert ){
907 rc = SQLITE_NOMEM;
908 }
danielk1977176f4d22006-06-15 10:41:15 +0000909 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
910 bindArgOne = 1;
911 zValues = sqlite3_mprintf("?");
danielk1977a1644fd2007-08-29 12:31:25 +0000912 string_concat(&zInsert, "rowid", 0, &rc);
danielk197726e41442006-06-14 15:16:35 +0000913 }
914
danielk1977176f4d22006-06-15 10:41:15 +0000915 assert((pVtab->nCol+2)==nData);
916 for(ii=2; ii<nData; ii++){
917 string_concat(&zInsert,
danielk1977a1644fd2007-08-29 12:31:25 +0000918 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
danielk1977176f4d22006-06-15 10:41:15 +0000919 string_concat(&zValues,
danielk1977a1644fd2007-08-29 12:31:25 +0000920 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
danielk197726e41442006-06-14 15:16:35 +0000921 }
922
danielk1977a1644fd2007-08-29 12:31:25 +0000923 string_concat(&z, zInsert, 1, &rc);
924 string_concat(&z, ") VALUES(", 0, &rc);
925 string_concat(&z, zValues, 1, &rc);
926 string_concat(&z, ")", 0, &rc);
danielk1977176f4d22006-06-15 10:41:15 +0000927 }
928
929 /* Anything else is an error */
930 else{
931 assert(0);
932 return SQLITE_ERROR;
933 }
934
danielk1977a1644fd2007-08-29 12:31:25 +0000935 if( rc==SQLITE_OK ){
936 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
937 }
danielk1977176f4d22006-06-15 10:41:15 +0000938 assert( rc!=SQLITE_OK || pStmt );
939 sqlite3_free(z);
940 if( rc==SQLITE_OK ) {
941 if( bindArgZero ){
942 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000943 }
danielk1977176f4d22006-06-15 10:41:15 +0000944 if( bindArgOne ){
945 sqlite3_bind_value(pStmt, 1, apData[1]);
946 }
947 for(i=2; i<nData; i++){
948 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
949 }
950 sqlite3_step(pStmt);
951 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000952 }
953
danielk19771f6eec52006-06-16 06:17:47 +0000954 if( pRowid && rc==SQLITE_OK ){
955 *pRowid = sqlite3_last_insert_rowid(db);
956 }
957
danielk197726e41442006-06-14 15:16:35 +0000958 return rc;
959}
960
drha967e882006-06-13 01:04:52 +0000961/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000962** xBegin, xSync, xCommit and xRollback callbacks for echo module
963** virtual tables. Do nothing other than add the name of the callback
964** to the $::echo_module Tcl variable.
965*/
966static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
967 char *z;
968 echo_vtab *pVtab = (echo_vtab *)tab;
969 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
970 appendToEchoModule(pVtab->interp, zCall);
971 appendToEchoModule(pVtab->interp, z);
972 sqlite3_free(z);
danielk1977a1644fd2007-08-29 12:31:25 +0000973 return (z?SQLITE_OK:SQLITE_NOMEM);
danielk1977c69cdfd2006-06-17 09:39:55 +0000974}
975static int echoBegin(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +0000976 int rc;
danielk19775017dc32006-06-24 09:34:22 +0000977 echo_vtab *pVtab = (echo_vtab *)tab;
978 Tcl_Interp *interp = pVtab->interp;
979 const char *zVal;
980
danielk1977a1644fd2007-08-29 12:31:25 +0000981 rc = echoTransactionCall(tab, "xBegin");
danielk19775017dc32006-06-24 09:34:22 +0000982
danielk1977a1644fd2007-08-29 12:31:25 +0000983 if( rc==SQLITE_OK ){
984 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
985 ** and it is set to the name of the real table underlying this virtual
986 ** echo module table, then cause this xSync operation to fail.
987 */
988 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
989 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
990 rc = SQLITE_ERROR;
991 }
danielk19775017dc32006-06-24 09:34:22 +0000992 }
danielk1977a1644fd2007-08-29 12:31:25 +0000993 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +0000994}
995static int echoSync(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +0000996 int rc;
danielk1977c69cdfd2006-06-17 09:39:55 +0000997 echo_vtab *pVtab = (echo_vtab *)tab;
998 Tcl_Interp *interp = pVtab->interp;
999 const char *zVal;
1000
danielk1977a1644fd2007-08-29 12:31:25 +00001001 rc = echoTransactionCall(tab, "xSync");
danielk1977c69cdfd2006-06-17 09:39:55 +00001002
danielk1977a1644fd2007-08-29 12:31:25 +00001003 if( rc==SQLITE_OK ){
1004 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
1005 ** and it is set to the name of the real table underlying this virtual
1006 ** echo module table, then cause this xSync operation to fail.
1007 */
1008 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
1009 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1010 rc = -1;
1011 }
danielk1977c69cdfd2006-06-17 09:39:55 +00001012 }
danielk1977a1644fd2007-08-29 12:31:25 +00001013 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001014}
1015static int echoCommit(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001016 sqlite3MallocBenignFailure(1);
danielk1977c69cdfd2006-06-17 09:39:55 +00001017 return echoTransactionCall(tab, "xCommit");
1018}
1019static int echoRollback(sqlite3_vtab *tab){
1020 return echoTransactionCall(tab, "xRollback");
1021}
1022
1023/*
drhe94b0c32006-07-08 18:09:15 +00001024** Implementation of "GLOB" function on the echo module. Pass
1025** all arguments to the ::echo_glob_overload procedure of TCL
1026** and return the result of that procedure as a string.
1027*/
1028static void overloadedGlobFunction(
1029 sqlite3_context *pContext,
1030 int nArg,
1031 sqlite3_value **apArg
1032){
1033 Tcl_Interp *interp = sqlite3_user_data(pContext);
1034 Tcl_DString str;
1035 int i;
1036 int rc;
1037 Tcl_DStringInit(&str);
1038 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
1039 for(i=0; i<nArg; i++){
1040 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
1041 }
1042 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
1043 Tcl_DStringFree(&str);
1044 if( rc ){
1045 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
1046 }else{
1047 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1048 -1, SQLITE_TRANSIENT);
1049 }
1050 Tcl_ResetResult(interp);
1051}
1052
1053/*
1054** This is the xFindFunction implementation for the echo module.
1055** SQLite calls this routine when the first argument of a function
1056** is a column of an echo virtual table. This routine can optionally
1057** override the implementation of that function. It will choose to
1058** do so if the function is named "glob", and a TCL command named
1059** ::echo_glob_overload exists.
1060*/
1061static int echoFindFunction(
1062 sqlite3_vtab *vtab,
1063 int nArg,
1064 const char *zFuncName,
1065 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1066 void **ppArg
1067){
1068 echo_vtab *pVtab = (echo_vtab *)vtab;
1069 Tcl_Interp *interp = pVtab->interp;
1070 Tcl_CmdInfo info;
1071 if( strcmp(zFuncName,"glob")!=0 ){
1072 return 0;
1073 }
1074 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1075 return 0;
1076 }
1077 *pxFunc = overloadedGlobFunction;
1078 *ppArg = interp;
1079 return 1;
1080}
1081
danielk1977182c4ba2007-06-27 15:53:34 +00001082static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1083 int rc = SQLITE_OK;
1084 echo_vtab *p = (echo_vtab *)vtab;
1085
1086 if( p->isPattern ){
1087 int nThis = strlen(p->zThis);
danielk19771e536952007-08-16 10:09:01 +00001088 char *zSql = sqlite3MPrintf(0, "ALTER TABLE %s RENAME TO %s%s",
danielk1977182c4ba2007-06-27 15:53:34 +00001089 p->zTableName, zNewName, &p->zTableName[nThis]
1090 );
1091 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +00001092 sqlite3_free(zSql);
danielk1977182c4ba2007-06-27 15:53:34 +00001093 }
1094
1095 return rc;
1096}
1097
drhe94b0c32006-07-08 18:09:15 +00001098/*
danielk1977cc013f82006-06-24 06:36:11 +00001099** A virtual table module that merely "echos" the contents of another
1100** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +00001101*/
1102static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +00001103 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +00001104 echoCreate,
1105 echoConnect,
drha967e882006-06-13 01:04:52 +00001106 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +00001107 echoDisconnect,
1108 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +00001109 echoOpen, /* xOpen - open a cursor */
1110 echoClose, /* xClose - close a cursor */
1111 echoFilter, /* xFilter - configure scan constraints */
1112 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +00001113 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +00001114 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +00001115 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +00001116 echoUpdate, /* xUpdate - write data */
1117 echoBegin, /* xBegin - begin transaction */
1118 echoSync, /* xSync - sync transaction */
1119 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +00001120 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +00001121 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:34 +00001122 echoRename, /* xRename - rename the table */
drhb9bb7c12006-06-11 23:41:55 +00001123};
1124
1125/*
1126** Decode a pointer to an sqlite3 object.
1127*/
1128static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
1129 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
1130 return TCL_OK;
1131}
1132
danielk1977860b6cd2007-09-03 11:51:50 +00001133static void moduleDestroy(void *p){
1134 sqlite3_free(p);
1135}
1136
drhb9bb7c12006-06-11 23:41:55 +00001137/*
1138** Register the echo virtual table module.
1139*/
1140static int register_echo_module(
1141 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1142 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1143 int objc, /* Number of arguments */
1144 Tcl_Obj *CONST objv[] /* Command arguments */
1145){
1146 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:50 +00001147 EchoModule *pMod;
drhb9bb7c12006-06-11 23:41:55 +00001148 if( objc!=2 ){
1149 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1150 return TCL_ERROR;
1151 }
1152 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977860b6cd2007-09-03 11:51:50 +00001153 pMod = sqlite3_malloc(sizeof(EchoModule));
1154 pMod->interp = interp;
1155 sqlite3_create_module_v2(db, "echo", &echoModule, (void*)pMod, moduleDestroy);
drhb9bb7c12006-06-11 23:41:55 +00001156 return TCL_OK;
1157}
1158
danielk19775017dc32006-06-24 09:34:22 +00001159/*
1160** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1161**
1162** sqlite3_declare_vtab DB SQL
1163*/
1164static int declare_vtab(
1165 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1166 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1167 int objc, /* Number of arguments */
1168 Tcl_Obj *CONST objv[] /* Command arguments */
1169){
1170 sqlite3 *db;
1171 int rc;
1172 if( objc!=3 ){
1173 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1174 return TCL_ERROR;
1175 }
1176 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1177 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1178 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001179 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001180 return TCL_ERROR;
1181 }
1182 return TCL_OK;
1183}
1184
danielk1977cc013f82006-06-24 06:36:11 +00001185#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001186
1187/*
1188** Register commands with the TCL interpreter.
1189*/
1190int Sqlitetest8_Init(Tcl_Interp *interp){
1191 static struct {
1192 char *zName;
1193 Tcl_ObjCmdProc *xProc;
1194 void *clientData;
1195 } aObjCmd[] = {
danielk1977cc013f82006-06-24 06:36:11 +00001196#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001197 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +00001198 { "sqlite3_declare_vtab", declare_vtab, 0 },
danielk1977cc013f82006-06-24 06:36:11 +00001199#endif
drhb9bb7c12006-06-11 23:41:55 +00001200 };
1201 int i;
1202 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1203 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1204 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1205 }
1206 return TCL_OK;
1207}