80
** Convert an SQL-style quoted string into a normal string by removing
81
** the quote characters. The conversion is done in-place. If the
82
** input does not begin with a quote character, then this routine
92
static void dequoteString(char *z){
100
case '`': break; /* For MySQL compatibility */
101
case '[': quote = ']'; break; /* For MS SqlServer compatibility */
104
for(i=1, j=0; z[i]; i++){
77
120
** Retrieve the column names for the table named zTab via database
78
121
** connection db. SQLITE_OK is returned on success, or an sqlite error
79
122
** code otherwise.
81
124
** 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
125
** set to point at sqlite3_malloc()'d space containing the array of
126
** nCol column names. The caller is responsible for calling sqlite3_free
86
129
static int getColumnNames(
172
220
/* Allocate space for the index array */
173
aIndex = (int *)sqliteMalloc(sizeof(int) * nCol);
221
aIndex = (int *)sqlite3MallocZero(sizeof(int) * nCol);
175
223
rc = SQLITE_NOMEM;
176
224
goto get_index_array_out;
179
227
/* Compile an sqlite pragma to loop through all indices on table zTab */
180
zSql = sqlite3MPrintf("PRAGMA index_list(%s)", zTab);
228
zSql = sqlite3MPrintf(0, "PRAGMA index_list(%s)", zTab);
182
230
rc = SQLITE_NOMEM;
183
231
goto get_index_array_out;
185
233
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
188
236
/* For each index, figure out the left-most column and set the
189
237
** corresponding entry in aIndex[] to 1.
191
239
while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
192
240
const char *zIdx = (const char *)sqlite3_column_text(pStmt, 1);
193
241
sqlite3_stmt *pStmt2 = 0;
194
zSql = sqlite3MPrintf("PRAGMA index_info(%s)", zIdx);
242
zSql = sqlite3MPrintf(0, "PRAGMA index_info(%s)", zIdx);
196
244
rc = SQLITE_NOMEM;
197
245
goto get_index_array_out;
199
247
rc = sqlite3_prepare(db, zSql, -1, &pStmt2, 0);
201
249
if( pStmt2 && sqlite3_step(pStmt2)==SQLITE_ROW ){
202
250
int cid = sqlite3_column_int(pStmt2, 1);
203
251
assert( cid>=0 && cid<nCol );
256
304
static int echoDeclareVtab(
257
305
echo_vtab *pVtab,
260
const char *const*argv
262
308
int rc = SQLITE_OK;
310
if( pVtab->zTableName ){
265
311
sqlite3_stmt *pStmt = 0;
312
rc = sqlite3_prepare(db,
267
313
"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);
316
sqlite3_bind_text(pStmt, 1, pVtab->zTableName, -1, 0);
317
if( sqlite3_step(pStmt)==SQLITE_ROW ){
319
const char *zCreateTable = (const char *)sqlite3_column_text(pStmt, 0);
320
rc = sqlite3_declare_vtab(db, zCreateTable);
321
rc2 = sqlite3_finalize(pStmt);
326
rc = sqlite3_finalize(pStmt);
332
rc = getColumnNames(db, pVtab->zTableName, &pVtab->aCol, &pVtab->nCol);
335
rc = getIndexArray(db, pVtab->zTableName, pVtab->nCol, &pVtab->aIndex);
315
372
sqlite3_vtab **ppVtab,
319
377
echo_vtab *pVtab;
321
379
/* Allocate the sqlite3_vtab/echo_vtab structure itself */
322
pVtab = sqliteMalloc( sizeof(*pVtab) );
380
pVtab = sqlite3MallocZero( sizeof(*pVtab) );
324
382
return SQLITE_NOMEM;
326
pVtab->interp = (Tcl_Interp *)pAux;
384
pVtab->interp = ((EchoModule *)pAux)->interp;
387
/* Allocate echo_vtab.zThis */
388
pVtab->zThis = sqlite3MPrintf(0, "%s", argv[2]);
390
echoDestructor((sqlite3_vtab *)pVtab);
329
394
/* Allocate echo_vtab.zTableName */
330
pVtab->zTableName = sqlite3MPrintf("%s", argv[3]);
331
if( !pVtab->zTableName ){
332
echoDestructor((sqlite3_vtab *)pVtab);
396
pVtab->zTableName = sqlite3MPrintf(0, "%s", argv[3]);
397
dequoteString(pVtab->zTableName);
398
if( pVtab->zTableName && pVtab->zTableName[0]=='*' ){
399
char *z = sqlite3MPrintf(0, "%s%s", argv[2], &(pVtab->zTableName[1]));
400
sqlite3_free(pVtab->zTableName);
401
pVtab->zTableName = z;
402
pVtab->isPattern = 1;
404
if( !pVtab->zTableName ){
405
echoDestructor((sqlite3_vtab *)pVtab);
336
410
/* Log the arguments to this function to Tcl var ::echo_module */
379
454
if( rc==SQLITE_OK && argc==5 ){
381
456
echo_vtab *pVtab = *(echo_vtab **)ppVtab;
382
pVtab->zLogName = sqlite3MPrintf("%s", argv[4]);
383
zSql = sqlite3MPrintf("CREATE TABLE %Q(logmsg)", pVtab->zLogName);
457
pVtab->zLogName = sqlite3MPrintf(0, "%s", argv[4]);
458
zSql = sqlite3MPrintf(0, "CREATE TABLE %Q(logmsg)", pVtab->zLogName);
384
459
rc = sqlite3_exec(db, zSql, 0, 0, 0);
462
*pzErr = sqlite3StrDup(sqlite3_errmsg(db));
466
if( *ppVtab && rc!=SQLITE_OK ){
467
echoDestructor(*ppVtab);
472
(*(echo_vtab**)ppVtab)->inTransaction = 1;
582
671
** If the third argument, doFree, is true, then sqlite3_free() is
583
672
** also called to free the buffer pointed to by zAppend.
585
static void string_concat(char **pzStr, char *zAppend, int doFree){
674
static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){
586
675
char *zIn = *pzStr;
589
zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
676
if( !zAppend && doFree && *pRc==SQLITE_OK ){
679
if( *pRc!=SQLITE_OK ){
592
zIn = sqlite3_mprintf("%s", zAppend);
685
zIn = sqlite3_mprintf("%s%s", zIn, zAppend);
688
zIn = sqlite3_mprintf("%s", zAppend);
627
727
const char *zSep = "WHERE";
628
728
echo_vtab *pVtab = (echo_vtab *)tab;
629
729
sqlite3_stmt *pStmt = 0;
730
Tcl_Interp *interp = pVtab->interp;
633
734
int rc = SQLITE_OK;
738
int isIgnoreUsable = 0;
739
if( Tcl_GetVar(interp, "echo_module_ignore_usable", TCL_GLOBAL_ONLY) ){
635
743
/* Determine the number of rows in the table and store this value in local
636
744
** variable nRow. The 'estimated-cost' of the scan will be the number of
637
745
** rows in the table for a linear scan, or the log (base 2) of the
638
746
** 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);
748
if( Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY) ){
749
cost = atof(Tcl_GetVar(interp, "echo_module_cost", TCL_GLOBAL_ONLY));
752
zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName);
756
rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0);
757
sqlite3_free(zQuery);
762
nRow = sqlite3_column_int(pStmt, 0);
763
rc = sqlite3_finalize(pStmt);
652
769
zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName);
653
773
for(ii=0; ii<pIdxInfo->nConstraint; ii++){
654
774
const struct sqlite3_index_constraint *pConstraint;
655
775
struct sqlite3_index_constraint_usage *pUsage;
708
830
zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir);
709
string_concat(&zQuery, zNew, 1);
831
string_concat(&zQuery, zNew, 1, &rc);
710
832
pIdxInfo->orderByConsumed = 1;
713
835
appendToEchoModule(pVtab->interp, "xBestIndex");;
714
836
appendToEchoModule(pVtab->interp, zQuery);
716
841
pIdxInfo->idxNum = hashString(zQuery);
717
842
pIdxInfo->idxStr = zQuery;
718
843
pIdxInfo->needToFreeIdxStr = 1;
845
pIdxInfo->estimatedCost = cost;
720
847
/* Approximation of log2(nRow). */
721
848
for( ii=0; ii<(sizeof(int)*8); ii++ ){
722
849
if( nRow & (1<<ii) ){
762
889
assert( nData==pVtab->nCol+2 || nData==1 );
891
/* Ticket #3083 - make sure we always start a transaction prior to
892
** making any changes to a virtual table */
893
assert( pVtab->inTransaction );
764
895
/* If apData[0] is an integer and nData>1 then do an UPDATE */
765
896
if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
766
897
char *zSep = " SET";
767
898
z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName);
769
903
bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER);
772
906
if( bindArgOne ){
773
string_concat(&z, " SET rowid=?1 ", 0);
907
string_concat(&z, " SET rowid=?1 ", 0, &rc);
776
910
for(i=2; i<nData; i++){
777
911
if( apData[i]==0 ) continue;
778
912
string_concat(&z, sqlite3_mprintf(
779
"%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1);
913
"%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc);
782
string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 0);
916
string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc);
785
919
/* If apData[0] is an integer and nData==1 then do a DELETE */
786
920
else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){
787
921
z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName);
795
932
char *zValues = 0;
797
934
zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName);
798
938
if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){
800
940
zValues = sqlite3_mprintf("?");
801
string_concat(&zInsert, "rowid", 0);
941
string_concat(&zInsert, "rowid", 0, &rc);
804
944
assert((pVtab->nCol+2)==nData);
805
945
for(ii=2; ii<nData; ii++){
806
946
string_concat(&zInsert,
807
sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1);
947
sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc);
808
948
string_concat(&zValues,
809
sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1);
949
sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc);
812
string_concat(&z, zInsert, 1);
813
string_concat(&z, ") VALUES(", 0);
814
string_concat(&z, zValues, 1);
815
string_concat(&z, ")", 0);
952
string_concat(&z, zInsert, 1, &rc);
953
string_concat(&z, ") VALUES(", 0, &rc);
954
string_concat(&z, zValues, 1, &rc);
955
string_concat(&z, ")", 0, &rc);
818
958
/* Anything else is an error */
855
1001
echo_vtab *pVtab = (echo_vtab *)tab;
856
1002
z = sqlite3_mprintf("echo(%s)", pVtab->zTableName);
1003
if( z==0 ) return SQLITE_NOMEM;
857
1004
appendToEchoModule(pVtab->interp, zCall);
858
1005
appendToEchoModule(pVtab->interp, z);
859
1006
sqlite3_free(z);
860
1007
return SQLITE_OK;
862
1009
static int echoBegin(sqlite3_vtab *tab){
863
1011
echo_vtab *pVtab = (echo_vtab *)tab;
864
1012
Tcl_Interp *interp = pVtab->interp;
865
1013
const char *zVal;
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) ){
1015
/* Ticket #3083 - do not start a transaction if we are already in
1017
assert( !pVtab->inTransaction );
1019
rc = echoTransactionCall(tab, "xBegin");
1021
if( rc==SQLITE_OK ){
1022
/* Check if the $::echo_module_begin_fail variable is defined. If it is,
1023
** and it is set to the name of the real table underlying this virtual
1024
** echo module table, then cause this xSync operation to fail.
1026
zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY);
1027
if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
1031
if( rc==SQLITE_OK ){
1032
pVtab->inTransaction = 1;
879
1036
static int echoSync(sqlite3_vtab *tab){
880
1038
echo_vtab *pVtab = (echo_vtab *)tab;
881
1039
Tcl_Interp *interp = pVtab->interp;
882
1040
const char *zVal;
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) ){
1042
/* Ticket #3083 - Only call xSync if we have previously started a
1044
assert( pVtab->inTransaction );
1046
rc = echoTransactionCall(tab, "xSync");
1048
if( rc==SQLITE_OK ){
1049
/* Check if the $::echo_module_sync_fail variable is defined. If it is,
1050
** and it is set to the name of the real table underlying this virtual
1051
** echo module table, then cause this xSync operation to fail.
1053
zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY);
1054
if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){
896
1060
static int echoCommit(sqlite3_vtab *tab){
897
return echoTransactionCall(tab, "xCommit");
1061
echo_vtab *pVtab = (echo_vtab*)tab;
1064
/* Ticket #3083 - Only call xCommit if we have previously started
1066
assert( pVtab->inTransaction );
1068
sqlite3FaultBeginBenign(SQLITE_FAULTINJECTOR_MALLOC);
1069
rc = echoTransactionCall(tab, "xCommit");
1070
sqlite3FaultEndBenign(SQLITE_FAULTINJECTOR_MALLOC);
1071
pVtab->inTransaction = 0;
899
1074
static int echoRollback(sqlite3_vtab *tab){
900
return echoTransactionCall(tab, "xRollback");
1076
echo_vtab *pVtab = (echo_vtab*)tab;
1078
/* Ticket #3083 - Only call xRollback if we have previously started
1080
assert( pVtab->inTransaction );
1082
rc = echoTransactionCall(tab, "xRollback");
1083
pVtab->inTransaction = 0;
1146
static int echoRename(sqlite3_vtab *vtab, const char *zNewName){
1148
echo_vtab *p = (echo_vtab *)vtab;
1151
int nThis = strlen(p->zThis);
1152
char *zSql = sqlite3MPrintf(0, "ALTER TABLE %s RENAME TO %s%s",
1153
p->zTableName, zNewName, &p->zTableName[nThis]
1155
rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
963
1163
** A virtual table module that merely "echos" the contents of another
964
1164
** table (like an SQL VIEW).
1003
1208
Tcl_Obj *CONST objv[] /* Command arguments */
1007
1213
Tcl_WrongNumArgs(interp, 1, objv, "DB");
1008
1214
return TCL_ERROR;
1010
1216
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
1011
sqlite3_create_module(db, "echo", &echoModule, (void *)interp);
1217
pMod = sqlite3_malloc(sizeof(EchoModule));
1218
pMod->interp = interp;
1219
sqlite3_create_module_v2(db, "echo", &echoModule, (void*)pMod, moduleDestroy);