KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.
2     fromclause;
3
4 import java.util.*;
5
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.plan.table.*;
15 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
16 import com.daffodilwoods.database.resource.*;
17 import com.daffodilwoods.database.sqlinitiator.*;
18 import com.daffodilwoods.database.utility.*;
19 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary.
20
    subquery;
21 import com.daffodilwoods.daffodildb.server.sql99.dml.*;
22 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession;
23
24 /**
25  *
26  * <p>Title: </p>
27  * <p>Description: Table referencelist is a list of table references separated
28  * by commas present in from clause. table reference can be -
29  * 1. Table name
30  * 2. View name
31  * 3. from subquery
32  * 4. Qualified join
33  * 5. Cross join
34  * 6. Natural join.
35  * <p>Copyright: Copyright (c) 2003</p>
36  * <p>Company: </p>
37  * @author unascribed
38  * @version 1.0
39  */

40
41 public class tablereferencelist
42     implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter {
43
44   /**
45        * The table reference list is an array of table references separated by commas
46    * This is an array of tablereferences
47    */

48
49   public tablereference[] _OptRepScomma94843605tablereference0;
50
51   /**
52    * Represents the tables present in this list.
53    */

54
55   private TableDetails[] tableDetails1;
56
57   /**
58    * It reperesnts list of all table detail involved in tablereferencelist.if
59    * table is of type view then list of underlying table detail are included.
60    */

61
62   private TableDetails[] allTableDetails;
63
64   /**
65    * This method is used to obtain the plan of all tables present in this
66    * list based on passed arguments. It firstly check whether single table
67    * order plans are present. If present, then it is checked whether it can
68    * be shifted to single table or not. If they can be shifted,then those table
69    * plans are placed first and in the same sequence because we've to maintain
70    * the order in resultant resultset.
71    *
72    * Detailed documentation is under given -
73    *
74    * <br>It gives the Plans of tableRefernces Present in this class.</br>
75    * <br>The Plan that can be returned from this method are</br>
76    * <br>SingleTablePlan , OrderSequencePlan , QualifiedPlans , ViewPlan</br>
77    * <br>for eg Select * from A, B will return</br>
78    * <br>SingleTablePlan[A] , SingleTablePlan[B]</br>
79    * <br>Select * from A LOJ B will return</br>
80        * <br>QualifiedLeftPlan[SingleTablePlan[A] , SingleTablePlan[B]]</br>
81    * <br>Select * from C , A LOJ B order by C , A , B</br>
82    * <br>OrderSequencePlan[ SingleTablePlan[C] , QualifiedLeftPlan[SingleTablePlan[A] , SingleTablePlan[B]]</br>
83    *
84    * <br>ALGO :</br>
85    * <br>It first Checks whether orderPlan contains the Order which can be
86    * shifted at singleTable Level</br>
87    * <br>If it contains the Order which can be shifted at SingleLevel</br>
88    * <br>Checks whether the OrderSequencePlan is possible ,and if possible it creates the OrderSequencePlan</br>
89    * <br>Creation Of OrderSequencePlan means we can shift Order to SingleTablePlan Level .</br>
90    * <br>The Following conditions need to be met for shifting the Order at the SingleTableLevel</br>
91    * <br>a) No Change in Sequence of TablePlans and Tables of Order</br>
92    * <br>For Eg</br>
93    * <br>A LOJ B LOJ C on A.id = 1 on B.Id =2 order by A.id , C.id2 , B.id</br>
94    * <br>The QualifiedLeftPlan will not be able to shift the order to SingleLevel as tables involved in
95    * qualified join is not in sequence with the order specified in the query.
96    *
97        * <br>b) No Gap in Plans table and OrderSequencePlan Table</br>
98    * <br>A LOJ B LOJ C on A.id = 1 on B.Id =2 order by A.id , C.id2</br>
99    * <br>The data will not be ordered as the Order of getting the values will be in Order A >> B >> C</br>
100    * <br>so Order will be destroyed.</br>
101    * <br>The above two responsibilities are given to individual tablerefs(Main Work is done by qualifiedJoin class)</br>
102    *
103    * <br>If the OrderPlan contains the SingleTableOrderPlan which can't be shifted to singlelevel because of the above</br>
104    * <br>problem then we rearrange the OrderPlan in which we moves the SingleTableOrder to JoinLevel and if any JoinLevel</br>
105        * <br>Order is present that will be shifted to GroupBy Level Order.</br>
106    * <br>We set a wrapper on all the Plans who are capable of Solving the Order at Single Level and this OrderSequencePlan is</br>
107    * <br>placed before all other Plans</br>
108    * <br>And then we update the TablePlan List</br>
109    * @param session
110    * @param datedCondition
111    * @param bvePlan
112    * @param orderPlan
113    * @param queryColumns
114    * @param conditionArray
115    * @return
116    * @throws DException
117    */

118   public _TablePlan[] getTablePlan(_ServerSession session,
119                                    _DatedFramework datedCondition,
120                                    _BVEPlan bvePlan, _OrderPlan orderPlan,
121                                    _QueryColumns queryColumns,
122                                    ConditionArray conditionArray) throws
123       DException {
124     if (isOrderSequencePlanPossible(orderPlan)) {
125       _TablePlan[] temp = getTablePlansAccordingToOrderSequencePlan(session,
126           datedCondition, bvePlan, orderPlan, queryColumns, conditionArray);
127       return temp;
128     }
129     return getTablePlansNotArrangedAccToOrderplan(session, datedCondition,
130                                                   bvePlan, null, queryColumns,
131                                                   conditionArray);
132   }
133
134   /**
135    * Checks whether OrderSequencePlan can be possible or not
136    * If OrderPlan is not null and it contains SingletableOrderPlans then
137    * OrderSequencePLan might be possible otherwise not possible.
138    * @param orderPlan final orderPlan of query
139    * @return
140    * @throws DException
141    */

142   private boolean isOrderSequencePlanPossible(_OrderPlan orderPlan) throws
143       DException {
144     return orderPlan != null && orderPlan.getSingleTableOrderPlans() != null;
145   }
146
147   /**
148    * Returns Plans arranged according to Order Sequence Plan
149    * Algo:
150    * 1) Clone of Condition Array is made, this is done as when we check the
151    * OrderSequencePlan possibility of all the tablereferences,
152    * then condition are added in the condition array to break up any
153    * qualifiedjoin if any condition lies on right table of qualified
154    * join, so clone is made as when we ask getTablePlans same process is
155    * to be repeated again to change qualified join to simple join..
156    * 2) Single Table OrderPlans of query are retrieved from OrderPlan.
157    * 3) Single Table OrderPlans are passed to table references to checkFor
158    * OrderSequencePlan possibility.
159    * If any exception is returned and is equal to DSE3899 then Order
160    * sequencePlan is not possible, then we rearrange the order in
161    * orderPlan and return the plan according to normal sequence of table references.
162    *
163    * 4) If no such exception appears then table plans are arranged according
164    * to Order SequencePlan and returned.
165    *
166    * @param session
167    * @param datedCondition
168    * @param bvePlan Condition plan, which is merged plan of view condition,
169    * where condition and having condition
170    * @param orderPlan Order Plan is merged plan of view order, order by order,
171    * group by order and distinct order if any.
172        * @param queryColumns Query columns are passed for effective cost computation
173    * @param conditionArray Condition Array is passed to change qualified join
174    * to simple join if possible.
175        * @return TablePlans array adjusted according to Order SequencePlan if possible,
176    * otherwise in the normal sequence of tablereferences.
177    * @throws DException
178    */

179   private _TablePlan[] getTablePlansAccordingToOrderSequencePlan(_ServerSession
180       session, _DatedFramework datedCondition, _BVEPlan bvePlan,
181       _OrderPlan orderPlan,
182       _QueryColumns queryColumns, ConditionArray conditionArray) throws
183       DException {
184     ConditionArray clonedConditionArray = getClonedConditionArray(
185         conditionArray);
186     OrderMappingWithTableRefernces[] orderPlansAccToTableReferences =
187         checkForOrderSequencePossibility(orderPlan, bvePlan, session,
188                                          queryColumns, clonedConditionArray);
189     if (orderPlansAccToTableReferences == null) {
190       reArrangeOrderPlansModified(orderPlan);
191       ArrayList allPlans = getAllTablePlans(session, datedCondition, bvePlan, null,
192                                             queryColumns, conditionArray);
193       return (_TablePlan[]) allPlans.toArray(new _TablePlan[allPlans.size()]);
194     }
195     return getTablePlansArrangedAccToOrderMappingWithTableReference(session,
196         datedCondition, bvePlan, orderPlan, queryColumns, conditionArray,
197         orderPlansAccToTableReferences);
198   }
199
200   /**
201    * Returns the clone of condition array passed.
202    * @param conditionArray
203    * @return
204    * @throws DException
205    */

206   private ConditionArray getClonedConditionArray(ConditionArray conditionArray) throws
207       DException {
208     try {
209       return (ConditionArray) conditionArray.clone();
210     }
211     catch (CloneNotSupportedException JavaDoc ex) {
212     }
213     throw new DException("DSE0",
214                          new Object JavaDoc[] {"Clone of ConditionArray is Not Successfull"});
215   }
216
217   /**
218    * This method is used to check whether single table level order present in
219        * passed order plan can be shifted to single table. There are some conditions
220    * present under which these single table level order can't be shifted to
221    * single table. The following cases can be arise -
222    * 1. If cross join or inner join are present, then single table level order
223    * can be shifted to single table level.
224    * 2. If Left outer join is present, If single table level order is present
225    * on all the tables lying under left outer join, then it can be shifted to
226        * single table level. If single table level order is present on all the tables
227    * lying under left side of LOJ, then it can be shifted to single table level
228    * in case when there are no more order present on some other table. In other
229    * cases order can't be shifted to single table level.
230        * 3. Right outer join is similar to left outer join if we change the position
231    * of underlying chidren, means left child becomes right and right child
232    * become left. So all the cases are similar to LOJ.
233    * 4. If Full outer join is present, then order is not shifted to single
234    * table.
235    * 5. If Natural join is present, it follows the rules of that join as the
236    * type of natural join.
237    *
238    * Detailed documentation is as given.
239    *
240    * This method checks for the possibiltiy of an OrderSequence Plan
241    * For each tableReference we ask for the OrderMapping by calling the checkForOrderSequencePlan
242        * The OrderMapping contains an array of singleTableOrderPlans and a match value:
243    * This Match value May be one of the following
244    * NOTMATCHED------------- this means there is no need to check any further.
245    * The order sequence plan is not possible.
246    * E.g.
247    * Select * from A LOJ B , C order By B.id,C.id
248    * PARTIALLYMATCHED------- this means that order sequence plan is possible if
249    * order is present on all the tables lying under left side of LOJ and there
250    * is no more single table level order.
251        * Select * from A LOJ B, C order by A.id --- in this case order sequence plan
252    * is possible in this case A LOJ B returns partially matched. So we search
253        * further if the next tablereference returns any OrderMapping we conclude that
254    * an orderSequence Plan is not possible..
255    * E.g.
256    * Select * from A LOJ B , C order By A.id, C.id.
257        * In the above e.g. the A LOJ B returns PARTIALLYMATCHED after this C returns
258    * A mapping hence we return i.e. an order seuence plan is not possible.
259    *
260    * After we have eliminated the above possibillty we check for
261    * presence of mixed sequence
262    * e.g.
263    * select * from A IJ B , C Order by A.a1 , C.c1 , B.b2
264    * In such cases it is not possible to create an OrderSequencePlan so we return null;
265    *
266    * @param plans
267    * @param session
268    * @param queryColumns
269    * @param conditionArray
270    * @return
271    * @throws DException
272    */

273   private OrderMappingWithTableRefernces[] checkForOrderSequencePossibility(
274       _OrderPlan orderPlan, _BVEPlan bvePlan, _ServerSession session,
275       _QueryColumns queryColumns, ConditionArray conditionArray) throws
276       DException {
277     boolean isPartiallyMatched = false;
278     ArrayList allGrouped = new ArrayList(5);
279     for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) {
280       OrderMappingWithTableRefernces[] mapping =
281           _OptRepScomma94843605tablereference0[i].checkForOrderSequencePlan(
282           session, orderPlan, bvePlan, queryColumns, conditionArray, false);
283       if (mapping != null) {
284         for (int j = 0; j < mapping.length; j++) {
285           if (isPartiallyMatched ||
286               mapping[j].getMatch() == TableExpressionConstants.NOTMATCHED) {
287             return null;
288           }
289           if (mapping[j].getMatch() ==
290               TableExpressionConstants.PARTIALLYMATCHED) {
291             isPartiallyMatched = true;
292           }
293           allGrouped.add(mapping[j]);
294         }
295       }
296     }
297     OrderMappingWithTableRefernces[] ref = (OrderMappingWithTableRefernces[])
298         allGrouped.toArray(new OrderMappingWithTableRefernces[allGrouped.size()]);
299     if (ref.length == 0 || isMixedSequencePresent(orderPlan, ref)) {
300       return null;
301     }
302     return ref;
303   }
304
305   /**
306    * In this method we check for the presence of mixed sequence in the single
307    * table order plans. If we find a mixed sequence we return true.
308    * A mixed sequence is
309    * Select * from A Loj B , C Order By A.id , C.id ,B.id---
310    * Algo :::
311    * STOP is single table order plan.
312    * We initialize a HashSet--
313    * For each STOP of the OrderBy we check if it belongs to any mapping::
314    * ---If it does we add the mapping to the HashSet
315    * if we are successful i.e. the mapping was not present in the HashSet
316    * we set the last encountered mapping to this mapping.
317    * if we fail to add the orderMapping i.e. the mapping was present in the HashSet
318    * we check if the mapping is the one that we encountered the last time
319    * if it is so we continue to check for the next STOP.
320    * else we return true i.e. mixed sequence is present.
321    * @param plans
322    * @param mappingArray
323    * @return
324    * @throws DException
325    */

