blob: be4327b435dc5ce981e69907b69131f6b6db3e99 [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**
danielk1977cc013f82006-06-24 06:36:11 +000016** $Id: test8.c,v 1.35 2006/06/24 06:36:11 danielk1977 Exp $
drhb9bb7c12006-06-11 23:41:55 +000017*/
18#include "sqliteInt.h"
19#include "tcl.h"
20#include "os.h"
21#include <stdlib.h>
22#include <string.h>
23
danielk1977cc013f82006-06-24 06:36:11 +000024#ifndef SQLITE_OMIT_VIRTUALTABLE
25
danielk1977b7a7b9a2006-06-13 10:24:42 +000026typedef struct echo_vtab echo_vtab;
27typedef struct echo_cursor echo_cursor;
28
danielk1977c69cdfd2006-06-17 09:39:55 +000029/*
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
danielk1977cc013f82006-06-24 06:36:11 +000035**
36** The variable ::echo_module is a list. Each time one of the following
37** methods is called, one or more elements are appended to the list.
38** This is used for automated testing of virtual table modules.
39**
40** The ::echo_module_sync_fail variable is set by test scripts and read
41** by code in this file. If it is set to the name of a real table in the
42** the database, then all xSync operations on echo virtual tables that
43** use the named table as a backing store will fail.
danielk1977c69cdfd2006-06-17 09:39:55 +000044*/
45
danielk19775fac9f82006-06-13 14:16:58 +000046/*
danielk1977d6e8dd02006-06-15 15:38:41 +000047** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:58 +000048**
danielk1977d6e8dd02006-06-15 15:38:41 +000049** echo.vtab.aIndex is an array of booleans. The nth entry is true if
50** the nth column of the real table is the left-most column of an index
51** (implicit or otherwise). In other words, if SQLite can optimize
52** a query like "SELECT * FROM real_table WHERE col = ?".
53**
danielk1977c69cdfd2006-06-17 09:39:55 +000054** Member variable aCol[] contains copies of the column names of the real
55** table.
danielk19775fac9f82006-06-13 14:16:58 +000056*/
danielk1977b7a7b9a2006-06-13 10:24:42 +000057struct echo_vtab {
58 sqlite3_vtab base;
danielk1977cc013f82006-06-24 06:36:11 +000059 Tcl_Interp *interp; /* Tcl interpreter containing debug variables */
60 sqlite3 *db; /* Database connection */
danielk19775fac9f82006-06-13 14:16:58 +000061
danielk1977a4e76362006-06-14 06:31:28 +000062 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:48 +000063 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:28 +000064 int nCol; /* Number of columns in the real table */
65 int *aIndex; /* Array of size nCol. True if column has an index */
66 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000067};
68
69/* An echo cursor object */
70struct echo_cursor {
71 sqlite3_vtab_cursor base;
72 sqlite3_stmt *pStmt;
danielk1977b7a7b9a2006-06-13 10:24:42 +000073};
74
danielk1977cc013f82006-06-24 06:36:11 +000075/*
76** Retrieve the column names for the table named zTab via database
77** connection db. SQLITE_OK is returned on success, or an sqlite error
78** code otherwise.
79**
80** If successful, the number of columns is written to *pnCol. *paCol is
81** set to point at sqliteMalloc()'d space containing the array of
82** nCol column names. The caller is responsible for calling sqliteFree
83** on *paCol.
84*/
danielk19775fac9f82006-06-13 14:16:58 +000085static int getColumnNames(
86 sqlite3 *db,
87 const char *zTab,
88 char ***paCol,
89 int *pnCol
90){
91 char **aCol = 0;
danielk1977cc013f82006-06-24 06:36:11 +000092 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +000093 sqlite3_stmt *pStmt = 0;
94 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19 +000095 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58 +000096
danielk1977cc013f82006-06-24 06:36:11 +000097 /* Prepare the statement "SELECT * FROM <tbl>". The column names
98 ** of the result set of the compiled SELECT will be the same as
99 ** the column names of table <tbl>.
100 */
101 zSql = sqlite3MPrintf("SELECT * FROM %Q", zTab);
102 if( !zSql ){
103 rc = SQLITE_NOMEM;
104 goto out;
105 }
106 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
107 sqliteFree(zSql);
108
danielk19775fac9f82006-06-13 14:16:58 +0000109 if( rc==SQLITE_OK ){
110 int ii;
danielk1977cc013f82006-06-24 06:36:11 +0000111 int nBytes;
112 char *zSpace;
danielk19775fac9f82006-06-13 14:16:58 +0000113 nCol = sqlite3_column_count(pStmt);
danielk1977cc013f82006-06-24 06:36:11 +0000114
115 /* Figure out how much space to allocate for the array of column names
116 ** (including space for the strings themselves). Then allocate it.
117 */
118 nBytes = sizeof(char *) * nCol;
119 for(ii=0; ii<nCol; ii++){
120 nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1);
121 }
122 aCol = (char **)sqliteMalloc(nBytes);
danielk19775fac9f82006-06-13 14:16:58 +0000123 if( !aCol ){
124 rc = SQLITE_NOMEM;
danielk1977cc013f82006-06-24 06:36:11 +0000125 goto out;
danielk19775fac9f82006-06-13 14:16:58 +0000126 }
danielk1977cc013f82006-06-24 06:36:11 +0000127
128 /* Copy the column names into the allocated space and set up the
129 ** pointers in the aCol[] array.
130 */
131 zSpace = (char *)(&aCol[nCol]);
danielk19775fac9f82006-06-13 14:16:58 +0000132 for(ii=0; ii<nCol; ii++){
danielk1977cc013f82006-06-24 06:36:11 +0000133 aCol[ii] = zSpace;
134 zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
135 zSpace++;
danielk19775fac9f82006-06-13 14:16:58 +0000136 }
danielk1977cc013f82006-06-24 06:36:11 +0000137 assert( (zSpace-nBytes)==(char *)aCol );
danielk19775fac9f82006-06-13 14:16:58 +0000138 }
139
140 *paCol = aCol;
141 *pnCol = nCol;
142
danielk1977cc013f82006-06-24 06:36:11 +0000143out:
danielk19775fac9f82006-06-13 14:16:58 +0000144 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000145 return rc;
146}
147
danielk1977cc013f82006-06-24 06:36:11 +0000148/*
149** Parameter zTab is the name of a table in database db with nCol
150** columns. This function allocates an array of integers nCol in
151** size and populates it according to any implicit or explicit
152** indices on table zTab.
153**
154** If successful, SQLITE_OK is returned and *paIndex set to point
155** at the allocated array. Otherwise, an error code is returned.
156**
157** See comments associated with the member variable aIndex above
158** "struct echo_vtab" for details of the contents of the array.
159*/
160static int getIndexArray(
161 sqlite3 *db, /* Database connection */
162 const char *zTab, /* Name of table in database db */
163 int nCol,
164 int **paIndex
165){
danielk19775fac9f82006-06-13 14:16:58 +0000166 sqlite3_stmt *pStmt = 0;
danielk19775fac9f82006-06-13 14:16:58 +0000167 int *aIndex = 0;
168 int rc;
danielk1977cc013f82006-06-24 06:36:11 +0000169 char *zSql;
danielk19775fac9f82006-06-13 14:16:58 +0000170
danielk1977cc013f82006-06-24 06:36:11 +0000171 /* Allocate space for the index array */
danielk19775fac9f82006-06-13 14:16:58 +0000172 aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
173 if( !aIndex ){
174 rc = SQLITE_NOMEM;
175 goto get_index_array_out;
176 }
177
danielk1977cc013f82006-06-24 06:36:11 +0000178 /* Compile an sqlite pragma to loop through all indices on table zTab */
179 zSql = sqlite3MPrintf("PRAGMA index_list(%s)", zTab);
180 if( !zSql ){
181 rc = SQLITE_NOMEM;
182 goto get_index_array_out;
183 }
184 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
185 sqliteFree(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000186
danielk1977cc013f82006-06-24 06:36:11 +0000187 /* For each index, figure out the left-most column and set the
188 ** corresponding entry in aIndex[] to 1.
189 */
danielk19775fac9f82006-06-13 14:16:58 +0000190 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
danielk1977cc013f82006-06-24 06:36:11 +0000191 const char *zIdx = sqlite3_column_text(pStmt, 1);
danielk19775fac9f82006-06-13 14:16:58 +0000192 sqlite3_stmt *pStmt2 = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000193 zSql = sqlite3MPrintf("PRAGMA index_info(%s)", zIdx);
194 if( !zSql ){
195 rc = SQLITE_NOMEM;
196 goto get_index_array_out;
197 }
198 rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
199 sqliteFree(zSql);
danielk19775fac9f82006-06-13 14:16:58 +0000200 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
201 int cid = sqlite3_column_int(pStmt2, 1);
202 assert( cid>=0 && cid<nCol );
203 aIndex[cid] = 1;
204 }
danielk1977be718892006-06-23 08:05:19 +0000205 if( pStmt2 ){
206 rc = sqlite3_finalize(pStmt2);
207 }
danielk19775fac9f82006-06-13 14:16:58 +0000208 if( rc!=SQLITE_OK ){
danielk19775fac9f82006-06-13 14:16:58 +0000209 goto get_index_array_out;
210 }
211 }
212
danielk19775fac9f82006-06-13 14:16:58 +0000213
214get_index_array_out:
danielk1977cc013f82006-06-24 06:36:11 +0000215 if( pStmt ){
216 int rc2 = sqlite3_finalize(pStmt);
217 if( rc==SQLITE_OK ){
218 rc = rc2;
219 }
220 }
danielk19775fac9f82006-06-13 14:16:58 +0000221 if( rc!=SQLITE_OK ){
222 sqliteFree(aIndex);
223 aIndex = 0;
224 }
225 *paIndex = aIndex;
226 return rc;
227}
228
danielk197778efaba2006-06-12 06:09:17 +0000229/*
230** Global Tcl variable $echo_module is a list. This routine appends
231** the string element zArg to that list in interpreter interp.
232*/
drha967e882006-06-13 01:04:52 +0000233static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000234 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000235 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000236}
237
danielk19777e6ebfb2006-06-12 11:24:37 +0000238/*
239** This function is called from within the echo-modules xCreate and
240** xConnect methods. The argc and argv arguments are copies of those
241** passed to the calling method. This function is responsible for
242** calling sqlite3_declare_vtab() to declare the schema of the virtual
243** table being created or connected.
244**
245** If the constructor was passed just one argument, i.e.:
246**
247** CREATE TABLE t1 AS echo(t2);
248**
249** Then t2 is assumed to be the name of a *real* database table. The
250** schema of the virtual table is declared by passing a copy of the
251** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
252** Hence, the virtual table should have exactly the same column names and
253** types as the real table.
254*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000255static int echoDeclareVtab(
256 echo_vtab *pVtab,
257 sqlite3 *db,
258 int argc,
259 char **argv
260){
danielk19777e6ebfb2006-06-12 11:24:37 +0000261 int rc = SQLITE_OK;
262
danielk1977a298e902006-06-22 09:53:48 +0000263 if( argc>=4 ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000264 sqlite3_stmt *pStmt = 0;
265 sqlite3_prepare(db,
266 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
267 -1, &pStmt, 0);
danielk197770ba1642006-06-21 16:02:42 +0000268 sqlite3_bind_text(pStmt, 1, argv[3], -1, 0);
danielk19777e6ebfb2006-06-12 11:24:37 +0000269 if( sqlite3_step(pStmt)==SQLITE_ROW ){
270 const char *zCreateTable = sqlite3_column_text(pStmt, 0);
271 sqlite3_declare_vtab(db, zCreateTable);
danielk1977be718892006-06-23 08:05:19 +0000272 rc = sqlite3_finalize(pStmt);
danielk19777e6ebfb2006-06-12 11:24:37 +0000273 } else {
danielk1977be718892006-06-23 08:05:19 +0000274 rc = sqlite3_finalize(pStmt);
275 if( rc==SQLITE_OK ){
276 rc = SQLITE_ERROR;
277 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000278 }
danielk1977be718892006-06-23 08:05:19 +0000279
danielk19775fac9f82006-06-13 14:16:58 +0000280 if( rc==SQLITE_OK ){
danielk1977cc013f82006-06-24 06:36:11 +0000281 rc = getColumnNames(db, argv[3], &pVtab->aCol, &pVtab->nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000282 }
283 if( rc==SQLITE_OK ){
danielk1977cc013f82006-06-24 06:36:11 +0000284 rc = getIndexArray(db, argv[3], pVtab->nCol, &pVtab->aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000285 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000286 }
287
288 return rc;
289}
290
danielk1977cc013f82006-06-24 06:36:11 +0000291/*
292** This function frees all runtime structures associated with the virtual
293** table pVtab.
294*/
danielk1977a4e76362006-06-14 06:31:28 +0000295static int echoDestructor(sqlite3_vtab *pVtab){
danielk1977a4e76362006-06-14 06:31:28 +0000296 echo_vtab *p = (echo_vtab*)pVtab;
297 sqliteFree(p->aIndex);
danielk1977a4e76362006-06-14 06:31:28 +0000298 sqliteFree(p->aCol);
299 sqliteFree(p->zTableName);
danielk1977212b2182006-06-23 14:32:08 +0000300 sqliteFree(p->zLogName);
danielk1977a4e76362006-06-14 06:31:28 +0000301 sqliteFree(p);
302 return 0;
303}
304
danielk1977cc013f82006-06-24 06:36:11 +0000305/*
306** This function is called to do the work of the xConnect() method -
307** to allocate the required in-memory structures for a newly connected
308** virtual table.
309*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000310static int echoConstructor(
311 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000312 void *pAux,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000313 int argc, char **argv,
314 sqlite3_vtab **ppVtab
315){
316 int i;
317 echo_vtab *pVtab;
318
danielk1977cc013f82006-06-24 06:36:11 +0000319 /* Allocate the sqlite3_vtab/echo_vtab structure itself */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000320 pVtab = sqliteMalloc( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000321 if( !pVtab ){
322 return SQLITE_NOMEM;
323 }
danielk19779da9d472006-06-14 06:58:15 +0000324 pVtab->interp = (Tcl_Interp *)pAux;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000325 pVtab->db = db;
danielk1977cc013f82006-06-24 06:36:11 +0000326
327 /* Allocate echo_vtab.zTableName */
danielk197770ba1642006-06-21 16:02:42 +0000328 pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
danielk1977be718892006-06-23 08:05:19 +0000329 if( !pVtab->zTableName ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000330 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000331 return SQLITE_NOMEM;
332 }
333
danielk1977cc013f82006-06-24 06:36:11 +0000334 /* Log the arguments to this function to Tcl var ::echo_module */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000335 for(i=0; i<argc; i++){
336 appendToEchoModule(pVtab->interp, argv[i]);
337 }
338
danielk1977cc013f82006-06-24 06:36:11 +0000339 /* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
340 ** structure. If an error occurs, delete the sqlite3_vtab structure and
341 ** return an error code.
342 */
danielk1977a4e76362006-06-14 06:31:28 +0000343 if( echoDeclareVtab(pVtab, db, argc, argv) ){
344 echoDestructor((sqlite3_vtab *)pVtab);
345 return SQLITE_ERROR;
346 }
347
danielk1977cc013f82006-06-24 06:36:11 +0000348 /* Success. Set *ppVtab and return */
danielk1977a4e76362006-06-14 06:31:28 +0000349 *ppVtab = &pVtab->base;
350 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000351}
drha967e882006-06-13 01:04:52 +0000352
danielk1977cc013f82006-06-24 06:36:11 +0000353/*
354** Echo virtual table module xCreate method.
355*/
drhb9bb7c12006-06-11 23:41:55 +0000356static int echoCreate(
357 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000358 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000359 int argc, char **argv,
360 sqlite3_vtab **ppVtab
361){
danielk1977a298e902006-06-22 09:53:48 +0000362 int rc = SQLITE_OK;
danielk19779da9d472006-06-14 06:58:15 +0000363 appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
danielk1977a298e902006-06-22 09:53:48 +0000364 rc = echoConstructor(db, pAux, argc, argv, ppVtab);
danielk1977cc013f82006-06-24 06:36:11 +0000365
366 /* If there were two arguments passed to the module at the SQL level
367 ** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
368 ** the second argument is used as a table name. Attempt to create
369 ** such a table with a single column, "logmsg". This table will
370 ** be used to log calls to the xUpdate method. It will be deleted
371 ** when the virtual table is DROPed.
372 **
373 ** Note: The main point of this is to test that we can drop tables
374 ** from within an xDestroy method call.
375 */
danielk1977a298e902006-06-22 09:53:48 +0000376 if( rc==SQLITE_OK && argc==5 ){
377 char *zSql;
378 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
379 pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
380 zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
381 rc = sqlite3_exec(db, zSql, 0, 0, 0);
382 sqliteFree(zSql);
383 }
danielk1977cc013f82006-06-24 06:36:11 +0000384
danielk1977a298e902006-06-22 09:53:48 +0000385 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000386}
danielk1977cc013f82006-06-24 06:36:11 +0000387
388/*
389** Echo virtual table module xConnect method.
390*/
drhb9bb7c12006-06-11 23:41:55 +0000391static int echoConnect(
392 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000393 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000394 int argc, char **argv,
395 sqlite3_vtab **ppVtab
396){
danielk19779da9d472006-06-14 06:58:15 +0000397 appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
398 return echoConstructor(db, pAux, argc, argv, ppVtab);
drhb9bb7c12006-06-11 23:41:55 +0000399}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000400
danielk1977cc013f82006-06-24 06:36:11 +0000401/*
402** Echo virtual table module xDisconnect method.
403*/
danielk19775fac9f82006-06-13 14:16:58 +0000404static int echoDisconnect(sqlite3_vtab *pVtab){
405 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
406 return echoDestructor(pVtab);
407}
danielk1977cc013f82006-06-24 06:36:11 +0000408
409/*
410** Echo virtual table module xDestroy method.
411*/
danielk19775fac9f82006-06-13 14:16:58 +0000412static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000413 int rc = SQLITE_OK;
414 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000415 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977cc013f82006-06-24 06:36:11 +0000416
417 /* Drop the "log" table, if one exists (see echoCreate() for details) */
danielk1977a298e902006-06-22 09:53:48 +0000418 if( p && p->zLogName ){
419 char *zSql;
420 zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName);
421 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
422 sqliteFree(zSql);
423 }
danielk1977cc013f82006-06-24 06:36:11 +0000424
danielk1977a298e902006-06-22 09:53:48 +0000425 if( rc==SQLITE_OK ){
426 rc = echoDestructor(pVtab);
427 }
428 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000429}
430
danielk1977cc013f82006-06-24 06:36:11 +0000431/*
432** Echo virtual table module xOpen method.
433*/
danielk19775fac9f82006-06-13 14:16:58 +0000434static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000435 echo_cursor *pCur;
436 pCur = sqliteMalloc(sizeof(echo_cursor));
437 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000438 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000439}
440
danielk1977cc013f82006-06-24 06:36:11 +0000441/*
442** Echo virtual table module xClose method.
443*/
danielk19775fac9f82006-06-13 14:16:58 +0000444static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000445 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000446 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000447 sqlite3_stmt *pStmt = pCur->pStmt;
448 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000449 sqliteFree(pCur);
danielk1977be718892006-06-23 08:05:19 +0000450 rc = sqlite3_finalize(pStmt);
451 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000452}
453
danielk1977a298e902006-06-22 09:53:48 +0000454/*
455** Return non-zero if the cursor does not currently point to a valid record
456** (i.e if the scan has finished), or zero otherwise.
457*/
458static int echoEof(sqlite3_vtab_cursor *cur){
459 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
460}
461
danielk1977cc013f82006-06-24 06:36:11 +0000462/*
463** Echo virtual table module xNext method.
464*/
danielk19775fac9f82006-06-13 14:16:58 +0000465static int echoNext(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000466 int rc;
467 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000468 rc = sqlite3_step(pCur->pStmt);
469
470 if( rc==SQLITE_ROW ){
danielk1977a298e902006-06-22 09:53:48 +0000471 rc = SQLITE_OK;
472 }else{
473 rc = sqlite3_finalize(pCur->pStmt);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000474 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000475 }
476
477 return rc;
478}
479
danielk1977cc013f82006-06-24 06:36:11 +0000480/*
481** Echo virtual table module xColumn method.
482*/
danielk19775fac9f82006-06-13 14:16:58 +0000483static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000484 int iCol = i + 1;
485 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk1977fbbe0052006-06-21 07:02:33 +0000486 if( !pStmt ){
487 sqlite3_result_null(ctx);
488 }else{
489 assert( sqlite3_data_count(pStmt)>iCol );
490 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
491 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000492 return SQLITE_OK;
493}
494
danielk1977cc013f82006-06-24 06:36:11 +0000495/*
496** Echo virtual table module xRowid method.
497*/
danielk19775fac9f82006-06-13 14:16:58 +0000498static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000499 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
500 *pRowid = sqlite3_column_int64(pStmt, 0);
501 return SQLITE_OK;
502}
503
danielk197798331532006-06-14 10:55:52 +0000504/*
505** Compute a simple hash of the null terminated string zString.
506**
507** This module uses only sqlite3_index_info.idxStr, not
508** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
509** in echoBestIndex(), idxNum is set to the corresponding hash value.
510** In echoFilter(), code assert()s that the supplied idxNum value is
511** indeed the hash of the supplied idxStr.
512*/
513static int hashString(const char *zString){
514 int val = 0;
515 int ii;
516 for(ii=0; zString[ii]; ii++){
517 val = (val << 3) + (int)zString[ii];
518 }
519 return val;
520}
521
danielk1977cc013f82006-06-24 06:36:11 +0000522/*
523** Echo virtual table module xFilter method.
524*/
danielk19775fac9f82006-06-13 14:16:58 +0000525static int echoFilter(
526 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000527 int idxNum, const char *idxStr,
528 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000529){
530 int rc;
drh4be8b512006-06-13 23:51:34 +0000531 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000532
533 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
534 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
535 sqlite3 *db = pVtab->db;
536
danielk1977cc013f82006-06-24 06:36:11 +0000537 /* Check that idxNum matches idxStr */
538 assert( idxNum==hashString(idxStr) );
539
540 /* Log arguments to the ::echo_module Tcl variable */
danielk1977be718892006-06-23 08:05:19 +0000541 appendToEchoModule(pVtab->interp, "xFilter");
542 appendToEchoModule(pVtab->interp, idxStr);
543 for(i=0; i<argc; i++){
544 appendToEchoModule(pVtab->interp, sqlite3_value_text(argv[i]));
545 }
546
danielk19775fac9f82006-06-13 14:16:58 +0000547 sqlite3_finalize(pCur->pStmt);
548 pCur->pStmt = 0;
danielk1977cc013f82006-06-24 06:36:11 +0000549
550 /* Prepare the SQL statement created by echoBestIndex and bind the
551 ** runtime parameters passed to this function to it.
552 */
drh4be8b512006-06-13 23:51:34 +0000553 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000554 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000555 for(i=0; rc==SQLITE_OK && i<argc; i++){
danielk1977cc013f82006-06-24 06:36:11 +0000556 sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
drh4be8b512006-06-13 23:51:34 +0000557 }
danielk1977cc013f82006-06-24 06:36:11 +0000558
559 /* If everything was successful, advance to the first row of the scan */
danielk19775fac9f82006-06-13 14:16:58 +0000560 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000561 rc = echoNext(pVtabCursor);
danielk19775fac9f82006-06-13 14:16:58 +0000562 }
563
danielk1977be718892006-06-23 08:05:19 +0000564 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000565}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000566
danielk1977cc013f82006-06-24 06:36:11 +0000567
568/*
569** A helper function used by echoUpdate() and echoBestIndex() for
570** manipulating strings in concert with the sqlite3_mprintf() function.
571**
572** Parameter pzStr points to a pointer to a string allocated with
573** sqlite3_mprintf. The second parameter, zAppend, points to another
574** string. The two strings are concatenated together and *pzStr
575** set to point at the result. The initial buffer pointed to by *pzStr
576** is deallocated via sqlite3_free().
577**
578** If the third argument, doFree, is true, then sqlite3_free() is
579** also called to free the buffer pointed to by zAppend.
580*/
581static void string_concat(char **pzStr, char *zAppend, int doFree){
582 char *zIn = *pzStr;
583 if( zIn ){
584 char *zTemp = zIn;
585 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
586 sqlite3_free(zTemp);
587 }else{
588 zIn = sqlite3_mprintf("%s", zAppend);
589 }
590 *pzStr = zIn;
591 if( doFree ){
592 sqlite3_free(zAppend);
593 }
594}
595
drhb9bb7c12006-06-11 23:41:55 +0000596/*
danielk19775fac9f82006-06-13 14:16:58 +0000597** The echo module implements the subset of query constraints and sort
598** orders that may take advantage of SQLite indices on the underlying
599** real table. For example, if the real table is declared as:
600**
601** CREATE TABLE real(a, b, c);
602** CREATE INDEX real_index ON real(b);
603**
604** then the echo module handles WHERE or ORDER BY clauses that refer
605** to the column "b", but not "a" or "c". If a multi-column index is
606** present, only it's left most column is considered.
danielk1977cc013f82006-06-24 06:36:11 +0000607**
608** This xBestIndex method encodes the proposed search strategy as
609** an SQL query on the real table underlying the virtual echo module
610** table and stores the query in sqlite3_index_info.idxStr. The SQL
611** statement is of the form:
612**
613** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
614**
615** where the <where-clause> and <order-by-clause> are determined
616** by the contents of the structure pointed to by the pIdxInfo argument.
drha967e882006-06-13 01:04:52 +0000617*/
danielk19775fac9f82006-06-13 14:16:58 +0000618static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
619 int ii;
drh4be8b512006-06-13 23:51:34 +0000620 char *zQuery = 0;
621 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000622 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000623 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000624 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000625 sqlite3_stmt *pStmt = 0;
626
627 int nRow;
628 int useIdx = 0;
629 int rc = SQLITE_OK;
630
631 /* Determine the number of rows in the table and store this value in local
632 ** variable nRow. The 'estimated-cost' of the scan will be the number of
633 ** rows in the table for a linear scan, or the log (base 2) of the
634 ** number of rows if the proposed scan uses an index.
635 */
636 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
637 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
638 if( rc!=SQLITE_OK ){
639 return rc;
640 }
641 sqlite3_step(pStmt);
642 nRow = sqlite3_column_int(pStmt, 0);
643 rc = sqlite3_finalize(pStmt);
644 if( rc!=SQLITE_OK ){
645 return rc;
646 }
danielk19775fac9f82006-06-13 14:16:58 +0000647
drh4be8b512006-06-13 23:51:34 +0000648 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000649 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
650 const struct sqlite3_index_constraint *pConstraint;
651 struct sqlite3_index_constraint_usage *pUsage;
652
653 pConstraint = &pIdxInfo->aConstraint[ii];
654 pUsage = &pIdxInfo->aConstraintUsage[ii];
655
656 int iCol = pConstraint->iColumn;
657 if( pVtab->aIndex[iCol] ){
658 char *zCol = pVtab->aCol[iCol];
659 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000660 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000661 if( iCol<0 ){
662 zCol = "rowid";
663 }
danielk19775fac9f82006-06-13 14:16:58 +0000664 switch( pConstraint->op ){
665 case SQLITE_INDEX_CONSTRAINT_EQ:
666 zOp = "="; break;
667 case SQLITE_INDEX_CONSTRAINT_LT:
668 zOp = "<"; break;
669 case SQLITE_INDEX_CONSTRAINT_GT:
670 zOp = ">"; break;
671 case SQLITE_INDEX_CONSTRAINT_LE:
672 zOp = "<="; break;
673 case SQLITE_INDEX_CONSTRAINT_GE:
674 zOp = ">="; break;
675 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000676 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000677 }
drh1a90e092006-06-14 22:07:10 +0000678 if( zOp[0]=='L' ){
danielk1977cc013f82006-06-24 06:36:11 +0000679 zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
680 zSep, zCol);
drh1a90e092006-06-14 22:07:10 +0000681 } else {
danielk1977cc013f82006-06-24 06:36:11 +0000682 zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
drh1a90e092006-06-14 22:07:10 +0000683 }
danielk1977cc013f82006-06-24 06:36:11 +0000684 string_concat(&zQuery, zNew, 1);
685
drh4be8b512006-06-13 23:51:34 +0000686 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000687 pUsage->argvIndex = ++nArg;
688 pUsage->omit = 1;
689 }
690 }
danielk197747d08092006-06-14 07:41:31 +0000691
692 /* If there is only one term in the ORDER BY clause, and it is
693 ** on a column that this virtual table has an index for, then consume
694 ** the ORDER BY clause.
695 */
696 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
697 char *zCol = pVtab->aCol[pIdxInfo->aOrderBy->iColumn];
698 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
danielk1977cc013f82006-06-24 06:36:11 +0000699 zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
700 string_concat(&zQuery, zNew, 1);
danielk197747d08092006-06-14 07:41:31 +0000701 pIdxInfo->orderByConsumed = 1;
702 }
703
danielk19775fac9f82006-06-13 14:16:58 +0000704 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000705 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000706
danielk197798331532006-06-14 10:55:52 +0000707 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000708 pIdxInfo->idxStr = zQuery;
709 pIdxInfo->needToFreeIdxStr = 1;
danielk197774cdba42006-06-19 12:02:58 +0000710 if( useIdx ){
711 /* Approximation of log2(nRow). */
712 for( ii=0; ii<(sizeof(int)*8); ii++ ){
713 if( nRow & (1<<ii) ){
714 pIdxInfo->estimatedCost = (double)ii;
715 }
716 }
717 } else {
718 pIdxInfo->estimatedCost = (double)nRow;
719 }
720 return rc;
drha967e882006-06-13 01:04:52 +0000721}
722
danielk1977176f4d22006-06-15 10:41:15 +0000723/*
danielk1977cc013f82006-06-24 06:36:11 +0000724** The xUpdate method for echo module virtual tables.
725**
danielk1977176f4d22006-06-15 10:41:15 +0000726** apData[0] apData[1] apData[2..]
727**
728** INTEGER DELETE
729**
730** INTEGER NULL (nCol args) UPDATE (do not set rowid)
731** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
732**
733** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
734** NULL INTEGER (nCol args) INSERT (incl. rowid value)
735**
736*/
danielk19771f6eec52006-06-16 06:17:47 +0000737int echoUpdate(
738 sqlite3_vtab *tab,
739 int nData,
740 sqlite3_value **apData,
741 sqlite_int64 *pRowid
742){
danielk197726e41442006-06-14 15:16:35 +0000743 echo_vtab *pVtab = (echo_vtab *)tab;
744 sqlite3 *db = pVtab->db;
745 int rc = SQLITE_OK;
746
danielk1977176f4d22006-06-15 10:41:15 +0000747 sqlite3_stmt *pStmt;
748 char *z = 0; /* SQL statement to execute */
749 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
750 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
751 int i; /* Counter variable used by for loops */
752
danielk197726e41442006-06-14 15:16:35 +0000753 assert( nData==pVtab->nCol+2 || nData==1 );
754
drh5aec0422006-06-14 23:43:31 +0000755 /* If apData[0] is an integer and nData>1 then do an UPDATE */
756 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
danielk1977176f4d22006-06-15 10:41:15 +0000757 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
drh5aec0422006-06-14 23:43:31 +0000758 char *zSep = " SET";
danielk1977176f4d22006-06-15 10:41:15 +0000759
760 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
761 bindArgZero = 1;
762
763 if( bindArgOne ){
764 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000765 zSep = ",";
766 }
767 for(i=2; i<nData; i++){
768 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000769 string_concat(&z, sqlite3_mprintf(
770 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000771 zSep = ",";
772 }
danielk1977176f4d22006-06-15 10:41:15 +0000773 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000774 }
775
danielk1977176f4d22006-06-15 10:41:15 +0000776 /* If apData[0] is an integer and nData==1 then do a DELETE */
777 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
778 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
779 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000780 }
781
danielk1977176f4d22006-06-15 10:41:15 +0000782 /* If the first argument is NULL and there are more than two args, INSERT */
783 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000784 int ii;
785 char *zInsert = 0;
786 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000787
danielk19774b2688a2006-06-20 11:01:07 +0000788 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000789 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
790 bindArgOne = 1;
791 zValues = sqlite3_mprintf("?");
792 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000793 }
794
danielk1977176f4d22006-06-15 10:41:15 +0000795 assert((pVtab->nCol+2)==nData);
796 for(ii=2; ii<nData; ii++){
797 string_concat(&zInsert,
798 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
799 string_concat(&zValues,
800 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000801 }
802
danielk1977176f4d22006-06-15 10:41:15 +0000803 string_concat(&z, zInsert, 1);
804 string_concat(&z, ") VALUES(", 0);
805 string_concat(&z, zValues, 1);
806 string_concat(&z, ")", 0);
807 }
808
809 /* Anything else is an error */
810 else{
811 assert(0);
812 return SQLITE_ERROR;
813 }
814
815 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
816 assert( rc!=SQLITE_OK || pStmt );
817 sqlite3_free(z);
818 if( rc==SQLITE_OK ) {
819 if( bindArgZero ){
820 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000821 }
danielk1977176f4d22006-06-15 10:41:15 +0000822 if( bindArgOne ){
823 sqlite3_bind_value(pStmt, 1, apData[1]);
824 }
825 for(i=2; i<nData; i++){
826 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
827 }
828 sqlite3_step(pStmt);
829 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000830 }
831
danielk19771f6eec52006-06-16 06:17:47 +0000832 if( pRowid && rc==SQLITE_OK ){
833 *pRowid = sqlite3_last_insert_rowid(db);
834 }
835
danielk197726e41442006-06-14 15:16:35 +0000836 return rc;
837}
838
drha967e882006-06-13 01:04:52 +0000839/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000840** xBegin, xSync, xCommit and xRollback callbacks for echo module
841** virtual tables. Do nothing other than add the name of the callback
842** to the $::echo_module Tcl variable.
843*/
844static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
845 char *z;
846 echo_vtab *pVtab = (echo_vtab *)tab;
847 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
848 appendToEchoModule(pVtab->interp, zCall);
849 appendToEchoModule(pVtab->interp, z);
850 sqlite3_free(z);
851 return SQLITE_OK;
852}
853static int echoBegin(sqlite3_vtab *tab){
854 return echoTransactionCall(tab, "xBegin");
855}
856static int echoSync(sqlite3_vtab *tab){
857 echo_vtab *pVtab = (echo_vtab *)tab;
858 Tcl_Interp *interp = pVtab->interp;
859 const char *zVal;
860
861 echoTransactionCall(tab, "xSync");
862
863 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
864 ** and it is set to the name of the real table underlying this virtual
865 ** echo module table, then cause this xSync operation to fail.
866 */
867 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
868 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
869 return -1;
870 }
871 return SQLITE_OK;
872}
873static int echoCommit(sqlite3_vtab *tab){
874 return echoTransactionCall(tab, "xCommit");
875}
876static int echoRollback(sqlite3_vtab *tab){
877 return echoTransactionCall(tab, "xRollback");
878}
879
880/*
danielk1977cc013f82006-06-24 06:36:11 +0000881** A virtual table module that merely "echos" the contents of another
882** table (like an SQL VIEW).
drhb9bb7c12006-06-11 23:41:55 +0000883*/
884static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000885 0, /* iVersion */
886 "echo", /* zName */
drhb9bb7c12006-06-11 23:41:55 +0000887 echoCreate,
888 echoConnect,
drha967e882006-06-13 01:04:52 +0000889 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000890 echoDisconnect,
891 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000892 echoOpen, /* xOpen - open a cursor */
893 echoClose, /* xClose - close a cursor */
894 echoFilter, /* xFilter - configure scan constraints */
895 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +0000896 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000897 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000898 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +0000899 echoUpdate, /* xUpdate - write data */
900 echoBegin, /* xBegin - begin transaction */
901 echoSync, /* xSync - sync transaction */
902 echoCommit, /* xCommit - commit transaction */
903 echoRollback /* xRollback - rollback transaction */
drhb9bb7c12006-06-11 23:41:55 +0000904};
905
906/*
907** Decode a pointer to an sqlite3 object.
908*/
909static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
910 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
911 return TCL_OK;
912}
913
drhb9bb7c12006-06-11 23:41:55 +0000914/*
915** Register the echo virtual table module.
916*/
917static int register_echo_module(
918 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
919 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
920 int objc, /* Number of arguments */
921 Tcl_Obj *CONST objv[] /* Command arguments */
922){
923 sqlite3 *db;
924 if( objc!=2 ){
925 Tcl_WrongNumArgs(interp, 1, objv, "DB");
926 return TCL_ERROR;
927 }
928 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
danielk1977d1ab1ba2006-06-15 04:28:13 +0000929 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +0000930 return TCL_OK;
931}
932
danielk1977cc013f82006-06-24 06:36:11 +0000933#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
drhb9bb7c12006-06-11 23:41:55 +0000934
935/*
936** Register commands with the TCL interpreter.
937*/
938int Sqlitetest8_Init(Tcl_Interp *interp){
939 static struct {
940 char *zName;
941 Tcl_ObjCmdProc *xProc;
942 void *clientData;
943 } aObjCmd[] = {
danielk1977cc013f82006-06-24 06:36:11 +0000944#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9bb7c12006-06-11 23:41:55 +0000945 { "register_echo_module", register_echo_module, 0 },
danielk1977cc013f82006-06-24 06:36:11 +0000946#endif
drhb9bb7c12006-06-11 23:41:55 +0000947 };
948 int i;
949 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
950 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
951 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
952 }
953 return TCL_OK;
954}