com.github.drinkjava2.jdbpro
Class ImprovedQueryRunner

java.lang.Object
  extended by org.apache.commons.dbutils.AbstractQueryRunner
      extended by org.apache.commons.dbutils.QueryRunner
          extended by com.github.drinkjava2.jdbpro.ImprovedQueryRunner
Direct Known Subclasses:
DbPro

public class ImprovedQueryRunner
extends org.apache.commons.dbutils.QueryRunner

ImprovedQueryRunner made below improvements compare DbUtils's QueryRunner: 1) Override close() and prepareConnection() method of QueryRunner, use a ConnectionManager to manage connection, ConnectionManager can get connection from DataSource or ThreadLocal or some other 3rd party tools like Spring.
2) Override some methods to add logger support
3) Override some execute/update/query methods to support batch operation and AroundSqlHandler

Since:
1.7.0
Author:
Yong Zhu

Field Summary
protected  Boolean allowShowSQL
           
protected  Integer batchSize
           
protected  ConnectionManager connectionManager
           
protected  IocTool iocTool
          An IOC tool is needed if want use SqlMapper style and Annotation has parameters
protected  DbProLogger logger
           
protected  DbPro[] masters
           
protected  SqlOption masterSlaveOption
           
protected  String name
           
protected  DbPro[] slaves
           
protected  SqlHandler[] sqlHandlers
           
protected  SqlTemplateEngine sqlTemplateEngine
           
 
Fields inherited from class org.apache.commons.dbutils.AbstractQueryRunner
ds
 
Constructor Summary
ImprovedQueryRunner()
           
ImprovedQueryRunner(DataSource ds)
           
ImprovedQueryRunner(DataSource ds, ConnectionManager cm)
           
 
Method Summary
 void close(Connection conn)
           
 void fillStatement(PreparedStatement stmt, Object... params)
           
protected  String formatParametersForLoggerOutput(Object... params)
          Format parameters for logger output, subClass can override this method to customise parameters format
protected  String formatSqlForLoggerOutput(String sql)
          Format SQL for logger output, subClass can override this method to customise SQL format
 Boolean getAllowShowSQL()
           
 Integer getBatchSize()
           
 ConnectionManager getConnectionManager()
           
 IocTool getIocTool()
           
 DbProLogger getLogger()
           
 DbPro[] getMasters()
           
 SqlOption getMasterSlaveOption()
           
 String getName()
           
 DbPro[] getSlaves()
           
 ThreadLocal<ArrayList<PreparedSQL>> getSqlBatchCache()
           
 SqlHandler[] getSqlHandlers()
           
 SqlTemplateEngine getSqlTemplateEngine()
           
static SqlHandler[] getThreadLocalSqlHandlers()
          Get current thread's ThreadLocal SqlHandler
 boolean isBatchEnabled()
           
static Object[][] listListToArray2D(List<List<?>> paramList)
          Convert Objects List to 2d array for insertBatch use, insertBatch's last parameter is a 2d array, not easy to use
 int[] nBatch(Connection conn, String sql, List<Object[]> params)
          Execute a batch of SQL INSERT, UPDATE, or DELETE queries.
 int[] nBatch(String sql, List<Object[]> params)
          Execute a batch of SQL INSERT, UPDATE, or DELETE queries.
 void nBatchBegin()
          Start batch sql
 void nBatchEnd()
          Stop batch sql
<T> T
nBatchFlush()
          Force flush cached SQLs
<T> T
nInsertBatch(Connection conn, String sql, org.apache.commons.dbutils.ResultSetHandler<T> rsh, List<Object[]> params)
          Executes the given batch of INSERT SQL statements.
<T> T
nInsertBatch(String sql, org.apache.commons.dbutils.ResultSetHandler<T> rsh, List<Object[]> params)
          Executes the given batch of INSERT SQL statements.
static Object[][] objectsListToArray2D(List<Object[]> paramList)
          Convert List List to 2d array for insertBatch use, insertBatch's last parameter is a 2d array, not easy to use
protected  CallableStatement prepareCall(Connection conn, String sql)
           
 Connection prepareConnection()
           
