drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 1 | # 2008 June 18 |
| 2 | # |
| 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
| 5 | # |
| 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. |
| 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. |
| 12 | # |
drh | f03d9cc | 2010-11-16 23:10:25 +0000 | [diff] [blame] | 13 | # This file is devoted to testing the sqlite3_next_stmt and |
drh | 2fb6693 | 2011-11-25 17:21:47 +0000 | [diff] [blame] | 14 | # sqlite3_stmt_readonly and sqlite3_stmt_busy interfaces. |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 15 | # |
drh | 582de4f | 2008-07-14 15:11:20 +0000 | [diff] [blame] | 16 | # $Id: capi3d.test,v 1.2 2008/07/14 15:11:20 drh Exp $ |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 17 | # |
| 18 | |
| 19 | set testdir [file dirname $argv0] |
| 20 | source $testdir/tester.tcl |
| 21 | |
| 22 | # Create N prepared statements against database connection db |
| 23 | # and return a list of all the generated prepared statements. |
| 24 | # |
| 25 | proc make_prepared_statements {N} { |
| 26 | set plist {} |
| 27 | for {set i 0} {$i<$N} {incr i} { |
| 28 | set sql "SELECT $i FROM sqlite_master WHERE name LIKE '%$i%'" |
| 29 | if {rand()<0.33} { |
| 30 | set s [sqlite3_prepare_v2 db $sql -1 notused] |
| 31 | } else { |
| 32 | ifcapable utf16 { |
| 33 | if {rand()<0.5} { |
| 34 | set sql [encoding convertto unicode $sql]\x00\x00 |
| 35 | set s [sqlite3_prepare16 db $sql -1 notused] |
| 36 | } else { |
| 37 | set s [sqlite3_prepare db $sql -1 notused] |
| 38 | } |
| 39 | } |
| 40 | ifcapable !utf16 { |
| 41 | set s [sqlite3_prepare db $sql -1 notused] |
| 42 | } |
| 43 | } |
| 44 | lappend plist $s |
| 45 | } |
| 46 | return $plist |
| 47 | } |
| 48 | |
| 49 | |
| 50 | # Scramble the $inlist into a random order. |
| 51 | # |
| 52 | proc scramble {inlist} { |
| 53 | set y {} |
| 54 | foreach x $inlist { |
| 55 | lappend y [list [expr {rand()}] $x] |
| 56 | } |
| 57 | set y [lsort $y] |
| 58 | set outlist {} |
| 59 | foreach x $y { |
| 60 | lappend outlist [lindex $x 1] |
| 61 | } |
| 62 | return $outlist |
| 63 | } |
| 64 | |
| 65 | # Database initially has no prepared statements. |
| 66 | # |
| 67 | do_test capi3d-1.1 { |
drh | 582de4f | 2008-07-14 15:11:20 +0000 | [diff] [blame] | 68 | db cache flush |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 69 | sqlite3_next_stmt db 0 |
| 70 | } {} |
| 71 | |
| 72 | # Run the following tests for between 1 and 100 prepared statements. |
| 73 | # |
| 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] |
| 78 | set x {} |
| 79 | while {$p!=""} { |
| 80 | lappend x $p |
| 81 | set p [sqlite3_next_stmt db $p] |
| 82 | } |
| 83 | lsort $x |
| 84 | } [lsort $stmtlist] |
| 85 | do_test capi3-1.2.$i.2 { |
| 86 | foreach p [scramble $::stmtlist] { |
| 87 | sqlite3_finalize $p |
| 88 | } |
| 89 | sqlite3_next_stmt db 0 |
| 90 | } {} |
| 91 | } |
drh | f03d9cc | 2010-11-16 23:10:25 +0000 | [diff] [blame] | 92 | |
| 93 | # Tests for the is-read-only interface. |
| 94 | # |
| 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 |
| 101 | set rc |
| 102 | } $sql] $truth |
| 103 | } |
| 104 | |
| 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 |
drh | 9e92a47 | 2013-06-27 17:40:30 +0000 | [diff] [blame] | 111 | ifcapable wal { |
| 112 | test_is_readonly capi3d-2.6 {PRAGMA journal_mode=WAL} 0 |
| 113 | test_is_readonly capi3d-2.7 {PRAGMA wal_checkpoint} 0 |
| 114 | } |
| 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 |
drh | 39c5c4a | 2019-03-06 14:53:27 +0000 | [diff] [blame] | 118 | do_test capi3-2.49 { |
drh | f03d9cc | 2010-11-16 23:10:25 +0000 | [diff] [blame] | 119 | sqlite3_stmt_readonly 0 |
| 120 | } 1 |
| 121 | |
drh | 39c5c4a | 2019-03-06 14:53:27 +0000 | [diff] [blame] | 122 | |
| 123 | # Tests for the is-explain interface. |
| 124 | # |
| 125 | proc test_is_explain {testname sql truth} { |
| 126 | do_test $testname [format { |
| 127 | set DB [sqlite3_connection_pointer db] |
| 128 | set STMT [sqlite3_prepare $DB {%s} -1 TAIL] |
| 129 | set rc [sqlite3_stmt_isexplain $STMT] |
| 130 | sqlite3_finalize $STMT |
| 131 | set rc |
| 132 | } $sql] $truth |
| 133 | } |
| 134 | |
| 135 | test_is_explain capi3d-2.51 {SELECT * FROM sqlite_master} 0 |
| 136 | test_is_explain capi3d-2.52 { explain SELECT * FROM sqlite_master} 1 |
| 137 | test_is_explain capi3d-2.53 { Explain Query Plan select * FROM sqlite_master} 2 |
| 138 | do_test capi3-2.99 { |
| 139 | sqlite3_stmt_isexplain 0 |
| 140 | } 0 |
| 141 | |
drh | 2fb6693 | 2011-11-25 17:21:47 +0000 | [diff] [blame] | 142 | # Tests for sqlite3_stmt_busy |
| 143 | # |
| 144 | do_test capi3d-3.1 { |
| 145 | db eval {INSERT INTO t1 VALUES(6); INSERT INTO t1 VALUES(7);} |
| 146 | set STMT [sqlite3_prepare db {SELECT * FROM t1} -1 TAIL] |
| 147 | sqlite3_stmt_busy $STMT |
| 148 | } {0} |
| 149 | do_test capi3d-3.2 { |
| 150 | sqlite3_step $STMT |
| 151 | sqlite3_stmt_busy $STMT |
| 152 | } {1} |
| 153 | do_test capi3d-3.3 { |
| 154 | sqlite3_step $STMT |
| 155 | sqlite3_stmt_busy $STMT |
| 156 | } {1} |
| 157 | do_test capi3d-3.4 { |
| 158 | sqlite3_reset $STMT |
| 159 | sqlite3_stmt_busy $STMT |
| 160 | } {0} |
| 161 | |
| 162 | do_test capi3d-3.99 { |
| 163 | sqlite3_finalize $STMT |
| 164 | sqlite3_stmt_busy 0 |
| 165 | } {0} |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 166 | |
dan | 857745c | 2014-07-19 17:57:10 +0000 | [diff] [blame] | 167 | #-------------------------------------------------------------------------- |
| 168 | # Test the sqlite3_stmt_busy() function with ROLLBACK statements. |
| 169 | # |
| 170 | reset_db |
| 171 | |
| 172 | do_execsql_test capi3d-4.1 { |
| 173 | CREATE TABLE t4(x,y); |
| 174 | BEGIN; |
| 175 | } |
| 176 | |
| 177 | do_test capi3d-4.2.1 { |
dan | 857745c | 2014-07-19 17:57:10 +0000 | [diff] [blame] | 178 | set ::s1 [sqlite3_prepare_v2 db "ROLLBACK" -1 notused] |
| 179 | sqlite3_step $::s1 |
| 180 | } {SQLITE_DONE} |
| 181 | |
| 182 | do_test capi3d-4.2.2 { |
| 183 | sqlite3_stmt_busy $::s1 |
drh | 8ff2587 | 2015-07-31 18:59:56 +0000 | [diff] [blame] | 184 | } {0} |
dan | 857745c | 2014-07-19 17:57:10 +0000 | [diff] [blame] | 185 | |
| 186 | do_catchsql_test capi3d-4.2.3 { |
| 187 | VACUUM |
drh | 8ff2587 | 2015-07-31 18:59:56 +0000 | [diff] [blame] | 188 | } {0 {}} |
dan | 857745c | 2014-07-19 17:57:10 +0000 | [diff] [blame] | 189 | |
| 190 | do_test capi3d-4.2.4 { |
| 191 | sqlite3_reset $::s1 |
| 192 | } {SQLITE_OK} |
| 193 | |
| 194 | do_catchsql_test capi3d-4.2.5 { |
| 195 | VACUUM |
| 196 | } {0 {}} |
| 197 | |
| 198 | do_test capi3d-4.2.6 { |
| 199 | sqlite3_finalize $::s1 |
| 200 | } {SQLITE_OK} |
| 201 | |
| 202 | |
drh | bb5a9c3 | 2008-06-19 02:52:25 +0000 | [diff] [blame] | 203 | finish_test |