2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the COPY statement.
17 # $Id: copy.test,v 1.17 2004/02/17 18:26:57 dougcurrie Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Create a file of data from which to copy.
24 set f [open data1.txt w]
28 set f [open data2.txt w]
33 set f [open data3.txt w]
34 puts $f "11\t22\t33\t44"
37 set f [open data4.txt w]
38 puts $f "11 | 22 | 33"
39 puts $f "22 | 33 | 11"
41 set f [open data5.txt w]
45 set f [open dataX.txt w]
46 fconfigure $f -translation binary
47 puts -nonewline $f "11|22|33\r"
48 puts -nonewline $f "22|33|44\r\n"
49 puts -nonewline $f "33|44|55\n"
50 puts -nonewline $f "44|55|66\r"
51 puts -nonewline $f "55|66|77\r\n"
52 puts -nonewline $f "66|77|88\n"
55 # Try to COPY into a non-existant table.
58 set v [catch {execsql {COPY test1 FROM 'data1.txt'}} msg]
60 } {1 {no such table: test1}}
62 # Try to insert into sqlite_master
65 set v [catch {execsql {COPY sqlite_master FROM 'data2.txt'}} msg]
67 } {1 {table sqlite_master may not be modified}}
69 # Do some actual inserts
72 execsql {CREATE TABLE test1(one int, two int, three int)}
73 execsql {COPY test1 FROM 'data1.txt'}
74 execsql {SELECT * FROM test1 ORDER BY one}
77 # Make sure input terminates at \.
80 execsql {DELETE FROM test1}
81 execsql {COPY test1 FROM 'data2.txt'}
82 execsql {SELECT * FROM test1 ORDER BY one}
85 # Test out the USING DELIMITERS clause
88 execsql {DELETE FROM test1}
89 execsql {COPY test1 FROM 'data4.txt' USING DELIMITERS ' | '}
90 execsql {SELECT * FROM test1 ORDER BY one}
93 execsql {DELETE FROM test1}
94 execsql {COPY test1 FROM 'data5.txt' USING DELIMITERS '|'}
95 execsql {SELECT * FROM test1 ORDER BY one}
98 execsql {DELETE FROM test1}
99 execsql {COPY test1 FROM 'data4.txt' USING DELIMITERS '|'}
100 execsql {SELECT * FROM test1 ORDER BY one}
101 } {{11 } { 22 } { 33} {22 } { 33 } { 11}}
103 # Try copying into a table that has one or more indices.
106 execsql {DELETE FROM test1}
107 execsql {CREATE INDEX index1 ON test1(one)}
108 execsql {CREATE INDEX index2 ON test1(two)}
109 execsql {CREATE INDEX index3 ON test1(three)}
110 execsql {COPY test1 from 'data1.txt'}
111 execsql {SELECT * FROM test1 WHERE one=11}
114 execsql {SELECT * FROM test1 WHERE one=22}
117 execsql {SELECT * FROM test1 WHERE two=22}
120 execsql {SELECT * FROM test1 WHERE three=11}
124 # Try inserting really long data
127 for {set i 0} {$i<100} {incr i} {
128 append x "($i)-abcdefghijklmnopqrstyvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-"
131 execsql {CREATE TABLE test2(a int, x text)}
132 set f [open data21.txt w]
135 execsql {COPY test2 FROM 'data21.txt'}
136 execsql {SELECT x from test2}
138 file delete -force data21.txt
140 # Test the escape character mechanism
143 set fd [open data6.txt w]
144 puts $fd "hello\\\tworld\t1"
145 puts $fd "hello\tworld\\\t2"
148 CREATE TABLE t1(a text, b text);
149 COPY t1 FROM 'data6.txt';
150 SELECT * FROM t1 ORDER BY a;
152 } {hello {world 2} {hello world} 1}
154 set fd [open data6.txt w]
155 puts $fd "1\thello\\\nworld"
156 puts $fd "2\thello world"
160 COPY t1 FROM 'data6.txt';
161 SELECT * FROM t1 ORDER BY a;
164 world} 2 {hello world}}
166 set fd [open data6.txt w]
167 puts $fd "1:hello\\b\\f\\n\\r\\t\\vworld"
168 puts $fd "2:hello world"
172 COPY t1 FROM 'data6.txt' USING DELIMITERS ':';
173 SELECT * FROM t1 ORDER BY a;
175 } [list 1 "hello\b\f\n\r\t\vworld" 2 "hello world"]
177 # Test the embedded NULL logic.
180 set fd [open data6.txt w]
182 puts $fd "\\N\thello world"
186 COPY t1 FROM 'data6.txt';
187 SELECT * FROM t1 WHERE a IS NULL;
192 SELECT * FROM t1 WHERE b IS NULL;
196 # Test the conflict resolution logic for COPY
201 CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE, c);
202 COPY t1 FROM 'data5.txt' USING DELIMITERS '|';
205 } {11 22 33 22 33 11}
207 set fd [open data6.txt w]
211 COPY t1 FROM 'data6.txt' USING DELIMITERS '|';
214 } {1 {column b is not unique}}
216 set fd [open data6.txt w]
220 COPY OR IGNORE t1 FROM 'data6.txt' USING DELIMITERS '|';
223 } {0 {11 22 33 22 33 11}}
225 set fd [open data6.txt w]
229 COPY OR REPLACE t1 FROM 'data6.txt' USING DELIMITERS '|';
232 } {0 {22 33 11 33 22 44}}
237 PRAGMA count_changes=on;
238 COPY t1 FROM 'data5.txt' USING DELIMITERS '|';
243 COPY OR REPLACE t1 FROM 'data5.txt' USING DELIMITERS '|';
248 COPY OR IGNORE t1 FROM 'data5.txt' USING DELIMITERS '|';
254 PRAGMA count_changes=off;
255 CREATE TABLE t2(a,b,c);
256 COPY t2 FROM 'dataX.txt' USING DELIMITERS '|';
259 } {11 22 33 22 33 44 33 44 55 44 55 66 55 66 77 66 77 88}
261 integrity_check copy-7.1
265 #file delete -force data1.txt data2.txt data3.txt data4.txt data5.txt \