blob: 70c6c57f119eee6890a439122b48d3bd0593b3dd [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**
danielk197731dad9d2007-08-16 11:36:15 +000016** $Id: test8.c,v 1.51 2007/08/16 11:36:15 danielk1977 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
drh17435752007-08-16 04:30:38 +0000125** set to point at sqlite3_malloc()'d space containing the array of
126** nCol column names. The caller is responsible for calling sqlite3_free
danielk1977cc013f82006-06-24 06:36:11 +0000127** 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 */
drh17435752007-08-16 04:30:38 +0000145 zSql = sqlite3_mprintf("SELECT * FROM %Q", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000146 if( !zSql ){
147 rc = SQLITE_NOMEM;
148 goto out;
149 }
150 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000151 sqlite3_free(zSql);
danielk1977cc013f82006-06-24 06:36:11 +0000152
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 }
danielk197731dad9d2007-08-16 11:36:15 +0000166 aCol = (char **)sqlite3MallocZero(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 */
danielk197731dad9d2007-08-16 11:36:15 +0000216 aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000217 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 */
danielk19771e536952007-08-16 10:09:01 +0000223 zSql = sqlite3MPrintf(0, "PRAGMA index_list(%s)", zTab);
danielk1977cc013f82006-06-24 06:36:11 +0000224 if( !zSql ){
225 rc = SQLITE_NOMEM;
226 goto get_index_array_out;
227 }
228 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
drh17435752007-08-16 04:30:38 +0000229 sqlite3_free(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;
danielk19771e536952007-08-16 10:09:01 +0000237 zSql = sqlite3MPrintf(0, "PRAGMA index_info(%s)", zIdx);
danielk1977cc013f82006-06-24 06:36:11 +0000238 if( !zSql ){
239 rc = SQLITE_NOMEM;
240 goto get_index_array_out;
241 }
242 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
drh17435752007-08-16 04:30:38 +0000243 sqlite3_free(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 ){
drh17435752007-08-16 04:30:38 +0000266 sqlite3_free(aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000267 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;
drh17435752007-08-16 04:30:38 +0000342 sqlite3_free(p->aIndex);
343 sqlite3_free(p->aCol);
344 sqlite3_free(p->zThis);
345 sqlite3_free(p->zTableName);
346 sqlite3_free(p->zLogName);
347 sqlite3_free(p);
danielk1977a4e76362006-06-14 06:31:28 +0000348 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 */
danielk197731dad9d2007-08-16 11:36:15 +0000367 pVtab = sqlite3MallocZero( 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 */
danielk19771e536952007-08-16 10:09:01 +0000375 pVtab->zThis = sqlite3MPrintf(0, "%s", argv[2]);
danielk1977182c4ba2007-06-27 15:53:34 +0000376 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 ){
danielk19771e536952007-08-16 10:09:01 +0000383 pVtab->zTableName = sqlite3MPrintf(0, "%s", argv[3]);
danielk1977182c4ba2007-06-27 15:53:34 +0000384 dequoteString(pVtab->zTableName);
385 if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
danielk19771e536952007-08-16 10:09:01 +0000386 char *z = sqlite3MPrintf(0, "%s%s", argv[2], &(pVtab->zTableName[1]));
drh17435752007-08-16 04:30:38 +0000387 sqlite3_free(pVtab->zTableName);
danielk1977182c4ba2007-06-27 15:53:34 +0000388 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;
danielk19771e536952007-08-16 10:09:01 +0000443 pVtab->zLogName = sqlite3MPrintf(0, "%s", argv[4]);
444 zSql = sqlite3MPrintf(0, "CREATE TABLE %Q(logmsg)", pVtab->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000445 rc = sqlite3_exec(db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000446 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000447 }
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;
danielk19771e536952007-08-16 10:09:01 +0000485 zSql = sqlite3MPrintf(0, "DROP TABLE %Q", p->zLogName);
danielk1977a298e902006-06-22 09:53:48 +0000486 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +0000487 sqlite3_free(zSql);
danielk1977a298e902006-06-22 09:53:48 +0000488 }
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;
danielk197731dad9d2007-08-16 11:36:15 +0000501 pCur = sqlite3MallocZero(sizeof(echo_cursor));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000502 *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;
drh17435752007-08-16 04:30:38 +0000514 sqlite3_free(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){
danielk197731dad9d2007-08-16 11:36:15 +0000531 int rc = SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000532 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000533
danielk197731dad9d2007-08-16 11:36:15 +0000534 if( pCur->pStmt ){
535 rc = sqlite3_step(pCur->pStmt);
536 if( rc==SQLITE_ROW ){
537 rc = SQLITE_OK;
538 }else{
539 rc = sqlite3_finalize(pCur->pStmt);
540 pCur->pStmt = 0;
541 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000542 }
543
544 return rc;
545}
546
danielk1977cc013f82006-06-24 06:36:11 +0000547/*
548** Echo virtual table module xColumn method.
549*/
danielk19775fac9f82006-06-13 14:16:58 +0000550static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000551 int iCol = i + 1;
552 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk1977fbbe0052006-06-21 07:02:33 +0000553 if( !pStmt ){
554 sqlite3_result_null(ctx);
555 }else{
556 assert( sqlite3_data_count(pStmt)>iCol );
557 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
558 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000559 return SQLITE_OK;
560}
561
danielk1977cc013f82006-06-24 06:36:11 +0000562/*
563** Echo virtual table module xRowid method.
564*/
danielk19775fac9f82006-06-13 14:16:58 +0000565static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000566 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
567 *pRowid = sqlite3_column_int64(pStmt, 0);
568 return SQLITE_OK;
569}
570
danielk197798331532006-06-14 10:55:52 +0000571/*
572** Compute a simple hash of the null terminated string zString.
573**
574** This module uses only sqlite3_index_info.idxStr, not
575** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
576** in echoBestIndex(), idxNum is set to the corresponding hash value.
577** In echoFilter(), code assert()s that the supplied idxNum value is
578** indeed the hash of the supplied idxStr.
579*/
580static int hashString(const char *zString){
581 int val = 0;
582 int ii;
583 for(ii=0; zString[ii]; ii++){
584 val = (val << 3) + (int)zString[ii];
585 }
586 return val;
587}
588
danielk1977cc013f82006-06-24 06:36:11 +0000589/*
590** Echo virtual table module xFilter method.
591*/
danielk19775fac9f82006-06-13 14:16:58 +0000592static int echoFilter(
593 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000594 int idxNum, const char *idxStr,
595 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000596){
597 int rc;
drh4be8b512006-06-13 23:51:34 +0000598 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000599
600 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
601 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
602 sqlite3 *db = pVtab->db;
603
danielk1977cc013f82006-06-24 06:36:11 +0000604 /* Check that idxNum matches idxStr */
605 assert( idxNum==hashString(idxStr) );
606
607 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000608 appendToEchoModule(pVtab->interp, "xFilter");
609 appendToEchoModule(pVtab->interp, idxStr);
610 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000611 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000612 }
613
danielk19775fac9f82006-06-13 14:16:58 +0000614 sqlite3_finalize(pCur->pStmt);
615 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000616
617 /* Prepare the SQL statement created by echoBestIndex and bind the
618 ** runtime parameters passed to this function to it.
619 */
drh4be8b512006-06-13 23:51:34 +0000620 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000621 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000622 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk1977cc013f82006-06-24 06:36:11 +0000623 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000624 }
danielk1977cc013f82006-06-24 06:36:11 +0000625
626 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000627 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000628 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000629 }
630
danielk1977be718892006-06-23 08:05:19 +0000631 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000632}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000633
danielk1977cc013f82006-06-24 06:36:11 +0000634
635/*
636** A helper function used by echoUpdate() and echoBestIndex() for
637** manipulating strings in concert with the sqlite3_mprintf() function.
638**
639** Parameter pzStr points to a pointer to a string allocated with
640** sqlite3_mprintf. The second parameter, zAppend, points to another
641** string. The two strings are concatenated together and *pzStr
642** set to point at the result. The initial buffer pointed to by *pzStr
643** is deallocated via sqlite3_free().
644**
645** If the third argument, doFree, is true, then sqlite3_free() is
646** also called to free the buffer pointed to by zAppend.
647*/
648static void string_concat(char **pzStr, char *zAppend, int doFree){
649 char *zIn = *pzStr;
650 if( zIn ){
651 char *zTemp = zIn;
652 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
653 sqlite3_free(zTemp);
654 }else{
655 zIn = sqlite3_mprintf("%s", zAppend);
656 }
657 *pzStr = zIn;
658 if( doFree ){
659 sqlite3_free(zAppend);
660 }
661}
662
drhb9bb7c12006-06-11 23:41:55 +0000663/*
danielk19775fac9f82006-06-13 14:16:58 +0000664** The echo module implements the subset of query constraints and sort
665** orders that may take advantage of SQLite indices on the underlying
666** real table. For example, if the real table is declared as:
667**
668** CREATE TABLE real(a, b, c);
669** CREATE INDEX real_index ON real(b);
670**
671** then the echo module handles WHERE or ORDER BY clauses that refer
672** to the column "b", but not "a" or "c". If a multi-column index is
673** present, only it's left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000674**
675** This xBestIndex method encodes the proposed search strategy as
676** an SQL query on the real table underlying the virtual echo module
677** table and stores the query in sqlite3_index_info.idxStr. The SQL
678** statement is of the form:
679**
680** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
681**
682** where the <where-clause> and <order-by-clause> are determined
683** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000684*/
danielk19775fac9f82006-06-13 14:16:58 +0000685static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
686 int ii;
drh4be8b512006-06-13 23:51:34 +0000687 char *zQuery = 0;
688 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000689 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000690 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000691 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000692 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000693 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000694
695 int nRow;
696 int useIdx = 0;
697 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000698 int useCost = 0;
699 double cost;
danielk197774cdba42006-06-19 12:02:58 +0000700
701 /* Determine the number of rows in the table and store this value in local
702 ** variable nRow. The 'estimated-cost' of the scan will be the number of
703 ** rows in the table for a linear scan, or the log (base 2) of the
704 ** number of rows if the proposed scan uses an index.
705 */
danielk1977780b1d92007-03-30 14:56:34 +0000706 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
707 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
708 useCost = 1;
709 } else {
710 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
711 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
712 sqlite3_free(zQuery);
713 if( rc!=SQLITE_OK ){
714 return rc;
715 }
716 sqlite3_step(pStmt);
717 nRow = sqlite3_column_int(pStmt, 0);
718 rc = sqlite3_finalize(pStmt);
719 if( rc!=SQLITE_OK ){
720 return rc;
721 }
danielk197774cdba42006-06-19 12:02:58 +0000722 }
danielk19775fac9f82006-06-13 14:16:58 +0000723
drh4be8b512006-06-13 23:51:34 +0000724 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000725 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
726 const struct sqlite3_index_constraint *pConstraint;
727 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000728 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000729
730 pConstraint = &pIdxInfo->aConstraint[ii];
731 pUsage = &pIdxInfo->aConstraintUsage[ii];
732
drh383736b2006-10-08 18:56:57 +0000733 iCol = pConstraint->iColumn;
danielk19775fac9f82006-06-13 14:16:58 +0000734 if( pVtab->aIndex[iCol] ){
735 char *zCol = pVtab->aCol[iCol];
736 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000737 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000738 if( iCol<0 ){
739 zCol = "rowid";
740 }
danielk19775fac9f82006-06-13 14:16:58 +0000741 switch( pConstraint->op ){
742 case SQLITE_INDEX_CONSTRAINT_EQ:
743 zOp = "="; break;
744 case SQLITE_INDEX_CONSTRAINT_LT:
745 zOp = "<"; break;
746 case SQLITE_INDEX_CONSTRAINT_GT:
747 zOp = ">"; break;
748 case SQLITE_INDEX_CONSTRAINT_LE:
749 zOp = "<="; break;
750 case SQLITE_INDEX_CONSTRAINT_GE:
751 zOp = ">="; break;
752 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000753 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000754 }
drh1a90e092006-06-14 22:07:10 +0000755 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000756 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
757 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000758 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000759 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000760 }
danielk1977cc013f82006-06-24 06:36:11 +0000761 string_concat(&zQuery, zNew, 1);
762
drh4be8b512006-06-13 23:51:34 +0000763 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000764 pUsage->argvIndex = ++nArg;
765 pUsage->omit = 1;
766 }
767 }
danielk197747d08092006-06-14 07:41:31 +0000768
769 /* If there is only one term in the ORDER BY clause, and it is
770 ** on a column that this virtual table has an index for, then consume
771 ** the ORDER BY clause.
772 */
773 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000774 int iCol = pIdxInfo->aOrderBy->iColumn;
775 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000776 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000777 if( iCol<0 ){
778 zCol = "rowid";
779 }
danielk1977cc013f82006-06-24 06:36:11 +0000780 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
781 string_concat(&zQuery, zNew, 1);
danielk197747d08092006-06-14 07:41:31 +0000782 pIdxInfo->orderByConsumed = 1;
783 }
784
danielk19775fac9f82006-06-13 14:16:58 +0000785 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000786 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000787
danielk197798331532006-06-14 10:55:52 +0000788 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000789 pIdxInfo->idxStr = zQuery;
790 pIdxInfo->needToFreeIdxStr = 1;
danielk1977780b1d92007-03-30 14:56:34 +0000791 if (useCost) {
792 pIdxInfo->estimatedCost = cost;
793 } else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000794 /* Approximation of log2(nRow). */
795 for( ii=0; ii<(sizeof(int)*8); ii++ ){
796 if( nRow & (1<<ii) ){
797 pIdxInfo->estimatedCost = (double)ii;
798 }
799 }
800 } else {
801 pIdxInfo->estimatedCost = (double)nRow;
802 }
803 return rc;
drha967e882006-06-13 01:04:52 +0000804}
805
danielk1977176f4d22006-06-15 10:41:15 +0000806/*
danielk1977cc013f82006-06-24 06:36:11 +0000807** The xUpdate method for echo module virtual tables.
808**
danielk1977176f4d22006-06-15 10:41:15 +0000809** apData[0] apData[1] apData[2..]
810**
811** INTEGER DELETE
812**
813** INTEGER NULL (nCol args) UPDATE (do not set rowid)
814** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
815**
816** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
817** NULL INTEGER (nCol args) INSERT (incl. rowid value)
818**
819*/
danielk19771f6eec52006-06-16 06:17:47 +0000820int echoUpdate(
821 sqlite3_vtab *tab,
822 int nData,
823 sqlite3_value **apData,
824 sqlite_int64 *pRowid
825){
danielk197726e41442006-06-14 15:16:35 +0000826 echo_vtab *pVtab = (echo_vtab *)tab;
827 sqlite3 *db = pVtab->db;
828 int rc = SQLITE_OK;
829
danielk1977176f4d22006-06-15 10:41:15 +0000830 sqlite3_stmt *pStmt;
831 char *z = 0; /* SQL statement to execute */
832 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
833 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
834 int i; /* Counter variable used by for loops */
835
danielk197726e41442006-06-14 15:16:35 +0000836 assert( nData==pVtab->nCol+2 || nData==1 );
837
drh5aec0422006-06-14 23:43:31 +0000838 /* If apData[0] is an integer and nData>1 then do an UPDATE */
839 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000840 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000841 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000842
843 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
844 bindArgZero = 1;
845
846 if( bindArgOne ){
847 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000848 zSep = ",";
849 }
850 for(i=2; i<nData; i++){
851 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000852 string_concat(&z, sqlite3_mprintf(
853 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000854 zSep = ",";
855 }
danielk1977176f4d22006-06-15 10:41:15 +0000856 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000857 }
858
danielk1977176f4d22006-06-15 10:41:15 +0000859 /* If apData[0] is an integer and nData==1 then do a DELETE */
860 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
861 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
862 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000863 }
864
danielk1977176f4d22006-06-15 10:41:15 +0000865 /* If the first argument is NULL and there are more than two args, INSERT */
866 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000867 int ii;
868 char *zInsert = 0;
869 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000870
danielk19774b2688a2006-06-20 11:01:07 +0000871 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000872 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
873 bindArgOne = 1;
874 zValues = sqlite3_mprintf("?");
875 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000876 }
877
danielk1977176f4d22006-06-15 10:41:15 +0000878 assert((pVtab->nCol+2)==nData);
879 for(ii=2; ii<nData; ii++){
880 string_concat(&zInsert,
881 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
882 string_concat(&zValues,
883 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000884 }
885
danielk1977176f4d22006-06-15 10:41:15 +0000886 string_concat(&z, zInsert, 1);
887 string_concat(&z, ") VALUES(", 0);
888 string_concat(&z, zValues, 1);
889 string_concat(&z, ")", 0);
890 }
891
892 /* Anything else is an error */
893 else{
894 assert(0);
895 return SQLITE_ERROR;
896 }
897
898 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
899 assert( rc!=SQLITE_OK || pStmt );
900 sqlite3_free(z);
901 if( rc==SQLITE_OK ) {
902 if( bindArgZero ){
903 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000904 }
danielk1977176f4d22006-06-15 10:41:15 +0000905 if( bindArgOne ){
906 sqlite3_bind_value(pStmt, 1, apData[1]);
907 }
908 for(i=2; i<nData; i++){
909 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
910 }
911 sqlite3_step(pStmt);
912 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000913 }
914
danielk19771f6eec52006-06-16 06:17:47 +0000915 if( pRowid && rc==SQLITE_OK ){
916 *pRowid = sqlite3_last_insert_rowid(db);
917 }
918
danielk197726e41442006-06-14 15:16:35 +0000919 return rc;
920}
921
drha967e882006-06-13 01:04:52 +0000922/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000923** xBegin, xSync, xCommit and xRollback callbacks for echo module
924** virtual tables. Do nothing other than add the name of the callback
925** to the $::echo_module Tcl variable.
926*/
927static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
928 char *z;
929 echo_vtab *pVtab = (echo_vtab *)tab;
930 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
931 appendToEchoModule(pVtab->interp, zCall);
932 appendToEchoModule(pVtab->interp, z);
933 sqlite3_free(z);
934 return SQLITE_OK;
935}
936static int echoBegin(sqlite3_vtab *tab){
danielk19775017dc32006-06-24 09:34:22 +0000937 echo_vtab *pVtab = (echo_vtab *)tab;
938 Tcl_Interp *interp = pVtab->interp;
939 const char *zVal;
940
941 echoTransactionCall(tab, "xBegin");
942
943 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
944 ** and it is set to the name of the real table underlying this virtual
945 ** echo module table, then cause this xSync operation to fail.
946 */
947 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
948 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
949 return SQLITE_ERROR;
950 }
951 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +0000952}
953static int echoSync(sqlite3_vtab *tab){
954 echo_vtab *pVtab = (echo_vtab *)tab;
955 Tcl_Interp *interp = pVtab->interp;
956 const char *zVal;
957
958 echoTransactionCall(tab, "xSync");
959
960 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
961 ** and it is set to the name of the real table underlying this virtual
962 ** echo module table, then cause this xSync operation to fail.
963 */
964 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
965 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
966 return -1;
967 }
968 return SQLITE_OK;
969}
970static int echoCommit(sqlite3_vtab *tab){
971 return echoTransactionCall(tab, "xCommit");
972}
973static int echoRollback(sqlite3_vtab *tab){
974 return echoTransactionCall(tab, "xRollback");
975}
976
977/*
drhe94b0c32006-07-08 18:09:15 +0000978** Implementation of "GLOB" function on the echo module. Pass
979** all arguments to the ::echo_glob_overload procedure of TCL
980** and return the result of that procedure as a string.
981*/
982static void overloadedGlobFunction(
983 sqlite3_context *pContext,
984 int nArg,
985 sqlite3_value **apArg
986){
987 Tcl_Interp *interp = sqlite3_user_data(pContext);
988 Tcl_DString str;
989 int i;
990 int rc;
991 Tcl_DStringInit(&str);
992 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
993 for(i=0; i<nArg; i++){
994 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
995 }
996 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
997 Tcl_DStringFree(&str);
998 if( rc ){
999 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
1000 }else{
1001 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
1002 -1, SQLITE_TRANSIENT);
1003 }
1004 Tcl_ResetResult(interp);
1005}
1006
1007/*
1008** This is the xFindFunction implementation for the echo module.
1009** SQLite calls this routine when the first argument of a function
1010** is a column of an echo virtual table. This routine can optionally
1011** override the implementation of that function. It will choose to
1012** do so if the function is named "glob", and a TCL command named
1013** ::echo_glob_overload exists.
1014*/
1015static int echoFindFunction(
1016 sqlite3_vtab *vtab,
1017 int nArg,
1018 const char *zFuncName,
1019 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
1020 void **ppArg
1021){
1022 echo_vtab *pVtab = (echo_vtab *)vtab;
1023 Tcl_Interp *interp = pVtab->interp;
1024 Tcl_CmdInfo info;
1025 if( strcmp(zFuncName,"glob")!=0 ){
1026 return 0;
1027 }
1028 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
1029 return 0;
1030 }
1031 *pxFunc = overloadedGlobFunction;
1032 *ppArg = interp;
1033 return 1;
1034}
1035
danielk1977182c4ba2007-06-27 15:53:34 +00001036static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1037 int rc = SQLITE_OK;
1038 echo_vtab *p = (echo_vtab *)vtab;
1039
1040 if( p->isPattern ){
1041 int nThis = strlen(p->zThis);
danielk19771e536952007-08-16 10:09:01 +00001042 char *zSql = sqlite3MPrintf(0, "ALTER TABLE %s RENAME TO %s%s",
danielk1977182c4ba2007-06-27 15:53:34 +00001043 p->zTableName, zNewName, &p->zTableName[nThis]
1044 );
1045 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
drh17435752007-08-16 04:30:38 +00001046 sqlite3_free(zSql);
danielk1977182c4ba2007-06-27 15:53:34 +00001047 }
1048
1049 return rc;
1050}
1051
drhe94b0c32006-07-08 18:09:15 +00001052/*
danielk1977cc013f82006-06-24 06:36:11 +00001053** A virtual table module that merely "echos" the contents of another
1054** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +00001055*/
1056static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +00001057 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +00001058 echoCreate,
1059 echoConnect,
drha967e882006-06-13 01:04:52 +00001060 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +00001061 echoDisconnect,
1062 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +00001063 echoOpen, /* xOpen - open a cursor */
1064 echoClose, /* xClose - close a cursor */
1065 echoFilter, /* xFilter - configure scan constraints */
1066 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +00001067 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +00001068 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +00001069 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +00001070 echoUpdate, /* xUpdate - write data */
1071 echoBegin, /* xBegin - begin transaction */
1072 echoSync, /* xSync - sync transaction */
1073 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +00001074 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +00001075 echoFindFunction, /* xFindFunction - function overloading */
danielk1977182c4ba2007-06-27 15:53:34 +00001076 echoRename, /* xRename - rename the table */
drhb9bb7c12006-06-11 23:41:55 +00001077};
1078
1079/*
1080** Decode a pointer to an sqlite3 object.
1081*/
1082static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
1083 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
1084 return TCL_OK;
1085}
1086
drhb9bb7c12006-06-11 23:41:55 +00001087/*
1088** Register the echo virtual table module.
1089*/
1090static int register_echo_module(
1091 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1092 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1093 int objc, /* Number of arguments */
1094 Tcl_Obj *CONST objv[] /* Command arguments */
1095){
1096 sqlite3 *db;
1097 if( objc!=2 ){
1098 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1099 return TCL_ERROR;
1100 }
1101 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977d1ab1ba2006-06-15 04:28:13 +00001102 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +00001103 return TCL_OK;
1104}
1105
danielk19775017dc32006-06-24 09:34:22 +00001106/*
1107** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1108**
1109** sqlite3_declare_vtab DB SQL
1110*/
1111static int declare_vtab(
1112 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1113 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1114 int objc, /* Number of arguments */
1115 Tcl_Obj *CONST objv[] /* Command arguments */
1116){
1117 sqlite3 *db;
1118 int rc;
1119 if( objc!=3 ){
1120 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1121 return TCL_ERROR;
1122 }
1123 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1124 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1125 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001126 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001127 return TCL_ERROR;
1128 }
1129 return TCL_OK;
1130}
1131
danielk1977cc013f82006-06-24 06:36:11 +00001132#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001133
1134/*
1135** Register commands with the TCL interpreter.
1136*/
1137int Sqlitetest8_Init(Tcl_Interp *interp){
1138 static struct {
1139 char *zName;
1140 Tcl_ObjCmdProc *xProc;
1141 void *clientData;
1142 } aObjCmd[] = {
danielk1977cc013f82006-06-24 06:36:11 +00001143#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001144 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +00001145 { "sqlite3_declare_vtab", declare_vtab, 0 },
danielk1977cc013f82006-06-24 06:36:11 +00001146#endif
drhb9bb7c12006-06-11 23:41:55 +00001147 };
1148 int i;
1149 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1150 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1151 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1152 }
1153 return TCL_OK;
1154}