protected  PreparedStatement prepareStatement(Connection conn, String sql)
           
 long queryForLongValue(Connection conn, String sql, Object... params)
          Query for a long value
 long queryForLongValue(String sql, Object... params)
          Query for a long value
<T> T
queryForObject(Connection conn, String sql, Object... params)
          Query for an Object, only return the first row and first column's value if more than one column or more than 1 rows returned, a null object may return if no result found, SQLException may be threw if some SQL operation Exception happen.
<T> T
queryForObject(String sql, Object... params)
          Query for an Object, only return the first row and first column's value if more than one column or more than 1 rows returned, a null object may return if no result found, SQLException may be threw if some SQL operation Exception happen.
protected
<T> T
runExecute(PreparedSQL ps)
          Executes the PreparedSQL query statement
protected
<T> T
runInsert(PreparedSQL ps)
          Executes the PreparedSQL insert statement
 Object runPreparedSQL(PreparedSQL ps)
          This is the core method of whole project, handle a PreparedSQL instance and return a result
protected
<T> T
runQuery(PreparedSQL ps)
          Executes the PreparedSQL query statement
 Object runRealSqlMethod(PreparedSQL ps)
          Execute real SQL operation according PreparedSql's SqlType
protected  int runUpdate(PreparedSQL ps)
          Executes the PreparedSQL update statement
 void setAllowShowSQL(Boolean allowShowSQL)
          Deprecated. 
 void setBatchSize(Integer batchSize)
          Deprecated. 
 void setConnectionManager(ConnectionManager connectionManager)
          Deprecated. 
 void setIocTool(IocTool iocTool)
          Deprecated. 
 void setLogger(DbProLogger logger)
          Deprecated. 
 void setMasters(DbPro[] masters)
          Deprecated. 
 void setMasterSlaveOption(SqlOption masterSlaveOption)
          Deprecated. 
 void setName(String name)
          This method is not thread safe, suggest only use at program starting
 void setSlaves(DbPro[] slaves)
          Deprecated. 
 void setSqlHandlers(SqlHandler[] sqlHandlers)
          Deprecated. 
 void setSqlTemplateEngine(SqlTemplateEngine sqlTemplateEngine)
          Deprecated. 
static void setThreadLocalSqlHandlers(SqlHandler... handlers)
          Set current thread's ThreadLocal SqlHandler
 
Methods inherited from class org.apache.commons.dbutils.QueryRunner
batch, batch, execute, execute, execute, execute, insert, insert, insert, insert, insertBatch, insertBatch, query, query, query, query, query, query, query, query, update, update, update, update, update, update
 
Methods inherited from class org.apache.commons.dbutils.AbstractQueryRunner
close, close, fillStatementWithBean, fillStatementWithBean, getDataSource, isPmdKnownBroken, prepareStatement, rethrow, wrap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sqlTemplateEngine

protected SqlTemplateEngine sqlTemplateEngine

connectionManager

protected ConnectionManager connectionManager

allowShowSQL

protected Boolean allowShowSQL

masterSlaveOption

protected SqlOption masterSlaveOption

logger

protected DbProLogger logger

batchSize

protected Integer batchSize

sqlHandlers

protected SqlHandler[] sqlHandlers

slaves

protected DbPro[] slaves

masters

protected DbPro[] masters

name

protected String name

iocTool

protected IocTool iocTool
An IOC tool is needed if want use SqlMapper style and Annotation has parameters

Constructor Detail

ImprovedQueryRunner

public ImprovedQueryRunner()

ImprovedQueryRunner

public ImprovedQueryRunner(DataSource ds)

ImprovedQueryRunner

public ImprovedQueryRunner(DataSource ds,
                           ConnectionManager cm)
Method Detail

close

public void close(Connection conn)
           throws SQLException
Overrides:
close in class org.apache.commons.dbutils.AbstractQueryRunner
Throws:
SQLException

prepareConnection

public Connection prepareConnection()
                             throws SQLException
Overrides:
prepareConnection in class org.apache.commons.dbutils.AbstractQueryRunner
Throws:
SQLException

prepareCall

protected CallableStatement prepareCall(Connection conn,
                                        String sql)
                                 throws SQLException
