com.github.drinkjava2.jdbpro
Class DbPro

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

public class DbPro
extends ImprovedQueryRunner
implements NormalJdbcTool

DbPro is the enhanced version of Apache Commons DbUtils's QueryRunner, add below improvements:

 1)Use ConnectionManager to manage connection for better transaction support
 2)normal style methods but no longer throw SQLException, methods named as nXxxxx() format
 3)In-line style methods, methods named as iXxxxx() format
 4)SQL Template style methods, methods named as tXxxxx() format
 

Since:
1.7.0
Author:
Yong Zhu

Field Summary
 
Fields inherited from class com.github.drinkjava2.jdbpro.ImprovedQueryRunner
allowShowSQL, batchSize, connectionManager, iocTool, logger, masters, masterSlaveOption, name, slaves, sqlHandlers, sqlTemplateEngine
 
Fields inherited from class org.apache.commons.dbutils.AbstractQueryRunner
ds
 
Constructor Summary
DbPro()
           
DbPro(DataSource ds)
           
DbPro(DataSource ds, DbProConfig config)
           
DbPro(DbProConfig config)
           
 
Method Summary
 void ________iXxxxStyles________()
           
 void ________nXxxxStyles________()
          nXxxx style series methods are design to replace QueryRunner's xxxx method, the difference is nXxxx methods do not throw SqlException
 void ________prepareMethods________()
           
 void ________pXxxxStyles________()
           
 void ________tXxxxStyles________()
           
protected  boolean dealOneSqlItem(boolean iXxxStyle, PreparedSQL predSQL, Object item)
          Here deal one SqlItem, if can deal it, return true, otherwise return false, subclass (like SqlBoxContext) can override this method
 PreparedSQL dealSqlItems(PreparedSQL lastPreSql, boolean iXxxStyle, Object... items)
          Deal with multiple SqlItems
<T> T
iExecute(Object... inlineSQL)
          Executes the in-line style execute statement
<T> T
iInsert(Object... inlineSQL)
          Executes the in-line style insert statement
 PreparedSQL iPrepare(Object... items)
          Prepare a PreparedSQL for iXxxx (Single SQL) style, unknown objects (include null) will automatically looked as SQL pieces, more detail see doPrepare method
<T> T
iQuery(Object... inlineSQL)
          Executes the in-line style query statement
 long iQueryForLongValue(Object... inlineSQL)
          In-line style execute query and force return a long value, runtime exception may throw if result can not be cast to long.
 List<Map<String,Object>> iQueryForMapList(Object... items)
          In-Line style execute query and force return a List> type result.
<T> T
iQueryForObject(Object... inlineSQL)
          Execute an In-line style 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
 String iQueryForString(Object... inlineSQL)
          In-line style execute query and force return a String object.
 int iUpdate(Object... inlineSQL)
          Executes the in-line style INSERT, UPDATE, or DELETE statement
<T> List<T>
nExecute(Connection conn, org.apache.commons.dbutils.ResultSetHandler<T> rsh, String sql, Object... params)
          Execute an statement, including a stored procedure call, which returns one or more result sets.
 int nExecute(Connection conn, String sql, Object... params)
          Execute an statement, including a stored procedure call, which does not return any result sets.
<T> List<T>
nExecute(org.apache.commons.dbutils.ResultSetHandler rsh, String sql, Object... params)
          Execute an statement, including a stored procedure call, which returns one or more result sets.
 int nExecute(String sql, Object... params)
          Execute an statement, including a stored procedure call, which does not return any result sets.
<T> T
nInsert(Connection conn, org.apache.commons.dbutils.ResultSetHandler<T> rsh, String sql, Object... params)
          Executes the given INSERT SQL statement.
<T> T
nInsert(org.apache.commons.dbutils.ResultSetHandler rsh, String sql, Object... params)
          Executes the given INSERT SQL statement.
<T> T
nQuery(Connection conn, org.apache.commons.dbutils.ResultSetHandler<T> rsh, String sql, Object... params)
          Executes the given SELECT SQL query and returns a result object.
