KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > plan > condition > BVEAllTablePlan


1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.sql99.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
7 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates.*;
8 import com.daffodilwoods.database.resource.*;
9
10 /**
11  * This class represents the plan of condition presents in select query.
12  * It is a container class which stores the condition divided in different levels
13  * condition divided into following 4 forms
14  * SingleLevelConditions involving singleTable
15  * JoinLevelConditions involving Two Tables
16  * Aggregate Condition condition involving aggregate function
17  * remainingConditions conditions which needs to be solved at last(after all cartesianing)
18  * <p>Title: </p>
19  * <p>Description: </p>
20  * <p>Copyright: Copyright (c) 2003</p>
21  * <p>Company: </p>
22  * @author unascribed
23  * @version 1.0
24  */

25 public class BVEAllTablePlan implements _BVEPlan, ExecutionPlanConstants, Cloneable JavaDoc {
26   /**
27    * It represents list of plan for singletablelevelcondition
28    */

29   public _BVESingleTablePlan[] bveSingleTablePlans;
30   /**
31    * It represents join conditions
32    */

33   public AllTablesJoinRelation allTablesJoinRelation;
34   /**
35    * It represents remaining condition which will be solved at top level.
36    */

37   public booleanvalueexpression remainingCondition;
38   /**
39    * It represents aggregate condition which will be solved on resultset of
40    * groupBy.
41    */

42   public booleanvalueexpression aggregateCondition;
43
44    public BVEAllTablePlan(_BVESingleTablePlan[] tablePlans0, AllTablesJoinRelation allTablesJoinRelation0, booleanvalueexpression remainingCondition0) {
45       bveSingleTablePlans = tablePlans0;
46       allTablesJoinRelation = allTablesJoinRelation0;
47       remainingCondition = remainingCondition0;
48    }
49
50    public BVEAllTablePlan(_BVESingleTablePlan[] tablePlans0, AllTablesJoinRelation allTablesJoinRelation0, booleanvalueexpression remainingCondition0, booleanvalueexpression nullCondition0, booleanvalueexpression aggregateCondition0) {
51       this(tablePlans0, allTablesJoinRelation0, remainingCondition0);
52       nullCondition = nullCondition0;
53       aggregateCondition = aggregateCondition0;
54    }
55    /**
56     * This method is required to get all tables involved in condition.
57     * @return
58     * @throws DException
59     */

60    public TableDetails[] getTableDetails() throws DException {
61       ArrayList tableDetailsList = new ArrayList();
62       if (bveSingleTablePlans != null) {
63          for (int i = 0; i < bveSingleTablePlans.length; i++) {
64             tableDetailsList.add(bveSingleTablePlans[i].getTableDetails()[0]);
65          }
66       }
67       if (allTablesJoinRelation != null) {
68          for (int i = 0; i < allTablesJoinRelation.twoTableJoinRelations.length; i++) {
69             TableDetails[] tds = allTablesJoinRelation.twoTableJoinRelations[i].getTableDetails();
70             for (int j = 0; j < tds.length; j++) {
71                if (!tableDetailsList.contains(tds[j])) {
72                   tableDetailsList.add(tds[j]);
73                }
74             }
75          }
76       }
77       if (remainingCondition != null) {
78          remainingCondition.getTablesIncluded(tableDetailsList);
79       }
80       if (aggregateCondition != null) {
81          aggregateCondition.getTablesIncluded(tableDetailsList);
82       }
83       return (TableDetails[]) tableDetailsList.toArray(new TableDetails[tableDetailsList.size()]);
84    }
85
86
87    /**
88     * This method is required by BVEPlanMerger during merging of two
89     * AllTablePlan which belongs to same table with AND condition.
90     * Merging of two AllTablePlan Performs following
91     * Merging of allSingleTableplan
92     * Merging of all join relation
93     * Merging of all remainingcondition
94     * Merging of all aggregatecondition
95     * Usage of this method is in merging of all remainingcondition.If condition
96     * is not null then merged with remaining condition else merged with null condition.
97     * @param condition
98     * @throws DException
99     */

100    public void andRemainingCondition(booleanvalueexpression condition) throws DException {
101       if (condition != null && condition.isNullPredicate()) {
102          booleanvalueexpression bve = BVEPlanMerger.addAndConditions(this.remainingCondition, condition);
103          this.remainingCondition = bve;
104       } else {
105          nullCondition = BVEPlanMerger.addAndConditions(nullCondition, condition);
106       }
107    }
108
109
110    /**
111     * This method is required by plan merger to merge two tableplan with AND .
112     * It provides functionality for merging of passed twotablejoinrelation and
113     * existing twotablejoinrelation with AND logical operator.
114     * @param allTableJoinRelation0
115     * @throws DException
116     */

117    public void andRelation(_AllTablesJoinRelation allTableJoinRelation0) throws DException {
118       if (allTableJoinRelation0 == null) {
119          return;
120       }
121       if (allTablesJoinRelation != null) {
122          allTablesJoinRelation.andRelation(allTableJoinRelation0.getRelations());
123          return;
124       }
125       allTablesJoinRelation = new AllTablesJoinRelation(allTableJoinRelation0.getRelations());
126    }
127    /**
128     * This method is required by plan merger to merge single table plan with AND.
129     * In merging of singletable plan,if either one of them is null return other.
130     * else merging of both single table plan are done. If table detail of both
131     * plans are matched then it is merged with AND, otherwise as such plan are
132     * added in resultant.
133     * @param allTableJoinRelation0
134     * @throws DException
135     */

136    public void andBVESingleTablePlan(_BVESingleTablePlan[] bveSingleTablePlans) throws DException {
137       if (this.bveSingleTablePlans == null) {
138          this.bveSingleTablePlans = bveSingleTablePlans;
139          return;
140       }
141       if (bveSingleTablePlans == null) {
142          return;
143       }
144       int length1 = bveSingleTablePlans.length;
145       int length2 = this.bveSingleTablePlans.length;
146       ArrayList list1 = new ArrayList(length1);
147       for (int i = 0; i < length1; i++) {
148          _BVESingleTablePlan bveSingleTablePlan = bveSingleTablePlans[i];
149          TableDetails[] tableDetails = bveSingleTablePlan.getTableDetails();
150          boolean found = false;
151          for (int j = 0; j < length2; j++) {
152             if (this.bveSingleTablePlans[j].getTableDetails()[0] == tableDetails[0]) {
153                BVEPlanMerger.mergeSamePlanWithAnd(this.bveSingleTablePlans[j], bveSingleTablePlan);
154                found = true;
155                break;
156             } //end if
157
} //end inner loop
158
if (!found) {
159             list1.add(bveSingleTablePlan);
160          }
161       } //end outer loop
162

163       int size = list1.size();
164       if (size != 0) {
165          BVESingleTablePlan[] tp = new BVESingleTablePlan[this.bveSingleTablePlans.length + size];
166          System.arraycopy(this.bveSingleTablePlans, 0, tp, 0, this.bveSingleTablePlans.length);
167          BVESingleTablePlan[] tp1 = (BVESingleTablePlan[]) list1.toArray(new BVESingleTablePlan[0]);
168          System.arraycopy(tp1, 0, tp, this.bveSingleTablePlans.length, tp1.length);
169          this.bveSingleTablePlans = tp;
170       }
171    }
172    /**
173     * This method is required by bveplanmerger to merge two singletableplan with
174     * OR logical operator if either one of them is null return null means singletableplan of
175     * other is set as remainingcondition otherwise merging of singletableplan
176     * are done,singletableplan are merged on basis of there tabledetails.when
177     * tabledetails of passed singletableplan are same with any existing singletableplan
178     * then it will be merged with OR logical operator and merged plan is added
179     * into resultantlist else singletableplan added as such.
180     * @param bveSingleTablePlans
181     * @throws DException
182     */

183    public void orBVESingleTablePlan(_BVESingleTablePlan[] bveSingleTablePlans) throws DException {
184       if (bveSingleTablePlans == null || this.bveSingleTablePlans == null) {
185          this.bveSingleTablePlans = null;
186          return;
187       }
188       int length1 = bveSingleTablePlans.length;
189       int length2 = this.bveSingleTablePlans.length;
190       ArrayList aList = new ArrayList(length1);
191       for (int i = 0; i < length1; i++) {
192          _BVESingleTablePlan bveSingleTablePlan = bveSingleTablePlans[i];
193          TableDetails[] tableDetails = bveSingleTablePlan.getTableDetails();
194          for (int j = 0; j < length2; j++) {
195             if (this.bveSingleTablePlans[j].getTableDetails()[0] == tableDetails[0]) {
196                aList.add(BVEPlanMerger.mergeSamePlanWithOr(this.bveSingleTablePlans[j], bveSingleTablePlan));
197                break;
198             }
199          }
200       }
201       this.bveSingleTablePlans = aList.size() != 0 ? (BVESingleTablePlan[]) aList.toArray(new BVESingleTablePlan[0]) : null;
202    }
203    /**
204     * see documentation of following method in _BVEPLAN
205     * @return
206     * @throws DException
207     */

208    public booleanvalueexpression getRemainingCondition() throws DException {
209       return BVEPlanMerger.addAndConditions(remainingCondition, nullCondition);
210    }
211
212    public void setRemainingCondition(booleanvalueexpression bve) throws DException {
213       remainingCondition = bve;
214    }
215    public int getType() throws DException {
216       return BVEConstants.BVEALLTABLEPLAN;
217    }
218    public _AllTablesJoinRelation getAllTableJoinRelation() throws DException {
219       return allTablesJoinRelation;
220    }
221    public booleanvalueexpression getAggregateCondition() throws DException {
222       return aggregateCondition;
223    }
224
225    public void setBVESingleTablePlans(_BVESingleTablePlan[] bveSingleTablePlans0) throws DException {
226       bveSingleTablePlans = bveSingleTablePlans0;
227    }
228    public _BVESingleTablePlan[] getBVESingleTablePlans() throws DException {
229       return bveSingleTablePlans;
230    }
231
232
233    public String JavaDoc toString() {
234       String JavaDoc str = "BVEALLTABLEPLAN";
235       if (bveSingleTablePlans != null) {
236          for (int i = 0; i < bveSingleTablePlans.length; i++) {
237             str += "BVESINGLETABLEPLAN[" + bveSingleTablePlans[i] + "]";
238          }
239       }
240       if (allTablesJoinRelation != null) {
241          str += "ALLTABLEJOINRELATION[" + allTablesJoinRelation + "]";
242       }
243       if (remainingCondition != null) {
244          str += "REMAININGCONDITION[" + remainingCondition + "]";
245       }
246       if (aggregateCondition != null) {
247          str += "AGGREGATECONDITION[" + aggregateCondition + "]";
248       }
249       return str;
250    }
251    /**
252     * This method is required by bveplan merger to merged passed joinrelation with
253     * this joinrelation. If passed joinrelation is null then this join relation
254     * set to null. If passed joinrelation are not null then check whether orrelation
255     * possible or not.OrRealtion will not be possible if passed joinrelation will not
256     * merged with this joinrelation then twotablejoinrelation of BVEALLTablePlan
257     * are set to null and joinrealtion will become remainingcondition of BVEALLTable
258     * Plan.
259     * @param allTablesJoinRelation
260     * @throws DException
261     */

262     public void orRelation(_AllTablesJoinRelation allTablesJoinRelation0) throws DException {
263       if (allTablesJoinRelation0 == null) {
264          allTablesJoinRelation = null;
265          return;
266       }
267       if (allTablesJoinRelation != null) {
268          if (!allTablesJoinRelation.orRelation(allTablesJoinRelation0.getRelations())) {
269             allTablesJoinRelation = null;
270          }
271       }
272    }
273
274    /**
275     * This method is responsible for merging of two aggregate condition with AND
276     * logical operator.
277     * @param condition
278     * @throws DException
279     */

280    public void andAggregateCondition(booleanvalueexpression condition) throws DException {
281       aggregateCondition = BVEPlanMerger.addAndConditions(aggregateCondition, condition);
282    }
283
284    public Object JavaDoc clone() {
285       BVEAllTablePlan bveAllTablePlan = new BVEAllTablePlan(bveSingleTablePlans, allTablesJoinRelation, remainingCondition, nullCondition, aggregateCondition);
286       return bveAllTablePlan;
287    }
288    /**
289     * It represents condition on notnullpredicate.
290     */

291    private booleanvalueexpression nullCondition;
292
293 }
294
Popular Tags