326   private boolean isMixedSequencePresent(_OrderPlan orderPlan,
327                                          OrderMappingWithTableRefernces[]
328                                          mapping) throws DException {
329     _SingleTableOrderPlan[] stops = orderPlan.getSingleTableOrderPlans();
330     HashSet sequence = new HashSet();
331     int sequenceIndx = 0;
332     OrderMappingWithTableRefernces lastMapping = null;
333     for (int i = 0; i < stops.length; i++) {
334       for (int j = 0; j < mapping.length; j++) {
335         if (mapping[j].isBelongTo(stops[i])) {
336           if (sequence.add(mapping[j])) {
337             lastMapping = mapping[j];
338             break;
339           }
340           else if (mapping[j] == lastMapping) {
341             break;
342           }
343           else {
344             return true;
345           }
346         }
347       }
348     }
349     return false;
350   }
351
352   /**
353    * Returns simple array of Table Plans and of underlying views if view is optimizable
354    * when they are not arranged according to OrderSequencePlan
355    * Each tableReference is capable of returning an array of table Plans.
356    * @param session current server Session
357    * @param datedCondition Dated Condition in which table result is expected
358    * @param bvePlan
359    * @param orderPlan
360    * @param queryColumns
361    * @param conditionArray
362    * @return Table Plans array for this query as also of underlying views if view is optmizable
363    * @throws DException
364    */

