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 } {bad parameter or other API misuse}
181 do_test capi3c-3.6.3-misuse {
182 utf8 [sqlite3_errmsg16 $db2]
183 } {bad parameter or other API misuse}
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
689 sqlite3_db_config db DEFENSIVE 0
691 PRAGMA writable_schema=ON;
692 INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL);
697 catch { sqlite3 db test.db }
699 SELECT * FROM sqlite_master;
701 } {1 {malformed database schema (?)}}
703 # Build a 5-field row record. The first field is a string 'table', and
704 # subsequent fields are all NULL.
706 forcedelete test.db test.db-journal
708 sqlite3_db_config db DEFENSIVE 0
711 PRAGMA writable_schema=ON;
712 INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL);
717 catch { sqlite3 db test.db }
719 SELECT * FROM sqlite_master;
721 } {1 {malformed database schema (?)}}
725 forcedelete test.db-journal
728 # Test the english language string equivalents for sqlite error codes
729 set code2english [list \
730 SQLITE_OK {not an error} \
731 SQLITE_ERROR {SQL logic error} \
732 SQLITE_PERM {access permission denied} \
733 SQLITE_ABORT {query aborted} \
734 SQLITE_BUSY {database is locked} \
735 SQLITE_LOCKED {database table is locked} \
736 SQLITE_NOMEM {out of memory} \
737 SQLITE_READONLY {attempt to write a readonly database} \
738 SQLITE_INTERRUPT {interrupted} \
739 SQLITE_IOERR {disk I/O error} \
740 SQLITE_CORRUPT {database disk image is malformed} \
741 SQLITE_FULL {database or disk is full} \
742 SQLITE_CANTOPEN {unable to open database file} \
743 SQLITE_EMPTY {unknown error} \
744 SQLITE_SCHEMA {database schema has changed} \
745 SQLITE_CONSTRAINT {constraint failed} \
746 SQLITE_MISMATCH {datatype mismatch} \
747 SQLITE_MISUSE {bad parameter or other API misuse} \
748 SQLITE_AUTH {authorization denied} \
749 SQLITE_RANGE {column index out of range} \
750 SQLITE_NOTADB {file 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" } {
762 do_test capi3c-10-1 {
764 set DB [sqlite3_connection_pointer db]
765 sqlite3_memdebug_fail 0
767 select * from sqlite_master;
769 } {1 {out of memory}}
770 do_test capi3c-10-2 {
774 do_test capi3c-10-3 {
775 utf8 [sqlite3_errmsg16 $::DB]
779 sqlite3_memdebug_fail -1
782 # The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK
783 # statement issued while there are still outstanding VMs that are part of
784 # the transaction fails.
786 set DB [sqlite3_connection_pointer db]
787 sqlite_register_test_function $DB func
788 do_test capi3c-11.1 {
791 CREATE TABLE t1(a, b);
792 INSERT INTO t1 VALUES(1, 'int');
793 INSERT INTO t1 VALUES(2, 'notatype');
796 do_test capi3c-11.1.1 {
797 sqlite3_get_autocommit $DB
799 do_test capi3c-11.2 {
800 set STMT [sqlite3_prepare_v2 $DB "SELECT func(b, a) FROM t1" -1 TAIL]
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.
807 do_test capi3-11.3.1 {
812 do_test capi3-11.3.2 {
813 sqlite3_extended_errcode $DB
815 do_test capi3-11.3.3 {
816 sqlite3_get_autocommit $DB
818 do_test capi3-11.3.4 {
819 db eval {PRAGMA lock_status}
820 } {main shared temp closed}
822 do_test capi3c-11.4 {
825 do_test capi3c-11.5 {
826 sqlite3_finalize $STMT
828 do_test capi3c-11.6 {
832 } {0 {1 int 2 notatype}}
833 do_test capi3c-11.7 {
834 sqlite3_get_autocommit $DB
836 do_test capi3c-11.8 {
839 INSERT INTO t2 VALUES(1);
840 INSERT INTO t2 VALUES(2);
842 INSERT INTO t2 VALUES(3);
845 do_test capi3c-11.8.1 {
846 sqlite3_get_autocommit $DB
848 do_test capi3c-11.9 {
849 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
852 do_test capi3c-11.9.1 {
853 sqlite3_get_autocommit $DB
855 do_test capi3c-11.9.2 {
860 do_test capi3c-11.9.3 {
861 sqlite3_get_autocommit $DB
863 do_test capi3c-11.10 {
866 do_test capi3c-11.11 {
869 ifcapable !autoreset {
870 do_test capi3c-11.12armor {
875 do_test capi3c-11.12 {
880 do_test capi3c-11.13 {
881 sqlite3_finalize $STMT
883 do_test capi3c-11.14 {
888 do_test capi3c-11.14.1 {
889 sqlite3_get_autocommit $DB
891 do_test capi3c-11.15 {
895 } {1 {cannot rollback - no transaction is active}}
896 do_test capi3c-11.15.1 {
897 sqlite3_get_autocommit $DB
899 do_test capi3c-11.16 {
905 # Sanity check on the definition of 'outstanding VM'. This means any VM
906 # that has had sqlite3_step() called more recently than sqlite3_finalize() or
907 # sqlite3_reset(). So a VM that has just been prepared or reset does not
908 # count as an active VM.
909 do_test capi3c-11.17 {
914 do_test capi3c-11.18 {
915 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t1" -1 TAIL]
920 do_test capi3c-11.19 {
923 do_test capi3c-11.20 {
929 do_test capi3c-11.20 {
934 } {1 {cannot commit - no transaction is active}}
935 do_test capi3c-11.21 {
936 sqlite3_finalize $STMT
939 # The following tests - capi3c-12.* - check that its Ok to start a
940 # transaction while other VMs are active, and that its Ok to execute
941 # atomic updates in the same situation
943 do_test capi3c-12.1 {
944 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
947 do_test capi3c-12.2 {
949 INSERT INTO t1 VALUES(3, NULL);
952 do_test capi3c-12.3 {
954 INSERT INTO t2 VALUES(4);
957 do_test capi3c-12.4 {
960 INSERT INTO t1 VALUES(4, NULL);
963 do_test capi3c-12.5 {
966 do_test capi3c-12.5.1 {
969 do_test capi3c-12.6 {
972 do_test capi3c-12.7 {
973 sqlite3_finalize $STMT
975 do_test capi3c-12.8 {
982 # Test cases capi3c-13.* test the sqlite3_clear_bindings() and
983 # sqlite3_sleep APIs.
985 if {[llength [info commands sqlite3_clear_bindings]]>0} {
986 do_test capi3c-13.1 {
990 set STMT [sqlite3_prepare_v2 $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL]
993 do_test capi3c-13.2 {
995 sqlite3_bind_text $STMT 1 hello 5
996 sqlite3_bind_text $STMT 2 world 5
999 do_test capi3c-13.3 {
1001 sqlite3_clear_bindings $STMT
1004 do_test capi3c-13-4 {
1005 sqlite3_finalize $STMT
1009 } {{} {} hello world {} {}}
1011 if {[llength [info commands sqlite3_sleep]]>0} {
1012 do_test capi3c-13-5 {
1013 set ms [sqlite3_sleep 80]
1014 expr {$ms==80 || $ms==1000}
1018 # Ticket #1219: Make sure binding APIs can handle a NULL pointer.
1020 do_test capi3c-14.1 {
1021 set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]
1025 # Ticket #1650: Honor the nBytes parameter to sqlite3_prepare.
1027 do_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]
1033 sqlite3_column_int $STMT 0
1035 do_test capi3c-15.2 {
1037 sqlite3_column_int $STMT 0
1039 do_test capi3c-15.3 {
1040 sqlite3_finalize $STMT
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 or
1045 # does exists. That way we will always have a prepared statement
1046 # to expire when the schema changes.
1048 do_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 $STMT
1054 do_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 $STMT
1061 # But still we do not generate code if there is no SQL
1063 do_test capi3c-16.3 {
1064 set STMT [sqlite3_prepare_v2 $DB {} -1 TAIL]
1065 sqlite3_finalize $STMT
1068 do_test capi3c-16.4 {
1069 set STMT [sqlite3_prepare_v2 $DB {;} -1 TAIL]
1070 sqlite3_finalize $STMT
1076 do_test capi3c-17.1 {
1077 set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t2} -1 TAIL]
1080 do_test capi3c-17.2 {
1081 sqlite3_column_int $STMT 0
1083 do_test capi3c-17.3 {
1086 do_test capi3c-17.4 {
1088 db eval {CREATE INDEX i2 ON t2(a)}
1091 do_test capi3c-17.5 {
1092 sqlite3_column_int $STMT 0
1094 do_test capi3c-17.6 {
1097 do_test capi3c-17.7 {
1099 db eval {DROP INDEX i2}
1102 do_test capi3c-17.8 {
1103 sqlite3_column_int $STMT 0
1105 do_test capi3c-17.9 {
1108 do_test capi3c-17.10 {
1109 sqlite3_finalize $STMT
1110 set STMT [sqlite3_prepare_v2 $DB {SELECT b FROM t1 WHERE a=?} -1 TAIL]
1111 sqlite3_bind_int $STMT 1 2
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');
1121 do_test capi3c-17.11 {
1122 sqlite3_column_text $STMT 0
1124 do_test capi3c-17.12 {
1127 do_test capi3c-17.13 {
1129 db eval {CREATE INDEX i1 ON t1(a)}
1132 do_test capi3c-17.14 {
1133 sqlite3_column_text $STMT 0
1135 do_test capi3c-17.15 {
1138 do_test capi3c-17.16 {
1140 db eval {DROP INDEX i1}
1143 do_test capi3c-17.17 {
1144 sqlite3_column_text $STMT 0
1146 do_test capi3c-17.18 {
1149 do_test capi3c-17.99 {
1150 sqlite3_finalize $STMT
1153 # On the mailing list it has been reported that finalizing after
1154 # an SQLITE_BUSY return leads to a segfault. Here we test that case.
1156 do_test capi3c-18.1 {
1158 set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t1} -1 TAIL]
1161 do_test capi3c-18.2 {
1162 sqlite3_column_int $STMT 0
1164 do_test capi3c-18.3 {
1166 db2 eval {BEGIN EXCLUSIVE}
1169 do_test capi3c-18.4 {
1170 sqlite3_finalize $STMT
1172 do_test capi3c-18.5 {
1177 # Ticket #2158. The sqlite3_step() will still return SQLITE_SCHEMA
1178 # if the database schema changes in a way that makes the statement
1181 do_test capi3c-19.1 {
1183 CREATE TABLE t3(x,y);
1184 INSERT INTO t3 VALUES(1,2);
1186 set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL]
1189 do_test capi3c-19.2 {
1190 sqlite3_column_int $STMT 0
1192 do_test capi3c-19.3 {
1195 do_test capi3c-19.4 {
1197 db eval {DROP TABLE t3}
1200 do_test capi3c-19.4.1 {
1202 } {no such table: t3}
1203 ifcapable deprecated {
1204 do_test capi3c-19.4.2 {
1205 sqlite3_expired $STMT
1208 do_test capi3c-19.4.3 {
1210 } {no such table: t3}
1211 ifcapable deprecated {
1212 do_test capi3c-19.4.4 {
1216 do_test capi3c-19.5 {
1219 CREATE TABLE t3(x,y);
1220 INSERT INTO t3 VALUES(1,2);
1224 ifcapable deprecated {
1225 do_test capi3c-19.5.2 {
1226 sqlite3_expired $STMT
1229 do_test capi3c-19.6 {
1230 sqlite3_column_int $STMT 1
1232 do_test capi3c-19.99 {
1233 sqlite3_finalize $STMT
1236 # Make sure a change in a separate database connection does not
1237 # cause an SQLITE_SCHEMA return.
1239 do_test capi3c-20.1 {
1240 set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL]
1242 db2 eval {CREATE TABLE t4(x)}
1245 do_test capi3c-20.2 {
1246 sqlite3_column_int $STMT 1
1248 do_test capi3c-20.3 {
1251 do_test capi3c-20.4 {
1253 sqlite3_finalize $STMT
1256 # Test that sqlite3_step() sets the database error code correctly.
1259 ifcapable progress {
1260 do_test capi3c-21.1 {
1261 set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL]
1262 db progress 5 "expr 1"
1264 } {SQLITE_INTERRUPT}
1265 do_test capi3c-21.2 {
1266 sqlite3_extended_errcode $DB
1267 } {SQLITE_INTERRUPT}
1268 do_test capi3c-21.3 {
1269 sqlite3_finalize $STMT
1270 } {SQLITE_INTERRUPT}
1271 do_test capi3c-21.4 {
1273 set STMT [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL]
1274 db progress 5 "expr 1"
1277 do_test capi3c-21.5 {
1280 do_test capi3c-21.6 {
1281 sqlite3_finalize $STMT
1282 } {SQLITE_INTERRUPT}
1283 do_test capi3c-21.7 {
1285 } {SQLITE_INTERRUPT}
1286 do_test capi3c-21.8 {
1287 sqlite3_extended_errcode $DB
1288 } {SQLITE_INTERRUPT}
1291 # Make sure sqlite3_result_error_code() returns the correct error code.
1294 do_test capi3c-22.1 {
1296 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',3)} -1 TAIL]
1299 sqlite3_finalize $STMT
1300 do_test capi3c-22.2 {
1301 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',4)} -1 TAIL]
1304 sqlite3_finalize $STMT
1305 do_test capi3c-22.3 {
1306 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',16)} -1 TAIL]
1309 sqlite3_finalize $STMT
1311 # For a multi-column result set where the same table column is repeated
1312 # in multiple columns of the output, verify that doing a UTF-8 to UTF-16
1313 # conversion (or vice versa) on one column does not change the value of
1317 do_test capi3c-23.1 {
1318 set STMT [sqlite3_prepare_v2 db {SELECT b,b,b,b FROM t1} -1 TAIL]
1321 do_test capi3c-23.2 {
1322 sqlite3_column_text16 $STMT 0
1323 sqlite3_column_text $STMT 1
1325 do_test capi3c-23.3 {
1326 sqlite3_column_text16 $STMT 2
1327 sqlite3_column_text $STMT 3
1329 sqlite3_finalize $STMT
1330 do_test capi3c-23.4 {
1331 set STMT [sqlite3_prepare_v2 db {SELECT b||'x',b,b,b FROM t1} -1 TAIL]
1334 do_test capi3c-23.5 {
1335 sqlite3_column_text16 $STMT 0
1336 sqlite3_column_text $STMT 1
1338 do_test capi3c-23.6 {
1339 sqlite3_column_text16 $STMT 2
1340 sqlite3_column_text $STMT 3
1342 sqlite3_finalize $STMT
1345 # Test decltype on some SELECT statements that contain sub-selects.
1347 proc decltype {zSql} {
1349 set STMT [sqlite3_prepare_v2 db $zSql -1 TAIL]
1350 for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
1351 lappend ret [sqlite3_column_decltype $STMT $i]
1353 sqlite3_finalize $STMT
1356 do_test capi3c-24.1 {
1357 execsql { CREATE TABLE t5(a INTEGER, b STRING, c DATETIME) }
1358 decltype {SELECT * FROM t5}
1359 } {INTEGER STRING DATETIME}
1360 do_test capi3c-24.2 {
1361 decltype {SELECT (SELECT c) FROM t5}
1363 do_test capi3c-24.3 {
1364 decltype {SELECT (SELECT * FROM (SELECT c)) FROM t5}
1366 do_test capi3c-24.4 {
1367 decltype {SELECT * FROM (SELECT * FROM t5 ORDER BY c LIMIT 1) ORDER BY b}
1368 } {INTEGER STRING DATETIME}
1369 do_test capi3c-24.5 {
1371 SELECT (SELECT x FROM (SELECT c AS x))
1372 FROM (SELECT * FROM t5 ORDER BY c LIMIT 1) ORDER BY b
1375 do_test capi3c-24.3 {
1376 decltype {SELECT (SELECT x FROM (SELECT t5.a AS x)) FROM t5}
1380 # Further tests of sqlite3_column_decltype():
1382 do_execsql_test 25.0 {
1383 CREATE TABLE t11(a VARCHAR(10), b INTEGER);
1384 CREATE TABLE t12(a VARCHAR(15), b FLOAT);
1388 1 "SELECT * FROM t11 UNION ALL SELECT * FROM t12"
1389 2 "SELECT * FROM t11 UNION SELECT * FROM t12"
1390 3 "SELECT * FROM t11 EXCEPT SELECT * FROM t12"
1391 4 "SELECT * FROM t11 INTERSECT SELECT * FROM t12"
1393 5 "SELECT * FROM t11 UNION ALL SELECT * FROM t12 ORDER BY 1"
1394 6 "SELECT * FROM t11 UNION SELECT * FROM t12 ORDER BY 1"
1395 7 "SELECT * FROM t11 EXCEPT SELECT * FROM t12 ORDER BY 1"
1396 8 "SELECT * FROM t11 INTERSECT SELECT * FROM t12 ORDER BY 1"
1398 do_test 25.$tn { decltype $sql } {VARCHAR(10) INTEGER}