blob: dae047d35a970278174cbafa1097c947fd1f3ab2 [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**
danielk1977a298e902006-06-22 09:53:48 +000016** $Id: test8.c,v 1.31 2006/06/22 09:53:50 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;
75 int nCol;
76
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 }
146 rc = sqlite3_finalize(pStmt2);
147 if( rc!=SQLITE_OK ){
148 sqlite3_finalize(pStmt);
149 goto get_index_array_out;
150 }
151 }
152
153 rc = sqlite3_finalize(pStmt);
154
155get_index_array_out:
156 if( rc!=SQLITE_OK ){
157 sqliteFree(aIndex);
158 aIndex = 0;
159 }
160 *paIndex = aIndex;
161 return rc;
162}
163
danielk197778efaba2006-06-12 06:09:17 +0000164/*
165** Global Tcl variable $echo_module is a list. This routine appends
166** the string element zArg to that list in interpreter interp.
167*/
drha967e882006-06-13 01:04:52 +0000168static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000169 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000170 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000171}
172
danielk19777e6ebfb2006-06-12 11:24:37 +0000173/*
174** This function is called from within the echo-modules xCreate and
175** xConnect methods. The argc and argv arguments are copies of those
176** passed to the calling method. This function is responsible for
177** calling sqlite3_declare_vtab() to declare the schema of the virtual
178** table being created or connected.
179**
180** If the constructor was passed just one argument, i.e.:
181**
182** CREATE TABLE t1 AS echo(t2);
183**
184** Then t2 is assumed to be the name of a *real* database table. The
185** schema of the virtual table is declared by passing a copy of the
186** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
187** Hence, the virtual table should have exactly the same column names and
188** types as the real table.
189*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000190static int echoDeclareVtab(
191 echo_vtab *pVtab,
192 sqlite3 *db,
193 int argc,
194 char **argv
195){
danielk19777e6ebfb2006-06-12 11:24:37 +0000196 int rc = SQLITE_OK;
197
danielk1977a298e902006-06-22 09:53:48 +0000198 if( argc>=4 ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000199 sqlite3_stmt *pStmt = 0;
200 sqlite3_prepare(db,
201 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
202 -1, &pStmt, 0);
danielk197770ba1642006-06-21 16:02:42 +0000203 sqlite3_bind_text(pStmt, 1, argv[3], -1, 0);
danielk19777e6ebfb2006-06-12 11:24:37 +0000204 if( sqlite3_step(pStmt)==SQLITE_ROW ){
205 const char *zCreateTable = sqlite3_column_text(pStmt, 0);
drha75803d2006-06-12 12:50:23 +0000206#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19777e6ebfb2006-06-12 11:24:37 +0000207 sqlite3_declare_vtab(db, zCreateTable);
drha75803d2006-06-12 12:50:23 +0000208#endif
danielk19777e6ebfb2006-06-12 11:24:37 +0000209 } else {
210 rc = SQLITE_ERROR;
211 }
212 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000213 if( rc==SQLITE_OK ){
danielk197770ba1642006-06-21 16:02:42 +0000214 rc = getIndexArray(db, argv[3], &pVtab->aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000215 }
216 if( rc==SQLITE_OK ){
danielk197770ba1642006-06-21 16:02:42 +0000217 rc = getColumnNames(db, argv[3], &pVtab->aCol, &pVtab->nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000218 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000219 }
220
221 return rc;
222}
223
danielk1977a4e76362006-06-14 06:31:28 +0000224static int echoDestructor(sqlite3_vtab *pVtab){
225 int ii;
226 echo_vtab *p = (echo_vtab*)pVtab;
227 sqliteFree(p->aIndex);
228 for(ii=0; ii<p->nCol; ii++){
229 sqliteFree(p->aCol[ii]);
230 }
231 sqliteFree(p->aCol);
232 sqliteFree(p->zTableName);
233 sqliteFree(p);
234 return 0;
235}
236
danielk1977b7a7b9a2006-06-13 10:24:42 +0000237static int echoConstructor(
238 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000239 void *pAux,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000240 int argc, char **argv,
241 sqlite3_vtab **ppVtab
242){
243 int i;
244 echo_vtab *pVtab;
245
246 pVtab = sqliteMalloc( sizeof(*pVtab) );
danielk19779da9d472006-06-14 06:58:15 +0000247 pVtab->interp = (Tcl_Interp *)pAux;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000248 pVtab->db = db;
danielk197770ba1642006-06-21 16:02:42 +0000249 pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000250 for(i=0; i<argc; i++){
251 appendToEchoModule(pVtab->interp, argv[i]);
252 }
253
danielk1977a4e76362006-06-14 06:31:28 +0000254 if( echoDeclareVtab(pVtab, db, argc, argv) ){
255 echoDestructor((sqlite3_vtab *)pVtab);
256 return SQLITE_ERROR;
257 }
258
259 *ppVtab = &pVtab->base;
260 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000261}
drha967e882006-06-13 01:04:52 +0000262
drhb9bb7c12006-06-11 23:41:55 +0000263/* Methods for the echo module */
264static int echoCreate(
265 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000266 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000267 int argc, char **argv,
268 sqlite3_vtab **ppVtab
269){
danielk1977a298e902006-06-22 09:53:48 +0000270 int rc = SQLITE_OK;
danielk19779da9d472006-06-14 06:58:15 +0000271 appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
danielk1977a298e902006-06-22 09:53:48 +0000272 rc = echoConstructor(db, pAux, argc, argv, ppVtab);
273#if 0
274 if( rc==SQLITE_OK && argc==5 ){
275 char *zSql;
276 echo_vtab *pVtab = *(echo_vtab **)ppVtab;
277 pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
278 zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
279 rc = sqlite3_exec(db, zSql, 0, 0, 0);
280 sqliteFree(zSql);
281 }
282#endif
283 return rc;
drhb9bb7c12006-06-11 23:41:55 +0000284}
285static int echoConnect(
286 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000287 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000288 int argc, char **argv,
289 sqlite3_vtab **ppVtab
290){
danielk19779da9d472006-06-14 06:58:15 +0000291 appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
292 return echoConstructor(db, pAux, argc, argv, ppVtab);
drhb9bb7c12006-06-11 23:41:55 +0000293}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000294
danielk19775fac9f82006-06-13 14:16:58 +0000295static int echoDisconnect(sqlite3_vtab *pVtab){
296 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
297 return echoDestructor(pVtab);
298}
299static int echoDestroy(sqlite3_vtab *pVtab){
danielk1977a298e902006-06-22 09:53:48 +0000300 int rc = SQLITE_OK;
301 echo_vtab *p = (echo_vtab *)pVtab;
danielk19775fac9f82006-06-13 14:16:58 +0000302 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
danielk1977a298e902006-06-22 09:53:48 +0000303#if 0
304 if( p && p->zLogName ){
305 char *zSql;
306 zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName);
307 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
308 sqliteFree(zSql);
309 }
310#endif
311 if( rc==SQLITE_OK ){
312 rc = echoDestructor(pVtab);
313 }
314 return rc;
danielk19775fac9f82006-06-13 14:16:58 +0000315}
316
317static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000318 echo_cursor *pCur;
319 pCur = sqliteMalloc(sizeof(echo_cursor));
320 *ppCursor = (sqlite3_vtab_cursor *)pCur;
321 return SQLITE_OK;
322}
323
danielk19775fac9f82006-06-13 14:16:58 +0000324static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000325 echo_cursor *pCur = (echo_cursor *)cur;
326 sqlite3_finalize(pCur->pStmt);
327 sqliteFree(pCur);
328 return SQLITE_OK;
329}
330
danielk1977a298e902006-06-22 09:53:48 +0000331/*
332** Return non-zero if the cursor does not currently point to a valid record
333** (i.e if the scan has finished), or zero otherwise.
334*/
335static int echoEof(sqlite3_vtab_cursor *cur){
336 return (((echo_cursor *)cur)->pStmt ? 0 : 1);
337}
338
danielk19775fac9f82006-06-13 14:16:58 +0000339static int echoNext(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000340 int rc;
341 echo_cursor *pCur = (echo_cursor *)cur;
342
343 rc = sqlite3_step(pCur->pStmt);
344
345 if( rc==SQLITE_ROW ){
danielk1977a298e902006-06-22 09:53:48 +0000346 rc = SQLITE_OK;
347 }else{
348 rc = sqlite3_finalize(pCur->pStmt);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000349 pCur->pStmt = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000350 }
351
352 return rc;
353}
354
danielk19775fac9f82006-06-13 14:16:58 +0000355static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000356 int iCol = i + 1;
357 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
danielk19774b2688a2006-06-20 11:01:07 +0000358 if( ((echo_cursor *)cur)->errcode ){
359 return ((echo_cursor *)cur)->errcode;
360 }
danielk1977fbbe0052006-06-21 07:02:33 +0000361 if( !pStmt ){
362 sqlite3_result_null(ctx);
363 }else{
364 assert( sqlite3_data_count(pStmt)>iCol );
365 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
366 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000367 return SQLITE_OK;
368}
369
danielk19775fac9f82006-06-13 14:16:58 +0000370static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000371 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
372 *pRowid = sqlite3_column_int64(pStmt, 0);
373 return SQLITE_OK;
374}
375
danielk197798331532006-06-14 10:55:52 +0000376/*
377** Compute a simple hash of the null terminated string zString.
378**
379** This module uses only sqlite3_index_info.idxStr, not
380** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
381** in echoBestIndex(), idxNum is set to the corresponding hash value.
382** In echoFilter(), code assert()s that the supplied idxNum value is
383** indeed the hash of the supplied idxStr.
384*/
385static int hashString(const char *zString){
386 int val = 0;
387 int ii;
388 for(ii=0; zString[ii]; ii++){
389 val = (val << 3) + (int)zString[ii];
390 }
391 return val;
392}
393
danielk1977b7a7b9a2006-06-13 10:24:42 +0000394
danielk19775fac9f82006-06-13 14:16:58 +0000395static int echoFilter(
396 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000397 int idxNum, const char *idxStr,
398 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000399){
400 int rc;
danielk19774b2688a2006-06-20 11:01:07 +0000401 int ret;
drh4be8b512006-06-13 23:51:34 +0000402 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000403
404 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
405 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
406 sqlite3 *db = pVtab->db;
407
danielk197798331532006-06-14 10:55:52 +0000408 assert( idxNum==hashString(idxStr) );
danielk19775fac9f82006-06-13 14:16:58 +0000409 sqlite3_finalize(pCur->pStmt);
410 pCur->pStmt = 0;
drh4be8b512006-06-13 23:51:34 +0000411 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000412 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000413 for(i=0; rc==SQLITE_OK && i<argc; i++){
drh4be8b512006-06-13 23:51:34 +0000414 switch( sqlite3_value_type(argv[i]) ){
415 case SQLITE_INTEGER: {
416 sqlite3_bind_int64(pCur->pStmt, i+1, sqlite3_value_int64(argv[i]));
417 break;
418 }
419 case SQLITE_FLOAT: {
420 sqlite3_bind_double(pCur->pStmt, i+1, sqlite3_value_double(argv[i]));
421 break;
422 }
423 case SQLITE_NULL: {
424 sqlite3_bind_null(pCur->pStmt, i+1);
425 break;
426 }
427 case SQLITE_TEXT: {
428 sqlite3_bind_text(pCur->pStmt, i+1, sqlite3_value_text(argv[i]),
429 sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
430 break;
431 }
432 case SQLITE_BLOB: {
433 sqlite3_bind_blob(pCur->pStmt, i+1, sqlite3_value_blob(argv[i]),
434 sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
435 break;
436 }
437 }
438 }
danielk19775fac9f82006-06-13 14:16:58 +0000439 if( rc==SQLITE_OK ){
danielk19774b2688a2006-06-20 11:01:07 +0000440 ret = echoNext(pVtabCursor);
441 }else{
danielk1977a298e902006-06-22 09:53:48 +0000442 assert( !pCur->pStmt );
danielk19774b2688a2006-06-20 11:01:07 +0000443 ret = 0;
444 pCur->errcode = rc;
danielk19775fac9f82006-06-13 14:16:58 +0000445 }
446
drh4be8b512006-06-13 23:51:34 +0000447 appendToEchoModule(pVtab->interp, "xFilter");
448 appendToEchoModule(pVtab->interp, idxStr);
449 for(i=0; i<argc; i++){
450 appendToEchoModule(pVtab->interp, sqlite3_value_text(argv[i]));
451 }
452
danielk19774b2688a2006-06-20 11:01:07 +0000453 return ret;
danielk19775fac9f82006-06-13 14:16:58 +0000454}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000455
drhb9bb7c12006-06-11 23:41:55 +0000456/*
danielk19775fac9f82006-06-13 14:16:58 +0000457** The echo module implements the subset of query constraints and sort
458** orders that may take advantage of SQLite indices on the underlying
459** real table. For example, if the real table is declared as:
460**
461** CREATE TABLE real(a, b, c);
462** CREATE INDEX real_index ON real(b);
463**
464** then the echo module handles WHERE or ORDER BY clauses that refer
465** to the column "b", but not "a" or "c". If a multi-column index is
466** present, only it's left most column is considered.
drha967e882006-06-13 01:04:52 +0000467*/
danielk19775fac9f82006-06-13 14:16:58 +0000468static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
469 int ii;
drh4be8b512006-06-13 23:51:34 +0000470 char *zQuery = 0;
471 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000472 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000473 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000474 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000475 sqlite3_stmt *pStmt = 0;
476
477 int nRow;
478 int useIdx = 0;
479 int rc = SQLITE_OK;
480
481 /* Determine the number of rows in the table and store this value in local
482 ** variable nRow. The 'estimated-cost' of the scan will be the number of
483 ** rows in the table for a linear scan, or the log (base 2) of the
484 ** number of rows if the proposed scan uses an index.
485 */
486 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
487 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
488 if( rc!=SQLITE_OK ){
489 return rc;
490 }
491 sqlite3_step(pStmt);
492 nRow = sqlite3_column_int(pStmt, 0);
493 rc = sqlite3_finalize(pStmt);
494 if( rc!=SQLITE_OK ){
495 return rc;
496 }
danielk19775fac9f82006-06-13 14:16:58 +0000497
drh4be8b512006-06-13 23:51:34 +0000498 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000499 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
500 const struct sqlite3_index_constraint *pConstraint;
501 struct sqlite3_index_constraint_usage *pUsage;
502
503 pConstraint = &pIdxInfo->aConstraint[ii];
504 pUsage = &pIdxInfo->aConstraintUsage[ii];
505
506 int iCol = pConstraint->iColumn;
507 if( pVtab->aIndex[iCol] ){
508 char *zCol = pVtab->aCol[iCol];
509 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000510 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000511 if( iCol<0 ){
512 zCol = "rowid";
513 }
danielk19775fac9f82006-06-13 14:16:58 +0000514 switch( pConstraint->op ){
515 case SQLITE_INDEX_CONSTRAINT_EQ:
516 zOp = "="; break;
517 case SQLITE_INDEX_CONSTRAINT_LT:
518 zOp = "<"; break;
519 case SQLITE_INDEX_CONSTRAINT_GT:
520 zOp = ">"; break;
521 case SQLITE_INDEX_CONSTRAINT_LE:
522 zOp = "<="; break;
523 case SQLITE_INDEX_CONSTRAINT_GE:
524 zOp = ">="; break;
525 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000526 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000527 }
drh1a90e092006-06-14 22:07:10 +0000528 if( zOp[0]=='L' ){
529 zNew = sqlite3_mprintf("%s %s %s LIKE (SELECT '%%'||?||'%%')",
530 zQuery, zSep, zCol);
531 } else {
532 zNew = sqlite3_mprintf("%s %s %s %s ?", zQuery, zSep, zCol, zOp);
533 }
drh4be8b512006-06-13 23:51:34 +0000534 sqlite3_free(zQuery);
535 zQuery = zNew;
536 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000537 pUsage->argvIndex = ++nArg;
538 pUsage->omit = 1;
539 }
540 }
danielk197747d08092006-06-14 07:41:31 +0000541
542 /* If there is only one term in the ORDER BY clause, and it is
543 ** on a column that this virtual table has an index for, then consume
544 ** the ORDER BY clause.
545 */
546 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
547 char *zCol = pVtab->aCol[pIdxInfo->aOrderBy->iColumn];
548 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
549 zNew = sqlite3_mprintf("%s ORDER BY %s %s", zQuery, zCol, zDir);
550 sqlite3_free(zQuery);
551 zQuery = zNew;
552 pIdxInfo->orderByConsumed = 1;
553 }
554
danielk19775fac9f82006-06-13 14:16:58 +0000555 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000556 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000557
danielk197798331532006-06-14 10:55:52 +0000558 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000559 pIdxInfo->idxStr = zQuery;
560 pIdxInfo->needToFreeIdxStr = 1;
danielk197774cdba42006-06-19 12:02:58 +0000561 if( useIdx ){
562 /* Approximation of log2(nRow). */
563 for( ii=0; ii<(sizeof(int)*8); ii++ ){
564 if( nRow & (1<<ii) ){
565 pIdxInfo->estimatedCost = (double)ii;
566 }
567 }
568 } else {
569 pIdxInfo->estimatedCost = (double)nRow;
570 }
571 return rc;
drha967e882006-06-13 01:04:52 +0000572}
573
danielk1977176f4d22006-06-15 10:41:15 +0000574static void string_concat(char **pzStr, char *zAppend, int doFree){
575 char *zIn = *pzStr;
576 if( zIn ){
577 char *zTemp = zIn;
578 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
579 sqlite3_free(zTemp);
580 }else{
581 zIn = sqlite3_mprintf("%s", zAppend);
582 }
583 *pzStr = zIn;
584 if( doFree ){
585 sqlite3_free(zAppend);
586 }
587}
588
589/*
590** apData[0] apData[1] apData[2..]
591**
592** INTEGER DELETE
593**
594** INTEGER NULL (nCol args) UPDATE (do not set rowid)
595** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
596**
597** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
598** NULL INTEGER (nCol args) INSERT (incl. rowid value)
599**
600*/
danielk19771f6eec52006-06-16 06:17:47 +0000601int echoUpdate(
602 sqlite3_vtab *tab,
603 int nData,
604 sqlite3_value **apData,
605 sqlite_int64 *pRowid
606){
danielk197726e41442006-06-14 15:16:35 +0000607 echo_vtab *pVtab = (echo_vtab *)tab;
608 sqlite3 *db = pVtab->db;
609 int rc = SQLITE_OK;
610
danielk1977176f4d22006-06-15 10:41:15 +0000611 sqlite3_stmt *pStmt;
612 char *z = 0; /* SQL statement to execute */
613 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
614 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
615 int i; /* Counter variable used by for loops */
616
danielk197726e41442006-06-14 15:16:35 +0000617 assert( nData==pVtab->nCol+2 || nData==1 );
618
drh5aec0422006-06-14 23:43:31 +0000619 /* If apData[0] is an integer and nData>1 then do an UPDATE */
620 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
danielk1977176f4d22006-06-15 10:41:15 +0000621 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
drh5aec0422006-06-14 23:43:31 +0000622 char *zSep = " SET";
danielk1977176f4d22006-06-15 10:41:15 +0000623
624 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
625 bindArgZero = 1;
626
627 if( bindArgOne ){
628 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000629 zSep = ",";
630 }
631 for(i=2; i<nData; i++){
632 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000633 string_concat(&z, sqlite3_mprintf(
634 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000635 zSep = ",";
636 }
danielk1977176f4d22006-06-15 10:41:15 +0000637 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000638 }
639
danielk1977176f4d22006-06-15 10:41:15 +0000640 /* If apData[0] is an integer and nData==1 then do a DELETE */
641 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
642 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
643 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000644 }
645
danielk1977176f4d22006-06-15 10:41:15 +0000646 /* If the first argument is NULL and there are more than two args, INSERT */
647 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000648 int ii;
649 char *zInsert = 0;
650 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000651
danielk19774b2688a2006-06-20 11:01:07 +0000652 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000653 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
654 bindArgOne = 1;
655 zValues = sqlite3_mprintf("?");
656 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000657 }
658
danielk1977176f4d22006-06-15 10:41:15 +0000659 assert((pVtab->nCol+2)==nData);
660 for(ii=2; ii<nData; ii++){
661 string_concat(&zInsert,
662 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
663 string_concat(&zValues,
664 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000665 }
666
danielk1977176f4d22006-06-15 10:41:15 +0000667 string_concat(&z, zInsert, 1);
668 string_concat(&z, ") VALUES(", 0);
669 string_concat(&z, zValues, 1);
670 string_concat(&z, ")", 0);
671 }
672
673 /* Anything else is an error */
674 else{
675 assert(0);
676 return SQLITE_ERROR;
677 }
678
679 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
680 assert( rc!=SQLITE_OK || pStmt );
681 sqlite3_free(z);
682 if( rc==SQLITE_OK ) {
683 if( bindArgZero ){
684 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000685 }
danielk1977176f4d22006-06-15 10:41:15 +0000686 if( bindArgOne ){
687 sqlite3_bind_value(pStmt, 1, apData[1]);
688 }
689 for(i=2; i<nData; i++){
690 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
691 }
692 sqlite3_step(pStmt);
693 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000694 }
695
danielk19771f6eec52006-06-16 06:17:47 +0000696 if( pRowid && rc==SQLITE_OK ){
697 *pRowid = sqlite3_last_insert_rowid(db);
698 }
699
danielk197726e41442006-06-14 15:16:35 +0000700 return rc;
701}
702
drha967e882006-06-13 01:04:52 +0000703/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000704** xBegin, xSync, xCommit and xRollback callbacks for echo module
705** virtual tables. Do nothing other than add the name of the callback
706** to the $::echo_module Tcl variable.
707*/
708static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
709 char *z;
710 echo_vtab *pVtab = (echo_vtab *)tab;
711 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
712 appendToEchoModule(pVtab->interp, zCall);
713 appendToEchoModule(pVtab->interp, z);
714 sqlite3_free(z);
715 return SQLITE_OK;
716}
717static int echoBegin(sqlite3_vtab *tab){
718 return echoTransactionCall(tab, "xBegin");
719}
720static int echoSync(sqlite3_vtab *tab){
721 echo_vtab *pVtab = (echo_vtab *)tab;
722 Tcl_Interp *interp = pVtab->interp;
723 const char *zVal;
724
725 echoTransactionCall(tab, "xSync");
726
727 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
728 ** and it is set to the name of the real table underlying this virtual
729 ** echo module table, then cause this xSync operation to fail.
730 */
731 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
732 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
733 return -1;
734 }
735 return SQLITE_OK;
736}
737static int echoCommit(sqlite3_vtab *tab){
738 return echoTransactionCall(tab, "xCommit");
739}
740static int echoRollback(sqlite3_vtab *tab){
741 return echoTransactionCall(tab, "xRollback");
742}
743
744/*
drhb9bb7c12006-06-11 23:41:55 +0000745** A virtual table module that merely echos method calls into TCL
746** variables.
747*/
748static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000749 0, /* iVersion */
750 "echo", /* zName */
drhb9bb7c12006-06-11 23:41:55 +0000751 echoCreate,
752 echoConnect,
drha967e882006-06-13 01:04:52 +0000753 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000754 echoDisconnect,
755 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000756 echoOpen, /* xOpen - open a cursor */
757 echoClose, /* xClose - close a cursor */
758 echoFilter, /* xFilter - configure scan constraints */
759 echoNext, /* xNext - advance a cursor */
danielk1977a298e902006-06-22 09:53:48 +0000760 echoEof, /* xEof */
danielk1977b7a7b9a2006-06-13 10:24:42 +0000761 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000762 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +0000763 echoUpdate, /* xUpdate - write data */
764 echoBegin, /* xBegin - begin transaction */
765 echoSync, /* xSync - sync transaction */
766 echoCommit, /* xCommit - commit transaction */
767 echoRollback /* xRollback - rollback transaction */
drhb9bb7c12006-06-11 23:41:55 +0000768};
769
770/*
771** Decode a pointer to an sqlite3 object.
772*/
773static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
774 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
775 return TCL_OK;
776}
777
778
779/*
780** Register the echo virtual table module.
781*/
782static int register_echo_module(
783 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
784 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
785 int objc, /* Number of arguments */
786 Tcl_Obj *CONST objv[] /* Command arguments */
787){
788 sqlite3 *db;
789 if( objc!=2 ){
790 Tcl_WrongNumArgs(interp, 1, objv, "DB");
791 return TCL_ERROR;
792 }
793 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhb9bb7c12006-06-11 23:41:55 +0000794#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:13 +0000795 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +0000796#endif
797 return TCL_OK;
798}
799
800
801/*
802** Register commands with the TCL interpreter.
803*/
804int Sqlitetest8_Init(Tcl_Interp *interp){
805 static struct {
806 char *zName;
807 Tcl_ObjCmdProc *xProc;
808 void *clientData;
809 } aObjCmd[] = {
810 { "register_echo_module", register_echo_module, 0 },
811 };
812 int i;
813 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
814 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
815 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
816 }
817 return TCL_OK;
818}