365   private _TablePlan[] getTablePlansNotArrangedAccToOrderplan(_ServerSession
366       session, _DatedFramework datedCondition, _BVEPlan bvePlan,
367       _OrderPlan orderPlan,
368       _QueryColumns queryColumns, ConditionArray conditionArray) throws
369       DException {
370     int length = _OptRepScomma94843605tablereference0.length;
371     _TablePlan[] tablePlans = null;
372     for (int i = 0; i < length; i++) {
373       _TablePlan[] temp = _OptRepScomma94843605tablereference0[i].getTablePlans(
374           session, datedCondition, bvePlan, null, queryColumns, conditionArray);
375       tablePlans = getJoinTablePlans(tablePlans, temp);
376     }
377     return tablePlans;
378   }
379
380   /**
381        * This method returns the tableplans arranged according to the order sequence
382    * Algo :::
383        * We initialize an arrayList that contains all the tablePlans of all the table
384    * references
385    * then we sort the mapping according to the single table order plans --
386    * The result of sorting is that the mapping is arranged according to the order sequence
387    * For each mapping we get an array of table details of the mapping.
388        * Then we get the table plan that contains all thoes tables. If such a plan is
389    * Found we add it to the list of adjusted plans and remove it from the list of plans
390        * Finally we make an order Sequence Plan on the plans that have been adjusted.
391    * And concat it with the array of table plans that could not be adjusted.
392    * @param session
393    * @param datedCondition
394    * @param bvePlan
395    * @param orderPlan
396    * @param queryColumns
397    * @param conditionArray
398    * @param mapping
399    * @return
400    * @throws DException
401    */

