KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > interpret > CreateTrigger


1 /**
2  * com.mckoi.database.interpret.CreateTrigger 14 Sep 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.interpret;
26
27 import com.mckoi.database.*;
28 import com.mckoi.util.IntegerVector;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * A parsed state container for the 'CREATE TRIGGER' statement.
34  *
35  * @author Tobias Downer
36  */

37
38 public class CreateTrigger extends Statement {
39
40   // ---------- Implemented from Statement ----------
41

42   public void prepare() throws DatabaseException {
43   }
44
45   public Table evaluate() throws DatabaseException {
46
47     String JavaDoc trigger_name = (String JavaDoc) cmd.getObject("trigger_name");
48     String JavaDoc type = (String JavaDoc) cmd.getObject("type");
49     String JavaDoc table_name = (String JavaDoc) cmd.getObject("table_name");
50     List JavaDoc types = (List JavaDoc) cmd.getObject("trigger_types");
51
52     DatabaseQueryContext context = new DatabaseQueryContext(database);
53
54     TableName tname = TableName.resolve(database.getCurrentSchema(),
55                                         table_name);
56
57     if (type.equals("callback_trigger")) {
58       // Callback trigger - notifies the client when an event on a table
59
// occurs.
60
if (types.size() > 1) {
61         throw new DatabaseException(
62               "Multiple triggered types not allowed for callback triggers.");
63       }
64       
65       String JavaDoc trig_type = ((String JavaDoc) types.get(0)).toUpperCase();
66       int int_type;
67       if (trig_type.equals("INSERT")) {
68         int_type = TriggerEvent.INSERT;
69       }
70       else if (trig_type.equals("DELETE")) {
71         int_type = TriggerEvent.DELETE;
72       }
73       else if (trig_type.equals("UPDATE")) {
74         int_type = TriggerEvent.UPDATE;
75       }
76       else {
77         throw new DatabaseException("Unknown trigger type: " + trig_type);
78       }
79
80       database.createTrigger(trigger_name, tname.toString(), int_type);
81
82     }
83     else if (type.equals("procedure_trigger")) {
84
85       // Get the procedure manager
86
ProcedureManager proc_manager = database.getProcedureManager();
87       
88       String JavaDoc before_after = (String JavaDoc) cmd.getObject("before_after");
89       String JavaDoc procedure_name = (String JavaDoc) cmd.getObject("procedure_name");
90       Expression[] procedure_args =
91                                (Expression[]) cmd.getObject("procedure_args");
92
93       // Convert the trigger into a table name,
94
String JavaDoc schema_name = database.getCurrentSchema();
95       TableName t_name = TableName.resolve(schema_name, trigger_name);
96       t_name = database.tryResolveCase(t_name);
97
98       // Resolve the procedure name into a TableName object.
99
TableName t_p_name = TableName.resolve(schema_name, procedure_name);
100       t_p_name = database.tryResolveCase(t_p_name);
101
102       // Does the procedure exist in the system schema?
103
ProcedureName p_name = new ProcedureName(t_p_name);
104
105       // Check the trigger name doesn't clash with any existing database object.
106
if (database.tableExists(t_name)) {
107         throw new DatabaseException("A database object with name '" + t_name +
108                                     "' already exists.");
109       }
110
111       // Check the procedure exists.
112
if (!proc_manager.procedureExists(p_name)) {
113         throw new DatabaseException("Procedure '" + p_name +
114                                     "' could not be found.");
115       }
116
117       // Resolve the listening type
118
int listen_type = 0;
119       if (before_after.equals("before")) {
120         listen_type |= TableModificationEvent.BEFORE;
121       }
122       else if (before_after.equals("after")) {
123         listen_type |= TableModificationEvent.AFTER;
124       }
125       else {
126         throw new RuntimeException JavaDoc("Unknown before/after type.");
127       }
128
129       for (int i = 0; i < types.size(); ++i) {
130         String JavaDoc trig_type = (String JavaDoc) types.get(i);
131         if (trig_type.equals("insert")) {
132           listen_type |= TableModificationEvent.INSERT;
133         }
134         else if (trig_type.equals("delete")) {
135           listen_type |= TableModificationEvent.DELETE;
136         }
137         else if (trig_type.equals("update")) {
138           listen_type |= TableModificationEvent.UPDATE;
139         }
140       }
141
142       // Resolve the procedure arguments,
143
TObject[] vals = new TObject[procedure_args.length];
144       for (int i = 0; i < procedure_args.length; ++i) {
145         vals[i] = procedure_args[i].evaluate(null, null, context);
146       }
147
148       // Create the trigger,
149
ConnectionTriggerManager manager = database.getConnectionTriggerManager();
150       manager.createTableTrigger(t_name.getSchema(), t_name.getName(),
151                                  listen_type, tname, p_name.toString(), vals);
152
153       // The initial grants for a trigger is to give the user who created it
154
// full access.
155
database.getGrantManager().addGrant(
156            Privileges.PROCEDURE_ALL_PRIVS, GrantManager.TABLE,
157            t_name.toString(), user.getUserName(), true,
158            Database.INTERNAL_SECURE_USERNAME);
159
160     }
161     else {
162       throw new RuntimeException JavaDoc("Unknown trigger type.");
163     }
164
165     // Return success
166
return FunctionTable.resultTable(context, 0);
167   }
168
169
170 }
171
Popular Tags