KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > expression > booleanvalueexpression > booleanfactor


1 package com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression;
2
3 import java.io.*;
4
5 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*;
6 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
7 import com.daffodilwoods.daffodildb.server.sql99.common.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*;
11 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates.*;
12 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*;
13 import com.daffodilwoods.daffodildb.server.sql99.token.*;
14 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
15 import com.daffodilwoods.database.resource.*;
16 import com.daffodilwoods.database.sqlinitiator.*;
17 import com.daffodilwoods.database.utility.*;
18 import com.daffodilwoods.daffodildb.server.sql99.dql.execution._SingleTableCondition;
19
20 /**
21  * <p>Title: BooleanFactor</p>
22  * <p>Description:
23  * This class represets Predicate/ParenthesizedBVE/TRUE/FALSE with one of its
24  * optional attribute which is NOT. means to negate the underlying result.
25  * Negation means to convert True into False, False into True and Unknown into
26  * Unknown
27  * It provides the functionality of Bve Execution Plan and execution of the
28  * condition.
29  * </p>
30  * <p>Copyright: Copyright (c) 2003</p>
31  * <p>Company: Daffodil S/W Ltd.</p>
32  * @author SelectTeam
33  * @version 1.0
34  */

35
36 public class booleanfactor extends AbstractBooleanValueExpression implements booleanterm, Serializable, IntegerPool, ExecutionPlanConstants, JoinConditionTableGetter,_SingleTableCondition {
37
38    public booleantest _booleantest0;
39    public SRESERVEDWORD1206543922 _OptSRESERVEDWORD12065439221;
40
41    public booleanfactor() {
42    }
43
44    public booleanfactor(booleantest object) {
45       _booleantest0 = object;
46    }
47
48    /**
49     * Returns the cost of evaluating this condition on passed number of rows, by
50     * considering the factor of index.
51     * @param rowCount
52     * @param index
53     * @return cost
54     * @throws DException
55     */

56
57    public double getCost(long rowCount, boolean index) throws DException {
58       return _booleantest0.getCost(rowCount, index);
59    }
60
61    /**
62     * This method is called from qualified join. Passed TableDetails are of
63     * Right plans in case of LOJ and of Left Table Plans in case of ROJ.
64     * With the help of this method we identify whether 'on condition' is solvable
65     * on SingleTablePlan or on Qualified Join Level.
66     * @param tableDetails
67     * @return
68     * @throws DException
69     */

70
71    public _QualifiedBVE getQualifiedBVE(TableDetails[] tableDetails) throws DException {
72       return (_OptSRESERVEDWORD12065439221 == null) ?
73           _booleantest0.getQualifiedBVE(tableDetails) : new QualifiedBVE(null, this);
74    }
75
76    /**
77     * This method tells whether this condition can be used for seek or not.
78     * The criteria for seek is -
79     * Individual predicates is Equal Predicate with one column of type
80     * Reference on each side of Equal Predicate.
81     * if NOT is present then we can't used it for seek.
82     * @return
83     * @throws DException
84     */

85
86    public int canUseForSeek() throws DException {
87       if (_OptSRESERVEDWORD12065439221 != null) {
88          return TypeConstants.NOSEEK;
89       }
90       return _booleantest0.canUseForSeek();
91
92
93    }
94
95    /**
96     * This method is called from top level condition class. And that top level
97     * class represents the condition of SingleTable. Now this method separates the
98     * condition of individual predicates based on the columnName as well as
99     * the condition which can't be used in index like not predicate etc.
100     * @param allColumnPredicates
101     * @throws DException
102     */

103
104    public void setColumnPredicates(_AllColumnPredicates allColumnPredicates) throws
105        DException {
106       if (_OptSRESERVEDWORD12065439221 != null) {
107          allColumnPredicates.addToNonIndex(this);
108       } else {
109          _booleantest0.setColumnPredicates(allColumnPredicates);
110       }
111    }
112
113    public int getConditionType() throws DException {
114       return _booleantest0.getConditionType();
115    }
116
117    /**
118     * This method returns the ExecutionPlan of the condition. Execution Plan
119     * contains the following information -
120     * 1. Array of BveSingleTablePlans, the condition which can be solved on
121     * single table level.
122     * 2. BveAllTablePlan which contains the condition in which two tables are
123     * involved(join condition) and it also contains remaining condition
124     * 3. BveAggregatePlan which contains the condition of aggregate columns.
125     * This condition can involve in having clause only.
126     * if NOT is present then we'll return BVEALLTABLEPLAN with this condition
127     * as remaining condition.
128     * @return
129     * @throws DException
130     */

131
132    public _BVEPlan getExecutionPlan() throws DException {
133       if (_OptSRESERVEDWORD12065439221 == null) { // ?? use ternary operator.
134
return _booleantest0.getExecutionPlan();
135       }
136       return new BVEAllTablePlan(null, null, this);
137    }
138
139    /**
140     * This method is called on execution time, to get the result.
141     * Passed Object is variableValues which contains -
142     * 1. Reference-Value mapping
143     * 2. Iterator.
144     * This variable values helps in retrieving the result of underlying
145     * predicates.
146     * @param object
147     * @return This method returns the result of anding of the result of
148     * underlying predicates -
149     * Zero means condition is satisfied.
150     * Non Zero(one or minu one) means condition is not satisfied.
151     * Two or minus two means unKnown
152     * if Not is present then the underlying result will be negate.
153     * i. if underlying result is zero then one is returned.
154     * ii. if underlying result is one/minus one then zero is returned.
155     * else underlying result is returned.
156     * @throws DException
157     */

158
159    public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.
160
       DException {
161       if (_OptSRESERVEDWORD12065439221 == null) {
162          return _booleantest0.run(object);
163       }
164       return predicate.boolResultForCompliment[_booleantest0.run(object).hashCode() +
165           2];
166    }
167
168    /**
169     * This method is called from Browser, when Parameterized query is fired from Browser.
170     * With the help of ParameterInfo Browser open ups daialog box(labelled by its corresponding name)
171     * for each question mark to put the values of question mark.
172     * @return
173     * @throws DException
174     */

175
176    public ParameterInfo[] getParameterInfo() throws DException {
177       return _booleantest0.getParameterInfo();
178    }
179
180    /**
181     * This method is used to verify the values given by the user
182     * corresponding to join-condition specified.
183     * If the values given mismatchs the join condition, exception is thrown.
184     */

185
186    public void verifyValues(_VariableValueOperations vv) throws DException {
187       if (_booleantest0._booleanprimary1 instanceof JoinConditionTableGetter) {
188          ( (JoinConditionTableGetter) _booleantest0._booleanprimary1).verifyValues(
189              vv);
190       }
191    }
192
193    /**
194     * This method returns all the children of this class. It is needed in
195     * Abstract classes, so as to move common methods in Abstract class.
196     * @return
197     */

198
199    public AbstractRowValueExpression[] getChilds() {
200       AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] {
201           (AbstractRowValueExpression) (_booleantest0)};
202       return childs;
203    }
204
205    /**
206     * This method tells whether null predicate is present in the condition or not.
207     * This is needed while shifting the condition to single table in case of
208     * Loj/Roj to identify whether to shift condition or not. As if null predicate
209     * is present in condition on right table of LOJ and left table of Roj, then
210     * we can't shift the condition to single table level.
211     * @return
212     * @throws DException
213     */