402   private _TablePlan[] getTablePlansArrangedAccToOrderMappingWithTableReference(
403       _ServerSession session, _DatedFramework datedCondition, _BVEPlan bvePlan,
404       _OrderPlan orderPlan, _QueryColumns queryColumns,
405       ConditionArray conditionArray, OrderMappingWithTableRefernces[] mapping) throws
406       DException {
407     ArrayList tablePlans = getAllTablePlans(session, datedCondition, bvePlan,
408                                             orderPlan, queryColumns,
409                                             conditionArray);
410     ArrayList adjusted = new ArrayList();
411     mapping = sortAccToOrder(mapping, orderPlan.getSingleTableOrderPlans());
412     for (int i = 0; i < mapping.length; i++) {
413       TableDetails[] td = mapping[i].getTableDetails();
414       _TablePlan found = getTablePlanForOrderMapping(td, tablePlans);
415       tablePlans.remove(found);
416       adjusted.add(found);
417     }
418     tablePlans.add(0,
419                    new OrderSequencePlan( (_TablePlan[]) adjusted.toArray(new _TablePlan[
420         adjusted.size()])));
421     return (_TablePlan[]) tablePlans.toArray(new _TablePlan[tablePlans.size()]);
422   }
423
424   /**
425    * This method returns an array of OrderMapping that is in the same sequence
426    * as the single table order plans.
427    * @param mapping
428    * @param stop
429    * @return
430    * @throws DException
431    */

432   private OrderMappingWithTableRefernces[] sortAccToOrder(
433       OrderMappingWithTableRefernces[] mapping, _SingleTableOrderPlan[] stop) throws
434       DException {
435     ArrayList list = new ArrayList(5);
436     for (int i = 0; i < stop.length; i++) {
437       for (int j = 0; j < mapping.length; j++) {
438         if (mapping[j].isBelongTo(stop[i]) && !list.contains(mapping[j])) {
439           list.add(mapping[j]);
440         }
441       }
442     }
443     return (OrderMappingWithTableRefernces[]) list.toArray(new
444         OrderMappingWithTableRefernces[list.size()]);
445   }
446
447   /**
448    * This method returns the TablePlan which contains all the passed
449    * mapping tables.
450    * @param mappingTables
451    * @param tablePlans
452    * @return
453    * @throws DException
454    */

455   private _TablePlan getTablePlanForOrderMapping(TableDetails[] mappingTables,
456                                                  ArrayList tablePlans) throws
457       DException {
458
459     for (int i = 0, size = tablePlans.size(); i < size; i++) {
460       _TablePlan present = (_TablePlan) tablePlans.get(i);
461       TableDetails[] tablesOfReference = (TableDetails[]) present.
462           getTableDetails();
463
464       if (isMappingPresentInTablePlans(tablesOfReference, mappingTables)) {
465         return present;
466       }
467     }
468     throw new DException("SOME THING WRONG", null);
469   }
470
471   /**
472    * This method checks if all the mapping tables are present in the tables of
473    * The table Plan.
474    * If all the table are present it returns true
475    * Else
476    * returns false
477    * @param tablesOfReference
478    * @param mappingTables
479    * @return
480    * @throws DException
481    */

