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.
     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 #
    15 # $Id: trace.test,v 1.7 2008/01/12 21:35:57 drh Exp $
    16 
    17 set testdir [file dirname $argv0]
    18 source $testdir/tester.tcl
    19 
    20 ifcapable !trace {
    21   finish_test
    22   return
    23 }
    24 
    25 set ::stmtlist {}
    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
    46 } {{CREATE TABLE t1(a,b);} {INSERT INTO t1 VALUES(1,2);} {SELECT * FROM t1;}}
    47 do_test trace-1.5 {
    48   db trace {}
    49   db trace
    50 } {}
    51 
    52 # If we prepare a statement and execute it multiple times, the trace
    53 # happens on each execution.
    54 #
    55 db close
    56 sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
    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}
    83 catch {sqlite3_finalize $STMT}
    84 
    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
   119 sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
   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}
   147 
   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 
   169 finish_test