1 # 2001 September 15 2 # 3 # The author disclaims copyright to this source code. In place of 4 # a legal notice, here is a blessing: 5 # 6 # May you do good and not evil. 7 # May you find forgiveness for yourself and forgive others. 8 # May you share freely, never taking more than you give. 9 # 10 #*********************************************************************** 11 # This file implements regression tests for SQLite library. The 12 # focus of this file is testing the sqlite_exec_printf() and 13 # sqlite_get_table_printf() APIs. 14 # 15 # $Id: tableapi.test,v 1.21 2009/07/17 14:37:25 shane Exp $ 16 17 set testdir [file dirname $argv0] 18 source $testdir/tester.tcl 19 20 ifcapable !gettable { 21 finish_test 22 return 23 } 24 25 ifcapable memdebug { 26 source $testdir/malloc_common.tcl 27 } 28 29 do_test tableapi-1.0 { 30 set ::dbx [sqlite3_open test.db] 31 catch {sqlite_exec_printf $::dbx {DROP TABLE xyz} {}} 32 sqlite3_exec_printf $::dbx {CREATE TABLE %s(a int, b text)} xyz 33 } {0 {}} 34 do_test tableapi-1.1 { 35 sqlite3_exec_printf $::dbx { 36 INSERT INTO xyz VALUES(1,'%q') 37 } {Hi Y'all} 38 } {0 {}} 39 do_test tableapi-1.2 { 40 sqlite3_exec_printf $::dbx {SELECT * FROM xyz} {} 41 } {0 {a b 1 {Hi Y'all}}} 42 43 do_test tableapi-2.1 { 44 sqlite3_get_table_printf $::dbx { 45 BEGIN TRANSACTION; 46 SELECT * FROM xyz WHERE b='%q' 47 } {Hi Y'all} 48 } {0 1 2 a b 1 {Hi Y'all}} 49 do_test tableapi-2.2 { 50 sqlite3_get_table_printf $::dbx { 51 SELECT * FROM xyz 52 } {} 53 } {0 1 2 a b 1 {Hi Y'all}} 54 do_test tableapi-2.3 { 55 for {set i 2} {$i<=50} {incr i} { 56 sqlite3_get_table_printf $::dbx \ 57 "INSERT INTO xyz VALUES($i,'(%s)')" $i 58 } 59 sqlite3_get_table_printf $::dbx { 60 SELECT * FROM xyz ORDER BY a 61 } {} 62 } {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)} 63 do_test tableapi-2.3.1 { 64 sqlite3_get_table_printf $::dbx { 65 SELECT * FROM xyz WHERE a>49 ORDER BY a 66 } {} 67 } {0 1 2 a b 50 (50)} 68 do_test tableapi-2.3.2 { 69 sqlite3_get_table_printf $::dbx { 70 SELECT * FROM xyz WHERE a>47 ORDER BY a 71 } {} 72 } {0 3 2 a b 48 (48) 49 (49) 50 (50)} 73 do_test tableapi-2.3.3 { 74 sqlite3_get_table_printf $::dbx { 75 SELECT * FROM xyz WHERE a>47 ORDER BY a; invalid 76 } {} 77 } {1 {near "invalid": syntax error}} 78 do_test tableapi-2.3.4 { 79 sqlite3_get_table_printf $::dbx { 80 SELECT * FROM xyz WHERE a>47 ORDER BY a 81 } {} 8 82 } {0 a b 48 (48) 49 (49) 50 (50)} 83 do_test tableapi-2.4 { 84 set manyquote '''''''' 85 append manyquote $manyquote 86 append manyquote $manyquote 87 append manyquote $manyquote 88 append manyquote $manyquote 89 append manyquote $manyquote 90 append manyquote $manyquote 91 set ::big_str "$manyquote Hello $manyquote" 92 sqlite3_get_table_printf $::dbx { 93 INSERT INTO xyz VALUES(51,'%q') 94 } $::big_str 95 } {0 0 0} 96 do_test tableapi-2.5 { 97 sqlite3_get_table_printf $::dbx { 98 SELECT * FROM xyz WHERE a>49 ORDER BY a; 99 } {} 100 } "0 2 2 a b 50 (50) 51 \173$::big_str\175" 101 do_test tableapi-2.6 { 102 sqlite3_get_table_printf $::dbx { 103 INSERT INTO xyz VALUES(52,NULL) 104 } {} 105 ifcapable subquery { 106 sqlite3_get_table_printf $::dbx { 107 SELECT * FROM xyz WHERE a IN (42,50,52) ORDER BY a DESC 108 } {} 109 } else { 110 sqlite3_get_table_printf $::dbx { 111 SELECT * FROM xyz WHERE a=42 OR a=50 OR a=52 ORDER BY a DESC 112 } {} 113 } 114 } {0 3 2 a b 52 NULL 50 (50) 42 (42)} 115 do_test tableapi-2.7 { 116 sqlite3_get_table_printf $::dbx { 117 SELECT * FROM xyz WHERE a>1000 118 } {} 119 } {0 0 0} 120 121 # Repeat all tests with the empty_result_callbacks pragma turned on 122 # 123 do_test tableapi-3.1 { 124 sqlite3_get_table_printf $::dbx { 125 ROLLBACK; 126 PRAGMA empty_result_callbacks = ON; 127 SELECT * FROM xyz WHERE b='%q' 128 } {Hi Y'all} 129 } {0 1 2 a b 1 {Hi Y'all}} 130 do_test tableapi-3.2 { 131 sqlite3_get_table_printf $::dbx { 132 SELECT * FROM xyz 133 } {} 134 } {0 1 2 a b 1 {Hi Y'all}} 135 do_test tableapi-3.3 { 136 for {set i 2} {$i<=50} {incr i} { 137 sqlite3_get_table_printf $::dbx \ 138 "INSERT INTO xyz VALUES($i,'(%s)')" $i 139 } 140 sqlite3_get_table_printf $::dbx { 141 SELECT * FROM xyz ORDER BY a 142 } {} 143 } {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)} 144 do_test tableapi-3.3.1 { 145 sqlite3_get_table_printf $::dbx { 146 SELECT * FROM xyz WHERE a>49 ORDER BY a 147 } {} 148 } {0 1 2 a b 50 (50)} 149 do_test tableapi-3.3.2 { 150 sqlite3_get_table_printf $::dbx { 151 SELECT * FROM xyz WHERE a>47 ORDER BY a 152 } {} 153 } {0 3 2 a b 48 (48) 49 (49) 50 (50)} 154 do_test tableapi-3.4 { 155 sqlite3_get_table_printf $::dbx { 156 INSERT INTO xyz VALUES(51,'%q') 157 } $::big_str 158 } {0 0 0} 159 do_test tableapi-3.5 { 160 sqlite3_get_table_printf $::dbx { 161 SELECT * FROM xyz WHERE a>49 ORDER BY a; 162 } {} 163 } "0 2 2 a b 50 (50) 51 \173$::big_str\175" 164 do_test tableapi-3.6 { 165 sqlite3_get_table_printf $::dbx { 166 INSERT INTO xyz VALUES(52,NULL) 167 } {} 168 ifcapable subquery { 169 sqlite3_get_table_printf $::dbx { 170 SELECT * FROM xyz WHERE a IN (42,50,52) ORDER BY a DESC 171 } {} 172 } else { 173 sqlite3_get_table_printf $::dbx { 174 SELECT * FROM xyz WHERE a=42 OR a=50 OR a=52 ORDER BY a DESC 175 } {} 176 } 177 } {0 3 2 a b 52 NULL 50 (50) 42 (42)} 178 do_test tableapi-3.7 { 179 sqlite3_get_table_printf $::dbx { 180 SELECT * FROM xyz WHERE a>1000 181 } {} 182 } {0 0 2 a b} 183 184 do_test tableapi-4.1 { 185 set rc [catch { 186 sqlite3_get_table_printf $::dbx { 187 SELECT * FROM xyz; SELECT * FROM sqlite_master 188 } {} 189 } msg] 190 concat $rc $msg 191 } {0 1 {sqlite3_get_table() called with two or more incompatible queries}} 192 193 # A report on the mailing list says that the sqlite_get_table() api fails 194 # on queries involving more than 40 columns. The following code attempts 195 # to test that complaint 196 # 197 do_test tableapi-5.1 { 198 set sql "CREATE TABLE t2(" 199 set sep "" 200 for {set i 1} {$i<=100} {incr i} { 201 append sql ${sep}x$i 202 set sep , 203 } 204 append sql ) 205 sqlite3_get_table_printf $::dbx $sql {} 206 set sql "INSERT INTO t2 VALUES(" 207 set sep "" 208 for {set i 1} {$i<=100} {incr i} { 209 append sql ${sep}$i 210 set sep , 211 } 212 append sql ) 213 sqlite3_get_table_printf $::dbx $sql {} 214 sqlite3_get_table_printf $::dbx {SELECT * FROM t2} {} 215 } {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} 216 do_test tableapi-5.2 { 217 set sql "INSERT INTO t2 VALUES(" 218 set sep "" 219 for {set i 1} {$i<=100} {incr i} { 220 append sql ${sep}[expr {$i+1000}] 221 set sep , 222 } 223 append sql ) 224 sqlite3_get_table_printf $::dbx $sql {} 225 sqlite3_get_table_printf $::dbx {SELECT * FROM t2} {} 226 } {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} 227 228 ifcapable schema_pragmas { 229 do_test tableapi-6.1 { 230 sqlite3_get_table_printf $::dbx {PRAGMA user_version} {} 231 } {0 1 1 user_version 0} 232 } 233 234 # do_malloc_test closes and deletes the usual db connections and files on 235 # each iteration. $::dbx is a seperate connection, and on Windows, will 236 # cause the file deletion of test.db to fail, so we move the close of $::dbx 237 # up to here before the do_malloc_test. 238 do_test tableapi-99.0 { 239 sqlite3_close $::dbx 240 } {SQLITE_OK} 241 242 ifcapable memdebug { 243 do_malloc_test tableapi-7 -sqlprep { 244 DROP TABLE IF EXISTS t1; 245 CREATE TABLE t1(a,b); 246 INSERT INTO t1 VALUES(1,2); 247 INSERT INTO t1 VALUES(3,4); 248 INSERT INTO t1 SELECT a+4, b+4 FROM t1; 249 INSERT INTO t1 SELECT a+8, b+8 FROM t1; 250 } -tclbody { 251 set r [sqlite3_get_table_printf db {SELECT rowid, a, b FROM t1} {}] 252 if {[llength $r]<26} {error "out of memory"} 253 } 254 } 255 256 finish_test 257