blob: 8c49571afa206707b0db8d3add2929ca34d20341 [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**
danielk1977a7a8e142008-02-13 18:25:27 +000016** $Id: test8.c,v 1.60 2008/02/13 18:25:27 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++){
danielk1977a7a8e142008-02-13 18:25:27 +0000163 const char *zName = sqlite3_column_name(pStmt, ii);
164 if( !zName ){
165 rc = SQLITE_NOMEM;
166 goto out;
167 }
168 nBytes += strlen(zName)+1;
danielk1977cc013f82006-06-24 06:36:11 +0000169 }
danielk197731dad9d2007-08-16 11:36:15 +0000170 aCol = (char **)sqlite3MallocZero(nBytes);
danielk19775fac9f82006-06-13 14:16:58 +0000171 if( !aCol ){
172 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11 +0000173 goto out;
danielk19775fac9f82006-06-13 14:16:58 +0000174 }
danielk1977cc013f82006-06-24 06:36:11 +0000175
176 /* Copy the column names into the allocated space and set up the
177 ** pointers in the aCol[] array.
178 */
179 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58 +0000180 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11 +0000181 aCol[ii] = zSpace;
182 zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
183 zSpace++;
danielk19775fac9f82006-06-13 14:16:58 +0000184 }
danielk1977cc013f82006-06-24 06:36:11 +0000185 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58 +0000186 }
187
188 *paCol = aCol;
189 *pnCol = nCol;
190
danielk1977cc013f82006-06-24 06:36:11 +0000191out:
danielk19775fac9f82006-06-13 14:16:58 +0000192 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000193 return rc;
194}
195
danielk1977cc013f82006-06-24 06:36:11 +0000196/*
197** Parameter zTab is the name of a table in database db with nCol
198** columns. This function allocates an array of integers nCol in
199** size and populates it according to any implicit or explicit
200** indices on table zTab.
201**
202** If successful, SQLITE_OK is returned and *paIndex set to point
203** at the allocated array. Otherwise, an error code is returned.
204**
205** See comments associated with the member variable aIndex above
206** "struct echo_vtab" for details of the contents of the array.
207*/
208static int getIndexArray(
209 sqlite3 *db, /* Database connection */
210 const char *zTab, /* Name of table in database db */
211 int nCol,
212 int **paIndex
213){
danielk19775fac9f82006-06-13 14:16:58 +0000214 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000215 int *aIndex = 0;
216 int rc;
danielk1977cc013f82006-06-24 06:36:11 +0000217 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000218
danielk1977cc013f82006-06-24 06:36:11 +0000219 /* Allocate space for the index array */
danielk197731dad9d2007-08-16 11:36:15 +0000220 aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000221 if( !aIndex ){
222 rc = SQLITE_NOMEM;
223 goto get_index_array_out;
224 }
225
danielk1977cc013f82006-06-24 06:36:11 +0000226 /* Compile an sqlite pragma to loop through all indices on table zTab */
danielk19771e536952007-08-16 10:09:01 +0000227 zSql = sqlite3MPrintf(0, "PRAGMA index_list(%s)", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000228 if( !zSql ){
229 rc = SQLITE_NOMEM;
230 goto get_index_array_out;
231 }
232 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000233 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000234
danielk1977cc013f82006-06-24 06:36:11 +0000235 /* For each index, figure out the left-most column and set the
236 ** corresponding entry in aIndex[] to 1.
237 */
danielk19775fac9f82006-06-13 14:16:58 +0000238 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000239 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58 +0000240 sqlite3_stmt *pStmt2 = 0;
danielk19771e536952007-08-16 10:09:01 +0000241 zSql = sqlite3MPrintf(0, "PRAGMA index_info(%s)", zIdx);
danielk1977cc013f82006-06-24 06:36:11 +0000242 if( !zSql ){
243 rc = SQLITE_NOMEM;
244 goto get_index_array_out;
245 }
246 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
drh17435752007-08-16 04:30:38 +0000247 sqlite3_free(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000248 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
249 int cid = sqlite3_column_int(pStmt2, 1);
250 assert( cid>=0 && cid<nCol );
251 aIndex[cid] = 1;
252 }
danielk1977be718892006-06-23 08:05:19 +0000253 if( pStmt2 ){
254 rc = sqlite3_finalize(pStmt2);
255 }
danielk19775fac9f82006-06-13 14:16:58 +0000256 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000257 goto get_index_array_out;
258 }
259 }
260
danielk19775fac9f82006-06-13 14:16:58 +0000261
262get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000263 if( pStmt ){
264 int rc2 = sqlite3_finalize(pStmt);
265 if( rc==SQLITE_OK ){
266 rc = rc2;
267 }
268 }
danielk19775fac9f82006-06-13 14:16:58 +0000269 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000270 sqlite3_free(aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000271 aIndex = 0;
272 }
273 *paIndex = aIndex;
274 return rc;
275}
276
danielk197778efaba2006-06-12 06:09:17 +0000277/*
278** Global Tcl variable $echo_module is a list. This routine appends
279** the string element zArg to that list in interpreter interp.
280*/
drha967e882006-06-13 01:04:52 +0000281static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000282 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000283 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000284}
285
danielk19777e6ebfb2006-06-12 11:24:37 +0000286/*
287** This function is called from within the echo-modules xCreate and
288** xConnect methods. The argc and argv arguments are copies of those
289** passed to the calling method. This function is responsible for
290** calling sqlite3_declare_vtab() to declare the schema of the virtual
291** table being created or connected.
292**
293** If the constructor was passed just one argument, i.e.:
294**
295** CREATE TABLE t1 AS echo(t2);
296**
297** Then t2 is assumed to be the name of a *real* database table. The
298** schema of the virtual table is declared by passing a copy of the
299** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
300** Hence, the virtual table should have exactly the same column names and
301** types as the real table.
302*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000303static int echoDeclareVtab(
304 echo_vtab *pVtab,
danielk1977182c4ba2007-06-27 15:53:34 +0000305 sqlite3 *db
danielk1977b7a7b9a2006-06-13 10:24:42 +0000306){
danielk19777e6ebfb2006-06-12 11:24:37 +0000307 int rc = SQLITE_OK;
308
danielk1977182c4ba2007-06-27 15:53:34 +0000309 if( pVtab->zTableName ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000310 sqlite3_stmt *pStmt = 0;
danielk1977222a7572007-08-25 13:37:48 +0000311 rc = sqlite3_prepare(db,
danielk19777e6ebfb2006-06-12 11:24:37 +0000312 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
313 -1, &pStmt, 0);
danielk1977222a7572007-08-25 13:37:48 +0000314 if( rc==SQLITE_OK ){
315 sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
316 if( sqlite3_step(pStmt)==SQLITE_ROW ){
317 int rc2;
318 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
319 rc = sqlite3_declare_vtab(db, zCreateTable);
320 rc2 = sqlite3_finalize(pStmt);
321 if( rc==SQLITE_OK ){
322 rc = rc2;
323 }
324 } else {
325 rc = sqlite3_finalize(pStmt);
326 if( rc==SQLITE_OK ){
327 rc = SQLITE_ERROR;
328 }
329 }
danielk19777fa5dd12007-04-18 17:04:00 +0000330 if( rc==SQLITE_OK ){
danielk1977222a7572007-08-25 13:37:48 +0000331 rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
danielk19777fa5dd12007-04-18 17:04:00 +0000332 }
danielk1977222a7572007-08-25 13:37:48 +0000333 if( rc==SQLITE_OK ){
334 rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
danielk1977be718892006-06-23 08:05:19 +0000335 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000336 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000337 }
338
339 return rc;
340}
341
danielk1977cc013f82006-06-24 06:36:11 +0000342/*
343** This function frees all runtime structures associated with the virtual
344** table pVtab.
345*/
danielk1977a4e76362006-06-14 06:31:28 +0000346static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000347 echo_vtab *p = (echo_vtab*)pVtab;
drh17435752007-08-16 04:30:38 +0000348 sqlite3_free(p->aIndex);
349 sqlite3_free(p->aCol);
350 sqlite3_free(p->zThis);
351 sqlite3_free(p->zTableName);
352 sqlite3_free(p->zLogName);
353 sqlite3_free(p);
danielk1977a4e76362006-06-14 06:31:28 +0000354 return 0;
355}
356
danielk1977860b6cd2007-09-03 11:51:50 +0000357typedef struct EchoModule EchoModule;
358struct EchoModule {
359 Tcl_Interp *interp;
360};
361
danielk1977cc013f82006-06-24 06:36:11 +0000362/*
363** This function is called to do the work of the xConnect() method -
364** to allocate the required in-memory structures for a newly connected
365** virtual table.
366*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000367static int echoConstructor(
368 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000369 void *pAux,
drhe4102962006-09-11 00:34:22 +0000370 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000371 sqlite3_vtab **ppVtab,
372 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000373){
danielk1977a1644fd2007-08-29 12:31:25 +0000374 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000375 int i;
376 echo_vtab *pVtab;
377
danielk1977cc013f82006-06-24 06:36:11 +0000378 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk197731dad9d2007-08-16 11:36:15 +0000379 pVtab = sqlite3MallocZero( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000380 if( !pVtab ){
381 return SQLITE_NOMEM;
382 }
danielk1977860b6cd2007-09-03 11:51:50 +0000383 pVtab->interp = ((EchoModule *)pAux)->interp;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000384 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000385
danielk1977182c4ba2007-06-27 15:53:34 +0000386 /* Allocate echo_vtab.zThis */
danielk19771e536952007-08-16 10:09:01 +0000387 pVtab->zThis = sqlite3MPrintf(0, "%s", argv[2]);
danielk1977182c4ba2007-06-27 15:53:34 +0000388 if( !pVtab->zThis ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000389 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000390 return SQLITE_NOMEM;
391 }
392
danielk1977182c4ba2007-06-27 15:53:34 +0000393 /* Allocate echo_vtab.zTableName */
394 if( argc>3 ){
danielk19771e536952007-08-16 10:09:01 +0000395 pVtab->zTableName = sqlite3MPrintf(0, "%s", argv[3]);
danielk1977182c4ba2007-06-27 15:53:34 +0000396 dequoteString(pVtab->zTableName);
397 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
danielk19771e536952007-08-16 10:09:01 +0000398 char *z = sqlite3MPrintf(0, "%s%s", argv[2], &(pVtab->zTableName[1]));
drh17435752007-08-16 04:30:38 +0000399 sqlite3_free(pVtab->zTableName);
danielk1977182c4ba2007-06-27 15:53:34 +0000400 pVtab->zTableName = z;
401 pVtab->isPattern = 1;
402 }
403 if( !pVtab->zTableName ){
404 echoDestructor((sqlite3_vtab *)pVtab);
405 return SQLITE_NOMEM;
406 }
407 }
408
danielk1977cc013f82006-06-24 06:36:11 +0000409 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000410 for(i=0; i<argc; i++){
411 appendToEchoModule(pVtab->interp, argv[i]);
412 }
413
danielk1977cc013f82006-06-24 06:36:11 +0000414 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
415 ** structure. If an error occurs, delete the sqlite3_vtab structure and
416 ** return an error code.
417 */
danielk1977a1644fd2007-08-29 12:31:25 +0000418 rc = echoDeclareVtab(pVtab, db);
419 if( rc!=SQLITE_OK ){
danielk1977a4e76362006-06-14 06:31:28 +0000420 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977a1644fd2007-08-29 12:31:25 +0000421 return rc;
danielk1977a4e76362006-06-14 06:31:28 +0000422 }
423
danielk1977cc013f82006-06-24 06:36:11 +0000424 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000425 *ppVtab = &pVtab->base;
426 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000427}
drha967e882006-06-13 01:04:52 +0000428
danielk1977cc013f82006-06-24 06:36:11 +0000429/*
430** Echo virtual table module xCreate method.
431*/
drhb9bb7c12006-06-11 23:41:55 +0000432static int echoCreate(
433 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000434 void *pAux,
drhe4102962006-09-11 00:34:22 +0000435 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000436 sqlite3_vtab **ppVtab,
437 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000438){
danielk1977a298e902006-06-22 09:53:48 +0000439 int rc = SQLITE_OK;
danielk1977860b6cd2007-09-03 11:51:50 +0000440 appendToEchoModule(((EchoModule *)pAux)->interp, "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000441 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000442
443 /* If there were two arguments passed to the module at the SQL level
444 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
445 ** the second argument is used as a table name. Attempt to create
446 ** such a table with a single column, "logmsg". This table will
447 ** be used to log calls to the xUpdate method. It will be deleted
448 ** when the virtual table is DROPed.
449 **
450 ** Note: The main point of this is to test that we can drop tables
451 ** from within an xDestroy method call.
452 */
danielk1977a298e902006-06-22 09:53:48 +0000453 if( rc==SQLITE_OK && argc==5 ){
454 char *zSql;
455 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
danielk19771e536952007-08-16 10:09:01 +0000456 pVtab->zLogName = sqlite3MPrintf(0, "%s", argv[4]);
457 zSql = sqlite3MPrintf(0, "CREATE TABLE %Q(logmsg)", pVtab->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000458 rc = sqlite3_exec(db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000459 sqlite3_free(zSql);
danielk1977cd2543b2007-09-03 15:03:20 +0000460 if( rc!=SQLITE_OK ){
461 *pzErr = sqlite3StrDup(sqlite3_errmsg(db));
462 }
463 }
464
465 if( *ppVtab && rc!=SQLITE_OK ){
466 echoDestructor(*ppVtab);
467 *ppVtab = 0;
danielk1977a298e902006-06-22 09:53:48 +0000468 }
danielk1977cc013f82006-06-24 06:36:11 +0000469
danielk1977a298e902006-06-22 09:53:48 +0000470 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000471}
danielk1977cc013f82006-06-24 06:36:11 +0000472
473/*
474** Echo virtual table module xConnect method.
475*/
drhb9bb7c12006-06-11 23:41:55 +0000476static int echoConnect(
477 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000478 void *pAux,
drhe4102962006-09-11 00:34:22 +0000479 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000480 sqlite3_vtab **ppVtab,
481 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000482){
danielk1977860b6cd2007-09-03 11:51:50 +0000483 appendToEchoModule(((EchoModule *)pAux)->interp, "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000484 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000485}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000486
danielk1977cc013f82006-06-24 06:36:11 +0000487/*
488** Echo virtual table module xDisconnect method.
489*/
danielk19775fac9f82006-06-13 14:16:58 +0000490static int echoDisconnect(sqlite3_vtab *pVtab){
491 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
492 return echoDestructor(pVtab);
493}
danielk1977cc013f82006-06-24 06:36:11 +0000494
495/*
496** Echo virtual table module xDestroy method.
497*/
danielk19775fac9f82006-06-13 14:16:58 +0000498static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000499 int rc = SQLITE_OK;
500 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000501 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000502
503 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000504 if( p && p->zLogName ){
505 char *zSql;
danielk19771e536952007-08-16 10:09:01 +0000506 zSql = sqlite3MPrintf(0, "DROP TABLE %Q", p->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000507 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000508 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000509 }
danielk1977cc013f82006-06-24 06:36:11 +0000510
danielk1977a298e902006-06-22 09:53:48 +0000511 if( rc==SQLITE_OK ){
512 rc = echoDestructor(pVtab);
513 }
514 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000515}
516
danielk1977cc013f82006-06-24 06:36:11 +0000517/*
518** Echo virtual table module xOpen method.
519*/
danielk19775fac9f82006-06-13 14:16:58 +0000520static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000521 echo_cursor *pCur;
danielk197731dad9d2007-08-16 11:36:15 +0000522 pCur = sqlite3MallocZero(sizeof(echo_cursor));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000523 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000524 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000525}
526
danielk1977cc013f82006-06-24 06:36:11 +0000527/*
528** Echo virtual table module xClose method.
529*/
danielk19775fac9f82006-06-13 14:16:58 +0000530static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000531 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000532 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000533 sqlite3_stmt *pStmt = pCur->pStmt;
534 pCur->pStmt = 0;
drh17435752007-08-16 04:30:38 +0000535 sqlite3_free(pCur);
danielk1977be718892006-06-23 08:05:19 +0000536 rc = sqlite3_finalize(pStmt);
537 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000538}
539
danielk1977a298e902006-06-22 09:53:48 +0000540/*
541** Return non-zero if the cursor does not currently point to a valid record
542** (i.e if the scan has finished), or zero otherwise.
543*/
544static int echoEof(sqlite3_vtab_cursor *cur){
545 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
546}
547
danielk1977cc013f82006-06-24 06:36:11 +0000548/*
549** Echo virtual table module xNext method.
550*/
danielk19775fac9f82006-06-13 14:16:58 +0000551static int echoNext(sqlite3_vtab_cursor *cur){
danielk197731dad9d2007-08-16 11:36:15 +0000552 int rc = SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000553 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000554
danielk197731dad9d2007-08-16 11:36:15 +0000555 if( pCur->pStmt ){
556 rc = sqlite3_step(pCur->pStmt);
557 if( rc==SQLITE_ROW ){
558 rc = SQLITE_OK;
559 }else{
560 rc = sqlite3_finalize(pCur->pStmt);
561 pCur->pStmt = 0;
562 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000563 }
564
565 return rc;
566}
567
danielk1977cc013f82006-06-24 06:36:11 +0000568/*
569** Echo virtual table module xColumn method.
570*/
danielk19775fac9f82006-06-13 14:16:58 +0000571static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000572 int iCol = i + 1;
573 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk1977fbbe0052006-06-21 07:02:33 +0000574 if( !pStmt ){
575 sqlite3_result_null(ctx);
576 }else{
577 assert( sqlite3_data_count(pStmt)>iCol );
578 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
579 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000580 return SQLITE_OK;
581}
582
danielk1977cc013f82006-06-24 06:36:11 +0000583/*
584** Echo virtual table module xRowid method.
585*/
danielk19775fac9f82006-06-13 14:16:58 +0000586static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000587 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
588 *pRowid = sqlite3_column_int64(pStmt, 0);
589 return SQLITE_OK;
590}
591
danielk197798331532006-06-14 10:55:52 +0000592/*
593** Compute a simple hash of the null terminated string zString.
594**
595** This module uses only sqlite3_index_info.idxStr, not
596** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
597** in echoBestIndex(), idxNum is set to the corresponding hash value.
598** In echoFilter(), code assert()s that the supplied idxNum value is
599** indeed the hash of the supplied idxStr.
600*/
601static int hashString(const char *zString){
602 int val = 0;
603 int ii;
604 for(ii=0; zString[ii]; ii++){
605 val = (val << 3) + (int)zString[ii];
606 }
607 return val;
608}
609
danielk1977cc013f82006-06-24 06:36:11 +0000610/*
611** Echo virtual table module xFilter method.
612*/
danielk19775fac9f82006-06-13 14:16:58 +0000613static int echoFilter(
614 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000615 int idxNum, const char *idxStr,
616 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000617){
618 int rc;
drh4be8b512006-06-13 23:51:34 +0000619 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000620
621 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
622 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
623 sqlite3 *db = pVtab->db;
624
danielk1977cc013f82006-06-24 06:36:11 +0000625 /* Check that idxNum matches idxStr */
626 assert( idxNum==hashString(idxStr) );
627
628 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000629 appendToEchoModule(pVtab->interp, "xFilter");
630 appendToEchoModule(pVtab->interp, idxStr);
631 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000632 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000633 }
634
danielk19775fac9f82006-06-13 14:16:58 +0000635 sqlite3_finalize(pCur->pStmt);
636 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000637
638 /* Prepare the SQL statement created by echoBestIndex and bind the
639 ** runtime parameters passed to this function to it.
640 */
drh4be8b512006-06-13 23:51:34 +0000641 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000642 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000643 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk1977cc013f82006-06-24 06:36:11 +0000644 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000645 }
danielk1977cc013f82006-06-24 06:36:11 +0000646
647 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000648 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000649 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000650 }
651
danielk1977be718892006-06-23 08:05:19 +0000652 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000653}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000654
danielk1977cc013f82006-06-24 06:36:11 +0000655
656/*
657** A helper function used by echoUpdate() and echoBestIndex() for
658** manipulating strings in concert with the sqlite3_mprintf() function.
659**
660** Parameter pzStr points to a pointer to a string allocated with
661** sqlite3_mprintf. The second parameter, zAppend, points to another
662** string. The two strings are concatenated together and *pzStr
663** set to point at the result. The initial buffer pointed to by *pzStr
664** is deallocated via sqlite3_free().
665**
666** If the third argument, doFree, is true, then sqlite3_free() is
667** also called to free the buffer pointed to by zAppend.
668*/
danielk1977a1644fd2007-08-29 12:31:25 +0000669static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
danielk1977cc013f82006-06-24 06:36:11 +0000670 char *zIn = *pzStr;
danielk1977a1644fd2007-08-29 12:31:25 +0000671 if( !zAppend && doFree && *pRc==SQLITE_OK ){
672 *pRc = SQLITE_NOMEM;
673 }
674 if( *pRc!=SQLITE_OK ){
675 sqlite3_free(zIn);
676 zIn = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000677 }else{
danielk1977a1644fd2007-08-29 12:31:25 +0000678 if( zIn ){
679 char *zTemp = zIn;
680 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
681 sqlite3_free(zTemp);
682 }else{
683 zIn = sqlite3_mprintf("%s", zAppend);
684 }
685 if( !zIn ){
686 *pRc = SQLITE_NOMEM;
687 }
danielk1977cc013f82006-06-24 06:36:11 +0000688 }
689 *pzStr = zIn;
690 if( doFree ){
691 sqlite3_free(zAppend);
692 }
693}
694
drhb9bb7c12006-06-11 23:41:55 +0000695/*
danielk19775fac9f82006-06-13 14:16:58 +0000696** The echo module implements the subset of query constraints and sort
697** orders that may take advantage of SQLite indices on the underlying
698** real table. For example, if the real table is declared as:
699**
700** CREATE TABLE real(a, b, c);
701** CREATE INDEX real_index ON real(b);
702**
703** then the echo module handles WHERE or ORDER BY clauses that refer
704** to the column "b", but not "a" or "c". If a multi-column index is
drh85b623f2007-12-13 21:54:09 +0000705** present, only its left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000706**
707** This xBestIndex method encodes the proposed search strategy as
708** an SQL query on the real table underlying the virtual echo module
709** table and stores the query in sqlite3_index_info.idxStr. The SQL
710** statement is of the form:
711**
712** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
713**
714** where the <where-clause> and <order-by-clause> are determined
715** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000716*/
danielk19775fac9f82006-06-13 14:16:58 +0000717static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
718 int ii;
drh4be8b512006-06-13 23:51:34 +0000719 char *zQuery = 0;
720 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000721 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000722 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000723 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000724 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000725 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000726
727 int nRow;
728 int useIdx = 0;
729 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000730 int useCost = 0;
731 double cost;
danielk197774cdba42006-06-19 12:02:58 +0000732
733 /* Determine the number of rows in the table and store this value in local
734 ** variable nRow. The 'estimated-cost' of the scan will be the number of
735 ** rows in the table for a linear scan, or the log (base 2) of the
736 ** number of rows if the proposed scan uses an index.
737 */
danielk1977780b1d92007-03-30 14:56:34 +0000738 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
739 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
740 useCost = 1;
741 } else {
742 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000743 if( !zQuery ){
744 return SQLITE_NOMEM;
745 }
danielk1977780b1d92007-03-30 14:56:34 +0000746 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
747 sqlite3_free(zQuery);
748 if( rc!=SQLITE_OK ){
749 return rc;
750 }
751 sqlite3_step(pStmt);
752 nRow = sqlite3_column_int(pStmt, 0);
753 rc = sqlite3_finalize(pStmt);
754 if( rc!=SQLITE_OK ){
755 return rc;
756 }
danielk197774cdba42006-06-19 12:02:58 +0000757 }
danielk19775fac9f82006-06-13 14:16:58 +0000758
drh4be8b512006-06-13 23:51:34 +0000759 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000760 if( !zQuery ){
761 return SQLITE_NOMEM;
762 }
danielk19775fac9f82006-06-13 14:16:58 +0000763 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
764 const struct sqlite3_index_constraint *pConstraint;
765 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000766 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000767
768 pConstraint = &pIdxInfo->aConstraint[ii];
769 pUsage = &pIdxInfo->aConstraintUsage[ii];
770
drh383736b2006-10-08 18:56:57 +0000771 iCol = pConstraint->iColumn;
danielk19775fac9f82006-06-13 14:16:58 +0000772 if( pVtab->aIndex[iCol] ){
773 char *zCol = pVtab->aCol[iCol];
774 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000775 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000776 if( iCol<0 ){
777 zCol = "rowid";
778 }
danielk19775fac9f82006-06-13 14:16:58 +0000779 switch( pConstraint->op ){
780 case SQLITE_INDEX_CONSTRAINT_EQ:
781 zOp = "="; break;
782 case SQLITE_INDEX_CONSTRAINT_LT:
783 zOp = "<"; break;
784 case SQLITE_INDEX_CONSTRAINT_GT:
785 zOp = ">"; break;
786 case SQLITE_INDEX_CONSTRAINT_LE:
787 zOp = "<="; break;
788 case SQLITE_INDEX_CONSTRAINT_GE:
789 zOp = ">="; break;
790 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000791 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000792 }
drh1a90e092006-06-14 22:07:10 +0000793 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000794 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
795 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000796 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000797 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000798 }
danielk1977a1644fd2007-08-29 12:31:25 +0000799 string_concat(&zQuery, zNew, 1, &rc);
danielk1977cc013f82006-06-24 06:36:11 +0000800
drh4be8b512006-06-13 23:51:34 +0000801 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000802 pUsage->argvIndex = ++nArg;
803 pUsage->omit = 1;
804 }
805 }
danielk197747d08092006-06-14 07:41:31 +0000806
807 /* If there is only one term in the ORDER BY clause, and it is
808 ** on a column that this virtual table has an index for, then consume
809 ** the ORDER BY clause.
810 */
811 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000812 int iCol = pIdxInfo->aOrderBy->iColumn;
813 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000814 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000815 if( iCol<0 ){
816 zCol = "rowid";
817 }
danielk1977cc013f82006-06-24 06:36:11 +0000818 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
danielk1977a1644fd2007-08-29 12:31:25 +0000819 string_concat(&zQuery, zNew, 1, &rc);
danielk197747d08092006-06-14 07:41:31 +0000820 pIdxInfo->orderByConsumed = 1;
821 }
822
danielk19775fac9f82006-06-13 14:16:58 +0000823 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000824 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000825
danielk1977222a7572007-08-25 13:37:48 +0000826 if( !zQuery ){
danielk1977a1644fd2007-08-29 12:31:25 +0000827 return rc;
danielk1977222a7572007-08-25 13:37:48 +0000828 }
danielk197798331532006-06-14 10:55:52 +0000829 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000830 pIdxInfo->idxStr = zQuery;
831 pIdxInfo->needToFreeIdxStr = 1;
danielk1977780b1d92007-03-30 14:56:34 +0000832 if (useCost) {
833 pIdxInfo->estimatedCost = cost;
834 } else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000835 /* Approximation of log2(nRow). */
836 for( ii=0; ii<(sizeof(int)*8); ii++ ){
837 if( nRow & (1<<ii) ){
838 pIdxInfo->estimatedCost = (double)ii;
839 }
840 }
841 } else {
842 pIdxInfo->estimatedCost = (double)nRow;
843 }
844 return rc;
drha967e882006-06-13 01:04:52 +0000845}
846
danielk1977176f4d22006-06-15 10:41:15 +0000847/*
danielk1977cc013f82006-06-24 06:36:11 +0000848** The xUpdate method for echo module virtual tables.
849**
danielk1977176f4d22006-06-15 10:41:15 +0000850** apData[0] apData[1] apData[2..]
851**
852** INTEGER DELETE
853**
854** INTEGER NULL (nCol args) UPDATE (do not set rowid)
855** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
856**
857** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
858** NULL INTEGER (nCol args) INSERT (incl. rowid value)
859**
860*/
danielk19771f6eec52006-06-16 06:17:47 +0000861int echoUpdate(
862 sqlite3_vtab *tab,
863 int nData,
864 sqlite3_value **apData,
865 sqlite_int64 *pRowid
866){
danielk197726e41442006-06-14 15:16:35 +0000867 echo_vtab *pVtab = (echo_vtab *)tab;
868 sqlite3 *db = pVtab->db;
869 int rc = SQLITE_OK;
870
danielk1977176f4d22006-06-15 10:41:15 +0000871 sqlite3_stmt *pStmt;
872 char *z = 0; /* SQL statement to execute */
873 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
874 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
875 int i; /* Counter variable used by for loops */
876
danielk197726e41442006-06-14 15:16:35 +0000877 assert( nData==pVtab->nCol+2 || nData==1 );
878
drh5aec0422006-06-14 23:43:31 +0000879 /* If apData[0] is an integer and nData>1 then do an UPDATE */
880 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000881 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000882 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000883 if( !z ){
884 rc = SQLITE_NOMEM;
885 }
danielk1977176f4d22006-06-15 10:41:15 +0000886
887 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
888 bindArgZero = 1;
889
890 if( bindArgOne ){
danielk1977a1644fd2007-08-29 12:31:25 +0000891 string_concat(&z, " SET rowid=?1 ", 0, &rc);
drh5aec0422006-06-14 23:43:31 +0000892 zSep = ",";
893 }
894 for(i=2; i<nData; i++){
895 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000896 string_concat(&z, sqlite3_mprintf(
danielk1977a1644fd2007-08-29 12:31:25 +0000897 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000898 zSep = ",";
899 }
danielk1977a1644fd2007-08-29 12:31:25 +0000900 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
drh5aec0422006-06-14 23:43:31 +0000901 }
902
danielk1977176f4d22006-06-15 10:41:15 +0000903 /* If apData[0] is an integer and nData==1 then do a DELETE */
904 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
905 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000906 if( !z ){
907 rc = SQLITE_NOMEM;
908 }
danielk1977176f4d22006-06-15 10:41:15 +0000909 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000910 }
911
danielk1977176f4d22006-06-15 10:41:15 +0000912 /* If the first argument is NULL and there are more than two args, INSERT */
913 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000914 int ii;
915 char *zInsert = 0;
916 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000917
danielk19774b2688a2006-06-20 11:01:07 +0000918 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977a1644fd2007-08-29 12:31:25 +0000919 if( !zInsert ){
920 rc = SQLITE_NOMEM;
921 }
danielk1977176f4d22006-06-15 10:41:15 +0000922 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
923 bindArgOne = 1;
924 zValues = sqlite3_mprintf("?");
danielk1977a1644fd2007-08-29 12:31:25 +0000925 string_concat(&zInsert, "rowid", 0, &rc);
danielk197726e41442006-06-14 15:16:35 +0000926 }
927
danielk1977176f4d22006-06-15 10:41:15 +0000928 assert((pVtab->nCol+2)==nData);
929 for(ii=2; ii<nData; ii++){
930 string_concat(&zInsert,
danielk1977a1644fd2007-08-29 12:31:25 +0000931 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
danielk1977176f4d22006-06-15 10:41:15 +0000932 string_concat(&zValues,
danielk1977a1644fd2007-08-29 12:31:25 +0000933 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
danielk197726e41442006-06-14 15:16:35 +0000934 }
935
danielk1977a1644fd2007-08-29 12:31:25 +0000936 string_concat(&z, zInsert, 1, &rc);
937 string_concat(&z, ") VALUES(", 0, &rc);
938 string_concat(&z, zValues, 1, &rc);
939 string_concat(&z, ")", 0, &rc);
danielk1977176f4d22006-06-15 10:41:15 +0000940 }
941
942 /* Anything else is an error */
943 else{
944 assert(0);
945 return SQLITE_ERROR;
946 }
947
danielk1977a1644fd2007-08-29 12:31:25 +0000948 if( rc==SQLITE_OK ){
949 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
950 }
danielk1977176f4d22006-06-15 10:41:15 +0000951 assert( rc!=SQLITE_OK || pStmt );
952 sqlite3_free(z);
953 if( rc==SQLITE_OK ) {
954 if( bindArgZero ){
955 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000956 }
danielk1977176f4d22006-06-15 10:41:15 +0000957 if( bindArgOne ){
958 sqlite3_bind_value(pStmt, 1, apData[1]);
959 }
danielk1977a7a8e142008-02-13 18:25:27 +0000960 for(i=2; i<nData && rc==SQLITE_OK; i++){
961 if( apData[i] ) rc = sqlite3_bind_value(pStmt, i, apData[i]);
danielk1977176f4d22006-06-15 10:41:15 +0000962 }
danielk1977a7a8e142008-02-13 18:25:27 +0000963 if( rc==SQLITE_OK ){
964 sqlite3_step(pStmt);
965 rc = sqlite3_finalize(pStmt);
966 }else{
967 sqlite3_finalize(pStmt);
968 }
danielk197726e41442006-06-14 15:16:35 +0000969 }
970
danielk19771f6eec52006-06-16 06:17:47 +0000971 if( pRowid && rc==SQLITE_OK ){
972 *pRowid = sqlite3_last_insert_rowid(db);
973 }
974
danielk197726e41442006-06-14 15:16:35 +0000975 return rc;
976}
977
drha967e882006-06-13 01:04:52 +0000978/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000979** xBegin, xSync, xCommit and xRollback callbacks for echo module
980** virtual tables. Do nothing other than add the name of the callback
981** to the $::echo_module Tcl variable.
982*/
983static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
984 char *z;
985 echo_vtab *pVtab = (echo_vtab *)tab;
986 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
987 appendToEchoModule(pVtab->interp, zCall);
988 appendToEchoModule(pVtab->interp, z);
989 sqlite3_free(z);
danielk1977a1644fd2007-08-29 12:31:25 +0000990 return (z?SQLITE_OK:SQLITE_NOMEM);
danielk1977c69cdfd2006-06-17 09:39:55 +0000991}
992static int echoBegin(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +0000993 int rc;
danielk19775017dc32006-06-24 09:34:22 +0000994 echo_vtab *pVtab = (echo_vtab *)tab;
995 Tcl_Interp *interp = pVtab->interp;
996 const char *zVal;
997
danielk1977a1644fd2007-08-29 12:31:25 +0000998 rc = echoTransactionCall(tab, "xBegin");
danielk19775017dc32006-06-24 09:34:22 +0000999
danielk1977a1644fd2007-08-29 12:31:25 +00001000 if( rc==SQLITE_OK ){
1001 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
1002 ** and it is set to the name of the real table underlying this virtual
1003 ** echo module table, then cause this xSync operation to fail.
1004 */
1005 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
1006 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1007 rc = SQLITE_ERROR;
1008 }
danielk19775017dc32006-06-24 09:34:22 +00001009 }
danielk1977a1644fd2007-08-29 12:31:25 +00001010 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001011}
1012static int echoSync(sqlite3_vtab *tab){
danielk1977a1644fd2007-08-29 12:31:25 +00001013 int rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001014 echo_vtab *pVtab = (echo_vtab *)tab;
1015 Tcl_Interp *interp = pVtab->interp;
1016 const char *zVal;
1017
danielk1977a1644fd2007-08-29 12:31:25 +00001018 rc = echoTransactionCall(tab, "xSync");
danielk1977c69cdfd2006-06-17 09:39:55 +00001019
danielk1977a1644fd2007-08-29 12:31:25 +00001020 if( rc==SQLITE_OK ){
1021 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
1022 ** and it is set to the name of the real table underlying this virtual
1023 ** echo module table, then cause this xSync operation to fail.
1024 */
1025 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
1026 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1027 rc = -1;
1028 }
danielk1977c69cdfd2006-06-17 09:39:55 +00001029 }
danielk1977a1644fd2007-08-29 12:31:25 +00001030 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001031}
1032static int echoCommit(sqlite3_vtab *tab){
drh643167f2008-01-22 21:30:53 +00001033 int rc;
1034 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 1);
1035 rc = echoTransactionCall(tab, "xCommit");
1036 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0);
1037 return rc;
danielk1977c69cdfd2006-06-17 09:39:55 +00001038}
1039static int echoRollback(sqlite3_vtab *tab){
1040 return echoTransactionCall(tab, "xRollback");
1041}
1042
1043/*
drhe94b0c32006-07-08 18:09:15 +00001044** Implementation of "GLOB" function on the echo module. Pass
1045** all arguments to the ::echo_glob_overload procedure of TCL
1046** and return the result of that procedure as a string.
1047*/
1048static void overloadedGlobFunction(
1049 sqlite3_context *pContext,
1050 int nArg,
1051 sqlite3_value **apArg
1052){
1053 Tcl_Interp *interp = sqlite3_user_data(pContext);
1054 Tcl_DString str;
1055 int i;
1056 int rc;
1057 Tcl_DStringInit(&str);
1058 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
1059 for(i=0; i<nArg; i++){
1060 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
1061 }
1062 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
1063 Tcl_DStringFree(&str);
1064 if( rc ){
1065 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
1066 }else{
1067 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1068 -1, SQLITE_TRANSIENT);
1069 }
1070 Tcl_ResetResult(interp);
1071}
1072
1073/*
1074** This is the xFindFunction implementation for the echo module.
1075** SQLite calls this routine when the first argument of a function
1076** is a column of an echo virtual table. This routine can optionally
1077** override the implementation of that function. It will choose to
1078** do so if the function is named "glob", and a TCL command named
1079** ::echo_glob_overload exists.
1080*/
1081static int echoFindFunction(
1082 sqlite3_vtab *vtab,
1083 int nArg,
1084 const char *zFuncName,
1085 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1086 void **ppArg
1087){
1088 echo_vtab *pVtab = (echo_vtab *)vtab;
1089 Tcl_Interp *interp = pVtab->interp;
1090 Tcl_CmdInfo info;
1091 if( strcmp(zFuncName,"glob")!=0 ){
1092 return 0;
1093 }
1094 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1095 return 0;
1096 }
1097 *pxFunc = overloadedGlobFunction;
1098 *ppArg = interp;
1099 return 1;
1100}
1101
danielk1977182c4ba2007-06-27 15:53:34 +00001102static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1103 int rc = SQLITE_OK;
1104 echo_vtab *p = (echo_vtab *)vtab;
1105
1106 if( p->isPattern ){
1107 int nThis = strlen(p->zThis);
danielk19771e536952007-08-16 10:09:01 +00001108 char *zSql = sqlite3MPrintf(0, "ALTER TABLE %s RENAME TO %s%s",
danielk1977182c4ba2007-06-27 15:53:34 +00001109 p->zTableName, zNewName, &p->zTableName[nThis]
1110 );
1111 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +00001112 sqlite3_free(zSql);
danielk1977182c4ba2007-06-27 15:53:34 +00001113 }
1114
1115 return rc;
1116}
1117
drhe94b0c32006-07-08 18:09:15 +00001118/*
danielk1977cc013f82006-06-24 06:36:11 +00001119** A virtual table module that merely "echos" the contents of another
1120** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +00001121*/
1122static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +00001123 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +00001124 echoCreate,
1125 echoConnect,
drha967e882006-06-13 01:04:52 +00001126 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +00001127 echoDisconnect,
1128 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +00001129 echoOpen, /* xOpen - open a cursor */
1130 echoClose, /* xClose - close a cursor */
1131 echoFilter, /* xFilter - configure scan constraints */
1132 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +00001133 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +00001134 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +00001135 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +00001136 echoUpdate, /* xUpdate - write data */
1137 echoBegin, /* xBegin - begin transaction */
1138 echoSync, /* xSync - sync transaction */
1139 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +00001140 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +00001141 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:34 +00001142 echoRename, /* xRename - rename the table */
drhb9bb7c12006-06-11 23:41:55 +00001143};
1144
1145/*
1146** Decode a pointer to an sqlite3 object.
1147*/
1148static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
1149 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
1150 return TCL_OK;
1151}
1152
danielk1977860b6cd2007-09-03 11:51:50 +00001153static void moduleDestroy(void *p){
1154 sqlite3_free(p);
1155}
1156
drhb9bb7c12006-06-11 23:41:55 +00001157/*
1158** Register the echo virtual table module.
1159*/
1160static int register_echo_module(
1161 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1162 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1163 int objc, /* Number of arguments */
1164 Tcl_Obj *CONST objv[] /* Command arguments */
1165){
1166 sqlite3 *db;
danielk1977860b6cd2007-09-03 11:51:50 +00001167 EchoModule *pMod;
drhb9bb7c12006-06-11 23:41:55 +00001168 if( objc!=2 ){
1169 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1170 return TCL_ERROR;
1171 }
1172 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977860b6cd2007-09-03 11:51:50 +00001173 pMod = sqlite3_malloc(sizeof(EchoModule));
1174 pMod->interp = interp;
1175 sqlite3_create_module_v2(db, "echo", &echoModule, (void*)pMod, moduleDestroy);
drhb9bb7c12006-06-11 23:41:55 +00001176 return TCL_OK;
1177}
1178
danielk19775017dc32006-06-24 09:34:22 +00001179/*
1180** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1181**
1182** sqlite3_declare_vtab DB SQL
1183*/
1184static int declare_vtab(
1185 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1186 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1187 int objc, /* Number of arguments */
1188 Tcl_Obj *CONST objv[] /* Command arguments */
1189){
1190 sqlite3 *db;
1191 int rc;
1192 if( objc!=3 ){
1193 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1194 return TCL_ERROR;
1195 }
1196 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1197 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1198 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001199 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001200 return TCL_ERROR;
1201 }
1202 return TCL_OK;
1203}
1204
danielk1977cc013f82006-06-24 06:36:11 +00001205#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001206
1207/*
1208** Register commands with the TCL interpreter.
1209*/
1210int Sqlitetest8_Init(Tcl_Interp *interp){
1211 static struct {
1212 char *zName;
1213 Tcl_ObjCmdProc *xProc;
1214 void *clientData;
1215 } aObjCmd[] = {
danielk1977cc013f82006-06-24 06:36:11 +00001216#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001217 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +00001218 { "sqlite3_declare_vtab", declare_vtab, 0 },
danielk1977cc013f82006-06-24 06:36:11 +00001219#endif
drhb9bb7c12006-06-11 23:41:55 +00001220 };
1221 int i;
1222 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1223 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1224 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1225 }
1226 return TCL_OK;
1227}