2002-06-11 06:25:40 +04:00
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
#
|
|
|
|
# This file tests the RAISE() function.
|
|
|
|
#
|
|
|
|
|
2004-06-10 05:30:59 +04:00
|
|
|
|
2002-06-11 06:25:40 +04:00
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
2004-10-31 05:22:47 +03:00
|
|
|
ifcapable {!trigger} {
|
|
|
|
finish_test
|
|
|
|
return
|
|
|
|
}
|
2002-06-11 06:25:40 +04:00
|
|
|
|
2009-08-30 15:42:51 +04:00
|
|
|
# The tests in this file were written before SQLite supported recursive }
|
|
|
|
# trigger invocation, and some tests depend on that to pass. So disable
|
|
|
|
# recursive triggers for this file.
|
2009-09-01 21:11:07 +04:00
|
|
|
catchsql { pragma recursive_triggers = off }
|
2002-06-11 06:25:40 +04:00
|
|
|
|
2009-08-30 15:42:51 +04:00
|
|
|
# Test that we can cause ROLLBACK, FAIL and ABORT correctly
|
|
|
|
#
|
|
|
|
catchsql { CREATE TABLE tbl(a, b ,c) }
|
2002-06-11 06:25:40 +04:00
|
|
|
execsql {
|
|
|
|
CREATE TRIGGER before_tbl_insert BEFORE INSERT ON tbl BEGIN SELECT CASE
|
2009-09-19 21:00:31 +04:00
|
|
|
WHEN (new.a = 4) THEN RAISE(IGNORE) END;
|
2002-06-11 06:25:40 +04:00
|
|
|
END;
|
|
|
|
|
|
|
|
CREATE TRIGGER after_tbl_insert AFTER INSERT ON tbl BEGIN SELECT CASE
|
2009-09-19 21:00:31 +04:00
|
|
|
WHEN (new.a = 1) THEN RAISE(ABORT, 'Trigger abort')
|
|
|
|
WHEN (new.a = 2) THEN RAISE(FAIL, 'Trigger fail')
|
|
|
|
WHEN (new.a = 3) THEN RAISE(ROLLBACK, 'Trigger rollback') END;
|
2002-06-11 06:25:40 +04:00
|
|
|
END;
|
|
|
|
}
|
|
|
|
# ABORT
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-1.1 {
|
2002-06-11 06:25:40 +04:00
|
|
|
catchsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
BEGIN;
|
2002-06-11 06:25:40 +04:00
|
|
|
INSERT INTO tbl VALUES (5, 5, 6);
|
|
|
|
INSERT INTO tbl VALUES (1, 5, 6);
|
|
|
|
}
|
|
|
|
} {1 {Trigger abort}}
|
2013-02-09 19:37:11 +04:00
|
|
|
verify_ex_errcode trigger3-1.1b SQLITE_CONSTRAINT_TRIGGER
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-1.2 {
|
2002-06-11 06:25:40 +04:00
|
|
|
execsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
SELECT * FROM tbl;
|
|
|
|
ROLLBACK;
|
2002-06-11 06:25:40 +04:00
|
|
|
}
|
|
|
|
} {5 5 6}
|
2003-01-16 16:42:43 +03:00
|
|
|
do_test trigger3-1.3 {
|
|
|
|
execsql {SELECT * FROM tbl}
|
|
|
|
} {}
|
2002-06-11 06:25:40 +04:00
|
|
|
|
|
|
|
# FAIL
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-2.1 {
|
2002-06-11 06:25:40 +04:00
|
|
|
catchsql {
|
2009-08-30 15:42:51 +04:00
|
|
|
BEGIN;
|
2002-06-11 06:25:40 +04:00
|
|
|
INSERT INTO tbl VALUES (5, 5, 6);
|
|
|
|
INSERT INTO tbl VALUES (2, 5, 6);
|
|
|
|
}
|
|
|
|
} {1 {Trigger fail}}
|
2013-02-09 19:37:11 +04:00
|
|
|
verify_ex_errcode trigger3-2.1b SQLITE_CONSTRAINT_TRIGGER
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-2.2 {
|
2002-06-11 06:25:40 +04:00
|
|
|
execsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
SELECT * FROM tbl;
|
|
|
|
ROLLBACK;
|
2002-06-11 06:25:40 +04:00
|
|
|
}
|
|
|
|
} {5 5 6 2 5 6}
|
|
|
|
# ROLLBACK
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-3.1 {
|
2002-06-11 06:25:40 +04:00
|
|
|
catchsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
BEGIN;
|
2002-06-11 06:25:40 +04:00
|
|
|
INSERT INTO tbl VALUES (5, 5, 6);
|
|
|
|
INSERT INTO tbl VALUES (3, 5, 6);
|
|
|
|
}
|
|
|
|
} {1 {Trigger rollback}}
|
2013-02-09 19:37:11 +04:00
|
|
|
verify_ex_errcode trigger3-3.1b SQLITE_CONSTRAINT_TRIGGER
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-3.2 {
|
2002-06-11 06:25:40 +04:00
|
|
|
execsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
SELECT * FROM tbl;
|
2002-06-11 06:25:40 +04:00
|
|
|
}
|
|
|
|
} {}
|
2008-04-10 19:12:45 +04:00
|
|
|
|
|
|
|
# Verify that a ROLLBACK trigger works like a FAIL trigger if
|
|
|
|
# we are not within a transaction. Ticket #3035.
|
|
|
|
#
|
|
|
|
do_test trigger3-3.3 {
|
|
|
|
catchsql {COMMIT}
|
|
|
|
catchsql {
|
|
|
|
INSERT INTO tbl VALUES (3, 9, 10);
|
|
|
|
}
|
|
|
|
} {1 {Trigger rollback}}
|
2013-02-09 19:37:11 +04:00
|
|
|
verify_ex_errcode trigger3-3.3b SQLITE_CONSTRAINT_TRIGGER
|
2008-04-10 19:12:45 +04:00
|
|
|
do_test trigger3-3.4 {
|
|
|
|
execsql {SELECT * FROM tbl}
|
|
|
|
} {}
|
|
|
|
|
2002-06-11 06:25:40 +04:00
|
|
|
# IGNORE
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-4.1 {
|
2002-06-11 06:25:40 +04:00
|
|
|
catchsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
BEGIN;
|
2002-06-11 06:25:40 +04:00
|
|
|
INSERT INTO tbl VALUES (5, 5, 6);
|
|
|
|
INSERT INTO tbl VALUES (4, 5, 6);
|
|
|
|
}
|
|
|
|
} {0 {}}
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-4.2 {
|
2002-06-11 06:25:40 +04:00
|
|
|
execsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
SELECT * FROM tbl;
|
|
|
|
ROLLBACK;
|
2002-06-11 06:25:40 +04:00
|
|
|
}
|
|
|
|
} {5 5 6}
|
|
|
|
|
|
|
|
# Check that we can also do RAISE(IGNORE) for UPDATE and DELETE
|
|
|
|
execsql {DROP TABLE tbl;}
|
|
|
|
execsql {CREATE TABLE tbl (a, b, c);}
|
|
|
|
execsql {INSERT INTO tbl VALUES(1, 2, 3);}
|
|
|
|
execsql {INSERT INTO tbl VALUES(4, 5, 6);}
|
|
|
|
execsql {
|
|
|
|
CREATE TRIGGER before_tbl_update BEFORE UPDATE ON tbl BEGIN
|
2009-09-19 21:00:31 +04:00
|
|
|
SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
|
2002-06-11 06:25:40 +04:00
|
|
|
END;
|
|
|
|
|
|
|
|
CREATE TRIGGER before_tbl_delete BEFORE DELETE ON tbl BEGIN
|
2009-09-19 21:00:31 +04:00
|
|
|
SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
|
2002-06-11 06:25:40 +04:00
|
|
|
END;
|
|
|
|
}
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-5.1 {
|
2002-06-11 06:25:40 +04:00
|
|
|
execsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
UPDATE tbl SET c = 10;
|
|
|
|
SELECT * FROM tbl;
|
2002-06-11 06:25:40 +04:00
|
|
|
}
|
|
|
|
} {1 2 3 4 5 10}
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-5.2 {
|
2002-06-11 06:25:40 +04:00
|
|
|
execsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
DELETE FROM tbl;
|
|
|
|
SELECT * FROM tbl;
|
2002-06-11 06:25:40 +04:00
|
|
|
}
|
|
|
|
} {1 2 3}
|
|
|
|
|
|
|
|
# Check that RAISE(IGNORE) works correctly for nested triggers:
|
|
|
|
execsql {CREATE TABLE tbl2(a, b, c)}
|
|
|
|
execsql {
|
|
|
|
CREATE TRIGGER after_tbl2_insert AFTER INSERT ON tbl2 BEGIN
|
2009-09-19 21:00:31 +04:00
|
|
|
UPDATE tbl SET c = 10;
|
2002-06-11 06:25:40 +04:00
|
|
|
INSERT INTO tbl2 VALUES (new.a, new.b, new.c);
|
|
|
|
END;
|
|
|
|
}
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-6 {
|
2002-06-11 06:25:40 +04:00
|
|
|
execsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
INSERT INTO tbl2 VALUES (1, 2, 3);
|
|
|
|
SELECT * FROM tbl2;
|
|
|
|
SELECT * FROM tbl;
|
2002-06-11 06:25:40 +04:00
|
|
|
}
|
|
|
|
} {1 2 3 1 2 3 1 2 3}
|
|
|
|
|
|
|
|
# Check that things also work for view-triggers
|
2004-11-22 11:43:32 +03:00
|
|
|
|
|
|
|
ifcapable view {
|
|
|
|
|
2002-06-11 06:25:40 +04:00
|
|
|
execsql {CREATE VIEW tbl_view AS SELECT * FROM tbl}
|
|
|
|
execsql {
|
|
|
|
CREATE TRIGGER tbl_view_insert INSTEAD OF INSERT ON tbl_view BEGIN
|
2009-09-19 21:00:31 +04:00
|
|
|
SELECT CASE WHEN (new.a = 1) THEN RAISE(ROLLBACK, 'View rollback')
|
|
|
|
WHEN (new.a = 2) THEN RAISE(IGNORE)
|
|
|
|
WHEN (new.a = 3) THEN RAISE(ABORT, 'View abort') END;
|
2002-06-11 06:25:40 +04:00
|
|
|
END;
|
|
|
|
}
|
|
|
|
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-7.1 {
|
2002-06-11 06:25:40 +04:00
|
|
|
catchsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
INSERT INTO tbl_view VALUES(1, 2, 3);
|
2002-06-11 06:25:40 +04:00
|
|
|
}
|
|
|
|
} {1 {View rollback}}
|
2013-02-09 19:37:11 +04:00
|
|
|
verify_ex_errcode trigger3-7.1b SQLITE_CONSTRAINT_TRIGGER
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-7.2 {
|
2002-06-11 06:25:40 +04:00
|
|
|
catchsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
INSERT INTO tbl_view VALUES(2, 2, 3);
|
2002-06-11 06:25:40 +04:00
|
|
|
}
|
|
|
|
} {0 {}}
|
2002-09-14 16:04:56 +04:00
|
|
|
do_test trigger3-7.3 {
|
2002-06-11 06:25:40 +04:00
|
|
|
catchsql {
|
2009-09-19 21:00:31 +04:00
|
|
|
INSERT INTO tbl_view VALUES(3, 2, 3);
|
2002-06-11 06:25:40 +04:00
|
|
|
}
|
|
|
|
} {1 {View abort}}
|
2013-02-09 19:37:11 +04:00
|
|
|
verify_ex_errcode trigger3-7.3b SQLITE_CONSTRAINT_TRIGGER
|
2002-06-11 06:25:40 +04:00
|
|
|
|
2004-11-22 11:43:32 +03:00
|
|
|
} ;# ifcapable view
|
|
|
|
|
2003-06-16 03:42:24 +04:00
|
|
|
integrity_check trigger3-8.1
|
|
|
|
|
2002-06-11 06:25:40 +04:00
|
|
|
catchsql { DROP TABLE tbl; }
|
|
|
|
catchsql { DROP TABLE tbl2; }
|
2002-06-25 05:09:11 +04:00
|
|
|
catchsql { DROP VIEW tbl_view; }
|
2002-06-11 06:25:40 +04:00
|
|
|
|
2002-06-25 05:09:11 +04:00
|
|
|
finish_test
|