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 file is devoted to testing the sqlite3_next_stmt and
14 # sqlite3_stmt_readonly and sqlite3_stmt_busy interfaces.
16 # $Id: capi3d.test,v 1.2 2008/07/14 15:11:20 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Create N prepared statements against database connection db
23 # and return a list of all the generated prepared statements.
25 proc make_prepared_statements {N} {
27 for {set i 0} {$i<$N} {incr i} {
28 set sql "SELECT $i FROM sqlite_master WHERE name LIKE '%$i%'"
30 set s [sqlite3_prepare_v2 db $sql -1 notused]
34 set sql [encoding convertto unicode $sql]\x00\x00
35 set s [sqlite3_prepare16 db $sql -1 notused]
37 set s [sqlite3_prepare db $sql -1 notused]
41 set s [sqlite3_prepare db $sql -1 notused]
50 # Scramble the $inlist into a random order.
52 proc scramble {inlist} {
55 lappend y [list [expr {rand()}] $x]
60 lappend outlist [lindex $x 1]
65 # Database initially has no prepared statements.
69 sqlite3_next_stmt db 0
72 # Run the following tests for between 1 and 100 prepared statements.
74 for {set i 1} {$i<=100} {incr i} {
75 set stmtlist [make_prepared_statements $i]
76 do_test capi3d-1.2.$i.1 {
77 set p [sqlite3_next_stmt db 0]
81 set p [sqlite3_next_stmt db $p]
85 do_test capi3-1.2.$i.2 {
86 foreach p [scramble $::stmtlist] {
89 sqlite3_next_stmt db 0
93 # Tests for the is-read-only interface.
95 proc test_is_readonly {testname sql truth} {
96 do_test $testname [format {
97 set DB [sqlite3_connection_pointer db]
98 set STMT [sqlite3_prepare $DB {%s} -1 TAIL]
99 set rc [sqlite3_stmt_readonly $STMT]
100 sqlite3_finalize $STMT
105 test_is_readonly capi3d-2.1 {SELECT * FROM sqlite_master} 1
106 test_is_readonly capi3d-2.2 {CREATE TABLE t1(x)} 0
107 db eval {CREATE TABLE t1(x)}
108 test_is_readonly capi3d-2.3 {INSERT INTO t1 VALUES(5)} 0
109 test_is_readonly capi3d-2.4 {UPDATE t1 SET x=x+1 WHERE x<0} 0
110 test_is_readonly capi3d-2.5 {SELECT * FROM t1} 1
112 test_is_readonly capi3d-2.6 {PRAGMA journal_mode=WAL} 0
113 test_is_readonly capi3d-2.7 {PRAGMA wal_checkpoint} 0
115 test_is_readonly capi3d-2.8 {PRAGMA application_id=1234} 0
116 test_is_readonly capi3d-2.9 {VACUUM} 0
117 test_is_readonly capi3d-2.10 {PRAGMA integrity_check} 1
119 sqlite3_stmt_readonly 0
122 # Tests for sqlite3_stmt_busy
125 db eval {INSERT INTO t1 VALUES(6); INSERT INTO t1 VALUES(7);}
126 set STMT [sqlite3_prepare db {SELECT * FROM t1} -1 TAIL]
127 sqlite3_stmt_busy $STMT
131 sqlite3_stmt_busy $STMT
135 sqlite3_stmt_busy $STMT
139 sqlite3_stmt_busy $STMT
142 do_test capi3d-3.99 {
143 sqlite3_finalize $STMT
147 #--------------------------------------------------------------------------
148 # Test the sqlite3_stmt_busy() function with ROLLBACK statements.
152 do_execsql_test capi3d-4.1 {
153 CREATE TABLE t4(x,y);
157 do_test capi3d-4.2.1 {
158 set ::s1 [sqlite3_prepare_v2 db "ROLLBACK" -1 notused]
162 do_test capi3d-4.2.2 {
163 sqlite3_stmt_busy $::s1
166 do_catchsql_test capi3d-4.2.3 {
170 do_test capi3d-4.2.4 {
174 do_catchsql_test capi3d-4.2.5 {
178 do_test capi3d-4.2.6 {
179 sqlite3_finalize $::s1