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 if {[clang_sanitize_address]==0} {
173 do_test capi3c-3.6.1-misuse {
176 do_test capi3c-3.6.2-misuse {
178 } {library routine called out of sequence}
180 do_test capi3c-3.6.3-misuse {
181 utf8 [sqlite3_errmsg16 $db2]
182 } {library routine called out of sequence}
186 # rename sqlite3_open ""
187 # rename sqlite3_open_old sqlite3_open
191 set db2 [sqlite3_open16 [utf16 test.db] {}]
194 # FIX ME: Should test the db handle works.
200 set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}]
205 utf8 [sqlite3_errmsg16 $db2]
206 } {unable to open database file}
212 # This proc is used to test the following API calls:
214 # sqlite3_column_count
215 # sqlite3_column_name
216 # sqlite3_column_name16
217 # sqlite3_column_decltype
218 # sqlite3_column_decltype16
220 # $STMT is a compiled SQL statement. $test is a prefix
221 # to use for test names within this proc. $names is a list
222 # of the column names that should be returned by $STMT.
223 # $decltypes is a list of column declaration types for $STMT.
227 # set STMT [sqlite3_prepare_v2 "SELECT 1, 2, 2;" -1 DUMMY]
228 # check_header test1.1 {1 2 3} {"" "" ""}
230 proc check_header {STMT test names decltypes} {
232 # Use the return value of sqlite3_column_count() to build
233 # a list of column indexes. i.e. If sqlite3_column_count
234 # is 3, build the list {0 1 2}.
236 set ::numcols [sqlite3_column_count $STMT]
237 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}
239 # Column names in UTF-8
242 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]}
246 # Column names in UTF-16
251 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]
257 # Column names in UTF-8
260 foreach i $idxlist {lappend cnamelist [sqlite3_column_name $STMT $i]}
264 # Column names in UTF-16
269 lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]
275 # Column names in UTF-8
278 foreach i $idxlist {lappend cnamelist [sqlite3_column_decltype $STMT $i]}
282 # Column declaration types in UTF-16
287 lappend cnamelist [utf8 [sqlite3_column_decltype16 $STMT $i]]
294 # Test some out of range conditions:
298 [sqlite3_column_name $STMT -1] \
299 [sqlite3_column_name16 $STMT -1] \
300 [sqlite3_column_decltype $STMT -1] \
301 [sqlite3_column_decltype16 $STMT -1] \
302 [sqlite3_column_name $STMT $numcols] \
303 [sqlite3_column_name16 $STMT $numcols] \
304 [sqlite3_column_decltype $STMT $numcols] \
305 [sqlite3_column_decltype16 $STMT $numcols]
306 } {{} {} {} {} {} {} {} {}}
310 # This proc is used to test the following API calls:
312 # sqlite3_column_origin_name
313 # sqlite3_column_origin_name16
314 # sqlite3_column_table_name
315 # sqlite3_column_table_name16
316 # sqlite3_column_database_name
317 # sqlite3_column_database_name16
319 # $STMT is a compiled SQL statement. $test is a prefix
320 # to use for test names within this proc. $names is a list
321 # of the column names that should be returned by $STMT.
322 # $decltypes is a list of column declaration types for $STMT.
326 # set STMT [sqlite3_prepare_v2 "SELECT 1, 2, 2;" -1 DUMMY]
327 # check_header test1.1 {1 2 3} {"" "" ""}
329 proc check_origin_header {STMT test dbs tables cols} {
330 # If sqlite3_column_origin_name() and friends are not compiled into
331 # this build, this proc is a no-op.
332 ifcapable columnmetadata {
334 # Use the return value of sqlite3_column_count() to build
335 # a list of column indexes. i.e. If sqlite3_column_count
336 # is 3, build the list {0 1 2}.
338 set ::numcols [sqlite3_column_count $STMT]
339 for {set i 0} {$i < $::numcols} {incr i} {lappend ::idxlist $i}
341 # Database names in UTF-8
345 lappend cnamelist [sqlite3_column_database_name $STMT $i]
350 # Database names in UTF-16
355 lappend cnamelist [utf8 [sqlite3_column_database_name16 $STMT $i]]
361 # Table names in UTF-8
365 lappend cnamelist [sqlite3_column_table_name $STMT $i]
370 # Table names in UTF-16
375 lappend cnamelist [utf8 [sqlite3_column_table_name16 $STMT $i]]
381 # Origin names in UTF-8
385 lappend cnamelist [sqlite3_column_origin_name $STMT $i]
390 # Origin declaration types in UTF-16
395 lappend cnamelist [utf8 [sqlite3_column_origin_name16 $STMT $i]]
403 # This proc is used to test the following APIs:
406 # sqlite3_column_type
408 # sqlite3_column_text
409 # sqlite3_column_text16
410 # sqlite3_column_double
412 # $STMT is a compiled SQL statement for which the previous call
413 # to sqlite3_step returned SQLITE_ROW. $test is a prefix to use
414 # for test names within this proc. $types is a list of the
415 # manifest types for the current row. $ints, $doubles and $strings
416 # are lists of the integer, real and string representations of
417 # the values in the current row.
421 # set STMT [sqlite3_prepare_v2 "SELECT 'hello', 1.1, NULL" -1 DUMMY]
423 # check_data test1.2 {TEXT REAL NULL} {0 1 0} {0 1.1 0} {hello 1.1 {}}
425 proc check_data {STMT test types ints doubles strings} {
427 # Use the return value of sqlite3_column_count() to build
428 # a list of column indexes. i.e. If sqlite3_column_count
429 # is 3, build the list {0 1 2}.
431 set numcols [sqlite3_data_count $STMT]
432 for {set i 0} {$i < $numcols} {incr i} {lappend ::idxlist $i}
437 foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}
444 foreach i $idxlist {lappend ints [sqlite3_column_int64 $STMT $i]}
450 foreach i $::idxlist {
451 lappend lens [string length [lindex $strings $i]]
457 lappend bytes [sqlite3_column_bytes $STMT $i]
465 foreach i $::idxlist {
466 lappend lens [expr 2 * [string length [lindex $strings $i]]]
472 lappend bytes [sqlite3_column_bytes16 $STMT $i]
481 foreach i $idxlist {lappend utf8 [sqlite3_column_blob $STMT $i]}
488 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}
495 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}
503 foreach i $idxlist {lappend utf8 [utf8 [sqlite3_column_text16 $STMT $i]]}
511 foreach i $idxlist {lappend ints [sqlite3_column_int $STMT $i]}
518 foreach i $idxlist {lappend utf8 [sqlite3_column_double $STMT $i]}
525 foreach i $idxlist {lappend utf8 [sqlite3_column_text $STMT $i]}
532 foreach i $idxlist {lappend types [sqlite3_column_type $STMT $i]}
536 # Test that an out of range request returns the equivalent of NULL
538 sqlite3_column_int $STMT -1
541 sqlite3_column_text $STMT -1
546 ifcapable !floatingpoint {
553 CREATE TABLE t1(a VARINT, b BLOB, c VARCHAR(16));
554 INSERT INTO t1 VALUES(1, 2, 3);
555 INSERT INTO t1 VALUES('one', 'two', NULL);
556 INSERT INTO t1 VALUES(1.2, 1.3, 1.4);
558 set sql "SELECT * FROM t1"
559 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
560 sqlite3_column_count $STMT
563 check_header $STMT capi3c-5.1 {a b c} {VARINT BLOB VARCHAR(16)}
564 check_origin_header $STMT capi3c-5.1 {main main main} {t1 t1 t1} {a b c}
569 check_header $STMT capi3c-5.3 {a b c} {VARINT BLOB VARCHAR(16)}
570 check_origin_header $STMT capi3c-5.3 {main main main} {t1 t1 t1} {a b c}
571 check_data $STMT capi3c-5.4 {INTEGER INTEGER TEXT} {1 2 3} {1.0 2.0 3.0} {1 2 3}
577 check_header $STMT capi3c-5.6 {a b c} {VARINT BLOB VARCHAR(16)}
578 check_origin_header $STMT capi3c-5.6 {main main main} {t1 t1 t1} {a b c}
579 check_data $STMT capi3c-5.7 {TEXT TEXT NULL} {0 0 0} {0.0 0.0 0.0} {one two {}}
585 check_header $STMT capi3c-5.9 {a b c} {VARINT BLOB VARCHAR(16)}
586 check_origin_header $STMT capi3c-5.9 {main main main} {t1 t1 t1} {a b c}
587 check_data $STMT capi3c-5.10 {FLOAT FLOAT TEXT} {1 1 1} {1.2 1.3 1.4} {1.2 1.3 1.4}
589 do_test capi3c-5.11 {
593 do_test capi3c-5.12 {
594 sqlite3_finalize $STMT
597 do_test capi3c-5.20 {
598 set sql "SELECT a, sum(b), max(c) FROM t1 GROUP BY a"
599 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
600 sqlite3_column_count $STMT
603 check_header $STMT capi3c-5.21 {a sum(b) max(c)} {VARINT {} {}}
604 check_origin_header $STMT capi3c-5.22 {main {} {}} {t1 {} {}} {a {} {}}
605 do_test capi3c-5.23 {
606 sqlite3_finalize $STMT
610 set ::ENC [execsql {pragma encoding}]
615 set DB [sqlite3_connection_pointer db]
616 if {[sqlite3 -has-codec]==0} { sqlite3_key $DB xyzzy }
617 set sql {SELECT a FROM t1 order by rowid}
618 set STMT [sqlite3_prepare_v2 $DB $sql -1 TAIL]
628 check_data $STMT capi3c-6.3 {INTEGER} {1} {1.0} {1}
630 sqlite3_finalize $STMT
632 if {[clang_sanitize_address]==0} {
637 do_test capi3c-6.99-misuse {
644 # This procedure sets the value of the file-format in file 'test.db'
645 # to $newval. Also, the schema cookie is incremented.
647 proc set_file_format {newval} {
648 hexio_write test.db 44 [hexio_render_int32 $newval]
649 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
651 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
655 # This procedure returns the value of the file-format in file 'test.db'.
657 proc get_file_format {{fname test.db}} {
658 return [hexio_get_int [hexio_read $fname 44 4]]
661 if {![sqlite3 -has-codec]} {
662 # Test what happens when the library encounters a newer file format.
667 catch { sqlite3 db test.db }
669 SELECT * FROM sqlite_master;
671 } {1 {unsupported file format}}
675 if {![sqlite3 -has-codec]} {
676 # Now test that the library correctly handles bogus entries in the
677 # sqlite_master table (schema corruption).
679 forcedelete test.db test.db-journal
689 PRAGMA writable_schema=ON;
690 INSERT INTO sqlite_master VALUES(NULL,NULL,NULL,NULL,NULL);
695 catch { sqlite3 db test.db }
697 SELECT * FROM sqlite_master;
699 } {1 {malformed database schema (?)}}
701 # Build a 5-field row record. The first field is a string 'table', and
702 # subsequent fields are all NULL.
704 forcedelete test.db test.db-journal
708 PRAGMA writable_schema=ON;
709 INSERT INTO sqlite_master VALUES('table',NULL,NULL,NULL,NULL);
714 catch { sqlite3 db test.db }
716 SELECT * FROM sqlite_master;
718 } {1 {malformed database schema (?)}}
722 forcedelete test.db-journal
725 # Test the english language string equivalents for sqlite error codes
726 set code2english [list \
727 SQLITE_OK {not an error} \
728 SQLITE_ERROR {SQL logic error or missing database} \
729 SQLITE_PERM {access permission denied} \
730 SQLITE_ABORT {callback requested query abort} \
731 SQLITE_BUSY {database is locked} \
732 SQLITE_LOCKED {database table is locked} \
733 SQLITE_NOMEM {out of memory} \
734 SQLITE_READONLY {attempt to write a readonly database} \
735 SQLITE_INTERRUPT {interrupted} \
736 SQLITE_IOERR {disk I/O error} \
737 SQLITE_CORRUPT {database disk image is malformed} \
738 SQLITE_FULL {database or disk is full} \
739 SQLITE_CANTOPEN {unable to open database file} \
740 SQLITE_EMPTY {table contains no data} \
741 SQLITE_SCHEMA {database schema has changed} \
742 SQLITE_CONSTRAINT {constraint failed} \
743 SQLITE_MISMATCH {datatype mismatch} \
744 SQLITE_MISUSE {library routine called out of sequence} \
745 SQLITE_NOLFS {large file support is disabled} \
746 SQLITE_AUTH {authorization denied} \
747 SQLITE_FORMAT {auxiliary database format error} \
748 SQLITE_RANGE {bind or column index out of range} \
749 SQLITE_NOTADB {file is encrypted or is not a database} \
750 unknownerror {unknown error} \
754 foreach {code english} $code2english {
755 do_test capi3c-9.$test_number "sqlite3_test_errstr $code" $english
759 # Test the error message when a "real" out of memory occurs.
760 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
783 # The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK
784 # statement issued while there are still outstanding VMs that are part of
785 # the transaction fails.
787 set DB [sqlite3_connection_pointer db]
788 sqlite_register_test_function $DB func
789 do_test capi3c-11.1 {
792 CREATE TABLE t1(a, b);
793 INSERT INTO t1 VALUES(1, 'int');
794 INSERT INTO t1 VALUES(2, 'notatype');
797 do_test capi3c-11.1.1 {
798 sqlite3_get_autocommit $DB
800 do_test capi3c-11.2 {
801 set STMT [sqlite3_prepare_v2 $DB "SELECT func(b, a) FROM t1" -1 TAIL]
805 # As of 3.6.5 a COMMIT is OK during while a query is still running -
806 # as long as it is a read-only query and not an incremental BLOB write.
808 do_test capi3-11.3.1 {
813 do_test capi3-11.3.2 {
814 sqlite3_extended_errcode $DB
816 do_test capi3-11.3.3 {
817 sqlite3_get_autocommit $DB
819 do_test capi3-11.3.4 {
820 db eval {PRAGMA lock_status}
821 } {main shared temp closed}
823 do_test capi3c-11.4 {
826 do_test capi3c-11.5 {
827 sqlite3_finalize $STMT
829 do_test capi3c-11.6 {
833 } {0 {1 int 2 notatype}}
834 do_test capi3c-11.7 {
835 sqlite3_get_autocommit $DB
837 do_test capi3c-11.8 {
840 INSERT INTO t2 VALUES(1);
841 INSERT INTO t2 VALUES(2);
843 INSERT INTO t2 VALUES(3);
846 do_test capi3c-11.8.1 {
847 sqlite3_get_autocommit $DB
849 do_test capi3c-11.9 {
850 set STMT [sqlite3_prepare_v2 $DB "SELECT a FROM t2" -1 TAIL]
853 do_test capi3c-11.9.1 {
854 sqlite3_get_autocommit $DB
856 do_test capi3c-11.9.2 {
861 do_test capi3c-11.9.3 {
862 sqlite3_get_autocommit $DB
864 do_test capi3c-11.10 {
867 ifcapable !autoreset {
868 # If SQLITE_OMIT_AUTORESET is defined, then the statement must be
869 # reset() before it can be passed to step() again.
870 do_test capi3-11.11a { sqlite3_step $STMT } {SQLITE_MISUSE}
871 do_test capi3-11.11b { sqlite3_reset $STMT } {SQLITE_ABORT}
873 do_test capi3c-11.11 {
876 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 {
1272 set STMT [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL]
1273 db progress 5 "expr 1"
1276 do_test capi3c-21.5 {
1279 do_test capi3c-21.6 {
1280 sqlite3_finalize $STMT
1281 } {SQLITE_INTERRUPT}
1282 do_test capi3c-21.7 {
1284 } {SQLITE_INTERRUPT}
1285 do_test capi3c-21.8 {
1286 sqlite3_extended_errcode $DB
1287 } {SQLITE_INTERRUPT}
1290 # Make sure sqlite3_result_error_code() returns the correct error code.
1293 do_test capi3c-22.1 {
1295 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',3)} -1 TAIL]
1298 sqlite3_finalize $STMT
1299 do_test capi3c-22.2 {
1300 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',4)} -1 TAIL]
1303 sqlite3_finalize $STMT
1304 do_test capi3c-22.3 {
1305 set STMT [sqlite3_prepare_v2 db {SELECT test_error('the message',16)} -1 TAIL]
1308 sqlite3_finalize $STMT
1310 # For a multi-column result set where the same table column is repeated
1311 # in multiple columns of the output, verify that doing a UTF-8 to UTF-16
1312 # conversion (or vice versa) on one column does not change the value of
1316 do_test capi3c-23.1 {
1317 set STMT [sqlite3_prepare_v2 db {SELECT b,b,b,b FROM t1} -1 TAIL]
1320 do_test capi3c-23.2 {
1321 sqlite3_column_text16 $STMT 0
1322 sqlite3_column_text $STMT 1
1324 do_test capi3c-23.3 {
1325 sqlite3_column_text16 $STMT 2
1326 sqlite3_column_text $STMT 3
1328 sqlite3_finalize $STMT
1329 do_test capi3c-23.4 {
1330 set STMT [sqlite3_prepare_v2 db {SELECT b||'x',b,b,b FROM t1} -1 TAIL]
1333 do_test capi3c-23.5 {
1334 sqlite3_column_text16 $STMT 0
1335 sqlite3_column_text $STMT 1
1337 do_test capi3c-23.6 {
1338 sqlite3_column_text16 $STMT 2
1339 sqlite3_column_text $STMT 3
1341 sqlite3_finalize $STMT
1344 # Test decltype on some SELECT statements that contain sub-selects.
1346 proc decltype {zSql} {
1348 set STMT [sqlite3_prepare_v2 db $zSql -1 TAIL]
1349 for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
1350 lappend ret [sqlite3_column_decltype $STMT $i]
1352 sqlite3_finalize $STMT
1355 do_test capi3c-24.1 {
1356 execsql { CREATE TABLE t5(a INTEGER, b STRING, c DATETIME) }
1357 decltype {SELECT * FROM t5}
1358 } {INTEGER STRING DATETIME}
1359 do_test capi3c-24.2 {
1360 decltype {SELECT (SELECT c) FROM t5}
1362 do_test capi3c-24.3 {
1363 decltype {SELECT (SELECT * FROM (SELECT c)) FROM t5}
1365 do_test capi3c-24.4 {
1366 decltype {SELECT * FROM (SELECT * FROM t5 ORDER BY c LIMIT 1) ORDER BY b}
1367 } {INTEGER STRING DATETIME}
1368 do_test capi3c-24.5 {
1370 SELECT (SELECT x FROM (SELECT c AS x))
1371 FROM (SELECT * FROM t5 ORDER BY c LIMIT 1) ORDER BY b
1374 do_test capi3c-24.3 {
1375 decltype {SELECT (SELECT x FROM (SELECT t5.a AS x)) FROM t5}