01: /*
02: $Header: /cvsroot/xorm/xorm/src/org/xorm/query/AbstractQueryLanguage.java,v 1.2 2003/08/29 16:41:29 wbiggs Exp $
03:
04: This file is part of XORM.
05:
06: XORM is free software; you can redistribute it and/or modify
07: it under the terms of the GNU General Public License as published by
08: the Free Software Foundation; either version 2 of the License, or
09: (at your option) any later version.
10:
11: XORM is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU General Public License for more details.
15:
16: You should have received a copy of the GNU General Public License
17: along with XORM; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20: package org.xorm.query;
21:
22: import java.util.ArrayList;
23: import java.util.HashMap;
24: import java.util.LinkedHashMap;
25: import java.util.List;
26: import java.util.Set;
27:
28: /**
29: * Implementation of QueryLanguage interface that provides common methods
30: * to set and get variables and parameters.
31: */
32: public abstract class AbstractQueryLanguage implements QueryLanguage {
33: private static final QueryOrdering[] EXAMPLE_ORDERING_ARRAY = new QueryOrdering[0];
34:
35: protected Class clazz;
36: protected LinkedHashMap paramNameToType = new LinkedHashMap();
37: protected HashMap varNameToType = new HashMap();
38: private List ordering;
39:
40: public abstract Expression getExpression();
41:
42: public abstract void compile() throws QuerySyntaxException;
43:
44: public abstract void setFilter(Object filter);
45:
46: public void setClass(Class clazz) {
47: this .clazz = clazz;
48: }
49:
50: public Class getCandidateClass() {
51: return clazz;
52: }
53:
54: public void declareVariable(String name, Class type) {
55: varNameToType.put(name, type);
56: }
57:
58: public void declareParameter(String name, Class type) {
59: paramNameToType.put(name, type);
60: }
61:
62: /**
63: * Guaranteed to come out in the same order they went in.
64: */
65: public List getParameterNames() {
66: return new ArrayList(paramNameToType.keySet());
67: }
68:
69: public Class getParameterType(String name) {
70: return (Class) paramNameToType.get(name);
71: }
72:
73: public Set getVariableNames() {
74: return varNameToType.keySet();
75: }
76:
77: public Class getVariableType(String name) {
78: return (Class) varNameToType.get(name);
79: }
80:
81: public String toString() {
82: return getExpression().toString();
83: }
84:
85: public QueryOrdering[] getOrdering() {
86: if (ordering == null) {
87: return null;
88: }
89: return (QueryOrdering[]) ordering
90: .toArray(EXAMPLE_ORDERING_ARRAY);
91: }
92:
93: public void addOrdering(QueryOrdering ordering) {
94: if (this .ordering == null) {
95: this .ordering = new ArrayList();
96: }
97: this.ordering.add(ordering);
98: }
99: }
|