os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/trigger3.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
# The author disclaims copyright to this source code.  In place of
sl@0
     2
# a legal notice, here is a blessing:
sl@0
     3
#
sl@0
     4
#    May you do good and not evil.
sl@0
     5
#    May you find forgiveness for yourself and forgive others.
sl@0
     6
#    May you share freely, never taking more than you give.
sl@0
     7
#
sl@0
     8
#***********************************************************************
sl@0
     9
#
sl@0
    10
# This file tests the RAISE() function.
sl@0
    11
#
sl@0
    12
sl@0
    13
sl@0
    14
set testdir [file dirname $argv0]
sl@0
    15
source $testdir/tester.tcl
sl@0
    16
ifcapable {!trigger} {
sl@0
    17
  finish_test
sl@0
    18
  return
sl@0
    19
}
sl@0
    20
sl@0
    21
# Test that we can cause ROLLBACK, FAIL and ABORT correctly
sl@0
    22
# catchsql { DROP TABLE tbl; }
sl@0
    23
catchsql { CREATE TABLE tbl (a, b, c) }
sl@0
    24
sl@0
    25
execsql {
sl@0
    26
    CREATE TRIGGER before_tbl_insert BEFORE INSERT ON tbl BEGIN SELECT CASE 
sl@0
    27
	WHEN (new.a = 4) THEN RAISE(IGNORE) END;
sl@0
    28
    END;
sl@0
    29
sl@0
    30
    CREATE TRIGGER after_tbl_insert AFTER INSERT ON tbl BEGIN SELECT CASE 
sl@0
    31
	WHEN (new.a = 1) THEN RAISE(ABORT,    'Trigger abort') 
sl@0
    32
	WHEN (new.a = 2) THEN RAISE(FAIL,     'Trigger fail') 
sl@0
    33
	WHEN (new.a = 3) THEN RAISE(ROLLBACK, 'Trigger rollback') END;
sl@0
    34
    END;
sl@0
    35
}
sl@0
    36
# ABORT
sl@0
    37
do_test trigger3-1.1 {
sl@0
    38
    catchsql {
sl@0
    39
	BEGIN;
sl@0
    40
        INSERT INTO tbl VALUES (5, 5, 6);
sl@0
    41
        INSERT INTO tbl VALUES (1, 5, 6);
sl@0
    42
    }
sl@0
    43
} {1 {Trigger abort}}
sl@0
    44
do_test trigger3-1.2 {
sl@0
    45
    execsql {
sl@0
    46
	SELECT * FROM tbl;
sl@0
    47
	ROLLBACK;
sl@0
    48
    }
sl@0
    49
} {5 5 6}
sl@0
    50
do_test trigger3-1.3 {
sl@0
    51
    execsql {SELECT * FROM tbl}
sl@0
    52
} {}
sl@0
    53
sl@0
    54
# FAIL
sl@0
    55
do_test trigger3-2.1 {
sl@0
    56
    catchsql {
sl@0
    57
	BEGIN;
sl@0
    58
        INSERT INTO tbl VALUES (5, 5, 6);
sl@0
    59
        INSERT INTO tbl VALUES (2, 5, 6);
sl@0
    60
    }
sl@0
    61
} {1 {Trigger fail}}
sl@0
    62
do_test trigger3-2.2 {
sl@0
    63
    execsql {
sl@0
    64
	SELECT * FROM tbl;
sl@0
    65
	ROLLBACK;
sl@0
    66
    }
sl@0
    67
} {5 5 6 2 5 6}
sl@0
    68
# ROLLBACK
sl@0
    69
do_test trigger3-3.1 {
sl@0
    70
    catchsql {
sl@0
    71
	BEGIN;
sl@0
    72
        INSERT INTO tbl VALUES (5, 5, 6);
sl@0
    73
        INSERT INTO tbl VALUES (3, 5, 6);
sl@0
    74
    }
sl@0
    75
} {1 {Trigger rollback}}
sl@0
    76
do_test trigger3-3.2 {
sl@0
    77
    execsql {
sl@0
    78
	SELECT * FROM tbl;
sl@0
    79
    }
sl@0
    80
} {}
sl@0
    81
sl@0
    82
# Verify that a ROLLBACK trigger works like a FAIL trigger if
sl@0
    83
# we are not within a transaction.  Ticket #3035.
sl@0
    84
#
sl@0
    85
do_test trigger3-3.3 {
sl@0
    86
    catchsql {COMMIT}
sl@0
    87
    catchsql {
sl@0
    88
        INSERT INTO tbl VALUES (3, 9, 10);
sl@0
    89
    }
sl@0
    90
} {1 {Trigger rollback}}
sl@0
    91
do_test trigger3-3.4 {
sl@0
    92
    execsql {SELECT * FROM tbl}
sl@0
    93
} {}
sl@0
    94
sl@0
    95
# IGNORE
sl@0
    96
