org.midao.jdbc.core
Class AbstractQueryRunner

java.lang.Object
  extended by org.midao.jdbc.core.AbstractQueryRunner
All Implemented Interfaces:
QueryRunnerService
Direct Known Subclasses:
QueryRunner

public abstract class AbstractQueryRunner
extends java.lang.Object
implements QueryRunnerService

Core of QueryRunner implementation.


Field Summary
protected  Overrider overrider
           
 
Method Summary
protected  int[] batch(StatementHandler stmtHandler, java.lang.String sql, QueryParameters[] params)
          Execute a batch of SQL INSERT, UPDATE, or DELETE queries.
protected
<T> QueryParameters
call(StatementHandler stmtHandler, java.lang.String sql, QueryParameters params, OutputHandler<T> outputHandler)
          Executes the given CALL SQL statement.
 void commit()
          Commits current Transaction Usable only when QueryRunnerService.setTransactionManualMode(boolean) set as true
protected  QueryInputHandler convertToQueryInputHandler(AbstractNamedInputHandler inputHandler, java.lang.String catalog, java.lang.String schema, boolean useCache)
          Uses MetadataHandler to read Stored Procedure/Function parameters and creates new QueryInputHandler instance with parameter values from @inputHandler
protected  java.sql.Statement createStatement(java.sql.Connection conn, java.lang.String sql)
          Creates new Statement instance
 ExceptionHandler getExceptionHandler()
          Returns assigned exception handler
 MetadataHandler getMetadataHandler()
          Returns MetadataHandler implementation assigned to this QueryRunnerService instance
 Overrider getOverrider()
          Returns Overrider instance assigned to this QueryRunnerService instance
protected  QueryParameters[] getQueryParams(InputHandler[] inputHandlers)
          Reads array of InputHandler and returns array of parameter values Used during batch invocation
protected  QueryParameters[] getQueryParams(java.lang.Object[][] params)
          Converts array of parameter values (as array) and converts it into array of QueryParameters Used during batch invocation
protected  java.lang.String getSqlQuery(InputHandler[] inputHandlers)
          Reads array of InputHandler and checks if they have one query string.
 StatementHandler getStatementHandler()
          Returns StatementHandler implementation assigned to this QueryRunnerService instance
 TransactionHandler getTransactionHandler()
          Returns TransactionHandler implementation assigned to this QueryRunnerService instance
 java.lang.Integer getTransactionIsolationLevel()
          Returns current Transaction Isolation level
 TypeHandler getTypeHandler()
          Returns TypeHandler implementation assigned to this QueryRunnerService instance
 boolean isTransactionManualMode()
          Returns current Transaction mode
protected  java.sql.SQLException nullException()
          Returns new SQLException instance.
 QueryRunnerService override(java.lang.String operation, java.lang.Object value)
          Adds override which would be used until removed.
 QueryRunnerService overrideOnce(java.lang.String operation, java.lang.Object value)
          Adds override which would be used only once.
protected  java.sql.CallableStatement prepareCall(java.sql.Connection conn, java.lang.String sql)
          Creates new CallableStatement instance
protected  java.sql.PreparedStatement prepareStatement(java.sql.Connection conn, java.lang.String sql, boolean getGeneratedKeys)
          Creates new PreparedStatement instance
protected
<T> T
query(StatementHandler stmtHandler, java.lang.String sql, OutputHandler<T> outputHandler, QueryParameters params)
          Executes the given SELECT SQL query and returns a result object.
 void releaseSavepoint(java.sql.Savepoint savepoint)
          Removes the specified Savepoint and subsequent Savepoint objects from the current transaction.
 QueryRunnerService removeOverride(java.lang.String operation)
          Removes override.
 void rollback()
          Rollbacks current Transaction Usable only when QueryRunnerService.setTransactionManualMode(boolean) set as true
 void rollback(java.sql.Savepoint savepoint)
          Undoes all changes made after the given Savepoint object was set.
 void setExceptionHandler(ExceptionHandler exceptionHandler)
          Allows assigning exception handler
 void setMetadataHandler(MetadataHandler metadataHandler)
          Assigns MetadataHandler implementation to this QueryRunnerService instance Please be aware that input MetadataHandler should be share same Overrider instance: Example: QueryRunner.setMetadataHandler(new BaseMetadataHandler(queryRunner.getOverrider()));
 java.sql.Savepoint setSavepoint()
          Creates an unnamed savepoint in the current transaction and returns the new Savepoint object that represents it.
 java.sql.Savepoint setSavepoint(java.lang.String name)
          Creates a savepoint with the given name in the current transaction and returns the new Savepoint object that represents it.
 void setStatementHandler(StatementHandler statementHandler)
          Assigns StatementHandler implementation to this QueryRunnerService instance Please be aware that input StatementHandler should be share same Overrider instance: Example: QueryRunner.setStatementHandler(new BaseStatementHandler(queryRunner.getOverrider()));
 void setTransactionHandler(TransactionHandler transactionHandler)
          Assigns TransactionHandler implementation to this QueryRunnerService instance Please be aware that input TransactionHandler should be share same Overrider instance: Example: QueryRunner.setTransactionHandler(new BaseTransactionHandler(queryRunner.getOverrider()));
 void setTransactionIsolationLevel(java.lang.Integer level)
          Transaction Isolation level.
 void setTransactionManualMode(boolean manualMode)
          Sets Transaction mode.
 void setTypeHandler(TypeHandler typeHandler)
          Assigns TypeHandler implementation to this QueryRunnerService instance Please be aware that input TypeHandler should be share same Overrider instance: Example: QueryRunner.setTypeHandler(new BaseTypeHandler(queryRunner.getOverrider()));
