blob: 619d9090d175cfa10c7bdf015ec63485e25d9d7f [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**
danielk197774cdba42006-06-19 12:02:58 +000016** $Id: test8.c,v 1.26 2006/06/19 12:02:59 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 */
52 int nCol; /* Number of columns in the real table */
53 int *aIndex; /* Array of size nCol. True if column has an index */
54 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000055};
56
57/* An echo cursor object */
58struct echo_cursor {
59 sqlite3_vtab_cursor base;
60 sqlite3_stmt *pStmt;
61 int errcode; /* Error code */
62};
63
danielk19775fac9f82006-06-13 14:16:58 +000064static int getColumnNames(
65 sqlite3 *db,
66 const char *zTab,
67 char ***paCol,
68 int *pnCol
69){
70 char **aCol = 0;
71 char zBuf[1024];
72 sqlite3_stmt *pStmt = 0;
73 int rc = SQLITE_OK;
74 int nCol;
75
76 sprintf(zBuf, "SELECT * FROM %s", zTab);
77 rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0);
78 if( rc==SQLITE_OK ){
79 int ii;
80 nCol = sqlite3_column_count(pStmt);
81 aCol = sqliteMalloc(sizeof(char *) * nCol);
82 if( !aCol ){
83 rc = SQLITE_NOMEM;
84 goto fail;
85 }
86 for(ii=0; ii<nCol; ii++){
87 aCol[ii] = sqlite3StrDup(sqlite3_column_name(pStmt, ii));
88 if( !aCol[ii] ){
89 rc = SQLITE_NOMEM;
90 goto fail;
91 }
92 }
93 }
94
95 *paCol = aCol;
96 *pnCol = nCol;
97
98fail:
99 sqlite3_finalize(pStmt);
100 if( rc!=SQLITE_OK && aCol ){
101 int ii;
102 for(ii=0; ii<nCol; ii++){
103 sqliteFree(aCol[ii]);
104 }
105 sqliteFree(aCol);
106 }
107 return rc;
108}
109
110static int getIndexArray(sqlite3 *db, const char *zTab, int **paIndex){
111 char zBuf[1024];
112 sqlite3_stmt *pStmt = 0;
113 int nCol;
114 int *aIndex = 0;
115 int rc;
116
117 sprintf(zBuf, "SELECT * FROM %s", zTab);
118 rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0);
119 nCol = sqlite3_column_count(pStmt);
120
121 sqlite3_finalize(pStmt);
122 pStmt = 0;
123 if( rc!=SQLITE_OK ){
124 goto get_index_array_out;
125 }
126
127 aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
128 if( !aIndex ){
129 rc = SQLITE_NOMEM;
130 goto get_index_array_out;
131 }
132
133 sprintf(zBuf, "PRAGMA index_list(%s)", zTab);
134 rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0);
135
136 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
137 sqlite3_stmt *pStmt2 = 0;
138 sprintf(zBuf, "PRAGMA index_info(%s)", sqlite3_column_text(pStmt, 1));
139 rc = sqlite3_prepare(db, zBuf, -1, &pStmt2, 0);
140 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
141 int cid = sqlite3_column_int(pStmt2, 1);
142 assert( cid>=0 && cid<nCol );
143 aIndex[cid] = 1;
144 }
145 rc = sqlite3_finalize(pStmt2);
146 if( rc!=SQLITE_OK ){
147 sqlite3_finalize(pStmt);
148 goto get_index_array_out;
149 }
150 }
151
152 rc = sqlite3_finalize(pStmt);
153
154get_index_array_out:
155 if( rc!=SQLITE_OK ){
156 sqliteFree(aIndex);
157 aIndex = 0;
158 }
159 *paIndex = aIndex;
160 return rc;
161}
162
danielk197778efaba2006-06-12 06:09:17 +0000163/*
164** Global Tcl variable $echo_module is a list. This routine appends
165** the string element zArg to that list in interpreter interp.
166*/
drha967e882006-06-13 01:04:52 +0000167static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000168 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000169 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000170}
171
danielk19777e6ebfb2006-06-12 11:24:37 +0000172/*
173** This function is called from within the echo-modules xCreate and
174** xConnect methods. The argc and argv arguments are copies of those
175** passed to the calling method. This function is responsible for
176** calling sqlite3_declare_vtab() to declare the schema of the virtual
177** table being created or connected.
178**
179** If the constructor was passed just one argument, i.e.:
180**
181** CREATE TABLE t1 AS echo(t2);
182**
183** Then t2 is assumed to be the name of a *real* database table. The
184** schema of the virtual table is declared by passing a copy of the
185** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
186** Hence, the virtual table should have exactly the same column names and
187** types as the real table.
188*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000189static int echoDeclareVtab(
190 echo_vtab *pVtab,
191 sqlite3 *db,
192 int argc,
193 char **argv
194){
danielk19777e6ebfb2006-06-12 11:24:37 +0000195 int rc = SQLITE_OK;
196
197 if( argc==2 ){
198 sqlite3_stmt *pStmt = 0;
199 sqlite3_prepare(db,
200 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
201 -1, &pStmt, 0);
202 sqlite3_bind_text(pStmt, 1, argv[1], -1, 0);
203 if( sqlite3_step(pStmt)==SQLITE_ROW ){
204 const char *zCreateTable = sqlite3_column_text(pStmt, 0);
drha75803d2006-06-12 12:50:23 +0000205#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19777e6ebfb2006-06-12 11:24:37 +0000206 sqlite3_declare_vtab(db, zCreateTable);
drha75803d2006-06-12 12:50:23 +0000207#endif
danielk19777e6ebfb2006-06-12 11:24:37 +0000208 } else {
209 rc = SQLITE_ERROR;
210 }
211 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000212 if( rc==SQLITE_OK ){
213 rc = getIndexArray(db, argv[1], &pVtab->aIndex);
214 }
215 if( rc==SQLITE_OK ){
216 rc = getColumnNames(db, argv[1], &pVtab->aCol, &pVtab->nCol);
217 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000218 }
219
220 return rc;
221}
222
danielk1977a4e76362006-06-14 06:31:28 +0000223static int echoDestructor(sqlite3_vtab *pVtab){
224 int ii;
225 echo_vtab *p = (echo_vtab*)pVtab;
226 sqliteFree(p->aIndex);
227 for(ii=0; ii<p->nCol; ii++){
228 sqliteFree(p->aCol[ii]);
229 }
230 sqliteFree(p->aCol);
231 sqliteFree(p->zTableName);
232 sqliteFree(p);
233 return 0;
234}
235
danielk1977b7a7b9a2006-06-13 10:24:42 +0000236static int echoConstructor(
237 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000238 void *pAux,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000239 int argc, char **argv,
240 sqlite3_vtab **ppVtab
241){
242 int i;
243 echo_vtab *pVtab;
244
245 pVtab = sqliteMalloc( sizeof(*pVtab) );
danielk19779da9d472006-06-14 06:58:15 +0000246 pVtab->interp = (Tcl_Interp *)pAux;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000247 pVtab->db = db;
drh4be8b512006-06-13 23:51:34 +0000248 pVtab->zTableName = sqlite3MPrintf("%s", argv[1]);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000249 for(i=0; i<argc; i++){
250 appendToEchoModule(pVtab->interp, argv[i]);
251 }
252
danielk1977a4e76362006-06-14 06:31:28 +0000253 if( echoDeclareVtab(pVtab, db, argc, argv) ){
254 echoDestructor((sqlite3_vtab *)pVtab);
255 return SQLITE_ERROR;
256 }
257
258 *ppVtab = &pVtab->base;
259 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000260}
drha967e882006-06-13 01:04:52 +0000261
drhb9bb7c12006-06-11 23:41:55 +0000262/* Methods for the echo module */
263static int echoCreate(
264 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000265 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000266 int argc, char **argv,
267 sqlite3_vtab **ppVtab
268){
danielk19779da9d472006-06-14 06:58:15 +0000269 appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
270 return echoConstructor(db, pAux, argc, argv, ppVtab);
drhb9bb7c12006-06-11 23:41:55 +0000271}
272static int echoConnect(
273 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000274 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000275 int argc, char **argv,
276 sqlite3_vtab **ppVtab
277){
danielk19779da9d472006-06-14 06:58:15 +0000278 appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
279 return echoConstructor(db, pAux, argc, argv, ppVtab);
drhb9bb7c12006-06-11 23:41:55 +0000280}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000281
danielk19775fac9f82006-06-13 14:16:58 +0000282static int echoDisconnect(sqlite3_vtab *pVtab){
283 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
284 return echoDestructor(pVtab);
285}
286static int echoDestroy(sqlite3_vtab *pVtab){
287 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
288 return echoDestructor(pVtab);
289}
290
291static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000292 echo_cursor *pCur;
293 pCur = sqliteMalloc(sizeof(echo_cursor));
294 *ppCursor = (sqlite3_vtab_cursor *)pCur;
295 return SQLITE_OK;
296}
297
danielk19775fac9f82006-06-13 14:16:58 +0000298static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000299 echo_cursor *pCur = (echo_cursor *)cur;
300 sqlite3_finalize(pCur->pStmt);
301 sqliteFree(pCur);
302 return SQLITE_OK;
303}
304
danielk19775fac9f82006-06-13 14:16:58 +0000305static int echoNext(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000306 int rc;
307 echo_cursor *pCur = (echo_cursor *)cur;
308
309 rc = sqlite3_step(pCur->pStmt);
310
311 if( rc==SQLITE_ROW ){
312 rc = 1;
313 } else {
314 pCur->errcode = sqlite3_finalize(pCur->pStmt);
315 pCur->pStmt = 0;
316 rc = 0;
317 }
318
319 return rc;
320}
321
danielk19775fac9f82006-06-13 14:16:58 +0000322static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000323 int iCol = i + 1;
324 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
325
326 assert( sqlite3_data_count(pStmt)>iCol );
drh4be8b512006-06-13 23:51:34 +0000327 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000328 return SQLITE_OK;
329}
330
danielk19775fac9f82006-06-13 14:16:58 +0000331static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000332 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
333 *pRowid = sqlite3_column_int64(pStmt, 0);
334 return SQLITE_OK;
335}
336
danielk197798331532006-06-14 10:55:52 +0000337/*
338** Compute a simple hash of the null terminated string zString.
339**
340** This module uses only sqlite3_index_info.idxStr, not
341** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
342** in echoBestIndex(), idxNum is set to the corresponding hash value.
343** In echoFilter(), code assert()s that the supplied idxNum value is
344** indeed the hash of the supplied idxStr.
345*/
346static int hashString(const char *zString){
347 int val = 0;
348 int ii;
349 for(ii=0; zString[ii]; ii++){
350 val = (val << 3) + (int)zString[ii];
351 }
352 return val;
353}
354
danielk1977b7a7b9a2006-06-13 10:24:42 +0000355
danielk19775fac9f82006-06-13 14:16:58 +0000356static int echoFilter(
357 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000358 int idxNum, const char *idxStr,
359 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000360){
361 int rc;
drh4be8b512006-06-13 23:51:34 +0000362 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000363
364 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
365 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
366 sqlite3 *db = pVtab->db;
367
danielk197798331532006-06-14 10:55:52 +0000368 assert( idxNum==hashString(idxStr) );
danielk19775fac9f82006-06-13 14:16:58 +0000369 sqlite3_finalize(pCur->pStmt);
370 pCur->pStmt = 0;
drh4be8b512006-06-13 23:51:34 +0000371 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
372 for(i=0; i<argc; i++){
373 switch( sqlite3_value_type(argv[i]) ){
374 case SQLITE_INTEGER: {
375 sqlite3_bind_int64(pCur->pStmt, i+1, sqlite3_value_int64(argv[i]));
376 break;
377 }
378 case SQLITE_FLOAT: {
379 sqlite3_bind_double(pCur->pStmt, i+1, sqlite3_value_double(argv[i]));
380 break;
381 }
382 case SQLITE_NULL: {
383 sqlite3_bind_null(pCur->pStmt, i+1);
384 break;
385 }
386 case SQLITE_TEXT: {
387 sqlite3_bind_text(pCur->pStmt, i+1, sqlite3_value_text(argv[i]),
388 sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
389 break;
390 }
391 case SQLITE_BLOB: {
392 sqlite3_bind_blob(pCur->pStmt, i+1, sqlite3_value_blob(argv[i]),
393 sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
394 break;
395 }
396 }
397 }
danielk19775fac9f82006-06-13 14:16:58 +0000398 if( rc==SQLITE_OK ){
399 rc = echoNext(pVtabCursor);
400 }
401
drh4be8b512006-06-13 23:51:34 +0000402 appendToEchoModule(pVtab->interp, "xFilter");
403 appendToEchoModule(pVtab->interp, idxStr);
404 for(i=0; i<argc; i++){
405 appendToEchoModule(pVtab->interp, sqlite3_value_text(argv[i]));
406 }
407
danielk19775fac9f82006-06-13 14:16:58 +0000408 return rc;
409}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000410
drhb9bb7c12006-06-11 23:41:55 +0000411/*
danielk19775fac9f82006-06-13 14:16:58 +0000412** The echo module implements the subset of query constraints and sort
413** orders that may take advantage of SQLite indices on the underlying
414** real table. For example, if the real table is declared as:
415**
416** CREATE TABLE real(a, b, c);
417** CREATE INDEX real_index ON real(b);
418**
419** then the echo module handles WHERE or ORDER BY clauses that refer
420** to the column "b", but not "a" or "c". If a multi-column index is
421** present, only it's left most column is considered.
drha967e882006-06-13 01:04:52 +0000422*/
danielk19775fac9f82006-06-13 14:16:58 +0000423static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
424 int ii;
drh4be8b512006-06-13 23:51:34 +0000425 char *zQuery = 0;
426 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000427 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000428 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000429 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000430 sqlite3_stmt *pStmt = 0;
431
432 int nRow;
433 int useIdx = 0;
434 int rc = SQLITE_OK;
435
436 /* Determine the number of rows in the table and store this value in local
437 ** variable nRow. The 'estimated-cost' of the scan will be the number of
438 ** rows in the table for a linear scan, or the log (base 2) of the
439 ** number of rows if the proposed scan uses an index.
440 */
441 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
442 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
443 if( rc!=SQLITE_OK ){
444 return rc;
445 }
446 sqlite3_step(pStmt);
447 nRow = sqlite3_column_int(pStmt, 0);
448 rc = sqlite3_finalize(pStmt);
449 if( rc!=SQLITE_OK ){
450 return rc;
451 }
danielk19775fac9f82006-06-13 14:16:58 +0000452
drh4be8b512006-06-13 23:51:34 +0000453 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000454 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
455 const struct sqlite3_index_constraint *pConstraint;
456 struct sqlite3_index_constraint_usage *pUsage;
457
458 pConstraint = &pIdxInfo->aConstraint[ii];
459 pUsage = &pIdxInfo->aConstraintUsage[ii];
460
461 int iCol = pConstraint->iColumn;
462 if( pVtab->aIndex[iCol] ){
463 char *zCol = pVtab->aCol[iCol];
464 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000465 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000466 if( iCol<0 ){
467 zCol = "rowid";
468 }
danielk19775fac9f82006-06-13 14:16:58 +0000469 switch( pConstraint->op ){
470 case SQLITE_INDEX_CONSTRAINT_EQ:
471 zOp = "="; break;
472 case SQLITE_INDEX_CONSTRAINT_LT:
473 zOp = "<"; break;
474 case SQLITE_INDEX_CONSTRAINT_GT:
475 zOp = ">"; break;
476 case SQLITE_INDEX_CONSTRAINT_LE:
477 zOp = "<="; break;
478 case SQLITE_INDEX_CONSTRAINT_GE:
479 zOp = ">="; break;
480 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000481 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000482 }
drh1a90e092006-06-14 22:07:10 +0000483 if( zOp[0]=='L' ){
484 zNew = sqlite3_mprintf("%s %s %s LIKE (SELECT '%%'||?||'%%')",
485 zQuery, zSep, zCol);
486 } else {
487 zNew = sqlite3_mprintf("%s %s %s %s ?", zQuery, zSep, zCol, zOp);
488 }
drh4be8b512006-06-13 23:51:34 +0000489 sqlite3_free(zQuery);
490 zQuery = zNew;
491 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000492 pUsage->argvIndex = ++nArg;
493 pUsage->omit = 1;
494 }
495 }
danielk197747d08092006-06-14 07:41:31 +0000496
497 /* If there is only one term in the ORDER BY clause, and it is
498 ** on a column that this virtual table has an index for, then consume
499 ** the ORDER BY clause.
500 */
501 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
502 char *zCol = pVtab->aCol[pIdxInfo->aOrderBy->iColumn];
503 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
504 zNew = sqlite3_mprintf("%s ORDER BY %s %s", zQuery, zCol, zDir);
505 sqlite3_free(zQuery);
506 zQuery = zNew;
507 pIdxInfo->orderByConsumed = 1;
508 }
509
danielk19775fac9f82006-06-13 14:16:58 +0000510 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000511 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000512
danielk197798331532006-06-14 10:55:52 +0000513 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000514 pIdxInfo->idxStr = zQuery;
515 pIdxInfo->needToFreeIdxStr = 1;
danielk197774cdba42006-06-19 12:02:58 +0000516 if( useIdx ){
517 /* Approximation of log2(nRow). */
518 for( ii=0; ii<(sizeof(int)*8); ii++ ){
519 if( nRow & (1<<ii) ){
520 pIdxInfo->estimatedCost = (double)ii;
521 }
522 }
523 } else {
524 pIdxInfo->estimatedCost = (double)nRow;
525 }
526 return rc;
drha967e882006-06-13 01:04:52 +0000527}
528
danielk1977176f4d22006-06-15 10:41:15 +0000529static void string_concat(char **pzStr, char *zAppend, int doFree){
530 char *zIn = *pzStr;
531 if( zIn ){
532 char *zTemp = zIn;
533 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
534 sqlite3_free(zTemp);
535 }else{
536 zIn = sqlite3_mprintf("%s", zAppend);
537 }
538 *pzStr = zIn;
539 if( doFree ){
540 sqlite3_free(zAppend);
541 }
542}
543
544/*
545** apData[0] apData[1] apData[2..]
546**
547** INTEGER DELETE
548**
549** INTEGER NULL (nCol args) UPDATE (do not set rowid)
550** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
551**
552** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
553** NULL INTEGER (nCol args) INSERT (incl. rowid value)
554**
555*/
danielk19771f6eec52006-06-16 06:17:47 +0000556int echoUpdate(
557 sqlite3_vtab *tab,
558 int nData,
559 sqlite3_value **apData,
560 sqlite_int64 *pRowid
561){
danielk197726e41442006-06-14 15:16:35 +0000562 echo_vtab *pVtab = (echo_vtab *)tab;
563 sqlite3 *db = pVtab->db;
564 int rc = SQLITE_OK;
565
danielk1977176f4d22006-06-15 10:41:15 +0000566 sqlite3_stmt *pStmt;
567 char *z = 0; /* SQL statement to execute */
568 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
569 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
570 int i; /* Counter variable used by for loops */
571
danielk197726e41442006-06-14 15:16:35 +0000572 assert( nData==pVtab->nCol+2 || nData==1 );
573
drh5aec0422006-06-14 23:43:31 +0000574 /* If apData[0] is an integer and nData>1 then do an UPDATE */
575 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
danielk1977176f4d22006-06-15 10:41:15 +0000576 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
drh5aec0422006-06-14 23:43:31 +0000577 char *zSep = " SET";
danielk1977176f4d22006-06-15 10:41:15 +0000578
579 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
580 bindArgZero = 1;
581
582 if( bindArgOne ){
583 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000584 zSep = ",";
585 }
586 for(i=2; i<nData; i++){
587 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000588 string_concat(&z, sqlite3_mprintf(
589 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000590 zSep = ",";
591 }
danielk1977176f4d22006-06-15 10:41:15 +0000592 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000593 }
594
danielk1977176f4d22006-06-15 10:41:15 +0000595 /* If apData[0] is an integer and nData==1 then do a DELETE */
596 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
597 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
598 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000599 }
600
danielk1977176f4d22006-06-15 10:41:15 +0000601 /* If the first argument is NULL and there are more than two args, INSERT */
602 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000603 int ii;
604 char *zInsert = 0;
605 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000606
danielk1977176f4d22006-06-15 10:41:15 +0000607 zInsert = sqlite3_mprintf("INSERT OR REPLACE INTO %Q (", pVtab->zTableName);
608 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
609 bindArgOne = 1;
610 zValues = sqlite3_mprintf("?");
611 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000612 }
613
danielk1977176f4d22006-06-15 10:41:15 +0000614 assert((pVtab->nCol+2)==nData);
615 for(ii=2; ii<nData; ii++){
616 string_concat(&zInsert,
617 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
618 string_concat(&zValues,
619 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000620 }
621
danielk1977176f4d22006-06-15 10:41:15 +0000622 string_concat(&z, zInsert, 1);
623 string_concat(&z, ") VALUES(", 0);
624 string_concat(&z, zValues, 1);
625 string_concat(&z, ")", 0);
626 }
627
628 /* Anything else is an error */
629 else{
630 assert(0);
631 return SQLITE_ERROR;
632 }
633
634 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
635 assert( rc!=SQLITE_OK || pStmt );
636 sqlite3_free(z);
637 if( rc==SQLITE_OK ) {
638 if( bindArgZero ){
639 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000640 }
danielk1977176f4d22006-06-15 10:41:15 +0000641 if( bindArgOne ){
642 sqlite3_bind_value(pStmt, 1, apData[1]);
643 }
644 for(i=2; i<nData; i++){
645 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
646 }
647 sqlite3_step(pStmt);
648 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000649 }
650
danielk19771f6eec52006-06-16 06:17:47 +0000651 if( pRowid && rc==SQLITE_OK ){
652 *pRowid = sqlite3_last_insert_rowid(db);
653 }
654
danielk197726e41442006-06-14 15:16:35 +0000655 return rc;
656}
657
drha967e882006-06-13 01:04:52 +0000658/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000659** xBegin, xSync, xCommit and xRollback callbacks for echo module
660** virtual tables. Do nothing other than add the name of the callback
661** to the $::echo_module Tcl variable.
662*/
663static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
664 char *z;
665 echo_vtab *pVtab = (echo_vtab *)tab;
666 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
667 appendToEchoModule(pVtab->interp, zCall);
668 appendToEchoModule(pVtab->interp, z);
669 sqlite3_free(z);
670 return SQLITE_OK;
671}
672static int echoBegin(sqlite3_vtab *tab){
673 return echoTransactionCall(tab, "xBegin");
674}
675static int echoSync(sqlite3_vtab *tab){
676 echo_vtab *pVtab = (echo_vtab *)tab;
677 Tcl_Interp *interp = pVtab->interp;
678 const char *zVal;
679
680 echoTransactionCall(tab, "xSync");
681
682 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
683 ** and it is set to the name of the real table underlying this virtual
684 ** echo module table, then cause this xSync operation to fail.
685 */
686 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
687 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
688 return -1;
689 }
690 return SQLITE_OK;
691}
692static int echoCommit(sqlite3_vtab *tab){
693 return echoTransactionCall(tab, "xCommit");
694}
695static int echoRollback(sqlite3_vtab *tab){
696 return echoTransactionCall(tab, "xRollback");
697}
698
699/*
drhb9bb7c12006-06-11 23:41:55 +0000700** A virtual table module that merely echos method calls into TCL
701** variables.
702*/
703static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000704 0, /* iVersion */
705 "echo", /* zName */
drhb9bb7c12006-06-11 23:41:55 +0000706 echoCreate,
707 echoConnect,
drha967e882006-06-13 01:04:52 +0000708 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000709 echoDisconnect,
710 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000711 echoOpen, /* xOpen - open a cursor */
712 echoClose, /* xClose - close a cursor */
713 echoFilter, /* xFilter - configure scan constraints */
714 echoNext, /* xNext - advance a cursor */
715 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000716 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +0000717 echoUpdate, /* xUpdate - write data */
718 echoBegin, /* xBegin - begin transaction */
719 echoSync, /* xSync - sync transaction */
720 echoCommit, /* xCommit - commit transaction */
721 echoRollback /* xRollback - rollback transaction */
drhb9bb7c12006-06-11 23:41:55 +0000722};
723
724/*
725** Decode a pointer to an sqlite3 object.
726*/
727static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
728 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
729 return TCL_OK;
730}
731
732
733/*
734** Register the echo virtual table module.
735*/
736static int register_echo_module(
737 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
738 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
739 int objc, /* Number of arguments */
740 Tcl_Obj *CONST objv[] /* Command arguments */
741){
742 sqlite3 *db;
743 if( objc!=2 ){
744 Tcl_WrongNumArgs(interp, 1, objv, "DB");
745 return TCL_ERROR;
746 }
747 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhb9bb7c12006-06-11 23:41:55 +0000748#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:13 +0000749 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +0000750#endif
751 return TCL_OK;
752}
753
754
755/*
756** Register commands with the TCL interpreter.
757*/
758int Sqlitetest8_Init(Tcl_Interp *interp){
759 static struct {
760 char *zName;
761 Tcl_ObjCmdProc *xProc;
762 void *clientData;
763 } aObjCmd[] = {
764 { "register_echo_module", register_echo_module, 0 },
765 };
766 int i;
767 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
768 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
769 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
770 }
771 return TCL_OK;
772}