os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/trace.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2004 Jun 29
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.
sl@0
    12
#
sl@0
    13
# This file implements tests for the "sqlite3_trace()" API.
sl@0
    14
#
sl@0
    15
# $Id: trace.test,v 1.7 2008/01/12 21:35:57 drh Exp $
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
ifcapable !trace {
sl@0
    21
  finish_test
sl@0
    22
  return
sl@0
    23
}
sl@0
    24
sl@0
    25
set ::stmtlist {}
sl@0
    26
do_test trace-1.1 {
sl@0
    27
  set rc [catch {db trace 1 2 3} msg]
sl@0
    28
  lappend rc $msg
sl@0
    29
} {1 {wrong # args: should be "db trace ?CALLBACK?"}}
sl@0
    30
proc trace_proc cmd {
sl@0
    31
  lappend ::stmtlist [string trim $cmd]
sl@0
    32
}
sl@0
    33
do_test trace-1.2 {
sl@0
    34
  db trace trace_proc
sl@0
    35
  db trace
sl@0
    36
} {trace_proc}
sl@0
    37
do_test trace-1.3 {
sl@0
    38
  execsql {
sl@0
    39
    CREATE TABLE t1(a,b);
sl@0
    40
    INSERT INTO t1 VALUES(1,2);
sl@0
    41
    SELECT * FROM t1;
sl@0
    42
  }
sl@0
    43
} {1 2}
sl@0
    44
do_test trace-1.4 {
sl@0
    45
  set ::stmtlist
sl@0
    46
} {{CREATE TABLE t1(a,b);} {INSERT INTO t1 VALUES(1,2);} {SELECT * FROM t1;}}
sl@0
    47
do_test trace-1.5 {
sl@0
    48
  db trace {}
sl@0
    49
  db trace
sl@0
    50
} {}
sl@0
    51
sl@0
    52
# If we prepare a statement and execute it multiple times, the trace
sl@0
    53
# happens on each execution.
sl@0
    54
#
sl@0
    55
db close
sl@0
    56
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
    57
do_test trace-2.1 {
sl@0
    58
  set STMT [sqlite3_prepare $DB {INSERT INTO t1 VALUES(2,3)} -1 TAIL]
sl@0
    59
  db trace trace_proc
sl@0
    60
  proc trace_proc sql {
sl@0
    61
    global TRACE_OUT
sl@0
    62
    set TRACE_OUT $sql
sl@0
    63
  }
sl@0
    64
  set TRACE_OUT {}
sl@0
    65
  sqlite3_step $STMT
sl@0
    66
  set TRACE_OUT
sl@0
    67
} {INSERT INTO t1 VALUES(2,3)}
sl@0
    68
do_test trace-2.2 {
sl@0
    69
  set TRACE_OUT {}
sl@0
    70
  sqlite3_reset $STMT
sl@0
    71
  set TRACE_OUT 
sl@0
    72
} {}
sl@0
    73
do_test trace-2.3 {
sl@0
    74
  sqlite3_step $STMT
sl@0
    75
  set TRACE_OUT
sl@0
    76
} {INSERT INTO t1 VALUES(2,3)}
sl@0
    77
do_test trace-2.4 {
sl@0
    78
  execsql {SELECT * FROM t1}
sl@0
    79
} {1 2 2 3 2 3}
sl@0
    80
do_test trace-2.5 {
sl@0
    81
  set TRACE_OUT
sl@0
    82
} {SELECT * FROM t1}
sl@0
    83
catch {sqlite3_finalize $STMT}
sl@0
    84
sl@0
    85
# Similar tests, but this time for profiling.
sl@0
    86
# 
sl@0
    87
do_test trace-3.1 {
sl@0
    88
  set rc [catch {db profile 1 2 3} msg]
sl@0
    89
  lappend rc $msg
sl@0
    90
} {1 {wrong # args: should be "db profile ?CALLBACK?"}}
sl@0
    91
set ::stmtlist {}
sl@0
    92
proc profile_proc {cmd tm} {
sl@0
    93
  lappend ::stmtlist [string trim $cmd]
sl@0
    94
}
sl@0
    95
do_test trace-3.2 {
sl@0
    96
  db trace {}
sl@0
    97
  db profile profile_proc
sl@0
    98
  db profile
sl@0
    99
} {profile_proc}
sl@0
   100
do_test trace-3.3 {
sl@0
   101
  execsql {
sl@0
   102
    CREATE TABLE t2(a,b);
sl@0
   103
    INSERT INTO t2 VALUES(1,2);
sl@0
   104
    SELECT * FROM t2;
sl@0
   105
  }
sl@0
   106
} {1 2}
sl@0
   107
do_test trace-3.4 {
sl@0
   108
  set ::stmtlist
sl@0
   109
} {{CREATE TABLE t2(a,b);} {INSERT INTO t2 VALUES(1,2);} {SELECT * FROM t2;}}
sl@0
   110
do_test trace-3.5 {
sl@0
   111
  db profile {}
sl@0
   112
  db profile
sl@0
   113
} {}
sl@0
   114
sl@0
   115
# If we prepare a statement and execute it multiple times, the profile
sl@0
   116
# happens on each execution.
sl@0
   117
#
sl@0
   118
db close
sl@0
   119
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   120
do_test trace-4.1 {
sl@0
   121
  set STMT [sqlite3_prepare $DB {INSERT INTO t2 VALUES(2,3)} -1 TAIL]
sl@0
   122
  db trace trace_proc
sl@0
   123
  proc profile_proc {sql tm} {
sl@0
   124
    global TRACE_OUT
sl@0
   125
    set TRACE_OUT $sql
sl@0
   126
  }
sl@0
   127
  set TRACE_OUT {}
sl@0
   128
  sqlite3_step $STMT
sl@0
   129
  set TRACE_OUT
sl@0
   130
} {INSERT INTO t2 VALUES(2,3)}
sl@0
   131
do_test trace-4.2 {
sl@0
   132
  set TRACE_OUT {}
sl@0
   133
  sqlite3_reset $STMT
sl@0
   134
  set TRACE_OUT 
sl@0
   135
} {}
sl@0
   136
do_test trace-4.3 {
sl@0
   137
  sqlite3_step $STMT
sl@0
   138
  set TRACE_OUT
sl@0
   139
} {INSERT INTO t2 VALUES(2,3)}
sl@0
   140
do_test trace-4.4 {
sl@0
   141
  execsql {SELECT * FROM t1}
sl@0
   142
} {1 2 2 3 2 3}
sl@0
   143
do_test trace-4.5 {
sl@0
   144
  set TRACE_OUT
sl@0
   145
} {SELECT * FROM t1}
sl@0
   146
catch {sqlite3_finalize $STMT}
sl@0
   147
sl@0
   148
# Trigger tracing.
sl@0
   149
#
sl@0
   150
do_test trace-5.1 {
sl@0
   151
  db eval {
sl@0
   152
    CREATE TRIGGER r1t1 AFTER UPDATE ON t1 BEGIN
sl@0
   153
      UPDATE t2 SET a=new.a WHERE rowid=new.rowid;
sl@0
   154
    END;
sl@0
   155
    CREATE TRIGGER r1t2 AFTER UPDATE ON t2 BEGIN
sl@0
   156
      SELECT 'hello';
sl@0
   157
    END;
sl@0
   158
  }
sl@0
   159
  set TRACE_OUT {}
sl@0
   160
  proc trace_proc cmd {
sl@0
   161
    lappend ::TRACE_OUT [string trim $cmd]
sl@0
   162
  }
sl@0
   163
  db eval {
sl@0
   164
    UPDATE t1 SET a=a+1;
sl@0
   165
  }
sl@0
   166
  set TRACE_OUT
sl@0
   167
} {{UPDATE t1 SET a=a+1;} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2} {-- TRIGGER r1t1} {-- TRIGGER r1t2}}
sl@0
   168
sl@0
   169
finish_test