KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > resultset > SelectExecuter


1 package com.daffodilwoods.daffodildb.server.sql99.dql.resultset;
2
3 import com.daffodilwoods.daffodildb.server.serversystem.*;
4 import com.daffodilwoods.daffodildb.server.sql99.*;
5 import com.daffodilwoods.daffodildb.server.sql99.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*;
11 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
12 import com.daffodilwoods.database.resource.*;
13 import com.daffodilwoods.database.utility.*;
14 import com.daffodilwoods.daffodildb.utils.FieldUtility;
15 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
16 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._DropTableInfo;
17 import com.daffodilwoods.database.general.QualifiedIdentifier;
18 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._MergeTable;
19 import java.util.*;
20
21 /**
22  * It is executer for Select Query. It provides the functionality of obtaining
23  * resultset depending on the type of resultset. Type of resultset may be
24  * 1. SCROLLABLE, 2. NONSCROLLABLE, 3. UPDATABLE. User of select query obtains
25  * the resultset with the help of this executer. And this executer is obtained
26  * from top level class(queryexpression) of select query. It is also capable of
27  * providing sharable resultset in two ways.
28  * 1. Plan of select query can be shared while getting resultset for same query.
29  * 2. Resultset can be shared for different values of parameters in parameterized
30  * select query.
31  * <p>Title: </p>
32  * <p>Description: </p>
33  * <p>Copyright: Copyright (c) 2003</p>
34  * <p>Company: </p>
35  * @author unascribed
36  * @version 1.0
37  */

