KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > queryexpression > queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm


1 package com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression;
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.dql.execution.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*;
11 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.set.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.set.SetOperatorAbstract.*;
13 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.queryspecification.*;
14 import com.daffodilwoods.daffodildb.server.sql99.dql.semanticchecker.*;
15 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
16 import com.daffodilwoods.daffodildb.server.sql99.token.*;
17 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
18 import com.daffodilwoods.database.resource.*;
19 import com.daffodilwoods.database.sqlinitiator.*;
20 import com.daffodilwoods.database.utility.P;
21
22 /**
23  * <p>Title: Union/Except </p>
24  * <p>Description:
25  * This class represents Union/Except of queries. It provides functionality to
26  * verify whether this set operator is semantically correct or not as well as
27  * functionality to get optimal plan. This plan helps in obtaining the resultset
28  * to retrieve the result from this set operator. It has two options -
29  * <ol>
30  * <li>All</li>
31  * <li>Distinct</li>
32  * </ol>
33  * If Union/Except is mentioned then distinct is implicit.
34  * i) Let R be a row that is a duplicate of some row in T1 or of some row in T2 or both. Let
35  * m be the number of duplicates of R in T1 and let n be the number of duplicates of R in
36  * T2 where m >=0 and n >= 0
37  * ii) If DISTINCT is specified or implicit, then
38  * Case:
39  * 1) If UNION is specified, then
40  * Case:
41  * A) If m > 0 or n > 0, then T contains exactly one duplicate of R.
42  * B) Otherwise, T contains no duplicate of R.
43  * 2) If EXCEPT is specified, then
44  * Case:
45  * A) If m > 0 and n = 0, then T contains exactly one duplicate of R.
46  * B) Otherwise, T contains no duplicate of R.
47  * iii) If ALL is specified, then
48  * Case:
49  * 1) If UNION is specified, then the number of duplicates of R that T contains is (m+ n).
50  * 2) If EXCEPT is specified, then the number of duplicates of R that T contains is the
51  * maximum of (m - n ) and 0(Zero)
52  * </p>
53  * <p>Copyright: Copyright (c) 2003</p>
54  * <p>Company: Daffodil S/W Ltd</p>
55  * @author SelectTeam
56  * @version 1.0
57  */

58
59 public class queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm extends SetOperatorAbstract implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter, non_joinqueryexpression, SetOperatorConstants, queryexpressionbody {
60
61    /**
62     * Represents the right query involved in set operator
63     */

64
65    public queryterm _queryterm0;
66    public dummyrule _Optdummyrule1;
67
68    /**
69     * Represents the option of set operator. It can be DISTINCT or ALL. It is
70     * optional. if it is not present, DISTINCT is implicit.
71     */

72
73    public setquantifier _Optsetquantifier2;
74
75    /**
76     * Represents the type of set operator. It can be Union or Except.
77     */

78
79    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439223;
80
81    /**
82     * Represents the left query involved in set operator
83     */

84
85    public queryexpressionbody _queryexpressionbody4;
86
87    public String JavaDoc toString() {
88       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
89       sb.append(" ");
90       sb.append(_queryexpressionbody4);
91       sb.append(" ");
92       sb.append(_SRESERVEDWORD12065439223);
93       sb.append(" ");
94       if (_Optsetquantifier2 != null) {
95          sb.append(_Optsetquantifier2);
96       }
97       sb.append(" ");
98       if (_Optdummyrule1 != null) {
99          sb.append(_Optdummyrule1);
100       }
101       sb.append(" ");
102       sb.append(_queryterm0);
103       return sb.toString();
104    }
105
106    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
107       queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm tempClass = new queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm();
108       tempClass._queryterm0 = (queryterm) _queryterm0.clone();
109       if (_Optdummyrule1 != null) {
110          tempClass._Optdummyrule1 = (dummyrule) _Optdummyrule1.clone();
111       }
112       if (_Optsetquantifier2 != null) {
113          tempClass._Optsetquantifier2 = (setquantifier) _Optsetquantifier2.clone();
114       }
115       tempClass._SRESERVEDWORD12065439223 = (SRESERVEDWORD1206543922) _SRESERVEDWORD12065439223.clone();
116       tempClass._queryexpressionbody4 = (queryexpressionbody) _queryexpressionbody4.clone();
117       return tempClass;
118    }
119
120    /**
121     * Returns clone of condition passed.
122     * This method is required when condition is given on a view in which set
123     * operator is present. And that condition is solvable by view. So we've to
124     * shift this condition to underlying queries of set operator so as to give
125     * result optimally. Now columns in the condition belongs to only query of
126     * left most side. So when we've to shift condition to query of right side,
127     * we've to change columns of condition according to columns of right side
128     * query.
129     * @param bve
130     * @return
131     * @throws DException
132     */

