KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > iterator > table > NaturalFullJoinUnionIterator


1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table;
2
3 import com.daffodilwoods.daffodildb.server.sql99.common.*;
4 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
5 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.set.*;
6 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
7 import com.daffodilwoods.daffodildb.utils.field.*;
8 import com.daffodilwoods.database.resource.*;
9
10 /**
11  * <p>Title: NaturalFullJoinUnionIterator </p>
12  * <p>Description: This Class assists to retrive the result of NATURAL FULL OUTER
13  * JOIN. FULL OUTER JOIN is retrived by Union All of LOJ of given underlying
14  * iterators and specialRightJoinIterator iterator. Natural eliminates the
15  * duplicate column name.
16  *
17  * This class has only retrieval methods. All the other methods are used
18  * from the extending class FullJoinUnionIterator that alignes underlying
19  * iterators acc. to navigation methods. </p>
20  * <p>Copyright: Copyright (c) 2003</p>
21  * <p>Company: </p>
22  * @author unascribed
23  * @version 1.0
24  */

25 public class NaturalFullJoinUnionIterator extends FullJoinUnionIterator {
26
27   /**
28    * 2-D Array representing the columns corresponding to left and right table.
29    */

30   private Object JavaDoc[][] commonColumns;
31
32   /**
33    * Initailizes the left and right iterator and column mapping.
34    * @param leftIterator0 left iterator
35    * @param rightIterator0 right iterator
36    * @param commonColumns0 left and right table column details
37    * @throws DException
38    */

39   public NaturalFullJoinUnionIterator(_Iterator leftIterator0, _Iterator rightIterator0, Object JavaDoc[][] commonColumns0) throws DException {
40       super(leftIterator0, rightIterator0);
41       commonColumns = modifyMapping(commonColumns0);
42    }
43
44    /**
45     * This method is responsible to update the mapping passed.
46     * Puts the left column name at 1st position.
47     * and the right column name at 2nd position.
48     * @param commonColumns0 mapping to be updated
49     * @return updated mapping
50     */

51    private Object JavaDoc[][] modifyMapping(Object JavaDoc[][] commonColumns0) {
52       Object JavaDoc[][] newMapping = new Object JavaDoc[commonColumns0.length][3];
53       for (int i = 0; i < commonColumns0.length; i++) {
54          ColumnDetails column = (ColumnDetails) commonColumns0[i][0];
55          newMapping[i][0] = column.getColumn();
56          newMapping[i][1] = column;
57          newMapping[i][2] = commonColumns0[i][1];
58       }
59       return newMapping;
60    }
61
62    /**
63     * The following methods are used for getting values of current record.
64     * These methods check the passed column, if the column corresponds to left
65     * iterator, its values are retrived from the left iterator, otherwise, from
66     * the right iterator.
67     */

68    /**
69     * This method is used to retrieve the value of passed reference.
70     * @param reference reference for which value is to be retrived.
71     * Reference may be column or parameter.
72     * @return NonShared FieldBase denoting the value of Reference. Non Shared
73     * FieldBases are those for which BufferRange is not shared with some other
74     * FieldBase.
75     * @throws DException
76     */

77    public Object JavaDoc getColumnValues(_Reference reference) throws DException {
78       reference = getAppropriateReference(reference);
79       if (state == FIRSTISCURRENT) {
80          return leftIterator.getColumnValues(reference);
81       } else if (state == SECONDISCURRENT) {
82          return rightIterator.getColumnValues(reference);
83       }
84       throw new DException("DSE3518", null);
85    }
86
87    /**
88     * This method is used to retrieve the values of passed references.
89     * @param references reference for which values are to be retrived.
90     * Reference may be column or parameter.
91     * @return NonShared FieldBases denoting the value of References. Non Shared
92     * FieldBases are those for which BufferRange is not shared with some other
93     * FieldBase.
94     * @throws DException
95     */

96    public Object JavaDoc getColumnValues(_Reference[] references) throws DException {
97       references = getAppropriateReference(references);
98       if (state == FIRSTISCURRENT) {
99          return leftIterator.getColumnValues(references);
100       } else if (state == SECONDISCURRENT) {
101          return rightIterator.getColumnValues(references);
102       }
103       throw new DException("DSE3518", null);
104    }
105
106    /**
107     * This method return the shared FieldBase for the passed reference. By Shared
108     * FieldBase we mean, BufferRange of FieldBase is shared with other FieldBase
109     * objects.
110     * @param reference reference for which value is to be retrived
111     * @return shared field base correspondition to passed column reference
112     * @throws DException
113     */

114    public FieldBase field(_Reference reference) throws com.daffodilwoods.database.resource.DException {
115       reference = getAppropriateReference(reference);
116       return state == FIRSTISCURRENT ? leftIterator.field(reference) :
117           rightIterator.field(reference);
118    }
119
120    /**
121     * This method return the shared FieldBases for the passed references. By Shared
122     * FieldBase we mean, BufferRange of FieldBase is shared with other FieldBase
123     * objects.
124     * @param references reference for which values are to be retrived
125     * @return shared field base correspondition to passed column reference
126     * @throws DException
127     */

128    public FieldBase[] fields(_Reference[] references) throws com.daffodilwoods.database.resource.DException {
129       references = getAppropriateReference(references);
130       return state == FIRSTISCURRENT ? leftIterator.fields(references) :
131           rightIterator.fields(references);
132    }
133
134    /**
135     * Checks the passed column, if it belongs to left table, left column
136     * name is retruned otherwise right table name is returned.
137     * @param reference passed column
138     * @return column name
139     * @throws DException
140     */

141    private _Reference getAppropriateReference(_Reference reference) throws DException {
142       for (int i = 0; i < commonColumns.length; i++) {
143          if ( ( (String JavaDoc) commonColumns[i][0]).equalsIgnoreCase(reference.getColumn())) {
144             if (state == FIRSTISCURRENT) {
145                return (_Reference) commonColumns[i][1];
146             }
147             return (_Reference) commonColumns[i][2];
148          }
149       }
150       return reference;
151    }
152
153    /**
154     * this method is responsible to return the references corresponding to
155     * the passed columns.
156     * @param reference columns
157     * @return references
158     * @throws DException
159     */

160    private _Reference[] getAppropriateReference(_Reference[] reference) throws DException {
161       _Reference[] newReferences = new _Reference[reference.length];
162       for (int i = 0; i < reference.length; i++) {
163          newReferences[i] = getAppropriateReference(reference[i]);
164       }
165       return newReferences;
166    }
167 }
168
Popular Tags