482   private boolean isMappingPresentInTablePlans(TableDetails[] tablesOfReference,
483                                                TableDetails[] mappingTables) throws
484       DException {
485     for (int i = 0, j = 0;
486          i < mappingTables.length && j < tablesOfReference.length; i++, j++) {
487       if (mappingTables[i] != tablesOfReference[j]) {
488         return false;
489       }
490     }
491     return true;
492   }
493
494   /**
495        * This method returns an array list containing all the table plans of all the
496    * table references.
497    * @param session
498    * @param datedCondition
499    * @param bvePlan
500    * @param orderPlan
501    * @param queryColumns
502    * @param conditionArray
503    * @return
504    * @throws DException
505    */

506   private ArrayList getAllTablePlans(_ServerSession session,
507                                      _DatedFramework datedCondition,
508                                      _BVEPlan bvePlan, _OrderPlan orderPlan,
509                                      _QueryColumns queryColumns,
510                                      ConditionArray conditionArray) throws
511       DException {
512     ArrayList tablePlans = new ArrayList();
513     for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) {
514       tablePlans.addAll(Arrays.asList(_OptRepScomma94843605tablereference0[i].
515                                       getTablePlans(session, datedCondition,
516           bvePlan, orderPlan, queryColumns, conditionArray)));
517     }
518     return tablePlans;
519   }
520
521   /**
522    * Simple Concatenates the two array of tablePlans passed and return the concatenated array.
523    * @param source1
524    * @param source2
525    * @return
526    * @throws DException
527    */

528
529   private _TablePlan[] getJoinTablePlans(_TablePlan[] source1,
530                                          _TablePlan[] source2) throws
531       DException {
532     if (source1 == null) {
533       return source2;
534     }
535     if (source2 == null) {
536       return source1;
537     }
538     _TablePlan buffered[] = new _TablePlan[source1.length + source2.length];
539     System.arraycopy(source1, 0, buffered, 0, source1.length);
540     System.arraycopy(source2, 0, buffered, source1.length, source2.length);
541     return buffered;
542   }
543
544   /**
545    * ReArrange Order in OrderPlan in case when single table Level Order involved in OrderPlan is not solvable
546    * at single Table Level.
547    * Join Level Order if present is shifted to next higher level of execution that to GroupByLevel
548    * and, single table level orders are shifted to Join Level Order.
549    * @param orderPlan
550    * @throws DException
551    */

552
553   private void reArrangeOrderPlansModified(_OrderPlan orderPlan) throws
554       DException {
555     _SingleTableOrderPlan[] stop = orderPlan.getSingleTableOrderPlans();
556     _Order order = orderPlan.getJoinLevelOrder();
557     if (order != null) {
558       orderPlan.setGroupByOrderPlan(order);
559     }
560     orderPlan.setJoinOrderPlan(OrderPlanMerger.getOrderFromSingleTableOrderPlan(
561         stop));
562     orderPlan.setSingleTableOrderPlans(null);
563   }
564
565   /**
566    * converts the whole tablereference list into string
567    * @return String
568    */

569   public String JavaDoc toString() {
570     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
571     sb.append(" ");
572     sb.append(_OptRepScomma94843605tablereference0[0]);
573     for (int i = 1; i < _OptRepScomma94843605tablereference0.length; i++) {
574       sb.append(",").append(_OptRepScomma94843605tablereference0[i]);
575     }
576     return sb.toString();
577   }
578
579   /**
580    *
581    * @return Object
582    * @throws CloneNotSupportedException
583    */

584   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
585     tablereferencelist tempClass = new tablereferencelist();
586     if (_OptRepScomma94843605tablereference0 != null) {
587       tablereference[] temp_OptRepScomma94843605tablereference0 = new
588           tablereference[_OptRepScomma94843605tablereference0.length];
589       for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) {
590         temp_OptRepScomma94843605tablereference0[i] = (tablereference)
591             _OptRepScomma94843605tablereference0[i].clone();
592       }
593       tempClass._OptRepScomma94843605tablereference0 =
594           temp_OptRepScomma94843605tablereference0;
595     }
596     return tempClass;
597   }
598
599   /**
600        * Note:-For documentation of following all method refers to queryexpressionbody.
601    * @param aList
602    * @throws DException
603    */

604
605   /**
606    * <br>This method is used to identify the tables that are required</br>
607        * <br>to complete the minimum requirements of the insert,update and delete</br>
608        * <br>in a select query (allcolumnIterator) with Qualified Joins Chains.</br>
609    * <br>This method calls the getTablesForBlankInsert method of each tablereference</br>
610    * <br>specified in the tablereferencelist.</br>
611    * @return TableDetails[]
612    * @throws DException
613    */