133
134    private booleanvalueexpression getClonedBVE(booleanvalueexpression bve) throws DException {
135       booleanvalueexpression clonedBVE = null;
136       try {
137          if (bve != null) {
138             clonedBVE = (booleanvalueexpression) bve.clone();
139             changeColumnDetails(clonedBVE.getColumnDetails(), _queryexpressionbody4.getColumnDetails(), _queryterm0.getColumnDetails());
140          }
141       } catch (CloneNotSupportedException JavaDoc ex) {
142          throw new DException("DSE3524", null);
143       }
144       return clonedBVE;
145    }
146
147    /**
148     * Returns Quantifier for Set Operator whether it is all or distinct
149     * @return
150     * @throws DException
151     */

152
153    private int isDistinctOrAll() throws DException {
154       int distinctOrAll = (_Optsetquantifier2 == null) ? DISTINCT
155           : ( (String JavaDoc) _Optsetquantifier2.run(null)).equalsIgnoreCase("all") ? ALL : DISTINCT;
156       return distinctOrAll;
157    }
158
159    /**
160     * Returns Type of Set Operator whether it is Union or Except
161     * @return
162     * @throws DException
163     */

164    private int isUnionOrExcept() throws DException {
165       String JavaDoc unionOrExcept = (String JavaDoc) _SRESERVEDWORD12065439223.run(null);
166       int except = (unionOrExcept.equalsIgnoreCase("union")) ? UNION : EXCEPT;
167       return except;
168    }
169
170    /**
171     * Returns Execution Plan for Set Operator.
172     * Various steps are performed as under:
173     * 1) Passed Condition is cloned to pass the condition to all the query
174     * involved in the set Operator.
175     * 2) DatedFramework is cloned to pass the dated condition to all the query
176     * involved in the set Operator.
177     * 3) Order has to be shifted to both the queries involved, for this we check
178     * whether order is already adjusted or not.
179     * for example
180     * a union b union c
181     * Parse Tree would be
182     * U
183     * U c
184     * a b
185     * for query 'a' we make order according to selected columns of query
186     * 'a' if Order passed is null,
187     * otherwise Order is adjusted according to columns of order passed.
188     * according to query 'a' adjusted order, we make order to be passed to
189     * query 'b'
190     *
191     * for query 'c' order will be same as selected columns are there at
192     * top level. so for 'c' order is alreday adjusted and we do not have
193     * to make a new order.
194     * 4) In the process discussed in step 3 we also make appropriate comparator
195     * of the order.
196     *
197     * Resultant SetOperator plan contains the clone of selected columns in
198     * natural order (in the same sequence as they are specified in select list)
199     * as well as selected columns which are adjusted according to order. They
200     * both are required. Selected columns with natural order is required to
201     * retrieve the values and adjusted selected columns are required to
202     * compare the result of underlying query.
203     * Also original selected columns are passed to plan. These are required to
204     * check whether conversion of objects are required in set operators or not.
205     * Set opearator result is formed from the result of both queries, so the
206     * objects of both query must be of same datatype and size. So cloned columns
207     * contain maximum datatype and original columns contain actual data types.
208     * @param session
209     * @param bve
210     * @param datedFrameWork
211     * @param order
212     * @param cdsWithActualTableDetails
213     * @param conditionTableDetails
214     * @return
215     * @throws DException
216     * @throws DException
217     */

