1b19a2bc6Sdrh# 2001 September 15 2d1bf3512Sdrh# 3b19a2bc6Sdrh# The author disclaims copyright to this source code. In place of 4b19a2bc6Sdrh# a legal notice, here is a blessing: 5d1bf3512Sdrh# 6b19a2bc6Sdrh# May you do good and not evil. 7b19a2bc6Sdrh# May you find forgiveness for yourself and forgive others. 8b19a2bc6Sdrh# May you share freely, never taking more than you give. 9d1bf3512Sdrh# 10d1bf3512Sdrh#*********************************************************************** 11d1bf3512Sdrh# This file implements regression tests for SQLite library. The 12d1bf3512Sdrh# focus of this file is testing the sqlite_exec_printf() and 13d1bf3512Sdrh# sqlite_get_table_printf() APIs. 14d1bf3512Sdrh# 159a8cee7fSshane# $Id: tableapi.test,v 1.21 2009/07/17 14:37:25 shane Exp $ 16d1bf3512Sdrh 17d1bf3512Sdrhset testdir [file dirname $argv0] 18d1bf3512Sdrhsource $testdir/tester.tcl 19d1bf3512Sdrh 209a8cee7fSshaneifcapable !gettable { 219a8cee7fSshane finish_test 229a8cee7fSshane return 239a8cee7fSshane} 249a8cee7fSshane 254b9507a0Sdanielk1977source $testdir/malloc_common.tcl 264b9507a0Sdanielk1977 27d1bf3512Sdrhdo_test tableapi-1.0 { 282dd59365Sdanielk1977 set ::dbx [sqlite3_open test.db] 29d1bf3512Sdrh catch {sqlite_exec_printf $::dbx {DROP TABLE xyz} {}} 302dd59365Sdanielk1977 sqlite3_exec_printf $::dbx {CREATE TABLE %s(a int, b text)} xyz 31d1bf3512Sdrh} {0 {}} 32d1bf3512Sdrhdo_test tableapi-1.1 { 332dd59365Sdanielk1977 sqlite3_exec_printf $::dbx { 34d1bf3512Sdrh INSERT INTO xyz VALUES(1,'%q') 35d1bf3512Sdrh } {Hi Y'all} 36d1bf3512Sdrh} {0 {}} 37d1bf3512Sdrhdo_test tableapi-1.2 { 382dd59365Sdanielk1977 sqlite3_exec_printf $::dbx {SELECT * FROM xyz} {} 39d1bf3512Sdrh} {0 {a b 1 {Hi Y'all}}} 40d1bf3512Sdrh 41d1bf3512Sdrhdo_test tableapi-2.1 { 422dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 4301a34661Sdrh BEGIN TRANSACTION; 44d1bf3512Sdrh SELECT * FROM xyz WHERE b='%q' 45d1bf3512Sdrh } {Hi Y'all} 46d1bf3512Sdrh} {0 1 2 a b 1 {Hi Y'all}} 47daffd0e5Sdrhdo_test tableapi-2.2 { 482dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 49daffd0e5Sdrh SELECT * FROM xyz 50daffd0e5Sdrh } {} 51daffd0e5Sdrh} {0 1 2 a b 1 {Hi Y'all}} 52daffd0e5Sdrhdo_test tableapi-2.3 { 53daffd0e5Sdrh for {set i 2} {$i<=50} {incr i} { 542dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx \ 55daffd0e5Sdrh "INSERT INTO xyz VALUES($i,'(%s)')" $i 56daffd0e5Sdrh } 572dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 58daffd0e5Sdrh SELECT * FROM xyz ORDER BY a 59daffd0e5Sdrh } {} 60daffd0e5Sdrh} {0 50 2 a b 1 {Hi Y'all} 2 (2) 3 (3) 4 (4) 5 (5) 6 (6) 7 (7) 8 (8) 9 (9) 10 (10) 11 (11) 12 (12) 13 (13) 14 (14) 15 (15) 16 (16) 17 (17) 18 (18) 19 (19) 20 (20) 21 (21) 22 (22) 23 (23) 24 (24) 25 (25) 26 (26) 27 (27) 28 (28) 29 (29) 30 (30) 31 (31) 32 (32) 33 (33) 34 (34) 35 (35) 36 (36) 37 (37) 38 (38) 39 (39) 40 (40) 41 (41) 42 (42) 43 (43) 44 (44) 45 (45) 46 (46) 47 (47) 48 (48) 49 (49) 50 (50)} 61daffd0e5Sdrhdo_test tableapi-2.3.1 { 622dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 63daffd0e5Sdrh SELECT * FROM xyz WHERE a>49 ORDER BY a 64daffd0e5Sdrh } {} 65daffd0e5Sdrh} {0 1 2 a b 50 (50)} 66daffd0e5Sdrhdo_test tableapi-2.3.2 { 672dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 68daffd0e5Sdrh SELECT * FROM xyz WHERE a>47 ORDER BY a 69daffd0e5Sdrh } {} 70daffd0e5Sdrh} {0 3 2 a b 48 (48) 49 (49) 50 (50)} 7101495b99Sdrhdo_test tableapi-2.3.3 { 7201495b99Sdrh sqlite3_get_table_printf $::dbx { 7301495b99Sdrh SELECT * FROM xyz WHERE a>47 ORDER BY a; invalid 7401495b99Sdrh } {} 7501495b99Sdrh} {1 {near "invalid": syntax error}} 76d2b3e23bSdrhdo_test tableapi-2.3.4 { 77d2b3e23bSdrh sqlite3_get_table_printf $::dbx { 78d2b3e23bSdrh SELECT * FROM xyz WHERE a>47 ORDER BY a 79d2b3e23bSdrh } {} 8 80d2b3e23bSdrh} {0 a b 48 (48) 49 (49) 50 (50)} 81daffd0e5Sdrhdo_test tableapi-2.4 { 82e84a306bSdrh set manyquote '''''''' 83e84a306bSdrh append manyquote $manyquote 84e84a306bSdrh append manyquote $manyquote 85e84a306bSdrh append manyquote $manyquote 86e84a306bSdrh append manyquote $manyquote 87e84a306bSdrh append manyquote $manyquote 88e84a306bSdrh append manyquote $manyquote 89e84a306bSdrh set ::big_str "$manyquote Hello $manyquote" 902dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 91daffd0e5Sdrh INSERT INTO xyz VALUES(51,'%q') 92daffd0e5Sdrh } $::big_str 93daffd0e5Sdrh} {0 0 0} 94daffd0e5Sdrhdo_test tableapi-2.5 { 952dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 96daffd0e5Sdrh SELECT * FROM xyz WHERE a>49 ORDER BY a; 97daffd0e5Sdrh } {} 98daffd0e5Sdrh} "0 2 2 a b 50 (50) 51 \173$::big_str\175" 99daffd0e5Sdrhdo_test tableapi-2.6 { 1002dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 101daffd0e5Sdrh INSERT INTO xyz VALUES(52,NULL) 102daffd0e5Sdrh } {} 1033e8c37e7Sdanielk1977 ifcapable subquery { 1042dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 105daffd0e5Sdrh SELECT * FROM xyz WHERE a IN (42,50,52) ORDER BY a DESC 106daffd0e5Sdrh } {} 1073e8c37e7Sdanielk1977 } else { 1083e8c37e7Sdanielk1977 sqlite3_get_table_printf $::dbx { 1093e8c37e7Sdanielk1977 SELECT * FROM xyz WHERE a=42 OR a=50 OR a=52 ORDER BY a DESC 1103e8c37e7Sdanielk1977 } {} 1113e8c37e7Sdanielk1977 } 112daffd0e5Sdrh} {0 3 2 a b 52 NULL 50 (50) 42 (42)} 11301a34661Sdrhdo_test tableapi-2.7 { 1142dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 11501a34661Sdrh SELECT * FROM xyz WHERE a>1000 11601a34661Sdrh } {} 11701a34661Sdrh} {0 0 0} 11801a34661Sdrh 11901a34661Sdrh# Repeat all tests with the empty_result_callbacks pragma turned on 12001a34661Sdrh# 12101a34661Sdrhdo_test tableapi-3.1 { 1222dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 12301a34661Sdrh ROLLBACK; 12401a34661Sdrh PRAGMA empty_result_callbacks = ON; 12501a34661Sdrh SELECT * FROM xyz WHERE b='%q' 12601a34661Sdrh } {Hi Y'all} 12701a34661Sdrh} {0 1 2 a b 1 {Hi Y'all}} 12801a34661Sdrhdo_test tableapi-3.2 { 1292dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 13001a34661Sdrh SELECT * FROM xyz 13101a34661Sdrh } {} 13201a34661Sdrh} {0 1 2 a b 1 {Hi Y'all}} 13301a34661Sdrhdo_test tableapi-3.3 { 13401a34661Sdrh for {set i 2} {$i<=50} {incr i} { 1352dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx \ 13601a34661Sdrh "INSERT INTO xyz VALUES($i,'(%s)')" $i 13701a34661Sdrh } 1382dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 13901a34661Sdrh SELECT * FROM xyz ORDER BY a 14001a34661Sdrh } {} 14101a34661Sdrh} {0 50 2 a b 1 {Hi Y'all} 2 (2) 3 (3) 4 (4) 5 (5) 6 (6) 7 (7) 8 (8) 9 (9) 10 (10) 11 (11) 12 (12) 13 (13) 14 (14) 15 (15) 16 (16) 17 (17) 18 (18) 19 (19) 20 (20) 21 (21) 22 (22) 23 (23) 24 (24) 25 (25) 26 (26) 27 (27) 28 (28) 29 (29) 30 (30) 31 (31) 32 (32) 33 (33) 34 (34) 35 (35) 36 (36) 37 (37) 38 (38) 39 (39) 40 (40) 41 (41) 42 (42) 43 (43) 44 (44) 45 (45) 46 (46) 47 (47) 48 (48) 49 (49) 50 (50)} 14201a34661Sdrhdo_test tableapi-3.3.1 { 1432dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 14401a34661Sdrh SELECT * FROM xyz WHERE a>49 ORDER BY a 14501a34661Sdrh } {} 14601a34661Sdrh} {0 1 2 a b 50 (50)} 14701a34661Sdrhdo_test tableapi-3.3.2 { 1482dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 14901a34661Sdrh SELECT * FROM xyz WHERE a>47 ORDER BY a 15001a34661Sdrh } {} 15101a34661Sdrh} {0 3 2 a b 48 (48) 49 (49) 50 (50)} 15201a34661Sdrhdo_test tableapi-3.4 { 1532dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 15401a34661Sdrh INSERT INTO xyz VALUES(51,'%q') 15501a34661Sdrh } $::big_str 15601a34661Sdrh} {0 0 0} 15701a34661Sdrhdo_test tableapi-3.5 { 1582dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 15901a34661Sdrh SELECT * FROM xyz WHERE a>49 ORDER BY a; 16001a34661Sdrh } {} 16101a34661Sdrh} "0 2 2 a b 50 (50) 51 \173$::big_str\175" 16201a34661Sdrhdo_test tableapi-3.6 { 1632dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 16401a34661Sdrh INSERT INTO xyz VALUES(52,NULL) 16501a34661Sdrh } {} 1663e8c37e7Sdanielk1977 ifcapable subquery { 1672dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 16801a34661Sdrh SELECT * FROM xyz WHERE a IN (42,50,52) ORDER BY a DESC 16901a34661Sdrh } {} 1703e8c37e7Sdanielk1977 } else { 1713e8c37e7Sdanielk1977 sqlite3_get_table_printf $::dbx { 1723e8c37e7Sdanielk1977 SELECT * FROM xyz WHERE a=42 OR a=50 OR a=52 ORDER BY a DESC 1733e8c37e7Sdanielk1977 } {} 1743e8c37e7Sdanielk1977 } 17501a34661Sdrh} {0 3 2 a b 52 NULL 50 (50) 42 (42)} 17601a34661Sdrhdo_test tableapi-3.7 { 1772dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 17801a34661Sdrh SELECT * FROM xyz WHERE a>1000 17901a34661Sdrh } {} 18001a34661Sdrh} {0 0 2 a b} 181d1bf3512Sdrh 182f1a7a139Sdrhdo_test tableapi-4.1 { 183f1a7a139Sdrh set rc [catch { 1842dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx { 185f1a7a139Sdrh SELECT * FROM xyz; SELECT * FROM sqlite_master 186f1a7a139Sdrh } {} 187f1a7a139Sdrh } msg] 188f1a7a139Sdrh concat $rc $msg 1892dd59365Sdanielk1977} {0 1 {sqlite3_get_table() called with two or more incompatible queries}} 190f1a7a139Sdrh 1919faae941Sdrh# A report on the mailing list says that the sqlite_get_table() api fails 1929faae941Sdrh# on queries involving more than 40 columns. The following code attempts 1939faae941Sdrh# to test that complaint 1949faae941Sdrh# 1959faae941Sdrhdo_test tableapi-5.1 { 1969faae941Sdrh set sql "CREATE TABLE t2(" 1979faae941Sdrh set sep "" 1989faae941Sdrh for {set i 1} {$i<=100} {incr i} { 1999faae941Sdrh append sql ${sep}x$i 2009faae941Sdrh set sep , 2019faae941Sdrh } 2029faae941Sdrh append sql ) 2032dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx $sql {} 2049faae941Sdrh set sql "INSERT INTO t2 VALUES(" 2059faae941Sdrh set sep "" 2069faae941Sdrh for {set i 1} {$i<=100} {incr i} { 2079faae941Sdrh append sql ${sep}$i 2089faae941Sdrh set sep , 2099faae941Sdrh } 2109faae941Sdrh append sql ) 2112dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx $sql {} 2122dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx {SELECT * FROM t2} {} 2139faae941Sdrh} {0 1 100 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 x61 x62 x63 x64 x65 x66 x67 x68 x69 x70 x71 x72 x73 x74 x75 x76 x77 x78 x79 x80 x81 x82 x83 x84 x85 x86 x87 x88 x89 x90 x91 x92 x93 x94 x95 x96 x97 x98 x99 x100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100} 2149faae941Sdrhdo_test tableapi-5.2 { 2159faae941Sdrh set sql "INSERT INTO t2 VALUES(" 2169faae941Sdrh set sep "" 2179faae941Sdrh for {set i 1} {$i<=100} {incr i} { 2189faae941Sdrh append sql ${sep}[expr {$i+1000}] 2199faae941Sdrh set sep , 2209faae941Sdrh } 2219faae941Sdrh append sql ) 2222dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx $sql {} 2232dd59365Sdanielk1977 sqlite3_get_table_printf $::dbx {SELECT * FROM t2} {} 2249faae941Sdrh} {0 2 100 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 x61 x62 x63 x64 x65 x66 x67 x68 x69 x70 x71 x72 x73 x74 x75 x76 x77 x78 x79 x80 x81 x82 x83 x84 x85 x86 x87 x88 x89 x90 x91 x92 x93 x94 x95 x96 x97 x98 x99 x100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100} 2259faae941Sdrh 2264152e677Sdanielk1977ifcapable schema_pragmas { 2272cc55698Sdrh do_test tableapi-6.1 { 2282cc55698Sdrh sqlite3_get_table_printf $::dbx {PRAGMA user_version} {} 22992895268Sdrh } {0 1 1 user_version 0} 2304152e677Sdanielk1977} 2312cc55698Sdrh 232*d71245c2Sshaneh# do_malloc_test closes and deletes the usual db connections and files on 233*d71245c2Sshaneh# each iteration. $::dbx is a seperate connection, and on Windows, will 234*d71245c2Sshaneh# cause the file deletion of test.db to fail, so we move the close of $::dbx 235*d71245c2Sshaneh# up to here before the do_malloc_test. 236*d71245c2Sshanehdo_test tableapi-99.0 { 237*d71245c2Sshaneh sqlite3_close $::dbx 238*d71245c2Sshaneh} {SQLITE_OK} 239*d71245c2Sshaneh 24001495b99Sdrhdo_malloc_test tableapi-7 -sqlprep { 2412a3c8ff8Sdrh DROP TABLE IF EXISTS t1; 24201495b99Sdrh CREATE TABLE t1(a,b); 24301495b99Sdrh INSERT INTO t1 VALUES(1,2); 24401495b99Sdrh INSERT INTO t1 VALUES(3,4); 24501495b99Sdrh INSERT INTO t1 SELECT a+4, b+4 FROM t1; 24601495b99Sdrh INSERT INTO t1 SELECT a+8, b+8 FROM t1; 24701495b99Sdrh} -tclbody { 24801495b99Sdrh set r [sqlite3_get_table_printf db {SELECT rowid, a, b FROM t1} {}] 24901495b99Sdrh if {[llength $r]<26} {error "out of memory"} 25001495b99Sdrh} 25101495b99Sdrh 252d1bf3512Sdrhfinish_test 253