KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > tools > dblook > DB_Trigger


1 /*
2
3    Derby - Class org.apache.derby.impl.tools.dblook.DB_Trigger
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.tools.dblook;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import java.util.HashMap JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import org.apache.derby.tools.dblook;
34
35 public class DB_Trigger {
36
37     /* ************************************************
38      * Generate the DDL for all triggers in a given
39      * database.
40      * @param conn Connection to the source database.
41      * @return The DDL for the triggers has been written
42      * to output via Logs.java.
43      ****/

44
45     public static void doTriggers (Connection JavaDoc conn)
46         throws SQLException JavaDoc
47     {
48
49         Statement JavaDoc stmt = conn.createStatement();
50         ResultSet JavaDoc rs = stmt.executeQuery("SELECT TRIGGERNAME, SCHEMAID, " +
51             "EVENT, FIRINGTIME, TYPE, TABLEID, REFERENCEDCOLUMNS, " +
52             "TRIGGERDEFINITION, REFERENCINGOLD, REFERENCINGNEW, OLDREFERENCINGNAME, " +
53             "NEWREFERENCINGNAME FROM SYS.SYSTRIGGERS WHERE STATE != 'D'");
54
55         boolean firstTime = true;
56         while (rs.next()) {
57
58             String JavaDoc trigName = dblook.addQuotes(
59                 dblook.expandDoubleQuotes(rs.getString(1)));
60             String JavaDoc trigSchema = dblook.lookupSchemaId(rs.getString(2));
61
62             if (dblook.isIgnorableSchema(trigSchema))
63                 continue;
64
65             trigName = trigSchema + "." + trigName;
66             String JavaDoc tableName = dblook.lookupTableId(rs.getString(6));
67
68             // We'll write the DDL for this trigger if either 1) it is on
69
// a table in the user-specified list, OR 2) the trigger text
70
// contains a reference to a table in the user-specified list.
71

72             if (!dblook.stringContainsTargetTable(rs.getString(8)) &&
73                 (dblook.isExcludedTable(tableName)))
74                 continue;
75
76             if (firstTime) {
77                 Logs.reportString("----------------------------------------------");
78                 Logs.reportMessage("DBLOOK_TriggersHeader");
79                 Logs.reportString("----------------------------------------------\n");
80             }
81
82             String JavaDoc createTrigString = createTrigger(trigName,
83                 tableName, rs);
84
85             Logs.writeToNewDDL(createTrigString);
86             Logs.writeStmtEndToNewDDL();
87             Logs.writeNewlineToNewDDL();
88             firstTime = false;
89
90         }
91
92         rs.close();
93         stmt.close();
94
95     }
96
97     /* ************************************************
98      * Generate DDL for a specific trigger.
99      * @param trigName Name of the trigger.
100      * @param tableName Name of the table on which the trigger
101      * fires.
102      * @param aTrig Information about the trigger.
103      * @return The DDL for the current trigger is returned
104      * as a String.
105      ****/

106
107     private static String JavaDoc createTrigger(String JavaDoc trigName, String JavaDoc tableName,
108         ResultSet JavaDoc aTrig) throws SQLException JavaDoc
109     {
110
111         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("CREATE TRIGGER ");
112         sb.append(trigName);
113
114         // Firing time.
115
if (aTrig.getString(4).charAt(0) == 'A')
116             sb.append(" AFTER ");
117         else
118             sb.append(" NO CASCADE BEFORE ");
119
120         // Event.
121
switch (aTrig.getString(3).charAt(0)) {
122             case 'I': sb.append("INSERT");
123                         break;
124             case 'D': sb.append("DELETE");
125                         break;
126             case 'U': sb.append("UPDATE");
127                         String JavaDoc updateCols = aTrig.getString(7);
128                         if (!aTrig.wasNull()) {
129                             sb.append(" OF ");
130                             sb.append(dblook.getColumnListFromDescription(
131                                 aTrig.getString(6), updateCols));
132                         }
133                         break;
134             default: // shouldn't happen.
135
Logs.debug("INTERNAL ERROR: unexpected trigger event: " +
136                             aTrig.getString(3), (String JavaDoc)null);
137                         break;
138         }
139
140         // On table...
141
sb.append(" ON ");
142         sb.append(tableName);
143
144         // Referencing...
145
char trigType = aTrig.getString(5).charAt(0);
146         String JavaDoc oldReferencing = aTrig.getString(11);
147         String JavaDoc newReferencing = aTrig.getString(12);
148         if ((oldReferencing != null) || (newReferencing != null)) {
149             sb.append(" REFERENCING");
150             if (aTrig.getBoolean(9)) {
151                 sb.append(" OLD");
152                 if (trigType == 'S')
153                 // Statement triggers work on tables.
154
sb.append("_TABLE AS ");
155                 else
156                 // don't include "ROW" keyword (DB2 doesn't).
157
sb.append(" AS ");
158                 sb.append(oldReferencing);
159             }
160             if (aTrig.getBoolean(10)) {
161                 sb.append(" NEW");
162                 if (trigType == 'S')
163                 // Statement triggers work on tables.
164
sb.append("_TABLE AS ");
165                 else
166                 // don't include "ROW" keyword (DB2 doesn't).
167
sb.append(" AS ");
168                 sb.append(newReferencing);
169             }
170         }
171
172         // Trigger type (row/statement).
173
sb.append(" FOR EACH ");
174         if (trigType == 'S')
175             sb.append("STATEMENT ");
176         else
177             sb.append("ROW ");
178
179         // DB2 requires the following keywords in order to work.
180
sb.append("MODE DB2SQL ");
181
182         // Finally, the trigger action.
183
sb.append(dblook.removeNewlines(aTrig.getString(8)));
184         return sb.toString();
185
186     }
187
188 }
189
Popular Tags