xref: /sqlite-3.40.0/test/capi3d.test (revision b15a3940)
1bb5a9c3eSdrh# 2008 June 18
2bb5a9c3eSdrh#
3bb5a9c3eSdrh# The author disclaims copyright to this source code.  In place of
4bb5a9c3eSdrh# a legal notice, here is a blessing:
5bb5a9c3eSdrh#
6bb5a9c3eSdrh#    May you do good and not evil.
7bb5a9c3eSdrh#    May you find forgiveness for yourself and forgive others.
8bb5a9c3eSdrh#    May you share freely, never taking more than you give.
9bb5a9c3eSdrh#
10bb5a9c3eSdrh#***********************************************************************
11bb5a9c3eSdrh# This file implements regression tests for SQLite library.
12bb5a9c3eSdrh#
13f03d9cccSdrh# This file is devoted to testing the sqlite3_next_stmt and
142fb6693eSdrh# sqlite3_stmt_readonly and sqlite3_stmt_busy interfaces.
15bb5a9c3eSdrh#
16582de4f2Sdrh# $Id: capi3d.test,v 1.2 2008/07/14 15:11:20 drh Exp $
17bb5a9c3eSdrh#
18bb5a9c3eSdrh
19bb5a9c3eSdrhset testdir [file dirname $argv0]
20bb5a9c3eSdrhsource $testdir/tester.tcl
21bb5a9c3eSdrh
22bb5a9c3eSdrh# Create N prepared statements against database connection db
23bb5a9c3eSdrh# and return a list of all the generated prepared statements.
24bb5a9c3eSdrh#
25bb5a9c3eSdrhproc make_prepared_statements {N} {
26bb5a9c3eSdrh  set plist {}
27bb5a9c3eSdrh  for {set i 0} {$i<$N} {incr i} {
28bb5a9c3eSdrh    set sql "SELECT $i FROM sqlite_master WHERE name LIKE '%$i%'"
29bb5a9c3eSdrh    if {rand()<0.33} {
30bb5a9c3eSdrh      set s [sqlite3_prepare_v2 db $sql -1 notused]
31bb5a9c3eSdrh    } else {
32bb5a9c3eSdrh      ifcapable utf16 {
33bb5a9c3eSdrh        if {rand()<0.5} {
34bb5a9c3eSdrh          set sql [encoding convertto unicode $sql]\x00\x00
35bb5a9c3eSdrh          set s [sqlite3_prepare16 db $sql -1 notused]
36bb5a9c3eSdrh        } else {
37bb5a9c3eSdrh          set s [sqlite3_prepare db $sql -1 notused]
38bb5a9c3eSdrh        }
39bb5a9c3eSdrh      }
40bb5a9c3eSdrh      ifcapable !utf16 {
41bb5a9c3eSdrh        set s [sqlite3_prepare db $sql -1 notused]
42bb5a9c3eSdrh      }
43bb5a9c3eSdrh    }
44bb5a9c3eSdrh    lappend plist $s
45bb5a9c3eSdrh  }
46bb5a9c3eSdrh  return $plist
47bb5a9c3eSdrh}
48bb5a9c3eSdrh
49bb5a9c3eSdrh
50bb5a9c3eSdrh# Scramble the $inlist into a random order.
51bb5a9c3eSdrh#
52bb5a9c3eSdrhproc scramble {inlist} {
53bb5a9c3eSdrh  set y {}
54bb5a9c3eSdrh  foreach x $inlist {
55bb5a9c3eSdrh    lappend y [list [expr {rand()}] $x]
56bb5a9c3eSdrh  }
57bb5a9c3eSdrh  set y [lsort $y]
58bb5a9c3eSdrh  set outlist {}
59bb5a9c3eSdrh  foreach x $y {
60bb5a9c3eSdrh    lappend outlist [lindex $x 1]
61bb5a9c3eSdrh  }
62bb5a9c3eSdrh  return $outlist
63bb5a9c3eSdrh}
64bb5a9c3eSdrh
65bb5a9c3eSdrh# Database initially has no prepared statements.
66bb5a9c3eSdrh#
67bb5a9c3eSdrhdo_test capi3d-1.1 {
68582de4f2Sdrh  db cache flush
69bb5a9c3eSdrh  sqlite3_next_stmt db 0
70bb5a9c3eSdrh} {}
71bb5a9c3eSdrh
72bb5a9c3eSdrh# Run the following tests for between 1 and 100 prepared statements.
73bb5a9c3eSdrh#
74bb5a9c3eSdrhfor {set i 1} {$i<=100} {incr i} {
75bb5a9c3eSdrh  set stmtlist [make_prepared_statements $i]
76bb5a9c3eSdrh  do_test capi3d-1.2.$i.1 {
77bb5a9c3eSdrh    set p [sqlite3_next_stmt db 0]
78bb5a9c3eSdrh    set x {}
79bb5a9c3eSdrh    while {$p!=""} {
80bb5a9c3eSdrh      lappend x $p
81bb5a9c3eSdrh      set p [sqlite3_next_stmt db $p]
82bb5a9c3eSdrh    }
83bb5a9c3eSdrh    lsort $x
84bb5a9c3eSdrh  } [lsort $stmtlist]
85bb5a9c3eSdrh  do_test capi3-1.2.$i.2 {
86bb5a9c3eSdrh    foreach p [scramble $::stmtlist] {
87bb5a9c3eSdrh      sqlite3_finalize $p
88bb5a9c3eSdrh    }
89bb5a9c3eSdrh    sqlite3_next_stmt db 0
90bb5a9c3eSdrh  } {}
91bb5a9c3eSdrh}
92bb5a9c3eSdrh
93f03d9cccSdrh# Tests for the is-read-only interface.
94f03d9cccSdrh#
95f03d9cccSdrhproc test_is_readonly {testname sql truth} {
96f03d9cccSdrh  do_test $testname [format {
97f03d9cccSdrh    set DB [sqlite3_connection_pointer db]
98f03d9cccSdrh    set STMT [sqlite3_prepare $DB {%s} -1 TAIL]
99f03d9cccSdrh    set rc [sqlite3_stmt_readonly $STMT]
100f03d9cccSdrh    sqlite3_finalize $STMT
101f03d9cccSdrh    set rc
102f03d9cccSdrh  } $sql] $truth
103*b15a3940Sdrh
104*b15a3940Sdrh  # EVIDENCE-OF: R-61212-30018 If prepared statement X is an EXPLAIN or
105*b15a3940Sdrh  # EXPLAIN QUERY PLAN statement, then sqlite3_stmt_readonly(X) returns
106*b15a3940Sdrh  # the same value as if the EXPLAIN or EXPLAIN QUERY PLAN prefix were
107*b15a3940Sdrh  # omitted.
108*b15a3940Sdrh  #
109*b15a3940Sdrh  do_test $testname.explain [format {
110*b15a3940Sdrh    set DB [sqlite3_connection_pointer db]
111*b15a3940Sdrh    set STMT [sqlite3_prepare $DB {EXPLAIN %s} -1 TAIL]
112*b15a3940Sdrh    set rc [sqlite3_stmt_readonly $STMT]
113*b15a3940Sdrh    sqlite3_finalize $STMT
114*b15a3940Sdrh    set rc
115*b15a3940Sdrh  } $sql] $truth
116*b15a3940Sdrh  do_test $testname.eqp [format {
117*b15a3940Sdrh    set DB [sqlite3_connection_pointer db]
118*b15a3940Sdrh    set STMT [sqlite3_prepare $DB {EXPLAIN QUERY PLAN %s} -1 TAIL]
119*b15a3940Sdrh    set rc [sqlite3_stmt_readonly $STMT]
120*b15a3940Sdrh    sqlite3_finalize $STMT
121*b15a3940Sdrh    set rc
122*b15a3940Sdrh  } $sql] $truth
123f03d9cccSdrh}
124f03d9cccSdrh
125*b15a3940Sdrh# EVIDENCE-OF: R-23332-64992 The sqlite3_stmt_readonly(X) interface
126*b15a3940Sdrh# returns true (non-zero) if and only if the prepared statement X makes
127*b15a3940Sdrh# no direct changes to the content of the database file.
128*b15a3940Sdrh#
129f03d9cccSdrhtest_is_readonly capi3d-2.1 {SELECT * FROM sqlite_master} 1
130f03d9cccSdrhtest_is_readonly capi3d-2.2 {CREATE TABLE t1(x)} 0
131f03d9cccSdrhdb eval {CREATE TABLE t1(x)}
132f03d9cccSdrhtest_is_readonly capi3d-2.3 {INSERT INTO t1 VALUES(5)} 0
133f03d9cccSdrhtest_is_readonly capi3d-2.4 {UPDATE t1 SET x=x+1 WHERE x<0} 0
134f03d9cccSdrhtest_is_readonly capi3d-2.5 {SELECT * FROM t1} 1
1359e92a47bSdrhifcapable wal {
1369e92a47bSdrh  test_is_readonly capi3d-2.6 {PRAGMA journal_mode=WAL} 0
1379e92a47bSdrh  test_is_readonly capi3d-2.7 {PRAGMA wal_checkpoint} 0
1389e92a47bSdrh}
1399e92a47bSdrhtest_is_readonly capi3d-2.8 {PRAGMA application_id=1234} 0
1409e92a47bSdrhtest_is_readonly capi3d-2.9 {VACUUM} 0
1419e92a47bSdrhtest_is_readonly capi3d-2.10 {PRAGMA integrity_check} 1
14239c5c4aeSdrhdo_test capi3-2.49 {
143f03d9cccSdrh  sqlite3_stmt_readonly 0
144f03d9cccSdrh} 1
145f03d9cccSdrh
14639c5c4aeSdrh
147*b15a3940Sdrh# EVIDENCE-OF: R-04929-09147 This routine returns false if there is any
148*b15a3940Sdrh# possibility that the statement might change the database file.
149*b15a3940Sdrh#
150*b15a3940Sdrh# EVIDENCE-OF: R-13288-53765 A false return does not guarantee that the
151*b15a3940Sdrh# statement will change the database file.
152*b15a3940Sdrh#
153*b15a3940Sdrh# EVIDENCE-OF: R-22182-18548 For example, an UPDATE statement might have
154*b15a3940Sdrh# a WHERE clause that makes it a no-op, but the sqlite3_stmt_readonly()
155*b15a3940Sdrh# result would still be false.
156*b15a3940Sdrh#
157*b15a3940Sdrh# EVIDENCE-OF: R-50998-48593 Similarly, a CREATE TABLE IF NOT EXISTS
158*b15a3940Sdrh# statement is a read-only no-op if the table already exists, but
159*b15a3940Sdrh# sqlite3_stmt_readonly() still returns false for such a statement.
160*b15a3940Sdrh#
161*b15a3940Sdrhdb eval {
162*b15a3940Sdrh  CREATE TABLE t2(a,b,c);
163*b15a3940Sdrh  INSERT INTO t2 VALUES(1,2,3);
164*b15a3940Sdrh}
165*b15a3940Sdrhtest_is_readonly capi3d-2.11 {UPDATE t2 SET a=a+1 WHERE false} 0
166*b15a3940Sdrhtest_is_readonly capi3d-2.12 {CREATE TABLE IF NOT EXISTS t2(x,y)} 0
167*b15a3940Sdrh
168*b15a3940Sdrh
169*b15a3940Sdrh# EVIDENCE-OF: R-37014-01401 The ATTACH and DETACH statements also cause
170*b15a3940Sdrh# sqlite3_stmt_readonly() to return true since, while those statements
171*b15a3940Sdrh# change the configuration of a database connection, they do not make
172*b15a3940Sdrh# changes to the content of the database files on disk.
173*b15a3940Sdrh#
174*b15a3940Sdrhtest_is_readonly capi3d-2.13 {ATTACH ':memory:' AS mem1} 1
175*b15a3940Sdrhdb eval {ATTACH ':memory:' AS mem1}
176*b15a3940Sdrhtest_is_readonly capi3d-2.14 {DETACH mem1} 1
177*b15a3940Sdrhdb eval {DETACH mem1}
178*b15a3940Sdrh
179*b15a3940Sdrh# EVIDENCE-OF: R-07474-04783 Transaction control statements such as
180*b15a3940Sdrh# BEGIN, COMMIT, ROLLBACK, SAVEPOINT, and RELEASE cause
181*b15a3940Sdrh# sqlite3_stmt_readonly() to return true, since the statements
182*b15a3940Sdrh# themselves do not actually modify the database but rather they control
183*b15a3940Sdrh# the timing of when other statements modify the database.
184*b15a3940Sdrh#
185*b15a3940Sdrhtest_is_readonly capi3d-2.15 {BEGIN} 1
186*b15a3940Sdrhtest_is_readonly capi3d-2.16 {COMMIT} 1
187*b15a3940Sdrhtest_is_readonly capi3d-2.17 {SAVEPOINT one} 1
188*b15a3940Sdrhtest_is_readonly capi3d-2.18 {RELEASE one} 1
189*b15a3940Sdrh
190*b15a3940Sdrh# EVIDENCE-OF: R-36961-63052 The sqlite3_stmt_readonly() interface
191*b15a3940Sdrh# returns true for BEGIN since BEGIN merely sets internal flags, but the
192*b15a3940Sdrh# BEGIN IMMEDIATE and BEGIN EXCLUSIVE commands do touch the database and
193*b15a3940Sdrh# so sqlite3_stmt_readonly() returns false for those commands.
194*b15a3940Sdrh#
195*b15a3940Sdrhtest_is_readonly capi3d-2.19 {BEGIN IMMEDIATE} 0
196*b15a3940Sdrhtest_is_readonly capi3d-2.20 {BEGIN EXCLUSIVE} 0
197*b15a3940Sdrh
198*b15a3940Sdrh# EVIDENCE-OF: R-21769-42523 For example, if an application defines a
199*b15a3940Sdrh# function "eval()" that calls sqlite3_exec(), then the following SQL
200*b15a3940Sdrh# statement would change the database file through side-effects: SELECT
201*b15a3940Sdrh# eval('DELETE FROM t1') FROM t2; But because the SELECT statement does
202*b15a3940Sdrh# not change the database file directly, sqlite3_stmt_readonly() would
203*b15a3940Sdrh# still return true.
204*b15a3940Sdrh#
205*b15a3940Sdrhproc evalsql {sql} {db eval $sql}
206*b15a3940Sdrhdb func eval evalsql
207*b15a3940Sdrhtest_is_readonly capi3d-2.21 {SELECT eval('DELETE FROM t1') FROM t2} 1
208*b15a3940Sdrh
20939c5c4aeSdrh# Tests for the is-explain interface.
21039c5c4aeSdrh#
21139c5c4aeSdrhproc test_is_explain {testname sql truth} {
21239c5c4aeSdrh  do_test $testname [format {
21339c5c4aeSdrh    set DB [sqlite3_connection_pointer db]
21439c5c4aeSdrh    set STMT [sqlite3_prepare $DB {%s} -1 TAIL]
21539c5c4aeSdrh    set rc [sqlite3_stmt_isexplain $STMT]
21639c5c4aeSdrh    sqlite3_finalize $STMT
21739c5c4aeSdrh    set rc
21839c5c4aeSdrh  } $sql] $truth
21939c5c4aeSdrh}
22039c5c4aeSdrh
22139c5c4aeSdrhtest_is_explain capi3d-2.51 {SELECT * FROM sqlite_master} 0
22239c5c4aeSdrhtest_is_explain capi3d-2.52 { explain SELECT * FROM sqlite_master} 1
22339c5c4aeSdrhtest_is_explain capi3d-2.53 {  Explain Query Plan select * FROM sqlite_master} 2
22439c5c4aeSdrhdo_test capi3-2.99 {
22539c5c4aeSdrh  sqlite3_stmt_isexplain 0
22639c5c4aeSdrh} 0
22739c5c4aeSdrh
2282fb6693eSdrh# Tests for sqlite3_stmt_busy
2292fb6693eSdrh#
2302fb6693eSdrhdo_test capi3d-3.1 {
2312fb6693eSdrh  db eval {INSERT INTO t1 VALUES(6); INSERT INTO t1 VALUES(7);}
2322fb6693eSdrh  set STMT [sqlite3_prepare db {SELECT * FROM t1} -1 TAIL]
2332fb6693eSdrh  sqlite3_stmt_busy $STMT
2342fb6693eSdrh} {0}
2352fb6693eSdrhdo_test capi3d-3.2 {
2362fb6693eSdrh  sqlite3_step $STMT
2372fb6693eSdrh  sqlite3_stmt_busy $STMT
2382fb6693eSdrh} {1}
2392fb6693eSdrhdo_test capi3d-3.3 {
2402fb6693eSdrh  sqlite3_step $STMT
2412fb6693eSdrh  sqlite3_stmt_busy $STMT
2422fb6693eSdrh} {1}
2432fb6693eSdrhdo_test capi3d-3.4 {
2442fb6693eSdrh  sqlite3_reset $STMT
2452fb6693eSdrh  sqlite3_stmt_busy $STMT
2462fb6693eSdrh} {0}
2472fb6693eSdrh
2482fb6693eSdrhdo_test capi3d-3.99 {
2492fb6693eSdrh  sqlite3_finalize $STMT
2502fb6693eSdrh  sqlite3_stmt_busy 0
2512fb6693eSdrh} {0}
252bb5a9c3eSdrh
253857745c0Sdan#--------------------------------------------------------------------------
254857745c0Sdan# Test the sqlite3_stmt_busy() function with ROLLBACK statements.
255857745c0Sdan#
256857745c0Sdanreset_db
257857745c0Sdan
258857745c0Sdando_execsql_test capi3d-4.1 {
259857745c0Sdan  CREATE TABLE t4(x,y);
260857745c0Sdan  BEGIN;
261857745c0Sdan}
262857745c0Sdan
263857745c0Sdando_test capi3d-4.2.1 {
264857745c0Sdan  set ::s1 [sqlite3_prepare_v2 db "ROLLBACK" -1 notused]
265857745c0Sdan  sqlite3_step $::s1
266857745c0Sdan} {SQLITE_DONE}
267857745c0Sdan
268857745c0Sdando_test capi3d-4.2.2 {
269857745c0Sdan  sqlite3_stmt_busy $::s1
2708ff2587bSdrh} {0}
271857745c0Sdan
272857745c0Sdando_catchsql_test capi3d-4.2.3 {
273857745c0Sdan  VACUUM
2748ff2587bSdrh} {0 {}}
275857745c0Sdan
276857745c0Sdando_test capi3d-4.2.4 {
277857745c0Sdan  sqlite3_reset $::s1
278857745c0Sdan} {SQLITE_OK}
279857745c0Sdan
280857745c0Sdando_catchsql_test capi3d-4.2.5 {
281857745c0Sdan  VACUUM
282857745c0Sdan} {0 {}}
283857745c0Sdan
284857745c0Sdando_test capi3d-4.2.6 {
285857745c0Sdan  sqlite3_finalize $::s1
286857745c0Sdan} {SQLITE_OK}
287857745c0Sdan
288857745c0Sdan
289bb5a9c3eSdrhfinish_test
290