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 } {bad parameter or other API misuse} 191 ifcapable {utf16} { 192 do_test capi3-3.6.3-misuse { 193 utf8 [sqlite3_errmsg16 $db2] 194 } {bad parameter or other API misuse} 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} \ 773SQLITE_PERM {access permission denied} \ 774SQLITE_ABORT {query aborted} \ 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_SCHEMA {database schema has changed} \ 785SQLITE_CONSTRAINT {constraint failed} \ 786SQLITE_MISMATCH {datatype mismatch} \ 787SQLITE_MISUSE {bad parameter or other API misuse} \ 788SQLITE_AUTH {authorization denied} \ 789SQLITE_RANGE {column index out of range} \ 790SQLITE_NOTADB {file is not a database} \ 791unknownerror {unknown error} \ 792] 793 794set test_number 1 795foreach {code english} $code2english { 796 do_test capi3-9.$test_number "sqlite3_test_errstr $code" $english 797 incr test_number 798} 799 800# Test the error message when a "real" out of memory occurs. 801if { [permutation] != "nofaultsim" } { 802ifcapable memdebug { 803 do_test capi3-10-1 { 804 sqlite3 db test.db 805 set DB [sqlite3_connection_pointer db] 806 sqlite3_memdebug_fail 1 807 catchsql { 808 select * from sqlite_master; 809 } 810 } {1 {out of memory}} 811 do_test capi3-10-2 { 812 sqlite3_errmsg $::DB 813 } {out of memory} 814 ifcapable {utf16} { 815 do_test capi3-10-3 { 816 utf8 [sqlite3_errmsg16 $::DB] 817 } {out of memory} 818 } 819 db close 820 sqlite3_memdebug_fail -1 821 do_test capi3-10-4 { 822 sqlite3 db test.db 823 set DB [sqlite3_connection_pointer db] 824 sqlite3_memdebug_fail 1 825 catchsql { 826 select * from sqlite_master where rowid>5; 827 } 828 } {1 {out of memory}} 829 do_test capi3-10-5 { 830 sqlite3_errmsg $::DB 831 } {out of memory} 832 ifcapable {utf16} { 833 do_test capi3-10-6 { 834 utf8 [sqlite3_errmsg16 $::DB] 835 } {out of memory} 836 } 837 db close 838 sqlite3_memdebug_fail -1 839} 840} 841 842# The following tests - capi3-11.* - test that a COMMIT or ROLLBACK 843# statement issued while there are still outstanding VMs that are part of 844# the transaction fails. 845sqlite3 db test.db 846set DB [sqlite3_connection_pointer db] 847sqlite_register_test_function $DB func 848do_test capi3-11.1 { 849 execsql { 850 BEGIN; 851 CREATE TABLE t1(a, b); 852 INSERT INTO t1 VALUES(1, 'int'); 853 INSERT INTO t1 VALUES(2, 'notatype'); 854 } 855} {} 856do_test capi3-11.1.1 { 857 sqlite3_get_autocommit $DB 858} 0 859do_test capi3-11.2 { 860 set STMT [sqlite3_prepare $DB "SELECT func(b, a) FROM t1" -1 TAIL] 861 sqlite3_step $STMT 862} {SQLITE_ROW} 863 864# As of 3.6.5 a COMMIT is OK during while a query is still running - 865# as long as it is a read-only query and not an incremental BLOB write. 866# 867do_test capi3-11.3.1 { 868 catchsql { 869 COMMIT; 870 } 871} {0 {}} 872do_test capi3-11.3.2 { 873 sqlite3_extended_errcode $DB 874} {SQLITE_OK} 875do_test capi3-11.3.3 { 876 sqlite3_get_autocommit $DB 877} 1 878do_test capi3-11.3.4 { 879 db eval {PRAGMA lock_status} 880} {main shared temp closed} 881 882do_test capi3-11.4 { 883 sqlite3_step $STMT 884} {SQLITE_ERROR} 885do_test capi3-11.5 { 886 sqlite3_finalize $STMT 887} {SQLITE_ERROR} 888do_test capi3-11.6 { 889 catchsql { 890 SELECT * FROM t1; 891 } 892} {0 {1 int 2 notatype}} 893do_test capi3-11.7 { 894 sqlite3_get_autocommit $DB 895} 1 896do_test capi3-11.8 { 897 execsql { 898 CREATE TABLE t2(a); 899 INSERT INTO t2 VALUES(1); 900 INSERT INTO t2 VALUES(2); 901 BEGIN; 902 INSERT INTO t2 VALUES(3); 903 } 904} {} 905do_test capi3-11.8.1 { 906 sqlite3_get_autocommit $DB 907} 0 908do_test capi3-11.9 { 909 set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL] 910 sqlite3_step $STMT 911} {SQLITE_ROW} 912do_test capi3-11.9.1 { 913 sqlite3_get_autocommit $DB 914} 0 915do_test capi3-11.9.2 { 916 catchsql { 917 ROLLBACK; 918 } 919} {0 {}} 920do_test capi3-11.9.3 { 921 sqlite3_get_autocommit $DB 922} 1 923do_test capi3-11.10 { 924 sqlite3_step $STMT 925} {SQLITE_ROW} 926do_test capi3-11.11 { 927 sqlite3_step $STMT 928} {SQLITE_DONE} 929ifcapable !autoreset { 930 do_test capi3-11.12armor { 931 sqlite3_step $STMT 932 sqlite3_step $STMT 933 } {SQLITE_MISUSE} 934} else { 935 do_test capi3-11.12 { 936 sqlite3_step $STMT 937 sqlite3_step $STMT 938 } {SQLITE_ROW} 939} 940do_test capi3-11.13 { 941 sqlite3_finalize $STMT 942} {SQLITE_OK} 943do_test capi3-11.14 { 944 execsql { 945 SELECT a FROM t2; 946 } 947} {1 2} 948do_test capi3-11.14.1 { 949 sqlite3_get_autocommit $DB 950} 1 951do_test capi3-11.15 { 952 catchsql { 953 ROLLBACK; 954 } 955} {1 {cannot rollback - no transaction is active}} 956do_test capi3-11.15.1 { 957 sqlite3_get_autocommit $DB 958} 1 959do_test capi3-11.16 { 960 execsql { 961 SELECT a FROM t2; 962 } 963} {1 2} 964 965# Sanity check on the definition of 'outstanding VM'. This means any VM 966# that has had sqlite3_step() called more recently than sqlite3_finalize() or 967# sqlite3_reset(). So a VM that has just been prepared or reset does not 968# count as an active VM. 969do_test capi3-11.17 { 970 execsql { 971 BEGIN; 972 } 973} {} 974do_test capi3-11.18 { 975 set STMT [sqlite3_prepare $DB "SELECT a FROM t1" -1 TAIL] 976 catchsql { 977 COMMIT; 978 } 979} {0 {}} 980do_test capi3-11.19 { 981 sqlite3_step $STMT 982} {SQLITE_ROW} 983do_test capi3-11.20 { 984 catchsql { 985 BEGIN; 986 COMMIT; 987 } 988} {0 {}} 989do_test capi3-11.20 { 990 sqlite3_reset $STMT 991 catchsql { 992 COMMIT; 993 } 994} {1 {cannot commit - no transaction is active}} 995do_test capi3-11.21 { 996 sqlite3_finalize $STMT 997} {SQLITE_OK} 998 999# The following tests - capi3-12.* - check that its Ok to start a 1000# transaction while other VMs are active, and that its Ok to execute 1001# atomic updates in the same situation 1002# 1003do_test capi3-12.1 { 1004 set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL] 1005 sqlite3_step $STMT 1006} {SQLITE_ROW} 1007do_test capi3-12.2 { 1008 catchsql { 1009 INSERT INTO t1 VALUES(3, NULL); 1010 } 1011} {0 {}} 1012do_test capi3-12.3 { 1013 catchsql { 1014 INSERT INTO t2 VALUES(4); 1015 } 1016} {0 {}} 1017do_test capi3-12.4 { 1018 catchsql { 1019 BEGIN; 1020 INSERT INTO t1 VALUES(4, NULL); 1021 } 1022} {0 {}} 1023do_test capi3-12.5 { 1024 sqlite3_step $STMT 1025} {SQLITE_ROW} 1026do_test capi3-12.5.1 { 1027 sqlite3_step $STMT 1028} {SQLITE_ROW} 1029do_test capi3-12.6 { 1030 sqlite3_step $STMT 1031} {SQLITE_DONE} 1032do_test capi3-12.7 { 1033 sqlite3_finalize $STMT 1034} {SQLITE_OK} 1035do_test capi3-12.8 { 1036 execsql { 1037 COMMIT; 1038 SELECT a FROM t1; 1039 } 1040} {1 2 3 4} 1041 1042# Test cases capi3-13.* test the sqlite3_clear_bindings() and 1043# sqlite3_sleep APIs. 1044# 1045if {[llength [info commands sqlite3_clear_bindings]]>0} { 1046 do_test capi3-13.1 { 1047 execsql { 1048 DELETE FROM t1; 1049 } 1050 set STMT [sqlite3_prepare $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL] 1051 sqlite3_step $STMT 1052 } {SQLITE_DONE} 1053 do_test capi3-13.2 { 1054 sqlite3_reset $STMT 1055 sqlite3_bind_text $STMT 1 hello 5 1056 sqlite3_bind_text $STMT 2 world 5 1057 sqlite3_step $STMT 1058 } {SQLITE_DONE} 1059 do_test capi3-13.3 { 1060 sqlite3_reset $STMT 1061 sqlite3_clear_bindings $STMT 1062 sqlite3_step $STMT 1063 } {SQLITE_DONE} 1064 do_test capi3-13-4 { 1065 sqlite3_finalize $STMT 1066 execsql { 1067 SELECT * FROM t1; 1068 } 1069 } {{} {} hello world {} {}} 1070} 1071if {[llength [info commands sqlite3_sleep]]>0} { 1072 do_test capi3-13-5 { 1073 set ms [sqlite3_sleep 80] 1074 expr {$ms==80 || $ms==1000} 1075 } {1} 1076} 1077 1078# Ticket #1219: Make sure binding APIs can handle a NULL pointer. 1079# 1080if {[clang_sanitize_address]==0} { 1081 do_test capi3-14.1-misuse { 1082 set rc [catch {sqlite3_bind_text 0 1 hello 5} msg] 1083 lappend rc $msg 1084 } {1 SQLITE_MISUSE} 1085} 1086 1087# Ticket #1650: Honor the nBytes parameter to sqlite3_prepare. 1088# 1089do_test capi3-15.1 { 1090 set sql {SELECT * FROM t2} 1091 set nbytes [string length $sql] 1092 append sql { WHERE a==1} 1093 set STMT [sqlite3_prepare $DB $sql $nbytes TAIL] 1094 sqlite3_step $STMT 1095 sqlite3_column_int $STMT 0 1096} {1} 1097do_test capi3-15.2 { 1098 sqlite3_step $STMT 1099 sqlite3_column_int $STMT 0 1100} {2} 1101do_test capi3-15.3 { 1102 sqlite3_finalize $STMT 1103} {SQLITE_OK} 1104do_test capi3-15.4 { 1105 # 123456789 1234567 1106 set sql {SELECT 1234567890} 1107 set STMT [sqlite3_prepare $DB $sql 8 TAIL] 1108 sqlite3_step $STMT 1109 set v1 [sqlite3_column_int $STMT 0] 1110 sqlite3_finalize $STMT 1111 set v1 1112} {1} 1113do_test capi3-15.5 { 1114 # 123456789 1234567 1115 set sql {SELECT 1234567890} 1116 set STMT [sqlite3_prepare $DB $sql 9 TAIL] 1117 sqlite3_step $STMT 1118 set v1 [sqlite3_column_int $STMT 0] 1119 sqlite3_finalize $STMT 1120 set v1 1121} {12} 1122do_test capi3-15.6 { 1123 # 123456789 1234567 1124 set sql {SELECT 1234567890} 1125 set STMT [sqlite3_prepare $DB $sql 12 TAIL] 1126 sqlite3_step $STMT 1127 set v1 [sqlite3_column_int $STMT 0] 1128 sqlite3_finalize $STMT 1129 set v1 1130} {12345} 1131do_test capi3-15.7 { 1132 # 123456789 1234567 1133 set sql {SELECT 12.34567890} 1134 set STMT [sqlite3_prepare $DB $sql 12 TAIL] 1135 sqlite3_step $STMT 1136 set v1 [sqlite3_column_double $STMT 0] 1137 sqlite3_finalize $STMT 1138 set v1 1139} {12.34} 1140do_test capi3-15.8 { 1141 # 123456789 1234567 1142 set sql {SELECT 12.34567890} 1143 set STMT [sqlite3_prepare $DB $sql 14 TAIL] 1144 sqlite3_step $STMT 1145 set v1 [sqlite3_column_double $STMT 0] 1146 sqlite3_finalize $STMT 1147 set v1 1148} {12.3456} 1149 1150# Make sure code is always generated even if an IF EXISTS or 1151# IF NOT EXISTS clause is present that the table does not or 1152# does exists. That way we will always have a prepared statement 1153# to expire when the schema changes. 1154# 1155do_test capi3-16.1 { 1156 set sql {DROP TABLE IF EXISTS t3} 1157 set STMT [sqlite3_prepare $DB $sql -1 TAIL] 1158 sqlite3_finalize $STMT 1159 expr {$STMT!=""} 1160} {1} 1161do_test capi3-16.2 { 1162 set sql {CREATE TABLE IF NOT EXISTS t1(x,y)} 1163 set STMT [sqlite3_prepare $DB $sql -1 TAIL] 1164 sqlite3_finalize $STMT 1165 expr {$STMT!=""} 1166} {1} 1167 1168# But still we do not generate code if there is no SQL 1169# 1170do_test capi3-16.3 { 1171 set STMT [sqlite3_prepare $DB {} -1 TAIL] 1172 sqlite3_finalize $STMT 1173 expr {$STMT==""} 1174} {1} 1175do_test capi3-16.4 { 1176 set STMT [sqlite3_prepare $DB {;} -1 TAIL] 1177 sqlite3_finalize $STMT 1178 expr {$STMT==""} 1179} {1} 1180 1181# Ticket #2426: Misuse of sqlite3_column_* by calling it after 1182# a sqlite3_reset should be harmless. 1183# 1184do_test capi3-17.1 { 1185 set STMT [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL] 1186 sqlite3_step $STMT 1187 sqlite3_column_int $STMT 0 1188} {1} 1189do_test capi3-17.2 { 1190 sqlite3_reset $STMT 1191 sqlite3_column_int $STMT 0 1192} {0} 1193do_test capi3-17.3 { 1194 sqlite3_finalize $STMT 1195} {SQLITE_OK} 1196 1197# Verify that sqlite3_step() fails with an SQLITE_SCHEMA error 1198# when the statement is prepared with sqlite3_prepare() (not 1199# sqlite3_prepare_v2()) and the schema has changed. 1200# 1201do_test capi3-18.1 { 1202 set STMT [sqlite3_prepare db {SELECT * FROM t2} -1 TAIL] 1203 sqlite3 db2 test.db 1204 db2 eval {CREATE TABLE t3(x)} 1205 db2 close 1206 sqlite3_step $STMT 1207} {SQLITE_ERROR} 1208do_test capi3-18.2 { 1209 sqlite3_reset $STMT 1210 sqlite3_errcode db 1211} {SQLITE_SCHEMA} 1212do_test capi3-18.3 { 1213 sqlite3_errmsg db 1214} {database schema has changed} 1215# The error persist on retry when sqlite3_prepare() has been used. 1216do_test capi3-18.4 { 1217 sqlite3_step $STMT 1218} {SQLITE_ERROR} 1219do_test capi3-18.5 { 1220 sqlite3_reset $STMT 1221 sqlite3_errcode db 1222} {SQLITE_SCHEMA} 1223do_test capi3-18.6 { 1224 sqlite3_errmsg db 1225} {database schema has changed} 1226sqlite3_finalize $STMT 1227 1228# Ticket #3134. Prepare a statement with an nBytes parameter of 0. 1229# Make sure this works correctly and does not reference memory out of 1230# range. 1231# 1232do_test capi3-19.1 { 1233 sqlite3_prepare_tkt3134 db 1234} {} 1235 1236# Test that calling sqlite3_column_blob() on a TEXT value does not change 1237# the return type of subsequent calls to sqlite3_column_type(). 1238# 1239do_execsql_test 20.1 { 1240 CREATE TABLE t4(x); 1241 INSERT INTO t4 VALUES('abcdefghij'); 1242} 1243do_test 20.2 { 1244 set stmt [sqlite3_prepare db "SELECT * FROM t4" -1 dummy] 1245 sqlite3_step $stmt 1246} {SQLITE_ROW} 1247do_test 20.3 { sqlite3_column_type $stmt 0 } {TEXT} 1248do_test 20.4 { sqlite3_column_blob $stmt 0 } {abcdefghij} 1249do_test 20.5 { sqlite3_column_type $stmt 0 } {TEXT} 1250do_test 20.6 { sqlite3_finalize $stmt } SQLITE_OK 1251 1252 1253# Tests of the interface when no VFS is registered. 1254# 1255if {![info exists tester_do_binarylog]} { 1256 db close 1257 vfs_unregister_all 1258 do_test capi3-20.1 { 1259 sqlite3_sleep 100 1260 } {0} 1261 vfs_reregister_all 1262} 1263 1264finish_test 1265