org.sqlproc.engine.spring
Class SpringQuery

java.lang.Object
  extended by org.sqlproc.engine.spring.SpringQuery
All Implemented Interfaces:
SqlQuery

public class SpringQuery
extends Object
implements SqlQuery

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

For more info please see the Tutorials.

Author:
Vladimir Hudec

Field Summary
(package private) static Pattern CALL
           
(package private)  Integer fetchSize
          The fetch size of rows to retrieve in one SQL.
(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.springframework.jdbc.core.JdbcTemplate jdbcTemplate
          The Spring JdbcTemplate, the central class for all Spring database operations.
(package private)  boolean logError
          The failed SQL command should be logged.
(package private)  org.slf4j.Logger logger
          The internal slf4j logger.
(package private)  Integer maxResults
          The maximum number of rows to retrieve.
(package private)  boolean ordered
          The SQL output is sorted.
(package private)  Map<String,OutValueSetter> parameterOutValueSetters
          The collection of all parameters output value setters.
(package private)  Map<Integer,Integer> parameterOutValuesToPickup
          The collection of all parameters, which have to be picked-up.
(package private)  Map<String,Object> parameterOutValueTypes
          The collection of all parameters types for output values.
(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
SpringQuery(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate, String queryString)
          Creates a new instance of this adapter.
 
Method Summary
 SqlQuery addScalar(String columnAlias)
          
 SqlQuery addScalar(String columnAlias, Object type)
          
 Object callFunction()
          
 List callList(SqlRuntimeContext runtimeCtx)
          
 Object callUnique(SqlRuntimeContext runtimeCtx)
          
 int callUpdate(SqlRuntimeContext runtimeCtx)
          
private  void doIdentitySelect(String identityName)
          Runs the select to obtain the value of auto-generated identity.
 int[] executeBatch(String[] statements)
          
private  void getGeneratedKeys(String identityName, Statement statement)
          Retrieves the value of auto-generated identity from executed prepared statement.
protected  Object getParameters(CallableStatement cs, boolean isFunction)
          Gets the value of the designated OUT parameters.
 Object getQuery()
          
protected  List getResults(ResultSet rs)
          Gets the value of the designated columns as the objects in the Java programming language.
private  boolean isSetJDBCIdentity()
           
 List list(SqlRuntimeContext runtimeCtx)
          
protected  SqlProcessorException newSqlProcessorException(org.springframework.dao.DataAccessException ex, String query)
           
 SqlQuery setFetchSize(int fetchSize)
          
 SqlQuery setFirstResult(int firstResult)
          
protected  int setLimits(PreparedStatement ps, SqlFromToPlugin.LimitType limitType, int ix, boolean afterSql)
          Sets the limit related parameters.
 void setLogError(boolean logError)
          Sets an indicator the failed SQL command should be logged
 SqlQuery setMaxResults(int maxResults)
          
 SqlQuery setOrdered(boolean ordered)
          
 SqlQuery setParameter(String name, Object val)
          
 SqlQuery setParameter(String name, Object val, Object type)
          
 SqlQuery setParameterList(String name, Object[] vals)
          
 SqlQuery setParameterList(String name, Object[] vals, Object type)
          
protected  void setParameters(PreparedStatement ps, SqlFromToPlugin.LimitType limitType, int start)
          Sets the value of the designated parameters.
 SqlQuery setTimeout(int timeout)
          
 Object unique(SqlRuntimeContext runtimeCtx)
          
 int update(SqlRuntimeContext runtimeCtx)
          
protected  int updateWithGenKeys(org.springframework.jdbc.core.PreparedStatementCreator psc, org.springframework.jdbc.core.PreparedStatementSetter pss, String identityName)
          This is a workaround, as this method is not visible in JdbcTemplate.
protected  int updateWithoutGenKeys(org.springframework.jdbc.core.PreparedStatementCreator psc, org.springframework.jdbc.core.PreparedStatementSetter pss)
          This is a workaround, as this method is not visible in JdbcTemplate.
 
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.


jdbcTemplate

org.springframework.jdbc.core.JdbcTemplate jdbcTemplate
The Spring JdbcTemplate, the central class for all Spring database operations.


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.


parameterOutValueTypes

Map<String,Object> parameterOutValueTypes
The collection of all parameters types for output values.


parameterOutValueSetters

Map<String,OutValueSetter> parameterOutValueSetters
The collection of all parameters output value setters.


parameterOutValuesToPickup

Map<Integer,Integer> parameterOutValuesToPickup
The collection of all parameters, which have to be picked-up.


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.


fetchSize

Integer fetchSize
The fetch size of rows to retrieve in one SQL.


ordered

boolean ordered
The SQL output is sorted.


logError

boolean logError
The failed SQL command should be logged.


CALL

static final Pattern CALL
Constructor Detail

SpringQuery

public SpringQuery(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate,
                   String queryString)
Creates a new instance of this adapter.

Parameters:
jdbcTemplate - the Spring JdbcTemplate instance
queryString - the SQL query/statement command
Method Detail

getQuery

public Object getQuery()

Specified by:
getQuery in interface SqlQuery

setTimeout

public SqlQuery setTimeout(int timeout)

Specified by:
setTimeout in interface SqlQuery

setFirstResult

public SqlQuery setFirstResult(int firstResult)

Specified by:
setFirstResult in interface SqlQuery

setMaxResults

public SqlQuery setMaxResults(int maxResults)

Specified by:
setMaxResults in interface SqlQuery

setFetchSize

public SqlQuery setFetchSize(int fetchSize)

Specified by:
setFetchSize in interface SqlQuery

setOrdered

public SqlQuery setOrdered(boolean ordered)

Specified by:
setOrdered in interface SqlQuery

list

public List list(SqlRuntimeContext runtimeCtx)
          throws SqlProcessorException

Specified by:
list in interface SqlQuery
Throws:
SqlProcessorException

unique

public Object unique(SqlRuntimeContext runtimeCtx)
              throws SqlProcessorException

Specified by:
unique in interface SqlQuery
Throws:
SqlProcessorException

update

public int update(SqlRuntimeContext runtimeCtx)
           throws SqlProcessorException

Specified by:
update in interface SqlQuery
Throws:
SqlProcessorException

isSetJDBCIdentity

private boolean isSetJDBCIdentity()

doIdentitySelect

private 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

getGeneratedKeys

private void getGeneratedKeys(String identityName,
                              Statement statement)
Retrieves the value of auto-generated identity from executed prepared statement.

Parameters:
identityName - the identity name from the META SQL statement
statement - statement to retrieve auto-generated keys from

updateWithGenKeys

protected int updateWithGenKeys(org.springframework.jdbc.core.PreparedStatementCreator psc,
                                org.springframework.jdbc.core.PreparedStatementSetter pss,
                                String identityName)
                         throws org.springframework.dao.DataAccessException
This is a workaround, as this method is not visible in JdbcTemplate.

It executes the prepared SQL statement and retrieves the values of generated identities from the statement. The generated identities cannot be obtained later because the ResultSet Statement.getGeneratedKeys() is closed after this method finishes.

Throws:
org.springframework.dao.DataAccessException

updateWithoutGenKeys

protected int updateWithoutGenKeys(org.springframework.jdbc.core.PreparedStatementCreator psc,
                                   org.springframework.jdbc.core.PreparedStatementSetter pss)
                            throws org.springframework.dao.DataAccessException
This is a workaround, as this method is not visible in JdbcTemplate.

Throws:
org.springframework.dao.DataAccessException

callList

public List callList(SqlRuntimeContext runtimeCtx)
              throws SqlProcessorException

Specified by:
callList in interface SqlQuery
Throws:
SqlProcessorException

callUnique

public Object callUnique(SqlRuntimeContext runtimeCtx)
                  throws SqlProcessorException

Specified by:
callUnique in interface SqlQuery
Throws:
SqlProcessorException

callUpdate

public int callUpdate(SqlRuntimeContext runtimeCtx)
               throws SqlProcessorException

Specified by:
callUpdate in interface SqlQuery
Throws:
SqlProcessorException

callFunction

public Object callFunction()
                    throws SqlProcessorException

Specified by:
callFunction in interface SqlQuery
Throws:
SqlProcessorException

addScalar

public SqlQuery addScalar(String columnAlias)

Specified by:
addScalar in interface SqlQuery

addScalar

public SqlQuery addScalar(String columnAlias,
                          Object type)

Specified by:
addScalar in interface SqlQuery

setParameter

public SqlQuery setParameter(String name,
                             Object val)
                      throws SqlProcessorException

Specified by:
setParameter in interface SqlQuery
Throws:
SqlProcessorException

setParameter

public SqlQuery setParameter(String name,
                             Object val,
                             Object type)
                      throws SqlProcessorException

Specified by:
setParameter in interface SqlQuery
Throws:
SqlProcessorException

setParameterList

public SqlQuery setParameterList(String name,
                                 Object[] vals)
                          throws SqlProcessorException

Specified by:
setParameterList in interface SqlQuery
Throws:
SqlProcessorException

setParameterList

public SqlQuery setParameterList(String name,
                                 Object[] vals,
                                 Object type)
                          throws SqlProcessorException

Specified by:
setParameterList in interface SqlQuery
Throws:
SqlProcessorException

setParameters

protected void setParameters(PreparedStatement ps,
                             SqlFromToPlugin.LimitType limitType,
                             int start)
                      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
start - the index of the first parameter to bind to prepared statement
Throws:
SQLException - if a database access error occurs or this method is called on a closed PreparedStatement

setLimits

protected int setLimits(PreparedStatement ps,
                        SqlFromToPlugin.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

getParameters

protected Object getParameters(CallableStatement cs,
                               boolean isFunction)
                        throws SQLException
Gets the value of the designated OUT parameters.

Parameters:
cs - an instance of CallableStatement
Throws:
SQLException - if a database access error occurs or this method is called on a closed CallableStatement

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

executeBatch

public int[] executeBatch(String[] statements)
                   throws SqlProcessorException

Specified by:
executeBatch in interface SqlQuery
Throws:
SqlProcessorException

newSqlProcessorException

protected SqlProcessorException newSqlProcessorException(org.springframework.dao.DataAccessException ex,
                                                         String query)

setLogError

public void setLogError(boolean logError)
Sets an indicator the failed SQL command should be logged

Specified by:
setLogError in interface SqlQuery
Parameters:
logError - an indicator the failed SQL command should be logged


Copyright © 2015. All rights reserved.