<T> T
nQuery(org.apache.commons.dbutils.ResultSetHandler<T> rsh, String sql, Object... params)
          Executes the given SELECT SQL query and returns a result object.
 long nQueryForLongValue(Connection conn, String sql, Object... params)
          Execute query and force return a Long object, no need catch SQLException, runtime exception may throw if result can not be cast to long.
 long nQueryForLongValue(String sql, Object... params)
          Execute query and force return a Long object, no need catch SQLException, runtime exception may throw if result can not be cast to long
 List<Map<String,Object>> nQueryForMapList(Connection conn, String sql, Object... params)
          Execute query and force return a List> type result, no need catch SQLException.
 List<Map<String,Object>> nQueryForMapList(String sql, Object... params)
          Execute query and force return a List> type result, no need catch SQLException
<T> T
nQueryForObject(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 , DbProRuntimeException may be threw if some SQL operation Exception happen.
<T> T
nQueryForObject(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 , DbProRuntimeException may be threw if some SQL operation Exception happen.
 String nQueryForString(Connection conn, String sql, Object... params)
          Execute query and force return a String object, no need catch SQLException.
 String nQueryForString(String sql, Object... params)
          Execute query and force return a String object, no need catch SQLException
 int nUpdate(Connection conn, String sql, Object... params)
          Executes the given INSERT, UPDATE, or DELETE SQL statement.
 int nUpdate(String sql, Object... params)
          Executes the given INSERT, UPDATE, or DELETE SQL statement.
<T> T
pExecute(Object... items)
          Executes the pXxxx style execute statement
<T> T
pInsert(Object... items)
          Executes the pXxxx style insert statement
 PreparedSQL pPrepare(Object... items)
          Prepare a PreparedSQL for pXxxx (Single SQL) style, pXxxx style only allow single String (The first appeared) as SQL, unknown objects (include null) will automatically looked as SQL parameters, more detail see doPrepare method
<T> T
pQuery(Object... items)
          Executes the pXxxx style query statement
 long pQueryForLongValue(Object... items)
          pXxxx style execute query and force return a long value, runtime exception may throw if result can not be cast to long.
 List<Map<String,Object>> pQueryForMapList(Object... items)
          pXxxx style execute query and force return a List> type result.
<T> T
pQueryForObject(Object... items)
          Execute an pXxxx style 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
 String pQueryForString(Object... items)
          pXxxx style execute query and force return a String object.
 int pUpdate(Object... items)
          Executes the pXxxx style INSERT, UPDATE, or DELETE statement
 int quiteExecute(String sql, Object... params)
          Quite execute a SQL, do not throw any exception, if any exception happen, return -1
<T> T
tExecute(Object... items)
          Executes the pXxxx style execute statement
<T> T
tInsert(Object... items)
          Executes the pXxxx style insert statement
<T> T
tQuery(Object... items)
          Executes the pXxxx style query statement
 long tQueryForLongValue(Object... items)
          pXxxx style execute query and force return a long value, runtime exception may throw if result can not be cast to long.
 List<Map<String,Object>> tQueryForMapList(Object... items)
          pXxxx style execute query and force return a List> type result.
<T> T
tQueryForObject(Object... items)
          Execute an pXxxx style 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
 String tQueryForString(Object... items)
          pXxxx style execute query and force return a String object.
 int tUpdate(Object... items)
          Executes the pXxxx style INSERT, UPDATE, or DELETE statement
 
Methods inherited from class com.github.drinkjava2.jdbpro.ImprovedQueryRunner
close, fillStatement, formatParametersForLoggerOutput, formatSqlForLoggerOutput, getAllowShowSQL, getBatchSize, getConnectionManager, getIocTool, getLogger, getMasters, getMasterSlaveOption, getName, getSlaves, getSqlBatchCache, getSqlHandlers, getSqlTemplateEngine, getThreadLocalSqlHandlers, isBatchEnabled, listListToArray2D, nBatch, nBatch, nBatchBegin, nBatchEnd, nBatchFlush, nInsertBatch, nInsertBatch, objectsListToArray2D, prepareCall, prepareConnection, prepareStatement, queryForLongValue, queryForLongValue, queryForObject, queryForObject, runExecute, runInsert, runPreparedSQL, runQuery, runRealSqlMethod, runUpdate, setAllowShowSQL, setBatchSize, setConnectionManager, setIocTool, setLogger, setMasters, setMasterSlaveOption, setName, setSlaves, setSqlHandlers, setSqlTemplateEngine, setThreadLocalSqlHandlers
 
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
 

Constructor Detail

DbPro

public DbPro()

DbPro

public DbPro(DataSource ds)

DbPro

public DbPro(DbProConfig config)

DbPro

public DbPro(DataSource ds,
             DbProConfig config)
Method Detail

quiteExecute

public int quiteExecute(String sql,
                        Object... params)
Quite execute a SQL, do not throw any exception, if any exception happen, return -1


________prepareMethods________

public void ________prepareMethods________()

iPrepare

public PreparedSQL iPrepare(Object... items)
Prepare a PreparedSQL for iXxxx (Single SQL) style, unknown objects (include null) will automatically looked as SQL pieces, more detail see doPrepare method


pPrepare

public PreparedSQL pPrepare(Object... items)
Prepare a PreparedSQL for pXxxx (Single SQL) style, pXxxx style only allow single String (The first appeared) as SQL, unknown objects (include null) will automatically looked as SQL parameters, more detail see doPrepare method


dealSqlItems

public PreparedSQL dealSqlItems(PreparedSQL lastPreSql,
                                boolean iXxxStyle,
                                Object... items)
Deal with multiple SqlItems


dealOneSqlItem

protected boolean dealOneSqlItem(boolean iXxxStyle,
                                 PreparedSQL predSQL,
                                 Object item)
Here deal one SqlItem, if can deal it, return true, otherwise return false, subclass (like SqlBoxContext) can override this method


________iXxxxStyles________

public void ________iXxxxStyles________()

iQuery

public <T> T iQuery(Object... inlineSQL)
Executes the in-line style query statement

Parameters:
inlineSQL - the in-line style SQL
Returns:
An object generated by the handler.

iQueryForObject

public <T> T iQueryForObject(Object... inlineSQL)
Execute an In-line style 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

Parameters:
inlineSQL -
params -
Returns:
An Object or null value determined by SQL content

iQueryForLongValue

public long iQueryForLongValue(Object... inlineSQL)
In-line style execute query and force return a long value, runtime exception may throw if result can not be cast to long.


iQueryForString

public String iQueryForString(Object... inlineSQL)
In-line style execute query and force return a String object.


iQueryForMapList

public List<Map<String,Object>> iQueryForMapList(Object... items)
In-Line style execute query and force return a List> type result.


iUpdate

public int iUpdate(Object... inlineSQL)
Executes the in-line style INSERT, UPDATE, or DELETE statement

Parameters:
inlineSQL - the in-line style SQL
Returns:
The number of rows updated.

iInsert

public <T> T iInsert(Object... inlineSQL)
Executes the in-line style insert statement

Parameters:
inlineSQL - the in-line style SQL
Returns:
An object generated by the handler.

iExecute

public <T> T iExecute(Object... inlineSQL)
Executes the in-line style execute statement

Parameters:
inlineSQL - the in-line style SQL
Returns:
A list of objects generated by the handler, or number of rows updated if no handler

________pXxxxStyles________

public void ________pXxxxStyles________()

pQuery

public <T> T pQuery(Object... items)
Executes the pXxxx style query statement

Parameters:
items - The items
Returns:
An object generated by the handler.

pQueryForObject

public <T> T pQueryForObject(Object... items)
Execute an pXxxx style 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

Parameters:
items - The items
Returns:
An Object or null value determined by SQL content

pQueryForLongValue

public long pQueryForLongValue(Object... items)
pXxxx style execute query and force return a long value, runtime exception may throw if result can not be cast to long.


pQueryForString

public String pQueryForString(Object... items)
pXxxx style execute query and force return a String object.


pQueryForMapList

public List<Map<String,Object>> pQueryForMapList(Object... items)
pXxxx style execute query and force return a List> type result.


pUpdate

public int pUpdate(Object... items)
Executes the pXxxx style INSERT, UPDATE, or DELETE statement

Parameters:
items - the items
Returns:
The number of rows updated.

pInsert

public <T> T pInsert(Object... items)
Executes the pXxxx style insert statement

Parameters:
inlineSQL - the in-line style SQL
Returns:
An object generated by the handler.

pExecute

public <T> T pExecute(Object... items)
Executes the pXxxx style execute statement

Parameters:
items - the items
Returns:
A list of objects generated by the handler, or number of rows updated if no handler

________tXxxxStyles________

public void ________tXxxxStyles________()

tQuery

public <T> T tQuery(Object... items)
Executes the pXxxx style query statement

Parameters:
items - The items
Returns:
An object generated by the handler.

tQueryForObject

public <T> T tQueryForObject(Object... items)
Execute an pXxxx style 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

Parameters:
items - The items
Returns:
An Object or null value determined by SQL content

tQueryForLongValue

public long tQueryForLongValue(Object... items)
pXxxx style execute query and force return a long value, runtime exception may throw if result can not be cast to long.


tQueryForString

public String tQueryForString(Object... items)
pXxxx style execute query and force return a String object.


tQueryForMapList

public List<Map<String,Object>> tQueryForMapList(Object... items)
pXxxx style execute query and force return a List> type result.


tUpdate

public int tUpdate(Object... items)
Executes the pXxxx style INSERT, UPDATE, or DELETE statement

Parameters:
items - the items
Returns:
The number of rows updated.

tInsert

public <T> T tInsert(Object... items)
Executes the pXxxx style insert statement

Parameters:
inlineSQL - the in-line style SQL
Returns:
An object generated by the handler.

tExecute

public <T> T tExecute(Object... items)
Executes the pXxxx style execute statement

Parameters:
items - the items
Returns:
A list of objects generated by the handler, or number of rows updated if no handler

________nXxxxStyles________

public void ________nXxxxStyles________()
nXxxx style series methods are design to replace QueryRunner's xxxx method, the difference is nXxxx methods do not throw SqlException


nQuery

public <T> T nQuery(Connection conn,
                    org.apache.commons.dbutils.ResultSetHandler<T> rsh,
                    String sql,
                    Object... params)
Executes the given SELECT SQL query and returns a result object.

Type Parameters:
T - The type of object that the handler returns
Parameters:
sql - the SQL
rsh - The handler used to create the result object from the ResultSet.
params - the parameters if have
Returns:
An object generated by the handler.

nQueryForObject

public <T> T nQueryForObject(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 , DbProRuntimeException may be threw if some SQL operation Exception happen.

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

nQueryForString

public String nQueryForString(Connection conn,
                              String sql,
                              Object... params)
Execute query and force return a String object, no need catch SQLException.


nQueryForLongValue

public long nQueryForLongValue(Connection conn,
                               String sql,
                               Object... params)
Execute query and force return a Long object, no need catch SQLException, runtime exception may throw if result can not be cast to long.


nQueryForMapList

public List<Map<String,Object>> nQueryForMapList(Connection conn,
                                                 String sql,
                                                 Object... params)
Execute query and force return a List> type result, no need catch SQLException.


nUpdate

public int nUpdate(Connection conn,
                   String sql,
                   Object... params)
Executes the given INSERT, UPDATE, or DELETE SQL statement.

Parameters:
sql - the SQL
params - the parameters if have
Returns:
The number of rows updated.

nInsert

public <T> T nInsert(Connection conn,
                     org.apache.commons.dbutils.ResultSetHandler<T> rsh,
                     String sql,
                     Object... params)
Executes the given INSERT SQL statement. Note: This method does not close connection.

Type Parameters:
T - The type of object that the handler returns
Parameters:
rsh - The resultSetHandler used to create the result object from the ResultSet of auto-generated keys.
sql - the SQL
params - the parameters if have
Returns:
An object generated by the handler.

nExecute

public int nExecute(Connection conn,
                    String sql,
                    Object... params)
Execute an statement, including a stored procedure call, which does not return any result sets. Any parameters which are instances of OutParameter will be registered as OUT parameters.

Use this method when invoking a stored procedure with OUT parameters that does not return any result sets.

Parameters:
sql - the SQL
Returns:
The number of rows updated.

nExecute

public <T> List<T> nExecute(Connection conn,
                            org.apache.commons.dbutils.ResultSetHandler<T> rsh,
                            String sql,
                            Object... params)
Execute an statement, including a stored procedure call, which returns one or more result sets. Any parameters which are instances of OutParameter will be registered as OUT parameters. Note: This method does not close connection. Use this method when: a) running SQL statements that return multiple result sets; b) invoking a stored procedure that return result sets and OUT parameters.

Type Parameters:
T - The type of object that the handler returns
Parameters:
rsh - The result set handler
sql - the SQL
Returns:
A list of objects generated by the handler

nQuery

public <T> T nQuery(org.apache.commons.dbutils.ResultSetHandler<T> rsh,
                    String sql,
                    Object... params)
Executes the given SELECT SQL query and returns a result object.

Type Parameters:
T - The type of object that the handler returns
Parameters:
sql - the SQL
rsh - The resultSetHandler used to create the result object from the ResultSet.
params - the parameters if have
Returns:
An object generated by the handler.

nQueryForObject

public <T> T nQueryForObject(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 , DbProRuntimeException may be threw if some SQL operation Exception happen.

Specified by:
nQueryForObject in interface NormalJdbcTool
Parameters:
sql -
params -
Returns:
An Object or null, Object type determined by SQL content

nQueryForString

public String nQueryForString(String sql,
                              Object... params)
Execute query and force return a String object, no need catch SQLException


nQueryForLongValue

public long nQueryForLongValue(String sql,
                               Object... params)
Execute query and force return a Long object, no need catch SQLException, runtime exception may throw if result can not be cast to long


nQueryForMapList

public List<Map<String,Object>> nQueryForMapList(String sql,
                                                 Object... params)
Execute query and force return a List> type result, no need catch SQLException


nUpdate

public int nUpdate(String sql,
                   Object... params)
Executes the given INSERT, UPDATE, or DELETE SQL statement.

Specified by:
nUpdate in interface NormalJdbcTool
Parameters:
sql - the SQL
params - the parameters if have
Returns:
The number of rows updated.

nInsert

public <T> T nInsert(org.apache.commons.dbutils.ResultSetHandler rsh,
                     String sql,
                     Object... params)
Executes the given INSERT SQL statement.

Type Parameters:
T - The type of object that the handler returns
Parameters:
rsh - The resultSetHandler used to create the result object from the ResultSet of auto-generated keys.
sql - the SQL
params - the parameters if have
Returns:
An object generated by the handler.

nExecute

public int nExecute(String sql,
                    Object... params)
Execute an statement, including a stored procedure call, which does not return any result sets. Any parameters which are instances of OutParameter will be registered as OUT parameters.

Use this method when invoking a stored procedure with OUT parameters that does not return any result sets.

Specified by:
nExecute in interface NormalJdbcTool
Parameters:
sql - the SQL
Returns:
The number of rows updated.

nExecute

public <T> List<T> nExecute(org.apache.commons.dbutils.ResultSetHandler rsh,
                            String sql,
                            Object... params)
Execute an statement, including a stored procedure call, which returns one or more result sets. Any parameters which are instances of OutParameter will be registered as OUT parameters. Use this method when: a) running SQL statements that return multiple result sets; b) invoking a stored procedure that return result sets and OUT parameters.

Type Parameters:
T - The type of object that the handler returns
Parameters:
rsh - The result set handler
sql - the SQL
Returns:
A list of objects generated by the handler


Copyright © 2018. All rights reserved.