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
22 # Do not use a codec for tests in this file, as the database file is
23 # manipulated directly using tcl scripts (using the [hexio_write] command).
27 # Return the UTF-16 representation of the supplied UTF-8 string $str.
28 # If $nt is true, append two 0x00 bytes as a nul terminator.
29 proc utf16 {str {nt 1}} {
30 set r [encoding convertto unicode $str]
37 # Return the UTF-8 representation of the supplied UTF-16 string $str.
39 # If $str ends in two 0x00 0x00 bytes, knock these off before
40 # converting to UTF-8 using TCL.
41 binary scan $str \c* vals
42 if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
43 set str [binary format \c* [lrange $vals 0 end-2]]
46 set r [encoding convertfrom unicode $str]
50 # These tests complement those in capi2.test. They are organized
53 # capi3c-1.*: Test sqlite3_prepare_v2
54 # capi3c-2.*: Test sqlite3_prepare16_v2
55 # capi3c-3.*: Test sqlite3_open
56 # capi3c-4.*: Test sqlite3_open16
57 # capi3c-5.*: Test the various sqlite3_result_* APIs
58 # capi3c-6.*: Test that sqlite3_close fails if there are outstanding VMs.
61 set DB [sqlite3_connection_pointer db]
64 sqlite3_get_autocommit $DB
67 set STMT [sqlite3_prepare_v2 $DB {SELECT name FROM sqlite_master} -1 TAIL]
68 sqlite3_finalize $STMT
71 do_test capi3c-1.2.1 {
74 do_test capi3c-1.2.2 {
75 sqlite3_extended_errcode $DB
81 set sql {SELECT name FROM sqlite_master;SELECT 10}
82 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
83 sqlite3_finalize $STMT
87 set sql {SELECT namex FROM sqlite_master}
89 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
92 do_test capi3c-1.6.1 {
95 do_test capi3c-1.6.2 {
96 sqlite3_extended_errcode $DB
100 } {no such column: namex}
105 set sql16 [utf16 {SELECT name FROM sqlite_master}]
106 set STMT [sqlite3_prepare16_v2 $DB $sql16 -1 ::TAIL]
107 sqlite3_finalize $STMT
111 set sql [utf16 {SELECT name FROM sqlite_master;SELECT 10}]
112 set STMT [sqlite3_prepare16_v2 $DB $sql -1 TAIL]
113 sqlite3_finalize $STMT
117 set sql [utf16 {SELECT namex FROM sqlite_master}]
119 set STMT [sqlite3_prepare16_v2 $DB $sql -1 TAIL]
122 do_test capi3c-2.4.1 {
125 do_test capi3c-2.4.2 {
126 sqlite3_extended_errcode $DB
130 } {no such column: namex}
132 ifcapable schema_pragmas {
134 execsql {CREATE TABLE tablename(x)}
135 set sql16 [utf16 {PRAGMA table_info("TableName")}]
136 set STMT [sqlite3_prepare16_v2 $DB $sql16 -1 TAIL]
143 sqlite3_finalize $STMT
149 # rename sqlite3_open sqlite3_open_old
150 # proc sqlite3_open {fname options} {sqlite3_open_new $fname $options}
153 set db2 [sqlite3_open test.db {}]
156 # FIX ME: Should test the db handle works.
162 set db2 [sqlite3_open /bogus/path/test.db {}]
168 } {unable to open database file}
172 do_test capi3c-3.6.1-misuse {
175 do_test capi3c-3.6.2-misuse {
177 } {library routine called out of sequence}
179 do_test capi3c-3.6.3-misuse {
180 utf8 [sqlite3_errmsg16 $db2]
181 } {library routine called out of sequence}
184 # rename sqlite3_open ""
185 # rename sqlite3_open_old sqlite3_open
189 set db2 [sqlite3_open16 [utf16 test.db] {}]
192 # FIX ME: Should test the db handle works.
198 set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}]
203 utf8 [sqlite3_errmsg16 $db2]
204 } {unable to open database file}
210 # This proc is used to test the following API calls:
212 # sqlite3_column_count
213 # sqlite3_column_name
214 # sqlite3_column_name16
215 # sqlite3_column_decltype
216 # sqlite3_column_decltype16
218 # $STMT is a compiled SQL statement. $test is a prefix
219 # to use for test names within this proc. $names is a list
220 # of the column names that should be returned by $STMT.
221 # $decltypes is a list of column declaration types for $STMT.
225 # set STMT [sqlite3_prepare_v2 "SELECT 1, 2, 2;" -1 DUMMY]
226 # check_header test1.1 {1 2 3} {"" "" ""}
228 proc check_header {STMT test names decltypes} {
230 # Use the return value of sqlite3_column_count() to build
231 # a list of column indexes. i.e. If sqlite3_column_count
232 # is 3, build the list {0 1 2}.
234 set ::numcols [sqlite3_column_count $STMT]
235 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}
237 # Column names in UTF-8
240 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]}
244 # Column names in UTF-16
249 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]
255 # Column names in UTF-8
258 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]}
262 # Column names in UTF-16
267 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]
273 # Column names in UTF-8
276 foreach i $idxlist {lappend cnamelist [sqlite3_column_decltype $STMT $i]}
280 # Column declaration types in UTF-16
285 lappend cnamelist [utf8 [sqlite3_column_decltype16 $STMT $i]]
292 # Test some out of range conditions:
296 [sqlite3_column_name $STMT -1] \
297 [sqlite3_column_name16 $STMT -1] \
298 [sqlite3_column_decltype $STMT -1] \
299 [sqlite3_column_decltype16 $STMT -1] \
300 [sqlite3_column_name $STMT $numcols] \
301 [sqlite3_column_name16 $STMT $numcols] \
302 [sqlite3_column_decltype $STMT $numcols] \
303 [sqlite3_column_decltype16 $STMT $numcols]
304 } {{} {} {} {} {} {} {} {}}
308 # This proc is used to test the following API calls:
310 # sqlite3_column_origin_name
311 # sqlite3_column_origin_name16
312 # sqlite3_column_table_name
313 # sqlite3_column_table_name16
314 # sqlite3_column_database_name
315 # sqlite3_column_database_name16
317 # $STMT is a compiled SQL statement. $test is a prefix
318 # to use for test names within this proc. $names is a list
319 # of the column names that should be returned by $STMT.
320 # $decltypes is a list of column declaration types for $STMT.
324 # set STMT [sqlite3_prepare_v2 "SELECT 1, 2, 2;" -1 DUMMY]
325 # check_header test1.1 {1 2 3} {"" "" ""}
327 proc check_origin_header {STMT test dbs tables cols} {
328 # If sqlite3_column_origin_name() and friends are not compiled into
329 # this build, this proc is a no-op.
330 ifcapable columnmetadata {
332 # Use the return value of sqlite3_column_count() to build
333 # a list of column indexes. i.e. If sqlite3_column_count
334 # is 3, build the list {0 1 2}.
336 set ::numcols [sqlite3_column_count $STMT]
337 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}
339 # Database names in UTF-8
343 lappend cnamelist [sqlite3_column_database_name $STMT $i]
348 # Database names in UTF-16
353 lappend cnamelist [utf8 [sqlite3_column_database_name16 $STMT $i]]
359 # Table names in UTF-8
363 lappend cnamelist [sqlite3_column_table_name $STMT $i]
368 # Table names in UTF-16
373 lappend cnamelist [utf8 [sqlite3_column_table_name16 $STMT $i]]
379 # Origin names in UTF-8
383 lappend cnamelist [sqlite3_column_origin_name $STMT $i]
388 # Origin declaration types in UTF-16
393 lappend cnamelist [utf8 [sqlite3_column_origin_name16 $STMT $i]]
401 # This proc is used to test the following APIs:
404 # sqlite3_column_type
406 # sqlite3_column_text
407 # sqlite3_column_text16
408 # sqlite3_column_double
410 # $STMT is a compiled SQL statement for which the previous call
411 # to sqlite3_step returned SQLITE_ROW. $test is a prefix to use
412 # for test names within this proc. $types is a list of the
413 # manifest types for the current row. $ints, $doubles and $strings
414 # are lists of the integer, real and string representations of
415 # the values in the current row.
419 # set STMT [sqlite3_prepare_v2 "SELECT 'hello', 1.1, NULL" -1 DUMMY]
421 # check_data test1.2 {TEXT REAL NULL} {0 1 0} {0 1.1 0} {hello 1.1 {}}
423 proc check_data {STMT test types ints doubles strings} {
425 # Use the return value of sqlite3_column_count() to build
426 # a list of column indexes. i.e. If sqlite3_column_count
427 # is 3, build the list {0 1 2}.
429 set numcols [sqlite3_data_count $STMT]
430 for {set i 0} {$i < $numcols} {incr i} {lappend ::idxlist $i}
435 foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}
442 foreach i $idxlist {lappend ints [sqlite3_column_int64 $STMT $i]}
448 foreach i $::idxlist {
449 lappend lens [string length [lindex $strings $i]]
455 lappend bytes [sqlite3_column_bytes $STMT $i]
463 foreach i $::idxlist {
464 lappend lens [expr 2 * [string length [lindex $strings $i]]]
470 lappend bytes [sqlite3_column_bytes16 $STMT $i]
479 foreach i $idxlist {lappend utf8 [sqlite3_column_blob $STMT $i]}
486 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}
493 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}
501 foreach i $idxlist {lappend utf8 [utf8 [sqlite3_column_text16 $STMT $i]]}
509 foreach i $idxlist {lappend ints [sqlite3_column_int $STMT $i]}
516 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}
523 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}
530 foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}
534 # Test that an out of range request returns the equivalent of NULL
536 sqlite3_column_int $STMT -1
539 sqlite3_column_text $STMT -1
544 ifcapable !floatingpoint {
551 CREATE TABLE t1(a VARINT, b BLOB, c VARCHAR(16));
552 INSERT INTO t1 VALUES(1, 2, 3);
553 INSERT INTO t1 VALUES('one', 'two', NULL);
554 INSERT INTO t1 VALUES(1.2, 1.3, 1.4);
556 set sql "SELECT * FROM t1"
557 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
558 sqlite3_column_count $STMT
561 check_header $STMT capi3c-5.1 {a b c} {VARINT BLOB VARCHAR(16)}
562 check_origin_header $STMT capi3c-5.1 {main main main} {t1 t1 t1} {a b c}
567 check_header $STMT capi3c-5.3 {a b c} {VARINT BLOB VARCHAR(16)}
568 check_origin_header $STMT capi3c-5.3 {main main main} {t1 t1 t1} {a b c}
569 check_data $STMT capi3c-5.4 {INTEGER INTEGER TEXT} {1 2 3} {1.0 2.0 3.0} {1 2 3}
575 check_header $STMT capi3c-5.6 {a b c} {VARINT BLOB VARCHAR(16)}
576 check_origin_header $STMT capi3c-5.6 {main main main} {t1 t1 t1} {a b c}
577 check_data $STMT capi3c-5.7 {TEXT TEXT NULL} {0 0 0} {0.0 0.0 0.0} {one two {}}
583 check_header $STMT capi3c-5.9 {a b c} {VARINT BLOB VARCHAR(16)}
584 check_origin_header $STMT capi3c-5.9 {main main main} {t1 t1 t1} {a b c}
585 check_data $STMT capi3c-5.10 {FLOAT FLOAT TEXT} {1 1 1} {1.2 1.3 1.4} {1.2 1.3 1.4}
587 do_test capi3c-5.11 {
591 do_test capi3c-5.12 {
592 sqlite3_finalize $STMT
595 do_test capi3c-5.20 {
596 set sql "SELECT a, sum(b), max(c) FROM t1 GROUP BY a"
597 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
598 sqlite3_column_count $STMT
601 check_header $STMT capi3c-5.21 {a sum(b) max(c)} {VARINT {} {}}
602 check_origin_header $STMT capi3c-5.22 {main {} {}} {t1 {} {}} {a {} {}}
603 do_test capi3c-5.23 {
604 sqlite3_finalize $STMT
608 set ::ENC [execsql {pragma encoding}]
613 set DB [sqlite3_connection_pointer db]
614 if {[sqlite3 -has-codec]==0} { sqlite3_key $DB xyzzy }
615 set sql {SELECT a FROM t1 order by rowid}
616 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
626 check_data $STMT capi3c-6.3 {INTEGER} {1} {1.0} {1}
628 sqlite3_finalize $STMT
634 do_test capi3c-6.99-misuse {
638 # This procedure sets the value of the file-format in file 'test.db'
639 # to $newval. Also, the schema cookie is incremented.
641 proc set_file_format {newval} {
642 hexio_write test.db 44 [hexio_render_int32 $newval]
643 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
645 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
649 # This procedure returns the value of the file-format in file 'test.db'.
651 proc get_file_format {{fname test.db}} {
652 return [hexio_get_int [hexio_read $fname 44 4]]
655 if {![sqlite3 -has-codec]} {
656 # Test what happens when the library encounters a newer file format.
661 catch { sqlite3 db test.db }
663 SELECT * FROM sqlite_master;
665 } {1 {unsupported file format}}
669 if {![sqlite3 -has-codec]} {
670 # Now test that the library correctly handles bogus entries in the
671 # sqlite_master table (schema corruption).
673 file delete -force test.db test.db-journal
683 PRAGMA writable_schema=ON;
684 INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL);
689 catch { sqlite3 db test.db }
691 SELECT * FROM sqlite_master;
693 } {1 {malformed database schema (?)}}
695 # Build a 5-field row record. The first field is a string 'table', and
696 # subsequent fields are all NULL.
698 file delete -force test.db test.db-journal
702 PRAGMA writable_schema=ON;
703 INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL);
708 catch { sqlite3 db test.db }
710 SELECT * FROM sqlite_master;
712 } {1 {malformed database schema (?)}}
715 file delete -force test.db
716 file delete -force test.db-journal
719 # Test the english language string equivalents for sqlite error codes
720 set code2english [list \
721 SQLITE_OK {not an error} \
722 SQLITE_ERROR {SQL logic error or missing database} \
723 SQLITE_PERM {access permission denied} \
724 SQLITE_ABORT {callback requested query abort} \
725 SQLITE_BUSY {database is locked} \
726 SQLITE_LOCKED {database table is locked} \
727 SQLITE_NOMEM {out of memory} \
728 SQLITE_READONLY {attempt to write a readonly database} \
729 SQLITE_INTERRUPT {interrupted} \
730 SQLITE_IOERR {disk I/O error} \
731 SQLITE_CORRUPT {database disk image is malformed} \
732 SQLITE_FULL {database or disk is full} \
733 SQLITE_CANTOPEN {unable to open database file} \
734 SQLITE_EMPTY {table contains no data} \
735 SQLITE_SCHEMA {database schema has changed} \
736 SQLITE_CONSTRAINT {constraint failed} \
737 SQLITE_MISMATCH {datatype mismatch} \
738 SQLITE_MISUSE {library routine called out of sequence} \
739 SQLITE_NOLFS {large file support is disabled} \
740 SQLITE_AUTH {authorization denied} \
741 SQLITE_FORMAT {auxiliary database format error} \
742 SQLITE_RANGE {bind or column index out of range} \
743 SQLITE_NOTADB {file is encrypted or is not a database} \
744 unknownerror {unknown error} \
748 foreach {code english} $code2english {
749 do_test capi3c-9.$test_number "sqlite3_test_errstr $code" $english
753 # Test the error message when a "real" out of memory occurs.
755 do_test capi3c-10-1 {
757 set DB [sqlite3_connection_pointer db]
758 sqlite3_memdebug_fail 0
760 select * from sqlite_master;
762 } {1 {out of memory}}
763 do_test capi3c-10-2 {
767 do_test capi3c-10-3 {
768 utf8 [sqlite3_errmsg16 $::DB]
772 sqlite3_memdebug_fail -1
775 # The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK
776 # statement issued while there are still outstanding VMs that are part of
777 # the transaction fails.
779 set DB [sqlite3_connection_pointer db]
780 sqlite_register_test_function $DB func
781 do_test capi3c-11.1 {
784 CREATE TABLE t1(a, b);
785 INSERT INTO t1 VALUES(1, 'int');
786 INSERT INTO t1 VALUES(2, 'notatype');
789 do_test capi3c-11.1.1 {
790 sqlite3_get_autocommit $DB
792 do_test capi3c-11.2 {
793 set STMT [sqlite3_prepare_v2 $DB "SELECT func(b, a) FROM t1" -1 TAIL]
797 # As of 3.6.5 a COMMIT is OK during while a query is still running -
798 # as long as it is a read-only query and not an incremental BLOB write.
800 do_test capi3-11.3.1 {
805 do_test capi3-11.3.2 {
806 sqlite3_extended_errcode $DB
808 do_test capi3-11.3.3 {
809 sqlite3_get_autocommit $DB
811 do_test capi3-11.3.4 {
812 db eval {PRAGMA lock_status}
813 } {main shared temp closed}
815 do_test capi3c-11.4 {
818 do_test capi3c-11.5 {
819 sqlite3_finalize $STMT
821 do_test capi3c-11.6 {
825 } {0 {1 int 2 notatype}}
826 do_test capi3c-11.7 {
827 sqlite3_get_autocommit $DB
829 do_test capi3c-11.8 {
832 INSERT INTO t2 VALUES(1);
833 INSERT INTO t2 VALUES(2);
835 INSERT INTO t2 VALUES(3);
838 do_test capi3c-11.8.1 {
839 sqlite3_get_autocommit $DB
841 do_test capi3c-11.9 {
842 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
845 do_test capi3c-11.9.1 {
846 sqlite3_get_autocommit $DB
848 do_test capi3c-11.9.2 {
852 } {1 {cannot rollback transaction - SQL statements in progress}}
853 do_test capi3c-11.9.3 {
854 sqlite3_get_autocommit $DB
856 do_test capi3c-11.10 {
859 do_test capi3c-11.11 {
862 do_test capi3c-11.12 {
865 do_test capi3c-11.13 {
866 sqlite3_finalize $STMT
868 do_test capi3c-11.14 {
873 do_test capi3c-11.14.1 {
874 sqlite3_get_autocommit $DB
876 do_test capi3c-11.15 {
881 do_test capi3c-11.15.1 {
882 sqlite3_get_autocommit $DB
884 do_test capi3c-11.16 {
890 # Sanity check on the definition of 'outstanding VM'. This means any VM
891 # that has had sqlite3_step() called more recently than sqlite3_finalize() or
892 # sqlite3_reset(). So a VM that has just been prepared or reset does not
893 # count as an active VM.
894 do_test capi3c-11.17 {
899 do_test capi3c-11.18 {
900 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t1" -1 TAIL]
905 do_test capi3c-11.19 {
908 do_test capi3c-11.20 {
914 do_test capi3c-11.20 {
919 } {1 {cannot commit - no transaction is active}}
920 do_test capi3c-11.21 {
921 sqlite3_finalize $STMT
924 # The following tests - capi3c-12.* - check that its Ok to start a
925 # transaction while other VMs are active, and that its Ok to execute
926 # atomic updates in the same situation
928 do_test capi3c-12.1 {
929 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
932 do_test capi3c-12.2 {
934 INSERT INTO t1 VALUES(3, NULL);
937 do_test capi3c-12.3 {
939 INSERT INTO t2 VALUES(4);
942 do_test capi3c-12.4 {
945 INSERT INTO t1 VALUES(4, NULL);
948 do_test capi3c-12.5 {
951 do_test capi3c-12.5.1 {
954 do_test capi3c-12.6 {
957 do_test capi3c-12.7 {
958 sqlite3_finalize $STMT
960 do_test capi3c-12.8 {
967 # Test cases capi3c-13.* test the sqlite3_clear_bindings() and
968 # sqlite3_sleep APIs.
970 if {[llength [info commands sqlite3_clear_bindings]]>0} {
971 do_test capi3c-13.1 {
975 set STMT [sqlite3_prepare_v2 $DB "INSERT INTO t1 VALUES(?, ?)" -1 TAIL]
978 do_test capi3c-13.2 {
980 sqlite3_bind_text $STMT 1 hello 5
981 sqlite3_bind_text $STMT 2 world 5
984 do_test capi3c-13.3 {
986 sqlite3_clear_bindings $STMT
989 do_test capi3c-13-4 {
990 sqlite3_finalize $STMT
994 } {{} {} hello world {} {}}
996 if {[llength [info commands sqlite3_sleep]]>0} {
997 do_test capi3c-13-5 {
998 set ms [sqlite3_sleep 80]
999 expr {$ms==80 || $ms==1000}
1003 # Ticket #1219: Make sure binding APIs can handle a NULL pointer.
1005 do_test capi3c-14.1 {
1006 set rc [catch {sqlite3_bind_text 0 1 hello 5} msg]
1010 # Ticket #1650: Honor the nBytes parameter to sqlite3_prepare.
1012 do_test capi3c-15.1 {
1013 set sql {SELECT * FROM t2}
1014 set nbytes [string length $sql]
1015 append sql { WHERE a==1}
1016 set STMT [sqlite3_prepare_v2 $DB $sql $nbytes TAIL]
1018 sqlite3_column_int $STMT 0
1020 do_test capi3c-15.2 {
1022 sqlite3_column_int $STMT 0
1024 do_test capi3c-15.3 {
1025 sqlite3_finalize $STMT
1028 # Make sure code is always generated even if an IF EXISTS or
1029 # IF NOT EXISTS clause is present that the table does not or
1030 # does exists. That way we will always have a prepared statement
1031 # to expire when the schema changes.
1033 do_test capi3c-16.1 {
1034 set sql {DROP TABLE IF EXISTS t3}
1035 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
1036 sqlite3_finalize $STMT
1039 do_test capi3c-16.2 {
1040 set sql {CREATE TABLE IF NOT EXISTS t1(x,y)}
1041 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
1042 sqlite3_finalize $STMT
1046 # But still we do not generate code if there is no SQL
1048 do_test capi3c-16.3 {
1049 set STMT [sqlite3_prepare_v2 $DB {} -1 TAIL]
1050 sqlite3_finalize $STMT
1053 do_test capi3c-16.4 {
1054 set STMT [sqlite3_prepare_v2 $DB {;} -1 TAIL]
1055 sqlite3_finalize $STMT
1061 do_test capi3c-17.1 {
1062 set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t2} -1 TAIL]
1065 do_test capi3c-17.2 {
1066 sqlite3_column_int $STMT 0
1068 do_test capi3c-17.3 {
1071 do_test capi3c-17.4 {
1073 db eval {CREATE INDEX i2 ON t2(a)}
1076 do_test capi3c-17.5 {
1077 sqlite3_column_int $STMT 0
1079 do_test capi3c-17.6 {
1082 do_test capi3c-17.7 {
1084 db eval {DROP INDEX i2}
1087 do_test capi3c-17.8 {
1088 sqlite3_column_int $STMT 0
1090 do_test capi3c-17.9 {
1093 do_test capi3c-17.10 {
1094 sqlite3_finalize $STMT
1095 set STMT [sqlite3_prepare_v2 $DB {SELECT b FROM t1 WHERE a=?} -1 TAIL]
1096 sqlite3_bind_int $STMT 1 2
1099 INSERT INTO t1 VALUES(1,'one');
1100 INSERT INTO t1 VALUES(2,'two');
1101 INSERT INTO t1 VALUES(3,'three');
1102 INSERT INTO t1 VALUES(4,'four');
1106 do_test capi3c-17.11 {
1107 sqlite3_column_text $STMT 0
1109 do_test capi3c-17.12 {
1112 do_test capi3c-17.13 {
1114 db eval {CREATE INDEX i1 ON t1(a)}
1117 do_test capi3c-17.14 {
1118 sqlite3_column_text $STMT 0
1120 do_test capi3c-17.15 {
1123 do_test capi3c-17.16 {
1125 db eval {DROP INDEX i1}
1128 do_test capi3c-17.17 {
1129 sqlite3_column_text $STMT 0
1131 do_test capi3c-17.18 {
1134 do_test capi3c-17.99 {
1135 sqlite3_finalize $STMT
1138 # On the mailing list it has been reported that finalizing after
1139 # an SQLITE_BUSY return leads to a segfault. Here we test that case.
1141 do_test capi3c-18.1 {
1143 set STMT [sqlite3_prepare_v2 $DB {SELECT max(a) FROM t1} -1 TAIL]
1146 do_test capi3c-18.2 {
1147 sqlite3_column_int $STMT 0
1149 do_test capi3c-18.3 {
1151 db2 eval {BEGIN EXCLUSIVE}
1154 do_test capi3c-18.4 {
1155 sqlite3_finalize $STMT
1157 do_test capi3c-18.5 {
1162 # Ticket #2158. The sqlite3_step() will still return SQLITE_SCHEMA
1163 # if the database schema changes in a way that makes the statement
1166 do_test capi3c-19.1 {
1168 CREATE TABLE t3(x,y);
1169 INSERT INTO t3 VALUES(1,2);
1171 set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL]
1174 do_test capi3c-19.2 {
1175 sqlite3_column_int $STMT 0
1177 do_test capi3c-19.3 {
1180 do_test capi3c-19.4 {
1182 db eval {DROP TABLE t3}
1185 do_test capi3c-19.4.1 {
1187 } {no such table: t3}
1188 ifcapable deprecated {
1189 do_test capi3c-19.4.2 {
1190 sqlite3_expired $STMT
1193 do_test capi3c-19.4.3 {
1195 } {no such table: t3}
1196 ifcapable deprecated {
1197 do_test capi3c-19.4.4 {
1201 do_test capi3c-19.5 {
1204 CREATE TABLE t3(x,y);
1205 INSERT INTO t3 VALUES(1,2);
1209 ifcapable deprecated {
1210 do_test capi3c-19.5.2 {
1211 sqlite3_expired $STMT
1214 do_test capi3c-19.6 {
1215 sqlite3_column_int $STMT 1
1217 do_test capi3c-19.99 {
1218 sqlite3_finalize $STMT
1221 # Make sure a change in a separate database connection does not
1222 # cause an SQLITE_SCHEMA return.
1224 do_test capi3c-20.1 {
1225 set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL]
1227 db2 eval {CREATE TABLE t4(x)}
1230 do_test capi3c-20.2 {
1231 sqlite3_column_int $STMT 1
1233 do_test capi3c-20.3 {
1236 do_test capi3c-20.4 {
1238 sqlite3_finalize $STMT
1241 # Test that sqlite3_step() sets the database error code correctly.
1244 ifcapable progress {
1245 do_test capi3c-21.1 {
1246 set STMT [sqlite3_prepare_v2 $DB {SELECT * FROM t3} -1 TAIL]
1247 db progress 5 "expr 1"
1249 } {SQLITE_INTERRUPT}
1250 do_test capi3c-21.2 {
1251 sqlite3_extended_errcode $DB
1252 } {SQLITE_INTERRUPT}
1253 do_test capi3c-21.3 {
1254 sqlite3_finalize $STMT
1255 } {SQLITE_INTERRUPT}
1256 do_test capi3c-21.4 {
1257 set STMT [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL]
1258 db progress 5 "expr 1"
1261 do_test capi3c-21.5 {
1264 do_test capi3c-21.6 {
1265 sqlite3_finalize $STMT
1266 } {SQLITE_INTERRUPT}
1267 do_test capi3c-21.7 {
1269 } {SQLITE_INTERRUPT}
1270 do_test capi3c-21.8 {
1271 sqlite3_extended_errcode $DB
1272 } {SQLITE_INTERRUPT}
1275 # Make sure sqlite3_result_error_code() returns the correct error code.
1278 do_test capi3c-22.1 {
1280 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',3)} -1 TAIL]
1283 sqlite3_finalize $STMT
1284 do_test capi3c-22.2 {
1285 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',4)} -1 TAIL]
1288 sqlite3_finalize $STMT
1289 do_test capi3c-22.3 {
1290 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',16)} -1 TAIL]
1293 sqlite3_finalize $STMT
1295 # For a multi-column result set where the same table column is repeated
1296 # in multiple columns of the output, verify that doing a UTF-8 to UTF-16
1297 # conversion (or vice versa) on one column does not change the value of
1301 do_test capi3c-23.1 {
1302 set STMT [sqlite3_prepare_v2 db {SELECT b,b,b,b FROM t1} -1 TAIL]
1305 do_test capi3c-23.2 {
1306 sqlite3_column_text16 $STMT 0
1307 sqlite3_column_text $STMT 1
1309 do_test capi3c-23.3 {
1310 sqlite3_column_text16 $STMT 2
1311 sqlite3_column_text $STMT 3
1313 sqlite3_finalize $STMT
1314 do_test capi3c-23.4 {
1315 set STMT [sqlite3_prepare_v2 db {SELECT b||'x',b,b,b FROM t1} -1 TAIL]
1318 do_test capi3c-23.5 {
1319 sqlite3_column_text16 $STMT 0
1320 sqlite3_column_text $STMT 1
1322 do_test capi3c-23.6 {
1323 sqlite3_column_text16 $STMT 2
1324 sqlite3_column_text $STMT 3
1326 sqlite3_finalize $STMT
1329 # Test decltype on some SELECT statements that contain sub-selects.
1331 proc decltype {zSql} {
1333 set STMT [sqlite3_prepare_v2 db $zSql -1 TAIL]
1334 for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
1335 lappend ret [sqlite3_column_decltype $STMT $i]
1337 sqlite3_finalize $STMT
1340 do_test capi3c-24.1 {
1341 execsql { CREATE TABLE t5(a INTEGER, b STRING, c DATETIME) }
1342 decltype {SELECT * FROM t5}
1343 } {INTEGER STRING DATETIME}
1344 do_test capi3c-24.2 {
1345 decltype {SELECT (SELECT c) FROM t5}
1347 do_test capi3c-24.3 {
1348 decltype {SELECT (SELECT * FROM (SELECT c)) FROM t5}
1350 do_test capi3c-24.4 {
1351 decltype {SELECT * FROM (SELECT * FROM t5 ORDER BY c LIMIT 1) ORDER BY b}
1352 } {INTEGER STRING DATETIME}
1353 do_test capi3c-24.5 {
1355 SELECT (SELECT x FROM (SELECT c AS x))
1356 FROM (SELECT * FROM t5 ORDER BY c LIMIT 1) ORDER BY b
1359 do_test capi3c-24.3 {
1360 decltype {SELECT (SELECT x FROM (SELECT t5.a AS x)) FROM t5}