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. The focus of
12 # this test file is the "sqlite3_trace_v2()" and "sqlite3_expanded_sql()"
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 ifcapable !trace { finish_test ; return }
19 set ::testprefix trace3
21 proc trace_v2_error { args } {
22 lappend ::stmtlist(error) [string trim $args]
23 error "trace error"; # this will be ignored.
25 proc trace_v2_record { args } {
26 lappend ::stmtlist(record) [string trim $args]
28 proc trace_v2_nop { args } {}; # do nothing.
33 INSERT INTO t1 VALUES(1,NULL);
34 INSERT INTO t1 VALUES(2,-1);
35 INSERT INTO t1 VALUES(3,0);
36 INSERT INTO t1 VALUES(4,1);
37 INSERT INTO t1 VALUES(5,-2147483648);
38 INSERT INTO t1 VALUES(6,2147483647);
39 INSERT INTO t1 VALUES(7,-9223372036854775808);
40 INSERT INTO t1 VALUES(8,9223372036854775807);
41 INSERT INTO t1 VALUES(9,-1.0);
42 INSERT INTO t1 VALUES(10,0.0);
43 INSERT INTO t1 VALUES(11,1.0);
44 INSERT INTO t1 VALUES(12,'');
45 INSERT INTO t1 VALUES(13,'1');
46 INSERT INTO t1 VALUES(14,'one');
47 INSERT INTO t1 VALUES(15,x'abcd0123');
48 INSERT INTO t1 VALUES(16,x'4567cdef');
53 set rc [catch {db trace_v2 1 2 3} msg]
55 } {1 {wrong # args: should be "db trace_v2 ?CALLBACK? ?MASK?"}}
57 set rc [catch {db trace_v2 1 bad} msg]
59 } {1 {bad trace type "bad": must be statement, profile, row, or close}}
62 db trace_v2 trace_v2_nop
67 unset -nocomplain ::stmtlist
68 db trace_v2 trace_v2_nop
70 SELECT a, b FROM t1 ORDER BY a;
75 set ::stmtlist(error) {}
76 db trace_v2 trace_v2_error
78 SELECT a, b FROM t1 ORDER BY a;
81 } {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/}
83 set ::stmtlist(record) {}
84 db trace_v2 trace_v2_record
86 SELECT a, b FROM t1 ORDER BY a;
88 set ::stmtlist(record)
89 } {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/}
91 set ::stmtlist(record) {}
92 db trace_v2 trace_v2_record statement
94 SELECT a, b FROM t1 ORDER BY a;
96 set ::stmtlist(record)
97 } {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/}
99 set ::stmtlist(record) {}
100 db trace_v2 trace_v2_record 1
102 SELECT a, b FROM t1 ORDER BY a;
104 set ::stmtlist(record)
105 } {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/}
108 set ::stmtlist(record) {}
109 db trace_v2 trace_v2_record profile
111 SELECT a, b FROM t1 ORDER BY a;
113 set ::stmtlist(record)
114 } {/^\{-?\d+ -?\d+\}$/}
116 set ::stmtlist(record) {}
117 db trace_v2 trace_v2_record 2
119 SELECT a, b FROM t1 ORDER BY a;
121 set ::stmtlist(record)
122 } {/^\{-?\d+ -?\d+\}$/}
125 set ::stmtlist(record) {}
126 db trace_v2 trace_v2_record profile
128 SELECT a, b FROM t1 ORDER BY a;
130 set stmt [lindex [lindex $::stmtlist(record) 0] 0]
131 set ns [lindex [lindex $::stmtlist(record) 0] 1]
132 list $stmt [expr {$ns >= 0 && $ns <= 9999999}]; # less than 0.010 seconds
135 set ::stmtlist(record) {}
136 db trace_v2 trace_v2_record 2
138 SELECT a, b FROM t1 ORDER BY a;
140 set stmt [lindex [lindex $::stmtlist(record) 0] 0]
141 set ns [lindex [lindex $::stmtlist(record) 0] 1]
142 list $stmt [expr {$ns >= 0 && $ns <= 9999999}]; # less than 0.010 seconds
146 set ::stmtlist(record) {}
147 db trace_v2 trace_v2_record row
149 SELECT a, b FROM t1 ORDER BY a;
151 set ::stmtlist(record)
152 } "/^[string trim [string repeat {-?\d+ } 16]]\$/"
154 set ::stmtlist(record) {}
155 db trace_v2 trace_v2_record 4
157 SELECT a, b FROM t1 ORDER BY a;
159 set ::stmtlist(record)
160 } "/^[string trim [string repeat {-?\d+ } 16]]\$/"
163 set ::stmtlist(record) {}
164 db trace_v2 trace_v2_record {profile row}
166 SELECT a, b FROM t1 ORDER BY a;
168 set ::stmtlist(record)
169 } "/^[string trim [string repeat {-?\d+ } 16]] \\\{-?\\d+ -?\\d+\\\}\$/"
171 set ::stmtlist(record) {}
172 db trace_v2 trace_v2_record {statement profile row}
174 SELECT a, b FROM t1 ORDER BY a;
176 set ::stmtlist(record)
177 } "/^\\\{-?\\d+ \\\{SELECT a, b FROM t1 ORDER BY a;\\\}\\\} [string trim \
178 [string repeat {-?\d+ } 16]] \\\{-?\\d+ -?\\d+\\\}\$/"
181 set DB [sqlite3_connection_pointer db]
183 set STMT [sqlite3_prepare_v2 $DB \
184 "SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 TAIL]
188 list [sqlite3_bind_null $STMT 1] [sqlite3_expanded_sql $STMT]
189 } {{} {SELECT a, b FROM t1 WHERE b = NULL ORDER BY a;}}
191 list [sqlite3_bind_int $STMT 1 123] [sqlite3_expanded_sql $STMT]
192 } {{} {SELECT a, b FROM t1 WHERE b = 123 ORDER BY a;}}
194 list [sqlite3_bind_int64 $STMT 1 123] [sqlite3_expanded_sql $STMT]
195 } {{} {SELECT a, b FROM t1 WHERE b = 123 ORDER BY a;}}
197 list [sqlite3_bind_text $STMT 1 "some string" 11] \
198 [sqlite3_expanded_sql $STMT]
199 } {{} {SELECT a, b FROM t1 WHERE b = 'some string' ORDER BY a;}}
201 list [sqlite3_bind_text $STMT 1 "some 'bad' string" 17] \
202 [sqlite3_expanded_sql $STMT]
203 } {{} {SELECT a, b FROM t1 WHERE b = 'some ''bad'' string' ORDER BY a;}}
205 list [sqlite3_bind_double $STMT 1 123] [sqlite3_expanded_sql $STMT]
206 } {{} {SELECT a, b FROM t1 WHERE b = 123.0 ORDER BY a;}}
208 list [sqlite3_bind_text16 $STMT 1 \
209 [encoding convertto unicode hi\000yall\000] 16] \
210 [sqlite3_expanded_sql $STMT]
211 } {{} {SELECT a, b FROM t1 WHERE b = 'hi' ORDER BY a;}}
213 list [sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3] \
214 [sqlite3_expanded_sql $STMT]
215 } {{} {SELECT a, b FROM t1 WHERE b = x'123456' ORDER BY a;}}
217 list [sqlite3_bind_blob $STMT 1 "\xAB\xCD\xEF" 3] \
218 [sqlite3_expanded_sql $STMT]
219 } {{} {SELECT a, b FROM t1 WHERE b = x'abcdef' ORDER BY a;}}
222 sqlite3_finalize $STMT
225 do_test trace3-10.1 {
229 do_test trace3-10.2 {
230 unset -nocomplain ::stmtlist
231 db trace_v2 "" {statement profile row}
233 SELECT a, b FROM t1 ORDER BY a;
238 do_test trace3-11.1 {
239 set ::stmtlist(record) {}
240 db trace_v2 trace_v2_record close
242 set ::stmtlist(record)
247 do_test trace3-11.2 {
248 set ::stmtlist(record) {}
249 db trace_v2 trace_v2_record 8
251 set ::stmtlist(record)
254 #-------------------------------------------------------------------------
257 set ::STMT [sqlite3_prepare_v2 $DB \
258 "SELECT ?1 || ?1 || ?1 || ?2 || ?3 || ?4 || ? || ?1 || ?" -1 TAIL
260 sqlite3_bind_parameter_count $::STMT
264 sqlite3_bind_text $STMT 1 "A" 1
265 sqlite3_bind_text $STMT 2 "B" 1
266 sqlite3_bind_text $STMT 3 "C" 1
267 sqlite3_bind_text $STMT 4 "D" 1
268 sqlite3_bind_text $STMT 5 "E" 1
269 sqlite3_bind_text $STMT 6 "F" 1
270 sqlite3_expanded_sql $STMT
271 } {SELECT 'A' || 'A' || 'A' || 'B' || 'C' || 'D' || 'E' || 'A' || 'F'}
275 sqlite3_column_text $STMT 0
279 sqlite3_finalize $STMT
284 CREATE TABLE nameFtsFuzzySearchTable(
285 word, distance, langid, score, top, scope
288 set ::STMT [sqlite3_prepare_v2 $DB {
290 substr(word,1,length(?1)-1) AS term,
295 nameFtsFuzzySearchTable
297 word MATCH (?1) AND abs(?1) = abs(term)
298 AND top = ?2 AND distance > ?3 AND scope = ?4 AND langid = ?
299 GROUP BY term, langid
300 HAVING (1.0 - ((distance / 100.0) / CAST( length(?1) - 1 AS REAL ))) >= ?
302 sqlite3_bind_parameter_count $::STMT
306 sqlite3_bind_text $STMT 1 "A" 1
307 sqlite3_bind_text $STMT 2 "B" 1
308 sqlite3_bind_text $STMT 3 "C" 1
309 sqlite3_bind_text $STMT 4 "D" 1
310 sqlite3_bind_text $STMT 5 "E" 1
311 sqlite3_bind_text $STMT 6 "F" 1
312 sqlite3_expanded_sql $STMT
315 substr(word,1,length('A')-1) AS term,
320 nameFtsFuzzySearchTable
322 word MATCH ('A') AND abs('A') = abs(term)
323 AND top = 'B' AND distance > 'C' AND scope = 'D' AND langid = 'E'
324 GROUP BY term, langid
325 HAVING (1.0 - ((distance / 100.0) / CAST( length('A') - 1 AS REAL ))) >= 'F'
329 sqlite3_finalize $STMT