218
219    public _TablePlan getExecutionPlan(_ServerSession session, booleanvalueexpression bve, _DatedFramework datedFrameWork, _Order order, ColumnDetails[] cdsWithActualTableDetails, ConditionArray conditionArray) throws DException, DException {
220       booleanvalueexpression clonedBVE = getClonedBVE(bve);
221       if (clonedBVE != null) {
222          cdsWithActualTableDetails = GeneralPurposeStaticClass.getCombinedCDArray(cdsWithActualTableDetails, clonedBVE.getColumnDetails());
223       }
224       ConditionArray clonedConditionArray = getClonedConditionArray(conditionArray, clonedBVE);
225       _DatedFramework clonedDatedFrameWork = getClonedDatedFrameWork(datedFrameWork);
226       int distinctOrAll = isDistinctOrAll();
227       int exceptOrUnion = isUnionOrExcept();
228       ComparatorAndOrder comparatorAndOrder = getAdjustedSelectOrder(order, exceptOrUnion, distinctOrAll, session);
229       _Order[] ordersForBothQueries = (comparatorAndOrder == null) ? new _Order[] {null, null}
230           : comparatorAndOrder.order;
231
232 /* Done by Kaushik */
233
234 /**Next 4 lines by Sandeep to solve problem in union/insertse4ct queries involving nested views */
235       _TablePlan tPlan1 = _queryexpressionbody4.getExecutionPlan(session, bve, datedFrameWork, ordersForBothQueries[0], GeneralPurposeStaticClass.getCombinedCDArray(cdsWithActualTableDetails, GeneralPurposeStaticClass.getCombinedCDArray(clonedLeftColumnDetails,clonedLeftOrderColumnDetails)), conditionArray);
236       _TablePlan tPlan2 = _queryterm0.getExecutionPlan(session, clonedBVE, clonedDatedFrameWork, ordersForBothQueries[1], GeneralPurposeStaticClass.getCombinedCDArray(cdsWithActualTableDetails,GeneralPurposeStaticClass.getCombinedCDArray( clonedRightColumnDetails,clonedRightOrderColumnDetails)) , clonedConditionArray);
237
238
239
240       SetOperatorPlan sop = new SetOperatorPlan(tPlan1, tPlan2, distinctOrAll, exceptOrUnion, getReferences(getAllTableDetails()), comparatorAndOrder == null ? null : comparatorAndOrder.comparator, clonedLeftColumnDetails, clonedRightColumnDetails, appropriateDataTypes, appropriateSizes, ordersForBothQueries[0] == null ? clonedLeftColumnDetails : ordersForBothQueries[0].getKeyColumnDetails(), ordersForBothQueries[1] == null ? clonedRightColumnDetails : ordersForBothQueries[1].getKeyColumnDetails());
241       sop.setOriginalColumnDetails(_queryexpressionbody4.getSelectedColumns(), _queryterm0.getSelectedColumns(), ordersForBothQueries[0], ordersForBothQueries[1]);
242
243       return sop;
244    }
245
246    /**
247     * The clone is needed for right child as the passed parameter belong to
248     * left child
249     * @param conditionArray
250     * @param clonedBVE
251     * @return
252     * @throws DException
253     */

254
255    private _DatedFramework getClonedDatedFrameWork(_DatedFramework datedFrameWork) throws DException {
256       try {
257          if (datedFrameWork != null) {
258             return (_DatedFramework) datedFrameWork.clone();
259          }
260       } catch (CloneNotSupportedException JavaDoc ex) {
261          throw new DException("DSE3524", null);
262       }
263       return datedFrameWork;
264    }
265
266    /**
267     * The clone is needed for right child as the passed parameter belong to
268     * left child
269     * @param conditionArray
270     * @param clonedBVE
271     * @return
272     * @throws DException
273     */

