KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdo > QueryImpl


1 package org.apache.ojb.jdo;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import javax.jdo.Extent;
19 import javax.jdo.JDOUserException;
20 import javax.jdo.PersistenceManager;
21 import javax.jdo.Query;
22
23 import org.apache.ojb.jdo.jdoql.Expression;
24 import org.apache.ojb.jdo.jdoql.LocalVariable;
25 import org.apache.ojb.jdo.jdoql.QueryParsingHelper;
26 import org.apache.ojb.jdo.jdoql.QueryTreeResolver;
27
28 import java.util.*;
29
30 /**
31  * Not Really Functional Yet:
32  * <p>
33  * Consider making this a front end for a State system where the
34  * compiled query is a different State The big list of args should/could
35  * be collected into something that knows how to compile and and
36  * apply the arguments passed at execute time.
37  * <p>
38  * Consider also, if do above, clone returns compiled query. Compiled
39  * probably needs to be able to uncompile itself if this the case,
40  * so continuing to return uncompiled may be best
41  *
42  * @author <a HREF="mailto:mattbaird@yahoo.com">Matthew Baird</a>
43  * @author <a HREF="mailto:brianm@apache.org">Brian McCallister</a>
44  * @author <a HREF="mailto:tomdz@apache.org">Thomas Dudziak</a>
45  */

