xref: /titanic_50/usr/src/lib/libsqlite/test/copy.test (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292
2*c5c4113dSnw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292# 2001 September 15
5*c5c4113dSnw141292#
6*c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
7*c5c4113dSnw141292# a legal notice, here is a blessing:
8*c5c4113dSnw141292#
9*c5c4113dSnw141292#    May you do good and not evil.
10*c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
11*c5c4113dSnw141292#    May you share freely, never taking more than you give.
12*c5c4113dSnw141292#
13*c5c4113dSnw141292#***********************************************************************
14*c5c4113dSnw141292# This file implements regression tests for SQLite library.  The
15*c5c4113dSnw141292# focus of this file is testing the COPY statement.
16*c5c4113dSnw141292#
17*c5c4113dSnw141292# $Id: copy.test,v 1.17 2004/02/17 18:26:57 dougcurrie Exp $
18*c5c4113dSnw141292
19*c5c4113dSnw141292set testdir [file dirname $argv0]
20*c5c4113dSnw141292source $testdir/tester.tcl
21*c5c4113dSnw141292
22*c5c4113dSnw141292# Create a file of data from which to copy.
23*c5c4113dSnw141292#
24*c5c4113dSnw141292set f [open data1.txt w]
25*c5c4113dSnw141292puts $f "11\t22\t33"
26*c5c4113dSnw141292puts $f "22\t33\t11"
27*c5c4113dSnw141292close $f
28*c5c4113dSnw141292set f [open data2.txt w]
29*c5c4113dSnw141292puts $f "11\t22\t33"
30*c5c4113dSnw141292puts $f "\\."
31*c5c4113dSnw141292puts $f "22\t33\t11"
32*c5c4113dSnw141292close $f
33*c5c4113dSnw141292set f [open data3.txt w]
34*c5c4113dSnw141292puts $f "11\t22\t33\t44"
35*c5c4113dSnw141292puts $f "22\t33\t11"
36*c5c4113dSnw141292close $f
37*c5c4113dSnw141292set f [open data4.txt w]
38*c5c4113dSnw141292puts $f "11 | 22 | 33"
39*c5c4113dSnw141292puts $f "22 | 33 | 11"
40*c5c4113dSnw141292close $f
41*c5c4113dSnw141292set f [open data5.txt w]
42*c5c4113dSnw141292puts $f "11|22|33"
43*c5c4113dSnw141292puts $f "22|33|11"
44*c5c4113dSnw141292close $f
45*c5c4113dSnw141292set f [open dataX.txt w]
46*c5c4113dSnw141292fconfigure $f -translation binary
47*c5c4113dSnw141292puts -nonewline $f "11|22|33\r"
48*c5c4113dSnw141292puts -nonewline $f "22|33|44\r\n"
49*c5c4113dSnw141292puts -nonewline $f "33|44|55\n"
50*c5c4113dSnw141292puts -nonewline $f "44|55|66\r"
51*c5c4113dSnw141292puts -nonewline $f "55|66|77\r\n"
52*c5c4113dSnw141292puts -nonewline $f "66|77|88\n"
53*c5c4113dSnw141292close $f
54*c5c4113dSnw141292
55*c5c4113dSnw141292# Try to COPY into a non-existant table.
56*c5c4113dSnw141292#
57*c5c4113dSnw141292do_test copy-1.1 {
58*c5c4113dSnw141292  set v [catch {execsql {COPY test1 FROM 'data1.txt'}} msg]
59*c5c4113dSnw141292  lappend v $msg
60*c5c4113dSnw141292} {1 {no such table: test1}}
61*c5c4113dSnw141292
62*c5c4113dSnw141292# Try to insert into sqlite_master
63*c5c4113dSnw141292#
64*c5c4113dSnw141292do_test copy-1.2 {
65*c5c4113dSnw141292  set v [catch {execsql {COPY sqlite_master FROM 'data2.txt'}} msg]
66*c5c4113dSnw141292  lappend v $msg
67*c5c4113dSnw141292} {1 {table sqlite_master may not be modified}}
68*c5c4113dSnw141292
69*c5c4113dSnw141292# Do some actual inserts
70*c5c4113dSnw141292#
71*c5c4113dSnw141292do_test copy-1.3 {
72*c5c4113dSnw141292  execsql {CREATE TABLE test1(one int, two int, three int)}
73*c5c4113dSnw141292  execsql {COPY test1 FROM 'data1.txt'}
74*c5c4113dSnw141292  execsql {SELECT * FROM test1 ORDER BY one}
75*c5c4113dSnw141292} {11 22 33 22 33 11}
76*c5c4113dSnw141292
77*c5c4113dSnw141292# Make sure input terminates at \.
78*c5c4113dSnw141292#
79*c5c4113dSnw141292do_test copy-1.4 {
80*c5c4113dSnw141292  execsql {DELETE FROM test1}
81*c5c4113dSnw141292  execsql {COPY test1 FROM 'data2.txt'}
82*c5c4113dSnw141292  execsql {SELECT * FROM test1 ORDER BY one}
83*c5c4113dSnw141292} {11 22 33}
84*c5c4113dSnw141292
85*c5c4113dSnw141292# Test out the USING DELIMITERS clause
86*c5c4113dSnw141292#
87*c5c4113dSnw141292do_test copy-1.5 {
88*c5c4113dSnw141292  execsql {DELETE FROM test1}
89*c5c4113dSnw141292  execsql {COPY test1 FROM 'data4.txt' USING DELIMITERS ' | '}
90*c5c4113dSnw141292  execsql {SELECT * FROM test1 ORDER BY one}
91*c5c4113dSnw141292} {11 22 33 22 33 11}
92*c5c4113dSnw141292do_test copy-1.6 {
93*c5c4113dSnw141292  execsql {DELETE FROM test1}
94*c5c4113dSnw141292  execsql {COPY test1 FROM 'data5.txt' USING DELIMITERS '|'}
95*c5c4113dSnw141292  execsql {SELECT * FROM test1 ORDER BY one}
96*c5c4113dSnw141292} {11 22 33 22 33 11}
97*c5c4113dSnw141292do_test copy-1.7 {
98*c5c4113dSnw141292  execsql {DELETE FROM test1}
99*c5c4113dSnw141292  execsql {COPY test1 FROM 'data4.txt' USING DELIMITERS '|'}
100*c5c4113dSnw141292  execsql {SELECT * FROM test1 ORDER BY one}
101*c5c4113dSnw141292} {{11 } { 22 } { 33} {22 } { 33 } { 11}}
102*c5c4113dSnw141292
103*c5c4113dSnw141292# Try copying into a table that has one or more indices.
104*c5c4113dSnw141292#
105*c5c4113dSnw141292do_test copy-1.8 {
106*c5c4113dSnw141292  execsql {DELETE FROM test1}
107*c5c4113dSnw141292  execsql {CREATE INDEX index1 ON test1(one)}
108*c5c4113dSnw141292  execsql {CREATE INDEX index2 ON test1(two)}
109*c5c4113dSnw141292  execsql {CREATE INDEX index3 ON test1(three)}
110*c5c4113dSnw141292  execsql {COPY test1 from 'data1.txt'}
111*c5c4113dSnw141292  execsql {SELECT * FROM test1 WHERE one=11}
112*c5c4113dSnw141292} {11 22 33}
113*c5c4113dSnw141292do_test copy-1.8b {
114*c5c4113dSnw141292  execsql {SELECT * FROM test1 WHERE one=22}
115*c5c4113dSnw141292} {22 33 11}
116*c5c4113dSnw141292do_test copy-1.8c {
117*c5c4113dSnw141292  execsql {SELECT * FROM test1 WHERE two=22}
118*c5c4113dSnw141292} {11 22 33}
119*c5c4113dSnw141292do_test copy-1.8d {
120*c5c4113dSnw141292  execsql {SELECT * FROM test1 WHERE three=11}
121*c5c4113dSnw141292} {22 33 11}
122*c5c4113dSnw141292
123*c5c4113dSnw141292
124*c5c4113dSnw141292# Try inserting really long data
125*c5c4113dSnw141292#
126*c5c4113dSnw141292set x {}
127*c5c4113dSnw141292for {set i 0} {$i<100} {incr i} {
128*c5c4113dSnw141292  append x "($i)-abcdefghijklmnopqrstyvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-"
129*c5c4113dSnw141292}
130*c5c4113dSnw141292do_test copy-2.1 {
131*c5c4113dSnw141292  execsql {CREATE TABLE test2(a int, x text)}
132*c5c4113dSnw141292  set f [open data21.txt w]
133*c5c4113dSnw141292  puts $f "123\t$x"
134*c5c4113dSnw141292  close $f
135*c5c4113dSnw141292  execsql {COPY test2 FROM 'data21.txt'}
136*c5c4113dSnw141292  execsql {SELECT x from test2}
137*c5c4113dSnw141292} $x
138*c5c4113dSnw141292file delete -force data21.txt
139*c5c4113dSnw141292
140*c5c4113dSnw141292# Test the escape character mechanism
141*c5c4113dSnw141292#
142*c5c4113dSnw141292do_test copy-3.1 {
143*c5c4113dSnw141292  set fd [open data6.txt w]
144*c5c4113dSnw141292  puts $fd "hello\\\tworld\t1"
145*c5c4113dSnw141292  puts $fd "hello\tworld\\\t2"
146*c5c4113dSnw141292  close $fd
147*c5c4113dSnw141292  execsql {
148*c5c4113dSnw141292    CREATE TABLE t1(a text, b text);
149*c5c4113dSnw141292    COPY t1 FROM 'data6.txt';
150*c5c4113dSnw141292    SELECT * FROM t1 ORDER BY a;
151*c5c4113dSnw141292  }
152*c5c4113dSnw141292} {hello {world	2} {hello	world} 1}
153*c5c4113dSnw141292do_test copy-3.2 {
154*c5c4113dSnw141292  set fd [open data6.txt w]
155*c5c4113dSnw141292  puts $fd "1\thello\\\nworld"
156*c5c4113dSnw141292  puts $fd "2\thello world"
157*c5c4113dSnw141292  close $fd
158*c5c4113dSnw141292  execsql {
159*c5c4113dSnw141292    DELETE FROM t1;
160*c5c4113dSnw141292    COPY t1 FROM 'data6.txt';
161*c5c4113dSnw141292    SELECT * FROM t1 ORDER BY a;
162*c5c4113dSnw141292  }
163*c5c4113dSnw141292} {1 {hello
164*c5c4113dSnw141292world} 2 {hello world}}
165*c5c4113dSnw141292do_test copy-3.3 {
166*c5c4113dSnw141292  set fd [open data6.txt w]
167*c5c4113dSnw141292  puts $fd "1:hello\\b\\f\\n\\r\\t\\vworld"
168*c5c4113dSnw141292  puts $fd "2:hello world"
169*c5c4113dSnw141292  close $fd
170*c5c4113dSnw141292  execsql {
171*c5c4113dSnw141292    DELETE FROM t1;
172*c5c4113dSnw141292    COPY t1 FROM 'data6.txt' USING DELIMITERS ':';
173*c5c4113dSnw141292    SELECT * FROM t1 ORDER BY a;
174*c5c4113dSnw141292  }
175*c5c4113dSnw141292} [list 1 "hello\b\f\n\r\t\vworld" 2 "hello world"]
176*c5c4113dSnw141292
177*c5c4113dSnw141292# Test the embedded NULL logic.
178*c5c4113dSnw141292#
179*c5c4113dSnw141292do_test copy-4.1 {
180*c5c4113dSnw141292  set fd [open data6.txt w]
181*c5c4113dSnw141292  puts $fd "1\t\\N"
182*c5c4113dSnw141292  puts $fd "\\N\thello world"
183*c5c4113dSnw141292  close $fd
184*c5c4113dSnw141292  execsql {
185*c5c4113dSnw141292    DELETE FROM t1;
186*c5c4113dSnw141292    COPY t1 FROM 'data6.txt';
187*c5c4113dSnw141292    SELECT * FROM t1 WHERE a IS NULL;
188*c5c4113dSnw141292  }
189*c5c4113dSnw141292} {{} {hello world}}
190*c5c4113dSnw141292do_test copy-4.2 {
191*c5c4113dSnw141292  execsql {
192*c5c4113dSnw141292    SELECT * FROM t1 WHERE b IS NULL;
193*c5c4113dSnw141292  }
194*c5c4113dSnw141292} {1 {}}
195*c5c4113dSnw141292
196*c5c4113dSnw141292# Test the conflict resolution logic for COPY
197*c5c4113dSnw141292#
198*c5c4113dSnw141292do_test copy-5.1 {
199*c5c4113dSnw141292  execsql {
200*c5c4113dSnw141292    DROP TABLE t1;
201*c5c4113dSnw141292    CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE, c);
202*c5c4113dSnw141292    COPY t1 FROM 'data5.txt' USING DELIMITERS '|';
203*c5c4113dSnw141292    SELECT * FROM t1;
204*c5c4113dSnw141292  }
205*c5c4113dSnw141292} {11 22 33 22 33 11}
206*c5c4113dSnw141292do_test copy-5.2 {
207*c5c4113dSnw141292  set fd [open data6.txt w]
208*c5c4113dSnw141292  puts $fd "33|22|44"
209*c5c4113dSnw141292  close $fd
210*c5c4113dSnw141292  catchsql {
211*c5c4113dSnw141292    COPY t1 FROM 'data6.txt' USING DELIMITERS '|';
212*c5c4113dSnw141292    SELECT * FROM t1;
213*c5c4113dSnw141292  }
214*c5c4113dSnw141292} {1 {column b is not unique}}
215*c5c4113dSnw141292do_test copy-5.3 {
216*c5c4113dSnw141292  set fd [open data6.txt w]
217*c5c4113dSnw141292  puts $fd "33|22|44"
218*c5c4113dSnw141292  close $fd
219*c5c4113dSnw141292  catchsql {
220*c5c4113dSnw141292    COPY OR IGNORE t1 FROM 'data6.txt' USING DELIMITERS '|';
221*c5c4113dSnw141292    SELECT * FROM t1;
222*c5c4113dSnw141292  }
223*c5c4113dSnw141292} {0 {11 22 33 22 33 11}}
224*c5c4113dSnw141292do_test copy-5.4 {
225*c5c4113dSnw141292  set fd [open data6.txt w]
226*c5c4113dSnw141292  puts $fd "33|22|44"
227*c5c4113dSnw141292  close $fd
228*c5c4113dSnw141292  catchsql {
229*c5c4113dSnw141292    COPY OR REPLACE t1 FROM 'data6.txt' USING DELIMITERS '|';
230*c5c4113dSnw141292    SELECT * FROM t1;
231*c5c4113dSnw141292  }
232*c5c4113dSnw141292} {0 {22 33 11 33 22 44}}
233*c5c4113dSnw141292
234*c5c4113dSnw141292do_test copy-5.5 {
235*c5c4113dSnw141292  execsql {
236*c5c4113dSnw141292    DELETE FROM t1;
237*c5c4113dSnw141292    PRAGMA count_changes=on;
238*c5c4113dSnw141292    COPY t1 FROM 'data5.txt' USING DELIMITERS '|';
239*c5c4113dSnw141292  }
240*c5c4113dSnw141292} {2}
241*c5c4113dSnw141292do_test copy-5.6 {
242*c5c4113dSnw141292  execsql {
243*c5c4113dSnw141292    COPY OR REPLACE t1 FROM 'data5.txt' USING DELIMITERS '|';
244*c5c4113dSnw141292  }
245*c5c4113dSnw141292} {2}
246*c5c4113dSnw141292do_test copy-5.7 {
247*c5c4113dSnw141292  execsql {
248*c5c4113dSnw141292    COPY OR IGNORE t1 FROM 'data5.txt' USING DELIMITERS '|';
249*c5c4113dSnw141292  }
250*c5c4113dSnw141292} {0}
251*c5c4113dSnw141292
252*c5c4113dSnw141292do_test copy-6.1 {
253*c5c4113dSnw141292  execsql {
254*c5c4113dSnw141292    PRAGMA count_changes=off;
255*c5c4113dSnw141292    CREATE TABLE t2(a,b,c);
256*c5c4113dSnw141292    COPY t2 FROM 'dataX.txt' USING DELIMITERS '|';
257*c5c4113dSnw141292    SELECT * FROM t2;
258*c5c4113dSnw141292  }
259*c5c4113dSnw141292} {11 22 33 22 33 44 33 44 55 44 55 66 55 66 77 66 77 88}
260*c5c4113dSnw141292
261*c5c4113dSnw141292integrity_check copy-7.1
262*c5c4113dSnw141292
263*c5c4113dSnw141292# Cleanup
264*c5c4113dSnw141292#
265*c5c4113dSnw141292#file delete -force data1.txt data2.txt data3.txt data4.txt data5.txt \
266*c5c4113dSnw141292                   data6.txt dataX.txt
267*c5c4113dSnw141292
268*c5c4113dSnw141292finish_test
269