274
275    private ConditionArray getClonedConditionArray(ConditionArray conditionArray, booleanvalueexpression clonedBVE) throws DException {
276       ConditionArray clonedArray = new ConditionArray();
277       if (clonedBVE != null) {
278          clonedArray.addCondition(clonedBVE);
279       }
280       return clonedArray;
281    }
282
283    /**
284     * It is required to obtain the order and comparator for this set operator.
285     * No implicit order is required for Union All. And for rest of options
286     * order is formed on the basis of passed order and clone of columns of
287     * both queries. Clone of column is required because it may possible that
288     * selected columns may belong to view, order formed may be shifted under
289     * view. And also we need a distinction between columns above the view and
290     * columns below the view. Same columns can't remain to both sides. So clone
291     * is needed.
292     * @param order
293     * @param exceptOrUnion
294     * @param distinctOrAll
295     * @param serverSession
296     * @return
297     * @throws DException
298     */

299    private ComparatorAndOrder getAdjustedSelectOrder(_Order order, int exceptOrUnion, int distinctOrAll, _ServerSession serverSession) throws DException {
300    /* if (exceptOrUnion == UNION && distinctOrAll == ALL && order == null) {
301          return null;
302       }
303
304       else if (exceptOrUnion == UNION && distinctOrAll == ALL && order != null){
305         return getOrderWhenUnionAllwithOrderby(order, SemanticChecker.getClonedColumns(_queryexpressionbody4.getSelectedColumns()), SemanticChecker.getClonedColumns( _queryterm0.getSelectedColumns()), serverSession);//Done by Sandeep to solve problem in query involving VIEWs :See also :ViewAbstract on 29 -June 2005
306       }
307
308
309       return getAdjustedSelectOrder(order, SemanticChecker.getClonedColumns(_queryexpressionbody4.getSelectedColumns()), SemanticChecker.getClonedColumns( _queryterm0.getSelectedColumns()),serverSession);
310 */

311    /*Method is rewritten by Sandeep to solve problem in Union Distinct involving Views :See also View Abstract */
312    if (exceptOrUnion == UNION && distinctOrAll == ALL && order == null) {
313      return null;
314    }
315    /**clonedLeftOrderColumnDetails & clonedRightOrderColumnDetails are initialized here */
316    clonedLeftOrderColumnDetails=SemanticChecker.getClonedColumns(_queryexpressionbody4.getSelectedColumns());
317    clonedRightOrderColumnDetails= SemanticChecker.getClonedColumns( _queryterm0.getSelectedColumns());
318    return exceptOrUnion == UNION && distinctOrAll == ALL && order != null ? getOrderWhenUnionAllwithOrderby(order,clonedLeftOrderColumnDetails, clonedRightOrderColumnDetails, serverSession): getAdjustedSelectOrder(order, clonedLeftOrderColumnDetails, clonedRightOrderColumnDetails,serverSession);
319    }
320
321    /**
322     * Initiates Semantic Checking Of Set Operators Union or Except.
323     *
324     * In case of Union All Blob/CLOB columns are allowed in select List of query
325     * thats why exception 3570 is catched and Process is continued, otherwise
326     * exception is thrown.
327     *
328     * Order Columns checking is also done at this level. It checks that whether
329     * the order by columns are valid ie do they appear in the select list and
330     * that they belong to the fist query.
331     *
332     * It also checks Cardinality and DataTypes of the left and right queries.
333     *
334     * It initializes the maximum datatypes and sizes among the corresponding
335     * columns of both side. It is required because Set opearator result is
336     * formed from the result of both queries, so the objects of both query must
337     * be of same datatype and size.In case of Union call of initializing data
338     * type and size is also deligated to underlying queries. it is done because
339     * underlying query can also be a union query. In case of except
340     * it is not needed because except always gives the result from first query,
341     * so no need of conversion.
342     *
343     * @param session
344     * @param orderClause0
345     * @return the joint references of the left and right queries
346     * @throws DException
347     */

