KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > tableexpression > fromclause > crossjoin


1 package com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause;
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.common.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.common.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*;
11 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*;
13 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.order.*;
14 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.*;
15 import com.daffodilwoods.daffodildb.server.sql99.dql.semanticchecker.*;
16 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
17 import com.daffodilwoods.daffodildb.server.sql99.token.*;
18 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
19 import com.daffodilwoods.daffodildb.utils.parser.*;
20 import com.daffodilwoods.database.resource.*;
21 import com.daffodilwoods.database.sqlinitiator.*;
22
23 /**
24  * This class represents cross join ,which means cartesian of two table. It
25  * provides the functionality of obtaining plan which in turn helps in obtaining
26  * resultset. The resultset of cross join will contain as many rows as
27  * equivalent to multiplication of rows obtained from each table involved in
28  * cross join.
29  * <p>Title: </p>
30  * <br> <table reference> CROSS JOIN <table primary></br>
31  * <p>Description: </p>
32  * <p>Copyright: Copyright (c) 2003</p>
33  * <p>Company: Daffodil S/W Ltd. </p>
34  * @author Select Team
35  * @version 1.0
36  */

37
38 public class crossjoin implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter, joinedtable, TypeConstants, queryexpressionbody, tableprimary {
39    /**
40     * It represents right side table of cross join.
41     */

42    public tableprimary _tableprimary0;
43    /**
44     * It represents keyword 'join' of cross join.
45     */

46    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439221;
47    /**
48     * It represents keyword 'cross' of cross join.
49     */

50    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439222;
51    /**
52     * It represents left side table of cross join.
53     */

54    public tablereference _tablereference3;
55    /**
56     * It represents all table involved in cross join.
57     */

58    private TableDetails[] tableDetails;
59    /**
60     * It represents all tables involved in cross join. If view is involved in
61     * cross join then underlying tables of view are also specified here.
62     */

63
64    private TableDetails[] allTableDetails;
65
66    public crossjoin(tablereference _tablereference4, tableprimary _tableprimary1, TableDetails[] tableDetails0) throws DException {
67       _tablereference3 = _tablereference4;
68       _tableprimary0 = _tableprimary1;
69       tableDetails = tableDetails0;
70       _SRESERVEDWORD12065439221 = new SRESERVEDWORD1206543922();
71       _SRESERVEDWORD12065439221._SRESERVEDWORD12065439220 = "JOIN";
72       _SRESERVEDWORD12065439222 = new SRESERVEDWORD1206543922();
73       _SRESERVEDWORD12065439222._SRESERVEDWORD12065439220 = "CROSS";
74       getColumnDetails();
75    }
76
77    public crossjoin() {
78    }
79
80    /**
81     * This method is used to obtain the plan of cross join with the help of
82     * passed condition plan and order plan. It returns array of underlying plans.
83     * It is assumed that resultant resultset will do cartesian product of
84     * resultsets obtained from each plan in the array.
85     * @param serverSession
86     * @param datedCondition
87     * @param bvePlan
88     * @param orderPlan
89     * @param queryColumns
90     * @param conditionArray
91     * @return
92     * @throws DException
93     */

94    public _TablePlan[] getTablePlan(_ServerSession serverSession, _DatedFramework datedCondition, _BVEPlan bvePlan, _OrderPlan orderPlan, _QueryColumns queryColumns, ConditionArray conditionArray) throws DException {
95       _TablePlan leftPlan[] = _tablereference3.getTablePlan(serverSession, datedCondition, bvePlan, orderPlan, queryColumns, conditionArray);
96       _TablePlan rightPlan[] = _tableprimary0.getTablePlan(serverSession, datedCondition, bvePlan, orderPlan, queryColumns, conditionArray);
97       _TablePlan tablePlans[] = new _TablePlan[leftPlan.length + rightPlan.length];
98       System.arraycopy(leftPlan, 0, tablePlans, 0, leftPlan.length);
99       System.arraycopy(rightPlan, 0, tablePlans, leftPlan.length, rightPlan.length);
100       return tablePlans;
101    }
102    /**
103     * It is similar to getTablePlan method.
104     * @param session
105     * @param datedCondition
106     * @param bvePlan
107     * @param orderPlan
108     * @param queryColumns
109     * @param conditionArray
110     * @return
111     * @throws DException
112     */

113    public _TablePlan[] getTablePlans(_ServerSession session, _DatedFramework datedCondition, _BVEPlan bvePlan, _OrderPlan orderPlan, _QueryColumns queryColumns, ConditionArray conditionArray) throws DException {
114       return getTablePlan(session, datedCondition, bvePlan, orderPlan, queryColumns, conditionArray);
115    }
116
117    /**
118     * It is used to perform semantic checking. It returns those columns whose
119     * values are to be provided by user like parameters(question mark). It
120     * diverts the call to underlying children.
121     * @param parent
122     * @return _References[]
123     * @throws DException
124     */

125    public com.daffodilwoods.daffodildb.server.sql99.utils._Reference[] checkSemantic(com.daffodilwoods.daffodildb.server.serversystem._ServerSession parent, ColumnDetails[] queryColumns, boolean checkUserRight) throws DException {
126       return GeneralPurposeStaticClass.getJointReferences(_tablereference3.checkSemantic(parent, queryColumns, checkUserRight), _tableprimary0.checkSemantic(parent, queryColumns, checkUserRight));
127    }
128
129
130    /**
131     * Note:-For documentation of following method please refers to queryexpressionbody.
132     * @param object
133     * @return
134     * @throws com.daffodilwoods.database.resource.DException
135     */

136    public TableDetails[] getTableDetails(_ServerSession serverSession, ColumnDetails[] queryColumns) throws DException {
137       TableDetails[] table1 = _tablereference3.getTableDetails(serverSession, queryColumns);
138       TableDetails[] table2 = _tableprimary0.getTableDetails(serverSession, queryColumns);
139       TableDetails[] result = new TableDetails[table1.length + table2.length];
140       int i;
141       for (i = 0; i < table1.length; i++) {
142          result[i] = table1[i];
143       }
144       for (int j = 0; j < table2.length; j++, i++) {
145          result[i] = table2[j];
146       }
147       this.tableDetails = result;
148       return result;
149    }
150
151    public ColumnDetails[] getColumnDetails() throws DException {
152       ColumnDetails[] columnDetails1 = _tablereference3.getColumnDetails();
153       ColumnDetails[] columnDetails2 = _tableprimary0.getColumnDetails();
154       return SemanticChecker.addColumnDetails(columnDetails1, columnDetails2);
155    }
156
157    public void getColumnsIncluded(ArrayList aList) throws DException {
158       _tablereference3.getColumnsIncluded(aList);
159       _tableprimary0.getColumnsIncluded(aList);
160    }
161
162    public void getTablesIncluded(ArrayList aList) throws DException {
163       _tablereference3.getTablesIncluded(aList);
164       _tableprimary0.getTablesIncluded(aList);
165    }
166
167    public ParameterInfo[] getParameterInfo() throws DException {
168       ParameterInfo[] parameterInfo1 = _tableprimary0.getParameterInfo();
169       ParameterInfo[] parameterInfo2 = _tablereference3.getParameterInfo();
170       if (parameterInfo1 == null && parameterInfo2 == null) {
171          return null;
172       }
173       if (parameterInfo1 == null) {
174          return parameterInfo2;
175       }
176       if (parameterInfo2 == null) {
177          return parameterInfo1;
178       }
179       ParameterInfo[] result = new ParameterInfo[parameterInfo1.length + parameterInfo2.length];
180       System.arraycopy(parameterInfo1, 0, result, 0, parameterInfo1.length);
181       System.arraycopy(parameterInfo2, 0, result, parameterInfo1.length, parameterInfo2.length);
182       return result;
183    }
184
185    public void setTablesForInsertion(ColumnMappingHandler parm1, _VariableValueOperations vv) throws com.daffodilwoods.database.resource.DException {
186       _tablereference3.setTablesForInsertion(parm1, vv);
187       _tableprimary0.setTablesForInsertion(parm1, vv);
188    }
189
190    public void verifyValues(_VariableValueOperations variableValueOperation) throws DException {
191    }
192
193    public void setDefaultValues(_VariableValueOperations variableValueOperation) throws DException {
194       _tablereference3.setDefaultValues(variableValueOperation);
195       _tableprimary0.setDefaultValues(variableValueOperation);
196    }
197    public Object JavaDoc[] getParameters(Object JavaDoc object) throws DException {
198       Object JavaDoc[] parameter1 = _tableprimary0.getParameters(object);
199       Object JavaDoc[] parameter2 = _tablereference3.getParameters(object);
200       if (parameter1 == null) {
201          return parameter2;
202       }
203       if (parameter2 == null) {
204          return parameter1;
205       }
206       Object JavaDoc[] result = new ParameterInfo[parameter1.length + parameter2.length];
207       System.arraycopy(parameter1, 0, result, 0, parameter1.length);
208       System.arraycopy(parameter2, 0, result, parameter1.length, parameter2.length);
209       return result;
210    }
211
212    public TableDetails[] getAllTableDetails() throws DException {
213       if (allTableDetails == null) {
214          TableDetails[] td1 = _tablereference3.getAllTableDetails();
215          TableDetails[] td2 = _tableprimary0.getAllTableDetails();
216          allTableDetails = GeneralPurposeStaticClass.getJointTableDetails(td1, td2);
217       }
218       return allTableDetails;
219    }
220
221    public TableDetails[] getViewTableDetails() throws DException {
222       TableDetails[] td1 = _tablereference3.getViewTableDetails();
223       TableDetails[] td2 = _tableprimary0.getViewTableDetails();
224       return GeneralPurposeStaticClass.getJointTableDetails(td1, td2);
225    }
226
227    public TableDetails[] getTablesForBlankInsert() throws DException {
228       ArrayList arr = new ArrayList();
229       arr.addAll(Arrays.asList(_tablereference3.getTablesForBlankInsert()));
230       arr.addAll(Arrays.asList(_tableprimary0.getTablesForBlankInsert()));
231       return (TableDetails[]) arr.toArray(new TableDetails[0]);
232    }
233
234    public _BVEPlan getBveExecutionPlan() throws DException {
235       return BVEPlanMerger.mergeTablePlansWithAnd(_tablereference3.getBveExecutionPlan(), _tableprimary0.getBveExecutionPlan());
236    }
237
238    /**
239     * This method is used to obtain those columns of join condition which avoids
240     * the scope management rule. Scope management rule is avoided when columns
241     * of condition don't belong to underlying tables.
242     * @return
243     * @throws DException
244     */

245
246    public _Reference[] getUnderlyingReferences() throws DException {
247       return GeneralPurposeStaticClass.getJointReferences(_tablereference3.getUnderlyingReferences(), _tableprimary0.getUnderlyingReferences());
248    }
249
250    public String JavaDoc toString() {
251       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
252       sb.append(" ");
253       sb.append(_tablereference3);
254       sb.append(" ");
255       sb.append(_SRESERVEDWORD12065439222);
256       sb.append(" ");
257       sb.append(_SRESERVEDWORD12065439221);
258       sb.append(" ");
259       sb.append(_tableprimary0);
260       return sb.toString();
261    }
262
263    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
264       crossjoin tempClass = new crossjoin();
265       tempClass._tableprimary0 = (tableprimary) _tableprimary0.clone();
266       tempClass._SRESERVEDWORD12065439221 = (SRESERVEDWORD1206543922) _SRESERVEDWORD12065439221.clone();
267       tempClass._SRESERVEDWORD12065439222 = (SRESERVEDWORD1206543922) _SRESERVEDWORD12065439222.clone();
268       tempClass._tablereference3 = (tablereference) _tablereference3.clone();
269       return tempClass;
270    }
271
272    /**
273     * This method is required to obtain those single tables on which order are
274     * shifted. It is done so as to maintain those tables in same sequence as
275     * that of order, present in order plan, in the final resultset.
276     * @param mapping1
277     * @param mapping2
278     * @return
279     * @throws DException
280     */

