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;}}
52 # If we prepare a statement and execute it multiple times, the trace
53 # happens on each execution.
56 sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
58 set STMT [sqlite3_prepare $DB {INSERT INTO t1 VALUES(2,3)} -1 TAIL]
62 lappend TRACE_OUT [string trim $sql]
67 } {{INSERT INTO t1 VALUES(2,3)}}
76 } {{INSERT INTO t1 VALUES(2,3)}}
79 execsql {SELECT * FROM t1}
83 } {{SELECT * FROM t1}}
84 catch {sqlite3_finalize $STMT}
92 # Similar tests, but this time for profiling.
95 set rc [catch {db profile 1 2 3} msg]
97 } {1 {wrong # args: should be "db profile ?CALLBACK?"}}
99 proc profile_proc {cmd tm} {
100 lappend ::stmtlist [string trim $cmd]
104 db profile profile_proc
109 CREATE TABLE t2(a,b);
110 INSERT INTO t2 VALUES(1,2);
116 } {{CREATE TABLE t2(a,b);} {INSERT INTO t2 VALUES(1,2);} {SELECT * FROM t2;}}
122 # If we prepare a statement and execute it multiple times, the profile
123 # happens on each execution.
126 sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
128 set STMT [sqlite3_prepare $DB {INSERT INTO t2 VALUES(2,3)} -1 TAIL]
130 proc profile_proc {sql tm} {
132 lappend TRACE_OUT [string trim $sql]
137 } {{INSERT INTO t2 VALUES(2,3)}}
146 } {{INSERT INTO t2 VALUES(2,3)}}
149 execsql {SELECT * FROM t1}
153 } {{SELECT * FROM t1}}
154 catch {sqlite3_finalize $STMT}
161 CREATE TRIGGER r1t1 AFTER UPDATE ON t1 BEGIN
162 UPDATE t2 SET a=new.a WHERE rowid=new.rowid;
164 CREATE TRIGGER r1t2 AFTER UPDATE ON t2 BEGIN
169 proc trace_proc cmd {
170 lappend ::TRACE_OUT [string trim $cmd]
176 } {{UPDATE t1 SET a=a+1;} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2}}
179 # With 3.6.21, we add the ability to expand host parameters in the trace
180 # output. Test this feature.
183 set ::t6int [expr {3+3}]
184 set ::t6real [expr {1.5*4.0}]
185 set ::t6str {test-six y'all}
186 db eval {SELECT x'3031323334' AS x} {set ::t6blob $x}
187 unset -nocomplain t6null
189 execsql {SELECT $::t6int, $::t6real, $t6str, $t6blob, $t6null}
190 } {6 6.0 {test-six y'all} 01234 {}}
193 } {{SELECT 6, 6.0, 'test-six y''all', x'3031323334', NULL}}
196 execsql {SELECT $::t6int, ?1, $::t6int}
202 execsql {CREATE TABLE t6([$::t6int],"?1"); INSERT INTO t6 VALUES(1,2)}
204 execsql {SELECT '$::t6int', [$::t6int], $::t6int, ?1, "?1", $::t6int FROM t6}
205 } {{$::t6int} 1 6 6 2 6}
208 } {{SELECT '$::t6int', [$::t6int], 6, 6, "?1", 6 FROM t6}}
210 # Do these same tests with a UTF16 database.
212 do_test trace-6.100 {
216 PRAGMA encoding=UTF16be;
217 CREATE TABLE t6([$::t6str],"?1");
218 INSERT INTO t6 VALUES(1,2);
222 execsql {SELECT '$::t6str', [$::t6str], $::t6str, ?1, "?1", $::t6str FROM t6}
223 } {{$::t6str} 1 {test-six y'all} {test-six y'all} 2 {test-six y'all}}
224 do_test trace-6.101 {
226 } {{SELECT '$::t6str', [$::t6str], 'test-six y''all', 'test-six y''all', "?1", 'test-six y''all' FROM t6}}
228 do_test trace-6.200 {
232 PRAGMA encoding=UTF16le;
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.201 {
242 } {{SELECT '$::t6str', [$::t6str], 'test-six y''all', 'test-six y''all', "?1", 'test-six y''all' FROM t6}}