001: package org.apache.ojb.jdo;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import javax.jdo.Extent;
019: import javax.jdo.JDOUserException;
020: import javax.jdo.PersistenceManager;
021: import javax.jdo.Query;
022:
023: import org.apache.ojb.jdo.jdoql.Expression;
024: import org.apache.ojb.jdo.jdoql.LocalVariable;
025: import org.apache.ojb.jdo.jdoql.QueryParsingHelper;
026: import org.apache.ojb.jdo.jdoql.QueryTreeResolver;
027:
028: import java.util.*;
029:
030: /**
031: * Not Really Functional Yet:
032: * <p>
033: * Consider making this a front end for a State system where the
034: * compiled query is a different State The big list of args should/could
035: * be collected into something that knows how to compile and and
036: * apply the arguments passed at execute time.
037: * <p>
038: * Consider also, if do above, clone returns compiled query. Compiled
039: * probably needs to be able to uncompile itself if this the case,
040: * so continuing to return uncompiled may be best
041: *
042: * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
043: * @author <a href="mailto:brianm@apache.org">Brian McCallister</a>
044: * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
045: */
046:
047: public class QueryImpl implements Query {
048: /** The persistence manager this query is bound to */
049: private PersistenceManagerImpl _persistenceManager;
050: /** The searched class */
051: private Class _searchedClass;
052: /** Candidate instances */
053: private Collection _candidateInstances;
054: /** The original filter string */
055: private String _filterString;
056: /** The parsed filter expression */
057: private Expression _filterExpression;
058: /** The original imports string */
059: private String _importString;
060: /** The imports */
061: private Collection _imports;
062: /** The original parameter string */
063: private String _parameterString;
064: /** The parameters */
065: private Map _parameters;
066: /** The original variables string */
067: private String _variableString;
068: /** The variables */
069: private Map _variables;
070: /** The original ordering string */
071: private String _orderingString;
072: /** The orderings */
073: private Collection _orderings;
074: /** Whether to ignore the cache while processing this query */
075: private boolean _ignoreCache;
076: /** Whether this query must be resolved and compiled first */
077: private boolean _needsCompilation = true;
078:
079: /**
080: * Creates a new query that uses the given persistence manager.
081: *
082: * @param pm The persistence manager to use
083: */
084: public QueryImpl(PersistenceManagerImpl pm) {
085: _persistenceManager = pm;
086: _candidateInstances = null;
087: }
088:
089: /**
090: * Returns the persistence manager that this query uses.
091: *
092: * @return The persistence manager
093: */
094: public PersistenceManager getPersistenceManager() {
095: return _persistenceManager;
096: }
097:
098: /**
099: * Sets the class whose objects this query searches for.
100: *
101: * @param searchedClass The class of the searched objects
102: */
103: public void setClass(Class searchedClass) {
104: _searchedClass = searchedClass;
105: _needsCompilation = true;
106: }
107:
108: /**
109: * Returns the class of the searched objects.
110: *
111: * @return The class of the searched objects
112: */
113: public Class getSearchedClass() {
114: return _searchedClass;
115: }
116:
117: /**
118: * Simply ovewrites the setClass(..) value
119: */
120: public void setCandidates(Extent extent) {
121: _searchedClass = ((ExtentImpl) extent).ojbGetClass();
122: _needsCompilation = true;
123: }
124:
125: public void setCandidates(Collection candidates) {
126: _candidateInstances = candidates;
127: _needsCompilation = true;
128: }
129:
130: /**
131: * Sets the filter of this query.
132: *
133: * @param filter The filter expression
134: */
135: public void setFilter(String filter) throws JDOUserException {
136: _filterString = filter;
137: _filterExpression = new QueryParsingHelper()
138: .parseFilter(filter);
139: _needsCompilation = true;
140: }
141:
142: /**
143: * Returns the filter expression.
144: *
145: * @return The filter expression
146: */
147: public Expression getFilterExpression() {
148: return _filterExpression;
149: }
150:
151: /**
152: * Specifies the classes/packages imported by this query for use in the filter
153: * and ordering expressions.
154: *
155: * @param imports The import declarations
156: */
157: public void declareImports(String imports) throws JDOUserException {
158: _importString = imports;
159: _imports = new QueryParsingHelper().parseImports(imports);
160: _needsCompilation = true;
161: }
162:
163: /**
164: * Returns the imports of this query.
165: *
166: * @return The imports, a collection of {@link org.apache.ojb.jdo.jdoql.Import} objects
167: */
168: public Collection getImports() {
169: return _imports;
170: }
171:
172: /**
173: * Sets the parameters of this query.
174: *
175: * @param params The parameter declarations
176: */
177: public void declareParameters(String params)
178: throws JDOUserException {
179: _parameterString = params;
180: _parameters = new QueryParsingHelper().parseParameters(params);
181: _needsCompilation = true;
182: }
183:
184: /**
185: * Returns the parameters of this query.
186: *
187: * @return The parameters, a map of {@link org.apache.ojb.jdo.jdoql.LocalVariable} objects
188: * indexed by their names
189: */
190: public Map getParameters() {
191: return _parameters;
192: }
193:
194: /**
195: * Returns the parameter of the given name if it exists.
196: *
197: * @param name The parameter name
198: * @return The parameter
199: */
200: public LocalVariable getParameter(String name) {
201: return (LocalVariable) _variables.get(name);
202: }
203:
204: /**
205: * Declares the variables used in the filter expression of this query.
206: *
207: * @param variables The variable declarations
208: */
209: public void declareVariables(String variables)
210: throws JDOUserException {
211: _variableString = variables;
212: _variables = new QueryParsingHelper().parseVariables(variables);
213: _needsCompilation = true;
214: }
215:
216: /**
217: * Returns the variables of this query.
218: *
219: * @return The variables, a map of {@link org.apache.ojb.jdo.jdoql.LocalVariable} objects
220: * indexed by their names
221: */
222: public Map getVariables() {
223: return _variables;
224: }
225:
226: /**
227: * Returns the variable of the given name if it exists.
228: *
229: * @param name The variable name
230: * @return The variable
231: */
232: public LocalVariable getVariable(String name) {
233: return (LocalVariable) _variables.get(name);
234: }
235:
236: /**
237: * Defines the ordering of this query.
238: *
239: * @param orderings The ordering specifications
240: */
241: public void setOrdering(String orderings) throws JDOUserException {
242: _orderingString = orderings;
243: _orderings = new QueryParsingHelper().parseOrderings(orderings);
244: _needsCompilation = true;
245: }
246:
247: /**
248: * Returns the orderings of this query.
249: *
250: * @return The orderings, a collection of {@link org.apache.ojb.jdo.jdoql.Ordering} objects
251: */
252: public Collection getOrderings() {
253: return _orderings;
254: }
255:
256: /**
257: * Specifies whether the query should ignore any objects in the cache.
258: *
259: * @param shouldIgnoreCache Whether to ignore the cache
260: */
261: public void setIgnoreCache(boolean shouldIgnoreCache) {
262: _ignoreCache = shouldIgnoreCache;
263: }
264:
265: /**
266: * Determines whether this query ignores objects in the cache.
267: *
268: * @return <code>true</code> if this query ignores cached objects
269: */
270: public boolean getIgnoreCache() {
271: return _ignoreCache;
272: }
273:
274: /**
275: * Compiles the query. In effect this resolves the various expressions and
276: * declarations against each other, checks that they are valid, and enhances
277: * the filter and ordering expressions with executable ojb queries.
278: */
279: public void compile() {
280: if (_needsCompilation) {
281: // first we resolve this query
282: new QueryTreeResolver().resolveAndCheck(this );
283: // TODO: next the filter and ordering expressions are enhanced with
284: // actual database queries, e.g. like this:
285: // new QueryCompiler().compile(this);
286: // which adds ojb queries to the filter expressions
287: // (including the ordering)
288: throw new UnsupportedOperationException(
289: "Not yet implemented");
290:
291: // _needsCompilation = false;
292: }
293: }
294:
295: /**
296: * Performs this query.
297: *
298: * @return The query result
299: */
300: public Object execute() {
301: if (_needsCompilation) {
302: compile();
303: }
304: throw new UnsupportedOperationException("Not yet implemented");
305: }
306:
307: /**
308: * @todo implement
309: */
310: public Object execute(Object o) {
311: throw new UnsupportedOperationException("Not yet implemented!");
312: }
313:
314: public Object execute(Object o, Object o1) {
315: throw new UnsupportedOperationException("Not yet implemented!");
316: }
317:
318: public Object execute(Object o, Object o1, Object o2) {
319: throw new UnsupportedOperationException("Not yet implemented!");
320: }
321:
322: public Object executeWithMap(Map map) {
323: throw new UnsupportedOperationException("Not yet implemented!");
324: }
325:
326: public Object executeWithArray(Object[] objects) {
327: throw new UnsupportedOperationException("Not yet implemented!");
328: }
329:
330: /**
331: * @param o is a Collection returned from execute()
332: */
333: public void close(Object o) {
334: }
335:
336: public void closeAll() {
337: }
338:
339: /**
340: * Creates an uncompiled deep clone of this query.
341: *
342: * @return The clone
343: */
344: QueryImpl ojbClone() {
345: QueryImpl query = new QueryImpl(_persistenceManager);
346:
347: query.setClass(_searchedClass);
348: query.setCandidates(_candidateInstances);
349: query.declareImports(_importString);
350: query.declareParameters(_parameterString);
351: query.declareVariables(_variableString);
352: query.setFilter(_filterString);
353: query.setOrdering(_orderingString);
354:
355: return query;
356: }
357:
358: }
|