214
215    public boolean isNullPredicate() throws DException {
216       if (_OptSRESERVEDWORD12065439221 != null) {
217          return false;
218       }
219       return _booleantest0.isNullPredicate();
220    }
221
222    /**
223     * This method returns the estimate of the rows remaining after the execution
224     * of this condition on passed number of rows.
225     * @param noOfRows
226     * @return
227     * @throws DException
228     */

229
230    public long getEstimatedRows(long noOfRows) throws DException {
231       return _booleantest0.getEstimatedRows(noOfRows);
232    }
233
234    /**
235     * This condition represents the condition of SingleTable.
236     * This method returns the cost of this condition based on following criteria -
237     * 1. Condition is solvable on index
238     * A. Is Order solvable on that index.
239     * a. Are Query columns solvable on that index.
240     * 2. Condition is not solvable on index
241     * A. Order is solvable on index.
242     * a. Are Query columns solvable on that index.
243     *
244     * For achieving that purpose we initialize AllColumnPredicate in which we
245     * separate out the individual predicates based on the column name as well
246     * as the predicates which can't be solved on index.
247     * Then We check cost of AllColumnPredicates for each index available on
248     * that table.
249     * @param order
250     * @param queryColumns
251     * @param session
252     * @param tableDetail
253     * @param aggregateColumns
254     * @return
255     * @throws DException
256     */

