blob: 818e6e583349502abf03bcfba5a87a3dd4519781 [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**
drhe4102962006-09-11 00:34:22 +000016** $Id: test8.c,v 1.42 2006/09/11 00:34:22 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;
656
657 pConstraint = &pIdxInfo->aConstraint[ii];
658 pUsage = &pIdxInfo->aConstraintUsage[ii];
659
660 int iCol = pConstraint->iColumn;
661 if( pVtab->aIndex[iCol] ){
662 char *zCol = pVtab->aCol[iCol];
663 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000664 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000665 if( iCol<0 ){
666 zCol = "rowid";
667 }
danielk19775fac9f82006-06-13 14:16:58 +0000668 switch( pConstraint->op ){
669 case SQLITE_INDEX_CONSTRAINT_EQ:
670 zOp = "="; break;
671 case SQLITE_INDEX_CONSTRAINT_LT:
672 zOp = "<"; break;
673 case SQLITE_INDEX_CONSTRAINT_GT:
674 zOp = ">"; break;
675 case SQLITE_INDEX_CONSTRAINT_LE:
676 zOp = "<="; break;
677 case SQLITE_INDEX_CONSTRAINT_GE:
678 zOp = ">="; break;
679 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000680 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000681 }
drh1a90e092006-06-14 22:07:10 +0000682 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000683 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
684 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000685 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000686 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000687 }
danielk1977cc013f82006-06-24 06:36:11 +0000688 string_concat(&zQuery, zNew, 1);
689
drh4be8b512006-06-13 23:51:34 +0000690 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000691 pUsage->argvIndex = ++nArg;
692 pUsage->omit = 1;
693 }
694 }
danielk197747d08092006-06-14 07:41:31 +0000695
696 /* If there is only one term in the ORDER BY clause, and it is
697 ** on a column that this virtual table has an index for, then consume
698 ** the ORDER BY clause.
699 */
700 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000701 int iCol = pIdxInfo->aOrderBy->iColumn;
702 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000703 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000704 if( iCol<0 ){
705 zCol = "rowid";
706 }
danielk1977cc013f82006-06-24 06:36:11 +0000707 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
708 string_concat(&zQuery, zNew, 1);
danielk197747d08092006-06-14 07:41:31 +0000709 pIdxInfo->orderByConsumed = 1;
710 }
711
danielk19775fac9f82006-06-13 14:16:58 +0000712 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000713 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000714
danielk197798331532006-06-14 10:55:52 +0000715 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000716 pIdxInfo->idxStr = zQuery;
717 pIdxInfo->needToFreeIdxStr = 1;
danielk197774cdba42006-06-19 12:02:58 +0000718 if( useIdx ){
719 /* Approximation of log2(nRow). */
720 for( ii=0; ii<(sizeof(int)*8); ii++ ){
721 if( nRow & (1<<ii) ){
722 pIdxInfo->estimatedCost = (double)ii;
723 }
724 }
725 } else {
726 pIdxInfo->estimatedCost = (double)nRow;
727 }
728 return rc;
drha967e882006-06-13 01:04:52 +0000729}
730
danielk1977176f4d22006-06-15 10:41:15 +0000731/*
danielk1977cc013f82006-06-24 06:36:11 +0000732** The xUpdate method for echo module virtual tables.
733**
danielk1977176f4d22006-06-15 10:41:15 +0000734** apData[0] apData[1] apData[2..]
735**
736** INTEGER DELETE
737**
738** INTEGER NULL (nCol args) UPDATE (do not set rowid)
739** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
740**
741** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
742** NULL INTEGER (nCol args) INSERT (incl. rowid value)
743**
744*/
danielk19771f6eec52006-06-16 06:17:47 +0000745int echoUpdate(
746 sqlite3_vtab *tab,
747 int nData,
748 sqlite3_value **apData,
749 sqlite_int64 *pRowid
750){
danielk197726e41442006-06-14 15:16:35 +0000751 echo_vtab *pVtab = (echo_vtab *)tab;
752 sqlite3 *db = pVtab->db;
753 int rc = SQLITE_OK;
754
danielk1977176f4d22006-06-15 10:41:15 +0000755 sqlite3_stmt *pStmt;
756 char *z = 0; /* SQL statement to execute */
757 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
758 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
759 int i; /* Counter variable used by for loops */
760
danielk197726e41442006-06-14 15:16:35 +0000761 assert( nData==pVtab->nCol+2 || nData==1 );
762
drh5aec0422006-06-14 23:43:31 +0000763 /* If apData[0] is an integer and nData>1 then do an UPDATE */
764 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
danielk1977176f4d22006-06-15 10:41:15 +0000765 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
drh5aec0422006-06-14 23:43:31 +0000766 char *zSep = " SET";
danielk1977176f4d22006-06-15 10:41:15 +0000767
768 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
769 bindArgZero = 1;
770
771 if( bindArgOne ){
772 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000773 zSep = ",";
774 }
775 for(i=2; i<nData; i++){
776 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000777 string_concat(&z, sqlite3_mprintf(
778 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000779 zSep = ",";
780 }
danielk1977176f4d22006-06-15 10:41:15 +0000781 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000782 }
783
danielk1977176f4d22006-06-15 10:41:15 +0000784 /* If apData[0] is an integer and nData==1 then do a DELETE */
785 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
786 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
787 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000788 }
789
danielk1977176f4d22006-06-15 10:41:15 +0000790 /* If the first argument is NULL and there are more than two args, INSERT */
791 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000792 int ii;
793 char *zInsert = 0;
794 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000795
danielk19774b2688a2006-06-20 11:01:07 +0000796 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000797 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
798 bindArgOne = 1;
799 zValues = sqlite3_mprintf("?");
800 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000801 }
802
danielk1977176f4d22006-06-15 10:41:15 +0000803 assert((pVtab->nCol+2)==nData);
804 for(ii=2; ii<nData; ii++){
805 string_concat(&zInsert,
806 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
807 string_concat(&zValues,
808 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000809 }
810
danielk1977176f4d22006-06-15 10:41:15 +0000811 string_concat(&z, zInsert, 1);
812 string_concat(&z, ") VALUES(", 0);
813 string_concat(&z, zValues, 1);
814 string_concat(&z, ")", 0);
815 }
816
817 /* Anything else is an error */
818 else{
819 assert(0);
820 return SQLITE_ERROR;
821 }
822
823 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
824 assert( rc!=SQLITE_OK || pStmt );
825 sqlite3_free(z);
826 if( rc==SQLITE_OK ) {
827 if( bindArgZero ){
828 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000829 }
danielk1977176f4d22006-06-15 10:41:15 +0000830 if( bindArgOne ){
831 sqlite3_bind_value(pStmt, 1, apData[1]);
832 }
833 for(i=2; i<nData; i++){
834 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
835 }
836 sqlite3_step(pStmt);
837 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000838 }
839
danielk19771f6eec52006-06-16 06:17:47 +0000840 if( pRowid && rc==SQLITE_OK ){
841 *pRowid = sqlite3_last_insert_rowid(db);
842 }
843
danielk197726e41442006-06-14 15:16:35 +0000844 return rc;
845}
846
drha967e882006-06-13 01:04:52 +0000847/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000848** xBegin, xSync, xCommit and xRollback callbacks for echo module
849** virtual tables. Do nothing other than add the name of the callback
850** to the $::echo_module Tcl variable.
851*/
852static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
853 char *z;
854 echo_vtab *pVtab = (echo_vtab *)tab;
855 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
856 appendToEchoModule(pVtab->interp, zCall);
857 appendToEchoModule(pVtab->interp, z);
858 sqlite3_free(z);
859 return SQLITE_OK;
860}
861static int echoBegin(sqlite3_vtab *tab){
danielk19775017dc32006-06-24 09:34:22 +0000862 echo_vtab *pVtab = (echo_vtab *)tab;
863 Tcl_Interp *interp = pVtab->interp;
864 const char *zVal;
865
866 echoTransactionCall(tab, "xBegin");
867
868 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
869 ** and it is set to the name of the real table underlying this virtual
870 ** echo module table, then cause this xSync operation to fail.
871 */
872 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
873 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
874 return SQLITE_ERROR;
875 }
876 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +0000877}
878static int echoSync(sqlite3_vtab *tab){
879 echo_vtab *pVtab = (echo_vtab *)tab;
880 Tcl_Interp *interp = pVtab->interp;
881 const char *zVal;
882
883 echoTransactionCall(tab, "xSync");
884
885 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
886 ** and it is set to the name of the real table underlying this virtual
887 ** echo module table, then cause this xSync operation to fail.
888 */
889 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
890 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
891 return -1;
892 }
893 return SQLITE_OK;
894}
895static int echoCommit(sqlite3_vtab *tab){
896 return echoTransactionCall(tab, "xCommit");
897}
898static int echoRollback(sqlite3_vtab *tab){
899 return echoTransactionCall(tab, "xRollback");
900}
901
902/*
drhe94b0c32006-07-08 18:09:15 +0000903** Implementation of "GLOB" function on the echo module. Pass
904** all arguments to the ::echo_glob_overload procedure of TCL
905** and return the result of that procedure as a string.
906*/
907static void overloadedGlobFunction(
908 sqlite3_context *pContext,
909 int nArg,
910 sqlite3_value **apArg
911){
912 Tcl_Interp *interp = sqlite3_user_data(pContext);
913 Tcl_DString str;
914 int i;
915 int rc;
916 Tcl_DStringInit(&str);
917 Tcl_DStringAppendElement(&str, "::echo_glob_overload");
918 for(i=0; i<nArg; i++){
919 Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
920 }
921 rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
922 Tcl_DStringFree(&str);
923 if( rc ){
924 sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
925 }else{
926 sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
927 -1, SQLITE_TRANSIENT);
928 }
929 Tcl_ResetResult(interp);
930}
931
932/*
933** This is the xFindFunction implementation for the echo module.
934** SQLite calls this routine when the first argument of a function
935** is a column of an echo virtual table. This routine can optionally
936** override the implementation of that function. It will choose to
937** do so if the function is named "glob", and a TCL command named
938** ::echo_glob_overload exists.
939*/
940static int echoFindFunction(
941 sqlite3_vtab *vtab,
942 int nArg,
943 const char *zFuncName,
944 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
945 void **ppArg
946){
947 echo_vtab *pVtab = (echo_vtab *)vtab;
948 Tcl_Interp *interp = pVtab->interp;
949 Tcl_CmdInfo info;
950 if( strcmp(zFuncName,"glob")!=0 ){
951 return 0;
952 }
953 if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
954 return 0;
955 }
956 *pxFunc = overloadedGlobFunction;
957 *ppArg = interp;
958 return 1;
959}
960
961/*
danielk1977cc013f82006-06-24 06:36:11 +0000962** A virtual table module that merely "echos" the contents of another
963** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +0000964*/
965static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000966 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +0000967 echoCreate,
968 echoConnect,
drha967e882006-06-13 01:04:52 +0000969 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000970 echoDisconnect,
971 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000972 echoOpen, /* xOpen - open a cursor */
973 echoClose, /* xClose - close a cursor */
974 echoFilter, /* xFilter - configure scan constraints */
975 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +0000976 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000977 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000978 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +0000979 echoUpdate, /* xUpdate - write data */
980 echoBegin, /* xBegin - begin transaction */
981 echoSync, /* xSync - sync transaction */
982 echoCommit, /* xCommit - commit transaction */
drhb7f6f682006-07-08 17:06:43 +0000983 echoRollback, /* xRollback - rollback transaction */
drhe94b0c32006-07-08 18:09:15 +0000984 echoFindFunction, /* xFindFunction - function overloading */
drhb9bb7c12006-06-11 23:41:55 +0000985};
986
987/*
988** Decode a pointer to an sqlite3 object.
989*/
990static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
991 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
992 return TCL_OK;
993}
994
drhb9bb7c12006-06-11 23:41:55 +0000995/*
996** Register the echo virtual table module.
997*/
998static int register_echo_module(
999 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1000 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1001 int objc, /* Number of arguments */
1002 Tcl_Obj *CONST objv[] /* Command arguments */
1003){
1004 sqlite3 *db;
1005 if( objc!=2 ){
1006 Tcl_WrongNumArgs(interp, 1, objv, "DB");
1007 return TCL_ERROR;
1008 }
1009 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977d1ab1ba2006-06-15 04:28:13 +00001010 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +00001011 return TCL_OK;
1012}
1013
danielk19775017dc32006-06-24 09:34:22 +00001014/*
1015** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1016**
1017** sqlite3_declare_vtab DB SQL
1018*/
1019static int declare_vtab(
1020 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1021 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1022 int objc, /* Number of arguments */
1023 Tcl_Obj *CONST objv[] /* Command arguments */
1024){
1025 sqlite3 *db;
1026 int rc;
1027 if( objc!=3 ){
1028 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1029 return TCL_ERROR;
1030 }
1031 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1032 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1033 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +00001034 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +00001035 return TCL_ERROR;
1036 }
1037 return TCL_OK;
1038}
1039
danielk1977cc013f82006-06-24 06:36:11 +00001040#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +00001041
1042/*
1043** Register commands with the TCL interpreter.
1044*/
1045int Sqlitetest8_Init(Tcl_Interp *interp){
1046 static struct {
1047 char *zName;
1048 Tcl_ObjCmdProc *xProc;
1049 void *clientData;
1050 } aObjCmd[] = {
danielk1977cc013f82006-06-24 06:36:11 +00001051#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +00001052 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +00001053 { "sqlite3_declare_vtab", declare_vtab, 0 },
danielk1977cc013f82006-06-24 06:36:11 +00001054#endif
drhb9bb7c12006-06-11 23:41:55 +00001055 };
1056 int i;
1057 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1058 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1059 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
1060 }
1061 return TCL_OK;
1062}