KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > serversystem > dmlvalidation > statementtriggersystem > StatementTriggerTable


1 package com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.statementtriggersystem;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
6 import com.daffodilwoods.daffodildb.server.datasystem.utility.*;
7 import com.daffodilwoods.daffodildb.server.serversystem.*;
8 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.triggersystem.*;
9 import com.daffodilwoods.daffodildb.server.sql99.*;
10 import com.daffodilwoods.daffodildb.server.sql99.common.*;
11 import com.daffodilwoods.daffodildb.server.sql99.dcl.sqlcontrolstatement.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dml.*;
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.expression.expressionprimary.*;
16 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
17 import com.daffodilwoods.database.general.*;
18 import com.daffodilwoods.database.resource.*;
19 import com.daffodilwoods.database.utility.*;
20 import com.daffodilwoods.daffodildb.server.serversystem.triggerInfo._Triggers;
21 import com.daffodilwoods.daffodildb.server.serversystem.triggerInfo._Statement;
22 /**
23  *
24  * <p>Title: Statement Trigger Table</p>
25  * <p>Description: </p>
26  * Objective of the StatementTriggerTable is to fire (After/Before) Statement Triggers
27  * when the respective call is made.
28  */

29
30 /**
31  *
32  * <p>Title: Statement Trigger Table</p>
33  * <p>Description: </p>
34  * StatementTriggerTable main purpose is to fire (After/Before) Statement Triggers from their triggerDescriptors
35  */

