Package org.sqlproc.engine.spring
Class SpringQuery
- java.lang.Object
-
- 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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.sqlproc.engine.SqlQuery
SqlQuery.SqlQueryRowProcessor
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static PatternCALL(package private) IntegerfetchSize- * The fetch size of rows to retrieve in one SQL.(package private) IntegerfirstResultThe first row to retrieve.(package private) List<String>identitiesThe collection of all (auto-generated) identities.(package private) Map<String,IdentitySetter>identitySettersThe collection of all identities setters.(package private) Map<String,Object>identityTypesThe collection of all identities types.(package private) org.springframework.jdbc.core.JdbcTemplatejdbcTemplateThe Spring JdbcTemplate, the central class for all Spring database operations.(package private) booleanlogErrorThe failed SQL command should be logged.(package private) org.slf4j.LoggerloggerThe internal slf4j logger.(package private) IntegermaxResults- * The maximum number of rows to retrieve.private static Map<String,Object>NO_MORE_DATAThe indicator there are no more data in ResultSet.(package private) booleanorderedThe SQL output is sorted.(package private) Map<String,OutValueSetter>parameterOutValueSettersThe collection of all parameters output value setters.(package private) Map<Integer,Integer>parameterOutValuesToPickupThe collection of all parameters, which have to be picked-up.(package private) Map<String,Object>parameterOutValueTypesThe collection of all parameters types for output values.(package private) List<String>parametersThe collection of all parameters (input value declarations).(package private) Map<String,Object>parameterTypesThe collection of all parameters types.(package private) Map<String,Object>parameterValuesThe collection of all parameters values.(package private) StringqueryStringThe SQL query/statement command.(package private) List<String>scalarsThe collection of all scalars (output values declarations).(package private) Map<String,Object>scalarTypesThe collection of all scalars types.(package private) SqlControlsqlControlThe compound parameters controlling the META SQL execution.(package private) IntegertimeoutA timeout for the underlying query.
-
Constructor Summary
Constructors Constructor Description SpringQuery(org.springframework.jdbc.core.JdbcTemplate jdbcTemplate, String queryString)Creates a new instance of this adapter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SqlQueryaddScalar(String columnAlias)SqlQueryaddScalar(String columnAlias, Object type, Class<?>... moreTypes)Map<String,Object>callFunction()List<Map<String,Object>>callList(SqlRuntimeContext runtimeCtx)Map<String,Object>callUnique(SqlRuntimeContext runtimeCtx)intcallUpdate(SqlRuntimeContext runtimeCtx)private voiddoIdentitySelect(String identityName)Runs the select to obtain the value of auto-generated identity.int[]executeBatch(String[] statements)private voidgetGeneratedKeys(String identityName, Statement statement)Retrieves the value of auto-generated identity from executed prepared statement.protected Map<String,Object>getOneResult(ResultSet rs)Gets the value of the designated columns for one database row as the object in the Java programming language.protected Map<String,Object>getParameters(CallableStatement cs, boolean isFunction)Gets the value of the designated OUT parameters.ObjectgetQuery()protected List<Map<String,Object>>getResults(ResultSet rs)Gets the value of the designated columns as the objects in the Java programming language.private booleanisSetJDBCIdentity()List<Map<String,Object>>list(SqlRuntimeContext runtimeCtx)protected SqlProcessorExceptionnewSqlProcessorException(org.springframework.dao.DataAccessException ex, String query)intquery(SqlRuntimeContext runtimeCtx, SqlQuery.SqlQueryRowProcessor sqlQueryRowProcessor)protected intsetLimits(PreparedStatement ps, SqlFromToPlugin.LimitType limitType, int ix, boolean afterSql)Sets the limit related parameters.voidsetLogError(boolean logError)Sets an indicator the failed SQL command should be loggedSqlQuerysetOrdered(boolean ordered)SqlQuerysetParameter(String name, Object val)SqlQuerysetParameter(String name, Object val, Object type, Class<?>... moreTypes)SqlQuerysetParameterList(String name, Object[] vals)SqlQuerysetParameterList(String name, Object[] vals, Object type, Class<?>... moreTypes)protected voidsetParameters(PreparedStatement ps, SqlFromToPlugin.LimitType limitType, int start)Sets the value of the designated parameters.SqlQuerysetSqlControl(SqlControl sqlControl)Map<String,Object>unique(SqlRuntimeContext runtimeCtx)intupdate(SqlRuntimeContext runtimeCtx)protected intupdateWithGenKeys(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 intupdateWithoutGenKeys(org.springframework.jdbc.core.PreparedStatementCreator psc, org.springframework.jdbc.core.PreparedStatementSetter pss)This is a workaround, as this method is not visible in JdbcTemplate.
-
-
-
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.
-
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.
-
identitySetters
Map<String,IdentitySetter> identitySetters
The collection of all identities setters.
-
sqlControl
SqlControl sqlControl
The compound parameters controlling the META SQL execution.
-
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.
-
NO_MORE_DATA
private static final Map<String,Object> NO_MORE_DATA
The indicator there are no more data in ResultSet.
-
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 instancequeryString- the SQL query/statement command
-
-
Method Detail
-
setSqlControl
public SqlQuery setSqlControl(SqlControl sqlControl)
- Specified by:
setSqlControlin interfaceSqlQuery
-
setOrdered
public SqlQuery setOrdered(boolean ordered)
- Specified by:
setOrderedin interfaceSqlQuery
-
list
public List<Map<String,Object>> list(SqlRuntimeContext runtimeCtx) throws SqlProcessorException
- Specified by:
listin interfaceSqlQuery- Throws:
SqlProcessorException
-
unique
public Map<String,Object> unique(SqlRuntimeContext runtimeCtx) throws SqlProcessorException
- Specified by:
uniquein interfaceSqlQuery- Throws:
SqlProcessorException
-
query
public int query(SqlRuntimeContext runtimeCtx, SqlQuery.SqlQueryRowProcessor sqlQueryRowProcessor) throws SqlProcessorException
- Specified by:
queryin interfaceSqlQuery- Throws:
SqlProcessorException
-
update
public int update(SqlRuntimeContext runtimeCtx) throws SqlProcessorException
- Specified by:
updatein interfaceSqlQuery- 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 statementstatement- 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.DataAccessExceptionThis 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 ResultSetStatement.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.DataAccessExceptionThis is a workaround, as this method is not visible in JdbcTemplate.- Throws:
org.springframework.dao.DataAccessException
-
callList
public List<Map<String,Object>> callList(SqlRuntimeContext runtimeCtx) throws SqlProcessorException
- Specified by:
callListin interfaceSqlQuery- Throws:
SqlProcessorException
-
callUnique
public Map<String,Object> callUnique(SqlRuntimeContext runtimeCtx) throws SqlProcessorException
- Specified by:
callUniquein interfaceSqlQuery- Throws:
SqlProcessorException
-
callUpdate
public int callUpdate(SqlRuntimeContext runtimeCtx) throws SqlProcessorException
- Specified by:
callUpdatein interfaceSqlQuery- Throws:
SqlProcessorException
-
callFunction
public Map<String,Object> callFunction() throws SqlProcessorException
- Specified by:
callFunctionin interfaceSqlQuery- Throws:
SqlProcessorException
-
setParameter
public SqlQuery setParameter(String name, Object val) throws SqlProcessorException
- Specified by:
setParameterin interfaceSqlQuery- Throws:
SqlProcessorException
-
setParameter
public SqlQuery setParameter(String name, Object val, Object type, Class<?>... moreTypes) throws SqlProcessorException
- Specified by:
setParameterin interfaceSqlQuery- Throws:
SqlProcessorException
-
setParameterList
public SqlQuery setParameterList(String name, Object[] vals) throws SqlProcessorException
- Specified by:
setParameterListin interfaceSqlQuery- Throws:
SqlProcessorException
-
setParameterList
public SqlQuery setParameterList(String name, Object[] vals, Object type, Class<?>... moreTypes) throws SqlProcessorException
- Specified by:
setParameterListin interfaceSqlQuery- 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 PreparedStatementlimitType- the limit type to restrict the number of rows in the result setstart- 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 closedPreparedStatement
-
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 PreparedStatementlimitType- the limit type to restrict the number of rows in the result setix- a column indexafterSql- 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 closedPreparedStatement
-
getParameters
protected Map<String,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 closedCallableStatement
-
getResults
protected List<Map<String,Object>> 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 closedResultSet
-
getOneResult
protected Map<String,Object> getOneResult(ResultSet rs) throws SQLException
Gets the value of the designated columns for one database row as the object in the Java programming language.- Parameters:
rs- an instance of ResultSet- Returns:
- the result object for one row
- Throws:
SQLException- if a database access error occurs or this method is called on a closedResultSet
-
executeBatch
public int[] executeBatch(String[] statements) throws SqlProcessorException
- Specified by:
executeBatchin interfaceSqlQuery- 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:
setLogErrorin interfaceSqlQuery- Parameters:
logError- an indicator the failed SQL command should be logged
-
-