Class ExpressionFactory

java.lang.Object
org.apache.cayenne.exp.ExpressionFactory

public class ExpressionFactory extends Object
Helper class to build expressions.
  • Field Details

    • SPLIT_SEPARATOR

      public static final char SPLIT_SEPARATOR
      A "split" character, "|", that is understood by some of the ExpressionFactory methods that require splitting joins in the middle of the path.
      Since:
      3.0
      See Also:
  • Constructor Details

    • ExpressionFactory

      public ExpressionFactory()
  • Method Details

    • wrapPathOperand

      protected static Object wrapPathOperand(Object op)
      Applies a few default rules for adding operands to expressions. In particular wraps all lists into LIST expressions. Applied only in path expressions.
    • matchAnyDbExp

      public static Expression matchAnyDbExp(Map<String,?> map)
      Creates an expression that matches any of the key-values pairs in map: an OR of "key = value" comparisons, each key taken as a DB_PATH.
      Since:
      5.0
    • matchAllDbExp

      public static Expression matchAllDbExp(Map<String,?> map)
      Creates an expression that matches all key-values pairs in map: an AND of "key = value" comparisons, each key taken as a DB_PATH.
      Since:
      5.0
    • matchAnyExp

      public static Expression matchAnyExp(Map<String,?> map)
      Creates an expression that matches any of the key-values pairs in the map: an OR of "key = value" comparisons, each key taken as an OBJ_PATH.
      Since:
      5.0
    • matchAllExp

      public static Expression matchAllExp(Map<String,?> map)
      Creates an expression that matches all key-values pairs in map: an AND of "key = value" comparisons, each key taken as an OBJ_PATH.
      Since:
      5.0
    • matchAllExp

      public static Expression matchAllExp(String path, Collection<?> values)
      Creates an expression to match a collection of values against a single path expression.

      Splits

      Note that "path" argument here can use a split character (a pipe symbol - '|') instead of dot to indicate that relationship following a path should be split into a separate set of joins. There can only be one split at most. Split must always precede a relationship. E.g. "|exhibits.paintings", "exhibits|paintings", etc.

      Parameters:
      path - expression
      values - collection to match
      Since:
      3.0
    • matchAllExp

      public static Expression matchAllExp(String path, Object... values)
      Since:
      3.0
    • matchDbExp

      public static Expression matchDbExp(String pathSpec, Object value)
      A convenience method to create an DB_PATH "equal to" expression.
    • noMatchDbExp

      public static Expression noMatchDbExp(String pathSpec, Object value)
      A convenience method to create an DB_PATH "not equal to" expression.
    • matchExp

      public static Expression matchExp(String pathSpec, Object value)
      A convenience method to create an OBJ_PATH "equal to" expression.
    • matchExp

      public static Expression matchExp(Expression exp, Object value)
      Since:
      4.0
      See Also:
    • noMatchExp

      public static Expression noMatchExp(String pathSpec, Object value)
      A convenience method to create an OBJ_PATH "not equal to" expression.
    • noMatchExp

      public static Expression noMatchExp(Expression exp, Object value)
      Since:
      4.0
      See Also:
    • matchDbIdExp

      public static Expression matchDbIdExp(String pathSpec, Object value)
      A convenience method to create an DBID_PATH "equal to" expression.
      Since:
      4.2
    • noMatchDbIdExp

      public static Expression noMatchDbIdExp(String pathSpec, Object value)
      A convenience method to create an DBID_PATH "not equal to" expression.
      Since:
      4.2
    • lessExp

      public static Expression lessExp(String pathSpec, Object value)
      A convenience method to create an OBJ_PATH "less than" expression.
    • lessExp

      public static Expression lessExp(Expression exp, Object value)
      Since:
      4.0
      See Also:
    • lessDbExp

      public static Expression lessDbExp(String pathSpec, Object value)
      A convenience method to create an DB_PATH "less than" expression.
      Since:
      3.0
    • lessOrEqualExp

      public static Expression lessOrEqualExp(String pathSpec, Object value)
      A convenience method to create an OBJ_PATH "less than or equal to" expression.
    • lessOrEqualExp

      public static Expression lessOrEqualExp(Expression exp, Object value)
      Since:
      4.0
      See Also:
    • lessOrEqualDbExp

      public static Expression lessOrEqualDbExp(String pathSpec, Object value)
      A convenience method to create an DB_PATH "less than or equal to" expression.
      Since:
      3.0
    • greaterExp

      public static Expression greaterExp(String pathSpec, Object value)
      A convenience method to create an OBJ_PATH "greater than" expression.
    • greaterExp

      public static Expression greaterExp(Expression exp, Object value)
      Since:
      4.0
      See Also:
    • greaterDbExp

      public static Expression greaterDbExp(String pathSpec, Object value)
      A convenience method to create an DB_PATH "greater than" expression.
      Since:
      3.0
    • greaterOrEqualExp

      public static Expression greaterOrEqualExp(String pathSpec, Object value)
      A convenience method to create an OBJ_PATH "greater than or equal to" expression.
    • greaterOrEqualExp

      public static Expression greaterOrEqualExp(Expression exp, Object value)
      Since:
      4.0
      See Also:
    • greaterOrEqualDbExp

      public static Expression greaterOrEqualDbExp(String pathSpec, Object value)
      A convenience method to create an DB_PATH "greater than or equal to" expression.
      Since:
      3.0
    • inExp

      public static Expression inExp(String pathSpec, Object... values)
      A convenience shortcut for building IN expression. Return FalseExp for empty collection.
    • inExp

      public static Expression inExp(Expression exp, Object... values)
      Since:
      4.0
      See Also:
    • inDbExp

      public static Expression inDbExp(String pathSpec, Object... values)
      A convenience shortcut for building IN DB expression. Return FalseExp for empty collection.
    • inExp

      public static Expression inExp(String pathSpec, Collection<?> values)
      A convenience shortcut for building IN expression. Return FalseExp for empty collection.
    • inDbIdExp

      public static Expression inDbIdExp(String pathSpec, Object... values)
      A convenience shortcut for building IN DBID expression. Return FalseExp for empty collection.
      Since:
      4.2
    • inExp

      public static Expression inExp(Expression exp, Collection<?> values)
      Since:
      4.0
      See Also:
    • inDbExp

      public static Expression inDbExp(String pathSpec, Collection<?> values)
      A convenience shortcut for building IN DB expression. Return FalseExp for empty collection.
    • inDbIdExp

      public static Expression inDbIdExp(String pathSpec, Collection<?> values)
      A convenience shortcut for building IN DBID expression. Return FalseExp for empty collection.
      Since:
      4.2
    • notInExp

      public static Expression notInExp(String pathSpec, Collection<?> values)
      A convenience shortcut for building NOT_IN expression. Return TrueExp for empty collection.
    • notInExp

      public static Expression notInExp(Expression exp, Collection<?> values)
      Since:
      4.0
      See Also:
    • notInDbExp

      public static Expression notInDbExp(String pathSpec, Collection<?> values)
      A convenience shortcut for building NOT_IN expression. Return TrueExp for empty collection.
      Since:
      3.0
    • notInDbIdExp

      public static Expression notInDbIdExp(String pathSpec, Collection<?> values)
      A convenience shortcut for building NOT_IN expression. Return TrueExp for empty collection.
      Since:
      4.2
    • notInExp

      public static Expression notInExp(String pathSpec, Object... values)
      A convenience shortcut for building NOT_IN expression. Return TrueExp for empty collection.
      Since:
      1.0.6
    • notInExp

      public static Expression notInExp(Expression exp, Object... values)
      Since:
      4.0
      See Also:
    • notInDbExp

      public static Expression notInDbExp(String pathSpec, Object... values)
      A convenience shortcut for building NOT_IN expression. Return TrueExp for empty collection.
      Since:
      3.0
    • notInDbIdExp

      public static Expression notInDbIdExp(String pathSpec, Object... values)
      A convenience shortcut for building NOT_IN expression. Return TrueExp for empty collection.
      Since:
      4.2
    • betweenExp

      public static Expression betweenExp(String pathSpec, Object value1, Object value2)
      A convenience shortcut for building BETWEEN expressions.
    • betweenExp

      public static Expression betweenExp(Expression exp, Object value1, Object value2)
      Since:
      4.0
      See Also:
    • betweenDbExp

      public static Expression betweenDbExp(String pathSpec, Object value1, Object value2)
      A convenience shortcut for building BETWEEN expressions.
      Since:
      3.0
    • notBetweenExp

      public static Expression notBetweenExp(String pathSpec, Object value1, Object value2)
      A convenience shortcut for building NOT_BETWEEN expressions.
    • notBetweenExp

      public static Expression notBetweenExp(Expression exp, Object value1, Object value2)
      Since:
      4.0
      See Also:
    • notBetweenDbExp

      public static Expression notBetweenDbExp(String pathSpec, Object value1, Object value2)
      A convenience shortcut for building NOT_BETWEEN expressions.
      Since:
      3.0
    • likeExp

      public static Expression likeExp(String pathSpec, Object value)
      A convenience shortcut for building LIKE expression.
    • likeExp

      public static Expression likeExp(Expression exp, Object value)
      Since:
      4.0
      See Also:
    • likeExp

      public static Expression likeExp(String pathSpec, Object value, char escapeChar)

      A convenience shortcut for building LIKE expression.

      The escape character allows for escaping meta-characters in the LIKE clause. Note that the escape character cannot be '?'. To specify no escape character, supply 0 as the escape character.

      Since:
      3.0.1
    • likeExp

      public static Expression likeExp(Expression exp, Object value, char escapeChar)
      Since:
      4.0
      See Also:
    • likeDbExp

      public static Expression likeDbExp(String pathSpec, Object value)
      A convenience shortcut for building LIKE DB_PATH expression.
      Since:
      3.0
    • likeDbExp

      public static Expression likeDbExp(String pathSpec, Object value, char escapeChar)

      A convenience shortcut for building LIKE DB_PATH expression.

      The escape character allows for escaping meta-characters in the LIKE clause. Note that the escape character cannot be '?'. To specify no escape character, supply 0 as the escape character.

      Since:
      3.0.1
    • notLikeExp

      public static Expression notLikeExp(String pathSpec, Object value)
      A convenience shortcut for building NOT_LIKE expression.
    • notLikeExp

      public static Expression notLikeExp(Expression exp, Object value)
      Since:
      4.0
      See Also:
    • notLikeExp

      public static Expression notLikeExp(String pathSpec, Object value, char escapeChar)

      A convenience shortcut for building NOT_LIKE expression.

      The escape character allows for escaping meta-characters in the LIKE clause. Note that the escape character cannot be '?'. To specify no escape character, supply 0 as the escape character.

      Since:
      3.0.1
    • notLikeExp

      public static Expression notLikeExp(Expression exp, Object value, char escapeChar)
      Since:
      4.0
      See Also:
    • notLikeDbExp

      public static Expression notLikeDbExp(String pathSpec, Object value)
      A convenience shortcut for building NOT_LIKE expression.
      Since:
      3.0
    • notLikeDbExp

      public static Expression notLikeDbExp(String pathSpec, Object value, char escapeChar)

      A convenience shortcut for building NOT_LIKE expression.

      The escape character allows for escaping meta-characters in the LIKE clause. Note that the escape character cannot be '?'. To specify no escape character, supply 0 as the escape character.

      Since:
      3.0.1
    • likeIgnoreCaseExp

      public static Expression likeIgnoreCaseExp(String pathSpec, Object value)
      A convenience shortcut for building LIKE_IGNORE_CASE expression.
    • likeIgnoreCaseExp

      public static Expression likeIgnoreCaseExp(Expression exp, Object value)
      Since:
      4.0
      See Also:
    • likeIgnoreCaseExp

      public static Expression likeIgnoreCaseExp(String pathSpec, Object value, char escapeChar)

      A convenience shortcut for building LIKE_IGNORE_CASE expression.

      The escape character allows for escaping meta-characters in the LIKE clause. Note that the escape character cannot be '?'. To specify no escape character, supply 0 as the escape character.

      Since:
      3.0.1
    • likeIgnoreCaseDbExp

      public static Expression likeIgnoreCaseDbExp(String pathSpec, Object value)
      A convenience shortcut for building LIKE_IGNORE_CASE expression.
      Since:
      3.0
    • likeIgnoreCaseDbExp

      public static Expression likeIgnoreCaseDbExp(String pathSpec, Object value, char escapeChar)

      A convenience shortcut for building LIKE_IGNORE_CASE expression.

      The escape character allows for escaping meta-characters in the LIKE clause. Note that the escape character cannot be '?'. To specify no escape character, supply 0 as the escape character.

      Since:
      3.0.1
    • notLikeIgnoreCaseExp

      public static Expression notLikeIgnoreCaseExp(String pathSpec, Object value)
      A convenience shortcut for building NOT_LIKE_IGNORE_CASE expression.
    • notLikeIgnoreCaseExp

      public static Expression notLikeIgnoreCaseExp(Expression exp, Object value)
      Since:
      4.0
      See Also:
    • notLikeIgnoreCaseExp

      public static Expression notLikeIgnoreCaseExp(String pathSpec, Object value, char escapeChar)

      A convenience shortcut for building NOT_LIKE_IGNORE_CASE expression.

      The escape character allows for escaping meta-characters in the LIKE clause. Note that the escape character cannot be '?'. To specify no escape character, supply 0 as the escape character.

      Since:
      3.0.1
    • notLikeIgnoreCaseExp

      public static Expression notLikeIgnoreCaseExp(Expression exp, Object value, char escapeChar)
      Since:
      4.0
      See Also:
    • notLikeIgnoreCaseDbExp

      public static Expression notLikeIgnoreCaseDbExp(String pathSpec, Object value)
      A convenience shortcut for building NOT_LIKE_IGNORE_CASE expression.
      Since:
      3.0
    • notLikeIgnoreCaseDbExp

      public static Expression notLikeIgnoreCaseDbExp(String pathSpec, Object value, char escapeChar)

      A convenience shortcut for building NOT_LIKE_IGNORE_CASE expression.

      The escape character allows for escaping meta-characters in the LIKE clause. Note that the escape character cannot be '?'. To specify no escape character, supply 0 as the escape character.

      Since:
      3.0.1
    • containsExp

      public static Expression containsExp(String pathSpec, String value)
      Returns:
      An expression for a database "LIKE" query with the value converted to a pattern matching anywhere in the String.
      Since:
      4.0
    • containsExp

      public static Expression containsExp(Expression exp, String value)
      Since:
      4.0
      See Also:
    • startsWithExp

      public static Expression startsWithExp(String pathSpec, String value)
      Returns:
      An expression for a database "LIKE" query with the value converted to a pattern matching the beginning of the String.
      Since:
      4.0
    • startsWithExp

      public static Expression startsWithExp(Expression exp, String value)
      Since:
      4.0
      See Also:
    • endsWithExp

      public static Expression endsWithExp(String pathSpec, String value)
      Returns:
      An expression for a database "LIKE" query with the value converted to a pattern matching the beginning of the String.
      Since:
      4.0
    • endsWithExp

      public static Expression endsWithExp(Expression exp, String value)
      Since:
      4.0
      See Also:
    • containsIgnoreCaseExp

      public static Expression containsIgnoreCaseExp(String pathSpec, String value)
      Same as containsExp(String, String) only using case-insensitive comparison.
      Since:
      4.0
    • containsIgnoreCaseExp

      public static Expression containsIgnoreCaseExp(Expression exp, String value)
      Since:
      4.0
      See Also:
    • startsWithIgnoreCaseExp

      public static Expression startsWithIgnoreCaseExp(String pathSpec, String value)
      Same as startsWithExp(String, String) only using case-insensitive comparison.
      Since:
      4.0
    • startsWithIgnoreCaseExp

      public static Expression startsWithIgnoreCaseExp(Expression exp, String value)
      Since:
      4.0
      See Also:
    • endsWithIgnoreCaseExp

      public static Expression endsWithIgnoreCaseExp(String pathSpec, String value)
      Same as endsWithExp(String, String) only using case-insensitive comparison.
      Since:
      4.0
    • endsWithIgnoreCaseExp

      public static Expression endsWithIgnoreCaseExp(Expression exp, String value)
      Since:
      4.0
      See Also:
    • pathExp

      public static Expression pathExp(String pathSpec)
      Parameters:
      pathSpec - a String "obj:" path.
      Returns:
      a new "obj:" path expression for the specified String path.
      Since:
      4.0
    • pathExp

      public static Expression pathExp(CayennePath path)
      Parameters:
      path - a path value.
      Returns:
      a new "obj:" path expression for the specified path.
      Since:
      5.0
    • dbPathExp

      public static Expression dbPathExp(String pathSpec)
      Parameters:
      pathSpec - a String db: path.
      Returns:
      a new "db:" path expression for the specified String path.
      Since:
      4.0
    • dbPathExp

      public static Expression dbPathExp(CayennePath path)
      Parameters:
      path - a path value
      Returns:
      a new "db:" path expression for the specified path.
      Since:
      5.0
    • dbIdPathExp

      public static Expression dbIdPathExp(String pathSpec)
      Parameters:
      pathSpec - a String "dbid:" path
      Returns:
      a new "dbid:" path expression for the specified String path
      Since:
      4.2
    • dbIdPathExp

      public static Expression dbIdPathExp(CayennePath pathSpec)
      Parameters:
      pathSpec - a "dbid:" path value
      Returns:
      a new "dbid:" path expression for the specified path
      Since:
      5.0
    • expTrue

      public static Expression expTrue()
      A convenience shortcut for boolean true expression.
      Since:
      3.0
    • expFalse

      public static Expression expFalse()
      A convenience shortcut for boolean false expression.
      Since:
      3.0
    • matchExp

      public static Expression matchExp(Persistent object)
      Creates an expression that matches the primary key of object in ObjectId's IdSnapshot for the argument object.
    • matchAnyExp

      public static Expression matchAnyExp(List<? extends Persistent> objects)
      Creates an expression that matches any of the objects contained in the list objects
    • matchAnyExp

      public static Expression matchAnyExp(Persistent... objects)
      Creates an expression that matches any of the objects contained in the objects array
    • fullObjectExp

      public static Expression fullObjectExp()
    • fullObjectExp

      @Deprecated(since="5.0", forRemoval=true) public static Expression fullObjectExp(Expression exp)
      Deprecated, for removal: This API element is subject to removal in a future version.
      a relationship path expression is all that is needed to select related objects as a column
    • enclosingObjectExp

      public static Expression enclosingObjectExp(Expression exp)
      Since:
      4.2
    • and

      public static Expression and(Collection<Expression> expressions)
      Since:
      4.0
    • and

      public static Expression and(Expression... expressions)
      Since:
      4.0
    • or

      public static Expression or(Collection<Expression> expressions)
      Since:
      4.0
    • or

      public static Expression or(Expression... expressions)
      Since:
      4.0
    • exp

      public static Expression exp(String expressionString, Object... parameters)
      Parses string, converting it to Expression and optionally binding positional parameters. If a string does not represent a semantically correct expression, an ExpressionException is thrown.

      Binding of parameters by name (as opposed to binding by position) can be achieved by chaining this call with Expression.params(Map).

      Since:
      4.0
    • wrapScalarValue

      public static Expression wrapScalarValue(Object value)
      Wraps a value into an expression, for the places that take an expression rather than a value: e.g. a constant select column, or a whole where clause.
      Since:
      4.0
    • exists

      public static Expression exists(FluentSelect<?,?> subQuery)
      Parameters:
      subQuery - ObjectSelect or ColumnSelect
      Since:
      4.2
    • exists

      public static Expression exists(Expression exp)
      Builds expression representing EXIST subquery over a given path
      Parameters:
      exp - expression to use for an EXISTS
      Returns:
      expression representing exists subquery
      Since:
      5.0
    • notExists

      public static Expression notExists(FluentSelect<?,?> subQuery)
      Parameters:
      subQuery - ObjectSelect or ColumnSelect
      Since:
      4.2
    • notExists

      public static Expression notExists(Expression exp)
      Builds expression representing NOT EXIST subquery over a given path
      Parameters:
      exp - expression to use for an NOT EXISTS
      Returns:
      expression representing exists subquery
      Since:
      5.0
    • inExp

      public static Expression inExp(Expression exp, ColumnSelect<?> subQuery)
      Since:
      4.2
    • notInExp

      public static Expression notInExp(Expression exp, ColumnSelect<?> subQuery)
      Since:
      4.2
    • all

      public static Expression all(ColumnSelect<?> subquery)
      Since:
      5.0
    • any

      public static Expression any(ColumnSelect<?> subquery)
      Since:
      5.0
    • caseWhen

      public static Expression caseWhen(List<Expression> whenExp, List<Expression> thenExp)
      Since:
      5.0
    • caseWhen

      public static Expression caseWhen(List<Expression> whenExp, List<Expression> thenExp, Expression caseDefault)
      Since:
      5.0