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 #***********************************************************************
13 # The focus of this file is testing the CLI shell tool.
14 # These tests are specific to the .import command.
16 # $Id: shell5.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $
21 # shell5-1.*: Basic tests specific to the ".import" command.
23 set testdir [file dirname $argv0]
24 source $testdir/tester.tcl
25 set CLI [test_cli_invocation]
27 forcedelete test.db test.db-journal test.db-wal
29 #----------------------------------------------------------------------------
30 # Test cases shell5-1.*: Basic handling of the .import and .separator commands.
33 # .import FILE TABLE Import data from FILE into TABLE
34 do_test shell5-1.1.1 {
35 catchcmd "test.db" ".import"
36 } {/1 .ERROR: missing FILE argument.*/}
37 do_test shell5-1.1.2 {
38 catchcmd "test.db" ".import FOO"
39 } {/1 .ERROR: missing TABLE argument.*/}
40 do_test shell5-1.1.3 {
42 catchcmd "test.db" ".import FOO BAR BAD"
43 } {/1 .ERROR: extra argument.*/}
45 # .separator STRING Change separator used by output mode and .import
46 do_test shell5-1.2.1 {
47 catchcmd "test.db" ".separator"
48 } {1 {Usage: .separator COL ?ROW?}}
49 do_test shell5-1.2.2 {
50 catchcmd "test.db" ".separator ONE"
52 do_test shell5-1.2.3 {
53 catchcmd "test.db" ".separator ONE TWO"
55 do_test shell5-1.2.4 {
57 catchcmd "test.db" ".separator ONE TWO THREE"
58 } {1 {Usage: .separator COL ?ROW?}}
60 # column separator should default to "|"
61 do_test shell5-1.3.1.1 {
62 set res [catchcmd "test.db" ".show"]
63 list [regexp {colseparator: \"\|\"} $res]
66 # row separator should default to "\n"
67 do_test shell5-1.3.1.2 {
68 set res [catchcmd "test.db" ".show"]
69 list [regexp {rowseparator: \"\\n\"} $res]
72 # set separator to different value.
73 # check that .show reports new value
74 do_test shell5-1.3.2 {
75 set res [catchcmd "test.db" {.separator ,
77 list [regexp {separator: \",\"} $res]
80 # import file doesn't exist
81 do_test shell5-1.4.1 {
83 set res [catchcmd "test.db" {CREATE TABLE t1(a, b);
85 } {1 {Error: cannot open "FOO"}}
88 do_test shell5-1.4.2 {
89 forcedelete shell5.csv
90 set in [open shell5.csv w]
92 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
93 .import -schema test shell5.csv t1
94 SELECT COUNT(*) FROM test.t1;}]
97 # import file with 1 row, 1 column (expecting 2 cols)
98 do_test shell5-1.4.3 {
99 set in [open shell5.csv w]
102 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
103 .import -schema test shell5.csv t1}]
104 } {1 {shell5.csv:1: expected 2 columns but found 1 - filling the rest with NULL}}
106 # import file with 1 row, 3 columns (expecting 2 cols)
107 do_test shell5-1.4.4 {
108 set in [open shell5.csv w]
111 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
112 .import --schema test shell5.csv t1}]
113 } {1 {shell5.csv:1: expected 2 columns but found 3 - extras ignored}}
115 # import file with 1 row, 2 columns
116 do_test shell5-1.4.5 {
117 set in [open shell5.csv w]
120 set res [catchcmd "test.db" {DELETE FROM t1;
121 .import shell5.csv t1
122 SELECT COUNT(*) FROM t1;}]
125 # import file with 2 rows, 2 columns
126 # note we end up with 3 rows because of the 1 row
128 do_test shell5-1.4.6 {
129 set in [open shell5.csv w]
133 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
134 .import -schema test shell5.csv t1
135 SELECT COUNT(*) FROM test.t1;}]
138 # import file with 1 row, 2 columns, using a comma
139 do_test shell5-1.4.7 {
140 set in [open shell5.csv w]
143 set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
145 .import --schema test shell5.csv t1
146 SELECT COUNT(*) FROM test.t1;}]
149 # import file with 1 row, 2 columns, text data
150 do_test shell5-1.4.8.1 {
151 set in [open shell5.csv w]
152 puts $in "5|Now is the time for all good men to come to the aid of their country."
154 set res [catchcmd "test.db" {.import shell5.csv t1
155 SELECT COUNT(*) FROM t1;}]
158 do_test shell5-1.4.8.2 {
159 catchcmd "test.db" {SELECT b FROM t1 WHERE a='5';}
160 } {0 {Now is the time for all good men to come to the aid of their country.}}
162 # import file with 1 row, 2 columns, quoted text data
163 # note that currently sqlite doesn't support quoted fields, and
164 # imports the entire field, quotes and all.
165 do_test shell5-1.4.9.1 {
166 set in [open shell5.csv w]
167 puts $in "6|'Now is the time for all good men to come to the aid of their country.'"
169 set res [catchcmd "test.db" {.import shell5.csv t1
170 SELECT COUNT(*) FROM t1;}]
173 do_test shell5-1.4.9.2 {
174 catchcmd "test.db" {SELECT b FROM t1 WHERE a='6';}
175 } {0 {'Now is the time for all good men to come to the aid of their country.'}}
177 # import file with 1 row, 2 columns, quoted text data
178 do_test shell5-1.4.10.1 {
179 set in [open shell5.csv w]
180 puts $in "7|\"Now is the time for all good men to come to the aid of their country.\""
182 set res [catchcmd "test.db" {.import shell5.csv t1
183 SELECT COUNT(*) FROM t1;}]
186 do_test shell5-1.4.10.2 {
187 catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';}
188 } {0 {Now is the time for all good men to come to the aid of their country.}}
190 # import file with 2 rows, 2 columns and an initial BOM
192 do_test shell5-1.4.11 {
193 set in [open shell5.csv wb]
194 puts -nonewline $in "\xef\xbb\xbf"
198 set res [catchcmd "test.db" {CREATE TABLE t2(x INT, y INT);
199 .import shell5.csv t2
203 string map {\n | \n\r |} $res
204 } {0 {'x','y'|2,3|4,5}}
206 # import file with 2 rows, 2 columns or text with an initial BOM
208 do_test shell5-1.4.12 {
209 set in [open shell5.csv wb]
210 puts $in "\xef\xbb\xbf\"two\"|3"
213 set res [catchcmd "test.db" {DELETE FROM t2;
214 .import shell5.csv t2
218 string map {\n | \n\r |} $res
219 } {0 {'x','y'|'two',3|4,5}}
221 # check importing very long field
222 do_test shell5-1.5.1 {
223 set str [string repeat X 999]
224 set in [open shell5.csv w]
227 set res [catchcmd "test.db" {.import shell5.csv t1
228 SELECT length(b) FROM t1 WHERE a='8';}]
231 # try importing into a table with a large number of columns.
232 # This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999.
234 do_test shell5-1.6.1 {
236 for {set i 1} {$i<$cols} {incr i} {
239 append data "c$cols\n";
240 for {set i 1} {$i<$cols} {incr i} {
244 set in [open shell5.csv w]
247 set res [catchcmd "test.db" {DROP TABLE IF EXISTS t2;
248 .import shell5.csv t2
249 SELECT COUNT(*) FROM t2;}]
252 # try importing a large number of rows
254 do_test shell5-1.7.1 {
255 set in [open shell5.csv w]
257 for {set i 1} {$i<=$rows} {incr i} {
261 set res [catchcmd "test.db" {.mode csv
262 .import shell5.csv t3
263 SELECT COUNT(*) FROM t3;}]
266 # Import from a pipe. (Unix only, as it requires "awk")
267 if {$tcl_platform(platform)=="unix"} {
270 catchcmd test.db {.mode csv
271 .import "|awk 'END{print \"x,y\";for(i=1;i<=5;i++){print i \",this is \" i}}'" t1
280 # Import columns containing quoted strings
282 set out [open shell5.csv w]
283 fconfigure $out -translation lf
286 puts $out {3,"""",33}
287 puts $out {4,"hello",44}
288 puts $out "5,55,\"\"\r"
290 puts $out {7,77,""""}
291 puts $out {8,88,"hello"}
293 puts $out {"x",10,110}
294 puts $out {"""",11,121}
295 puts $out {"hello",12,132}
298 catchcmd test.db {.mode csv
299 CREATE TABLE t1(a,b,c);
300 .import shell5.csv t1
303 db eval {SELECT *, '|' FROM t1 ORDER BY rowid}
304 } {1 {} 11 | 2 x 22 | 3 {"} 33 | 4 hello 44 | 5 55 {} | 6 66 x | 7 77 {"} | 8 88 hello | {} 9 99 | x 10 110 | {"} 11 121 | hello 12 132 |}
307 # Import columns containing quoted strings
308 do_test shell5-1.10 {
309 set out [open shell5.csv w]
310 fconfigure $out -translation lf
311 puts $out {column1,column2,column3,column4}
312 puts $out "field1,field2,\"x3 \"\"\r\ndata\"\" 3\",field4"
313 puts $out "x1,x2,\"x3 \"\"\ndata\"\" 3\",x4"
316 catchcmd test.db {.mode csv
317 CREATE TABLE t1(a,b,c,d);
318 .import shell5.csv t1
321 db eval {SELECT hex(c) FROM t1 ORDER BY rowid}
322 } {636F6C756D6E33 783320220D0A64617461222033 783320220A64617461222033}
324 # Blank last column with \r\n line endings.
325 do_test shell5-1.11 {
326 set out [open shell5.csv w]
327 fconfigure $out -translation binary
328 puts $out "column1,column2,column3\r"
335 catchcmd test.db {.mode csv
336 .import shell5.csv t1
339 db eval {SELECT *, '|' FROM t1}
340 } {a b { } | x y {} | p q r |}
343 #----------------------------------------------------------------------------
348 set fd [open shell5.csv w]
351 catchcmd test.db [string trim {
353 CREATE TABLE t1(a, b);
354 .import shell5.csv t1
356 db eval { SELECT * FROM t1 }
360 set fd [open shell5.csv w]
363 catchcmd test.db [string trim {
365 CREATE TABLE t2(a, b);
366 .import shell5.csv t2
368 db eval { SELECT * FROM t2 }
372 set fd [open shell5.csv w]
373 puts $fd {"x""y",hello}
375 catchcmd test.db [string trim {
377 CREATE TABLE t3(a, b);
378 .import shell5.csv t3
380 db eval { SELECT * FROM t3 }
384 set fd [open shell5.csv w]
385 puts $fd {"xy""",hello}
387 catchcmd test.db [string trim {
389 CREATE TABLE t4(a, b);
390 .import shell5.csv t4
392 db eval { SELECT * FROM t4 }
396 set fd [open shell5.csv w]
400 catchcmd test.db [string trim {
402 CREATE TABLE t4(a, b);
403 .import shell5.csv t4
405 db eval { SELECT * FROM t4 }
406 } {xy\" hello one 2 {} {}}
408 #----------------------------------------------------------------------------
409 # Tests for the shell "ascii" import/export mode.
412 set fd [open shell5.csv w]
413 fconfigure $fd -encoding binary -translation binary
414 puts -nonewline $fd "\"test 1\"\x1F,test 2\r\n\x1E"
415 puts -nonewline $fd "test 3\x1Ftest 4\n"
419 CREATE TABLE t5(a, b);
420 .import shell5.csv t5
422 db eval { SELECT * FROM t5 }
423 } "\{\"test 1\"} \{,test 2\r\n\} \{test 3\} \{test 4\n\}"
426 set x [catchcmd test.db {
430 # Handle platform end-of-line differences
431 regsub -all {[\n\r]?\n} $x <EOL> x
433 } "0 \{\"test 1\"\x1F,test 2<EOL>\x1Etest 3\x1Ftest 4<EOL>\x1E\}"
436 forcedelete shell5.csv
437 set fd [open shell5.csv w]
442 catchcmd test.db [string trim {
444 CREATE TABLE t6(a, b, c);
445 .import shell5.csv t6
447 db eval { SELECT * FROM t6 ORDER BY a }
448 } {1 2 3 4 5 {} 6 7 8}
451 forcedelete shell5.csv
452 set fd [open shell5.csv w]
457 catchcmd test.db [string trim {
459 CREATE TABLE t7(a, b, c);
460 .import shell5.csv t7
462 db eval { SELECT * FROM t7 ORDER BY a }
463 } {1 2 3 4 5 {} 6 7 8}
466 forcedelete shell5.csv
467 set fd [open shell5.csv w]
471 catchcmd test.db [string trim {
473 CREATE TABLE t8(a, b, c);
474 .import -skip 1 shell5.csv t8
477 db eval { SELECT * FROM t8 }
481 forcedelete shell5.csv
482 set fd [open shell5.csv w]
485 catchcmd test.db [string trim {
487 CREATE TEMP TABLE t8(a, b, c);
488 .import shell5.csv t8
490 SELECT * FROM temp.t8
494 #----------------------------------------------------------------------------
495 # Tests for the shell automatic column rename.
499 # Import columns containing duplicates
501 set out [open shell5.csv w]
502 fconfigure $out -translation lf
503 puts $out {"","x","x","y","z","z_0","z_5","z"}
504 puts $out {0,"x2","x3","y4","z5","z6","z7","z8"}
507 catchcmd test.db {.import -csv shell5.csv t1
518 Columns renamed during .import shell5.csv due to duplicates:
525 set out [open shell5.csv w]
526 fconfigure $out -translation lf
527 puts $out {"COW","cow","CoW","cOw"}
528 puts $out {"uuu","lll","ulu","lul"}
531 catchcmd test.db {.import -csv shell5.csv t1
538 Columns renamed during .import shell5.csv due to duplicates:
544 #----------------------------------------------------------------------------
545 # Tests for preserving utf-8 that is not also ASCII.
549 set out [open shell5.csv w]
550 fconfigure $out -translation lf
555 catchcmd test.db {.import -csv shell5.csv t1
562 set out [open shell5.csv w]
563 fconfigure $out -translation lf
568 catchcmd test.db {.import -csv shell5.csv t1
574 # 2024-03-11 https://sqlite.org/forum/forumpost/ca014d7358
575 # Import into a table that contains computed columns.
578 set out [open shell5.csv w]
579 fconfigure $out -translation lf
583 catchcmd :memory: {CREATE TABLE t1(a TEXT, b TEXT, c AS (a||b));
584 .import shell5.csv t1
588 #-------------------------------------------------------------------------
592 set out [open shell5.csv w]
593 fconfigure $out -translation lf
597 catchcmd :memory: {.import --csv shell5.csv '""""""""""""""""""""""""""""""""""""""""""""""'}