protected
<T> T
update(StatementHandler stmtHandler, java.lang.String sql, OutputHandler<T> outputHandler, QueryParameters params)
          Executes the given INSERT, UPDATE, or DELETE SQL statement without any replacement parameters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.midao.jdbc.core.service.QueryRunnerService
batch, batch, call, call, call, call, call, query, query, query, update, update, update, update, update, update
 

Field Detail

overrider

protected final Overrider overrider
Method Detail

isTransactionManualMode

public boolean isTransactionManualMode()
Returns current Transaction mode

Specified by:
isTransactionManualMode in interface QueryRunnerService
Returns:
current Transaction mode

setTransactionManualMode

public void setTransactionManualMode(boolean manualMode)
Sets Transaction mode. By default system works in automatic mode. No additional actions is not required as every query execution would be committed right away. In manual mode every query execution is performed in current transaction. In order to commit changes execution of QueryRunnerService.commit() is required.

Specified by:
setTransactionManualMode in interface QueryRunnerService
Parameters:
manualMode - manual mode

setTransactionIsolationLevel

public void setTransactionIsolationLevel(java.lang.Integer level)
Transaction Isolation level. For general information please look at Java Tutorials (docs.oracle.com/javase/tutorial/index.html) JDBC basics / Using Transactions For detailed information please look at vendor JDBC driver description

Specified by:
setTransactionIsolationLevel in interface QueryRunnerService
Parameters:
level - Transaction Isolation level

getTransactionIsolationLevel

public java.lang.Integer getTransactionIsolationLevel()
Returns current Transaction Isolation level

Specified by:
getTransactionIsolationLevel in interface QueryRunnerService
Returns:
current Transaction Isolation level

commit

public void commit()
            throws java.sql.SQLException
Commits current Transaction Usable only when QueryRunnerService.setTransactionManualMode(boolean) set as true

Specified by:
commit in interface QueryRunnerService
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

rollback

public void rollback()
              throws java.sql.SQLException
Rollbacks current Transaction Usable only when QueryRunnerService.setTransactionManualMode(boolean) set as true

Specified by:
rollback in interface QueryRunnerService
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

setSavepoint

public java.sql.Savepoint setSavepoint()
                                throws java.sql.SQLException
Creates an unnamed savepoint in the current transaction and returns the new Savepoint object that represents it. Connection.setSavepoint() Usable only when QueryRunnerService.setTransactionManualMode(boolean) set as true

Specified by:
setSavepoint in interface QueryRunnerService
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

setSavepoint

public java.sql.Savepoint setSavepoint(java.lang.String name)
                                throws java.sql.SQLException
Creates a savepoint with the given name in the current transaction and returns the new Savepoint object that represents it. Connection.setSavepoint(String) Usable only when QueryRunnerService.setTransactionManualMode(boolean) set as true

Specified by:
setSavepoint in interface QueryRunnerService
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

rollback

public void rollback(java.sql.Savepoint savepoint)
              throws java.sql.SQLException
Undoes all changes made after the given Savepoint object was set. Connection.rollback(java.sql.Savepoint) Usable only when QueryRunnerService.setTransactionManualMode(boolean) set as true

Specified by:
rollback in interface QueryRunnerService
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

releaseSavepoint

public void releaseSavepoint(java.sql.Savepoint savepoint)
                      throws java.sql.SQLException
