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