KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 package com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression;
3
4 import java.util.*;
5
6 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
7 import com.daffodilwoods.daffodildb.server.serversystem.*;
8 import com.daffodilwoods.daffodildb.server.sql99.*;
9 import com.daffodilwoods.daffodildb.server.sql99.common.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
11 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*;
13 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*;
14 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*;
15 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.orderbyclause.*;
16 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.queryspecification.*;
17 import com.daffodilwoods.daffodildb.server.sql99.dql.resultset.*;
18 import com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause.*;
19 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
20 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
21 import com.daffodilwoods.daffodildb.utils.parser.*;
22 import com.daffodilwoods.database.resource.*;
23 import com.daffodilwoods.database.sqlinitiator.*;
24 import com.daffodilwoods.database.utility.P;
25 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.constraintsystem.TypeConstant;
26 import com.daffodilwoods.daffodildb.server.sql99.common.TypeConstants;
27
28 /**
29  *
30  * <p>Title: queryexpression </p>
31  * <p>Description:
32  * queryexpression represents the Select Query(Set/Simple) with order by clause.
33  * It is the top most class of Select. It provides functionality to verify
34  * whether query is semantically correct or not as well as functionality to
35  * get optimal plan. This plan helps in obtaining the resultset to retrieve the
36  * result from this select query.
37  * </p>
38  * <p>Copyright: Copyright (c) 2003</p>
39  * <p>Company: Daffodil S/W Ltd </p>
40  * @author SelectTeam
41  * @version 1.0
42  */

