KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > ddl > CreateTrigger


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.ddl;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.engine.Database;
10 import org.h2.engine.Session;
11 import org.h2.message.Message;
12 import org.h2.schema.Schema;
13 import org.h2.schema.TriggerObject;
14 import org.h2.table.Table;
15
16 /**
17  * @author Thomas
18  */

19
20 public class CreateTrigger extends SchemaCommand {
21     // TODO implement drop trigger
22

23     private String JavaDoc triggerName;
24     private boolean ifNotExists;
25
26     private boolean before;
27     private int typeMask;
28     private boolean rowBased;
29     private int queueSize = TriggerObject.DEFAULT_QUEUE_SIZE;
30     private boolean noWait;
31     private String JavaDoc tableName;
32     private String JavaDoc triggerClassName;
33
34     public CreateTrigger(Session session, Schema schema) {
35         super(session, schema);
36     }
37
38     public void setBefore(boolean before) {
39         this.before = before;
40     }
41
42     public void setTriggerClassName(String JavaDoc triggerClassName) {
43         this.triggerClassName = triggerClassName;
44     }
45
46     public void setTypeMask(int typeMask) {
47         this.typeMask = typeMask;
48     }
49
50     public void setRowBased(boolean rowBased) {
51         this.rowBased = rowBased;
52     }
53
54     public void setQueueSize(int size) {
55         this.queueSize = size;
56     }
57
58     public void setNoWait(boolean noWait) {
59         this.noWait = noWait;
60     }
61
62     public void setTableName(String JavaDoc tableName) {
63         this.tableName = tableName;
64     }
65
66     public void setTriggerName(String JavaDoc name) {
67         this.triggerName = name;
68     }
69
70     public void setIfNotExists(boolean ifNotExists) {
71         // TODO trigger: if exists - probably better use 'or replace'
72
this.ifNotExists = ifNotExists;
73     }
74
75     public int update() throws SQLException JavaDoc {
76         // TODO rights: what rights are required to create a trigger?
77
session.commit();
78         Database db = session.getDatabase();
79         if(getSchema().findTrigger(triggerName)!=null) {
80             if (ifNotExists) {
81                 return 0;
82             }
83             throw Message.getSQLException(Message.TRIGGER_ALREADY_EXISTS_1,
84                     triggerName);
85         }
86         int id = getObjectId(false, true);
87         Table table = getSchema().getTableOrView(session, tableName);
88         TriggerObject trigger = new TriggerObject(getSchema(), id, triggerName, table);
89         trigger.setBefore(before);
90         trigger.setNoWait(noWait);
91         trigger.setQueueSize(queueSize);
92         trigger.setRowBased(rowBased);
93         trigger.setTriggerClassName(session, triggerClassName);
94         trigger.setTypeMask(typeMask);
95         db.addSchemaObject(session, trigger);
96         table.addTrigger(trigger);
97         return 0;
98     }
99
100 }
101
Popular Tags