KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > query > EJBQuery


1 /*
2  * Created on 13-Jul-2005
3  *
4  */

5 package com.jofti.query;
6
7
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import com.jofti.api.IndexQuery;
12 import com.jofti.core.QueryId;
13 import com.jofti.core.QueryType;
14
15 /**
16  *
17  *
18  * Basic usage is:<br> "select ${identifier} from ${classname} AS ${identifier} where ${identifier}.${propertyname} [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter]"
19  *
20  * <p></p>
21  * multiproperty queries are of the form<br>
22  * "select ${identifier} from ${classname} AS ${identifier} where ${identifier}.${propertyname} [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter] [and,or] ${identifier}.${propertyname}
23  * [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter]"
24  * <p>
25  * compound queries are supported as ${query} [and,or] ${query}
26  * <p>
27  * ordering for compound queries is achieved through use of bracket groups<br>
28   * "... where ( ${identifier}.${propertyname} [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter] [and,or] ${identifier}.${propertyname} [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter] )
29   * [and,or] ${identifier}.${propertyname} [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter] "
30   <p>
31  *
32  * multiclass queries are of the form
33  * <p>
34  * select ${identifierA}, ${identifierB} From ${classname} AS ${identifierA}, ${classname} AS ${identifierB} where ${identifierA}.${propertyname} [=,<,>,!=,>=,<=] 'value' [and.or] ${identifierB}.${propertyname} [=,<,>,!=,>=,<=] 'value'"
35  * <p>
36  *
37  * All values are created using a reflective String constructor .So if you want to Compare an Integer(22) you just specify
38  * value=22 - the value type is chosen based on the declared type of the field.
39  * <p>
40  * For primitives, primitive wrappers or some natively comparable classes in the JVM the propertyname is always the literal string 'VALUE' (case insensitive)
41  * * <ul>
42  * <li>java.lang.String.class,
43             <li>java.lang.Integer.class,
44             <li>java.lang.Double.class,
45             <li>java.lang.Float.class,
46             <li>java.lang.Long.class,
47             <li>java.lang.Short.class,
48             <li>java.math.BigInteger.class,
49             <li>java.math.BigDecimal.class,
50             <li>java.util.Date.class,
51             <li>java.net.URI.class
52             <li>java.lang.Byte.class
53             <li>java.lang.Character.class
54             <li>java.sql.Timestamp.class
55             </ul>
56  * <p>
57  * For example to query an integer you would write
58  * <p>
59  * select i from java.lang.Integer i where i.VALUE = 20
60  * <p>
61  * or <br>
62  * select i from java.lang.Integer as i where i.VALUE =20
63  * <p>
64  *
65  * Dates are constructed using the default encoding of the JVM. So a UK encoding would be:
66  * <p>
67  * select d from java.util.Date d where d.value <='4/10/1901 00:00:00'
68  * <p>
69  * For user defined classes in order to query the property type must have a String constructor and must
70  * implement equals and hashCode correctly.
71  *
72  * <p>
73  *
74  * For those classes that do not support String constructors or if you wish to use already existing values then you can use either
75  * the Named parameter or Positional parameter format.
76  * </p>
77  *
78  * <p>
79  * For instance to query for an integer as above we can use:<br>
80  * new EJBQuery("select i from java.lang.Integer as i where i.value =:intVal").setParameter("intVal", new Integer(20));
81  * <br>
82  * or<br>
83  * new EJBQuery("select i from java.lang.Integer as i where i.value =?1").setParameter(1, new Integer(20));
84  * </p>
85  *
86  *
87  * @author Steve Woodcock
88  * @version 1.0
89  */

90 public class EJBQuery implements IndexQuery, QueryId {
91
92     String JavaDoc query = null;
93     
94     int hashCode =0;
95     Map JavaDoc parameterMap = new HashMap JavaDoc();
96     
97     int maxResults;
98     int firstResult;
99     
100     static final QueryType QUERY_ID=QueryType.UNPARSED_QUERY;
101     
102     private static final String JavaDoc TERMINATOR =";";
103     
104     public EJBQuery(String JavaDoc query){
105         this.query = query + TERMINATOR;
106     }
107     
108     public String JavaDoc getQuery(){
109         return this.query;
110     }
111     
112
113     public QueryType getQueryType()
114     {
115         
116         return QUERY_ID;
117     }
118     
119     /**
120      * Sets a parameter that must match a named parameter in the query string.<br>
121      * The nameParameter in the Query is in the form :name and the parameter is set as "name"
122      * without the leading ':'. The value can be any object but its type must be assignable to the parameter
123      * type using normal Java assignability rules. So if the parameter is a String we cannot set an Integer
124      * and expect the type to be correctly coerced.<p>
125      * @param name - the name of the parameter
126      * @param value - the value the query should compare against
127      * @return - itself to allow method chaining.
128      */

129     public IndexQuery setParameter(String JavaDoc name, Object JavaDoc value){
130         parameterMap.put(name, value);
131         return this;
132     }
133     
134     
135     /**
136      * Sets a parameter that must match a poitional parameter in the query string.<br>
137      * The positional Parameter in the Query is in the form ?number and the parameter is set as an int. The value can be any object but its type must be assignable to the parameter
138      * type using normal Java assignability rules. So if the parameter is a String we cannot set an Integer
139      * and expect the type to be correctly coerced.<p>
140      * @param parameter - the position of the parameter
141      * @param value - the value the query should compare against
142      * @return - itself to allow method chaining.
143      */

144     public IndexQuery setParameter(int parameter, Object JavaDoc value){
145         parameterMap.put(""+parameter, value);
146         return this;
147     }
148     
149     /**
150      * Returns a Map of the parameters set on the query keyed by name and/or position. Positional parameters
151      * are stored in the Map as String representation of the int
152      * @return - the map of parameters.
153      */

154     public Map JavaDoc getParameterMap(){
155         return parameterMap;
156     }
157     
158     public void clearParameters(){
159         parameterMap.clear();
160     }
161     
162     /* (non-Javadoc)
163      * @see java.lang.Object#hashCode()
164      */

165     public int hashCode(){
166         if (hashCode ==0){
167             hashCode = query.hashCode();
168         }
169         return hashCode;
170     }
171     
172     /* (non-Javadoc)
173      * @see java.lang.Object#equals(java.lang.Object)
174      */

175     public boolean equals(Object JavaDoc obj){
176         return query.equals(obj);
177     }
178     
179     public String JavaDoc toString(){
180         return query;
181     }
182
183     public IndexQuery setFirstResult(int firstResult) {
184         this.firstResult= firstResult;
185         return this;
186     }
187
188     public int getFirstResult() {
189         
190         return firstResult;
191     }
192     
193     public int getMaxResults() {
194         
195         return maxResults;
196     }
197
198     public IndexQuery setMaxResults(int maxResults) {
199         this.maxResults=maxResults;
200         return this;
201     }
202 }
203
Popular Tags