614   public TableDetails[] getTablesForBlankInsert() throws DException {
615     int len = _OptRepScomma94843605tablereference0.length;
616     ArrayList arr = new ArrayList();
617     for (int i = 0; i < len; i++) {
618       arr.addAll(Arrays.asList(_OptRepScomma94843605tablereference0[i].
619                                getTablesForBlankInsert()));
620     }
621     return (TableDetails[]) arr.toArray(new TableDetails[0]);
622   }
623
624   /**
625    * <br>In this method columnMappingHandler is passed as parameter which maintains</br>
626    * <br>a list of the tables in which insertion will be made for a insert operation</br>
627    * <br>in select query with qualified join chain. This method adds the tables if there</br>
628    * <br>exists one or more tables for insert operation according to the values specified by the user.</br>
629    * <br>This methods calls the setTablesForInsertion method for each tablereference specified.</br>
630    * @param mappingHandler
631    * @param vv
632    * @throws DException
633    */

634   public void setTablesForInsertion(ColumnMappingHandler mappingHandler,
635                                     _VariableValueOperations vv) throws
636       DException {
637     int len = _OptRepScomma94843605tablereference0.length;
638     for (int i = 0; i < len; i++) {
639       _OptRepScomma94843605tablereference0[i].setTablesForInsertion(
640           mappingHandler, vv);
641     }
642   }
643
644   /**
645    * <br>Merges BveExecutionPlans of underLying tableRefernces .</br>
646    * <br>This method gets the a BVEPlan from each tablereference and then merges them using the BVEPlanMerger class</br>
647    * <br>MergeTablePlansWithAnd and returns the merged plan</br>
648    * @return _BVEPlan
649    * @throws DException
650    */

651   public _BVEPlan getBveExecutionPlan() throws DException {
652     int length = _OptRepScomma94843605tablereference0.length;
653     _BVEPlan bvePlan = _OptRepScomma94843605tablereference0[0].
654         getBveExecutionPlan();
655     for (int i = 1; i < length; i++) {
656       bvePlan = BVEPlanMerger.mergeTablePlansWithAnd(bvePlan,
657           _OptRepScomma94843605tablereference0[i].getBveExecutionPlan());
658     }
659     return bvePlan;
660   }
661
662   /**
663    * <br>Returns all the tables included in the qualified-join chain.</br>
664    * <br>Calls the getAllTableDetails for each tablereference specified.</br>
665    * @return TableDetails[]
666    * @throws DException
667    */

668   public TableDetails[] getAllTableDetails() throws DException {
669     TableDetails[] tdToReturn = null;
670     int len = _OptRepScomma94843605tablereference0.length;
671     for (int i = 0; i < len; i++) {
672       tdToReturn = GeneralPurposeStaticClass.getJointTableDetails(tdToReturn,
673           _OptRepScomma94843605tablereference0[i].getAllTableDetails());
674     }
675     return tdToReturn;
676   }
677
678   /**
679    * <br>returns unknown references</br>
680    * <br>it gets the unknown reference from all the table references and then joins them and returns the joint references</br>
681    * @return _Reference[]
682    * @throws DException
683    */

684   public _Reference[] getUnderlyingReferences() throws DException {
685     _Reference[] references = null;
686     for (int i = 0, length = _OptRepScomma94843605tablereference0.length;
687          i < length; i++) {
688       references = GeneralPurposeStaticClass.getJointReferences(references,
689           _OptRepScomma94843605tablereference0[i].getUnderlyingReferences());
690     }
691     return references;
692   }
693
694   /**
695    * gets the views table details
696    * @return TableDetails[]
697    * @throws DException
698    */

699
700   public TableDetails[] getViewTableDetails() throws DException {
701     TableDetails[] tdToReturn = null;
702     int len = _OptRepScomma94843605tablereference0.length;
703     for (int i = 0; i < len; i++) {
704       tdToReturn = GeneralPurposeStaticClass.getJointTableDetails(tdToReturn,
705           _OptRepScomma94843605tablereference0[i].getViewTableDetails());
706     }
707     return tdToReturn;
708   }
709
710   /**
711    * This method is used to verify the values given by the user.
712    * <ol><li>Checks the conditional Column values given by user.
713    * If the values mismatches the join condition, exception is thrown.</li>
714    * <li>Checks the values must satisfy where clause condition, if violates,
715    * exception is thrown</li></ol>
716    * This methods calls the verifyValues method for each tablereference.
717    */

718
719   public void verifyValues(_VariableValueOperations variableValueOperation) throws
720       DException {
721     int length = _OptRepScomma94843605tablereference0.length;
722     for (int i = 0; i < length; i++) {
723       _OptRepScomma94843605tablereference0[i].verifyValues(
724           variableValueOperation);
725     }
726   }
727
728   /**
729    * This method is used to set the default values according to where clause
730    * and join condition,<li><li>If user gives value of one of the join specification
731        * conditional column used in join condition, value is set for the unspecified
732        * conditonal column to maintain the join condition.</li><li>If the where clause
733    * conditional column value is not given by the user, it is set as default value.</li></ol>
734    * This methods calls the setDefaultValues method for each tablereference.
735    */

736
737   public void setDefaultValues(_VariableValueOperations variableValueOperation) throws
738       DException {
739     for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) {
740       _OptRepScomma94843605tablereference0[i].setDefaultValues(
741           variableValueOperation);
742     }
743   }
744
745   /**
746    * Calls the run method of each table reference
747    * @param object
748    * @return Object
749    * @throws com.daffodilwoods.database.resource.DException
750    */