46
47 public class QueryImpl implements Query
48 {
49     /** The persistence manager this query is bound to */
50     private PersistenceManagerImpl _persistenceManager;
51     /** The searched class */
52     private Class JavaDoc _searchedClass;
53     /** Candidate instances */
54     private Collection _candidateInstances;
55     /** The original filter string */
56     private String JavaDoc _filterString;
57     /** The parsed filter expression */
58     private Expression _filterExpression;
59     /** The original imports string */
60     private String JavaDoc _importString;
61     /** The imports */
62     private Collection _imports;
63     /** The original parameter string */
64     private String JavaDoc _parameterString;
65     /** The parameters */
66     private Map _parameters;
67     /** The original variables string */
68     private String JavaDoc _variableString;
69     /** The variables */
70     private Map _variables;
71     /** The original ordering string */
72     private String JavaDoc _orderingString;
73     /** The orderings */
74     private Collection _orderings;
75     /** Whether to ignore the cache while processing this query */
76     private boolean _ignoreCache;
77     /** Whether this query must be resolved and compiled first */
78     private boolean _needsCompilation = true;
79
80     /**
81      * Creates a new query that uses the given persistence manager.
82      *
83      * @param pm The persistence manager to use
84      */

85     public QueryImpl(PersistenceManagerImpl pm)
86     {
87         _persistenceManager = pm;
88         _candidateInstances = null;
89     }
90
91     /**
92      * Returns the persistence manager that this query uses.
93      *
94      * @return The persistence manager
95      */

96     public PersistenceManager getPersistenceManager()
97     {
98         return _persistenceManager;
99     }
100
101     /**
102      * Sets the class whose objects this query searches for.
103      *
104      * @param searchedClass The class of the searched objects
105      */

106     public void setClass(Class JavaDoc searchedClass)
107     {
108         _searchedClass = searchedClass;
109         _needsCompilation = true;
110     }
111
112     /**
113      * Returns the class of the searched objects.
114      *
115      * @return The class of the searched objects
116      */

117     public Class JavaDoc getSearchedClass()
118     {
119         return _searchedClass;
120     }
121     
122     /**
123      * Simply ovewrites the setClass(..) value
124      */

125     public void setCandidates(Extent extent)
126     {
127         _searchedClass = ((ExtentImpl)extent).ojbGetClass();
128         _needsCompilation = true;
129     }
130
131     public void setCandidates(Collection candidates)
132     {
133         _candidateInstances = candidates;
134         _needsCompilation = true;
135     }
136
137     /**
138      * Sets the filter of this query.
139      *
140      * @param filter The filter expression
141      */

142     public void setFilter(String JavaDoc filter) throws JDOUserException
143     {
144         _filterString = filter;
145         _filterExpression = new QueryParsingHelper().parseFilter(filter);
146         _needsCompilation = true;
147     }
148
149     /**
150      * Returns the filter expression.
151      *
152      * @return The filter expression
153      */

154     public Expression getFilterExpression()
155     {
156         return _filterExpression;
157     }
158
159     /**
160      * Specifies the classes/packages imported by this query for use in the filter
161      * and ordering expressions.
162      *
163      * @param imports The import declarations
164      */

165     public void declareImports(String JavaDoc imports) throws JDOUserException
166     {
167         _importString = imports;
168         _imports = new QueryParsingHelper().parseImports(imports);
169         _needsCompilation = true;
170     }
171
172     /**
173      * Returns the imports of this query.
174      *
175      * @return The imports, a collection of {@link org.apache.ojb.jdo.jdoql.Import} objects
176      */

177     public Collection getImports()
178     {
179         return _imports;
180     }
181
182     /**
183      * Sets the parameters of this query.
184      *
185      * @param params The parameter declarations
186      */

187     public void declareParameters(String JavaDoc params) throws JDOUserException
188     {
189         _parameterString = params;
190         _parameters = new QueryParsingHelper().parseParameters(params);
191         _needsCompilation = true;
192     }
193
194     /**
195      * Returns the parameters of this query.
196      *
197      * @return The parameters, a map of {@link org.apache.ojb.jdo.jdoql.LocalVariable} objects
198      * indexed by their names
199      */

200     public Map getParameters()
201     {
202         return _parameters;
203     }
204
205     /**
206      * Returns the parameter of the given name if it exists.
207      *
208      * @param name The parameter name
209      * @return The parameter
210      */

211     public LocalVariable getParameter(String JavaDoc name)
212     {
213         return (LocalVariable)_variables.get(name);
214     }
215
216     /**
217      * Declares the variables used in the filter expression of this query.
218      *
219      * @param variables The variable declarations
220      */

221     public void declareVariables(String JavaDoc variables) throws JDOUserException
222     {
223         _variableString = variables;
224         _variables = new QueryParsingHelper().parseVariables(variables);
225         _needsCompilation = true;
226     }
227
228     /**
229      * Returns the variables of this query.
230      *
231      * @return The variables, a map of {@link org.apache.ojb.jdo.jdoql.LocalVariable} objects
232      * indexed by their names
233      */

234     public Map getVariables()
235     {
236         return _variables;
237     }
238
239     /**
240      * Returns the variable of the given name if it exists.
241      *
242      * @param name The variable name
243      * @return The variable
244      */

245     public LocalVariable getVariable(String JavaDoc name)
246     {
247         return (LocalVariable)_variables.get(name);
248     }
249     
250     /**
251      * Defines the ordering of this query.
252      *
253      * @param orderings The ordering specifications
254      */

255     public void setOrdering(String JavaDoc orderings) throws JDOUserException
256     {
257         _orderingString = orderings;
258         _orderings = new QueryParsingHelper().parseOrderings(orderings);
259         _needsCompilation = true;
260     }
261
262     /**
263      * Returns the orderings of this query.
264      *
265      * @return The orderings, a collection of {@link org.apache.ojb.jdo.jdoql.Ordering} objects
266      */

267     public Collection getOrderings()
268     {
269         return _orderings;
270     }
271
272     /**
273      * Specifies whether the query should ignore any objects in the cache.
274      *
275      * @param shouldIgnoreCache Whether to ignore the cache
276      */

277     public void setIgnoreCache(boolean shouldIgnoreCache)
278     {
279         _ignoreCache = shouldIgnoreCache;
280     }
281
282     /**
283      * Determines whether this query ignores objects in the cache.
284      *
285      * @return <code>true</code> if this query ignores cached objects
286      */

287     public boolean getIgnoreCache()
288     {
289         return _ignoreCache;
290     }
291
292     /**
293      * Compiles the query. In effect this resolves the various expressions and
294      * declarations against each other, checks that they are valid, and enhances
295      * the filter and ordering expressions with executable ojb queries.
296      */

297     public void compile()
298     {
299         if (_needsCompilation)
300         {
301             // first we resolve this query
302
new QueryTreeResolver().resolveAndCheck(this);
303             // TODO: next the filter and ordering expressions are enhanced with
304
// actual database queries, e.g. like this:
305
// new QueryCompiler().compile(this);
306
// which adds ojb queries to the filter expressions
307
// (including the ordering)
308
throw new UnsupportedOperationException JavaDoc("Not yet implemented");
309
310             // _needsCompilation = false;
311
}
312     }
313
314     /**
315      * Performs this query.
316      *
317      * @return The query result
318      */

319     public Object JavaDoc execute()
320     {
321         if (_needsCompilation)
322         {
323             compile();
324         }
325         throw new UnsupportedOperationException JavaDoc("Not yet implemented");
326     }
327
328     /**
329      * @todo implement
330      */

331     public Object JavaDoc execute(Object JavaDoc o)
332     {
333         throw new UnsupportedOperationException JavaDoc("Not yet implemented!");
334     }
335
336     public Object JavaDoc execute(Object JavaDoc o, Object JavaDoc o1)
337     {
338         throw new UnsupportedOperationException JavaDoc("Not yet implemented!");
339     }
340
341     public Object JavaDoc execute(Object JavaDoc o, Object JavaDoc o1, Object JavaDoc o2)
342     {
343         throw new UnsupportedOperationException JavaDoc("Not yet implemented!");
344     }
345
346     public Object JavaDoc executeWithMap(Map map)
347     {
348         throw new UnsupportedOperationException JavaDoc("Not yet implemented!");
349     }
350
351     public Object JavaDoc executeWithArray(Object JavaDoc[] objects)
352     {
353         throw new UnsupportedOperationException JavaDoc("Not yet implemented!");
354     }
355
356     /**
357      * @param o is a Collection returned from execute()
358      */

359     public void close(Object JavaDoc o)
360     {
361     }
362
363     public void closeAll()
364     {
365     }
366
367     /**
368      * Creates an uncompiled deep clone of this query.
369      *
370      * @return The clone
371      */

372     QueryImpl ojbClone()
373     {
374         QueryImpl query = new QueryImpl(_persistenceManager);
375
376         query.setClass(_searchedClass);
377         query.setCandidates(_candidateInstances);
378         query.declareImports(_importString);
379         query.declareParameters(_parameterString);
380         query.declareVariables(_variableString);
381         query.setFilter(_filterString);
382         query.setOrdering(_orderingString);
383
384         return query;
385     }
386
387 }
388
Popular Tags