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 sqlite_exec_printf() and 16*c5c4113dSnw141292# sqlite_get_table_printf() APIs. 17*c5c4113dSnw141292# 18*c5c4113dSnw141292# $Id: tableapi.test,v 1.7 2004/02/02 12:29:25 drh Exp $ 19*c5c4113dSnw141292 20*c5c4113dSnw141292set testdir [file dirname $argv0] 21*c5c4113dSnw141292source $testdir/tester.tcl 22*c5c4113dSnw141292 23*c5c4113dSnw141292do_test tableapi-1.0 { 24*c5c4113dSnw141292 set ::dbx [sqlite_open test.db] 25*c5c4113dSnw141292 catch {sqlite_exec_printf $::dbx {DROP TABLE xyz} {}} 26*c5c4113dSnw141292 sqlite_exec_printf $::dbx {CREATE TABLE %s(a int, b text)} xyz 27*c5c4113dSnw141292} {0 {}} 28*c5c4113dSnw141292do_test tableapi-1.1 { 29*c5c4113dSnw141292 sqlite_exec_printf $::dbx { 30*c5c4113dSnw141292 INSERT INTO xyz VALUES(1,'%q') 31*c5c4113dSnw141292 } {Hi Y'all} 32*c5c4113dSnw141292} {0 {}} 33*c5c4113dSnw141292do_test tableapi-1.2 { 34*c5c4113dSnw141292 sqlite_exec_printf $::dbx {SELECT * FROM xyz} {} 35*c5c4113dSnw141292} {0 {a b 1 {Hi Y'all}}} 36*c5c4113dSnw141292 37*c5c4113dSnw141292do_test tableapi-2.1 { 38*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 39*c5c4113dSnw141292 BEGIN TRANSACTION; 40*c5c4113dSnw141292 SELECT * FROM xyz WHERE b='%q' 41*c5c4113dSnw141292 } {Hi Y'all} 42*c5c4113dSnw141292} {0 1 2 a b 1 {Hi Y'all}} 43*c5c4113dSnw141292do_test tableapi-2.2 { 44*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 45*c5c4113dSnw141292 SELECT * FROM xyz 46*c5c4113dSnw141292 } {} 47*c5c4113dSnw141292} {0 1 2 a b 1 {Hi Y'all}} 48*c5c4113dSnw141292do_test tableapi-2.3 { 49*c5c4113dSnw141292 for {set i 2} {$i<=50} {incr i} { 50*c5c4113dSnw141292 sqlite_get_table_printf $::dbx \ 51*c5c4113dSnw141292 "INSERT INTO xyz VALUES($i,'(%s)')" $i 52*c5c4113dSnw141292 } 53*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 54*c5c4113dSnw141292 SELECT * FROM xyz ORDER BY a 55*c5c4113dSnw141292 } {} 56*c5c4113dSnw141292} {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)} 57*c5c4113dSnw141292do_test tableapi-2.3.1 { 58*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 59*c5c4113dSnw141292 SELECT * FROM xyz WHERE a>49 ORDER BY a 60*c5c4113dSnw141292 } {} 61*c5c4113dSnw141292} {0 1 2 a b 50 (50)} 62*c5c4113dSnw141292do_test tableapi-2.3.2 { 63*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 64*c5c4113dSnw141292 SELECT * FROM xyz WHERE a>47 ORDER BY a 65*c5c4113dSnw141292 } {} 66*c5c4113dSnw141292} {0 3 2 a b 48 (48) 49 (49) 50 (50)} 67*c5c4113dSnw141292do_test tableapi-2.4 { 68*c5c4113dSnw141292 set manyquote '''''''' 69*c5c4113dSnw141292 append manyquote $manyquote 70*c5c4113dSnw141292 append manyquote $manyquote 71*c5c4113dSnw141292 append manyquote $manyquote 72*c5c4113dSnw141292 append manyquote $manyquote 73*c5c4113dSnw141292 append manyquote $manyquote 74*c5c4113dSnw141292 append manyquote $manyquote 75*c5c4113dSnw141292 set ::big_str "$manyquote Hello $manyquote" 76*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 77*c5c4113dSnw141292 INSERT INTO xyz VALUES(51,'%q') 78*c5c4113dSnw141292 } $::big_str 79*c5c4113dSnw141292} {0 0 0} 80*c5c4113dSnw141292do_test tableapi-2.5 { 81*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 82*c5c4113dSnw141292 SELECT * FROM xyz WHERE a>49 ORDER BY a; 83*c5c4113dSnw141292 } {} 84*c5c4113dSnw141292} "0 2 2 a b 50 (50) 51 \173$::big_str\175" 85*c5c4113dSnw141292do_test tableapi-2.6 { 86*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 87*c5c4113dSnw141292 INSERT INTO xyz VALUES(52,NULL) 88*c5c4113dSnw141292 } {} 89*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 90*c5c4113dSnw141292 SELECT * FROM xyz WHERE a IN (42,50,52) ORDER BY a DESC 91*c5c4113dSnw141292 } {} 92*c5c4113dSnw141292} {0 3 2 a b 52 NULL 50 (50) 42 (42)} 93*c5c4113dSnw141292do_test tableapi-2.7 { 94*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 95*c5c4113dSnw141292 SELECT * FROM xyz WHERE a>1000 96*c5c4113dSnw141292 } {} 97*c5c4113dSnw141292} {0 0 0} 98*c5c4113dSnw141292 99*c5c4113dSnw141292# Repeat all tests with the empty_result_callbacks pragma turned on 100*c5c4113dSnw141292# 101*c5c4113dSnw141292do_test tableapi-3.1 { 102*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 103*c5c4113dSnw141292 ROLLBACK; 104*c5c4113dSnw141292 PRAGMA empty_result_callbacks = ON; 105*c5c4113dSnw141292 SELECT * FROM xyz WHERE b='%q' 106*c5c4113dSnw141292 } {Hi Y'all} 107*c5c4113dSnw141292} {0 1 2 a b 1 {Hi Y'all}} 108*c5c4113dSnw141292do_test tableapi-3.2 { 109*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 110*c5c4113dSnw141292 SELECT * FROM xyz 111*c5c4113dSnw141292 } {} 112*c5c4113dSnw141292} {0 1 2 a b 1 {Hi Y'all}} 113*c5c4113dSnw141292do_test tableapi-3.3 { 114*c5c4113dSnw141292 for {set i 2} {$i<=50} {incr i} { 115*c5c4113dSnw141292 sqlite_get_table_printf $::dbx \ 116*c5c4113dSnw141292 "INSERT INTO xyz VALUES($i,'(%s)')" $i 117*c5c4113dSnw141292 } 118*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 119*c5c4113dSnw141292 SELECT * FROM xyz ORDER BY a 120*c5c4113dSnw141292 } {} 121*c5c4113dSnw141292} {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)} 122*c5c4113dSnw141292do_test tableapi-3.3.1 { 123*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 124*c5c4113dSnw141292 SELECT * FROM xyz WHERE a>49 ORDER BY a 125*c5c4113dSnw141292 } {} 126*c5c4113dSnw141292} {0 1 2 a b 50 (50)} 127*c5c4113dSnw141292do_test tableapi-3.3.2 { 128*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 129*c5c4113dSnw141292 SELECT * FROM xyz WHERE a>47 ORDER BY a 130*c5c4113dSnw141292 } {} 131*c5c4113dSnw141292} {0 3 2 a b 48 (48) 49 (49) 50 (50)} 132*c5c4113dSnw141292do_test tableapi-3.4 { 133*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 134*c5c4113dSnw141292 INSERT INTO xyz VALUES(51,'%q') 135*c5c4113dSnw141292 } $::big_str 136*c5c4113dSnw141292} {0 0 0} 137*c5c4113dSnw141292do_test tableapi-3.5 { 138*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 139*c5c4113dSnw141292 SELECT * FROM xyz WHERE a>49 ORDER BY a; 140*c5c4113dSnw141292 } {} 141*c5c4113dSnw141292} "0 2 2 a b 50 (50) 51 \173$::big_str\175" 142*c5c4113dSnw141292do_test tableapi-3.6 { 143*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 144*c5c4113dSnw141292 INSERT INTO xyz VALUES(52,NULL) 145*c5c4113dSnw141292 } {} 146*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 147*c5c4113dSnw141292 SELECT * FROM xyz WHERE a IN (42,50,52) ORDER BY a DESC 148*c5c4113dSnw141292 } {} 149*c5c4113dSnw141292} {0 3 2 a b 52 NULL 50 (50) 42 (42)} 150*c5c4113dSnw141292do_test tableapi-3.7 { 151*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 152*c5c4113dSnw141292 SELECT * FROM xyz WHERE a>1000 153*c5c4113dSnw141292 } {} 154*c5c4113dSnw141292} {0 0 2 a b} 155*c5c4113dSnw141292 156*c5c4113dSnw141292do_test tableapi-4.1 { 157*c5c4113dSnw141292 set rc [catch { 158*c5c4113dSnw141292 sqlite_get_table_printf $::dbx { 159*c5c4113dSnw141292 SELECT * FROM xyz; SELECT * FROM sqlite_master 160*c5c4113dSnw141292 } {} 161*c5c4113dSnw141292 } msg] 162*c5c4113dSnw141292 concat $rc $msg 163*c5c4113dSnw141292} {0 1 {sqlite_get_table() called with two or more incompatible queries}} 164*c5c4113dSnw141292 165*c5c4113dSnw141292# A report on the mailing list says that the sqlite_get_table() api fails 166*c5c4113dSnw141292# on queries involving more than 40 columns. The following code attempts 167*c5c4113dSnw141292# to test that complaint 168*c5c4113dSnw141292# 169*c5c4113dSnw141292do_test tableapi-5.1 { 170*c5c4113dSnw141292 set sql "CREATE TABLE t2(" 171*c5c4113dSnw141292 set sep "" 172*c5c4113dSnw141292 for {set i 1} {$i<=100} {incr i} { 173*c5c4113dSnw141292 append sql ${sep}x$i 174*c5c4113dSnw141292 set sep , 175*c5c4113dSnw141292 } 176*c5c4113dSnw141292 append sql ) 177*c5c4113dSnw141292 sqlite_get_table_printf $::dbx $sql {} 178*c5c4113dSnw141292 set sql "INSERT INTO t2 VALUES(" 179*c5c4113dSnw141292 set sep "" 180*c5c4113dSnw141292 for {set i 1} {$i<=100} {incr i} { 181*c5c4113dSnw141292 append sql ${sep}$i 182*c5c4113dSnw141292 set sep , 183*c5c4113dSnw141292 } 184*c5c4113dSnw141292 append sql ) 185*c5c4113dSnw141292 sqlite_get_table_printf $::dbx $sql {} 186*c5c4113dSnw141292 sqlite_get_table_printf $::dbx {SELECT * FROM t2} {} 187*c5c4113dSnw141292} {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} 188*c5c4113dSnw141292do_test tableapi-5.2 { 189*c5c4113dSnw141292 set sql "INSERT INTO t2 VALUES(" 190*c5c4113dSnw141292 set sep "" 191*c5c4113dSnw141292 for {set i 1} {$i<=100} {incr i} { 192*c5c4113dSnw141292 append sql ${sep}[expr {$i+1000}] 193*c5c4113dSnw141292 set sep , 194*c5c4113dSnw141292 } 195*c5c4113dSnw141292 append sql ) 196*c5c4113dSnw141292 sqlite_get_table_printf $::dbx $sql {} 197*c5c4113dSnw141292 sqlite_get_table_printf $::dbx {SELECT * FROM t2} {} 198*c5c4113dSnw141292} {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} 199*c5c4113dSnw141292 200*c5c4113dSnw141292do_test tableapi-99.0 { 201*c5c4113dSnw141292 sqlite_close $::dbx 202*c5c4113dSnw141292} {} 203*c5c4113dSnw141292 204*c5c4113dSnw141292finish_test 205