drh | b97759e | 2004-06-29 11:26:59 +0000 | [diff] [blame] | 1 | # 2004 Jun 29 |
| 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. |
| 12 | # |
| 13 | # This file implements tests for the "sqlite3_trace()" API. |
| 14 | # |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame^] | 15 | # $Id: trace.test,v 1.7 2008/01/12 21:35:57 drh Exp $ |
drh | b97759e | 2004-06-29 11:26:59 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 20 | ifcapable !trace { |
| 21 | finish_test |
| 22 | return |
| 23 | } |
| 24 | |
danielk1977 | 93cd039 | 2004-06-30 02:35:51 +0000 | [diff] [blame] | 25 | set ::stmtlist {} |
drh | b97759e | 2004-06-29 11:26:59 +0000 | [diff] [blame] | 26 | do_test trace-1.1 { |
| 27 | set rc [catch {db trace 1 2 3} msg] |
| 28 | lappend rc $msg |
| 29 | } {1 {wrong # args: should be "db trace ?CALLBACK?"}} |
| 30 | proc trace_proc cmd { |
| 31 | lappend ::stmtlist [string trim $cmd] |
| 32 | } |
| 33 | do_test trace-1.2 { |
| 34 | db trace trace_proc |
| 35 | db trace |
| 36 | } {trace_proc} |
| 37 | do_test trace-1.3 { |
| 38 | execsql { |
| 39 | CREATE TABLE t1(a,b); |
| 40 | INSERT INTO t1 VALUES(1,2); |
| 41 | SELECT * FROM t1; |
| 42 | } |
| 43 | } {1 2} |
| 44 | do_test trace-1.4 { |
| 45 | set ::stmtlist |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 46 | } {{CREATE TABLE t1(a,b);} {INSERT INTO t1 VALUES(1,2);} {SELECT * FROM t1;}} |
drh | b97759e | 2004-06-29 11:26:59 +0000 | [diff] [blame] | 47 | do_test trace-1.5 { |
| 48 | db trace {} |
| 49 | db trace |
| 50 | } {} |
| 51 | |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 52 | # If we prepare a statement and execute it multiple times, the trace |
| 53 | # happens on each execution. |
| 54 | # |
| 55 | db close |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 56 | sqlite3 db test.db; set DB [sqlite3_connection_pointer db] |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 57 | do_test trace-2.1 { |
| 58 | set STMT [sqlite3_prepare $DB {INSERT INTO t1 VALUES(2,3)} -1 TAIL] |
| 59 | db trace trace_proc |
| 60 | proc trace_proc sql { |
| 61 | global TRACE_OUT |
| 62 | set TRACE_OUT $sql |
| 63 | } |
| 64 | set TRACE_OUT {} |
| 65 | sqlite3_step $STMT |
| 66 | set TRACE_OUT |
| 67 | } {INSERT INTO t1 VALUES(2,3)} |
| 68 | do_test trace-2.2 { |
| 69 | set TRACE_OUT {} |
| 70 | sqlite3_reset $STMT |
| 71 | set TRACE_OUT |
| 72 | } {} |
| 73 | do_test trace-2.3 { |
| 74 | sqlite3_step $STMT |
| 75 | set TRACE_OUT |
| 76 | } {INSERT INTO t1 VALUES(2,3)} |
| 77 | do_test trace-2.4 { |
| 78 | execsql {SELECT * FROM t1} |
| 79 | } {1 2 2 3 2 3} |
| 80 | do_test trace-2.5 { |
| 81 | set TRACE_OUT |
| 82 | } {SELECT * FROM t1} |
drh | 31f33e1 | 2004-09-17 20:46:54 +0000 | [diff] [blame] | 83 | catch {sqlite3_finalize $STMT} |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 84 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 85 | # Similar tests, but this time for profiling. |
| 86 | # |
| 87 | do_test trace-3.1 { |
| 88 | set rc [catch {db profile 1 2 3} msg] |
| 89 | lappend rc $msg |
| 90 | } {1 {wrong # args: should be "db profile ?CALLBACK?"}} |
| 91 | set ::stmtlist {} |
| 92 | proc profile_proc {cmd tm} { |
| 93 | lappend ::stmtlist [string trim $cmd] |
| 94 | } |
| 95 | do_test trace-3.2 { |
| 96 | db trace {} |
| 97 | db profile profile_proc |
| 98 | db profile |
| 99 | } {profile_proc} |
| 100 | do_test trace-3.3 { |
| 101 | execsql { |
| 102 | CREATE TABLE t2(a,b); |
| 103 | INSERT INTO t2 VALUES(1,2); |
| 104 | SELECT * FROM t2; |
| 105 | } |
| 106 | } {1 2} |
| 107 | do_test trace-3.4 { |
| 108 | set ::stmtlist |
| 109 | } {{CREATE TABLE t2(a,b);} {INSERT INTO t2 VALUES(1,2);} {SELECT * FROM t2;}} |
| 110 | do_test trace-3.5 { |
| 111 | db profile {} |
| 112 | db profile |
| 113 | } {} |
| 114 | |
| 115 | # If we prepare a statement and execute it multiple times, the profile |
| 116 | # happens on each execution. |
| 117 | # |
| 118 | db close |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 119 | sqlite3 db test.db; set DB [sqlite3_connection_pointer db] |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 120 | do_test trace-4.1 { |
| 121 | set STMT [sqlite3_prepare $DB {INSERT INTO t2 VALUES(2,3)} -1 TAIL] |
| 122 | db trace trace_proc |
| 123 | proc profile_proc {sql tm} { |
| 124 | global TRACE_OUT |
| 125 | set TRACE_OUT $sql |
| 126 | } |
| 127 | set TRACE_OUT {} |
| 128 | sqlite3_step $STMT |
| 129 | set TRACE_OUT |
| 130 | } {INSERT INTO t2 VALUES(2,3)} |
| 131 | do_test trace-4.2 { |
| 132 | set TRACE_OUT {} |
| 133 | sqlite3_reset $STMT |
| 134 | set TRACE_OUT |
| 135 | } {} |
| 136 | do_test trace-4.3 { |
| 137 | sqlite3_step $STMT |
| 138 | set TRACE_OUT |
| 139 | } {INSERT INTO t2 VALUES(2,3)} |
| 140 | do_test trace-4.4 { |
| 141 | execsql {SELECT * FROM t1} |
| 142 | } {1 2 2 3 2 3} |
| 143 | do_test trace-4.5 { |
| 144 | set TRACE_OUT |
| 145 | } {SELECT * FROM t1} |
| 146 | catch {sqlite3_finalize $STMT} |
drh | c16a03b | 2004-09-15 13:38:10 +0000 | [diff] [blame] | 147 | |
drh | 949f9cd | 2008-01-12 21:35:57 +0000 | [diff] [blame^] | 148 | # Trigger tracing. |
| 149 | # |
| 150 | do_test trace-5.1 { |
| 151 | db eval { |
| 152 | CREATE TRIGGER r1t1 AFTER UPDATE ON t1 BEGIN |
| 153 | UPDATE t2 SET a=new.a WHERE rowid=new.rowid; |
| 154 | END; |
| 155 | CREATE TRIGGER r1t2 AFTER UPDATE ON t2 BEGIN |
| 156 | SELECT 'hello'; |
| 157 | END; |
| 158 | } |
| 159 | set TRACE_OUT {} |
| 160 | proc trace_proc cmd { |
| 161 | lappend ::TRACE_OUT [string trim $cmd] |
| 162 | } |
| 163 | db eval { |
| 164 | UPDATE t1 SET a=a+1; |
| 165 | } |
| 166 | set TRACE_OUT |
| 167 | } {{UPDATE t1 SET a=a+1;} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2}} |
| 168 | |
drh | b97759e | 2004-06-29 11:26:59 +0000 | [diff] [blame] | 169 | finish_test |