KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > serversystem > chainedcolumn > TemporaryForeignKeyIterator


1 package com.daffodilwoods.daffodildb.server.serversystem.chainedcolumn;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.client.*;
6 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record;
7 import com.daffodilwoods.daffodildb.server.serversystem.*;
8 import com.daffodilwoods.daffodildb.server.sql99.common.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
11 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
12 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
13 import com.daffodilwoods.daffodildb.utils.field.*;
14 import com.daffodilwoods.database.general.*;
15 import com.daffodilwoods.database.resource.*;
16 import com.daffodilwoods.database.sqlinitiator.*;
17 public class TemporaryForeignKeyIterator extends BaseExceptionSingleIterator
18  implements _Iterator {
19
20    ArrayList parentsMapping;
21    HashMap iteratorMapping;
22    HashMap tablesMapping;
23    HashMap columnsMapping;
24    _Iterator startingIterator;
25    _ServerSession serverSession;
26    QualifiedIdentifier tableName;
27    ArrayList tableInfos = null;
28    int sourceTableInfoIndex = 0;
29
30    public TemporaryForeignKeyIterator(_ServerSession serverSession0,
31                                       HashMap tablesMapping0,
32                                       _Iterator temporaryIterator0,
33                                       QualifiedIdentifier tableName0,
34                                       HashMap columnsMapping0)
35          throws DException
36    {
37       tablesMapping = tablesMapping0;
38       startingIterator = temporaryIterator0;
39       serverSession = serverSession0;
40       tableName = tableName0;
41       columnsMapping = columnsMapping0;
42
43
44       setChainedTableInfos(tableName);
45       ChainedTableInfo temp = (ChainedTableInfo) tableInfos.get(0);
46       setMappings(temp);
47    }
48
49    private void setMappings(ChainedTableInfo tableInfo) throws DException {
50      parentsMapping = new ArrayList();
51      iteratorMapping = new HashMap();
52      parentsMapping.add(tableInfo.getChainedColumnInfo());
53      iteratorMapping.put(tableInfo.getChainedColumnInfo(),startingIterator);
54      makeIterator(tableInfo,null);
55      while(true) {
56         ChainedTableInfo parentInfo = tableInfo.getParentChainedTableInfo();
57         if(parentInfo == null)
58           break;
59         parentsMapping.add(parentInfo.getChainedColumnInfo());
60         IteratorInfo ii = tableInfo.getReferencedToReferencingTableIteratorInfo();
61
62         _SingleTableExecuter singleTableExecuter = new ConditionSingleTableExecuter( null , ii.tableDetails, serverSession , ii.bve , null );
63
64         _Iterator iterator = serverSession.getIterator(
65             parentInfo.getTableName(), singleTableExecuter);
66         iteratorMapping.put(parentInfo.getChainedColumnInfo(), iterator);
67
68         makeIterator(parentInfo,tableInfo);
69         tableInfo = parentInfo;
70      }
71    }
72
73    private void makeIterator(ChainedTableInfo parentInfo,
74                              ChainedTableInfo tempInfo)
75          throws DException {
76       ArrayList childInfos = parentInfo.getChildsChainedTableInfo();
77       if(childInfos == null)
78         return;
79       for (int i = 0, size = childInfos.size() ; i < size; i++) {
80          ChainedTableInfo childInfo = (ChainedTableInfo) childInfos.get(i);
81          QualifiedIdentifier childTableName = childInfo.getTableName();
82          if(tempInfo != null && childTableName.equals(tempInfo.getTableName()))
83             continue;
84          IteratorInfo ii = childInfo.getReferencingToReferencedTableIteratorInfo();
85
86          _SingleTableExecuter conditionExecuter = new ConditionSingleTableExecuter( null , ii.tableDetails, serverSession , ii.bve , null );
87
88          _Iterator childIterator = serverSession.getIterator(childInfo.getTableName(), conditionExecuter);
89          iteratorMapping.put(childInfo.getChainedColumnInfo(),childIterator);
90          makeIterator(childInfo,null);
91       }
92    }
93 /*
94    private void alignChilds(ChainedTableInfo tableInfo,
95                             ChainedColumnInfo cci0,
96                             _Record record)
97          throws DException {
98      ArrayList childInfos = tableInfo.getChildsChainedTableInfo();
99      if(childInfos == null)
100          return;
101       for (int i = 0, size = childInfos.size() ; i < size; i++) {
102          ChainedTableInfo childTableInfo = (ChainedTableInfo)childInfos.get(i);
103          ChainedColumnInfo childColumnInfo = childTableInfo.getChainedColumnInfo();
104          if(cci0 != null && childColumnInfo.equals(cci0))
105            return;
106       _Iterator iterator = (_Iterator) iteratorMapping.get(childColumnInfo);
107       IteratorInfo ii = childTableInfo.getReferencedTableToReferencingTableIterator();
108       iterator.setConditionVariableValue(ii.ref,new Object[]{record.getObject(ii.columnIndex)},0);
109       boolean first = iterator.first();
110       if( first == false )
111          continue;
112       else
113          alignChilds(childTableInfo,null,record);
114       }
115    }
116
117
118    private void alignParents(_Iterator iterator,_Record record) throws DException {
119       for (int i = 0 , size = parentsMapping.size() ; i < size ; i++) {
120          QualifiedIdentifier parentName = (QualifiedIdentifier) parentsMapping.get(i);
121          ChainedTableInfo parentCTi = getChainedTableInfo(parentName);
122          if(i != 0 ) {
123             IteratorInfo itInfo = parentCTi.getReferencingTableToReferencedTableIterator();
124             iterator.setConditionVariableValue(itInfo.ref,
125                   new Object[]{record.getObject(itInfo.columnIndex)},0);
126             boolean isFirst = iterator.first();
127             if( !isFirst) {
128                continue;
129             }
130          }
131          alignChilds(parentCTi,parentCTi.getChainedColumnInfo(),record);
132       }
133    }
134 */

135    /*
136       alignChilds(cci,index, iterator) of the parent passed.
137       Loop starting from parentIndex+1 index till the last of the arraylist
138           get the iterator info for the current index
139           set the conditionVariableValue of the iterator.
140           call the iterator first;
141           alignChilds(currentindex's cci, currentIndex,iterator);
142    */

143
144    private void alignParents(int parentIndex, _Iterator iterator)
145        throws DException{
146
147      alignChilds((ChainedColumnInfo)parentsMapping.get(parentIndex),parentIndex,iterator);
148      _Record record = iterator.getRecord();
149      int parentSize = parentsMapping.size();
150      for(int size=parentSize-1;parentIndex<size;){//sachin
151
ChainedColumnInfo cci = (ChainedColumnInfo) parentsMapping.get(parentIndex++); // sachin
152
ChainedTableInfo tableInfo = (ChainedTableInfo) tablesMapping.get(cci);
153        IteratorInfo ii = tableInfo.getReferencedToReferencingTableIteratorInfo();
154        _Iterator iter = (_Iterator) iteratorMapping.get(parentsMapping.get(parentIndex));
155        iter.setConditionVariableValue(ii.ref,new Object JavaDoc[]{record.getObject(ii.columnIndex)},0);
156        boolean isFirst = iter.first();
157        if(!isFirst)
158          break;
159        record = iter.getRecord();
160        alignChilds((ChainedColumnInfo)parentsMapping.get(parentIndex) ,parentIndex,iter);
161      }
162    }
163
164    /*
165        get the childs of the cci passed.
166        Loop for the childs
167           get the child chained table info
168           if( parentIndex > 0 )
169              check the childChainedTableInfo equals the parentMapping.get(parentIndex-1)
170                   continue;
171           get the iterator from the iterator mapping;
172           set the condition variable values;
173           call the iterator first;
174           call alignChilds(child cci, -1, iterator);
175    */

176
177    private void alignChilds(ChainedColumnInfo cci , int parentIndex , _Iterator iterator)
178        throws DException {
179      ChainedTableInfo cti = (ChainedTableInfo)tablesMapping.get(cci);
180      ArrayList list = cti.getChildsChainedTableInfo();
181      if(list == null) {
182          ;//// Removed By Program ** System.out.println(" Childs are null.");
183
return;
184      }
185      for (int i = 0, size = list.size(); i < size ; i++) {
186          ;//// Removed By Program ** System.out.println(" value of i inside alignChilds == " + i);
187
ChainedTableInfo childTableInfo = (ChainedTableInfo) list.get(i);
188        ChainedColumnInfo childColumnInfo = childTableInfo.getChainedColumnInfo();
189        if(parentIndex > 0 &&
190           childColumnInfo.equals((ChainedColumnInfo) parentsMapping.get(parentIndex -1))) {
191          continue;
192        }
193        IteratorInfo ii = childTableInfo.getReferencingToReferencedTableIteratorInfo();
194        _Iterator iter = (_Iterator) iteratorMapping.get(childColumnInfo);
195        iter.setConditionVariableValue(ii.ref,new Object JavaDoc[] { iterator.getRecord().getObject(ii.columnIndex)} ,0);
196        boolean isFirst = iter.first();
197        if(!isFirst)
198          continue;
199        alignChilds(childColumnInfo, -1 ,iter);
200      }
201    }
202
203    public _OrderCount getOrderCounts() throws com.daffodilwoods.database.resource.DException {
204       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.interfaces._Iterator method*/
205       throw new java.lang.UnsupportedOperationException JavaDoc("Method getOrderCounts() not yet implemented.");
206    }
207    public TableDetails[] getTableDetails() throws com.daffodilwoods.database.resource.DException {
208       TableDetails tableD = new TableDetails();
209       tableD.setTableName(new String JavaDoc[]{ tableName.catalog,tableName.schema,tableName.name });
210       return new TableDetails[]{tableD};
211    }
212    public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException {
213       return this;
214    }
215    public void setConditionVariableValue(_Reference[] parm1, Object JavaDoc[] parm2, int parm3) throws com.daffodilwoods.database.resource.DException {
216    }
217    public Object JavaDoc getColumnValues() throws com.daffodilwoods.database.resource.DException {
218       return startingIterator.getColumnValues();
219    }
220    public _KeyColumnInformation[] getKeyColumnInformations() throws com.daffodilwoods.database.resource.DException {
221       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.interfaces._Iterator method*/
222       throw new java.lang.UnsupportedOperationException JavaDoc("Method getKeyColumnInformations() not yet implemented.");
223    }
224    public Object JavaDoc[] getUniqueColumnReference() throws com.daffodilwoods.database.resource.DException {
225       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.interfaces._Iterator method*/
226       throw new java.lang.UnsupportedOperationException JavaDoc("Method getUniqueColumnReference() not yet implemented.");
227    }
228    public boolean seek(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
229       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.interfaces._Iterator method*/
230       throw new java.lang.UnsupportedOperationException JavaDoc("Method seek() not yet implemented.");
231    }
232    public _Order getDefaultOrder() throws com.daffodilwoods.database.resource.DException {
233       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.interfaces._Iterator method*/
234       throw new java.lang.UnsupportedOperationException JavaDoc("Method getDefaultOrder() not yet implemented.");
235    }
236    public Object JavaDoc getColumnValues(_Reference[] parm1) throws com.daffodilwoods.database.resource.DException {
237       Object JavaDoc[] values = new Object JavaDoc[parm1.length];
238       for (int i = 0; i < parm1.length; i++) {
239          String JavaDoc[] aliasName = ((ColumnDetails)parm1[i]).getTableAliasArray();
240          if ( aliasName == null )
241             values[i] = startingIterator.getColumnValues(parm1);
242          _Iterator iterator = (_Iterator) iteratorMapping.get(columnsMapping.get(aliasName));
243          if(iterator != null)
244             values[i] = iterator.getColumnValues(parm1);
245       }
246       return values;
247    }
248    public Object JavaDoc getColumnValues(_Reference parm1) throws com.daffodilwoods.database.resource.DException {
249       String JavaDoc[] aliasName = ((ColumnDetails)parm1).getTableAliasArray();
250       if ( aliasName == null )
251          return startingIterator.getColumnValues(parm1);
252       _Iterator iterator = (_Iterator) iteratorMapping.get(columnsMapping.get(aliasName));
253       if(iterator != null)
254          return iterator.getColumnValues(parm1);
255       return null;
256    }
257    public void setIterator(_Iterator parm1) throws com.daffodilwoods.database.resource.DException {
258       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.utils._VariableValues method*/
259       throw new java.lang.UnsupportedOperationException JavaDoc("Method setIterator() not yet implemented.");
260    }
261    public void addReferences(_Reference[] parm1) throws com.daffodilwoods.database.resource.DException {
262       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.utils._VariableValues method*/
263       throw new java.lang.UnsupportedOperationException JavaDoc("Method addReferences() not yet implemented.");
264    }
265    public Object JavaDoc[][] getReferenceAndValuePair() throws com.daffodilwoods.database.resource.DException {
266       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.utils._VariableValues method*/
267       throw new java.lang.UnsupportedOperationException JavaDoc("Method getReferenceAndValuePair() not yet implemented.");
268    }
269    public boolean seekFromTop(_IndexPredicate[] parm1) throws com.daffodilwoods.database.resource.DException {
270       /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIterator method*/
271       throw new java.lang.UnsupportedOperationException JavaDoc("Method seekFromTop() not yet implemented.");
272    }
273    public boolean seekFromTopRelative(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
274       /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIterator method*/
275       throw new java.lang.UnsupportedOperationException JavaDoc("Method seekFromTopRelative() not yet implemented.");
276    }
277    public boolean seekFromBottom(_IndexPredicate[] parm1) throws com.daffodilwoods.database.resource.DException {
278       /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIterator method*/
279       throw new java.lang.UnsupportedOperationException JavaDoc("Method seekFromBottom() not yet implemented.");
280    }
281    public boolean seekFromBottomRelative(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
282       /**@todo: Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexIterator method*/
283       throw new java.lang.UnsupportedOperationException JavaDoc("Method seekFromBottomRelative() not yet implemented.");
284    }
285
286    public boolean first() throws com.daffodilwoods.database.resource.DException {
287       if(sourceTableInfoIndex != 0)
288        setMappings((ChainedTableInfo)tableInfos.get(0));
289       boolean found = startingIterator.first();
290       if(found) {
291          alignParents(0,startingIterator);
292       }
293       return found;
294    }
295
296    public boolean last() throws com.daffodilwoods.database.resource.DException {
297       if(sourceTableInfoIndex != 0)
298        setMappings((ChainedTableInfo)tableInfos.get(tableInfos.size()-1));
299       boolean found = startingIterator.last();
300       if(found) {
301          alignParents(0,startingIterator);
302       }
303       return found;
304    }
305
306    /*
307       check the last iterator has next;
308       if nextfound call the alignparent with respective values;
309       if not check for iterator with index-1 in the parent mapping;
310    */

311
312    public boolean next() throws com.daffodilwoods.database.resource.DException {
313      int currentIndex = parentsMapping.size()-1;
314      ChainedColumnInfo cci = (ChainedColumnInfo)parentsMapping.get(currentIndex);
315      _Iterator it = (_Iterator) iteratorMapping.get(cci); //retrieve from the iterator mapping;
316
while(!it.next()){
317        if( --currentIndex == -1 ) {
318          return reInitialize(++sourceTableInfoIndex);
319        }
320        cci = (ChainedColumnInfo) parentsMapping.get(currentIndex);
321        it = (_Iterator) iteratorMapping.get(cci);
322      }
323      alignParents(currentIndex, it);
324      return true;
325    }
326
327    public boolean previous() throws com.daffodilwoods.database.resource.DException {
328      int currentIndex = parentsMapping.size()-1;
329      ChainedColumnInfo cci = (ChainedColumnInfo)parentsMapping.get(currentIndex);
330      _Iterator it = (_Iterator) iteratorMapping.get(cci); //retrieve from the iterator mapping;
331
while(!(it.previous())){
332        if( --currentIndex == -1 ) {
333          return reInitialize(--sourceTableInfoIndex);
334        }
335        cci = (ChainedColumnInfo) parentsMapping.get(currentIndex);
336        it = (_Iterator) iteratorMapping.get(cci);
337      }
338      alignParents(currentIndex, it);
339      return true;
340    }
341    public Object JavaDoc getKey() throws com.daffodilwoods.database.resource.DException {
342       /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._TableIterator method*/
343       throw new java.lang.UnsupportedOperationException JavaDoc("Method getKey() not yet implemented.");
344    }
345    public void move(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
346       /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._TableIterator method*/
347       throw new java.lang.UnsupportedOperationException JavaDoc("Method move() not yet implemented.");
348    }
349    public Object JavaDoc getColumnValues(int[] parm1) throws com.daffodilwoods.database.resource.DException {
350       return startingIterator.getColumnValues(parm1);
351    }
352    public _Record getRecord() throws com.daffodilwoods.database.resource.DException {
353       /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._TableIterator method*/
354       throw new java.lang.UnsupportedOperationException JavaDoc("Method getRecord() not yet implemented.");
355    }
356
357    private void setChainedTableInfos(QualifiedIdentifier tableName) throws DException {
358       Iterator iterator = tablesMapping.values().iterator();
359       tableInfos = new ArrayList ();
360       boolean tableFound = false;
361       while(iterator.hasNext()) {
362       ChainedTableInfo cti = (ChainedTableInfo) iterator.next();
363         if(cti.getTableName().equals(tableName)) {
364          ;//// Removed By Program ** System.out.println(" TableInfo : " + cti + " CCI = " + cti.getChainedColumnInfo());
365
tableInfos.add(cti);
366           tableFound = true;
367         }
368       }
369       if(!tableFound)
370       throw new DException("DSE0",new Object JavaDoc[]{"TableInfo Not Found"});
371    }
372
373    private boolean reInitialize(int index) throws DException {
374      int size = tableInfos.size();
375      if( size > 1 && index < size ){
376        ChainedTableInfo cti = (ChainedTableInfo) tableInfos.get(index);
377        setMappings(cti);
378        boolean first = startingIterator.first();
379        if( first )
380          alignParents(0,startingIterator);
381        return true;
382      }
383      else
384        return false;
385    }
386
387
388   public Object JavaDoc getColumnValues(int column) throws DException {
389     /**@todo Implement this com.daffodilwoods.daffodildb.server.datasystem.interfaces._TableIterator method*/
390     throw new java.lang.UnsupportedOperationException JavaDoc("Method getColumnValues() not yet implemented.");
391   }
392    public _ExecutionPlan getExecutionPlan() throws DException{
393       _ExecutionPlan cplan = startingIterator.getExecutionPlan();
394       _ExecutionPlan cplans[] = cplan == null ? null : new _ExecutionPlan[]{cplan};
395       ExecutionPlan plan = new ExecutionPlan("TemporaryForeignKeyIterator",cplans,null,null,null);
396       return plan;
397    }
398    public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException {
399       return startingIterator.getExecutionPlanForBrowser();
400    }
401   public _Iterator getBaseIteratorHasRecord(ColumnDetails hasRecordColumn) throws DException {
402     return startingIterator.getBaseIteratorHasRecord(hasRecordColumn);
403   }
404     public FieldBase field(_Reference reference) throws com.daffodilwoods.database.resource.DException {
405         throw new java.lang.UnsupportedOperationException JavaDoc("Method not yet implemented.");
406     }
407     public FieldBase[] fields(_Reference[] references) throws com.daffodilwoods.database.resource.DException {
408         throw new java.lang.UnsupportedOperationException JavaDoc("Method fields() not yet implemented.");
409     }
410     public FieldBase[] fields(int[] columns) throws com.daffodilwoods.database.resource.DException {
411         throw new java.lang.UnsupportedOperationException JavaDoc("Method fields() not yet implemented.");
412     }
413   public void releaseResource() throws DException {
414
415   }
416   public void setSpecificUnderlyingReferences(_Reference[] specificUnderlyingReferences) throws DException{
417         throw new java.lang.UnsupportedOperationException JavaDoc(
418              "Method setSpecificUnderlyingReferences() not yet implemented.");
419       }
420
421 }
422
Popular Tags