Overrides:
prepareCall in class org.apache.commons.dbutils.AbstractQueryRunner
Throws:
SQLException

prepareStatement

protected PreparedStatement prepareStatement(Connection conn,
                                             String sql)
                                      throws SQLException
Overrides:
prepareStatement in class org.apache.commons.dbutils.AbstractQueryRunner
Throws:
SQLException

fillStatement

public void fillStatement(PreparedStatement stmt,
                          Object... params)
                   throws SQLException
Overrides:
fillStatement in class org.apache.commons.dbutils.AbstractQueryRunner
Throws:
SQLException

formatSqlForLoggerOutput

protected String formatSqlForLoggerOutput(String sql)
Format SQL for logger output, subClass can override this method to customise SQL format


formatParametersForLoggerOutput

protected String formatParametersForLoggerOutput(Object... params)
Format parameters for logger output, subClass can override this method to customise parameters format


nBatchFlush

public <T> T nBatchFlush()
Force flush cached SQLs


nBatchBegin

public void nBatchBegin()
Start batch sql


nBatchEnd

public void nBatchEnd()
Stop batch sql


queryForObject

public <T> T queryForObject(Connection conn,
                            String sql,
                            Object... params)
                 throws SQLException
Query for an Object, only return the first row and first column's value if more than one column or more than 1 rows returned, a null object may return if no result found, SQLException may be threw if some SQL operation Exception happen.

Parameters:
sql - The SQL
params - The parameters
Returns:
An Object or null, Object type determined by SQL content
Throws:
SQLException

queryForLongValue

public long queryForLongValue(Connection conn,
                              String sql,
                              Object... params)
                       throws SQLException
Query for a long value

Parameters:
sql - The SQL
params - The parameters
Returns:
A long value
Throws:
SQLException

queryForObject

public <T> T queryForObject(String sql,
                            Object... params)
                 throws SQLException
Query for an Object, only return the first row and first column's value if more than one column or more than 1 rows returned, a null object may return if no result found, SQLException may be threw if some SQL operation Exception happen.

Parameters:
sql - The SQL
params - The parameters
Returns:
An Object or null, Object type determined by SQL content
Throws:
SQLException

queryForLongValue

public long queryForLongValue(String sql,
                              Object... params)
                       throws SQLException
Query for a long value

Parameters:
sql - The SQL
params - The parameters
Returns:
A long value
Throws:
SQLException

runPreparedSQL

public Object runPreparedSQL(PreparedSQL ps)
This is the core method of whole project, handle a PreparedSQL instance and return a result


runRealSqlMethod

public Object runRealSqlMethod(PreparedSQL ps)
Execute real SQL operation according PreparedSql's SqlType


runQuery

protected <T> T runQuery(PreparedSQL ps)
Executes the PreparedSQL query statement

Parameters:
ps - The PreparedSQL
Returns:
object(s) generated by ResultSetHandler

runInsert

protected <T> T runInsert(PreparedSQL ps)
Executes the PreparedSQL insert statement

Parameters:
ps - The PreparedSQL
Returns:
An object generated by ResultSetHandler

runExecute

protected <T> T runExecute(PreparedSQL ps)
Executes the PreparedSQL query statement

Parameters:
ps - The PreparedSQL
Returns:
The number of rows updated.

runUpdate

protected int runUpdate(PreparedSQL ps)
Executes the PreparedSQL update statement

Parameters:
ps - The PreparedSQL
Returns:
The number of rows updated

nBatch

public int[] nBatch(String sql,
                    List<Object[]> params)
Execute a batch of SQL INSERT, UPDATE, or DELETE queries.

Parameters:
sql - The SQL to execute.
params - A List of parameter list.
Returns:
The number of rows updated per statement.

nBatch

public int[] nBatch(Connection conn,
                    String sql,
                    List<Object[]> params)
             throws SQLException
Execute a batch of SQL INSERT, UPDATE, or DELETE queries.

Parameters:
conn - The Connection to use to run the query. The caller is responsible for closing this Connection.
sql - The SQL to execute.
params - A List of parameter list.
Returns:
The number of rows updated per statement.
Throws:
SQLException

nInsertBatch

