1 | |
package org.apache.ojb.odmg.oql; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.io.StringReader; |
19 | |
import java.util.Enumeration; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.ListIterator; |
22 | |
import java.util.Vector; |
23 | |
import java.util.List; |
24 | |
|
25 | |
import antlr.RecognitionException; |
26 | |
import antlr.TokenStreamException; |
27 | |
import org.apache.ojb.broker.ManageableCollection; |
28 | |
import org.apache.ojb.broker.PBKey; |
29 | |
import org.apache.ojb.broker.PersistenceBroker; |
30 | |
import org.apache.ojb.broker.PersistenceBrokerFactory; |
31 | |
import org.apache.ojb.broker.accesslayer.OJBIterator; |
32 | |
import org.apache.ojb.broker.query.BetweenCriteria; |
33 | |
import org.apache.ojb.broker.query.Criteria; |
34 | |
import org.apache.ojb.broker.query.Query; |
35 | |
import org.apache.ojb.broker.query.ReportQuery; |
36 | |
import org.apache.ojb.broker.query.SelectionCriteria; |
37 | |
import org.apache.ojb.broker.util.collections.ManageableArrayList; |
38 | |
import org.apache.ojb.broker.util.configuration.Configurable; |
39 | |
import org.apache.ojb.broker.util.configuration.Configuration; |
40 | |
import org.apache.ojb.broker.util.configuration.ConfigurationException; |
41 | |
import org.apache.ojb.broker.util.logging.Logger; |
42 | |
import org.apache.ojb.broker.util.logging.LoggerFactory; |
43 | |
import org.apache.ojb.odmg.ImplementationImpl; |
44 | |
import org.apache.ojb.odmg.OdmgConfiguration; |
45 | |
import org.apache.ojb.odmg.PBCapsule; |
46 | |
import org.apache.ojb.odmg.RuntimeObject; |
47 | |
import org.apache.ojb.odmg.TransactionImpl; |
48 | |
import org.odmg.QueryInvalidException; |
49 | |
import org.odmg.Transaction; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public class OQLQueryImpl implements EnhancedOQLQuery, Configurable |
57 | |
{ |
58 | |
private Logger log = LoggerFactory.getLogger(OQLQueryImpl.class); |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private Query query = null; |
64 | |
private ListIterator bindIterator = null; |
65 | |
private ImplementationImpl odmg; |
66 | |
|
67 | |
public OQLQueryImpl(ImplementationImpl odmg) |
68 | |
{ |
69 | |
this.odmg = odmg; |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public OQLQueryImpl(PBKey pbKey) |
77 | |
{ |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public Query getQuery() |
85 | |
{ |
86 | |
return query; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
protected Class getCollectionClass() |
93 | |
{ |
94 | |
return odmg.getOqlCollectionClass(); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public void bind(Object parameter) |
119 | |
throws org.odmg.QueryParameterCountInvalidException, org.odmg.QueryParameterTypeInvalidException |
120 | |
{ |
121 | |
try |
122 | |
{ |
123 | |
SelectionCriteria crit = (SelectionCriteria) getBindIterator().next(); |
124 | |
crit.bind(parameter); |
125 | |
|
126 | |
|
127 | |
if (crit instanceof BetweenCriteria && !crit.isBound()) |
128 | |
{ |
129 | |
getBindIterator().previous(); |
130 | |
} |
131 | |
} |
132 | |
catch (Exception e) |
133 | |
{ |
134 | |
throw new org.odmg.QueryParameterCountInvalidException(e.getMessage()); |
135 | |
} |
136 | |
} |
137 | |
|
138 | |
private Vector flatten(Criteria crit, Vector acc) |
139 | |
{ |
140 | |
Enumeration e = crit.getElements(); |
141 | |
while (e.hasMoreElements()) |
142 | |
{ |
143 | |
Object o = e.nextElement(); |
144 | |
if (o instanceof Criteria) |
145 | |
{ |
146 | |
Criteria pc = (Criteria) o; |
147 | |
flatten(pc, acc); |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | |
SelectionCriteria c = (SelectionCriteria) o; |
152 | |
|
153 | |
if (!c.isBound()) |
154 | |
{ |
155 | |
acc.add(c); |
156 | |
} |
157 | |
} |
158 | |
} |
159 | |
return acc; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public void create(String queryString) throws org.odmg.QueryInvalidException |
175 | |
{ |
176 | |
create(queryString, Query.NO_START_AT_INDEX, Query.NO_END_AT_INDEX); |
177 | |
} |
178 | |
|
179 | |
public void create(String queryString, int startAtIndex, int endAtIndex) throws QueryInvalidException |
180 | |
{ |
181 | |
if (log.isDebugEnabled()) log.debug("create query for query-string: " + queryString); |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
if ((endAtIndex != Query.NO_END_AT_INDEX) && (endAtIndex < startAtIndex)) |
188 | |
{ |
189 | |
throw new QueryInvalidException("endAtIndex must be greater than startAtIndex"); |
190 | |
} |
191 | |
if (((endAtIndex != Query.NO_END_AT_INDEX) && (startAtIndex != Query.NO_START_AT_INDEX)) |
192 | |
&& (endAtIndex == startAtIndex)) |
193 | |
{ |
194 | |
throw new QueryInvalidException("endAtIndex cannot be set equal to startAtIndex"); |
195 | |
} |
196 | |
|
197 | |
try |
198 | |
{ |
199 | |
|
200 | |
|
201 | |
Query _query; |
202 | |
StringReader reader = new StringReader(queryString); |
203 | |
OQLLexer lexer = new OQLLexer(reader); |
204 | |
OQLParser parser = new OQLParser(lexer); |
205 | |
_query = parser.buildQuery(); |
206 | |
setBindIterator(flatten(_query.getCriteria(), new Vector()).listIterator()); |
207 | |
_query.setStartAtIndex(startAtIndex); |
208 | |
_query.setEndAtIndex(endAtIndex); |
209 | |
setQuery(_query); |
210 | |
} |
211 | |
catch (RecognitionException e) |
212 | |
{ |
213 | |
throw new QueryInvalidException(e.getMessage()); |
214 | |
} |
215 | |
catch (TokenStreamException e) |
216 | |
{ |
217 | |
throw new QueryInvalidException(e.getMessage()); |
218 | |
} |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public Object execute() throws org.odmg.QueryException |
235 | |
{ |
236 | |
if (log.isDebugEnabled()) log.debug("Start execute query"); |
237 | |
|
238 | |
|
239 | |
TransactionImpl tx = odmg.getTxManager().getTransaction(); |
240 | |
|
241 | |
PBCapsule capsule = null; |
242 | |
ManageableCollection result = null; |
243 | |
|
244 | |
try |
245 | |
{ |
246 | |
capsule = new PBCapsule(odmg.getCurrentPBKey(), tx); |
247 | |
PersistenceBroker broker = capsule.getBroker(); |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
if (!(query instanceof ReportQuery)) |
253 | |
{ |
254 | |
result = broker.getCollectionByQuery(this.getCollectionClass(), query); |
255 | |
performLockingIfRequired(tx, broker, result); |
256 | |
} |
257 | |
else |
258 | |
{ |
259 | |
Iterator iter = null; |
260 | |
result = new ManageableArrayList(); |
261 | |
iter = broker.getReportQueryIteratorByQuery(query); |
262 | |
try |
263 | |
{ |
264 | |
while (iter.hasNext()) |
265 | |
{ |
266 | |
Object[] res = (Object[]) iter.next(); |
267 | |
|
268 | |
if (res.length == 1) |
269 | |
{ |
270 | |
if (res[0] != null) |
271 | |
{ |
272 | |
result.ojbAdd(res[0]); |
273 | |
} |
274 | |
} |
275 | |
else |
276 | |
{ |
277 | |
|
278 | |
for (int i = 0; i < res.length; i++) |
279 | |
{ |
280 | |
if (res[i] != null) |
281 | |
{ |
282 | |
result.ojbAdd(res); |
283 | |
break; |
284 | |
} |
285 | |
} |
286 | |
} |
287 | |
} |
288 | |
} |
289 | |
finally |
290 | |
{ |
291 | |
if (iter instanceof OJBIterator) |
292 | |
{ |
293 | |
((OJBIterator) iter).releaseDbResources(); |
294 | |
} |
295 | |
} |
296 | |
} |
297 | |
|
298 | |
ListIterator it = getBindIterator(); |
299 | |
while (it.hasPrevious()) |
300 | |
{ |
301 | |
it.previous(); |
302 | |
} |
303 | |
} |
304 | |
finally |
305 | |
{ |
306 | |
if (capsule != null) capsule.destroy(); |
307 | |
} |
308 | |
return result; |
309 | |
} |
310 | |
|
311 | |
|
312 | |
protected void performLockingIfRequired( |
313 | |
TransactionImpl tx, |
314 | |
PersistenceBroker broker, |
315 | |
ManageableCollection result) |
316 | |
{ |
317 | |
|
318 | |
|
319 | |
if ((tx != null) && tx.isImplicitLocking() && tx.isOpen()) |
320 | |
{ |
321 | |
|
322 | |
Iterator iter = result.ojbIterator(); |
323 | |
Object toBeLocked = null; |
324 | |
try |
325 | |
{ |
326 | |
List regList = tx.getRegistrationList(); |
327 | |
while (iter.hasNext()) |
328 | |
{ |
329 | |
toBeLocked = iter.next(); |
330 | |
RuntimeObject rt = new RuntimeObject(toBeLocked, tx, false); |
331 | |
tx.lockAndRegister(rt, Transaction.READ, true, regList); |
332 | |
} |
333 | |
} |
334 | |
finally |
335 | |
{ |
336 | |
tx.clearRegistrationList(); |
337 | |
} |
338 | |
} |
339 | |
} |
340 | |
|
341 | |
|
342 | |
protected OdmgConfiguration getConfiguration() |
343 | |
{ |
344 | |
OdmgConfiguration config = (OdmgConfiguration) PersistenceBrokerFactory.getConfigurator().getConfigurationFor(null); |
345 | |
return config; |
346 | |
} |
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
private void setQuery(Query query) |
354 | |
{ |
355 | |
this.query = query; |
356 | |
} |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
protected ListIterator getBindIterator() |
363 | |
{ |
364 | |
return bindIterator; |
365 | |
} |
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
private void setBindIterator(ListIterator bindIterator) |
372 | |
{ |
373 | |
this.bindIterator = bindIterator; |
374 | |
} |
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
public void configure(Configuration pConfig) throws ConfigurationException |
380 | |
{ |
381 | |
} |
382 | |
|
383 | |
public int fullSize() |
384 | |
{ |
385 | |
return this.query.fullSize(); |
386 | |
} |
387 | |
} |