do_test trigger3-4.1 {
sl@0
    97
    catchsql {
sl@0
    98
	BEGIN;
sl@0
    99
        INSERT INTO tbl VALUES (5, 5, 6);
sl@0
   100
        INSERT INTO tbl VALUES (4, 5, 6);
sl@0
   101
    }
sl@0
   102
} {0 {}}
sl@0
   103
do_test trigger3-4.2 {
sl@0
   104
    execsql {
sl@0
   105
	SELECT * FROM tbl;
sl@0
   106
	ROLLBACK;
sl@0
   107
    }
sl@0
   108
} {5 5 6}
sl@0
   109
sl@0
   110
# Check that we can also do RAISE(IGNORE) for UPDATE and DELETE
sl@0
   111
execsql {DROP TABLE tbl;}
sl@0
   112
execsql {CREATE TABLE tbl (a, b, c);}
sl@0
   113
execsql {INSERT INTO tbl VALUES(1, 2, 3);}
sl@0
   114
execsql {INSERT INTO tbl VALUES(4, 5, 6);}
sl@0
   115
execsql {
sl@0
   116
    CREATE TRIGGER before_tbl_update BEFORE UPDATE ON tbl BEGIN
sl@0
   117
	SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
sl@0
   118
    END;
sl@0
   119
sl@0
   120
    CREATE TRIGGER before_tbl_delete BEFORE DELETE ON tbl BEGIN
sl@0
   121
	SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
sl@0
   122
    END;
sl@0
   123
}
sl@0
   124
do_test trigger3-5.1 {
sl@0
   125
    execsql {
sl@0
   126
	UPDATE tbl SET c = 10;
sl@0
   127
	SELECT * FROM tbl;
sl@0
   128
    }
sl@0
   129
} {1 2 3 4 5 10}
sl@0
   130
do_test trigger3-5.2 {
sl@0
   131
    execsql {
sl@0
   132
	DELETE FROM tbl;
sl@0
   133
	SELECT * FROM tbl;
sl@0
   134
    }
sl@0
   135
} {1 2 3}
sl@0
   136
sl@0
   137
# Check that RAISE(IGNORE) works correctly for nested triggers:
sl@0
   138
execsql {CREATE TABLE tbl2(a, b, c)}
sl@0
   139
execsql {
sl@0
   140
    CREATE TRIGGER after_tbl2_insert AFTER INSERT ON tbl2 BEGIN
sl@0
   141
	UPDATE tbl SET c = 10;
sl@0
   142
        INSERT INTO tbl2 VALUES (new.a, new.b, new.c);
sl@0
   143
    END;
sl@0
   144
}
sl@0
   145
do_test trigger3-6 {
sl@0
   146
    execsql {
sl@0
   147
	INSERT INTO tbl2 VALUES (1, 2, 3);
sl@0
   148
	SELECT * FROM tbl2;
sl@0
   149
	SELECT * FROM tbl;
sl@0
   150
    }
sl@0
   151
} {1 2 3 1 2 3 1 2 3}
sl@0
   152
sl@0
   153
# Check that things also work for view-triggers
sl@0
   154
sl@0
   155
ifcapable view {
sl@0
   156
sl@0
   157
execsql {CREATE VIEW tbl_view AS SELECT * FROM tbl}
sl@0
   158
execsql {
sl@0
   159
    CREATE TRIGGER tbl_view_insert INSTEAD OF INSERT ON tbl_view BEGIN
sl@0
   160
	SELECT CASE WHEN (new.a = 1) THEN RAISE(ROLLBACK, 'View rollback')
sl@0
   161
	            WHEN (new.a = 2) THEN RAISE(IGNORE) 
sl@0
   162
	            WHEN (new.a = 3) THEN RAISE(ABORT, 'View abort') END;
sl@0
   163
    END;
sl@0
   164
}
sl@0
   165
sl@0
   166
do_test trigger3-7.1 {
sl@0
   167
    catchsql {
sl@0
   168
	INSERT INTO tbl_view VALUES(1, 2, 3);
sl@0
   169
    }
sl@0
   170
} {1 {View rollback}}
sl@0
   171
do_test trigger3-7.2 {
sl@0
   172
    catchsql {
sl@0
   173
	INSERT INTO tbl_view VALUES(2, 2, 3);
sl@0
   174
    }
sl@0
   175
} {0 {}}
sl@0
   176
do_test trigger3-7.3 {
sl@0
   177
    catchsql {
sl@0
   178
	INSERT INTO tbl_view VALUES(3, 2, 3);
sl@0
   179
    }
sl@0
   180
} {1 {View abort}}
sl@0
   181
sl@0
   182
} ;# ifcapable view
sl@0
   183
sl@0
   184
integrity_check trigger3-8.1
sl@0
   185
sl@0
   186
catchsql { DROP TABLE tbl; } 
sl@0
   187
catchsql { DROP TABLE tbl2; } 
sl@0
   188
catchsql { DROP VIEW tbl_view; }
sl@0
   189
sl@0
   190
finish_test