001: /**********************************************************************
002: Copyright (c) 2007 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.jdo;
018:
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import javax.jdo.Extent;
024: import javax.jdo.FetchPlan;
025: import javax.jdo.PersistenceManager;
026: import javax.jdo.Query;
027:
028: import org.jpox.exceptions.JPOXException;
029: import org.jpox.store.query.NoQueryResultsException;
030:
031: /**
032: * Wrapper for JDO Query class.
033: * Stores the PM the query is exected against, the internal JPOX query, and the query language.
034: * The language is stored since it is referenced by the JDO API and so we dont have to embody knowledge
035: * of which internal query type is for which language (could be moved to the internal query).
036: *
037: * @version $Revision: 1.4 $
038: */
039: public class JDOQuery implements Query {
040: /** PersistenceManager for the query. */
041: transient PersistenceManager pm;
042:
043: /** Underlying query that will be executed by JPOX. */
044: org.jpox.store.query.Query query;
045:
046: /** Query language. */
047: String language;
048:
049: /** JDO Fetch Plan. */
050: JDOFetchPlan fetchPlan = null;
051:
052: /**
053: * Constructor for a query used by JDO.
054: * @param pm PersistenceManager
055: * @param query Underlying query
056: * @param language Query language
057: */
058: public JDOQuery(PersistenceManager pm,
059: org.jpox.store.query.Query query, String language) {
060: this .pm = pm;
061: this .query = query;
062: this .language = language;
063: }
064:
065: /**
066: * Close the query result.
067: * @param queryResult Query result
068: */
069: public void close(Object queryResult) {
070: query.close(queryResult);
071: }
072:
073: /**
074: * Close all query results for this query.
075: */
076: public void closeAll() {
077: query.closeAll();
078: }
079:
080: /**
081: * Compile the query.
082: */
083: public void compile() {
084: try {
085: query.compile();
086: } catch (JPOXException jpe) {
087: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
088: }
089: }
090:
091: /**
092: * Declare any imports for the query.
093: * @param imports The imports
094: */
095: public void declareImports(String imports) {
096: try {
097: query.declareImports(imports);
098: } catch (JPOXException jpe) {
099: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
100: }
101: }
102:
103: /**
104: * Declare any parameters for the query.
105: * @param parameters The parameters
106: */
107: public void declareParameters(String parameters) {
108: try {
109: query.declareExplicitParameters(parameters);
110: } catch (JPOXException jpe) {
111: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
112: }
113: }
114:
115: /**
116: * Declare any variables for the query.
117: * @param variables The variables
118: */
119: public void declareVariables(String variables) {
120: try {
121: query.declareExplicitVariables(variables);
122: } catch (JPOXException jpe) {
123: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
124: }
125: }
126:
127: /**
128: * Execute the query deleting all instances found.
129: * @return Number of deleted instances
130: */
131: public long deletePersistentAll() {
132: try {
133: return query.deletePersistentAll();
134: } catch (NoQueryResultsException nqre) {
135: return 0;
136: } catch (JPOXException jpe) {
137: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
138: }
139: }
140:
141: /**
142: * Execute the query deleting all instances found.
143: * @param parameters Parameters to use when executing
144: * @return Number of deleted instances
145: */
146: public long deletePersistentAll(Object[] parameters) {
147: try {
148: return query.deletePersistentAll(parameters);
149: } catch (NoQueryResultsException nqre) {
150: return 0;
151: } catch (JPOXException jpe) {
152: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
153: }
154: }
155:
156: /**
157: * Execute the query deleting all instances found.
158: * @param parameters Parameters to use when executing
159: * @return Number of deleted instances
160: */
161: public long deletePersistentAll(Map parameters) {
162: try {
163: return query.deletePersistentAll(parameters);
164: } catch (NoQueryResultsException nqre) {
165: return 0;
166: } catch (JPOXException jpe) {
167: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
168: }
169: }
170:
171: /**
172: * Execute the query.
173: * @return The results
174: */
175: public Object execute() {
176: try {
177: return query.execute();
178: } catch (NoQueryResultsException nqre) {
179: return null;
180: } catch (JPOXException jpe) {
181: // Convert any JPOX exceptions into what JDO expects
182: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
183: }
184: }
185:
186: /**
187: * Execute the query.
188: * @param p1 First param value
189: * @return The results
190: */
191: public Object execute(Object p1) {
192: try {
193: return query.execute(p1);
194: } catch (NoQueryResultsException nqre) {
195: return null;
196: } catch (JPOXException jpe) {
197: // Convert any JPOX exceptions into what JDO expects
198: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
199: }
200: }
201:
202: /**
203: * Execute the query.
204: * @param p1 First param value
205: * @param p2 Second param value
206: * @return The results
207: */
208: public Object execute(Object p1, Object p2) {
209: try {
210: return query.execute(p1, p2);
211: } catch (NoQueryResultsException nqre) {
212: return null;
213: } catch (JPOXException jpe) {
214: // Convert any JPOX exceptions into what JDO expects
215: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
216: }
217: }
218:
219: /**
220: * Execute the query.
221: * @param p1 First param value
222: * @param p2 Second param value
223: * @param p3 Third param value
224: * @return The results
225: */
226: public Object execute(Object p1, Object p2, Object p3) {
227: try {
228: return query.execute(p1, p2, p3);
229: } catch (NoQueryResultsException nqre) {
230: return null;
231: } catch (JPOXException jpe) {
232: // Convert any JPOX exceptions into what JDO expects
233: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
234: }
235: }
236:
237: /**
238: * Execute the query.
239: * @param parameterValues Param values
240: * @return The results
241: */
242: public Object executeWithArray(Object[] parameterValues) {
243: try {
244: return query.executeWithArray(parameterValues);
245: } catch (NoQueryResultsException nqre) {
246: return null;
247: } catch (JPOXException jpe) {
248: // Convert any JPOX exceptions into what JDO expects
249: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
250: }
251: }
252:
253: /**
254: * Execute the query.
255: * @param parameters Param values
256: * @return The results
257: */
258: public Object executeWithMap(Map parameters) {
259: try {
260: return query.executeWithMap(parameters);
261: } catch (NoQueryResultsException nqre) {
262: return null;
263: } catch (JPOXException jpe) {
264: // Convert any JPOX exceptions into what JDO expects
265: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
266: }
267:
268: }
269:
270: /**
271: * Set the candidates for the query.
272: * @param extent Extent defining the candidates
273: */
274: public void setCandidates(Extent extent) {
275: try {
276: if (extent == null) {
277: query.setCandidates((org.jpox.store.Extent) null);
278: } else {
279: query.setCandidates(((JDOExtent) extent).getExtent());
280: }
281: } catch (JPOXException jpe) {
282: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
283: }
284: }
285:
286: /**
287: * Set the candidates for the query.
288: * @param pcs PC candidates
289: */
290: public void setCandidates(Collection pcs) {
291: try {
292: query.setCandidates(pcs);
293: } catch (JPOXException jpe) {
294: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
295: }
296: }
297:
298: /**
299: * Set the candidate class for the query.
300: * @param candidateClass Candidate class
301: */
302: public void setClass(Class candidateClass) {
303: try {
304: query.setClass(candidateClass);
305: } catch (JPOXException jpe) {
306: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
307: }
308: }
309:
310: /**
311: * Method to add an extension to the query.
312: * @param key Key for the extension
313: * @param value Value for the extension
314: */
315: public void addExtension(String key, Object value) {
316: query.addExtension(key, value);
317: }
318:
319: /**
320: * Set the extensions for the query.
321: * @param extensions The extensions
322: */
323: public void setExtensions(Map extensions) {
324: query.setExtensions(extensions);
325: }
326:
327: /**
328: * Accessor for the fetch plan to use.
329: * @return The fetch plan
330: */
331: public FetchPlan getFetchPlan() {
332: if (fetchPlan == null) {
333: // Not yet assigned so give a JDO wrapper to the underlying FetchPlan
334: fetchPlan = new JDOFetchPlan(query.getFetchPlan());
335: }
336: return fetchPlan;
337: }
338:
339: /**
340: * Set the filter for the query.
341: * @param filter The query filter
342: */
343: public void setFilter(String filter) {
344: try {
345: query.setFilter(filter);
346: } catch (JPOXException jpe) {
347: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
348: }
349: }
350:
351: /**
352: * Set the grouping for the query.
353: * @param grouping The grouping
354: */
355: public void setGrouping(String grouping) {
356: try {
357: query.setGrouping(grouping);
358: } catch (JPOXException jpe) {
359: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
360: }
361: }
362:
363: /**
364: * Accessor for the ignore cache setting
365: * @return Ignore cache
366: */
367: public boolean getIgnoreCache() {
368: return query.getIgnoreCache();
369: }
370:
371: /**
372: * Set the ignore cache setting for the query.
373: * @param ignoreCache The ignore cache setting
374: */
375: public void setIgnoreCache(boolean ignoreCache) {
376: query.setIgnoreCache(ignoreCache);
377: }
378:
379: /**
380: * Set the ordering for the query.
381: * @param ordering The ordering
382: */
383: public void setOrdering(String ordering) {
384: try {
385: query.setOrdering(ordering);
386: } catch (JPOXException jpe) {
387: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
388: }
389: }
390:
391: /**
392: * Accessor for the PersistenceManager.
393: * @return PM
394: */
395: public PersistenceManager getPersistenceManager() {
396: return pm;
397: }
398:
399: /**
400: * Set the range for the query.
401: * @param range The range specification
402: */
403: public void setRange(String range) {
404: try {
405: query.setRange(range);
406: } catch (JPOXException jpe) {
407: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
408: }
409: }
410:
411: /**
412: * Set the range for the query.
413: * @param fromIncl From range inclusive
414: * @param toExcl To range exclusive
415: */
416: public void setRange(long fromIncl, long toExcl) {
417: try {
418: query.setRange(fromIncl, toExcl);
419: } catch (JPOXException jpe) {
420: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
421: }
422: }
423:
424: /**
425: * Set the result for the query.
426: * @param result Result clause
427: */
428: public void setResult(String result) {
429: try {
430: query.setResult(result);
431: } catch (JPOXException jpe) {
432: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
433: }
434: }
435:
436: /**
437: * Set the result class for the query.
438: * @param result_cls Result class
439: */
440: public void setResultClass(Class result_cls) {
441: try {
442: query.setResultClass(result_cls);
443: } catch (JPOXException jpe) {
444: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
445: }
446: }
447:
448: /**
449: * Set whether to expect a unique result.
450: * @param unique Whether results are unique
451: */
452: public void setUnique(boolean unique) {
453: try {
454: query.setUnique(unique);
455: } catch (JPOXException jpe) {
456: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
457: }
458: }
459:
460: /**
461: * Accessor for whether the query is modifiable.
462: * @return Whether it is modifiable
463: */
464: public boolean isUnmodifiable() {
465: return query.isUnmodifiable();
466: }
467:
468: /**
469: * Set the query to be unmodifiable.
470: */
471: public void setUnmodifiable() {
472: query.setUnmodifiable();
473: }
474:
475: /**
476: * Add a subquery to this query.
477: * @param sub the subquery to add to this Query
478: * @param variableDecl the name of the variable in the outer query to bind the results of the subquery
479: * @param candidateExpr the candidate collection of the subquery as an expression using terms of the outer query
480: * @since 1.2
481: */
482: public void addSubquery(Query sub, String variableDecl,
483: String candidateExpr) {
484: addSubquery(sub, variableDecl, candidateExpr, (Map) null);
485: }
486:
487: /**
488: * Add a subquery to this query.
489: * The String version of the method binds the named expression to the parameter implictly or explicitly
490: * declared in the subquery.
491: * @param sub the subquery to add to this Query
492: * @param variableDecl the name of the variable to be used in this Query
493: * @param candidateExpr the candidate collection to apply to the subquery
494: * @param parameter the expression from the outer query to bind the parameter in the subquery
495: * @since 1.2
496: */
497: public void addSubquery(Query sub, String variableDecl,
498: String candidateExpr, String parameter) {
499: Map paramMap = new HashMap();
500: if (parameter != null) {
501: paramMap.put("JPOX_0", parameter);
502: }
503: addSubquery(sub, variableDecl, candidateExpr, paramMap);
504: }
505:
506: /**
507: * Add a subquery to this query.
508: * The String version of the method binds the named expression to the parameter implictly or explicitly
509: * declared in the subquery.
510: * @param sub the subquery to add to this Query
511: * @param variableDecl the name of the variable to be used in this Query
512: * @param candidateExpr the candidate collection to apply to the subquery
513: * @param parameters the expressions from the outer query to bind the parameter in the subquery
514: * @since 1.2
515: */
516: public void addSubquery(Query sub, String variableDecl,
517: String candidateExpr, String[] parameters) {
518: Map paramMap = new HashMap();
519: if (parameters != null) {
520: for (int i = 0; i < parameters.length; i++) {
521: paramMap.put("JPOX_" + i, parameters[i]);
522: }
523: }
524: addSubquery(sub, variableDecl, candidateExpr, paramMap);
525: }
526:
527: /**
528: * Add a subquery to this query.
529: * The String version of the method binds the named expression to the parameter implictly or explicitly
530: * declared in the subquery.
531: * @param sub the subquery to add to this Query
532: * @param variableDecl the name of the variable to be used in this Query
533: * @param candidateExpr the candidate collection to apply to the subquery
534: * @param parameters the expressions from the outer query to bind the parameter in the subquery
535: * @since 1.2
536: */
537: public void addSubquery(Query sub, String variableDecl,
538: String candidateExpr, Map parameters) {
539: try {
540: org.jpox.store.query.Query subquery = null;
541: if (sub != null) {
542: subquery = ((JDOQuery) sub).query;
543: }
544: query.addSubquery(subquery, variableDecl, candidateExpr,
545: parameters);
546: } catch (JPOXException jpe) {
547: throw JPOXJDOHelper.getJDOExceptionForJPOXException(jpe);
548: }
549: }
550:
551: /**
552: * Accessor for the internal query.
553: * @return Internal query
554: */
555: public org.jpox.store.query.Query getInternalQuery() {
556: return query;
557: }
558:
559: /**
560: * Accessor for the query language.
561: * @return Query language
562: */
563: public String getLanguage() {
564: return language;
565: }
566:
567: /**
568: * Convenience method to return the query in string form.
569: * @return Stringifier method
570: */
571: public String toString() {
572: return query.toString();
573: }
574: }
|