38
39 public class SelectExecuter implements _Executer, _QueryExecuter {
40
41    /**
42     * Represents the resultset of select query.
43     */

44
45    private _Iterator selectIterator;
46
47    /**
48     * Represents the parameters(question mark) of select query.
49     */

50
51    private _Reference[] queryParameters;
52
53    /**
54     * Represents the plan of select query.
55     */

56
57    private _TablePlan tPlan;
58
59    /**
60     * Represents the object of connection.
61     */

62
63    private _ServerSession serverSession;
64
65    /**
66     * Represents the tables involved in select query.
67     */

68
69    private TableDetails[] tableDetails;
70
71    /**
72     * Represents the UPDATABLE resultset.
73     */

74
75    private Object JavaDoc executer;
76
77    /**
78     * Represents the type of resultset.
79     */

80
81    private int type;
82
83    /**
84     * Represents the value of parameters. It is maintained to check whether the
85     * new values of parameters are same as that of old values.
86     */

87
88    private Object JavaDoc[] previousParameters;
89
90    _DropTableInfo[] dropTableInfo;
91
92    public SelectExecuter(_Reference[] queryParameters0, _ServerSession serverSession0, _TablePlan tPlan0, TableDetails[] tableDetails0) throws DException {
93       queryParameters = queryParameters0;
94       serverSession = serverSession0;
95       tPlan = tPlan0;
96       tableDetails = tableDetails0;
97       type = serverSession.getType();
98       try {
99          initializeDropTableInfo(serverSession);
100       } catch (DException ex) {
101          throw ex;
102       }
103    }
104
105    private void initializeDropTableInfo(_ServerSession serverSession) throws DException {
106       ArrayList dropTablesList = new ArrayList();
107       QualifiedIdentifier table;
108       for (int i = 0; i < tableDetails.length; i++) {
109          table = tableDetails[i].getQualifiedIdentifier();
110          _DropTableInfo dti = ( (_MergeTable) serverSession.getIndexTable(table)).getDropTableInfo();
111          if (serverSession.getColumnCharacteristics(table).getTableType() == TypeConstants.TABLE)
112             dropTablesList.add(dti);
113       }
114       dropTableInfo = (_DropTableInfo[]) dropTablesList.toArray(new _DropTableInfo[0]);
115    }
116
117    /**
118     * This method is used to obtain the resultset based on the type of resultset.
119     * It may be - 1. SCROLLABLE, 2. NONSCROLLABLE, 3. UPDATABLE.
120     * @param parm1 represents VariableValues which contains values of parameters.
121     * @return
122     * @throws com.daffodilwoods.database.resource.DException
123     */

124
125    public Object JavaDoc execute(_VariableValues parm1) throws com.daffodilwoods.database.resource.DException {
126       serverSession.resetTime();
127       switch (type) {
128          case IteratorConstants.NONSCROLLABLE:
129             return executeNonScrollable(parm1);
130          case IteratorConstants.UPDATABLE:
131             return executeUpdatable(parm1);
132          case IteratorConstants.SCROLLABLE:
133             return executeScrollable(parm1);
134       }
135       throw new DException("DSE0", new Object JavaDoc[] {"InValid SessionType"});
136    }
137
138    /**
139     * This method is used to obtain the resultset when resultset type is
140     * NONSCROLLABLE. It firstly checks whether previously resultset is formed
141     * or not. If already resultset is formed, then it returns the same resultset
142     * by setting the new values of parameters(which are contained in passed
143     * VariableValues) in resultset.
144     *
145     * @param variableValues
146     * @return
147     * @throws DException
148     */

149
150    public Object JavaDoc executeNonScrollable(_VariableValues variableValues) throws DException {
151       dropTableTest();
152       initializeSelectIterator(serverSession);
153       executeWithVV(variableValues);
154       return selectIterator;
155    }
156
157    /**
158     * This method is used to compute the cost of plan, which in turn makes the
159     * setting for execution of that plan.
160     * @param serverSessionWrapper
161     * @throws DException
162     */

163
164    private void computingPlanCost(_ServerSession serverSessionWrapper) throws DException {
165       tPlan.getCost(serverSessionWrapper);
166    }
167
168    /**
169     * It is used to obtain resultset. It firstly checks whether previously
170     * resultset is formed or not. If already resultset is formed, then it
171     * returns the same resultset.
172     * @param serverSession
173     * @throws DException
174     */

175
176    private void initializeSelectIterator(_ServerSession serverSession) throws DException {
177       if (selectIterator == null) {
178          computingPlanCost(serverSession);
179          selectIterator = tPlan.execute(serverSession);
180       }
181    }
182
183    /**
184     * This method is used to obtain the resultset when resultset type is
185     * SCROLLABLE. It firstly checks whether previously resultset is formed
186     * or not. If already resultset is formed, then it returns the same resultset
187     * by setting the new values of parameters(which are contained in passed
188     * VariableValues) in resultset.
189     * @param variableValues
190     * @return
191     * @throws DException
192     */

193
194    public Object JavaDoc executeScrollable(_VariableValues variableValues) throws DException {
195       dropTableTest();
196       initializeSelectIterator(serverSession);
197       executeWithVV(variableValues);
198       return selectIterator;
199    }
200
201    /**
202     * This method is used to obtain the resultset when resultset type is
203     * UPDATABLE. It firstly checks whether previously resultset is formed
204     * or not. If already resultset is formed, then it returns the same resultset
205     * by setting the new values of parameters(which are contained in passed
206     * VariableValues) in resultset. It also checks data types of new values of
207     * parameters with data types of old values of parameter. If data types are
208     * same then there is no need of reinitialization in resultset, otherwise
209     * reinitialization is performed in resultset.
210     * @param variableValues
211     * @return
212     * @throws DException
213     */

214
215    public Object JavaDoc executeUpdatable(_VariableValues variableValues) throws DException {
216       dropTableTest();
217       initializeSelectIterator(serverSession);
218       _Reference[] references = ( (VariableValues) variableValues).getReferences();
219       Object JavaDoc[] values = ( (VariableValues) variableValues).getValues();
220       checkForParameters( (Object JavaDoc[]) references, values);
221       executer = executer == null ? getExecutionObject(selectIterator, serverSession)
222           : executer;
223
224       previousParameters = values;
225       selectIterator.releaseResource();
226       GeneralPurposeStaticClass.initiateSetConditionVariableValues( (_Iterator) executer, references, values, 1);
227       return executer;
228    }
229
230    /**
231     * This method is used to check data types of new values of parameters with
232     * data types of old values of parameter. If data types are same then there
233     * is no need of reinitialization in resultset, otherwise reinitialization is
234     * performed in resultset. It also sets the new parameters in resultset.
235     * @param variableValues
236     * @param iterator
237     * @throws DException
238     */

239
240    private void executeVVFresh(_VariableValues variableValues, _Iterator iterator) throws DException {
241       _Reference[] references = ( (VariableValues) variableValues).getReferences();
242       Object JavaDoc[] values = ( (VariableValues) variableValues).getValues();
243       checkForParameters( (Object JavaDoc[]) references, values);
244       GeneralPurposeStaticClass.initiateSetConditionVariableValues(iterator, references, values, 1);
245    }
246
247    /**
248     * This method is used to check data types of new values of parameters with
249     * data types of old values of parameter. If data types are same then there
250     * is no need of reinitialization in resultset, otherwise reinitialization is
251     * performed in resultset.
252     * @param values
253     * @param iterator
254     * @throws DException
255     */

256
257    private Object JavaDoc[] callingReleaseResource(Object JavaDoc[] values, _Iterator iterator) throws DException {
258       if (previousParameters == null) {
259          previousParameters = values;
260          return null;
261       }
262       previousParameters = values;
263       iterator.releaseResource();
264       return null;
265    }
266
267    /**
268     * This method is similar to executeVVFresh(_VariableValues variableValues, _Iterator iterator)
269     * but with a little difference that it uses the same resultset each time,
270     * instead of passed resultset.
271     */

272
273    private void executeWithVV(_VariableValues parm1) throws DException {
274       _Reference[] references = ( (VariableValues) parm1).getReferences();
275       Object JavaDoc[] values = ( (VariableValues) parm1).getValues();
276       checkForParameters( (Object JavaDoc[]) references, values);
277       previousParameters = values;
278       selectIterator.releaseResource();
279       GeneralPurposeStaticClass.initiateSetConditionVariableValues(selectIterator, references, values, 1);
280    }
281
282    /**
283     * This method is used to obtain the resultset without taking care of
284     * parameters. It is required in case of subquery.
285     * @return
286     * @throws com.daffodilwoods.database.resource.DException
287     */

288
289    public Object JavaDoc executeWithOutValues() throws com.daffodilwoods.database.resource.DException {
290       dropTableTest();
291       initializeSelectIterator(serverSession);
292       return selectIterator;
293    }
294
295    public String JavaDoc toString() {
296       return "" + selectIterator;
297    }
298
299    /**
300     * It checks whether the number of parameters present in query and number of
301     * values supplied by user are equal or not.
302     * @param param1
303     * @param param2
304     * @throws com.daffodilwoods.database.resource.DException
305     */

306
307    private void checkForParameters(Object JavaDoc[] param1, Object JavaDoc[] param2) throws com.daffodilwoods.database.resource.DException {
308       if (param1 == null) {
309          if (param2 != null) {
310             throw new DException("DSE785", null);
311          }
312
313       } else if (param2 == null) {
314          throw new DException("DSE786", null);
315       } else if (param1.length != param2.length) {
316          ;//// Removed By Program ** System.out.println("@@@@@@@@@@@@@@@@@In SelectExecuter : " + P.print(param1) + " == " + P.print(param2));
317
throw new DException("DSE785", null);
318       }
319    }
320
321    private void showHashCode() {
322       for (int i = 0; i < queryParameters.length; i++) {
323          ;//// Removed By Program ** System.out.println(i + " hashCode = " + queryParameters[i].hashCode());
324
}
325    }
326
327    /**
328     * Returns the object in case when UPDATABLE resultset is needed.
329     * @param iterator
330     * @param serverSession
331     * @return
332     * @throws DException
333     */

334
335    private Object JavaDoc getExecutionObject(_Iterator iterator, _ServerSession serverSession) throws DException {
336       return new Executer(iterator, tableDetails, tPlan);
337    }
338
339    /**
340     * The following methods are similar to their counterparts
341     * execute(_VariableValues) methods, but with a little difference that values
342     * of parameters are passed directly in this method instead of values of
343     * parameters are contained in VariableValues.
344     */

345
346    public Object JavaDoc execute(Object JavaDoc[] parm1) throws com.daffodilwoods.database.resource.DException {
347       serverSession.resetTime();
348       switch (type) {
349          case IteratorConstants.NONSCROLLABLE:
350             return executeNonScrollable(parm1);
351          case IteratorConstants.UPDATABLE:
352             return executeUpdatable(parm1);
353          case IteratorConstants.SCROLLABLE:
354             return executeScrollable(parm1);
355       }
356       throw new DException("DSE0", new Object JavaDoc[] {"InValid SessionType"});
357    }
358
359    public Object JavaDoc executeNonScrollable(Object JavaDoc[] values) throws DException {
360       dropTableTest();
361
362       initializeSelectIterator(serverSession);
363       checkForParameters(queryParameters, values);
364       Object JavaDoc[] ob = callingReleaseResource(values, selectIterator);
365       if (ob == null)
366          GeneralPurposeStaticClass.initiateSetConditionVariableValues(selectIterator, queryParameters, values, 1);
367       else
368          GeneralPurposeStaticClass.initiateSetConditionVariableValues(selectIterator, queryParameters, ob, 1);
369       return selectIterator;
370    }
371
372    public boolean dropTableTest() throws DException {
373       for (int i = 0; i < dropTableInfo.length; i++) {
374          if (dropTableInfo[i].isTableDropped())
375             throw new DException("DSE5584", null);
376       }
377       return true;
378    }
379
380    public Object JavaDoc executeScrollable(Object JavaDoc[] values) throws DException {
381       dropTableTest();
382
383       initializeSelectIterator(serverSession);
384       checkForParameters(queryParameters, values);
385       Object JavaDoc[] ob = callingReleaseResource(values, selectIterator);
386       if (ob == null)
387          GeneralPurposeStaticClass.initiateSetConditionVariableValues(selectIterator, queryParameters, values, 1);
388       else
389          GeneralPurposeStaticClass.initiateSetConditionVariableValues(selectIterator, queryParameters, ob, 1);
390       return selectIterator;
391    }
392
393    public Object JavaDoc executeUpdatable(Object JavaDoc[] values) throws DException {
394       dropTableTest();
395
396       initializeSelectIterator(serverSession);
397       checkForParameters(queryParameters, values);
398       executer = executer == null ? getExecutionObject(selectIterator, serverSession)
399           : executer;
400       Object JavaDoc[] ob = callingReleaseResource(values, (_Iterator) executer);
401       if (ob == null)
402          GeneralPurposeStaticClass.initiateSetConditionVariableValues( (_Iterator) executer, queryParameters, values, 1);
403       else
404          GeneralPurposeStaticClass.initiateSetConditionVariableValues( (_Iterator) executer, queryParameters, ob, 1);
405       return executer;
406    }
407
408    /**
409     * The following methods are similar to their counterparts
410     * execute(_VariableValues) methods, but with a little difference that in
411     * these methods plan is shared, means new resultset is formed every time by
412     * using same plan, instead of only one resultset by setting the new values
413     * of parameters in it.
414     */

415
416    public Object JavaDoc executeForFresh(_VariableValues parm1) throws com.daffodilwoods.database.resource.DException {
417       switch (type) {
418          case IteratorConstants.NONSCROLLABLE:
419             return executeNonScrollableFresh(parm1);
420          case IteratorConstants.UPDATABLE:
421             return executeUpdatableFresh(parm1);
422          case IteratorConstants.SCROLLABLE:
423             return executeScrollableFresh(parm1);
424       }
425       throw new DException("DSE0", new Object JavaDoc[] {"InValid SessionType"});
426    }
427
428    public Object JavaDoc executeNonScrollableFresh(_VariableValues variableValues) throws DException {
429       ServerSessionWrapper session = new ServerSessionWrapper(serverSession);
430       dropTableTest();
431       session.setType(IteratorConstants.NONSCROLLABLE);
432       computingPlanCost(session);
433       _Iterator iterator = tPlan.execute(session);
434       executeVVFresh(variableValues, iterator);
435       return iterator;
436    }
437
438    public Object JavaDoc executeScrollableFresh(_VariableValues variableValues) throws DException {
439       dropTableTest();
440
441       computingPlanCost(serverSession);
442       _Iterator iterator = tPlan.execute(serverSession);
443       executeVVFresh(variableValues, iterator);
444       return iterator;
445    }
446
447    public Object JavaDoc executeUpdatableFresh(_VariableValues variableValues) throws DException {
448       dropTableTest();
449       computingPlanCost(serverSession);
450       _Iterator iterator = tPlan.execute(serverSession);
451       _Reference[] references = ( (VariableValues) variableValues).getReferences();
452       Object JavaDoc[] values = ( (VariableValues) variableValues).getValues();
453       checkForParameters( (Object JavaDoc[]) references, values);
454       Object JavaDoc executer = getExecutionObject(iterator, serverSession);
455       GeneralPurposeStaticClass.initiateSetConditionVariableValues( (_Iterator) executer, references, values, 1);
456       return executer;
457    }
458
459    /**
460     * The following methods are similar to their counterparts
461     * executeForFresh(_VariableValues) methods, but with a little difference
462     * that values of parameters are passed directly in this method instead of
463     * values of parameters are contained in VariableValues.
464     */

465
466    public Object JavaDoc executeForFresh(Object JavaDoc[] parm1) throws com.daffodilwoods.database.resource.DException {
467       serverSession.resetTime();
468       switch (type) {
469          case IteratorConstants.NONSCROLLABLE:
470             return executeNonScrollableFresh(parm1);
471          case IteratorConstants.UPDATABLE:
472             return executeUpdatableFresh(parm1);
473          case IteratorConstants.SCROLLABLE:
474             return executeScrollableFresh(parm1);
475       }
476       throw new DException("DSE0", new Object JavaDoc[] {"InValid SessionType"});
477    }
478
479    public Object JavaDoc executeNonScrollableFresh(Object JavaDoc[] values) throws DException {
480       dropTableTest();
481       checkForParameters(queryParameters, values);
482       computingPlanCost(serverSession);
483       _Iterator iterator = tPlan.execute(serverSession);
484       Object JavaDoc[] ob = callingReleaseResource(values, iterator);
485       if (ob == null)
486          GeneralPurposeStaticClass.initiateSetConditionVariableValues(iterator, queryParameters, values, 1);
487       else
488          GeneralPurposeStaticClass.initiateSetConditionVariableValues(iterator, queryParameters, ob, 1);
489       return iterator;
490    }
491
492    public Object JavaDoc executeScrollableFresh(Object JavaDoc[] values) throws DException {
493       dropTableTest();
494       checkForParameters(queryParameters, values);
495       computingPlanCost(serverSession);
496       _Iterator iterator = tPlan.execute(serverSession);
497       GeneralPurposeStaticClass.initiateSetConditionVariableValues(iterator, queryParameters, values, 1);
498       return iterator;
499    }
500
501    public Object JavaDoc executeUpdatableFresh(Object JavaDoc[] values) throws DException {
502       dropTableTest();
503       checkForParameters(queryParameters, values);
504       computingPlanCost(serverSession);
505       _Iterator iterator = tPlan.execute(serverSession);
506       Object JavaDoc executer = getExecutionObject(iterator, serverSession);
507       GeneralPurposeStaticClass.initiateSetConditionVariableValues( (_Iterator) executer, queryParameters, values, 1);
508       return executer;
509    }
510 }
511
Popular Tags