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
137 set ::stmtlist(record) {}
138 db trace_v2 trace_v2_record 2
140 SELECT a, b FROM t1 ORDER BY a;
142 set stmt [lindex [lindex $::stmtlist(record) 0] 0]
143 set ns [lindex [lindex $::stmtlist(record) 0] 1]
144 if {$ns<0 || $ns>9999999} { #less than 0.010 seconds
147 set res "time out of bounds. Expected less than 99999999. Got $ns"
159 set ::stmtlist(record) {}
160 db trace_v2 trace_v2_record row
162 SELECT a, b FROM t1 ORDER BY a;
164 set ::stmtlist(record)
165 } "/^[string trim [string repeat {-?\d+ } 16]]\$/"
167 set ::stmtlist(record) {}
168 db trace_v2 trace_v2_record 4
170 SELECT a, b FROM t1 ORDER BY a;
172 set ::stmtlist(record)
173 } "/^[string trim [string repeat {-?\d+ } 16]]\$/"
176 set ::stmtlist(record) {}
177 db trace_v2 trace_v2_record {profile row}
179 SELECT a, b FROM t1 ORDER BY a;
181 set ::stmtlist(record)
182 } "/^[string trim [string repeat {-?\d+ } 16]] \\\{-?\\d+ -?\\d+\\\}\$/"
184 set ::stmtlist(record) {}
185 db trace_v2 trace_v2_record {statement profile row}
187 SELECT a, b FROM t1 ORDER BY a;
189 set ::stmtlist(record)
190 } "/^\\\{-?\\d+ \\\{SELECT a, b FROM t1 ORDER BY a;\\\}\\\} [string trim \
191 [string repeat {-?\d+ } 16]] \\\{-?\\d+ -?\\d+\\\}\$/"
194 set DB [sqlite3_connection_pointer db]
196 set STMT [sqlite3_prepare_v2 $DB \
197 "SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 TAIL]
201 list [sqlite3_bind_null $STMT 1] [sqlite3_expanded_sql $STMT]
202 } {{} {SELECT a, b FROM t1 WHERE b = NULL ORDER BY a;}}
204 list [sqlite3_bind_int $STMT 1 123] [sqlite3_expanded_sql $STMT]
205 } {{} {SELECT a, b FROM t1 WHERE b = 123 ORDER BY a;}}
207 list [sqlite3_bind_int64 $STMT 1 123] [sqlite3_expanded_sql $STMT]
208 } {{} {SELECT a, b FROM t1 WHERE b = 123 ORDER BY a;}}
210 list [sqlite3_bind_text $STMT 1 "some string" 11] \
211 [sqlite3_expanded_sql $STMT]
212 } {{} {SELECT a, b FROM t1 WHERE b = 'some string' ORDER BY a;}}
214 list [sqlite3_bind_text $STMT 1 "some 'bad' string" 17] \
215 [sqlite3_expanded_sql $STMT]
216 } {{} {SELECT a, b FROM t1 WHERE b = 'some ''bad'' string' ORDER BY a;}}
218 list [sqlite3_bind_double $STMT 1 123] [sqlite3_expanded_sql $STMT]
219 } {{} {SELECT a, b FROM t1 WHERE b = 123.0 ORDER BY a;}}
221 list [sqlite3_bind_text16 $STMT 1 \
222 [encoding convertto unicode hi\000yall\000] 16] \
223 [sqlite3_expanded_sql $STMT]
224 } {{} {SELECT a, b FROM t1 WHERE b = 'hi' ORDER BY a;}}
226 list [sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3] \
227 [sqlite3_expanded_sql $STMT]
228 } {{} {SELECT a, b FROM t1 WHERE b = x'123456' ORDER BY a;}}
230 list [sqlite3_bind_blob $STMT 1 "\xAB\xCD\xEF" 3] \
231 [sqlite3_expanded_sql $STMT]
232 } {{} {SELECT a, b FROM t1 WHERE b = x'abcdef' ORDER BY a;}}
235 sqlite3_finalize $STMT
238 do_test trace3-10.1 {
242 do_test trace3-10.2 {
243 unset -nocomplain ::stmtlist
244 db trace_v2 "" {statement profile row}
246 SELECT a, b FROM t1 ORDER BY a;
251 do_test trace3-11.1 {
252 set ::stmtlist(record) {}
253 db trace_v2 trace_v2_record close
255 set ::stmtlist(record)
260 do_test trace3-11.2 {
261 set ::stmtlist(record) {}
262 db trace_v2 trace_v2_record 8
264 set ::stmtlist(record)
267 #-------------------------------------------------------------------------
270 set ::STMT [sqlite3_prepare_v2 $DB \
271 "SELECT ?1 || ?1 || ?1 || ?2 || ?3 || ?4 || ? || ?1 || ?" -1 TAIL
273 sqlite3_bind_parameter_count $::STMT
277 sqlite3_bind_text $STMT 1 "A" 1
278 sqlite3_bind_text $STMT 2 "B" 1
279 sqlite3_bind_text $STMT 3 "C" 1
280 sqlite3_bind_text $STMT 4 "D" 1
281 sqlite3_bind_text $STMT 5 "E" 1
282 sqlite3_bind_text $STMT 6 "F" 1
283 sqlite3_expanded_sql $STMT
284 } {SELECT 'A' || 'A' || 'A' || 'B' || 'C' || 'D' || 'E' || 'A' || 'F'}
288 sqlite3_column_text $STMT 0
292 sqlite3_finalize $STMT
297 CREATE TABLE nameFtsFuzzySearchTable(
298 word, distance, langid, score, top, scope
301 set ::STMT [sqlite3_prepare_v2 $DB {
303 substr(word,1,length(?1)-1) AS term,
308 nameFtsFuzzySearchTable
310 word MATCH (?1) AND abs(?1) = abs(term)
311 AND top = ?2 AND distance > ?3 AND scope = ?4 AND langid = ?
312 GROUP BY term, langid
313 HAVING (1.0 - ((distance / 100.0) / CAST( length(?1) - 1 AS REAL ))) >= ?
315 sqlite3_bind_parameter_count $::STMT
319 sqlite3_bind_text $STMT 1 "A" 1
320 sqlite3_bind_text $STMT 2 "B" 1
321 sqlite3_bind_text $STMT 3 "C" 1
322 sqlite3_bind_text $STMT 4 "D" 1
323 sqlite3_bind_text $STMT 5 "E" 1
324 sqlite3_bind_text $STMT 6 "F" 1
325 sqlite3_expanded_sql $STMT
328 substr(word,1,length('A')-1) AS term,
333 nameFtsFuzzySearchTable
335 word MATCH ('A') AND abs('A') = abs(term)
336 AND top = 'B' AND distance > 'C' AND scope = 'D' AND langid = 'E'
337 GROUP BY term, langid
338 HAVING (1.0 - ((distance / 100.0) / CAST( length('A') - 1 AS REAL ))) >= 'F'
342 sqlite3_finalize $STMT