blob: 842000d28b273791f309cfce1b17ddbeabc9501d [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**
danielk1977b7a2f2e2006-06-23 11:34:54 +000016** $Id: test8.c,v 1.33 2006/06/23 11:34:55 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
danielk1977b7a7b9a2006-06-13 10:24:42 +000024typedef struct echo_vtab echo_vtab;
25typedef struct echo_cursor echo_cursor;
26
danielk1977c69cdfd2006-06-17 09:39:55 +000027/*
28** The test module defined in this file uses two global Tcl variables to
29** commicate with test-scripts:
30**
31** $::echo_module
32** $::echo_module_sync_fail
33*/
34
danielk19775fac9f82006-06-13 14:16:58 +000035/*
danielk1977d6e8dd02006-06-15 15:38:41 +000036** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:58 +000037**
danielk1977d6e8dd02006-06-15 15:38:41 +000038** echo.vtab.aIndex is an array of booleans. The nth entry is true if
39** the nth column of the real table is the left-most column of an index
40** (implicit or otherwise). In other words, if SQLite can optimize
41** a query like "SELECT * FROM real_table WHERE col = ?".
42**
danielk1977c69cdfd2006-06-17 09:39:55 +000043** Member variable aCol[] contains copies of the column names of the real
44** table.
danielk19775fac9f82006-06-13 14:16:58 +000045*/
danielk1977b7a7b9a2006-06-13 10:24:42 +000046struct echo_vtab {
47 sqlite3_vtab base;
48 Tcl_Interp *interp;
49 sqlite3 *db;
danielk19775fac9f82006-06-13 14:16:58 +000050
danielk1977a4e76362006-06-14 06:31:28 +000051 char *zTableName; /* Name of the real table */
danielk1977a298e902006-06-22 09:53:48 +000052 char *zLogName; /* Name of the log table */
danielk1977a4e76362006-06-14 06:31:28 +000053 int nCol; /* Number of columns in the real table */
54 int *aIndex; /* Array of size nCol. True if column has an index */
55 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000056};
57
58/* An echo cursor object */
59struct echo_cursor {
60 sqlite3_vtab_cursor base;
61 sqlite3_stmt *pStmt;
62 int errcode; /* Error code */
63};
64
danielk19775fac9f82006-06-13 14:16:58 +000065static int getColumnNames(
66 sqlite3 *db,
67 const char *zTab,
68 char ***paCol,
69 int *pnCol
70){
71 char **aCol = 0;
72 char zBuf[1024];
73 sqlite3_stmt *pStmt = 0;
74 int rc = SQLITE_OK;
danielk1977be718892006-06-23 08:05:19 +000075 int nCol = 0;
danielk19775fac9f82006-06-13 14:16:58 +000076
77 sprintf(zBuf, "SELECT * FROM %s", zTab);
78 rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0);
79 if( rc==SQLITE_OK ){
80 int ii;
81 nCol = sqlite3_column_count(pStmt);
82 aCol = sqliteMalloc(sizeof(char *) * nCol);
83 if( !aCol ){
84 rc = SQLITE_NOMEM;
85 goto fail;
86 }
87 for(ii=0; ii<nCol; ii++){
88 aCol[ii] = sqlite3StrDup(sqlite3_column_name(pStmt, ii));
89 if( !aCol[ii] ){
90 rc = SQLITE_NOMEM;
91 goto fail;
92 }
93 }
94 }
95
96 *paCol = aCol;
97 *pnCol = nCol;
98
99fail:
100 sqlite3_finalize(pStmt);
101 if( rc!=SQLITE_OK && aCol ){
102 int ii;
103 for(ii=0; ii<nCol; ii++){
104 sqliteFree(aCol[ii]);
105 }
106 sqliteFree(aCol);
107 }
108 return rc;
109}
110
111static int getIndexArray(sqlite3 *db, const char *zTab, int **paIndex){
112 char zBuf[1024];
113 sqlite3_stmt *pStmt = 0;
114 int nCol;
115 int *aIndex = 0;
116 int rc;
117
118 sprintf(zBuf, "SELECT * FROM %s", zTab);
119 rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0);
120 nCol = sqlite3_column_count(pStmt);
121
122 sqlite3_finalize(pStmt);
123 pStmt = 0;
124 if( rc!=SQLITE_OK ){
125 goto get_index_array_out;
126 }
127
128 aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
129 if( !aIndex ){
130 rc = SQLITE_NOMEM;
131 goto get_index_array_out;
132 }
133
134 sprintf(zBuf, "PRAGMA index_list(%s)", zTab);
135 rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0);
136
137 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
138 sqlite3_stmt *pStmt2 = 0;
139 sprintf(zBuf, "PRAGMA index_info(%s)", sqlite3_column_text(pStmt, 1));
140 rc = sqlite3_prepare(db, zBuf, -1, &pStmt2, 0);
141 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
142 int cid = sqlite3_column_int(pStmt2, 1);
143 assert( cid>=0 && cid<nCol );
144 aIndex[cid] = 1;
145 }
danielk1977be718892006-06-23 08:05:19 +0000146 if( pStmt2 ){
147 rc = sqlite3_finalize(pStmt2);
148 }
danielk19775fac9f82006-06-13 14:16:58 +0000149 if( rc!=SQLITE_OK ){
150 sqlite3_finalize(pStmt);
151 goto get_index_array_out;
152 }
153 }
154
danielk1977be718892006-06-23 08:05:19 +0000155 if( pStmt ){
156 rc = sqlite3_finalize(pStmt);
157 }
danielk19775fac9f82006-06-13 14:16:58 +0000158
159get_index_array_out:
160 if( rc!=SQLITE_OK ){
161 sqliteFree(aIndex);
162 aIndex = 0;
163 }
164 *paIndex = aIndex;
165 return rc;
166}
167
danielk197778efaba2006-06-12 06:09:17 +0000168/*
169** Global Tcl variable $echo_module is a list. This routine appends
170** the string element zArg to that list in interpreter interp.
171*/
drha967e882006-06-13 01:04:52 +0000172static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000173 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000174 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000175}
176
danielk19777e6ebfb2006-06-12 11:24:37 +0000177/*
178** This function is called from within the echo-modules xCreate and
179** xConnect methods. The argc and argv arguments are copies of those
180** passed to the calling method. This function is responsible for
181** calling sqlite3_declare_vtab() to declare the schema of the virtual
182** table being created or connected.
183**
184** If the constructor was passed just one argument, i.e.:
185**
186** CREATE TABLE t1 AS echo(t2);
187**
188** Then t2 is assumed to be the name of a *real* database table. The
189** schema of the virtual table is declared by passing a copy of the
190** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
191** Hence, the virtual table should have exactly the same column names and
192** types as the real table.
193*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000194static int echoDeclareVtab(
195 echo_vtab *pVtab,
196 sqlite3 *db,
197 int argc,
198 char **argv
199){
danielk19777e6ebfb2006-06-12 11:24:37 +0000200 int rc = SQLITE_OK;
201
danielk1977a298e902006-06-22 09:53:48 +0000202 if( argc>=4 ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000203 sqlite3_stmt *pStmt = 0;
204 sqlite3_prepare(db,
205 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
206 -1, &pStmt, 0);
danielk197770ba1642006-06-21 16:02:42 +0000207 sqlite3_bind_text(pStmt, 1, argv[3], -1, 0);
danielk19777e6ebfb2006-06-12 11:24:37 +0000208 if( sqlite3_step(pStmt)==SQLITE_ROW ){
209 const char *zCreateTable = sqlite3_column_text(pStmt, 0);
drha75803d2006-06-12 12:50:23 +0000210#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19777e6ebfb2006-06-12 11:24:37 +0000211 sqlite3_declare_vtab(db, zCreateTable);
drha75803d2006-06-12 12:50:23 +0000212#endif
danielk1977be718892006-06-23 08:05:19 +0000213 rc = sqlite3_finalize(pStmt);
danielk19777e6ebfb2006-06-12 11:24:37 +0000214 } else {
danielk1977be718892006-06-23 08:05:19 +0000215 rc = sqlite3_finalize(pStmt);
216 if( rc==SQLITE_OK ){
217 rc = SQLITE_ERROR;
218 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000219 }
danielk1977be718892006-06-23 08:05:19 +0000220
danielk19775fac9f82006-06-13 14:16:58 +0000221 if( rc==SQLITE_OK ){
danielk197770ba1642006-06-21 16:02:42 +0000222 rc = getIndexArray(db, argv[3], &pVtab->aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000223 }
224 if( rc==SQLITE_OK ){
danielk197770ba1642006-06-21 16:02:42 +0000225 rc = getColumnNames(db, argv[3], &pVtab->aCol, &pVtab->nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000226 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000227 }
228
229 return rc;
230}
231
danielk1977a4e76362006-06-14 06:31:28 +0000232static int echoDestructor(sqlite3_vtab *pVtab){
233 int ii;
234 echo_vtab *p = (echo_vtab*)pVtab;
235 sqliteFree(p->aIndex);
236 for(ii=0; ii<p->nCol; ii++){
237 sqliteFree(p->aCol[ii]);
238 }
239 sqliteFree(p->aCol);
240 sqliteFree(p->zTableName);
241 sqliteFree(p);
242 return 0;
243}
244
danielk1977b7a7b9a2006-06-13 10:24:42 +0000245static int echoConstructor(
246 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000247 void *pAux,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000248 int argc, char **argv,
249 sqlite3_vtab **ppVtab
250){
251 int i;
252 echo_vtab *pVtab;
253
254 pVtab = sqliteMalloc( sizeof(*pVtab) );
danielk1977be718892006-06-23 08:05:19 +0000255 if( !pVtab ){
256 return SQLITE_NOMEM;
257 }
danielk19779da9d472006-06-14 06:58:15 +0000258 pVtab->interp = (Tcl_Interp *)pAux;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000259 pVtab->db = db;
danielk197770ba1642006-06-21 16:02:42 +0000260 pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
danielk1977be718892006-06-23 08:05:19 +0000261 if( !pVtab->zTableName ){
danielk1977b7a2f2e2006-06-23 11:34:54 +0000262 echoDestructor((sqlite3_vtab *)pVtab);
danielk1977be718892006-06-23 08:05:19 +0000263 return SQLITE_NOMEM;
264 }
265
danielk1977b7a7b9a2006-06-13 10:24:42 +0000266 for(i=0; i<argc; i++){
267 appendToEchoModule(pVtab->interp, argv[i]);
268 }
269
danielk1977a4e76362006-06-14 06:31:28 +0000270 if( echoDeclareVtab(pVtab, db, argc, argv) ){
271 echoDestructor((sqlite3_vtab *)pVtab);
272 return SQLITE_ERROR;
273 }
274
275 *ppVtab = &pVtab->base;
276 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000277}
drha967e882006-06-13 01:04:52 +0000278
drhb9bb7c12006-06-11 23:41:55 +0000279/* Methods for the echo module */
280static int echoCreate(
281 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000282 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000283 int argc, char **argv,
284 sqlite3_vtab **ppVtab
285){
danielk1977a298e902006-06-22 09:53:48 +0000286 int rc = SQLITE_OK;
danielk19779da9d472006-06-14 06:58:15 +0000287 appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
danielk1977a298e902006-06-22 09:53:48 +0000288 rc = echoConstructor(db, pAux, argc, argv, ppVtab);
289#if 0
290 if( rc==SQLITE_OK && argc==5 ){
291 char *zSql;
292 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
293 pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
294 zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
295 rc = sqlite3_exec(db, zSql, 0, 0, 0);
296 sqliteFree(zSql);
297 }
298#endif
299 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000300}
301static int echoConnect(
302 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000303 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000304 int argc, char **argv,
305 sqlite3_vtab **ppVtab
306){
danielk19779da9d472006-06-14 06:58:15 +0000307 appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
308 return echoConstructor(db, pAux, argc, argv, ppVtab);
drhb9bb7c12006-06-11 23:41:55 +0000309}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000310
danielk19775fac9f82006-06-13 14:16:58 +0000311static int echoDisconnect(sqlite3_vtab *pVtab){
312 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
313 return echoDestructor(pVtab);
314}
315static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000316 int rc = SQLITE_OK;
317 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000318 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977a298e902006-06-22 09:53:48 +0000319#if 0
320 if( p && p->zLogName ){
321 char *zSql;
322 zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName);
323 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
324 sqliteFree(zSql);
325 }
326#endif
327 if( rc==SQLITE_OK ){
328 rc = echoDestructor(pVtab);
329 }
330 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000331}
332
333static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000334 echo_cursor *pCur;
335 pCur = sqliteMalloc(sizeof(echo_cursor));
336 *ppCursor = (sqlite3_vtab_cursor *)pCur;
danielk1977be718892006-06-23 08:05:19 +0000337 return (pCur ? SQLITE_OK : SQLITE_NOMEM);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000338}
339
danielk19775fac9f82006-06-13 14:16:58 +0000340static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977be718892006-06-23 08:05:19 +0000341 int rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000342 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000343 sqlite3_stmt *pStmt = pCur->pStmt;
344 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000345 sqliteFree(pCur);
danielk1977be718892006-06-23 08:05:19 +0000346 rc = sqlite3_finalize(pStmt);
347 return rc;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000348}
349
danielk1977a298e902006-06-22 09:53:48 +0000350/*
351** Return non-zero if the cursor does not currently point to a valid record
352** (i.e if the scan has finished), or zero otherwise.
353*/
354static int echoEof(sqlite3_vtab_cursor *cur){
355 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
356}
357
danielk19775fac9f82006-06-13 14:16:58 +0000358static int echoNext(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000359 int rc;
360 echo_cursor *pCur = (echo_cursor *)cur;
danielk1977be718892006-06-23 08:05:19 +0000361 sqlite3_stmt *pStmt = pCur->pStmt;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000362 rc = sqlite3_step(pCur->pStmt);
363
364 if( rc==SQLITE_ROW ){
danielk1977a298e902006-06-22 09:53:48 +0000365 rc = SQLITE_OK;
366 }else{
367 rc = sqlite3_finalize(pCur->pStmt);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000368 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000369 }
370
371 return rc;
372}
373
danielk19775fac9f82006-06-13 14:16:58 +0000374static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000375 int iCol = i + 1;
376 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19774b2688a2006-06-20 11:01:07 +0000377 if( ((echo_cursor *)cur)->errcode ){
378 return ((echo_cursor *)cur)->errcode;
379 }
danielk1977fbbe0052006-06-21 07:02:33 +0000380 if( !pStmt ){
381 sqlite3_result_null(ctx);
382 }else{
383 assert( sqlite3_data_count(pStmt)>iCol );
384 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
385 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000386 return SQLITE_OK;
387}
388
danielk19775fac9f82006-06-13 14:16:58 +0000389static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000390 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
391 *pRowid = sqlite3_column_int64(pStmt, 0);
392 return SQLITE_OK;
393}
394
danielk197798331532006-06-14 10:55:52 +0000395/*
396** Compute a simple hash of the null terminated string zString.
397**
398** This module uses only sqlite3_index_info.idxStr, not
399** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
400** in echoBestIndex(), idxNum is set to the corresponding hash value.
401** In echoFilter(), code assert()s that the supplied idxNum value is
402** indeed the hash of the supplied idxStr.
403*/
404static int hashString(const char *zString){
405 int val = 0;
406 int ii;
407 for(ii=0; zString[ii]; ii++){
408 val = (val << 3) + (int)zString[ii];
409 }
410 return val;
411}
412
danielk1977b7a7b9a2006-06-13 10:24:42 +0000413
danielk19775fac9f82006-06-13 14:16:58 +0000414static int echoFilter(
415 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000416 int idxNum, const char *idxStr,
417 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000418){
419 int rc;
drh4be8b512006-06-13 23:51:34 +0000420 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000421
422 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
423 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
424 sqlite3 *db = pVtab->db;
425
danielk1977be718892006-06-23 08:05:19 +0000426 appendToEchoModule(pVtab->interp, "xFilter");
427 appendToEchoModule(pVtab->interp, idxStr);
428 for(i=0; i<argc; i++){
429 appendToEchoModule(pVtab->interp, sqlite3_value_text(argv[i]));
430 }
431
danielk197798331532006-06-14 10:55:52 +0000432 assert( idxNum==hashString(idxStr) );
danielk19775fac9f82006-06-13 14:16:58 +0000433 sqlite3_finalize(pCur->pStmt);
434 pCur->pStmt = 0;
drh4be8b512006-06-13 23:51:34 +0000435 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000436 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000437 for(i=0; rc==SQLITE_OK && i<argc; i++){
drh4be8b512006-06-13 23:51:34 +0000438 switch( sqlite3_value_type(argv[i]) ){
439 case SQLITE_INTEGER: {
440 sqlite3_bind_int64(pCur->pStmt, i+1, sqlite3_value_int64(argv[i]));
441 break;
442 }
443 case SQLITE_FLOAT: {
444 sqlite3_bind_double(pCur->pStmt, i+1, sqlite3_value_double(argv[i]));
445 break;
446 }
447 case SQLITE_NULL: {
448 sqlite3_bind_null(pCur->pStmt, i+1);
449 break;
450 }
451 case SQLITE_TEXT: {
452 sqlite3_bind_text(pCur->pStmt, i+1, sqlite3_value_text(argv[i]),
453 sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
454 break;
455 }
456 case SQLITE_BLOB: {
457 sqlite3_bind_blob(pCur->pStmt, i+1, sqlite3_value_blob(argv[i]),
458 sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
459 break;
460 }
461 }
462 }
danielk19775fac9f82006-06-13 14:16:58 +0000463 if( rc==SQLITE_OK ){
danielk1977be718892006-06-23 08:05:19 +0000464 rc = echoNext(pVtabCursor);
danielk19774b2688a2006-06-20 11:01:07 +0000465 }else{
danielk1977a298e902006-06-22 09:53:48 +0000466 assert( !pCur->pStmt );
danielk19775fac9f82006-06-13 14:16:58 +0000467 }
468
danielk1977be718892006-06-23 08:05:19 +0000469 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000470}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000471
drhb9bb7c12006-06-11 23:41:55 +0000472/*
danielk19775fac9f82006-06-13 14:16:58 +0000473** The echo module implements the subset of query constraints and sort
474** orders that may take advantage of SQLite indices on the underlying
475** real table. For example, if the real table is declared as:
476**
477** CREATE TABLE real(a, b, c);
478** CREATE INDEX real_index ON real(b);
479**
480** then the echo module handles WHERE or ORDER BY clauses that refer
481** to the column "b", but not "a" or "c". If a multi-column index is
482** present, only it's left most column is considered.
drha967e882006-06-13 01:04:52 +0000483*/
danielk19775fac9f82006-06-13 14:16:58 +0000484static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
485 int ii;
drh4be8b512006-06-13 23:51:34 +0000486 char *zQuery = 0;
487 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000488 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000489 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000490 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000491 sqlite3_stmt *pStmt = 0;
492
493 int nRow;
494 int useIdx = 0;
495 int rc = SQLITE_OK;
496
497 /* Determine the number of rows in the table and store this value in local
498 ** variable nRow. The 'estimated-cost' of the scan will be the number of
499 ** rows in the table for a linear scan, or the log (base 2) of the
500 ** number of rows if the proposed scan uses an index.
501 */
502 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
503 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
504 if( rc!=SQLITE_OK ){
505 return rc;
506 }
507 sqlite3_step(pStmt);
508 nRow = sqlite3_column_int(pStmt, 0);
509 rc = sqlite3_finalize(pStmt);
510 if( rc!=SQLITE_OK ){
511 return rc;
512 }
danielk19775fac9f82006-06-13 14:16:58 +0000513
drh4be8b512006-06-13 23:51:34 +0000514 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000515 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
516 const struct sqlite3_index_constraint *pConstraint;
517 struct sqlite3_index_constraint_usage *pUsage;
518
519 pConstraint = &pIdxInfo->aConstraint[ii];
520 pUsage = &pIdxInfo->aConstraintUsage[ii];
521
522 int iCol = pConstraint->iColumn;
523 if( pVtab->aIndex[iCol] ){
524 char *zCol = pVtab->aCol[iCol];
525 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000526 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000527 if( iCol<0 ){
528 zCol = "rowid";
529 }
danielk19775fac9f82006-06-13 14:16:58 +0000530 switch( pConstraint->op ){
531 case SQLITE_INDEX_CONSTRAINT_EQ:
532 zOp = "="; break;
533 case SQLITE_INDEX_CONSTRAINT_LT:
534 zOp = "<"; break;
535 case SQLITE_INDEX_CONSTRAINT_GT:
536 zOp = ">"; break;
537 case SQLITE_INDEX_CONSTRAINT_LE:
538 zOp = "<="; break;
539 case SQLITE_INDEX_CONSTRAINT_GE:
540 zOp = ">="; break;
541 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000542 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000543 }
drh1a90e092006-06-14 22:07:10 +0000544 if( zOp[0]=='L' ){
545 zNew = sqlite3_mprintf("%s %s %s LIKE (SELECT '%%'||?||'%%')",
546 zQuery, zSep, zCol);
547 } else {
548 zNew = sqlite3_mprintf("%s %s %s %s ?", zQuery, zSep, zCol, zOp);
549 }
drh4be8b512006-06-13 23:51:34 +0000550 sqlite3_free(zQuery);
551 zQuery = zNew;
552 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000553 pUsage->argvIndex = ++nArg;
554 pUsage->omit = 1;
555 }
556 }
danielk197747d08092006-06-14 07:41:31 +0000557
558 /* If there is only one term in the ORDER BY clause, and it is
559 ** on a column that this virtual table has an index for, then consume
560 ** the ORDER BY clause.
561 */
562 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
563 char *zCol = pVtab->aCol[pIdxInfo->aOrderBy->iColumn];
564 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
565 zNew = sqlite3_mprintf("%s ORDER BY %s %s", zQuery, zCol, zDir);
566 sqlite3_free(zQuery);
567 zQuery = zNew;
568 pIdxInfo->orderByConsumed = 1;
569 }
570
danielk19775fac9f82006-06-13 14:16:58 +0000571 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000572 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000573
danielk197798331532006-06-14 10:55:52 +0000574 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000575 pIdxInfo->idxStr = zQuery;
576 pIdxInfo->needToFreeIdxStr = 1;
danielk197774cdba42006-06-19 12:02:58 +0000577 if( useIdx ){
578 /* Approximation of log2(nRow). */
579 for( ii=0; ii<(sizeof(int)*8); ii++ ){
580 if( nRow & (1<<ii) ){
581 pIdxInfo->estimatedCost = (double)ii;
582 }
583 }
584 } else {
585 pIdxInfo->estimatedCost = (double)nRow;
586 }
587 return rc;
drha967e882006-06-13 01:04:52 +0000588}
589
danielk1977176f4d22006-06-15 10:41:15 +0000590static void string_concat(char **pzStr, char *zAppend, int doFree){
591 char *zIn = *pzStr;
592 if( zIn ){
593 char *zTemp = zIn;
594 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
595 sqlite3_free(zTemp);
596 }else{
597 zIn = sqlite3_mprintf("%s", zAppend);
598 }
599 *pzStr = zIn;
600 if( doFree ){
601 sqlite3_free(zAppend);
602 }
603}
604
605/*
606** apData[0] apData[1] apData[2..]
607**
608** INTEGER DELETE
609**
610** INTEGER NULL (nCol args) UPDATE (do not set rowid)
611** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
612**
613** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
614** NULL INTEGER (nCol args) INSERT (incl. rowid value)
615**
616*/
danielk19771f6eec52006-06-16 06:17:47 +0000617int echoUpdate(
618 sqlite3_vtab *tab,
619 int nData,
620 sqlite3_value **apData,
621 sqlite_int64 *pRowid
622){
danielk197726e41442006-06-14 15:16:35 +0000623 echo_vtab *pVtab = (echo_vtab *)tab;
624 sqlite3 *db = pVtab->db;
625 int rc = SQLITE_OK;
626
danielk1977176f4d22006-06-15 10:41:15 +0000627 sqlite3_stmt *pStmt;
628 char *z = 0; /* SQL statement to execute */
629 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
630 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
631 int i; /* Counter variable used by for loops */
632
danielk197726e41442006-06-14 15:16:35 +0000633 assert( nData==pVtab->nCol+2 || nData==1 );
634
drh5aec0422006-06-14 23:43:31 +0000635 /* If apData[0] is an integer and nData>1 then do an UPDATE */
636 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
danielk1977176f4d22006-06-15 10:41:15 +0000637 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
drh5aec0422006-06-14 23:43:31 +0000638 char *zSep = " SET";
danielk1977176f4d22006-06-15 10:41:15 +0000639
640 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
641 bindArgZero = 1;
642
643 if( bindArgOne ){
644 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000645 zSep = ",";
646 }
647 for(i=2; i<nData; i++){
648 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000649 string_concat(&z, sqlite3_mprintf(
650 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000651 zSep = ",";
652 }
danielk1977176f4d22006-06-15 10:41:15 +0000653 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000654 }
655
danielk1977176f4d22006-06-15 10:41:15 +0000656 /* If apData[0] is an integer and nData==1 then do a DELETE */
657 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
658 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
659 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000660 }
661
danielk1977176f4d22006-06-15 10:41:15 +0000662 /* If the first argument is NULL and there are more than two args, INSERT */
663 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000664 int ii;
665 char *zInsert = 0;
666 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000667
danielk19774b2688a2006-06-20 11:01:07 +0000668 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000669 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
670 bindArgOne = 1;
671 zValues = sqlite3_mprintf("?");
672 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000673 }
674
danielk1977176f4d22006-06-15 10:41:15 +0000675 assert((pVtab->nCol+2)==nData);
676 for(ii=2; ii<nData; ii++){
677 string_concat(&zInsert,
678 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
679 string_concat(&zValues,
680 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000681 }
682
danielk1977176f4d22006-06-15 10:41:15 +0000683 string_concat(&z, zInsert, 1);
684 string_concat(&z, ") VALUES(", 0);
685 string_concat(&z, zValues, 1);
686 string_concat(&z, ")", 0);
687 }
688
689 /* Anything else is an error */
690 else{
691 assert(0);
692 return SQLITE_ERROR;
693 }
694
695 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
696 assert( rc!=SQLITE_OK || pStmt );
697 sqlite3_free(z);
698 if( rc==SQLITE_OK ) {
699 if( bindArgZero ){
700 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000701 }
danielk1977176f4d22006-06-15 10:41:15 +0000702 if( bindArgOne ){
703 sqlite3_bind_value(pStmt, 1, apData[1]);
704 }
705 for(i=2; i<nData; i++){
706 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
707 }
708 sqlite3_step(pStmt);
709 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000710 }
711
danielk19771f6eec52006-06-16 06:17:47 +0000712 if( pRowid && rc==SQLITE_OK ){
713 *pRowid = sqlite3_last_insert_rowid(db);
714 }
715
danielk197726e41442006-06-14 15:16:35 +0000716 return rc;
717}
718
drha967e882006-06-13 01:04:52 +0000719/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000720** xBegin, xSync, xCommit and xRollback callbacks for echo module
721** virtual tables. Do nothing other than add the name of the callback
722** to the $::echo_module Tcl variable.
723*/
724static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
725 char *z;
726 echo_vtab *pVtab = (echo_vtab *)tab;
727 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
728 appendToEchoModule(pVtab->interp, zCall);
729 appendToEchoModule(pVtab->interp, z);
730 sqlite3_free(z);
731 return SQLITE_OK;
732}
733static int echoBegin(sqlite3_vtab *tab){
734 return echoTransactionCall(tab, "xBegin");
735}
736static int echoSync(sqlite3_vtab *tab){
737 echo_vtab *pVtab = (echo_vtab *)tab;
738 Tcl_Interp *interp = pVtab->interp;
739 const char *zVal;
740
741 echoTransactionCall(tab, "xSync");
742
743 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
744 ** and it is set to the name of the real table underlying this virtual
745 ** echo module table, then cause this xSync operation to fail.
746 */
747 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
748 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
749 return -1;
750 }
751 return SQLITE_OK;
752}
753static int echoCommit(sqlite3_vtab *tab){
754 return echoTransactionCall(tab, "xCommit");
755}
756static int echoRollback(sqlite3_vtab *tab){
757 return echoTransactionCall(tab, "xRollback");
758}
759
760/*
drhb9bb7c12006-06-11 23:41:55 +0000761** A virtual table module that merely echos method calls into TCL
762** variables.
763*/
764static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000765 0, /* iVersion */
766 "echo", /* zName */
drhb9bb7c12006-06-11 23:41:55 +0000767 echoCreate,
768 echoConnect,
drha967e882006-06-13 01:04:52 +0000769 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000770 echoDisconnect,
771 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000772 echoOpen, /* xOpen - open a cursor */
773 echoClose, /* xClose - close a cursor */
774 echoFilter, /* xFilter - configure scan constraints */
775 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +0000776 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000777 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000778 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +0000779 echoUpdate, /* xUpdate - write data */
780 echoBegin, /* xBegin - begin transaction */
781 echoSync, /* xSync - sync transaction */
782 echoCommit, /* xCommit - commit transaction */
783 echoRollback /* xRollback - rollback transaction */
drhb9bb7c12006-06-11 23:41:55 +0000784};
785
786/*
787** Decode a pointer to an sqlite3 object.
788*/
789static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
790 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
791 return TCL_OK;
792}
793
794
795/*
796** Register the echo virtual table module.
797*/
798static int register_echo_module(
799 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
800 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
801 int objc, /* Number of arguments */
802 Tcl_Obj *CONST objv[] /* Command arguments */
803){
804 sqlite3 *db;
805 if( objc!=2 ){
806 Tcl_WrongNumArgs(interp, 1, objv, "DB");
807 return TCL_ERROR;
808 }
809 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhb9bb7c12006-06-11 23:41:55 +0000810#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:13 +0000811 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +0000812#endif
813 return TCL_OK;
814}
815
816
817/*
818** Register commands with the TCL interpreter.
819*/
820int Sqlitetest8_Init(Tcl_Interp *interp){
821 static struct {
822 char *zName;
823 Tcl_ObjCmdProc *xProc;
824 void *clientData;
825 } aObjCmd[] = {
826 { "register_echo_module", register_echo_module, 0 },
827 };
828 int i;
829 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
830 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
831 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
832 }
833 return TCL_OK;
834}