281    private OrderMappingWithTableRefernces[] concat(OrderMappingWithTableRefernces[] mapping1, OrderMappingWithTableRefernces[] mapping2) throws DException {
282       OrderMappingWithTableRefernces[] finalOrderMappingWithTableRefernces = new OrderMappingWithTableRefernces[mapping1.length + mapping2.length];
283       System.arraycopy(mapping1, 0, finalOrderMappingWithTableRefernces, 0, mapping1.length);
284       System.arraycopy(mapping2, 0, finalOrderMappingWithTableRefernces, mapping1.length, mapping2.length);
285       return finalOrderMappingWithTableRefernces;
286    }
287    /**
288     * This method is used to concat two arrays into one.
289     * @param mapping1
290     * @param mapping2
291     * @return
292     * @throws DException
293     */

294    private OrderMappingWithTableRefernces[] changeTableRefernceInMapping(OrderMappingWithTableRefernces mapping1[], OrderMappingWithTableRefernces[] mapping2) throws DException {
295       if (mapping1 == null) {
296          return mapping2;
297       }
298       if (mapping2 == null) {
299          return mapping1;
300       }
301       return concat(mapping1, mapping2);
302    }
303
304    /**
305     * This method is used to check whether single table level order passed to
306     * fromclause can be shifted to single table. If cross join occured under
307     * left outer join, then order will be shifted only when single table level
308     * order is present on all tables. otherwise single table level order can't
309     * be shifted to single table level. If cross join is present independently,
310     * then order can be shifted to single table. For further details refer to
311     * documentation of tablereferencelist.
312     * @param session
313     * @param orderPlan
314     * @param bvePlan
315     * @param queryColumns
316     * @param conditionArray
317     * @param isUnderLoj
318     * @return
319     * @throws DException
320     */

