blob: ca04dc9c7502362148036929f6afb27eee015686 [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**
danielk197770ba1642006-06-21 16:02:42 +000016** $Id: test8.c,v 1.30 2006/06/21 16:02:43 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
danielk197770ba1642006-06-21 16:02:42 +0000197 if( argc==4 ){
danielk19777e6ebfb2006-06-12 11:24:37 +0000198 sqlite3_stmt *pStmt = 0;
199 sqlite3_prepare(db,
200 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
201 -1, &pStmt, 0);
danielk197770ba1642006-06-21 16:02:42 +0000202 sqlite3_bind_text(pStmt, 1, argv[3], -1, 0);
danielk19777e6ebfb2006-06-12 11:24:37 +0000203 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 ){
danielk197770ba1642006-06-21 16:02:42 +0000213 rc = getIndexArray(db, argv[3], &pVtab->aIndex);
danielk19775fac9f82006-06-13 14:16:58 +0000214 }
215 if( rc==SQLITE_OK ){
danielk197770ba1642006-06-21 16:02:42 +0000216 rc = getColumnNames(db, argv[3], &pVtab->aCol, &pVtab->nCol);
danielk19775fac9f82006-06-13 14:16:58 +0000217 }
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;
danielk197770ba1642006-06-21 16:02:42 +0000248 pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
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;
danielk19774b2688a2006-06-20 11:01:07 +0000325 if( ((echo_cursor *)cur)->errcode ){
326 return ((echo_cursor *)cur)->errcode;
327 }
danielk1977fbbe0052006-06-21 07:02:33 +0000328 if( !pStmt ){
329 sqlite3_result_null(ctx);
330 }else{
331 assert( sqlite3_data_count(pStmt)>iCol );
332 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
333 }
danielk1977b7a7b9a2006-06-13 10:24:42 +0000334 return SQLITE_OK;
335}
336
danielk19775fac9f82006-06-13 14:16:58 +0000337static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000338 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
339 *pRowid = sqlite3_column_int64(pStmt, 0);
340 return SQLITE_OK;
341}
342
danielk197798331532006-06-14 10:55:52 +0000343/*
344** Compute a simple hash of the null terminated string zString.
345**
346** This module uses only sqlite3_index_info.idxStr, not
347** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
348** in echoBestIndex(), idxNum is set to the corresponding hash value.
349** In echoFilter(), code assert()s that the supplied idxNum value is
350** indeed the hash of the supplied idxStr.
351*/
352static int hashString(const char *zString){
353 int val = 0;
354 int ii;
355 for(ii=0; zString[ii]; ii++){
356 val = (val << 3) + (int)zString[ii];
357 }
358 return val;
359}
360
danielk1977b7a7b9a2006-06-13 10:24:42 +0000361
danielk19775fac9f82006-06-13 14:16:58 +0000362static int echoFilter(
363 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000364 int idxNum, const char *idxStr,
365 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000366){
367 int rc;
danielk19774b2688a2006-06-20 11:01:07 +0000368 int ret;
drh4be8b512006-06-13 23:51:34 +0000369 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000370
371 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
372 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
373 sqlite3 *db = pVtab->db;
374
danielk197798331532006-06-14 10:55:52 +0000375 assert( idxNum==hashString(idxStr) );
danielk19775fac9f82006-06-13 14:16:58 +0000376 sqlite3_finalize(pCur->pStmt);
377 pCur->pStmt = 0;
drh4be8b512006-06-13 23:51:34 +0000378 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
danielk1977fbbe0052006-06-21 07:02:33 +0000379 assert( pCur->pStmt || rc!=SQLITE_OK );
danielk19774b2688a2006-06-20 11:01:07 +0000380 for(i=0; rc==SQLITE_OK && i<argc; i++){
drh4be8b512006-06-13 23:51:34 +0000381 switch( sqlite3_value_type(argv[i]) ){
382 case SQLITE_INTEGER: {
383 sqlite3_bind_int64(pCur->pStmt, i+1, sqlite3_value_int64(argv[i]));
384 break;
385 }
386 case SQLITE_FLOAT: {
387 sqlite3_bind_double(pCur->pStmt, i+1, sqlite3_value_double(argv[i]));
388 break;
389 }
390 case SQLITE_NULL: {
391 sqlite3_bind_null(pCur->pStmt, i+1);
392 break;
393 }
394 case SQLITE_TEXT: {
395 sqlite3_bind_text(pCur->pStmt, i+1, sqlite3_value_text(argv[i]),
396 sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
397 break;
398 }
399 case SQLITE_BLOB: {
400 sqlite3_bind_blob(pCur->pStmt, i+1, sqlite3_value_blob(argv[i]),
401 sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
402 break;
403 }
404 }
405 }
danielk19775fac9f82006-06-13 14:16:58 +0000406 if( rc==SQLITE_OK ){
danielk19774b2688a2006-06-20 11:01:07 +0000407 ret = echoNext(pVtabCursor);
408 }else{
409 ret = 0;
410 pCur->errcode = rc;
danielk19775fac9f82006-06-13 14:16:58 +0000411 }
412
drh4be8b512006-06-13 23:51:34 +0000413 appendToEchoModule(pVtab->interp, "xFilter");
414 appendToEchoModule(pVtab->interp, idxStr);
415 for(i=0; i<argc; i++){
416 appendToEchoModule(pVtab->interp, sqlite3_value_text(argv[i]));
417 }
418
danielk19774b2688a2006-06-20 11:01:07 +0000419 return ret;
danielk19775fac9f82006-06-13 14:16:58 +0000420}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000421
drhb9bb7c12006-06-11 23:41:55 +0000422/*
danielk19775fac9f82006-06-13 14:16:58 +0000423** The echo module implements the subset of query constraints and sort
424** orders that may take advantage of SQLite indices on the underlying
425** real table. For example, if the real table is declared as:
426**
427** CREATE TABLE real(a, b, c);
428** CREATE INDEX real_index ON real(b);
429**
430** then the echo module handles WHERE or ORDER BY clauses that refer
431** to the column "b", but not "a" or "c". If a multi-column index is
432** present, only it's left most column is considered.
drha967e882006-06-13 01:04:52 +0000433*/
danielk19775fac9f82006-06-13 14:16:58 +0000434static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
435 int ii;
drh4be8b512006-06-13 23:51:34 +0000436 char *zQuery = 0;
437 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000438 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000439 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000440 echo_vtab *pVtab = (echo_vtab *)tab;
danielk197774cdba42006-06-19 12:02:58 +0000441 sqlite3_stmt *pStmt = 0;
442
443 int nRow;
444 int useIdx = 0;
445 int rc = SQLITE_OK;
446
447 /* Determine the number of rows in the table and store this value in local
448 ** variable nRow. The 'estimated-cost' of the scan will be the number of
449 ** rows in the table for a linear scan, or the log (base 2) of the
450 ** number of rows if the proposed scan uses an index.
451 */
452 zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
453 rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
454 if( rc!=SQLITE_OK ){
455 return rc;
456 }
457 sqlite3_step(pStmt);
458 nRow = sqlite3_column_int(pStmt, 0);
459 rc = sqlite3_finalize(pStmt);
460 if( rc!=SQLITE_OK ){
461 return rc;
462 }
danielk19775fac9f82006-06-13 14:16:58 +0000463
drh4be8b512006-06-13 23:51:34 +0000464 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000465 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
466 const struct sqlite3_index_constraint *pConstraint;
467 struct sqlite3_index_constraint_usage *pUsage;
468
469 pConstraint = &pIdxInfo->aConstraint[ii];
470 pUsage = &pIdxInfo->aConstraintUsage[ii];
471
472 int iCol = pConstraint->iColumn;
473 if( pVtab->aIndex[iCol] ){
474 char *zCol = pVtab->aCol[iCol];
475 char *zOp = 0;
danielk197774cdba42006-06-19 12:02:58 +0000476 useIdx = 1;
danielk1977176f4d22006-06-15 10:41:15 +0000477 if( iCol<0 ){
478 zCol = "rowid";
479 }
danielk19775fac9f82006-06-13 14:16:58 +0000480 switch( pConstraint->op ){
481 case SQLITE_INDEX_CONSTRAINT_EQ:
482 zOp = "="; break;
483 case SQLITE_INDEX_CONSTRAINT_LT:
484 zOp = "<"; break;
485 case SQLITE_INDEX_CONSTRAINT_GT:
486 zOp = ">"; break;
487 case SQLITE_INDEX_CONSTRAINT_LE:
488 zOp = "<="; break;
489 case SQLITE_INDEX_CONSTRAINT_GE:
490 zOp = ">="; break;
491 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000492 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000493 }
drh1a90e092006-06-14 22:07:10 +0000494 if( zOp[0]=='L' ){
495 zNew = sqlite3_mprintf("%s %s %s LIKE (SELECT '%%'||?||'%%')",
496 zQuery, zSep, zCol);
497 } else {
498 zNew = sqlite3_mprintf("%s %s %s %s ?", zQuery, zSep, zCol, zOp);
499 }
drh4be8b512006-06-13 23:51:34 +0000500 sqlite3_free(zQuery);
501 zQuery = zNew;
502 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000503 pUsage->argvIndex = ++nArg;
504 pUsage->omit = 1;
505 }
506 }
danielk197747d08092006-06-14 07:41:31 +0000507
508 /* If there is only one term in the ORDER BY clause, and it is
509 ** on a column that this virtual table has an index for, then consume
510 ** the ORDER BY clause.
511 */
512 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
513 char *zCol = pVtab->aCol[pIdxInfo->aOrderBy->iColumn];
514 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
515 zNew = sqlite3_mprintf("%s ORDER BY %s %s", zQuery, zCol, zDir);
516 sqlite3_free(zQuery);
517 zQuery = zNew;
518 pIdxInfo->orderByConsumed = 1;
519 }
520
danielk19775fac9f82006-06-13 14:16:58 +0000521 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000522 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000523
danielk197798331532006-06-14 10:55:52 +0000524 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000525 pIdxInfo->idxStr = zQuery;
526 pIdxInfo->needToFreeIdxStr = 1;
danielk197774cdba42006-06-19 12:02:58 +0000527 if( useIdx ){
528 /* Approximation of log2(nRow). */
529 for( ii=0; ii<(sizeof(int)*8); ii++ ){
530 if( nRow & (1<<ii) ){
531 pIdxInfo->estimatedCost = (double)ii;
532 }
533 }
534 } else {
535 pIdxInfo->estimatedCost = (double)nRow;
536 }
537 return rc;
drha967e882006-06-13 01:04:52 +0000538}
539
danielk1977176f4d22006-06-15 10:41:15 +0000540static void string_concat(char **pzStr, char *zAppend, int doFree){
541 char *zIn = *pzStr;
542 if( zIn ){
543 char *zTemp = zIn;
544 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
545 sqlite3_free(zTemp);
546 }else{
547 zIn = sqlite3_mprintf("%s", zAppend);
548 }
549 *pzStr = zIn;
550 if( doFree ){
551 sqlite3_free(zAppend);
552 }
553}
554
555/*
556** apData[0] apData[1] apData[2..]
557**
558** INTEGER DELETE
559**
560** INTEGER NULL (nCol args) UPDATE (do not set rowid)
561** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
562**
563** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
564** NULL INTEGER (nCol args) INSERT (incl. rowid value)
565**
566*/
danielk19771f6eec52006-06-16 06:17:47 +0000567int echoUpdate(
568 sqlite3_vtab *tab,
569 int nData,
570 sqlite3_value **apData,
571 sqlite_int64 *pRowid
572){
danielk197726e41442006-06-14 15:16:35 +0000573 echo_vtab *pVtab = (echo_vtab *)tab;
574 sqlite3 *db = pVtab->db;
575 int rc = SQLITE_OK;
576
danielk1977176f4d22006-06-15 10:41:15 +0000577 sqlite3_stmt *pStmt;
578 char *z = 0; /* SQL statement to execute */
579 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
580 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
581 int i; /* Counter variable used by for loops */
582
danielk197726e41442006-06-14 15:16:35 +0000583 assert( nData==pVtab->nCol+2 || nData==1 );
584
drh5aec0422006-06-14 23:43:31 +0000585 /* If apData[0] is an integer and nData>1 then do an UPDATE */
586 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
danielk1977176f4d22006-06-15 10:41:15 +0000587 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
drh5aec0422006-06-14 23:43:31 +0000588 char *zSep = " SET";
danielk1977176f4d22006-06-15 10:41:15 +0000589
590 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
591 bindArgZero = 1;
592
593 if( bindArgOne ){
594 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000595 zSep = ",";
596 }
597 for(i=2; i<nData; i++){
598 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000599 string_concat(&z, sqlite3_mprintf(
600 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000601 zSep = ",";
602 }
danielk1977176f4d22006-06-15 10:41:15 +0000603 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000604 }
605
danielk1977176f4d22006-06-15 10:41:15 +0000606 /* If apData[0] is an integer and nData==1 then do a DELETE */
607 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
608 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
609 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000610 }
611
danielk1977176f4d22006-06-15 10:41:15 +0000612 /* If the first argument is NULL and there are more than two args, INSERT */
613 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000614 int ii;
615 char *zInsert = 0;
616 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000617
danielk19774b2688a2006-06-20 11:01:07 +0000618 zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
danielk1977176f4d22006-06-15 10:41:15 +0000619 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
620 bindArgOne = 1;
621 zValues = sqlite3_mprintf("?");
622 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000623 }
624
danielk1977176f4d22006-06-15 10:41:15 +0000625 assert((pVtab->nCol+2)==nData);
626 for(ii=2; ii<nData; ii++){
627 string_concat(&zInsert,
628 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
629 string_concat(&zValues,
630 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000631 }
632
danielk1977176f4d22006-06-15 10:41:15 +0000633 string_concat(&z, zInsert, 1);
634 string_concat(&z, ") VALUES(", 0);
635 string_concat(&z, zValues, 1);
636 string_concat(&z, ")", 0);
637 }
638
639 /* Anything else is an error */
640 else{
641 assert(0);
642 return SQLITE_ERROR;
643 }
644
645 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
646 assert( rc!=SQLITE_OK || pStmt );
647 sqlite3_free(z);
648 if( rc==SQLITE_OK ) {
649 if( bindArgZero ){
650 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000651 }
danielk1977176f4d22006-06-15 10:41:15 +0000652 if( bindArgOne ){
653 sqlite3_bind_value(pStmt, 1, apData[1]);
654 }
655 for(i=2; i<nData; i++){
656 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
657 }
658 sqlite3_step(pStmt);
659 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000660 }
661
danielk19771f6eec52006-06-16 06:17:47 +0000662 if( pRowid && rc==SQLITE_OK ){
663 *pRowid = sqlite3_last_insert_rowid(db);
664 }
665
danielk197726e41442006-06-14 15:16:35 +0000666 return rc;
667}
668
drha967e882006-06-13 01:04:52 +0000669/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000670** xBegin, xSync, xCommit and xRollback callbacks for echo module
671** virtual tables. Do nothing other than add the name of the callback
672** to the $::echo_module Tcl variable.
673*/
674static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
675 char *z;
676 echo_vtab *pVtab = (echo_vtab *)tab;
677 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
678 appendToEchoModule(pVtab->interp, zCall);
679 appendToEchoModule(pVtab->interp, z);
680 sqlite3_free(z);
681 return SQLITE_OK;
682}
683static int echoBegin(sqlite3_vtab *tab){
684 return echoTransactionCall(tab, "xBegin");
685}
686static int echoSync(sqlite3_vtab *tab){
687 echo_vtab *pVtab = (echo_vtab *)tab;
688 Tcl_Interp *interp = pVtab->interp;
689 const char *zVal;
690
691 echoTransactionCall(tab, "xSync");
692
693 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
694 ** and it is set to the name of the real table underlying this virtual
695 ** echo module table, then cause this xSync operation to fail.
696 */
697 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
698 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
699 return -1;
700 }
701 return SQLITE_OK;
702}
703static int echoCommit(sqlite3_vtab *tab){
704 return echoTransactionCall(tab, "xCommit");
705}
706static int echoRollback(sqlite3_vtab *tab){
707 return echoTransactionCall(tab, "xRollback");
708}
709
710/*
drhb9bb7c12006-06-11 23:41:55 +0000711** A virtual table module that merely echos method calls into TCL
712** variables.
713*/
714static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000715 0, /* iVersion */
716 "echo", /* zName */
drhb9bb7c12006-06-11 23:41:55 +0000717 echoCreate,
718 echoConnect,
drha967e882006-06-13 01:04:52 +0000719 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000720 echoDisconnect,
721 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000722 echoOpen, /* xOpen - open a cursor */
723 echoClose, /* xClose - close a cursor */
724 echoFilter, /* xFilter - configure scan constraints */
725 echoNext, /* xNext - advance a cursor */
726 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000727 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +0000728 echoUpdate, /* xUpdate - write data */
729 echoBegin, /* xBegin - begin transaction */
730 echoSync, /* xSync - sync transaction */
731 echoCommit, /* xCommit - commit transaction */
732 echoRollback /* xRollback - rollback transaction */
drhb9bb7c12006-06-11 23:41:55 +0000733};
734
735/*
736** Decode a pointer to an sqlite3 object.
737*/
738static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
739 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
740 return TCL_OK;
741}
742
743
744/*
745** Register the echo virtual table module.
746*/
747static int register_echo_module(
748 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
749 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
750 int objc, /* Number of arguments */
751 Tcl_Obj *CONST objv[] /* Command arguments */
752){
753 sqlite3 *db;
754 if( objc!=2 ){
755 Tcl_WrongNumArgs(interp, 1, objv, "DB");
756 return TCL_ERROR;
757 }
758 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhb9bb7c12006-06-11 23:41:55 +0000759#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:13 +0000760 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +0000761#endif
762 return TCL_OK;
763}
764
765
766/*
767** Register commands with the TCL interpreter.
768*/
769int Sqlitetest8_Init(Tcl_Interp *interp){
770 static struct {
771 char *zName;
772 Tcl_ObjCmdProc *xProc;
773 void *clientData;
774 } aObjCmd[] = {
775 { "register_echo_module", register_echo_module, 0 },
776 };
777 int i;
778 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
779 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
780 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
781 }
782 return TCL_OK;
783}