KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > execution > ConditionSingleTableExecuter


1 package com.daffodilwoods.daffodildb.server.sql99.dql.execution;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
4 import com.daffodilwoods.daffodildb.server.serversystem.*;
5 import com.daffodilwoods.daffodildb.server.sql99.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*;
9 import com.daffodilwoods.daffodildb.server.sql99.expression.
10     booleanvalueexpression.*;
11 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
12 import com.daffodilwoods.database.resource.*;
13 import com.daffodilwoods.database.sqlinitiator.*;
14 import java.util.*;
15
16 /**
17  * It allows user to obtain resultset for single table. It takes care of
18  * condition and order present on single table.
19  * <p>Title: </p>
20  * <p>Description: </p>
21  * <p>Copyright: Copyright (c) 2003</p>
22  * <p>Company: </p>
23  * @author unascribed
24  * @version 1.0
25  */

26
27 public class ConditionSingleTableExecuter
28     extends SingleTableExecuter {
29
30   /**
31    * Represents the condition present on the table.
32    */

33
34   private booleanvalueexpression condition;
35
36   public ConditionSingleTableExecuter(_Order order0, TableDetails table0,
37                                       _ServerSession serverSession0,
38                                       booleanvalueexpression condition0,
39                                       String JavaDoc[] queryColumns) {
40     super(order0, table0, serverSession0, queryColumns, null);
41     condition = condition0;
42   }
43
44   public ConditionSingleTableExecuter(_Order order0, TableDetails table0,
45                                       _ServerSession serverSession0,
46                                       booleanvalueexpression condition0,
47                                       String JavaDoc[] queryColumns,
48                                       ColumnDetails[] aggregateColumns0) {
49     super(order0, table0, serverSession0, queryColumns, aggregateColumns0);
50     condition = condition0;
51   }
52
53   /**
54    * This method is required to obtain the cost of solving this table. Cost
55    * is calculated on the basis of condition,order and columns present on the
56    * table. If no condition is present then the parent class is used to
57    * calculate the cost. otherwise condition is responsible for calculating
58    * the cost of table.
59    * @param indexTable
60    * @return
61    * @throws DException
62    */

63
64   public double getCost(_IndexTable indexTable) throws DException {
65     /*
66           if (condition == null && order != null)
67           changes done by neeraj khanna to solve problem of autoincrement when query is run internally
68      */

69
70     if (condition == null) {
71       return super.getCost(indexTable);
72     }
73     _IndexPredicateInterface indexPredicateInterface = condition.getCost(order,
74         queryColumns, indexTable, table, aggregateColumns);
75     return indexPredicateInterface.getCost();
76   }
77
78   /**
79    * It is required to obtain the resultset of single table. Resultset is
80    * formed on the basis of cost computed.
81    * @param indexTable
82    * @return
83    * @throws DException
84    */

85   public boolean updateQuery = false;
86   public _Iterator execute(_IndexTable indexTable) throws DException {
87     /*
88       if (condition == null && order != null)
89       changes done by neeraj khanna to solve problem of autoincrement when query is run internally
90      */

91
92     if (condition == null) {
93       return super.execute(indexTable);
94     }
95     try {
96       _Iterator iterator = condition.getCost(order, queryColumns, indexTable,
97                                              table,
98           aggregateColumns).execute(indexTable);
99       if (updateQuery) {
100         _Reference[] ref = condition.getReferences(new TableDetails[] {table});
101         if (ref != null) {
102           ArrayList list = new ArrayList();
103           for (int i = 0; i < ref.length; i++) {
104             if (ref[i].getReferenceType() != SimpleConstants.VARIABLECOLUMN)
105               list.add(ref[i]);
106           }
107           if (list.size() > 0) {
108             _Reference[] ref1 = (_Reference[]) list.toArray(new _Reference[0]);
109             ref1 = GeneralPurposeStaticClass.getAllReferences(ref1);
110             if (ref1 != null) {
111               iterator.setSpecificUnderlyingReferences(ref1);
112             }
113           }
114         }
115       }
116       return iterator;
117     }
118     catch (NullPointerException JavaDoc ex) {
119       throw new DException("DSE0", new Object JavaDoc[] {"Condition cannot be null"});
120     }
121   }
122
123   /**
124    * Following methods are used by sessiontable. For the documentation please
125    * refer to documentation of sessiontable.
126    */

127   /**
128     SessionTable add conditon 'true' to condition of Single Table .But this condition
129    does not filter any records or we can say,there is no use of solving this condition .
130    @author Sandeep Kadiyan
131    @since after release 4.0
132    */

133
134   public void addCondition(booleanvalueexpression condition0) throws DException {
135     /*Dst */
136     /*Dend */
137     condition = BVEPlanMerger.addAndConditions(condition, condition0);
138   }
139
140   public _Reference[] getReferences() throws DException {
141     return condition == null ? null :
142         condition.getReferences(new TableDetails[] {table});
143   }
144
145   public booleanvalueexpression getCondition() throws DException {
146     return condition;
147   }
148
149   public _Iterator executeForDirtyLock(_IndexTable indexTable) throws
150       DException {
151     booleanvalueexpression condition = getSingleTableCondition();
152     if (condition == null && order != null) {
153       return super.execute(indexTable);
154     }
155     return condition.getCost(order, queryColumns, indexTable, table,
156                              aggregateColumns).execute(indexTable);
157   }
158
159   private booleanvalueexpression getSingleTableCondition() throws DException {
160     if (condition == null) {
161       return null;
162     }
163     if (condition instanceof _SingleTableCondition) {
164       return ( (_SingleTableCondition) condition).getSingleTableCondition();
165     }
166     else {
167       throw new DException("DSE0",
168                            new Object JavaDoc[] {"Not handled the condition " +
169                            condition.getClass()});
170     }
171   }
172
173   public boolean isContainsPresent() {
174     return containsFlag;
175   }
176
177   public void setContainsClauseFlag(boolean containsFlag) throws DException {
178     this.containsFlag = containsFlag;
179   }
180
181   public boolean isParameterisedQuery() throws DException {
182     _Reference[] references = getReferences();
183     if (references == null)
184       return false;
185     for (int i = 0; i < references.length; i++) {
186       if (references[i].getReferenceType() == SimpleConstants.VARIABLECOLUMN)
187         return true;
188     }
189     return false;
190   }
191 }
192
Popular Tags