Index: Makefile.in ================================================================== --- Makefile.in +++ Makefile.in @@ -415,10 +415,11 @@ # Statically linked extensions # TESTSRC += \ $(TOP)/ext/misc/amatch.c \ $(TOP)/ext/misc/closure.c \ + $(TOP)/ext/misc/csv.c \ $(TOP)/ext/misc/eval.c \ $(TOP)/ext/misc/fileio.c \ $(TOP)/ext/misc/fuzzer.c \ $(TOP)/ext/fts5/fts5_tcl.c \ $(TOP)/ext/fts5/fts5_test_mi.c \ Index: Makefile.msc ================================================================== --- Makefile.msc +++ Makefile.msc @@ -1291,10 +1291,11 @@ # Statically linked extensions. # TESTEXT = \ $(TOP)\ext\misc\amatch.c \ $(TOP)\ext\misc\closure.c \ + $(TOP)\ext\misc\csv.c \ $(TOP)\ext\misc\eval.c \ $(TOP)\ext\misc\fileio.c \ $(TOP)\ext\misc\fuzzer.c \ $(TOP)\ext\fts5\fts5_tcl.c \ $(TOP)\ext\fts5\fts5_test_mi.c \ Index: ext/misc/csv.c ================================================================== --- ext/misc/csv.c +++ ext/misc/csv.c @@ -300,10 +300,11 @@ ** This method is the destructor fo a CsvTable object. */ static int csvtabDisconnect(sqlite3_vtab *pVtab){ CsvTable *p = (CsvTable*)pVtab; sqlite3_free(p->zFilename); + sqlite3_free(p->zData); sqlite3_free(p); return SQLITE_OK; } /* Skip leading whitespace. Return a pointer to the first non-whitespace Index: main.mk ================================================================== --- main.mk +++ main.mk @@ -323,10 +323,11 @@ # Extensions to be statically loaded. # TESTSRC += \ $(TOP)/ext/misc/amatch.c \ $(TOP)/ext/misc/closure.c \ + $(TOP)/ext/misc/csv.c \ $(TOP)/ext/misc/eval.c \ $(TOP)/ext/misc/fileio.c \ $(TOP)/ext/misc/fuzzer.c \ $(TOP)/ext/misc/ieee754.c \ $(TOP)/ext/misc/nextchar.c \ Index: src/expr.c ================================================================== --- src/expr.c +++ src/expr.c @@ -2533,11 +2533,11 @@ if( iCol<0 || iCol==pTab->iPKey ){ sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); }else{ int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; int x = iCol; - if( !HasRowid(pTab) ){ + if( !HasRowid(pTab) && !IsVirtual(pTab) ){ x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); } sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); } if( iCol>=0 ){ Index: src/test1.c ================================================================== --- src/test1.c +++ src/test1.c @@ -6582,10 +6582,11 @@ int objc, Tcl_Obj *CONST objv[] ){ extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*); + extern int sqlite3_csv_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*); @@ -6599,10 +6600,11 @@ const char *zExtName; int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); } aExtension[] = { { "amatch", sqlite3_amatch_init }, { "closure", sqlite3_closure_init }, + { "csv", sqlite3_csv_init }, { "eval", sqlite3_eval_init }, { "fileio", sqlite3_fileio_init }, { "fuzzer", sqlite3_fuzzer_init }, { "ieee754", sqlite3_ieee_init }, { "nextchar", sqlite3_nextchar_init }, ADDED test/csv01.test Index: test/csv01.test ================================================================== --- /dev/null +++ test/csv01.test @@ -0,0 +1,91 @@ +# 2016-06-02 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test cases for CSV virtual table. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix closure01 + +ifcapable !vtab||!cte { finish_test ; return } + +load_static_extension db csv + +do_execsql_test 1.0 { + CREATE VIRTUAL TABLE temp.t1 USING csv( + data= +'1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16 +', + columns=4 + ); + SELECT * FROM t1 WHERE c1=10; +} {9 10 11 12} +do_execsql_test 1.1 { + SELECT * FROM t1 WHERE c1='10'; +} {9 10 11 12} +do_execsql_test 1.2 { + SELECT rowid FROM t1; +} {1 2 3 4} + +do_execsql_test 2.0 { + DROP TABLE t1; + CREATE VIRTUAL TABLE temp.t2 USING csv( + data= +'1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16 +', + columns=4, + schema='CREATE TABLE t2(a INT, b TEXT, c REAL, d BLOB)' + ); + SELECT * FROM t2 WHERE a=9; +} {9 10 11 12} +do_execsql_test 2.1 { + SELECT * FROM t2 WHERE b=10; +} {9 10 11 12} +do_execsql_test 2.2 { + SELECT * FROM t2 WHERE c=11; +} {9 10 11 12} +do_execsql_test 2.3 { + SELECT * FROM t2 WHERE d=12; +} {} +do_execsql_test 2.4 { + SELECT * FROM t2 WHERE d='12'; +} {9 10 11 12} +do_execsql_test 2.5 { + SELECT * FROM t2 WHERE a='9'; +} {9 10 11 12} + +do_execsql_test 3.0 { + DROP TABLE t2; + CREATE VIRTUAL TABLE temp.t3 USING csv( + data= +'1,2,3,4 +5,6,7,8 +9,10,11,12 +13,14,15,16 +', + columns=4, + schema= + 'CREATE TABLE t3(a PRIMARY KEY,b TEXT,c TEXT,d TEXT) WITHOUT ROWID', + testflags=1 + ); + SELECT a FROM t3 WHERE b=6 OR c=7 OR d=12 ORDER BY +a; +} {5 9} +do_execsql_test 3.1 { + SELECT a FROM t3 WHERE +b=6 OR c=7 OR d=12 ORDER BY +a; +} {5 9} + +finish_test