blob: a3f0aeb878004104894099ddc80abd364b633a0d [file] [log] [blame]
dan27381bd2011-01-22 13:32:29 +00001# 2011 Jan 21
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. Specifically,
14# it tests the special handling of nested SQL statements (those executed
15# by virtual table or user function callbacks). These statements are treated
16# differently in two respects:
17#
18# 1. Each line of the statement is prefixed with "-- " to turn it into
19# an SQL comment.
20#
21# 2. Parameter expansion is not performed.
22#
23
24set testdir [file dirname $argv0]
25source $testdir/tester.tcl
26ifcapable !trace { finish_test ; return }
27set ::testprefix trace2
28
29proc sql {zSql} { db one $zSql }
30proc trace {zSql} { lappend ::trace $zSql }
31
32db func sql sql
33db trace trace
34
35proc do_trace_test {tn sql expected} {
36 # Test that the list of string passed to the trace callback when $sql
37 # is executed is equivalent to the list of strings in $expected.
38 #
39 set ::trace [list]
40 execsql $sql
41 uplevel do_test $tn [list {set ::trace}] [list [list {*}$expected]]
42}
43
44proc do_trace_select_test {tn sql expected} {
45
46 uplevel [list do_trace_test ${tn}.a $sql $expected]
47
48 # Now execute each SQL statement passed to the trace callback in the
49 # block above. Check that this causes the same set of strings to be
50 # passed to the trace callback again. i.e. that executing the output
51 # of the trace callback is equivalent to the SQL script in $sql.
52 #
53 set sqllist $::trace
54 set ::trace [list]
55 foreach item $sqllist { execsql $item }
56 uplevel do_test $tn.b [list {set ::trace}] [list $sqllist]
57}
58
59do_trace_select_test 1.1 {
60 SELECT 1, 2, 3;
61} {
62 "SELECT 1, 2, 3;"
63}
64
65do_trace_select_test 1.2 {
66 SELECT sql('SELECT 1, 2, 3');
67} {
68 "SELECT sql('SELECT 1, 2, 3');"
69 "-- SELECT 1, 2, 3"
70}
71
72do_trace_select_test 1.3 {
73 SELECT sql('SELECT 1,
74 2,
75 3'
76 );
77} {
78 "SELECT sql('SELECT 1,
79 2,
80 3'
81 );"
82 "-- SELECT 1,
83-- 2,
84-- 3"
85}
86
87do_trace_select_test 1.4 {
88 SELECT sql('SELECT 1,
89
90
91 3'
92 );
93} {
94 "SELECT sql('SELECT 1,
95
96
97 3'
98 );"
99 "-- SELECT 1,
100--
101--
102-- 3"
103}
104
105do_trace_select_test 1.5 {
106 SELECT $var, sql('SELECT 1,
107 $var,
108 3'
109 );
110} {
111 "SELECT NULL, sql('SELECT 1,
112 $var,
113 3'
114 );"
115 "-- SELECT 1,
116-- $var,
117-- 3"
118}
119
120ifcapable fts3 {
121 do_execsql_test 2.1 {
122 CREATE VIRTUAL TABLE x1 USING fts4;
123 INSERT INTO x1 VALUES('Cloudy, with a high near 16');
124 INSERT INTO x1 VALUES('Wind chill values as low as -13');
125 }
126
127 do_trace_test 2.2 {
128 INSERT INTO x1 VALUES('North northwest wind between 8 and 14 mph');
129 } {
130 "INSERT INTO x1 VALUES('North northwest wind between 8 and 14 mph');"
dan87ddfeb2013-05-27 10:11:53 +0000131 "-- DELETE FROM 'main'.'x1_segdir' WHERE level = ?"
dana240fd02011-02-01 16:34:32 +0000132 "-- INSERT INTO 'main'.'x1_content' VALUES(?,(?))"
dan27381bd2011-01-22 13:32:29 +0000133 "-- REPLACE INTO 'main'.'x1_docsize' VALUES(?,?)"
dan40c21432012-03-23 11:07:22 +0000134 "-- SELECT value FROM 'main'.'x1_stat' WHERE id=?"
dan865e26a2012-03-23 13:40:59 +0000135 "-- REPLACE INTO 'main'.'x1_stat' VALUES(?,?)"
dan27381bd2011-01-22 13:32:29 +0000136 "-- SELECT (SELECT max(idx) FROM 'main'.'x1_segdir' WHERE level = ?) + 1"
137 "-- SELECT coalesce((SELECT max(blockid) FROM 'main'.'x1_segments') + 1, 1)"
drhe2392332012-03-20 17:04:17 +0000138 "-- REPLACE INTO 'main'.'x1_segdir' VALUES(?,?,?,?,?,?)"
dan5b34f152014-05-14 19:49:17 +0000139 "-- SELECT level, idx, end_block FROM 'main'.'x1_segdir' WHERE level BETWEEN ? AND ? ORDER BY level DESC, idx ASC"
dan27381bd2011-01-22 13:32:29 +0000140 }
141
142 do_trace_test 2.3 {
143 INSERT INTO x1(x1) VALUES('optimize');
144 } {
145 "INSERT INTO x1(x1) VALUES('optimize');"
dan38f20242015-04-23 11:52:04 +0000146 "-- SELECT ? UNION SELECT level / (1024 * ?) FROM 'main'.'x1_segdir'"
dan653265d2011-06-13 18:21:11 +0000147 "-- SELECT idx, start_block, leaves_end_block, end_block, root FROM 'main'.'x1_segdir' WHERE level BETWEEN ? AND ?ORDER BY level DESC, idx ASC"
148 "-- SELECT max(level) FROM 'main'.'x1_segdir' WHERE level BETWEEN ? AND ?"
dan27381bd2011-01-22 13:32:29 +0000149 "-- SELECT coalesce((SELECT max(blockid) FROM 'main'.'x1_segments') + 1, 1)"
dan653265d2011-06-13 18:21:11 +0000150 "-- DELETE FROM 'main'.'x1_segdir' WHERE level BETWEEN ? AND ?"
drhe2392332012-03-20 17:04:17 +0000151 "-- REPLACE INTO 'main'.'x1_segdir' VALUES(?,?,?,?,?,?)"
dan27381bd2011-01-22 13:32:29 +0000152 }
153}
154
155finish_test