Package net.sf.basedb.core.query
Class Restrictions
java.lang.Object
net.sf.basedb.core.query.Restrictions
A factory class to create restrictions.
- Version:
- 2.0
- Author:
- Samuel, Nicklas
- Last modified
- $Date: 2015-12-04 08:47:16 +0100 (fr, 04 dec 2015) $
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Restriction
and
(Restriction... r) Combine one or more restrictions with AND: new restriction = r[0] AND r[1] AND ...static Restriction
between
(Expression e, Expression low, Expression high) Compare if one expression falls between two other expressions: new expression = low <= e AND e < high.static Restriction
betweenInclusive
(Expression e, Expression low, Expression high) Compare if one expression falls between two other expressions (inclusive): new expression = low <= e AND e <= high.static Restriction
conditionalRestriction
(Filter<? extends Query> filter, Restriction ifTrue, Restriction ifFalse, boolean nullMatchNone) Create a restriction that falls back to one of two restrictions depending on if the query passes a filter or not.private static <T> T[]
copyNotNulls
(Collection<? extends T> source, T[] dest) private static <T> T[]
copyNotNulls
(T[] source, T[] dest) private static int
countNotNulls
(Object[] array) private static int
countNotNulls
(Collection<?> array) static Restriction
eq
(Expression e1, Expression e2) Compare if two expressions are equal: new restriction = e1 == e2.private static int
getFirstNull
(Object[] array) Test if there is null in the array.static Restriction
gt
(Expression e1, Expression e2) Compare if one expression is greater than another: new restriction = e1 > e2.static Restriction
gteq
(Expression e1, Expression e2) Compare if one expression is greater than or equal to another: new restriction = e1 >= e2.static Restriction
in
(Expression e1, Expression... e2) Check if an expression is contained in a set of other expressions: e1 IN (e2[0], e2[1], ...)static Restriction
in
(Expression e1, Query subquery) Create a subquery restriction:e1 IN (subquery)
.static Restriction
like
(Expression e1, Expression e2) Check if one expression matches another: e1 LIKE e2static Restriction
like_in
(Expression e1, Expression... e2) Check if one expression matches any expressions in an array: (e1 LIKE e2.1) OR (e1 LIKE e2.2) OR ....static Restriction
lt
(Expression e1, Expression e2) Compare if one expression is less than another: new restriction = e1 < e2.static Restriction
lteq
(Expression e1, Expression e2) Compare if one expression is less than or equal to another: new restriction = e1 <= e2.static Restriction
neq
(Expression e1, Expression e2) Compare if two expressions are inequal: new restriction = e1 <> e2.static Restriction
not
(Restriction r) Negate a restriction: new restriction = NOT rstatic Restriction
notLike_in
(Expression e1, Expression... e2) Check if one expression doesn't matches any of the expressions in an array.static Restriction
nullSafeAnd
(Collection<? extends Restriction> restrictions) Combine one or more restrictions with AND: new restriction = r[0] AND r[1] AND ...static Restriction
nullSafeAnd
(Restriction... r) Combine one or more restrictions with AND ignoring null elements.static Restriction
nullSafeOr
(Collection<? extends Restriction> restrictions) Combine one or more restrictions with OR ignoring null elements.static Restriction
nullSafeOr
(Restriction... r) Combine one or more restrictions with OR ignoring null elements.static Restriction
or
(Restriction... r) Combine one or more restrictions with OR: new restriction = r[0] OR r[1] ...static Restriction
rlike
(Expression e, Expression regexp) Check if an expression matches a regular expression.
-
Constructor Details
-
Restrictions
public Restrictions()
-
-
Method Details
-
and
Combine one or more restrictions with AND: new restriction = r[0] AND r[1] AND ...- Parameters:
r
- The restrictions to combine- Returns:
- The new restriction
- Throws:
InvalidDataException
- If any of the elements is null or the array doesn't contain at least one element
-
nullSafeAnd
Combine one or more restrictions with AND ignoring null elements.- Parameters:
r
- The restrictions to combine- Returns:
- The new restriction, or null if the array was null or didn't contain any not-null elements
- Throws:
InvalidDataException
- Since:
- 2.15
-
nullSafeAnd
public static Restriction nullSafeAnd(Collection<? extends Restriction> restrictions) throws InvalidDataException Combine one or more restrictions with AND: new restriction = r[0] AND r[1] AND ...- Parameters:
restrictions
- The restrictions to combine- Returns:
- The new restriction, or null if the collection was null or didn't contain any not-null elements
- Throws:
InvalidDataException
- Since:
- 3.5
-
or
Combine one or more restrictions with OR: new restriction = r[0] OR r[1] ...- Parameters:
r
- The restrictions to combine- Returns:
- The new restriction
- Throws:
InvalidDataException
- If any of the elements is null or the array doesn't contain at least one element
-
nullSafeOr
public static Restriction nullSafeOr(Collection<? extends Restriction> restrictions) throws InvalidDataException Combine one or more restrictions with OR ignoring null elements.- Parameters:
restrictions
- The restrictions to combine- Returns:
- The new restriction, or null if the array was null or didn't contain any not-null elements
- Throws:
InvalidDataException
- Since:
- 3.5
-
nullSafeOr
Combine one or more restrictions with OR ignoring null elements.- Parameters:
r
- The restrictions to combine- Returns:
- The new restriction, or null if the array was null or didn't contain any not-null elements
- Throws:
InvalidDataException
- Since:
- 2.15
-
not
Negate a restriction: new restriction = NOT r- Parameters:
r
- The restrictions to negate- Returns:
- The new restriction
- Throws:
InvalidDataException
- If the restriction is null
-
eq
Compare if two expressions are equal: new restriction = e1 == e2. If one of the expressions are null, the IS NULL operation is used: new restriction = e1 IS NULL (or e2 IS NULL).- Parameters:
e1
- The left expressione2
- The right expression- Returns:
- The new restriction
- Throws:
InvalidDataException
- If both expressions are null
-
neq
Compare if two expressions are inequal: new restriction = e1 <> e2. If one of the expressions are null, the NOT IS NULL operation is used: new restriction = NOT e1 IS NULL (or NOT e2 IS NULL).- Parameters:
e1
- The left expressione2
- The right expression- Returns:
- The new restriction
- Throws:
InvalidDataException
- If both expressions are null
-
gt
Compare if one expression is greater than another: new restriction = e1 > e2.- Parameters:
e1
- The left expressione2
- The right expression- Returns:
- The new restriction
- Throws:
InvalidDataException
- If any of the expressions are null
-
gteq
Compare if one expression is greater than or equal to another: new restriction = e1 >= e2.- Parameters:
e1
- The left expressione2
- The right expression- Returns:
- The new restriction
- Throws:
InvalidDataException
- If any of the expressions are null
-
lt
Compare if one expression is less than another: new restriction = e1 < e2.- Parameters:
e1
- The left expressione2
- The right expression- Returns:
- The new restriction
- Throws:
InvalidDataException
- If any of the expressions are null
-
lteq
Compare if one expression is less than or equal to another: new restriction = e1 <= e2.- Parameters:
e1
- The left expressione2
- The right expression- Returns:
- The new restriction
- Throws:
InvalidDataException
- If any of the expressions are null
-
between
public static Restriction between(Expression e, Expression low, Expression high) throws InvalidDataException Compare if one expression falls between two other expressions: new expression = low <= e AND e < high. Since the inclusion/exclusion of the low and high limits are dependent of the database BASE hase defined the between this way to make sure that BASE will treat BETWEEN the same way regardless of database.- Parameters:
e
- The expression to check against the limitslow
- The expression for the low limithigh
- The expression for the high limit- Returns:
- The new restriction
- Throws:
InvalidDataException
- If any of the expressions are null
-
betweenInclusive
public static Restriction betweenInclusive(Expression e, Expression low, Expression high) throws InvalidDataException Compare if one expression falls between two other expressions (inclusive): new expression = low <= e AND e <= high. Since the inclusion/exclusion of the low and high limits are dependent of the database BASE hase defined the between this way to make sure that BASE will treat BETWEEN the same way regardless of database.- Parameters:
e
- The expression to check against the limitslow
- The expression for the low limithigh
- The expression for the high limit- Returns:
- The new restriction
- Throws:
InvalidDataException
- If any of the expressions are null- Since:
- 3.0
-
in
Check if an expression is contained in a set of other expressions: e1 IN (e2[0], e2[1], ...)- Parameters:
e1
- The expression to checke2
- The set of expression to check against- Returns:
- The new restriction
- Throws:
InvalidDataException
- If the set is null or empty
-
in
Create a subquery restriction:e1 IN (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.in(Hql.property("id"), groupFilter));
- Parameters:
e1
- The left expressionsubquery
- The subquery- Returns:
- The new restriction
- Throws:
InvalidDataException
- If the subquery is null- Since:
- 3.5
-
like
Check if one expression matches another: e1 LIKE e2- Parameters:
e1
- The left expressione2
- The right expression- Returns:
- The new restriction
- Throws:
InvalidDataException
- If any of the expressions are null
-
like_in
Check if one expression matches any expressions in an array: (e1 LIKE e2.1) OR (e1 LIKE e2.2) OR ....- Parameters:
e1
- The left expressione2
- The array with expression to match against, null values are allowed and compared with IS NULL instead- Returns:
- The new restriction.
- Throws:
InvalidDataException
- If any of the expressions are null.- Since:
- 2.6
-
notLike_in
Check if one expression doesn't matches any of the expressions in an array. (e1 NOT LIKE e2.1) AND (e1 NOT LIKE e2.2) AND...- Parameters:
e1
- The left expression. Null is not allowede2
- The array with expressions that shouldn't match, null values are allows and compared with NOT IS NULL- Returns:
- The new restriction
- Throws:
InvalidDataException
- If any of the required expressions are null.- Since:
- 2.6
-
rlike
Check if an expression matches a regular expression. The support for this is database dependent, but both MySQL and Postgres supports it. If the database doesn't support regular expression, this method will create a restriction for an exact match.- Parameters:
e
- The expression to matchregexp
- The regular expression to test against- Returns:
- A restriction
- Throws:
InvalidDataException
- If any of the values are null- Since:
- 2.8
-
conditionalRestriction
public static Restriction conditionalRestriction(Filter<? extends Query> filter, Restriction ifTrue, Restriction ifFalse, boolean nullMatchNone) Create a restriction that falls back to one of two restrictions depending on if the query passes a filter or not. A typical use case is to apply a restriction depending on the return type of the query. This method will allow "null" restrictions but they are converted to "1=1" or "1=0" depending on the nullMatchNone parameter.- Parameters:
filter
- A filter, if null the 'ifTrue' restriction is returnedifTrue
- The restriction to apply if the filter evaluates to TRUE, or null to not apply any restriction in this caseifFalse
- The restriction to apply if the filter evaluates to FALSE, or null to not apply any restriction in this casenullMatchNone
- FALSE to match all items if the filter is null, TRUE to match no items if the filter is null- Returns:
- A conditional restriction, or the 'ifTrue' restriction if the filter is null, or null if both restrictions are null
- Since:
- 3.7
-
getFirstNull
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
-
countNotNulls
-
countNotNulls
-
copyNotNulls
private static <T> T[] copyNotNulls(T[] source, T[] dest) -
copyNotNulls
-