001: /*
002: * Created on 13-Jul-2005
003: *
004: */
005: package com.jofti.query;
006:
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: import com.jofti.api.IndexQuery;
011: import com.jofti.core.QueryId;
012: import com.jofti.core.QueryType;
013:
014: /**
015: *
016: *
017: * Basic usage is:<br> "select ${identifier} from ${classname} AS ${identifier} where ${identifier}.${propertyname} [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter]"
018: *
019: * <p></p>
020: * multiproperty queries are of the form<br>
021: * "select ${identifier} from ${classname} AS ${identifier} where ${identifier}.${propertyname} [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter] [and,or] ${identifier}.${propertyname}
022: * [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter]"
023: * <p>
024: * compound queries are supported as ${query} [and,or] ${query}
025: * <p>
026: * ordering for compound queries is achieved through use of bracket groups<br>
027: * "... where ( ${identifier}.${propertyname} [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter] [and,or] ${identifier}.${propertyname} [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter] )
028: * [and,or] ${identifier}.${propertyname} [=,<,>,!=,>=,<=, LIKE, IN] ['value',:namedParameter,?positionalParameter] "
029: <p>
030: *
031: * multiclass queries are of the form
032: * <p>
033: * select ${identifierA}, ${identifierB} From ${classname} AS ${identifierA}, ${classname} AS ${identifierB} where ${identifierA}.${propertyname} [=,<,>,!=,>=,<=] 'value' [and.or] ${identifierB}.${propertyname} [=,<,>,!=,>=,<=] 'value'"
034: * <p>
035: *
036: * All values are created using a reflective String constructor .So if you want to Compare an Integer(22) you just specify
037: * value=22 - the value type is chosen based on the declared type of the field.
038: * <p>
039: * For primitives, primitive wrappers or some natively comparable classes in the JVM the propertyname is always the literal string 'VALUE' (case insensitive)
040: * * <ul>
041: * <li>java.lang.String.class,
042: <li>java.lang.Integer.class,
043: <li>java.lang.Double.class,
044: <li>java.lang.Float.class,
045: <li>java.lang.Long.class,
046: <li>java.lang.Short.class,
047: <li>java.math.BigInteger.class,
048: <li>java.math.BigDecimal.class,
049: <li>java.util.Date.class,
050: <li>java.net.URI.class
051: <li>java.lang.Byte.class
052: <li>java.lang.Character.class
053: <li>java.sql.Timestamp.class
054: </ul>
055: * <p>
056: * For example to query an integer you would write
057: * <p>
058: * select i from java.lang.Integer i where i.VALUE = 20
059: * <p>
060: * or <br>
061: * select i from java.lang.Integer as i where i.VALUE =20
062: * <p>
063: *
064: * Dates are constructed using the default encoding of the JVM. So a UK encoding would be:
065: * <p>
066: * select d from java.util.Date d where d.value <='4/10/1901 00:00:00'
067: * <p>
068: * For user defined classes in order to query the property type must have a String constructor and must
069: * implement equals and hashCode correctly.
070: *
071: * <p>
072: *
073: * For those classes that do not support String constructors or if you wish to use already existing values then you can use either
074: * the Named parameter or Positional parameter format.
075: * </p>
076: *
077: * <p>
078: * For instance to query for an integer as above we can use:<br>
079: * new EJBQuery("select i from java.lang.Integer as i where i.value =:intVal").setParameter("intVal", new Integer(20));
080: * <br>
081: * or<br>
082: * new EJBQuery("select i from java.lang.Integer as i where i.value =?1").setParameter(1, new Integer(20));
083: * </p>
084: *
085: *
086: * @author Steve Woodcock
087: * @version 1.0
088: */
089: public class EJBQuery implements IndexQuery, QueryId {
090:
091: String query = null;
092:
093: int hashCode = 0;
094: Map parameterMap = new HashMap();
095:
096: int maxResults;
097: int firstResult;
098:
099: static final QueryType QUERY_ID = QueryType.UNPARSED_QUERY;
100:
101: private static final String TERMINATOR = ";";
102:
103: public EJBQuery(String query) {
104: this .query = query + TERMINATOR;
105: }
106:
107: public String getQuery() {
108: return this .query;
109: }
110:
111: public QueryType getQueryType() {
112:
113: return QUERY_ID;
114: }
115:
116: /**
117: * Sets a parameter that must match a named parameter in the query string.<br>
118: * The nameParameter in the Query is in the form :name and the parameter is set as "name"
119: * without the leading ':'. The value can be any object but its type must be assignable to the parameter
120: * type using normal Java assignability rules. So if the parameter is a String we cannot set an Integer
121: * and expect the type to be correctly coerced.<p>
122: * @param name - the name of the parameter
123: * @param value - the value the query should compare against
124: * @return - itself to allow method chaining.
125: */
126: public IndexQuery setParameter(String name, Object value) {
127: parameterMap.put(name, value);
128: return this ;
129: }
130:
131: /**
132: * Sets a parameter that must match a poitional parameter in the query string.<br>
133: * 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
134: * type using normal Java assignability rules. So if the parameter is a String we cannot set an Integer
135: * and expect the type to be correctly coerced.<p>
136: * @param parameter - the position of the parameter
137: * @param value - the value the query should compare against
138: * @return - itself to allow method chaining.
139: */
140: public IndexQuery setParameter(int parameter, Object value) {
141: parameterMap.put("" + parameter, value);
142: return this ;
143: }
144:
145: /**
146: * Returns a Map of the parameters set on the query keyed by name and/or position. Positional parameters
147: * are stored in the Map as String representation of the int
148: * @return - the map of parameters.
149: */
150: public Map getParameterMap() {
151: return parameterMap;
152: }
153:
154: public void clearParameters() {
155: parameterMap.clear();
156: }
157:
158: /* (non-Javadoc)
159: * @see java.lang.Object#hashCode()
160: */
161: public int hashCode() {
162: if (hashCode == 0) {
163: hashCode = query.hashCode();
164: }
165: return hashCode;
166: }
167:
168: /* (non-Javadoc)
169: * @see java.lang.Object#equals(java.lang.Object)
170: */
171: public boolean equals(Object obj) {
172: return query.equals(obj);
173: }
174:
175: public String toString() {
176: return query;
177: }
178:
179: public IndexQuery setFirstResult(int firstResult) {
180: this .firstResult = firstResult;
181: return this ;
182: }
183:
184: public int getFirstResult() {
185:
186: return firstResult;
187: }
188:
189: public int getMaxResults() {
190:
191: return maxResults;
192: }
193:
194: public IndexQuery setMaxResults(int maxResults) {
195: this.maxResults = maxResults;
196: return this;
197: }
198: }
|