AryaWu/sqlite
0
1# 2006 November 082#3# The author disclaims copyright to this source code. In place of4# 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. 12#13# This is a copy of the capi3.test file that has been adapted to14# test the new sqlite3_prepare_v2 interface.15#16# $Id: capi3c.test,v 1.23 2009/07/22 07:27:57 danielk1977 Exp $17#18 19set testdir [file dirname $argv0]20source $testdir/tester.tcl21set testprefix capi3c22 23# Do not use a codec for tests in this file, as the database file is24# manipulated directly using tcl scripts (using the [hexio_write] command).25#26do_not_use_codec27 28# Return the UTF-16 representation of the supplied UTF-8 string $str.29# If $nt is true, append two 0x00 bytes as a nul terminator.30proc utf16 {str {nt 1}} {31 set r [encoding convertto unicode $str]32 if {$nt} {33 append r "\x00\x00"34 }35 return $r36}37 38# Return the UTF-8 representation of the supplied UTF-16 string $str. 39proc utf8 {str} {40 # If $str ends in two 0x00 0x00 bytes, knock these off before41 # converting to UTF-8 using TCL.42 binary scan $str \c* vals43 if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {44 set str [binary format \c* [lrange $vals 0 end-2]]45 }46 47 set r [encoding convertfrom unicode $str]48 return $r49}50 51# These tests complement those in capi2.test. They are organized52# as follows:53#54# capi3c-1.*: Test sqlite3_prepare_v2 55# capi3c-2.*: Test sqlite3_prepare16_v2 56# capi3c-3.*: Test sqlite3_open57# capi3c-4.*: Test sqlite3_open1658# capi3c-5.*: Test the various sqlite3_result_* APIs59# capi3c-6.*: Test that sqlite3_close fails if there are outstanding VMs.60#61 62set DB [sqlite3_connection_pointer db]63 64do_test capi3c-1.0 {65 sqlite3_get_autocommit $DB66} 167do_test capi3c-1.1 {68 set STMT [sqlite3_prepare_v2 $DB {SELECT name FROM sqlite_master} -1 TAIL]69 sqlite3_finalize $STMT70 set TAIL71} {}72do_test capi3c-1.2.1 {73 sqlite3_errcode $DB74} {SQLITE_OK}75do_test capi3c-1.2.2 {76 sqlite3_extended_errcode $DB77} {SQLITE_OK}78do_test capi3c-1.3 {79 sqlite3_errmsg $DB80} {not an error}81do_test capi3c-1.4 {82 set sql {SELECT name FROM sqlite_master;SELECT 10}83 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]84 sqlite3_finalize $STMT85 set TAIL86} {SELECT 10}87do_test capi3c-1.5 {88 set sql {SELECT namex FROM sqlite_master}89 catch {90 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]91 }92} {1}93do_test capi3c-1.6.1 {94 sqlite3_errcode $DB95} {SQLITE_ERROR}96do_test capi3c-1.6.2 {97 sqlite3_extended_errcode $DB98} {SQLITE_ERROR}99do_test capi3c-1.7 {100 sqlite3_errmsg $DB101} {no such column: namex}102 103 104ifcapable {utf16} {105 do_test capi3c-2.1 {106 set sql16 [utf16 {SELECT name FROM sqlite_master}]107 set STMT [sqlite3_prepare16_v2 $DB $sql16 -1 ::TAIL]108 sqlite3_finalize $STMT109 utf8 $::TAIL110 } {}111 do_test capi3c-2.2 {112 set sql [utf16 {SELECT name FROM sqlite_master;SELECT 10}]113 set STMT [sqlite3_prepare16_v2 $DB $sql -1 TAIL]114 sqlite3_finalize $STMT115 utf8 $TAIL116 } {SELECT 10}117 do_test capi3c-2.3 {118 set sql [utf16 {SELECT namex FROM sqlite_master}]119 catch {120 set STMT [sqlite3_prepare16_v2 $DB $sql -1 TAIL]121 }122 } {1}123 do_test capi3c-2.4.1 {124 sqlite3_errcode $DB125 } {SQLITE_ERROR}126 do_test capi3c-2.4.2 {127 sqlite3_extended_errcode $DB128 } {SQLITE_ERROR}129 do_test capi3c-2.5 {130 sqlite3_errmsg $DB131 } {no such column: namex}132 133 ifcapable schema_pragmas {134 do_test capi3c-2.6 {135 execsql {CREATE TABLE tablename(x)}136 set sql16 [utf16 {PRAGMA table_info("TableName")}]137 set STMT [sqlite3_prepare16_v2 $DB $sql16 -1 TAIL]138 sqlite3_step $STMT139 } SQLITE_ROW140 do_test capi3c-2.7 {141 sqlite3_step $STMT142 } SQLITE_DONE143 do_test capi3c-2.8 {144 sqlite3_finalize $STMT145 } SQLITE_OK146 }147 148} ;# endif utf16149 150# rename sqlite3_open sqlite3_open_old151# proc sqlite3_open {fname options} {sqlite3_open_new $fname $options}152 153do_test capi3c-3.1 {154 set db2 [sqlite3_open test.db {}]155 sqlite3_errcode $db2156} {SQLITE_OK}157# FIX ME: Should test the db handle works.158do_test capi3c-3.2 {159 sqlite3_close $db2160} {SQLITE_OK}161do_test capi3c-3.3 {162 catch {163 set db2 [sqlite3_open /bogus/path/test.db {}]164 }165 sqlite3_errcode $db2166} {SQLITE_CANTOPEN}167do_test capi3c-3.4 {168 sqlite3_errmsg $db2169} {unable to open database file}170do_test capi3c-3.5 {171 sqlite3_close $db2172} {SQLITE_OK}173if {[clang_sanitize_address]==0} {174 do_test capi3c-3.6.1-misuse {175 sqlite3_close $db2176 } {SQLITE_MISUSE}177 do_test capi3c-3.6.2-misuse {178 sqlite3_errmsg $db2179 } {bad parameter or other API misuse}180 ifcapable {utf16} {181 do_test capi3c-3.6.3-misuse {182 utf8 [sqlite3_errmsg16 $db2]183 } {bad parameter or other API misuse}184 }185}186 187# rename sqlite3_open ""188# rename sqlite3_open_old sqlite3_open189 190ifcapable {utf16} {191do_test capi3c-4.1 {192 set db2 [sqlite3_open16 [utf16 test.db] {}]193 sqlite3_errcode $db2194} {SQLITE_OK}195# FIX ME: Should test the db handle works.196do_test capi3c-4.2 {197 sqlite3_close $db2198} {SQLITE_OK}199do_test capi3c-4.3 {200 catch {201 set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}]202 }203 sqlite3_errcode $db2204} {SQLITE_CANTOPEN}205do_test capi3c-4.4 {206 utf8 [sqlite3_errmsg16 $db2]207} {unable to open database file}208do_test capi3c-4.5 {209 sqlite3_close $db2210} {SQLITE_OK}211} ;# utf16212 213# This proc is used to test the following API calls:214#215# sqlite3_column_count216# sqlite3_column_name217# sqlite3_column_name16218# sqlite3_column_decltype219# sqlite3_column_decltype16220#221# $STMT is a compiled SQL statement. $test is a prefix222# to use for test names within this proc. $names is a list223# of the column names that should be returned by $STMT.224# $decltypes is a list of column declaration types for $STMT.225#226# Example:227#228# set STMT [sqlite3_prepare_v2 "SELECT 1, 2, 2;" -1 DUMMY]229# check_header test1.1 {1 2 3} {"" "" ""}230#231proc check_header {STMT test names decltypes} {232 233 # Use the return value of sqlite3_column_count() to build234 # a list of column indexes. i.e. If sqlite3_column_count235 # is 3, build the list {0 1 2}.236 set ::idxlist [list]237 set ::numcols [sqlite3_column_count $STMT]238 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}239 240 # Column names in UTF-8241 do_test $test.1 {242 set cnamelist [list]243 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]} 244 set cnamelist245 } $names246 247 # Column names in UTF-16248 ifcapable {utf16} {249 do_test $test.2 {250 set cnamelist [list]251 foreach i $idxlist {252 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]253 }254 set cnamelist255 } $names256 }257 258 # Column names in UTF-8259 do_test $test.3 {260 set cnamelist [list]261 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]} 262 set cnamelist263 } $names264 265 # Column names in UTF-16266 ifcapable {utf16} {267 do_test $test.4 {268 set cnamelist [list]269 foreach i $idxlist {270 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]271 }272 set cnamelist273 } $names274 }275 276 # Column names in UTF-8277 do_test $test.5 {278 set cnamelist [list]279 foreach i $idxlist {lappend cnamelist [sqlite3_column_decltype $STMT $i]} 280 set cnamelist281 } $decltypes282 283 # Column declaration types in UTF-16284 ifcapable {utf16} {285 do_test $test.6 {286 set cnamelist [list]287 foreach i $idxlist {288 lappend cnamelist [utf8 [sqlite3_column_decltype16 $STMT $i]]289 }290 set cnamelist291 } $decltypes292 }293 294 295 # Test some out of range conditions:296 ifcapable {utf16} {297 do_test $test.7 {298 list \299 [sqlite3_column_name $STMT -1] \300 [sqlite3_column_name16 $STMT -1] \301 [sqlite3_column_decltype $STMT -1] \302 [sqlite3_column_decltype16 $STMT -1] \303 [sqlite3_column_name $STMT $numcols] \304 [sqlite3_column_name16 $STMT $numcols] \305 [sqlite3_column_decltype $STMT $numcols] \306 [sqlite3_column_decltype16 $STMT $numcols]307 } {{} {} {} {} {} {} {} {}}308 }309} 310 311# This proc is used to test the following API calls:312#313# sqlite3_column_origin_name314# sqlite3_column_origin_name16315# sqlite3_column_table_name316# sqlite3_column_table_name16317# sqlite3_column_database_name318# sqlite3_column_database_name16319#320# $STMT is a compiled SQL statement. $test is a prefix321# to use for test names within this proc. $names is a list322# of the column names that should be returned by $STMT.323# $decltypes is a list of column declaration types for $STMT.324#325# Example:326#327# set STMT [sqlite3_prepare_v2 "SELECT 1, 2, 2;" -1 DUMMY]328# check_header test1.1 {1 2 3} {"" "" ""}329#330proc check_origin_header {STMT test dbs tables cols} {331 # If sqlite3_column_origin_name() and friends are not compiled into332 # this build, this proc is a no-op.333ifcapable columnmetadata {334 335 # Use the return value of sqlite3_column_count() to build336 # a list of column indexes. i.e. If sqlite3_column_count337 # is 3, build the list {0 1 2}.338 set ::idxlist [list]339 set ::numcols [sqlite3_column_count $STMT]340 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}341 342 # Database names in UTF-8343 do_test $test.8 {344 set cnamelist [list]345 foreach i $idxlist {346 lappend cnamelist [sqlite3_column_database_name $STMT $i]347 } 348 set cnamelist349 } $dbs350 351 # Database names in UTF-16352 ifcapable {utf16} {353 do_test $test.9 {354 set cnamelist [list]355 foreach i $idxlist {356 lappend cnamelist [utf8 [sqlite3_column_database_name16 $STMT $i]]357 }358 set cnamelist359 } $dbs360 }361 362 # Table names in UTF-8363 do_test $test.10 {364 set cnamelist [list]365 foreach i $idxlist {366 lappend cnamelist [sqlite3_column_table_name $STMT $i]367 } 368 set cnamelist369 } $tables370 371 # Table names in UTF-16372 ifcapable {utf16} {373 do_test $test.11 {374 set cnamelist [list]375 foreach i $idxlist {376 lappend cnamelist [utf8 [sqlite3_column_table_name16 $STMT $i]]377 }378 set cnamelist379 } $tables380 }381 382 # Origin names in UTF-8383 do_test $test.12 {384 set cnamelist [list]385 foreach i $idxlist {386 lappend cnamelist [sqlite3_column_origin_name $STMT $i]387 } 388 set cnamelist389 } $cols390 391 # Origin declaration types in UTF-16392 ifcapable {utf16} {393 do_test $test.13 {394 set cnamelist [list]395 foreach i $idxlist {396 lappend cnamelist [utf8 [sqlite3_column_origin_name16 $STMT $i]]397 }398 set cnamelist399 } $cols400 }401 }402}403 404# This proc is used to test the following APIs:405#406# sqlite3_data_count407# sqlite3_column_type408# sqlite3_column_int409# sqlite3_column_text410# sqlite3_column_text16411# sqlite3_column_double412#413# $STMT is a compiled SQL statement for which the previous call 414# to sqlite3_step returned SQLITE_ROW. $test is a prefix to use 415# for test names within this proc. $types is a list of the 416# manifest types for the current row. $ints, $doubles and $strings417# are lists of the integer, real and string representations of418# the values in the current row.419#420# Example:421#422# set STMT [sqlite3_prepare_v2 "SELECT 'hello', 1.1, NULL" -1 DUMMY]423# sqlite3_step $STMT424# check_data test1.2 {TEXT REAL NULL} {0 1 0} {0 1.1 0} {hello 1.1 {}}425#426proc check_data {STMT test types ints doubles strings} {427 428 # Use the return value of sqlite3_column_count() to build429 # a list of column indexes. i.e. If sqlite3_column_count430 # is 3, build the list {0 1 2}.431 set ::idxlist [list]432 set numcols [sqlite3_data_count $STMT]433 for {set i 0} {$i < $numcols} {incr i} {lappend ::idxlist $i}434 435# types436do_test $test.1 {437 set types [list]438 foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}439 set types440} $types441 442# Integers443do_test $test.2 {444 set ints [list]445 foreach i $idxlist {lappend ints [sqlite3_column_int64 $STMT $i]}446 set ints447} $ints448 449# bytes450set lens [list]451foreach i $::idxlist {452 lappend lens [string length [lindex $strings $i]]453}454do_test $test.3 {455 set bytes [list]456 set lens [list]457 foreach i $idxlist {458 lappend bytes [sqlite3_column_bytes $STMT $i]459 }460 set bytes461} $lens462 463# bytes16464ifcapable {utf16} {465 set lens [list]466 foreach i $::idxlist {467 lappend lens [expr 2 * [string length [lindex $strings $i]]]468 }469 do_test $test.4 {470 set bytes [list]471 set lens [list]472 foreach i $idxlist {473 lappend bytes [sqlite3_column_bytes16 $STMT $i]474 }475 set bytes476 } $lens477}478 479# Blob480do_test $test.5 {481 set utf8 [list]482 foreach i $idxlist {lappend utf8 [sqlite3_column_blob $STMT $i]}483 set utf8484} $strings485 486# UTF-8487do_test $test.6 {488 set utf8 [list]489 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}490 set utf8491} $strings492 493# Floats494do_test $test.7 {495 set utf8 [list]496 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}497 set utf8498} $doubles499 500# UTF-16501ifcapable {utf16} {502 do_test $test.8 {503 set utf8 [list]504 foreach i $idxlist {lappend utf8 [utf8 [sqlite3_column_text16 $STMT $i]]}505 set utf8506 } $strings507}508 509# Integers510do_test $test.9 {511 set ints [list]512 foreach i $idxlist {lappend ints [sqlite3_column_int $STMT $i]}513 set ints514} $ints515 516# Floats517do_test $test.10 {518 set utf8 [list]519 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}520 set utf8521} $doubles522 523# UTF-8524do_test $test.11 {525 set utf8 [list]526 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}527 set utf8528} $strings529 530# Types531do_test $test.12 {532 set types [list]533 foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}534 set types535} $types536 537# Test that an out of range request returns the equivalent of NULL538do_test $test.13 {539 sqlite3_column_int $STMT -1540} {0}541do_test $test.13 {542 sqlite3_column_text $STMT -1543} {}544 545}546 547ifcapable !floatingpoint {548 finish_test549 return550}551 552do_test capi3c-5.0 {553 execsql {554 CREATE TABLE t1(a VARINT, b BLOB, c VARCHAR(16));555 INSERT INTO t1 VALUES(1, 2, 3);556 INSERT INTO t1 VALUES('one', 'two', NULL);557 INSERT INTO t1 VALUES(1.2, 1.3, 1.4);558 }559 set sql "SELECT * FROM t1"560 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]561 sqlite3_column_count $STMT562} 3563 564check_header $STMT capi3c-5.1 {a b c} {VARINT BLOB VARCHAR(16)}565check_origin_header $STMT capi3c-5.1 {main main main} {t1 t1 t1} {a b c}566do_test capi3c-5.2 {567 sqlite3_step $STMT568} SQLITE_ROW569 570check_header $STMT capi3c-5.3 {a b c} {VARINT BLOB VARCHAR(16)}571check_origin_header $STMT capi3c-5.3 {main main main} {t1 t1 t1} {a b c}572check_data $STMT capi3c-5.4 {INTEGER INTEGER TEXT} {1 2 3} {1.0 2.0 3.0} {1 2 3}573 574do_test capi3c-5.5 {575 sqlite3_step $STMT576} SQLITE_ROW577 578check_header $STMT capi3c-5.6 {a b c} {VARINT BLOB VARCHAR(16)}579check_origin_header $STMT capi3c-5.6 {main main main} {t1 t1 t1} {a b c}580check_data $STMT capi3c-5.7 {TEXT TEXT NULL} {0 0 0} {0.0 0.0 0.0} {one two {}}581 582do_test capi3c-5.8 {583 sqlite3_step $STMT584} SQLITE_ROW585 586check_header $STMT capi3c-5.9 {a b c} {VARINT BLOB VARCHAR(16)}587check_origin_header $STMT capi3c-5.9 {main main main} {t1 t1 t1} {a b c}588check_data $STMT capi3c-5.10 {FLOAT FLOAT TEXT} {1 1 1} {1.2 1.3 1.4} {1.2 1.3 1.4}589 590do_test capi3c-5.11 {591 sqlite3_step $STMT592} SQLITE_DONE593 594do_test capi3c-5.12 {595 sqlite3_finalize $STMT596} SQLITE_OK597 598do_test capi3c-5.20 {599 set sql "SELECT a, sum(b), max(c) FROM t1 GROUP BY a"600 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]601 sqlite3_column_count $STMT602} 3603 604check_header $STMT capi3c-5.21 {a sum(b) max(c)} {VARINT {} {}}605check_origin_header $STMT capi3c-5.22 {main {} {}} {t1 {} {}} {a {} {}}606do_test capi3c-5.23 {607 sqlite3_finalize $STMT608} SQLITE_OK609 610 611set ::ENC [execsql {pragma encoding}]612db close613 614do_test capi3c-6.0 {615 sqlite3 db test.db616 set DB [sqlite3_connection_pointer db]617 if {[sqlite3 -has-codec]==0} { sqlite3_key $DB xyzzy }618 set sql {SELECT a FROM t1 order by rowid}619 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]620 expr 0621} {0}622do_test capi3c-6.1 {623 db cache flush624 sqlite3_close $DB625} {SQLITE_BUSY}626do_test capi3c-6.2 {627 sqlite3_step $STMT628} {SQLITE_ROW}629check_data $STMT capi3c-6.3 {INTEGER} {1} {1.0} {1}630do_test capi3c-6.3 {631 sqlite3_finalize $STMT632} {SQLITE_OK}633if {[clang_sanitize_address]==0} {634 do_test capi3c-6.4 {635 db cache flush636 sqlite3_close $DB637 } {SQLITE_OK}638 do_test capi3c-6.99-misuse {639 db close640 } {}641} else {642 db close643}644 645# This procedure sets the value of the file-format in file 'test.db'646# to $newval. Also, the schema cookie is incremented.647# 648proc set_file_format {newval} {649 hexio_write test.db 44 [hexio_render_int32 $newval]650 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]651 incr schemacookie652 hexio_write test.db 40 [hexio_render_int32 $schemacookie]653 return {}654}655 656# This procedure returns the value of the file-format in file 'test.db'.657# 658proc get_file_format {{fname test.db}} {659 return [hexio_get_int [hexio_read $fname 44 4]]660}661 662if {![sqlite3 -has-codec]} {663 # Test what happens when the library encounters a newer file format.664 do_test capi3c-7.1 {665 set_file_format 5666 } {}667 do_test capi3c-7.2 {668 catch { sqlite3 db test.db }669 catchsql {670 SELECT * FROM sqlite_master;671 }672 } {1 {unsupported file format}}673 db close674}675 676if {![sqlite3 -has-codec]} {677 # Now test that the library correctly handles bogus entries in the678 # sqlite_master table (schema corruption).679 do_test capi3c-8.1 {680 forcedelete test.db test.db-journal681 sqlite3 db test.db682 execsql {683 CREATE TABLE t1(a);684 }685 db close686 } {}687 do_test capi3c-8.2 {688 sqlite3 db test.db689 sqlite3_db_config db DEFENSIVE 0690 execsql {691 PRAGMA writable_schema=ON;692 INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL);693 }694 db close695 } {}696 do_test capi3c-8.3 {697 catch { sqlite3 db test.db }698 catchsql {699 SELECT * FROM sqlite_master;700 }701 } {1 {malformed database schema (?)}}702 do_test capi3c-8.4 {703 # Build a 5-field row record. The first field is a string 'table', and704 # subsequent fields are all NULL.705 db close706 forcedelete test.db test.db-journal707 sqlite3 db test.db708 sqlite3_db_config db DEFENSIVE 0709 execsql {710 CREATE TABLE t1(a);711 PRAGMA writable_schema=ON;712 INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL);713 }714 db close715 } {};716 do_test capi3c-8.5 {717 catch { sqlite3 db test.db }718 catchsql {719 SELECT * FROM sqlite_master;720 }721 } {1 {malformed database schema (?)}}722 db close723}724forcedelete test.db725forcedelete test.db-journal726 727 728# Test the english language string equivalents for sqlite error codes729set code2english [list \730SQLITE_OK {not an error} \731SQLITE_ERROR {SQL logic error} \732SQLITE_PERM {access permission denied} \733SQLITE_ABORT {query aborted} \734SQLITE_BUSY {database is locked} \735SQLITE_LOCKED {database table is locked} \736SQLITE_NOMEM {out of memory} \737SQLITE_READONLY {attempt to write a readonly database} \738SQLITE_INTERRUPT {interrupted} \739SQLITE_IOERR {disk I/O error} \740SQLITE_CORRUPT {database disk image is malformed} \741SQLITE_FULL {database or disk is full} \742SQLITE_CANTOPEN {unable to open database file} \743SQLITE_EMPTY {unknown error} \744SQLITE_SCHEMA {database schema has changed} \745SQLITE_CONSTRAINT {constraint failed} \746SQLITE_MISMATCH {datatype mismatch} \747SQLITE_MISUSE {bad parameter or other API misuse} \748SQLITE_AUTH {authorization denied} \749SQLITE_RANGE {column index out of range} \750SQLITE_NOTADB {file is not a database} \751unknownerror {unknown error} \752]753 754set test_number 1755foreach {code english} $code2english {756 do_test capi3c-9.$test_number "sqlite3_test_errstr $code" $english757 incr test_number758}759 760# Test the error message when a "real" out of memory occurs.761if { [permutation] != "nofaultsim" } {762 do_test capi3c-10-1 {763 sqlite3 db test.db764 set DB [sqlite3_connection_pointer db]765 sqlite3_memdebug_fail 0766 catchsql {767 select * from sqlite_master;768 }769 } {1 {out of memory}}770 do_test capi3c-10-2 {771 sqlite3_errmsg $::DB772 } {out of memory}773 ifcapable {utf16} {774 do_test capi3c-10-3 {775 utf8 [sqlite3_errmsg16 $::DB]776 } {out of memory}777 }778 db close779 sqlite3_memdebug_fail -1780}781 782# The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK783# statement issued while there are still outstanding VMs that are part of784# the transaction fails.785sqlite3 db test.db786set DB [sqlite3_connection_pointer db]787sqlite_register_test_function $DB func788do_test capi3c-11.1 {789 execsql {790 BEGIN;791 CREATE TABLE t1(a, b);792 INSERT INTO t1 VALUES(1, 'int');793 INSERT INTO t1 VALUES(2, 'notatype');794 }795} {}796do_test capi3c-11.1.1 {797 sqlite3_get_autocommit $DB798} 0799do_test capi3c-11.2 {800 set STMT [sqlite3_prepare_v2 $DB "SELECT func(b, a) FROM t1" -1 TAIL]801 sqlite3_step $STMT802} {SQLITE_ROW}803 804# As of 3.6.5 a COMMIT is OK during while a query is still running -805# as long as it is a read-only query and not an incremental BLOB write.806#807do_test capi3-11.3.1 {808 catchsql {809 COMMIT;810 }811} {0 {}}812do_test capi3-11.3.2 {813 sqlite3_extended_errcode $DB814} {SQLITE_OK}815do_test capi3-11.3.3 {816 sqlite3_get_autocommit $DB817} 1818do_test capi3-11.3.4 {819 db eval {PRAGMA lock_status}820} {main shared temp closed}821 822do_test capi3c-11.4 {823 sqlite3_step $STMT824} {SQLITE_ERROR}825do_test capi3c-11.5 {826 sqlite3_finalize $STMT827} {SQLITE_ERROR}828do_test capi3c-11.6 {829 catchsql {830 SELECT * FROM t1;831 }832} {0 {1 int 2 notatype}}833do_test capi3c-11.7 {834 sqlite3_get_autocommit $DB835} 1836do_test capi3c-11.8 {837 execsql {838 CREATE TABLE t2(a);839 INSERT INTO t2 VALUES(1);840 INSERT INTO t2 VALUES(2);841 BEGIN;842 INSERT INTO t2 VALUES(3);843 }844} {}845do_test capi3c-11.8.1 {846 sqlite3_get_autocommit $DB847} 0848do_test capi3c-11.9 {849 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]850 sqlite3_step $STMT851} {SQLITE_ROW}852do_test capi3c-11.9.1 {853 sqlite3_get_autocommit $DB854} 0855do_test capi3c-11.9.2 {856 catchsql {857 ROLLBACK;858 }859} {0 {}}860do_test capi3c-11.9.3 {861 sqlite3_get_autocommit $DB862} 1863do_test capi3c-11.10 {864 sqlite3_step $STMT865} {SQLITE_ROW}866do_test capi3c-11.11 {867 sqlite3_step $STMT868} {SQLITE_DONE}869ifcapable !autoreset {870 do_test capi3c-11.12armor {871 sqlite3_step $STMT872 sqlite3_step $STMT873 } {SQLITE_MISUSE}874} else {875 do_test capi3c-11.12 {876 sqlite3_step $STMT877 sqlite3_step $STMT878 } {SQLITE_ROW}879}880do_test capi3c-11.13 {881 sqlite3_finalize $STMT882} {SQLITE_OK}883do_test capi3c-11.14 {884 execsql {885 SELECT a FROM t2;886 }887} {1 2}888do_test capi3c-11.14.1 {889 sqlite3_get_autocommit $DB890} 1891do_test capi3c-11.15 {892 catchsql {893 ROLLBACK;894 }895} {1 {cannot rollback - no transaction is active}}896do_test capi3c-11.15.1 {897 sqlite3_get_autocommit $DB898} 1899do_test capi3c-11.16 {900 execsql {901 SELECT a FROM t2;902 }903} {1 2}904 905# Sanity check on the definition of 'outstanding VM'. This means any VM906# that has had sqlite3_step() called more recently than sqlite3_finalize() or907# sqlite3_reset(). So a VM that has just been prepared or reset does not908# count as an active VM.909do_test capi3c-11.17 {910 execsql {911 BEGIN;912 }913} {}914do_test capi3c-11.18 {915 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t1" -1 TAIL]916 catchsql {917 COMMIT;918 }919} {0 {}}920do_test capi3c-11.19 {921 sqlite3_step $STMT922} {SQLITE_ROW}923do_test capi3c-11.20 {924 catchsql {925 BEGIN;926 COMMIT;927 }928} {0 {}}929do_test capi3c-11.20 {930 sqlite3_reset $STMT931 catchsql {932 COMMIT;933 }934} {1 {cannot commit - no transaction is active}}935do_test capi3c-11.21 {936 sqlite3_finalize $STMT937} {SQLITE_OK}938 939# The following tests - capi3c-12.* - check that its Ok to start a940# transaction while other VMs are active, and that its Ok to execute941# atomic updates in the same situation 942#943do_test capi3c-12.1 {944 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]945 sqlite3_step $STMT946} {SQLITE_ROW}947do_test capi3c-12.2 {948 catchsql {949 INSERT INTO t1 VALUES(3, NULL);950 }951} {0 {}}952do_test capi3c-12.3 {953 catchsql {954 INSERT INTO t2 VALUES(4);955 }956} {0 {}}957do_test capi3c-12.4 {958 catchsql {959 BEGIN;960 INSERT INTO t1 VALUES(4, NULL);961 }962} {0 {}}963do_test capi3c-12.5 {964 sqlite3_step $STMT965} {SQLITE_ROW}966do_test capi3c-12.5.1 {967 sqlite3_step $STMT968} {SQLITE_ROW}969do_test capi3c-12.6 {970 sqlite3_step $STMT971} {SQLITE_DONE}972do_test capi3c-12.7 {973 sqlite3_finalize $STMT974} {SQLITE_OK}975do_test capi3c-12.8 {976 execsql {977 COMMIT;978 SELECT a FROM t1;979 }980} {1 2 3 4}981 982# Test cases capi3c-13.* test the sqlite3_clear_bindings() and 983# sqlite3_sleep APIs.984#985if {[llength [info commands sqlite3_clear_bindings]]>0} {986 do_test capi3c-13.1 {987 execsql {988 DELETE FROM t1;989 }990 set STMT [sqlite3_prepare_v2 $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL]991 sqlite3_step $STMT992 } {SQLITE_DONE}993 do_test capi3c-13.2 {994 sqlite3_reset $STMT995 sqlite3_bind_text $STMT 1 hello 5996 sqlite3_bind_text $STMT 2 world 5997 sqlite3_step $STMT998 } {SQLITE_DONE}999 do_test capi3c-13.3 {1000 sqlite3_reset $STMT1001 sqlite3_clear_bindings $STMT1002 sqlite3_step $STMT1003 } {SQLITE_DONE}1004 do_test capi3c-13-4 {1005 sqlite3_finalize $STMT1006 execsql {1007 SELECT * FROM t1;1008 }1009 } {{} {} hello world {} {}}1010}1011if {[llength [info commands sqlite3_sleep]]>0} {1012 do_test capi3c-13-5 {1013 set ms [sqlite3_sleep 80]1014 expr {$ms==80 || $ms==1000}1015 } {1}1016}1017 1018# Ticket #1219: Make sure binding APIs can handle a NULL pointer.1019#1020do_test capi3c-14.1 {1021 set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]1022 lappend rc $msg1023} {1 SQLITE_MISUSE}1024 1025# Ticket #1650: Honor the nBytes parameter to sqlite3_prepare.1026#1027do_test capi3c-15.1 {1028 set sql {SELECT * FROM t2}1029 set nbytes [string length $sql]1030 append sql { WHERE a==1}1031 set STMT [sqlite3_prepare_v2 $DB $sql $nbytes TAIL]1032 sqlite3_step $STMT1033 sqlite3_column_int $STMT 01034} {1}1035do_test capi3c-15.2 {1036 sqlite3_step $STMT1037 sqlite3_column_int $STMT 01038} {2}1039do_test capi3c-15.3 {1040 sqlite3_finalize $STMT1041} {SQLITE_OK}1042 1043# Make sure code is always generated even if an IF EXISTS or 1044# IF NOT EXISTS clause is present that the table does not or1045# does exists. That way we will always have a prepared statement1046# to expire when the schema changes.1047#1048do_test capi3c-16.1 {1049 set sql {DROP TABLE IF EXISTS t3}1050 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]1051 sqlite3_finalize $STMT1052 expr {$STMT!=""}1053} {1}1054do_test capi3c-16.2 {1055 set sql {CREATE TABLE IF NOT EXISTS t1(x,y)}1056 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]1057 sqlite3_finalize $STMT1058 expr {$STMT!=""}1059} {1}1060 1061# But still we do not generate code if there is no SQL1062#1063do_test capi3c-16.3 {1064 set STMT [sqlite3_prepare_v2 $DB {} -1 TAIL]1065 sqlite3_finalize $STMT1066 expr {$STMT==""}1067} {1}1068do_test capi3c-16.4 {1069 set STMT [sqlite3_prepare_v2 $DB {;} -1 TAIL]1070 sqlite3_finalize $STMT1071 expr {$STMT==""}1072} {1}1073 1074# Ticket #2154.1075#1076do_test capi3c-17.1 {1077 set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t2} -1 TAIL]1078 sqlite3_step $STMT1079} SQLITE_ROW1080do_test capi3c-17.2 {1081 sqlite3_column_int $STMT 01082} 41083do_test capi3c-17.3 {1084 sqlite3_step $STMT1085} SQLITE_DONE1086do_test capi3c-17.4 {1087 sqlite3_reset $STMT1088 db eval {CREATE INDEX i2 ON t2(a)}1089 sqlite3_step $STMT1090} SQLITE_ROW1091do_test capi3c-17.5 {1092 sqlite3_column_int $STMT 01093} 41094do_test capi3c-17.6 {1095 sqlite3_step $STMT1096} SQLITE_DONE1097do_test capi3c-17.7 {1098 sqlite3_reset $STMT1099 db eval {DROP INDEX i2}1100 sqlite3_step $STMT1101} SQLITE_ROW1102do_test capi3c-17.8 {1103 sqlite3_column_int $STMT 01104} 41105do_test capi3c-17.9 {1106 sqlite3_step $STMT1107} SQLITE_DONE1108do_test capi3c-17.10 {1109 sqlite3_finalize $STMT1110 set STMT [sqlite3_prepare_v2 $DB {SELECT b FROM t1 WHERE a=?} -1 TAIL]1111 sqlite3_bind_int $STMT 1 21112 db eval {1113 DELETE FROM t1;1114 INSERT INTO t1 VALUES(1,'one');1115 INSERT INTO t1 VALUES(2,'two');1116 INSERT INTO t1 VALUES(3,'three');1117 INSERT INTO t1 VALUES(4,'four');1118 }1119 sqlite3_step $STMT1120} SQLITE_ROW1121do_test capi3c-17.11 {1122 sqlite3_column_text $STMT 01123} two1124do_test capi3c-17.12 {1125 sqlite3_step $STMT1126} SQLITE_DONE1127do_test capi3c-17.13 {1128 sqlite3_reset $STMT1129 db eval {CREATE INDEX i1 ON t1(a)}1130 sqlite3_step $STMT1131} SQLITE_ROW1132do_test capi3c-17.14 {1133 sqlite3_column_text $STMT 01134} two1135do_test capi3c-17.15 {1136 sqlite3_step $STMT1137} SQLITE_DONE1138do_test capi3c-17.16 {1139 sqlite3_reset $STMT1140 db eval {DROP INDEX i1}1141 sqlite3_step $STMT1142} SQLITE_ROW1143do_test capi3c-17.17 {1144 sqlite3_column_text $STMT 01145} two1146do_test capi3c-17.18 {1147 sqlite3_step $STMT1148} SQLITE_DONE1149do_test capi3c-17.99 {1150 sqlite3_finalize $STMT1151} SQLITE_OK1152 1153# On the mailing list it has been reported that finalizing after1154# an SQLITE_BUSY return leads to a segfault. Here we test that case.1155#1156do_test capi3c-18.1 {1157 sqlite3 db2 test.db1158 set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t1} -1 TAIL]1159 sqlite3_step $STMT1160} SQLITE_ROW1161do_test capi3c-18.2 {1162 sqlite3_column_int $STMT 01163} 41164do_test capi3c-18.3 {1165 sqlite3_reset $STMT1166 db2 eval {BEGIN EXCLUSIVE}1167 sqlite3_step $STMT1168} SQLITE_BUSY1169do_test capi3c-18.4 {1170 sqlite3_finalize $STMT1171} SQLITE_BUSY1172do_test capi3c-18.5 {1173 db2 eval {COMMIT}1174 db2 close1175} {}1176 1177# Ticket #2158. The sqlite3_step() will still return SQLITE_SCHEMA1178# if the database schema changes in a way that makes the statement1179# no longer valid.1180#1181do_test capi3c-19.1 {1182 db eval {1183 CREATE TABLE t3(x,y);1184 INSERT INTO t3 VALUES(1,2);1185 }1186 set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL]1187 sqlite3_step $STMT1188} SQLITE_ROW1189do_test capi3c-19.2 {1190 sqlite3_column_int $STMT 01191} 11192do_test capi3c-19.3 {1193 sqlite3_step $STMT1194} SQLITE_DONE1195do_test capi3c-19.4 {1196 sqlite3_reset $STMT1197 db eval {DROP TABLE t3}1198 sqlite3_step $STMT1199} SQLITE_ERROR1200do_test capi3c-19.4.1 {