ca.krasnay.sqlbuilder
Class Predicates

java.lang.Object
  extended by ca.krasnay.sqlbuilder.Predicates

public final class Predicates
extends Object

Collection of commonly used predicates, implemented by static methods.

Most predicates accept a SQL expression and one or more values to which the SQL is compared. Predicates do not escape this expression. As such, do not accept arbitrary expressions from users or other sources, as it may be a source of SQL injection vulnerabilities. The normal use-case is that these expressions are hard-coded in your application. Values, on the other hand, are substituted as proper prepared statement parameters so they are safe from SQL injection.

Author:
John Krasnay

Constructor Summary
Predicates()
           
 
Method Summary
static Predicate all()
          Returns a predicate that does not constrain the result set.
static Predicate allBitsSet(String expr, long bits)
          Adds a clause that checks whether ALL set bits in a bitmask are present in a numeric expression.
static Predicate and(List<Predicate> predicates)
          Joins a series of predicates with AND.
static Predicate and(Predicate... predicates)
          Joins a series of predicates with AND.
static Predicate anyBitsSet(String expr, long bits)
          Adds a clause that checks whether ANY set bits in a bitmask are present in a numeric expression.
static Predicate eq(String expr, Object value)
          Adds an equals clause to a creator.
static ExistsPredicate exists(String table)
          Adds an EXISTS clause to a creator.
static Predicate in(String expr, List<?> values)
          Adds an IN clause to a creator.
static Predicate in(String expr, Object... values)
          Adds an IN clause to a creator.
static Predicate is(String sql)
          Returns a predicate that takes no parameters.
static Predicate neq(String expr, Object value)
          Adds a not equals clause to a creator.
static Predicate not(Predicate childPredicate)
          Inverts the sense of the given child predicate.
static Predicate or(List<Predicate> predicates)
          Joins a series of predicates with OR.
static Predicate or(Predicate... predicates)
          Joins a series of predicates with OR.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Predicates

public Predicates()
Method Detail

all

public static Predicate all()
Returns a predicate that does not constrain the result set.


allBitsSet

public static Predicate allBitsSet(String expr,
                                   long bits)
Adds a clause that checks whether ALL set bits in a bitmask are present in a numeric expression.

Parameters:
expr - SQL numeric expression to check.
bits - Integer containing the bits for which to check.

and

public static Predicate and(Predicate... predicates)
Joins a series of predicates with AND.


and

public static Predicate and(List<Predicate> predicates)
Joins a series of predicates with AND.


anyBitsSet

public static Predicate anyBitsSet(String expr,
                                   long bits)
Adds a clause that checks whether ANY set bits in a bitmask are present in a numeric expression.

Parameters:
expr - SQL numeric expression to check.
bits - Integer containing the bits for which to check.

eq

public static Predicate eq(String expr,
                           Object value)
Adds an equals clause to a creator.

Parameters:
expr - SQL expression to be compared for equality.
value - Value to which the SQL expression is compared.

in

public static Predicate in(String expr,
                           List<?> values)
Adds an IN clause to a creator.

Parameters:
expr - SQL expression to be tested for inclusion.
values - Values for the IN clause.

in

public static Predicate in(String expr,
                           Object... values)
Adds an IN clause to a creator.

Parameters:
expr - SQL expression to be tested for inclusion.
values - Values for the IN clause.

is

public static Predicate is(String sql)
Returns a predicate that takes no parameters. The given SQL expression is used directly.

Parameters:
sql - SQL text of the expression

exists

public static ExistsPredicate exists(String table)
Adds an EXISTS clause to a creator. Typical usage is as follows:
 new SelectCreator()
 .column("name")
 .from("Emp e")
 .where(exists("SickDay sd").where("sd.emp_id = e.id").and(eq("sd.dow", "Monday")));
 

Parameters:
table - Table that forms the basis of the sub-select.

neq

public static Predicate neq(String expr,
                            Object value)
Adds a not equals clause to a creator.

Parameters:
expr - SQL expression to be compared for equality.
value - Value to which the SQL expression is compared.

not

public static Predicate not(Predicate childPredicate)
Inverts the sense of the given child predicate. In SQL terms, this surrounds the given predicate with "not (...)".

Parameters:
childPredicate - Predicate whose sense is to be inverted.

or

public static Predicate or(Predicate... predicates)
Joins a series of predicates with OR.


or

public static Predicate or(List<Predicate> predicates)
Joins a series of predicates with OR.



Copyright © 2014. All rights reserved.