751
752   public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.
753
      DException {
754     int length = _OptRepScomma94843605tablereference0.length;
755     Object JavaDoc[] result = new Object JavaDoc[length];
756     for (int i = 0; i < length; i++) {
757       result[i] = _OptRepScomma94843605tablereference0[i].run(object);
758     }
759     return result;
760   }
761
762   /**
763    * <br>This method gets the table details of the table reference list</br>
764    * <br>It asks for the table details from each table reference in the list,
765    * and adds them to a single arraylist</br>
766    * <br>And returns this array list</br>
767    * @param session
768    * @return TableDetails[]
769    * @throws DException
770    */

771
772   public TableDetails[] getTableDetails(_ServerSession session,
773                                         ColumnDetails[] queryColumns) throws
774       DException {
775     ArrayList finalTableDetails = new ArrayList();
776     for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) {
777       finalTableDetails.addAll(Arrays.asList(
778           _OptRepScomma94843605tablereference0[i].getTableDetails(session,
779           queryColumns)));
780     }
781     return (TableDetails[]) finalTableDetails.toArray(new TableDetails[0]);
782   }
783
784   /**
785    * <br>This method returns the column details</br>
786    * <br>This method gets the column details from all the tablereferences and
787    * returns the combined list</br>
788    * @return ColumnDetails[]
789    * @throws DException
790    */

791
792   public ColumnDetails[] getColumnDetails() throws DException {
793     ArrayList aList = new ArrayList(5);
794     for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) {
795       aList.addAll(Arrays.asList(_OptRepScomma94843605tablereference0[i].
796                                  getColumnDetails()));
797     }
798     return (ColumnDetails[]) aList.toArray(new ColumnDetails[0]);
799   }
800
801   public com.daffodilwoods.daffodildb.server.sql99.utils._Reference[]
802       checkSemantic(com.daffodilwoods.daffodildb.server.serversystem.
803
                    _ServerSession parent, ColumnDetails[] queryColumns,
804                     boolean checkUserRight) throws DException {
805     ArrayList aList = new ArrayList();
806     for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) {
807       _Reference refArray[] = _OptRepScomma94843605tablereference0[i].
808           checkSemantic(parent, queryColumns, checkUserRight);
809       if (refArray != null) {
810         aList.addAll(Arrays.asList(refArray));
811       }
812     }
813    /*dst by Sandeep Kadiyan to solve BUG--12547 on 14-01-2005 */
814    if(aList.size()==0)
815      return null;
816    _Reference[] refs= (_Reference[]) aList.toArray(new _Reference[0]);
817     checkReferenceForAllTables(refs,parent);
818     return refs;
819   }
820
821   public void getColumnsIncluded(ArrayList aList) throws DException {
822     for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) {
823       _OptRepScomma94843605tablereference0[i].getColumnsIncluded(aList);
824     }
825   }
826
827   public void getTablesIncluded(ArrayList aList) throws DException {
828     for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) {
829       _OptRepScomma94843605tablereference0[i].getTablesIncluded(aList);
830     }
831   }
832
833   public ParameterInfo[] getParameterInfo() throws DException {
834     if (_OptRepScomma94843605tablereference0 == null) {
835       return null;
836     }
837     ArrayList aList = new ArrayList(5);
838     int length = _OptRepScomma94843605tablereference0.length;
839     for (int i = 0; i < length; i++) {
840       ParameterInfo[] pi = _OptRepScomma94843605tablereference0[i].
841           getParameterInfo();
842       if (pi != null) {
843         aList.addAll(Arrays.asList(pi));
844       }
845     }
846     return (ParameterInfo[]) aList.toArray(new ParameterInfo[0]);
847   }
848
849   public Object JavaDoc[] getParameters(Object JavaDoc object) throws DException {
850     ArrayList result = new ArrayList(5);
851     for (int i = 0; i < _OptRepScomma94843605tablereference0.length; i++) {
852       Object JavaDoc[] param = _OptRepScomma94843605tablereference0[i].getParameters(
853           object);
854       if (param != null) {
855         result.addAll(Arrays.asList(param));
856       }
857     }
858     return result.size() == 0 ? null : result.toArray(new Object JavaDoc[0]);
859   }
860
861   /**
862    * this method is used to merge two passed parameter list.
863    * @param param1
864    * @param param2
865    * @return Object[]
866    */

867
868   private Object JavaDoc[] addParameters(Object JavaDoc[] param1, Object JavaDoc[] param2) {
869     return param1 == null ? param2 : param2 == null ? param1 :
870         mergeParameters(param1, param2);
871   }
872
873   /**
874    * This method done merging of passed parameter list
875    * @param param1
876    * @param param2
877    * @return Object[]
878    */

