Package org.sqlproc.engine.jdbc
Class JdbcQuery
java.lang.Object
org.sqlproc.engine.jdbc.JdbcQuery
- All Implemented Interfaces:
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 Tutorials.
- Author:
- Vladimir Hudec
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.sqlproc.engine.SqlQuery
SqlQuery.SqlQueryRowProcessor -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final Pattern(package private) ConnectionThe connection to the database.(package private) Integer- * The fetch size of rows to retrieve in one SQL.(package private) IntegerThe first row to retrieve.The collection of all (auto-generated) identities.(package private) Map<String, IdentitySetter> The collection of all identities setters.The collection of all identities types.(package private) booleanThe failed SQL command should be logged.(package private) final org.slf4j.LoggerThe internal slf4j logger.(package private) Integer- * The maximum number of rows to retrieve.The indicator there are no more data in ResultSet.(package private) booleanThe SQL output is sorted.(package private) Map<String, OutValueSetter> The collection of all parameters output value setters.The collection of all parameters, which have to be picked-up.The collection of all parameters types for output values.The collection of all parameters (input value declarations).The collection of all parameters types.The collection of all parameters values.(package private) StringThe SQL query/statement command.The collection of all scalars (output values declarations).The collection of all scalars types.(package private) SqlControlThe compound parameters controlling the META SQL execution.(package private) IntegerA timeout for the underlying query. -
Constructor Summary
ConstructorsConstructorDescriptionJdbcQuery(Connection connection, String queryString) Creates a new instance of this adapter. -
Method Summary
Modifier and TypeMethodDescriptionDeclares a scalar query result, which is an SQL query execution output value.Declares a scalar query result, which is an SQL query execution output value.Executes the statements in the stored function, which return value of any type but the result set.callList(SqlRuntimeContext runtimeCtx) Returns the stored procedure execution results as aList.callUnique(SqlRuntimeContext runtimeCtx) Convenience method to return a single instance that matches the stored procedure execution, or null if the stored procedure execution returns no results.intcallUpdate(SqlRuntimeContext runtimeCtx) Executes the update, delete, insert statement or other statements in the stored procedure, which don't return the result set.protected voiddoIdentitySelect(String identityName) Runs the select to obtain the value of auto-generated identity.int[]executeBatch(String[] statements) Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.protected voidgetGeneratedKeys(String identityName, Statement statement) Retrieves the value of auto-generated identity from executed prepared statement.Gets the value of the designated columns for one database row as the object in the Java programming language.getParameters(CallableStatement cs, boolean isFunction) Gets the value of the designated OUT parameters.getQuery()Returns the internal representation of this query.getResults(ResultSet rs) Gets the value of the designated columns as the objects in the Java programming language.private booleanlist(SqlRuntimeContext runtimeCtx) Returns the query results as aList.protected SqlProcessorExceptionnewSqlProcessorException(SQLException ex, String query) intquery(SqlRuntimeContext runtimeCtx, SqlQuery.SqlQueryRowProcessor sqlQueryRowProcessor) Process the query output using theSqlQuery.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 loggedsetOrdered(boolean ordered) Sets the indicator the SQL output is sorted.setParameter(String name, Object val) Binds a value to a named query parameter.setParameter(String name, Object val, Object type, Class<?>... moreTypes) Binds a value to a named query parameter.setParameterList(String name, Object[] vals) Binds multiple values to a named query parameter.setParameterList(String name, Object[] vals, Object type, Class<?>... moreTypes) Binds multiple values to a named query parameter.protected voidsetParameters(PreparedStatement ps, SqlFromToPlugin.LimitType limitType, int start) Sets the value of the designated parameters.setSqlControl(SqlControl sqlControl) Sets the compound parameters controlling the META SQL execution.unique(SqlRuntimeContext runtimeCtx) Convenience method to return a single instance that matches the query, or null if the query returns no results.intupdate(SqlRuntimeContext runtimeCtx) Executes the update, delete or insert statement.
-
Field Details
-
logger
final org.slf4j.Logger loggerThe internal slf4j logger. -
connection
Connection connectionThe connection to the database. It should be opened. -
queryString
String queryStringThe SQL query/statement command. -
scalars
The collection of all scalars (output values declarations). -
scalarTypes
The collection of all scalars types. -
parameters
The collection of all parameters (input value declarations). -
parameterValues
The collection of all parameters values. -
parameterTypes
The collection of all parameters types. -
parameterOutValueTypes
The collection of all parameters types for output values. -
parameterOutValueSetters
Map<String,OutValueSetter> parameterOutValueSettersThe collection of all parameters output value setters. -
parameterOutValuesToPickup
The collection of all parameters, which have to be picked-up. -
identities
The collection of all (auto-generated) identities. -
identitySetters
Map<String,IdentitySetter> identitySettersThe collection of all identities setters. -
identityTypes
The collection of all identities types. -
sqlControl
SqlControl sqlControlThe compound parameters controlling the META SQL execution. -
timeout
Integer timeoutA timeout for the underlying query. -
firstResult
Integer firstResultThe 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 orderedThe SQL output is sorted. -
logError
boolean logErrorThe failed SQL command should be logged. -
NO_MORE_DATA
The indicator there are no more data in ResultSet. -
CALL
-
-
Constructor Details
-
JdbcQuery
Creates a new instance of this adapter.- Parameters:
connection- the connection to the databasequeryString- the SQL query/statement command
-
-
Method Details
-
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. -
setSqlControl
Sets the compound parameters controlling the META SQL execution.- Specified by:
setSqlControlin interfaceSqlQuery- Parameters:
sqlControl- the compound parameters controlling the META SQL execution- Returns:
- this instance to enable the methods chaining
-
setOrdered
Sets the indicator the SQL output is sorted.- Specified by:
setOrderedin interfaceSqlQuery- Parameters:
ordered- the indicator the SQL output is sorted- Returns:
- this instance to enable the methods chaining
-
list
Returns the query results as aList. If the query contains multiple results per row, the results are returned in an instance ofObject[].- Specified by:
listin interfaceSqlQuery- Parameters:
runtimeCtx- the public runtime context- Returns:
- the result list
- Throws:
SqlProcessorException- in the case of any problem in ORM or JDBC stack
-
unique
Convenience method to return a single instance that matches the query, or null if the query returns no results.- Specified by:
uniquein interfaceSqlQuery- Parameters:
runtimeCtx- the public runtime context- Returns:
- the single result or
null - Throws:
SqlProcessorException- in the case of any problem in ORM or JDBC stack
-
query
public int query(SqlRuntimeContext runtimeCtx, SqlQuery.SqlQueryRowProcessor sqlQueryRowProcessor) throws SqlProcessorException Process the query output using theSqlQuery.SqlQueryRowProcessor.- Specified by:
queryin interfaceSqlQuery- Parameters:
runtimeCtx- the public runtime contextsqlQueryRowProcessor- the callback designated to process every database row- Returns:
- the total number of processed database rows
- Throws:
SqlProcessorException- in the case of any problem in ORM or JDBC stack
-
update
Executes the update, delete or insert statement.- Specified by:
updatein interfaceSqlQuery- Parameters:
runtimeCtx- the public runtime context- Returns:
- the number of affected rows
- Throws:
SqlProcessorException- in the case of any problem in ORM or JDBC stack
-
isSetJDBCIdentity
private boolean isSetJDBCIdentity() -
getGeneratedKeys
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
-
doIdentitySelect
Runs the select to obtain the value of auto-generated identity.- Parameters:
identityName- the identity name from the META SQL statement
-
callList
Returns the stored procedure execution results as aList. If the query contains multiple results per row, the results are returned in an instance ofObject[]. It's based on the CallableStatement invocation.- Specified by:
callListin interfaceSqlQuery- Parameters:
runtimeCtx- the public runtime context- Returns:
- the result list
- Throws:
SqlProcessorException- in the case of any problem in ORM or JDBC stack
-
callUnique
Convenience method to return a single instance that matches the stored procedure execution, or null if the stored procedure execution returns no results. It's based on the CallableStatement invocation.- Specified by:
callUniquein interfaceSqlQuery- Parameters:
runtimeCtx- the public runtime context- Returns:
- the single result or
null - Throws:
SqlProcessorException- in the case of any problem in ORM or JDBC stack
-
callUpdate
Executes the update, delete, insert statement or other statements in the stored procedure, which don't return the result set. It's based on the CallableStatement invocation.- Specified by:
callUpdatein interfaceSqlQuery- Parameters:
runtimeCtx- the public runtime context- Returns:
- the number of affected rows
- Throws:
SqlProcessorException- in the case of any problem in ORM or JDBC stack
-
callFunction
Executes the statements in the stored function, which return value of any type but the result set. It's based on the CallableStatement invocation.- Specified by:
callFunctionin interfaceSqlQuery- Returns:
- the result of the stored function invocation
- Throws:
SqlProcessorException- in the case of any problem in ORM or JDBC stack
-
addScalar
Declares a scalar query result, which is an SQL query execution output value. -
addScalar
Declares a scalar query result, which is an SQL query execution output value. -
setParameter
Binds a value to a named query parameter.- Specified by:
setParameterin interfaceSqlQuery- Parameters:
name- the name of the parameterval- 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, Class<?>... moreTypes) throws SqlProcessorException Binds a value to a named query parameter.- Specified by:
setParameterin interfaceSqlQuery- Parameters:
name- the name of the parameterval- the possibly-null parameter valuetype- 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
Binds multiple values to a named query parameter. This is useful for binding a list of values to the query fragmentin (:value_list).- Specified by:
setParameterListin interfaceSqlQuery- Parameters:
name- the name of the parametervals- 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, Class<?>... moreTypes) throws SqlProcessorException Binds multiple values to a named query parameter. This is useful for binding a list of values to the query fragmentin (:value_list).- Specified by:
setParameterListin interfaceSqlQuery- Parameters:
name- the name of the parametervals- a collection of valuestype- 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, SqlFromToPlugin.LimitType limitType, int start) throws SQLException, SqlProcessorException 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 closedPreparedStatementSqlProcessorException
-
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
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
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
Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.- Specified by:
executeBatchin interfaceSqlQuery- Parameters:
statements- SQL statements to be executed in batch- Returns:
- an array of update counts containing one element for each command in the batch
- Throws:
SqlProcessorException- in the case of any problem in ORM or JDBC stack
-
newSqlProcessorException
-
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
-