43
44 public class queryexpression implements com.daffodilwoods.daffodildb.utils.parser.StatementExecuter, SQL99, Parameters {
45
46    /**
47     * Represents the 'For Update' option of select query.
48     */

49
50      public forupdateclause _Optforupdateclause0 ;
51
52    /**
53     * Represents the order by clause. It is optional.
54     */

55
56    public orderbyclause _Optorderbyclause1;
57
58    /**
59     * Represents the select query without order by.
60     */

61
62    public queryexpressionbody _queryexpressionbody2;
63    public dummyrule _Optdummyrule3;
64
65    /**
66     * It is used to ensure that semantic checkinf is performed only once.
67     */

68
69    private int statusOfSemanticChecking = -1;
70
71    /**
72     * Represents the objects whose value is to be provided by user.
73     */

74
75    private _Reference[] underLyingReferences;
76
77    public queryexpression() {
78    }
79
80    /**
81     * Constructor required in select statement single row query
82     * @param qs0
83     */

84    public queryexpression(queryspecification qs0) {
85       _queryexpressionbody2 = qs0;
86    }
87
88    /**
89     * Method is basically used in Test Cases to check the order of query
90     * @return
91     */

92
93    public orderbyclause getOrderByclause() {
94       return _Optorderbyclause1;
95    }
96
97    /**
98     * It allows user to obtain a plan through the help of which resultset is
99     * obtained. It also ensures whether query is semantically correct before
100     * giving plan.
101     * @param object
102     * @return SelectExecuter
103     * @throws com.daffodilwoods.database.resource.DException
104     */

105
106    public Object JavaDoc run(Object JavaDoc object) throws com.daffodilwoods.database.resource.DException {
107       _ServerSession session = (_ServerSession) object;
108       int a = statusOfSemanticChecking;
109       _Reference[] references = checkSemanticChecking(session, true); // Question Marks ,,,
110

111       /* Done by Kaushik on 4/8/2004 to solve some failed update test cases involving Union
112       if (references != null && a==-1) {
113         checkForUnknownReferences(references);
114         if(_queryexpressionbody2 instanceof queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm)
115           ((queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm)_queryexpressionbody2).setDataTypesForSetOperatorQuery(_Optorderbyclause1);
116         if(_queryexpressionbody2 instanceof queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryprimary){
117            ( (queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryprimary) _queryexpressionbody2).initializeAppropriateDataTypeAndSizeWithOrderBy(_Optorderbyclause1);
118         }
119       }
120    */

121       if (references != null) {
122         if(a==-1)
123           checkForUnknownReferences(references);
124         if(_queryexpressionbody2 instanceof queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm)
125           ((queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm)_queryexpressionbody2).setDataTypesForSetOperatorQuery(_Optorderbyclause1);
126         if(_queryexpressionbody2 instanceof queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryprimary){
127            ( (queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryprimary) _queryexpressionbody2).initializeAppropriateDataTypeAndSizeWithOrderBy(_Optorderbyclause1);
128         }
129       }
130
131       _TablePlan tablePlan = getExecutionPlan(session, null, null, null, null, null);
132       return new SelectExecuter(references, session, tablePlan, getAllTableDetails());
133    }
134
135    /**
136     * It is required to ensure whether query is semantically correct or not.
137     * The objects whose value is not provided by this query, are returned. And
138     * it is assumed that their values are to be provided by user. It performs
139     * two steps -
140     * 1. Perform the semantic checking of whole select query.
141     * 2. Perform the semantic checking in case if 'For Update' is present.
142     * @param session
143     * @param checkUserRight
144     * @return
145     * @throws DException
146     */

147
148    public _Reference[] checkSemantic(_ServerSession session,boolean checkUserRight) throws DException {
149       if (statusOfSemanticChecking == -1){
150          underLyingReferences = _queryexpressionbody2.checkSemantic(session,_Optorderbyclause1,checkUserRight,false);
151
152          if (_Optorderbyclause1 != null) { // for order by ?
153
_Reference[] orderByRef = _Optorderbyclause1.checkSemantic(session);
154             underLyingReferences = orderByRef != null && orderByRef.length != 0 ? GeneralPurposeStaticClass.getJointReferences(underLyingReferences, orderByRef) : underLyingReferences;
155          }
156          statusOfSemanticChecking = 0;
157          checkSemanticForUpdate();
158       }
159       return underLyingReferences;
160    }
161
162    /**
163     * It is required for semantic checking of select when 'For update' is
164     * present. 'For update' option is not supported for set operator.
165     * @throws DException
166     */

167
168    private void checkSemanticForUpdate() throws DException {
169       if (_Optforupdateclause0 == null) {
170          return;
171       }
172       if (! (_queryexpressionbody2 instanceof queryspecification)) {
173          throw new DException("DSE0", null);
174       }
175       ( (queryspecification) _queryexpressionbody2).checkSemanticForUpdate();
176    }
177
178    /**
179     * Returns those objects whose value is to be provided by user.
180     * @usage subquery
181     * @return
182     * @throws DException
183     */

184
185    public _Reference[] getUnderlyingReferences() throws DException {
186       return underLyingReferences;
187    }
188
189    /**
190     * Method initiates the semantic Checking of query. When this method is called,
191     * it means it is top level Select query so it can contains only question
192     * marks as unresolved objects. So exception is thrown for unresolved objects
193     * except questionmarks.
194     * @param object
195     * @return Array of Question marks if any.
196     * @throws DException
197     */

198    private _Reference[] checkSemanticChecking(_ServerSession object, boolean checkUserRight) throws DException {
199       if (statusOfSemanticChecking == -1) {
200          underLyingReferences = checkSemantic(object, checkUserRight);
201
202 /* if (underLyingReferences != null) {
203            if(_queryexpressionbody2 instanceof queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm)
204              ((queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryterm)_queryexpressionbody2).setDataTypesForSetOperatorQuery(_Optorderbyclause1);
205            if(_queryexpressionbody2 instanceof queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryprimary){
206               ( (queryexpressionbodySRESERVEDWORD1206543922OptsetquantifierOptdummyrulequeryprimary) _queryexpressionbody2).initializeAppropriateDataTypeAndSizeWithOrderBy(_Optorderbyclause1);
207            }
208             return underLyingReferences;
209          }
210          return null;
211 */

212       }
213       return underLyingReferences;
214    }
215
216    /**
217     * Separate Out the Question Marks and UnKnown References from UnderLying References.
218     * And check if there are any unResolved Reference except Question Marks.
219     * @param underLyingReferences
220     * @return
221     * @throws DException
222     */

223    private void checkForUnknownReferences(_Reference[] underLyingReferences) throws DException {
224       ArrayList unResolvedReferences = new ArrayList();
225       for (int i = 0, length = underLyingReferences.length; i < length; i++) {
226         if (underLyingReferences[i].getReferenceType() == SimpleConstants.COLUMNDETAIL) {
227           unResolvedReferences.add(underLyingReferences[i]);
228         }
229       }
230       if (!unResolvedReferences.isEmpty()) {
231         String JavaDoc DE= ((_Reference)unResolvedReferences.get(0)).getReason();
232
233         if(DE!=null){
234           throw new DException(DE,new Object JavaDoc[]{((_Reference)unResolvedReferences.get(0)).getColumn()});
235         }
236         else{
237           throw new DException("DSE256", extractStrings(unResolvedReferences.toArray()));
238         }
239       }
240     }
241
242
243    /**
244     * It is used to return the name of columns.
245     * @param rfs
246     * @return
247     * @throws DException
248     */

249
250    private String JavaDoc[] extractStrings(Object JavaDoc[] rfs) throws DException {
251       String JavaDoc[] s = new String JavaDoc[rfs.length];
252       for (int i = 0; i < rfs.length; i++) {
253          s[i] = ( (_Reference) rfs[i]).getColumn();
254       }
255       return s;
256    }
257
258    /**
259     * Method returns _ColumnCharacteristics of query which is required in case
260     * of From Sub Query.
261     * @param object
262     * @return
263     * @throws DException
264     */

265    public _ColumnCharacteristics getColumnCharacteristics(Object JavaDoc object) throws DException {
266       checkSemanticChecking( (_ServerSession) object, true);
267       return _queryexpressionbody2.getColumnCharacteristics(object);
268    }
269
270    /**
271     * It allows user to obtain the plan for this query. Plan is formed on the
272     * basis of this select query and passed parameters. It also makes a special
273     * provision for 'For Update'.
274     * @param session
275     * @param bve represents condition which is to be solved on this query.
276     * @param datedCondition represents condition for dated framework
277     * @param order represents order which is to be solved on this query.
278     * @param cdsWithActualTableDetails represents the columns of actual tables,
279     * if this query in involved in view or 'from subquery'
280     * @param conditionArray - represents the array of conditions.
281     * @return
282     * @throws DException
283     */

284
285    public _TablePlan getExecutionPlan(_ServerSession session, booleanvalueexpression bve, _DatedFramework datedCondition, _Order order, ColumnDetails[] cdsWithActualTableDetails, ConditionArray conditionArray) throws DException {
286       order = order == null ? _Optorderbyclause1 == null ? null : _Optorderbyclause1.getOrder() : order;
287       if (conditionArray == null) {
288          conditionArray = new ConditionArray();
289       }
290       _TablePlan tablePlan = _queryexpressionbody2.getExecutionPlan(session, bve, datedCondition, order, cdsWithActualTableDetails, conditionArray);
291       handleForUpdate(tablePlan);
292       return tablePlan;
293    }
294
295    /**
296     * Sets a flag in QuerySpecificationPlan to represents 'For Update' option.
297     * @param tablePlan
298     * @throws DException
299     */

300
301    private void handleForUpdate(_TablePlan tablePlan) throws DException {
302       if (_Optforupdateclause0 == null) {
303          return;
304       }
305       ( (QuerySpecificationPlan) tablePlan).setForUpdate();
306    }
307
308    public String JavaDoc toString() {
309       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
310       sb.append(" ");
311       if (_Optdummyrule3 != null) {
312          sb.append(_Optdummyrule3);
313       }
314       sb.append(" ");
315       sb.append(_queryexpressionbody2);
316       sb.append(" ");
317       if (_Optorderbyclause1 != null) {
318          sb.append(_Optorderbyclause1);
319       }
320       if (_Optforupdateclause0 != null) {
321          sb.append(_Optforupdateclause0);
322       }
323       return sb.toString();
324    }
325
326    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
327    return this ;// Done by Sandeep. See 10949
328
}
329
330    /**
331     * Optimizable view means to put the contents of this query in place of
332     * view used in outer query. The criteria for view not optimizable are -
333     * <ol>
334     * <li> If order/group/distinct is present</li>
335     * <li> If functional/aggregate columns are present</li>
336     * <li> If Set Operators are present</li>
337     * </ol>
338     * @return boolean
339     * @throws DException
340     */

