blob: 847ae20f55b972c1172769790fce7780a1b47fa6 [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**
danielk1977d6e8dd02006-06-15 15:38:41 +000016** $Id: test8.c,v 1.23 2006/06/15 15:38:42 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
danielk19775fac9f82006-06-13 14:16:58 +000027/*
danielk1977d6e8dd02006-06-15 15:38:41 +000028** An echo virtual-table object.
danielk19775fac9f82006-06-13 14:16:58 +000029**
danielk1977d6e8dd02006-06-15 15:38:41 +000030** echo.vtab.aIndex is an array of booleans. The nth entry is true if
31** the nth column of the real table is the left-most column of an index
32** (implicit or otherwise). In other words, if SQLite can optimize
33** a query like "SELECT * FROM real_table WHERE col = ?".
34**
35** Member variable contains copies of the column names of the real table.
danielk19775fac9f82006-06-13 14:16:58 +000036*/
danielk1977b7a7b9a2006-06-13 10:24:42 +000037struct echo_vtab {
38 sqlite3_vtab base;
39 Tcl_Interp *interp;
40 sqlite3 *db;
danielk19775fac9f82006-06-13 14:16:58 +000041
danielk1977a4e76362006-06-14 06:31:28 +000042 char *zTableName; /* Name of the real table */
43 int nCol; /* Number of columns in the real table */
44 int *aIndex; /* Array of size nCol. True if column has an index */
45 char **aCol; /* Array of size nCol. Column names */
danielk1977b7a7b9a2006-06-13 10:24:42 +000046};
47
48/* An echo cursor object */
49struct echo_cursor {
50 sqlite3_vtab_cursor base;
51 sqlite3_stmt *pStmt;
52 int errcode; /* Error code */
53};
54
danielk19775fac9f82006-06-13 14:16:58 +000055static int getColumnNames(
56 sqlite3 *db,
57 const char *zTab,
58 char ***paCol,
59 int *pnCol
60){
61 char **aCol = 0;
62 char zBuf[1024];
63 sqlite3_stmt *pStmt = 0;
64 int rc = SQLITE_OK;
65 int nCol;
66
67 sprintf(zBuf, "SELECT * FROM %s", zTab);
68 rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0);
69 if( rc==SQLITE_OK ){
70 int ii;
71 nCol = sqlite3_column_count(pStmt);
72 aCol = sqliteMalloc(sizeof(char *) * nCol);
73 if( !aCol ){
74 rc = SQLITE_NOMEM;
75 goto fail;
76 }
77 for(ii=0; ii<nCol; ii++){
78 aCol[ii] = sqlite3StrDup(sqlite3_column_name(pStmt, ii));
79 if( !aCol[ii] ){
80 rc = SQLITE_NOMEM;
81 goto fail;
82 }
83 }
84 }
85
86 *paCol = aCol;
87 *pnCol = nCol;
88
89fail:
90 sqlite3_finalize(pStmt);
91 if( rc!=SQLITE_OK && aCol ){
92 int ii;
93 for(ii=0; ii<nCol; ii++){
94 sqliteFree(aCol[ii]);
95 }
96 sqliteFree(aCol);
97 }
98 return rc;
99}
100
101static int getIndexArray(sqlite3 *db, const char *zTab, int **paIndex){
102 char zBuf[1024];
103 sqlite3_stmt *pStmt = 0;
104 int nCol;
105 int *aIndex = 0;
106 int rc;
107
108 sprintf(zBuf, "SELECT * FROM %s", zTab);
109 rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0);
110 nCol = sqlite3_column_count(pStmt);
111
112 sqlite3_finalize(pStmt);
113 pStmt = 0;
114 if( rc!=SQLITE_OK ){
115 goto get_index_array_out;
116 }
117
118 aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
119 if( !aIndex ){
120 rc = SQLITE_NOMEM;
121 goto get_index_array_out;
122 }
123
124 sprintf(zBuf, "PRAGMA index_list(%s)", zTab);
125 rc = sqlite3_prepare(db, zBuf, -1, &pStmt, 0);
126
127 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
128 sqlite3_stmt *pStmt2 = 0;
129 sprintf(zBuf, "PRAGMA index_info(%s)", sqlite3_column_text(pStmt, 1));
130 rc = sqlite3_prepare(db, zBuf, -1, &pStmt2, 0);
131 if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
132 int cid = sqlite3_column_int(pStmt2, 1);
133 assert( cid>=0 && cid<nCol );
134 aIndex[cid] = 1;
135 }
136 rc = sqlite3_finalize(pStmt2);
137 if( rc!=SQLITE_OK ){
138 sqlite3_finalize(pStmt);
139 goto get_index_array_out;
140 }
141 }
142
143 rc = sqlite3_finalize(pStmt);
144
145get_index_array_out:
146 if( rc!=SQLITE_OK ){
147 sqliteFree(aIndex);
148 aIndex = 0;
149 }
150 *paIndex = aIndex;
151 return rc;
152}
153
danielk197778efaba2006-06-12 06:09:17 +0000154/*
155** Global Tcl variable $echo_module is a list. This routine appends
156** the string element zArg to that list in interpreter interp.
157*/
drha967e882006-06-13 01:04:52 +0000158static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
danielk197778efaba2006-06-12 06:09:17 +0000159 int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
danielk19775fac9f82006-06-13 14:16:58 +0000160 Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
danielk197778efaba2006-06-12 06:09:17 +0000161}
162
danielk19777e6ebfb2006-06-12 11:24:37 +0000163/*
164** This function is called from within the echo-modules xCreate and
165** xConnect methods. The argc and argv arguments are copies of those
166** passed to the calling method. This function is responsible for
167** calling sqlite3_declare_vtab() to declare the schema of the virtual
168** table being created or connected.
169**
170** If the constructor was passed just one argument, i.e.:
171**
172** CREATE TABLE t1 AS echo(t2);
173**
174** Then t2 is assumed to be the name of a *real* database table. The
175** schema of the virtual table is declared by passing a copy of the
176** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
177** Hence, the virtual table should have exactly the same column names and
178** types as the real table.
179*/
danielk1977b7a7b9a2006-06-13 10:24:42 +0000180static int echoDeclareVtab(
181 echo_vtab *pVtab,
182 sqlite3 *db,
183 int argc,
184 char **argv
185){
danielk19777e6ebfb2006-06-12 11:24:37 +0000186 int rc = SQLITE_OK;
187
188 if( argc==2 ){
189 sqlite3_stmt *pStmt = 0;
190 sqlite3_prepare(db,
191 "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
192 -1, &pStmt, 0);
193 sqlite3_bind_text(pStmt, 1, argv[1], -1, 0);
194 if( sqlite3_step(pStmt)==SQLITE_ROW ){
195 const char *zCreateTable = sqlite3_column_text(pStmt, 0);
drha75803d2006-06-12 12:50:23 +0000196#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19777e6ebfb2006-06-12 11:24:37 +0000197 sqlite3_declare_vtab(db, zCreateTable);
drha75803d2006-06-12 12:50:23 +0000198#endif
danielk19777e6ebfb2006-06-12 11:24:37 +0000199 } else {
200 rc = SQLITE_ERROR;
201 }
202 sqlite3_finalize(pStmt);
danielk19775fac9f82006-06-13 14:16:58 +0000203 if( rc==SQLITE_OK ){
204 rc = getIndexArray(db, argv[1], &pVtab->aIndex);
205 }
206 if( rc==SQLITE_OK ){
207 rc = getColumnNames(db, argv[1], &pVtab->aCol, &pVtab->nCol);
208 }
danielk19777e6ebfb2006-06-12 11:24:37 +0000209 }
210
211 return rc;
212}
213
danielk1977a4e76362006-06-14 06:31:28 +0000214static int echoDestructor(sqlite3_vtab *pVtab){
215 int ii;
216 echo_vtab *p = (echo_vtab*)pVtab;
217 sqliteFree(p->aIndex);
218 for(ii=0; ii<p->nCol; ii++){
219 sqliteFree(p->aCol[ii]);
220 }
221 sqliteFree(p->aCol);
222 sqliteFree(p->zTableName);
223 sqliteFree(p);
224 return 0;
225}
226
danielk1977b7a7b9a2006-06-13 10:24:42 +0000227static int echoConstructor(
228 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000229 void *pAux,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000230 int argc, char **argv,
231 sqlite3_vtab **ppVtab
232){
233 int i;
234 echo_vtab *pVtab;
235
236 pVtab = sqliteMalloc( sizeof(*pVtab) );
danielk19779da9d472006-06-14 06:58:15 +0000237 pVtab->interp = (Tcl_Interp *)pAux;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000238 pVtab->db = db;
drh4be8b512006-06-13 23:51:34 +0000239 pVtab->zTableName = sqlite3MPrintf("%s", argv[1]);
danielk1977b7a7b9a2006-06-13 10:24:42 +0000240 for(i=0; i<argc; i++){
241 appendToEchoModule(pVtab->interp, argv[i]);
242 }
243
danielk1977a4e76362006-06-14 06:31:28 +0000244 if( echoDeclareVtab(pVtab, db, argc, argv) ){
245 echoDestructor((sqlite3_vtab *)pVtab);
246 return SQLITE_ERROR;
247 }
248
249 *ppVtab = &pVtab->base;
250 return SQLITE_OK;
danielk1977b7a7b9a2006-06-13 10:24:42 +0000251}
drha967e882006-06-13 01:04:52 +0000252
drhb9bb7c12006-06-11 23:41:55 +0000253/* Methods for the echo module */
254static int echoCreate(
255 sqlite3 *db,
danielk19779da9d472006-06-14 06:58:15 +0000256 void *pAux,
drhb9bb7c12006-06-11 23:41:55 +0000257 int argc, char **argv,
258 sqlite3_vtab **ppVtab
259){
danielk19779da9d472006-06-14 06:58:15 +0000260 appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
261 return echoConstructor(db, pAux, argc, argv, ppVtab);
drhb9bb7c12006-06-11 23:41:55 +0000262}
263static int echoConnect(
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), "xConnect");
270 return echoConstructor(db, pAux, argc, argv, ppVtab);
drhb9bb7c12006-06-11 23:41:55 +0000271}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000272
danielk19775fac9f82006-06-13 14:16:58 +0000273static int echoDisconnect(sqlite3_vtab *pVtab){
274 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
275 return echoDestructor(pVtab);
276}
277static int echoDestroy(sqlite3_vtab *pVtab){
278 appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
279 return echoDestructor(pVtab);
280}
281
282static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000283 echo_cursor *pCur;
284 pCur = sqliteMalloc(sizeof(echo_cursor));
285 *ppCursor = (sqlite3_vtab_cursor *)pCur;
286 return SQLITE_OK;
287}
288
danielk19775fac9f82006-06-13 14:16:58 +0000289static int echoClose(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000290 echo_cursor *pCur = (echo_cursor *)cur;
291 sqlite3_finalize(pCur->pStmt);
292 sqliteFree(pCur);
293 return SQLITE_OK;
294}
295
danielk19775fac9f82006-06-13 14:16:58 +0000296static int echoNext(sqlite3_vtab_cursor *cur){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000297 int rc;
298 echo_cursor *pCur = (echo_cursor *)cur;
299
300 rc = sqlite3_step(pCur->pStmt);
301
302 if( rc==SQLITE_ROW ){
303 rc = 1;
304 } else {
305 pCur->errcode = sqlite3_finalize(pCur->pStmt);
306 pCur->pStmt = 0;
307 rc = 0;
308 }
309
310 return rc;
311}
312
danielk19775fac9f82006-06-13 14:16:58 +0000313static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000314 int iCol = i + 1;
315 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
316
317 assert( sqlite3_data_count(pStmt)>iCol );
drh4be8b512006-06-13 23:51:34 +0000318 sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
danielk1977b7a7b9a2006-06-13 10:24:42 +0000319 return SQLITE_OK;
320}
321
danielk19775fac9f82006-06-13 14:16:58 +0000322static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
danielk1977b7a7b9a2006-06-13 10:24:42 +0000323 sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
324 *pRowid = sqlite3_column_int64(pStmt, 0);
325 return SQLITE_OK;
326}
327
danielk197798331532006-06-14 10:55:52 +0000328/*
329** Compute a simple hash of the null terminated string zString.
330**
331** This module uses only sqlite3_index_info.idxStr, not
332** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
333** in echoBestIndex(), idxNum is set to the corresponding hash value.
334** In echoFilter(), code assert()s that the supplied idxNum value is
335** indeed the hash of the supplied idxStr.
336*/
337static int hashString(const char *zString){
338 int val = 0;
339 int ii;
340 for(ii=0; zString[ii]; ii++){
341 val = (val << 3) + (int)zString[ii];
342 }
343 return val;
344}
345
danielk1977b7a7b9a2006-06-13 10:24:42 +0000346
danielk19775fac9f82006-06-13 14:16:58 +0000347static int echoFilter(
348 sqlite3_vtab_cursor *pVtabCursor,
drh4be8b512006-06-13 23:51:34 +0000349 int idxNum, const char *idxStr,
350 int argc, sqlite3_value **argv
danielk19775fac9f82006-06-13 14:16:58 +0000351){
352 int rc;
drh4be8b512006-06-13 23:51:34 +0000353 int i;
danielk19775fac9f82006-06-13 14:16:58 +0000354
355 echo_cursor *pCur = (echo_cursor *)pVtabCursor;
356 echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
357 sqlite3 *db = pVtab->db;
358
danielk197798331532006-06-14 10:55:52 +0000359 assert( idxNum==hashString(idxStr) );
danielk19775fac9f82006-06-13 14:16:58 +0000360 sqlite3_finalize(pCur->pStmt);
361 pCur->pStmt = 0;
drh4be8b512006-06-13 23:51:34 +0000362 rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
363 for(i=0; i<argc; i++){
364 switch( sqlite3_value_type(argv[i]) ){
365 case SQLITE_INTEGER: {
366 sqlite3_bind_int64(pCur->pStmt, i+1, sqlite3_value_int64(argv[i]));
367 break;
368 }
369 case SQLITE_FLOAT: {
370 sqlite3_bind_double(pCur->pStmt, i+1, sqlite3_value_double(argv[i]));
371 break;
372 }
373 case SQLITE_NULL: {
374 sqlite3_bind_null(pCur->pStmt, i+1);
375 break;
376 }
377 case SQLITE_TEXT: {
378 sqlite3_bind_text(pCur->pStmt, i+1, sqlite3_value_text(argv[i]),
379 sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
380 break;
381 }
382 case SQLITE_BLOB: {
383 sqlite3_bind_blob(pCur->pStmt, i+1, sqlite3_value_blob(argv[i]),
384 sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
385 break;
386 }
387 }
388 }
danielk19775fac9f82006-06-13 14:16:58 +0000389 if( rc==SQLITE_OK ){
390 rc = echoNext(pVtabCursor);
391 }
392
drh4be8b512006-06-13 23:51:34 +0000393 appendToEchoModule(pVtab->interp, "xFilter");
394 appendToEchoModule(pVtab->interp, idxStr);
395 for(i=0; i<argc; i++){
396 appendToEchoModule(pVtab->interp, sqlite3_value_text(argv[i]));
397 }
398
danielk19775fac9f82006-06-13 14:16:58 +0000399 return rc;
400}
danielk1977b7a7b9a2006-06-13 10:24:42 +0000401
drhb9bb7c12006-06-11 23:41:55 +0000402/*
danielk19775fac9f82006-06-13 14:16:58 +0000403** The echo module implements the subset of query constraints and sort
404** orders that may take advantage of SQLite indices on the underlying
405** real table. For example, if the real table is declared as:
406**
407** CREATE TABLE real(a, b, c);
408** CREATE INDEX real_index ON real(b);
409**
410** then the echo module handles WHERE or ORDER BY clauses that refer
411** to the column "b", but not "a" or "c". If a multi-column index is
412** present, only it's left most column is considered.
drha967e882006-06-13 01:04:52 +0000413*/
danielk19775fac9f82006-06-13 14:16:58 +0000414static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
415 int ii;
drh4be8b512006-06-13 23:51:34 +0000416 char *zQuery = 0;
417 char *zNew;
danielk19775fac9f82006-06-13 14:16:58 +0000418 int nArg = 0;
drh4be8b512006-06-13 23:51:34 +0000419 const char *zSep = "WHERE";
danielk19775fac9f82006-06-13 14:16:58 +0000420 echo_vtab *pVtab = (echo_vtab *)tab;
421
drh4be8b512006-06-13 23:51:34 +0000422 zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
danielk19775fac9f82006-06-13 14:16:58 +0000423 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
424 const struct sqlite3_index_constraint *pConstraint;
425 struct sqlite3_index_constraint_usage *pUsage;
426
427 pConstraint = &pIdxInfo->aConstraint[ii];
428 pUsage = &pIdxInfo->aConstraintUsage[ii];
429
430 int iCol = pConstraint->iColumn;
431 if( pVtab->aIndex[iCol] ){
432 char *zCol = pVtab->aCol[iCol];
433 char *zOp = 0;
danielk1977176f4d22006-06-15 10:41:15 +0000434 if( iCol<0 ){
435 zCol = "rowid";
436 }
danielk19775fac9f82006-06-13 14:16:58 +0000437 switch( pConstraint->op ){
438 case SQLITE_INDEX_CONSTRAINT_EQ:
439 zOp = "="; break;
440 case SQLITE_INDEX_CONSTRAINT_LT:
441 zOp = "<"; break;
442 case SQLITE_INDEX_CONSTRAINT_GT:
443 zOp = ">"; break;
444 case SQLITE_INDEX_CONSTRAINT_LE:
445 zOp = "<="; break;
446 case SQLITE_INDEX_CONSTRAINT_GE:
447 zOp = ">="; break;
448 case SQLITE_INDEX_CONSTRAINT_MATCH:
drh1a90e092006-06-14 22:07:10 +0000449 zOp = "LIKE"; break;
danielk19775fac9f82006-06-13 14:16:58 +0000450 }
drh1a90e092006-06-14 22:07:10 +0000451 if( zOp[0]=='L' ){
452 zNew = sqlite3_mprintf("%s %s %s LIKE (SELECT '%%'||?||'%%')",
453 zQuery, zSep, zCol);
454 } else {
455 zNew = sqlite3_mprintf("%s %s %s %s ?", zQuery, zSep, zCol, zOp);
456 }
drh4be8b512006-06-13 23:51:34 +0000457 sqlite3_free(zQuery);
458 zQuery = zNew;
459 zSep = "AND";
danielk19775fac9f82006-06-13 14:16:58 +0000460 pUsage->argvIndex = ++nArg;
461 pUsage->omit = 1;
462 }
463 }
danielk197747d08092006-06-14 07:41:31 +0000464
465 /* If there is only one term in the ORDER BY clause, and it is
466 ** on a column that this virtual table has an index for, then consume
467 ** the ORDER BY clause.
468 */
469 if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
470 char *zCol = pVtab->aCol[pIdxInfo->aOrderBy->iColumn];
471 char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
472 zNew = sqlite3_mprintf("%s ORDER BY %s %s", zQuery, zCol, zDir);
473 sqlite3_free(zQuery);
474 zQuery = zNew;
475 pIdxInfo->orderByConsumed = 1;
476 }
477
danielk19775fac9f82006-06-13 14:16:58 +0000478 appendToEchoModule(pVtab->interp, "xBestIndex");;
drh4be8b512006-06-13 23:51:34 +0000479 appendToEchoModule(pVtab->interp, zQuery);
danielk19775fac9f82006-06-13 14:16:58 +0000480
danielk197798331532006-06-14 10:55:52 +0000481 pIdxInfo->idxNum = hashString(zQuery);
drh4be8b512006-06-13 23:51:34 +0000482 pIdxInfo->idxStr = zQuery;
483 pIdxInfo->needToFreeIdxStr = 1;
484 pIdxInfo->estimatedCost = 1.0;
drha967e882006-06-13 01:04:52 +0000485 return SQLITE_OK;
486}
487
danielk1977176f4d22006-06-15 10:41:15 +0000488static void string_concat(char **pzStr, char *zAppend, int doFree){
489 char *zIn = *pzStr;
490 if( zIn ){
491 char *zTemp = zIn;
492 zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
493 sqlite3_free(zTemp);
494 }else{
495 zIn = sqlite3_mprintf("%s", zAppend);
496 }
497 *pzStr = zIn;
498 if( doFree ){
499 sqlite3_free(zAppend);
500 }
501}
502
503/*
504** apData[0] apData[1] apData[2..]
505**
506** INTEGER DELETE
507**
508** INTEGER NULL (nCol args) UPDATE (do not set rowid)
509** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
510**
511** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
512** NULL INTEGER (nCol args) INSERT (incl. rowid value)
513**
514*/
danielk197726e41442006-06-14 15:16:35 +0000515int echoUpdate(sqlite3_vtab *tab, int nData, sqlite3_value **apData){
516 echo_vtab *pVtab = (echo_vtab *)tab;
517 sqlite3 *db = pVtab->db;
518 int rc = SQLITE_OK;
519
danielk1977176f4d22006-06-15 10:41:15 +0000520 sqlite3_stmt *pStmt;
521 char *z = 0; /* SQL statement to execute */
522 int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
523 int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
524 int i; /* Counter variable used by for loops */
525
danielk197726e41442006-06-14 15:16:35 +0000526 assert( nData==pVtab->nCol+2 || nData==1 );
527
drh5aec0422006-06-14 23:43:31 +0000528 /* If apData[0] is an integer and nData>1 then do an UPDATE */
529 if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
danielk1977176f4d22006-06-15 10:41:15 +0000530 z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
drh5aec0422006-06-14 23:43:31 +0000531 char *zSep = " SET";
danielk1977176f4d22006-06-15 10:41:15 +0000532
533 bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
534 bindArgZero = 1;
535
536 if( bindArgOne ){
537 string_concat(&z, " SET rowid=?1 ", 0);
drh5aec0422006-06-14 23:43:31 +0000538 zSep = ",";
539 }
540 for(i=2; i<nData; i++){
541 if( apData[i]==0 ) continue;
danielk1977176f4d22006-06-15 10:41:15 +0000542 string_concat(&z, sqlite3_mprintf(
543 "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
drh5aec0422006-06-14 23:43:31 +0000544 zSep = ",";
545 }
danielk1977176f4d22006-06-15 10:41:15 +0000546 string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
drh5aec0422006-06-14 23:43:31 +0000547 }
548
danielk1977176f4d22006-06-15 10:41:15 +0000549 /* If apData[0] is an integer and nData==1 then do a DELETE */
550 else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
551 z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
552 bindArgZero = 1;
danielk197726e41442006-06-14 15:16:35 +0000553 }
554
danielk1977176f4d22006-06-15 10:41:15 +0000555 /* If the first argument is NULL and there are more than two args, INSERT */
556 else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
danielk197726e41442006-06-14 15:16:35 +0000557 int ii;
558 char *zInsert = 0;
559 char *zValues = 0;
danielk197726e41442006-06-14 15:16:35 +0000560
danielk1977176f4d22006-06-15 10:41:15 +0000561 zInsert = sqlite3_mprintf("INSERT OR REPLACE INTO %Q (", pVtab->zTableName);
562 if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
563 bindArgOne = 1;
564 zValues = sqlite3_mprintf("?");
565 string_concat(&zInsert, "rowid", 0);
danielk197726e41442006-06-14 15:16:35 +0000566 }
567
danielk1977176f4d22006-06-15 10:41:15 +0000568 assert((pVtab->nCol+2)==nData);
569 for(ii=2; ii<nData; ii++){
570 string_concat(&zInsert,
571 sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
572 string_concat(&zValues,
573 sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
danielk197726e41442006-06-14 15:16:35 +0000574 }
575
danielk1977176f4d22006-06-15 10:41:15 +0000576 string_concat(&z, zInsert, 1);
577 string_concat(&z, ") VALUES(", 0);
578 string_concat(&z, zValues, 1);
579 string_concat(&z, ")", 0);
580 }
581
582 /* Anything else is an error */
583 else{
584 assert(0);
585 return SQLITE_ERROR;
586 }
587
588 rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
589 assert( rc!=SQLITE_OK || pStmt );
590 sqlite3_free(z);
591 if( rc==SQLITE_OK ) {
592 if( bindArgZero ){
593 sqlite3_bind_value(pStmt, nData, apData[0]);
danielk197726e41442006-06-14 15:16:35 +0000594 }
danielk1977176f4d22006-06-15 10:41:15 +0000595 if( bindArgOne ){
596 sqlite3_bind_value(pStmt, 1, apData[1]);
597 }
598 for(i=2; i<nData; i++){
599 if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
600 }
601 sqlite3_step(pStmt);
602 rc = sqlite3_finalize(pStmt);
danielk197726e41442006-06-14 15:16:35 +0000603 }
604
605 return rc;
606}
607
drha967e882006-06-13 01:04:52 +0000608/*
drhb9bb7c12006-06-11 23:41:55 +0000609** A virtual table module that merely echos method calls into TCL
610** variables.
611*/
612static sqlite3_module echoModule = {
danielk197778efaba2006-06-12 06:09:17 +0000613 0, /* iVersion */
614 "echo", /* zName */
drhb9bb7c12006-06-11 23:41:55 +0000615 echoCreate,
616 echoConnect,
drha967e882006-06-13 01:04:52 +0000617 echoBestIndex,
drhb9bb7c12006-06-11 23:41:55 +0000618 echoDisconnect,
619 echoDestroy,
danielk1977b7a7b9a2006-06-13 10:24:42 +0000620 echoOpen, /* xOpen - open a cursor */
621 echoClose, /* xClose - close a cursor */
622 echoFilter, /* xFilter - configure scan constraints */
623 echoNext, /* xNext - advance a cursor */
624 echoColumn, /* xColumn - read data */
danielk197726e41442006-06-14 15:16:35 +0000625 echoRowid, /* xRowid - read data */
626 echoUpdate /* xUpdate - write data */
drhb9bb7c12006-06-11 23:41:55 +0000627};
628
629/*
630** Decode a pointer to an sqlite3 object.
631*/
632static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
633 *ppDb = (sqlite3*)sqlite3TextToPtr(zA);
634 return TCL_OK;
635}
636
637
638/*
639** Register the echo virtual table module.
640*/
641static int register_echo_module(
642 ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
643 Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
644 int objc, /* Number of arguments */
645 Tcl_Obj *CONST objv[] /* Command arguments */
646){
647 sqlite3 *db;
648 if( objc!=2 ){
649 Tcl_WrongNumArgs(interp, 1, objv, "DB");
650 return TCL_ERROR;
651 }
652 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
drhb9bb7c12006-06-11 23:41:55 +0000653#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:13 +0000654 sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
drhb9bb7c12006-06-11 23:41:55 +0000655#endif
656 return TCL_OK;
657}
658
659
660/*
661** Register commands with the TCL interpreter.
662*/
663int Sqlitetest8_Init(Tcl_Interp *interp){
664 static struct {
665 char *zName;
666 Tcl_ObjCmdProc *xProc;
667 void *clientData;
668 } aObjCmd[] = {
669 { "register_echo_module", register_echo_module, 0 },
670 };
671 int i;
672 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
673 Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
674 aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
675 }
676 return TCL_OK;
677}