4
** The author disclaims copyright to this source code. In place of
5
** a legal notice, here is a blessing:
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.
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.
16
** $Id: test8.c,v 1.43 2006/10/08 18:56:57 drh Exp $
18
#include "sqliteInt.h"
24
#ifndef SQLITE_OMIT_VIRTUALTABLE
26
typedef struct echo_vtab echo_vtab;
27
typedef struct echo_cursor echo_cursor;
30
** The test module defined in this file uses two global Tcl variables to
31
** commicate with test-scripts:
34
** $::echo_module_sync_fail
35
** $::echo_module_begin_fail
37
** The variable ::echo_module is a list. Each time one of the following
38
** methods is called, one or more elements are appended to the list.
39
** This is used for automated testing of virtual table modules.
41
** The ::echo_module_sync_fail variable is set by test scripts and read
42
** by code in this file. If it is set to the name of a real table in the
43
** the database, then all xSync operations on echo virtual tables that
44
** use the named table as a backing store will fail.
48
** An echo virtual-table object.
50
** echo.vtab.aIndex is an array of booleans. The nth entry is true if
51
** the nth column of the real table is the left-most column of an index
52
** (implicit or otherwise). In other words, if SQLite can optimize
53
** a query like "SELECT * FROM real_table WHERE col = ?".
55
** Member variable aCol[] contains copies of the column names of the real
60
Tcl_Interp *interp; /* Tcl interpreter containing debug variables */
61
sqlite3 *db; /* Database connection */
63
char *zTableName; /* Name of the real table */
64
char *zLogName; /* Name of the log table */
65
int nCol; /* Number of columns in the real table */
66
int *aIndex; /* Array of size nCol. True if column has an index */
67
char **aCol; /* Array of size nCol. Column names */
70
/* An echo cursor object */
72
sqlite3_vtab_cursor base;
77
** Retrieve the column names for the table named zTab via database
78
** connection db. SQLITE_OK is returned on success, or an sqlite error
81
** If successful, the number of columns is written to *pnCol. *paCol is
82
** set to point at sqliteMalloc()'d space containing the array of
83
** nCol column names. The caller is responsible for calling sqliteFree
86
static int getColumnNames(
94
sqlite3_stmt *pStmt = 0;
98
/* Prepare the statement "SELECT * FROM <tbl>". The column names
99
** of the result set of the compiled SELECT will be the same as
100
** the column names of table <tbl>.
102
zSql = sqlite3MPrintf("SELECT * FROM %Q", zTab);
107
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
114
nCol = sqlite3_column_count(pStmt);
116
/* Figure out how much space to allocate for the array of column names
117
** (including space for the strings themselves). Then allocate it.
119
nBytes = sizeof(char *) * nCol;
120
for(ii=0; ii<nCol; ii++){
121
nBytes += (strlen(sqlite3_column_name(pStmt, ii)) + 1);
123
aCol = (char **)sqliteMalloc(nBytes);
129
/* Copy the column names into the allocated space and set up the
130
** pointers in the aCol[] array.
132
zSpace = (char *)(&aCol[nCol]);
133
for(ii=0; ii<nCol; ii++){
135
zSpace += sprintf(zSpace, "%s", sqlite3_column_name(pStmt, ii));
138
assert( (zSpace-nBytes)==(char *)aCol );
145
sqlite3_finalize(pStmt);
150
** Parameter zTab is the name of a table in database db with nCol
151
** columns. This function allocates an array of integers nCol in
152
** size and populates it according to any implicit or explicit
153
** indices on table zTab.
155
** If successful, SQLITE_OK is returned and *paIndex set to point
156
** at the allocated array. Otherwise, an error code is returned.
158
** See comments associated with the member variable aIndex above
159
** "struct echo_vtab" for details of the contents of the array.
161
static int getIndexArray(
162
sqlite3 *db, /* Database connection */
163
const char *zTab, /* Name of table in database db */
167
sqlite3_stmt *pStmt = 0;
172
/* Allocate space for the index array */
173
aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
176
goto get_index_array_out;
179
/* Compile an sqlite pragma to loop through all indices on table zTab */
180
zSql = sqlite3MPrintf("PRAGMA index_list(%s)", zTab);
183
goto get_index_array_out;
185
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
188
/* For each index, figure out the left-most column and set the
189
** corresponding entry in aIndex[] to 1.
191
while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
192
const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
193
sqlite3_stmt *pStmt2 = 0;
194
zSql = sqlite3MPrintf("PRAGMA index_info(%s)", zIdx);
197
goto get_index_array_out;
199
rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
201
if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
202
int cid = sqlite3_column_int(pStmt2, 1);
203
assert( cid>=0 && cid<nCol );
207
rc = sqlite3_finalize(pStmt2);
210
goto get_index_array_out;
217
int rc2 = sqlite3_finalize(pStmt);
231
** Global Tcl variable $echo_module is a list. This routine appends
232
** the string element zArg to that list in interpreter interp.
234
static void appendToEchoModule(Tcl_Interp *interp, const char *zArg){
235
int flags = (TCL_APPEND_VALUE | TCL_LIST_ELEMENT | TCL_GLOBAL_ONLY);
236
Tcl_SetVar(interp, "echo_module", (zArg?zArg:""), flags);
240
** This function is called from within the echo-modules xCreate and
241
** xConnect methods. The argc and argv arguments are copies of those
242
** passed to the calling method. This function is responsible for
243
** calling sqlite3_declare_vtab() to declare the schema of the virtual
244
** table being created or connected.
246
** If the constructor was passed just one argument, i.e.:
248
** CREATE TABLE t1 AS echo(t2);
250
** Then t2 is assumed to be the name of a *real* database table. The
251
** schema of the virtual table is declared by passing a copy of the
252
** CREATE TABLE statement for the real table to sqlite3_declare_vtab().
253
** Hence, the virtual table should have exactly the same column names and
254
** types as the real table.
256
static int echoDeclareVtab(
260
const char *const*argv
265
sqlite3_stmt *pStmt = 0;
267
"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?",
269
sqlite3_bind_text(pStmt, 1, argv[3], -1, 0);
270
if( sqlite3_step(pStmt)==SQLITE_ROW ){
271
const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
272
sqlite3_declare_vtab(db, zCreateTable);
273
rc = sqlite3_finalize(pStmt);
275
rc = sqlite3_finalize(pStmt);
282
rc = getColumnNames(db, argv[3], &pVtab->aCol, &pVtab->nCol);
285
rc = getIndexArray(db, argv[3], pVtab->nCol, &pVtab->aIndex);
293
** This function frees all runtime structures associated with the virtual
296
static int echoDestructor(sqlite3_vtab *pVtab){
297
echo_vtab *p = (echo_vtab*)pVtab;
298
sqliteFree(p->aIndex);
300
sqliteFree(p->zTableName);
301
sqliteFree(p->zLogName);
307
** This function is called to do the work of the xConnect() method -
308
** to allocate the required in-memory structures for a newly connected
311
static int echoConstructor(
314
int argc, const char *const*argv,
315
sqlite3_vtab **ppVtab,
321
/* Allocate the sqlite3_vtab/echo_vtab structure itself */
322
pVtab = sqliteMalloc( sizeof(*pVtab) );
326
pVtab->interp = (Tcl_Interp *)pAux;
329
/* Allocate echo_vtab.zTableName */
330
pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
331
if( !pVtab->zTableName ){
332
echoDestructor((sqlite3_vtab *)pVtab);
336
/* Log the arguments to this function to Tcl var ::echo_module */
337
for(i=0; i<argc; i++){
338
appendToEchoModule(pVtab->interp, argv[i]);
341
/* Invoke sqlite3_declare_vtab and set up other members of the echo_vtab
342
** structure. If an error occurs, delete the sqlite3_vtab structure and
343
** return an error code.
345
if( echoDeclareVtab(pVtab, db, argc, argv) ){
346
echoDestructor((sqlite3_vtab *)pVtab);
350
/* Success. Set *ppVtab and return */
351
*ppVtab = &pVtab->base;
356
** Echo virtual table module xCreate method.
358
static int echoCreate(
361
int argc, const char *const*argv,
362
sqlite3_vtab **ppVtab,
366
appendToEchoModule((Tcl_Interp *)(pAux), "xCreate");
367
rc = echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
369
/* If there were two arguments passed to the module at the SQL level
370
** (i.e. "CREATE VIRTUAL TABLE tbl USING echo(arg1, arg2)"), then
371
** the second argument is used as a table name. Attempt to create
372
** such a table with a single column, "logmsg". This table will
373
** be used to log calls to the xUpdate method. It will be deleted
374
** when the virtual table is DROPed.
376
** Note: The main point of this is to test that we can drop tables
377
** from within an xDestroy method call.
379
if( rc==SQLITE_OK && argc==5 ){
381
echo_vtab *pVtab = *(echo_vtab **)ppVtab;
382
pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
383
zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
384
rc = sqlite3_exec(db, zSql, 0, 0, 0);
392
** Echo virtual table module xConnect method.
394
static int echoConnect(
397
int argc, const char *const*argv,
398
sqlite3_vtab **ppVtab,
401
appendToEchoModule((Tcl_Interp *)(pAux), "xConnect");
402
return echoConstructor(db, pAux, argc, argv, ppVtab, pzErr);
406
** Echo virtual table module xDisconnect method.
408
static int echoDisconnect(sqlite3_vtab *pVtab){
409
appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDisconnect");
410
return echoDestructor(pVtab);
414
** Echo virtual table module xDestroy method.
416
static int echoDestroy(sqlite3_vtab *pVtab){
418
echo_vtab *p = (echo_vtab *)pVtab;
419
appendToEchoModule(((echo_vtab *)pVtab)->interp, "xDestroy");
421
/* Drop the "log" table, if one exists (see echoCreate() for details) */
422
if( p && p->zLogName ){
424
zSql = sqlite3MPrintf("DROP TABLE %Q", p->zLogName);
425
rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
430
rc = echoDestructor(pVtab);
436
** Echo virtual table module xOpen method.
438
static int echoOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
440
pCur = sqliteMalloc(sizeof(echo_cursor));
441
*ppCursor = (sqlite3_vtab_cursor *)pCur;
442
return (pCur ? SQLITE_OK : SQLITE_NOMEM);
446
** Echo virtual table module xClose method.
448
static int echoClose(sqlite3_vtab_cursor *cur){
450
echo_cursor *pCur = (echo_cursor *)cur;
451
sqlite3_stmt *pStmt = pCur->pStmt;
454
rc = sqlite3_finalize(pStmt);
459
** Return non-zero if the cursor does not currently point to a valid record
460
** (i.e if the scan has finished), or zero otherwise.
462
static int echoEof(sqlite3_vtab_cursor *cur){
463
return (((echo_cursor *)cur)->pStmt ? 0 : 1);
467
** Echo virtual table module xNext method.
469
static int echoNext(sqlite3_vtab_cursor *cur){
471
echo_cursor *pCur = (echo_cursor *)cur;
472
rc = sqlite3_step(pCur->pStmt);
474
if( rc==SQLITE_ROW ){
477
rc = sqlite3_finalize(pCur->pStmt);
485
** Echo virtual table module xColumn method.
487
static int echoColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
489
sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
491
sqlite3_result_null(ctx);
493
assert( sqlite3_data_count(pStmt)>iCol );
494
sqlite3_result_value(ctx, sqlite3_column_value(pStmt, iCol));
500
** Echo virtual table module xRowid method.
502
static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
503
sqlite3_stmt *pStmt = ((echo_cursor *)cur)->pStmt;
504
*pRowid = sqlite3_column_int64(pStmt, 0);
509
** Compute a simple hash of the null terminated string zString.
511
** This module uses only sqlite3_index_info.idxStr, not
512
** sqlite3_index_info.idxNum. So to test idxNum, when idxStr is set
513
** in echoBestIndex(), idxNum is set to the corresponding hash value.
514
** In echoFilter(), code assert()s that the supplied idxNum value is
515
** indeed the hash of the supplied idxStr.
517
static int hashString(const char *zString){
520
for(ii=0; zString[ii]; ii++){
521
val = (val << 3) + (int)zString[ii];
527
** Echo virtual table module xFilter method.
529
static int echoFilter(
530
sqlite3_vtab_cursor *pVtabCursor,
531
int idxNum, const char *idxStr,
532
int argc, sqlite3_value **argv
537
echo_cursor *pCur = (echo_cursor *)pVtabCursor;
538
echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab;
539
sqlite3 *db = pVtab->db;
541
/* Check that idxNum matches idxStr */
542
assert( idxNum==hashString(idxStr) );
544
/* Log arguments to the ::echo_module Tcl variable */
545
appendToEchoModule(pVtab->interp, "xFilter");
546
appendToEchoModule(pVtab->interp, idxStr);
547
for(i=0; i<argc; i++){
548
appendToEchoModule(pVtab->interp, (const char*)sqlite3_value_text(argv[i]));
551
sqlite3_finalize(pCur->pStmt);
554
/* Prepare the SQL statement created by echoBestIndex and bind the
555
** runtime parameters passed to this function to it.
557
rc = sqlite3_prepare(db, idxStr, -1, &pCur->pStmt, 0);
558
assert( pCur->pStmt || rc!=SQLITE_OK );
559
for(i=0; rc==SQLITE_OK && i<argc; i++){
560
sqlite3_bind_value(pCur->pStmt, i+1, argv[i]);
563
/* If everything was successful, advance to the first row of the scan */
565
rc = echoNext(pVtabCursor);
573
** A helper function used by echoUpdate() and echoBestIndex() for
574
** manipulating strings in concert with the sqlite3_mprintf() function.
576
** Parameter pzStr points to a pointer to a string allocated with
577
** sqlite3_mprintf. The second parameter, zAppend, points to another
578
** string. The two strings are concatenated together and *pzStr
579
** set to point at the result. The initial buffer pointed to by *pzStr
580
** is deallocated via sqlite3_free().
582
** If the third argument, doFree, is true, then sqlite3_free() is
583
** also called to free the buffer pointed to by zAppend.
585
static void string_concat(char **pzStr, char *zAppend, int doFree){
589
zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
592
zIn = sqlite3_mprintf("%s", zAppend);
596
sqlite3_free(zAppend);
601
** The echo module implements the subset of query constraints and sort
602
** orders that may take advantage of SQLite indices on the underlying
603
** real table. For example, if the real table is declared as:
605
** CREATE TABLE real(a, b, c);
606
** CREATE INDEX real_index ON real(b);
608
** then the echo module handles WHERE or ORDER BY clauses that refer
609
** to the column "b", but not "a" or "c". If a multi-column index is
610
** present, only it's left most column is considered.
612
** This xBestIndex method encodes the proposed search strategy as
613
** an SQL query on the real table underlying the virtual echo module
614
** table and stores the query in sqlite3_index_info.idxStr. The SQL
615
** statement is of the form:
617
** SELECT rowid, * FROM <real-table> ?<where-clause>? ?<order-by-clause>?
619
** where the <where-clause> and <order-by-clause> are determined
620
** by the contents of the structure pointed to by the pIdxInfo argument.
622
static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
627
const char *zSep = "WHERE";
628
echo_vtab *pVtab = (echo_vtab *)tab;
629
sqlite3_stmt *pStmt = 0;
635
/* Determine the number of rows in the table and store this value in local
636
** variable nRow. The 'estimated-cost' of the scan will be the number of
637
** rows in the table for a linear scan, or the log (base 2) of the
638
** number of rows if the proposed scan uses an index.
640
zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
641
rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
646
nRow = sqlite3_column_int(pStmt, 0);
647
rc = sqlite3_finalize(pStmt);
652
zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
653
for(ii=0; ii<pIdxInfo->nConstraint; ii++){
654
const struct sqlite3_index_constraint *pConstraint;
655
struct sqlite3_index_constraint_usage *pUsage;
658
pConstraint = &pIdxInfo->aConstraint[ii];
659
pUsage = &pIdxInfo->aConstraintUsage[ii];
661
iCol = pConstraint->iColumn;
662
if( pVtab->aIndex[iCol] ){
663
char *zCol = pVtab->aCol[iCol];
669
switch( pConstraint->op ){
670
case SQLITE_INDEX_CONSTRAINT_EQ:
672
case SQLITE_INDEX_CONSTRAINT_LT:
674
case SQLITE_INDEX_CONSTRAINT_GT:
676
case SQLITE_INDEX_CONSTRAINT_LE:
678
case SQLITE_INDEX_CONSTRAINT_GE:
680
case SQLITE_INDEX_CONSTRAINT_MATCH:
684
zNew = sqlite3_mprintf(" %s %s LIKE (SELECT '%%'||?||'%%')",
687
zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp);
689
string_concat(&zQuery, zNew, 1);
692
pUsage->argvIndex = ++nArg;
697
/* If there is only one term in the ORDER BY clause, and it is
698
** on a column that this virtual table has an index for, then consume
699
** the ORDER BY clause.
701
if( pIdxInfo->nOrderBy==1 && pVtab->aIndex[pIdxInfo->aOrderBy->iColumn] ){
702
int iCol = pIdxInfo->aOrderBy->iColumn;
703
char *zCol = pVtab->aCol[iCol];
704
char *zDir = pIdxInfo->aOrderBy->desc?"DESC":"ASC";
708
zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
709
string_concat(&zQuery, zNew, 1);
710
pIdxInfo->orderByConsumed = 1;
713
appendToEchoModule(pVtab->interp, "xBestIndex");;
714
appendToEchoModule(pVtab->interp, zQuery);
716
pIdxInfo->idxNum = hashString(zQuery);
717
pIdxInfo->idxStr = zQuery;
718
pIdxInfo->needToFreeIdxStr = 1;
720
/* Approximation of log2(nRow). */
721
for( ii=0; ii<(sizeof(int)*8); ii++ ){
722
if( nRow & (1<<ii) ){
723
pIdxInfo->estimatedCost = (double)ii;
727
pIdxInfo->estimatedCost = (double)nRow;
733
** The xUpdate method for echo module virtual tables.
735
** apData[0] apData[1] apData[2..]
739
** INTEGER NULL (nCol args) UPDATE (do not set rowid)
740
** INTEGER INTEGER (nCol args) UPDATE (with SET rowid = <arg1>)
742
** NULL NULL (nCol args) INSERT INTO (automatic rowid value)
743
** NULL INTEGER (nCol args) INSERT (incl. rowid value)
749
sqlite3_value **apData,
752
echo_vtab *pVtab = (echo_vtab *)tab;
753
sqlite3 *db = pVtab->db;
757
char *z = 0; /* SQL statement to execute */
758
int bindArgZero = 0; /* True to bind apData[0] to sql var no. nData */
759
int bindArgOne = 0; /* True to bind apData[1] to sql var no. 1 */
760
int i; /* Counter variable used by for loops */
762
assert( nData==pVtab->nCol+2 || nData==1 );
764
/* If apData[0] is an integer and nData>1 then do an UPDATE */
765
if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
767
z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
769
bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
773
string_concat(&z, " SET rowid=?1 ", 0);
776
for(i=2; i<nData; i++){
777
if( apData[i]==0 ) continue;
778
string_concat(&z, sqlite3_mprintf(
779
"%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
782
string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
785
/* If apData[0] is an integer and nData==1 then do a DELETE */
786
else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
787
z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
791
/* If the first argument is NULL and there are more than two args, INSERT */
792
else if( nData>2 && sqlite3_value_type(apData[0])==SQLITE_NULL ){
797
zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
798
if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
800
zValues = sqlite3_mprintf("?");
801
string_concat(&zInsert, "rowid", 0);
804
assert((pVtab->nCol+2)==nData);
805
for(ii=2; ii<nData; ii++){
806
string_concat(&zInsert,
807
sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
808
string_concat(&zValues,
809
sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
812
string_concat(&z, zInsert, 1);
813
string_concat(&z, ") VALUES(", 0);
814
string_concat(&z, zValues, 1);
815
string_concat(&z, ")", 0);
818
/* Anything else is an error */
824
rc = sqlite3_prepare(db, z, -1, &pStmt, 0);
825
assert( rc!=SQLITE_OK || pStmt );
827
if( rc==SQLITE_OK ) {
829
sqlite3_bind_value(pStmt, nData, apData[0]);
832
sqlite3_bind_value(pStmt, 1, apData[1]);
834
for(i=2; i<nData; i++){
835
if( apData[i] ) sqlite3_bind_value(pStmt, i, apData[i]);
838
rc = sqlite3_finalize(pStmt);
841
if( pRowid && rc==SQLITE_OK ){
842
*pRowid = sqlite3_last_insert_rowid(db);
849
** xBegin, xSync, xCommit and xRollback callbacks for echo module
850
** virtual tables. Do nothing other than add the name of the callback
851
** to the $::echo_module Tcl variable.
853
static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){
855
echo_vtab *pVtab = (echo_vtab *)tab;
856
z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
857
appendToEchoModule(pVtab->interp, zCall);
858
appendToEchoModule(pVtab->interp, z);
862
static int echoBegin(sqlite3_vtab *tab){
863
echo_vtab *pVtab = (echo_vtab *)tab;
864
Tcl_Interp *interp = pVtab->interp;
867
echoTransactionCall(tab, "xBegin");
869
/* Check if the $::echo_module_begin_fail variable is defined. If it is,
870
** and it is set to the name of the real table underlying this virtual
871
** echo module table, then cause this xSync operation to fail.
873
zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
874
if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
879
static int echoSync(sqlite3_vtab *tab){
880
echo_vtab *pVtab = (echo_vtab *)tab;
881
Tcl_Interp *interp = pVtab->interp;
884
echoTransactionCall(tab, "xSync");
886
/* Check if the $::echo_module_sync_fail variable is defined. If it is,
887
** and it is set to the name of the real table underlying this virtual
888
** echo module table, then cause this xSync operation to fail.
890
zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
891
if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
896
static int echoCommit(sqlite3_vtab *tab){
897
return echoTransactionCall(tab, "xCommit");
899
static int echoRollback(sqlite3_vtab *tab){
900
return echoTransactionCall(tab, "xRollback");
904
** Implementation of "GLOB" function on the echo module. Pass
905
** all arguments to the ::echo_glob_overload procedure of TCL
906
** and return the result of that procedure as a string.
908
static void overloadedGlobFunction(
909
sqlite3_context *pContext,
911
sqlite3_value **apArg
913
Tcl_Interp *interp = sqlite3_user_data(pContext);
917
Tcl_DStringInit(&str);
918
Tcl_DStringAppendElement(&str, "::echo_glob_overload");
919
for(i=0; i<nArg; i++){
920
Tcl_DStringAppendElement(&str, (char*)sqlite3_value_text(apArg[i]));
922
rc = Tcl_Eval(interp, Tcl_DStringValue(&str));
923
Tcl_DStringFree(&str);
925
sqlite3_result_error(pContext, Tcl_GetStringResult(interp), -1);
927
sqlite3_result_text(pContext, Tcl_GetStringResult(interp),
928
-1, SQLITE_TRANSIENT);
930
Tcl_ResetResult(interp);
934
** This is the xFindFunction implementation for the echo module.
935
** SQLite calls this routine when the first argument of a function
936
** is a column of an echo virtual table. This routine can optionally
937
** override the implementation of that function. It will choose to
938
** do so if the function is named "glob", and a TCL command named
939
** ::echo_glob_overload exists.
941
static int echoFindFunction(
944
const char *zFuncName,
945
void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
948
echo_vtab *pVtab = (echo_vtab *)vtab;
949
Tcl_Interp *interp = pVtab->interp;
951
if( strcmp(zFuncName,"glob")!=0 ){
954
if( Tcl_GetCommandInfo(interp, "::echo_glob_overload", &info)==0 ){
957
*pxFunc = overloadedGlobFunction;
963
** A virtual table module that merely "echos" the contents of another
964
** table (like an SQL VIEW).
966
static sqlite3_module echoModule = {
973
echoOpen, /* xOpen - open a cursor */
974
echoClose, /* xClose - close a cursor */
975
echoFilter, /* xFilter - configure scan constraints */
976
echoNext, /* xNext - advance a cursor */
978
echoColumn, /* xColumn - read data */
979
echoRowid, /* xRowid - read data */
980
echoUpdate, /* xUpdate - write data */
981
echoBegin, /* xBegin - begin transaction */
982
echoSync, /* xSync - sync transaction */
983
echoCommit, /* xCommit - commit transaction */
984
echoRollback, /* xRollback - rollback transaction */
985
echoFindFunction, /* xFindFunction - function overloading */
989
** Decode a pointer to an sqlite3 object.
991
static int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb){
992
*ppDb = (sqlite3*)sqlite3TextToPtr(zA);
997
** Register the echo virtual table module.
999
static int register_echo_module(
1000
ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1001
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1002
int objc, /* Number of arguments */
1003
Tcl_Obj *CONST objv[] /* Command arguments */
1007
Tcl_WrongNumArgs(interp, 1, objv, "DB");
1010
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1011
sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
1016
** Tcl interface to sqlite3_declare_vtab, invoked as follows from Tcl:
1018
** sqlite3_declare_vtab DB SQL
1020
static int declare_vtab(
1021
ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
1022
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
1023
int objc, /* Number of arguments */
1024
Tcl_Obj *CONST objv[] /* Command arguments */
1029
Tcl_WrongNumArgs(interp, 1, objv, "DB SQL");
1032
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1033
rc = sqlite3_declare_vtab(db, Tcl_GetString(objv[2]));
1034
if( rc!=SQLITE_OK ){
1035
Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
1041
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
1044
** Register commands with the TCL interpreter.
1046
int Sqlitetest8_Init(Tcl_Interp *interp){
1049
Tcl_ObjCmdProc *xProc;
1052
#ifndef SQLITE_OMIT_VIRTUALTABLE
1053
{ "register_echo_module", register_echo_module, 0 },
1054
{ "sqlite3_declare_vtab", declare_vtab, 0 },
1058
for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
1059
Tcl_CreateObjCommand(interp, aObjCmd[i].zName,
1060
aObjCmd[i].xProc, aObjCmd[i].clientData, 0);