3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This is a copy of the capi3.test file that has been adapted to
14 # test the new sqlite3_prepare_v2 interface.
16 # $Id: capi3c.test,v 1.23 2009/07/22 07:27:57 danielk1977 Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
23 # Do not use a codec for tests in this file, as the database file is
24 # manipulated directly using tcl scripts (using the [hexio_write] command).
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.
30 proc utf16 {str {nt 1}} {
31 set r [encoding convertto unicode $str]
38 # Return the UTF-8 representation of the supplied UTF-16 string $str.
40 # If $str ends in two 0x00 0x00 bytes, knock these off before
41 # converting to UTF-8 using TCL.
42 binary scan $str \c* vals
43 if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
44 set str [binary format \c* [lrange $vals 0 end-2]]
47 set r [encoding convertfrom unicode $str]
51 # These tests complement those in capi2.test. They are organized
54 # capi3c-1.*: Test sqlite3_prepare_v2
55 # capi3c-2.*: Test sqlite3_prepare16_v2
56 # capi3c-3.*: Test sqlite3_open
57 # capi3c-4.*: Test sqlite3_open16
58 # capi3c-5.*: Test the various sqlite3_result_* APIs
59 # capi3c-6.*: Test that sqlite3_close fails if there are outstanding VMs.
62 set DB [sqlite3_connection_pointer db]
65 sqlite3_get_autocommit $DB
68 set STMT [sqlite3_prepare_v2 $DB {SELECT name FROM sqlite_master} -1 TAIL]
69 sqlite3_finalize $STMT
72 do_test capi3c-1.2.1 {
75 do_test capi3c-1.2.2 {
76 sqlite3_extended_errcode $DB
82 set sql {SELECT name FROM sqlite_master;SELECT 10}
83 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
84 sqlite3_finalize $STMT
88 set sql {SELECT namex FROM sqlite_master}
90 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
93 do_test capi3c-1.6.1 {
96 do_test capi3c-1.6.2 {
97 sqlite3_extended_errcode $DB
101 } {no such column: namex}
106 set sql16 [utf16 {SELECT name FROM sqlite_master}]
107 set STMT [sqlite3_prepare16_v2 $DB $sql16 -1 ::TAIL]
108 sqlite3_finalize $STMT
112 set sql [utf16 {SELECT name FROM sqlite_master;SELECT 10}]
113 set STMT [sqlite3_prepare16_v2 $DB $sql -1 TAIL]
114 sqlite3_finalize $STMT
118 set sql [utf16 {SELECT namex FROM sqlite_master}]
120 set STMT [sqlite3_prepare16_v2 $DB $sql -1 TAIL]
123 do_test capi3c-2.4.1 {
126 do_test capi3c-2.4.2 {
127 sqlite3_extended_errcode $DB
131 } {no such column: namex}
133 ifcapable schema_pragmas {
135 execsql {CREATE TABLE tablename(x)}
136 set sql16 [utf16 {PRAGMA table_info("TableName")}]
137 set STMT [sqlite3_prepare16_v2 $DB $sql16 -1 TAIL]
144 sqlite3_finalize $STMT
150 # rename sqlite3_open sqlite3_open_old
151 # proc sqlite3_open {fname options} {sqlite3_open_new $fname $options}
154 set db2 [sqlite3_open test.db {}]
157 # FIX ME: Should test the db handle works.
163 set db2 [sqlite3_open /bogus/path/test.db {}]
169 } {unable to open database file}
173 if {[clang_sanitize_address]==0} {
174 do_test capi3c-3.6.1-misuse {
177 do_test capi3c-3.6.2-misuse {
179 } {library routine called out of sequence}
181 do_test capi3c-3.6.3-misuse {
182 utf8 [sqlite3_errmsg16 $db2]
183 } {library routine called out of sequence}
187 # rename sqlite3_open ""
188 # rename sqlite3_open_old sqlite3_open
192 set db2 [sqlite3_open16 [utf16 test.db] {}]
195 # FIX ME: Should test the db handle works.
201 set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}]
206 utf8 [sqlite3_errmsg16 $db2]
207 } {unable to open database file}
213 # This proc is used to test the following API calls:
215 # sqlite3_column_count
216 # sqlite3_column_name
217 # sqlite3_column_name16
218 # sqlite3_column_decltype
219 # sqlite3_column_decltype16
221 # $STMT is a compiled SQL statement. $test is a prefix
222 # to use for test names within this proc. $names is a list
223 # of the column names that should be returned by $STMT.
224 # $decltypes is a list of column declaration types for $STMT.
228 # set STMT [sqlite3_prepare_v2 "SELECT 1, 2, 2;" -1 DUMMY]
229 # check_header test1.1 {1 2 3} {"" "" ""}
231 proc check_header {STMT test names decltypes} {
233 # Use the return value of sqlite3_column_count() to build
234 # a list of column indexes. i.e. If sqlite3_column_count
235 # is 3, build the list {0 1 2}.
237 set ::numcols [sqlite3_column_count $STMT]
238 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}
240 # Column names in UTF-8
243 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]}
247 # Column names in UTF-16
252 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]
258 # Column names in UTF-8
261 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]}
265 # Column names in UTF-16
270 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]
276 # Column names in UTF-8
279 foreach i $idxlist {lappend cnamelist [sqlite3_column_decltype $STMT $i]}
283 # Column declaration types in UTF-16
288 lappend cnamelist [utf8 [sqlite3_column_decltype16 $STMT $i]]
295 # Test some out of range conditions:
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 } {{} {} {} {} {} {} {} {}}
311 # This proc is used to test the following API calls:
313 # sqlite3_column_origin_name
314 # sqlite3_column_origin_name16
315 # sqlite3_column_table_name
316 # sqlite3_column_table_name16
317 # sqlite3_column_database_name
318 # sqlite3_column_database_name16
320 # $STMT is a compiled SQL statement. $test is a prefix
321 # to use for test names within this proc. $names is a list
322 # of the column names that should be returned by $STMT.
323 # $decltypes is a list of column declaration types for $STMT.
327 # set STMT [sqlite3_prepare_v2 "SELECT 1, 2, 2;" -1 DUMMY]
328 # check_header test1.1 {1 2 3} {"" "" ""}
330 proc check_origin_header {STMT test dbs tables cols} {
331 # If sqlite3_column_origin_name() and friends are not compiled into
332 # this build, this proc is a no-op.
333 ifcapable columnmetadata {
335 # Use the return value of sqlite3_column_count() to build
336 # a list of column indexes. i.e. If sqlite3_column_count
337 # is 3, build the list {0 1 2}.
339 set ::numcols [sqlite3_column_count $STMT]
340 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}
342 # Database names in UTF-8
346 lappend cnamelist [sqlite3_column_database_name $STMT $i]
351 # Database names in UTF-16
356 lappend cnamelist [utf8 [sqlite3_column_database_name16 $STMT $i]]
362 # Table names in UTF-8
366 lappend cnamelist [sqlite3_column_table_name $STMT $i]
371 # Table names in UTF-16
376 lappend cnamelist [utf8 [sqlite3_column_table_name16 $STMT $i]]
382 # Origin names in UTF-8
386 lappend cnamelist [sqlite3_column_origin_name $STMT $i]
391 # Origin declaration types in UTF-16
396 lappend cnamelist [utf8 [sqlite3_column_origin_name16 $STMT $i]]
404 # This proc is used to test the following APIs:
407 # sqlite3_column_type
409 # sqlite3_column_text
410 # sqlite3_column_text16
411 # sqlite3_column_double
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 $strings
417 # are lists of the integer, real and string representations of
418 # the values in the current row.
422 # set STMT [sqlite3_prepare_v2 "SELECT 'hello', 1.1, NULL" -1 DUMMY]
424 # check_data test1.2 {TEXT REAL NULL} {0 1 0} {0 1.1 0} {hello 1.1 {}}
426 proc check_data {STMT test types ints doubles strings} {
428 # Use the return value of sqlite3_column_count() to build
429 # a list of column indexes. i.e. If sqlite3_column_count
430 # is 3, build the list {0 1 2}.
432 set numcols [sqlite3_data_count $STMT]
433 for {set i 0} {$i < $numcols} {incr i} {lappend ::idxlist $i}
438 foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}
445 foreach i $idxlist {lappend ints [sqlite3_column_int64 $STMT $i]}
451 foreach i $::idxlist {
452 lappend lens [string length [lindex $strings $i]]
458 lappend bytes [sqlite3_column_bytes $STMT $i]
466 foreach i $::idxlist {
467 lappend lens [expr 2 * [string length [lindex $strings $i]]]
473 lappend bytes [sqlite3_column_bytes16 $STMT $i]
482 foreach i $idxlist {lappend utf8 [sqlite3_column_blob $STMT $i]}
489 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}
496 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}
504 foreach i $idxlist {lappend utf8 [utf8 [sqlite3_column_text16 $STMT $i]]}
512 foreach i $idxlist {lappend ints [sqlite3_column_int $STMT $i]}
519 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}
526 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}
533 foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}
537 # Test that an out of range request returns the equivalent of NULL
539 sqlite3_column_int $STMT -1
542 sqlite3_column_text $STMT -1
547 ifcapable !floatingpoint {
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);
559 set sql "SELECT * FROM t1"
560 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
561 sqlite3_column_count $STMT
564 check_header $STMT capi3c-5.1 {a b c} {VARINT BLOB VARCHAR(16)}
565 check_origin_header $STMT capi3c-5.1 {main main main} {t1 t1 t1} {a b c}
570 check_header $STMT capi3c-5.3 {a b c} {VARINT BLOB VARCHAR(16)}
571 check_origin_header $STMT capi3c-5.3 {main main main} {t1 t1 t1} {a b c}
572 check_data $STMT capi3c-5.4 {INTEGER INTEGER TEXT} {1 2 3} {1.0 2.0 3.0} {1 2 3}
578 check_header $STMT capi3c-5.6 {a b c} {VARINT BLOB VARCHAR(16)}
579 check_origin_header $STMT capi3c-5.6 {main main main} {t1 t1 t1} {a b c}
580 check_data $STMT capi3c-5.7 {TEXT TEXT NULL} {0 0 0} {0.0 0.0 0.0} {one two {}}
586 check_header $STMT capi3c-5.9 {a b c} {VARINT BLOB VARCHAR(16)}
587 check_origin_header $STMT capi3c-5.9 {main main main} {t1 t1 t1} {a b c}
588 check_data $STMT capi3c-5.10 {FLOAT FLOAT TEXT} {1 1 1} {1.2 1.3 1.4} {1.2 1.3 1.4}
590 do_test capi3c-5.11 {
594 do_test capi3c-5.12 {
595 sqlite3_finalize $STMT
598 do_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 $STMT
604 check_header $STMT capi3c-5.21 {a sum(b) max(c)} {VARINT {} {}}
605 check_origin_header $STMT capi3c-5.22 {main {} {}} {t1 {} {}} {a {} {}}
606 do_test capi3c-5.23 {
607 sqlite3_finalize $STMT
611 set ::ENC [execsql {pragma encoding}]
616 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]
629 check_data $STMT capi3c-6.3 {INTEGER} {1} {1.0} {1}
631 sqlite3_finalize $STMT
633 if {[clang_sanitize_address]==0} {
638 do_test capi3c-6.99-misuse {
645 # This procedure sets the value of the file-format in file 'test.db'
646 # to $newval. Also, the schema cookie is incremented.
648 proc 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]]
652 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
656 # This procedure returns the value of the file-format in file 'test.db'.
658 proc get_file_format {{fname test.db}} {
659 return [hexio_get_int [hexio_read $fname 44 4]]
662 if {![sqlite3 -has-codec]} {
663 # Test what happens when the library encounters a newer file format.
668 catch { sqlite3 db test.db }
670 SELECT * FROM sqlite_master;
672 } {1 {unsupported file format}}
676 if {![sqlite3 -has-codec]} {
677 # Now test that the library correctly handles bogus entries in the
678 # sqlite_master table (schema corruption).
680 forcedelete test.db test.db-journal
690 PRAGMA writable_schema=ON;
691 INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL);
696 catch { sqlite3 db test.db }
698 SELECT * FROM sqlite_master;
700 } {1 {malformed database schema (?)}}
702 # Build a 5-field row record. The first field is a string 'table', and
703 # subsequent fields are all NULL.
705 forcedelete test.db test.db-journal
709 PRAGMA writable_schema=ON;
710 INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL);
715 catch { sqlite3 db test.db }
717 SELECT * FROM sqlite_master;
719 } {1 {malformed database schema (?)}}
723 forcedelete test.db-journal
726 # Test the english language string equivalents for sqlite error codes
727 set code2english [list \
728 SQLITE_OK {not an error} \
729 SQLITE_ERROR {SQL logic error or missing database} \
730 SQLITE_PERM {access permission denied} \
731 SQLITE_ABORT {callback requested query abort} \
732 SQLITE_BUSY {database is locked} \
733 SQLITE_LOCKED {database table is locked} \
734 SQLITE_NOMEM {out of memory} \
735 SQLITE_READONLY {attempt to write a readonly database} \
736 SQLITE_INTERRUPT {interrupted} \
737 SQLITE_IOERR {disk I/O error} \
738 SQLITE_CORRUPT {database disk image is malformed} \
739 SQLITE_FULL {database or disk is full} \
740 SQLITE_CANTOPEN {unable to open database file} \
741 SQLITE_EMPTY {table contains no data} \
742 SQLITE_SCHEMA {database schema has changed} \
743 SQLITE_CONSTRAINT {constraint failed} \
744 SQLITE_MISMATCH {datatype mismatch} \
745 SQLITE_MISUSE {library routine called out of sequence} \
746 SQLITE_NOLFS {large file support is disabled} \
747 SQLITE_AUTH {authorization denied} \
748 SQLITE_FORMAT {auxiliary database format error} \
749 SQLITE_RANGE {bind or column index out of range} \
750 SQLITE_NOTADB {file is encrypted or is not a database} \
751 unknownerror {unknown error} \
755 foreach {code english} $code2english {
756 do_test capi3c-9.$test_number "sqlite3_test_errstr $code" $english
760 # Test the error message when a "real" out of memory occurs.
761 if { [permutation] != "nofaultsim" } {
763 do_test capi3c-10-1 {
765 set DB [sqlite3_connection_pointer db]
766 sqlite3_memdebug_fail 0
768 select * from sqlite_master;
770 } {1 {out of memory}}
771 do_test capi3c-10-2 {
775 do_test capi3c-10-3 {
776 utf8 [sqlite3_errmsg16 $::DB]
780 sqlite3_memdebug_fail -1
784 # The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK
785 # statement issued while there are still outstanding VMs that are part of
786 # the transaction fails.
788 set DB [sqlite3_connection_pointer db]
789 sqlite_register_test_function $DB func
790 do_test capi3c-11.1 {
793 CREATE TABLE t1(a, b);
794 INSERT INTO t1 VALUES(1, 'int');
795 INSERT INTO t1 VALUES(2, 'notatype');
798 do_test capi3c-11.1.1 {
799 sqlite3_get_autocommit $DB
801 do_test capi3c-11.2 {
802 set STMT [sqlite3_prepare_v2 $DB "SELECT func(b, a) FROM t1" -1 TAIL]
806 # As of 3.6.5 a COMMIT is OK during while a query is still running -
807 # as long as it is a read-only query and not an incremental BLOB write.
809 do_test capi3-11.3.1 {
814 do_test capi3-11.3.2 {
815 sqlite3_extended_errcode $DB
817 do_test capi3-11.3.3 {
818 sqlite3_get_autocommit $DB
820 do_test capi3-11.3.4 {
821 db eval {PRAGMA lock_status}
822 } {main shared temp closed}
824 do_test capi3c-11.4 {
827 do_test capi3c-11.5 {
828 sqlite3_finalize $STMT
830 do_test capi3c-11.6 {
834 } {0 {1 int 2 notatype}}
835 do_test capi3c-11.7 {
836 sqlite3_get_autocommit $DB
838 do_test capi3c-11.8 {
841 INSERT INTO t2 VALUES(1);
842 INSERT INTO t2 VALUES(2);
844 INSERT INTO t2 VALUES(3);
847 do_test capi3c-11.8.1 {
848 sqlite3_get_autocommit $DB
850 do_test capi3c-11.9 {
851 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
854 do_test capi3c-11.9.1 {
855 sqlite3_get_autocommit $DB
857 do_test capi3c-11.9.2 {
862 do_test capi3c-11.9.3 {
863 sqlite3_get_autocommit $DB
865 do_test capi3c-11.10 {
868 do_test capi3c-11.11 {
871 ifcapable !autoreset {
872 do_test capi3c-11.12armor {
877 do_test capi3c-11.12 {
882 do_test capi3c-11.13 {
883 sqlite3_finalize $STMT
885 do_test capi3c-11.14 {
890 do_test capi3c-11.14.1 {
891 sqlite3_get_autocommit $DB
893 do_test capi3c-11.15 {
897 } {1 {cannot rollback - no transaction is active}}
898 do_test capi3c-11.15.1 {
899 sqlite3_get_autocommit $DB
901 do_test capi3c-11.16 {
907 # Sanity check on the definition of 'outstanding VM'. This means any VM
908 # that has had sqlite3_step() called more recently than sqlite3_finalize() or
909 # sqlite3_reset(). So a VM that has just been prepared or reset does not
910 # count as an active VM.
911 do_test capi3c-11.17 {
916 do_test capi3c-11.18 {
917 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t1" -1 TAIL]
922 do_test capi3c-11.19 {
925 do_test capi3c-11.20 {
931 do_test capi3c-11.20 {
936 } {1 {cannot commit - no transaction is active}}
937 do_test capi3c-11.21 {
938 sqlite3_finalize $STMT
941 # The following tests - capi3c-12.* - check that its Ok to start a
942 # transaction while other VMs are active, and that its Ok to execute
943 # atomic updates in the same situation
945 do_test capi3c-12.1 {
946 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
949 do_test capi3c-12.2 {
951 INSERT INTO t1 VALUES(3, NULL);
954 do_test capi3c-12.3 {
956 INSERT INTO t2 VALUES(4);
959 do_test capi3c-12.4 {
962 INSERT INTO t1 VALUES(4, NULL);
965 do_test capi3c-12.5 {
968 do_test capi3c-12.5.1 {
971 do_test capi3c-12.6 {
974 do_test capi3c-12.7 {
975 sqlite3_finalize $STMT
977 do_test capi3c-12.8 {
984 # Test cases capi3c-13.* test the sqlite3_clear_bindings() and
985 # sqlite3_sleep APIs.
987 if {[llength [info commands sqlite3_clear_bindings]]>0} {
988 do_test capi3c-13.1 {
992 set STMT [sqlite3_prepare_v2 $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL]
995 do_test capi3c-13.2 {
997 sqlite3_bind_text $STMT 1 hello 5
998 sqlite3_bind_text $STMT 2 world 5
1001 do_test capi3c-13.3 {
1003 sqlite3_clear_bindings $STMT
1006 do_test capi3c-13-4 {
1007 sqlite3_finalize $STMT
1011 } {{} {} hello world {} {}}
1013 if {[llength [info commands sqlite3_sleep]]>0} {
1014 do_test capi3c-13-5 {
1015 set ms [sqlite3_sleep 80]
1016 expr {$ms==80 || $ms==1000}
1020 # Ticket #1219: Make sure binding APIs can handle a NULL pointer.
1022 do_test capi3c-14.1 {
1023 set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]
1027 # Ticket #1650: Honor the nBytes parameter to sqlite3_prepare.
1029 do_test capi3c-15.1 {
1030 set sql {SELECT * FROM t2}
1031 set nbytes [string length $sql]
1032 append sql { WHERE a==1}
1033 set STMT [sqlite3_prepare_v2 $DB $sql $nbytes TAIL]
1035 sqlite3_column_int $STMT 0
1037 do_test capi3c-15.2 {
1039 sqlite3_column_int $STMT 0
1041 do_test capi3c-15.3 {
1042 sqlite3_finalize $STMT
1045 # Make sure code is always generated even if an IF EXISTS or
1046 # IF NOT EXISTS clause is present that the table does not or
1047 # does exists. That way we will always have a prepared statement
1048 # to expire when the schema changes.
1050 do_test capi3c-16.1 {
1051 set sql {DROP TABLE IF EXISTS t3}
1052 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
1053 sqlite3_finalize $STMT
1056 do_test capi3c-16.2 {
1057 set sql {CREATE TABLE IF NOT EXISTS t1(x,y)}
1058 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
1059 sqlite3_finalize $STMT
1063 # But still we do not generate code if there is no SQL
1065 do_test capi3c-16.3 {
1066 set STMT [sqlite3_prepare_v2 $DB {} -1 TAIL]
1067 sqlite3_finalize $STMT
1070 do_test capi3c-16.4 {
1071 set STMT [sqlite3_prepare_v2 $DB {;} -1 TAIL]
1072 sqlite3_finalize $STMT
1078 do_test capi3c-17.1 {
1079 set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t2} -1 TAIL]
1082 do_test capi3c-17.2 {
1083 sqlite3_column_int $STMT 0
1085 do_test capi3c-17.3 {
1088 do_test capi3c-17.4 {
1090 db eval {CREATE INDEX i2 ON t2(a)}
1093 do_test capi3c-17.5 {
1094 sqlite3_column_int $STMT 0
1096 do_test capi3c-17.6 {
1099 do_test capi3c-17.7 {
1101 db eval {DROP INDEX i2}
1104 do_test capi3c-17.8 {
1105 sqlite3_column_int $STMT 0
1107 do_test capi3c-17.9 {
1110 do_test capi3c-17.10 {
1111 sqlite3_finalize $STMT
1112 set STMT [sqlite3_prepare_v2 $DB {SELECT b FROM t1 WHERE a=?} -1 TAIL]
1113 sqlite3_bind_int $STMT 1 2
1116 INSERT INTO t1 VALUES(1,'one');
1117 INSERT INTO t1 VALUES(2,'two');
1118 INSERT INTO t1 VALUES(3,'three');
1119 INSERT INTO t1 VALUES(4,'four');
1123 do_test capi3c-17.11 {
1124 sqlite3_column_text $STMT 0
1126 do_test capi3c-17.12 {
1129 do_test capi3c-17.13 {
1131 db eval {CREATE INDEX i1 ON t1(a)}
1134 do_test capi3c-17.14 {
1135 sqlite3_column_text $STMT 0
1137 do_test capi3c-17.15 {
1140 do_test capi3c-17.16 {
1142 db eval {DROP INDEX i1}
1145 do_test capi3c-17.17 {
1146 sqlite3_column_text $STMT 0
1148 do_test capi3c-17.18 {
1151 do_test capi3c-17.99 {
1152 sqlite3_finalize $STMT
1155 # On the mailing list it has been reported that finalizing after
1156 # an SQLITE_BUSY return leads to a segfault. Here we test that case.
1158 do_test capi3c-18.1 {
1160 set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t1} -1 TAIL]
1163 do_test capi3c-18.2 {
1164 sqlite3_column_int $STMT 0
1166 do_test capi3c-18.3 {
1168 db2 eval {BEGIN EXCLUSIVE}
1171 do_test capi3c-18.4 {
1172 sqlite3_finalize $STMT
1174 do_test capi3c-18.5 {
1179 # Ticket #2158. The sqlite3_step() will still return SQLITE_SCHEMA
1180 # if the database schema changes in a way that makes the statement
1183 do_test capi3c-19.1 {
1185 CREATE TABLE t3(x,y);
1186 INSERT INTO t3 VALUES(1,2);
1188 set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL]
1191 do_test capi3c-19.2 {
1192 sqlite3_column_int $STMT 0
1194 do_test capi3c-19.3 {
1197 do_test capi3c-19.4 {
1199 db eval {DROP TABLE t3}
1202 do_test capi3c-19.4.1 {
1204 } {no such table: t3}
1205 ifcapable deprecated {
1206 do_test capi3c-19.4.2 {
1207 sqlite3_expired $STMT
1210 do_test capi3c-19.4.3 {
1212 } {no such table: t3}
1213 ifcapable deprecated {
1214 do_test capi3c-19.4.4 {
1218 do_test capi3c-19.5 {
1221 CREATE TABLE t3(x,y);
1222 INSERT INTO t3 VALUES(1,2);
1226 ifcapable deprecated {
1227 do_test capi3c-19.5.2 {
1228 sqlite3_expired $STMT
1231 do_test capi3c-19.6 {
1232 sqlite3_column_int $STMT 1
1234 do_test capi3c-19.99 {
1235 sqlite3_finalize $STMT
1238 # Make sure a change in a separate database connection does not
1239 # cause an SQLITE_SCHEMA return.
1241 do_test capi3c-20.1 {
1242 set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL]
1244 db2 eval {CREATE TABLE t4(x)}
1247 do_test capi3c-20.2 {
1248 sqlite3_column_int $STMT 1
1250 do_test capi3c-20.3 {
1253 do_test capi3c-20.4 {
1255 sqlite3_finalize $STMT
1258 # Test that sqlite3_step() sets the database error code correctly.
1261 ifcapable progress {
1262 do_test capi3c-21.1 {
1263 set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL]
1264 db progress 5 "expr 1"
1266 } {SQLITE_INTERRUPT}
1267 do_test capi3c-21.2 {
1268 sqlite3_extended_errcode $DB
1269 } {SQLITE_INTERRUPT}
1270 do_test capi3c-21.3 {
1271 sqlite3_finalize $STMT
1272 } {SQLITE_INTERRUPT}
1273 do_test capi3c-21.4 {
1274 set STMT [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL]
1275 db progress 5 "expr 1"
1278 do_test capi3c-21.5 {
1281 do_test capi3c-21.6 {
1282 sqlite3_finalize $STMT
1283 } {SQLITE_INTERRUPT}
1284 do_test capi3c-21.7 {
1286 } {SQLITE_INTERRUPT}
1287 do_test capi3c-21.8 {
1288 sqlite3_extended_errcode $DB
1289 } {SQLITE_INTERRUPT}
1292 # Make sure sqlite3_result_error_code() returns the correct error code.
1295 do_test capi3c-22.1 {
1297 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',3)} -1 TAIL]
1300 sqlite3_finalize $STMT
1301 do_test capi3c-22.2 {
1302 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',4)} -1 TAIL]
1305 sqlite3_finalize $STMT
1306 do_test capi3c-22.3 {
1307 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',16)} -1 TAIL]
1310 sqlite3_finalize $STMT
1312 # For a multi-column result set where the same table column is repeated
1313 # in multiple columns of the output, verify that doing a UTF-8 to UTF-16
1314 # conversion (or vice versa) on one column does not change the value of
1318 do_test capi3c-23.1 {
1319 set STMT [sqlite3_prepare_v2 db {SELECT b,b,b,b FROM t1} -1 TAIL]
1322 do_test capi3c-23.2 {
1323 sqlite3_column_text16 $STMT 0
1324 sqlite3_column_text $STMT 1
1326 do_test capi3c-23.3 {
1327 sqlite3_column_text16 $STMT 2
1328 sqlite3_column_text $STMT 3
1330 sqlite3_finalize $STMT
1331 do_test capi3c-23.4 {
1332 set STMT [sqlite3_prepare_v2 db {SELECT b||'x',b,b,b FROM t1} -1 TAIL]
1335 do_test capi3c-23.5 {
1336 sqlite3_column_text16 $STMT 0
1337 sqlite3_column_text $STMT 1
1339 do_test capi3c-23.6 {
1340 sqlite3_column_text16 $STMT 2
1341 sqlite3_column_text $STMT 3
1343 sqlite3_finalize $STMT
1346 # Test decltype on some SELECT statements that contain sub-selects.
1348 proc decltype {zSql} {
1350 set STMT [sqlite3_prepare_v2 db $zSql -1 TAIL]
1351 for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
1352 lappend ret [sqlite3_column_decltype $STMT $i]
1354 sqlite3_finalize $STMT
1357 do_test capi3c-24.1 {
1358 execsql { CREATE TABLE t5(a INTEGER, b STRING, c DATETIME) }
1359 decltype {SELECT * FROM t5}
1360 } {INTEGER STRING DATETIME}
1361 do_test capi3c-24.2 {
1362 decltype {SELECT (SELECT c) FROM t5}
1364 do_test capi3c-24.3 {
1365 decltype {SELECT (SELECT * FROM (SELECT c)) FROM t5}
1367 do_test capi3c-24.4 {
1368 decltype {SELECT * FROM (SELECT * FROM t5 ORDER BY c LIMIT 1) ORDER BY b}
1369 } {INTEGER STRING DATETIME}
1370 do_test capi3c-24.5 {
1372 SELECT (SELECT x FROM (SELECT c AS x))
1373 FROM (SELECT * FROM t5 ORDER BY c LIMIT 1) ORDER BY b
1376 do_test capi3c-24.3 {
1377 decltype {SELECT (SELECT x FROM (SELECT t5.a AS x)) FROM t5}
1381 # Further tests of sqlite3_column_decltype():
1383 do_execsql_test 25.0 {
1384 CREATE TABLE t11(a VARCHAR(10), b INTEGER);
1385 CREATE TABLE t12(a VARCHAR(15), b FLOAT);
1389 1 "SELECT * FROM t11 UNION ALL SELECT * FROM t12"
1390 2 "SELECT * FROM t11 UNION SELECT * FROM t12"
1391 3 "SELECT * FROM t11 EXCEPT SELECT * FROM t12"
1392 4 "SELECT * FROM t11 INTERSECT SELECT * FROM t12"
1394 5 "SELECT * FROM t11 UNION ALL SELECT * FROM t12 ORDER BY 1"
1395 6 "SELECT * FROM t11 UNION SELECT * FROM t12 ORDER BY 1"
1396 7 "SELECT * FROM t11 EXCEPT SELECT * FROM t12 ORDER BY 1"
1397 8 "SELECT * FROM t11 INTERSECT SELECT * FROM t12 ORDER BY 1"
1399 do_test 25.$tn { decltype $sql } {VARCHAR(10) INTEGER}