348    public _Reference[] checkSemantic(_ServerSession session, _OrderByClause orderClause0, boolean checkUserRight,boolean checkSetOperatorPresent) throws DException {
349       _Reference[] leftRef = _queryexpressionbody4.checkSemantic(session, null, checkUserRight,true);
350       _Reference[] rightRef = _queryterm0.checkSemantic(session, null, checkUserRight,true); // Order is Not Valid In Case of 2nd Query
351
_Reference[] references = GeneralPurposeStaticClass.getJointReferences(leftRef, rightRef);
352       ColumnDetails[] leftColDetails = _queryexpressionbody4.getSelectedColumns();
353       ColumnDetails[] rightColDetails = _queryterm0.getSelectedColumns();
354
355       try{
356          SemanticChecker.checkCardinalityAndDataTypeComptability(leftColDetails, rightColDetails);
357       } catch (DException ex) {
358          if (ex.getDseCode().equalsIgnoreCase("DSE6001")) { // done by kaushik corresponding to work in Check.
359
if (! (_Optsetquantifier2 != null && ( (String JavaDoc) _SRESERVEDWORD12065439223.run(null)).equalsIgnoreCase("Union") && ( (String JavaDoc) _Optsetquantifier2.run(null)).equalsIgnoreCase("ALL"))) {
360                throw ex;
361             }
362          } else {
363             throw ex;
364          }
365       }
366       /**
367      * Due to Exception in checkCardinalityAndDatatypeCompatibility when one side column is null ,
368      * Following two lines are shifted here. -- Manoj Kr. Sheoran
369      */

370     clonedLeftColumnDetails = SemanticChecker.getClonedColumns(leftColDetails);
371     clonedRightColumnDetails = SemanticChecker.getClonedColumns(rightColDetails);
372       if (references==null){
373          if ( ( (String JavaDoc) _SRESERVEDWORD12065439223.run(null)).equalsIgnoreCase("union")) {
374             initializeAppropriateDataTypeAndSizeForUnion();
375             deligateCall(appropriateDataTypes, appropriateSizes);
376          } else {
377             initializeAppropriateDataTypeAndSize();
378          }
379          if (orderClause0 != null) {
380             ColumnDetails[] orderKeyColDetails = orderClause0.getKeyColumnDetails();
381             SemanticChecker.checkingForOrder(orderKeyColDetails);
382             SemanticChecker.checkSemanticCheckingForOrderInSetOperators(leftColDetails, orderClause0.getKeyColumnDetails());
383          }
384       }
385       return references;
386    }
387
388    public void setDataTypesForSetOperatorQuery(_OrderByClause orderClause0) throws DException {
389       if ( ( (String JavaDoc) _SRESERVEDWORD12065439223.run(null)).equalsIgnoreCase("union")) {
390          initializeAppropriateDataTypeAndSizeForUnion();
391          deligateCall(appropriateDataTypes, appropriateSizes);
392       } else {
393          initializeAppropriateDataTypeAndSize();
394       }
395       if (orderClause0 != null) {
396          ColumnDetails[] orderKeyColDetails = orderClause0.getKeyColumnDetails();
397          SemanticChecker.checkingForOrder(orderKeyColDetails);
398          SemanticChecker.checkSemanticCheckingForOrderInSetOperators(_queryexpressionbody4.getSelectedColumns(), orderClause0.getKeyColumnDetails());
399       }
400    }
401
402    /**
403     * It is requied in case of Union. It delegates call of initializing data
404     * type and size to underlying queries. it is done because underlying query
405     * can also be a union query.
406     * @param appropriateDataTypes0
407     * @param appropriateSizes0
408     * @returns true if union exists in hierarchy otherwise false.
409     * @throws DException
410     */

411
412    private boolean deligateCall(int[] appropriateDataTypes0, int[] appropriateSizes0) throws DException {
413       boolean result = false;
414       if (_queryexpressionbody4 instanceof queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm) {
415          result = ( (queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm) _queryexpressionbody4).initializeDataTypeAndSize(appropriateDataTypes0, appropriateSizes0);
416       } else if (_queryexpressionbody4 instanceof queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryprimary) {
417          result = ( (queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryprimary) _queryexpressionbody4).initializeDataTypeAndSize(appropriateDataTypes0, appropriateSizes0);
418       }
419       if (_queryterm0 instanceof queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm) {
420          ( (queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm) _queryterm0).initializeDataTypeAndSize(appropriateDataTypes0, appropriateSizes0);
421       } else if (_queryterm0 instanceof queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryprimary) {
422          ( (queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryprimary) _queryterm0).initializeDataTypeAndSize(appropriateDataTypes0, appropriateSizes0);
423       }
424       return result;
425    }
426
427    /**
428     * This method initializes the DataType and Sizes according to thoes that
429     * are passed to it. If union is present in hierarchy of setoperators, then
430     * these maximum datatypes and sizes are also set into column details.
431     * select a_int from A
432     * union
433     * select a_float from B
434     * union
435     * select a_small_int from C
436     * @param appropriateDataTypes0
437     * @param appropriateSizes0
438     * @return
439     * @throws DException
440     */

