org.hdpagination.dataaccess.support
Class Restrictions

java.lang.Object
  extended by org.hdpagination.dataaccess.support.Restrictions

public final class Restrictions
extends java.lang.Object

This class provides static factory methods to produce the Restriction. Typically, these factory methods are called when calling JdbcQueryCallbackWrapper.addRestriction(Restriction) or HBN3QueryCallbackWrapper.addRestriction(Restriction) or JPAQueryCallbackWrapper.addRestriction(Restriction).

A sample code is as follows:

   JdbcQueryCallbackWrapper callback = JdbcQueryCallbackWrapper.newInstance("select productId,prodNo,price,madeIn,description from product p", ProductVO.class)
       .addRestriction( Restrictions.eq("prodNo", crit.getProdNo()) )
       .addRestriction( Restrictions.in("madeIn", crit.getMadeIn()) );

One generic rule applied to some of its simple static factory methods (eq, gt, ge, lt, le, contains, startsWith, endsWith) is:

  • If the type of argument param is java.lang.String, it returns null when the param is null or blank.
  • If the argument param is of other types, it returns null when the param is null.
  • If the Restriction object passed to the method JdbcQueryCallbackWrapper.addRestriction(Restriction) or HBN3QueryCallbackWrapper.addRestriction(Restriction) or JPAQueryCallbackWrapper.addRestriction(Restriction) is null, the restriction will not be added.

    The benefit of doing this way is you can avoid IF .. ELSE check in your code. Consequently, the code is much neater.

    Since:
    1.3.1
    Author:
    Liangfeng Ren

    Method Summary
    static Restriction contains(java.lang.String column, java.lang.String param)
              Apply "LIKE" constraint ([column] like ?).
    static Restriction endsWith(java.lang.String column, java.lang.String param)
              Apply "LIKE" constraint ([column] like ?).
    static Restriction eq(java.lang.String column, java.lang.Object param)
              Apply equal constraint ([column] = ?).
    static Restriction ge(java.lang.String column, java.lang.Object param)
              Apply "greater than or equal" constraint ([column] >= ?).
    static Restriction gt(java.lang.String column, java.lang.Object param)
              Apply "greater than" constraint ([column] > ?).
    static Restriction in(java.lang.String column, java.util.List params)
              Apply "IN" constraint ([column] IN (?, ?...)).
    static Restriction in(java.lang.String column, java.lang.Object[] params)
              Apply "IN" constraint ([column] IN (?, ?...)).
    static Restriction le(java.lang.String column, java.lang.Object param)
              Apply "less than or equal" constraint ([column] <= ?).
    static Restriction lt(java.lang.String column, java.lang.Object param)
              Apply "less than" constraint ([column] < ?).
    static Restriction range(java.lang.String column, java.lang.Object fromParam, java.lang.Object toParam)
              Apply range constraint ( [column] >= ? AND [column] <= ? ).
    static Restriction rangeBothExclusive(java.lang.String column, java.lang.Object fromParam, java.lang.Object toParam)
              Apply range constraint ( [column] > ? AND [column] < ? ).
    static Restriction rangeLeftExclusive(java.lang.String column, java.lang.Object fromParam, java.lang.Object toParam)
              Apply range constraint ( [column] > ? AND [column] <= ? ).
    static Restriction rangeRightExclusive(java.lang.String column, java.lang.Object fromParam, java.lang.Object toParam)
              Apply range constraint ( [column] >= ? AND [column] < ? ).
    static Restriction startsWith(java.lang.String column, java.lang.String param)
              Apply "LIKE" constraint ([column] like ?).
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Method Detail

    eq

    public static Restriction eq(java.lang.String column,
                                 java.lang.Object param)
    Apply equal constraint ([column] = ?). If the value of parameter (argument param) is null, then return null. It also returns null if the value of parameter (with the type of java.lang.String) is blank.

    Parameters:
    column - The column (before =) in the clause fragment. It can not be null or empty.
    param - the value of parameter
    Returns:

    gt

    public static Restriction gt(java.lang.String column,
                                 java.lang.Object param)
    Apply "greater than" constraint ([column] > ?). If the value of parameter (argument param) is null, then return null. It also returns null if the value of parameter (with the type of java.lang.String) is blank.

    Parameters:
    column - The column (before >) in the clause fragment.It can not be null or empty.
    param - the value of parameter
    Returns:

    ge

    public static Restriction ge(java.lang.String column,
                                 java.lang.Object param)
    Apply "greater than or equal" constraint ([column] >= ?). If the value of parameter (argument param) is null, then return null. It also returns null if the value of parameter (with the type of java.lang.String) is blank.

    Parameters:
    column - The column (before >=) in the clause fragment.It can not be null or empty.
    param - the value of parameter
    Returns:

    lt

    public static Restriction lt(java.lang.String column,
                                 java.lang.Object param)
    Apply "less than" constraint ([column] < ?). If the value of parameter (argument param) is null, then return null. It also returns null if the value of parameter (with the type of java.lang.String) is blank.

    Parameters:
    column - The column (before <) in the clause fragment.It can not be null or empty.
    param - the value of parameter
    Returns:

    le

    public static Restriction le(java.lang.String column,
                                 java.lang.Object param)
    Apply "less than or equal" constraint ([column] <= ?). If the value of parameter (argument param) is null, then return null. It also returns null if the value of parameter (with the type of java.lang.String) is blank.

    Parameters:
    column - The column (before <=) in the clause fragment.It can not be null or empty.
    param - the value of parameter
    Returns:

    in

    public static Restriction in(java.lang.String column,
                                 java.lang.Object[] params)
    Apply "IN" constraint ([column] IN (?, ?...)). If the parameters (argument params) is null or has zero size, then return null.

    Parameters:
    column - The column (before IN) in the clause fragment.It can not be null or empty.
    params - the array of parameters
    Returns:

    in

    public static Restriction in(java.lang.String column,
                                 java.util.List params)
    Apply "IN" constraint ([column] IN (?, ?...)). If the parameters (argument params) is null or has zero size, then return null.

    Parameters:
    column - The column (before IN) in the clause fragment.It can not be null or empty.
    params - the list of parameters
    Returns:

    range

    public static Restriction range(java.lang.String column,
                                    java.lang.Object fromParam,
                                    java.lang.Object toParam)
    Apply range constraint ( [column] >= ? AND [column] <= ? ).

    The rule is:

  • If both 'fromParam' and 'toParam' are null or blank (with type of java.lang.String), then return null;
  • If only 'fromParam' is null or blank, then ([column] <= ? ) applies, and only argument'toParam' is for parameter binding;
  • If only 'toParam' is null or blank, then ([column] >= ? ) applies, and only argument'fromParam' is for parameter binding;
  • If both 'fromParam' and 'toParam' are not null and blank , then ( [column] >= ? AND [column] <= ? ) applies;
  • Parameters:
    column - It can not be null or empty.
    fromParam -
    toParam -
    Returns:

    rangeRightExclusive

    public static Restriction rangeRightExclusive(java.lang.String column,
                                                  java.lang.Object fromParam,
                                                  java.lang.Object toParam)
    Apply range constraint ( [column] >= ? AND [column] < ? ).

    The rule is:

  • If both 'fromParam' and 'toParam' are null or blank (with type of java.lang.String), then return null;
  • If only 'fromParam' is null or blank, then ([column] < ? ) applies, and only argument'toParam' is for parameter binding;
  • If only 'toParam' is null or blank, then ([column] >= ? ) applies, and only argument'fromParam' is for parameter binding;
  • If both 'fromParam' and 'toParam' are not null and blank , then ( [column] >= ? AND [column] < ? ) applies;
  • Parameters:
    column - It can not be null or empty.
    fromParam -
    toParam -
    Returns:

    rangeLeftExclusive

    public static Restriction rangeLeftExclusive(java.lang.String column,
                                                 java.lang.Object fromParam,
                                                 java.lang.Object toParam)
    Apply range constraint ( [column] > ? AND [column] <= ? ).

    The rule is:

  • If both 'fromParam' and 'toParam' are null or blank (with type of java.lang.String), then return null;
  • If only 'fromParam' is null or blank, then ([column] <= ? ) applies, and only argument'toParam' is for parameter binding;
  • If only 'toParam' is null or blank, then ([column] > ? ) applies, and only argument'fromParam' is for parameter binding;
  • If both 'fromParam' and 'toParam' are not null and blank , then ( [column] > ? AND [column] <= ? ) applies;
  • Parameters:
    column - It can not be null or empty.
    fromParam -
    toParam -
    Returns:

    rangeBothExclusive

    public static Restriction rangeBothExclusive(java.lang.String column,
                                                 java.lang.Object fromParam,
                                                 java.lang.Object toParam)
    Apply range constraint ( [column] > ? AND [column] < ? ).

    The rule is:

  • If both 'fromParam' and 'toParam' are null or blank (with type of java.lang.String), then return null;
  • If only 'fromParam' is null or blank, then ([column] < ? ) applies, and only argument'toParam' is for parameter binding;
  • If only 'toParam' is null or blank, then ([column] > ? ) applies, and only argument'fromParam' is for parameter binding;
  • If both 'fromParam' and 'toParam' are not null and blank , then ( [column] > ? AND [column] < ? ) applies;
  • Parameters:
    column - It can not be null or empty.
    fromParam -
    toParam -
    Returns:

    contains

    public static Restriction contains(java.lang.String column,
                                       java.lang.String param)
    Apply "LIKE" constraint ([column] like ?). The actual binding parameter value is %param%. If the parameter (argument param) is null or blank, then return null.

    Parameters:
    column - The column (before LIKE) in the clause fragment. It can not be null or empty.
    param - The actual binding parameter value is %param%.
    Returns:

    startsWith

    public static Restriction startsWith(java.lang.String column,
                                         java.lang.String param)
    Apply "LIKE" constraint ([column] like ?). The actual binding parameter value is param%. If the parameter (argument param) is null or blank, then return null.

    Parameters:
    column - The column (before LIKE) in the clause fragment. It can not be null or empty.
    param - The actual binding parameter value is param%.
    Returns:

    endsWith

    public static Restriction endsWith(java.lang.String column,
                                       java.lang.String param)
    Apply "LIKE" constraint ([column] like ?). The actual binding parameter value is %param. If the parameter (argument param) is null or blank, then return null.

    Parameters:
    column - The column (before LIKE) in the clause fragment. It can not be null or empty.
    param - The actual binding parameter value is param%.
    Returns:


    Copyright © 2008 HDPagination All Rights Reserved.