public <T> T nInsertBatch(String sql,
                          org.apache.commons.dbutils.ResultSetHandler<T> rsh,
                          List<Object[]> params)
Executes the given batch of INSERT SQL statements.

Type Parameters:
T - The type of object that the handler returns
Parameters:
sql - The SQL statement to execute.
rsh - The handler used to create the result object from the ResultSet of auto-generated keys.
params - A List of parameter list.
Returns:
The result generated by the handler.

nInsertBatch

public <T> T nInsertBatch(Connection conn,
                          String sql,
                          org.apache.commons.dbutils.ResultSetHandler<T> rsh,
                          List<Object[]> params)
Executes the given batch of INSERT SQL statements.

Type Parameters:
T - The type of object that the handler returns
Parameters:
conn - The connection to use to run the query.
sql - The SQL to execute.
rsh - The handler used to create the result object from the ResultSet of auto-generated keys.
params - A List of parameter list.
Returns:
The result generated by the handler.

listListToArray2D

public static Object[][] listListToArray2D(List<List<?>> paramList)
Convert Objects List to 2d array for insertBatch use, insertBatch's last parameter is a 2d array, not easy to use


objectsListToArray2D

public static Object[][] objectsListToArray2D(List<Object[]> paramList)
Convert List List to 2d array for insertBatch use, insertBatch's last parameter is a 2d array, not easy to use


getThreadLocalSqlHandlers

public static SqlHandler[] getThreadLocalSqlHandlers()
Get current thread's ThreadLocal SqlHandler


setThreadLocalSqlHandlers

public static void setThreadLocalSqlHandlers(SqlHandler... handlers)
Set current thread's ThreadLocal SqlHandler


getAllowShowSQL

public Boolean getAllowShowSQL()

setAllowShowSQL

@Deprecated
public void setAllowShowSQL(Boolean allowShowSQL)
Deprecated. 

This method is not thread safe, suggest only use at program starting


getSqlTemplateEngine

public SqlTemplateEngine getSqlTemplateEngine()

setSqlTemplateEngine

@Deprecated
public void setSqlTemplateEngine(SqlTemplateEngine sqlTemplateEngine)
Deprecated. 

This method is not thread safe, suggest only use at program starting


getConnectionManager

public ConnectionManager getConnectionManager()

setConnectionManager

@Deprecated
public void setConnectionManager(ConnectionManager connectionManager)
Deprecated. 

This method is not thread safe, suggest only use at program starting


getLogger

public DbProLogger getLogger()

setLogger

@Deprecated
public void setLogger(DbProLogger logger)
Deprecated. 

This method is not thread safe, suggest only use at program starting


getBatchSize

public Integer getBatchSize()

setBatchSize

@Deprecated
public void setBatchSize(Integer batchSize)
Deprecated. 

This method is not thread safe, suggest only use at program starting


getSqlHandlers

public SqlHandler[] getSqlHandlers()

setSqlHandlers

@Deprecated
public void setSqlHandlers(SqlHandler[] sqlHandlers)
Deprecated. 

This method is not thread safe, suggest only use at program starting


getSlaves

public DbPro[] getSlaves()

setSlaves

@Deprecated
public void setSlaves(DbPro[] slaves)
Deprecated. 

This method is not thread safe, suggest only use at program starting


getMasters

public DbPro[] getMasters()

setMasters

@Deprecated
public void setMasters(DbPro[] masters)
Deprecated. 

This method is not thread safe, suggest only use at program starting


getIocTool

public IocTool getIocTool()

setIocTool

@Deprecated
public void setIocTool(IocTool iocTool)
Deprecated. 

This method is not thread safe, suggest only use at program starting


getMasterSlaveOption

public SqlOption getMasterSlaveOption()

setMasterSlaveOption

@Deprecated
public void setMasterSlaveOption(SqlOption masterSlaveOption)
Deprecated. 

This method is not thread safe, suggest only use at program starting


getName

public String getName()

setName

public void setName(String name)
This method is not thread safe, suggest only use at program starting


isBatchEnabled

public boolean isBatchEnabled()

getSqlBatchCache

public ThreadLocal<ArrayList<PreparedSQL>> getSqlBatchCache()


Copyright © 2018. All rights reserved.