Add the json_pretty(J) function for pretty-printing of JSON. An optional
[sqlite.git] / test / trace3.test
blob496cc2360a31bb6074312ce5c4194bf94bb09b3f
1 # 2016 July 14
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()"
13 # APIs.
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.
30 do_test trace3-1.0 {
31   execsql {
32     CREATE TABLE t1(a,b);
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');
49   }
50 } {}
52 do_test trace3-1.1 {
53   set rc [catch {db trace_v2 1 2 3} msg]
54   lappend rc $msg
55 } {1 {wrong # args: should be "db trace_v2 ?CALLBACK? ?MASK?"}}
56 do_test trace3-1.2 {
57   set rc [catch {db trace_v2 1 bad} msg]
58   lappend rc $msg
59 } {1 {bad trace type "bad": must be statement, profile, row, or close}}
61 do_test trace3-2.1 {
62   db trace_v2 trace_v2_nop
63   db trace_v2
64 } {trace_v2_nop}
66 do_test trace3-3.1 {
67   unset -nocomplain ::stmtlist
68   db trace_v2 trace_v2_nop
69   execsql {
70     SELECT a, b FROM t1 ORDER BY a;
71   }
72   array get ::stmtlist
73 } {}
74 do_test trace3-3.2 {
75   set ::stmtlist(error) {}
76   db trace_v2 trace_v2_error
77   execsql {
78     SELECT a, b FROM t1 ORDER BY a;
79   }
80   set ::stmtlist(error)
81 } {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/}
82 do_test trace3-3.3 {
83   set ::stmtlist(record) {}
84   db trace_v2 trace_v2_record
85   execsql {
86     SELECT a, b FROM t1 ORDER BY a;
87   }
88   set ::stmtlist(record)
89 } {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/}
90 do_test trace3-3.4 {
91   set ::stmtlist(record) {}
92   db trace_v2 trace_v2_record statement
93   execsql {
94     SELECT a, b FROM t1 ORDER BY a;
95   }
96   set ::stmtlist(record)
97 } {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/}
98 do_test trace3-3.5 {
99   set ::stmtlist(record) {}
100   db trace_v2 trace_v2_record 1
101   execsql {
102     SELECT a, b FROM t1 ORDER BY a;
103   }
104   set ::stmtlist(record)
105 } {/^\{-?\d+ \{SELECT a, b FROM t1 ORDER BY a;\}\}$/}
107 do_test trace3-4.1 {
108   set ::stmtlist(record) {}
109   db trace_v2 trace_v2_record profile
110   execsql {
111     SELECT a, b FROM t1 ORDER BY a;
112   }
113   set ::stmtlist(record)
114 } {/^\{-?\d+ -?\d+\}$/}
115 do_test trace3-4.2 {
116   set ::stmtlist(record) {}
117   db trace_v2 trace_v2_record 2
118   execsql {
119     SELECT a, b FROM t1 ORDER BY a;
120   }
121   set ::stmtlist(record)
122 } {/^\{-?\d+ -?\d+\}$/}
124 do_test trace3-4.3 {
125   set ::stmtlist(record) {}
126   db trace_v2 trace_v2_record profile
127   execsql {
128     SELECT a, b FROM t1 ORDER BY a;
129   }
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
133 } {/^-?\d+ 1$/}
134 do_test trace3-4.4 {
135   set cnt 0
136   while {1} {
137     set ::stmtlist(record) {}
138     db trace_v2 trace_v2_record 2
139     execsql {
140       SELECT a, b FROM t1 ORDER BY a;
141     }
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
145       incr cnt
146       if {$cnt>3} {
147         set res "time out of bounds.  Expected less than 99999999.  Got $ns"
148         break
149       }
150     } else {
151       set res 1
152       break
153     }
154   }
155   list $stmt $res
156 } {/^-?\d+ 1$/}
158 do_test trace3-5.1 {
159   set ::stmtlist(record) {}
160   db trace_v2 trace_v2_record row
161   execsql {
162     SELECT a, b FROM t1 ORDER BY a;
163   }
164   set ::stmtlist(record)
165 } "/^[string trim [string repeat {-?\d+ } 16]]\$/"
166 do_test trace3-5.2 {
167   set ::stmtlist(record) {}
168   db trace_v2 trace_v2_record 4
169   execsql {
170     SELECT a, b FROM t1 ORDER BY a;
171   }
172   set ::stmtlist(record)
173 } "/^[string trim [string repeat {-?\d+ } 16]]\$/"
175 do_test trace3-6.1 {
176   set ::stmtlist(record) {}
177   db trace_v2 trace_v2_record {profile row}
178   execsql {
179     SELECT a, b FROM t1 ORDER BY a;
180   }
181   set ::stmtlist(record)
182 } "/^[string trim [string repeat {-?\d+ } 16]] \\\{-?\\d+ -?\\d+\\\}\$/"
183 do_test trace3-6.2 {
184   set ::stmtlist(record) {}
185   db trace_v2 trace_v2_record {statement profile row}
186   execsql {
187     SELECT a, b FROM t1 ORDER BY a;
188   }
189   set ::stmtlist(record)
190 } "/^\\\{-?\\d+ \\\{SELECT a, b FROM t1 ORDER BY a;\\\}\\\} [string trim \
191 [string repeat {-?\d+ } 16]] \\\{-?\\d+ -?\\d+\\\}\$/"
193 do_test trace3-7.1 {
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]
198 } {/^[0-9A-Fa-f]+$/}
200 do_test trace3-8.1 {
201   list [sqlite3_bind_null $STMT 1] [sqlite3_expanded_sql $STMT]
202 } {{} {SELECT a, b FROM t1 WHERE b = NULL ORDER BY a;}}
203 do_test trace3-8.2 {
204   list [sqlite3_bind_int $STMT 1 123] [sqlite3_expanded_sql $STMT]
205 } {{} {SELECT a, b FROM t1 WHERE b = 123 ORDER BY a;}}
206 do_test trace3-8.3 {
207   list [sqlite3_bind_int64 $STMT 1 123] [sqlite3_expanded_sql $STMT]
208 } {{} {SELECT a, b FROM t1 WHERE b = 123 ORDER BY a;}}
209 do_test trace3-8.4 {
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;}}
213 do_test trace3-8.5 {
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;}}
217 do_test trace3-8.6 {
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;}}
220 do_test trace3-8.7 {
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;}}
225 do_test trace3-8.8 {
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;}}
229 do_test trace3-8.9 {
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;}}
234 do_test trace3-9.1 {
235   sqlite3_finalize $STMT
236 } {SQLITE_OK}
238 do_test trace3-10.1 {
239   db trace_v2 ""
240   db trace_v2
241 } {}
242 do_test trace3-10.2 {
243   unset -nocomplain ::stmtlist
244   db trace_v2 "" {statement profile row}
245   execsql {
246     SELECT a, b FROM t1 ORDER BY a;
247   }
248   array get ::stmtlist
249 } {}
251 do_test trace3-11.1 {
252   set ::stmtlist(record) {}
253   db trace_v2 trace_v2_record close
254   db close
255   set ::stmtlist(record)
256 } {/^-?\d+$/}
258 reset_db
260 do_test trace3-11.2 {
261   set ::stmtlist(record) {}
262   db trace_v2 trace_v2_record 8
263   db close
264   set ::stmtlist(record)
265 } {/^-?\d+$/}
267 #-------------------------------------------------------------------------
268 reset_db
269 do_test 12.1.0 {
270   set ::STMT [sqlite3_prepare_v2 $DB \
271     "SELECT ?1 || ?1 || ?1 || ?2 || ?3 || ?4 || ? || ?1 || ?" -1 TAIL
272   ]
273   sqlite3_bind_parameter_count $::STMT
274 } {6}
276 do_test 12.1.1 {
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'}
286 do_test 12.1.2 {
287   sqlite3_step $STMT
288   sqlite3_column_text $STMT 0
289 } {AAABCDEAF}
291 do_test 12.1.3 {
292   sqlite3_finalize $STMT
293 } {SQLITE_OK}
295 do_test 12.2.0 {
296   execsql {
297     CREATE TABLE nameFtsFuzzySearchTable(
298       word, distance, langid, score, top, scope
299     );
300   }
301   set ::STMT [sqlite3_prepare_v2 $DB {
302     SELECT
303       substr(word,1,length(?1)-1) AS term,
304       distance,
305       langid,
306       score
307     FROM
308       nameFtsFuzzySearchTable
309     WHERE
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 ))) >= ?
314   } -1 TAIL]
315   sqlite3_bind_parameter_count $::STMT
316 } {6}
318 do_test 12.1.1 {
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
326 } {
327     SELECT
328       substr(word,1,length('A')-1) AS term,
329       distance,
330       langid,
331       score
332     FROM
333       nameFtsFuzzySearchTable
334     WHERE
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'
339   }
341 do_test 12.1.2 {
342   sqlite3_finalize $STMT
343 } {SQLITE_OK}
346 finish_test