drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1 | # 2006 November 08 |
| 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. |
| 12 | # |
| 13 | # This is a copy of the capi3.test file that has been adapted to |
| 14 | # test the new sqlite3_prepare_v2 interface. |
| 15 | # |
danielk1977 | 257d9dc | 2009-07-22 07:27:56 +0000 | [diff] [blame] | 16 | # $Id: capi3c.test,v 1.23 2009/07/22 07:27:57 danielk1977 Exp $ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 17 | # |
| 18 | |
| 19 | set testdir [file dirname $argv0] |
| 20 | source $testdir/tester.tcl |
| 21 | |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 22 | # Do not use a codec for tests in this file, as the database file is |
| 23 | # manipulated directly using tcl scripts (using the [hexio_write] command). |
| 24 | # |
| 25 | do_not_use_codec |
| 26 | |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 27 | # Return the UTF-16 representation of the supplied UTF-8 string $str. |
| 28 | # If $nt is true, append two 0x00 bytes as a nul terminator. |
| 29 | proc utf16 {str {nt 1}} { |
| 30 | set r [encoding convertto unicode $str] |
| 31 | if {$nt} { |
| 32 | append r "\x00\x00" |
| 33 | } |
| 34 | return $r |
| 35 | } |
| 36 | |
| 37 | # Return the UTF-8 representation of the supplied UTF-16 string $str. |
| 38 | proc utf8 {str} { |
| 39 | # If $str ends in two 0x00 0x00 bytes, knock these off before |
| 40 | # converting to UTF-8 using TCL. |
| 41 | binary scan $str \c* vals |
| 42 | if {[lindex $vals end]==0 && [lindex $vals end-1]==0} { |
| 43 | set str [binary format \c* [lrange $vals 0 end-2]] |
| 44 | } |
| 45 | |
| 46 | set r [encoding convertfrom unicode $str] |
| 47 | return $r |
| 48 | } |
| 49 | |
| 50 | # These tests complement those in capi2.test. They are organized |
| 51 | # as follows: |
| 52 | # |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 53 | # capi3c-1.*: Test sqlite3_prepare_v2 |
| 54 | # capi3c-2.*: Test sqlite3_prepare16_v2 |
| 55 | # capi3c-3.*: Test sqlite3_open |
| 56 | # capi3c-4.*: Test sqlite3_open16 |
| 57 | # capi3c-5.*: Test the various sqlite3_result_* APIs |
| 58 | # capi3c-6.*: Test that sqlite3_close fails if there are outstanding VMs. |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 59 | # |
| 60 | |
| 61 | set DB [sqlite3_connection_pointer db] |
| 62 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 63 | do_test capi3c-1.0 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 64 | sqlite3_get_autocommit $DB |
| 65 | } 1 |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 66 | do_test capi3c-1.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 67 | set STMT [sqlite3_prepare_v2 $DB {SELECT name FROM sqlite_master} -1 TAIL] |
| 68 | sqlite3_finalize $STMT |
| 69 | set TAIL |
| 70 | } {} |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 71 | do_test capi3c-1.2.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 72 | sqlite3_errcode $DB |
| 73 | } {SQLITE_OK} |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 74 | do_test capi3c-1.2.2 { |
| 75 | sqlite3_extended_errcode $DB |
| 76 | } {SQLITE_OK} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 77 | do_test capi3c-1.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 78 | sqlite3_errmsg $DB |
| 79 | } {not an error} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 80 | do_test capi3c-1.4 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 81 | set sql {SELECT name FROM sqlite_master;SELECT 10} |
| 82 | set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL] |
| 83 | sqlite3_finalize $STMT |
| 84 | set TAIL |
| 85 | } {SELECT 10} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 86 | do_test capi3c-1.5 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 87 | set sql {SELECT namex FROM sqlite_master} |
| 88 | catch { |
| 89 | set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL] |
| 90 | } |
| 91 | } {1} |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 92 | do_test capi3c-1.6.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 93 | sqlite3_errcode $DB |
| 94 | } {SQLITE_ERROR} |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 95 | do_test capi3c-1.6.2 { |
| 96 | sqlite3_extended_errcode $DB |
| 97 | } {SQLITE_ERROR} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 98 | do_test capi3c-1.7 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 99 | sqlite3_errmsg $DB |
| 100 | } {no such column: namex} |
| 101 | |
drh | 00e087b | 2008-04-10 17:14:07 +0000 | [diff] [blame] | 102 | |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 103 | ifcapable {utf16} { |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 104 | do_test capi3c-2.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 105 | set sql16 [utf16 {SELECT name FROM sqlite_master}] |
| 106 | set STMT [sqlite3_prepare16_v2 $DB $sql16 -1 ::TAIL] |
| 107 | sqlite3_finalize $STMT |
| 108 | utf8 $::TAIL |
| 109 | } {} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 110 | do_test capi3c-2.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 111 | set sql [utf16 {SELECT name FROM sqlite_master;SELECT 10}] |
| 112 | set STMT [sqlite3_prepare16_v2 $DB $sql -1 TAIL] |
| 113 | sqlite3_finalize $STMT |
| 114 | utf8 $TAIL |
| 115 | } {SELECT 10} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 116 | do_test capi3c-2.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 117 | set sql [utf16 {SELECT namex FROM sqlite_master}] |
| 118 | catch { |
| 119 | set STMT [sqlite3_prepare16_v2 $DB $sql -1 TAIL] |
| 120 | } |
| 121 | } {1} |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 122 | do_test capi3c-2.4.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 123 | sqlite3_errcode $DB |
| 124 | } {SQLITE_ERROR} |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 125 | do_test capi3c-2.4.2 { |
| 126 | sqlite3_extended_errcode $DB |
| 127 | } {SQLITE_ERROR} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 128 | do_test capi3c-2.5 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 129 | sqlite3_errmsg $DB |
| 130 | } {no such column: namex} |
| 131 | |
| 132 | ifcapable schema_pragmas { |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 133 | do_test capi3c-2.6 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 134 | execsql {CREATE TABLE tablename(x)} |
| 135 | set sql16 [utf16 {PRAGMA table_info("TableName")}] |
| 136 | set STMT [sqlite3_prepare16_v2 $DB $sql16 -1 TAIL] |
| 137 | sqlite3_step $STMT |
| 138 | } SQLITE_ROW |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 139 | do_test capi3c-2.7 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 140 | sqlite3_step $STMT |
| 141 | } SQLITE_DONE |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 142 | do_test capi3c-2.8 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 143 | sqlite3_finalize $STMT |
| 144 | } SQLITE_OK |
| 145 | } |
| 146 | |
| 147 | } ;# endif utf16 |
| 148 | |
| 149 | # rename sqlite3_open sqlite3_open_old |
| 150 | # proc sqlite3_open {fname options} {sqlite3_open_new $fname $options} |
| 151 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 152 | do_test capi3c-3.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 153 | set db2 [sqlite3_open test.db {}] |
| 154 | sqlite3_errcode $db2 |
| 155 | } {SQLITE_OK} |
| 156 | # FIX ME: Should test the db handle works. |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 157 | do_test capi3c-3.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 158 | sqlite3_close $db2 |
| 159 | } {SQLITE_OK} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 160 | do_test capi3c-3.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 161 | catch { |
| 162 | set db2 [sqlite3_open /bogus/path/test.db {}] |
| 163 | } |
| 164 | sqlite3_errcode $db2 |
| 165 | } {SQLITE_CANTOPEN} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 166 | do_test capi3c-3.4 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 167 | sqlite3_errmsg $db2 |
| 168 | } {unable to open database file} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 169 | do_test capi3c-3.5 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 170 | sqlite3_close $db2 |
| 171 | } {SQLITE_OK} |
dan | afcf9bd | 2014-01-23 14:44:08 +0000 | [diff] [blame] | 172 | if {[clang_sanitize_address]==0} { |
| 173 | do_test capi3c-3.6.1-misuse { |
| 174 | sqlite3_close $db2 |
| 175 | } {SQLITE_MISUSE} |
| 176 | do_test capi3c-3.6.2-misuse { |
| 177 | sqlite3_errmsg $db2 |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 178 | } {library routine called out of sequence} |
dan | afcf9bd | 2014-01-23 14:44:08 +0000 | [diff] [blame] | 179 | ifcapable {utf16} { |
| 180 | do_test capi3c-3.6.3-misuse { |
| 181 | utf8 [sqlite3_errmsg16 $db2] |
| 182 | } {library routine called out of sequence} |
| 183 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | # rename sqlite3_open "" |
| 187 | # rename sqlite3_open_old sqlite3_open |
| 188 | |
| 189 | ifcapable {utf16} { |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 190 | do_test capi3c-4.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 191 | set db2 [sqlite3_open16 [utf16 test.db] {}] |
| 192 | sqlite3_errcode $db2 |
| 193 | } {SQLITE_OK} |
| 194 | # FIX ME: Should test the db handle works. |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 195 | do_test capi3c-4.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 196 | sqlite3_close $db2 |
| 197 | } {SQLITE_OK} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 198 | do_test capi3c-4.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 199 | catch { |
| 200 | set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}] |
| 201 | } |
| 202 | sqlite3_errcode $db2 |
| 203 | } {SQLITE_CANTOPEN} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 204 | do_test capi3c-4.4 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 205 | utf8 [sqlite3_errmsg16 $db2] |
| 206 | } {unable to open database file} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 207 | do_test capi3c-4.5 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 208 | sqlite3_close $db2 |
| 209 | } {SQLITE_OK} |
| 210 | } ;# utf16 |
| 211 | |
| 212 | # This proc is used to test the following API calls: |
| 213 | # |
| 214 | # sqlite3_column_count |
| 215 | # sqlite3_column_name |
| 216 | # sqlite3_column_name16 |
| 217 | # sqlite3_column_decltype |
| 218 | # sqlite3_column_decltype16 |
| 219 | # |
| 220 | # $STMT is a compiled SQL statement. $test is a prefix |
| 221 | # to use for test names within this proc. $names is a list |
| 222 | # of the column names that should be returned by $STMT. |
| 223 | # $decltypes is a list of column declaration types for $STMT. |
| 224 | # |
| 225 | # Example: |
| 226 | # |
| 227 | # set STMT [sqlite3_prepare_v2 "SELECT 1, 2, 2;" -1 DUMMY] |
| 228 | # check_header test1.1 {1 2 3} {"" "" ""} |
| 229 | # |
| 230 | proc check_header {STMT test names decltypes} { |
| 231 | |
| 232 | # Use the return value of sqlite3_column_count() to build |
| 233 | # a list of column indexes. i.e. If sqlite3_column_count |
| 234 | # is 3, build the list {0 1 2}. |
| 235 | set ::idxlist [list] |
| 236 | set ::numcols [sqlite3_column_count $STMT] |
| 237 | for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i} |
| 238 | |
| 239 | # Column names in UTF-8 |
| 240 | do_test $test.1 { |
| 241 | set cnamelist [list] |
| 242 | foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]} |
| 243 | set cnamelist |
| 244 | } $names |
| 245 | |
| 246 | # Column names in UTF-16 |
| 247 | ifcapable {utf16} { |
| 248 | do_test $test.2 { |
| 249 | set cnamelist [list] |
| 250 | foreach i $idxlist { |
| 251 | lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]] |
| 252 | } |
| 253 | set cnamelist |
| 254 | } $names |
| 255 | } |
| 256 | |
| 257 | # Column names in UTF-8 |
| 258 | do_test $test.3 { |
| 259 | set cnamelist [list] |
| 260 | foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]} |
| 261 | set cnamelist |
| 262 | } $names |
| 263 | |
| 264 | # Column names in UTF-16 |
| 265 | ifcapable {utf16} { |
| 266 | do_test $test.4 { |
| 267 | set cnamelist [list] |
| 268 | foreach i $idxlist { |
| 269 | lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]] |
| 270 | } |
| 271 | set cnamelist |
| 272 | } $names |
| 273 | } |
| 274 | |
| 275 | # Column names in UTF-8 |
| 276 | do_test $test.5 { |
| 277 | set cnamelist [list] |
| 278 | foreach i $idxlist {lappend cnamelist [sqlite3_column_decltype $STMT $i]} |
| 279 | set cnamelist |
| 280 | } $decltypes |
| 281 | |
| 282 | # Column declaration types in UTF-16 |
| 283 | ifcapable {utf16} { |
| 284 | do_test $test.6 { |
| 285 | set cnamelist [list] |
| 286 | foreach i $idxlist { |
| 287 | lappend cnamelist [utf8 [sqlite3_column_decltype16 $STMT $i]] |
| 288 | } |
| 289 | set cnamelist |
| 290 | } $decltypes |
| 291 | } |
| 292 | |
| 293 | |
| 294 | # Test some out of range conditions: |
| 295 | ifcapable {utf16} { |
| 296 | do_test $test.7 { |
| 297 | list \ |
| 298 | [sqlite3_column_name $STMT -1] \ |
| 299 | [sqlite3_column_name16 $STMT -1] \ |
| 300 | [sqlite3_column_decltype $STMT -1] \ |
| 301 | [sqlite3_column_decltype16 $STMT -1] \ |
| 302 | [sqlite3_column_name $STMT $numcols] \ |
| 303 | [sqlite3_column_name16 $STMT $numcols] \ |
| 304 | [sqlite3_column_decltype $STMT $numcols] \ |
| 305 | [sqlite3_column_decltype16 $STMT $numcols] |
| 306 | } {{} {} {} {} {} {} {} {}} |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | # This proc is used to test the following API calls: |
| 311 | # |
| 312 | # sqlite3_column_origin_name |
| 313 | # sqlite3_column_origin_name16 |
| 314 | # sqlite3_column_table_name |
| 315 | # sqlite3_column_table_name16 |
| 316 | # sqlite3_column_database_name |
| 317 | # sqlite3_column_database_name16 |
| 318 | # |
| 319 | # $STMT is a compiled SQL statement. $test is a prefix |
| 320 | # to use for test names within this proc. $names is a list |
| 321 | # of the column names that should be returned by $STMT. |
| 322 | # $decltypes is a list of column declaration types for $STMT. |
| 323 | # |
| 324 | # Example: |
| 325 | # |
| 326 | # set STMT [sqlite3_prepare_v2 "SELECT 1, 2, 2;" -1 DUMMY] |
| 327 | # check_header test1.1 {1 2 3} {"" "" ""} |
| 328 | # |
| 329 | proc check_origin_header {STMT test dbs tables cols} { |
| 330 | # If sqlite3_column_origin_name() and friends are not compiled into |
| 331 | # this build, this proc is a no-op. |
| 332 | ifcapable columnmetadata { |
| 333 | |
| 334 | # Use the return value of sqlite3_column_count() to build |
| 335 | # a list of column indexes. i.e. If sqlite3_column_count |
| 336 | # is 3, build the list {0 1 2}. |
| 337 | set ::idxlist [list] |
| 338 | set ::numcols [sqlite3_column_count $STMT] |
| 339 | for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i} |
| 340 | |
| 341 | # Database names in UTF-8 |
| 342 | do_test $test.8 { |
| 343 | set cnamelist [list] |
| 344 | foreach i $idxlist { |
| 345 | lappend cnamelist [sqlite3_column_database_name $STMT $i] |
| 346 | } |
| 347 | set cnamelist |
| 348 | } $dbs |
| 349 | |
| 350 | # Database names in UTF-16 |
| 351 | ifcapable {utf16} { |
| 352 | do_test $test.9 { |
| 353 | set cnamelist [list] |
| 354 | foreach i $idxlist { |
| 355 | lappend cnamelist [utf8 [sqlite3_column_database_name16 $STMT $i]] |
| 356 | } |
| 357 | set cnamelist |
| 358 | } $dbs |
| 359 | } |
| 360 | |
| 361 | # Table names in UTF-8 |
| 362 | do_test $test.10 { |
| 363 | set cnamelist [list] |
| 364 | foreach i $idxlist { |
| 365 | lappend cnamelist [sqlite3_column_table_name $STMT $i] |
| 366 | } |
| 367 | set cnamelist |
| 368 | } $tables |
| 369 | |
| 370 | # Table names in UTF-16 |
| 371 | ifcapable {utf16} { |
| 372 | do_test $test.11 { |
| 373 | set cnamelist [list] |
| 374 | foreach i $idxlist { |
| 375 | lappend cnamelist [utf8 [sqlite3_column_table_name16 $STMT $i]] |
| 376 | } |
| 377 | set cnamelist |
| 378 | } $tables |
| 379 | } |
| 380 | |
| 381 | # Origin names in UTF-8 |
| 382 | do_test $test.12 { |
| 383 | set cnamelist [list] |
| 384 | foreach i $idxlist { |
| 385 | lappend cnamelist [sqlite3_column_origin_name $STMT $i] |
| 386 | } |
| 387 | set cnamelist |
| 388 | } $cols |
| 389 | |
| 390 | # Origin declaration types in UTF-16 |
| 391 | ifcapable {utf16} { |
| 392 | do_test $test.13 { |
| 393 | set cnamelist [list] |
| 394 | foreach i $idxlist { |
| 395 | lappend cnamelist [utf8 [sqlite3_column_origin_name16 $STMT $i]] |
| 396 | } |
| 397 | set cnamelist |
| 398 | } $cols |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | # This proc is used to test the following APIs: |
| 404 | # |
| 405 | # sqlite3_data_count |
| 406 | # sqlite3_column_type |
| 407 | # sqlite3_column_int |
| 408 | # sqlite3_column_text |
| 409 | # sqlite3_column_text16 |
| 410 | # sqlite3_column_double |
| 411 | # |
| 412 | # $STMT is a compiled SQL statement for which the previous call |
| 413 | # to sqlite3_step returned SQLITE_ROW. $test is a prefix to use |
| 414 | # for test names within this proc. $types is a list of the |
| 415 | # manifest types for the current row. $ints, $doubles and $strings |
| 416 | # are lists of the integer, real and string representations of |
| 417 | # the values in the current row. |
| 418 | # |
| 419 | # Example: |
| 420 | # |
| 421 | # set STMT [sqlite3_prepare_v2 "SELECT 'hello', 1.1, NULL" -1 DUMMY] |
| 422 | # sqlite3_step $STMT |
| 423 | # check_data test1.2 {TEXT REAL NULL} {0 1 0} {0 1.1 0} {hello 1.1 {}} |
| 424 | # |
| 425 | proc check_data {STMT test types ints doubles strings} { |
| 426 | |
| 427 | # Use the return value of sqlite3_column_count() to build |
| 428 | # a list of column indexes. i.e. If sqlite3_column_count |
| 429 | # is 3, build the list {0 1 2}. |
| 430 | set ::idxlist [list] |
| 431 | set numcols [sqlite3_data_count $STMT] |
| 432 | for {set i 0} {$i < $numcols} {incr i} {lappend ::idxlist $i} |
| 433 | |
| 434 | # types |
| 435 | do_test $test.1 { |
| 436 | set types [list] |
| 437 | foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]} |
| 438 | set types |
| 439 | } $types |
| 440 | |
| 441 | # Integers |
| 442 | do_test $test.2 { |
| 443 | set ints [list] |
| 444 | foreach i $idxlist {lappend ints [sqlite3_column_int64 $STMT $i]} |
| 445 | set ints |
| 446 | } $ints |
| 447 | |
| 448 | # bytes |
| 449 | set lens [list] |
| 450 | foreach i $::idxlist { |
| 451 | lappend lens [string length [lindex $strings $i]] |
| 452 | } |
| 453 | do_test $test.3 { |
| 454 | set bytes [list] |
| 455 | set lens [list] |
| 456 | foreach i $idxlist { |
| 457 | lappend bytes [sqlite3_column_bytes $STMT $i] |
| 458 | } |
| 459 | set bytes |
| 460 | } $lens |
| 461 | |
| 462 | # bytes16 |
| 463 | ifcapable {utf16} { |
| 464 | set lens [list] |
| 465 | foreach i $::idxlist { |
| 466 | lappend lens [expr 2 * [string length [lindex $strings $i]]] |
| 467 | } |
| 468 | do_test $test.4 { |
| 469 | set bytes [list] |
| 470 | set lens [list] |
| 471 | foreach i $idxlist { |
| 472 | lappend bytes [sqlite3_column_bytes16 $STMT $i] |
| 473 | } |
| 474 | set bytes |
| 475 | } $lens |
| 476 | } |
| 477 | |
| 478 | # Blob |
| 479 | do_test $test.5 { |
| 480 | set utf8 [list] |
| 481 | foreach i $idxlist {lappend utf8 [sqlite3_column_blob $STMT $i]} |
| 482 | set utf8 |
| 483 | } $strings |
| 484 | |
| 485 | # UTF-8 |
| 486 | do_test $test.6 { |
| 487 | set utf8 [list] |
| 488 | foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]} |
| 489 | set utf8 |
| 490 | } $strings |
| 491 | |
| 492 | # Floats |
| 493 | do_test $test.7 { |
| 494 | set utf8 [list] |
| 495 | foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]} |
| 496 | set utf8 |
| 497 | } $doubles |
| 498 | |
| 499 | # UTF-16 |
| 500 | ifcapable {utf16} { |
| 501 | do_test $test.8 { |
| 502 | set utf8 [list] |
| 503 | foreach i $idxlist {lappend utf8 [utf8 [sqlite3_column_text16 $STMT $i]]} |
| 504 | set utf8 |
| 505 | } $strings |
| 506 | } |
| 507 | |
| 508 | # Integers |
| 509 | do_test $test.9 { |
| 510 | set ints [list] |
| 511 | foreach i $idxlist {lappend ints [sqlite3_column_int $STMT $i]} |
| 512 | set ints |
| 513 | } $ints |
| 514 | |
| 515 | # Floats |
| 516 | do_test $test.10 { |
| 517 | set utf8 [list] |
| 518 | foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]} |
| 519 | set utf8 |
| 520 | } $doubles |
| 521 | |
| 522 | # UTF-8 |
| 523 | do_test $test.11 { |
| 524 | set utf8 [list] |
| 525 | foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]} |
| 526 | set utf8 |
| 527 | } $strings |
| 528 | |
| 529 | # Types |
| 530 | do_test $test.12 { |
| 531 | set types [list] |
| 532 | foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]} |
| 533 | set types |
| 534 | } $types |
| 535 | |
| 536 | # Test that an out of range request returns the equivalent of NULL |
| 537 | do_test $test.13 { |
| 538 | sqlite3_column_int $STMT -1 |
| 539 | } {0} |
| 540 | do_test $test.13 { |
| 541 | sqlite3_column_text $STMT -1 |
| 542 | } {} |
| 543 | |
| 544 | } |
| 545 | |
| 546 | ifcapable !floatingpoint { |
| 547 | finish_test |
| 548 | return |
| 549 | } |
| 550 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 551 | do_test capi3c-5.0 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 552 | execsql { |
| 553 | CREATE TABLE t1(a VARINT, b BLOB, c VARCHAR(16)); |
| 554 | INSERT INTO t1 VALUES(1, 2, 3); |
| 555 | INSERT INTO t1 VALUES('one', 'two', NULL); |
| 556 | INSERT INTO t1 VALUES(1.2, 1.3, 1.4); |
| 557 | } |
| 558 | set sql "SELECT * FROM t1" |
| 559 | set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL] |
| 560 | sqlite3_column_count $STMT |
| 561 | } 3 |
| 562 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 563 | check_header $STMT capi3c-5.1 {a b c} {VARINT BLOB VARCHAR(16)} |
| 564 | check_origin_header $STMT capi3c-5.1 {main main main} {t1 t1 t1} {a b c} |
| 565 | do_test capi3c-5.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 566 | sqlite3_step $STMT |
| 567 | } SQLITE_ROW |
| 568 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 569 | check_header $STMT capi3c-5.3 {a b c} {VARINT BLOB VARCHAR(16)} |
| 570 | check_origin_header $STMT capi3c-5.3 {main main main} {t1 t1 t1} {a b c} |
| 571 | check_data $STMT capi3c-5.4 {INTEGER INTEGER TEXT} {1 2 3} {1.0 2.0 3.0} {1 2 3} |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 572 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 573 | do_test capi3c-5.5 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 574 | sqlite3_step $STMT |
| 575 | } SQLITE_ROW |
| 576 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 577 | check_header $STMT capi3c-5.6 {a b c} {VARINT BLOB VARCHAR(16)} |
| 578 | check_origin_header $STMT capi3c-5.6 {main main main} {t1 t1 t1} {a b c} |
| 579 | check_data $STMT capi3c-5.7 {TEXT TEXT NULL} {0 0 0} {0.0 0.0 0.0} {one two {}} |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 580 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 581 | do_test capi3c-5.8 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 582 | sqlite3_step $STMT |
| 583 | } SQLITE_ROW |
| 584 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 585 | check_header $STMT capi3c-5.9 {a b c} {VARINT BLOB VARCHAR(16)} |
| 586 | check_origin_header $STMT capi3c-5.9 {main main main} {t1 t1 t1} {a b c} |
| 587 | check_data $STMT capi3c-5.10 {FLOAT FLOAT TEXT} {1 1 1} {1.2 1.3 1.4} {1.2 1.3 1.4} |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 588 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 589 | do_test capi3c-5.11 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 590 | sqlite3_step $STMT |
| 591 | } SQLITE_DONE |
| 592 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 593 | do_test capi3c-5.12 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 594 | sqlite3_finalize $STMT |
| 595 | } SQLITE_OK |
| 596 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 597 | do_test capi3c-5.20 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 598 | set sql "SELECT a, sum(b), max(c) FROM t1 GROUP BY a" |
| 599 | set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL] |
| 600 | sqlite3_column_count $STMT |
| 601 | } 3 |
| 602 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 603 | check_header $STMT capi3c-5.21 {a sum(b) max(c)} {VARINT {} {}} |
| 604 | check_origin_header $STMT capi3c-5.22 {main {} {}} {t1 {} {}} {a {} {}} |
| 605 | do_test capi3c-5.23 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 606 | sqlite3_finalize $STMT |
| 607 | } SQLITE_OK |
| 608 | |
| 609 | |
| 610 | set ::ENC [execsql {pragma encoding}] |
| 611 | db close |
| 612 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 613 | do_test capi3c-6.0 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 614 | sqlite3 db test.db |
| 615 | set DB [sqlite3_connection_pointer db] |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 616 | if {[sqlite3 -has-codec]==0} { sqlite3_key $DB xyzzy } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 617 | set sql {SELECT a FROM t1 order by rowid} |
| 618 | set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL] |
| 619 | expr 0 |
| 620 | } {0} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 621 | do_test capi3c-6.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 622 | db cache flush |
| 623 | sqlite3_close $DB |
| 624 | } {SQLITE_BUSY} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 625 | do_test capi3c-6.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 626 | sqlite3_step $STMT |
| 627 | } {SQLITE_ROW} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 628 | check_data $STMT capi3c-6.3 {INTEGER} {1} {1.0} {1} |
| 629 | do_test capi3c-6.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 630 | sqlite3_finalize $STMT |
| 631 | } {SQLITE_OK} |
dan | afcf9bd | 2014-01-23 14:44:08 +0000 | [diff] [blame] | 632 | if {[clang_sanitize_address]==0} { |
| 633 | do_test capi3c-6.4 { |
| 634 | db cache flush |
| 635 | sqlite3_close $DB |
| 636 | } {SQLITE_OK} |
| 637 | do_test capi3c-6.99-misuse { |
| 638 | db close |
| 639 | } {} |
| 640 | } else { |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 641 | db close |
dan | afcf9bd | 2014-01-23 14:44:08 +0000 | [diff] [blame] | 642 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 643 | |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 644 | # This procedure sets the value of the file-format in file 'test.db' |
| 645 | # to $newval. Also, the schema cookie is incremented. |
| 646 | # |
| 647 | proc set_file_format {newval} { |
| 648 | hexio_write test.db 44 [hexio_render_int32 $newval] |
| 649 | set schemacookie [hexio_get_int [hexio_read test.db 40 4]] |
| 650 | incr schemacookie |
| 651 | hexio_write test.db 40 [hexio_render_int32 $schemacookie] |
| 652 | return {} |
| 653 | } |
| 654 | |
| 655 | # This procedure returns the value of the file-format in file 'test.db'. |
| 656 | # |
| 657 | proc get_file_format {{fname test.db}} { |
| 658 | return [hexio_get_int [hexio_read $fname 44 4]] |
| 659 | } |
| 660 | |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 661 | if {![sqlite3 -has-codec]} { |
| 662 | # Test what happens when the library encounters a newer file format. |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 663 | do_test capi3c-7.1 { |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 664 | set_file_format 5 |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 665 | } {} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 666 | do_test capi3c-7.2 { |
dan | cb35460 | 2010-07-08 09:44:42 +0000 | [diff] [blame] | 667 | catch { sqlite3 db test.db } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 668 | catchsql { |
| 669 | SELECT * FROM sqlite_master; |
| 670 | } |
| 671 | } {1 {unsupported file format}} |
| 672 | db close |
| 673 | } |
| 674 | |
| 675 | if {![sqlite3 -has-codec]} { |
| 676 | # Now test that the library correctly handles bogus entries in the |
| 677 | # sqlite_master table (schema corruption). |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 678 | do_test capi3c-8.1 { |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 679 | forcedelete test.db test.db-journal |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 680 | sqlite3 db test.db |
| 681 | execsql { |
| 682 | CREATE TABLE t1(a); |
| 683 | } |
| 684 | db close |
| 685 | } {} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 686 | do_test capi3c-8.2 { |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 687 | sqlite3 db test.db |
| 688 | execsql { |
| 689 | PRAGMA writable_schema=ON; |
| 690 | INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL); |
| 691 | } |
| 692 | db close |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 693 | } {} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 694 | do_test capi3c-8.3 { |
dan | cb35460 | 2010-07-08 09:44:42 +0000 | [diff] [blame] | 695 | catch { sqlite3 db test.db } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 696 | catchsql { |
| 697 | SELECT * FROM sqlite_master; |
| 698 | } |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 699 | } {1 {malformed database schema (?)}} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 700 | do_test capi3c-8.4 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 701 | # Build a 5-field row record. The first field is a string 'table', and |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 702 | # subsequent fields are all NULL. |
| 703 | db close |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 704 | forcedelete test.db test.db-journal |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 705 | sqlite3 db test.db |
| 706 | execsql { |
| 707 | CREATE TABLE t1(a); |
| 708 | PRAGMA writable_schema=ON; |
| 709 | INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL); |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 710 | } |
drh | bb8a279 | 2008-03-19 00:21:30 +0000 | [diff] [blame] | 711 | db close |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 712 | } {}; |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 713 | do_test capi3c-8.5 { |
dan | cb35460 | 2010-07-08 09:44:42 +0000 | [diff] [blame] | 714 | catch { sqlite3 db test.db } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 715 | catchsql { |
| 716 | SELECT * FROM sqlite_master; |
| 717 | } |
drh | 3453315 | 2008-03-19 13:03:33 +0000 | [diff] [blame] | 718 | } {1 {malformed database schema (?)}} |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 719 | db close |
| 720 | } |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 721 | forcedelete test.db |
| 722 | forcedelete test.db-journal |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 723 | |
| 724 | |
| 725 | # Test the english language string equivalents for sqlite error codes |
| 726 | set code2english [list \ |
| 727 | SQLITE_OK {not an error} \ |
| 728 | SQLITE_ERROR {SQL logic error or missing database} \ |
| 729 | SQLITE_PERM {access permission denied} \ |
| 730 | SQLITE_ABORT {callback requested query abort} \ |
| 731 | SQLITE_BUSY {database is locked} \ |
| 732 | SQLITE_LOCKED {database table is locked} \ |
| 733 | SQLITE_NOMEM {out of memory} \ |
| 734 | SQLITE_READONLY {attempt to write a readonly database} \ |
| 735 | SQLITE_INTERRUPT {interrupted} \ |
| 736 | SQLITE_IOERR {disk I/O error} \ |
| 737 | SQLITE_CORRUPT {database disk image is malformed} \ |
| 738 | SQLITE_FULL {database or disk is full} \ |
| 739 | SQLITE_CANTOPEN {unable to open database file} \ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 740 | SQLITE_EMPTY {table contains no data} \ |
| 741 | SQLITE_SCHEMA {database schema has changed} \ |
| 742 | SQLITE_CONSTRAINT {constraint failed} \ |
| 743 | SQLITE_MISMATCH {datatype mismatch} \ |
| 744 | SQLITE_MISUSE {library routine called out of sequence} \ |
drh | 6559404 | 2008-05-05 16:56:34 +0000 | [diff] [blame] | 745 | SQLITE_NOLFS {large file support is disabled} \ |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 746 | SQLITE_AUTH {authorization denied} \ |
| 747 | SQLITE_FORMAT {auxiliary database format error} \ |
| 748 | SQLITE_RANGE {bind or column index out of range} \ |
| 749 | SQLITE_NOTADB {file is encrypted or is not a database} \ |
| 750 | unknownerror {unknown error} \ |
| 751 | ] |
| 752 | |
| 753 | set test_number 1 |
| 754 | foreach {code english} $code2english { |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 755 | do_test capi3c-9.$test_number "sqlite3_test_errstr $code" $english |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 756 | incr test_number |
| 757 | } |
| 758 | |
| 759 | # Test the error message when a "real" out of memory occurs. |
dan | a879342 | 2012-06-07 07:24:04 +0000 | [diff] [blame] | 760 | if { [permutation] != "nofaultsim" } { |
danielk1977 | 369ff42 | 2007-09-03 07:31:09 +0000 | [diff] [blame] | 761 | ifcapable memdebug { |
danielk1977 | 222a757 | 2007-08-25 13:37:48 +0000 | [diff] [blame] | 762 | do_test capi3c-10-1 { |
| 763 | sqlite3 db test.db |
| 764 | set DB [sqlite3_connection_pointer db] |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 765 | sqlite3_memdebug_fail 0 |
danielk1977 | 222a757 | 2007-08-25 13:37:48 +0000 | [diff] [blame] | 766 | catchsql { |
| 767 | select * from sqlite_master; |
| 768 | } |
| 769 | } {1 {out of memory}} |
| 770 | do_test capi3c-10-2 { |
| 771 | sqlite3_errmsg $::DB |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 772 | } {out of memory} |
danielk1977 | 222a757 | 2007-08-25 13:37:48 +0000 | [diff] [blame] | 773 | ifcapable {utf16} { |
| 774 | do_test capi3c-10-3 { |
| 775 | utf8 [sqlite3_errmsg16 $::DB] |
| 776 | } {out of memory} |
| 777 | } |
| 778 | db close |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 779 | sqlite3_memdebug_fail -1 |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 780 | } |
dan | a879342 | 2012-06-07 07:24:04 +0000 | [diff] [blame] | 781 | } |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 782 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 783 | # The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 784 | # statement issued while there are still outstanding VMs that are part of |
| 785 | # the transaction fails. |
| 786 | sqlite3 db test.db |
| 787 | set DB [sqlite3_connection_pointer db] |
| 788 | sqlite_register_test_function $DB func |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 789 | do_test capi3c-11.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 790 | execsql { |
| 791 | BEGIN; |
| 792 | CREATE TABLE t1(a, b); |
| 793 | INSERT INTO t1 VALUES(1, 'int'); |
| 794 | INSERT INTO t1 VALUES(2, 'notatype'); |
| 795 | } |
| 796 | } {} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 797 | do_test capi3c-11.1.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 798 | sqlite3_get_autocommit $DB |
| 799 | } 0 |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 800 | do_test capi3c-11.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 801 | set STMT [sqlite3_prepare_v2 $DB "SELECT func(b, a) FROM t1" -1 TAIL] |
| 802 | sqlite3_step $STMT |
| 803 | } {SQLITE_ROW} |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 804 | |
| 805 | # As of 3.6.5 a COMMIT is OK during while a query is still running - |
| 806 | # as long as it is a read-only query and not an incremental BLOB write. |
| 807 | # |
| 808 | do_test capi3-11.3.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 809 | catchsql { |
| 810 | COMMIT; |
| 811 | } |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 812 | } {0 {}} |
| 813 | do_test capi3-11.3.2 { |
| 814 | sqlite3_extended_errcode $DB |
| 815 | } {SQLITE_OK} |
| 816 | do_test capi3-11.3.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 817 | sqlite3_get_autocommit $DB |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 818 | } 1 |
| 819 | do_test capi3-11.3.4 { |
| 820 | db eval {PRAGMA lock_status} |
| 821 | } {main shared temp closed} |
| 822 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 823 | do_test capi3c-11.4 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 824 | sqlite3_step $STMT |
| 825 | } {SQLITE_ERROR} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 826 | do_test capi3c-11.5 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 827 | sqlite3_finalize $STMT |
| 828 | } {SQLITE_ERROR} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 829 | do_test capi3c-11.6 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 830 | catchsql { |
| 831 | SELECT * FROM t1; |
| 832 | } |
| 833 | } {0 {1 int 2 notatype}} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 834 | do_test capi3c-11.7 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 835 | sqlite3_get_autocommit $DB |
| 836 | } 1 |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 837 | do_test capi3c-11.8 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 838 | execsql { |
| 839 | CREATE TABLE t2(a); |
| 840 | INSERT INTO t2 VALUES(1); |
| 841 | INSERT INTO t2 VALUES(2); |
| 842 | BEGIN; |
| 843 | INSERT INTO t2 VALUES(3); |
| 844 | } |
| 845 | } {} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 846 | do_test capi3c-11.8.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 847 | sqlite3_get_autocommit $DB |
| 848 | } 0 |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 849 | do_test capi3c-11.9 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 850 | set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL] |
| 851 | sqlite3_step $STMT |
| 852 | } {SQLITE_ROW} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 853 | do_test capi3c-11.9.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 854 | sqlite3_get_autocommit $DB |
| 855 | } 0 |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 856 | do_test capi3c-11.9.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 857 | catchsql { |
| 858 | ROLLBACK; |
| 859 | } |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 860 | } {0 {}} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 861 | do_test capi3c-11.9.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 862 | sqlite3_get_autocommit $DB |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 863 | } 1 |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 864 | do_test capi3c-11.10 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 865 | sqlite3_step $STMT |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 866 | } {SQLITE_ROW} |
dan | 2bd2cfc | 2012-04-03 18:33:43 +0000 | [diff] [blame] | 867 | ifcapable !autoreset { |
| 868 | # If SQLITE_OMIT_AUTORESET is defined, then the statement must be |
| 869 | # reset() before it can be passed to step() again. |
| 870 | do_test capi3-11.11a { sqlite3_step $STMT } {SQLITE_MISUSE} |
| 871 | do_test capi3-11.11b { sqlite3_reset $STMT } {SQLITE_ABORT} |
| 872 | } |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 873 | do_test capi3c-11.11 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 874 | sqlite3_step $STMT |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 875 | } {SQLITE_DONE} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 876 | do_test capi3c-11.12 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 877 | sqlite3_step $STMT |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 878 | sqlite3_step $STMT |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 879 | } {SQLITE_ROW} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 880 | do_test capi3c-11.13 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 881 | sqlite3_finalize $STMT |
| 882 | } {SQLITE_OK} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 883 | do_test capi3c-11.14 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 884 | execsql { |
| 885 | SELECT a FROM t2; |
| 886 | } |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 887 | } {1 2} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 888 | do_test capi3c-11.14.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 889 | sqlite3_get_autocommit $DB |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 890 | } 1 |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 891 | do_test capi3c-11.15 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 892 | catchsql { |
| 893 | ROLLBACK; |
| 894 | } |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 895 | } {1 {cannot rollback - no transaction is active}} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 896 | do_test capi3c-11.15.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 897 | sqlite3_get_autocommit $DB |
| 898 | } 1 |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 899 | do_test capi3c-11.16 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 900 | execsql { |
| 901 | SELECT a FROM t2; |
| 902 | } |
| 903 | } {1 2} |
| 904 | |
| 905 | # Sanity check on the definition of 'outstanding VM'. This means any VM |
| 906 | # that has had sqlite3_step() called more recently than sqlite3_finalize() or |
| 907 | # sqlite3_reset(). So a VM that has just been prepared or reset does not |
| 908 | # count as an active VM. |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 909 | do_test capi3c-11.17 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 910 | execsql { |
| 911 | BEGIN; |
| 912 | } |
| 913 | } {} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 914 | do_test capi3c-11.18 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 915 | set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t1" -1 TAIL] |
| 916 | catchsql { |
| 917 | COMMIT; |
| 918 | } |
| 919 | } {0 {}} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 920 | do_test capi3c-11.19 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 921 | sqlite3_step $STMT |
| 922 | } {SQLITE_ROW} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 923 | do_test capi3c-11.20 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 924 | catchsql { |
| 925 | BEGIN; |
| 926 | COMMIT; |
| 927 | } |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 928 | } {0 {}} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 929 | do_test capi3c-11.20 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 930 | sqlite3_reset $STMT |
| 931 | catchsql { |
| 932 | COMMIT; |
| 933 | } |
drh | ad4a4b8 | 2008-11-05 16:37:34 +0000 | [diff] [blame] | 934 | } {1 {cannot commit - no transaction is active}} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 935 | do_test capi3c-11.21 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 936 | sqlite3_finalize $STMT |
| 937 | } {SQLITE_OK} |
| 938 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 939 | # The following tests - capi3c-12.* - check that its Ok to start a |
| 940 | # transaction while other VMs are active, and that its Ok to execute |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 941 | # atomic updates in the same situation |
| 942 | # |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 943 | do_test capi3c-12.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 944 | set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL] |
| 945 | sqlite3_step $STMT |
| 946 | } {SQLITE_ROW} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 947 | do_test capi3c-12.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 948 | catchsql { |
| 949 | INSERT INTO t1 VALUES(3, NULL); |
| 950 | } |
| 951 | } {0 {}} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 952 | do_test capi3c-12.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 953 | catchsql { |
| 954 | INSERT INTO t2 VALUES(4); |
| 955 | } |
| 956 | } {0 {}} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 957 | do_test capi3c-12.4 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 958 | catchsql { |
| 959 | BEGIN; |
| 960 | INSERT INTO t1 VALUES(4, NULL); |
| 961 | } |
| 962 | } {0 {}} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 963 | do_test capi3c-12.5 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 964 | sqlite3_step $STMT |
| 965 | } {SQLITE_ROW} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 966 | do_test capi3c-12.5.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 967 | sqlite3_step $STMT |
| 968 | } {SQLITE_ROW} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 969 | do_test capi3c-12.6 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 970 | sqlite3_step $STMT |
| 971 | } {SQLITE_DONE} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 972 | do_test capi3c-12.7 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 973 | sqlite3_finalize $STMT |
| 974 | } {SQLITE_OK} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 975 | do_test capi3c-12.8 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 976 | execsql { |
| 977 | COMMIT; |
| 978 | SELECT a FROM t1; |
| 979 | } |
| 980 | } {1 2 3 4} |
| 981 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 982 | # Test cases capi3c-13.* test the sqlite3_clear_bindings() and |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 983 | # sqlite3_sleep APIs. |
| 984 | # |
| 985 | if {[llength [info commands sqlite3_clear_bindings]]>0} { |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 986 | do_test capi3c-13.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 987 | execsql { |
| 988 | DELETE FROM t1; |
| 989 | } |
| 990 | set STMT [sqlite3_prepare_v2 $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL] |
| 991 | sqlite3_step $STMT |
| 992 | } {SQLITE_DONE} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 993 | do_test capi3c-13.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 994 | sqlite3_reset $STMT |
| 995 | sqlite3_bind_text $STMT 1 hello 5 |
| 996 | sqlite3_bind_text $STMT 2 world 5 |
| 997 | sqlite3_step $STMT |
| 998 | } {SQLITE_DONE} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 999 | do_test capi3c-13.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1000 | sqlite3_reset $STMT |
| 1001 | sqlite3_clear_bindings $STMT |
| 1002 | sqlite3_step $STMT |
| 1003 | } {SQLITE_DONE} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1004 | do_test capi3c-13-4 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1005 | sqlite3_finalize $STMT |
| 1006 | execsql { |
| 1007 | SELECT * FROM t1; |
| 1008 | } |
| 1009 | } {{} {} hello world {} {}} |
| 1010 | } |
| 1011 | if {[llength [info commands sqlite3_sleep]]>0} { |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1012 | do_test capi3c-13-5 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1013 | set ms [sqlite3_sleep 80] |
| 1014 | expr {$ms==80 || $ms==1000} |
| 1015 | } {1} |
| 1016 | } |
| 1017 | |
| 1018 | # Ticket #1219: Make sure binding APIs can handle a NULL pointer. |
| 1019 | # |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1020 | do_test capi3c-14.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1021 | set rc [catch {sqlite3_bind_text 0 1 hello 5} msg] |
| 1022 | lappend rc $msg |
| 1023 | } {1 SQLITE_MISUSE} |
| 1024 | |
| 1025 | # Ticket #1650: Honor the nBytes parameter to sqlite3_prepare. |
| 1026 | # |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1027 | do_test capi3c-15.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1028 | set sql {SELECT * FROM t2} |
| 1029 | set nbytes [string length $sql] |
| 1030 | append sql { WHERE a==1} |
| 1031 | set STMT [sqlite3_prepare_v2 $DB $sql $nbytes TAIL] |
| 1032 | sqlite3_step $STMT |
| 1033 | sqlite3_column_int $STMT 0 |
| 1034 | } {1} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1035 | do_test capi3c-15.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1036 | sqlite3_step $STMT |
| 1037 | sqlite3_column_int $STMT 0 |
| 1038 | } {2} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1039 | do_test capi3c-15.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1040 | sqlite3_finalize $STMT |
| 1041 | } {SQLITE_OK} |
| 1042 | |
| 1043 | # Make sure code is always generated even if an IF EXISTS or |
| 1044 | # IF NOT EXISTS clause is present that the table does not or |
| 1045 | # does exists. That way we will always have a prepared statement |
| 1046 | # to expire when the schema changes. |
| 1047 | # |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1048 | do_test capi3c-16.1 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1049 | set sql {DROP TABLE IF EXISTS t3} |
| 1050 | set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL] |
| 1051 | sqlite3_finalize $STMT |
| 1052 | expr {$STMT!=""} |
| 1053 | } {1} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1054 | do_test capi3c-16.2 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1055 | set sql {CREATE TABLE IF NOT EXISTS t1(x,y)} |
| 1056 | set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL] |
| 1057 | sqlite3_finalize $STMT |
| 1058 | expr {$STMT!=""} |
| 1059 | } {1} |
| 1060 | |
| 1061 | # But still we do not generate code if there is no SQL |
| 1062 | # |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1063 | do_test capi3c-16.3 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1064 | set STMT [sqlite3_prepare_v2 $DB {} -1 TAIL] |
| 1065 | sqlite3_finalize $STMT |
| 1066 | expr {$STMT==""} |
| 1067 | } {1} |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1068 | do_test capi3c-16.4 { |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1069 | set STMT [sqlite3_prepare_v2 $DB {;} -1 TAIL] |
| 1070 | sqlite3_finalize $STMT |
| 1071 | expr {$STMT==""} |
| 1072 | } {1} |
| 1073 | |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 1074 | # Ticket #2154. |
| 1075 | # |
| 1076 | do_test capi3c-17.1 { |
| 1077 | set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t2} -1 TAIL] |
| 1078 | sqlite3_step $STMT |
| 1079 | } SQLITE_ROW |
| 1080 | do_test capi3c-17.2 { |
| 1081 | sqlite3_column_int $STMT 0 |
| 1082 | } 4 |
| 1083 | do_test capi3c-17.3 { |
| 1084 | sqlite3_step $STMT |
| 1085 | } SQLITE_DONE |
| 1086 | do_test capi3c-17.4 { |
| 1087 | sqlite3_reset $STMT |
| 1088 | db eval {CREATE INDEX i2 ON t2(a)} |
| 1089 | sqlite3_step $STMT |
| 1090 | } SQLITE_ROW |
| 1091 | do_test capi3c-17.5 { |
| 1092 | sqlite3_column_int $STMT 0 |
| 1093 | } 4 |
| 1094 | do_test capi3c-17.6 { |
| 1095 | sqlite3_step $STMT |
| 1096 | } SQLITE_DONE |
| 1097 | do_test capi3c-17.7 { |
| 1098 | sqlite3_reset $STMT |
| 1099 | db eval {DROP INDEX i2} |
| 1100 | sqlite3_step $STMT |
| 1101 | } SQLITE_ROW |
| 1102 | do_test capi3c-17.8 { |
| 1103 | sqlite3_column_int $STMT 0 |
| 1104 | } 4 |
| 1105 | do_test capi3c-17.9 { |
| 1106 | sqlite3_step $STMT |
| 1107 | } SQLITE_DONE |
| 1108 | do_test capi3c-17.10 { |
| 1109 | sqlite3_finalize $STMT |
| 1110 | set STMT [sqlite3_prepare_v2 $DB {SELECT b FROM t1 WHERE a=?} -1 TAIL] |
| 1111 | sqlite3_bind_int $STMT 1 2 |
| 1112 | db eval { |
| 1113 | DELETE FROM t1; |
| 1114 | INSERT INTO t1 VALUES(1,'one'); |
| 1115 | INSERT INTO t1 VALUES(2,'two'); |
| 1116 | INSERT INTO t1 VALUES(3,'three'); |
| 1117 | INSERT INTO t1 VALUES(4,'four'); |
| 1118 | } |
| 1119 | sqlite3_step $STMT |
| 1120 | } SQLITE_ROW |
| 1121 | do_test capi3c-17.11 { |
| 1122 | sqlite3_column_text $STMT 0 |
| 1123 | } two |
| 1124 | do_test capi3c-17.12 { |
| 1125 | sqlite3_step $STMT |
| 1126 | } SQLITE_DONE |
| 1127 | do_test capi3c-17.13 { |
| 1128 | sqlite3_reset $STMT |
| 1129 | db eval {CREATE INDEX i1 ON t1(a)} |
| 1130 | sqlite3_step $STMT |
| 1131 | } SQLITE_ROW |
| 1132 | do_test capi3c-17.14 { |
| 1133 | sqlite3_column_text $STMT 0 |
| 1134 | } two |
| 1135 | do_test capi3c-17.15 { |
| 1136 | sqlite3_step $STMT |
| 1137 | } SQLITE_DONE |
| 1138 | do_test capi3c-17.16 { |
| 1139 | sqlite3_reset $STMT |
| 1140 | db eval {DROP INDEX i1} |
| 1141 | sqlite3_step $STMT |
| 1142 | } SQLITE_ROW |
| 1143 | do_test capi3c-17.17 { |
| 1144 | sqlite3_column_text $STMT 0 |
| 1145 | } two |
| 1146 | do_test capi3c-17.18 { |
| 1147 | sqlite3_step $STMT |
| 1148 | } SQLITE_DONE |
| 1149 | do_test capi3c-17.99 { |
| 1150 | sqlite3_finalize $STMT |
| 1151 | } SQLITE_OK |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1152 | |
drh | f1d89f0 | 2007-01-08 22:40:32 +0000 | [diff] [blame] | 1153 | # On the mailing list it has been reported that finalizing after |
| 1154 | # an SQLITE_BUSY return leads to a segfault. Here we test that case. |
| 1155 | # |
| 1156 | do_test capi3c-18.1 { |
| 1157 | sqlite3 db2 test.db |
| 1158 | set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t1} -1 TAIL] |
| 1159 | sqlite3_step $STMT |
| 1160 | } SQLITE_ROW |
| 1161 | do_test capi3c-18.2 { |
| 1162 | sqlite3_column_int $STMT 0 |
| 1163 | } 4 |
| 1164 | do_test capi3c-18.3 { |
| 1165 | sqlite3_reset $STMT |
| 1166 | db2 eval {BEGIN EXCLUSIVE} |
| 1167 | sqlite3_step $STMT |
| 1168 | } SQLITE_BUSY |
| 1169 | do_test capi3c-18.4 { |
| 1170 | sqlite3_finalize $STMT |
| 1171 | } SQLITE_BUSY |
| 1172 | do_test capi3c-18.5 { |
| 1173 | db2 eval {COMMIT} |
| 1174 | db2 close |
| 1175 | } {} |
| 1176 | |
drh | d47bcb9 | 2007-01-09 15:02:03 +0000 | [diff] [blame] | 1177 | # Ticket #2158. The sqlite3_step() will still return SQLITE_SCHEMA |
| 1178 | # if the database schema changes in a way that makes the statement |
| 1179 | # no longer valid. |
| 1180 | # |
| 1181 | do_test capi3c-19.1 { |
| 1182 | db eval { |
| 1183 | CREATE TABLE t3(x,y); |
| 1184 | INSERT INTO t3 VALUES(1,2); |
| 1185 | } |
| 1186 | set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL] |
| 1187 | sqlite3_step $STMT |
| 1188 | } SQLITE_ROW |
| 1189 | do_test capi3c-19.2 { |
| 1190 | sqlite3_column_int $STMT 0 |
| 1191 | } 1 |
| 1192 | do_test capi3c-19.3 { |
| 1193 | sqlite3_step $STMT |
| 1194 | } SQLITE_DONE |
| 1195 | do_test capi3c-19.4 { |
| 1196 | sqlite3_reset $STMT |
| 1197 | db eval {DROP TABLE t3} |
| 1198 | sqlite3_step $STMT |
drh | 7823006 | 2010-02-24 18:40:39 +0000 | [diff] [blame] | 1199 | } SQLITE_ERROR |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1200 | do_test capi3c-19.4.1 { |
drh | 297a66c | 2007-01-09 15:06:41 +0000 | [diff] [blame] | 1201 | sqlite3_errmsg $DB |
| 1202 | } {no such table: t3} |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1203 | ifcapable deprecated { |
| 1204 | do_test capi3c-19.4.2 { |
| 1205 | sqlite3_expired $STMT |
| 1206 | } 1 |
| 1207 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1208 | do_test capi3c-19.4.3 { |
| 1209 | sqlite3_errmsg $DB |
| 1210 | } {no such table: t3} |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1211 | ifcapable deprecated { |
| 1212 | do_test capi3c-19.4.4 { |
| 1213 | sqlite3_expired 0 |
| 1214 | } 1 |
| 1215 | } |
drh | d47bcb9 | 2007-01-09 15:02:03 +0000 | [diff] [blame] | 1216 | do_test capi3c-19.5 { |
| 1217 | sqlite3_reset $STMT |
| 1218 | db eval { |
| 1219 | CREATE TABLE t3(x,y); |
| 1220 | INSERT INTO t3 VALUES(1,2); |
| 1221 | } |
| 1222 | sqlite3_step $STMT |
| 1223 | } SQLITE_ROW |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 1224 | ifcapable deprecated { |
| 1225 | do_test capi3c-19.5.2 { |
| 1226 | sqlite3_expired $STMT |
| 1227 | } 0 |
| 1228 | } |
drh | d47bcb9 | 2007-01-09 15:02:03 +0000 | [diff] [blame] | 1229 | do_test capi3c-19.6 { |
| 1230 | sqlite3_column_int $STMT 1 |
| 1231 | } 2 |
| 1232 | do_test capi3c-19.99 { |
| 1233 | sqlite3_finalize $STMT |
| 1234 | } SQLITE_OK |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1235 | |
drh | f6d8ab8 | 2007-01-12 23:43:42 +0000 | [diff] [blame] | 1236 | # Make sure a change in a separate database connection does not |
| 1237 | # cause an SQLITE_SCHEMA return. |
| 1238 | # |
| 1239 | do_test capi3c-20.1 { |
| 1240 | set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL] |
| 1241 | sqlite3 db2 test.db |
| 1242 | db2 eval {CREATE TABLE t4(x)} |
| 1243 | sqlite3_step $STMT |
| 1244 | } SQLITE_ROW |
| 1245 | do_test capi3c-20.2 { |
| 1246 | sqlite3_column_int $STMT 1 |
| 1247 | } 2 |
| 1248 | do_test capi3c-20.3 { |
| 1249 | sqlite3_step $STMT |
| 1250 | } SQLITE_DONE |
| 1251 | do_test capi3c-20.4 { |
| 1252 | db2 close |
| 1253 | sqlite3_finalize $STMT |
| 1254 | } SQLITE_OK |
| 1255 | |
danielk1977 | 612642d | 2007-07-12 13:18:05 +0000 | [diff] [blame] | 1256 | # Test that sqlite3_step() sets the database error code correctly. |
| 1257 | # See ticket #2497. |
| 1258 | # |
drh | 3260548 | 2007-07-19 22:30:19 +0000 | [diff] [blame] | 1259 | ifcapable progress { |
| 1260 | do_test capi3c-21.1 { |
| 1261 | set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL] |
| 1262 | db progress 5 "expr 1" |
| 1263 | sqlite3_step $STMT |
| 1264 | } {SQLITE_INTERRUPT} |
| 1265 | do_test capi3c-21.2 { |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 1266 | sqlite3_extended_errcode $DB |
drh | 3260548 | 2007-07-19 22:30:19 +0000 | [diff] [blame] | 1267 | } {SQLITE_INTERRUPT} |
| 1268 | do_test capi3c-21.3 { |
| 1269 | sqlite3_finalize $STMT |
| 1270 | } {SQLITE_INTERRUPT} |
| 1271 | do_test capi3c-21.4 { |
| 1272 | set STMT [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL] |
| 1273 | db progress 5 "expr 1" |
| 1274 | sqlite3_step $STMT |
| 1275 | } {SQLITE_ERROR} |
| 1276 | do_test capi3c-21.5 { |
| 1277 | sqlite3_errcode $DB |
| 1278 | } {SQLITE_ERROR} |
| 1279 | do_test capi3c-21.6 { |
| 1280 | sqlite3_finalize $STMT |
| 1281 | } {SQLITE_INTERRUPT} |
| 1282 | do_test capi3c-21.7 { |
| 1283 | sqlite3_errcode $DB |
| 1284 | } {SQLITE_INTERRUPT} |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 1285 | do_test capi3c-21.8 { |
| 1286 | sqlite3_extended_errcode $DB |
| 1287 | } {SQLITE_INTERRUPT} |
drh | 00e087b | 2008-04-10 17:14:07 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | # Make sure sqlite3_result_error_code() returns the correct error code. |
| 1291 | # See ticket #2940 |
| 1292 | # |
| 1293 | do_test capi3c-22.1 { |
| 1294 | db progress 0 {} |
| 1295 | set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',3)} -1 TAIL] |
| 1296 | sqlite3_step $STMT |
| 1297 | } {SQLITE_PERM} |
| 1298 | sqlite3_finalize $STMT |
| 1299 | do_test capi3c-22.2 { |
| 1300 | set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',4)} -1 TAIL] |
| 1301 | sqlite3_step $STMT |
| 1302 | } {SQLITE_ABORT} |
| 1303 | sqlite3_finalize $STMT |
| 1304 | do_test capi3c-22.3 { |
| 1305 | set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',16)} -1 TAIL] |
| 1306 | sqlite3_step $STMT |
| 1307 | } {SQLITE_EMPTY} |
| 1308 | sqlite3_finalize $STMT |
| 1309 | |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 1310 | # For a multi-column result set where the same table column is repeated |
| 1311 | # in multiple columns of the output, verify that doing a UTF-8 to UTF-16 |
| 1312 | # conversion (or vice versa) on one column does not change the value of |
| 1313 | # the second. |
| 1314 | # |
danielk1977 | 257d9dc | 2009-07-22 07:27:56 +0000 | [diff] [blame] | 1315 | ifcapable utf16 { |
| 1316 | do_test capi3c-23.1 { |
| 1317 | set STMT [sqlite3_prepare_v2 db {SELECT b,b,b,b FROM t1} -1 TAIL] |
| 1318 | sqlite3_step $STMT |
| 1319 | } {SQLITE_ROW} |
| 1320 | do_test capi3c-23.2 { |
| 1321 | sqlite3_column_text16 $STMT 0 |
| 1322 | sqlite3_column_text $STMT 1 |
| 1323 | } {one} |
| 1324 | do_test capi3c-23.3 { |
| 1325 | sqlite3_column_text16 $STMT 2 |
| 1326 | sqlite3_column_text $STMT 3 |
| 1327 | } {one} |
| 1328 | sqlite3_finalize $STMT |
| 1329 | do_test capi3c-23.4 { |
| 1330 | set STMT [sqlite3_prepare_v2 db {SELECT b||'x',b,b,b FROM t1} -1 TAIL] |
| 1331 | sqlite3_step $STMT |
| 1332 | } {SQLITE_ROW} |
| 1333 | do_test capi3c-23.5 { |
| 1334 | sqlite3_column_text16 $STMT 0 |
| 1335 | sqlite3_column_text $STMT 1 |
| 1336 | } {one} |
| 1337 | do_test capi3c-23.6 { |
| 1338 | sqlite3_column_text16 $STMT 2 |
| 1339 | sqlite3_column_text $STMT 3 |
| 1340 | } {one} |
| 1341 | sqlite3_finalize $STMT |
| 1342 | } |
danielk1977 | 612642d | 2007-07-12 13:18:05 +0000 | [diff] [blame] | 1343 | |
dan | 43bc88b | 2009-09-10 10:15:59 +0000 | [diff] [blame] | 1344 | # Test decltype on some SELECT statements that contain sub-selects. |
| 1345 | # |
| 1346 | proc decltype {zSql} { |
| 1347 | set ret [list] |
| 1348 | set STMT [sqlite3_prepare_v2 db $zSql -1 TAIL] |
| 1349 | for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} { |
| 1350 | lappend ret [sqlite3_column_decltype $STMT $i] |
| 1351 | } |
| 1352 | sqlite3_finalize $STMT |
| 1353 | return $ret |
| 1354 | } |
| 1355 | do_test capi3c-24.1 { |
| 1356 | execsql { CREATE TABLE t5(a INTEGER, b STRING, c DATETIME) } |
| 1357 | decltype {SELECT * FROM t5} |
| 1358 | } {INTEGER STRING DATETIME} |
| 1359 | do_test capi3c-24.2 { |
| 1360 | decltype {SELECT (SELECT c) FROM t5} |
| 1361 | } {DATETIME} |
| 1362 | do_test capi3c-24.3 { |
| 1363 | decltype {SELECT (SELECT * FROM (SELECT c)) FROM t5} |
| 1364 | } {DATETIME} |
| 1365 | do_test capi3c-24.4 { |
| 1366 | decltype {SELECT * FROM (SELECT * FROM t5 ORDER BY c LIMIT 1) ORDER BY b} |
| 1367 | } {INTEGER STRING DATETIME} |
| 1368 | do_test capi3c-24.5 { |
| 1369 | decltype { |
| 1370 | SELECT (SELECT x FROM (SELECT c AS x)) |
| 1371 | FROM (SELECT * FROM t5 ORDER BY c LIMIT 1) ORDER BY b |
| 1372 | } |
| 1373 | } {DATETIME} |
| 1374 | do_test capi3c-24.3 { |
| 1375 | decltype {SELECT (SELECT x FROM (SELECT t5.a AS x)) FROM t5} |
| 1376 | } {INTEGER} |
| 1377 | |
drh | b900aaf | 2006-11-09 00:24:53 +0000 | [diff] [blame] | 1378 | finish_test |