AryaWu/sqlite
0
1# 2003 January 292#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. The12# 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.tcl19set ::testprefix capi320 21# Do not use a codec for tests in this file, as the database file is22# manipulated directly using tcl scripts (using the [hexio_write] command).23#24do_not_use_codec25 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 $r34}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 before39 # converting to UTF-8 using TCL.40 binary scan $str \c* vals41 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 $r47}48 49# These tests complement those in capi2.test. They are organized50# as follows:51#52# capi3-1.*: Test sqlite3_prepare 53# capi3-2.*: Test sqlite3_prepare1654# capi3-3.*: Test sqlite3_open55# capi3-4.*: Test sqlite3_open1656# capi3-5.*: Test the various sqlite3_result_* APIs57# 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 $DB64} 165do_test capi3-1.1 {66 set STMT [sqlite3_prepare $DB {SELECT name FROM sqlite_master} -1 TAIL]67 sqlite3_finalize $STMT68 set TAIL69} {}70do_test capi3-1.2.1 {71 sqlite3_errcode $DB72} {SQLITE_OK}73do_test capi3-1.2.2 {74 sqlite3_extended_errcode $DB75} {SQLITE_OK}76do_test capi3-1.3 {77 sqlite3_errmsg $DB78} {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 $STMT83 set TAIL84} {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 $STMT89 set TAIL90} {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 $STMT95 set TAIL96} {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 $DB106} {SQLITE_ERROR}107do_test capi3-1.8.2 {108 sqlite3_extended_errcode $DB109} {SQLITE_ERROR}110do_test capi3-1.9 {111 sqlite3_errmsg $DB112} {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 $STMT119 utf8 $::TAIL120 } {}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 $STMT125 utf8 $TAIL126 } {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 $DB135 } {SQLITE_ERROR}136 do_test capi3-2.4.2 {137 sqlite3_extended_errcode $DB138 } {SQLITE_ERROR}139 do_test capi3-2.5 {140 sqlite3_errmsg $DB141 } {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 $STMT149 } SQLITE_ROW150 do_test capi3-2.7 {151 sqlite3_step $STMT152 } SQLITE_DONE153 do_test capi3-2.8 {154 sqlite3_finalize $STMT155 } SQLITE_OK156 }157 158} ;# endif utf16159 160# rename sqlite3_open sqlite3_open_old161# 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 $db2166} {SQLITE_OK}167# FIX ME: Should test the db handle works.168do_test capi3-3.2 {169 sqlite3_close $db2170} {SQLITE_OK}171do_test capi3-3.3 {172 catch {173 set db2 [sqlite3_open /bogus/path/test.db {}]174 }175 set ::capi3_errno [sqlite3_system_errno $db2]176 list [sqlite3_extended_errcode $db2] [expr {$::capi3_errno!=0}]177} {SQLITE_CANTOPEN 1}178do_test capi3-3.4 {179 sqlite3_errmsg $db2180} {unable to open database file}181do_test capi3-3.5 {182 list [sqlite3_system_errno $db2] [sqlite3_close $db2]183} [list $::capi3_errno SQLITE_OK]184if {[clang_sanitize_address]==0 && 0} {185 do_test capi3-3.6.1-misuse {186 sqlite3_close $db2187 } {SQLITE_MISUSE}188 do_test capi3-3.6.2-misuse {189 sqlite3_errmsg $db2190 } {bad parameter or other API misuse}191 ifcapable {utf16} {192 do_test capi3-3.6.3-misuse {193 utf8 [sqlite3_errmsg16 $db2]194 } {bad parameter or other API misuse}195 }196}197 198do_test capi3-3.7 {199 set db2 [sqlite3_open]200 sqlite3_errcode $db2201} {SQLITE_OK}202do_test capi3-3.8 {203 sqlite3_close $db2204} {SQLITE_OK}205 206# rename sqlite3_open ""207# rename sqlite3_open_old sqlite3_open208 209ifcapable {utf16} {210do_test capi3-4.1 {211 set db2 [sqlite3_open16 [utf16 test.db] {}]212 sqlite3_errcode $db2213} {SQLITE_OK}214# FIX ME: Should test the db handle works.215do_test capi3-4.2 {216 sqlite3_close $db2217} {SQLITE_OK}218do_test capi3-4.3 {219 catch {220 set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}]221 }222 sqlite3_errcode $db2223} {SQLITE_CANTOPEN}224do_test capi3-4.4 {225 utf8 [sqlite3_errmsg16 $db2]226} {unable to open database file}227do_test capi3-4.5 {228 sqlite3_close $db2229} {SQLITE_OK}230} ;# utf16231 232# This proc is used to test the following API calls:233#234# sqlite3_column_count235# sqlite3_column_name236# sqlite3_column_name16237# sqlite3_column_decltype238# sqlite3_column_decltype16239#240# $STMT is a compiled SQL statement. $test is a prefix241# to use for test names within this proc. $names is a list242# of the column names that should be returned by $STMT.243# $decltypes is a list of column declaration types for $STMT.244#245# Example:246#247# set STMT [sqlite3_prepare "SELECT 1, 2, 2;" -1 DUMMY]248# check_header test1.1 {1 2 3} {"" "" ""}249#250proc check_header {STMT test names decltypes} {251 252 # Use the return value of sqlite3_column_count() to build253 # a list of column indexes. i.e. If sqlite3_column_count254 # is 3, build the list {0 1 2}.255 set ::idxlist [list]256 set ::numcols [sqlite3_column_count $STMT]257 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}258 259 # Column names in UTF-8260 do_test $test.1 {261 set cnamelist [list]262 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]} 263 set cnamelist264 } $names265 266 # Column names in UTF-16267 ifcapable {utf16} {268 do_test $test.2 {269 set cnamelist [list]270 foreach i $idxlist {271 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]272 }273 set cnamelist274 } $names275 }276 277 # Column names in UTF-8278 do_test $test.3 {279 set cnamelist [list]280 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]} 281 set cnamelist282 } $names283 284 # Column names in UTF-16285 ifcapable {utf16} {286 do_test $test.4 {287 set cnamelist [list]288 foreach i $idxlist {289 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]290 }291 set cnamelist292 } $names293 }294 295 # Column names in UTF-8296 do_test $test.5 {297 set cnamelist [list]298 foreach i $idxlist {lappend cnamelist [sqlite3_column_decltype $STMT $i]} 299 set cnamelist300 } $decltypes301 302 # Column declaration types in UTF-16303 ifcapable {utf16} {304 do_test $test.6 {305 set cnamelist [list]306 foreach i $idxlist {307 lappend cnamelist [utf8 [sqlite3_column_decltype16 $STMT $i]]308 }309 set cnamelist310 } $decltypes311 }312 313 314 # Test some out of range conditions:315 ifcapable {utf16} {316 do_test $test.7 {317 list \318 [sqlite3_column_name $STMT -1] \319 [sqlite3_column_name16 $STMT -1] \320 [sqlite3_column_decltype $STMT -1] \321 [sqlite3_column_decltype16 $STMT -1] \322 [sqlite3_column_name $STMT $numcols] \323 [sqlite3_column_name16 $STMT $numcols] \324 [sqlite3_column_decltype $STMT $numcols] \325 [sqlite3_column_decltype16 $STMT $numcols]326 } {{} {} {} {} {} {} {} {}}327 }328} 329 330# This proc is used to test the following API calls:331#332# sqlite3_column_origin_name333# sqlite3_column_origin_name16334# sqlite3_column_table_name335# sqlite3_column_table_name16336# sqlite3_column_database_name337# sqlite3_column_database_name16338#339# $STMT is a compiled SQL statement. $test is a prefix340# to use for test names within this proc. $names is a list341# of the column names that should be returned by $STMT.342# $decltypes is a list of column declaration types for $STMT.343#344# Example:345#346# set STMT [sqlite3_prepare "SELECT 1, 2, 2;" -1 DUMMY]347# check_header test1.1 {1 2 3} {"" "" ""}348#349proc check_origin_header {STMT test dbs tables cols} {350 # If sqlite3_column_origin_name() and friends are not compiled into351 # this build, this proc is a no-op.352 ifcapable columnmetadata {353 # Use the return value of sqlite3_column_count() to build354 # a list of column indexes. i.e. If sqlite3_column_count355 # is 3, build the list {0 1 2}.356 set ::idxlist [list]357 set ::numcols [sqlite3_column_count $STMT]358 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}359 360 # Database names in UTF-8361 do_test $test.8 {362 set cnamelist [list]363 foreach i $idxlist {364 lappend cnamelist [sqlite3_column_database_name $STMT $i]365 } 366 set cnamelist367 } $dbs368 369 # Database names in UTF-16370 ifcapable {utf16} {371 do_test $test.9 {372 set cnamelist [list]373 foreach i $idxlist {374 lappend cnamelist [utf8 [sqlite3_column_database_name16 $STMT $i]]375 }376 set cnamelist377 } $dbs378 }379 380 # Table names in UTF-8381 do_test $test.10 {382 set cnamelist [list]383 foreach i $idxlist {384 lappend cnamelist [sqlite3_column_table_name $STMT $i]385 } 386 set cnamelist387 } $tables388 389 # Table names in UTF-16390 ifcapable {utf16} {391 do_test $test.11 {392 set cnamelist [list]393 foreach i $idxlist {394 lappend cnamelist [utf8 [sqlite3_column_table_name16 $STMT $i]]395 }396 set cnamelist397 } $tables398 }399 400 # Origin names in UTF-8401 do_test $test.12 {402 set cnamelist [list]403 foreach i $idxlist {404 lappend cnamelist [sqlite3_column_origin_name $STMT $i]405 } 406 set cnamelist407 } $cols408 409 # Origin declaration types in UTF-16410 ifcapable {utf16} {411 do_test $test.13 {412 set cnamelist [list]413 foreach i $idxlist {414 lappend cnamelist [utf8 [sqlite3_column_origin_name16 $STMT $i]]415 }416 set cnamelist417 } $cols418 }419 }420}421 422# This proc is used to test the following APIs:423#424# sqlite3_data_count425# sqlite3_column_type426# sqlite3_column_int427# sqlite3_column_text428# sqlite3_column_text16429# sqlite3_column_double430#431# $STMT is a compiled SQL statement for which the previous call 432# to sqlite3_step returned SQLITE_ROW. $test is a prefix to use 433# for test names within this proc. $types is a list of the 434# manifest types for the current row. $ints, $doubles and $strings435# are lists of the integer, real and string representations of436# the values in the current row.437#438# Example:439#440# set STMT [sqlite3_prepare "SELECT 'hello', 1.1, NULL" -1 DUMMY]441# sqlite3_step $STMT442# check_data test1.2 {TEXT REAL NULL} {0 1 0} {0 1.1 0} {hello 1.1 {}}443#444proc check_data {STMT test types ints doubles strings} {445 446 # Use the return value of sqlite3_column_count() to build447 # a list of column indexes. i.e. If sqlite3_column_count448 # is 3, build the list {0 1 2}.449 set ::idxlist [list]450 set numcols [sqlite3_data_count $STMT]451 for {set i 0} {$i < $numcols} {incr i} {lappend ::idxlist $i}452 453# types454do_test $test.1 {455 set types [list]456 foreach i $idxlist {457 set x [sqlite3_column_type $STMT $i]458 # EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five459 # fundamental datatypes: 64-bit signed integer 64-bit IEEE floating460 # point number string BLOB NULL461 if {[lsearch {INTEGER FLOAT TEXT BLOB NULL} $x]<0} {462 set types ERROR463 break464 } else {465 lappend types $x466 }467 }468 set types469} $types470 471 472# Integers473do_test $test.2 {474 set ints [list]475 foreach i $idxlist {lappend ints [sqlite3_column_int64 $STMT $i]}476 set ints477} $ints478 479# bytes480set lens [list]481foreach i $::idxlist {482 lappend lens [string length [lindex $strings $i]]483}484do_test $test.3 {485 set bytes [list]486 set lens [list]487 foreach i $idxlist {488 lappend bytes [sqlite3_column_bytes $STMT $i]489 }490 set bytes491} $lens492 493# bytes16494ifcapable {utf16} {495 set lens [list]496 foreach i $::idxlist {497 lappend lens [expr 2 * [string length [lindex $strings $i]]]498 }499 do_test $test.4 {500 set bytes [list]501 set lens [list]502 foreach i $idxlist {503 lappend bytes [sqlite3_column_bytes16 $STMT $i]504 }505 set bytes506 } $lens507}508 509# Blob510do_test $test.5 {511 set utf8 [list]512 foreach i $idxlist {lappend utf8 [sqlite3_column_blob $STMT $i]}513 set utf8514} $strings515 516# UTF-8517do_test $test.6 {518 set utf8 [list]519 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}520 set utf8521} $strings522 523# Floats524do_test $test.7 {525 set utf8 [list]526 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}527 set utf8528} $doubles529 530# UTF-16531ifcapable {utf16} {532 do_test $test.8 {533 set utf8 [list]534 foreach i $idxlist {lappend utf8 [utf8 [sqlite3_column_text16 $STMT $i]]}535 set utf8536 } $strings537}538 539# Integers540do_test $test.9 {541 set ints [list]542 foreach i $idxlist {lappend ints [sqlite3_column_int $STMT $i]}543 set ints544} $ints545 546# Floats547do_test $test.10 {548 set utf8 [list]549 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}550 set utf8551} $doubles552 553# UTF-8554do_test $test.11 {555 set utf8 [list]556 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}557 set utf8558} $strings559 560# Types561do_test $test.12 {562 set types [list]563 foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}564 set types565} $types566 567# Test that an out of range request returns the equivalent of NULL568do_test $test.13 {569 sqlite3_column_int $STMT -1570} {0}571do_test $test.13 {572 sqlite3_column_text $STMT -1573} {}574 575}576 577ifcapable !floatingpoint {578 finish_test579 return580}581 582do_test capi3-5.0 {583 execsql {584 CREATE TABLE t1(a VARINT, b BLOB, c VARCHAR(16));585 INSERT INTO t1 VALUES(1, 2, 3);586 INSERT INTO t1 VALUES('one', 'two', NULL);587 INSERT INTO t1 VALUES(1.2, 1.3, 1.4);588 }589 set sql "SELECT * FROM t1"590 set STMT [sqlite3_prepare $DB $sql -1 TAIL]591 sqlite3_column_count $STMT592} 3593 594check_header $STMT capi3-5.1 {a b c} {VARINT BLOB VARCHAR(16)}595check_origin_header $STMT capi3-5.1 {main main main} {t1 t1 t1} {a b c}596do_test capi3-5.2 {597 sqlite3_step $STMT598} SQLITE_ROW599 600check_header $STMT capi3-5.3 {a b c} {VARINT BLOB VARCHAR(16)}601check_origin_header $STMT capi3-5.3 {main main main} {t1 t1 t1} {a b c}602check_data $STMT capi3-5.4 {INTEGER INTEGER TEXT} {1 2 3} {1.0 2.0 3.0} {1 2 3}603 604do_test capi3-5.5 {605 sqlite3_step $STMT606} SQLITE_ROW607 608check_header $STMT capi3-5.6 {a b c} {VARINT BLOB VARCHAR(16)}609check_origin_header $STMT capi3-5.6 {main main main} {t1 t1 t1} {a b c}610check_data $STMT capi3-5.7 {TEXT TEXT NULL} {0 0 0} {0.0 0.0 0.0} {one two {}}611 612do_test capi3-5.8 {613 sqlite3_step $STMT614} SQLITE_ROW615 616check_header $STMT capi3-5.9 {a b c} {VARINT BLOB VARCHAR(16)}617check_origin_header $STMT capi3-5.9 {main main main} {t1 t1 t1} {a b c}618check_data $STMT capi3-5.10 {FLOAT FLOAT TEXT} {1 1 1} {1.2 1.3 1.4} {1.2 1.3 1.4}619 620do_test capi3-5.11 {621 sqlite3_step $STMT622} SQLITE_DONE623 624do_test capi3-5.12 {625 sqlite3_finalize $STMT626} SQLITE_OK627 628do_test capi3-5.20 {629 set sql "SELECT a, sum(b), max(c) FROM t1 GROUP BY a"630 set STMT [sqlite3_prepare $DB $sql -1 TAIL]631 sqlite3_column_count $STMT632} 3633 634check_header $STMT capi3-5.21 {a sum(b) max(c)} {VARINT {} {}}635check_origin_header $STMT capi3-5.22 {main {} {}} {t1 {} {}} {a {} {}}636do_test capi3-5.23 {637 sqlite3_finalize $STMT638} SQLITE_OK639 640do_test capi3-5.30 {641 set sql "SELECT a AS x, sum(b) AS y, max(c) AS z FROM t1 AS m GROUP BY x"642 set STMT [sqlite3_prepare $DB $sql -1 TAIL]643 sqlite3_column_count $STMT644} 3645 646check_header $STMT capi3-5.31 {x y z} {VARINT {} {}}647check_origin_header $STMT capi3-5.32 {main {} {}} {t1 {} {}} {a {} {}}648do_test capi3-5.33 {649 sqlite3_finalize $STMT650} SQLITE_OK651 652# 2018-01-09: If a column is the last token if a string, the column name653# was not being set correctly, due to changes in check-in654# https://sqlite.org/src/info/0fdf97efe5df7455655#656# This problem was detected by the community during beta-testing.657#658do_test capi3-5.34 {659 set STMT [sqlite3_prepare $DB {SELECT :a, :b} -1 TAIL]660 sqlite3_column_count $STMT661} 2662check_header $STMT capi-5.35 {:a :b} {{} {}}663sqlite3_finalize $STMT664 665set ::ENC [execsql {pragma encoding}]666db close667 668do_test capi3-6.0 {669 sqlite3 db test.db670 set DB [sqlite3_connection_pointer db]671 if {[sqlite3 -has-codec]==0} { sqlite3_key $DB xyzzy }672 set sql {SELECT a FROM t1 order by rowid}673 set STMT [sqlite3_prepare $DB $sql -1 TAIL]674 expr 0675} {0}676do_test capi3-6.1 {677 db cache flush678 sqlite3_close $DB679} {SQLITE_BUSY}680 681# 6.2 and 6.3 used to return SQLITE_ERROR and SQLITE_SCHEMA, respectively.682# But since attempting to close a connection no longer resets the internal683# schema and expires all statements, this is no longer the case.684do_test capi3-6.2 {685 sqlite3_step $STMT686} {SQLITE_ROW}687#check_data $STMT capi3-6.3 {INTEGER} {1} {1.0} {1}688do_test capi3-6.3 {689 sqlite3_finalize $STMT690} {SQLITE_OK}691 692if {0 && [clang_sanitize_address]==0} {693 # This use-after-free occasionally causes segfaults during ordinary694 # builds. Let's just disable it completely.695 do_test capi3-6.4-misuse {696 db cache flush697 sqlite3_close $DB698 } {SQLITE_OK}699}700db close701 702# This procedure sets the value of the file-format in file 'test.db'703# to $newval. Also, the schema cookie is incremented.704# 705proc set_file_format {newval} {706 hexio_write test.db 44 [hexio_render_int32 $newval]707 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]708 incr schemacookie709 hexio_write test.db 40 [hexio_render_int32 $schemacookie]710 return {}711}712 713# This procedure returns the value of the file-format in file 'test.db'.714# 715proc get_file_format {{fname test.db}} {716 return [hexio_get_int [hexio_read $fname 44 4]]717}718 719if {![sqlite3 -has-codec]} {720 # Test what happens when the library encounters a newer file format.721 do_test capi3-7.1 {722 set_file_format 5723 } {}724 do_test capi3-7.2 {725 catch { sqlite3 db test.db }726 catchsql {727 SELECT * FROM sqlite_master;728 }729 } {1 {unsupported file format}}730 db close731}732 733if {![sqlite3 -has-codec]} {734 # Now test that the library correctly handles bogus entries in the735 # sqlite_master table (schema corruption).736 do_test capi3-8.1 {737 forcedelete test.db test.db-journal738 sqlite3 db test.db739 execsql {740 CREATE TABLE t1(a);741 }742 db close743 } {}744 do_test capi3-8.2 {745 sqlite3 db test.db746 sqlite3_db_config db DEFENSIVE 0747 execsql {748 PRAGMA writable_schema=ON;749 INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL);750 }751 db close752 } {}753 do_test capi3-8.3 {754 catch { sqlite3 db test.db }755 catchsql {756 SELECT * FROM sqlite_master;757 }758 } {1 {malformed database schema (?)}}759 do_test capi3-8.4 {760 # Build a 5-field row record. The first field is a string 'table', and761 # subsequent fields are all NULL.762 db close763 forcedelete test.db test.db-journal764 sqlite3 db test.db765 sqlite3_db_config db DEFENSIVE 0766 execsql {767 CREATE TABLE t1(a);768 PRAGMA writable_schema=ON;769 INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL);770 }771 db close772 } {};773 do_test capi3-8.5 {774 catch { sqlite3 db test.db }775 catchsql {776 SELECT * FROM sqlite_master;777 }778 } {1 {malformed database schema (?)}}779 db close780}781forcedelete test.db782forcedelete test.db-journal783 784 785# Test the english language string equivalents for sqlite error codes786set code2english [list \787SQLITE_OK {not an error} \788SQLITE_ERROR {SQL logic error} \789SQLITE_PERM {access permission denied} \790SQLITE_ABORT {query aborted} \791SQLITE_BUSY {database is locked} \792SQLITE_LOCKED {database table is locked} \793SQLITE_NOMEM {out of memory} \794SQLITE_READONLY {attempt to write a readonly database} \795SQLITE_INTERRUPT {interrupted} \796SQLITE_IOERR {disk I/O error} \797SQLITE_CORRUPT {database disk image is malformed} \798SQLITE_FULL {database or disk is full} \799SQLITE_CANTOPEN {unable to open database file} \800SQLITE_SCHEMA {database schema has changed} \801SQLITE_CONSTRAINT {constraint failed} \802SQLITE_MISMATCH {datatype mismatch} \803SQLITE_MISUSE {bad parameter or other API misuse} \804SQLITE_AUTH {authorization denied} \805SQLITE_RANGE {column index out of range} \806SQLITE_NOTADB {file is not a database} \807unknownerror {unknown error} \808]809 810set test_number 1811foreach {code english} $code2english {812 do_test capi3-9.$test_number "sqlite3_test_errstr $code" $english813 incr test_number814}815 816# Test the error message when a "real" out of memory occurs.817if { [permutation] != "nofaultsim" } {818 do_test capi3-10-1 {819 sqlite3 db test.db820 set DB [sqlite3_connection_pointer db]821 sqlite3_memdebug_fail 1822 catchsql {823 select * from sqlite_master;824 }825 } {1 {out of memory}}826 do_test capi3-10-2 {827 sqlite3_errmsg $::DB828 } {out of memory}829 ifcapable {utf16} {830 do_test capi3-10-3 {831 utf8 [sqlite3_errmsg16 $::DB]832 } {out of memory}833 }834 db close835 sqlite3_memdebug_fail -1836 do_test capi3-10-4 {837 sqlite3 db test.db838 set DB [sqlite3_connection_pointer db]839 sqlite3_memdebug_fail 1840 catchsql {841 select * from sqlite_master where rowid>5;842 }843 } {1 {out of memory}}844 do_test capi3-10-5 {845 sqlite3_errmsg $::DB846 } {out of memory}847 ifcapable {utf16} {848 do_test capi3-10-6 {849 utf8 [sqlite3_errmsg16 $::DB]850 } {out of memory}851 }852 db close853 sqlite3_memdebug_fail -1854}855 856# The following tests - capi3-11.* - test that a COMMIT or ROLLBACK857# statement issued while there are still outstanding VMs that are part of858# the transaction fails.859sqlite3 db test.db860set DB [sqlite3_connection_pointer db]861sqlite_register_test_function $DB func862do_test capi3-11.1 {863 execsql {864 BEGIN;865 CREATE TABLE t1(a, b);866 INSERT INTO t1 VALUES(1, 'int');867 INSERT INTO t1 VALUES(2, 'notatype');868 }869} {}870do_test capi3-11.1.1 {871 sqlite3_get_autocommit $DB872} 0873do_test capi3-11.2 {874 set STMT [sqlite3_prepare $DB "SELECT func(b, a) FROM t1" -1 TAIL]875 sqlite3_step $STMT876} {SQLITE_ROW}877 878# As of 3.6.5 a COMMIT is OK during while a query is still running -879# as long as it is a read-only query and not an incremental BLOB write.880#881do_test capi3-11.3.1 {882 catchsql {883 COMMIT;884 }885} {0 {}}886do_test capi3-11.3.2 {887 sqlite3_extended_errcode $DB888} {SQLITE_OK}889do_test capi3-11.3.3 {890 sqlite3_get_autocommit $DB891} 1892do_test capi3-11.3.4 {893 db eval {PRAGMA lock_status}894} {main shared temp closed}895 896do_test capi3-11.4 {897 sqlite3_step $STMT898} {SQLITE_ERROR}899do_test capi3-11.5 {900 sqlite3_finalize $STMT901} {SQLITE_ERROR}902do_test capi3-11.6 {903 catchsql {904 SELECT * FROM t1;905 }906} {0 {1 int 2 notatype}}907do_test capi3-11.7 {908 sqlite3_get_autocommit $DB909} 1910do_test capi3-11.8 {911 execsql {912 CREATE TABLE t2(a);913 INSERT INTO t2 VALUES(1);914 INSERT INTO t2 VALUES(2);915 BEGIN;916 INSERT INTO t2 VALUES(3);917 }918} {}919do_test capi3-11.8.1 {920 sqlite3_get_autocommit $DB921} 0922do_test capi3-11.9 {923 set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL]924 sqlite3_step $STMT925} {SQLITE_ROW}926do_test capi3-11.9.1 {927 sqlite3_get_autocommit $DB928} 0929do_test capi3-11.9.2 {930 catchsql {931 ROLLBACK;932 }933} {0 {}}934do_test capi3-11.9.3 {935 sqlite3_get_autocommit $DB936} 1937do_test capi3-11.10 {938 sqlite3_step $STMT939} {SQLITE_ROW}940do_test capi3-11.11 {941 sqlite3_step $STMT942} {SQLITE_DONE}943ifcapable !autoreset {944 do_test capi3-11.12armor {945 sqlite3_step $STMT946 sqlite3_step $STMT947 } {SQLITE_MISUSE}948} else {949 do_test capi3-11.12 {950 sqlite3_step $STMT951 sqlite3_step $STMT952 } {SQLITE_ROW}953}954do_test capi3-11.13 {955 sqlite3_finalize $STMT956} {SQLITE_OK}957do_test capi3-11.14 {958 execsql {959 SELECT a FROM t2;960 }961} {1 2}962do_test capi3-11.14.1 {963 sqlite3_get_autocommit $DB964} 1965do_test capi3-11.15 {966 catchsql {967 ROLLBACK;968 }969} {1 {cannot rollback - no transaction is active}}970do_test capi3-11.15.1 {971 sqlite3_get_autocommit $DB972} 1973do_test capi3-11.16 {974 execsql {975 SELECT a FROM t2;976 }977} {1 2}978 979# Sanity check on the definition of 'outstanding VM'. This means any VM980# that has had sqlite3_step() called more recently than sqlite3_finalize() or981# sqlite3_reset(). So a VM that has just been prepared or reset does not982# count as an active VM.983do_test capi3-11.17 {984 execsql {985 BEGIN;986 }987} {}988do_test capi3-11.18 {989 set STMT [sqlite3_prepare $DB "SELECT a FROM t1" -1 TAIL]990 catchsql {991 COMMIT;992 }993} {0 {}}994do_test capi3-11.19 {995 sqlite3_step $STMT996} {SQLITE_ROW}997do_test capi3-11.20 {998 catchsql {999 BEGIN;1000 COMMIT;1001 }1002} {0 {}}1003do_test capi3-11.20 {1004 sqlite3_reset $STMT1005 catchsql {1006 COMMIT;1007 }1008} {1 {cannot commit - no transaction is active}}1009do_test capi3-11.21 {1010 sqlite3_finalize $STMT1011} {SQLITE_OK}1012 1013# The following tests - capi3-12.* - check that its Ok to start a1014# transaction while other VMs are active, and that its Ok to execute1015# atomic updates in the same situation 1016#1017do_test capi3-12.1 {1018 set STMT [sqlite3_prepare $DB "SELECT a FROM t2" -1 TAIL]1019 sqlite3_step $STMT1020} {SQLITE_ROW}1021do_test capi3-12.2 {1022 catchsql {1023 INSERT INTO t1 VALUES(3, NULL);1024 }1025} {0 {}}1026do_test capi3-12.3 {1027 catchsql {1028 INSERT INTO t2 VALUES(4);1029 }1030} {0 {}}1031do_test capi3-12.4 {1032 catchsql {1033 BEGIN;1034 INSERT INTO t1 VALUES(4, NULL);1035 }1036} {0 {}}1037do_test capi3-12.5 {1038 sqlite3_step $STMT1039} {SQLITE_ROW}1040do_test capi3-12.5.1 {1041 sqlite3_step $STMT1042} {SQLITE_ROW}1043do_test capi3-12.6 {1044 sqlite3_step $STMT1045} {SQLITE_DONE}1046do_test capi3-12.7 {1047 sqlite3_finalize $STMT1048} {SQLITE_OK}1049do_test capi3-12.8 {1050 execsql {1051 COMMIT;1052 SELECT a FROM t1;1053 }1054} {1 2 3 4}1055 1056# Test cases capi3-13.* test the sqlite3_clear_bindings() and 1057# sqlite3_sleep APIs.1058#1059if {[llength [info commands sqlite3_clear_bindings]]>0} {1060 do_test capi3-13.1 {1061 execsql {1062 DELETE FROM t1;1063 }1064 set STMT [sqlite3_prepare $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL]1065 sqlite3_step $STMT1066 } {SQLITE_DONE}1067 do_test capi3-13.2 {1068 sqlite3_reset $STMT1069 sqlite3_bind_text $STMT 1 hello 51070 sqlite3_bind_text $STMT 2 world 51071 sqlite3_step $STMT1072 } {SQLITE_DONE}1073 do_test capi3-13.3 {1074 sqlite3_reset $STMT1075 sqlite3_clear_bindings $STMT1076 sqlite3_step $STMT1077 } {SQLITE_DONE}1078 do_test capi3-13-4 {1079 sqlite3_finalize $STMT1080 execsql {1081 SELECT * FROM t1;1082 }1083 } {{} {} hello world {} {}}1084}1085if {[llength [info commands sqlite3_sleep]]>0} {1086 do_test capi3-13-5 {1087 set ms [sqlite3_sleep 80]1088 expr {$ms==80 || $ms==1000}1089 } {1}1090}1091 1092# Ticket #1219: Make sure binding APIs can handle a NULL pointer.1093# 1094if {[clang_sanitize_address]==0} {1095 do_test capi3-14.1-misuse {1096 set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]1097 lappend rc $msg1098 } {1 SQLITE_MISUSE}1099}1100 1101# Ticket #1650: Honor the nBytes parameter to sqlite3_prepare.1102#1103do_test capi3-15.1 {1104 set sql {SELECT * FROM t2}1105 set nbytes [string length $sql]1106 append sql { WHERE a==1}1107 set STMT [sqlite3_prepare $DB $sql $nbytes TAIL]1108 sqlite3_step $STMT1109 sqlite3_column_int $STMT 01110} {1}1111do_test capi3-15.2 {1112 sqlite3_step $STMT1113 sqlite3_column_int $STMT 01114} {2}1115do_test capi3-15.3 {1116 sqlite3_finalize $STMT1117} {SQLITE_OK}1118do_test capi3-15.4 {1119 # 123456789 12345671120 set sql {SELECT 1234567890}1121 set STMT [sqlite3_prepare $DB $sql 8 TAIL]1122 sqlite3_step $STMT1123 set v1 [sqlite3_column_int $STMT 0]1124 sqlite3_finalize $STMT1125 set v11126} {1}1127do_test capi3-15.5 {1128 # 123456789 12345671129 set sql {SELECT 1234567890}1130 set STMT [sqlite3_prepare $DB $sql 9 TAIL]1131 sqlite3_step $STMT1132 set v1 [sqlite3_column_int $STMT 0]1133 sqlite3_finalize $STMT1134 set v11135} {12}1136do_test capi3-15.6 {1137 # 123456789 12345671138 set sql {SELECT 1234567890}1139 set STMT [sqlite3_prepare $DB $sql 12 TAIL]1140 sqlite3_step $STMT1141 set v1 [sqlite3_column_int $STMT 0]1142 sqlite3_finalize $STMT1143 set v11144} {12345}1145do_test capi3-15.7 {1146 # 123456789 12345671147 set sql {SELECT 12.34567890}1148 set STMT [sqlite3_prepare $DB $sql 12 TAIL]1149 sqlite3_step $STMT1150 set v1 [sqlite3_column_double $STMT 0]1151 sqlite3_finalize $STMT1152 set v11153} {12.34}1154do_test capi3-15.8 {1155 # 123456789 12345671156 set sql {SELECT 12.34567890}1157 set STMT [sqlite3_prepare $DB $sql 14 TAIL]1158 sqlite3_step $STMT1159 set v1 [sqlite3_column_double $STMT 0]1160 sqlite3_finalize $STMT1161 set v11162} {12.3456}1163 1164# Make sure code is always generated even if an IF EXISTS or 1165# IF NOT EXISTS clause is present that the table does not or1166# does exists. That way we will always have a prepared statement1167# to expire when the schema changes.1168#1169do_test capi3-16.1 {1170 set sql {DROP TABLE IF EXISTS t3}1171 set STMT [sqlite3_prepare $DB $sql -1 TAIL]1172 sqlite3_finalize $STMT1173 expr {$STMT!=""}1174} {1}1175do_test capi3-16.2 {1176 set sql {CREATE TABLE IF NOT EXISTS t1(x,y)}1177 set STMT [sqlite3_prepare $DB $sql -1 TAIL]1178 sqlite3_finalize $STMT1179 expr {$STMT!=""}1180} {1}1181 1182# But still we do not generate code if there is no SQL1183#1184do_test capi3-16.3 {1185 set STMT [sqlite3_prepare $DB {} -1 TAIL]1186 sqlite3_finalize $STMT1187 expr {$STMT==""}1188} {1}1189do_test capi3-16.4 {1190 set STMT [sqlite3_prepare $DB {;} -1 TAIL]1191 sqlite3_finalize $STMT1192 expr {$STMT==""}1193} {1}1194 1195# Ticket #2426: Misuse of sqlite3_column_* by calling it after1196# a sqlite3_reset should be harmless.1197#1198do_test capi3-17.1 {1199 set STMT [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]1200 sqlite3_step $STMT