001package com.avaje.ebean;
002
003import java.util.Collection;
004import java.util.Map;
005
006/**
007 * Expression factory for creating standard expressions for WHERE and HAVING
008 * clauses.
009 * <p>
010 * Generally you will only need to use this object for creating OR, JUNCTION or
011 * CONJUNCTION expressions. To create simple expressions you will most likely
012 * just use the methods on the ExpressionList object that is returned via
013 * {@link Query#where()}.
014 * </p>
015 * <p>
016 * This provides a convenient way to create expressions for the 'Default'
017 * server. It is actually a short cut for using the ExpressionFactory of the
018 * 'default' EbeanServer.
019 * <p>
020 * See also {@link Ebean#getExpressionFactory()}
021 * </p>
022 * <p>
023 * Creates standard common expressions for using in a Query Where or Having
024 * clause.
025 * </p>
026 * 
027 * <pre class="code">
028 *  // Example: Using an Expr.or() method
029 * Query&lt;Order&gt; query = Ebean.createQuery(Order.class);
030 * query.where( 
031 *              Expr.or(Expr.eq(&quot;status&quot;, Order.NEW),
032 *                  Expr.gt(&quot;orderDate&quot;, lastWeek));
033 *     
034 * List&lt;Order&gt; list = query.findList();
035 * ...
036 * </pre>
037 * 
038 * @see Query#where()
039 * @author Rob Bygrave
040 */
041public class Expr {
042
043  private Expr() {
044  }
045
046  /**
047   * Equal To - property equal to the given value.
048   */
049  public static Expression eq(String propertyName, Object value) {
050    return Ebean.getExpressionFactory().eq(propertyName, value);
051  }
052
053  /**
054   * Not Equal To - property not equal to the given value.
055   */
056  public static Expression ne(String propertyName, Object value) {
057    return Ebean.getExpressionFactory().ne(propertyName, value);
058  }
059
060  /**
061   * Case Insensitive Equal To - property equal to the given value (typically
062   * using a lower() function to make it case insensitive).
063   */
064  public static Expression ieq(String propertyName, String value) {
065    return Ebean.getExpressionFactory().ieq(propertyName, value);
066  }
067
068  /**
069   * Between - property between the two given values.
070   */
071  public static Expression between(String propertyName, Object value1, Object value2) {
072
073    return Ebean.getExpressionFactory().between(propertyName, value1, value2);
074  }
075
076  /**
077   * Between - value between two given properties.
078   */
079  public static Expression between(String lowProperty, String highProperty, Object value) {
080    
081    return Ebean.getExpressionFactory().betweenProperties(lowProperty, highProperty, value);
082  }
083  
084  /**
085   * Greater Than - property greater than the given value.
086   */
087  public static Expression gt(String propertyName, Object value) {
088    return Ebean.getExpressionFactory().gt(propertyName, value);
089  }
090
091  /**
092   * Greater Than or Equal to - property greater than or equal to the given
093   * value.
094   */
095  public static Expression ge(String propertyName, Object value) {
096    return Ebean.getExpressionFactory().ge(propertyName, value);
097  }
098
099  /**
100   * Less Than - property less than the given value.
101   */
102  public static Expression lt(String propertyName, Object value) {
103    return Ebean.getExpressionFactory().lt(propertyName, value);
104  }
105
106  /**
107   * Less Than or Equal to - property less than or equal to the given value.
108   */
109  public static Expression le(String propertyName, Object value) {
110    return Ebean.getExpressionFactory().le(propertyName, value);
111  }
112
113  /**
114   * Is Null - property is null.
115   */
116  public static Expression isNull(String propertyName) {
117    return Ebean.getExpressionFactory().isNull(propertyName);
118  }
119
120  /**
121   * Is Not Null - property is not null.
122   */
123  public static Expression isNotNull(String propertyName) {
124    return Ebean.getExpressionFactory().isNotNull(propertyName);
125  }
126
127  /**
128   * Case insensitive {@link #exampleLike(Object)}
129   */
130  public static ExampleExpression iexampleLike(Object example) {
131    return Ebean.getExpressionFactory().iexampleLike(example);
132  }
133
134  /**
135   * Create the query by Example expression which is case sensitive and using
136   * LikeType.RAW (you need to add you own wildcards % and _).
137   */
138  public static ExampleExpression exampleLike(Object example) {
139    return Ebean.getExpressionFactory().exampleLike(example);
140  }
141
142  /**
143   * Create the query by Example expression specifying more options.
144   */
145  public static ExampleExpression exampleLike(Object example, boolean caseInsensitive,
146      LikeType likeType) {
147    return Ebean.getExpressionFactory().exampleLike(example, caseInsensitive, likeType);
148  }
149
150  /**
151   * Like - property like value where the value contains the SQL wild card
152   * characters % (percentage) and _ (underscore).
153   */
154  public static Expression like(String propertyName, String value) {
155    return Ebean.getExpressionFactory().like(propertyName, value);
156  }
157
158  /**
159   * Case insensitive Like - property like value where the value contains the
160   * SQL wild card characters % (percentage) and _ (underscore). Typically uses
161   * a lower() function to make the expression case insensitive.
162   */
163  public static Expression ilike(String propertyName, String value) {
164    return Ebean.getExpressionFactory().ilike(propertyName, value);
165  }
166
167  /**
168   * Starts With - property like value%.
169   */
170  public static Expression startsWith(String propertyName, String value) {
171    return Ebean.getExpressionFactory().startsWith(propertyName, value);
172  }
173
174  /**
175   * Case insensitive Starts With - property like value%. Typically uses a
176   * lower() function to make the expression case insensitive.
177   */
178  public static Expression istartsWith(String propertyName, String value) {
179    return Ebean.getExpressionFactory().istartsWith(propertyName, value);
180  }
181
182  /**
183   * Ends With - property like %value.
184   */
185  public static Expression endsWith(String propertyName, String value) {
186    return Ebean.getExpressionFactory().endsWith(propertyName, value);
187  }
188
189  /**
190   * Case insensitive Ends With - property like %value. Typically uses a lower()
191   * function to make the expression case insensitive.
192   */
193  public static Expression iendsWith(String propertyName, String value) {
194    return Ebean.getExpressionFactory().iendsWith(propertyName, value);
195  }
196
197  /**
198   * Contains - property like %value%.
199   */
200  public static Expression contains(String propertyName, String value) {
201    return Ebean.getExpressionFactory().contains(propertyName, value);
202  }
203
204  /**
205   * Case insensitive Contains - property like %value%. Typically uses a lower()
206   * function to make the expression case insensitive.
207   */
208  public static Expression icontains(String propertyName, String value) {
209    return Ebean.getExpressionFactory().icontains(propertyName, value);
210  }
211
212  /**
213   * For collection properties that are empty (have not existing elements).
214   */
215  public static Expression isEmpty(String propertyName) {
216    return Ebean.getExpressionFactory().isEmpty(propertyName);
217  }
218
219  /**
220   * For collection properties that are not empty (have existing elements).
221   */
222  public static Expression isNotEmpty(String propertyName) {
223    return Ebean.getExpressionFactory().isNotEmpty(propertyName);
224  }
225
226  /**
227   * In - property has a value in the array of values.
228   */
229  public static Expression in(String propertyName, Object[] values) {
230    return Ebean.getExpressionFactory().in(propertyName, values);
231  }
232
233  /**
234   * In - using a subQuery.
235   */
236  public static Expression in(String propertyName, Query<?> subQuery) {
237    return Ebean.getExpressionFactory().in(propertyName, subQuery);
238  }
239
240  /**
241   * In - property has a value in the collection of values.
242   */
243  public static Expression in(String propertyName, Collection<?> values) {
244    return Ebean.getExpressionFactory().in(propertyName, values);
245  }
246
247  /**
248   * Id Equal to - ID property is equal to the value.
249   */
250  public static Expression idEq(Object value) {
251    return Ebean.getExpressionFactory().idEq(value);
252  }
253
254  /**
255   * All Equal - Map containing property names and their values.
256   * <p>
257   * Expression where all the property names in the map are equal to the
258   * corresponding value.
259   * </p>
260   * 
261   * @param propertyMap
262   *          a map keyed by property names.
263   */
264  public static Expression allEq(Map<String, Object> propertyMap) {
265    return Ebean.getExpressionFactory().allEq(propertyMap);
266  }
267
268  /**
269   * Add raw expression with a single parameter.
270   * <p>
271   * The raw expression should contain a single ? at the location of the
272   * parameter.
273   * </p>
274   */
275  public static Expression raw(String raw, Object value) {
276    return Ebean.getExpressionFactory().raw(raw, value);
277  }
278
279  /**
280   * Add raw expression with an array of parameters.
281   * <p>
282   * The raw expression should contain the same number of ? as there are
283   * parameters.
284   * </p>
285   */
286  public static Expression raw(String raw, Object[] values) {
287    return Ebean.getExpressionFactory().raw(raw, values);
288  }
289
290  /**
291   * Add raw expression with no parameters.
292   */
293  public static Expression raw(String raw) {
294    return Ebean.getExpressionFactory().raw(raw);
295  }
296
297  /**
298   * And - join two expressions with a logical and.
299   */
300  public static Expression and(Expression expOne, Expression expTwo) {
301
302    return Ebean.getExpressionFactory().and(expOne, expTwo);
303  }
304
305  /**
306   * Or - join two expressions with a logical or.
307   */
308  public static Expression or(Expression expOne, Expression expTwo) {
309
310    return Ebean.getExpressionFactory().or(expOne, expTwo);
311  }
312
313  /**
314   * Negate the expression (prefix it with NOT).
315   */
316  public static Expression not(Expression exp) {
317
318    return Ebean.getExpressionFactory().not(exp);
319  }
320
321  /**
322   * Return a list of expressions that will be joined by AND's.
323   */
324  public static <T> Junction<T> conjunction(Query<T> query) {
325
326    return Ebean.getExpressionFactory().conjunction(query);
327  }
328
329  /**
330   * Return a list of expressions that will be joined by OR's.
331   */
332  public static <T> Junction<T> disjunction(Query<T> query) {
333
334    return Ebean.getExpressionFactory().disjunction(query);
335  }
336}