321    public OrderMappingWithTableRefernces[] checkForOrderSequencePlan(_ServerSession session, _OrderPlan orderPlan, _BVEPlan bvePlan, _QueryColumns queryColumns, ConditionArray conditionArray, boolean isUnderLoj) throws DException {
322       OrderMappingWithTableRefernces[] leftMapping = _tablereference3.checkForOrderSequencePlan(session, orderPlan, null, queryColumns, conditionArray, isUnderLoj);
323       OrderMappingWithTableRefernces[] rightMapping = _tableprimary0.checkForOrderSequencePlan(session, orderPlan, null, queryColumns, conditionArray, isUnderLoj);
324       return getJoinOrderMappingWithTableRefernces(leftMapping, rightMapping, isUnderLoj);
325    }
326
327    /**
328     * This method returns the joint OrderMapping of the Order Mappings obtained
329     * From The left and right Table References.
330     * There are two case ---
331     * 1. If the inner join is under LOJ Or ROJ
332     * e.g. select * from ( A inner join B ) LOJ C
333     * in such cases we need to return just one object of Order Mapping.
334     * also we can return something if and only if both sides return something.
335     * e.g. select * from ( A inner join B) LOJ C order by A.id , B.id
336     * in this case we return a single object of Order Mapping containg
337     * STOPs of A.id and B.id and a match value = FULLYMATCHED
338     * ---------------------------------------------------------------
339     * while in the case
340     * e.g. select * from ( A inner join B) LOJ C order by A.id , C.id
341     * In this case we return null.
342     * 2. If it is not under any LOJ or ROJ then we simply concatenate the two
343     * mappings and return the array of Order Mapping.
344     * @param source1
345     * @param source2
346     * @param isUnderLoj
347     * @return
348     * @throws DException
349     */

