interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / reprepare.test

157 lines · 4451 bytes Raw Blame History
1# 2026 October 12
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#
13# Specifically, it tests that statements that should only be reprepared
14# when a bound variable is changed to or from a small integer value
15# (0 or 1), really is only reprepared in those cases.
16#
17
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21set testprefix reprepare
22
23do_execsql_test 1.0 {
24 CREATE TABLE t1(id INTEGER PRIMARY KEY, nm TEXT);
25 INSERT INTO t1 VALUES(4294967296,'a'), (4294967297,'b');
26}
27
28proc do_reprepare_count_test {tn sql lInt nReprepare} {
29 set stmt [sqlite3_prepare_v2 db $sql -1 dummy]
30 foreach ii $lInt {
31 set ii [expr $ii]
32 sqlite3_bind_int64 $stmt 1 $ii
33 sqlite3_step $stmt
34 sqlite3_reset $stmt
35 }
36
37 do_test $tn [list \
38 sqlite3_stmt_status $stmt SQLITE_STMTSTATUS_REPREPARE 0
39 ] [list $nReprepare]
40
41 sqlite3_finalize $stmt
42}
43
44do_reprepare_count_test 1.1 {
45 SELECT * FROM t1 WHERE +t1.id=?1
46} {4294967296 4294967297 4294967296 4294967297} 0
47
48do_reprepare_count_test 1.2 {
49 SELECT a.nm, b.nm FROM t1 a, t1 b WHERE a.id=?1 AND b.id=a.id
50} {4294967296 4294967297 4294967296 4294967297} 0
51
52proc do_reprepare_test {tn sql lTest} {
53 set stmt [sqlite3_prepare_v2 db $sql -1 dummy]
54
55 set cnt 0
56 foreach {t res} $lTest {
57 incr cnt
58 eval $t
59 sqlite3_step $stmt
60 sqlite3_reset $stmt
61 do_test $tn.$cnt [list \
62 sqlite3_stmt_status $stmt SQLITE_STMTSTATUS_REPREPARE 1
63 ] [list $res]
64 }
65
66 sqlite3_finalize $stmt
67}
68
69do_reprepare_test 1.3 {
70 SELECT a.nm, b.nm FROM t1 a, t1 b WHERE a.id=?1 AND b.id=a.id
71} {
72 { sqlite3_bind_int64 $stmt 1 45 } 0
73 { sqlite3_bind_int64 $stmt 1 0 } 1
74 { sqlite3_bind_int64 $stmt 1 1 } 0
75 { sqlite3_bind_int64 $stmt 1 101 } 1
76 { sqlite3_bind_null $stmt 1 } 0
77 { sqlite3_bind_text $stmt 1 hello -1 } 0
78 { sqlite3_bind_int64 $stmt 1 1 } 1
79 { sqlite3_bind_text $stmt 1 world -1 } 1
80 { sqlite3_bind_int64 $stmt 1 450 } 0
81 { sqlite3_bind_int64 $stmt 1 0 } 1
82 { sqlite3_bind_null $stmt 1 } 1
83 { sqlite3_bind_int64 $stmt 1 0 } 1
84 { sqlite3_bind_int64 $stmt 1 1 } 0
85 { sqlite3_bind_int64 $stmt 1 2 } 1
86}
87
88do_reprepare_test 1.4 {
89 SELECT * FROM t1 LIMIT ?1
90} {
91 { sqlite3_bind_int64 $stmt 1 12 } 1
92 { sqlite3_bind_int64 $stmt 1 12 } 0
93 { sqlite3_bind_int64 $stmt 1 13 } 1
94 { sqlite3_bind_int64 $stmt 1 13 } 0
95 { sqlite3_bind_int64 $stmt 1 14 } 1
96}
97
98#-------------------------------------------------------------------------
99# Test that if a variable is used both in a context where only a change
100# to or from a small integer requires a reprepare (an equality comparison)
101# and in a context where any change requires a reprepare (a LIMIT clause),
102# the statement is reprepared every time a new value is bound.
103#
104do_execsql_test 2.0 {
105 CREATE TABLE t2(x INTEGER PRIMARY KEY, y);
106 WITH s(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<10)
107 INSERT INTO t2 SELECT i, i FROM s;
108}
109
110proc do_limit_test {tn sql lTest} {
111 set stmt [sqlite3_prepare_v2 db $sql -1 dummy]
112 set cnt 0
113 foreach {val res} $lTest {
114 incr cnt
115 sqlite3_bind_int64 $stmt 1 $val
116 set nRow 0
117 while {[sqlite3_step $stmt]=="SQLITE_ROW"} { incr nRow }
118 sqlite3_reset $stmt
119 do_test $tn.$cnt [list set {} $nRow] [list $res]
120 }
121 sqlite3_finalize $stmt
122}
123
124do_limit_test 2.1 {
125 SELECT b.x FROM t2 a, t2 b WHERE a.x=?1 AND b.x>=a.x LIMIT ?1
126} {
127 2 2 3 3 0 0 4 4 6 5 1 1 5 5 7 4 4 4
128}
129
130#-------------------------------------------------------------------------
131# Test case from https://sqlite.org/bugs/info/2026-10-11T16:09:34Z
132#
133reset_db
134
135do_execsql_test 3.0 {
136 CREATE TABLE t(k INT, v INT);
137 WITH RECURSIVE c(x) AS (SELECT 0 UNION ALL SELECT x+1 FROM c WHERE x<399)
138 INSERT INTO t SELECT x%10, x FROM c;
139}
140
141set V [list 1 5 7 9]
142for {set ii 0} {$ii < 4} {incr ii} {
143 set val [expr [lindex $V $ii]]
144 do_test 3.1.$ii {
145 set cnt 0
146 db eval {
147 SELECT x.v FROM t x, t y WHERE x.k=$val AND y.v=x.v LIMIT $val
148 } {
149 incr cnt
150 }
151 set cnt
152 } $val
153}
154
155
156finish_test
157