blob: 06cf55863b7de894ff85f09ddf88951cc8e92091 [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**
drha693bcd2007-07-20 00:35:58 +000016** $Id: test8.c,v 1.48 2007/07/20 00:35:59 drh Exp $
drhb9bb7c12006-06-11 23:41:55 +000017*/
18#include "sqliteInt.h"
19#include "tcl.h"
20#include "os.h"
21#include <stdlib.h>
22#include <string.h>
23
danielk1977cc013f82006-06-24 06:36:11 +000024#ifndef SQLITE_OMIT_VIRTUALTABLE
25
danielk1977b7a7b9a2006-06-13 10:24:42 +000026typedef struct echo_vtab echo_vtab;
27typedef struct echo_cursor echo_cursor;
28
danielk1977c69cdfd2006-06-17 09:39:55 +000029/*
danielk1977780b1d92007-03-30 14:56:34 +000030** The test module defined in this file uses four global Tcl variables to
danielk1977c69cdfd2006-06-17 09:39:55 +000031** commicate with test-scripts:
32**
33** $::echo_module
34** $::echo_module_sync_fail
danielk19775017dc32006-06-24 09:34:22 +000035** $::echo_module_begin_fail
danielk1977780b1d92007-03-30 14:56:34 +000036** $::echo_module_cost
danielk1977cc013f82006-06-24 06:36:11 +000037**
38** The variable ::echo_module is a list. Each time one of the following
39** methods is called, one or more elements are appended to the list.
40** This is used for automated testing of virtual table modules.
41**
42** The ::echo_module_sync_fail variable is set by test scripts and read
43** by code in this file. If it is set to the name of a real table in the
44** the database, then all xSync operations on echo virtual tables that
45** use the named table as a backing store will fail.
danielk1977c69cdfd2006-06-17 09:39:55 +000046*/
47
danielk19775fac9f82006-06-13 14:16:58 +000048/*
danielk1977d6e8dd02006-06-15 15:38:41 +000049** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:58 +000050**
danielk1977d6e8dd02006-06-15 15:38:41 +000051** echo.vtab.aIndex is an array of booleans. The nth entry is true if
52** the nth column of the real table is the left-most column of an index
53** (implicit or otherwise). In other words, if SQLite can optimize
54** a query like "SELECT * FROM real_table WHERE col = ?".
55**
danielk1977c69cdfd2006-06-17 09:39:55 +000056** Member variable aCol[] contains copies of the column names of the real
57** table.
danielk19775fac9f82006-06-13 14:16:58 +000058*/
danielk1977b7a7b9a2006-06-13 10:24:42 +000059struct echo_vtab {
60 sqlite3_vtab base;
danielk1977cc013f82006-06-24 06:36:11 +000061 Tcl_Interp *interp; /* Tcl interpreter containing debug variables */
62 sqlite3 *db; /* Database connection */
danielk19775fac9f82006-06-13 14:16:58 +000063
danielk1977182c4ba2007-06-27 15:53:34 +000064 int isPattern;
65 char *zThis; /* Name of the echo table */
danielk1977a4e76362006-06-14 06:31:28 +000066 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:48 +000067 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:28 +000068 int nCol; /* Number of columns in the real table */
69 int *aIndex; /* Array of size nCol. True if column has an index */
70 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000071};
72
73/* An echo cursor object */
74struct echo_cursor {
75 sqlite3_vtab_cursor base;
76 sqlite3_stmt *pStmt;
danielk1977b7a7b9a2006-06-13 10:24:42 +000077};
78
danielk1977cc013f82006-06-24 06:36:11 +000079/*
danielk1977182c4ba2007-06-27 15:53:34 +000080** Convert an SQL-style quoted string into a normal string by removing
81** the quote characters. The conversion is done in-place. If the
82** input does not begin with a quote character, then this routine
83** is a no-op.
84**
85** Examples:
86**
87** "abc" becomes abc
88** 'xyz' becomes xyz
89** [pqr] becomes pqr
90** `mno` becomes mno
91*/
92static void dequoteString(char *z){
93 int quote;
94 int i, j;
95 if( z==0 ) return;
96 quote = z[0];
97 switch( quote ){
98 case '\'': break;
99 case '"': break;
100 case '`': break; /* For MySQL compatibility */
101 case '[': quote = ']'; break; /* For MS SqlServer compatibility */
102 default: return;
103 }
104 for(i=1, j=0; z[i]; i++){
105 if( z[i]==quote ){
106 if( z[i+1]==quote ){
107 z[j++] = quote;
108 i++;
109 }else{
110 z[j++] = 0;
111 break;
112 }
113 }else{
114 z[j++] = z[i];
115 }
116 }
117}
118
119/*
danielk1977cc013f82006-06-24 06:36:11 +0000120** Retrieve the column names for the table named zTab via database
121** connection db. SQLITE_OK is returned on success, or an sqlite error
122** code otherwise.
123**
124** If successful, the number of columns is written to *pnCol. *paCol is
125** set to point at sqliteMalloc()'d space containing the array of
126** nCol column names. The caller is responsible for calling sqliteFree
127** on *paCol.
128*/
danielk19775fac9f82006-06-13 14:16:58 +0000129static int getColumnNames(
130 sqlite3 *db,
131 const char *zTab,
132 char ***paCol,
133 int *pnCol
134){
135 char **aCol = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000136 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000137 sqlite3_stmt *pStmt = 0;
138 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19 +0000139 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000140
danielk1977cc013f82006-06-24 06:36:11 +0000141 /* Prepare the statement "SELECT * FROM <tbl>". The column names
142 ** of the result set of the compiled SELECT will be the same as
143 ** the column names of table <tbl>.
144 */
145 zSql = sqlite3MPrintf("SELECT * FROM %Q", zTab);
146 if( !zSql ){
147 rc = SQLITE_NOMEM;
148 goto out;
149 }
150 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
151 sqliteFree(zSql);
152
danielk19775fac9f82006-06-13 14:16:58 +0000153 if( rc==SQLITE_OK ){
154 int ii;
danielk1977cc013f82006-06-24 06:36:11 +0000155 int nBytes;
156 char *zSpace;
danielk19775fac9f82006-06-13 14:16:58 +0000157 nCol = sqlite3_column_count(pStmt);
danielk1977cc013f82006-06-24 06:36:11 +0000158
159 /* Figure out how much space to allocate for the array of column names
160 ** (including space for the strings themselves). Then allocate it.
161 */
162 nBytes = sizeof(char *) * nCol;
163 for(ii=0; ii<nCol; ii++){
164 nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1);
165 }
166 aCol = (char **)sqliteMalloc(nBytes);
danielk19775fac9f82006-06-13 14:16:58 +0000167 if( !aCol ){
168 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11 +0000169 goto out;
danielk19775fac9f82006-06-13 14:16:58 +0000170 }
danielk1977cc013f82006-06-24 06:36:11 +0000171
172 /* Copy the column names into the allocated space and set up the
173 ** pointers in the aCol[] array.
174 */
175 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58 +0000176 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11 +0000177 aCol[ii] = zSpace;
178 zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
179 zSpace++;
danielk19775fac9f82006-06-13 14:16:58 +0000180 }
danielk1977cc013f82006-06-24 06:36:11 +0000181 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58 +0000182 }
183
184 *paCol = aCol;
185 *pnCol = nCol;
186
danielk1977cc013f82006-06-24 06:36:11 +0000187out:
danielk19775fac9f82006-06-13 14:16:58 +0000188 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000189 return rc;
190}
191
danielk1977cc013f82006-06-24 06:36:11 +0000192/*
193** Parameter zTab is the name of a table in database db with nCol
194** columns. This function allocates an array of integers nCol in
195** size and populates it according to any implicit or explicit
196** indices on table zTab.
197**
198** If successful, SQLITE_OK is returned and *paIndex set to point
199** at the allocated array. Otherwise, an error code is returned.
200**
201** See comments associated with the member variable aIndex above
202** "struct echo_vtab" for details of the contents of the array.
203*/
204static int getIndexArray(
205 sqlite3 *db, /* Database connection */
206 const char *zTab, /* Name of table in database db */
207 int nCol,
208 int **paIndex
209){
danielk19775fac9f82006-06-13 14:16:58 +0000210 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000211 int *aIndex = 0;
212 int rc;
danielk1977cc013f82006-06-24 06:36:11 +0000213 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000214
danielk1977cc013f82006-06-24 06:36:11 +0000215 /* Allocate space for the index array */
danielk19775fac9f82006-06-13 14:16:58 +0000216 aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
217 if( !aIndex ){
218 rc = SQLITE_NOMEM;
219 goto get_index_array_out;
220 }
221
danielk1977cc013f82006-06-24 06:36:11 +0000222 /* Compile an sqlite pragma to loop through all indices on table zTab */
223 zSql = sqlite3MPrintf("PRAGMA index_list(%s)", zTab);
224 if( !zSql ){
225 rc = SQLITE_NOMEM;
226 goto get_index_array_out;
227 }
228 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
229 sqliteFree(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000230
danielk1977cc013f82006-06-24 06:36:11 +0000231 /* For each index, figure out the left-most column and set the
232 ** corresponding entry in aIndex[] to 1.
233 */
danielk19775fac9f82006-06-13 14:16:58 +0000234 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000235 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58 +0000236 sqlite3_stmt *pStmt2 = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000237 zSql = sqlite3MPrintf("PRAGMA index_info(%s)", zIdx);
238 if( !zSql ){
239 rc = SQLITE_NOMEM;
240 goto get_index_array_out;
241 }
242 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
243 sqliteFree(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000244 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
245 int cid = sqlite3_column_int(pStmt2, 1);
246 assert( cid>=0 && cid<nCol );
247 aIndex[cid] = 1;
248 }
danielk1977be718892006-06-23 08:05:19 +0000249 if( pStmt2 ){
250 rc = sqlite3_finalize(pStmt2);
251 }
danielk19775fac9f82006-06-13 14:16:58 +0000252 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000253 goto get_index_array_out;
254 }
255 }
256
danielk19775fac9f82006-06-13 14:16:58 +0000257
258get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000259 if( pStmt ){
260 int rc2 = sqlite3_finalize(pStmt);
261 if( rc==SQLITE_OK ){
262 rc = rc2;
263 }
264 }
danielk19775fac9f82006-06-13 14:16:58 +0000265 if( rc!=SQLITE_OK ){
266 sqliteFree(aIndex);
267 aIndex = 0;
268 }
269 *paIndex = aIndex;
270 return rc;
271}
272
danielk197778efaba2006-06-12 06:09:17 +0000273/*
274** Global Tcl variable $echo_module is a list. This routine appends
275** the string element zArg to that list in interpreter interp.
276*/
drha967e882006-06-13 01:04:52 +0000277static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000278 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000279 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000280}
281
danielk19777e6ebfb2006-06-12 11:24:37 +0000282/*
283** This function is called from within the echo-modules xCreate and
284** xConnect methods. The argc and argv arguments are copies of those
285** passed to the calling method. This function is responsible for
286** calling sqlite3_declare_vtab() to declare the schema of the virtual
287** table being created or connected.
288**
289** If the constructor was passed just one argument, i.e.:
290**
291** CREATE TABLE t1 AS echo(t2);
292**
293** Then t2 is assumed to be the name of a *real* database table. The
294** schema of the virtual table is declared by passing a copy of the
295** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
296** Hence, the virtual table should have exactly the same column names and
297** types as the real table.
298*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000299static int echoDeclareVtab(
300 echo_vtab *pVtab,
danielk1977182c4ba2007-06-27 15:53:34 +0000301 sqlite3 *db
danielk1977b7a7b9a2006-06-13 10:24:42 +0000302){
danielk19777e6ebfb2006-06-12 11:24:37 +0000303 int rc = SQLITE_OK;
304
danielk1977182c4ba2007-06-27 15:53:34 +0000305 if( pVtab->zTableName ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000306 sqlite3_stmt *pStmt = 0;
307 sqlite3_prepare(db,
308 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
309 -1, &pStmt, 0);
danielk1977182c4ba2007-06-27 15:53:34 +0000310 sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
danielk19777e6ebfb2006-06-12 11:24:37 +0000311 if( sqlite3_step(pStmt)==SQLITE_ROW ){
danielk19777fa5dd12007-04-18 17:04:00 +0000312 int rc2;
danielk197765fd59f2006-06-24 11:51:33 +0000313 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
danielk19777fa5dd12007-04-18 17:04:00 +0000314 rc = sqlite3_declare_vtab(db, zCreateTable);
315 rc2 = sqlite3_finalize(pStmt);
316 if( rc==SQLITE_OK ){
317 rc = rc2;
318 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000319 } else {
danielk1977be718892006-06-23 08:05:19 +0000320 rc = sqlite3_finalize(pStmt);
321 if( rc==SQLITE_OK ){
322 rc = SQLITE_ERROR;
323 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000324 }
danielk19775fac9f82006-06-13 14:16:58 +0000325 if( rc==SQLITE_OK ){
danielk1977182c4ba2007-06-27 15:53:34 +0000326 rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000327 }
328 if( rc==SQLITE_OK ){
danielk1977182c4ba2007-06-27 15:53:34 +0000329 rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000330 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000331 }
332
333 return rc;
334}
335
danielk1977cc013f82006-06-24 06:36:11 +0000336/*
337** This function frees all runtime structures associated with the virtual
338** table pVtab.
339*/
danielk1977a4e76362006-06-14 06:31:28 +0000340static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000341 echo_vtab *p = (echo_vtab*)pVtab;
342 sqliteFree(p->aIndex);
danielk1977a4e76362006-06-14 06:31:28 +0000343 sqliteFree(p->aCol);
danielk1977182c4ba2007-06-27 15:53:34 +0000344 sqliteFree(p->zThis);
danielk1977a4e76362006-06-14 06:31:28 +0000345 sqliteFree(p->zTableName);
danielk1977212b2182006-06-23 14:32:08 +0000346 sqliteFree(p->zLogName);
danielk1977a4e76362006-06-14 06:31:28 +0000347 sqliteFree(p);
348 return 0;
349}
350
danielk1977cc013f82006-06-24 06:36:11 +0000351/*
352** This function is called to do the work of the xConnect() method -
353** to allocate the required in-memory structures for a newly connected
354** virtual table.
355*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000356static int echoConstructor(
357 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000358 void *pAux,
drhe4102962006-09-11 00:34:22 +0000359 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000360 sqlite3_vtab **ppVtab,
361 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000362){
363 int i;
364 echo_vtab *pVtab;
365
danielk1977cc013f82006-06-24 06:36:11 +0000366 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000367 pVtab = sqliteMalloc( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000368 if( !pVtab ){
369 return SQLITE_NOMEM;
370 }
danielk19779da9d472006-06-14 06:58:15 +0000371 pVtab->interp = (Tcl_Interp *)pAux;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000372 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000373
danielk1977182c4ba2007-06-27 15:53:34 +0000374 /* Allocate echo_vtab.zThis */
375 pVtab->zThis = sqlite3MPrintf("%s", argv[2]);
376 if( !pVtab->zThis ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000377 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000378 return SQLITE_NOMEM;
379 }
380
danielk1977182c4ba2007-06-27 15:53:34 +0000381 /* Allocate echo_vtab.zTableName */
382 if( argc>3 ){
383 pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
384 dequoteString(pVtab->zTableName);
385 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
386 char *z = sqlite3MPrintf("%s%s", argv[2], &(pVtab->zTableName[1]));
387 sqliteFree(pVtab->zTableName);
388 pVtab->zTableName = z;
389 pVtab->isPattern = 1;
390 }
391 if( !pVtab->zTableName ){
392 echoDestructor((sqlite3_vtab *)pVtab);
393 return SQLITE_NOMEM;
394 }
395 }
396
danielk1977cc013f82006-06-24 06:36:11 +0000397 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000398 for(i=0; i<argc; i++){
399 appendToEchoModule(pVtab->interp, argv[i]);
400 }
401
danielk1977cc013f82006-06-24 06:36:11 +0000402 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
403 ** structure. If an error occurs, delete the sqlite3_vtab structure and
404 ** return an error code.
405 */
danielk1977182c4ba2007-06-27 15:53:34 +0000406 if( echoDeclareVtab(pVtab, db) ){
danielk1977a4e76362006-06-14 06:31:28 +0000407 echoDestructor((sqlite3_vtab *)pVtab);
408 return SQLITE_ERROR;
409 }
410
danielk1977cc013f82006-06-24 06:36:11 +0000411 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000412 *ppVtab = &pVtab->base;
413 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000414}
drha967e882006-06-13 01:04:52 +0000415
danielk1977cc013f82006-06-24 06:36:11 +0000416/*
417** Echo virtual table module xCreate method.
418*/
drhb9bb7c12006-06-11 23:41:55 +0000419static int echoCreate(
420 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000421 void *pAux,
drhe4102962006-09-11 00:34:22 +0000422 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000423 sqlite3_vtab **ppVtab,
424 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000425){
danielk1977a298e902006-06-22 09:53:48 +0000426 int rc = SQLITE_OK;
danielk19779da9d472006-06-14 06:58:15 +0000427 appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000428 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000429
430 /* If there were two arguments passed to the module at the SQL level
431 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
432 ** the second argument is used as a table name. Attempt to create
433 ** such a table with a single column, "logmsg". This table will
434 ** be used to log calls to the xUpdate method. It will be deleted
435 ** when the virtual table is DROPed.
436 **
437 ** Note: The main point of this is to test that we can drop tables
438 ** from within an xDestroy method call.
439 */
danielk1977a298e902006-06-22 09:53:48 +0000440 if( rc==SQLITE_OK && argc==5 ){
441 char *zSql;
442 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
443 pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
444 zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
445 rc = sqlite3_exec(db, zSql, 0, 0, 0);
446 sqliteFree(zSql);
447 }
danielk1977cc013f82006-06-24 06:36:11 +0000448
danielk1977a298e902006-06-22 09:53:48 +0000449 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000450}
danielk1977cc013f82006-06-24 06:36:11 +0000451
452/*
453** Echo virtual table module xConnect method.
454*/
drhb9bb7c12006-06-11 23:41:55 +0000455static int echoConnect(
456 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000457 void *pAux,
drhe4102962006-09-11 00:34:22 +0000458 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000459 sqlite3_vtab **ppVtab,
460 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000461){
danielk19779da9d472006-06-14 06:58:15 +0000462 appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000463 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000464}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000465
danielk1977cc013f82006-06-24 06:36:11 +0000466/*
467** Echo virtual table module xDisconnect method.
468*/
danielk19775fac9f82006-06-13 14:16:58 +0000469static int echoDisconnect(sqlite3_vtab *pVtab){
470 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
471 return echoDestructor(pVtab);
472}
danielk1977cc013f82006-06-24 06:36:11 +0000473
474/*
475** Echo virtual table module xDestroy method.
476*/
danielk19775fac9f82006-06-13 14:16:58 +0000477static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000478 int rc = SQLITE_OK;
479 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000480 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000481
482 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000483 if( p && p->zLogName ){
484 char *zSql;
485 zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName);
486 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
487 sqliteFree(zSql);
488 }
danielk1977cc013f82006-06-24 06:36:11 +0000489
danielk1977a298e902006-06-22 09:53:48 +0000490 if( rc==SQLITE_OK ){
491 rc = echoDestructor(pVtab);
492 }
493 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000494}
495
danielk1977cc013f82006-06-24 06:36:11 +0000496/*
497** Echo virtual table module xOpen method.
498*/
danielk19775fac9f82006-06-13 14:16:58 +0000499static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000500 echo_cursor *pCur;
501 pCur = sqliteMalloc(sizeof(echo_cursor));
502 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000503 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000504}
505
danielk1977cc013f82006-06-24 06:36:11 +0000506/*
507** Echo virtual table module xClose method.
508*/
danielk19775fac9f82006-06-13 14:16:58 +0000509static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000510 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000511 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000512 sqlite3_stmt *pStmt = pCur->pStmt;
513 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000514 sqliteFree(pCur);
danielk1977be718892006-06-23 08:05:19 +0000515 rc = sqlite3_finalize(pStmt);
516 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000517}
518
danielk1977a298e902006-06-22 09:53:48 +0000519/*
520** Return non-zero if the cursor does not currently point to a valid record
521** (i.e if the scan has finished), or zero otherwise.
522*/
523static int echoEof(sqlite3_vtab_cursor *cur){
524 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
525}
526
danielk1977cc013f82006-06-24 06:36:11 +0000527/*
528** Echo virtual table module xNext method.
529*/
danielk19775fac9f82006-06-13 14:16:58 +0000530static int echoNext(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000531 int rc;
532 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000533 rc = sqlite3_step(pCur->pStmt);
534
535 if( rc==SQLITE_ROW ){
danielk1977a298e902006-06-22 09:53:48 +0000536 rc = SQLITE_OK;
537 }else{
538 rc = sqlite3_finalize(pCur->pStmt);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000539 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000540 }
541
542 return rc;
543}
544
danielk1977cc013f82006-06-24 06:36:11 +0000545/*
546** Echo virtual table module xColumn method.
547*/
danielk19775fac9f82006-06-13 14:16:58 +0000548static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000549 int iCol = i + 1;
550 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk1977fbbe0052006-06-21 07:02:33 +0000551 if( !pStmt ){
552 sqlite3_result_null(ctx);
553 }else{
554 assert( sqlite3_data_count(pStmt)>iCol );
555 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
556 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000557 return SQLITE_OK;
558}
559
danielk1977cc013f82006-06-24 06:36:11 +0000560/*
561** Echo virtual table module xRowid method.
562*/
danielk19775fac9f82006-06-13 14:16:58 +0000563static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000564 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
565 *pRowid = sqlite3_column_int64(pStmt, 0);
566 return SQLITE_OK;
567}
568
danielk197798331532006-06-14 10:55:52 +0000569/*
570** Compute a simple hash of the null terminated string zString.
571**
572** This module uses only sqlite3_index_info.idxStr, not
573** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
574** in echoBestIndex(), idxNum is set to the corresponding hash value.
575** In echoFilter(), code assert()s that the supplied idxNum value is
576** indeed the hash of the supplied idxStr.
577*/
578static int hashString(const char *zString){
579 int val = 0;
580 int ii;
581 for(ii=0; zString[ii]; ii++){
582 val = (val << 3) + (int)zString[ii];
583 }
584 return val;
585}
586
danielk1977cc013f82006-06-24 06:36:11 +0000587/*
588** Echo virtual table module xFilter method.
589*/
danielk19775fac9f82006-06-13 14:16:58 +0000590static int echoFilter(
591 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000592 int idxNum, const char *idxStr,
593 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000594){
595 int rc;
drh4be8b512006-06-13 23:51:34 +0000596 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000597
598 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
599 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
600 sqlite3 *db = pVtab->db;
601
danielk1977cc013f82006-06-24 06:36:11 +0000602 /* Check that idxNum matches idxStr */
603 assert( idxNum==hashString(idxStr) );
604
605 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000606 appendToEchoModule(pVtab->interp, "xFilter");
607 appendToEchoModule(pVtab->interp, idxStr);
608 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000609 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000610 }
611
danielk19775fac9f82006-06-13 14:16:58 +0000612 sqlite3_finalize(pCur->pStmt);
613 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000614
615 /* Prepare the SQL statement created by echoBestIndex and bind the
616 ** runtime parameters passed to this function to it.
617 */
drh4be8b512006-06-13 23:51:34 +0000618 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000619 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000620 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk1977cc013f82006-06-24 06:36:11 +0000621 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000622 }
danielk1977cc013f82006-06-24 06:36:11 +0000623
624 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000625 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000626 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000627 }
628
danielk1977be718892006-06-23 08:05:19 +0000629 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000630}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000631
danielk1977cc013f82006-06-24 06:36:11 +0000632
633/*
634** A helper function used by echoUpdate() and echoBestIndex() for
635** manipulating strings in concert with the sqlite3_mprintf() function.
636**
637** Parameter pzStr points to a pointer to a string allocated with
638** sqlite3_mprintf. The second parameter, zAppend, points to another
639** string. The two strings are concatenated together and *pzStr
640** set to point at the result. The initial buffer pointed to by *pzStr
641** is deallocated via sqlite3_free().
642**
643** If the third argument, doFree, is true, then sqlite3_free() is
644** also called to free the buffer pointed to by zAppend.
645*/
646static void string_concat(char **pzStr, char *zAppend, int doFree){
647 char *zIn = *pzStr;
648 if( zIn ){
649 char *zTemp = zIn;
650 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
651 sqlite3_free(zTemp);
652 }else{
653 zIn = sqlite3_mprintf("%s", zAppend);
654 }
655 *pzStr = zIn;
656 if( doFree ){
657 sqlite3_free(zAppend);
658 }
659}
660
drhb9bb7c12006-06-11 23:41:55 +0000661/*
danielk19775fac9f82006-06-13 14:16:58 +0000662** The echo module implements the subset of query constraints and sort
663** orders that may take advantage of SQLite indices on the underlying
664** real table. For example, if the real table is declared as:
665**
666** CREATE TABLE real(a, b, c);
667** CREATE INDEX real_index ON real(b);
668**
669** then the echo module handles WHERE or ORDER BY clauses that refer
670** to the column "b", but not "a" or "c". If a multi-column index is
671** present, only it's left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000672**
673** This xBestIndex method encodes the proposed search strategy as
674** an SQL query on the real table underlying the virtual echo module
675** table and stores the query in sqlite3_index_info.idxStr. The SQL
676** statement is of the form:
677**
678** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
679**
680** where the <where-clause> and <order-by-clause> are determined
681** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000682*/
danielk19775fac9f82006-06-13 14:16:58 +0000683static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
684 int ii;
drh4be8b512006-06-13 23:51:34 +0000685 char *zQuery = 0;
686 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000687 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000688 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000689 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000690 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000691 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000692
693 int nRow;
694 int useIdx = 0;
695 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000696 int useCost = 0;
697 double cost;
danielk197774cdba42006-06-19 12:02:58 +0000698
699 /* Determine the number of rows in the table and store this value in local
700 ** variable nRow. The 'estimated-cost' of the scan will be the number of
701 ** rows in the table for a linear scan, or the log (base 2) of the
702 ** number of rows if the proposed scan uses an index.
703 */
danielk1977780b1d92007-03-30 14:56:34 +0000704 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
705 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
706 useCost = 1;
707 } else {
708 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
709 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
710 sqlite3_free(zQuery);
711 if( rc!=SQLITE_OK ){
712 return rc;
713 }
714 sqlite3_step(pStmt);
715 nRow = sqlite3_column_int(pStmt, 0);
716 rc = sqlite3_finalize(pStmt);
717 if( rc!=SQLITE_OK ){
718 return rc;
719 }
danielk197774cdba42006-06-19 12:02:58 +0000720 }
danielk19775fac9f82006-06-13 14:16:58 +0000721
drh4be8b512006-06-13 23:51:34 +0000722 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000723 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
724 const struct sqlite3_index_constraint *pConstraint;
725 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000726 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000727
728 pConstraint = &pIdxInfo->aConstraint[ii];
729 pUsage = &pIdxInfo->aConstraintUsage[ii];
730
drh383736b2006-10-08 18:56:57 +0000731 iCol = pConstraint->iColumn;
danielk19775fac9f82006-06-13 14:16:58 +0000732 if( pVtab->aIndex[iCol] ){
733 char *zCol = pVtab->aCol[iCol];
734 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000735 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000736 if( iCol<0 ){
737 zCol = "rowid";
738 }
danielk19775fac9f82006-06-13 14:16:58 +0000739 switch( pConstraint->op ){
740 case SQLITE_INDEX_CONSTRAINT_EQ:
741 zOp = "="; break;
742 case SQLITE_INDEX_CONSTRAINT_LT:
743 zOp = "<"; break;
744 case SQLITE_INDEX_CONSTRAINT_GT:
745 zOp = ">"; break;
746 case SQLITE_INDEX_CONSTRAINT_LE:
747 zOp = "<="; break;
748 case SQLITE_INDEX_CONSTRAINT_GE:
749 zOp = ">="; break;
750 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000751 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000752 }
drh1a90e092006-06-14 22:07:10 +0000753 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000754 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
755 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000756 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000757 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000758 }
danielk1977cc013f82006-06-24 06:36:11 +0000759 string_concat(&zQuery, zNew, 1);
760
drh4be8b512006-06-13 23:51:34 +0000761 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000762 pUsage->argvIndex = ++nArg;
763 pUsage->omit = 1;
764 }
765 }
danielk197747d08092006-06-14 07:41:31 +0000766
767 /* If there is only one term in the ORDER BY clause, and it is
768 ** on a column that this virtual table has an index for, then consume
769 ** the ORDER BY clause.
770 */
771 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000772 int iCol = pIdxInfo->aOrderBy->iColumn;
773 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000774 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000775 if( iCol<0 ){
776 zCol = "rowid";
777 }
danielk1977cc013f82006-06-24 06:36:11 +0000778 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
779 string_concat(&zQuery, zNew, 1);
danielk197747d08092006-06-14 07:41:31 +0000780 pIdxInfo->orderByConsumed = 1;
781 }
782
danielk19775fac9f82006-06-13 14:16:58 +0000783 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000784 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000785
danielk197798331532006-06-14 10:55:52 +0000786 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000787 pIdxInfo->idxStr = zQuery;
788 pIdxInfo->needToFreeIdxStr = 1;
danielk1977780b1d92007-03-30 14:56:34 +0000789 if (useCost) {
790 pIdxInfo->estimatedCost = cost;
791 } else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000792 /* Approximation of log2(nRow). */
793 for( ii=0; ii<(sizeof(int)*8); ii++ ){
794 if( nRow & (1<<ii) ){
795 pIdxInfo->estimatedCost = (double)ii;
796 }
797 }
798 } else {
799 pIdxInfo->estimatedCost = (double)nRow;
800 }
801 return rc;
drha967e882006-06-13 01:04:52 +0000802}
803
danielk1977176f4d22006-06-15 10:41:15 +0000804/*
danielk1977cc013f82006-06-24 06:36:11 +0000805** The xUpdate method for echo module virtual tables.
806**
danielk1977176f4d22006-06-15 10:41:15 +0000807** apData[0] apData[1] apData[2..]
808**
809** INTEGER DELETE
810**
811** INTEGER NULL (nCol args) UPDATE (do not set rowid)
812** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
813**
814** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
815** NULL INTEGER (nCol args) INSERT (incl. rowid value)
816**
817*/
danielk19771f6eec52006-06-16 06:17:47 +0000818int echoUpdate(
819 sqlite3_vtab *tab,
820 int nData,
821 sqlite3_value **apData,
822 sqlite_int64 *pRowid
823){
danielk197726e41442006-06-14 15:16:35 +0000824 echo_vtab *pVtab = (echo_vtab *)tab;
825 sqlite3 *db = pVtab->db;
826 int rc = SQLITE_OK;
827
danielk1977176f4d22006-06-15 10:41:15 +0000828 sqlite3_stmt *pStmt;
829 char *z = 0; /* SQL statement to execute */
830 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
831 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
832 int i; /* Counter variable used by for loops */
833
danielk197726e41442006-06-14 15:16:35 +0000834 assert( nData==pVtab->nCol+2 || nData==1 );
835
drh5aec0422006-06-14 23:43:31 +0000836 /* If apData[0] is an integer and nData>1 then do an UPDATE */
837 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000838 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000839 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000840
841 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
842 bindArgZero = 1;
843
844 if( bindArgOne ){
845 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000846 zSep = ",";
847 }
848 for(i=2; i<nData; i++){
849 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000850 string_concat(&z, sqlite3_mprintf(
851 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000852 zSep = ",";
853 }
danielk1977176f4d22006-06-15 10:41:15 +0000854 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000855 }
856
danielk1977176f4d22006-06-15 10:41:15 +0000857 /* If apData[0] is an integer and nData==1 then do a DELETE */
858 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
859 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
860 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000861 }
862
danielk1977176f4d22006-06-15 10:41:15 +0000863 /* If the first argument is NULL and there are more than two args, INSERT */
864 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000865 int ii;
866 char *zInsert = 0;
867 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000868
danielk19774b2688a2006-06-20 11:01:07 +0000869 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000870 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
871 bindArgOne = 1;
872 zValues = sqlite3_mprintf("?");
873 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000874 }
875
danielk1977176f4d22006-06-15 10:41:15 +0000876 assert((pVtab->nCol+2)==nData);
877 for(ii=2; ii<nData; ii++){
878 string_concat(&zInsert,
879 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
880 string_concat(&zValues,
881 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000882 }
883
danielk1977176f4d22006-06-15 10:41:15 +0000884 string_concat(&z, zInsert, 1);
885 string_concat(&z, ") VALUES(", 0);
886 string_concat(&z, zValues, 1);
887 string_concat(&z, ")", 0);
888 }
889
890 /* Anything else is an error */
891 else{
892 assert(0);
893 return SQLITE_ERROR;
894 }
895
896 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
897 assert( rc!=SQLITE_OK || pStmt );
898 sqlite3_free(z);
899 if( rc==SQLITE_OK ) {
900 if( bindArgZero ){
901 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000902 }
danielk1977176f4d22006-06-15 10:41:15 +0000903 if( bindArgOne ){
904 sqlite3_bind_value(pStmt, 1, apData[1]);
905 }
906 for(i=2; i<nData; i++){
907 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
908 }
909 sqlite3_step(pStmt);
910 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000911 }
912
danielk19771f6eec52006-06-16 06:17:47 +0000913 if( pRowid && rc==SQLITE_OK ){
914 *pRowid = sqlite3_last_insert_rowid(db);
915 }
916
danielk197726e41442006-06-14 15:16:35 +0000917 return rc;
918}
919
drha967e882006-06-13 01:04:52 +0000920/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000921** xBegin, xSync, xCommit and xRollback callbacks for echo module
922** virtual tables. Do nothing other than add the name of the callback
923** to the $::echo_module Tcl variable.
924*/
925static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
926 char *z;
927 echo_vtab *pVtab = (echo_vtab *)tab;
928 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
929 appendToEchoModule(pVtab->interp, zCall);
930 appendToEchoModule(pVtab->interp, z);
931 sqlite3_free(z);
932 return SQLITE_OK;
933}
934static int echoBegin(sqlite3_vtab *tab){
danielk19775017dc32006-06-24 09:34:22 +0000935 echo_vtab *pVtab = (echo_vtab *)tab;
936 Tcl_Interp *interp = pVtab->interp;
937 const char *zVal;
938
939 echoTransactionCall(tab, "xBegin");
940
941 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
942 ** and it is set to the name of the real table underlying this virtual
943 ** echo module table, then cause this xSync operation to fail.
944 */
945 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
946 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
947 return SQLITE_ERROR;
948 }
949 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +0000950}
951static int echoSync(sqlite3_vtab *tab){
952 echo_vtab *pVtab = (echo_vtab *)tab;
953 Tcl_Interp *interp = pVtab->interp;
954 const char *zVal;
955
956 echoTransactionCall(tab, "xSync");
957
958 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
959 ** and it is set to the name of the real table underlying this virtual
960 ** echo module table, then cause this xSync operation to fail.
961 */
962 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
963 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
964 return -1;
965 }
966 return SQLITE_OK;
967}
968static int echoCommit(sqlite3_vtab *tab){
969 return echoTransactionCall(tab, "xCommit");
970}
971static int echoRollback(sqlite3_vtab *tab){
972 return echoTransactionCall(tab, "xRollback");
973}
974
975/*
drhe94b0c32006-07-08 18:09:15 +0000976** Implementation of "GLOB" function on the echo module. Pass
977** all arguments to the ::echo_glob_overload procedure of TCL
978** and return the result of that procedure as a string.
979*/
980static void overloadedGlobFunction(
981 sqlite3_context *pContext,
982 int nArg,
983 sqlite3_value **apArg
984){
985 Tcl_Interp *interp = sqlite3_user_data(pContext);
986 Tcl_DString str;
987 int i;
988 int rc;
989 Tcl_DStringInit(&str);
990 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
991 for(i=0; i<nArg; i++){
992 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
993 }
994 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
995 Tcl_DStringFree(&str);
996 if( rc ){
997 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
998 }else{
999 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1000 -1, SQLITE_TRANSIENT);
1001 }
1002 Tcl_ResetResult(interp);
1003}
1004
1005/*
1006** This is the xFindFunction implementation for the echo module.
1007** SQLite calls this routine when the first argument of a function
1008** is a column of an echo virtual table. This routine can optionally
1009** override the implementation of that function. It will choose to
1010** do so if the function is named "glob", and a TCL command named
1011** ::echo_glob_overload exists.
1012*/
1013static int echoFindFunction(
1014 sqlite3_vtab *vtab,
1015 int nArg,
1016 const char *zFuncName,
1017 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1018 void **ppArg
1019){
1020 echo_vtab *pVtab = (echo_vtab *)vtab;
1021 Tcl_Interp *interp = pVtab->interp;
1022 Tcl_CmdInfo info;
1023 if( strcmp(zFuncName,"glob")!=0 ){
1024 return 0;
1025 }
1026 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1027 return 0;
1028 }
1029 *pxFunc = overloadedGlobFunction;
1030 *ppArg = interp;
1031 return 1;
1032}
1033
danielk1977182c4ba2007-06-27 15:53:34 +00001034static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1035 int rc = SQLITE_OK;
1036 echo_vtab *p = (echo_vtab *)vtab;
1037
1038 if( p->isPattern ){
1039 int nThis = strlen(p->zThis);
1040 char *zSql = sqlite3MPrintf("ALTER TABLE %s RENAME TO %s%s",
1041 p->zTableName, zNewName, &p->zTableName[nThis]
1042 );
1043 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drha693bcd2007-07-20 00:35:58 +00001044 sqliteFree(zSql);
danielk1977182c4ba2007-06-27 15:53:34 +00001045 }
1046
1047 return rc;
1048}
1049
drhe94b0c32006-07-08 18:09:15 +00001050/*
danielk1977cc013f82006-06-24 06:36:11 +00001051** A virtual table module that merely "echos" the contents of another
1052** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +00001053*/
1054static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +00001055 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +00001056 echoCreate,
1057 echoConnect,
drha967e882006-06-13 01:04:52 +00001058 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +00001059 echoDisconnect,
1060 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +00001061 echoOpen, /* xOpen - open a cursor */
1062 echoClose, /* xClose - close a cursor */
1063 echoFilter, /* xFilter - configure scan constraints */
1064 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +00001065 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +00001066 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +00001067 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +00001068 echoUpdate, /* xUpdate - write data */
1069 echoBegin, /* xBegin - begin transaction */
1070 echoSync, /* xSync - sync transaction */
1071 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +00001072 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +00001073 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:34 +00001074 echoRename, /* xRename - rename the table */
drhb9bb7c12006-06-11 23:41:55 +00001075};
1076
1077/*
1078** Decode a pointer to an sqlite3 object.
1079*/
1080static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
1081 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
1082 return TCL_OK;
1083}
1084
drhb9bb7c12006-06-11 23:41:55 +00001085/*
1086** Register the echo virtual table module.
1087*/
1088static int register_echo_module(
1089 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1090 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1091 int objc, /* Number of arguments */
1092 Tcl_Obj *CONST objv[] /* Command arguments */
1093){
1094 sqlite3 *db;
1095 if( objc!=2 ){
1096 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1097 return TCL_ERROR;
1098 }
1099 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977d1ab1ba2006-06-15 04:28:13 +00001100 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +00001101 return TCL_OK;
1102}
1103
danielk19775017dc32006-06-24 09:34:22 +00001104/*
1105** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1106**
1107** sqlite3_declare_vtab DB SQL
1108*/
1109static int declare_vtab(
1110 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1111 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1112 int objc, /* Number of arguments */
1113 Tcl_Obj *CONST objv[] /* Command arguments */
1114){
1115 sqlite3 *db;
1116 int rc;
1117 if( objc!=3 ){
1118 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1119 return TCL_ERROR;
1120 }
1121 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1122 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1123 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001124 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001125 return TCL_ERROR;
1126 }
1127 return TCL_OK;
1128}
1129
danielk1977cc013f82006-06-24 06:36:11 +00001130#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001131
1132/*
1133** Register commands with the TCL interpreter.
1134*/
1135int Sqlitetest8_Init(Tcl_Interp *interp){
1136 static struct {
1137 char *zName;
1138 Tcl_ObjCmdProc *xProc;
1139 void *clientData;
1140 } aObjCmd[] = {
danielk1977cc013f82006-06-24 06:36:11 +00001141#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001142 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +00001143 { "sqlite3_declare_vtab", declare_vtab, 0 },
danielk1977cc013f82006-06-24 06:36:11 +00001144#endif
drhb9bb7c12006-06-11 23:41:55 +00001145 };
1146 int i;
1147 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1148 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1149 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1150 }
1151 return TCL_OK;
1152}