3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # Test cases for CSV virtual table.
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
18 ifcapable !vtab||!cte { finish_test ; return }
20 load_static_extension db csv
23 CREATE VIRTUAL TABLE temp.t1 USING csv(
32 SELECT * FROM t1 WHERE c1=10;
35 SELECT * FROM t1 WHERE c1='10';
43 CREATE VIRTUAL TABLE temp.t1 USING csv(
45 'a,b,"mix-bloom-eel","soft opinion"
53 SELECT * FROM t1 WHERE "soft opinion"=12;
56 SELECT name FROM pragma_table_xinfo('t1');
57 } {a b mix-bloom-eel {soft opinion}}
61 CREATE VIRTUAL TABLE temp.t1 USING csv(
63 'a,b,"mix-bloom-eel","soft opinion"
71 SELECT * FROM t1 WHERE c1='b';
72 } {a b mix-bloom-eel {soft opinion}}
74 SELECT name FROM pragma_table_xinfo('t1');
79 CREATE VIRTUAL TABLE temp.t1 USING csv(
81 'a,b,"mix-bloom-eel","soft opinion"
88 schema='CREATE TABLE x(x0,x1,x2,x3,x4)',
91 SELECT * FROM t1 WHERE x1='6';
94 SELECT name FROM pragma_table_xinfo('t1');
100 CREATE VIRTUAL TABLE temp.t2 USING csv(
108 schema='CREATE TABLE t2(a INT, b TEXT, c REAL, d BLOB)'
110 SELECT * FROM t2 WHERE a=9;
112 do_execsql_test 2.1 {
113 SELECT * FROM t2 WHERE b=10;
115 do_execsql_test 2.2 {
116 SELECT * FROM t2 WHERE c=11;
118 do_execsql_test 2.3 {
119 SELECT * FROM t2 WHERE d=12;
121 do_execsql_test 2.4 {
122 SELECT * FROM t2 WHERE d='12';
124 do_execsql_test 2.5 {
125 SELECT * FROM t2 WHERE a='9';
128 do_execsql_test 3.0 {
130 CREATE VIRTUAL TABLE temp.t3 USING csv(
139 'CREATE TABLE t3(a PRIMARY KEY,b TEXT,c TEXT,d TEXT) WITHOUT ROWID',
142 SELECT a FROM t3 WHERE b=6 OR c=7 OR d=12 ORDER BY +a;
144 do_execsql_test 3.1 {
145 SELECT a FROM t3 WHERE +b=6 OR c=7 OR d=12 ORDER BY +a;
148 # The rowid column is not visible on a WITHOUT ROWID virtual table
149 do_catchsql_test 3.2 {
150 SELECT rowid, a FROM t3;
151 } {1 {no such column: rowid}}
153 # Multi-column WITHOUT ROWID virtual tables may not be writable.
154 do_catchsql_test 4.0 {
156 CREATE VIRTUAL TABLE temp.t4 USING csv_wr(
164 'CREATE TABLE t3(a,b,c,d,PRIMARY KEY(a,b)) WITHOUT ROWID',
167 } {1 {bad schema: 'CREATE TABLE t3(a,b,c,d,PRIMARY KEY(a,b)) WITHOUT ROWID' - not an error}}
169 # WITHOUT ROWID tables with a single-column PRIMARY KEY may be writable.
170 do_catchsql_test 4.1 {
171 DROP TABLE IF EXISTS t4;
172 CREATE VIRTUAL TABLE temp.t4 USING csv_wr(
180 'CREATE TABLE t3(a,b,c,d,PRIMARY KEY(b)) WITHOUT ROWID',
185 do_catchsql_test 4.2 {
186 DROP TABLE IF EXISTS t5;
187 CREATE VIRTUAL TABLE temp.t5 USING csv_wr(
195 'CREATE TABLE t3(a,b,c,d) WITHOUT ROWID',
198 } {1 {bad schema: 'CREATE TABLE t3(a,b,c,d) WITHOUT ROWID' - PRIMARY KEY missing on table t3}}
201 # Memory leak reported on the sqlite-users mailing list by Ralf Junker.
203 do_catchsql_test 4.3 {
204 CREATE VIRTUAL TABLE IF NOT EXISTS temp.t1
205 USING csv(filename='FileDoesNotExist.csv');
206 } {1 {cannot open 'FileDoesNotExist.csv' for reading}}
209 # Problem with single-column CSV support reported on the mailing list
212 do_execsql_test 4.4 {
213 CREATE VIRTUAL TABLE temp.trent USING csv(data='1');
218 # Bug report on the mailing list
220 forcedelete csv01.csv
221 set fd [open csv01.csv wb]
222 puts $fd "a,b,c,d\r\n1,2,3,4\r\none,two,three,four\r\n5,6,7,8"
224 do_execsql_test 5.1 {
225 CREATE VIRTUAL TABLE t5_1 USING csv(filename='csv01.csv');
226 SELECT name FROM temp.pragma_table_info('t5_1');
228 do_execsql_test 5.2 {
229 SELECT *, '|' FROM t5_1;
230 } {a b c d | 1 2 3 4 | one two three four | 5 6 7 8 |}
231 do_execsql_test 5.3 {
233 CREATE VIRTUAL TABLE t5_1 USING csv(filename='csv01.csv', header);
234 SELECT name FROM temp.pragma_table_info('t5_1');
236 do_execsql_test 5.4 {
237 SELECT *, '|' FROM t5_1;
238 } {1 2 3 4 | one two three four | 5 6 7 8 |}
240 #-------------------------------------------------------------------------
242 proc randomtext {n} {
243 string range [db one {SELECT hex(randomblob($n))}] 1 $n
246 for {set ii 0} {$ii < 200} {incr ii} {
248 load_static_extension db csv
249 set fd [open csv.data w]
251 puts $fd "[randomtext $ii],abcd"
253 do_execsql_test 6.$ii.1 {
254 CREATE VIRTUAL TABLE abc USING csv(filename='csv.data', header=true);
256 do_execsql_test 6.$ii.2 {
257 SELECT count(*) FROM abc
261 for {set ii 0} {$ii < 20} {incr ii} {
263 load_static_extension db csv
264 set T [randomtext $ii]
265 set fd [open csv.data w]
267 puts -nonewline $fd "abcd,$T"
269 do_execsql_test 7.$ii.1 {
270 CREATE VIRTUAL TABLE abc USING csv(filename='csv.data', header=true);
273 do_execsql_test 7.$ii.2 {