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.
13 # This file implements tests for the "sqlite3_trace()" API.
15 # $Id: trace.test,v 1.8 2009/04/07 14:14:23 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
27 set rc [catch {db trace 1 2 3} msg]
29 } {1 {wrong # args: should be "db trace ?CALLBACK?"}}
31 lappend ::stmtlist [string trim $cmd]
40 INSERT INTO t1 VALUES(1,2);
46 } {{CREATE TABLE t1(a,b);} {INSERT INTO t1 VALUES(1,2);} {SELECT * FROM t1;}}
53 CREATE TABLE t1b(x TEXT PRIMARY KEY, y);
54 INSERT INTO t1b VALUES('abc','def'),('ghi','jkl'),('mno','pqr');
60 SELECT y FROM t1b WHERE x GLOB $xyzzy
65 } {{SELECT y FROM t1b WHERE x GLOB 'a*'}}
68 # If we prepare a statement and execute it multiple times, the trace
69 # happens on each execution.
72 sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
74 set STMT [sqlite3_prepare $DB {INSERT INTO t1 VALUES(2,3)} -1 TAIL]
78 lappend TRACE_OUT [string trim $sql]
83 } {{INSERT INTO t1 VALUES(2,3)}}
92 } {{INSERT INTO t1 VALUES(2,3)}}
95 execsql {SELECT * FROM t1}
99 } {{SELECT * FROM t1}}
100 catch {sqlite3_finalize $STMT}
108 # Similar tests, but this time for profiling.
111 set rc [catch {db profile 1 2 3} msg]
113 } {1 {wrong # args: should be "db profile ?CALLBACK?"}}
115 proc profile_proc {cmd tm} {
116 lappend ::stmtlist [string trim $cmd]
120 db profile profile_proc
125 CREATE TABLE t2(a,b);
126 INSERT INTO t2 VALUES(1,2);
132 } {{CREATE TABLE t2(a,b);} {INSERT INTO t2 VALUES(1,2);} {SELECT * FROM t2;}}
138 # If we prepare a statement and execute it multiple times, the profile
139 # happens on each execution.
142 sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
144 set STMT [sqlite3_prepare $DB {INSERT INTO t2 VALUES(2,3)} -1 TAIL]
146 proc profile_proc {sql tm} {
148 lappend TRACE_OUT [string trim $sql]
153 } {{INSERT INTO t2 VALUES(2,3)}}
162 } {{INSERT INTO t2 VALUES(2,3)}}
165 execsql {SELECT * FROM t1}
169 } {{SELECT * FROM t1}}
170 catch {sqlite3_finalize $STMT}
172 # 3.8.11: Profile output even if the statement is not run to completion.
175 db eval {SELECT * FROM t1} {} {if {$a>=1} break}
177 } {{SELECT * FROM t1}}
185 CREATE TRIGGER r1t1 AFTER UPDATE ON t1 BEGIN
186 UPDATE t2 SET a=new.a WHERE rowid=new.rowid;
188 CREATE TRIGGER r1t2 AFTER UPDATE ON t2 BEGIN
193 proc trace_proc cmd {
194 lappend ::TRACE_OUT [string trim $cmd]
200 } {{UPDATE t1 SET a=a+1;} {-- TRIGGER r1t1} {-- UPDATE t2 SET a=new.a WHERE rowid=new.rowid} {-- TRIGGER r1t2} {-- SELECT 'hello'} {-- TRIGGER r1t1} {-- UPDATE t2 SET a=new.a WHERE rowid=new.rowid} {-- TRIGGER r1t2} {-- SELECT 'hello'} {-- TRIGGER r1t1} {-- UPDATE t2 SET a=new.a WHERE rowid=new.rowid} {-- TRIGGER r1t2} {-- SELECT 'hello'}}
203 # With 3.6.21, we add the ability to expand host parameters in the trace
204 # output. Test this feature.
207 set ::t6int [expr {3+3}]
208 set ::t6real [expr {1.5*4.0}]
209 set ::t6str {test-six y'all}
210 db eval {SELECT x'3031323334' AS x} {set ::t6blob $x}
211 unset -nocomplain t6null
213 execsql {SELECT $::t6int, $::t6real, $t6str, $t6blob, $t6null}
214 } {6 6.0 {test-six y'all} 01234 {}}
217 } {{SELECT 6, 6.0, 'test-six y''all', x'3031323334', NULL}}
220 execsql {SELECT $::t6int, ?1, $::t6int}
226 execsql {CREATE TABLE t6([$::t6int],"?1"); INSERT INTO t6 VALUES(1,2)}
228 execsql {SELECT '$::t6int', [$::t6int], $::t6int, ?1, "?1", $::t6int FROM t6}
229 } {{$::t6int} 1 6 6 2 6}
232 } {{SELECT '$::t6int', [$::t6int], 6, 6, "?1", 6 FROM t6}}
234 # Do these same tests with a UTF16 database.
236 do_test trace-6.100 {
240 PRAGMA encoding=UTF16be;
241 CREATE TABLE t6([$::t6str],"?1");
242 INSERT INTO t6 VALUES(1,2);
246 execsql {SELECT '$::t6str', [$::t6str], $::t6str, ?1, "?1", $::t6str FROM t6}
247 } {{$::t6str} 1 {test-six y'all} {test-six y'all} 2 {test-six y'all}}
248 do_test trace-6.101 {
250 } {{SELECT '$::t6str', [$::t6str], 'test-six y''all', 'test-six y''all', "?1", 'test-six y''all' FROM t6}}
252 do_test trace-6.200 {
256 PRAGMA encoding=UTF16le;
257 CREATE TABLE t6([$::t6str],"?1");
258 INSERT INTO t6 VALUES(1,2);
262 execsql {SELECT '$::t6str', [$::t6str], $::t6str, ?1, "?1", $::t6str FROM t6}
263 } {{$::t6str} 1 {test-six y'all} {test-six y'all} 2 {test-six y'all}}
264 do_test trace-6.201 {
266 } {{SELECT '$::t6str', [$::t6str], 'test-six y''all', 'test-six y''all', "?1", 'test-six y''all' FROM t6}}