127381bd5Sdan# 2011 Jan 21 227381bd5Sdan# 327381bd5Sdan# The author disclaims copyright to this source code. In place of 427381bd5Sdan# a legal notice, here is a blessing: 527381bd5Sdan# 627381bd5Sdan# May you do good and not evil. 727381bd5Sdan# May you find forgiveness for yourself and forgive others. 827381bd5Sdan# May you share freely, never taking more than you give. 927381bd5Sdan# 1027381bd5Sdan#*********************************************************************** 1127381bd5Sdan# This file implements regression tests for SQLite library. 1227381bd5Sdan# 1327381bd5Sdan# This file implements tests for the "sqlite3_trace()" API. Specifically, 1427381bd5Sdan# it tests the special handling of nested SQL statements (those executed 1527381bd5Sdan# by virtual table or user function callbacks). These statements are treated 1627381bd5Sdan# differently in two respects: 1727381bd5Sdan# 1827381bd5Sdan# 1. Each line of the statement is prefixed with "-- " to turn it into 1927381bd5Sdan# an SQL comment. 2027381bd5Sdan# 2127381bd5Sdan# 2. Parameter expansion is not performed. 2227381bd5Sdan# 2327381bd5Sdan 2427381bd5Sdanset testdir [file dirname $argv0] 2527381bd5Sdansource $testdir/tester.tcl 2627381bd5Sdanifcapable !trace { finish_test ; return } 2727381bd5Sdanset ::testprefix trace2 2827381bd5Sdan 2927381bd5Sdanproc sql {zSql} { db one $zSql } 3027381bd5Sdanproc trace {zSql} { lappend ::trace $zSql } 3127381bd5Sdan 3227381bd5Sdandb func sql sql 3327381bd5Sdandb trace trace 3427381bd5Sdan 3527381bd5Sdanproc do_trace_test {tn sql expected} { 3627381bd5Sdan # Test that the list of string passed to the trace callback when $sql 3727381bd5Sdan # is executed is equivalent to the list of strings in $expected. 3827381bd5Sdan # 3927381bd5Sdan set ::trace [list] 4027381bd5Sdan execsql $sql 4127381bd5Sdan uplevel do_test $tn [list {set ::trace}] [list [list {*}$expected]] 4227381bd5Sdan} 4327381bd5Sdan 4427381bd5Sdanproc do_trace_select_test {tn sql expected} { 4527381bd5Sdan 4627381bd5Sdan uplevel [list do_trace_test ${tn}.a $sql $expected] 4727381bd5Sdan 4827381bd5Sdan # Now execute each SQL statement passed to the trace callback in the 4927381bd5Sdan # block above. Check that this causes the same set of strings to be 5027381bd5Sdan # passed to the trace callback again. i.e. that executing the output 5127381bd5Sdan # of the trace callback is equivalent to the SQL script in $sql. 5227381bd5Sdan # 5327381bd5Sdan set sqllist $::trace 5427381bd5Sdan set ::trace [list] 5527381bd5Sdan foreach item $sqllist { execsql $item } 5627381bd5Sdan uplevel do_test $tn.b [list {set ::trace}] [list $sqllist] 5727381bd5Sdan} 5827381bd5Sdan 5927381bd5Sdando_trace_select_test 1.1 { 6027381bd5Sdan SELECT 1, 2, 3; 6127381bd5Sdan} { 6227381bd5Sdan "SELECT 1, 2, 3;" 6327381bd5Sdan} 6427381bd5Sdan 6527381bd5Sdando_trace_select_test 1.2 { 6627381bd5Sdan SELECT sql('SELECT 1, 2, 3'); 6727381bd5Sdan} { 6827381bd5Sdan "SELECT sql('SELECT 1, 2, 3');" 6927381bd5Sdan "-- SELECT 1, 2, 3" 7027381bd5Sdan} 7127381bd5Sdan 7227381bd5Sdando_trace_select_test 1.3 { 7327381bd5Sdan SELECT sql('SELECT 1, 7427381bd5Sdan 2, 7527381bd5Sdan 3' 7627381bd5Sdan ); 7727381bd5Sdan} { 7827381bd5Sdan "SELECT sql('SELECT 1, 7927381bd5Sdan 2, 8027381bd5Sdan 3' 8127381bd5Sdan );" 8227381bd5Sdan "-- SELECT 1, 8327381bd5Sdan-- 2, 8427381bd5Sdan-- 3" 8527381bd5Sdan} 8627381bd5Sdan 8727381bd5Sdando_trace_select_test 1.4 { 8827381bd5Sdan SELECT sql('SELECT 1, 8927381bd5Sdan 9027381bd5Sdan 9127381bd5Sdan 3' 9227381bd5Sdan ); 9327381bd5Sdan} { 9427381bd5Sdan "SELECT sql('SELECT 1, 9527381bd5Sdan 9627381bd5Sdan 9727381bd5Sdan 3' 9827381bd5Sdan );" 9927381bd5Sdan "-- SELECT 1, 10027381bd5Sdan-- 10127381bd5Sdan-- 10227381bd5Sdan-- 3" 10327381bd5Sdan} 10427381bd5Sdan 10527381bd5Sdando_trace_select_test 1.5 { 10627381bd5Sdan SELECT $var, sql('SELECT 1, 10727381bd5Sdan $var, 10827381bd5Sdan 3' 10927381bd5Sdan ); 11027381bd5Sdan} { 11127381bd5Sdan "SELECT NULL, sql('SELECT 1, 11227381bd5Sdan $var, 11327381bd5Sdan 3' 11427381bd5Sdan );" 11527381bd5Sdan "-- SELECT 1, 11627381bd5Sdan-- $var, 11727381bd5Sdan-- 3" 11827381bd5Sdan} 11927381bd5Sdan 12027381bd5Sdanifcapable fts3 { 12127381bd5Sdan do_execsql_test 2.1 { 12227381bd5Sdan CREATE VIRTUAL TABLE x1 USING fts4; 12327381bd5Sdan INSERT INTO x1 VALUES('Cloudy, with a high near 16'); 12427381bd5Sdan INSERT INTO x1 VALUES('Wind chill values as low as -13'); 12527381bd5Sdan } 12627381bd5Sdan 12727381bd5Sdan do_trace_test 2.2 { 12827381bd5Sdan INSERT INTO x1 VALUES('North northwest wind between 8 and 14 mph'); 12927381bd5Sdan } { 13027381bd5Sdan "INSERT INTO x1 VALUES('North northwest wind between 8 and 14 mph');" 13187ddfeb0Sdan "-- DELETE FROM 'main'.'x1_segdir' WHERE level = ?" 132a240fd01Sdan "-- INSERT INTO 'main'.'x1_content' VALUES(?,(?))" 13327381bd5Sdan "-- REPLACE INTO 'main'.'x1_docsize' VALUES(?,?)" 13440c21432Sdan "-- SELECT value FROM 'main'.'x1_stat' WHERE id=?" 135865e26afSdan "-- REPLACE INTO 'main'.'x1_stat' VALUES(?,?)" 13627381bd5Sdan "-- SELECT (SELECT max(idx) FROM 'main'.'x1_segdir' WHERE level = ?) + 1" 13727381bd5Sdan "-- SELECT coalesce((SELECT max(blockid) FROM 'main'.'x1_segments') + 1, 1)" 138e2392339Sdrh "-- REPLACE INTO 'main'.'x1_segdir' VALUES(?,?,?,?,?,?)" 1395b34f15aSdan "-- SELECT level, idx, end_block FROM 'main'.'x1_segdir' WHERE level BETWEEN ? AND ? ORDER BY level DESC, idx ASC" 14027381bd5Sdan } 14127381bd5Sdan 14227381bd5Sdan do_trace_test 2.3 { 14327381bd5Sdan INSERT INTO x1(x1) VALUES('optimize'); 14427381bd5Sdan } { 14527381bd5Sdan "INSERT INTO x1(x1) VALUES('optimize');" 146*38f20240Sdan "-- SELECT ? UNION SELECT level / (1024 * ?) FROM 'main'.'x1_segdir'" 147653265d9Sdan "-- SELECT idx, start_block, leaves_end_block, end_block, root FROM 'main'.'x1_segdir' WHERE level BETWEEN ? AND ?ORDER BY level DESC, idx ASC" 148653265d9Sdan "-- SELECT max(level) FROM 'main'.'x1_segdir' WHERE level BETWEEN ? AND ?" 14927381bd5Sdan "-- SELECT coalesce((SELECT max(blockid) FROM 'main'.'x1_segments') + 1, 1)" 150653265d9Sdan "-- DELETE FROM 'main'.'x1_segdir' WHERE level BETWEEN ? AND ?" 151e2392339Sdrh "-- REPLACE INTO 'main'.'x1_segdir' VALUES(?,?,?,?,?,?)" 15227381bd5Sdan } 15327381bd5Sdan} 15427381bd5Sdan 15527381bd5Sdanfinish_test 156