KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > ddl > descriptors > TriggerDescriptor


1 package com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors;
2
3 import java.sql.*;
4 import java.util.*;
5
6 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
7 import com.daffodilwoods.daffodildb.server.serversystem.*;
8 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.constraintsystem.*;
9 import com.daffodilwoods.daffodildb.server.sql99.*;
10 import com.daffodilwoods.daffodildb.server.sql99.common.*;
11 import com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
13 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
14 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
15 import com.daffodilwoods.daffodildb.server.sql99.utils.parser.*;
16 import com.daffodilwoods.daffodildb.utils.*;
17 import com.daffodilwoods.daffodildb.utils.parser.*;
18 import com.daffodilwoods.database.general.*;
19 import com.daffodilwoods.database.resource.*;
20
21 public class TriggerDescriptor extends Descriptor implements _Trigger {
22
23    private static String JavaDoc triggerUpdateColumnsQuery =
24        "select event_object_column from " +
25        SystemTables.triggered_update_columns_TableName
26        +
27        " where trigger_catalog = ? and trigger_schema = ? and trigger_name = ?";
28
29    private static String JavaDoc triggerQuery = "select * from " +
30        SystemTables.triggers_TableName
31        +
32        " where trigger_catalog = ? and trigger_schema = ? and trigger_name = ? ";
33
34
35
36    public String JavaDoc trigger_catalog;
37    public String JavaDoc trigger_schema;
38    public String JavaDoc trigger_name;
39    public String JavaDoc event_manipulation;
40
41    public String JavaDoc event_object_catalog;
42    public String JavaDoc event_object_schema;
43    public String JavaDoc event_object_name;
44
45    public int action_order;
46    public String JavaDoc action_condition;
47    public String JavaDoc action_statement;
48    public String JavaDoc action_orientation = SqlKeywords.STATEMENT;
49
50    public String JavaDoc condition_timing;
51    public String JavaDoc condition_reference_old_table = SqlKeywords.OLD;
52    public String JavaDoc condition_reference_new_table = SqlKeywords.NEW;
53
54    public Timestamp created; // time at which the trigger is created;
55
public ArrayList triggerColumnsList;
56
57    private ArrayList tablesUsed;
58    private ArrayList columnsUsed;
59    public TableDescriptor tableDescriptor;
60
61    public TriggerDescriptor() throws DException {
62    }
63
64    public void load(_ServerSession serverSession) throws com.daffodilwoods.
65
       database.resource.DException {
66       _SelectQueryIterator triggerIterator = SqlSchemaConstants.getIterator(serverSession,
67           triggerQuery, getParameters());
68       if (!triggerIterator.first()) {
69          throw new InitializeException("DSE1013", null);
70       }
71       loadDataFromRecord(triggerIterator);
72       loadTriggerColumns(serverSession);
73    }
74
75
76    public void loadDataFromRecord(_SelectQueryIterator iter) throws DException {
77       loadDataFromRecord( (Object JavaDoc[]) iter.getObject());
78    }
79
80    public void loadDataFromRecord(Object JavaDoc[] values) throws DException {
81       trigger_catalog = (String JavaDoc) values[SystemTablesFields.
82           triggerDescriptor_trigger_catalog];
83       trigger_schema = (String JavaDoc) values[SystemTablesFields.
84           triggerDescriptor_trigger_schema];
85       trigger_name = (String JavaDoc) values[SystemTablesFields.
86           triggerDescriptor_trigger_name];
87       event_manipulation = (String JavaDoc) values[SystemTablesFields.
88           triggerDescriptor_event_manipulation];
89       event_object_catalog = (String JavaDoc) values[SystemTablesFields.
90           triggerDescriptor_event_object_catalog];
91       event_object_schema = (String JavaDoc) values[SystemTablesFields.
92           triggerDescriptor_event_object_schema];
93       event_object_name = (String JavaDoc) values[SystemTablesFields.
94           triggerDescriptor_event_object_name];
95       action_order = ( (Integer JavaDoc) values[SystemTablesFields.
96                       triggerDescriptor_action_order]).intValue();
97       action_condition = (String JavaDoc) values[SystemTablesFields.
98           triggerDescriptor_action_condition];
99       action_statement = (String JavaDoc) values[SystemTablesFields.
100           triggerDescriptor_action_statement];
101       action_orientation = (String JavaDoc) values[SystemTablesFields.
102           triggerDescriptor_action_orientation];
103       condition_timing = (String JavaDoc) values[SystemTablesFields.
104           triggerDescriptor_condition_timing];
105       condition_reference_old_table = (String JavaDoc) values[SystemTablesFields.
106           triggerDescriptor_condition_reference_old_table];
107       condition_reference_new_table = (String JavaDoc) values[SystemTablesFields.
108           triggerDescriptor_condition_reference_new_table];
109       created = (java.sql.Timestamp JavaDoc) values[SystemTablesFields.
110           triggerDescriptor_created];
111    }
112
113    public void load_record_from_Array(PreparedStatementGetter
114                                       preparedStatementGetter, Object JavaDoc[] obj) throws
115        DException {
116       loadDataFromRecord(obj);
117       if (!event_manipulation.equalsIgnoreCase(SqlKeywords.UPDATE)) {
118          return;
119       }
120       loadTriggerColumns( (_SelectQueryIterator) preparedStatementGetter.
121                          getTriggerUpdateColumnsExecuter().executeForFresh(
122           getParameters()));
123    }
124
125    private void loadTriggerColumns(_ServerSession serverSession) throws com.
126
       daffodilwoods.database.resource.DException {
127
128       if (!event_manipulation.equalsIgnoreCase(SqlKeywords.UPDATE)) {
129          return;
130       }
131       loadTriggerColumns(SqlSchemaConstants.getIterator(serverSession,
132           triggerUpdateColumnsQuery, getParameters()));
133    }
134
135    private void loadTriggerColumns(_SelectQueryIterator triggerColumns) throws DException {
136       if (triggerColumns.first()) {
137          triggerColumnsList = new ArrayList(3);
138          do {
139             String JavaDoc columnName = (String JavaDoc) ( (Object JavaDoc[]) triggerColumns.
140                                           getObject())[0];
141             triggerColumnsList.add(columnName);
142          } while (triggerColumns.next());
143       }
144    }
145
146    private String JavaDoc[] getParameters() throws DException {
147       return new String JavaDoc[] {
148           trigger_catalog, trigger_schema, trigger_name};
149    }
150
151    private booleanvalueexpression _bve;
152
153    public booleanvalueexpression getSearchCondition() throws DException {
154       try {
155          if (action_condition == null || action_condition.equals("")) {
156             return null;
157          }
158          if (_bve != null) {
159             return _bve;
160          }
161          _bve = (booleanvalueexpression) ConditionParser.parseCondition(
162              action_condition);
163          return _bve;
164       } catch (Exception JavaDoc ex) {
165          return null;
166       }
167    }
168
169    /* optimize this save procedure statements */
170    private boolean searchedProcedureStatements = false;
171    public SQLexecutablestatement[] getStatement() throws DException {
172       SQLexecutablestatement[] sqlExecutableStatements = null;
173       Object JavaDoc parsedObject = Parser.parseTriggeredSQL(action_statement);
174       if (parsedObject instanceof SQLexecutablestatement) {
175          sqlExecutableStatements = new SQLexecutablestatement[] {
176              (SQLexecutablestatement) parsedObject};
177          return sqlExecutableStatements;
178       }
179       triggeredSQLstatement statements = (triggeredSQLstatement) parsedObject;
180       ArrayList procedureStatement = statements.getStatement();
181       searchedProcedureStatements = true;
182       Object JavaDoc[] obj = procedureStatement.toArray();
183       SQLexecutablestatement[] s = new SQLexecutablestatement[
184           procedureStatement.size()];
185       for (int i = 0; i < procedureStatement.size(); i++) {
186          s[i] = (SQLexecutablestatement) obj[i];
187       }
188       sqlExecutableStatements = s;
189       return s;
190    }
191
192    public String JavaDoc getTriggerType() throws DException {
193       return event_manipulation;
194    }
195
196
197    public void save(_ServerSession serverSession) throws com.daffodilwoods.
198
       database.resource.DException {
199       Object JavaDoc[] columnValues = {
200           trigger_catalog, trigger_schema, trigger_name,
201           event_manipulation, event_object_catalog, event_object_schema,
202           event_object_name, new Integer JavaDoc(action_order), action_condition,
203           action_statement, action_orientation, condition_timing,
204           condition_reference_old_table, condition_reference_new_table,
205           new java.sql.Timestamp JavaDoc(System.currentTimeMillis())};
206       try {
207          SqlSchemaConstants.insert(serverSession,
208                                    SqlSchemaConstants.triggers_TableName, null,
209                                    columnValues);
210       } catch (PrimaryConstraintException de) {
211          DException tde = new DException("DSE1135", new Object JavaDoc[] {getQualifiedIdentifier().getIdentifier()});
212          throw tde;
213       } catch (SizeMisMatchException de) {
214          if (de.getDseCode().equals("DSE773")) {
215             DException tde = new DException("DSE8103", null);
216             throw tde;
217          }
218       } catch (DException de) {
219          if (de.getDseCode().equals("DSE1255")) {
220             DException tde = new DException("DSE1135", new Object JavaDoc[] {getQualifiedIdentifier().getIdentifier()});
221             throw tde;
222          }
223          if (de.getDseCode().equals("DSE773")) {
224             DException tde = new DException("DSE8103", null);
225             throw tde;
226          }
227          throw de;
228       }
229       saveTriggerColumns(serverSession);
230       saveTablesUsedInTriggerAction(serverSession);
231       saveColumnsUsedInTriggerAction(serverSession);
232    }
233
234    private void saveTriggerColumns(_ServerSession serverSession) throws com.
235
       daffodilwoods.database.resource.DException {
236       if (triggerColumnsList == null) {
237          return;
238       }
239       int columnCount = triggerColumnsList.size();
240       Object JavaDoc[] columnValues = new Object JavaDoc[7];
241       columnValues[0] = trigger_catalog;
242       columnValues[1] = trigger_schema;
243       columnValues[2] = trigger_name;
244       columnValues[3] = event_object_catalog;
245       columnValues[4] = event_object_schema;
246       columnValues[5] = event_object_name;
247       for (int i = 0; i < columnCount; i++) {
248          columnValues[6] = triggerColumnsList.get(i);
249          SqlSchemaConstants.insert(serverSession,
250                                    SqlSchemaConstants.
251                                    triggered_update_columns_TableName, null,
252                                    columnValues);
253       }
254    }
255
256    private void saveTablesUsedInTriggerAction(_ServerSession serverSession) throws
257        com.daffodilwoods.database.resource.DException {
258       if (tablesUsed == null) {
259          return;
260       }
261       int tableCount = tablesUsed.size();
262       Object JavaDoc[] columnValues = new Object JavaDoc[6];
263       columnValues[0] = trigger_catalog;
264       columnValues[1] = trigger_schema;
265       columnValues[2] = trigger_name;
266       for (int i = 0; i < tableCount; i++) {
267          String JavaDoc[] tableName = (String JavaDoc[]) tablesUsed.get(i);
268          columnValues[3] = tableName[0];
269          columnValues[4] = tableName[1];
270          columnValues[5] = tableName[2];
271          SqlSchemaConstants.insert(serverSession,
272                                    SqlSchemaConstants.
273                                    trigger_table_usage_TableName, null,
274                                    columnValues);
275       }
276    }
277
278    private void saveColumnsUsedInTriggerAction(_ServerSession serverSession) throws
279        com.daffodilwoods.database.resource.DException {
280       if (columnsUsed == null) {
281          return;
282       }
283       int columnCount = columnsUsed.size();
284       Object JavaDoc[] columnValues = new Object JavaDoc[7];
285       columnValues[0] = trigger_catalog;
286       columnValues[1] = trigger_schema;
287       columnValues[2] = trigger_name;
288       for (int i = 0; i < columnCount; i++) {
289          String JavaDoc[] columnName = (String JavaDoc[]) columnsUsed.get(i);
290          columnValues[3] = columnName[0];
291          columnValues[4] = columnName[1];
292          columnValues[5] = columnName[2];
293          columnValues[6] = columnName[3];
294          SqlSchemaConstants.insert(serverSession,
295                                    SqlSchemaConstants.
296                                    trigger_column_usage_TableName, null,
297                                    columnValues);
298       }
299    }
300
301    public void delete(_ServerSession serverSession) throws com.daffodilwoods.database.resource.DException {
302       String JavaDoc bve = "TRIGGER_CATALOG=? and TRIGGER_SCHEMA=? and TRIGGER_NAME=?";
303       _ServerSession globalSession = serverSession.getGlobalSession();
304       booleanvalueexpression condition = ConditionParser.parseCondition(bve, globalSession, SqlSchemaConstants.triggers_TableName);
305       super.delete(serverSession, SqlSchemaConstants.triggers_TableName, condition, new Object JavaDoc[] {trigger_catalog, trigger_schema, trigger_name});
306
307       deleteTriggerColumns(serverSession);
308       deleteTablesUsedInTriggerAction(serverSession);
309       deleteColumnsUsedInTriggerAction(serverSession);
310    }
311
312    private void deleteTriggerColumns(_ServerSession serverSession) throws com.daffodilwoods.database.resource.DException {
313       if (triggerColumnsList == null) {
314          return; // case trigger has no columns;
315
}
316       TableDetails tableDetail = new TableDetails();
317       tableDetail.setTableName(SqlSchemaConstants.triggered_update_columns_TableName.getTableName());
318       String JavaDoc bve = "TRIGGER_CATALOG=? and TRIGGER_SCHEMA=? and TRIGGER_NAME=?";
319       _ServerSession globalSession = serverSession.getGlobalSession();
320       booleanvalueexpression condition = ConditionParser.parseCondition(bve, globalSession, SqlSchemaConstants.triggered_update_columns_TableName);
321       ConditionSingleTableExecuter conSingTE = new ConditionSingleTableExecuter(null, tableDetail, globalSession, condition, null);
322       _Iterator iter = globalSession.getInternalIterator(SqlSchemaConstants.triggered_update_columns_TableName, conSingTE);
323       iter.setConditionVariableValue(condition.getReferences(new TableDetails[] {tableDetail}), FieldUtility.getFields(new Object JavaDoc[] {trigger_catalog, trigger_schema, trigger_name}), 1);
324       SqlSchemaConstants.deleteAll(serverSession.getSystemServerSession(), SqlSchemaConstants.triggered_update_columns_TableName, iter);
325    }
326
327    private void deleteTablesUsedInTriggerAction(_ServerSession serverSession) throws com.daffodilwoods.database.resource.DException {
328       TableDetails tableDetail = new TableDetails();
329       tableDetail.setTableName(SqlSchemaConstants.trigger_table_usage_TableName.
330                                getTableName());
331       String JavaDoc bve = "TRIGGER_CATALOG=? and TRIGGER_SCHEMA=? and TRIGGER_NAME=?";
332       _ServerSession globalSession = serverSession.getGlobalSession();
333       booleanvalueexpression condition = ConditionParser.parseCondition(bve,
334           globalSession, SqlSchemaConstants.trigger_table_usage_TableName);
335       ConditionSingleTableExecuter conSingTE = new ConditionSingleTableExecuter(null,
336           tableDetail, globalSession, condition, null);
337       _Iterator iter = globalSession.getInternalIterator(SqlSchemaConstants.
338           trigger_table_usage_TableName,
339           conSingTE);
340       iter.setConditionVariableValue(condition.getReferences(new TableDetails[] {
341           tableDetail}), FieldUtility.getFields(new Object JavaDoc[] {trigger_catalog, trigger_schema,
342                                                 trigger_name})
343                                      , 1);
344       SqlSchemaConstants.deleteAll(serverSession.getSystemServerSession(),
345                                    SqlSchemaConstants.
346                                    trigger_table_usage_TableName, iter);
347    }
348
349    private void deleteColumnsUsedInTriggerAction(_ServerSession serverSession) throws com.daffodilwoods.database.resource.DException {
350       TableDetails tableDetail = new TableDetails();
351       tableDetail.setTableName(SqlSchemaConstants.trigger_column_usage_TableName.getTableName());
352       String JavaDoc bve = "TRIGGER_CATALOG=? and TRIGGER_SCHEMA=? and TRIGGER_NAME=?";
353       _ServerSession globalSession = serverSession.getGlobalSession();
354       booleanvalueexpression condition = ConditionParser.parseCondition(bve, globalSession, SqlSchemaConstants.trigger_column_usage_TableName);
355       ConditionSingleTableExecuter conSingTE = new ConditionSingleTableExecuter(null, tableDetail, globalSession, condition, null);
356       _Iterator iter = globalSession.getInternalIterator(SqlSchemaConstants.trigger_column_usage_TableName, conSingTE);
357       iter.setConditionVariableValue(condition.getReferences(new TableDetails[] {tableDetail}), FieldUtility.getFields(new Object JavaDoc[] {trigger_catalog, trigger_schema, trigger_name}), 1);
358       SqlSchemaConstants.deleteAll(serverSession.getSystemServerSession(), SqlSchemaConstants.trigger_column_usage_TableName, iter);
359    }
360
361    public String JavaDoc getOldAliasName() throws DException {
362       return condition_reference_old_table;
363    }
364
365    public String JavaDoc getNewAliasName() throws DException {
366       return condition_reference_new_table;
367    }
368
369    public QualifiedIdentifier getQualifiedIdentifier() {
370       return new QualifiedIdentifier(trigger_catalog, trigger_schema,
371                                      trigger_name);
372    }
373
374    public QualifiedIdentifier getQualifiedTableName() {
375       return new QualifiedIdentifier(event_object_catalog, event_object_schema,
376                                      event_object_name);
377    }
378
379    public boolean equals(Object JavaDoc object) {
380       if (! (object instanceof _Trigger)) {
381          return false;
382       }
383       _Trigger trigger = (_Trigger) object;
384       return getQualifiedIdentifier().equals(trigger.getQualifiedIdentifier());
385
386    }
387
388    public int getDescriptorType() {
389       return TRIGGER_DESCRIPTOR;
390    }
391
392    public void setActionOrder(_ServerSession serverSession) throws DException {
393       StringBuffer JavaDoc query = new StringBuffer JavaDoc();
394       query.append("select count(*) from ").append(SystemTables.
395           triggers_TableName)
396           .append(" where ").append("event_object_catalog = ? ")
397           .append("and event_object_schema = ? ").append(
398           "and event_object_table = ? ")
399           .append(" and event_manipulation = ? ").append(
400           "and condition_timing = ? ")
401           .append("and action_orientation = ? ");
402       Object JavaDoc[] parameters = new Object JavaDoc[] {
403           event_object_catalog, event_object_schema, event_object_name,
404           event_manipulation, condition_timing, action_orientation};
405       _SelectQueryIterator iter = SqlSchemaConstants.getIterator(serverSession,
406           query.toString(), parameters);
407       int count = 0;
408       if (iter.first()) {
409          count = ( (Object JavaDoc[]) iter.getObject())[0].hashCode();
410       }
411       action_order = ++count;
412    }
413
414    public void setTablesUsed(ArrayList tablesUsed0) {
415       tablesUsed = tablesUsed0;
416    }
417
418    public void setColumnsUsed(ArrayList columnsUsed0) {
419       columnsUsed = columnsUsed0;
420    }
421 }
422
Popular Tags