All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avaje.ebean.ExpressionList Maven / Gradle / Ivy

There is a newer version: 8.1.1
Show newest version
package com.avaje.ebean;

import com.avaje.ebean.search.Match;
import com.avaje.ebean.search.MultiMatch;
import com.avaje.ebean.search.TextCommonTerms;
import com.avaje.ebean.search.TextQueryString;
import com.avaje.ebean.search.TextSimple;
import org.jetbrains.annotations.Nullable;

import javax.persistence.NonUniqueResultException;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * List of Expressions that make up a where or having clause.
 * 

* An ExpressionList is returned from {@link Query#where()}. *

*

* The ExpressionList has a list of convenience methods that create the standard * expressions and add them to this list. *

*

* The ExpressionList also duplicates methods that are found on the Query such * as findList() and orderBy(). The purpose of these methods is provide a fluid * API. The upside of this approach is that you can build and execute a query * via chained methods. The down side is that this ExpressionList object has * more methods than you would initially expect (the ones duplicated from * Query). *

* * @see Query#where() */ public interface ExpressionList { /** * Return the query that owns this expression list. *

* This is a convenience method solely to support a fluid API where the * methods are chained together. Adding expressions returns this expression * list and this method can be used after that to return back the original * query so that further things can be added to it. *

*/ Query query(); /** * Set the order by clause replacing the existing order by clause if there is * one. *

* This follows SQL syntax using commas between each property with the * optional asc and desc keywords representing ascending and descending order * respectively. *

*

* This is EXACTLY the same as {@link #orderBy(String)}. *

*/ Query order(String orderByClause); /** * Return the OrderBy so that you can append an ascending or descending * property to the order by clause. *

* This will never return a null. If no order by clause exists then an 'empty' * OrderBy object is returned. *

*/ OrderBy order(); /** * Return the OrderBy so that you can append an ascending or descending * property to the order by clause. *

* This will never return a null. If no order by clause exists then an 'empty' * OrderBy object is returned. *

*/ OrderBy orderBy(); /** * Add an orderBy clause to the query. * * @see Query#orderBy(String) */ Query orderBy(String orderBy); /** * Add an orderBy clause to the query. * * @see Query#orderBy(String) */ Query setOrderBy(String orderBy); /** * Apply the path properties to the query replacing the select and fetch clauses. */ Query apply(FetchPath fetchPath); /** * Perform an 'As of' query using history tables to return the object graph * as of a time in the past. *

* To perform this query the DB must have underlying history tables. *

* * @param asOf the date time in the past at which you want to view the data */ Query asOf(Timestamp asOf); /** * Execute the query against the draft set of tables. */ Query asDraft(); /** * Deprecated in favour of setIncludeSoftDeletes(). */ @Deprecated Query includeSoftDeletes(); /** * Execute the query including soft deleted rows. */ Query setIncludeSoftDeletes(); /** * Execute as a delete query deleting the 'root level' beans that match the predicates * in the query. *

* Note that if the query includes joins then the generated delete statement may not be * optimal depending on the database platform. *

* * @return the number of rows that were deleted. */ int delete(); /** * Execute as a update query. * * @return the number of rows that were updated. * @see UpdateQuery */ int update(); /** * Execute the query process the beans one at a time. * * @see Query#findEach(QueryEachConsumer) */ void findEach(QueryEachConsumer consumer); /** * Execute the query processing the beans one at a time with the ability to * stop processing before reading all the beans. * * @see Query#findEachWhile(QueryEachWhileConsumer) */ void findEachWhile(QueryEachWhileConsumer consumer); /** * Execute the query returning a list. * * @see Query#findList() */ List findList(); /** * Execute the query returning the list of Id's. * * @see Query#findIds() */ List findIds(); /** * Return the count of entities this query should return. *

* This is the number of 'top level' or 'root level' entities. *

*/ int findRowCount(); /** * Execute the query returning a set. * * @see Query#findSet() */ Set findSet(); /** * Execute the query returning a map. * * @see Query#findMap() */ Map findMap(); /** * Return a typed map specifying the key property and type. */ Map findMap(String keyProperty, Class keyType); /** * Execute the query returning a single bean or null (if no matching * bean is found). *

* If more than 1 row is found for this query then a NonUniqueResultException is * thrown. *

* * @throws NonUniqueResultException if more than one result was found * * @see Query#findUnique() */ @Nullable T findUnique(); /** * Execute find row count query in a background thread. *

* This returns a Future object which can be used to cancel, check the * execution status (isDone etc) and get the value (with or without a * timeout). *

* * @return a Future object for the row count query */ FutureRowCount findFutureRowCount(); /** * Execute find Id's query in a background thread. *

* This returns a Future object which can be used to cancel, check the * execution status (isDone etc) and get the value (with or without a * timeout). *

* * @return a Future object for the list of Id's */ FutureIds findFutureIds(); /** * Execute find list query in a background thread. *

* This returns a Future object which can be used to cancel, check the * execution status (isDone etc) and get the value (with or without a * timeout). *

* * @return a Future object for the list result of the query */ FutureList findFutureList(); /** * Return a PagedList for this query using firstRow and maxRows. *

* The benefit of using this over findList() is that it provides functionality to get the * total row count etc. *

*

* If maxRows is not set on the query prior to calling findPagedList() then a * PersistenceException is thrown. *

* *
{@code
   *
   *  PagedList pagedList = Ebean.find(Order.class)
   *       .setFirstRow(50)
   *       .setMaxRows(20)
   *       .findPagedList();
   *
   *       // fetch the total row count in the background
   *       pagedList.loadRowCount();
   *
   *       List orders = pagedList.getList();
   *       int totalRowCount = pagedList.getTotalRowCount();
   *
   * }
* * @return The PagedList * * @see Query#findPagedList() */ PagedList findPagedList(); /** * Return versions of a @History entity bean. *

* Generally this query is expected to be a find by id or unique predicates query. * It will execute the query against the history returning the versions of the bean. *

*/ List> findVersions(); /** * Return versions of a @History entity bean between the 2 timestamps. *

* Generally this query is expected to be a find by id or unique predicates query. * It will execute the query against the history returning the versions of the bean. *

*/ List> findVersionsBetween(Timestamp start, Timestamp end); /** * Add some filter predicate expressions to the many property. */ ExpressionList filterMany(String prop); /** * Specify specific properties to fetch on the main/root bean (aka partial * object). * * @see Query#select(String) */ Query select(String properties); /** * Set whether this query uses DISTINCT. *

* The select() clause MUST be specified when setDistinct(true) is set. The reason for this is that * generally ORM queries include the "id" property and this doesn't make sense for distinct queries. *

*
{@code
   *
   *   List customers =
   *       Ebean.find(Customer.class)
   *          .setDistinct(true)
   *          .select("name")     // only select the customer name
   *          .findList();
   *
   * }
*/ Query setDistinct(boolean distinct); /** * Set the first row to fetch. * * @see Query#setFirstRow(int) */ Query setFirstRow(int firstRow); /** * Set the maximum number of rows to fetch. * * @see Query#setMaxRows(int) */ Query setMaxRows(int maxRows); /** * Set the name of the property which values become the key of a map. * * @see Query#setMapKey(String) */ Query setMapKey(String mapKey); /** * Set to true to use the query for executing this query. * * @see Query#setUseCache(boolean) */ Query setUseCache(boolean useCache); /** * Set to true to use the query for executing this query. * * @see Query#setUseQueryCache(boolean) */ Query setUseQueryCache(boolean useCache); /** * Set to true if this query should execute against the doc store. *

* When setting this you may also consider disabling lazy loading. *

*/ Query setUseDocStore(boolean useDocsStore); /** * Set true if you want to disable lazy loading. *

* That is, once the object graph is returned further lazy loading is disabled. *

*/ Query setDisableLazyLoading(boolean disableLazyLoading); /** * Disable read auditing for this query. *

* This is intended to be used when the query is not a user initiated query and instead * part of the internal processing in an application to load a cache or document store etc. * In these cases we don't want the query to be part of read auditing. *

*/ Query setDisableReadAuditing(); /** * Add expressions to the having clause. *

* The having clause is only used for queries based on raw sql (via SqlSelect * annotation etc). *

*/ ExpressionList having(); /** * Add another expression to the where clause. */ ExpressionList where(); /** * Path exists - for the given path in a JSON document. * *
{@code
   *
   *   where().jsonExists("content", "path.other")
   *
   * }
* * @param propertyName the property that holds a JSON document * @param path the nested path in the JSON document in dot notation */ ExpressionList jsonExists(String propertyName, String path); /** * Path does not exist - for the given path in a JSON document. * *
{@code
   *
   *   where().jsonNotExists("content", "path.other")
   *
   * }
* * @param propertyName the property that holds a JSON document * @param path the nested path in the JSON document in dot notation */ ExpressionList jsonNotExists(String propertyName, String path); /** * Equal to expression for the value at the given path in the JSON document. * *
{@code
   *
   *   where().jsonEqualTo("content", "path.other", 34)
   *
   * }
* * @param propertyName the property that holds a JSON document * @param path the nested path in the JSON document in dot notation * @param value the value used to test against the document path's value */ ExpressionList jsonEqualTo(String propertyName, String path, Object value); /** * Not Equal to - for the given path in a JSON document. * *
{@code
   *
   *   where().jsonNotEqualTo("content", "path.other", 34)
   *
   * }
* * @param propertyName the property that holds a JSON document * @param path the nested path in the JSON document in dot notation * @param value the value used to test against the document path's value */ ExpressionList jsonNotEqualTo(String propertyName, String path, Object value); /** * Greater than - for the given path in a JSON document. * *
{@code
   *
   *   where().jsonGreaterThan("content", "path.other", 34)
   *
   * }
*/ ExpressionList jsonGreaterThan(String propertyName, String path, Object value); /** * Greater than or equal to - for the given path in a JSON document. * *
{@code
   *
   *   where().jsonGreaterOrEqual("content", "path.other", 34)
   *
   * }
*/ ExpressionList jsonGreaterOrEqual(String propertyName, String path, Object value); /** * Less than - for the given path in a JSON document. * *
{@code
   *
   *   where().jsonLessThan("content", "path.other", 34)
   *
   * }
*/ ExpressionList jsonLessThan(String propertyName, String path, Object value); /** * Less than or equal to - for the given path in a JSON document. * *
{@code
   *
   *   where().jsonLessOrEqualTo("content", "path.other", 34)
   *
   * }
*/ ExpressionList jsonLessOrEqualTo(String propertyName, String path, Object value); /** * Between - for the given path in a JSON document. * *
{@code
   *
   *   where().jsonBetween("content", "orderDate", lowerDateTime, upperDateTime)
   *
   * }
*/ ExpressionList jsonBetween(String propertyName, String path, Object lowerValue, Object upperValue); /** * Add an Expression to the list. *

* This returns the list so that add() can be chained. *

* *
{@code
   *
   * Query query = Ebean.find(Customer.class);
   * query.where()
   *     .like("name","Rob%")
   *     .eq("status", Customer.ACTIVE);
   *
   * List list = query.findList();
   * ...
   *
   * }
*/ ExpressionList add(Expression expr); /** * Add a list of Expressions to this ExpressionList.s */ ExpressionList addAll(ExpressionList exprList); /** * Equal To - property is equal to a given value. */ ExpressionList eq(String propertyName, Object value); /** * Not Equal To - property not equal to the given value. */ ExpressionList ne(String propertyName, Object value); /** * Case Insensitive Equal To - property equal to the given value (typically * using a lower() function to make it case insensitive). */ ExpressionList ieq(String propertyName, String value); /** * Between - property between the two given values. */ ExpressionList between(String propertyName, Object value1, Object value2); /** * Between - value between the two properties. */ ExpressionList betweenProperties(String lowProperty, String highProperty, Object value); /** * Greater Than - property greater than the given value. */ ExpressionList gt(String propertyName, Object value); /** * Greater Than or Equal to - property greater than or equal to the given * value. */ ExpressionList ge(String propertyName, Object value); /** * Less Than - property less than the given value. */ ExpressionList lt(String propertyName, Object value); /** * Less Than or Equal to - property less than or equal to the given value. */ ExpressionList le(String propertyName, Object value); /** * Is Null - property is null. */ ExpressionList isNull(String propertyName); /** * Is Not Null - property is not null. */ ExpressionList isNotNull(String propertyName); /** * A "Query By Example" type of expression. *

* Pass in an example entity and for each non-null scalar properties an * expression is added. *

*

* By Default this case sensitive, will ignore numeric zero values and will * use a Like for string values (you must put in your own wildcards). *

*

* To get control over the options you can create an ExampleExpression and set * those options such as case insensitive etc. *

* *
{@code
   *
   * // create an example bean and set the properties
   * // with the query parameters you want
   * Customer example = new Customer();
   * example.setName("Rob%");
   * example.setNotes("%something%");
   * 
   * List<Customer> list = Ebean.find(Customer.class).where()
   *     // pass the bean into the where() clause
   *     .exampleLike(example)
   *     // you can add other expressions to the same query
   *     .gt("id", 2).findList();
   * 
   * }
* * Similarly you can create an ExampleExpression * *
{@code
   *
   * Customer example = new Customer();
   * example.setName("Rob%");
   * example.setNotes("%something%");
   * 
   * // create a ExampleExpression with more control
   * ExampleExpression qbe = new ExampleExpression(example, true, LikeType.EQUAL_TO).includeZeros();
   * 
   * List list = Ebean.find(Customer.class).where().add(qbe).findList();
   *
   * }
*/ ExpressionList exampleLike(Object example); /** * Case insensitive version of {@link #exampleLike(Object)} */ ExpressionList iexampleLike(Object example); /** * Like - property like value where the value contains the SQL wild card * characters % (percentage) and _ (underscore). */ ExpressionList like(String propertyName, String value); /** * Case insensitive Like - property like value where the value contains the * SQL wild card characters % (percentage) and _ (underscore). Typically uses * a lower() function to make the expression case insensitive. */ ExpressionList ilike(String propertyName, String value); /** * Starts With - property like value%. */ ExpressionList startsWith(String propertyName, String value); /** * Case insensitive Starts With - property like value%. Typically uses a * lower() function to make the expression case insensitive. */ ExpressionList istartsWith(String propertyName, String value); /** * Ends With - property like %value. */ ExpressionList endsWith(String propertyName, String value); /** * Case insensitive Ends With - property like %value. Typically uses a lower() * function to make the expression case insensitive. */ ExpressionList iendsWith(String propertyName, String value); /** * Contains - property like %value%. */ ExpressionList contains(String propertyName, String value); /** * Case insensitive Contains - property like %value%. Typically uses a lower() * function to make the expression case insensitive. */ ExpressionList icontains(String propertyName, String value); /** * In - using a subQuery. */ ExpressionList in(String propertyName, Query subQuery); /** * In - property has a value in the array of values. */ ExpressionList in(String propertyName, Object... values); /** * In - property has a value in the collection of values. */ ExpressionList in(String propertyName, Collection values); /** * Not In - property has a value in the array of values. */ ExpressionList notIn(String propertyName, Object... values); /** * Not In - property has a value in the collection of values. */ ExpressionList notIn(String propertyName, Collection values); /** * Not In - using a subQuery. */ ExpressionList notIn(String propertyName, Query subQuery); /** * Is empty expression for collection properties. */ ExpressionList isEmpty(String propertyName); /** * Is not empty expression for collection properties. */ ExpressionList isNotEmpty(String propertyName); /** * Exists expression */ ExpressionList exists(Query subQuery); /** * Not exists expression */ ExpressionList notExists(Query subQuery); /** * Id IN a list of id values. */ ExpressionList idIn(Object... idValues); /** * Id IN a list of id values. */ ExpressionList idIn(List idValues); /** * Id Equal to - ID property is equal to the value. */ ExpressionList idEq(Object value); /** * All Equal - Map containing property names and their values. *

* Expression where all the property names in the map are equal to the * corresponding value. *

* * @param propertyMap * a map keyed by property names. */ ExpressionList allEq(Map propertyMap); /** * Array property contains entries with the given values. */ ExpressionList arrayContains(String propertyName, Object... values); /** * Array does not contain the given values. *

* Array support is effectively limited to Postgres at this time. *

*/ ExpressionList arrayNotContains(String propertyName, Object... values); /** * Array is empty - for the given array property. *

* Array support is effectively limited to Postgres at this time. *

*/ ExpressionList arrayIsEmpty(String propertyName); /** * Array is not empty - for the given array property. *

* Array support is effectively limited to Postgres at this time. *

*/ ExpressionList arrayIsNotEmpty(String propertyName); /** * Add raw expression with a single parameter. *

* The raw expression should contain a single ? at the location of the * parameter. *

*

* When properties in the clause are fully qualified as table-column names * then they are not translated. logical property name names (not fully * qualified) will still be translated to their physical name. *

* *

Example:

*
{@code
   *
   *   // use a database function
   *   raw("add_days(orderDate, 10) < ?", someDate)
   *
   * }
*/ ExpressionList raw(String raw, Object value); /** * Add raw expression with an array of parameters. *

* The raw expression should contain the same number of ? as there are * parameters. *

*

* When properties in the clause are fully qualified as table-column names * then they are not translated. logical property name names (not fully * qualified) will still be translated to their physical name. *

*/ ExpressionList raw(String raw, Object... values); /** * Add raw expression with no parameters. *

* When properties in the clause are fully qualified as table-column names * then they are not translated. logical property name names (not fully * qualified) will still be translated to their physical name. *

* *
{@code
   *
   *   raw("orderQty < shipQty")
   *
   * }
*/ ExpressionList raw(String raw); /** * Add a match expression. * * @param propertyName The property name for the match * @param search The search value */ ExpressionList match(String propertyName, String search); /** * Add a match expression with options. * * @param propertyName The property name for the match * @param search The search value */ ExpressionList match(String propertyName, String search, Match options); /** * Add a multi-match expression. */ ExpressionList multiMatch(String search, String... properties); /** * Add a multi-match expression using options. */ ExpressionList multiMatch(String search, MultiMatch options); /** * Add a simple query string expression. */ ExpressionList textSimple(String search, TextSimple options); /** * Add a query string expression. */ ExpressionList textQueryString(String search, TextQueryString options); /** * Add common terms expression. */ ExpressionList textCommonTerms(String search, TextCommonTerms options); /** * And - join two expressions with a logical and. */ ExpressionList and(Expression expOne, Expression expTwo); /** * Or - join two expressions with a logical or. */ ExpressionList or(Expression expOne, Expression expTwo); /** * Negate the expression (prefix it with NOT). */ ExpressionList not(Expression exp); /** * Start a list of expressions that will be joined by AND's * returning the expression list the expressions are added to. *

* This is exactly the same as conjunction(); *

*

* Use endAnd() or endJunction() to end the AND junction. *

*

* Note that a where() clause defaults to an AND junction so * typically you only explicitly need to use the and() junction * when it is nested inside an or() or not() junction. *

* *
{@code
   *
   *  // Example: Nested and()
   *
   *  Ebean.find(Customer.class)
   *    .where()
   *    .or()
   *      .and() // nested and
   *        .startsWith("name", "r")
   *        .eq("anniversary", onAfter)
   *        .endAnd()
   *      .and()
   *        .eq("status", Customer.Status.ACTIVE)
   *        .gt("id", 0)
   *        .endAnd()
   *      .orderBy().asc("name")
   *      .findList();
   * }
*/ Junction and(); /** * Return a list of expressions that will be joined by OR's. * This is exactly the same as disjunction(); * *

* Use endOr() or endJunction() to end the OR junction. *

* *
{@code
   *
   *  // Example: Use or() to join
   *  // two nested and() expressions
   *
   *  Ebean.find(Customer.class)
   *    .where()
   *    .or()
   *      .and()
   *        .startsWith("name", "r")
   *        .eq("anniversary", onAfter)
   *        .endAnd()
   *      .and()
   *        .eq("status", Customer.Status.ACTIVE)
   *        .gt("id", 0)
   *        .endAnd()
   *      .orderBy().asc("name")
   *      .findList();
   *
   * }
*/ Junction or(); /** * Return a list of expressions that will be wrapped by NOT. *

* Use endNot() or endJunction() to end expressions being added to the * NOT expression list. *

* *
@{code
   *
   *    .where()
   *      .not()
   *        .gt("id", 1)
   *        .eq("anniversary", onAfter)
   *        .endNot()
   *
   * }
* *
@{code
   *
   * // Example: nested not()
   *
   * Ebean.find(Customer.class)
   *   .where()
   *     .eq("status", Customer.Status.ACTIVE)
   *     .not()
   *       .gt("id", 1)
   *       .eq("anniversary", onAfter)
   *       .endNot()
   *     .orderBy()
   *       .asc("name")
   *     .findList();
   *
   * }
*/ Junction not(); /** * Start (and return) a list of expressions that will be joined by AND's. *

* This is the same as and(). *

*/ Junction conjunction(); /** * Start (and return) a list of expressions that will be joined by OR's. *

* This is the same as or(). *

*/ Junction disjunction(); /** * Start a list of expressions that will be joined by MUST. *

* This automatically makes the query a useDocStore(true) query that * will execute against the document store (ElasticSearch etc). *

*

* This is logically similar to and(). *

*/ Junction must(); /** * Start a list of expressions that will be joined by SHOULD. *

* This automatically makes the query a useDocStore(true) query that * will execute against the document store (ElasticSearch etc). *

*

* This is logically similar to or(). *

*/ Junction should(); /** * Start a list of expressions that will be joined by MUST NOT. *

* This automatically makes the query a useDocStore(true) query that * will execute against the document store (ElasticSearch etc). *

*

* This is logically similar to not(). *

*/ Junction mustNot(); /** * End a junction returning the parent expression list. *

* Ends a and(), or(), not(), must(), mustNot() or should() junction * such that you get the parent expression. *

*

* Alternatively you can always use where() to return the top level expression list. *

*/ ExpressionList endJunction(); /** * End a AND junction - synonym for endJunction(). */ ExpressionList endAnd(); /** * End a AND junction - synonym for endJunction(). */ ExpressionList endOr(); /** * End a NOT junction - synonym for endJunction(). */ ExpressionList endNot(); }