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