blob: 5e412e1c6841da4534368a22187bb7c1e27a9ed7 [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**
danielk1977c69cdfd2006-06-17 09:39:55 +000016** $Id: test8.c,v 1.25 2006/06/17 09:39:56 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;
430
drh4be8b512006-06-13 23:51:34 +0000431 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000432 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
433 const struct sqlite3_index_constraint *pConstraint;
434 struct sqlite3_index_constraint_usage *pUsage;
435
436 pConstraint = &pIdxInfo->aConstraint[ii];
437 pUsage = &pIdxInfo->aConstraintUsage[ii];
438
439 int iCol = pConstraint->iColumn;
440 if( pVtab->aIndex[iCol] ){
441 char *zCol = pVtab->aCol[iCol];
442 char *zOp = 0;
danielk1977176f4d22006-06-15 10:41:15 +0000443 if( iCol<0 ){
444 zCol = "rowid";
445 }
danielk19775fac9f82006-06-13 14:16:58 +0000446 switch( pConstraint->op ){
447 case SQLITE_INDEX_CONSTRAINT_EQ:
448 zOp = "="; break;
449 case SQLITE_INDEX_CONSTRAINT_LT:
450 zOp = "<"; break;
451 case SQLITE_INDEX_CONSTRAINT_GT:
452 zOp = ">"; break;
453 case SQLITE_INDEX_CONSTRAINT_LE:
454 zOp = "<="; break;
455 case SQLITE_INDEX_CONSTRAINT_GE:
456 zOp = ">="; break;
457 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000458 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000459 }
drh1a90e092006-06-14 22:07:10 +0000460 if( zOp[0]=='L' ){
461 zNew = sqlite3_mprintf("%s %s %s LIKE (SELECT '%%'||?||'%%')",
462 zQuery, zSep, zCol);
463 } else {
464 zNew = sqlite3_mprintf("%s %s %s %s ?", zQuery, zSep, zCol, zOp);
465 }
drh4be8b512006-06-13 23:51:34 +0000466 sqlite3_free(zQuery);
467 zQuery = zNew;
468 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000469 pUsage->argvIndex = ++nArg;
470 pUsage->omit = 1;
471 }
472 }
danielk197747d08092006-06-14 07:41:31 +0000473
474 /* If there is only one term in the ORDER BY clause, and it is
475 ** on a column that this virtual table has an index for, then consume
476 ** the ORDER BY clause.
477 */
478 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
479 char *zCol = pVtab->aCol[pIdxInfo->aOrderBy->iColumn];
480 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
481 zNew = sqlite3_mprintf("%s ORDER BY %s %s", zQuery, zCol, zDir);
482 sqlite3_free(zQuery);
483 zQuery = zNew;
484 pIdxInfo->orderByConsumed = 1;
485 }
486
danielk19775fac9f82006-06-13 14:16:58 +0000487 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000488 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000489
danielk197798331532006-06-14 10:55:52 +0000490 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000491 pIdxInfo->idxStr = zQuery;
492 pIdxInfo->needToFreeIdxStr = 1;
493 pIdxInfo->estimatedCost = 1.0;
drha967e882006-06-13 01:04:52 +0000494 return SQLITE_OK;
495}
496
danielk1977176f4d22006-06-15 10:41:15 +0000497static void string_concat(char **pzStr, char *zAppend, int doFree){
498 char *zIn = *pzStr;
499 if( zIn ){
500 char *zTemp = zIn;
501 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
502 sqlite3_free(zTemp);
503 }else{
504 zIn = sqlite3_mprintf("%s", zAppend);
505 }
506 *pzStr = zIn;
507 if( doFree ){
508 sqlite3_free(zAppend);
509 }
510}
511
512/*
513** apData[0] apData[1] apData[2..]
514**
515** INTEGER DELETE
516**
517** INTEGER NULL (nCol args) UPDATE (do not set rowid)
518** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
519**
520** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
521** NULL INTEGER (nCol args) INSERT (incl. rowid value)
522**
523*/
danielk19771f6eec52006-06-16 06:17:47 +0000524int echoUpdate(
525 sqlite3_vtab *tab,
526 int nData,
527 sqlite3_value **apData,
528 sqlite_int64 *pRowid
529){
danielk197726e41442006-06-14 15:16:35 +0000530 echo_vtab *pVtab = (echo_vtab *)tab;
531 sqlite3 *db = pVtab->db;
532 int rc = SQLITE_OK;
533
danielk1977176f4d22006-06-15 10:41:15 +0000534 sqlite3_stmt *pStmt;
535 char *z = 0; /* SQL statement to execute */
536 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
537 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
538 int i; /* Counter variable used by for loops */
539
danielk197726e41442006-06-14 15:16:35 +0000540 assert( nData==pVtab->nCol+2 || nData==1 );
541
drh5aec0422006-06-14 23:43:31 +0000542 /* If apData[0] is an integer and nData>1 then do an UPDATE */
543 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
danielk1977176f4d22006-06-15 10:41:15 +0000544 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
drh5aec0422006-06-14 23:43:31 +0000545 char *zSep = " SET";
danielk1977176f4d22006-06-15 10:41:15 +0000546
547 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
548 bindArgZero = 1;
549
550 if( bindArgOne ){
551 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000552 zSep = ",";
553 }
554 for(i=2; i<nData; i++){
555 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000556 string_concat(&z, sqlite3_mprintf(
557 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000558 zSep = ",";
559 }
danielk1977176f4d22006-06-15 10:41:15 +0000560 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000561 }
562
danielk1977176f4d22006-06-15 10:41:15 +0000563 /* If apData[0] is an integer and nData==1 then do a DELETE */
564 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
565 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
566 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000567 }
568
danielk1977176f4d22006-06-15 10:41:15 +0000569 /* If the first argument is NULL and there are more than two args, INSERT */
570 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000571 int ii;
572 char *zInsert = 0;
573 char *zValues = 0;
danielk19771f6eec52006-06-16 06:17:47 +0000574
danielk1977176f4d22006-06-15 10:41:15 +0000575 zInsert = sqlite3_mprintf("INSERT OR REPLACE INTO %Q (", pVtab->zTableName);
576 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
577 bindArgOne = 1;
578 zValues = sqlite3_mprintf("?");
579 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000580 }
581
danielk1977176f4d22006-06-15 10:41:15 +0000582 assert((pVtab->nCol+2)==nData);
583 for(ii=2; ii<nData; ii++){
584 string_concat(&zInsert,
585 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
586 string_concat(&zValues,
587 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000588 }
589
danielk1977176f4d22006-06-15 10:41:15 +0000590 string_concat(&z, zInsert, 1);
591 string_concat(&z, ") VALUES(", 0);
592 string_concat(&z, zValues, 1);
593 string_concat(&z, ")", 0);
594 }
595
596 /* Anything else is an error */
597 else{
598 assert(0);
599 return SQLITE_ERROR;
600 }
601
602 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
603 assert( rc!=SQLITE_OK || pStmt );
604 sqlite3_free(z);
605 if( rc==SQLITE_OK ) {
606 if( bindArgZero ){
607 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000608 }
danielk1977176f4d22006-06-15 10:41:15 +0000609 if( bindArgOne ){
610 sqlite3_bind_value(pStmt, 1, apData[1]);
611 }
612 for(i=2; i<nData; i++){
613 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
614 }
615 sqlite3_step(pStmt);
616 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000617 }
618
danielk19771f6eec52006-06-16 06:17:47 +0000619 if( pRowid && rc==SQLITE_OK ){
620 *pRowid = sqlite3_last_insert_rowid(db);
621 }
622
danielk197726e41442006-06-14 15:16:35 +0000623 return rc;
624}
625
drha967e882006-06-13 01:04:52 +0000626/*
danielk1977c69cdfd2006-06-17 09:39:55 +0000627** xBegin, xSync, xCommit and xRollback callbacks for echo module
628** virtual tables. Do nothing other than add the name of the callback
629** to the $::echo_module Tcl variable.
630*/
631static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
632 char *z;
633 echo_vtab *pVtab = (echo_vtab *)tab;
634 z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
635 appendToEchoModule(pVtab->interp, zCall);
636 appendToEchoModule(pVtab->interp, z);
637 sqlite3_free(z);
638 return SQLITE_OK;
639}
640static int echoBegin(sqlite3_vtab *tab){
641 return echoTransactionCall(tab, "xBegin");
642}
643static int echoSync(sqlite3_vtab *tab){
644 echo_vtab *pVtab = (echo_vtab *)tab;
645 Tcl_Interp *interp = pVtab->interp;
646 const char *zVal;
647
648 echoTransactionCall(tab, "xSync");
649
650 /* Check if the $::echo_module_sync_fail variable is defined. If it is,
651 ** and it is set to the name of the real table underlying this virtual
652 ** echo module table, then cause this xSync operation to fail.
653 */
654 zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
655 if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
656 return -1;
657 }
658 return SQLITE_OK;
659}
660static int echoCommit(sqlite3_vtab *tab){
661 return echoTransactionCall(tab, "xCommit");
662}
663static int echoRollback(sqlite3_vtab *tab){
664 return echoTransactionCall(tab, "xRollback");
665}
666
667/*
drhb9bb7c12006-06-11 23:41:55 +0000668** A virtual table module that merely echos method calls into TCL
669** variables.
670*/
671static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000672 0, /* iVersion */
673 "echo", /* zName */
drhb9bb7c12006-06-11 23:41:55 +0000674 echoCreate,
675 echoConnect,
drha967e882006-06-13 01:04:52 +0000676 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000677 echoDisconnect,
678 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000679 echoOpen, /* xOpen - open a cursor */
680 echoClose, /* xClose - close a cursor */
681 echoFilter, /* xFilter - configure scan constraints */
682 echoNext, /* xNext - advance a cursor */
683 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000684 echoRowid, /* xRowid - read data */
danielk1977c69cdfd2006-06-17 09:39:55 +0000685 echoUpdate, /* xUpdate - write data */
686 echoBegin, /* xBegin - begin transaction */
687 echoSync, /* xSync - sync transaction */
688 echoCommit, /* xCommit - commit transaction */
689 echoRollback /* xRollback - rollback transaction */
drhb9bb7c12006-06-11 23:41:55 +0000690};
691
692/*
693** Decode a pointer to an sqlite3 object.
694*/
695static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
696 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
697 return TCL_OK;
698}
699
700
701/*
702** Register the echo virtual table module.
703*/
704static int register_echo_module(
705 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
706 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
707 int objc, /* Number of arguments */
708 Tcl_Obj *CONST objv[] /* Command arguments */
709){
710 sqlite3 *db;
711 if( objc!=2 ){
712 Tcl_WrongNumArgs(interp, 1, objv, "DB");
713 return TCL_ERROR;
714 }
715 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhb9bb7c12006-06-11 23:41:55 +0000716#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:13 +0000717 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +0000718#endif
719 return TCL_OK;
720}
721
722
723/*
724** Register commands with the TCL interpreter.
725*/
726int Sqlitetest8_Init(Tcl_Interp *interp){
727 static struct {
728 char *zName;
729 Tcl_ObjCmdProc *xProc;
730 void *clientData;
731 } aObjCmd[] = {
732 { "register_echo_module", register_echo_module, 0 },
733 };
734 int i;
735 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
736 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
737 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
738 }
739 return TCL_OK;
740}