Removes the specified Savepoint and subsequent Savepoint objects from the current transaction. Connection.releaseSavepoint(java.sql.Savepoint) Usable only when QueryRunnerService.setTransactionManualMode(boolean) set as true

Specified by:
releaseSavepoint in interface QueryRunnerService
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

overrideOnce

public QueryRunnerService overrideOnce(java.lang.String operation,
                                       java.lang.Object value)
Adds override which would be used only once. Overriders provide wide variety of functionality. For whole list please look at MidaoConstants OVERRIDE_* Constants

Specified by:
overrideOnce in interface QueryRunnerService
Parameters:
operation - name of the operation
value - override value
Returns:
this instance of QueryRunner

override

public QueryRunnerService override(java.lang.String operation,
                                   java.lang.Object value)
Adds override which would be used until removed. Overriders provide wide variety of functionality. For whole list please look at MidaoConstants OVERRIDE_* Constants

Specified by:
override in interface QueryRunnerService
Parameters:
operation - name of the operation
value - override value
Returns:
this instance of QueryRunner

removeOverride

public QueryRunnerService removeOverride(java.lang.String operation)
Removes override. Overriders provide wide variety of functionality. For whole list please look at MidaoConstants OVERRIDE_* Constants

Specified by:
removeOverride in interface QueryRunnerService
Parameters:
operation - name of the operation
Returns:
this instance of QueryRunner

getTypeHandler

public TypeHandler getTypeHandler()
Returns TypeHandler implementation assigned to this QueryRunnerService instance

Returns:
TypeHandler implementation assigned to this QueryRunnerService instance

setTypeHandler

public void setTypeHandler(TypeHandler typeHandler)
Assigns TypeHandler implementation to this QueryRunnerService instance Please be aware that input TypeHandler should be share same Overrider instance: Example: QueryRunner.setTypeHandler(new BaseTypeHandler(queryRunner.getOverrider()));

Parameters:
typeHandler - TypeHandler implementation

getTransactionHandler

public TransactionHandler getTransactionHandler()
Returns TransactionHandler implementation assigned to this QueryRunnerService instance

Returns:
TransactionHandler implementation assigned to this QueryRunnerService instance

setTransactionHandler

public void setTransactionHandler(TransactionHandler transactionHandler)
Assigns TransactionHandler implementation to this QueryRunnerService instance Please be aware that input TransactionHandler should be share same Overrider instance: Example: QueryRunner.setTransactionHandler(new BaseTransactionHandler(queryRunner.getOverrider()));

Parameters:
transactionHandler - TransactionHandler implementation

getStatementHandler

public StatementHandler getStatementHandler()
Returns StatementHandler implementation assigned to this QueryRunnerService instance

Returns:
StatementHandler implementation assigned to this QueryRunnerService instance

setStatementHandler

public void setStatementHandler(StatementHandler statementHandler)
Assigns StatementHandler implementation to this QueryRunnerService instance Please be aware that input StatementHandler should be share same Overrider instance: Example: QueryRunner.setStatementHandler(new BaseStatementHandler(queryRunner.getOverrider()));

Parameters:
statementHandler - StatementHandler implementation

getMetadataHandler

public MetadataHandler getMetadataHandler()
Returns MetadataHandler implementation assigned to this QueryRunnerService instance

Returns:
MetadataHandler implementation assigned to this QueryRunnerService instance

setMetadataHandler

public void setMetadataHandler(MetadataHandler metadataHandler)
Assigns MetadataHandler implementation to this QueryRunnerService instance Please be aware that input MetadataHandler should be share same Overrider instance: Example: QueryRunner.setMetadataHandler(new BaseMetadataHandler(queryRunner.getOverrider()));

Parameters:
metadataHandler - MetadataHandler implementation

getOverrider

public Overrider getOverrider()
Returns Overrider instance assigned to this QueryRunnerService instance

Returns:
Overrider instance assigned to this QueryRunnerService instance

createStatement

protected java.sql.Statement createStatement(java.sql.Connection conn,
                                             java.lang.String sql)
                                      throws java.sql.SQLException
Creates new Statement instance

Parameters:
conn - SQL Connection
sql - SQL Query string
Returns:
new Statement instance
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

prepareStatement

protected java.sql.PreparedStatement prepareStatement(java.sql.Connection conn,
                                                      java.lang.String sql,
                                                      boolean getGeneratedKeys)
                                               throws java.sql.SQLException
Creates new PreparedStatement instance

Parameters:
conn - SQL Connection
sql - SQL Query string
getGeneratedKeys - specifies if generated keys should be returned
Returns:
new PreparedStatement instance
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

