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