441    public boolean initializeDataTypeAndSize(int[] appropriateDataTypes0, int[] appropriateSizes0) throws DException {
442       appropriateDataTypes = appropriateDataTypes0;
443       appropriateSizes = appropriateSizes0;
444       boolean b = deligateCall(appropriateDataTypes0, appropriateSizes0);
445       boolean union = ( (String JavaDoc) _SRESERVEDWORD12065439223.run(null)).equalsIgnoreCase("union");
446       if (union || b) {
447          for (int i = 0; i < clonedLeftColumnDetails.length; i++) {
448             clonedLeftColumnDetails[i].setDatatype(appropriateDataTypes0[i]);
449             clonedRightColumnDetails[i].setDatatype(appropriateDataTypes0[i]);
450             clonedLeftColumnDetails[i].setSize(appropriateSizes0[i]);
451             clonedRightColumnDetails[i].setSize(appropriateSizes0[i]);
452          }
453       }
454       return union || b;
455    }
456
457    /**
458     * This method is called by the checkSemantic method in case of UNION
459     * This method initializes appropriate Datatypes and their sizes
460     * e.g.
461     * select a_int from A
462     * union
463     * select a_float from B
464     * in the above query the column of the result will be
465     * a_int it's datatype must be the larger of the two i.e. Float
466     * Also in the case of char or varchar the the two size may be different
467     * in such cases the bigger size is to be kept
468     * @throws DException
469     */

470
471    private void initializeAppropriateDataTypeAndSizeForUnion() throws DException{
472    int length = clonedLeftColumnDetails.length;
473    appropriateDataTypes = new int[length];
474    appropriateSizes = new int[length];
475    for (int i = 0; i < length; i++) {
476       int dt1 = clonedLeftColumnDetails[i].getDatatype();
477       int dt2 = clonedRightColumnDetails[i].getDatatype();
478
479       appropriateDataTypes[i] = dt1 > dt2 ? dt1 : dt2;
480       clonedLeftColumnDetails[i].setDataTypeForSetOperators(appropriateDataTypes[i]);
481       clonedRightColumnDetails[i].setDataTypeForSetOperators(appropriateDataTypes[i]);
482       int sz1 = clonedLeftColumnDetails[i].getSize();
483       int sz2 = clonedRightColumnDetails[i].getSize();
484       appropriateSizes[i] = sz1 > sz2 ? sz1 : sz2;
485       clonedLeftColumnDetails[i].setSizeForSetOperators(appropriateSizes[i]);
486       clonedRightColumnDetails[i].setSizeForSetOperators(appropriateSizes[i]);
487       if(clonedRightColumnDetails[i].getType() != TypeConstants.REFERENCE)
488          clonedLeftColumnDetails[i].setByteComparison(false);
489    }
490 }
491
492
493    /**
494     * It allows user to obtain order and its comparator for both queries when
495     * no order is present on this set operator. But to solve this set operator,
496     * selected columns needs to be in order. Order contains the selected column
497     * in the same sequence as they are present in select list.
498     * @param columnDetailsLeft - columns of left query.
499     * @param columnDetailsRight - columns of right query.
500     * @param serverSession
501     * @param columnOrder - represents the orderspecification of order.
502     * @return order and its comparator.
503     * @throws DException
504     */

