xref: /sqlite-3.40.0/test/trace3.test (revision e2f84b40)
1# 2016 July 14
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.  The focus of
12# this test file is the "sqlite3_trace_v2()" and "sqlite3_expanded_sql()"
13# APIs.
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18ifcapable !trace { finish_test ; return }
19set ::testprefix trace3
20
21proc trace_v2_error { args } {
22  lappend ::stmtlist(error) [string trim $args]
23  error "trace error"; # this will be ignored.
24}
25proc trace_v2_record { args } {
26  lappend ::stmtlist(record) [string trim $args]
27}
28proc trace_v2_nop { args } {}; # do nothing.
29
30do_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} {}
51
52do_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?"}}
56do_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}}
60
61do_test trace3-2.1 {
62  db trace_v2 trace_v2_nop
63  db trace_v2
64} {trace_v2_nop}
65
66do_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} {}
74do_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;\}\}$/}
82do_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
91do_test trace3-4.1 {
92  set ::stmtlist(record) {}
93  db trace_v2 trace_v2_record profile
94  execsql {
95    SELECT a, b FROM t1 ORDER BY a;
96  }
97  set ::stmtlist(record)
98} {/^\{-?\d+ -?\d+\}$/}
99
100do_test trace3-5.1 {
101  set ::stmtlist(record) {}
102  db trace_v2 trace_v2_record row
103  execsql {
104    SELECT a, b FROM t1 ORDER BY a;
105  }
106  set ::stmtlist(record)
107} "/^[string trim [string repeat {\d+ } 16]]\$/"
108
109do_test trace3-6.1 {
110  set ::stmtlist(record) {}
111  db trace_v2 trace_v2_record {profile row}
112  execsql {
113    SELECT a, b FROM t1 ORDER BY a;
114  }
115  set ::stmtlist(record)
116} "/^[string trim [string repeat {-?\d+ } 16]] \\\{-?\\d+ -?\\d+\\\}\$/"
117do_test trace3-6.2 {
118  set ::stmtlist(record) {}
119  db trace_v2 trace_v2_record {statement profile row}
120  execsql {
121    SELECT a, b FROM t1 ORDER BY a;
122  }
123  set ::stmtlist(record)
124} "/^\\\{-?\\d+ \\\{SELECT a, b FROM t1 ORDER BY a;\\\}\\\} [string trim \
125[string repeat {-?\d+ } 16]] \\\{-?\\d+ -?\\d+\\\}\$/"
126
127do_test trace3-7.1 {
128  set DB [sqlite3_connection_pointer db]
129
130  set STMT [sqlite3_prepare_v2 $DB \
131      "SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 TAIL]
132} {/^[0-9A-F]+$/}
133
134do_test trace3-8.1 {
135  list [sqlite3_bind_null $STMT 1] [sqlite3_expanded_sql $STMT]
136} {{} {SELECT a, b FROM t1 WHERE b = NULL ORDER BY a;}}
137do_test trace3-8.2 {
138  list [sqlite3_bind_int $STMT 1 123] [sqlite3_expanded_sql $STMT]
139} {{} {SELECT a, b FROM t1 WHERE b = 123 ORDER BY a;}}
140do_test trace3-8.3 {
141  list [sqlite3_bind_int64 $STMT 1 123] [sqlite3_expanded_sql $STMT]
142} {{} {SELECT a, b FROM t1 WHERE b = 123 ORDER BY a;}}
143do_test trace3-8.4 {
144  list [sqlite3_bind_text $STMT 1 "some string" 11] \
145      [sqlite3_expanded_sql $STMT]
146} {{} {SELECT a, b FROM t1 WHERE b = 'some string' ORDER BY a;}}
147do_test trace3-8.5 {
148  list [sqlite3_bind_text $STMT 1 "some 'bad' string" 17] \
149      [sqlite3_expanded_sql $STMT]
150} {{} {SELECT a, b FROM t1 WHERE b = 'some ''bad'' string' ORDER BY a;}}
151do_test trace3-8.6 {
152  list [sqlite3_bind_double $STMT 1 123] [sqlite3_expanded_sql $STMT]
153} {{} {SELECT a, b FROM t1 WHERE b = 123.0 ORDER BY a;}}
154do_test trace3-8.7 {
155  list [sqlite3_bind_text16 $STMT 1 \
156      [encoding convertto unicode hi\000yall\000] 16] \
157      [sqlite3_expanded_sql $STMT]
158} {{} {SELECT a, b FROM t1 WHERE b = 'hi' ORDER BY a;}}
159do_test trace3-8.8 {
160  list [sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3] \
161      [sqlite3_expanded_sql $STMT]
162} {{} {SELECT a, b FROM t1 WHERE b = x'123456' ORDER BY a;}}
163
164do_test trace3-9.1 {
165  db trace_v2 ""
166  db trace_v2
167} {}
168do_test trace3-9.2 {
169  unset -nocomplain ::stmtlist
170  db trace_v2 "" {statement profile row}
171  execsql {
172    SELECT a, b FROM t1 ORDER BY a;
173  }
174  array get ::stmtlist
175} {}
176
177do_test trace3-10.1 {
178  set ::stmtlist(record) {}
179  db trace_v2 trace_v2_record close
180  db close
181  set ::stmtlist(record)
182} {/^-?\d+$/}
183
184finish_test
185