350    private OrderMappingWithTableRefernces[] getJoinOrderMappingWithTableRefernces(OrderMappingWithTableRefernces[] source1, OrderMappingWithTableRefernces[] source2, boolean isUnderLoj) throws DException {
351       ArrayList array = new ArrayList();
352       if (isUnderLoj) {
353          if (source1 == null || source2 == null) {
354             return null;
355          }
356
357          for (int i = 0; i < source1.length; i++) {
358             _SingleTableOrderPlan[] source1Stops = source1[i].getStops();
359             array.addAll(Arrays.asList(source1Stops));
360          }
361          for (int j = 0; j < source2.length; j++) {
362             _SingleTableOrderPlan[] source2Stops = source2[j].getStops();
363             array.addAll(Arrays.asList(source2Stops));
364          }
365          _SingleTableOrderPlan[] resultStops = (_SingleTableOrderPlan[]) array.toArray(new _SingleTableOrderPlan[array.size()]);
366          int resultMatch = resultantMatchOfInnerJoin(source1, source2);
367          return new OrderMappingWithTableRefernces[] {new OrderMappingWithTableRefernces(resultStops, resultMatch, getResultantTableDetails(source1, source2))};
368       } else {
369          if (source1 == null) {
370             return source2;
371          }
372          if (source2 == null) {
373             return source1;
374          }
375          OrderMappingWithTableRefernces buffered[] = new
376                OrderMappingWithTableRefernces[source1.length + source2.length];
377          System.arraycopy(source1, 0, buffered, 0, source1.length);
378          System.arraycopy(source2, 0, buffered, source1.length, source2.length);
379          return buffered;
380       }
381    }
382
383    /**
384     * This method returns the resultant match of the two matchs passed to it.
385     * The lowest match is the resultant match. The following match are in
386     * ascending order NoMatch, PartialMatch, FullMatch.
387     * @param source1
388     * @param source2
389     * @return
390     * @throws DException
391     */

