blob: 92ca8e27e44703bf40e3b5fdd3d66f2b5915e57e [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**
drh383736b2006-10-08 18:56:57 +000016** $Id: test8.c,v 1.43 2006/10/08 18:56:57 drh Exp $
drhb9bb7c12006-06-11 23:41:55 +000017*/
18#include "sqliteInt.h"
19#include "tcl.h"
20#include "os.h"
21#include <stdlib.h>
22#include <string.h>
23
danielk1977cc013f82006-06-24 06:36:11 +000024#ifndef SQLITE_OMIT_VIRTUALTABLE
25
danielk1977b7a7b9a2006-06-13 10:24:42 +000026typedef struct echo_vtab echo_vtab;
27typedef struct echo_cursor echo_cursor;
28
danielk1977c69cdfd2006-06-17 09:39:55 +000029/*
30** The test module defined in this file uses two global Tcl variables to
31** commicate with test-scripts:
32**
33** $::echo_module
34** $::echo_module_sync_fail
danielk19775017dc32006-06-24 09:34:22 +000035** $::echo_module_begin_fail
danielk1977cc013f82006-06-24 06:36:11 +000036**
37** The variable ::echo_module is a list. Each time one of the following
38** methods is called, one or more elements are appended to the list.
39** This is used for automated testing of virtual table modules.
40**
41** The ::echo_module_sync_fail variable is set by test scripts and read
42** by code in this file. If it is set to the name of a real table in the
43** the database, then all xSync operations on echo virtual tables that
44** use the named table as a backing store will fail.
danielk1977c69cdfd2006-06-17 09:39:55 +000045*/
46
danielk19775fac9f82006-06-13 14:16:58 +000047/*
danielk1977d6e8dd02006-06-15 15:38:41 +000048** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:58 +000049**
danielk1977d6e8dd02006-06-15 15:38:41 +000050** echo.vtab.aIndex is an array of booleans. The nth entry is true if
51** the nth column of the real table is the left-most column of an index
52** (implicit or otherwise). In other words, if SQLite can optimize
53** a query like "SELECT * FROM real_table WHERE col = ?".
54**
danielk1977c69cdfd2006-06-17 09:39:55 +000055** Member variable aCol[] contains copies of the column names of the real
56** table.
danielk19775fac9f82006-06-13 14:16:58 +000057*/
danielk1977b7a7b9a2006-06-13 10:24:42 +000058struct echo_vtab {
59 sqlite3_vtab base;
danielk1977cc013f82006-06-24 06:36:11 +000060 Tcl_Interp *interp; /* Tcl interpreter containing debug variables */
61 sqlite3 *db; /* Database connection */
danielk19775fac9f82006-06-13 14:16:58 +000062
danielk1977a4e76362006-06-14 06:31:28 +000063 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:48 +000064 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:28 +000065 int nCol; /* Number of columns in the real table */
66 int *aIndex; /* Array of size nCol. True if column has an index */
67 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000068};
69
70/* An echo cursor object */
71struct echo_cursor {
72 sqlite3_vtab_cursor base;
73 sqlite3_stmt *pStmt;
danielk1977b7a7b9a2006-06-13 10:24:42 +000074};
75
danielk1977cc013f82006-06-24 06:36:11 +000076/*
77** Retrieve the column names for the table named zTab via database
78** connection db. SQLITE_OK is returned on success, or an sqlite error
79** code otherwise.
80**
81** If successful, the number of columns is written to *pnCol. *paCol is
82** set to point at sqliteMalloc()'d space containing the array of
83** nCol column names. The caller is responsible for calling sqliteFree
84** on *paCol.
85*/
danielk19775fac9f82006-06-13 14:16:58 +000086static int getColumnNames(
87 sqlite3 *db,
88 const char *zTab,
89 char ***paCol,
90 int *pnCol
91){
92 char **aCol = 0;
danielk1977cc013f82006-06-24 06:36:11 +000093 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +000094 sqlite3_stmt *pStmt = 0;
95 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19 +000096 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58 +000097
danielk1977cc013f82006-06-24 06:36:11 +000098 /* Prepare the statement "SELECT * FROM <tbl>". The column names
99 ** of the result set of the compiled SELECT will be the same as
100 ** the column names of table <tbl>.
101 */
102 zSql = sqlite3MPrintf("SELECT * FROM %Q", zTab);
103 if( !zSql ){
104 rc = SQLITE_NOMEM;
105 goto out;
106 }
107 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
108 sqliteFree(zSql);
109
danielk19775fac9f82006-06-13 14:16:58 +0000110 if( rc==SQLITE_OK ){
111 int ii;
danielk1977cc013f82006-06-24 06:36:11 +0000112 int nBytes;
113 char *zSpace;
danielk19775fac9f82006-06-13 14:16:58 +0000114 nCol = sqlite3_column_count(pStmt);
danielk1977cc013f82006-06-24 06:36:11 +0000115
116 /* Figure out how much space to allocate for the array of column names
117 ** (including space for the strings themselves). Then allocate it.
118 */
119 nBytes = sizeof(char *) * nCol;
120 for(ii=0; ii<nCol; ii++){
121 nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1);
122 }
123 aCol = (char **)sqliteMalloc(nBytes);
danielk19775fac9f82006-06-13 14:16:58 +0000124 if( !aCol ){
125 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11 +0000126 goto out;
danielk19775fac9f82006-06-13 14:16:58 +0000127 }
danielk1977cc013f82006-06-24 06:36:11 +0000128
129 /* Copy the column names into the allocated space and set up the
130 ** pointers in the aCol[] array.
131 */
132 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58 +0000133 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11 +0000134 aCol[ii] = zSpace;
135 zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
136 zSpace++;
danielk19775fac9f82006-06-13 14:16:58 +0000137 }
danielk1977cc013f82006-06-24 06:36:11 +0000138 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58 +0000139 }
140
141 *paCol = aCol;
142 *pnCol = nCol;
143
danielk1977cc013f82006-06-24 06:36:11 +0000144out:
danielk19775fac9f82006-06-13 14:16:58 +0000145 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000146 return rc;
147}
148
danielk1977cc013f82006-06-24 06:36:11 +0000149/*
150** Parameter zTab is the name of a table in database db with nCol
151** columns. This function allocates an array of integers nCol in
152** size and populates it according to any implicit or explicit
153** indices on table zTab.
154**
155** If successful, SQLITE_OK is returned and *paIndex set to point
156** at the allocated array. Otherwise, an error code is returned.
157**
158** See comments associated with the member variable aIndex above
159** "struct echo_vtab" for details of the contents of the array.
160*/
161static int getIndexArray(
162 sqlite3 *db, /* Database connection */
163 const char *zTab, /* Name of table in database db */
164 int nCol,
165 int **paIndex
166){
danielk19775fac9f82006-06-13 14:16:58 +0000167 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000168 int *aIndex = 0;
169 int rc;
danielk1977cc013f82006-06-24 06:36:11 +0000170 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000171
danielk1977cc013f82006-06-24 06:36:11 +0000172 /* Allocate space for the index array */
danielk19775fac9f82006-06-13 14:16:58 +0000173 aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
174 if( !aIndex ){
175 rc = SQLITE_NOMEM;
176 goto get_index_array_out;
177 }
178
danielk1977cc013f82006-06-24 06:36:11 +0000179 /* Compile an sqlite pragma to loop through all indices on table zTab */
180 zSql = sqlite3MPrintf("PRAGMA index_list(%s)", zTab);
181 if( !zSql ){
182 rc = SQLITE_NOMEM;
183 goto get_index_array_out;
184 }
185 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
186 sqliteFree(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000187
danielk1977cc013f82006-06-24 06:36:11 +0000188 /* For each index, figure out the left-most column and set the
189 ** corresponding entry in aIndex[] to 1.
190 */
danielk19775fac9f82006-06-13 14:16:58 +0000191 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000192 const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58 +0000193 sqlite3_stmt *pStmt2 = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000194 zSql = sqlite3MPrintf("PRAGMA index_info(%s)", zIdx);
195 if( !zSql ){
196 rc = SQLITE_NOMEM;
197 goto get_index_array_out;
198 }
199 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
200 sqliteFree(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000201 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
202 int cid = sqlite3_column_int(pStmt2, 1);
203 assert( cid>=0 && cid<nCol );
204 aIndex[cid] = 1;
205 }
danielk1977be718892006-06-23 08:05:19 +0000206 if( pStmt2 ){
207 rc = sqlite3_finalize(pStmt2);
208 }
danielk19775fac9f82006-06-13 14:16:58 +0000209 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000210 goto get_index_array_out;
211 }
212 }
213
danielk19775fac9f82006-06-13 14:16:58 +0000214
215get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000216 if( pStmt ){
217 int rc2 = sqlite3_finalize(pStmt);
218 if( rc==SQLITE_OK ){
219 rc = rc2;
220 }
221 }
danielk19775fac9f82006-06-13 14:16:58 +0000222 if( rc!=SQLITE_OK ){
223 sqliteFree(aIndex);
224 aIndex = 0;
225 }
226 *paIndex = aIndex;
227 return rc;
228}
229
danielk197778efaba2006-06-12 06:09:17 +0000230/*
231** Global Tcl variable $echo_module is a list. This routine appends
232** the string element zArg to that list in interpreter interp.
233*/
drha967e882006-06-13 01:04:52 +0000234static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000235 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000236 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000237}
238
danielk19777e6ebfb2006-06-12 11:24:37 +0000239/*
240** This function is called from within the echo-modules xCreate and
241** xConnect methods. The argc and argv arguments are copies of those
242** passed to the calling method. This function is responsible for
243** calling sqlite3_declare_vtab() to declare the schema of the virtual
244** table being created or connected.
245**
246** If the constructor was passed just one argument, i.e.:
247**
248** CREATE TABLE t1 AS echo(t2);
249**
250** Then t2 is assumed to be the name of a *real* database table. The
251** schema of the virtual table is declared by passing a copy of the
252** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
253** Hence, the virtual table should have exactly the same column names and
254** types as the real table.
255*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000256static int echoDeclareVtab(
257 echo_vtab *pVtab,
258 sqlite3 *db,
259 int argc,
drhe4102962006-09-11 00:34:22 +0000260 const char *const*argv
danielk1977b7a7b9a2006-06-13 10:24:42 +0000261){
danielk19777e6ebfb2006-06-12 11:24:37 +0000262 int rc = SQLITE_OK;
263
danielk1977a298e902006-06-22 09:53:48 +0000264 if( argc>=4 ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000265 sqlite3_stmt *pStmt = 0;
266 sqlite3_prepare(db,
267 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
268 -1, &pStmt, 0);
danielk197770ba1642006-06-21 16:02:42 +0000269 sqlite3_bind_text(pStmt, 1, argv[3], -1, 0);
danielk19777e6ebfb2006-06-12 11:24:37 +0000270 if( sqlite3_step(pStmt)==SQLITE_ROW ){
danielk197765fd59f2006-06-24 11:51:33 +0000271 const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
danielk19777e6ebfb2006-06-12 11:24:37 +0000272 sqlite3_declare_vtab(db, zCreateTable);
danielk1977be718892006-06-23 08:05:19 +0000273 rc = sqlite3_finalize(pStmt);
danielk19777e6ebfb2006-06-12 11:24:37 +0000274 } else {
danielk1977be718892006-06-23 08:05:19 +0000275 rc = sqlite3_finalize(pStmt);
276 if( rc==SQLITE_OK ){
277 rc = SQLITE_ERROR;
278 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000279 }
danielk1977be718892006-06-23 08:05:19 +0000280
danielk19775fac9f82006-06-13 14:16:58 +0000281 if( rc==SQLITE_OK ){
danielk1977cc013f82006-06-24 06:36:11 +0000282 rc = getColumnNames(db, argv[3], &pVtab->aCol, &pVtab->nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000283 }
284 if( rc==SQLITE_OK ){
danielk1977cc013f82006-06-24 06:36:11 +0000285 rc = getIndexArray(db, argv[3], pVtab->nCol, &pVtab->aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000286 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000287 }
288
289 return rc;
290}
291
danielk1977cc013f82006-06-24 06:36:11 +0000292/*
293** This function frees all runtime structures associated with the virtual
294** table pVtab.
295*/
danielk1977a4e76362006-06-14 06:31:28 +0000296static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000297 echo_vtab *p = (echo_vtab*)pVtab;
298 sqliteFree(p->aIndex);
danielk1977a4e76362006-06-14 06:31:28 +0000299 sqliteFree(p->aCol);
300 sqliteFree(p->zTableName);
danielk1977212b2182006-06-23 14:32:08 +0000301 sqliteFree(p->zLogName);
danielk1977a4e76362006-06-14 06:31:28 +0000302 sqliteFree(p);
303 return 0;
304}
305
danielk1977cc013f82006-06-24 06:36:11 +0000306/*
307** This function is called to do the work of the xConnect() method -
308** to allocate the required in-memory structures for a newly connected
309** virtual table.
310*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000311static int echoConstructor(
312 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000313 void *pAux,
drhe4102962006-09-11 00:34:22 +0000314 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000315 sqlite3_vtab **ppVtab,
316 char **pzErr
danielk1977b7a7b9a2006-06-13 10:24:42 +0000317){
318 int i;
319 echo_vtab *pVtab;
320
danielk1977cc013f82006-06-24 06:36:11 +0000321 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000322 pVtab = sqliteMalloc( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000323 if( !pVtab ){
324 return SQLITE_NOMEM;
325 }
danielk19779da9d472006-06-14 06:58:15 +0000326 pVtab->interp = (Tcl_Interp *)pAux;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000327 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000328
329 /* Allocate echo_vtab.zTableName */
danielk197770ba1642006-06-21 16:02:42 +0000330 pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
danielk1977be718892006-06-23 08:05:19 +0000331 if( !pVtab->zTableName ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000332 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000333 return SQLITE_NOMEM;
334 }
335
danielk1977cc013f82006-06-24 06:36:11 +0000336 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000337 for(i=0; i<argc; i++){
338 appendToEchoModule(pVtab->interp, argv[i]);
339 }
340
danielk1977cc013f82006-06-24 06:36:11 +0000341 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
342 ** structure. If an error occurs, delete the sqlite3_vtab structure and
343 ** return an error code.
344 */
danielk1977a4e76362006-06-14 06:31:28 +0000345 if( echoDeclareVtab(pVtab, db, argc, argv) ){
346 echoDestructor((sqlite3_vtab *)pVtab);
347 return SQLITE_ERROR;
348 }
349
danielk1977cc013f82006-06-24 06:36:11 +0000350 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000351 *ppVtab = &pVtab->base;
352 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000353}
drha967e882006-06-13 01:04:52 +0000354
danielk1977cc013f82006-06-24 06:36:11 +0000355/*
356** Echo virtual table module xCreate method.
357*/
drhb9bb7c12006-06-11 23:41:55 +0000358static int echoCreate(
359 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000360 void *pAux,
drhe4102962006-09-11 00:34:22 +0000361 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000362 sqlite3_vtab **ppVtab,
363 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000364){
danielk1977a298e902006-06-22 09:53:48 +0000365 int rc = SQLITE_OK;
danielk19779da9d472006-06-14 06:58:15 +0000366 appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
drh4ca8aac2006-09-10 17:31:58 +0000367 rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
danielk1977cc013f82006-06-24 06:36:11 +0000368
369 /* If there were two arguments passed to the module at the SQL level
370 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
371 ** the second argument is used as a table name. Attempt to create
372 ** such a table with a single column, "logmsg". This table will
373 ** be used to log calls to the xUpdate method. It will be deleted
374 ** when the virtual table is DROPed.
375 **
376 ** Note: The main point of this is to test that we can drop tables
377 ** from within an xDestroy method call.
378 */
danielk1977a298e902006-06-22 09:53:48 +0000379 if( rc==SQLITE_OK && argc==5 ){
380 char *zSql;
381 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
382 pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
383 zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
384 rc = sqlite3_exec(db, zSql, 0, 0, 0);
385 sqliteFree(zSql);
386 }
danielk1977cc013f82006-06-24 06:36:11 +0000387
danielk1977a298e902006-06-22 09:53:48 +0000388 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000389}
danielk1977cc013f82006-06-24 06:36:11 +0000390
391/*
392** Echo virtual table module xConnect method.
393*/
drhb9bb7c12006-06-11 23:41:55 +0000394static int echoConnect(
395 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000396 void *pAux,
drhe4102962006-09-11 00:34:22 +0000397 int argc, const char *const*argv,
drh4ca8aac2006-09-10 17:31:58 +0000398 sqlite3_vtab **ppVtab,
399 char **pzErr
drhb9bb7c12006-06-11 23:41:55 +0000400){
danielk19779da9d472006-06-14 06:58:15 +0000401 appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
drh4ca8aac2006-09-10 17:31:58 +0000402 return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
drhb9bb7c12006-06-11 23:41:55 +0000403}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000404
danielk1977cc013f82006-06-24 06:36:11 +0000405/*
406** Echo virtual table module xDisconnect method.
407*/
danielk19775fac9f82006-06-13 14:16:58 +0000408static int echoDisconnect(sqlite3_vtab *pVtab){
409 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
410 return echoDestructor(pVtab);
411}
danielk1977cc013f82006-06-24 06:36:11 +0000412
413/*
414** Echo virtual table module xDestroy method.
415*/
danielk19775fac9f82006-06-13 14:16:58 +0000416static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000417 int rc = SQLITE_OK;
418 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000419 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000420
421 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000422 if( p && p->zLogName ){
423 char *zSql;
424 zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName);
425 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
426 sqliteFree(zSql);
427 }
danielk1977cc013f82006-06-24 06:36:11 +0000428
danielk1977a298e902006-06-22 09:53:48 +0000429 if( rc==SQLITE_OK ){
430 rc = echoDestructor(pVtab);
431 }
432 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000433}
434
danielk1977cc013f82006-06-24 06:36:11 +0000435/*
436** Echo virtual table module xOpen method.
437*/
danielk19775fac9f82006-06-13 14:16:58 +0000438static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000439 echo_cursor *pCur;
440 pCur = sqliteMalloc(sizeof(echo_cursor));
441 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000442 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000443}
444
danielk1977cc013f82006-06-24 06:36:11 +0000445/*
446** Echo virtual table module xClose method.
447*/
danielk19775fac9f82006-06-13 14:16:58 +0000448static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000449 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000450 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000451 sqlite3_stmt *pStmt = pCur->pStmt;
452 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000453 sqliteFree(pCur);
danielk1977be718892006-06-23 08:05:19 +0000454 rc = sqlite3_finalize(pStmt);
455 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000456}
457
danielk1977a298e902006-06-22 09:53:48 +0000458/*
459** Return non-zero if the cursor does not currently point to a valid record
460** (i.e if the scan has finished), or zero otherwise.
461*/
462static int echoEof(sqlite3_vtab_cursor *cur){
463 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
464}
465
danielk1977cc013f82006-06-24 06:36:11 +0000466/*
467** Echo virtual table module xNext method.
468*/
danielk19775fac9f82006-06-13 14:16:58 +0000469static int echoNext(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000470 int rc;
471 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000472 rc = sqlite3_step(pCur->pStmt);
473
474 if( rc==SQLITE_ROW ){
danielk1977a298e902006-06-22 09:53:48 +0000475 rc = SQLITE_OK;
476 }else{
477 rc = sqlite3_finalize(pCur->pStmt);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000478 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000479 }
480
481 return rc;
482}
483
danielk1977cc013f82006-06-24 06:36:11 +0000484/*
485** Echo virtual table module xColumn method.
486*/
danielk19775fac9f82006-06-13 14:16:58 +0000487static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000488 int iCol = i + 1;
489 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk1977fbbe0052006-06-21 07:02:33 +0000490 if( !pStmt ){
491 sqlite3_result_null(ctx);
492 }else{
493 assert( sqlite3_data_count(pStmt)>iCol );
494 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
495 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000496 return SQLITE_OK;
497}
498
danielk1977cc013f82006-06-24 06:36:11 +0000499/*
500** Echo virtual table module xRowid method.
501*/
danielk19775fac9f82006-06-13 14:16:58 +0000502static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000503 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
504 *pRowid = sqlite3_column_int64(pStmt, 0);
505 return SQLITE_OK;
506}
507
danielk197798331532006-06-14 10:55:52 +0000508/*
509** Compute a simple hash of the null terminated string zString.
510**
511** This module uses only sqlite3_index_info.idxStr, not
512** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
513** in echoBestIndex(), idxNum is set to the corresponding hash value.
514** In echoFilter(), code assert()s that the supplied idxNum value is
515** indeed the hash of the supplied idxStr.
516*/
517static int hashString(const char *zString){
518 int val = 0;
519 int ii;
520 for(ii=0; zString[ii]; ii++){
521 val = (val << 3) + (int)zString[ii];
522 }
523 return val;
524}
525
danielk1977cc013f82006-06-24 06:36:11 +0000526/*
527** Echo virtual table module xFilter method.
528*/
danielk19775fac9f82006-06-13 14:16:58 +0000529static int echoFilter(
530 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000531 int idxNum, const char *idxStr,
532 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000533){
534 int rc;
drh4be8b512006-06-13 23:51:34 +0000535 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000536
537 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
538 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
539 sqlite3 *db = pVtab->db;
540
danielk1977cc013f82006-06-24 06:36:11 +0000541 /* Check that idxNum matches idxStr */
542 assert( idxNum==hashString(idxStr) );
543
544 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000545 appendToEchoModule(pVtab->interp, "xFilter");
546 appendToEchoModule(pVtab->interp, idxStr);
547 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000548 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000549 }
550
danielk19775fac9f82006-06-13 14:16:58 +0000551 sqlite3_finalize(pCur->pStmt);
552 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000553
554 /* Prepare the SQL statement created by echoBestIndex and bind the
555 ** runtime parameters passed to this function to it.
556 */
drh4be8b512006-06-13 23:51:34 +0000557 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000558 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000559 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk1977cc013f82006-06-24 06:36:11 +0000560 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000561 }
danielk1977cc013f82006-06-24 06:36:11 +0000562
563 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000564 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000565 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000566 }
567
danielk1977be718892006-06-23 08:05:19 +0000568 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000569}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000570
danielk1977cc013f82006-06-24 06:36:11 +0000571
572/*
573** A helper function used by echoUpdate() and echoBestIndex() for
574** manipulating strings in concert with the sqlite3_mprintf() function.
575**
576** Parameter pzStr points to a pointer to a string allocated with
577** sqlite3_mprintf. The second parameter, zAppend, points to another
578** string. The two strings are concatenated together and *pzStr
579** set to point at the result. The initial buffer pointed to by *pzStr
580** is deallocated via sqlite3_free().
581**
582** If the third argument, doFree, is true, then sqlite3_free() is
583** also called to free the buffer pointed to by zAppend.
584*/
585static void string_concat(char **pzStr, char *zAppend, int doFree){
586 char *zIn = *pzStr;
587 if( zIn ){
588 char *zTemp = zIn;
589 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
590 sqlite3_free(zTemp);
591 }else{
592 zIn = sqlite3_mprintf("%s", zAppend);
593 }
594 *pzStr = zIn;
595 if( doFree ){
596 sqlite3_free(zAppend);
597 }
598}
599
drhb9bb7c12006-06-11 23:41:55 +0000600/*
danielk19775fac9f82006-06-13 14:16:58 +0000601** The echo module implements the subset of query constraints and sort
602** orders that may take advantage of SQLite indices on the underlying
603** real table. For example, if the real table is declared as:
604**
605** CREATE TABLE real(a, b, c);
606** CREATE INDEX real_index ON real(b);
607**
608** then the echo module handles WHERE or ORDER BY clauses that refer
609** to the column "b", but not "a" or "c". If a multi-column index is
610** present, only it's left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000611**
612** This xBestIndex method encodes the proposed search strategy as
613** an SQL query on the real table underlying the virtual echo module
614** table and stores the query in sqlite3_index_info.idxStr. The SQL
615** statement is of the form:
616**
617** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
618**
619** where the <where-clause> and <order-by-clause> are determined
620** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000621*/
danielk19775fac9f82006-06-13 14:16:58 +0000622static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
623 int ii;
drh4be8b512006-06-13 23:51:34 +0000624 char *zQuery = 0;
625 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000626 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000627 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000628 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000629 sqlite3_stmt *pStmt = 0;
630
631 int nRow;
632 int useIdx = 0;
633 int rc = SQLITE_OK;
634
635 /* Determine the number of rows in the table and store this value in local
636 ** variable nRow. The 'estimated-cost' of the scan will be the number of
637 ** rows in the table for a linear scan, or the log (base 2) of the
638 ** number of rows if the proposed scan uses an index.
639 */
640 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
641 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
642 if( rc!=SQLITE_OK ){
643 return rc;
644 }
645 sqlite3_step(pStmt);
646 nRow = sqlite3_column_int(pStmt, 0);
647 rc = sqlite3_finalize(pStmt);
648 if( rc!=SQLITE_OK ){
649 return rc;
650 }
danielk19775fac9f82006-06-13 14:16:58 +0000651
drh4be8b512006-06-13 23:51:34 +0000652 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000653 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
654 const struct sqlite3_index_constraint *pConstraint;
655 struct sqlite3_index_constraint_usage *pUsage;
drh383736b2006-10-08 18:56:57 +0000656 int iCol;
danielk19775fac9f82006-06-13 14:16:58 +0000657
658 pConstraint = &pIdxInfo->aConstraint[ii];
659 pUsage = &pIdxInfo->aConstraintUsage[ii];
660
drh383736b2006-10-08 18:56:57 +0000661 iCol = pConstraint->iColumn;
danielk19775fac9f82006-06-13 14:16:58 +0000662 if( pVtab->aIndex[iCol] ){
663 char *zCol = pVtab->aCol[iCol];
664 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000665 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000666 if( iCol<0 ){
667 zCol = "rowid";
668 }
danielk19775fac9f82006-06-13 14:16:58 +0000669 switch( pConstraint->op ){
670 case SQLITE_INDEX_CONSTRAINT_EQ:
671 zOp = "="; break;
672 case SQLITE_INDEX_CONSTRAINT_LT:
673 zOp = "<"; break;
674 case SQLITE_INDEX_CONSTRAINT_GT:
675 zOp = ">"; break;
676 case SQLITE_INDEX_CONSTRAINT_LE:
677 zOp = "<="; break;
678 case SQLITE_INDEX_CONSTRAINT_GE:
679 zOp = ">="; break;
680 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000681 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000682 }
drh1a90e092006-06-14 22:07:10 +0000683 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000684 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
685 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000686 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000687 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000688 }
danielk1977cc013f82006-06-24 06:36:11 +0000689 string_concat(&zQuery, zNew, 1);
690
drh4be8b512006-06-13 23:51:34 +0000691 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000692 pUsage->argvIndex = ++nArg;
693 pUsage->omit = 1;
694 }
695 }
danielk197747d08092006-06-14 07:41:31 +0000696
697 /* If there is only one term in the ORDER BY clause, and it is
698 ** on a column that this virtual table has an index for, then consume
699 ** the ORDER BY clause.
700 */
701 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000702 int iCol = pIdxInfo->aOrderBy->iColumn;
703 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000704 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000705 if( iCol<0 ){
706 zCol = "rowid";
707 }
danielk1977cc013f82006-06-24 06:36:11 +0000708 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
709 string_concat(&zQuery, zNew, 1);
danielk197747d08092006-06-14 07:41:31 +0000710 pIdxInfo->orderByConsumed = 1;
711 }
712
danielk19775fac9f82006-06-13 14:16:58 +0000713 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000714 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000715
danielk197798331532006-06-14 10:55:52 +0000716 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000717 pIdxInfo->idxStr = zQuery;
718 pIdxInfo->needToFreeIdxStr = 1;
danielk197774cdba42006-06-19 12:02:58 +0000719 if( useIdx ){
720 /* Approximation of log2(nRow). */
721 for( ii=0; ii<(sizeof(int)*8); ii++ ){
722 if( nRow & (1<<ii) ){
723 pIdxInfo->estimatedCost = (double)ii;
724 }
725 }
726 } else {
727 pIdxInfo->estimatedCost = (double)nRow;
728 }
729 return rc;
drha967e882006-06-13 01:04:52 +0000730}
731
danielk1977176f4d22006-06-15 10:41:15 +0000732/*
danielk1977cc013f82006-06-24 06:36:11 +0000733** The xUpdate method for echo module virtual tables.
734**
danielk1977176f4d22006-06-15 10:41:15 +0000735** apData[0] apData[1] apData[2..]
736**
737** INTEGER DELETE
738**
739** INTEGER NULL (nCol args) UPDATE (do not set rowid)
740** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
741**
742** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
743** NULL INTEGER (nCol args) INSERT (incl. rowid value)
744**
745*/
danielk19771f6eec52006-06-16 06:17:47 +0000746int echoUpdate(
747 sqlite3_vtab *tab,
748 int nData,
749 sqlite3_value **apData,
750 sqlite_int64 *pRowid
751){
danielk197726e41442006-06-14 15:16:35 +0000752 echo_vtab *pVtab = (echo_vtab *)tab;
753 sqlite3 *db = pVtab->db;
754 int rc = SQLITE_OK;
755
danielk1977176f4d22006-06-15 10:41:15 +0000756 sqlite3_stmt *pStmt;
757 char *z = 0; /* SQL statement to execute */
758 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
759 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
760 int i; /* Counter variable used by for loops */
761
danielk197726e41442006-06-14 15:16:35 +0000762 assert( nData==pVtab->nCol+2 || nData==1 );
763
drh5aec0422006-06-14 23:43:31 +0000764 /* If apData[0] is an integer and nData>1 then do an UPDATE */
765 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
drh5aec0422006-06-14 23:43:31 +0000766 char *zSep = " SET";
drh383736b2006-10-08 18:56:57 +0000767 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000768
769 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
770 bindArgZero = 1;
771
772 if( bindArgOne ){
773 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000774 zSep = ",";
775 }
776 for(i=2; i<nData; i++){
777 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000778 string_concat(&z, sqlite3_mprintf(
779 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000780 zSep = ",";
781 }
danielk1977176f4d22006-06-15 10:41:15 +0000782 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000783 }
784
danielk1977176f4d22006-06-15 10:41:15 +0000785 /* If apData[0] is an integer and nData==1 then do a DELETE */
786 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
787 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
788 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000789 }
790
danielk1977176f4d22006-06-15 10:41:15 +0000791 /* If the first argument is NULL and there are more than two args, INSERT */
792 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000793 int ii;
794 char *zInsert = 0;
795 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000796
danielk19774b2688a2006-06-20 11:01:07 +0000797 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000798 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
799 bindArgOne = 1;
800 zValues = sqlite3_mprintf("?");
801 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000802 }
803
danielk1977176f4d22006-06-15 10:41:15 +0000804 assert((pVtab->nCol+2)==nData);
805 for(ii=2; ii<nData; ii++){
806 string_concat(&zInsert,
807 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
808 string_concat(&zValues,
809 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000810 }
811
danielk1977176f4d22006-06-15 10:41:15 +0000812 string_concat(&z, zInsert, 1);
813 string_concat(&z, ") VALUES(", 0);
814 string_concat(&z, zValues, 1);
815 string_concat(&z, ")", 0);
816 }
817
818 /* Anything else is an error */
819 else{
820 assert(0);
821 return SQLITE_ERROR;
822 }
823
824 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
825 assert( rc!=SQLITE_OK || pStmt );
826 sqlite3_free(z);
827 if( rc==SQLITE_OK ) {
828 if( bindArgZero ){
829 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000830 }
danielk1977176f4d22006-06-15 10:41:15 +0000831 if( bindArgOne ){
832 sqlite3_bind_value(pStmt, 1, apData[1]);
833 }
834 for(i=2; i<nData; i++){
835 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
836 }
837 sqlite3_step(pStmt);
838 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000839 }
840
danielk19771f6eec52006-06-16 06:17:47 +0000841 if( pRowid && rc==SQLITE_OK ){
842 *pRowid = sqlite3_last_insert_rowid(db);
843 }
844
danielk197726e41442006-06-14 15:16:35 +0000845 return rc;
846}
847
drha967e882006-06-13 01:04:52 +0000848/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000849** xBegin, xSync, xCommit and xRollback callbacks for echo module
850** virtual tables. Do nothing other than add the name of the callback
851** to the $::echo_module Tcl variable.
852*/
853static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
854 char *z;
855 echo_vtab *pVtab = (echo_vtab *)tab;
856 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
857 appendToEchoModule(pVtab->interp, zCall);
858 appendToEchoModule(pVtab->interp, z);
859 sqlite3_free(z);
860 return SQLITE_OK;
861}
862static int echoBegin(sqlite3_vtab *tab){
danielk19775017dc32006-06-24 09:34:22 +0000863 echo_vtab *pVtab = (echo_vtab *)tab;
864 Tcl_Interp *interp = pVtab->interp;
865 const char *zVal;
866
867 echoTransactionCall(tab, "xBegin");
868
869 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
870 ** and it is set to the name of the real table underlying this virtual
871 ** echo module table, then cause this xSync operation to fail.
872 */
873 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
874 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
875 return SQLITE_ERROR;
876 }
877 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +0000878}
879static int echoSync(sqlite3_vtab *tab){
880 echo_vtab *pVtab = (echo_vtab *)tab;
881 Tcl_Interp *interp = pVtab->interp;
882 const char *zVal;
883
884 echoTransactionCall(tab, "xSync");
885
886 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
887 ** and it is set to the name of the real table underlying this virtual
888 ** echo module table, then cause this xSync operation to fail.
889 */
890 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
891 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
892 return -1;
893 }
894 return SQLITE_OK;
895}
896static int echoCommit(sqlite3_vtab *tab){
897 return echoTransactionCall(tab, "xCommit");
898}
899static int echoRollback(sqlite3_vtab *tab){
900 return echoTransactionCall(tab, "xRollback");
901}
902
903/*
drhe94b0c32006-07-08 18:09:15 +0000904** Implementation of "GLOB" function on the echo module. Pass
905** all arguments to the ::echo_glob_overload procedure of TCL
906** and return the result of that procedure as a string.
907*/
908static void overloadedGlobFunction(
909 sqlite3_context *pContext,
910 int nArg,
911 sqlite3_value **apArg
912){
913 Tcl_Interp *interp = sqlite3_user_data(pContext);
914 Tcl_DString str;
915 int i;
916 int rc;
917 Tcl_DStringInit(&str);
918 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
919 for(i=0; i<nArg; i++){
920 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
921 }
922 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
923 Tcl_DStringFree(&str);
924 if( rc ){
925 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
926 }else{
927 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
928 -1, SQLITE_TRANSIENT);
929 }
930 Tcl_ResetResult(interp);
931}
932
933/*
934** This is the xFindFunction implementation for the echo module.
935** SQLite calls this routine when the first argument of a function
936** is a column of an echo virtual table. This routine can optionally
937** override the implementation of that function. It will choose to
938** do so if the function is named "glob", and a TCL command named
939** ::echo_glob_overload exists.
940*/
941static int echoFindFunction(
942 sqlite3_vtab *vtab,
943 int nArg,
944 const char *zFuncName,
945 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
946 void **ppArg
947){
948 echo_vtab *pVtab = (echo_vtab *)vtab;
949 Tcl_Interp *interp = pVtab->interp;
950 Tcl_CmdInfo info;
951 if( strcmp(zFuncName,"glob")!=0 ){
952 return 0;
953 }
954 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
955 return 0;
956 }
957 *pxFunc = overloadedGlobFunction;
958 *ppArg = interp;
959 return 1;
960}
961
962/*
danielk1977cc013f82006-06-24 06:36:11 +0000963** A virtual table module that merely "echos" the contents of another
964** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +0000965*/
966static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000967 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +0000968 echoCreate,
969 echoConnect,
drha967e882006-06-13 01:04:52 +0000970 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000971 echoDisconnect,
972 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000973 echoOpen, /* xOpen - open a cursor */
974 echoClose, /* xClose - close a cursor */
975 echoFilter, /* xFilter - configure scan constraints */
976 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +0000977 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000978 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000979 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +0000980 echoUpdate, /* xUpdate - write data */
981 echoBegin, /* xBegin - begin transaction */
982 echoSync, /* xSync - sync transaction */
983 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +0000984 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +0000985 echoFindFunction, /* xFindFunction - function overloading */
drhb9bb7c12006-06-11 23:41:55 +0000986};
987
988/*
989** Decode a pointer to an sqlite3 object.
990*/
991static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
992 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
993 return TCL_OK;
994}
995
drhb9bb7c12006-06-11 23:41:55 +0000996/*
997** Register the echo virtual table module.
998*/
999static int register_echo_module(
1000 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1001 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1002 int objc, /* Number of arguments */
1003 Tcl_Obj *CONST objv[] /* Command arguments */
1004){
1005 sqlite3 *db;
1006 if( objc!=2 ){
1007 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1008 return TCL_ERROR;
1009 }
1010 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977d1ab1ba2006-06-15 04:28:13 +00001011 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +00001012 return TCL_OK;
1013}
1014
danielk19775017dc32006-06-24 09:34:22 +00001015/*
1016** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1017**
1018** sqlite3_declare_vtab DB SQL
1019*/
1020static int declare_vtab(
1021 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1022 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1023 int objc, /* Number of arguments */
1024 Tcl_Obj *CONST objv[] /* Command arguments */
1025){
1026 sqlite3 *db;
1027 int rc;
1028 if( objc!=3 ){
1029 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1030 return TCL_ERROR;
1031 }
1032 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1033 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1034 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001035 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001036 return TCL_ERROR;
1037 }
1038 return TCL_OK;
1039}
1040
danielk1977cc013f82006-06-24 06:36:11 +00001041#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001042
1043/*
1044** Register commands with the TCL interpreter.
1045*/
1046int Sqlitetest8_Init(Tcl_Interp *interp){
1047 static struct {
1048 char *zName;
1049 Tcl_ObjCmdProc *xProc;
1050 void *clientData;
1051 } aObjCmd[] = {
danielk1977cc013f82006-06-24 06:36:11 +00001052#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001053 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +00001054 { "sqlite3_declare_vtab", declare_vtab, 0 },
danielk1977cc013f82006-06-24 06:36:11 +00001055#endif
drhb9bb7c12006-06-11 23:41:55 +00001056 };
1057 int i;
1058 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1059 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1060 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1061 }
1062 return TCL_OK;
1063}