505
506    public ComparatorAndOrder getComparatorAndOrderForNormalOrder(ColumnDetails[] columnDetailsLeft, ColumnDetails[] columnDetailsRight, _ServerSession serverSession, boolean[] columnOrder) throws DException {
507       _Order leftQueryOrder = new SelectOrder(columnDetailsLeft, columnOrder);
508       _Order rightQueryOrder = new SelectOrder(columnDetailsRight, columnOrder);
509       return new ComparatorAndOrder(getComparableComparator(_queryexpressionbody4.getSelectedColumns(), _queryterm0.getSelectedColumns(), columnOrder, serverSession), new _Order[] {leftQueryOrder, rightQueryOrder});
510    }
511
512    /**
513     * It allows user to obtain order and its comparator for both queries when
514     * order is present on this set operator. And the order is arranged so as to
515     * get all the selected columns to be involved in this order. It is required
516     * to obtain their result optimally.
517     * @param columnDetailsLeft - columns of left query.
518     * @param columnDetailsRight - columns of right query.
519     * @param serverSession
520     * @param columnOrder - represents the orderspecification of order.
521     * @return order and its comparator.
522     * @throws DException
523     */

524    public ComparatorAndOrder getComparatorAndOrderForRearrangedOrder(ColumnDetails[] columnDetailsLeft, ColumnDetails[] columnDetailsRight, _ServerSession serverSession, boolean[] orderSpecifications) throws DException {
525       _Order derivedOrderLeft = new SelectOrder(columnDetailsLeft, orderSpecifications, true);
526       _Order derivedOrderRight = new SelectOrder(columnDetailsRight, orderSpecifications, true);
527       return new ComparatorAndOrder(getComparableComparator(adjust(_queryexpressionbody4.getSelectedColumns(), columnDetailsLeft), adjust(_queryterm0.getSelectedColumns(), columnDetailsRight), orderSpecifications, serverSession), new _Order[] {derivedOrderLeft, derivedOrderRight});
528       }
529
530    /**
531     * It allows user to obtain order and its comparator for both queries when
532     * order is present on this set operator. And the order is already adjusted
533     * according to the selected columns. It is the case when setoperator is left
534     * child of set operator. In that case topmost setoperator perform the
535     * adjustment of selected column, so in this case only the selected column of
536     * right child of underlying set operator needs to be adjusted acc. to order.
537     * @param columnDetailsLeft - columns of left query.
538     * @param columnDetailsRight - columns of right query.
539     * @param serverSession
540     * @param columnOrder - represents the orderspecification of order.
541     * @return order and its comparator.
542     * @throws DException
543     */

544    public ComparatorAndOrder getComparatorAndOrderForAlreadyAdjustedOrder(_Order order, ColumnDetails[] columnDetailsLeft, ColumnDetails[] columnDetailsRight, _ServerSession serverSession, boolean[] orderSpecifications) throws DException {
545       _Order derivedOrderRight = new SelectOrder(columnDetailsRight, orderSpecifications, true);
546 /** 03-06-2004 changes being done to solve a problem related to union query involving views */
547 _Order derivedOrderLeft = new SelectOrder(columnDetailsLeft,orderSpecifications,true);
548
549
550
551       ColumnDetails[] cd = adjust(_queryterm0.getSelectedColumns(),columnDetailsRight);
552 ColumnDetails[] cdLeft = adjust(_queryexpressionbody4.getSelectedColumns(),columnDetailsLeft);
553 /** commented on 03-06-2004 */
554
555       return new ComparatorAndOrder(getComparableComparator(cdLeft, cd, orderSpecifications, serverSession), new _Order[] {derivedOrderLeft, derivedOrderRight});
556    }
557
558    /**
559     * It allows user to get the target columns in the same sequence as that of
560     * source column.
561     * @param source
562     * @param target
563     * @return
564     * @throws DException
565     */

566
567    private ColumnDetails[] adjust(ColumnDetails[] source, ColumnDetails[] target) throws DException {
568      int length = target.length;
569       ColumnDetails[] result = new ColumnDetails[length];
570       for (int i = 0; i < length; i++) {
571         for (int j = 0; j < source.length; j++) {
572   if (source[j].getTable() == target[i].getTable() && source[j].getAppropriateColumn().equalsIgnoreCase(target[i].getAppropriateColumn()) && result[i] == null) {
573             result[i] = source[j];
574             break;
575           }
576         }
577       }
578       return result;
579    }
580
581
582
583
584 /**
585    private ColumnDetails[] adjust(ColumnDetails[] source, ColumnDetails[] target) throws DException {
586       int length = source.length;
587       ColumnDetails[] result = new ColumnDetails[length];
588       for (int i = 0; i < length; i++) {
589        String column = source[i].getAppropriateColumn().trim();
590         for (int j = 0; j < length; j++) {
591          ;//// Removed By Program ** System.out.println("Target["+j+"] = ["+target[j].getColumn().trim()+"]");
592           /** Done by Sandeep to solve bug 12288 **/

