001package io.ebean; 002 003import io.ebean.search.Match; 004import io.ebean.search.MultiMatch; 005import io.ebean.search.TextCommonTerms; 006import io.ebean.search.TextQueryString; 007import io.ebean.search.TextSimple; 008 009import java.util.Collection; 010import java.util.Map; 011 012/** 013 * Expression factory for creating standard expressions. 014 * <p> 015 * Creates standard common expressions for using in a Query Where or Having 016 * clause. 017 * </p> 018 * <p> 019 * You will often not use this class directly but instead just add expressions 020 * via the methods on ExpressionList such as 021 * {@link ExpressionList#gt(String, Object)}. 022 * </p> 023 * <p> 024 * The ExpressionList is returned from {@link Query#where()}. 025 * </p> 026 * <pre>{@code 027 * // Example: fetch orders where status equals new or orderDate > lastWeek. 028 * 029 * Expression newOrLastWeek = 030 * Expr.or(Expr.eq("status", Order.Status.NEW), 031 * Expr.gt("orderDate", lastWeek)); 032 * 033 * List<Order> list = DB.find(Order.class) 034 * .where().add(newOrLastWeek) 035 * .findList(); 036 * ... 037 * }</pre> 038 * 039 * @see Query#where() 040 */ 041public interface ExpressionFactory { 042 043 /** 044 * Path exists - for the given path in a JSON document. 045 */ 046 Expression jsonExists(String propertyName, String path); 047 048 /** 049 * Path does not exist - for the given path in a JSON document. 050 */ 051 Expression jsonNotExists(String propertyName, String path); 052 053 /** 054 * Equal to - for the given path in a JSON document. 055 */ 056 Expression jsonEqualTo(String propertyName, String path, Object val); 057 058 /** 059 * Not Equal to - for the given path in a JSON document. 060 */ 061 Expression jsonNotEqualTo(String propertyName, String path, Object val); 062 063 /** 064 * Greater than - for the given path in a JSON document. 065 */ 066 Expression jsonGreaterThan(String propertyName, String path, Object val); 067 068 /** 069 * Greater than or equal to - for the given path in a JSON document. 070 */ 071 Expression jsonGreaterOrEqual(String propertyName, String path, Object val); 072 073 /** 074 * Less than - for the given path in a JSON document. 075 */ 076 Expression jsonLessThan(String propertyName, String path, Object val); 077 078 /** 079 * Less than or equal to - for the given path in a JSON document. 080 */ 081 Expression jsonLessOrEqualTo(String propertyName, String path, Object val); 082 083 /** 084 * Between - for the given path in a JSON document. 085 */ 086 Expression jsonBetween(String propertyName, String path, Object lowerValue, Object upperValue); 087 088 /** 089 * Array contains all the given values. 090 * <p> 091 * Array support is effectively limited to Postgres at this time. 092 * </p> 093 */ 094 Expression arrayContains(String propertyName, Object... values); 095 096 /** 097 * Array does not contain the given values. 098 * <p> 099 * Array support is effectively limited to Postgres at this time. 100 * </p> 101 */ 102 Expression arrayNotContains(String propertyName, Object... values); 103 104 /** 105 * Array is empty - for the given array property. 106 * <p> 107 * Array support is effectively limited to Postgres at this time. 108 * </p> 109 */ 110 Expression arrayIsEmpty(String propertyName); 111 112 /** 113 * Array is not empty - for the given array property. 114 * <p> 115 * Array support is effectively limited to Postgres at this time. 116 * </p> 117 */ 118 Expression arrayIsNotEmpty(String propertyName); 119 120 /** 121 * Equal To - property equal to the given value. 122 */ 123 Expression eq(String propertyName, Object value); 124 125 /** 126 * Equal To or Null - property equal to the given value or null. 127 */ 128 Expression eqOrNull(String propertyName, Object value); 129 130 /** 131 * Not Equal To - property not equal to the given value. 132 */ 133 Expression ne(String propertyName, Object value); 134 135 /** 136 * Case Insensitive Equal To - property equal to the given value (typically 137 * using a lower() function to make it case insensitive). 138 */ 139 Expression ieq(String propertyName, String value); 140 141 /** 142 * Case Insensitive Not Equal To - property not equal to the given value (typically 143 * using a lower() function to make it case insensitive). 144 */ 145 Expression ine(String propertyName, String value); 146 147 /** 148 * Case Insensitive Equal To that allows for named parameter use. 149 */ 150 Expression ieqObject(String propertyName, Object value); 151 152 /** 153 * Case Insensitive Not Equal To that allows for named parameter use. 154 */ 155 Expression ineObject(String propertyName, Object value); 156 157 /** 158 * In Range - property >= value1 and property < value2. 159 * <p> 160 * Unlike Between inRange is "half open" and usually more useful for use with dates or timestamps. 161 * </p> 162 */ 163 Expression inRange(String propertyName, Object value1, Object value2); 164 165 /** 166 * Value in Range between 2 properties. 167 * 168 * <pre>{@code 169 * 170 * .startDate.inRangeWith(endDate, now) 171 * 172 * // which equates to 173 * startDate <= now and (endDate > now or endDate is null) 174 * 175 * }</pre> 176 * 177 * <p> 178 * This is a convenience expression combining a number of simple expressions. 179 * The most common use of this could be called "effective dating" where 2 date or 180 * timestamp columns represent the date range in which 181 */ 182 Expression inRangeWith(String lowProperty, String highProperty, Object value); 183 184 /** 185 * Between - property between the two given values. 186 */ 187 Expression between(String propertyName, Object value1, Object value2); 188 189 /** 190 * Between - value between two given properties. 191 */ 192 Expression betweenProperties(String lowProperty, String highProperty, Object value); 193 194 /** 195 * Greater Than Or Null - property greater than the given value or null. 196 * <p> 197 * A convenient expression combining GT and Is Null. Most often useful for range 198 * expressions where the top range value is nullable. 199 */ 200 Expression gtOrNull(String propertyName, Object value); 201 202 /** 203 * Greater Than - property greater than the given value. 204 */ 205 Expression gt(String propertyName, Object value); 206 207 /** 208 * Greater Than or Equal to - property greater than or equal to the given 209 * value. 210 */ 211 Expression ge(String propertyName, Object value); 212 213 /** 214 * Less Than or Null - property less than the given value or null. 215 * <p> 216 * A convenient expression combining LT and Is Null. Most often useful for range 217 * expressions where the bottom range value is nullable. 218 */ 219 Expression ltOrNull(String propertyName, Object value); 220 221 /** 222 * Less Than - property less than the given value. 223 */ 224 Expression lt(String propertyName, Object value); 225 226 /** 227 * Less Than or Equal to - property less than or equal to the given value. 228 */ 229 Expression le(String propertyName, Object value); 230 231 /** 232 * Is Null - property is null. 233 */ 234 Expression isNull(String propertyName); 235 236 /** 237 * Is Not Null - property is not null. 238 */ 239 Expression isNotNull(String propertyName); 240 241 /** 242 * Case insensitive {@link #exampleLike(Object)} 243 */ 244 ExampleExpression iexampleLike(Object example); 245 246 /** 247 * Create the query by Example expression which is case sensitive and using 248 * LikeType.RAW (you need to add you own wildcards % and _). 249 */ 250 ExampleExpression exampleLike(Object example); 251 252 /** 253 * Create the query by Example expression specifying more options. 254 */ 255 ExampleExpression exampleLike(Object example, boolean caseInsensitive, LikeType likeType); 256 257 /** 258 * Like with support for named parameters. 259 */ 260 Expression like(String propertyName, Object value, boolean caseInsensitive, LikeType likeType); 261 262 /** 263 * Like - property like value where the value contains the SQL wild card 264 * characters % (percentage) and _ (underscore). 265 */ 266 Expression like(String propertyName, String value); 267 268 /** 269 * Case insensitive Like - property like value where the value contains the 270 * SQL wild card characters % (percentage) and _ (underscore). Typically uses 271 * a lower() function to make the expression case insensitive. 272 */ 273 Expression ilike(String propertyName, String value); 274 275 /** 276 * Starts With - property like value%. 277 */ 278 Expression startsWith(String propertyName, String value); 279 280 /** 281 * Case insensitive Starts With - property like value%. Typically uses a 282 * lower() function to make the expression case insensitive. 283 */ 284 Expression istartsWith(String propertyName, String value); 285 286 /** 287 * Ends With - property like %value. 288 */ 289 Expression endsWith(String propertyName, String value); 290 291 /** 292 * Case insensitive Ends With - property like %value. Typically uses a lower() 293 * function to make the expression case insensitive. 294 */ 295 Expression iendsWith(String propertyName, String value); 296 297 /** 298 * Contains - property like %value%. 299 */ 300 Expression contains(String propertyName, String value); 301 302 /** 303 * Case insensitive Contains - property like %value%. Typically uses a lower() 304 * function to make the expression case insensitive. 305 */ 306 Expression icontains(String propertyName, String value); 307 308 /** 309 * In expression using pairs of value objects. 310 */ 311 Expression inPairs(Pairs pairs); 312 313 /** 314 * In - property has a value in the array of values. 315 */ 316 Expression in(String propertyName, Object[] values); 317 318 /** 319 * In - using a subQuery. 320 */ 321 Expression in(String propertyName, Query<?> subQuery); 322 323 /** 324 * In - property has a value in the collection of values. 325 */ 326 Expression in(String propertyName, Collection<?> values); 327 328 /** 329 * In where null or empty values means that no predicate is added to the query. 330 * <p> 331 * That is, only add the IN predicate if the values are not null or empty. 332 * <p> 333 * Without this we typically need to code an <code>if</code> block to only add 334 * the IN predicate if the collection is not empty like: 335 * </p> 336 * 337 * <h3>Without inOrEmpty()</h3> 338 * <pre>{@code 339 * 340 * query.where() // add some predicates 341 * .eq("status", Status.NEW); 342 * 343 * if (ids != null && !ids.isEmpty()) { 344 * query.where().in("customer.id", ids); 345 * } 346 * 347 * query.findList(); 348 * 349 * }</pre> 350 * 351 * <h3>Using inOrEmpty()</h3> 352 * <pre>{@code 353 * 354 * query.where() 355 * .eq("status", Status.NEW) 356 * .inOrEmpty("customer.id", ids) 357 * .findList(); 358 * 359 * }</pre> 360 */ 361 Expression inOrEmpty(String propertyName, Collection<?> values); 362 363 /** 364 * Not In - property has a value in the array of values. 365 */ 366 Expression notIn(String propertyName, Object[] values); 367 368 /** 369 * Not In - property has a value in the collection of values. 370 */ 371 Expression notIn(String propertyName, Collection<?> values); 372 373 /** 374 * Not In - using a subQuery. 375 */ 376 Expression notIn(String propertyName, Query<?> subQuery); 377 378 /** 379 * Exists expression 380 */ 381 Expression exists(Query<?> subQuery); 382 383 /** 384 * Not exists expression 385 */ 386 Expression notExists(Query<?> subQuery); 387 388 /** 389 * Is empty expression for collection properties. 390 */ 391 Expression isEmpty(String propertyName); 392 393 /** 394 * Is not empty expression for collection properties. 395 */ 396 Expression isNotEmpty(String propertyName); 397 398 /** 399 * Id Equal to - ID property is equal to the value. 400 */ 401 Expression idEq(Object value); 402 403 /** 404 * Id IN a list of Id values. 405 */ 406 Expression idIn(Object... idValues); 407 408 /** 409 * Id IN a collection of Id values. 410 */ 411 Expression idIn(Collection<?> idCollection); 412 413 /** 414 * All Equal - Map containing property names and their values. 415 * <p> 416 * Expression where all the property names in the map are equal to the 417 * corresponding value. 418 * </p> 419 * 420 * @param propertyMap a map keyed by property names. 421 */ 422 Expression allEq(Map<String, Object> propertyMap); 423 424 /** 425 * Add expression for ANY of the given bit flags to be set. 426 * 427 * @param propertyName The property that holds the flags value 428 * @param flags The flags we are looking for 429 */ 430 Expression bitwiseAny(String propertyName, long flags); 431 432 /** 433 * Add expression for ALL of the given bit flags to be set. 434 * 435 * @param propertyName The property that holds the flags value 436 * @param flags The flags we are looking for 437 */ 438 Expression bitwiseAll(String propertyName, long flags); 439 440 /** 441 * Add bitwise AND expression of the given bit flags to compare with the match/mask. 442 * 443 * @param propertyName The property that holds the flags value 444 * @param flags The flags we are looking for 445 */ 446 Expression bitwiseAnd(String propertyName, long flags, long match); 447 448 /** 449 * Add raw expression with a single parameter. 450 * <p> 451 * The raw expression should contain a single ? at the location of the 452 * parameter. 453 * </p> 454 */ 455 Expression raw(String raw, Object value); 456 457 /** 458 * Add raw expression with an array of parameters. 459 * <p> 460 * The raw expression should contain the same number of ? as there are 461 * parameters. 462 * </p> 463 */ 464 Expression raw(String raw, Object[] values); 465 466 /** 467 * Add raw expression with no parameters. 468 */ 469 Expression raw(String raw); 470 471 /** 472 * Create a Text Match expression (currently doc store/Elastic only). 473 */ 474 Expression textMatch(String propertyName, String search, Match options); 475 476 /** 477 * Create a Text Multi match expression (currently doc store/Elastic only). 478 */ 479 Expression textMultiMatch(String query, MultiMatch options); 480 481 /** 482 * Create a text simple query expression (currently doc store/Elastic only). 483 */ 484 Expression textSimple(String search, TextSimple options); 485 486 /** 487 * Create a text query string expression (currently doc store/Elastic only). 488 */ 489 Expression textQueryString(String search, TextQueryString options); 490 491 /** 492 * Create a text common terms expression (currently doc store/Elastic only). 493 */ 494 Expression textCommonTerms(String search, TextCommonTerms options); 495 496 /** 497 * And - join two expressions with a logical and. 498 */ 499 Expression and(Expression expOne, Expression expTwo); 500 501 /** 502 * Or - join two expressions with a logical or. 503 */ 504 Expression or(Expression expOne, Expression expTwo); 505 506 /** 507 * Negate the expression (prefix it with NOT). 508 */ 509 Expression not(Expression exp); 510 511 /** 512 * Return a list of expressions that will be joined by AND's. 513 */ 514 <T> Junction<T> conjunction(Query<T> query); 515 516 /** 517 * Return a list of expressions that will be joined by OR's. 518 */ 519 <T> Junction<T> disjunction(Query<T> query); 520 521 /** 522 * Return a list of expressions that will be joined by AND's. 523 */ 524 <T> Junction<T> conjunction(Query<T> query, ExpressionList<T> parent); 525 526 /** 527 * Return a list of expressions that will be joined by OR's. 528 */ 529 <T> Junction<T> disjunction(Query<T> query, ExpressionList<T> parent); 530 531 /** 532 * Return a Text query junction for MUST, SHOULD or MUST NOT. 533 * <p> 534 * This is doc store Elastic only. 535 * </p> 536 */ 537 <T> Junction<T> junction(Junction.Type type, Query<T> query, ExpressionList<T> parent); 538 539}