KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > ddl > schemadefinition > triggeredaction


1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
6 import com.daffodilwoods.daffodildb.server.serversystem.*;
7 import com.daffodilwoods.daffodildb.server.sql99.*;
8 import com.daffodilwoods.daffodildb.server.sql99.common.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dcl.sqlcontrolstatement.*;
10 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*;
11 import com.daffodilwoods.daffodildb.server.sql99.ddl.utility.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dml.*;
13 import com.daffodilwoods.daffodildb.server.sql99.dql.semanticchecker.*;
14 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
15 import com.daffodilwoods.database.general.*;
16 import com.daffodilwoods.database.resource.*;
17 import com.daffodilwoods.database.utility.*;
18 import com.daffodilwoods.daffodildb.server.sql99.common.ColumnDetails;
19
20 public class triggeredaction {
21    public triggeredSQLstatement _triggeredSQLstatement0;
22    public
23        SNONRESERVEDWORD136444255Sleftparen653880241searchconditionSrightparen_1874859514 _OptSNONRESERVEDWORD136444255Sleftparen653880241searchconditionSrightparen_18748595141;
24    public SRESERVEDWORD1206543922SRESERVEDWORD1206543922actiontyperule
25        _OptSRESERVEDWORD1206543922SRESERVEDWORD1206543922actiontyperule2;
26
27    private TriggerDescriptor triggerDescriptor;
28    private ArrayList tablesIncluded = new ArrayList();
29    private ArrayList columnsIncluded = new ArrayList();
30
31    public void setTriggerDescriptor(_Descriptor triggerDes0) throws DException {
32       triggerDescriptor = (TriggerDescriptor) triggerDes0;
33    }
34
35    /** @todo don't store the when condition and triggered statements thro' the
36     * run method of triggeredaction class.
37     */

38
39    public Object JavaDoc run(Object JavaDoc object) throws DException {
40       return null;
41    }
42
43    /* Trigger is row-Level or statement level */
44    public void setTriggerActionOrientation() throws DException {
45       triggerDescriptor.action_orientation =
46           _OptSRESERVEDWORD1206543922SRESERVEDWORD1206543922actiontyperule2 == null
47           ? SqlKeywords.STATEMENT
48           :
49           (String JavaDoc) _OptSRESERVEDWORD1206543922SRESERVEDWORD1206543922actiontyperule2.
50           run(null);
51    }
52
53    public void setTriggerSearchCondition() throws DException {
54       if (_OptSNONRESERVEDWORD136444255Sleftparen653880241searchconditionSrightparen_18748595141 != null) {
55          triggerDescriptor.action_condition = _OptSNONRESERVEDWORD136444255Sleftparen653880241searchconditionSrightparen_18748595141.
56              getSeatchCondition();
57       }
58    }
59
60    public void setTriggeredStatement(_ServerSession serverSession) throws
61        DException {
62       triggerDescriptor.action_statement = _triggeredSQLstatement0.toString();
63    }
64
65    public void validateTriggeredAction(_ServerSession currentSession) throws
66        DException {
67       QualifiedIdentifier tableName = triggerDescriptor.getQualifiedTableName();
68       _ColumnCharacteristics cc = currentSession.getColumnCharacteristics(
69           tableName);
70       String JavaDoc[] columns = cc.getColumnNames();
71       if (_OptSNONRESERVEDWORD136444255Sleftparen653880241searchconditionSrightparen_18748595141 != null) {
72          TableDetails[] tabDetails = null;
73          if (triggerDescriptor.event_manipulation.equalsIgnoreCase(SqlKeywords.INSERT)) {
74             TableDetails tableDetails = new TableDetails();
75             tableDetails.setTableName(tableName.getTableName());
76             tableDetails.setAliasName(triggerDescriptor.getNewAliasName());
77             tabDetails = new TableDetails[] {tableDetails};
78          } else if (triggerDescriptor.event_manipulation.equalsIgnoreCase(SqlKeywords.DELETE)) {
79             TableDetails tableDetails1 = new TableDetails();
80             tableDetails1.setTableName(tableName.getTableName());
81             tableDetails1.setAliasName(triggerDescriptor.getOldAliasName());
82             tabDetails = new TableDetails[] {tableDetails1};
83          } else {
84             TableDetails tableDetails = new TableDetails();
85             tableDetails.setTableName(tableName.getTableName());
86             tableDetails.setAliasName(triggerDescriptor.getNewAliasName());
87             TableDetails tableDetails1 = new TableDetails();
88             tableDetails1.setTableName(tableName.getTableName());
89             tableDetails1.setAliasName(triggerDescriptor.getOldAliasName());
90             tabDetails = new TableDetails[] {tableDetails, tableDetails1};
91          }
92          searchcondition _searchCondition = _OptSNONRESERVEDWORD136444255Sleftparen653880241searchconditionSrightparen_18748595141.
93              _searchcondition1;
94          ColumnDetails[] cd = _searchCondition.getColumnDetails();
95
96          for (int i = 0; i < cd.length; i++) {
97             if (cd[i].getType() == TypeConstants.REFERENCE)
98                if (cd[i].getTriggerTableAlias() == null)
99                   throw new DException("DSE8135", new Object JavaDoc[] {cd[i].getColumn()});
100                else if (! (cd[i].getTriggerTableAlias().equalsIgnoreCase(triggerDescriptor.getNewAliasName()) ||
101                            cd[i].getTriggerTableAlias().equalsIgnoreCase(triggerDescriptor.getOldAliasName()))) {
102                   throw new DException("DSE8186", new Object JavaDoc[] {cd[i].getTriggerTableAlias()});
103                }
104          }
105
106          SemanticChecker semanticChecker = new SemanticChecker(tabDetails, cd);
107          ArrayList syntaxResult = new ArrayList();
108          semanticChecker.semanticCheckingUpdate(currentSession, syntaxResult);
109
110          _Reference[] ref = null;
111          try {
112             ref = _searchCondition.checkSemantic(currentSession);
113          } catch (DException ex) {
114             throw new DException("DSE8109", null);
115          }
116
117          for (int j = 0; j < ref.length; j++) {
118             if (ref[j].getReferenceType() == SimpleConstants.VARIABLECOLUMN)
119                throw new DException("DSE8142", null);
120
121             if (P.indexOfIgnoreCase(columns, ref[j].getColumn()) == -1) {
122                throw new DException("DSE8027",
123                                     new Object JavaDoc[] {triggerDescriptor.getQualifiedIdentifier().getIdentifier(), ref[j].getColumn()});
124             }
125
126          }
127       }
128       validateTriggeredSQLStatement(currentSession, columns, cc);
129    }
130
131    private void validateTriggeredSQLStatement(_ServerSession currentSession, String JavaDoc[] columns, _ColumnCharacteristics cc) throws
132        DException {
133       ArrayList statementsList = _triggeredSQLstatement0.getStatement();
134       QualifiedIdentifier tableName = triggerDescriptor.getQualifiedTableName();
135       if (statementsList != null) {
136          for (int i = 0; i < statementsList.size(); i++) {
137             SQLexecutablestatement st = (SQLexecutablestatement) statementsList.get(i);
138             _Reference[] ref = null;
139             if (st instanceof SQLcontrolstatement || st instanceof SQLdatachangestatement) {
140                if (st instanceof SQLcontrolstatement) {
141                   SQLcontrolstatement sqlControlSt = (SQLcontrolstatement) st;
142                   String JavaDoc oldAliasName = triggerDescriptor.getOldAliasName();
143                   String JavaDoc newAliasName = triggerDescriptor.getNewAliasName();
144                   ColumnDetails[] cd = getColumnDetails();
145                   if (cd != null) {
146                      for (int j = 0; j < cd.length; j++) {
147                         String JavaDoc aliasName = cd[j].getNameOfTable();
148                         for (int k = 0; k < columns.length; k++) {
149                            if (aliasName != null && (aliasName.equalsIgnoreCase(oldAliasName) ||
150                                aliasName.equalsIgnoreCase(newAliasName))) {
151                               if (cd[j].getColumn().equalsIgnoreCase(columns[k])) {
152                                  cd[j].setDatatype(cc.getColumnType(k));
153                                  cd[j].setSize(cc.getPrecision(k));
154                               }
155                            }
156                         }
157                      }
158                   }
159                   try {
160                      ref = sqlControlSt.checkSemantic(currentSession);
161                   } catch (DException ex) {
162                      throw ex;
163                   }
164
165                   /** Following if block as suggest by Ms. Kiran. */
166                   if (ref != null && ref.length > 0) {
167                      for (int j = 0; j < ref.length; j++) {
168                         if (ref[j] instanceof ColumnDetails && ref[j].getTriggerTableAlias() != null) {
169                            boolean isAssignmentTarget = ( (ColumnDetails) ref[j]).isAssignmentTarget();
170                            String JavaDoc aliasName = ref[j].getTriggerTableAlias();
171                            if (isAssignmentTarget) {
172                               if (aliasName.equalsIgnoreCase(newAliasName) && triggerDescriptor.condition_timing.equalsIgnoreCase(SqlKeywords.AFTER) &&
173                                   (triggerDescriptor.event_manipulation.equalsIgnoreCase(SqlKeywords.UPDATE) ||
174                                    triggerDescriptor.event_manipulation.equalsIgnoreCase(SqlKeywords.INSERT))) {
175                                  throw new DException("DSE8198", null);
176                               } else if (aliasName.equalsIgnoreCase(oldAliasName) &&
177                                          (triggerDescriptor.condition_timing.equalsIgnoreCase(SqlKeywords.AFTER) || triggerDescriptor.condition_timing.equalsIgnoreCase(SqlKeywords.BEFORE))
178                                          && triggerDescriptor.event_manipulation.equalsIgnoreCase(SqlKeywords.DELETE)) {
179                                  throw new DException("DSE8199", null);
180                               }
181                            }
182                         }
183                      }
184                   }
185                } else if (st instanceof SQLdatachangestatement) {
186                   SQLdatachangestatement sqlDataChangeSt = (SQLdatachangestatement) st;
187                   try {
188                      ref = sqlDataChangeSt.checkSemantic(currentSession);
189                   } catch (DException ex) {
190                      throw ex;
191                   }
192                }
193                if (ref != null) {
194                   String JavaDoc oldAliasName = triggerDescriptor.getOldAliasName();
195                   String JavaDoc newAliasName = triggerDescriptor.getNewAliasName();
196
197                   for (int j = 0; j < ref.length; j++) {
198                      if (ref[j].getReferenceType() == SimpleConstants.SEQUENCE)
199                         continue;
200                      if (ref[j].getReferenceType() == SimpleConstants.VARIABLECOLUMN)
201                         throw new DException("DSE8142", null);
202
203                      if (P.indexOfIgnoreCase(columns, ref[j].getColumn()) == -1) {
204                         throw new DException("DSE8028",
205                                              new Object JavaDoc[] {ref[j].getColumn(), triggerDescriptor.getQualifiedIdentifier().getIdentifier()});
206                      }
207
208                      String JavaDoc referenceTriggerAlias = ref[j].getTriggerTableAlias();
209
210                      if (referenceTriggerAlias == null)
211                         throw new DException("DSE8135", new Object JavaDoc[] {ref[j].getColumn()});
212
213                      if (! (referenceTriggerAlias.equalsIgnoreCase(oldAliasName) || referenceTriggerAlias.equalsIgnoreCase(newAliasName)))
214                         throw new DException("DSE8134", new Object JavaDoc[] {referenceTriggerAlias});
215                   }
216                   /** @todo
217                    * to uncommecnt this work after provision done by MR. Rohit
218                    * we are taking here to avoid time for again looping and casting
219                    * */

220                }
221             } else {
222                throw new DException("DSE8153", new Object JavaDoc[] {"Invalid Query" + st, triggerDescriptor.trigger_name});
223             }
224
225             /** @todo
226              * to uncommecnt this work after provision done by MR. Rohit
227              * we are taking here to avoid time for again looping and casting
228              * */

229
230          }
231       }
232    }
233
234    /** @todo implemetation of table included and column included work */
235    public void setTablesIncludedAndColumnIncluded(_ServerSession currentSession) throws
236        DException {
237
238       if (_OptSNONRESERVEDWORD136444255Sleftparen653880241searchconditionSrightparen_18748595141 != null) {
239          searchcondition sc = _OptSNONRESERVEDWORD136444255Sleftparen653880241searchconditionSrightparen_18748595141.
240              _searchcondition1;
241          sc.getTablesIncluded(tablesIncluded);
242          sc.getTablesIncluded(tablesIncluded);
243       }
244       ArrayList tablesPresent = GeneralUtility.removeRedundentTables(
245           triggerDescriptor.trigger_catalog, triggerDescriptor.trigger_schema,
246           tablesIncluded);
247       ArrayList columnsPresent = GeneralUtility.removeRedundentColumns(
248           triggerDescriptor.trigger_catalog, triggerDescriptor.trigger_schema, null,
249           columnsIncluded);
250       triggerDescriptor.setTablesUsed(tablesPresent);
251       triggerDescriptor.setColumnsUsed(columnsPresent);
252    }
253
254    public String JavaDoc toString() {
255       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
256       sb.append(" ");
257       if (_OptSRESERVEDWORD1206543922SRESERVEDWORD1206543922actiontyperule2 != null) {
258          sb.append(
259              _OptSRESERVEDWORD1206543922SRESERVEDWORD1206543922actiontyperule2);
260       }
261       sb.append(" ");
262       if (_OptSNONRESERVEDWORD136444255Sleftparen653880241searchconditionSrightparen_18748595141 != null) {
263          sb.append(_OptSNONRESERVEDWORD136444255Sleftparen653880241searchconditionSrightparen_18748595141);
264       }
265       sb.append(" ");
266       sb.append(_triggeredSQLstatement0);
267       return sb.toString();
268    }
269
270    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
271       return this;
272    }
273
274    public ColumnDetails[] getColumnDetails() throws DException {
275       ArrayList statements = _triggeredSQLstatement0.getStatement();
276       ArrayList listOfColumnDetails = new ArrayList();
277       if (statements != null) {
278          for (int i = 0; i < statements.size(); i++) {
279             if (statements.get(i) instanceof SQLcontrolstatement) {
280                SQLcontrolstatement contStt = (SQLcontrolstatement) statements.get(i);
281                ColumnDetails[] cd = contStt.getColumnDetails();
282                if (cd != null)
283                   listOfColumnDetails.addAll(Arrays.asList(cd));
284             }
285          }
286          return (ColumnDetails[]) listOfColumnDetails.toArray(new ColumnDetails[0]);
287       }
288       return new ColumnDetails[0];
289    }
290 }
291
Popular Tags