392    private int resultantMatchOfInnerJoin(OrderMappingWithTableRefernces[]
393          source1,
394          OrderMappingWithTableRefernces[]
395          source2) throws
396    DException {
397       int resultMatch = TableExpressionConstants.FULLYMATCHED;
398       int match;
399       for (int i = 0; i < source1.length; i++) {
400          match = source1[i].getMatch();
401          if (match < resultMatch) {
402             resultMatch = match;
403          }
404       }
405       for (int j = 0; j < source2.length; j++) {
406          match = source2[j].getMatch();
407          if (match < resultMatch) {
408             resultMatch = match;
409          }
410       }
411       return resultMatch;
412
413    }
414
415    /**
416     * This method is required to get all tabledetails involved in both passed
417     * mapping.
418     * @param mapping1
419     * @param mapping2
420     * @return
421     * @throws DException
422     */

423    private TableDetails[] getResultantTableDetails(
424          OrderMappingWithTableRefernces[] mapping1,
425          OrderMappingWithTableRefernces[] mapping2) throws DException {
426       ArrayList list = new ArrayList();
427       for (int i = 0; i < mapping1.length; i++) {
428          list.addAll(Arrays.asList(mapping1[i].getTableDetails()));
429       }
430       for (int i = 0; i < mapping2.length; i++) {
431          list.addAll(Arrays.asList(mapping2[i].getTableDetails()));
432       }
433       return (TableDetails[]) list.toArray(new TableDetails[list.size()]);
434    }
435
436    /**
437     * This method is used to concat both passed arrays into one.
438     * @param source1
439     * @param source2
440     * @return
441     * @throws DException
442     */