36 public class StatementTriggerTable implements _StatementTriggerTable {
37
38   private _TriggerCharacteristics triggerCharacteristics;
39   QualifiedIdentifier tableName ;
40   TableDetails tableDetails;
41   /**
42    * used to construct an instance of statement trigger table with the given trigger characteristics.
43    * @param triggerCharacteristics instance of trigger characteristics which is used to retreive the
44    * respective triggers at the time of execution.
45    * @throws DException
46    */

47   public StatementTriggerTable( _TriggerCharacteristics triggerCharacteristics , QualifiedIdentifier tableName ) throws DException {
48     this.triggerCharacteristics = triggerCharacteristics;
49     this.tableName = tableName;
50     tableDetails = new TableDetails();
51     tableDetails.setTableName(new String JavaDoc[]{tableName.catalog,tableName.schema,tableName.name});
52
53   }
54
55   /**
56    * used to fire the after insert Statement level Trigger with SEC
57    * @param statementExecutionContext instance of StatementExecutionContext to get the record version
58    * and set variable values.
59    * @throws DException
60    */

61
62   public void fireAfterInsertStatementLevelTriggers(_StatementExecutionContext statementExecutionVariables) throws DException {
63     fireStatementTriggers( ((ServerSession)statementExecutionVariables.getServerSession()).getAfterInsertTriggerOfStatementLevel(tableName,triggerCharacteristics ,statementExecutionVariables ), statementExecutionVariables );
64   }
65
66   /**
67    * used to Fire the after update Statement level Trigger with SEC and the after Update
68    * Triggers for cloumn indexes fetched from trigger Charactersitics.
69    * @param columns columns for which update is performed.
70    * @param statementExecutionContext instance of StatementExecutionContext to get the record version
71    * and set variable values.
72    * @throws DException
73    */

74
75   public void fireAfterUpdateStatementLevelTriggers(_StatementExecutionContext statementExecutionVariables , int[] columns ) throws DException {
76     fireStatementTriggers(((ServerSession)statementExecutionVariables.getServerSession()).getAfterUpdateTriggerOfStatementLevel(tableName,triggerCharacteristics ,statementExecutionVariables,columns ), statementExecutionVariables );
77   }
78
79   /**
80    * used to Fire the after delete statement level Trigger with SEC and after Delete Triggers fetched from trigger Charactersitics.
81    * @param statementExecutionContext instance of StatementExecutionContext to get the record version
82    * and set variable values.
83    * @throws DException
84    */

85
86   public void fireAfterDeleteStatementLevelTriggers(_StatementExecutionContext statementExecutionVariables) throws DException {
87     fireStatementTriggers( ((ServerSession)statementExecutionVariables.getServerSession()).getAfterDeleteTriggerOfStatementLevel( tableName,triggerCharacteristics ,statementExecutionVariables.getChildStatementExecutionContext() ),statementExecutionVariables );
88   }
89
90   /**
91    * used to Fire the before insert Statement level Trigger with SEC and before Insert Triggers
92    * fetched from trigger characteristics.
93    * @param statementExecutionContext instance of StatementExecutionContext to get the record version
94    * and set variable values.
95    * @throws DException
96    */

97
98   public void fireBeforeInsertStatementLevelTriggers(_StatementExecutionContext statementExecutionVariables) throws DException {
99     fireStatementTriggers( ((ServerSession)statementExecutionVariables.getServerSession()).getBeforeInsertTriggerOfStatementLevel( tableName,triggerCharacteristics ,statementExecutionVariables ) ,statementExecutionVariables );
100   }
101
102   /**
103    * Used to Fire the before update Statement level Trigger with SEC and before update Triggers for
104    * column indexes fetched from trigger charactersitics.
105    * @param columns columns for which update is performed.
106    * @param statementExecutionContext instance of StatementExecutionContext to get the record version
107    * and set variable values.
108    * @throws DException
109    */

110
111   public void fireBeforeUpdateStatementLevelTriggers(_StatementExecutionContext statementExecutionVariables , int[] columns ) throws DException {
112     fireStatementTriggers( ((ServerSession)statementExecutionVariables.getServerSession()).getBeforeUpdateTriggerOfStatementLevel( tableName,triggerCharacteristics ,statementExecutionVariables ,columns) , statementExecutionVariables );
113   }
114
115   /**
116    * used to Fire the before delete Statement level Trigger with SEC and before Delete Triggers
117    * fetched from trigger Charactersitics.
118    * @param statementExecutionContext instance of StatementExecutionContext to get the record version
119    * and set variable values.
120    * @throws DException
121    */

122
123   public void fireBeforeDeleteStatementLevelTriggers(_StatementExecutionContext statementExecutionVariables) throws DException {
124     fireStatementTriggers( ((ServerSession)statementExecutionVariables.getServerSession()).getBeforeDeleteTriggerOfStatementLevel( tableName,triggerCharacteristics ,statementExecutionVariables ), statementExecutionVariables );
125   }
126
127
128    /**
129      It Executes all the statements from all the Trigger Descriptors one by one and add the corresponding event
130      in the sec.
131      * Trigger Descriptors contains all the Triggers (after or before update/delete/insert) that are to be fired
132      * @param statementExecutionContext instance of StatementExecutionContext to get the record version
133    * and set variable values. */

134
135   private void fireStatementTriggers( _Triggers[] triggers, _StatementExecutionContext statementExecutionContext )throws DException{
136
137     if( triggers == null )
138       return ;
139     if(((StatementExecutionContext)statementExecutionContext).getTriggerCount() >=16)
140       throw new DException("DSE5583", null);
141
142     RecordVersion recordVersion = statementExecutionContext.getRecordVersion() ;
143     _ColumnCharacteristics cc = statementExecutionContext.getColumnCharacteristics(tableName) ;
144     _VariableValues tvv = null;
145     Object JavaDoc[] values = null;
146     _Reference[] references = null;
147     for( int i = 0 ; i < triggers.length ; i++ ){
148        String JavaDoc newAlias = triggers[i].getNewAliasName() ;
149        String JavaDoc oldAlias = triggers[i].getOldAliasName() ;
150        tvv = new TriggerVariableValues( recordVersion , oldAlias , newAlias,statementExecutionContext.getServerSession() );
151        booleanvalueexpression bve = triggers[i].getCondition() ;
152        if(bve!=null){
153            tvv = initializeVariableValues( triggers[i].getReferences() , statementExecutionContext.getServerSession() ,recordVersion,newAlias,oldAlias );
154        if( bve.run(tvv).hashCode() != 0){
155          continue ;
156        }
157        }
158       _Statement[] statements = triggers[i].getStatements();
159       _StatementExecutionContext sec = triggers[i].getStatementExecutionContext() ;
160        _VariableValues vv = null;
161       for( int j = 0 ; j < statements.length ; j++ ){
162         ((StatementExecutionContext)statementExecutionContext).addTriggerCount();
163         try {
164           sec.setRecordVersion(statementExecutionContext.getRecordVersion());
165           _Executer executer = null;
166           references = statements[j].getReferences();
167                        executer = statements[j].getExecuter() ;
168                        vv = new VariableValues(references, sec.getServerSession());
169                        if (references != null) {
170                           values = getValues(references, recordVersion, oldAlias, newAlias);
171                           vv.setConditionVariableValue(references, values, 1);
172                        }
173                        sec.setVariableValues(vv);
174                        executer.execute(vv);
175         }
176         catch (DException ex) {
177           throw ex;
178         }
179         finally{
180           ( (StatementExecutionContext) statementExecutionContext).
181               subTriggerCount();
182         }
183
184       }
185     }
186   }
187
188
189   private void getAllReferencesIncludingSubQuery(ArrayList aList,_Reference[] references)throws DException{
190     if(references == null)
191       return;
192     for (int i = 0; i < references.length; i++) {
193       if(references[i] instanceof subquery){
194         TableDetails[] td = ((subquery)references[i])._queryexpression0.getAllTableDetails();
195         aList.add(references[i]);
196         getAllReferencesIncludingSubQuery(aList,((subquery)references[i])._queryexpression0.getReferences(td));
197       }
198       else
199         aList.add(references[i]);
200     }
201   }
202
203
204   private void setDataTypeAndSizeForReferences( _Reference[] ref , _ColumnCharacteristics cc ) throws DException {
205      if( ref != null ) {
206          int len = ref.length;
207          String JavaDoc[] clms = cc.getColumnNames();
208          for( int i=0; i<len; i++ ){
209              String JavaDoc name = null;
210              try{
211                 name = ref[i].getColumn();
212              } catch( Exception JavaDoc E ){
213                 continue;
214              }
215             if( P.indexOfIgnoreCase( clms , name ) != -1 ){
216                int columnIndex = cc.getColumnIndex(name);
217                int type = cc.getColumnType(columnIndex);
218                 ((ColumnDetails )ref[i]).setDatatype( type );
219                 ((ColumnDetails )ref[i]).setSize( cc.getSize(columnIndex) );
220             }
221             else{
222          ;//// Removed By Program ** System.out.println(" Arraylist does not contains name = " + name );
223
}
224         }
225     }
226  }
227  private _VariableValues initializeVariableValues(_Reference[] references,_ServerSession serverSession , RecordVersion recVersion , String JavaDoc newAlias , String JavaDoc oldAlias ) throws DException {
228    if( references != null && checkForSubQuery(references)){
229      Object JavaDoc[][] subQueryIteratorMapping = getSubQueryIteratorMapping(references,serverSession);
230      _Reference[] ref1 = getSimpleReferences( references);
231      _VariableValues vv = new SubQueryVariableValues(ref1,subQueryIteratorMapping, serverSession);
232      if(ref1 != null){
233        vv.setConditionVariableValue( ref1 , getValues( ref1 , recVersion , oldAlias , newAlias ) , 0 );
234      }
235      return vv;
236    }
237    return new TriggerVariableValues( recVersion , oldAlias , newAlias, serverSession );
238  }
239  private boolean checkForSubQuery(_Reference[] references) throws DException{
240    if (references != null){
241      int length = references.length;
242      for (int i = 0; i < length; i++)
243        if(references[i].getReferenceType() == com.daffodilwoods.daffodildb.server.sql99.common.SimpleConstants.SUBQUERY)
244          return true;
245    }
246    return false;
247  }
248  private Object JavaDoc[][] getSubQueryIteratorMapping(_Reference[] references,_ServerSession object) throws DException{
249    int length = references.length;
250    ArrayList aList = new ArrayList(length);
251    for (int i = 0; i < length; i++) {
252      if(references[i].getReferenceType() == com.daffodilwoods.daffodildb.server.sql99.common.SimpleConstants.SUBQUERY){
253    _Iterator iterator = ((com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary.subquery)references[i]).getSelectIterator(object);
254    aList.add(new Object JavaDoc[]{references[i],iterator}); // make provision of getting _SelectIterator from _Executer
255
}
256    }
257    return (Object JavaDoc[][])aList.toArray(new Object JavaDoc[0][]);
258  }
259  private _Reference[] getSimpleReferences(_Reference[] references) throws DException{
260    int length = references.length;
261    ArrayList aList = new ArrayList(length);
262    for (int i = 0; i < length; i++) {
263      if(references[i].getReferenceType() != com.daffodilwoods.daffodildb.server.sql99.common.SimpleConstants.SUBQUERY){
264    aList.add(references[i]); // make provision of getting _SelectIterator from _Executer
265
}
266    }
267    return aList.size() == 0 ? null : (_Reference[])aList.toArray(new _Reference[0]);
268  }
269  public Object JavaDoc[] getValues(_Reference[] references,RecordVersion recordVersion,String JavaDoc oldAlias,String JavaDoc newAlias) throws DException {
270    Object JavaDoc[] values = new Object JavaDoc[references.length];
271    for (int k = 0; k < references.length; k++) {
272      String JavaDoc column = references[k].getColumn();
273      String JavaDoc tableName = null;
274      try{
275        tableName = references[k].getTriggerTableAlias();
276      } catch(DException de ) {
277         throw new TriggerException("DSE1303",new Object JavaDoc[]{ column , tableDetails.getQualifiedTableName() });
278      }
279      if( tableName == null || tableName.equalsIgnoreCase("new")||tableName.equalsIgnoreCase(newAlias)){
280        values[k] = recordVersion.getCurrentRecord().getObject(column);
281      }
282      if(tableName.equalsIgnoreCase("old")||tableName.equalsIgnoreCase(oldAlias)){
283        values[k] = recordVersion.getPreviousRecord().getObject(column);
284      }
285    }
286    return values;
287  }
288
289  public _Reference[] checkColumnsValidityAsReference( ColumnDetails[] columns , TableDetails tableDetails , _ServerSession serverSession ) throws DException {
290    ArrayList aList = new ArrayList(5);
291    if( columns != null ){
292       SetColumnProperties.setTableNamesAndDatatypesOfAllColumns( serverSession , columns , new TableDetails[]{tableDetails} ,aList, new ArrayList() );
293       for (int i = 0, length = aList.size() ; i < length; i++)
294         ((ColumnDetails)aList.get(i)).setUnderLyingReference(true);
295       return aList.size() > 0 ? (_Reference[]) aList.toArray( new _Reference[0] ) : null;
296    }
297    return null;
298  }
299
300
301
302
303
304
305
306
307
308
309 /*
310
311     private void fireStatementTriggers( _Trigger[] triggerDescriptors, _StatementExecutionContext statementExecutionContext )throws DException{
312
313     if( triggerDescriptors == null )
314       return ;
315     if(((StatementExecutionContext)statementExecutionContext).getTriggerCount() >=16)
316       throw new DException("DSE5583", null);
317
318     RecordVersion recordVersion = statementExecutionContext.getRecordVersion() ;
319     _ColumnCharacteristics cc = statementExecutionContext.getColumnCharacteristics(tableName) ;
320     _VariableValues tvv = null;
321     _Reference[] references = null;
322     for( int i = 0 ; i < triggerDescriptors.length ; i++ ){
323        String newAlias = triggerDescriptors[i].getNewAliasName() ;
324        String oldAlias = triggerDescriptors[i].getOldAliasName() ;
325        tvv = new TriggerVariableValues( recordVersion , oldAlias , newAlias );
326        booleanvalueexpression bve = triggerDescriptors[i].getSearchCondition();
327        if(bve!=null){
328          ColumnDetails[] columnDetails = bve.getColumnDetails();
329          _ServerSession serversession = statementExecutionContext.getServerSession();
330          checkColumnsValidityAsReference(columnDetails, tableDetails, serversession);
331          bve.checkSemantic(serversession);
332          ArrayList aList = new ArrayList(5);
333          getAllReferencesIncludingSubQuery(aList,bve.getReferences( new TableDetails[]{tableDetails} ));
334          int size = aList.size();
335          _Reference[] ref = size == 0 ? null : (_Reference[])aList.toArray(new _Reference[size]);
336          setDataTypeAndSizeForReferences( ref , cc );
337          tvv = initializeVariableValues( ref , statementExecutionContext.getServerSession() ,recordVersion,newAlias,oldAlias );
338        if( bve.run(tvv).hashCode() != 0){
339          continue ;
340        }
341        }
342       SQLexecutablestatement[] statements = triggerDescriptors[i].getStatement();
343       _StatementExecutionContext sec = null;
344       for( int j = 0 ; j < statements.length ; j++ ){
345         ((StatementExecutionContext)statementExecutionContext).addTriggerCount();
346         try {
347           sec = statementExecutionContext.getChildStatementExecutionContext();
348           sec.setRecordVersion(statementExecutionContext.getRecordVersion());
349           _Executer executer = null;
350
351           if (statements[j] instanceof SQLdatachangestatement) {
352             references = ( (SQLdatachangestatement) statements[j]).
353                 checkSemantic(sec.getServerSession());
354             if (references != null) {
355               throw new DException("DSE1262", null);
356             }
357             executer = ( (_Executer) ( (SQLdatachangestatement) statements[j]).
358                         run(sec));
359             executer.execute( (_VariableValues)null);
360           }
361           if (statements[j] instanceof SQLcontrolstatement) {
362             references = ( (SQLcontrolstatement) statements[j]).checkSemantic(
363                 sec.getServerSession());
364             if (references != null && references.length != 0) {
365               throw new DException("DSE1262", null);
366             }
367             executer = ( (_Executer) ( (SQLcontrolstatement) statements[j]).run(
368                 sec));
369             executer.execute(new VariableValues(sec.getServerSession()));
370           }
371         }
372         catch (DException ex) {
373           throw ex;
374         }
375         finally{
376           ( (StatementExecutionContext) statementExecutionContext).
377               subTriggerCount();
378         }
379
380       }
381     }
382  }*/

383
384
385
386
387 }
388
Popular Tags