593    /** if (source[i].getTable() == target[j].getTable() && column.equalsIgnoreCase(target[j].getAppropriateColumn().trim()) && result[j] == null) {
594             result[j] = source[i];
595             break;
596           }
597         }
598       }
599       return result;
600    }
601 **/

602    /**
603    * Cloned Columns of left most query are returned.
604    * @return Columns of LeftMost Query
605    * @throws DException
606    */

607
608
609   public ColumnDetails[] getSelectedColumns() throws DException {
610      return clonedLeftColumnDetails;
611   }
612
613   /**
614     * For documentation of following methods refer to documentation of
615     * queryexpressionbody.
616     * @return
617     * @throws DException
618     */

619
620    public ColumnDetails[] getColumnDetails() throws DException {
621       ColumnDetails columnDetails1[] = _queryexpressionbody4.getColumnDetails();
622       ColumnDetails columnDetails2[] = _queryterm0.getColumnDetails();
623       return GeneralPurposeStaticClass.getCombinedCDArray(columnDetails1, columnDetails2);
624    }
625
626    public Object JavaDoc[] getParameters(Object JavaDoc object) throws DException {
627       Object JavaDoc[] first = _queryexpressionbody4.getParameters(object);
628       Object JavaDoc[] second = _queryterm0.getParameters(object);
629       return GeneralPurposeStaticClass.getCombinedArray(first, second);
630    }
631
632    public void getColumnsIncluded(ArrayList aList) throws DException {
633       _queryexpressionbody4.getColumnsIncluded(aList);
634       _queryterm0.getColumnsIncluded(aList);
635    }
636
637    public void getTablesIncluded(ArrayList aList) throws DException {
638       _queryexpressionbody4.getTablesIncluded(aList);
639       _queryterm0.getTablesIncluded(aList);
640    }
641
642    public ParameterInfo[] getParameterInfo() throws DException {
643       ParameterInfo[] info1 = _queryexpressionbody4.getParameterInfo();
644       ParameterInfo[] info2 = _queryterm0.getParameterInfo();
645       return GeneralPurposeStaticClass.getCombinedParameterInfoArray(info1, info2);
646    }
647
648    public _Reference[] getReferences(TableDetails[] tableDetails) throws DException {
649       _Reference[] first = _queryexpressionbody4.getReferences(tableDetails);
650       _Reference[] second = _queryterm0.getReferences(tableDetails);
651       return GeneralPurposeStaticClass.getJointReferences(first, second);
652    }
653
654    /**
655     * All the following method delegates the call to first query. For
656     * documentation of these method refer to queryexpressionbody.
657     */

658
659    public void setDefaultValues(_VariableValueOperations variableValueOperation) throws DException {
660       _queryexpressionbody4.setDefaultValues(variableValueOperation);
661    }
662
663    public _ColumnCharacteristics getColumnCharacteristics(Object JavaDoc object) throws DException {
664       return _queryexpressionbody4.getColumnCharacteristics(object);
665    }
666
667    public void setFKeyColumnDetails(ColumnDetails[] fKeyColumns) throws DException {
668       _queryexpressionbody4.setFKeyColumnDetails(fKeyColumns);
669    }
670
671    public TableDetails[] getAllTableDetails() throws DException {
672       return _queryexpressionbody4.getAllTableDetails();
673    }
674
675   public boolean hasConstantSelectedColumn(booleanvalueexpression bve) throws DException{
676       return _queryexpressionbody4.hasConstantSelectedColumn(bve) || _queryterm0.hasConstantSelectedColumn(bve);
677   }
678 }
679
Popular Tags