prepareCall

protected java.sql.CallableStatement prepareCall(java.sql.Connection conn,
                                                 java.lang.String sql)
                                          throws java.sql.SQLException
Creates new CallableStatement instance

Parameters:
conn - SQL Connection
sql - SQL Query string
Returns:
new CallableStatement instance
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

nullException

protected java.sql.SQLException nullException()
Returns new SQLException instance. Used to throw exception during input arguments null check

Returns:
new SQLException instance

batch

protected int[] batch(StatementHandler stmtHandler,
                      java.lang.String sql,
                      QueryParameters[] params)
               throws java.sql.SQLException
Execute a batch of SQL INSERT, UPDATE, or DELETE queries.

Parameters:
stmtHandler - StatementHandler implementation
sql - The SQL query to execute.
params - An array of query replacement parameters. Each row in this array is one set of batch replacement values.
Returns:
array of row affected
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

query

protected <T> T query(StatementHandler stmtHandler,
                      java.lang.String sql,
                      OutputHandler<T> outputHandler,
                      QueryParameters params)
           throws java.sql.SQLException
Executes the given SELECT SQL query and returns a result object.

Type Parameters:
T - The type of object that the handler returns
Parameters:
stmtHandler - StatementHandler implementation
sql - The SQL query to execute.
outputHandler - OutputHandler implementation
params - parameter values
Returns:
converted result
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

update

protected <T> T update(StatementHandler stmtHandler,
                       java.lang.String sql,
                       OutputHandler<T> outputHandler,
                       QueryParameters params)
            throws java.sql.SQLException
Executes the given INSERT, UPDATE, or DELETE SQL statement without any replacement parameters.

Type Parameters:
T - The type of object that the handler returns
Parameters:
stmtHandler - StatementHandler implementation
sql - The SQL query to execute.
outputHandler - OutputHandler implementation
params - parameter values
Returns:
converted result
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

call

protected <T> QueryParameters call(StatementHandler stmtHandler,
                                   java.lang.String sql,
                                   QueryParameters params,
                                   OutputHandler<T> outputHandler)
                        throws java.sql.SQLException
Executes the given CALL SQL statement. Allows execution of Stored Procedures/Functions

Parameters:
stmtHandler - StatementHandler implementation
sql - The SQL query to execute.
params - parameter values
outputHandler - OutputHandler implementation
Returns:
Query Output. All input parameters are updated from OUT parameters. Stored Function return is stored there as well. Can be received by invoking QueryParameters.getReturn()
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

getSqlQuery

protected java.lang.String getSqlQuery(InputHandler[] inputHandlers)
                                throws java.sql.SQLException
Reads array of InputHandler and checks if they have one query string. Used during batch invocation

Parameters:
inputHandlers - array of InputHandler
Returns:
SQL query string
Throws:
java.sql.SQLException - if SQL Query string is not the same among whole array of InputHandler

getQueryParams

protected QueryParameters[] getQueryParams(InputHandler[] inputHandlers)
Reads array of InputHandler and returns array of parameter values Used during batch invocation

Parameters:
inputHandlers - array of InputHandler
Returns:
array of query parameter values

getQueryParams

protected QueryParameters[] getQueryParams(java.lang.Object[][] params)
Converts array of parameter values (as array) and converts it into array of QueryParameters Used during batch invocation

Parameters:
params -
Returns:

convertToQueryInputHandler

protected QueryInputHandler convertToQueryInputHandler(AbstractNamedInputHandler inputHandler,
                                                       java.lang.String catalog,
                                                       java.lang.String schema,
                                                       boolean useCache)
                                                throws java.sql.SQLException
Uses MetadataHandler to read Stored Procedure/Function parameters and creates new QueryInputHandler instance with parameter values from @inputHandler

Parameters:
inputHandler - AbstractNamedInputHandler which used as source for QueryInputHandler parameter values
catalog - Database Catalog
schema - Database Schema
useCache - specifies if MetadataHandler should use cache
Returns:
new filled QueryInputHandler instance with values from @inputHandler
Throws:
java.sql.SQLException - if exception would be thrown by Driver/Database

getExceptionHandler

public ExceptionHandler getExceptionHandler()
Returns assigned exception handler

Returns:
assigned exception handler

setExceptionHandler

public void setExceptionHandler(ExceptionHandler exceptionHandler)
Allows assigning exception handler

Parameters:
exceptionHandler - new exception handler which should be used by this instance of QueryRunner


Copyright © 2013. All Rights Reserved.