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# 2018-01-09: If a column is the last token if a string, the column name 653# was not being set correctly, due to changes in check-in 654# https://sqlite.org/src/info/0fdf97efe5df7455 655# 656# This problem was detected by the community during beta-testing. 657# 658do_test capi3-5.34 { 659 set STMT [sqlite3_prepare $DB {SELECT :a, :b} -1 TAIL] 660 sqlite3_column_count $STMT 661} 2 662check_header $STMT capi-5.35 {:a :b} {{} {}} 663sqlite3_finalize $STMT 664 665set ::ENC [execsql {pragma encoding}] 666db close 667 668do_test capi3-6.0 { 669 sqlite3 db test.db 670 set DB [sqlite3_connection_pointer db] 671 if {[sqlite3 -has-codec]==0} { sqlite3_key $DB xyzzy } 672 set sql {SELECT a FROM t1 order by rowid} 673 set STMT [sqlite3_prepare $DB $sql -1 TAIL] 674 expr 0 675} {0} 676do_test capi3-6.1 { 677 db cache flush 678 sqlite3_close $DB 679} {SQLITE_BUSY} 680 681# 6.2 and 6.3 used to return SQLITE_ERROR and SQLITE_SCHEMA, respectively. 682# But since attempting to close a connection no longer resets the internal 683# schema and expires all statements, this is no longer the case. 684do_test capi3-6.2 { 685 sqlite3_step $STMT 686} {SQLITE_ROW} 687#check_data $STMT capi3-6.3 {INTEGER} {1} {1.0} {1} 688do_test capi3-6.3 { 689 sqlite3_finalize $STMT 690} {SQLITE_OK} 691 692if {[clang_sanitize_address]==0} { 693 do_test capi3-6.4-misuse { 694 db cache flush 695 sqlite3_close $DB 696 } {SQLITE_OK} 697} 698db close 699 700# This procedure sets the value of the file-format in file 'test.db' 701# to $newval. Also, the schema cookie is incremented. 702# 703proc set_file_format {newval} { 704 hexio_write test.db 44 [hexio_render_int32 $newval] 705 set schemacookie [hexio_get_int [hexio_read test.db 40 4]] 706 incr schemacookie 707 hexio_write test.db 40 [hexio_render_int32 $schemacookie] 708 return {} 709} 710 711# This procedure returns the value of the file-format in file 'test.db'. 712# 713proc get_file_format {{fname test.db}} { 714 return [hexio_get_int [hexio_read $fname 44 4]] 715} 716 717if {![sqlite3 -has-codec]} { 718 # Test what happens when the library encounters a newer file format. 719 do_test capi3-7.1 { 720 set_file_format 5 721 } {} 722 do_test capi3-7.2 { 723 catch { sqlite3 db test.db } 724 catchsql { 725 SELECT * FROM sqlite_master; 726 } 727 } {1 {unsupported file format}} 728 db close 729} 730 731if {![sqlite3 -has-codec]} { 732 # Now test that the library correctly handles bogus entries in the 733 # sqlite_master table (schema corruption). 734 do_test capi3-8.1 { 735 forcedelete test.db test.db-journal 736 sqlite3 db test.db 737 execsql { 738 CREATE TABLE t1(a); 739 } 740 db close 741 } {} 742 do_test capi3-8.2 { 743 sqlite3 db test.db 744 execsql { 745 PRAGMA writable_schema=ON; 746 INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL); 747 } 748 db close 749 } {} 750 do_test capi3-8.3 { 751 catch { sqlite3 db test.db } 752 catchsql { 753 SELECT * FROM sqlite_master; 754 } 755 } {1 {malformed database schema (?)}} 756 do_test capi3-8.4 { 757 # Build a 5-field row record. The first field is a string 'table', and 758 # subsequent fields are all NULL. 759 db close 760 forcedelete test.db test.db-journal 761 sqlite3 db test.db 762 execsql { 763 CREATE TABLE t1(a); 764 PRAGMA writable_schema=ON; 765 INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL); 766 } 767 db close 768 } {}; 769 do_test capi3-8.5 { 770 catch { sqlite3 db test.db } 771 catchsql { 772 SELECT * FROM sqlite_master; 773 } 774 } {1 {malformed database schema (?)}} 775 db close 776} 777forcedelete test.db 778forcedelete test.db-journal 779 780 781# Test the english language string equivalents for sqlite error codes 782set code2english [list \ 783SQLITE_OK {not an error} \ 784SQLITE_ERROR {SQL logic error} \ 785SQLITE_PERM {access permission denied} \ 786SQLITE_ABORT {query aborted} \ 787SQLITE_BUSY {database is locked} \ 788SQLITE_LOCKED {database table is locked} \ 789SQLITE_NOMEM {out of memory} \ 790SQLITE_READONLY {attempt to write a readonly database} \ 791SQLITE_INTERRUPT {interrupted} \ 792SQLITE_IOERR {disk I/O error} \ 793SQLITE_CORRUPT {database disk image is malformed} \ 794SQLITE_FULL {database or disk is full} \ 795SQLITE_CANTOPEN {unable to open database file} \ 796SQLITE_SCHEMA {database schema has changed} \ 797SQLITE_CONSTRAINT {constraint failed} \ 798SQLITE_MISMATCH {datatype mismatch} \ 799SQLITE_MISUSE {bad parameter or other API misuse} \ 800SQLITE_AUTH {authorization denied} \ 801SQLITE_RANGE {column index out of range} \ 802SQLITE_NOTADB {file is not a database} \ 803unknownerror {unknown error} \ 804] 805 806set test_number 1 807foreach {code english} $code2english { 808 do_test capi3-9.$test_number "sqlite3_test_errstr $code" $english 809 incr test_number 810} 811 812# Test the error message when a "real" out of memory occurs. 813if { [permutation] != "nofaultsim" } { 814ifcapable memdebug { 815 do_test capi3-10-1 { 816 sqlite3 db test.db 817 set DB [sqlite3_connection_pointer db] 818 sqlite3_memdebug_fail 1 819 catchsql { 820 select * from sqlite_master; 821 } 822 } {1 {out of memory}} 823 do_test capi3-10-2 { 824 sqlite3_errmsg $::DB 825 } {out of memory} 826 ifcapable {utf16} { 827 do_test capi3-10-3 { 828 utf8 [sqlite3_errmsg16 $::DB] 829 } {out of memory} 830 } 831 db close 832 sqlite3_memdebug_fail -1 833 do_test capi3-10-4 { 834 sqlite3 db test.db 835 set DB [sqlite3_connection_pointer db] 836 sqlite3_memdebug_fail 1 837 catchsql { 838 select * from sqlite_master where rowid>5; 839 } 840 } {1 {out of memory}} 841 do_test capi3-10-5 { 842 sqlite3_errmsg $::DB 843 } {out of memory} 844 ifcapable {utf16} { 845 do_test capi3-10-6 { 846 utf8 [sqlite3_errmsg16 $::DB] 847 } {out of memory} 848 } 849 db close 850 sqlite3_memdebug_fail -1 851} 852} 853 854# The following tests - capi3-11.* - test that a COMMIT or ROLLBACK 855# statement issued while there are still outstanding VMs that are part of 856# the transaction fails. 857sqlite3 db test.db 858set DB [sqlite3_connection_pointer db] 859sqlite_register_test_function $DB func 860do_test capi3-11.1 { 861 execsql { 862 BEGIN; 863 CREATE TABLE t1(a, b); 864 INSERT INTO t1 VALUES(1, 'int'); 865 INSERT INTO t1 VALUES(2, 'notatype'); 866 } 867} {} 868do_test capi3-11.1.1 { 869 sqlite3_get_autocommit $DB 870} 0 871do_test capi3-11.2 { 872 set STMT [sqlite3_prepare $DB "SELECT func(b, a) FROM t1" -1 TAIL] 873 sqlite3_step $STMT 874} {SQLITE_ROW} 875 876# As of 3.6.5 a COMMIT is OK during while a query is still running - 877# as long as it is a read-only query and not an incremental BLOB write. 878# 879do_test capi3-11.3.1 { 880 catchsql { 881 COMMIT; 882 } 883} {0 {}} 884do_test capi3-11.3.2 { 885 sqlite3_extended_errcode $DB 886} {SQLITE_OK} 887do_test capi3-11.3.3 { 888 sqlite3_get_autocommit $DB 889} 1 890do_test capi3-11.3.4 { 891 db eval {PRAGMA lock_status} 892} {main shared temp closed} 893 894do_test capi3-11.4 { 895 sqlite3_step $STMT 896} {SQLITE_ERROR} 897do_test capi3-11.5 { 898 sqlite3_finalize $STMT 899} {SQLITE_ERROR} 900do_test capi3-11.6 { 901 catchsql { 902 SELECT * FROM t1; 903 } 904} {0 {1 int 2 notatype}} 905do_test capi3-11.7 { 906 sqlite3_get_autocommit $DB 907} 1 908do_test capi3-11.8 { 909 execsql { 910 CREATE TABLE t2(a); 911 INSERT INTO t2 VALUES(1); 912 INSERT INTO t2 VALUES(2); 913 BEGIN; 914 INSERT INTO t2 VALUES(3); 915 } 916} {} 917do_test capi3-11.8.1 { 918 sqlite3_get_autocommit $DB 919} 0 920do_test capi3-11.9 { 921 set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL] 922 sqlite3_step $STMT 923} {SQLITE_ROW} 924do_test capi3-11.9.1 { 925 sqlite3_get_autocommit $DB 926} 0 927do_test capi3-11.9.2 { 928 catchsql { 929 ROLLBACK; 930 } 931} {0 {}} 932do_test capi3-11.9.3 { 933 sqlite3_get_autocommit $DB 934} 1 935do_test capi3-11.10 { 936 sqlite3_step $STMT 937} {SQLITE_ROW} 938do_test capi3-11.11 { 939 sqlite3_step $STMT 940} {SQLITE_DONE} 941ifcapable !autoreset { 942 do_test capi3-11.12armor { 943 sqlite3_step $STMT 944 sqlite3_step $STMT 945 } {SQLITE_MISUSE} 946} else { 947 do_test capi3-11.12 { 948 sqlite3_step $STMT 949 sqlite3_step $STMT 950 } {SQLITE_ROW} 951} 952do_test capi3-11.13 { 953 sqlite3_finalize $STMT 954} {SQLITE_OK} 955do_test capi3-11.14 { 956 execsql { 957 SELECT a FROM t2; 958 } 959} {1 2} 960do_test capi3-11.14.1 { 961 sqlite3_get_autocommit $DB 962} 1 963do_test capi3-11.15 { 964 catchsql { 965 ROLLBACK; 966 } 967} {1 {cannot rollback - no transaction is active}} 968do_test capi3-11.15.1 { 969 sqlite3_get_autocommit $DB 970} 1 971do_test capi3-11.16 { 972 execsql { 973 SELECT a FROM t2; 974 } 975} {1 2} 976 977# Sanity check on the definition of 'outstanding VM'. This means any VM 978# that has had sqlite3_step() called more recently than sqlite3_finalize() or 979# sqlite3_reset(). So a VM that has just been prepared or reset does not 980# count as an active VM. 981do_test capi3-11.17 { 982 execsql { 983 BEGIN; 984 } 985} {} 986do_test capi3-11.18 { 987 set STMT [sqlite3_prepare $DB "SELECT a FROM t1" -1 TAIL] 988 catchsql { 989 COMMIT; 990 } 991} {0 {}} 992do_test capi3-11.19 { 993 sqlite3_step $STMT 994} {SQLITE_ROW} 995do_test capi3-11.20 { 996 catchsql { 997 BEGIN; 998 COMMIT; 999 } 1000} {0 {}} 1001do_test capi3-11.20 { 1002 sqlite3_reset $STMT 1003 catchsql { 1004 COMMIT; 1005 } 1006} {1 {cannot commit - no transaction is active}} 1007do_test capi3-11.21 { 1008 sqlite3_finalize $STMT 1009} {SQLITE_OK} 1010 1011# The following tests - capi3-12.* - check that its Ok to start a 1012# transaction while other VMs are active, and that its Ok to execute 1013# atomic updates in the same situation 1014# 1015do_test capi3-12.1 { 1016 set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL] 1017 sqlite3_step $STMT 1018} {SQLITE_ROW} 1019do_test capi3-12.2 { 1020 catchsql { 1021 INSERT INTO t1 VALUES(3, NULL); 1022 } 1023} {0 {}} 1024do_test capi3-12.3 { 1025 catchsql { 1026 INSERT INTO t2 VALUES(4); 1027 } 1028} {0 {}} 1029do_test capi3-12.4 { 1030 catchsql { 1031 BEGIN; 1032 INSERT INTO t1 VALUES(4, NULL); 1033 } 1034} {0 {}} 1035do_test capi3-12.5 { 1036 sqlite3_step $STMT 1037} {SQLITE_ROW} 1038do_test capi3-12.5.1 { 1039 sqlite3_step $STMT 1040} {SQLITE_ROW} 1041do_test capi3-12.6 { 1042 sqlite3_step $STMT 1043} {SQLITE_DONE} 1044do_test capi3-12.7 { 1045 sqlite3_finalize $STMT 1046} {SQLITE_OK} 1047do_test capi3-12.8 { 1048 execsql { 1049 COMMIT; 1050 SELECT a FROM t1; 1051 } 1052} {1 2 3 4} 1053 1054# Test cases capi3-13.* test the sqlite3_clear_bindings() and 1055# sqlite3_sleep APIs. 1056# 1057if {[llength [info commands sqlite3_clear_bindings]]>0} { 1058 do_test capi3-13.1 { 1059 execsql { 1060 DELETE FROM t1; 1061 } 1062 set STMT [sqlite3_prepare $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL] 1063 sqlite3_step $STMT 1064 } {SQLITE_DONE} 1065 do_test capi3-13.2 { 1066 sqlite3_reset $STMT 1067 sqlite3_bind_text $STMT 1 hello 5 1068 sqlite3_bind_text $STMT 2 world 5 1069 sqlite3_step $STMT 1070 } {SQLITE_DONE} 1071 do_test capi3-13.3 { 1072 sqlite3_reset $STMT 1073 sqlite3_clear_bindings $STMT 1074 sqlite3_step $STMT 1075 } {SQLITE_DONE} 1076 do_test capi3-13-4 { 1077 sqlite3_finalize $STMT 1078 execsql { 1079 SELECT * FROM t1; 1080 } 1081 } {{} {} hello world {} {}} 1082} 1083if {[llength [info commands sqlite3_sleep]]>0} { 1084 do_test capi3-13-5 { 1085 set ms [sqlite3_sleep 80] 1086 expr {$ms==80 || $ms==1000} 1087 } {1} 1088} 1089 1090# Ticket #1219: Make sure binding APIs can handle a NULL pointer. 1091# 1092if {[clang_sanitize_address]==0} { 1093 do_test capi3-14.1-misuse { 1094 set rc [catch {sqlite3_bind_text 0 1 hello 5} msg] 1095 lappend rc $msg 1096 } {1 SQLITE_MISUSE} 1097} 1098 1099# Ticket #1650: Honor the nBytes parameter to sqlite3_prepare. 1100# 1101do_test capi3-15.1 { 1102 set sql {SELECT * FROM t2} 1103 set nbytes [string length $sql] 1104 append sql { WHERE a==1} 1105 set STMT [sqlite3_prepare $DB $sql $nbytes TAIL] 1106 sqlite3_step $STMT 1107 sqlite3_column_int $STMT 0 1108} {1} 1109do_test capi3-15.2 { 1110 sqlite3_step $STMT 1111 sqlite3_column_int $STMT 0 1112} {2} 1113do_test capi3-15.3 { 1114 sqlite3_finalize $STMT 1115} {SQLITE_OK} 1116do_test capi3-15.4 { 1117 # 123456789 1234567 1118 set sql {SELECT 1234567890} 1119 set STMT [sqlite3_prepare $DB $sql 8 TAIL] 1120 sqlite3_step $STMT 1121 set v1 [sqlite3_column_int $STMT 0] 1122 sqlite3_finalize $STMT 1123 set v1 1124} {1} 1125do_test capi3-15.5 { 1126 # 123456789 1234567 1127 set sql {SELECT 1234567890} 1128 set STMT [sqlite3_prepare $DB $sql 9 TAIL] 1129 sqlite3_step $STMT 1130 set v1 [sqlite3_column_int $STMT 0] 1131 sqlite3_finalize $STMT 1132 set v1 1133} {12} 1134do_test capi3-15.6 { 1135 # 123456789 1234567 1136 set sql {SELECT 1234567890} 1137 set STMT [sqlite3_prepare $DB $sql 12 TAIL] 1138 sqlite3_step $STMT 1139 set v1 [sqlite3_column_int $STMT 0] 1140 sqlite3_finalize $STMT 1141 set v1 1142} {12345} 1143do_test capi3-15.7 { 1144 # 123456789 1234567 1145 set sql {SELECT 12.34567890} 1146 set STMT [sqlite3_prepare $DB $sql 12 TAIL] 1147 sqlite3_step $STMT 1148 set v1 [sqlite3_column_double $STMT 0] 1149 sqlite3_finalize $STMT 1150 set v1 1151} {12.34} 1152do_test capi3-15.8 { 1153 # 123456789 1234567 1154 set sql {SELECT 12.34567890} 1155 set STMT [sqlite3_prepare $DB $sql 14 TAIL] 1156 sqlite3_step $STMT 1157 set v1 [sqlite3_column_double $STMT 0] 1158 sqlite3_finalize $STMT 1159 set v1 1160} {12.3456} 1161 1162# Make sure code is always generated even if an IF EXISTS or 1163# IF NOT EXISTS clause is present that the table does not or 1164# does exists. That way we will always have a prepared statement 1165# to expire when the schema changes. 1166# 1167do_test capi3-16.1 { 1168 set sql {DROP TABLE IF EXISTS t3} 1169 set STMT [sqlite3_prepare $DB $sql -1 TAIL] 1170 sqlite3_finalize $STMT 1171 expr {$STMT!=""} 1172} {1} 1173do_test capi3-16.2 { 1174 set sql {CREATE TABLE IF NOT EXISTS t1(x,y)} 1175 set STMT [sqlite3_prepare $DB $sql -1 TAIL] 1176 sqlite3_finalize $STMT 1177 expr {$STMT!=""} 1178} {1} 1179 1180# But still we do not generate code if there is no SQL 1181# 1182do_test capi3-16.3 { 1183 set STMT [sqlite3_prepare $DB {} -1 TAIL] 1184 sqlite3_finalize $STMT 1185 expr {$STMT==""} 1186} {1} 1187do_test capi3-16.4 { 1188 set STMT [sqlite3_prepare $DB {;} -1 TAIL] 1189 sqlite3_finalize $STMT 1190 expr {$STMT==""} 1191} {1} 1192 1193# Ticket #2426: Misuse of sqlite3_column_* by calling it after 1194# a sqlite3_reset should be harmless. 1195# 1196do_test capi3-17.1 { 1197 set STMT [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL] 1198 sqlite3_step $STMT 1199 sqlite3_column_int $STMT 0 1200} {1} 1201do_test capi3-17.2 { 1202 sqlite3_reset $STMT 1203 sqlite3_column_int $STMT 0 1204} {0} 1205do_test capi3-17.3 { 1206 sqlite3_finalize $STMT 1207} {SQLITE_OK} 1208 1209# Verify that sqlite3_step() fails with an SQLITE_SCHEMA error 1210# when the statement is prepared with sqlite3_prepare() (not 1211# sqlite3_prepare_v2()) and the schema has changed. 1212# 1213do_test capi3-18.1 { 1214 set STMT [sqlite3_prepare db {SELECT * FROM t2} -1 TAIL] 1215 sqlite3 db2 test.db 1216 db2 eval {CREATE TABLE t3(x)} 1217 db2 close 1218 sqlite3_step $STMT 1219} {SQLITE_ERROR} 1220do_test capi3-18.2 { 1221 sqlite3_reset $STMT 1222 sqlite3_errcode db 1223} {SQLITE_SCHEMA} 1224do_test capi3-18.3 { 1225 sqlite3_errmsg db 1226} {database schema has changed} 1227# The error persist on retry when sqlite3_prepare() has been used. 1228do_test capi3-18.4 { 1229 sqlite3_step $STMT 1230} {SQLITE_ERROR} 1231do_test capi3-18.5 { 1232 sqlite3_reset $STMT 1233 sqlite3_errcode db 1234} {SQLITE_SCHEMA} 1235do_test capi3-18.6 { 1236 sqlite3_errmsg db 1237} {database schema has changed} 1238sqlite3_finalize $STMT 1239 1240# Ticket #3134. Prepare a statement with an nBytes parameter of 0. 1241# Make sure this works correctly and does not reference memory out of 1242# range. 1243# 1244do_test capi3-19.1 { 1245 sqlite3_prepare_tkt3134 db 1246} {} 1247 1248# Test that calling sqlite3_column_blob() on a TEXT value does not change 1249# the return type of subsequent calls to sqlite3_column_type(). 1250# 1251do_execsql_test 20.1 { 1252 CREATE TABLE t4(x); 1253 INSERT INTO t4 VALUES('abcdefghij'); 1254} 1255do_test 20.2 { 1256 set stmt [sqlite3_prepare db "SELECT * FROM t4" -1 dummy] 1257 sqlite3_step $stmt 1258} {SQLITE_ROW} 1259do_test 20.3 { sqlite3_column_type $stmt 0 } {TEXT} 1260do_test 20.4 { sqlite3_column_blob $stmt 0 } {abcdefghij} 1261do_test 20.5 { sqlite3_column_type $stmt 0 } {TEXT} 1262do_test 20.6 { sqlite3_finalize $stmt } SQLITE_OK 1263 1264 1265# Tests of the interface when no VFS is registered. 1266# 1267if {![info exists tester_do_binarylog]} { 1268 db close 1269 vfs_unregister_all 1270 do_test capi3-20.1 { 1271 sqlite3_sleep 100 1272 } {0} 1273 vfs_reregister_all 1274} 1275 1276finish_test 1277