blob: d1081d7d39a63af8f7f93e25c6805182ce87bfad [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**
danielk1977780b1d92007-03-30 14:56:34 +000016** $Id: test8.c,v 1.45 2007/03/30 14:56:35 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
danielk1977a4e76362006-06-14 06:31:28 +000064 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:48 +000065 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:28 +000066 int nCol; /* Number of columns in the real table */
67 int *aIndex; /* Array of size nCol. True if column has an index */
68 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000069};
70
71/* An echo cursor object */
72struct echo_cursor {
73 sqlite3_vtab_cursor base;
74 sqlite3_stmt *pStmt;
danielk1977b7a7b9a2006-06-13 10:24:42 +000075};
76
danielk1977cc013f82006-06-24 06:36:11 +000077/*
78** Retrieve the column names for the table named zTab via database
79** connection db. SQLITE_OK is returned on success, or an sqlite error
80** code otherwise.
81**
82** If successful, the number of columns is written to *pnCol. *paCol is
83** set to point at sqliteMalloc()'d space containing the array of
84** nCol column names. The caller is responsible for calling sqliteFree
85** on *paCol.
86*/
danielk19775fac9f82006-06-13 14:16:58 +000087static int getColumnNames(
88 sqlite3 *db,
89 const char *zTab,
90 char ***paCol,
91 int *pnCol
92){
93 char **aCol = 0;
danielk1977cc013f82006-06-24 06:36:11 +000094 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +000095 sqlite3_stmt *pStmt = 0;
96 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19 +000097 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58 +000098
danielk1977cc013f82006-06-24 06:36:11 +000099 /* Prepare the statement "SELECT * FROM <tbl>". The column names
100 ** of the result set of the compiled SELECT will be the same as
101 ** the column names of table <tbl>.
102 */
103 zSql = sqlite3MPrintf("SELECT * FROM %Q", zTab);
104 if( !zSql ){
105 rc = SQLITE_NOMEM;
106 goto out;
107 }
108 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
109 sqliteFree(zSql);
110
danielk19775fac9f82006-06-13 14:16:58 +0000111 if( rc==SQLITE_OK ){
112 int ii;
danielk1977cc013f82006-06-24 06:36:11 +0000113 int nBytes;
114 char *zSpace;
danielk19775fac9f82006-06-13 14:16:58 +0000115 nCol = sqlite3_column_count(pStmt);
danielk1977cc013f82006-06-24 06:36:11 +0000116
117 /* Figure out how much space to allocate for the array of column names
118 ** (including space for the strings themselves). Then allocate it.
119 */
120 nBytes = sizeof(char *) * nCol;
121 for(ii=0; ii<nCol; ii++){
122 nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1);
123 }
124 aCol = (char **)sqliteMalloc(nBytes);
danielk19775fac9f82006-06-13 14:16:58 +0000125 if( !aCol ){
126 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11 +0000127 goto out;
danielk19775fac9f82006-06-13 14:16:58 +0000128 }
danielk1977cc013f82006-06-24 06:36:11 +0000129
130 /* Copy the column names into the allocated space and set up the
131 ** pointers in the aCol[] array.
132 */
133 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58 +0000134 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11 +0000135 aCol[ii] = zSpace;
136 zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
137 zSpace++;
danielk19775fac9f82006-06-13 14:16:58 +0000138 }
danielk1977cc013f82006-06-24 06:36:11 +0000139 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58 +0000140 }
141
142 *paCol = aCol;
143 *pnCol = nCol;
144
danielk1977cc013f82006-06-24 06:36:11 +0000145out:
danielk19775fac9f82006-06-13 14:16:58 +0000146 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000147 return rc;
148}
149
danielk1977cc013f82006-06-24 06:36:11 +0000150/*
151** Parameter zTab is the name of a table in database db with nCol
152** columns. This function allocates an array of integers nCol in
153** size and populates it according to any implicit or explicit
154** indices on table zTab.
155**
156** If successful, SQLITE_OK is returned and *paIndex set to point
157** at the allocated array. Otherwise, an error code is returned.
158**
159** See comments associated with the member variable aIndex above
160** "struct echo_vtab" for details of the contents of the array.
161*/
162static int getIndexArray(
163 sqlite3 *db, /* Database connection */
164 const char *zTab, /* Name of table in database db */
165 int nCol,
166 int **paIndex
167){
danielk19775fac9f82006-06-13 14:16:58 +0000168 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000169 int *aIndex = 0;
170 int rc;
danielk1977cc013f82006-06-24 06:36:11 +0000171 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000172
danielk1977cc013f82006-06-24 06:36:11 +0000173 /* Allocate space for the index array */
danielk19775fac9f82006-06-13 14:16:58 +0000174 aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
175 if( !aIndex ){
176 rc = SQLITE_NOMEM;
177 goto get_index_array_out;
178 }
179
danielk1977cc013f82006-06-24 06:36:11 +0000180 /* Compile an sqlite pragma to loop through all indices on table zTab */
181 zSql = sqlite3MPrintf("PRAGMA index_list(%s)", zTab);
182 if( !zSql ){
183 rc = SQLITE_NOMEM;
184 goto get_index_array_out;
185 }
186 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
187 sqliteFree(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000188
danielk1977cc013f82006-06-24 06:36:11 +0000189 /* For each index, figure out the left-most column and set the
190 ** corresponding entry in aIndex[] to 1.
191 */
danielk19775fac9f82006-06-13 14:16:58 +0000192 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000193 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58 +0000194 sqlite3_stmt *pStmt2 = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000195 zSql = sqlite3MPrintf("PRAGMA index_info(%s)", zIdx);
196 if( !zSql ){
197 rc = SQLITE_NOMEM;
198 goto get_index_array_out;
199 }
200 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
201 sqliteFree(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000202 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
203 int cid = sqlite3_column_int(pStmt2, 1);
204 assert( cid>=0 && cid<nCol );
205 aIndex[cid] = 1;
206 }
danielk1977be718892006-06-23 08:05:19 +0000207 if( pStmt2 ){
208 rc = sqlite3_finalize(pStmt2);
209 }
danielk19775fac9f82006-06-13 14:16:58 +0000210 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000211 goto get_index_array_out;
212 }
213 }
214
danielk19775fac9f82006-06-13 14:16:58 +0000215
216get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000217 if( pStmt ){
218 int rc2 = sqlite3_finalize(pStmt);
219 if( rc==SQLITE_OK ){
220 rc = rc2;
221 }
222 }
danielk19775fac9f82006-06-13 14:16:58 +0000223 if( rc!=SQLITE_OK ){
224 sqliteFree(aIndex);
225 aIndex = 0;
226 }
227 *paIndex = aIndex;
228 return rc;
229}
230
danielk197778efaba2006-06-12 06:09:17 +0000231/*
232** Global Tcl variable $echo_module is a list. This routine appends
233** the string element zArg to that list in interpreter interp.
234*/
drha967e882006-06-13 01:04:52 +0000235static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000236 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000237 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000238}
239
danielk19777e6ebfb2006-06-12 11:24:37 +0000240/*
241** This function is called from within the echo-modules xCreate and
242** xConnect methods. The argc and argv arguments are copies of those
243** passed to the calling method. This function is responsible for
244** calling sqlite3_declare_vtab() to declare the schema of the virtual
245** table being created or connected.
246**
247** If the constructor was passed just one argument, i.e.:
248**
249** CREATE TABLE t1 AS echo(t2);
250**
251** Then t2 is assumed to be the name of a *real* database table. The
252** schema of the virtual table is declared by passing a copy of the
253** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
254** Hence, the virtual table should have exactly the same column names and
255** types as the real table.
256*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000257static int echoDeclareVtab(
258 echo_vtab *pVtab,
259 sqlite3 *db,
260 int argc,
drhe4102962006-09-11 00:34:22 +0000261 const char *const*argv
danielk1977b7a7b9a2006-06-13 10:24:42 +0000262){
danielk19777e6ebfb2006-06-12 11:24:37 +0000263 int rc = SQLITE_OK;
264
danielk1977a298e902006-06-22 09:53:48 +0000265 if( argc>=4 ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000266 sqlite3_stmt *pStmt = 0;
267 sqlite3_prepare(db,
268 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
269 -1, &pStmt, 0);
danielk197770ba1642006-06-21 16:02:42 +0000270 sqlite3_bind_text(pStmt, 1, argv[3], -1, 0);
danielk19777e6ebfb2006-06-12 11:24:37 +0000271 if( sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000272 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
danielk19777e6ebfb2006-06-12 11:24:37 +0000273 sqlite3_declare_vtab(db, zCreateTable);
danielk1977be718892006-06-23 08:05:19 +0000274 rc = sqlite3_finalize(pStmt);
danielk19777e6ebfb2006-06-12 11:24:37 +0000275 } else {
danielk1977be718892006-06-23 08:05:19 +0000276 rc = sqlite3_finalize(pStmt);
277 if( rc==SQLITE_OK ){
278 rc = SQLITE_ERROR;
279 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000280 }
danielk1977be718892006-06-23 08:05:19 +0000281
danielk19775fac9f82006-06-13 14:16:58 +0000282 if( rc==SQLITE_OK ){
danielk1977cc013f82006-06-24 06:36:11 +0000283 rc = getColumnNames(db, argv[3], &pVtab->aCol, &pVtab->nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000284 }
285 if( rc==SQLITE_OK ){
danielk1977cc013f82006-06-24 06:36:11 +0000286 rc = getIndexArray(db, argv[3], pVtab->nCol, &pVtab->aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000287 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000288 }
289
290 return rc;
291}
292
danielk1977cc013f82006-06-24 06:36:11 +0000293/*
294** This function frees all runtime structures associated with the virtual
295** table pVtab.
296*/
danielk1977a4e76362006-06-14 06:31:28 +0000297static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000298 echo_vtab *p = (echo_vtab*)pVtab;
299 sqliteFree(p->aIndex);
danielk1977a4e76362006-06-14 06:31:28 +0000300 sqliteFree(p->aCol);
301 sqliteFree(p->zTableName);
danielk1977212b2182006-06-23 14:32:08 +0000302 sqliteFree(p->zLogName);
danielk1977a4e76362006-06-14 06:31:28 +0000303 sqliteFree(p);
304 return 0;
305}
306
danielk1977cc013f82006-06-24 06:36:11 +0000307/*
308** This function is called to do the work of the xConnect() method -
309** to allocate the required in-memory structures for a newly connected
310** virtual table.
311*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000312static int echoConstructor(
313 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000314 void *pAux,
drhe4102962006-09-11 00:34:22 +0000315 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000316 sqlite3_vtab **ppVtab,
317 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000318){
319 int i;
320 echo_vtab *pVtab;
321
danielk1977cc013f82006-06-24 06:36:11 +0000322 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000323 pVtab = sqliteMalloc( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000324 if( !pVtab ){
325 return SQLITE_NOMEM;
326 }
danielk19779da9d472006-06-14 06:58:15 +0000327 pVtab->interp = (Tcl_Interp *)pAux;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000328 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000329
330 /* Allocate echo_vtab.zTableName */
danielk197770ba1642006-06-21 16:02:42 +0000331 pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
danielk1977be718892006-06-23 08:05:19 +0000332 if( !pVtab->zTableName ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000333 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000334 return SQLITE_NOMEM;
335 }
336
danielk1977cc013f82006-06-24 06:36:11 +0000337 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000338 for(i=0; i<argc; i++){
339 appendToEchoModule(pVtab->interp, argv[i]);
340 }
341
danielk1977cc013f82006-06-24 06:36:11 +0000342 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
343 ** structure. If an error occurs, delete the sqlite3_vtab structure and
344 ** return an error code.
345 */
danielk1977a4e76362006-06-14 06:31:28 +0000346 if( echoDeclareVtab(pVtab, db, argc, argv) ){
347 echoDestructor((sqlite3_vtab *)pVtab);
348 return SQLITE_ERROR;
349 }
350
danielk1977cc013f82006-06-24 06:36:11 +0000351 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000352 *ppVtab = &pVtab->base;
353 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000354}
drha967e882006-06-13 01:04:52 +0000355
danielk1977cc013f82006-06-24 06:36:11 +0000356/*
357** Echo virtual table module xCreate method.
358*/
drhb9bb7c12006-06-11 23:41:55 +0000359static int echoCreate(
360 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000361 void *pAux,
drhe4102962006-09-11 00:34:22 +0000362 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000363 sqlite3_vtab **ppVtab,
364 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000365){
danielk1977a298e902006-06-22 09:53:48 +0000366 int rc = SQLITE_OK;
danielk19779da9d472006-06-14 06:58:15 +0000367 appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000368 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000369
370 /* If there were two arguments passed to the module at the SQL level
371 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
372 ** the second argument is used as a table name. Attempt to create
373 ** such a table with a single column, "logmsg". This table will
374 ** be used to log calls to the xUpdate method. It will be deleted
375 ** when the virtual table is DROPed.
376 **
377 ** Note: The main point of this is to test that we can drop tables
378 ** from within an xDestroy method call.
379 */
danielk1977a298e902006-06-22 09:53:48 +0000380 if( rc==SQLITE_OK && argc==5 ){
381 char *zSql;
382 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
383 pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
384 zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
385 rc = sqlite3_exec(db, zSql, 0, 0, 0);
386 sqliteFree(zSql);
387 }
danielk1977cc013f82006-06-24 06:36:11 +0000388
danielk1977a298e902006-06-22 09:53:48 +0000389 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000390}
danielk1977cc013f82006-06-24 06:36:11 +0000391
392/*
393** Echo virtual table module xConnect method.
394*/
drhb9bb7c12006-06-11 23:41:55 +0000395static int echoConnect(
396 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000397 void *pAux,
drhe4102962006-09-11 00:34:22 +0000398 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000399 sqlite3_vtab **ppVtab,
400 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000401){
danielk19779da9d472006-06-14 06:58:15 +0000402 appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000403 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000404}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000405
danielk1977cc013f82006-06-24 06:36:11 +0000406/*
407** Echo virtual table module xDisconnect method.
408*/
danielk19775fac9f82006-06-13 14:16:58 +0000409static int echoDisconnect(sqlite3_vtab *pVtab){
410 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
411 return echoDestructor(pVtab);
412}
danielk1977cc013f82006-06-24 06:36:11 +0000413
414/*
415** Echo virtual table module xDestroy method.
416*/
danielk19775fac9f82006-06-13 14:16:58 +0000417static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000418 int rc = SQLITE_OK;
419 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000420 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000421
422 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000423 if( p && p->zLogName ){
424 char *zSql;
425 zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName);
426 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
427 sqliteFree(zSql);
428 }
danielk1977cc013f82006-06-24 06:36:11 +0000429
danielk1977a298e902006-06-22 09:53:48 +0000430 if( rc==SQLITE_OK ){
431 rc = echoDestructor(pVtab);
432 }
433 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000434}
435
danielk1977cc013f82006-06-24 06:36:11 +0000436/*
437** Echo virtual table module xOpen method.
438*/
danielk19775fac9f82006-06-13 14:16:58 +0000439static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000440 echo_cursor *pCur;
441 pCur = sqliteMalloc(sizeof(echo_cursor));
442 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000443 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000444}
445
danielk1977cc013f82006-06-24 06:36:11 +0000446/*
447** Echo virtual table module xClose method.
448*/
danielk19775fac9f82006-06-13 14:16:58 +0000449static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000450 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000451 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000452 sqlite3_stmt *pStmt = pCur->pStmt;
453 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000454 sqliteFree(pCur);
danielk1977be718892006-06-23 08:05:19 +0000455 rc = sqlite3_finalize(pStmt);
456 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000457}
458
danielk1977a298e902006-06-22 09:53:48 +0000459/*
460** Return non-zero if the cursor does not currently point to a valid record
461** (i.e if the scan has finished), or zero otherwise.
462*/
463static int echoEof(sqlite3_vtab_cursor *cur){
464 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
465}
466
danielk1977cc013f82006-06-24 06:36:11 +0000467/*
468** Echo virtual table module xNext method.
469*/
danielk19775fac9f82006-06-13 14:16:58 +0000470static int echoNext(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000471 int rc;
472 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000473 rc = sqlite3_step(pCur->pStmt);
474
475 if( rc==SQLITE_ROW ){
danielk1977a298e902006-06-22 09:53:48 +0000476 rc = SQLITE_OK;
477 }else{
478 rc = sqlite3_finalize(pCur->pStmt);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000479 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000480 }
481
482 return rc;
483}
484
danielk1977cc013f82006-06-24 06:36:11 +0000485/*
486** Echo virtual table module xColumn method.
487*/
danielk19775fac9f82006-06-13 14:16:58 +0000488static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000489 int iCol = i + 1;
490 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk1977fbbe0052006-06-21 07:02:33 +0000491 if( !pStmt ){
492 sqlite3_result_null(ctx);
493 }else{
494 assert( sqlite3_data_count(pStmt)>iCol );
495 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
496 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000497 return SQLITE_OK;
498}
499
danielk1977cc013f82006-06-24 06:36:11 +0000500/*
501** Echo virtual table module xRowid method.
502*/
danielk19775fac9f82006-06-13 14:16:58 +0000503static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000504 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
505 *pRowid = sqlite3_column_int64(pStmt, 0);
506 return SQLITE_OK;
507}
508
danielk197798331532006-06-14 10:55:52 +0000509/*
510** Compute a simple hash of the null terminated string zString.
511**
512** This module uses only sqlite3_index_info.idxStr, not
513** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
514** in echoBestIndex(), idxNum is set to the corresponding hash value.
515** In echoFilter(), code assert()s that the supplied idxNum value is
516** indeed the hash of the supplied idxStr.
517*/
518static int hashString(const char *zString){
519 int val = 0;
520 int ii;
521 for(ii=0; zString[ii]; ii++){
522 val = (val << 3) + (int)zString[ii];
523 }
524 return val;
525}
526
danielk1977cc013f82006-06-24 06:36:11 +0000527/*
528** Echo virtual table module xFilter method.
529*/
danielk19775fac9f82006-06-13 14:16:58 +0000530static int echoFilter(
531 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000532 int idxNum, const char *idxStr,
533 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000534){
535 int rc;
drh4be8b512006-06-13 23:51:34 +0000536 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000537
538 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
539 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
540 sqlite3 *db = pVtab->db;
541
danielk1977cc013f82006-06-24 06:36:11 +0000542 /* Check that idxNum matches idxStr */
543 assert( idxNum==hashString(idxStr) );
544
545 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000546 appendToEchoModule(pVtab->interp, "xFilter");
547 appendToEchoModule(pVtab->interp, idxStr);
548 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000549 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000550 }
551
danielk19775fac9f82006-06-13 14:16:58 +0000552 sqlite3_finalize(pCur->pStmt);
553 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000554
555 /* Prepare the SQL statement created by echoBestIndex and bind the
556 ** runtime parameters passed to this function to it.
557 */
drh4be8b512006-06-13 23:51:34 +0000558 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000559 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000560 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk1977cc013f82006-06-24 06:36:11 +0000561 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000562 }
danielk1977cc013f82006-06-24 06:36:11 +0000563
564 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000565 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000566 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000567 }
568
danielk1977be718892006-06-23 08:05:19 +0000569 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000570}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000571
danielk1977cc013f82006-06-24 06:36:11 +0000572
573/*
574** A helper function used by echoUpdate() and echoBestIndex() for
575** manipulating strings in concert with the sqlite3_mprintf() function.
576**
577** Parameter pzStr points to a pointer to a string allocated with
578** sqlite3_mprintf. The second parameter, zAppend, points to another
579** string. The two strings are concatenated together and *pzStr
580** set to point at the result. The initial buffer pointed to by *pzStr
581** is deallocated via sqlite3_free().
582**
583** If the third argument, doFree, is true, then sqlite3_free() is
584** also called to free the buffer pointed to by zAppend.
585*/
586static void string_concat(char **pzStr, char *zAppend, int doFree){
587 char *zIn = *pzStr;
588 if( zIn ){
589 char *zTemp = zIn;
590 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
591 sqlite3_free(zTemp);
592 }else{
593 zIn = sqlite3_mprintf("%s", zAppend);
594 }
595 *pzStr = zIn;
596 if( doFree ){
597 sqlite3_free(zAppend);
598 }
599}
600
drhb9bb7c12006-06-11 23:41:55 +0000601/*
danielk19775fac9f82006-06-13 14:16:58 +0000602** The echo module implements the subset of query constraints and sort
603** orders that may take advantage of SQLite indices on the underlying
604** real table. For example, if the real table is declared as:
605**
606** CREATE TABLE real(a, b, c);
607** CREATE INDEX real_index ON real(b);
608**
609** then the echo module handles WHERE or ORDER BY clauses that refer
610** to the column "b", but not "a" or "c". If a multi-column index is
611** present, only it's left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000612**
613** This xBestIndex method encodes the proposed search strategy as
614** an SQL query on the real table underlying the virtual echo module
615** table and stores the query in sqlite3_index_info.idxStr. The SQL
616** statement is of the form:
617**
618** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
619**
620** where the <where-clause> and <order-by-clause> are determined
621** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000622*/
danielk19775fac9f82006-06-13 14:16:58 +0000623static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
624 int ii;
drh4be8b512006-06-13 23:51:34 +0000625 char *zQuery = 0;
626 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000627 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000628 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000629 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000630 sqlite3_stmt *pStmt = 0;
danielk1977780b1d92007-03-30 14:56:34 +0000631 Tcl_Interp *interp = pVtab->interp;
danielk197774cdba42006-06-19 12:02:58 +0000632
633 int nRow;
634 int useIdx = 0;
635 int rc = SQLITE_OK;
danielk1977780b1d92007-03-30 14:56:34 +0000636 int useCost = 0;
637 double cost;
danielk197774cdba42006-06-19 12:02:58 +0000638
639 /* Determine the number of rows in the table and store this value in local
640 ** variable nRow. The 'estimated-cost' of the scan will be the number of
641 ** rows in the table for a linear scan, or the log (base 2) of the
642 ** number of rows if the proposed scan uses an index.
643 */
danielk1977780b1d92007-03-30 14:56:34 +0000644 if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
645 cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
646 useCost = 1;
647 } else {
648 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
649 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
650 sqlite3_free(zQuery);
651 if( rc!=SQLITE_OK ){
652 return rc;
653 }
654 sqlite3_step(pStmt);
655 nRow = sqlite3_column_int(pStmt, 0);
656 rc = sqlite3_finalize(pStmt);
657 if( rc!=SQLITE_OK ){
658 return rc;
659 }
danielk197774cdba42006-06-19 12:02:58 +0000660 }
danielk19775fac9f82006-06-13 14:16:58 +0000661
drh4be8b512006-06-13 23:51:34 +0000662 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000663 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
664 const struct sqlite3_index_constraint *pConstraint;
665 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000666 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000667
668 pConstraint = &pIdxInfo->aConstraint[ii];
669 pUsage = &pIdxInfo->aConstraintUsage[ii];
670
drh383736b2006-10-08 18:56:57 +0000671 iCol = pConstraint->iColumn;
danielk19775fac9f82006-06-13 14:16:58 +0000672 if( pVtab->aIndex[iCol] ){
673 char *zCol = pVtab->aCol[iCol];
674 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000675 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000676 if( iCol<0 ){
677 zCol = "rowid";
678 }
danielk19775fac9f82006-06-13 14:16:58 +0000679 switch( pConstraint->op ){
680 case SQLITE_INDEX_CONSTRAINT_EQ:
681 zOp = "="; break;
682 case SQLITE_INDEX_CONSTRAINT_LT:
683 zOp = "<"; break;
684 case SQLITE_INDEX_CONSTRAINT_GT:
685 zOp = ">"; break;
686 case SQLITE_INDEX_CONSTRAINT_LE:
687 zOp = "<="; break;
688 case SQLITE_INDEX_CONSTRAINT_GE:
689 zOp = ">="; break;
690 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000691 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000692 }
drh1a90e092006-06-14 22:07:10 +0000693 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000694 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
695 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000696 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000697 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000698 }
danielk1977cc013f82006-06-24 06:36:11 +0000699 string_concat(&zQuery, zNew, 1);
700
drh4be8b512006-06-13 23:51:34 +0000701 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000702 pUsage->argvIndex = ++nArg;
703 pUsage->omit = 1;
704 }
705 }
danielk197747d08092006-06-14 07:41:31 +0000706
707 /* If there is only one term in the ORDER BY clause, and it is
708 ** on a column that this virtual table has an index for, then consume
709 ** the ORDER BY clause.
710 */
711 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000712 int iCol = pIdxInfo->aOrderBy->iColumn;
713 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000714 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000715 if( iCol<0 ){
716 zCol = "rowid";
717 }
danielk1977cc013f82006-06-24 06:36:11 +0000718 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
719 string_concat(&zQuery, zNew, 1);
danielk197747d08092006-06-14 07:41:31 +0000720 pIdxInfo->orderByConsumed = 1;
721 }
722
danielk19775fac9f82006-06-13 14:16:58 +0000723 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000724 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000725
danielk197798331532006-06-14 10:55:52 +0000726 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000727 pIdxInfo->idxStr = zQuery;
728 pIdxInfo->needToFreeIdxStr = 1;
danielk1977780b1d92007-03-30 14:56:34 +0000729 if (useCost) {
730 pIdxInfo->estimatedCost = cost;
731 } else if( useIdx ){
danielk197774cdba42006-06-19 12:02:58 +0000732 /* Approximation of log2(nRow). */
733 for( ii=0; ii<(sizeof(int)*8); ii++ ){
734 if( nRow & (1<<ii) ){
735 pIdxInfo->estimatedCost = (double)ii;
736 }
737 }
738 } else {
739 pIdxInfo->estimatedCost = (double)nRow;
740 }
741 return rc;
drha967e882006-06-13 01:04:52 +0000742}
743
danielk1977176f4d22006-06-15 10:41:15 +0000744/*
danielk1977cc013f82006-06-24 06:36:11 +0000745** The xUpdate method for echo module virtual tables.
746**
danielk1977176f4d22006-06-15 10:41:15 +0000747** apData[0] apData[1] apData[2..]
748**
749** INTEGER DELETE
750**
751** INTEGER NULL (nCol args) UPDATE (do not set rowid)
752** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
753**
754** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
755** NULL INTEGER (nCol args) INSERT (incl. rowid value)
756**
757*/
danielk19771f6eec52006-06-16 06:17:47 +0000758int echoUpdate(
759 sqlite3_vtab *tab,
760 int nData,
761 sqlite3_value **apData,
762 sqlite_int64 *pRowid
763){
danielk197726e41442006-06-14 15:16:35 +0000764 echo_vtab *pVtab = (echo_vtab *)tab;
765 sqlite3 *db = pVtab->db;
766 int rc = SQLITE_OK;
767
danielk1977176f4d22006-06-15 10:41:15 +0000768 sqlite3_stmt *pStmt;
769 char *z = 0; /* SQL statement to execute */
770 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
771 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
772 int i; /* Counter variable used by for loops */
773
danielk197726e41442006-06-14 15:16:35 +0000774 assert( nData==pVtab->nCol+2 || nData==1 );
775
drh5aec0422006-06-14 23:43:31 +0000776 /* If apData[0] is an integer and nData>1 then do an UPDATE */
777 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000778 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000779 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000780
781 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
782 bindArgZero = 1;
783
784 if( bindArgOne ){
785 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000786 zSep = ",";
787 }
788 for(i=2; i<nData; i++){
789 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000790 string_concat(&z, sqlite3_mprintf(
791 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000792 zSep = ",";
793 }
danielk1977176f4d22006-06-15 10:41:15 +0000794 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000795 }
796
danielk1977176f4d22006-06-15 10:41:15 +0000797 /* If apData[0] is an integer and nData==1 then do a DELETE */
798 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
799 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
800 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000801 }
802
danielk1977176f4d22006-06-15 10:41:15 +0000803 /* If the first argument is NULL and there are more than two args, INSERT */
804 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000805 int ii;
806 char *zInsert = 0;
807 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000808
danielk19774b2688a2006-06-20 11:01:07 +0000809 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000810 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
811 bindArgOne = 1;
812 zValues = sqlite3_mprintf("?");
813 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000814 }
815
danielk1977176f4d22006-06-15 10:41:15 +0000816 assert((pVtab->nCol+2)==nData);
817 for(ii=2; ii<nData; ii++){
818 string_concat(&zInsert,
819 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
820 string_concat(&zValues,
821 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000822 }
823
danielk1977176f4d22006-06-15 10:41:15 +0000824 string_concat(&z, zInsert, 1);
825 string_concat(&z, ") VALUES(", 0);
826 string_concat(&z, zValues, 1);
827 string_concat(&z, ")", 0);
828 }
829
830 /* Anything else is an error */
831 else{
832 assert(0);
833 return SQLITE_ERROR;
834 }
835
836 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
837 assert( rc!=SQLITE_OK || pStmt );
838 sqlite3_free(z);
839 if( rc==SQLITE_OK ) {
840 if( bindArgZero ){
841 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000842 }
danielk1977176f4d22006-06-15 10:41:15 +0000843 if( bindArgOne ){
844 sqlite3_bind_value(pStmt, 1, apData[1]);
845 }
846 for(i=2; i<nData; i++){
847 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
848 }
849 sqlite3_step(pStmt);
850 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000851 }
852
danielk19771f6eec52006-06-16 06:17:47 +0000853 if( pRowid && rc==SQLITE_OK ){
854 *pRowid = sqlite3_last_insert_rowid(db);
855 }
856
danielk197726e41442006-06-14 15:16:35 +0000857 return rc;
858}
859
drha967e882006-06-13 01:04:52 +0000860/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000861** xBegin, xSync, xCommit and xRollback callbacks for echo module
862** virtual tables. Do nothing other than add the name of the callback
863** to the $::echo_module Tcl variable.
864*/
865static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
866 char *z;
867 echo_vtab *pVtab = (echo_vtab *)tab;
868 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
869 appendToEchoModule(pVtab->interp, zCall);
870 appendToEchoModule(pVtab->interp, z);
871 sqlite3_free(z);
872 return SQLITE_OK;
873}
874static int echoBegin(sqlite3_vtab *tab){
danielk19775017dc32006-06-24 09:34:22 +0000875 echo_vtab *pVtab = (echo_vtab *)tab;
876 Tcl_Interp *interp = pVtab->interp;
877 const char *zVal;
878
879 echoTransactionCall(tab, "xBegin");
880
881 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
882 ** and it is set to the name of the real table underlying this virtual
883 ** echo module table, then cause this xSync operation to fail.
884 */
885 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
886 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
887 return SQLITE_ERROR;
888 }
889 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +0000890}
891static int echoSync(sqlite3_vtab *tab){
892 echo_vtab *pVtab = (echo_vtab *)tab;
893 Tcl_Interp *interp = pVtab->interp;
894 const char *zVal;
895
896 echoTransactionCall(tab, "xSync");
897
898 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
899 ** and it is set to the name of the real table underlying this virtual
900 ** echo module table, then cause this xSync operation to fail.
901 */
902 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
903 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
904 return -1;
905 }
906 return SQLITE_OK;
907}
908static int echoCommit(sqlite3_vtab *tab){
909 return echoTransactionCall(tab, "xCommit");
910}
911static int echoRollback(sqlite3_vtab *tab){
912 return echoTransactionCall(tab, "xRollback");
913}
914
915/*
drhe94b0c32006-07-08 18:09:15 +0000916** Implementation of "GLOB" function on the echo module. Pass
917** all arguments to the ::echo_glob_overload procedure of TCL
918** and return the result of that procedure as a string.
919*/
920static void overloadedGlobFunction(
921 sqlite3_context *pContext,
922 int nArg,
923 sqlite3_value **apArg
924){
925 Tcl_Interp *interp = sqlite3_user_data(pContext);
926 Tcl_DString str;
927 int i;
928 int rc;
929 Tcl_DStringInit(&str);
930 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
931 for(i=0; i<nArg; i++){
932 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
933 }
934 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
935 Tcl_DStringFree(&str);
936 if( rc ){
937 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
938 }else{
939 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
940 -1, SQLITE_TRANSIENT);
941 }
942 Tcl_ResetResult(interp);
943}
944
945/*
946** This is the xFindFunction implementation for the echo module.
947** SQLite calls this routine when the first argument of a function
948** is a column of an echo virtual table. This routine can optionally
949** override the implementation of that function. It will choose to
950** do so if the function is named "glob", and a TCL command named
951** ::echo_glob_overload exists.
952*/
953static int echoFindFunction(
954 sqlite3_vtab *vtab,
955 int nArg,
956 const char *zFuncName,
957 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
958 void **ppArg
959){
960 echo_vtab *pVtab = (echo_vtab *)vtab;
961 Tcl_Interp *interp = pVtab->interp;
962 Tcl_CmdInfo info;
963 if( strcmp(zFuncName,"glob")!=0 ){
964 return 0;
965 }
966 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
967 return 0;
968 }
969 *pxFunc = overloadedGlobFunction;
970 *ppArg = interp;
971 return 1;
972}
973
974/*
danielk1977cc013f82006-06-24 06:36:11 +0000975** A virtual table module that merely "echos" the contents of another
976** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +0000977*/
978static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000979 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +0000980 echoCreate,
981 echoConnect,
drha967e882006-06-13 01:04:52 +0000982 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000983 echoDisconnect,
984 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000985 echoOpen, /* xOpen - open a cursor */
986 echoClose, /* xClose - close a cursor */
987 echoFilter, /* xFilter - configure scan constraints */
988 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +0000989 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000990 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000991 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +0000992 echoUpdate, /* xUpdate - write data */
993 echoBegin, /* xBegin - begin transaction */
994 echoSync, /* xSync - sync transaction */
995 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +0000996 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +0000997 echoFindFunction, /* xFindFunction - function overloading */
drhb9bb7c12006-06-11 23:41:55 +0000998};
999
1000/*
1001** Decode a pointer to an sqlite3 object.
1002*/
1003static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
1004 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
1005 return TCL_OK;
1006}
1007
drhb9bb7c12006-06-11 23:41:55 +00001008/*
1009** Register the echo virtual table module.
1010*/
1011static int register_echo_module(
1012 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1013 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1014 int objc, /* Number of arguments */
1015 Tcl_Obj *CONST objv[] /* Command arguments */
1016){
1017 sqlite3 *db;
1018 if( objc!=2 ){
1019 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1020 return TCL_ERROR;
1021 }
1022 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977d1ab1ba2006-06-15 04:28:13 +00001023 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +00001024 return TCL_OK;
1025}
1026
danielk19775017dc32006-06-24 09:34:22 +00001027/*
1028** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1029**
1030** sqlite3_declare_vtab DB SQL
1031*/
1032static int declare_vtab(
1033 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1034 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1035 int objc, /* Number of arguments */
1036 Tcl_Obj *CONST objv[] /* Command arguments */
1037){
1038 sqlite3 *db;
1039 int rc;
1040 if( objc!=3 ){
1041 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1042 return TCL_ERROR;
1043 }
1044 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1045 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1046 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001047 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001048 return TCL_ERROR;
1049 }
1050 return TCL_OK;
1051}
1052
danielk1977cc013f82006-06-24 06:36:11 +00001053#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001054
1055/*
1056** Register commands with the TCL interpreter.
1057*/
1058int Sqlitetest8_Init(Tcl_Interp *interp){
1059 static struct {
1060 char *zName;
1061 Tcl_ObjCmdProc *xProc;
1062 void *clientData;
1063 } aObjCmd[] = {
danielk1977cc013f82006-06-24 06:36:11 +00001064#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001065 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +00001066 { "sqlite3_declare_vtab", declare_vtab, 0 },
danielk1977cc013f82006-06-24 06:36:11 +00001067#endif
drhb9bb7c12006-06-11 23:41:55 +00001068 };
1069 int i;
1070 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1071 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1072 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1073 }
1074 return TCL_OK;
1075}