xref: /sqlite-3.40.0/test/shell5.test (revision 2f5f6740)
18df9185cSdrh# 2010 August 4
28df9185cSdrh#
38df9185cSdrh# The author disclaims copyright to this source code.  In place of
48df9185cSdrh# a legal notice, here is a blessing:
58df9185cSdrh#
68df9185cSdrh#    May you do good and not evil.
78df9185cSdrh#    May you find forgiveness for yourself and forgive others.
88df9185cSdrh#    May you share freely, never taking more than you give.
98df9185cSdrh#
108df9185cSdrh#***********************************************************************
118df9185cSdrh#
128df9185cSdrh# The focus of this file is testing the CLI shell tool.
138df9185cSdrh# These tests are specific to the .import command.
148df9185cSdrh#
158df9185cSdrh# $Id: shell5.test,v 1.7 2009/07/17 16:54:48 shaneh Exp $
168df9185cSdrh#
178df9185cSdrh
188df9185cSdrh# Test plan:
198df9185cSdrh#
208df9185cSdrh#   shell5-1.*: Basic tests specific to the ".import" command.
218df9185cSdrh#
228df9185cSdrhset testdir [file dirname $argv0]
238df9185cSdrhsource $testdir/tester.tcl
24*2f5f6740Slarrybrset CLI [test_cli_invocation]
258df9185cSdrhdb close
268df9185cSdrhforcedelete test.db test.db-journal test.db-wal
278df9185cSdrh
288df9185cSdrh#----------------------------------------------------------------------------
298df9185cSdrh# Test cases shell5-1.*: Basic handling of the .import and .separator commands.
308df9185cSdrh#
318df9185cSdrh
328df9185cSdrh# .import FILE TABLE     Import data from FILE into TABLE
338df9185cSdrhdo_test shell5-1.1.1 {
348df9185cSdrh  catchcmd "test.db" ".import"
35ccb37816Sdrh} {/1 .ERROR: missing FILE argument.*/}
368df9185cSdrhdo_test shell5-1.1.2 {
378df9185cSdrh  catchcmd "test.db" ".import FOO"
38ccb37816Sdrh} {/1 .ERROR: missing TABLE argument.*/}
398df9185cSdrhdo_test shell5-1.1.3 {
408df9185cSdrh  # too many arguments
418df9185cSdrh  catchcmd "test.db" ".import FOO BAR BAD"
42ccb37816Sdrh} {/1 .ERROR: extra argument.*/}
438df9185cSdrh
448df9185cSdrh# .separator STRING      Change separator used by output mode and .import
45c2ce0beaSdrhdo_test shell5-1.2.1 {
468df9185cSdrh  catchcmd "test.db" ".separator"
47e0d6885fSmistachkin} {1 {Usage: .separator COL ?ROW?}}
48c2ce0beaSdrhdo_test shell5-1.2.2 {
496976c212Sdrh  catchcmd "test.db" ".separator ONE"
508df9185cSdrh} {0 {}}
51c2ce0beaSdrhdo_test shell5-1.2.3 {
526976c212Sdrh  catchcmd "test.db" ".separator ONE TWO"
536976c212Sdrh} {0 {}}
546976c212Sdrhdo_test shell5-1.2.4 {
558df9185cSdrh  # too many arguments
566976c212Sdrh  catchcmd "test.db" ".separator ONE TWO THREE"
57e0d6885fSmistachkin} {1 {Usage: .separator COL ?ROW?}}
588df9185cSdrh
59636bf9f7Smistachkin# column separator should default to "|"
60636bf9f7Smistachkindo_test shell5-1.3.1.1 {
618df9185cSdrh  set res [catchcmd "test.db" ".show"]
62636bf9f7Smistachkin  list [regexp {colseparator: \"\|\"} $res]
63636bf9f7Smistachkin} {1}
64636bf9f7Smistachkin
65636bf9f7Smistachkin# row separator should default to "\n"
66636bf9f7Smistachkindo_test shell5-1.3.1.2 {
67636bf9f7Smistachkin  set res [catchcmd "test.db" ".show"]
68636bf9f7Smistachkin  list [regexp {rowseparator: \"\\n\"} $res]
698df9185cSdrh} {1}
708df9185cSdrh
718df9185cSdrh# set separator to different value.
728df9185cSdrh# check that .show reports new value
738df9185cSdrhdo_test shell5-1.3.2 {
748df9185cSdrh  set res [catchcmd "test.db" {.separator ,
758df9185cSdrh.show}]
768df9185cSdrh  list [regexp {separator: \",\"} $res]
778df9185cSdrh} {1}
788df9185cSdrh
798df9185cSdrh# import file doesn't exist
808df9185cSdrhdo_test shell5-1.4.1 {
819ac99313Smistachkin  forcedelete FOO
828df9185cSdrh  set res [catchcmd "test.db" {CREATE TABLE t1(a, b);
838df9185cSdrh.import FOO t1}]
848df9185cSdrh} {1 {Error: cannot open "FOO"}}
858df9185cSdrh
868df9185cSdrh# empty import file
878df9185cSdrhdo_test shell5-1.4.2 {
889ac99313Smistachkin  forcedelete shell5.csv
898df9185cSdrh  set in [open shell5.csv w]
908df9185cSdrh  close $in
91738d7b9fSlarrybr  set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
92738d7b9fSlarrybr.import -schema test shell5.csv t1
93738d7b9fSlarrybrSELECT COUNT(*) FROM test.t1;}]
948df9185cSdrh} {0 0}
958df9185cSdrh
968df9185cSdrh# import file with 1 row, 1 column (expecting 2 cols)
978df9185cSdrhdo_test shell5-1.4.3 {
988df9185cSdrh  set in [open shell5.csv w]
998df9185cSdrh  puts $in "1"
1008df9185cSdrh  close $in
101738d7b9fSlarrybr  set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
102738d7b9fSlarrybr.import -schema test shell5.csv t1}]
103db95f68bSdrh} {1 {shell5.csv:1: expected 2 columns but found 1 - filling the rest with NULL}}
1048df9185cSdrh
1058df9185cSdrh# import file with 1 row, 3 columns (expecting 2 cols)
1068df9185cSdrhdo_test shell5-1.4.4 {
1078df9185cSdrh  set in [open shell5.csv w]
1088df9185cSdrh  puts $in "1|2|3"
1098df9185cSdrh  close $in
110738d7b9fSlarrybr  set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
111738d7b9fSlarrybr.import --schema test shell5.csv t1}]
112db95f68bSdrh} {1 {shell5.csv:1: expected 2 columns but found 3 - extras ignored}}
1138df9185cSdrh
1148df9185cSdrh# import file with 1 row, 2 columns
1158df9185cSdrhdo_test shell5-1.4.5 {
1168df9185cSdrh  set in [open shell5.csv w]
1178df9185cSdrh  puts $in "1|2"
1188df9185cSdrh  close $in
119db95f68bSdrh  set res [catchcmd "test.db" {DELETE FROM t1;
120db95f68bSdrh.import shell5.csv t1
1218df9185cSdrhSELECT COUNT(*) FROM t1;}]
1228df9185cSdrh} {0 1}
1238df9185cSdrh
1248df9185cSdrh# import file with 2 rows, 2 columns
1258df9185cSdrh# note we end up with 3 rows because of the 1 row
1268df9185cSdrh# imported above.
1278df9185cSdrhdo_test shell5-1.4.6 {
1288df9185cSdrh  set in [open shell5.csv w]
1298df9185cSdrh  puts $in "2|3"
1308df9185cSdrh  puts $in "3|4"
1318df9185cSdrh  close $in
132738d7b9fSlarrybr  set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
133738d7b9fSlarrybr.import -schema test shell5.csv t1
134738d7b9fSlarrybrSELECT COUNT(*) FROM test.t1;}]
1358df9185cSdrh} {0 3}
1368df9185cSdrh
1378df9185cSdrh# import file with 1 row, 2 columns, using a comma
1388df9185cSdrhdo_test shell5-1.4.7 {
1398df9185cSdrh  set in [open shell5.csv w]
1408df9185cSdrh  puts $in "4,5"
1418df9185cSdrh  close $in
142738d7b9fSlarrybr  set res [catchcmd ":memory:" {ATTACH 'test.db' AS test;
143738d7b9fSlarrybr.separator ,
144738d7b9fSlarrybr.import --schema test shell5.csv t1
145738d7b9fSlarrybrSELECT COUNT(*) FROM test.t1;}]
1468df9185cSdrh} {0 4}
1478df9185cSdrh
1488df9185cSdrh# import file with 1 row, 2 columns, text data
1498df9185cSdrhdo_test shell5-1.4.8.1 {
1508df9185cSdrh  set in [open shell5.csv w]
1518df9185cSdrh  puts $in "5|Now is the time for all good men to come to the aid of their country."
1528df9185cSdrh  close $in
1538df9185cSdrh  set res [catchcmd "test.db" {.import shell5.csv t1
1548df9185cSdrhSELECT COUNT(*) FROM t1;}]
1558df9185cSdrh} {0 5}
1568df9185cSdrh
1578df9185cSdrhdo_test shell5-1.4.8.2 {
1588df9185cSdrh  catchcmd "test.db" {SELECT b FROM t1 WHERE a='5';}
1598df9185cSdrh} {0 {Now is the time for all good men to come to the aid of their country.}}
1608df9185cSdrh
1618df9185cSdrh# import file with 1 row, 2 columns, quoted text data
1628df9185cSdrh# note that currently sqlite doesn't support quoted fields, and
1638df9185cSdrh# imports the entire field, quotes and all.
1648df9185cSdrhdo_test shell5-1.4.9.1 {
1658df9185cSdrh  set in [open shell5.csv w]
1668df9185cSdrh  puts $in "6|'Now is the time for all good men to come to the aid of their country.'"
1678df9185cSdrh  close $in
1688df9185cSdrh  set res [catchcmd "test.db" {.import shell5.csv t1
1698df9185cSdrhSELECT COUNT(*) FROM t1;}]
1708df9185cSdrh} {0 6}
1718df9185cSdrh
1728df9185cSdrhdo_test shell5-1.4.9.2 {
1738df9185cSdrh  catchcmd "test.db" {SELECT b FROM t1 WHERE a='6';}
1748df9185cSdrh} {0 {'Now is the time for all good men to come to the aid of their country.'}}
1758df9185cSdrh
1768df9185cSdrh# import file with 1 row, 2 columns, quoted text data
1778df9185cSdrhdo_test shell5-1.4.10.1 {
1788df9185cSdrh  set in [open shell5.csv w]
1798df9185cSdrh  puts $in "7|\"Now is the time for all good men to come to the aid of their country.\""
1808df9185cSdrh  close $in
1818df9185cSdrh  set res [catchcmd "test.db" {.import shell5.csv t1
1828df9185cSdrhSELECT COUNT(*) FROM t1;}]
1838df9185cSdrh} {0 7}
1848df9185cSdrh
1858df9185cSdrhdo_test shell5-1.4.10.2 {
1868df9185cSdrh  catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';}
1878df9185cSdrh} {0 {Now is the time for all good men to come to the aid of their country.}}
1888df9185cSdrh
189d5fbde80Sdrh# import file with 2 rows, 2 columns and an initial BOM
190d5fbde80Sdrh#
191d5fbde80Sdrhdo_test shell5-1.4.11 {
192d5fbde80Sdrh  set in [open shell5.csv wb]
193a4d770cfSdrh  puts -nonewline $in "\xef\xbb\xbf"
194a4d770cfSdrh  puts $in "2|3"
195d5fbde80Sdrh  puts $in "4|5"
196d5fbde80Sdrh  close $in
197d5fbde80Sdrh  set res [catchcmd "test.db" {CREATE TABLE t2(x INT, y INT);
198d5fbde80Sdrh.import shell5.csv t2
199d5fbde80Sdrh.mode quote
200d5fbde80Sdrh.header on
201d5fbde80SdrhSELECT * FROM t2;}]
202d5fbde80Sdrh string map {\n | \n\r |} $res
203d5fbde80Sdrh} {0 {'x','y'|2,3|4,5}}
204d5fbde80Sdrh
205d5fbde80Sdrh# import file with 2 rows, 2 columns or text with an initial BOM
206d5fbde80Sdrh#
207d5fbde80Sdrhdo_test shell5-1.4.12 {
208d5fbde80Sdrh  set in [open shell5.csv wb]
209d5fbde80Sdrh  puts $in "\xef\xbb\xbf\"two\"|3"
210d5fbde80Sdrh  puts $in "4|5"
211d5fbde80Sdrh  close $in
212d5fbde80Sdrh  set res [catchcmd "test.db" {DELETE FROM t2;
213d5fbde80Sdrh.import shell5.csv t2
214d5fbde80Sdrh.mode quote
215d5fbde80Sdrh.header on
216d5fbde80SdrhSELECT * FROM t2;}]
217d5fbde80Sdrh string map {\n | \n\r |} $res
218d5fbde80Sdrh} {0 {'x','y'|'two',3|4,5}}
219d5fbde80Sdrh
2208df9185cSdrh# check importing very long field
2218df9185cSdrhdo_test shell5-1.5.1 {
2228df9185cSdrh  set str [string repeat X 999]
2238df9185cSdrh  set in [open shell5.csv w]
2248df9185cSdrh  puts $in "8|$str"
2258df9185cSdrh  close $in
2268df9185cSdrh  set res [catchcmd "test.db" {.import shell5.csv t1
2278df9185cSdrhSELECT length(b) FROM t1 WHERE a='8';}]
2288df9185cSdrh} {0 999}
2298df9185cSdrh
2308df9185cSdrh# try importing into a table with a large number of columns.
2318df9185cSdrh# This is limited by SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999.
2328df9185cSdrhset cols 999
2338df9185cSdrhdo_test shell5-1.6.1 {
2348df9185cSdrh  set data {}
2358df9185cSdrh  for {set i 1} {$i<$cols} {incr i} {
236db95f68bSdrh    append data "c$i|"
237db95f68bSdrh  }
238db95f68bSdrh  append data "c$cols\n";
239db95f68bSdrh  for {set i 1} {$i<$cols} {incr i} {
2408df9185cSdrh    append data "$i|"
2418df9185cSdrh  }
2428df9185cSdrh  append data "$cols"
2438df9185cSdrh  set in [open shell5.csv w]
2448df9185cSdrh  puts $in $data
2458df9185cSdrh  close $in
246d5fbde80Sdrh  set res [catchcmd "test.db" {DROP TABLE IF EXISTS t2;
247d5fbde80Sdrh.import shell5.csv t2
2488df9185cSdrhSELECT COUNT(*) FROM t2;}]
2498df9185cSdrh} {0 1}
2508df9185cSdrh
2518df9185cSdrh# try importing a large number of rows
2525bde8165Sdrhset rows 9999
2538df9185cSdrhdo_test shell5-1.7.1 {
2548df9185cSdrh  set in [open shell5.csv w]
255db95f68bSdrh  puts $in a
2568df9185cSdrh  for {set i 1} {$i<=$rows} {incr i} {
2578df9185cSdrh    puts $in $i
2588df9185cSdrh  }
2598df9185cSdrh  close $in
260db95f68bSdrh  set res [catchcmd "test.db" {.mode csv
2618df9185cSdrh.import shell5.csv t3
2628df9185cSdrhSELECT COUNT(*) FROM t3;}]
2638df9185cSdrh} [list 0 $rows]
2648df9185cSdrh
26553e1186dSlarrybr# Import from a pipe.  (Unix only, as it requires "awk")
2665bde8165Sdrhif {$tcl_platform(platform)=="unix"} {
2675bde8165Sdrh  do_test shell5-1.8 {
2689ac99313Smistachkin    forcedelete test.db
2695bde8165Sdrh    catchcmd test.db {.mode csv
2705bde8165Sdrh.import "|awk 'END{print \"x,y\";for(i=1;i<=5;i++){print i \",this is \" i}}'" t1
2715bde8165SdrhSELECT * FROM t1;}
2725bde8165Sdrh  } {0 {1,"this is 1"
2735bde8165Sdrh2,"this is 2"
2745bde8165Sdrh3,"this is 3"
2755bde8165Sdrh4,"this is 4"
2765bde8165Sdrh5,"this is 5"}}
2775bde8165Sdrh}
2785bde8165Sdrh
279f7f8de5fSdrh# Import columns containing quoted strings
280f7f8de5fSdrhdo_test shell5-1.9 {
281f7f8de5fSdrh  set out [open shell5.csv w]
2829ac99313Smistachkin  fconfigure $out -translation lf
283f7f8de5fSdrh  puts $out {1,"",11}
284f7f8de5fSdrh  puts $out {2,"x",22}
285f7f8de5fSdrh  puts $out {3,"""",33}
286f7f8de5fSdrh  puts $out {4,"hello",44}
287868ccf26Sdrh  puts $out "5,55,\"\"\r"
288f7f8de5fSdrh  puts $out {6,66,"x"}
289f7f8de5fSdrh  puts $out {7,77,""""}
290f7f8de5fSdrh  puts $out {8,88,"hello"}
291f7f8de5fSdrh  puts $out {"",9,99}
292f7f8de5fSdrh  puts $out {"x",10,110}
293f7f8de5fSdrh  puts $out {"""",11,121}
294f7f8de5fSdrh  puts $out {"hello",12,132}
295f7f8de5fSdrh  close $out
2969ac99313Smistachkin  forcedelete test.db
297f7f8de5fSdrh  catchcmd test.db {.mode csv
298f7f8de5fSdrh    CREATE TABLE t1(a,b,c);
299f7f8de5fSdrh.import shell5.csv t1
300f7f8de5fSdrh  }
301f7f8de5fSdrh  sqlite3 db test.db
302f7f8de5fSdrh  db eval {SELECT *, '|' FROM t1 ORDER BY rowid}
303f7f8de5fSdrh} {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 |}
304f7f8de5fSdrhdb close
305f7f8de5fSdrh
306a81ad175Sdrh# Import columns containing quoted strings
307a81ad175Sdrhdo_test shell5-1.10 {
308a81ad175Sdrh  set out [open shell5.csv w]
309a81ad175Sdrh  fconfigure $out -translation lf
310a81ad175Sdrh  puts $out {column1,column2,column3,column4}
311a81ad175Sdrh  puts $out "field1,field2,\"x3 \"\"\r\ndata\"\" 3\",field4"
312a81ad175Sdrh  puts $out "x1,x2,\"x3 \"\"\ndata\"\" 3\",x4"
313a81ad175Sdrh  close $out
314a81ad175Sdrh  forcedelete test.db
315a81ad175Sdrh  catchcmd test.db {.mode csv
316a81ad175Sdrh    CREATE TABLE t1(a,b,c,d);
317a81ad175Sdrh.import shell5.csv t1
318a81ad175Sdrh  }
319a81ad175Sdrh  sqlite3 db test.db
320a81ad175Sdrh  db eval {SELECT hex(c) FROM t1 ORDER BY rowid}
321a81ad175Sdrh} {636F6C756D6E33 783320220D0A64617461222033 783320220A64617461222033}
322a81ad175Sdrh
3233852b68eSdrh# Blank last column with \r\n line endings.
3243852b68eSdrhdo_test shell5-1.11 {
3253852b68eSdrh  set out [open shell5.csv w]
3263852b68eSdrh  fconfigure $out -translation binary
3273852b68eSdrh  puts $out "column1,column2,column3\r"
3283852b68eSdrh  puts $out "a,b, \r"
3293852b68eSdrh  puts $out "x,y,\r"
3303852b68eSdrh  puts $out "p,q,r\r"
3313852b68eSdrh  close $out
3323852b68eSdrh  catch {db close}
3333852b68eSdrh  forcedelete test.db
3343852b68eSdrh  catchcmd test.db {.mode csv
3353852b68eSdrh.import shell5.csv t1
3363852b68eSdrh  }
3373852b68eSdrh  sqlite3 db test.db
3383852b68eSdrh  db eval {SELECT *, '|' FROM t1}
3393852b68eSdrh} {a b { } | x y {} | p q r |}
340a81ad175Sdrhdb close
341a81ad175Sdrh
3426a8ac852Sdan#----------------------------------------------------------------------------
3436a8ac852Sdan#
3446a8ac852Sdanreset_db
3456a8ac852Sdansqlite3 db test.db
3466a8ac852Sdando_test shell5-2.1 {
3476a8ac852Sdan  set fd [open shell5.csv w]
3486a8ac852Sdan  puts $fd ",hello"
3496a8ac852Sdan  close $fd
3506a8ac852Sdan  catchcmd test.db [string trim {
3516a8ac852Sdan.mode csv
3526a8ac852SdanCREATE TABLE t1(a, b);
3536a8ac852Sdan.import shell5.csv t1
3546a8ac852Sdan  }]
3556a8ac852Sdan  db eval { SELECT * FROM t1 }
3566a8ac852Sdan} {{} hello}
3576a8ac852Sdan
3586a8ac852Sdando_test shell5-2.2 {
3596a8ac852Sdan  set fd [open shell5.csv w]
3606a8ac852Sdan  puts $fd {"",hello}
3616a8ac852Sdan  close $fd
3626a8ac852Sdan  catchcmd test.db [string trim {
3636a8ac852Sdan.mode csv
3646a8ac852SdanCREATE TABLE t2(a, b);
3656a8ac852Sdan.import shell5.csv t2
3666a8ac852Sdan  }]
3676a8ac852Sdan  db eval { SELECT * FROM t2 }
3686a8ac852Sdan} {{} hello}
3696a8ac852Sdan
3706a8ac852Sdando_test shell5-2.3 {
3716a8ac852Sdan  set fd [open shell5.csv w]
3726a8ac852Sdan  puts $fd {"x""y",hello}
3736a8ac852Sdan  close $fd
3746a8ac852Sdan  catchcmd test.db [string trim {
3756a8ac852Sdan.mode csv
3766a8ac852SdanCREATE TABLE t3(a, b);
3776a8ac852Sdan.import shell5.csv t3
3786a8ac852Sdan  }]
3796a8ac852Sdan  db eval { SELECT * FROM t3 }
3806a8ac852Sdan} {x\"y hello}
3816a8ac852Sdan
3826a8ac852Sdando_test shell5-2.4 {
3836a8ac852Sdan  set fd [open shell5.csv w]
3846a8ac852Sdan  puts $fd {"xy""",hello}
3856a8ac852Sdan  close $fd
3866a8ac852Sdan  catchcmd test.db [string trim {
3876a8ac852Sdan.mode csv
3886a8ac852SdanCREATE TABLE t4(a, b);
3896a8ac852Sdan.import shell5.csv t4
3906a8ac852Sdan  }]
3916a8ac852Sdan  db eval { SELECT * FROM t4 }
3926a8ac852Sdan} {xy\" hello}
3936a8ac852Sdan
3946fe0338cSmistachkindo_test shell5-2.5 {
3956fe0338cSmistachkin  set fd [open shell5.csv w]
3966fe0338cSmistachkin  puts $fd {"one","2"}
3976fe0338cSmistachkin  puts $fd {}
3986fe0338cSmistachkin  close $fd
3996fe0338cSmistachkin  catchcmd test.db [string trim {
4006fe0338cSmistachkin.mode csv
4016fe0338cSmistachkinCREATE TABLE t4(a, b);
4026fe0338cSmistachkin.import shell5.csv t4
4036fe0338cSmistachkin  }]
4046fe0338cSmistachkin  db eval { SELECT * FROM t4 }
4056fe0338cSmistachkin} {xy\" hello one 2 {} {}}
4066a8ac852Sdan
407636bf9f7Smistachkin#----------------------------------------------------------------------------
408636bf9f7Smistachkin# Tests for the shell "ascii" import/export mode.
409636bf9f7Smistachkin#
410636bf9f7Smistachkindo_test shell5-3.1 {
411636bf9f7Smistachkin  set fd [open shell5.csv w]
412636bf9f7Smistachkin  fconfigure $fd -encoding binary -translation binary
413636bf9f7Smistachkin  puts -nonewline $fd "\"test 1\"\x1F,test 2\r\n\x1E"
414636bf9f7Smistachkin  puts -nonewline $fd "test 3\x1Ftest 4\n"
415636bf9f7Smistachkin  close $fd
416636bf9f7Smistachkin  catchcmd test.db {
417636bf9f7Smistachkin.mode ascii
418636bf9f7SmistachkinCREATE TABLE t5(a, b);
419636bf9f7Smistachkin.import shell5.csv t5
420636bf9f7Smistachkin  }
421636bf9f7Smistachkin  db eval { SELECT * FROM t5 }
422636bf9f7Smistachkin} "\{\"test 1\"} \{,test 2\r\n\} \{test 3\} \{test 4\n\}"
423636bf9f7Smistachkin
424636bf9f7Smistachkindo_test shell5-3.2 {
425fa5ed028Sdrh  set x [catchcmd test.db {
426636bf9f7Smistachkin.mode ascii
427636bf9f7SmistachkinSELECT * FROM t5;
428fa5ed028Sdrh  }]
429fa5ed028Sdrh  # Handle platform end-of-line differences
430fa5ed028Sdrh  regsub -all {[\n\r]?\n} $x <EOL> x
431fa5ed028Sdrh  set x
432fa5ed028Sdrh} "0 \{\"test 1\"\x1F,test 2<EOL>\x1Etest 3\x1Ftest 4<EOL>\x1E\}"
4336a8ac852Sdan
434a0efb1aeSmistachkindo_test shell5-4.1 {
435a0efb1aeSmistachkin  forcedelete shell5.csv
436a0efb1aeSmistachkin  set fd [open shell5.csv w]
437a0efb1aeSmistachkin  puts $fd "1,2,3"
438a0efb1aeSmistachkin  puts $fd "4,5"
439a0efb1aeSmistachkin  puts $fd "6,7,8"
440a0efb1aeSmistachkin  close $fd
441a0efb1aeSmistachkin  catchcmd test.db [string trim {
442a0efb1aeSmistachkin.mode csv
443a0efb1aeSmistachkinCREATE TABLE t6(a, b, c);
444a0efb1aeSmistachkin.import shell5.csv t6
445a0efb1aeSmistachkin  }]
446a0efb1aeSmistachkin  db eval { SELECT * FROM t6 ORDER BY a }
447a0efb1aeSmistachkin} {1 2 3 4 5 {} 6 7 8}
448a0efb1aeSmistachkin
449a0efb1aeSmistachkindo_test shell5-4.2 {
450a0efb1aeSmistachkin  forcedelete shell5.csv
451a0efb1aeSmistachkin  set fd [open shell5.csv w]
452a0efb1aeSmistachkin  puts $fd "1,2,3"
453a0efb1aeSmistachkin  puts $fd "4,5"
454a0efb1aeSmistachkin  puts $fd "6,7,8,9"
455a0efb1aeSmistachkin  close $fd
456a0efb1aeSmistachkin  catchcmd test.db [string trim {
457a0efb1aeSmistachkin.mode csv
458a0efb1aeSmistachkinCREATE TABLE t7(a, b, c);
459a0efb1aeSmistachkin.import shell5.csv t7
460a0efb1aeSmistachkin  }]
461a0efb1aeSmistachkin  db eval { SELECT * FROM t7 ORDER BY a }
462a0efb1aeSmistachkin} {1 2 3 4 5 {} 6 7 8}
463a0efb1aeSmistachkin
4646ac9a5cfSlarrybrdo_test shell5-4.3 {
4656ac9a5cfSlarrybr  forcedelete shell5.csv
4666ac9a5cfSlarrybr  set fd [open shell5.csv w]
4676ac9a5cfSlarrybr  puts $fd ",,"
4686ac9a5cfSlarrybr  puts $fd "1,2,3"
4696ac9a5cfSlarrybr  close $fd
4706ac9a5cfSlarrybr  catchcmd test.db [string trim {
4716ac9a5cfSlarrybr.mode csv
4726ac9a5cfSlarrybrCREATE TABLE t8(a, b, c);
4736ac9a5cfSlarrybr.import -skip 1 shell5.csv t8
4746ac9a5cfSlarrybr.nullvalue #
4756ac9a5cfSlarrybr  }]
4766ac9a5cfSlarrybr  db eval { SELECT * FROM t8 }
4776ac9a5cfSlarrybr} {1 2 3}
4786ac9a5cfSlarrybr
47953e1186dSlarrybrdo_test shell5-4.4 {
48053e1186dSlarrybr  forcedelete shell5.csv
48153e1186dSlarrybr  set fd [open shell5.csv w]
48253e1186dSlarrybr  puts $fd "1,2,3"
48353e1186dSlarrybr  close $fd
48453e1186dSlarrybr  catchcmd test.db [string trim {
48553e1186dSlarrybr.mode csv
48653e1186dSlarrybrCREATE TEMP TABLE t8(a, b, c);
48753e1186dSlarrybr.import shell5.csv t8
48853e1186dSlarrybr.nullvalue #
48953e1186dSlarrybrSELECT * FROM temp.t8
49053e1186dSlarrybr  }]
49153e1186dSlarrybr} {0 1,2,3}
49253e1186dSlarrybr
493a0337274Slarrybr#----------------------------------------------------------------------------
494a0337274Slarrybr# Tests for the shell automatic column rename.
495a0337274Slarrybr#
49624ea5fb3Sdrhdb close
497a0337274Slarrybr
498a0337274Slarrybr# Import columns containing duplicates
499a0337274Slarrybrdo_test shell5-5.1 {
500a0337274Slarrybr  set out [open shell5.csv w]
501a0337274Slarrybr  fconfigure $out -translation lf
50233633861Slarrybr  puts $out {"","x","x","y","z","z_0","z_5","z"}
50333633861Slarrybr  puts $out {0,"x2","x3","y4","z5","z6","z7","z8"}
504a0337274Slarrybr  close $out
505a0337274Slarrybr  forcedelete test.db
506a0337274Slarrybr  catchcmd test.db {.import -csv shell5.csv t1
507a0337274Slarrybr.mode line
508a0337274SlarrybrSELECT * FROM t1;}
509a0337274Slarrybr} {1 {    ? = 0
51033633861Slarrybr x_02 = x2
51133633861Slarrybr x_03 = x3
51233633861Slarrybr    y = y4
51333633861Slarrybr z_05 = z5
51433633861Slarrybr  z_0 = z6
51533633861Slarrybr  z_5 = z7
51633633861Slarrybr z_08 = z8
51733633861SlarrybrColumns renamed during .import shell5.csv due to duplicates:
51833633861Slarrybr"x" to "x_02",
51933633861Slarrybr"x" to "x_03",
52033633861Slarrybr"z" to "z_05",
52133633861Slarrybr"z" to "z_08"}}
522a0337274Slarrybr
5230a704c31Slarrybrdo_test shell5-5.1 {
5240a704c31Slarrybr  set out [open shell5.csv w]
5250a704c31Slarrybr  fconfigure $out -translation lf
5260a704c31Slarrybr  puts $out {"COW","cow","CoW","cOw"}
5270a704c31Slarrybr  puts $out {"uuu","lll","ulu","lul"}
5280a704c31Slarrybr  close $out
5290a704c31Slarrybr  forcedelete test.db
5300a704c31Slarrybr  catchcmd test.db {.import -csv shell5.csv t1
5310a704c31Slarrybr.mode line
5320a704c31SlarrybrSELECT * FROM t1;}
5330a704c31Slarrybr} {1 {COW_1 = uuu
5340a704c31Slarrybrcow_2 = lll
5350a704c31SlarrybrCoW_3 = ulu
5360a704c31SlarrybrcOw_4 = lul
53733633861SlarrybrColumns renamed during .import shell5.csv due to duplicates:
53833633861Slarrybr"COW" to "COW_1",
53933633861Slarrybr"cow" to "cow_2",
54033633861Slarrybr"CoW" to "CoW_3",
54133633861Slarrybr"cOw" to "cOw_4"}}
5420a704c31Slarrybr
543039132beSlarrybr#----------------------------------------------------------------------------
544039132beSlarrybr# Tests for preserving utf-8 that is not also ASCII.
545039132beSlarrybr#
546039132beSlarrybr
547039132beSlarrybrdo_test shell5-6.1 {
548039132beSlarrybr  set out [open shell5.csv w]
549039132beSlarrybr  fconfigure $out -translation lf
550039132beSlarrybr  puts $out {あい,うえお}
551039132beSlarrybr  puts $out {1,2}
552039132beSlarrybr  close $out
553039132beSlarrybr  forcedelete test.db
554039132beSlarrybr  catchcmd test.db {.import -csv shell5.csv t1
555039132beSlarrybr.mode line
556039132beSlarrybrSELECT * FROM t1;}
557039132beSlarrybr} {0 {   あい = 1
558039132beSlarrybrうえお = 2}}
559039132beSlarrybr
560039132beSlarrybrdo_test shell5-6.2 {
561039132beSlarrybr  set out [open shell5.csv w]
562039132beSlarrybr  fconfigure $out -translation lf
563039132beSlarrybr  puts $out {1,2}
564039132beSlarrybr  puts $out {あい,うえお}
565039132beSlarrybr  close $out
566039132beSlarrybr  forcedelete test.db
567039132beSlarrybr  catchcmd test.db {.import -csv shell5.csv t1
568039132beSlarrybr.mode line
569039132beSlarrybrSELECT * FROM t1;}
570039132beSlarrybr} {0 {    1 = あい
571039132beSlarrybr    2 = うえお}}
572039132beSlarrybr
5738df9185cSdrhfinish_test
574