blob: 179dc6d492e1a80e83226a04bb0c7c8c5aa9559b [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**
drh19146192006-06-26 19:10:32 +000016** $Id: test8.c,v 1.38 2006/06/26 19:10:32 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,
260 char **argv
261){
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,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000314 int argc, char **argv,
315 sqlite3_vtab **ppVtab
316){
317 int i;
318 echo_vtab *pVtab;
319
danielk1977cc013f82006-06-24 06:36:11 +0000320 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000321 pVtab = sqliteMalloc( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000322 if( !pVtab ){
323 return SQLITE_NOMEM;
324 }
danielk19779da9d472006-06-14 06:58:15 +0000325 pVtab->interp = (Tcl_Interp *)pAux;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000326 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000327
328 /* Allocate echo_vtab.zTableName */
danielk197770ba1642006-06-21 16:02:42 +0000329 pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
danielk1977be718892006-06-23 08:05:19 +0000330 if( !pVtab->zTableName ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000331 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000332 return SQLITE_NOMEM;
333 }
334
danielk1977cc013f82006-06-24 06:36:11 +0000335 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000336 for(i=0; i<argc; i++){
337 appendToEchoModule(pVtab->interp, argv[i]);
338 }
339
danielk1977cc013f82006-06-24 06:36:11 +0000340 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
341 ** structure. If an error occurs, delete the sqlite3_vtab structure and
342 ** return an error code.
343 */
danielk1977a4e76362006-06-14 06:31:28 +0000344 if( echoDeclareVtab(pVtab, db, argc, argv) ){
345 echoDestructor((sqlite3_vtab *)pVtab);
346 return SQLITE_ERROR;
347 }
348
danielk1977cc013f82006-06-24 06:36:11 +0000349 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000350 *ppVtab = &pVtab->base;
351 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000352}
drha967e882006-06-13 01:04:52 +0000353
danielk1977cc013f82006-06-24 06:36:11 +0000354/*
355** Echo virtual table module xCreate method.
356*/
drhb9bb7c12006-06-11 23:41:55 +0000357static int echoCreate(
358 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000359 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000360 int argc, char **argv,
361 sqlite3_vtab **ppVtab
362){
danielk1977a298e902006-06-22 09:53:48 +0000363 int rc = SQLITE_OK;
danielk19779da9d472006-06-14 06:58:15 +0000364 appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
danielk1977a298e902006-06-22 09:53:48 +0000365 rc = echoConstructor(db, pAux, argc, argv, ppVtab);
danielk1977cc013f82006-06-24 06:36:11 +0000366
367 /* If there were two arguments passed to the module at the SQL level
368 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
369 ** the second argument is used as a table name. Attempt to create
370 ** such a table with a single column, "logmsg". This table will
371 ** be used to log calls to the xUpdate method. It will be deleted
372 ** when the virtual table is DROPed.
373 **
374 ** Note: The main point of this is to test that we can drop tables
375 ** from within an xDestroy method call.
376 */
danielk1977a298e902006-06-22 09:53:48 +0000377 if( rc==SQLITE_OK && argc==5 ){
378 char *zSql;
379 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
380 pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
381 zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
382 rc = sqlite3_exec(db, zSql, 0, 0, 0);
383 sqliteFree(zSql);
384 }
danielk1977cc013f82006-06-24 06:36:11 +0000385
danielk1977a298e902006-06-22 09:53:48 +0000386 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000387}
danielk1977cc013f82006-06-24 06:36:11 +0000388
389/*
390** Echo virtual table module xConnect method.
391*/
drhb9bb7c12006-06-11 23:41:55 +0000392static int echoConnect(
393 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000394 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000395 int argc, char **argv,
396 sqlite3_vtab **ppVtab
397){
danielk19779da9d472006-06-14 06:58:15 +0000398 appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
399 return echoConstructor(db, pAux, argc, argv, ppVtab);
drhb9bb7c12006-06-11 23:41:55 +0000400}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000401
danielk1977cc013f82006-06-24 06:36:11 +0000402/*
403** Echo virtual table module xDisconnect method.
404*/
danielk19775fac9f82006-06-13 14:16:58 +0000405static int echoDisconnect(sqlite3_vtab *pVtab){
406 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
407 return echoDestructor(pVtab);
408}
danielk1977cc013f82006-06-24 06:36:11 +0000409
410/*
411** Echo virtual table module xDestroy method.
412*/
danielk19775fac9f82006-06-13 14:16:58 +0000413static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000414 int rc = SQLITE_OK;
415 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000416 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000417
418 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000419 if( p && p->zLogName ){
420 char *zSql;
421 zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName);
422 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
423 sqliteFree(zSql);
424 }
danielk1977cc013f82006-06-24 06:36:11 +0000425
danielk1977a298e902006-06-22 09:53:48 +0000426 if( rc==SQLITE_OK ){
427 rc = echoDestructor(pVtab);
428 }
429 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000430}
431
danielk1977cc013f82006-06-24 06:36:11 +0000432/*
433** Echo virtual table module xOpen method.
434*/
danielk19775fac9f82006-06-13 14:16:58 +0000435static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000436 echo_cursor *pCur;
437 pCur = sqliteMalloc(sizeof(echo_cursor));
438 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000439 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000440}
441
danielk1977cc013f82006-06-24 06:36:11 +0000442/*
443** Echo virtual table module xClose method.
444*/
danielk19775fac9f82006-06-13 14:16:58 +0000445static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000446 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000447 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000448 sqlite3_stmt *pStmt = pCur->pStmt;
449 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000450 sqliteFree(pCur);
danielk1977be718892006-06-23 08:05:19 +0000451 rc = sqlite3_finalize(pStmt);
452 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000453}
454
danielk1977a298e902006-06-22 09:53:48 +0000455/*
456** Return non-zero if the cursor does not currently point to a valid record
457** (i.e if the scan has finished), or zero otherwise.
458*/
459static int echoEof(sqlite3_vtab_cursor *cur){
460 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
461}
462
danielk1977cc013f82006-06-24 06:36:11 +0000463/*
464** Echo virtual table module xNext method.
465*/
danielk19775fac9f82006-06-13 14:16:58 +0000466static int echoNext(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000467 int rc;
468 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000469 rc = sqlite3_step(pCur->pStmt);
470
471 if( rc==SQLITE_ROW ){
danielk1977a298e902006-06-22 09:53:48 +0000472 rc = SQLITE_OK;
473 }else{
474 rc = sqlite3_finalize(pCur->pStmt);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000475 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000476 }
477
478 return rc;
479}
480
danielk1977cc013f82006-06-24 06:36:11 +0000481/*
482** Echo virtual table module xColumn method.
483*/
danielk19775fac9f82006-06-13 14:16:58 +0000484static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000485 int iCol = i + 1;
486 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk1977fbbe0052006-06-21 07:02:33 +0000487 if( !pStmt ){
488 sqlite3_result_null(ctx);
489 }else{
490 assert( sqlite3_data_count(pStmt)>iCol );
491 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
492 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000493 return SQLITE_OK;
494}
495
danielk1977cc013f82006-06-24 06:36:11 +0000496/*
497** Echo virtual table module xRowid method.
498*/
danielk19775fac9f82006-06-13 14:16:58 +0000499static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000500 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
501 *pRowid = sqlite3_column_int64(pStmt, 0);
502 return SQLITE_OK;
503}
504
danielk197798331532006-06-14 10:55:52 +0000505/*
506** Compute a simple hash of the null terminated string zString.
507**
508** This module uses only sqlite3_index_info.idxStr, not
509** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
510** in echoBestIndex(), idxNum is set to the corresponding hash value.
511** In echoFilter(), code assert()s that the supplied idxNum value is
512** indeed the hash of the supplied idxStr.
513*/
514static int hashString(const char *zString){
515 int val = 0;
516 int ii;
517 for(ii=0; zString[ii]; ii++){
518 val = (val << 3) + (int)zString[ii];
519 }
520 return val;
521}
522
danielk1977cc013f82006-06-24 06:36:11 +0000523/*
524** Echo virtual table module xFilter method.
525*/
danielk19775fac9f82006-06-13 14:16:58 +0000526static int echoFilter(
527 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000528 int idxNum, const char *idxStr,
529 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000530){
531 int rc;
drh4be8b512006-06-13 23:51:34 +0000532 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000533
534 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
535 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
536 sqlite3 *db = pVtab->db;
537
danielk1977cc013f82006-06-24 06:36:11 +0000538 /* Check that idxNum matches idxStr */
539 assert( idxNum==hashString(idxStr) );
540
541 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000542 appendToEchoModule(pVtab->interp, "xFilter");
543 appendToEchoModule(pVtab->interp, idxStr);
544 for(i=0; i<argc; i++){
danielk197765fd59f2006-06-24 11:51:33 +0000545 appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
danielk1977be718892006-06-23 08:05:19 +0000546 }
547
danielk19775fac9f82006-06-13 14:16:58 +0000548 sqlite3_finalize(pCur->pStmt);
549 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000550
551 /* Prepare the SQL statement created by echoBestIndex and bind the
552 ** runtime parameters passed to this function to it.
553 */
drh4be8b512006-06-13 23:51:34 +0000554 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000555 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000556 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk1977cc013f82006-06-24 06:36:11 +0000557 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000558 }
danielk1977cc013f82006-06-24 06:36:11 +0000559
560 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000561 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000562 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000563 }
564
danielk1977be718892006-06-23 08:05:19 +0000565 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000566}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000567
danielk1977cc013f82006-06-24 06:36:11 +0000568
569/*
570** A helper function used by echoUpdate() and echoBestIndex() for
571** manipulating strings in concert with the sqlite3_mprintf() function.
572**
573** Parameter pzStr points to a pointer to a string allocated with
574** sqlite3_mprintf. The second parameter, zAppend, points to another
575** string. The two strings are concatenated together and *pzStr
576** set to point at the result. The initial buffer pointed to by *pzStr
577** is deallocated via sqlite3_free().
578**
579** If the third argument, doFree, is true, then sqlite3_free() is
580** also called to free the buffer pointed to by zAppend.
581*/
582static void string_concat(char **pzStr, char *zAppend, int doFree){
583 char *zIn = *pzStr;
584 if( zIn ){
585 char *zTemp = zIn;
586 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
587 sqlite3_free(zTemp);
588 }else{
589 zIn = sqlite3_mprintf("%s", zAppend);
590 }
591 *pzStr = zIn;
592 if( doFree ){
593 sqlite3_free(zAppend);
594 }
595}
596
drhb9bb7c12006-06-11 23:41:55 +0000597/*
danielk19775fac9f82006-06-13 14:16:58 +0000598** The echo module implements the subset of query constraints and sort
599** orders that may take advantage of SQLite indices on the underlying
600** real table. For example, if the real table is declared as:
601**
602** CREATE TABLE real(a, b, c);
603** CREATE INDEX real_index ON real(b);
604**
605** then the echo module handles WHERE or ORDER BY clauses that refer
606** to the column "b", but not "a" or "c". If a multi-column index is
607** present, only it's left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000608**
609** This xBestIndex method encodes the proposed search strategy as
610** an SQL query on the real table underlying the virtual echo module
611** table and stores the query in sqlite3_index_info.idxStr. The SQL
612** statement is of the form:
613**
614** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
615**
616** where the <where-clause> and <order-by-clause> are determined
617** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000618*/
danielk19775fac9f82006-06-13 14:16:58 +0000619static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
620 int ii;
drh4be8b512006-06-13 23:51:34 +0000621 char *zQuery = 0;
622 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000623 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000624 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000625 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000626 sqlite3_stmt *pStmt = 0;
627
628 int nRow;
629 int useIdx = 0;
630 int rc = SQLITE_OK;
631
632 /* Determine the number of rows in the table and store this value in local
633 ** variable nRow. The 'estimated-cost' of the scan will be the number of
634 ** rows in the table for a linear scan, or the log (base 2) of the
635 ** number of rows if the proposed scan uses an index.
636 */
637 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
638 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
639 if( rc!=SQLITE_OK ){
640 return rc;
641 }
642 sqlite3_step(pStmt);
643 nRow = sqlite3_column_int(pStmt, 0);
644 rc = sqlite3_finalize(pStmt);
645 if( rc!=SQLITE_OK ){
646 return rc;
647 }
danielk19775fac9f82006-06-13 14:16:58 +0000648
drh4be8b512006-06-13 23:51:34 +0000649 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000650 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
651 const struct sqlite3_index_constraint *pConstraint;
652 struct sqlite3_index_constraint_usage *pUsage;
653
654 pConstraint = &pIdxInfo->aConstraint[ii];
655 pUsage = &pIdxInfo->aConstraintUsage[ii];
656
657 int iCol = pConstraint->iColumn;
658 if( pVtab->aIndex[iCol] ){
659 char *zCol = pVtab->aCol[iCol];
660 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000661 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000662 if( iCol<0 ){
663 zCol = "rowid";
664 }
danielk19775fac9f82006-06-13 14:16:58 +0000665 switch( pConstraint->op ){
666 case SQLITE_INDEX_CONSTRAINT_EQ:
667 zOp = "="; break;
668 case SQLITE_INDEX_CONSTRAINT_LT:
669 zOp = "<"; break;
670 case SQLITE_INDEX_CONSTRAINT_GT:
671 zOp = ">"; break;
672 case SQLITE_INDEX_CONSTRAINT_LE:
673 zOp = "<="; break;
674 case SQLITE_INDEX_CONSTRAINT_GE:
675 zOp = ">="; break;
676 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000677 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000678 }
drh1a90e092006-06-14 22:07:10 +0000679 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000680 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
681 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000682 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000683 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000684 }
danielk1977cc013f82006-06-24 06:36:11 +0000685 string_concat(&zQuery, zNew, 1);
686
drh4be8b512006-06-13 23:51:34 +0000687 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000688 pUsage->argvIndex = ++nArg;
689 pUsage->omit = 1;
690 }
691 }
danielk197747d08092006-06-14 07:41:31 +0000692
693 /* If there is only one term in the ORDER BY clause, and it is
694 ** on a column that this virtual table has an index for, then consume
695 ** the ORDER BY clause.
696 */
697 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
danielk197765fd59f2006-06-24 11:51:33 +0000698 int iCol = pIdxInfo->aOrderBy->iColumn;
699 char *zCol = pVtab->aCol[iCol];
danielk197747d08092006-06-14 07:41:31 +0000700 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk197765fd59f2006-06-24 11:51:33 +0000701 if( iCol<0 ){
702 zCol = "rowid";
703 }
danielk1977cc013f82006-06-24 06:36:11 +0000704 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
705 string_concat(&zQuery, zNew, 1);
danielk197747d08092006-06-14 07:41:31 +0000706 pIdxInfo->orderByConsumed = 1;
707 }
708
danielk19775fac9f82006-06-13 14:16:58 +0000709 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000710 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000711
danielk197798331532006-06-14 10:55:52 +0000712 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000713 pIdxInfo->idxStr = zQuery;
714 pIdxInfo->needToFreeIdxStr = 1;
danielk197774cdba42006-06-19 12:02:58 +0000715 if( useIdx ){
716 /* Approximation of log2(nRow). */
717 for( ii=0; ii<(sizeof(int)*8); ii++ ){
718 if( nRow & (1<<ii) ){
719 pIdxInfo->estimatedCost = (double)ii;
720 }
721 }
722 } else {
723 pIdxInfo->estimatedCost = (double)nRow;
724 }
725 return rc;
drha967e882006-06-13 01:04:52 +0000726}
727
danielk1977176f4d22006-06-15 10:41:15 +0000728/*
danielk1977cc013f82006-06-24 06:36:11 +0000729** The xUpdate method for echo module virtual tables.
730**
danielk1977176f4d22006-06-15 10:41:15 +0000731** apData[0] apData[1] apData[2..]
732**
733** INTEGER DELETE
734**
735** INTEGER NULL (nCol args) UPDATE (do not set rowid)
736** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
737**
738** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
739** NULL INTEGER (nCol args) INSERT (incl. rowid value)
740**
741*/
danielk19771f6eec52006-06-16 06:17:47 +0000742int echoUpdate(
743 sqlite3_vtab *tab,
744 int nData,
745 sqlite3_value **apData,
746 sqlite_int64 *pRowid
747){
danielk197726e41442006-06-14 15:16:35 +0000748 echo_vtab *pVtab = (echo_vtab *)tab;
749 sqlite3 *db = pVtab->db;
750 int rc = SQLITE_OK;
751
danielk1977176f4d22006-06-15 10:41:15 +0000752 sqlite3_stmt *pStmt;
753 char *z = 0; /* SQL statement to execute */
754 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
755 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
756 int i; /* Counter variable used by for loops */
757
danielk197726e41442006-06-14 15:16:35 +0000758 assert( nData==pVtab->nCol+2 || nData==1 );
759
drh5aec0422006-06-14 23:43:31 +0000760 /* If apData[0] is an integer and nData>1 then do an UPDATE */
761 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
danielk1977176f4d22006-06-15 10:41:15 +0000762 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
drh5aec0422006-06-14 23:43:31 +0000763 char *zSep = " SET";
danielk1977176f4d22006-06-15 10:41:15 +0000764
765 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
766 bindArgZero = 1;
767
768 if( bindArgOne ){
769 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000770 zSep = ",";
771 }
772 for(i=2; i<nData; i++){
773 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000774 string_concat(&z, sqlite3_mprintf(
775 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000776 zSep = ",";
777 }
danielk1977176f4d22006-06-15 10:41:15 +0000778 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000779 }
780
danielk1977176f4d22006-06-15 10:41:15 +0000781 /* If apData[0] is an integer and nData==1 then do a DELETE */
782 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
783 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
784 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000785 }
786
danielk1977176f4d22006-06-15 10:41:15 +0000787 /* If the first argument is NULL and there are more than two args, INSERT */
788 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000789 int ii;
790 char *zInsert = 0;
791 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000792
danielk19774b2688a2006-06-20 11:01:07 +0000793 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000794 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
795 bindArgOne = 1;
796 zValues = sqlite3_mprintf("?");
797 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000798 }
799
danielk1977176f4d22006-06-15 10:41:15 +0000800 assert((pVtab->nCol+2)==nData);
801 for(ii=2; ii<nData; ii++){
802 string_concat(&zInsert,
803 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
804 string_concat(&zValues,
805 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000806 }
807
danielk1977176f4d22006-06-15 10:41:15 +0000808 string_concat(&z, zInsert, 1);
809 string_concat(&z, ") VALUES(", 0);
810 string_concat(&z, zValues, 1);
811 string_concat(&z, ")", 0);
812 }
813
814 /* Anything else is an error */
815 else{
816 assert(0);
817 return SQLITE_ERROR;
818 }
819
820 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
821 assert( rc!=SQLITE_OK || pStmt );
822 sqlite3_free(z);
823 if( rc==SQLITE_OK ) {
824 if( bindArgZero ){
825 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000826 }
danielk1977176f4d22006-06-15 10:41:15 +0000827 if( bindArgOne ){
828 sqlite3_bind_value(pStmt, 1, apData[1]);
829 }
830 for(i=2; i<nData; i++){
831 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
832 }
833 sqlite3_step(pStmt);
834 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000835 }
836
danielk19771f6eec52006-06-16 06:17:47 +0000837 if( pRowid && rc==SQLITE_OK ){
838 *pRowid = sqlite3_last_insert_rowid(db);
839 }
840
danielk197726e41442006-06-14 15:16:35 +0000841 return rc;
842}
843
drha967e882006-06-13 01:04:52 +0000844/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000845** xBegin, xSync, xCommit and xRollback callbacks for echo module
846** virtual tables. Do nothing other than add the name of the callback
847** to the $::echo_module Tcl variable.
848*/
849static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
850 char *z;
851 echo_vtab *pVtab = (echo_vtab *)tab;
852 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
853 appendToEchoModule(pVtab->interp, zCall);
854 appendToEchoModule(pVtab->interp, z);
855 sqlite3_free(z);
856 return SQLITE_OK;
857}
858static int echoBegin(sqlite3_vtab *tab){
danielk19775017dc32006-06-24 09:34:22 +0000859 echo_vtab *pVtab = (echo_vtab *)tab;
860 Tcl_Interp *interp = pVtab->interp;
861 const char *zVal;
862
863 echoTransactionCall(tab, "xBegin");
864
865 /* Check if the $::echo_module_begin_fail variable is defined. If it is,
866 ** and it is set to the name of the real table underlying this virtual
867 ** echo module table, then cause this xSync operation to fail.
868 */
869 zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
870 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
871 return SQLITE_ERROR;
872 }
873 return SQLITE_OK;
danielk1977c69cdfd2006-06-17 09:39:55 +0000874}
875static int echoSync(sqlite3_vtab *tab){
876 echo_vtab *pVtab = (echo_vtab *)tab;
877 Tcl_Interp *interp = pVtab->interp;
878 const char *zVal;
879
880 echoTransactionCall(tab, "xSync");
881
882 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
883 ** and it is set to the name of the real table underlying this virtual
884 ** echo module table, then cause this xSync operation to fail.
885 */
886 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
887 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
888 return -1;
889 }
890 return SQLITE_OK;
891}
892static int echoCommit(sqlite3_vtab *tab){
893 return echoTransactionCall(tab, "xCommit");
894}
895static int echoRollback(sqlite3_vtab *tab){
896 return echoTransactionCall(tab, "xRollback");
897}
898
899/*
danielk1977cc013f82006-06-24 06:36:11 +0000900** A virtual table module that merely "echos" the contents of another
901** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +0000902*/
903static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000904 0, /* iVersion */
drhb9bb7c12006-06-11 23:41:55 +0000905 echoCreate,
906 echoConnect,
drha967e882006-06-13 01:04:52 +0000907 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000908 echoDisconnect,
909 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000910 echoOpen, /* xOpen - open a cursor */
911 echoClose, /* xClose - close a cursor */
912 echoFilter, /* xFilter - configure scan constraints */
913 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +0000914 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000915 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000916 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +0000917 echoUpdate, /* xUpdate - write data */
918 echoBegin, /* xBegin - begin transaction */
919 echoSync, /* xSync - sync transaction */
920 echoCommit, /* xCommit - commit transaction */
921 echoRollback /* xRollback - rollback transaction */
drhb9bb7c12006-06-11 23:41:55 +0000922};
923
924/*
925** Decode a pointer to an sqlite3 object.
926*/
927static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
928 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
929 return TCL_OK;
930}
931
drhb9bb7c12006-06-11 23:41:55 +0000932/*
933** Register the echo virtual table module.
934*/
935static int register_echo_module(
936 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
937 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
938 int objc, /* Number of arguments */
939 Tcl_Obj *CONST objv[] /* Command arguments */
940){
941 sqlite3 *db;
942 if( objc!=2 ){
943 Tcl_WrongNumArgs(interp, 1, objv, "DB");
944 return TCL_ERROR;
945 }
946 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000947 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +0000948 return TCL_OK;
949}
950
danielk19775017dc32006-06-24 09:34:22 +0000951/*
952** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
953**
954** sqlite3_declare_vtab DB SQL
955*/
956static int declare_vtab(
957 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
958 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
959 int objc, /* Number of arguments */
960 Tcl_Obj *CONST objv[] /* Command arguments */
961){
962 sqlite3 *db;
963 int rc;
964 if( objc!=3 ){
965 Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
966 return TCL_ERROR;
967 }
968 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
969 rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
970 if( rc!=SQLITE_OK ){
danielk197765fd59f2006-06-24 11:51:33 +0000971 Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
danielk19775017dc32006-06-24 09:34:22 +0000972 return TCL_ERROR;
973 }
974 return TCL_OK;
975}
976
danielk1977cc013f82006-06-24 06:36:11 +0000977#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +0000978
979/*
980** Register commands with the TCL interpreter.
981*/
982int Sqlitetest8_Init(Tcl_Interp *interp){
983 static struct {
984 char *zName;
985 Tcl_ObjCmdProc *xProc;
986 void *clientData;
987 } aObjCmd[] = {
danielk1977cc013f82006-06-24 06:36:11 +0000988#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +0000989 { "register_echo_module", register_echo_module, 0 },
danielk19775017dc32006-06-24 09:34:22 +0000990 { "sqlite3_declare_vtab", declare_vtab, 0 },
danielk1977cc013f82006-06-24 06:36:11 +0000991#endif
drhb9bb7c12006-06-11 23:41:55 +0000992 };
993 int i;
994 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
995 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
996 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
997 }
998 return TCL_OK;
999}