public class DBUtils extends Object implements DBContextAware
Modifier and Type | Field and Description |
---|---|
protected DBContext |
context |
protected DBMSHandler |
dbms |
protected int |
DEFAULT_LIST_CAPACITY |
protected static org.slf4j.Logger |
log |
protected int |
LOG_MAX_STRING_LENGTH |
protected String |
LOG_NEW_LINE |
protected long |
longRunndingStmtThreshold |
protected int |
MAX_QUERY_ROWS |
Constructor and Description |
---|
DBUtils(DBContext context)
DBUtils constructor
|
Modifier and Type | Method and Description |
---|---|
protected <T> DBBeanListFactory<T> |
createDefaultBeanListFactory(Class<T> beanType,
Column[] keyColumns,
List<? extends DBColumnExpr> selectColumns)
Crates a default DBBeanListFactory for Java bean class
The DBRecord class must provide
either a standard construtor with correspondig property set fundtions
or a constructor using the fields of the query
|
protected <T extends DataListEntry> |
createDefaultDataListFactory(Class<T> entryClass,
DataListHead head)
Crates a default DataListFactory for a DataListEntry class
The DataListEntry class must provide the following constructor
DataListEntry(DataListFactory<?
|
protected DataListHead |
createDefaultDataListHead(DBCommandExpr cmd,
Class<? extends DataListEntry> entryClass)
Crates a default DataListHead for a DataListEntry class
|
protected <R extends DBRecordBase> |
createDefaultRecordListFactory(Class<R> recordClass,
DBRowSet rowset)
Crates a default DBRecordListFactory for a DBRecord class
The DBRecord class must provide the following constructor
DBRecord(DBContext context, DBRowSet rowset)
|
ResultSet |
executeQuery(String sqlCmd,
Object[] sqlParams,
boolean scrollable)
Executes a select SQL-Statement and returns a ResultSet containing the query results.
|
int |
executeSQL(String sqlCmd,
Object[] sqlParams,
DBMSHandler.DBSetGenKeys setGenKeys)
Executes an update, insert or delete SQL-Statement.
|
<T> DBBeanListFactory<T> |
getCommandBeanListFactory(Class<T> beanType,
DBCommandExpr cmd)
gets or creates DBBeanListFactory for the given rowset
|
DBContext |
getContext()
Returns the current Context
|
<T> DBBeanListFactory<T> |
getRowsetBeanListFactory(Class<T> beanType,
DBRowSet rowset)
gets or creates DBBeanListFactory for the given rowset
|
protected void |
logQueryStatement(String sqlCmd,
Object[] sqlParams)
Log Query Statement
|
protected void |
logUpdateStatement(String sqlCmd,
Object[] sqlParams)
Log Update Statement
|
protected String |
paramsToString(Object[] params)
Get all parameters as string (for logging only)
|
protected String |
paramValueToString(Object param)
Get single parameter as string (for logging only)
|
<T> T |
queryBean(Class<T> beanType,
DBCompareExpr whereConstraints)
Queries a single bean based on a where constraint
|
<T> T |
queryBean(Class<T> beanType,
DBRowSet rowset,
DBCompareExpr whereConstraints)
Queries a single bean based on a where constraint
|
<T> T |
queryBean(Class<T> beanType,
DBRowSet rowset,
Object[] key)
Queries a single bean based on primary key values
|
<T> T |
queryBean(DBCommandExpr cmd,
Class<T> beanType)
Queries a single Java Bean for a given command
|
<T> T |
queryBean(DBCommandExpr cmd,
DBBeanListFactory<T> factory)
queries a single Java Bean for a given command
|
<T> List<T> |
queryBeanList(DBCommandExpr cmd,
Class<T> beanType,
DBRowSet rowset,
Object parent)
Queries a list of Java beans for a given command
|
<T> List<T> |
queryBeanList(DBCommandExpr cmd,
Class<T> beanType,
Object parent)
Queries a list of Java beans for a given command
|
<T> List<T> |
queryBeanList(DBCommandExpr cmd,
DBBeanListFactory<T> factory,
Object parent)
Queries a list of Java beans for a given command
|
<T> List<T> |
queryBeanList(DBCommandExpr cmd,
DBBeanListFactory<T> factory,
Object parent,
int first,
int pageSize)
Query a list of simple Java objects (beans)
|
DataListEntry |
queryDataEntry(DBCommandExpr cmd)
Queries a single DataListEntry item
|
<T extends DataListEntry> |
queryDataEntry(DBCommandExpr cmd,
Class<T> entryClass)
Queries a single DataListEntry item
|
<T extends DataListEntry> |
queryDataEntry(DBCommandExpr cmd,
Class<T> entryClass,
boolean failOnNoResult)
Queries a single DataListEntry item
|
List<DataListEntry> |
queryDataList(DBCommandExpr cmd)
Queries a list of DataListEntry items
|
<T extends DataListEntry> |
queryDataList(DBCommandExpr cmd,
Class<T> entryClass)
Queries a list of DataListEntry items
|
<T extends DataListEntry> |
queryDataList(DBCommandExpr cmd,
Class<T> entryClass,
DataListHead head)
Queries a list of DataListEntry items
|
<T extends DataListEntry> |
queryDataList(DBCommandExpr cmd,
Class<T> entryClass,
int first,
int maxItems)
Queries a list of DataListEntry items
|
<T extends DataListEntry> |
queryDataList(DBCommandExpr cmd,
DataListFactory<T> factory,
int first,
int pageSize)
Executes a query and returns a list of DataListEntry items
|
List<DataListEntry> |
queryDataList(DBCommandExpr cmd,
int first,
int maxItems)
Queries a list of DataListEntry items
|
List<Object[]> |
queryObjectList(DBCommandExpr cmd)
Returns the result of a query as a list Object-Arrays
This function should only be used for small lists.
|
int |
queryObjectList(String sqlCmd,
Object[] sqlParams,
Collection<Object[]> result,
int maxRows)
Adds the result of a query to a given collection.
|
Options |
queryOptionList(DBCommandExpr cmd)
Returns a list of key value pairs from an sql query.
|
int |
queryOptionList(DBCommandExpr cmd,
Options options)
Fills an option list provided with the result from a query.
|
int |
queryOptionList(String sqlCmd,
Object[] sqlParams,
Options options)
Fills an option list provided with the result from a query.
|
<R extends DBRecordBase> |
queryRecordList(DBCommand cmd,
DBRecordListFactory<R> factory,
int first,
int pageSize)
Executes a query and returns a list of DBRecord items
|
List<DBRecord> |
queryRecordList(DBCommand cmd,
DBRowSet rowset)
Executes a query and returns a list of DBRecord items
|
<R extends DBRecordBase> |
queryRecordList(DBCommand cmd,
DBRowSet rowset,
Class<R> recordType)
Executes a query and returns a list of DBRecord items
|
int |
queryRowCount(DBCommand cmd)
Returns the number of rows returned by executing the select statement
|
protected void |
queryRowLimitExeeded()
Called to inform that the limit for DataList, Record and Bean queries has exceeded the maximum value
|
<T> List<T> |
querySimpleList(Class<T> c,
DBCommandExpr cmd)
Returns a one dimensional array from an sql query.
|
<T> int |
querySimpleList(Class<T> c,
DBCommandExpr cmd,
Collection<T> result)
Adds the first column of a query result to a collection.
|
<T> int |
querySimpleList(Class<T> c,
String sqlCmd,
Object[] sqlParams,
DataType dataType,
Collection<T> result,
int maxRows)
Adds the first column of a query result to a collection.
|
List<Object> |
querySimpleList(DBCommandExpr cmd)
Returns a one dimensional array from an sql query.
|
int |
querySingleInt(DBCommandExpr cmd)
Returns the value of the first row/column of a sql-query as an int.
|
int |
querySingleInt(DBCommandExpr cmd,
int defaultValue)
Returns the value of the first row/column of a sql-query as an int.
|
int |
querySingleInt(String sqlCmd,
Object[] sqlParams,
int defaultValue)
Returns the value of the first row/column of a sql-query as an int.
|
long |
querySingleLong(DBCommandExpr cmd)
Returns the value of the first row/column of a sql-query as a long.
|
long |
querySingleLong(DBCommandExpr cmd,
long defaultValue)
Returns the value of the first row/column of a sql-query as a long.
|
long |
querySingleLong(String sqlCmd,
Object[] sqlParams,
long defaultValue)
Returns the value of the first row/column of a sql-query as an int.
|
Object[] |
querySingleRow(DBCommandExpr cmd)
Returns all values of the first row of a sql-query as an array.
|
Object[] |
querySingleRow(String sqlCmd,
Object[] sqlParams)
Returns all values of the first row of a sql-query as an array.
|
String |
querySingleString(DBCommandExpr cmd)
Returns the value of the first row/column of a sql-query as a string.
|
String |
querySingleString(DBCommandExpr cmd,
String defaultValue)
Returns the value of the first row/column of a sql-query as a string.
|
Object |
querySingleValue(DBCommandExpr cmd)
Returns the value of the first row/column of a sql-query as an object.
|
Object |
querySingleValue(DBCommandExpr cmd,
boolean failOnNoResult)
Returns the value of the first row/column of a sql-query as an object.
|
<T> T |
querySingleValue(DBCommandExpr cmd,
Class<T> resultType,
boolean failOnNoResult)
Returns the value of the first row/column of a sql-query as an object.
|
Object |
querySingleValue(String sqlCmd,
Object[] sqlParams,
DataType dataType,
boolean failOnNoResult)
Returns the value of the first row/column of a sql-query as an object.
|
protected static final org.slf4j.Logger log
protected long longRunndingStmtThreshold
protected int DEFAULT_LIST_CAPACITY
protected int MAX_QUERY_ROWS
protected int LOG_MAX_STRING_LENGTH
protected String LOG_NEW_LINE
protected final DBContext context
protected final DBMSHandler dbms
public DBUtils(DBContext context)
context
- the database contextpublic DBContext getContext()
getContext
in interface DBContextAware
protected String paramValueToString(Object param)
param
- the parameterprotected String paramsToString(Object[] params)
params
- the parameterprotected void logQueryStatement(String sqlCmd, Object[] sqlParams)
sqlCmd
- the sql commandsqlParams
- the command paramsprotected void logUpdateStatement(String sqlCmd, Object[] sqlParams)
sqlCmd
- the sql commandsqlParams
- the command paramspublic int executeSQL(String sqlCmd, Object[] sqlParams, DBMSHandler.DBSetGenKeys setGenKeys)
sqlCmd
- the SQL-CommandsqlParams
- a list of objects to replace sql parameterssetGenKeys
- callback to set the generated key for a each new recordpublic ResultSet executeQuery(String sqlCmd, Object[] sqlParams, boolean scrollable)
sqlCmd
- the SQL-CommandsqlParams
- a list of parameters for parameter queries (may depend on dbms)scrollable
- true if the reader should be scrollable or false if notpublic Object querySingleValue(String sqlCmd, Object[] sqlParams, DataType dataType, boolean failOnNoResult)
sqlCmd
- the SQL-CommandsqlParams
- list of query parameter valuesdataType
- the expected data typefailOnNoResult
- if true a QueryNoResultException result is thrown if no record exists otherwise null is returnedpublic final <T> T querySingleValue(DBCommandExpr cmd, Class<T> resultType, boolean failOnNoResult)
cmd
- the Command object that contains the select statementresultType
- the expected data typefailOnNoResult
- flag whether to fail on empty resultsetpublic final Object querySingleValue(DBCommandExpr cmd, boolean failOnNoResult)
cmd
- the Command object that contains the select statementfailOnNoResult
- flag whether to fail on empty resultsetpublic final Object querySingleValue(DBCommandExpr cmd)
cmd
- the Command object that contains the select statementpublic final int querySingleInt(String sqlCmd, Object[] sqlParams, int defaultValue)
sqlCmd
- the sql commandsqlParams
- the command paramsdefaultValue
- the default valuepublic final int querySingleInt(DBCommandExpr cmd, int defaultValue)
cmd
- the Command object that contains the select statementdefaultValue
- the default value if no value was returned by the databasepublic final int querySingleInt(DBCommandExpr cmd)
cmd
- the Command object that contains the select statementpublic final long querySingleLong(String sqlCmd, Object[] sqlParams, long defaultValue)
sqlCmd
- the sql commandsqlParams
- the command paramsdefaultValue
- the default valuepublic final long querySingleLong(DBCommandExpr cmd, long defaultValue)
cmd
- the Command object that contains the select statementdefaultValue
- the default valuepublic final long querySingleLong(DBCommandExpr cmd)
cmd
- the Command object that contains the select statementpublic final String querySingleString(DBCommandExpr cmd, String defaultValue)
cmd
- the Command object that contains the select statementdefaultValue
- the default value if no value was returned by the databasepublic final String querySingleString(DBCommandExpr cmd)
cmd
- the Command object that contains the select statementpublic int queryRowCount(DBCommand cmd)
cmd
- the select commandpublic <T> int querySimpleList(Class<T> c, String sqlCmd, Object[] sqlParams, DataType dataType, Collection<T> result, int maxRows)
T
- the type for the listc
- the class type for the listsqlCmd
- the sql commandsqlParams
- the command paramsdataType
- the expected data typeresult
- the reusult colletionmaxRows
- maximum number of rows or -1 for all rowspublic final <T> int querySimpleList(Class<T> c, DBCommandExpr cmd, Collection<T> result)
T
- the type for the listc
- the class type for the listcmd
- the Command object that contains the select statementpublic final <T> List<T> querySimpleList(Class<T> c, DBCommandExpr cmd)
T
- the type for the listc
- the class type for the listcmd
- the Command object that contains the select statementpublic final List<Object> querySimpleList(DBCommandExpr cmd)
cmd
- the Command object that contains the select statementpublic int queryOptionList(String sqlCmd, Object[] sqlParams, Options options)
sqlCmd
- the sql commandsqlParams
- the command paramsoptions
- the option list to where the options are addedpublic final int queryOptionList(DBCommandExpr cmd, Options options)
cmd
- the Command object that contains the select statementoptions
- the option list to where the options are addedpublic final Options queryOptionList(DBCommandExpr cmd)
cmd
- the Command object that contains the select statementpublic int queryObjectList(String sqlCmd, Object[] sqlParams, Collection<Object[]> result, int maxRows)
This function should only be used for small lists. Otherwise a DBReader should be used!
sqlCmd
- the sql commandsqlParams
- the command paramsresult
- the result collecitonmaxRows
- the maximum number of rowspublic final List<Object[]> queryObjectList(DBCommandExpr cmd)
cmd
- the Command object that contains the select statementpublic Object[] querySingleRow(String sqlCmd, Object[] sqlParams)
sqlCmd
- the SQL-CommandsqlParams
- list of query parameter valuespublic final Object[] querySingleRow(DBCommandExpr cmd)
cmd
- the Command object that contains the select statementprotected void queryRowLimitExeeded()
protected <T extends DataListEntry> DataListFactory<T> createDefaultDataListFactory(Class<T> entryClass, DataListHead head)
entryClass
- the entryClass for which to create the list headprotected DataListHead createDefaultDataListHead(DBCommandExpr cmd, Class<? extends DataListEntry> entryClass)
cmd
- the cmd for which to create the DataListHeadpublic <T extends DataListEntry> List<T> queryDataList(DBCommandExpr cmd, DataListFactory<T> factory, int first, int pageSize)
cmd
- the commandfactory
- the Factory to be used for each list itemfirst
- the number of records to skip from the beginning of the resultpageSize
- the maximum number of items to add to the list or -1 (default) for allpublic final <T extends DataListEntry> List<T> queryDataList(DBCommandExpr cmd, Class<T> entryClass, DataListHead head)
public final <T extends DataListEntry> List<T> queryDataList(DBCommandExpr cmd, Class<T> entryClass)
public final <T extends DataListEntry> List<T> queryDataList(DBCommandExpr cmd, Class<T> entryClass, int first, int maxItems)
public final List<DataListEntry> queryDataList(DBCommandExpr cmd)
public final List<DataListEntry> queryDataList(DBCommandExpr cmd, int first, int maxItems)
public final <T extends DataListEntry> T queryDataEntry(DBCommandExpr cmd, Class<T> entryClass, boolean failOnNoResult)
failOnNoResult
- flag whether to fail on empty resultsetpublic final <T extends DataListEntry> T queryDataEntry(DBCommandExpr cmd, Class<T> entryClass)
public final DataListEntry queryDataEntry(DBCommandExpr cmd)
protected <R extends DBRecordBase> DBRecordListFactory<R> createDefaultRecordListFactory(Class<R> recordClass, DBRowSet rowset)
recordClass
- the recordClass for which to create the list headpublic <R extends DBRecordBase> List<R> queryRecordList(DBCommand cmd, DBRecordListFactory<R> factory, int first, int pageSize)
cmd
- the commandfactory
- the factory for creating record objectsfirst
- the number of records to skip from the beginning of the resultpageSize
- the maximum number of items to add to the list or -1 (default) for allpublic final <R extends DBRecordBase> List<R> queryRecordList(DBCommand cmd, DBRowSet rowset, Class<R> recordType)
cmd
- the command holding the constraints and order or the queryrowset
- the rowset for which to query the recordspublic final List<DBRecord> queryRecordList(DBCommand cmd, DBRowSet rowset)
cmd
- the command holding the constraints and order or the queryrowset
- the rowset for which to query the recordsprotected <T> DBBeanListFactory<T> createDefaultBeanListFactory(Class<T> beanType, Column[] keyColumns, List<? extends DBColumnExpr> selectColumns)
beanType
- the beanType for which to create the list headkeyColumns
- the key columnsselectColumns
- the select columnspublic <T> DBBeanListFactory<T> getRowsetBeanListFactory(Class<T> beanType, DBRowSet rowset)
beanType
- the beanType for which to create the list headrowset
- the rowset for which to return the factorypublic <T> DBBeanListFactory<T> getCommandBeanListFactory(Class<T> beanType, DBCommandExpr cmd)
beanType
- the beanType for which to create the list headcmd
- the commandpublic <T> List<T> queryBeanList(DBCommandExpr cmd, DBBeanListFactory<T> factory, Object parent, int first, int pageSize)
cmd
- the commandfactory
- the bean factoryparent
- the parent object for the created beans (optional)first
- the first rowpageSize
- the maximum number of items to add to the list or -1 (default) for allpublic final <T> List<T> queryBeanList(DBCommandExpr cmd, DBBeanListFactory<T> factory, Object parent)
cmd
- the query commandfactory
- the beanType factory used to instantiate the beanparent
- (optional) the parent bean if anypublic <T> List<T> queryBeanList(DBCommandExpr cmd, Class<T> beanType, DBRowSet rowset, Object parent)
cmd
- the query commandbeanType
- the beanTyperowset
- the rowsetparent
- (optional) the parent bean if anypublic <T> List<T> queryBeanList(DBCommandExpr cmd, Class<T> beanType, Object parent)
cmd
- the query commandbeanType
- the beanTypeparent
- (optional) the parent bean if anypublic <T> T queryBean(DBCommandExpr cmd, DBBeanListFactory<T> factory)
cmd
- the query commandfactory
- the factory to create the bean instancepublic <T> T queryBean(DBCommandExpr cmd, Class<T> beanType)
cmd
- the query commandbeanType
- the beanTypepublic final <T> T queryBean(Class<T> beanType, DBRowSet rowset, DBCompareExpr whereConstraints)
beanType
- the beanTyperowset
- the rowset used for the querywhereConstraints
- the constraints for the querypublic final <T> T queryBean(Class<T> beanType, DBCompareExpr whereConstraints)
beanType
- the beanTypewhereConstraints
- the constraints for the queryCopyright © 2008–2023 Apache Software Foundation. All rights reserved.