341    public boolean isViewOptimizationPossible() throws DException {
342       if (_Optorderbyclause1 != null) {
343          return false;
344       }
345       return _queryexpressionbody2.isViewOptimizationPossible();
346    }
347
348    /**
349     * This method is called when the query is part of view definition and that
350     * view is optimizable. Then the upward query needs the condition plan of
351     * view's query to merge this condition plan with its condition plan.
352     * @return
353     * @throws DException
354     */

355
356    public _BVEPlan getBveExecutionPlan() throws DException {
357       return _queryexpressionbody2.getBveExecutionPlan();
358    }
359
360    /**
361     * It is required when view is optimizable and we have to merge all the table
362     * plans of this query with table plans of upward query in which this view is
363     * involved.
364     * @param session
365     * @param condition
366     * @param order
367     * @param columnDetails
368     * @param conditionArray
369     * @return
370     * @throws DException
371     */

372
373    public _TablePlan[] getTablePlans(_ServerSession session, booleanvalueexpression condition, _Order order, ColumnDetails[] columnDetails, ConditionArray conditionArray) throws DException {
374       return _queryexpressionbody2.getTablePlans(session, condition, order, columnDetails, conditionArray);
375    }
376
377    /**
378     * This method is needed for Browser to perform insertion/updation/deletion
379     * in resultSet.
380     * Returns trdafue if the query contains a single table with no where clause,
381     * group by clause, having clause, order by clause, functional/aggregate columns.
382     * @return boolean
383     * @throws DException
384     */

