blob: 327e9e35ea3b559252bc1a780c5866f15bd94b59 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhd1bf3512001-04-07 15:24:33 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhd1bf3512001-04-07 15:24:33 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drhd1bf3512001-04-07 15:24:33 +00009#
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#
drh01495b92008-01-23 12:52:40 +000015# $Id: tableapi.test,v 1.14 2008/01/23 12:52:41 drh Exp $
drhd1bf3512001-04-07 15:24:33 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20do_test tableapi-1.0 {
danielk19772dd59362004-06-14 23:46:47 +000021 set ::dbx [sqlite3_open test.db]
drhd1bf3512001-04-07 15:24:33 +000022 catch {sqlite_exec_printf $::dbx {DROP TABLE xyz} {}}
danielk19772dd59362004-06-14 23:46:47 +000023 sqlite3_exec_printf $::dbx {CREATE TABLE %s(a int, b text)} xyz
drhd1bf3512001-04-07 15:24:33 +000024} {0 {}}
25do_test tableapi-1.1 {
danielk19772dd59362004-06-14 23:46:47 +000026 sqlite3_exec_printf $::dbx {
drhd1bf3512001-04-07 15:24:33 +000027 INSERT INTO xyz VALUES(1,'%q')
28 } {Hi Y'all}
29} {0 {}}
30do_test tableapi-1.2 {
danielk19772dd59362004-06-14 23:46:47 +000031 sqlite3_exec_printf $::dbx {SELECT * FROM xyz} {}
drhd1bf3512001-04-07 15:24:33 +000032} {0 {a b 1 {Hi Y'all}}}
33
34do_test tableapi-2.1 {
danielk19772dd59362004-06-14 23:46:47 +000035 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +000036 BEGIN TRANSACTION;
drhd1bf3512001-04-07 15:24:33 +000037 SELECT * FROM xyz WHERE b='%q'
38 } {Hi Y'all}
39} {0 1 2 a b 1 {Hi Y'all}}
drhdaffd0e2001-04-11 14:28:42 +000040do_test tableapi-2.2 {
danielk19772dd59362004-06-14 23:46:47 +000041 sqlite3_get_table_printf $::dbx {
drhdaffd0e2001-04-11 14:28:42 +000042 SELECT * FROM xyz
43 } {}
44} {0 1 2 a b 1 {Hi Y'all}}
45do_test tableapi-2.3 {
46 for {set i 2} {$i<=50} {incr i} {
danielk19772dd59362004-06-14 23:46:47 +000047 sqlite3_get_table_printf $::dbx \
drhdaffd0e2001-04-11 14:28:42 +000048 "INSERT INTO xyz VALUES($i,'(%s)')" $i
49 }
danielk19772dd59362004-06-14 23:46:47 +000050 sqlite3_get_table_printf $::dbx {
drhdaffd0e2001-04-11 14:28:42 +000051 SELECT * FROM xyz ORDER BY a
52 } {}
53} {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)}
54do_test tableapi-2.3.1 {
danielk19772dd59362004-06-14 23:46:47 +000055 sqlite3_get_table_printf $::dbx {
drhdaffd0e2001-04-11 14:28:42 +000056 SELECT * FROM xyz WHERE a>49 ORDER BY a
57 } {}
58} {0 1 2 a b 50 (50)}
59do_test tableapi-2.3.2 {
danielk19772dd59362004-06-14 23:46:47 +000060 sqlite3_get_table_printf $::dbx {
drhdaffd0e2001-04-11 14:28:42 +000061 SELECT * FROM xyz WHERE a>47 ORDER BY a
62 } {}
63} {0 3 2 a b 48 (48) 49 (49) 50 (50)}
drh01495b92008-01-23 12:52:40 +000064do_test tableapi-2.3.3 {
65 sqlite3_get_table_printf $::dbx {
66 SELECT * FROM xyz WHERE a>47 ORDER BY a; invalid
67 } {}
68} {1 {near "invalid": syntax error}}
drhdaffd0e2001-04-11 14:28:42 +000069do_test tableapi-2.4 {
drhe84a3062004-02-02 12:29:25 +000070 set manyquote ''''''''
71 append manyquote $manyquote
72 append manyquote $manyquote
73 append manyquote $manyquote
74 append manyquote $manyquote
75 append manyquote $manyquote
76 append manyquote $manyquote
77 set ::big_str "$manyquote Hello $manyquote"
danielk19772dd59362004-06-14 23:46:47 +000078 sqlite3_get_table_printf $::dbx {
drhdaffd0e2001-04-11 14:28:42 +000079 INSERT INTO xyz VALUES(51,'%q')
80 } $::big_str
81} {0 0 0}
82do_test tableapi-2.5 {
danielk19772dd59362004-06-14 23:46:47 +000083 sqlite3_get_table_printf $::dbx {
drhdaffd0e2001-04-11 14:28:42 +000084 SELECT * FROM xyz WHERE a>49 ORDER BY a;
85 } {}
86} "0 2 2 a b 50 (50) 51 \173$::big_str\175"
87do_test tableapi-2.6 {
danielk19772dd59362004-06-14 23:46:47 +000088 sqlite3_get_table_printf $::dbx {
drhdaffd0e2001-04-11 14:28:42 +000089 INSERT INTO xyz VALUES(52,NULL)
90 } {}
danielk19773e8c37e2005-01-21 03:12:14 +000091 ifcapable subquery {
92 sqlite3_get_table_printf $::dbx {
93 SELECT * FROM xyz WHERE a IN (42,50,52) ORDER BY a DESC
94 } {}
95 } else {
96 sqlite3_get_table_printf $::dbx {
97 SELECT * FROM xyz WHERE a=42 OR a=50 OR a=52 ORDER BY a DESC
98 } {}
99 }
drhdaffd0e2001-04-11 14:28:42 +0000100} {0 3 2 a b 52 NULL 50 (50) 42 (42)}
drh01a34662001-10-20 12:30:10 +0000101do_test tableapi-2.7 {
danielk19772dd59362004-06-14 23:46:47 +0000102 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +0000103 SELECT * FROM xyz WHERE a>1000
104 } {}
105} {0 0 0}
106
107# Repeat all tests with the empty_result_callbacks pragma turned on
108#
109do_test tableapi-3.1 {
danielk19772dd59362004-06-14 23:46:47 +0000110 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +0000111 ROLLBACK;
112 PRAGMA empty_result_callbacks = ON;
113 SELECT * FROM xyz WHERE b='%q'
114 } {Hi Y'all}
115} {0 1 2 a b 1 {Hi Y'all}}
116do_test tableapi-3.2 {
danielk19772dd59362004-06-14 23:46:47 +0000117 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +0000118 SELECT * FROM xyz
119 } {}
120} {0 1 2 a b 1 {Hi Y'all}}
121do_test tableapi-3.3 {
122 for {set i 2} {$i<=50} {incr i} {
danielk19772dd59362004-06-14 23:46:47 +0000123 sqlite3_get_table_printf $::dbx \
drh01a34662001-10-20 12:30:10 +0000124 "INSERT INTO xyz VALUES($i,'(%s)')" $i
125 }
danielk19772dd59362004-06-14 23:46:47 +0000126 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +0000127 SELECT * FROM xyz ORDER BY a
128 } {}
129} {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)}
130do_test tableapi-3.3.1 {
danielk19772dd59362004-06-14 23:46:47 +0000131 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +0000132 SELECT * FROM xyz WHERE a>49 ORDER BY a
133 } {}
134} {0 1 2 a b 50 (50)}
135do_test tableapi-3.3.2 {
danielk19772dd59362004-06-14 23:46:47 +0000136 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +0000137 SELECT * FROM xyz WHERE a>47 ORDER BY a
138 } {}
139} {0 3 2 a b 48 (48) 49 (49) 50 (50)}
140do_test tableapi-3.4 {
danielk19772dd59362004-06-14 23:46:47 +0000141 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +0000142 INSERT INTO xyz VALUES(51,'%q')
143 } $::big_str
144} {0 0 0}
145do_test tableapi-3.5 {
danielk19772dd59362004-06-14 23:46:47 +0000146 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +0000147 SELECT * FROM xyz WHERE a>49 ORDER BY a;
148 } {}
149} "0 2 2 a b 50 (50) 51 \173$::big_str\175"
150do_test tableapi-3.6 {
danielk19772dd59362004-06-14 23:46:47 +0000151 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +0000152 INSERT INTO xyz VALUES(52,NULL)
153 } {}
danielk19773e8c37e2005-01-21 03:12:14 +0000154 ifcapable subquery {
155 sqlite3_get_table_printf $::dbx {
156 SELECT * FROM xyz WHERE a IN (42,50,52) ORDER BY a DESC
157 } {}
158 } else {
159 sqlite3_get_table_printf $::dbx {
160 SELECT * FROM xyz WHERE a=42 OR a=50 OR a=52 ORDER BY a DESC
161 } {}
162 }
drh01a34662001-10-20 12:30:10 +0000163} {0 3 2 a b 52 NULL 50 (50) 42 (42)}
164do_test tableapi-3.7 {
danielk19772dd59362004-06-14 23:46:47 +0000165 sqlite3_get_table_printf $::dbx {
drh01a34662001-10-20 12:30:10 +0000166 SELECT * FROM xyz WHERE a>1000
167 } {}
168} {0 0 2 a b}
drhd1bf3512001-04-07 15:24:33 +0000169
drhf1a7a132002-03-23 00:52:01 +0000170do_test tableapi-4.1 {
171 set rc [catch {
danielk19772dd59362004-06-14 23:46:47 +0000172 sqlite3_get_table_printf $::dbx {
drhf1a7a132002-03-23 00:52:01 +0000173 SELECT * FROM xyz; SELECT * FROM sqlite_master
174 } {}
175 } msg]
176 concat $rc $msg
danielk19772dd59362004-06-14 23:46:47 +0000177} {0 1 {sqlite3_get_table() called with two or more incompatible queries}}
drhf1a7a132002-03-23 00:52:01 +0000178
drh9faae942003-09-09 00:47:47 +0000179# A report on the mailing list says that the sqlite_get_table() api fails
180# on queries involving more than 40 columns. The following code attempts
181# to test that complaint
182#
183do_test tableapi-5.1 {
184 set sql "CREATE TABLE t2("
185 set sep ""
186 for {set i 1} {$i<=100} {incr i} {
187 append sql ${sep}x$i
188 set sep ,
189 }
190 append sql )
danielk19772dd59362004-06-14 23:46:47 +0000191 sqlite3_get_table_printf $::dbx $sql {}
drh9faae942003-09-09 00:47:47 +0000192 set sql "INSERT INTO t2 VALUES("
193 set sep ""
194 for {set i 1} {$i<=100} {incr i} {
195 append sql ${sep}$i
196 set sep ,
197 }
198 append sql )
danielk19772dd59362004-06-14 23:46:47 +0000199 sqlite3_get_table_printf $::dbx $sql {}
200 sqlite3_get_table_printf $::dbx {SELECT * FROM t2} {}
drh9faae942003-09-09 00:47:47 +0000201} {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}
202do_test tableapi-5.2 {
203 set sql "INSERT INTO t2 VALUES("
204 set sep ""
205 for {set i 1} {$i<=100} {incr i} {
206 append sql ${sep}[expr {$i+1000}]
207 set sep ,
208 }
209 append sql )
danielk19772dd59362004-06-14 23:46:47 +0000210 sqlite3_get_table_printf $::dbx $sql {}
211 sqlite3_get_table_printf $::dbx {SELECT * FROM t2} {}
drh9faae942003-09-09 00:47:47 +0000212} {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}
213
danielk19774152e672007-09-12 17:01:45 +0000214ifcapable schema_pragmas {
215 do_test tableapi-6.1 {
216 sqlite3_get_table_printf $::dbx {PRAGMA user_version} {}
217 } {0 1 1 user_version 0}
218}
drh2cc55692006-06-27 20:39:04 +0000219
drh01495b92008-01-23 12:52:40 +0000220ifcapable memdebug {
221 source $testdir/malloc_common.tcl
222 do_malloc_test tableapi-7 -sqlprep {
223 CREATE TABLE t1(a,b);
224 INSERT INTO t1 VALUES(1,2);
225 INSERT INTO t1 VALUES(3,4);
226 INSERT INTO t1 SELECT a+4, b+4 FROM t1;
227 INSERT INTO t1 SELECT a+8, b+8 FROM t1;
228 } -tclbody {
229 set r [sqlite3_get_table_printf db {SELECT rowid, a, b FROM t1} {}]
230 if {[llength $r]<26} {error "out of memory"}
231 }
232}
233
drhd1bf3512001-04-07 15:24:33 +0000234do_test tableapi-99.0 {
danielk19772dd59362004-06-14 23:46:47 +0000235 sqlite3_close $::dbx
danielk197796d81f92004-06-19 03:33:57 +0000236} {SQLITE_OK}
drhd1bf3512001-04-07 15:24:33 +0000237
238finish_test