1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# 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 csv01
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 1.3 {
DROP TABLE temp.t1;
CREATE VIRTUAL TABLE temp.t1 USING csv(
data=
'a,b,"mix-bloom-eel","soft opinion"
1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16
',
header=1
);
SELECT * FROM t1 WHERE "soft opinion"=12;
} {9 10 11 12}
do_execsql_test 1.4 {
SELECT name FROM pragma_table_xinfo('t1');
} {a b mix-bloom-eel {soft opinion}}
do_execsql_test 1.5 {
DROP TABLE temp.t1;
CREATE VIRTUAL TABLE temp.t1 USING csv(
data=
'a,b,"mix-bloom-eel","soft opinion"
1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16
',
header=false
);
SELECT * FROM t1 WHERE c1='b';
} {a b mix-bloom-eel {soft opinion}}
do_execsql_test 1.6 {
SELECT name FROM pragma_table_xinfo('t1');
} {c0 c1 c2 c3}
do_execsql_test 1.7 {
DROP TABLE temp.t1;
CREATE VIRTUAL TABLE temp.t1 USING csv(
data=
'a,b,"mix-bloom-eel","soft opinion"
1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16
',
header,
schema='CREATE TABLE x(x0,x1,x2,x3,x4)',
columns=5
);
SELECT * FROM t1 WHERE x1='6';
} {5 6 7 8 {}}
do_execsql_test 1.8 {
SELECT name FROM pragma_table_xinfo('t1');
} {x0 x1 x2 x3 x4}
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}
# The rowid column is not visible on a WITHOUT ROWID virtual table
do_catchsql_test 3.2 {
SELECT rowid, a FROM t3;
} {1 {no such column: rowid}}
# Multi-column WITHOUT ROWID virtual tables may not be writable.
do_catchsql_test 4.0 {
DROP TABLE t3;
CREATE VIRTUAL TABLE temp.t4 USING csv_wr(
data=
'1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16',
columns=4,
schema=
'CREATE TABLE t3(a,b,c,d,PRIMARY KEY(a,b)) WITHOUT ROWID',
testflags=1
);
} {1 {bad schema: 'CREATE TABLE t3(a,b,c,d,PRIMARY KEY(a,b)) WITHOUT ROWID' - not an error}}
# WITHOUT ROWID tables with a single-column PRIMARY KEY may be writable.
do_catchsql_test 4.1 {
DROP TABLE IF EXISTS t4;
CREATE VIRTUAL TABLE temp.t4 USING csv_wr(
data=
'1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16',
columns=4,
schema=
'CREATE TABLE t3(a,b,c,d,PRIMARY KEY(b)) WITHOUT ROWID',
testflags=1
);
} {0 {}}
do_catchsql_test 4.2 {
DROP TABLE IF EXISTS t5;
CREATE VIRTUAL TABLE temp.t5 USING csv_wr(
data=
'1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16',
columns=4,
schema=
'CREATE TABLE t3(a,b,c,d) WITHOUT ROWID',
testflags=1
);
} {1 {bad schema: 'CREATE TABLE t3(a,b,c,d) WITHOUT ROWID' - PRIMARY KEY missing on table t3}}
# 2018-04-24
# Memory leak reported on the sqlite-users mailing list by Ralf Junker.
#
do_catchsql_test 4.3 {
CREATE VIRTUAL TABLE IF NOT EXISTS temp.t1
USING csv(filename='FileDoesNotExist.csv');
} {1 {cannot open 'FileDoesNotExist.csv' for reading}}
# 2018-06-02
# Problem with single-column CSV support reported on the mailing list
# by Trent W. Buck.
#
do_execsql_test 4.4 {
CREATE VIRTUAL TABLE temp.trent USING csv(data='1');
SELECT * FROM trent;
} {1}
# 2018-12-26
# Bug report on the mailing list
#
forcedelete csv01.csv
set fd [open csv01.csv wb]
puts $fd "a,b,c,d\r\n1,2,3,4\r\none,two,three,four\r\n5,6,7,8"
close $fd
do_execsql_test 5.1 {
CREATE VIRTUAL TABLE t5_1 USING csv(filename='csv01.csv');
SELECT name FROM temp.pragma_table_info('t5_1');
} {c0 c1 c2 c3}
do_execsql_test 5.2 {
SELECT *, '|' FROM t5_1;
} {a b c d | 1 2 3 4 | one two three four | 5 6 7 8 |}
do_execsql_test 5.3 {
DROP TABLE t5_1;
CREATE VIRTUAL TABLE t5_1 USING csv(filename='csv01.csv', header);
SELECT name FROM temp.pragma_table_info('t5_1');
} {a b c d}
do_execsql_test 5.4 {
SELECT *, '|' FROM t5_1;
} {1 2 3 4 | one two three four | 5 6 7 8 |}
#-------------------------------------------------------------------------
proc randomtext {n} {
string range [db one {SELECT hex(randomblob($n))}] 1 $n
}
for {set ii 0} {$ii < 200} {incr ii} {
reset_db
load_static_extension db csv
set fd [open csv.data w]
puts $fd "a,b"
puts $fd "[randomtext $ii],abcd"
close $fd
do_execsql_test 6.$ii.1 {
CREATE VIRTUAL TABLE abc USING csv(filename='csv.data', header=true);
}
do_execsql_test 6.$ii.2 {
SELECT count(*) FROM abc
} 1
}
for {set ii 0} {$ii < 20} {incr ii} {
reset_db
load_static_extension db csv
set T [randomtext $ii]
set fd [open csv.data w]
puts $fd "a,b"
puts -nonewline $fd "abcd,$T"
close $fd
do_execsql_test 7.$ii.1 {
CREATE VIRTUAL TABLE abc USING csv(filename='csv.data', header=true);
}
breakpoint
do_execsql_test 7.$ii.2 {
SELECT * FROM abc
} [list abcd $T]
}
finish_test
|