Class Expressions

java.lang.Object
net.sf.basedb.core.query.Expressions

public class Expressions
extends Object
A factory class to create expressions.
Version:
2.0
Author:
Samuel, Nicklas
Last modified
$Date: 2015-12-04 08:47:16 +0100 (fr, 04 dec 2015) $
  • Field Details

    • PARAMETER_REGEXP

      public static final Pattern PARAMETER_REGEXP
      A parameter name can only contain the characters a-z, A-Z or 0-9.
  • Constructor Details

    • Expressions

      public Expressions()
  • Method Details

    • add

      public static Expression add​(Expression e1, Expression e2) throws InvalidDataException
      Add two expressions: new expression = e1 + e2.
      Parameters:
      e1 - The left value
      e2 - The right value
      Returns:
      The new expression
      Throws:
      InvalidDataException - If any of the arguments are null
    • subtract

      public static Expression subtract​(Expression e1, Expression e2) throws InvalidDataException
      Subtract one expression from another: new expression = e1 - e2.
      Parameters:
      e1 - The left value
      e2 - The right value
      Returns:
      The new expression
      Throws:
      InvalidDataException - If any of the arguments are null
    • divide

      public static Expression divide​(Expression e1, Expression e2) throws InvalidDataException
      Divide one expression by another: new expression = e1 / e2.
      Parameters:
      e1 - The numerator
      e2 - The denominator
      Returns:
      The new expression
      Throws:
      InvalidDataException - If any of the arguments are null
    • multiply

      public static Expression multiply​(Expression e1, Expression e2) throws InvalidDataException
      Multiply two expressions: new expression = e1 * e2.
      Parameters:
      e1 - The left value
      e2 - The right value
      Returns:
      The new expression
      Throws:
      InvalidDataException - If any of the arguments are null
    • negate

      public static Expression negate​(Expression e) throws InvalidDataException
      Negate an expression: new expression = -e1.
      Parameters:
      e - The expression to negate
      Returns:
      The new expression
      Throws:
      InvalidDataException - If the argument is null
    • integer

      public static Expression integer​(int value)
      Create a constant expression from an integer.
      Parameters:
      value - The value to create an expression for
      Returns:
      The expression
    • aFloat

      public static Expression aFloat​(float value)
      Create a constant expression from a float.
      Parameters:
      value - The value to create an expression for
      Returns:
      The expression
    • string

      public static Expression string​(String value) throws InvalidDataException
      Create a constant expression from a string. Internally this is implemented as parameter with a default value. Ie. parameter(String, Object). In most cases it is better to use that method instead of this.
      Parameters:
      value - The string value
      Returns:
      The expression
      Throws:
      InvalidDataException - If the value is null
    • bool

      public static Expression bool​(boolean value)
      Create a constant expression from a boolean.
      Parameters:
      value - The value to create an expression for
      Returns:
      The expression
      Since:
      3.1
    • bool

      public static Expression bool​(Restriction restriction)
      Create a boolean expression view of a restriction.
      Parameters:
      restriction - The restriction
      Returns:
      The expression
      Since:
      3.1
    • parameter

      public static Expression parameter​(String name) throws InvalidDataException
      Create an expression for a parameter without a default value or type.
      Parameters:
      name - Name of the parameter. Can not be null.
      Returns:
      An expression.
      Throws:
      InvalidDataException - If the parameter is null.
      See Also:
      parameter(String, Object, Type), Query.setParameter(String, Object, Type)
    • parameter

      public static Expression parameter​(String name, Type valueType) throws InvalidDataException
      Create an expression for a typed parameter without a default value.
      Parameters:
      name - Name of the parameter. Can not be null
      valueType - Type of value the parameter holds.
      Returns:
      An expression
      Throws:
      InvalidDataException - If required argument is null.
      See Also:
      parameter(String, Object, Type), Query.setParameter(String, Object, Type)
    • parameter

      public static Expression parameter​(String name, Object value) throws InvalidDataException
      Create an expression for a parameter with a default value but no type.
      Parameters:
      name - Name of the parameter. Can not be null.
      value - Value of the parameter.
      Returns:
      An expression to use in queries.
      Throws:
      InvalidDataException - If required parameter is null.
      See Also:
      parameter(String, Object, Type)
    • parameter

      public static Expression parameter​(String name, Object value, Type valueType) throws InvalidDataException
      Create an expression for a parameter with a default value and type.
      Parameters:
      name - The name of the parameter. Can not be null
      value - The default value of the parameter, or null if no default is needed
      valueType - The type of the parameter, or null if not used
      Returns:
      An expression representing the parameter
      Throws:
      InvalidDataException - If the argument 'name' is null or contains invalid characters
      See Also:
      Query.setParameter(String, Object, Type), PARAMETER_REGEXP
    • selected

      public static Expression selected​(Select select) throws InvalidDataException
      Create an expression for an already selected query element. If an alias has been specified it will be used, otherwise the entire expression is built again.
      Parameters:
      select - A selected query element. Can not be null.
      Returns:
      A expression object representing the selected expression
      Throws:
      InvalidDataException - If the argument is null
    • log10

      public static Expression log10​(Expression e) throws InvalidDataException
      Calculate the 10-based logarithm of an expression: new expression = log10(e).
      Parameters:
      e - The expression to take the logarithm of
      Returns:
      The new expression
      Throws:
      InvalidDataException - If the argument is null
    • log2

      public static Expression log2​(Expression e) throws InvalidDataException
      Calculate the 2-based logarithm of an expression: new expression = log2(e).
      Parameters:
      e - The expression to take the logarithm of
      Returns:
      The new expression
      Throws:
      InvalidDataException - If the argument is null
    • ln

      public static Expression ln​(Expression e) throws InvalidDataException
      Calculate the natural logarithm of an expression: new expression = ln(e).
      Parameters:
      e - The expression to take the logarithm of
      Returns:
      The new expression
      Throws:
      InvalidDataException - If the argument is null
    • log

      public static Expression log​(double n, Expression e) throws InvalidDataException
      Calculate the n-based logarithm of an expression: new expression = log(n, e).
      Parameters:
      n - The log base, must be > 0
      e - The expression to take the logarithm of
      Returns:
      The new expression
      Throws:
      InvalidDataException - If the expression is null or if the base is 0 or below
    • exp

      public static Expression exp​(Expression e) throws InvalidDataException
      Calculate the exponential of an expression: new expression = exp(e).
      Parameters:
      e - The expression to take the exponential of
      Returns:
      The new expression
      Throws:
      InvalidDataException - If the argument is null
    • power

      public static Expression power​(Expression e1, Expression e2)
      Calculate the first expression raised to the power of the second expression: new expression = e1 ^ e2
      Parameters:
      e1 - The base expression
      e2 - The exponent expression
      Returns:
      The new expression
      Throws:
      InvalidDataException - If some of the arguments are null
      Since:
      2.12
    • sqrt

      public static Expression sqrt​(Expression e) throws InvalidDataException
      Calculate the square root of an expression: new expression = sqrt(e).
      Parameters:
      e - The expression to take the square root of
      Returns:
      The new expression
      Throws:
      InvalidDataException - If the argument is null
    • abs

      public static Expression abs​(Expression e) throws InvalidDataException
      Calculate the absolute value of an expression: new expression = abs(e).
      Parameters:
      e - The expression to take the absolute value of
      Returns:
      The new expression
      Throws:
      InvalidDataException - If the argument is null
    • toDate

      public static Expression toDate​(Expression e)
      Get the date part from a date or timestamp expression.
      Parameters:
      e - An expression which should evaluate to a date or timestamp
      Returns:
      The new expression
      Since:
      2.16
    • caseWhen

      public static Expression caseWhen​(Expression elseExpression, WhenStatement... whenStatements) throws InvalidDataException
      Create a conditional CASE-WHEN expression:
      CASE
      WHEN r1 THEN e1
      WHEN r2 THEN e2
      ...
      ELSE elseExpression
      END
      
      Parameters:
      elseExpression - The optional else part of the expression
      whenStatements - An array of WhenStatement:s
      Returns:
      The new expression
      Throws:
      InvalidDataException - If any of the when statements are null or if no when statements have been specified
    • any

      public static Expression any​(Query subquery)
      Create a subquery expression: ANY (subquery). The expression is useful in the WHERE part of a query only. For example, to find all users that are members of a group having a name starting with A.
      Query groupFilter = User.getQuery();
      groupFilter.join(Hql.innerJoin("groups", "grp"));
      groupFilter.restrict(Restrictions.like(Hql.property("grp", "name"), Expressions.string("A%")));
      Query userQuery = User.getQuery();
      userQuery.restrict(Restrictions.eq(Hql.property("id"), Expressions.any(groupFilter)));
      
      Parameters:
      subquery - The subquery
      Returns:
      The new expression
      Throws:
      InvalidDataException - If the subquery is null
      Since:
      2.9
    • all

      public static Expression all​(Query subquery)
      Create a subquery expression: ANY (subquery). WARNING!!! It is not recommended to use this method for creating restriction like <>ANY (subquery) since it may have a bad impact on performance. Consider using Restrictions.not(Restriction) and Restrictions.in(Expression, Query) instead.
      Parameters:
      subquery - The subquery
      Returns:
      The new expression
      Throws:
      InvalidDataException - If the subquery is null
      Since:
      2.9
      See Also:
      any(Query)
    • conditionalExpression

      public static Expression conditionalExpression​(Filter<? extends Query> filter, Expression ifTrue, Expression ifFalse)
      Create an expression that falls back to one of two expression depending on if the query passes a filter or not.
      Parameters:
      filter - A filter, if null the 'ifTrue' expression is returned
      ifTrue - The epxression to use if the filter evaluates to TRUE
      ifFalse - The expression to use if the filter evaluates to FALSE
      Returns:
      A conditional expression, or the 'ifTrue' expression if the filter is null, or null if both expressions are null
      Since:
      3.7
    • getFirstNull

      private static int getFirstNull​(Object[] array)
      Test if there is null in the array.
      Returns:
      The index of the first null element, or -1 if no null elements are found, or 0 if the array itself is null