257
258    public _IndexPredicateInterface getCost(_Order order, String JavaDoc[] queryColumns,
259                                            Object JavaDoc session,
260                                            TableDetails tableDetail, ColumnDetails[] aggregateColumns) throws
261        DException {
262       AllColumnPredicates allColumnPredicate = initializeAllColumnPredicate(
263           tableDetail);
264       return calculateCost(order, queryColumns, session, tableDetail,
265                            allColumnPredicate, aggregateColumns);
266    }
267
268    private IndexPredicate calculateCost(_Order order, String JavaDoc[] queryColumns,
269                                         Object JavaDoc session, TableDetails tableDetail,
270                                         AllColumnPredicates allColumnPredicate, ColumnDetails[] aggregateColumns) throws DException {
271       IndexPredicate resultantIndexPredicate = null;
272       _IndexInformation[] indexInformation = ( (_IndexTable) session).
273           getIndexInformations();
274       ColumnDetails aggColumn = aggregateColumns == null ? null : aggregateColumns[0].getExistingColumnDetails()[0];
275       for (int i = 0; i < indexInformation.length; ++i) {
276          IndexPredicate indexPredicate = (IndexPredicate) allColumnPredicate.
277              splitForIndex(indexInformation[i], i);
278          indexPredicate.setTableDetails(tableDetail);
279          if (TableReferenceMerger.checkForAggregateColumn(aggColumn, indexInformation[i])) {
280             double cost = CostCalculator.computeCostForIndex(indexPredicate, session,
281                 order, queryColumns, indexInformation[i], i);
282             indexPredicate.setCost(cost);
283             if (aggColumn != null) {
284                indexPredicate.setIndex(i);
285             }
286             resultantIndexPredicate = checkForMinimum(indexPredicate,
287                 resultantIndexPredicate);
288          }
289       }
290       if (resultantIndexPredicate.getOrder() == null && order != null) {
291          order.setSolvableByIndex(true);
292       }
293       return resultantIndexPredicate;
294    }
295
296    private AllColumnPredicates initializeAllColumnPredicate(TableDetails
297        tableDetail) throws DException {
298       AllColumnPredicates allColumnPredicate = new AllColumnPredicates();
299       allColumnPredicate.setTableDetails(tableDetail);
300       _booleantest0.setColumnPredicates(allColumnPredicate);
301       return allColumnPredicate;
302    }
303
304    private IndexPredicate checkForMinimum(IndexPredicate indexPredicate1,
305                                           IndexPredicate indexPredicate2) throws
306        DException {
307       if (indexPredicate2 == null) {
308          return indexPredicate1;
309       }
310       double comingCost = indexPredicate1.getCost();
311       double inCost = indexPredicate2.getCost();
312       return inCost > comingCost
313           ? indexPredicate1 : indexPredicate2;
314    }
315
316    /**
317     * This method is needed in insert through resultset and is called from
318     * join condition of qualified join.
319     * @param td
320     * @return
321     * @throws com.daffodilwoods.database.resource.DException
322     */

323
324    public TableDetails[] getCorrespondingTablesInvolved(TableDetails[] td) throws
325        com.daffodilwoods.database.resource.DException {
326       if (_booleantest0._booleanprimary1 instanceof JoinConditionTableGetter) {
327          return ( (JoinConditionTableGetter) _booleantest0._booleanprimary1).
328              getCorrespondingTablesInvolved(td);
329       }
330       return null;
331    }
332
333    /**
334     * This method is needed while inserting through ResultSet, so as to insert
335     * some default values according to ResultSet. E.g ResultSet is taken on
336     * Select * from Country where id = 5 and if user inserts a row in that
337     * resultset then we'll put 5 in id column by default.
338     * @param variableValueOperation
339     * @throws DException
340     */

341
342    public void setDefaultValues(_VariableValueOperations variableValueOperation) throws
343        DException {
344       if (_OptSRESERVEDWORD12065439221 == null) {
345          _booleantest0.setDefaultValues(variableValueOperation);
346       }
347    }
348
349    /**
350     * This method checks whether condition contains any subQuery or not.
351     * @return
352     * @throws DException
353     */

354
355    public boolean checkForSubQuery() throws DException {
356       return _booleantest0.checkForSubQuery();
357    }
358
359    public String JavaDoc toString() {
360       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
361       sb.append(" ");
362       if (_OptSRESERVEDWORD12065439221 != null) {
363          sb.append(_OptSRESERVEDWORD12065439221);
364       }
365       sb.append(" ");
366       sb.append(_booleantest0);
367       return sb.toString();
368    }
369
370    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
371       booleanfactor tempClass = new booleanfactor();
372       tempClass._booleantest0 = (booleantest) _booleantest0.clone();
373       if (_OptSRESERVEDWORD12065439221 != null) {
374          tempClass._OptSRESERVEDWORD12065439221 = (SRESERVEDWORD1206543922)
375              _OptSRESERVEDWORD12065439221.clone();
376       }
377       try {
378          tempClass.getColumnDetails();
379       } catch (DException ex) {
380          throw new RuntimeException JavaDoc(ex.getMessage());
381       }
382       return tempClass;
383    }
384
385    public booleanvalueexpression getSingleTableCondition() throws DException {
386       return checkForMultipleTables() ? null : this;
387    }
388
389 }
390
Popular Tags