443
444    private OrderMappingWithTableRefernces[] getJoinOrderMappingWithTableRefernces(OrderMappingWithTableRefernces[] source1, OrderMappingWithTableRefernces[] source2) throws DException {
445       if (source1 == null) {
446          return source2;
447       }
448       if (source2 == null) {
449          return source1;
450       }
451       OrderMappingWithTableRefernces buffered[] = new OrderMappingWithTableRefernces[source1.length + source2.length];
452       System.arraycopy(source1, 0, buffered, 0, source1.length);
453       System.arraycopy(source2, 0, buffered, source1.length, source2.length);
454       return buffered;
455    }
456
457    /**
458     * returns true if the query does not have where, orderby, groupby, having
459     * and union clause otherwise returns false. Its invoked only from
460     * SelectColumnIterator.
461     * @return
462     * @throws DException
463     */

464    public boolean isSimpleQuery(_ServerSession serverSession) throws DException {
465       return false;
466    }
467
468    /**
469     * The following methods are never called. They are just place holders.
470     * @param object
471     * @return
472     * @throws com.daffodilwoods.database.resource.DException
473     */

474
475    public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.DException {
476       throw new DException("DSE16", new Object JavaDoc[] {"run"});
477    }
478
479    public _Reference[] checkSemantic(_ServerSession session,_OrderByClause orderClause,boolean checkUserRight,boolean checkSetOperatorPresent ) throws DException {
480       throw new DException("DSE565",new Object JavaDoc[]{"checkSemantic()"});
481    }
482
483    public _ColumnCharacteristics getColumnCharacteristics(Object JavaDoc parm1) throws com.daffodilwoods.database.resource.DException {
484       throw new java.lang.UnsupportedOperationException JavaDoc("Method getColumnCharacteristics() not yet implemented.");
485    }
486
487    public QueryProperty getStrings() throws com.daffodilwoods.database.resource.DException {
488       /**@todo: Implement this com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.queryexpressionbody method*/
489       throw new java.lang.UnsupportedOperationException JavaDoc("Method getStrings() not yet implemented.");
490    }
491
492    public _TablePlan getExecutionPlan(_ServerSession session, booleanvalueexpression bve, _DatedFramework datedCondition, _Order order, ColumnDetails[] cdsWithActualTableDetails, ConditionArray conditionArray) throws DException {
493       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.queryexpressionbody method*/
494       throw new java.lang.UnsupportedOperationException JavaDoc("Method getExecutionPlan() not yet implemented.");
495    }
496
497    public void setFKeyColumnDetails(ColumnDetails[] parm1) throws com.daffodilwoods.database.resource.DException {
498       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.queryexpressionbody method*/
499       throw new java.lang.UnsupportedOperationException JavaDoc("Method setFKeyColumnDetails() not yet implemented.");
500    }
501
502    public ColumnDetails[] getSelectedColumns() throws com.daffodilwoods.database.resource.DException {
503       /**@todo Implement this com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.queryexpressionbody method*/
504       throw new java.lang.UnsupportedOperationException JavaDoc("Method getSelectedColumns() not yet implemented.");
505    }
506
507    public _Reference[] getReferences(TableDetails[] tableDetails) throws DException {
508       throw new java.lang.UnsupportedOperationException JavaDoc("Method getReferences( TableDetails[] ) not yet implemented.");
509    }
510
511    public boolean isViewOptimizationPossible() throws DException {
512       throw new UnsupportedOperationException JavaDoc("Method isViewOptimizationPossible is not supported.");
513    }
514
515    public _TablePlan[] getTablePlans(_ServerSession session, booleanvalueexpression condition, _Order order, ColumnDetails[] columnDetails, ConditionArray conditionArray) throws DException {
516       throw new UnsupportedOperationException JavaDoc("Method getTablePlans() not supported");
517    }
518
519   public boolean hasConstantSelectedColumn(booleanvalueexpression bve) throws DException {
520     throw new UnsupportedOperationException JavaDoc("Method hasConstantSelectedColumn() not supported");
521   }
522 }
523
Popular Tags