toolbox.dao
Class PreparedQuery

Package class diagram package PreparedQuery
java.lang.Object
  extended by toolbox.dao.PreparedQuery

public class PreparedQuery
extends java.lang.Object

Convenience helper class to generate SQL queries in Java PreparedStatement format. This class aims to be a more complete implementation than the current Query class provided by this package.

This class provides mutators to add and and or clauses to the query. These mutators are implemented in the and(..) and or(..) methods.

The getSQL(..) method returns a String in the Java PreparedStatement format and the getValuesAs..() methods return the field values of the query.

Please note that this class is not thread safe and calling the and and or methods from multiple threads will result in a corrupt reference.


Field Summary
static java.lang.String EQUALS
          The equals operator (=).
static java.lang.String GREATER
          The greater than (>) operator.
static java.lang.String GREATER_EQUALS
          The greater than or equals (>=) operator.
static java.lang.String IN
          The in operator.
static java.lang.String LESS
          The less than (<) operator.
static java.lang.String LESS_EQUALS
          The less than or equals (<=) operator.
static java.lang.String LIKE
          The like operator.
static java.lang.String NOT_EQUALS
          The not equals (!=) operator.
static java.lang.String NOT_LIKE
          The not like operator.
 
Constructor Summary
PreparedQuery()
          Default constructor.
 
Method Summary
 void and(PreparedQuery query)
          Add an and clause to this query.
 void and(java.lang.String field, java.lang.String op, java.lang.Object val)
          Add an and clause to this query.
 java.lang.String getSQL(java.lang.String prefix, java.lang.String postfix)
          Return the SQL representation of this query as a String in Java PreparedStatement format.
 java.lang.Object[] getValuesAsArray()
          Return the values of this query as an Object array.
 java.util.List getValuesAsList()
          Return the values of this query as a List.
 void or(PreparedQuery query)
          Add an or clause to this query.
 void or(java.lang.String field, java.lang.String op, java.lang.Object val)
          Add an or clause to this query.
 void reset()
          Reset the internal buffer and values List.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EQUALS

public static final java.lang.String EQUALS
The equals operator (=).

See Also:
Constant Field Values

NOT_EQUALS

public static final java.lang.String NOT_EQUALS
The not equals (!=) operator.

See Also:
Constant Field Values

LESS_EQUALS

public static final java.lang.String LESS_EQUALS
The less than or equals (<=) operator.

See Also:
Constant Field Values

GREATER_EQUALS

public static final java.lang.String GREATER_EQUALS
The greater than or equals (>=) operator.

See Also:
Constant Field Values

LESS

public static final java.lang.String LESS
The less than (<) operator.

See Also:
Constant Field Values

GREATER

public static final java.lang.String GREATER
The greater than (>) operator.

See Also:
Constant Field Values

LIKE

public static final java.lang.String LIKE
The like operator.

See Also:
Constant Field Values

NOT_LIKE

public static final java.lang.String NOT_LIKE
The not like operator.

See Also:
Constant Field Values

IN

public static final java.lang.String IN
The in operator.

See Also:
Constant Field Values
Constructor Detail

PreparedQuery

public PreparedQuery()
Default constructor.

Method Detail

reset

public void reset()
Reset the internal buffer and values List.


and

public void and(java.lang.String field,
                java.lang.String op,
                java.lang.Object val)
         throws DaoException
Add an and clause to this query.

Parameters:
field - String containing the field name to add.
op - String containing the name of the operator to use.
val - Object containing the value to use in the clause.
Throws:
DaoException - if the given operator name is invalid.

or

public void or(java.lang.String field,
               java.lang.String op,
               java.lang.Object val)
        throws DaoException
Add an or clause to this query.

Parameters:
field - String containing the field name to add.
op - String containing the name of the operator to use.
val - Object containing the value to use in the clause.
Throws:
DaoException - if the given operator name is invalid.

and

public void and(PreparedQuery query)
         throws DaoException
Add an and clause to this query.

The provided PreparedQuery's SQL string and values will be appended to this query and placed within parenthesis.

Parameters:
query - PreparedQuery that must be appended to this query.
Throws:
DaoException - if the provide query is invalid.

or

public void or(PreparedQuery query)
        throws DaoException
Add an or clause to this query.

The provided PreparedQuery's SQL string and values will be appended to this query and placed within parenthesis.

Parameters:
query - PreparedQuery that must be appended to this query.
Throws:
DaoException - if the provide query is invalid.

getSQL

public java.lang.String getSQL(java.lang.String prefix,
                               java.lang.String postfix)
                        throws DaoException
Return the SQL representation of this query as a String in Java PreparedStatement format.

Parameters:
prefix - String containing a prefix for the generated query. May be null.
postfix - String containing a postfix for the generated query. May be null.
Returns:
String containing the generated query string.
Throws:
DaoException - if the generated query string is invalid.

getValuesAsList

public java.util.List getValuesAsList()
Return the values of this query as a List.

Returns:
List containing the query values.

getValuesAsArray

public java.lang.Object[] getValuesAsArray()
Return the values of this query as an Object array.

Returns:
Object[] containing the query values.