📄 capi3c.test
字号:
do_test capi3c-6.2 { sqlite3_step $STMT} {SQLITE_ROW}check_data $STMT capi3c-6.3 {INTEGER} {1} {1.0} {1}do_test capi3c-6.3 { sqlite3_finalize $STMT} {SQLITE_OK}do_test capi3c-6.4 { db cache flush sqlite3_close $DB} {SQLITE_OK}do_test capi3c-6.99-misuse { db close} {}if {![sqlite3 -has-codec]} { # Test what happens when the library encounters a newer file format. # Do this by updating the file format via the btree layer. do_test capi3c-7.1 { set ::bt [btree_open test.db 10 0] btree_begin_transaction $::bt set meta [btree_get_meta $::bt] lset meta 2 5 eval [concat btree_update_meta $::bt [lrange $meta 0 end]] btree_commit $::bt btree_close $::bt } {} do_test capi3c-7.2 { sqlite3 db test.db catchsql { SELECT * FROM sqlite_master; } } {1 {unsupported file format}} db close}if {![sqlite3 -has-codec]} { # Now test that the library correctly handles bogus entries in the # sqlite_master table (schema corruption). do_test capi3c-8.1 { file delete -force test.db file delete -force test.db-journal sqlite3 db test.db execsql { CREATE TABLE t1(a); } db close } {} do_test capi3c-8.2 { set ::bt [btree_open test.db 10 0] btree_begin_transaction $::bt set ::bc [btree_cursor $::bt 1 1] # Build a 5-field row record consisting of 5 null records. This is # officially black magic. catch {unset data} set data [binary format c6 {6 0 0 0 0 0}] btree_insert $::bc 5 $data btree_close_cursor $::bc btree_commit $::bt btree_close $::bt } {} do_test capi3c-8.3 { sqlite3 db test.db catchsql { SELECT * FROM sqlite_master; } } {1 {malformed database schema}} do_test capi3c-8.4 { set ::bt [btree_open test.db 10 0] btree_begin_transaction $::bt set ::bc [btree_cursor $::bt 1 1] # Build a 5-field row record. The first field is a string 'table', and # subsequent fields are all NULL. Replace the other broken record with # this one and try to read the schema again. The broken record uses # either UTF-8 or native UTF-16 (if this file is being run by # utf16.test). if { [string match UTF-16* $::ENC] } { set data [binary format c6a10 {6 33 0 0 0 0} [utf16 table]] } else { set data [binary format c6a5 {6 23 0 0 0 0} table] } btree_insert $::bc 5 $data btree_close_cursor $::bc btree_commit $::bt btree_close $::bt } {}; do_test capi3c-8.5 { db close sqlite3 db test.db catchsql { SELECT * FROM sqlite_master; } } {1 {malformed database schema}} db close}file delete -force test.dbfile delete -force test.db-journal# Test the english language string equivalents for sqlite error codesset code2english [list \SQLITE_OK {not an error} \SQLITE_ERROR {SQL logic error or missing database} \SQLITE_PERM {access permission denied} \SQLITE_ABORT {callback requested query abort} \SQLITE_BUSY {database is locked} \SQLITE_LOCKED {database table is locked} \SQLITE_NOMEM {out of memory} \SQLITE_READONLY {attempt to write a readonly database} \SQLITE_INTERRUPT {interrupted} \SQLITE_IOERR {disk I/O error} \SQLITE_CORRUPT {database disk image is malformed} \SQLITE_FULL {database or disk is full} \SQLITE_CANTOPEN {unable to open database file} \SQLITE_PROTOCOL {database locking protocol failure} \SQLITE_EMPTY {table contains no data} \SQLITE_SCHEMA {database schema has changed} \SQLITE_CONSTRAINT {constraint failed} \SQLITE_MISMATCH {datatype mismatch} \SQLITE_MISUSE {library routine called out of sequence} \SQLITE_NOLFS {kernel lacks large file support} \SQLITE_AUTH {authorization denied} \SQLITE_FORMAT {auxiliary database format error} \SQLITE_RANGE {bind or column index out of range} \SQLITE_NOTADB {file is encrypted or is not a database} \unknownerror {unknown error} \]set test_number 1foreach {code english} $code2english { do_test capi3c-9.$test_number "sqlite3_test_errstr $code" $english incr test_number}# Test the error message when a "real" out of memory occurs.if {[info command sqlite_malloc_stat]!=""} {set sqlite_malloc_fail 1do_test capi3c-10-1 { sqlite3 db test.db set DB [sqlite3_connection_pointer db] sqlite_malloc_fail 1 catchsql { select * from sqlite_master; }} {1 {out of memory}}do_test capi3c-10-2 { sqlite3_errmsg $::DB} {out of memory}ifcapable {utf16} { do_test capi3c-10-3 { utf8 [sqlite3_errmsg16 $::DB] } {out of memory}}db closesqlite_malloc_fail 0}# The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK# statement issued while there are still outstanding VMs that are part of# the transaction fails.sqlite3 db test.dbset DB [sqlite3_connection_pointer db]sqlite_register_test_function $DB funcdo_test capi3c-11.1 { execsql { BEGIN; CREATE TABLE t1(a, b); INSERT INTO t1 VALUES(1, 'int'); INSERT INTO t1 VALUES(2, 'notatype'); }} {}do_test capi3c-11.1.1 { sqlite3_get_autocommit $DB} 0do_test capi3c-11.2 { set STMT [sqlite3_prepare_v2 $DB "SELECT func(b, a) FROM t1" -1 TAIL] sqlite3_step $STMT} {SQLITE_ROW}do_test capi3c-11.3 { catchsql { COMMIT; }} {1 {cannot commit transaction - SQL statements in progress}}do_test capi3c-11.3.1 { sqlite3_get_autocommit $DB} 0do_test capi3c-11.4 { sqlite3_step $STMT} {SQLITE_ERROR}do_test capi3c-11.5 { sqlite3_finalize $STMT} {SQLITE_ERROR}do_test capi3c-11.6 { catchsql { SELECT * FROM t1; }} {0 {1 int 2 notatype}}do_test capi3c-11.6.1 { sqlite3_get_autocommit $DB} 0do_test capi3c-11.7 { catchsql { COMMIT; }} {0 {}}do_test capi3c-11.7.1 { sqlite3_get_autocommit $DB} 1do_test capi3c-11.8 { execsql { CREATE TABLE t2(a); INSERT INTO t2 VALUES(1); INSERT INTO t2 VALUES(2); BEGIN; INSERT INTO t2 VALUES(3); }} {}do_test capi3c-11.8.1 { sqlite3_get_autocommit $DB} 0do_test capi3c-11.9 { set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL] sqlite3_step $STMT} {SQLITE_ROW}do_test capi3c-11.9.1 { sqlite3_get_autocommit $DB} 0do_test capi3c-11.9.2 { catchsql { ROLLBACK; }} {1 {cannot rollback transaction - SQL statements in progress}}do_test capi3c-11.9.3 { sqlite3_get_autocommit $DB} 0do_test capi3c-11.10 { sqlite3_step $STMT} {SQLITE_ROW}do_test capi3c-11.11 { sqlite3_step $STMT} {SQLITE_ROW}do_test capi3c-11.12 { sqlite3_step $STMT} {SQLITE_DONE}do_test capi3c-11.13 { sqlite3_finalize $STMT} {SQLITE_OK}do_test capi3c-11.14 { execsql { SELECT a FROM t2; }} {1 2 3}do_test capi3c-11.14.1 { sqlite3_get_autocommit $DB} 0do_test capi3c-11.15 { catchsql { ROLLBACK; }} {0 {}}do_test capi3c-11.15.1 { sqlite3_get_autocommit $DB} 1do_test capi3c-11.16 { execsql { SELECT a FROM t2; }} {1 2}# Sanity check on the definition of 'outstanding VM'. This means any VM# that has had sqlite3_step() called more recently than sqlite3_finalize() or# sqlite3_reset(). So a VM that has just been prepared or reset does not# count as an active VM.do_test capi3c-11.17 { execsql { BEGIN; }} {}do_test capi3c-11.18 { set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t1" -1 TAIL] catchsql { COMMIT; }} {0 {}}do_test capi3c-11.19 { sqlite3_step $STMT} {SQLITE_ROW}do_test capi3c-11.20 { catchsql { BEGIN; COMMIT; }} {1 {cannot commit transaction - SQL statements in progress}}do_test capi3c-11.20 { sqlite3_reset $STMT catchsql { COMMIT; }} {0 {}}do_test capi3c-11.21 { sqlite3_finalize $STMT} {SQLITE_OK}# The following tests - capi3c-12.* - check that it's Ok to start a# transaction while other VMs are active, and that it's Ok to execute# atomic updates in the same situation #do_test capi3c-12.1 { set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL] sqlite3_step $STMT} {SQLITE_ROW}do_test capi3c-12.2 { catchsql { INSERT INTO t1 VALUES(3, NULL); }} {0 {}}do_test capi3c-12.3 { catchsql { INSERT INTO t2 VALUES(4); }} {0 {}}do_test capi3c-12.4 { catchsql { BEGIN; INSERT INTO t1 VALUES(4, NULL); }} {0 {}}do_test capi3c-12.5 { sqlite3_step $STMT} {SQLITE_ROW}do_test capi3c-12.5.1 { sqlite3_step $STMT} {SQLITE_ROW}do_test capi3c-12.6 { sqlite3_step $STMT} {SQLITE_DONE}do_test capi3c-12.7 { sqlite3_finalize $STMT} {SQLITE_OK}do_test capi3c-12.8 { execsql { COMMIT; SELECT a FROM t1; }} {1 2 3 4}# Test cases capi3c-13.* test the sqlite3_clear_bindings() and # sqlite3_sleep APIs.#if {[llength [info commands sqlite3_clear_bindings]]>0} { do_test capi3c-13.1 { execsql { DELETE FROM t1; } set STMT [sqlite3_prepare_v2 $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL] sqlite3_step $STMT } {SQLITE_DONE} do_test capi3c-13.2 { sqlite3_reset $STMT sqlite3_bind_text $STMT 1 hello 5 sqlite3_bind_text $STMT 2 world 5 sqlite3_step $STMT } {SQLITE_DONE} do_test capi3c-13.3 { sqlite3_reset $STMT sqlite3_clear_bindings $STMT sqlite3_step $STMT } {SQLITE_DONE} do_test capi3c-13-4 { sqlite3_finalize $STMT execsql { SELECT * FROM t1; } } {{} {} hello world {} {}}}if {[llength [info commands sqlite3_sleep]]>0} { do_test capi3c-13-5 { set ms [sqlite3_sleep 80] expr {$ms==80 || $ms==1000} } {1}}# Ticket #1219: Make sure binding APIs can handle a NULL pointer.#do_test capi3c-14.1 { set rc [catch {sqlite3_bind_text 0 1 hello 5} msg] lappend rc $msg} {1 SQLITE_MISUSE}# Ticket #1650: Honor the nBytes parameter to sqlite3_prepare.#do_test capi3c-15.1 { set sql {SELECT * FROM t2} set nbytes [string length $sql] append sql { WHERE a==1} set STMT [sqlite3_prepare_v2 $DB $sql $nbytes TAIL] sqlite3_step $STMT sqlite3_column_int $STMT 0} {1}do_test capi3c-15.2 { sqlite3_step $STMT sqlite3_column_int $STMT 0} {2}do_test capi3c-15.3 { sqlite3_finalize $STMT} {SQLITE_OK}# Make sure code is always generated even if an IF EXISTS or # IF NOT EXISTS clause is present that the table does not or# does exists. That way we will always have a prepared statement# to expire when the schema changes.#do_test capi3c-16.1 { set sql {DROP TABLE IF EXISTS t3} set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL] sqlite3_finalize $STMT expr {$STMT!=""}} {1}do_test capi3c-16.2 { set sql {CREATE TABLE IF NOT EXISTS t1(x,y)} set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL] sqlite3_finalize $STMT expr {$STMT!=""}} {1}# But still we do not generate code if there is no SQL#do_test capi3c-16.3 { set STMT [sqlite3_prepare_v2 $DB {} -1 TAIL] sqlite3_finalize $STMT expr {$STMT==""}} {1}do_test capi3c-16.4 { set STMT [sqlite3_prepare_v2 $DB {;} -1 TAIL] sqlite3_finalize $STMT expr {$STMT==""}} {1}# Ticket #2154.#do_test capi3c-17.1 { set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t2} -1 TAIL] sqlite3_step $STMT} SQLITE_ROWdo_test capi3c-17.2 { sqlite3_column_int $STMT 0} 4do_test capi3c-17.3 { sqlite3_step $STMT} SQLITE_DONEdo_test capi3c-17.4 { sqlite3_reset $STMT db eval {CREATE INDEX i2 ON t2(a)} sqlite3_step $STMT} SQLITE_ROWdo_test capi3c-17.5 { sqlite3_column_int $STMT 0} 4do_test capi3c-17.6 { sqlite3_step $STMT} SQLITE_DONEdo_test capi3c-17.7 { sqlite3_reset $STMT db eval {DROP INDEX i2} sqlite3_step $STMT} SQLITE_ROWdo_test capi3c-17.8 { sqlite3_column_int $STMT 0} 4do_test capi3c-17.9 { sqlite3_step $STMT} SQLITE_DONEdo_test capi3c-17.10 { sqlite3_finalize $STMT set STMT [sqlite3_prepare_v2 $DB {SELECT b FROM t1 WHERE a=?} -1 TAIL] sqlite3_bind_int $STMT 1 2 db eval { DELETE FROM t1; INSERT INTO t1 VALUES(1,'one'); INSERT INTO t1 VALUES(2,'two'); INSERT INTO t1 VALUES(3,'three'); INSERT INTO t1 VALUES(4,'four'); } sqlite3_step $STMT} SQLITE_ROWdo_test capi3c-17.11 { sqlite3_column_text $STMT 0} twodo_test capi3c-17.12 { sqlite3_step $STMT} SQLITE_DONEdo_test capi3c-17.13 { sqlite3_reset $STMT db eval {CREATE INDEX i1 ON t1(a)} sqlite3_step $STMT} SQLITE_ROWdo_test capi3c-17.14 { sqlite3_column_text $STMT 0} twodo_test capi3c-17.15 { sqlite3_step $STMT} SQLITE_DONEdo_test capi3c-17.16 { sqlite3_reset $STMT db eval {DROP INDEX i1} sqlite3_step $STMT} SQLITE_ROWdo_test capi3c-17.17 { sqlite3_column_text $STMT 0} twodo_test capi3c-17.18 { sqlite3_step $STMT} SQLITE_DONEdo_test capi3c-17.99 { sqlite3_finalize $STMT} SQLITE_OK# On the mailing list it has been reported that finalizing after# an SQLITE_BUSY return leads to a segfault. Here we test that case.#do_test capi3c-18.1 { sqlite3 db2 test.db set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t1} -1 TAIL] sqlite3_step $STMT} SQLITE_ROWdo_test capi3c-18.2 { sqlite3_column_int $STMT 0} 4do_test capi3c-18.3 { sqlite3_reset $STMT db2 eval {BEGIN EXCLUSIVE} sqlite3_step $STMT} SQLITE_BUSYdo_test capi3c-18.4 { sqlite3_finalize $STMT} SQLITE_BUSYdo_test capi3c-18.5 { db2 eval {COMMIT} db2 close} {}# Ticket #2158. The sqlite3_step() will still return SQLITE_SCHEMA# if the database schema changes in a way that makes the statement# no longer valid.#do_test capi3c-19.1 { db eval { CREATE TABLE t3(x,y); INSERT INTO t3 VALUES(1,2); } set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL] sqlite3_step $STMT} SQLITE_ROWdo_test capi3c-19.2 { sqlite3_column_int $STMT 0} 1do_test capi3c-19.3 { sqlite3_step $STMT} SQLITE_DONEdo_test capi3c-19.4 { sqlite3_reset $STMT db eval {DROP TABLE t3} sqlite3_step $STMT} SQLITE_SCHEMAdo_test capi3c-19.4.2 { sqlite3_errmsg $DB} {no such table: t3}do_test capi3c-19.5 { sqlite3_reset $STMT db eval { CREATE TABLE t3(x,y); INSERT INTO t3 VALUES(1,2); } sqlite3_step $STMT} SQLITE_ROWdo_test capi3c-19.6 { sqlite3_column_int $STMT 1} 2do_test capi3c-19.99 { sqlite3_finalize $STMT} SQLITE_OK# Make sure a change in a separate database connection does not# cause an SQLITE_SCHEMA return.#do_test capi3c-20.1 { set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL] sqlite3 db2 test.db db2 eval {CREATE TABLE t4(x)} sqlite3_step $STMT} SQLITE_ROWdo_test capi3c-20.2 { sqlite3_column_int $STMT 1} 2do_test capi3c-20.3 { sqlite3_step $STMT} SQLITE_DONEdo_test capi3c-20.4 { db2 close sqlite3_finalize $STMT} SQLITE_OKfinish_test
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -