org.sqlproc.engine.jdbc
Class JdbcQuery

java.lang.Object
  extended by org.sqlproc.engine.jdbc.JdbcQuery
All Implemented Interfaces:
SqlQuery

public class JdbcQuery
extends Object
implements SqlQuery

The JDBC stack implementation of the SQL Engine query contract. In fact it's an adapter the internal JDBC stuff.

For more info please see the Reference Guide or the tutorials.

Author:
Vladimir Hudec

Field Summary
(package private)  Connection connection
          The connection to the database.
(package private)  Integer firstResult
          The first row to retrieve.
(package private)  List<String> identities
          The collection of all (auto-generated) identities.
(package private)  Map<String,IdentitySetter> identitySetters
          The collection of all identities setters.
(package private)  Map<String,Object> identityTypes
          The collection of all identities types.
(package private)  org.slf4j.Logger logger
          The internal slf4j logger.
(package private)  Integer maxResults
          The maximum number of rows to retrieve.
(package private)  List<String> parameters
          The collection of all parameters (input value declarations).
(package private)  Map<String,Object> parameterTypes
          The collection of all parameters types.
(package private)  Map<String,Object> parameterValues
          The collection of all parameters values.
(package private)  String queryString
          The SQL query/statement command.
(package private)  List<String> scalars
          The collection of all scalars (output values declarations).
(package private)  Map<String,Object> scalarTypes
          The collection of all scalars types.
(package private)  Integer timeout
          A timeout for the underlying query.
 
Constructor Summary
JdbcQuery(Connection connection, String queryString)
          Creates a new instance of this adapter.
 
Method Summary
 SqlQuery addScalar(String columnAlias)
          Declares a scalar query result, which is an SQL query execution output value.
 SqlQuery addScalar(String columnAlias, Object type)
          Declares a scalar query result, which is an SQL query execution output value.
protected  void doIdentitySelect(String identityName)
          Runs the select to obtain the value of auto-generated identity.
 int executeUpdate()
          Executes the update, delete or insert statement.
 Object getQuery()
          Returns the internal representation of this query.
protected  List getResults(ResultSet rs)
          Gets the value of the designated columns as the objects in the Java programming language.
 List list()
          Returns the query results as a List.
 SqlQuery setFirstResult(int firstResult)
          Sets the first row to retrieve.
protected  int setLimits(PreparedStatement ps, SqlUtils.LimitType limitType, int ix, boolean afterSql)
          Sets the limit related parameters.
 SqlQuery setMaxResults(int maxResults)
          Sets the maximum number of rows to retrieve.
 SqlQuery setParameter(String name, Object val)
          Binds a value to a named query parameter.
 SqlQuery setParameter(String name, Object val, Object type)
          Binds a value to a named query parameter.
 SqlQuery setParameterList(String name, Object[] vals)
          Binds multiple values to a named query parameter.
 SqlQuery setParameterList(String name, Object[] vals, Object type)
          Binds multiple values to a named query parameter.
protected  void setParameters(PreparedStatement ps, SqlUtils.LimitType limitType)
          Sets the value of the designated parameters.
 SqlQuery setTimeout(int timeout)
          Sets a timeout for the underlying query.
 Object uniqueResult()
          Convenience method to return a single instance that matches the query, or null if the query returns no results.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

final org.slf4j.Logger logger
The internal slf4j logger.


connection

Connection connection
The connection to the database. It should be opened.


queryString

String queryString
The SQL query/statement command.


scalars

List<String> scalars
The collection of all scalars (output values declarations).


scalarTypes

Map<String,Object> scalarTypes
The collection of all scalars types.


parameters

List<String> parameters
The collection of all parameters (input value declarations).


parameterValues

Map<String,Object> parameterValues
The collection of all parameters values.


parameterTypes

Map<String,Object> parameterTypes
The collection of all parameters types.


identities

List<String> identities
The collection of all (auto-generated) identities.


identitySetters

Map<String,IdentitySetter> identitySetters
The collection of all identities setters.


identityTypes

Map<String,Object> identityTypes
The collection of all identities types.


timeout

Integer timeout
A timeout for the underlying query.


firstResult

Integer firstResult
The first row to retrieve.


maxResults

Integer maxResults
The maximum number of rows to retrieve.

Constructor Detail

JdbcQuery

public JdbcQuery(Connection connection,
                 String queryString)
Creates a new instance of this adapter.

Parameters:
connection - the connection to the database
queryString - the SQL query/statement command
Method Detail

getQuery

public Object getQuery()
Returns the internal representation of this query. This implementation depends on the stack on top of which the SQL Processor works. It can be for example the Hibernate SQLQuery instance.

Specified by:
getQuery in interface SqlQuery
Returns:
the internal representation of this query

setTimeout

public SqlQuery setTimeout(int timeout)
Sets a timeout for the underlying query.

Specified by:
setTimeout in interface SqlQuery
Parameters:
timeout - the timeout in seconds
Returns:
this instance to enable the methods chaining

setFirstResult

public SqlQuery setFirstResult(int firstResult)
Sets the first row to retrieve. If not set, rows will be retrieved beginning from row 0.

Specified by:
setFirstResult in interface SqlQuery
Parameters:
firstResult - a row number, numbered from 0
Returns:
this instance to enable the methods chaining

setMaxResults

public SqlQuery setMaxResults(int maxResults)
Sets the maximum number of rows to retrieve. If not set, there is no limit to the number of rows retrieved.

Specified by:
setMaxResults in interface SqlQuery
Parameters:
maxResults - the maximum number of rows
Returns:
this instance to enable the methods chaining

list

public List list()
          throws SqlProcessorException
Returns the query results as a List. If the query contains multiple results pre row, the results are returned in an instance of Object[].

Specified by:
list in interface SqlQuery
Returns:
the result list
Throws:
SqlProcessorException - in the case of any problem in ORM or JDBC stack

uniqueResult

public Object uniqueResult()
                    throws SqlProcessorException
Convenience method to return a single instance that matches the query, or null if the query returns no results.

Specified by:
uniqueResult in interface SqlQuery
Returns:
the single result or null
Throws:
SqlProcessorException - in the case of any problem in ORM or JDBC stack

executeUpdate

public int executeUpdate()
                  throws SqlProcessorException
Executes the update, delete or insert statement.

Specified by:
executeUpdate in interface SqlQuery
Returns:
the number of affected rows
Throws:
SqlProcessorException - in the case of any problem in ORM or JDBC stack

doIdentitySelect

protected void doIdentitySelect(String identityName)
Runs the select to obtain the value of auto-generated identity.

Parameters:
identityName - the identity name from the META SQL statement

addScalar

public SqlQuery addScalar(String columnAlias)
Declares a scalar query result, which is an SQL query execution output value.

Specified by:
addScalar in interface SqlQuery
Parameters:
columnAlias - the name of the database column or the alias in the result set
Returns:
this instance to enable the methods chaining

addScalar

public SqlQuery addScalar(String columnAlias,
                          Object type)
Declares a scalar query result, which is an SQL query execution output value.

Specified by:
addScalar in interface SqlQuery
Parameters:
columnAlias - the name of the database column or the alias in the result set
type - the (META) type of the parameter
Returns:
this instance to enable the methods chaining

setParameter

public SqlQuery setParameter(String name,
                             Object val)
                      throws SqlProcessorException
Binds a value to a named query parameter.

Specified by:
setParameter in interface SqlQuery
Parameters:
name - the name of the parameter
val - the not-null parameter value
Returns:
this instance to enable the methods chaining
Throws:
SqlProcessorException - in the case of any problem in ORM or JDBC stack

setParameter

public SqlQuery setParameter(String name,
                             Object val,
                             Object type)
                      throws SqlProcessorException
Binds a value to a named query parameter.

Specified by:
setParameter in interface SqlQuery
Parameters:
name - the name of the parameter
val - the possibly-null parameter value
type - the (META) type of the parameter
Returns:
this instance to enable the methods chaining
Throws:
SqlProcessorException - in the case of any problem in ORM or JDBC stack

setParameterList

public SqlQuery setParameterList(String name,
                                 Object[] vals)
                          throws SqlProcessorException
Binds multiple values to a named query parameter. This is useful for binding a list of values to the query fragment in (:value_list).

Specified by:
setParameterList in interface SqlQuery
Parameters:
name - the name of the parameter
vals - a collection of values
Returns:
this instance to enable the methods chaining
Throws:
SqlProcessorException - in the case of any problem in ORM or JDBC stack

setParameterList

public SqlQuery setParameterList(String name,
                                 Object[] vals,
                                 Object type)
                          throws SqlProcessorException
Binds multiple values to a named query parameter. This is useful for binding a list of values to the query fragment in (:value_list).

Specified by:
setParameterList in interface SqlQuery
Parameters:
name - the name of the parameter
vals - a collection of values
type - the (META) type of the parameter
Returns:
this instance to enable the methods chaining
Throws:
SqlProcessorException - in the case of any problem in ORM or JDBC stack

setParameters

protected void setParameters(PreparedStatement ps,
                             SqlUtils.LimitType limitType)
                      throws SQLException
Sets the value of the designated parameters.

Parameters:
ps - an instance of PreparedStatement
limitType - the limit type to restrict the number of rows in the result set
Throws:
SQLException - if a database access error occurs or this method is called on a closed PreparedStatement

setLimits

protected int setLimits(PreparedStatement ps,
                        SqlUtils.LimitType limitType,
                        int ix,
                        boolean afterSql)
                 throws SQLException
Sets the limit related parameters.

Parameters:
ps - an instance of PreparedStatement
limitType - the limit type to restrict the number of rows in the result set
ix - a column index
afterSql - an indicator it's done after the main SQL statement execution
Returns:
the updated column index
Throws:
SQLException - if a database access error occurs or this method is called on a closed PreparedStatement

getResults

protected List getResults(ResultSet rs)
                   throws SQLException
Gets the value of the designated columns as the objects in the Java programming language.

Parameters:
rs - an instance of ResultSet
Returns:
the result list
Throws:
SQLException - if a database access error occurs or this method is called on a closed ResultSet


Copyright © 2011. All Rights Reserved.