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}
177 CREATE TRIGGER r1t1 AFTER UPDATE ON t1 BEGIN
178 UPDATE t2 SET a=new.a WHERE rowid=new.rowid;
180 CREATE TRIGGER r1t2 AFTER UPDATE ON t2 BEGIN
185 proc trace_proc cmd {
186 lappend ::TRACE_OUT [string trim $cmd]
192 } {{UPDATE t1 SET a=a+1;} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2}}
195 # With 3.6.21, we add the ability to expand host parameters in the trace
196 # output. Test this feature.
199 set ::t6int [expr {3+3}]
200 set ::t6real [expr {1.5*4.0}]
201 set ::t6str {test-six y'all}
202 db eval {SELECT x'3031323334' AS x} {set ::t6blob $x}
203 unset -nocomplain t6null
205 execsql {SELECT $::t6int, $::t6real, $t6str, $t6blob, $t6null}
206 } {6 6.0 {test-six y'all} 01234 {}}
209 } {{SELECT 6, 6.0, 'test-six y''all', x'3031323334', NULL}}
212 execsql {SELECT $::t6int, ?1, $::t6int}
218 execsql {CREATE TABLE t6([$::t6int],"?1"); INSERT INTO t6 VALUES(1,2)}
220 execsql {SELECT '$::t6int', [$::t6int], $::t6int, ?1, "?1", $::t6int FROM t6}
221 } {{$::t6int} 1 6 6 2 6}
224 } {{SELECT '$::t6int', [$::t6int], 6, 6, "?1", 6 FROM t6}}
226 # Do these same tests with a UTF16 database.
228 do_test trace-6.100 {
232 PRAGMA encoding=UTF16be;
233 CREATE TABLE t6([$::t6str],"?1");
234 INSERT INTO t6 VALUES(1,2);
238 execsql {SELECT '$::t6str', [$::t6str], $::t6str, ?1, "?1", $::t6str FROM t6}
239 } {{$::t6str} 1 {test-six y'all} {test-six y'all} 2 {test-six y'all}}
240 do_test trace-6.101 {
242 } {{SELECT '$::t6str', [$::t6str], 'test-six y''all', 'test-six y''all', "?1", 'test-six y''all' FROM t6}}
244 do_test trace-6.200 {
248 PRAGMA encoding=UTF16le;
249 CREATE TABLE t6([$::t6str],"?1");
250 INSERT INTO t6 VALUES(1,2);
254 execsql {SELECT '$::t6str', [$::t6str], $::t6str, ?1, "?1", $::t6str FROM t6}
255 } {{$::t6str} 1 {test-six y'all} {test-six y'all} 2 {test-six y'all}}
256 do_test trace-6.201 {
258 } {{SELECT '$::t6str', [$::t6str], 'test-six y''all', 'test-six y''all', "?1", 'test-six y''all' FROM t6}}