385    public boolean isSimpleQuery(_ServerSession serverSession) throws DException {
386       boolean b = false;
387           if (_queryexpressionbody2 instanceof queryspecification) {
388          b = ( (queryspecification) _queryexpressionbody2).isSimpleQuery(serverSession);
389           } else {
390          return b;
391       }
392       /*Work done for updatable resultset in case of order by . Dec . 4, 2004 -- Bug : 12650 -- Manoj Kr. */
393       if (b && _Optorderbyclause1 != null) {
394           ColumnDetails orderByCDs[] = _Optorderbyclause1.getColumnDetails();
395           for (int i = 0; i < orderByCDs.length; i++) {
396             if (orderByCDs[i].getType() != TypeConstants.REFERENCE)
397               return false;
398           }
399         }
400       return b ;
401    }
402
403    /**
404     * All the following methods deligate their calls to queryexpressionbody. So
405     * for the documentation of these methods, refer the documentation of
406     * queryexpressionbody.
407     */

408
409    public void setTablesForInsertion(ColumnMappingHandler parm1, _VariableValueOperations vv) throws com.daffodilwoods.database.resource.DException {
410       _queryexpressionbody2.setTablesForInsertion(parm1, vv);
411    }
412
413    public TableDetails[] getTablesForBlankInsert() throws com.daffodilwoods.database.resource.DException {
414       return _queryexpressionbody2.getTablesForBlankInsert();
415    }
416
417    public TableDetails[] getAllTableDetails() throws DException {
418       return _queryexpressionbody2.getAllTableDetails();
419    }
420
421    public TableDetails[] getViewTableDetails() throws DException {
422       return _queryexpressionbody2.getViewTableDetails();
423    }
424
425    public void setDefaultValues(_VariableValueOperations variableValueOperation) throws DException {
426       _queryexpressionbody2.setDefaultValues(variableValueOperation);
427    }
428
429    public _Reference[] getReferences(TableDetails[] tableDetails) throws DException {
430       return _queryexpressionbody2.getReferences(tableDetails);
431    }
432
433    public ParameterInfo[] getParameterInfo() throws DException {
434      if (_Optorderbyclause1 == null)
435        return _queryexpressionbody2.getParameterInfo();
436      ParameterInfo[] qe = _queryexpressionbody2.getParameterInfo();
437      ParameterInfo[] order = _Optorderbyclause1.getParameterInfo();
438      if (qe == null && order==null)
439         return null;
440       int j = 0;
441       ArrayList aList = new ArrayList(5);
442       if(qe != null) {
443         for (int i = 0; i < qe.length; i++) {
444           if (qe[i].getQuestionMark()) {
445             if (qe[i].getName() == null) {
446               qe[i].setName("Expr" + j++);
447             }
448             aList.add(qe[i]);
449           }
450         }
451       }
452       if(order != null) {
453         for (int i = 0; i < order.length; i++) {
454           if (order[i].getQuestionMark()) {
455             if (order[i].getName() == null) {
456               order[i].setName("Expr" + j++);
457             }
458             aList.add(order[i]);
459           }
460         }
461       }
462       return aList.size() == 0 ? null : (ParameterInfo[]) aList.toArray(new ParameterInfo[0]);
463    }
464
465
466    public ColumnDetails[] getSelectedColumns() throws DException {
467       return _queryexpressionbody2.getSelectedColumns();
468    }
469
470    public ColumnDetails[] getColumnDetails() throws DException {
471       return _queryexpressionbody2.getColumnDetails();
472    }
473
474 /* Currently we are not supporting ? in order by clause . So error will be thrown from SemanticChecking.
475   _Optorderbyclause1.getParameters(object)) will be added in parameters if in future we will
476    allow ? in order by clause .
477
478   */

479
480    public Object JavaDoc[] getParameters(Object JavaDoc object) throws DException {
481      if( _Optorderbyclause1 == null)
482      return _queryexpressionbody2.getParameters(object);
483    else
484   return GeneralPurposeStaticClass.getCombinedArray(_queryexpressionbody2.getParameters(object),
485      _Optorderbyclause1.getParameters(object));
486  }
487
488    public void getColumnsIncluded(ArrayList aList) throws DException {
489       _queryexpressionbody2.getColumnsIncluded(aList);
490    }
491
492    public void getTablesIncluded(ArrayList aList) throws DException {
493       _queryexpressionbody2.getTablesIncluded(aList);
494    }
495
496    public void setFKeyColumnDetails(ColumnDetails[] fKeyColumns) throws DException {
497       _queryexpressionbody2.setFKeyColumnDetails(fKeyColumns);
498    }
499
500    public QueryProperty getStrings() throws DException {
501       QueryProperty qr = _queryexpressionbody2.getStrings();
502       qr.setOrderByClause(_Optorderbyclause1 == null ? null : _Optorderbyclause1.toString());
503       return qr;
504    }
505
506   public boolean hasConstantSelectedColumn(booleanvalueexpression bve) throws DException {
507      return _queryexpressionbody2.hasConstantSelectedColumn(bve);
508    }
509
510 }
511
512
Popular Tags