xref: /sqlite-3.40.0/test/trigger7.test (revision 6ab91a7a)
15eba8c09Sdrh# 2005 August 18
25eba8c09Sdrh#
35eba8c09Sdrh# The author disclaims copyright to this source code.  In place of
45eba8c09Sdrh# a legal notice, here is a blessing:
55eba8c09Sdrh#
65eba8c09Sdrh#    May you do good and not evil.
75eba8c09Sdrh#    May you find forgiveness for yourself and forgive others.
85eba8c09Sdrh#    May you share freely, never taking more than you give.
95eba8c09Sdrh#
105eba8c09Sdrh#***********************************************************************
115eba8c09Sdrh# This file implements regression tests for SQLite library.
125eba8c09Sdrh#
135eba8c09Sdrh# This file implements tests to increase coverage of trigger.c.
145eba8c09Sdrh#
15c456e57aSdrh# $Id: trigger7.test,v 1.3 2008/08/11 18:44:58 drh Exp $
165eba8c09Sdrh
175eba8c09Sdrhset testdir [file dirname $argv0]
185eba8c09Sdrhsource $testdir/tester.tcl
195eba8c09Sdrhifcapable {!trigger} {
205eba8c09Sdrh  finish_test
215eba8c09Sdrh  return
225eba8c09Sdrh}
235eba8c09Sdrh
245eba8c09Sdrh# Error messages resulting from qualified trigger names.
255eba8c09Sdrh#
265eba8c09Sdrhdo_test trigger7-1.1 {
275eba8c09Sdrh  execsql {
285eba8c09Sdrh    CREATE TABLE t1(x, y);
295eba8c09Sdrh  }
305eba8c09Sdrh  catchsql {
315eba8c09Sdrh    CREATE TEMP TRIGGER main.r1 AFTER INSERT ON t1 BEGIN
325eba8c09Sdrh      SELECT 'no nothing';
335eba8c09Sdrh    END
345eba8c09Sdrh  }
355eba8c09Sdrh} {1 {temporary trigger may not have qualified name}}
365eba8c09Sdrhdo_test trigger7-1.2 {
375eba8c09Sdrh  catchsql {
385eba8c09Sdrh    CREATE TRIGGER not_a_db.r1 AFTER INSERT ON t1 BEGIN
395eba8c09Sdrh      SELECT 'no nothing';
405eba8c09Sdrh    END
415eba8c09Sdrh  }
425eba8c09Sdrh} {1 {unknown database not_a_db}}
435eba8c09Sdrh
445eba8c09Sdrh
455eba8c09Sdrh# When the UPDATE OF syntax is used, no code is generated for triggers
465eba8c09Sdrh# that do not match the update columns.
475eba8c09Sdrh#
485eba8c09Sdrhifcapable explain {
495eba8c09Sdrh  do_test trigger7-2.1 {
505eba8c09Sdrh    execsql {
515eba8c09Sdrh      CREATE TRIGGER r1 AFTER UPDATE OF x ON t1 BEGIN
525eba8c09Sdrh        SELECT '___update_t1.x___';
535eba8c09Sdrh      END;
545eba8c09Sdrh      CREATE TRIGGER r2 AFTER UPDATE OF y ON t1 BEGIN
555eba8c09Sdrh        SELECT '___update_t1.y___';
565eba8c09Sdrh      END;
575eba8c09Sdrh    }
585eba8c09Sdrh    set txt [db eval {EXPLAIN UPDATE t1 SET x=5}]
595eba8c09Sdrh    string match *___update_t1.x___* $txt
605eba8c09Sdrh  } 1
615eba8c09Sdrh  do_test trigger7-2.2 {
625eba8c09Sdrh    set txt [db eval {EXPLAIN UPDATE t1 SET x=5}]
635eba8c09Sdrh    string match *___update_t1.y___* $txt
645eba8c09Sdrh  } 0
655eba8c09Sdrh  do_test trigger7-2.3 {
665eba8c09Sdrh    set txt [db eval {EXPLAIN UPDATE t1 SET y=5}]
675eba8c09Sdrh    string match *___update_t1.x___* $txt
685eba8c09Sdrh  } 0
695eba8c09Sdrh  do_test trigger7-2.4 {
705eba8c09Sdrh    set txt [db eval {EXPLAIN UPDATE t1 SET y=5}]
715eba8c09Sdrh    string match *___update_t1.y___* $txt
725eba8c09Sdrh  } 1
735eba8c09Sdrh  do_test trigger7-2.5 {
745eba8c09Sdrh    set txt [db eval {EXPLAIN UPDATE t1 SET rowid=5}]
755eba8c09Sdrh    string match *___update_t1.x___* $txt
765eba8c09Sdrh  } 0
775eba8c09Sdrh  do_test trigger7-2.6 {
785eba8c09Sdrh    set txt [db eval {EXPLAIN UPDATE t1 SET rowid=5}]
795eba8c09Sdrh    string match *___update_t1.x___* $txt
805eba8c09Sdrh  } 0
815eba8c09Sdrh}
825eba8c09Sdrh
835eba8c09Sdrh# Test the ability to create many triggers on the same table, then
845eba8c09Sdrh# selectively drop those triggers.
855eba8c09Sdrh#
865eba8c09Sdrhdo_test trigger7-3.1 {
875eba8c09Sdrh  execsql {
885eba8c09Sdrh    CREATE TABLE t2(x,y,z);
895eba8c09Sdrh    CREATE TRIGGER t2r1 AFTER INSERT ON t2 BEGIN SELECT 1; END;
905eba8c09Sdrh    CREATE TRIGGER t2r2 BEFORE INSERT ON t2 BEGIN SELECT 1; END;
915eba8c09Sdrh    CREATE TRIGGER t2r3 AFTER UPDATE ON t2 BEGIN SELECT 1; END;
925eba8c09Sdrh    CREATE TRIGGER t2r4 BEFORE UPDATE ON t2 BEGIN SELECT 1; END;
935eba8c09Sdrh    CREATE TRIGGER t2r5 AFTER DELETE ON t2 BEGIN SELECT 1; END;
945eba8c09Sdrh    CREATE TRIGGER t2r6 BEFORE DELETE ON t2 BEGIN SELECT 1; END;
955eba8c09Sdrh    CREATE TRIGGER t2r7 AFTER INSERT ON t2 BEGIN SELECT 1; END;
965eba8c09Sdrh    CREATE TRIGGER t2r8 BEFORE INSERT ON t2 BEGIN SELECT 1; END;
975eba8c09Sdrh    CREATE TRIGGER t2r9 AFTER UPDATE ON t2 BEGIN SELECT 1; END;
985eba8c09Sdrh    CREATE TRIGGER t2r10 BEFORE UPDATE ON t2 BEGIN SELECT 1; END;
995eba8c09Sdrh    CREATE TRIGGER t2r11 AFTER DELETE ON t2 BEGIN SELECT 1; END;
1005eba8c09Sdrh    CREATE TRIGGER t2r12 BEFORE DELETE ON t2 BEGIN SELECT 1; END;
1015eba8c09Sdrh    DROP TRIGGER t2r6;
1025eba8c09Sdrh  }
1035eba8c09Sdrh} {}
1045eba8c09Sdrh
1055eba8c09Sdrh# This test corrupts the database file so it must be the last test
1065eba8c09Sdrh# in the series.
1075eba8c09Sdrh#
1085eba8c09Sdrhdo_test trigger7-99.1 {
109*6ab91a7aSdrh  sqlite3_db_config db DEFENSIVE 0
1105eba8c09Sdrh  execsql {
1115eba8c09Sdrh    PRAGMA writable_schema=on;
1125eba8c09Sdrh    UPDATE sqlite_master SET sql='nonsense';
1135eba8c09Sdrh  }
1145eba8c09Sdrh  db close
115cb354603Sdan  catch { sqlite3 db test.db }
116cb354603Sdan  catchsql { DROP TRIGGER t2r5 }
1171595abcdSdrh} {/1 {malformed database schema .*}/}
1185eba8c09Sdrh
1195eba8c09Sdrhfinish_test
120