os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/trigger7.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
# 2005 August 18
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 to increase coverage of trigger.c.
sl@0
    14
#
sl@0
    15
# $Id: trigger7.test,v 1.3 2008/08/11 18:44:58 drh Exp $
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
ifcapable {!trigger} {
sl@0
    20
  finish_test
sl@0
    21
  return
sl@0
    22
}
sl@0
    23
sl@0
    24
sl@0
    25
# Error messages resulting from qualified trigger names.
sl@0
    26
#
sl@0
    27
do_test trigger7-1.1 {
sl@0
    28
  execsql {
sl@0
    29
    CREATE TABLE t1(x, y);
sl@0
    30
  }
sl@0
    31
  catchsql {
sl@0
    32
    CREATE TEMP TRIGGER main.r1 AFTER INSERT ON t1 BEGIN
sl@0
    33
      SELECT 'no nothing';
sl@0
    34
    END
sl@0
    35
  }
sl@0
    36
} {1 {temporary trigger may not have qualified name}}
sl@0
    37
do_test trigger7-1.2 {
sl@0
    38
  catchsql {
sl@0
    39
    CREATE TRIGGER not_a_db.r1 AFTER INSERT ON t1 BEGIN
sl@0
    40
      SELECT 'no nothing';
sl@0
    41
    END
sl@0
    42
  }
sl@0
    43
} {1 {unknown database not_a_db}}
sl@0
    44
sl@0
    45
sl@0
    46
# When the UPDATE OF syntax is used, no code is generated for triggers
sl@0
    47
# that do not match the update columns.
sl@0
    48
#
sl@0
    49
ifcapable explain {
sl@0
    50
  do_test trigger7-2.1 {
sl@0
    51
    execsql {
sl@0
    52
      CREATE TRIGGER r1 AFTER UPDATE OF x ON t1 BEGIN
sl@0
    53
        SELECT '___update_t1.x___';
sl@0
    54
      END;
sl@0
    55
      CREATE TRIGGER r2 AFTER UPDATE OF y ON t1 BEGIN
sl@0
    56
        SELECT '___update_t1.y___';
sl@0
    57
      END;
sl@0
    58
    }
sl@0
    59
    set txt [db eval {EXPLAIN UPDATE t1 SET x=5}]
sl@0
    60
    string match *___update_t1.x___* $txt
sl@0
    61
  } 1
sl@0
    62
  do_test trigger7-2.2 {
sl@0
    63
    set txt [db eval {EXPLAIN UPDATE t1 SET x=5}]
sl@0
    64
    string match *___update_t1.y___* $txt
sl@0
    65
  } 0
sl@0
    66
  do_test trigger7-2.3 {
sl@0
    67
    set txt [db eval {EXPLAIN UPDATE t1 SET y=5}]
sl@0
    68
    string match *___update_t1.x___* $txt
sl@0
    69
  } 0
sl@0
    70
  do_test trigger7-2.4 {
sl@0
    71
    set txt [db eval {EXPLAIN UPDATE t1 SET y=5}]
sl@0
    72
    string match *___update_t1.y___* $txt
sl@0
    73
  } 1
sl@0
    74
  do_test trigger7-2.5 {
sl@0
    75
    set txt [db eval {EXPLAIN UPDATE t1 SET rowid=5}]
sl@0
    76
    string match *___update_t1.x___* $txt
sl@0
    77
  } 0
sl@0
    78
  do_test trigger7-2.6 {
sl@0
    79
    set txt [db eval {EXPLAIN UPDATE t1 SET rowid=5}]
sl@0
    80
    string match *___update_t1.x___* $txt
sl@0
    81
  } 0
sl@0
    82
}
sl@0
    83
sl@0
    84
# Test the ability to create many triggers on the same table, then
sl@0
    85
# selectively drop those triggers.
sl@0
    86
#
sl@0
    87
do_test trigger7-3.1 {
sl@0
    88
  execsql {
sl@0
    89
    CREATE TABLE t2(x,y,z);
sl@0
    90
    CREATE TRIGGER t2r1 AFTER INSERT ON t2 BEGIN SELECT 1; END;
sl@0
    91
    CREATE TRIGGER t2r2 BEFORE INSERT ON t2 BEGIN SELECT 1; END;
sl@0
    92
    CREATE TRIGGER t2r3 AFTER UPDATE ON t2 BEGIN SELECT 1; END;
sl@0
    93
    CREATE TRIGGER t2r4 BEFORE UPDATE ON t2 BEGIN SELECT 1; END;
sl@0
    94
    CREATE TRIGGER t2r5 AFTER DELETE ON t2 BEGIN SELECT 1; END;
sl@0
    95
    CREATE TRIGGER t2r6 BEFORE DELETE ON t2 BEGIN SELECT 1; END;
sl@0
    96
    CREATE TRIGGER t2r7 AFTER INSERT ON t2 BEGIN SELECT 1; END;
sl@0
    97
    CREATE TRIGGER t2r8 BEFORE INSERT ON t2 BEGIN SELECT 1; END;
sl@0
    98
    CREATE TRIGGER t2r9 AFTER UPDATE ON t2 BEGIN SELECT 1; END;
sl@0
    99
    CREATE TRIGGER t2r10 BEFORE UPDATE ON t2 BEGIN SELECT 1; END;
sl@0
   100
    CREATE TRIGGER t2r11 AFTER DELETE ON t2 BEGIN SELECT 1; END;
sl@0
   101
    CREATE TRIGGER t2r12 BEFORE DELETE ON t2 BEGIN SELECT 1; END;
sl@0
   102
    DROP TRIGGER t2r6;
sl@0
   103
  }
sl@0
   104
} {}
sl@0
   105
sl@0
   106
# This test corrupts the database file so it must be the last test
sl@0
   107
# in the series.
sl@0
   108
#
sl@0
   109
do_test trigger7-99.1 {
sl@0
   110
  execsql {
sl@0
   111
    PRAGMA writable_schema=on;
sl@0
   112
    UPDATE sqlite_master SET sql='nonsense';
sl@0
   113
  }
sl@0
   114
  db close
sl@0
   115
  sqlite3 db test.db
sl@0
   116
  catchsql {
sl@0
   117
    DROP TRIGGER t2r5
sl@0
   118
  }
sl@0
   119
} {1 {malformed database schema (t2r12) - near "nonsense": syntax error}}
sl@0
   120
sl@0
   121
finish_test