879   private Object JavaDoc[] mergeParameters(Object JavaDoc[] param1, Object JavaDoc[] param2) {
880     int len1 = param1.length;
881     int len2 = param2.length;
882     int len = len1 + len2;
883     Object JavaDoc[] result = new Object JavaDoc[len];
884     System.arraycopy(param1, 0, result, 0, len1);
885     System.arraycopy(param2, 0, result, len1, len2);
886     return result;
887   }
888
889   public boolean isSimpleQuery(_ServerSession serverSession) throws DException {
890     /*More than two objects are not allowed in tablereferencelist*/
891     if (_OptRepScomma94843605tablereference0.length > 2)
892       return false;
893     else if (_OptRepScomma94843605tablereference0.length == 1) {
894       if (_OptRepScomma94843605tablereference0[0] instanceof derivedtableOptSRESERVEDWORD1206543922correlationnameOptparenderivedcolumnlist)
895         return false;
896
897      if (_OptRepScomma94843605tablereference0[0] instanceof subquery || _OptRepScomma94843605tablereference0[0] instanceof parentablereference)
898         return false;
899       if (_OptRepScomma94843605tablereference0[0] instanceof joinedtable) {
900         return ( (joinedtable) _OptRepScomma94843605tablereference0[0]).
901             isSimpleQuery(serverSession);
902       }
903       TableDetails[] td = _OptRepScomma94843605tablereference0[0].
904           getTableDetails(serverSession, null);
905       if (td.length > 2)
906         return false;
907       /*Multiple views does not represent a isSimpleQuery()*/
908       if (td.length == 2) {
909         if (td[0].getTableType() == TableDetails.VIEW ||
910             td[1].getTableType() == TableDetails.VIEW)
911           return false;
912       }
913       /*Here exceptino is cached because there may be reference of parentablereference .
914         we have to do handing of this case for updatable result set in future .-- Manoj Kr. */

915       try {
916           return td[0].getTableType() == TableDetails.VIEW ? ( (ViewAbstract)
917               _OptRepScomma94843605tablereference0[0]).getViewObject().
918               getQueryExpression().isSimpleQuery(serverSession) : true;
919       } catch (Exception JavaDoc ex) {
920           return false;
921       }
922
923     }
924     else
925       return isSimpleQueryCompliance(serverSession,
926                                      _OptRepScomma94843605tablereference0);
927   }
928
929   final private boolean isSimpleQueryCompliance(_ServerSession serverSession,
930                                                 tablereference tableReferences[]) throws
931       DException {
932     boolean firstTableFlag = true;
933     boolean secoundTableFlag = true;
934     if (tableReferences[0] instanceof subquery ||
935         tableReferences[1] instanceof subquery ||
936         tableReferences[0] instanceof derivedtableOptSRESERVEDWORD1206543922correlationnameOptparenderivedcolumnlist ||
937         tableReferences[1] instanceof derivedtableOptSRESERVEDWORD1206543922correlationnameOptparenderivedcolumnlist) {
938       return false;
939     }
940     if (tableReferences[0] instanceof joinedtable) {
941       firstTableFlag = ( (joinedtable) tableReferences[0]).
942           isSimpleQuery(serverSession);
943     }
944     if (tableReferences[1] instanceof joinedtable) {
945       secoundTableFlag = ( (joinedtable) tableReferences[1]).
946           isSimpleQuery(serverSession);
947     }
948     else
949       return false;
950     ;
951     return firstTableFlag && secoundTableFlag;
952   }
953   /* Method written by Sandeep to solve bug --12544
954      This method ensure that the passed references doesn't belong to the any table in the references list
955     First of all it make a list of references whose reference type is Columnetail. And then call the
956    SetColumnProperties.setTableNamesAndDatatypesOfAllColumn() method . This methods tries to resolve the passed
957    references with the passed table . and return the unresolved refernces in the passed 'underLyingRefs' list .
958    If the size of underLyingRefs AND list is different means any one refernces in resolved .Then we throw an exception
959    with appropriate columnname.
960
961    */

962   private void checkReferenceForAllTables(_Reference[] refs,
963                                           com.daffodilwoods.daffodildb.server.
964
                                          serversystem._ServerSession session) throws
965       DException {
966     if (refs == null)
967       return;
968     ArrayList list = new ArrayList();
969     for (int i = 0; i < refs.length; i++) {
970       if (refs[i].getReferenceType() == SimpleConstants.COLUMNDETAIL)
971         list.add( (ColumnDetails) refs[i]);
972     }
973     if (list.size() == 0)
974       return;
975     ArrayList underLyingRefs = new ArrayList();
976     SetColumnProperties.setTableNamesAndDatatypesOfAllColumns(session,
977         (ColumnDetails[]) list.toArray(new ColumnDetails[list.size()]),
978         getAllTableDetails(), underLyingRefs, new ArrayList(0));
979
980     if (list.size() != underLyingRefs.size()) { //means invalid column in tableReferences
981
if (underLyingRefs.size() == 0)
982         throw new DException("DSE256",
983                              new Object JavaDoc[] { ( (ColumnDetails) list.get(0)).
984                              getColumn()});
985       else
986         for (int i = 0; i < underLyingRefs.size(); i++) { //to print the appropriate column Name in exception
987
if (!underLyingRefs.contains(list.get(i)))
988             throw new DException("DSE256",
989                                  new Object JavaDoc[] { ( (ColumnDetails) list.get(i)).
990                                  getAppropriateColumn()});
991         }
992     }
993   }
994
995 }
996
Popular Tags