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