@NotThreadSafe public class DBExecutor extends Object implements Serializable
| Modifier and Type | Class and Description |
|---|---|
static class |
DBExecutor.CountAndKey |
| Modifier and Type | Field and Description |
|---|---|
static boolean |
DEFAULT_DEBUG_CONNECTIONS |
static boolean |
DEFAULT_DEBUG_SQL_STATEMENTS |
static boolean |
DEFAULT_DEBUG_TRANSACTIONS |
static long |
DEFAULT_EXECUTION_DURATION_WARN_MS |
| Constructor and Description |
|---|
DBExecutor(IHasConnection aConnectionProvider) |
DBExecutor(IHasDataSource aDataSourceProvider) |
| Modifier and Type | Method and Description |
|---|---|
protected static void |
debugLog(String sMessage)
Debug logging method.
|
com.helger.commons.callback.CallbackList<com.helger.commons.callback.exception.IExceptionCallback<? super Exception>> |
exceptionCallbacks() |
com.helger.commons.state.ESuccess |
executePreparedStatement(String sSQL,
IPreparedStatementDataProvider aPSDP) |
com.helger.commons.state.ESuccess |
executePreparedStatement(String sSQL,
IPreparedStatementDataProvider aPSDP,
IUpdatedRowCountCallback aURWCC,
IGeneratedKeysCallback aGeneratedKeysCB,
com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB) |
Object |
executePreparedStatementAndGetGeneratedKey(String sSQL,
IPreparedStatementDataProvider aPSDP,
com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
Execute a prepared statement and return the generated key.
|
com.helger.commons.state.ESuccess |
executeStatement(String sSQL) |
com.helger.commons.state.ESuccess |
executeStatement(String sSQL,
IGeneratedKeysCallback aGeneratedKeysCB,
com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB) |
static com.helger.commons.callback.CallbackList<com.helger.db.api.callback.IExecutionTimeExceededCallback> |
executionTimeExceededHandlers()
Get the custom exception handler list.
|
static com.helger.commons.state.ETriState |
getConnectionEstablished() |
static IConnectionStatusChangeCallback |
getConnectionStatusChangeCallback() |
long |
getExecutionDurationWarnMS() |
protected static void |
handleGeneratedKeys(ResultSet aGeneratedKeysRS,
IGeneratedKeysCallback aGeneratedKeysCB) |
DBExecutor.CountAndKey |
insertOrUpdateAndGetGeneratedKey(String sSQL,
IPreparedStatementDataProvider aPSDP,
com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB) |
long |
insertOrUpdateOrDelete(String sSQL,
IPreparedStatementDataProvider aPSDP)
Perform an INSERT or UPDATE statement.
|
long |
insertOrUpdateOrDelete(String sSQL,
IPreparedStatementDataProvider aPSDP,
IGeneratedKeysCallback aGeneratedKeysCB,
com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
Perform an INSERT or UPDATE statement.
|
boolean |
isDebugConnections() |
boolean |
isDebugSQLStatements() |
boolean |
isDebugTransactions() |
boolean |
isExecutionDurationWarnEnabled() |
protected static long |
iterateResultSet(ResultSet aRS,
IResultSetRowCallback aCallback)
Iterate the passed result set, collect all values of a single result row,
and call the callback for each row of result objects.
|
void |
onExecutionTimeExceeded(String sMsg,
long nExecutionMillis) |
com.helger.commons.state.ESuccess |
performInTransaction(com.helger.commons.callback.IThrowingRunnable<Exception> aRunnable) |
com.helger.commons.state.ESuccess |
performInTransaction(com.helger.commons.callback.IThrowingRunnable<Exception> aRunnable,
com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB) |
com.helger.commons.collection.impl.ICommonsList<DBResultRow> |
queryAll(String sSQL)
Query a list of 0-n rows with an SQL script without parameters.
|
com.helger.commons.collection.impl.ICommonsList<DBResultRow> |
queryAll(String sSQL,
IPreparedStatementDataProvider aPSDP)
Query a list of 0-n rows with an SQL script with parameters.
|
com.helger.commons.state.ESuccess |
queryAll(String sSQL,
IPreparedStatementDataProvider aPSDP,
IResultSetRowCallback aResultItemCallback)
Perform an SQL query that does contains parameters to be filled with the
provided
IPreparedStatementDataProvider. |
com.helger.commons.state.ESuccess |
queryAll(String sSQL,
IResultSetRowCallback aResultItemCallback)
Perform an SQL query that does not contain any parameters.
|
long |
queryCount(String sSQL) |
long |
queryCount(String sSQL,
IPreparedStatementDataProvider aPSDP) |
com.helger.commons.state.ESuccess |
querySingle(String sSQL,
Consumer<? super DBResultRow> aConsumer)
Query a a single result row with an SQL script without parameters.
|
com.helger.commons.state.ESuccess |
querySingle(String sSQL,
IPreparedStatementDataProvider aPSDP,
Consumer<? super DBResultRow> aConsumer)
Query a a single result row with an SQL script with parameters.
|
static void |
resetConnectionEstablished()
Reset the "Connection established" flag.
|
static void |
setConnectionEstablished(com.helger.commons.state.ETriState eNewState)
Set the "Connection established" state.
|
static void |
setConnectionStatusChangeCallback(IConnectionStatusChangeCallback aCB)
Set the callback to be invoked, if the connection status changes.
|
DBExecutor |
setDebugConnections(boolean bDebugConnections) |
DBExecutor |
setDebugSQLStatements(boolean bDebugSQLStatements) |
DBExecutor |
setDebugTransactions(boolean bDebugTransactions) |
DBExecutor |
setExecutionDurationWarnMS(long nExecutionDurationWarnMS) |
String |
toString() |
protected com.helger.commons.state.ESuccess |
withExistingConnectionDo(Connection aConnection,
com.helger.db.jdbc.executor.DBExecutor.IWithConnectionCallback aCB,
com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB) |
protected com.helger.commons.state.ESuccess |
withNewConnectionDo(com.helger.db.jdbc.executor.DBExecutor.IWithConnectionCallback aCB,
com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB) |
protected com.helger.commons.state.ESuccess |
withPreparedStatementDo(String sSQL,
IPreparedStatementDataProvider aPSDP,
com.helger.db.jdbc.executor.DBExecutor.IWithPreparedStatementCallback aPSCallback,
IUpdatedRowCountCallback aUpdatedRowCountCB,
IGeneratedKeysCallback aGeneratedKeysCB,
com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB) |
protected com.helger.commons.state.ESuccess |
withStatementDo(com.helger.db.jdbc.executor.DBExecutor.IWithStatementCallback aCB,
IGeneratedKeysCallback aGeneratedKeysCB,
com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB) |
protected void |
withTimingDo(String sDescription,
com.helger.commons.callback.IThrowingRunnable<SQLException> aRunnable) |
public static final long DEFAULT_EXECUTION_DURATION_WARN_MS
public static final boolean DEFAULT_DEBUG_CONNECTIONS
public static final boolean DEFAULT_DEBUG_TRANSACTIONS
public static final boolean DEFAULT_DEBUG_SQL_STATEMENTS
public DBExecutor(@Nonnull IHasDataSource aDataSourceProvider)
public DBExecutor(@Nonnull IHasConnection aConnectionProvider)
protected static final void debugLog(@Nonnull String sMessage)
sMessage - The message to log. May not be null.@Nonnull public static final com.helger.commons.state.ETriState getConnectionEstablished()
null.public static final void setConnectionEstablished(@Nonnull com.helger.commons.state.ETriState eNewState)
eNewState - The new state. May not be null.public static final void resetConnectionEstablished()
setConnectionEstablished (ETriState.UNDEFINED).@Nullable public static final IConnectionStatusChangeCallback getConnectionStatusChangeCallback()
null.public static final void setConnectionStatusChangeCallback(@Nullable IConnectionStatusChangeCallback aCB)
aCB - The callback to be invoked. May be null.@Nonnull @ReturnsMutableObject public final com.helger.commons.callback.CallbackList<com.helger.commons.callback.exception.IExceptionCallback<? super Exception>> exceptionCallbacks()
@CheckForSigned public final long getExecutionDurationWarnMS()
public final boolean isExecutionDurationWarnEnabled()
@Nonnull public final DBExecutor setExecutionDurationWarnMS(long nExecutionDurationWarnMS)
@Nonnull @ReturnsMutableObject public static final com.helger.commons.callback.CallbackList<com.helger.db.api.callback.IExecutionTimeExceededCallback> executionTimeExceededHandlers()
null.public final void onExecutionTimeExceeded(@Nonnull String sMsg, @Nonnegative long nExecutionMillis)
public final boolean isDebugConnections()
@Nonnull public final DBExecutor setDebugConnections(boolean bDebugConnections)
public final boolean isDebugTransactions()
@Nonnull public final DBExecutor setDebugTransactions(boolean bDebugTransactions)
public final boolean isDebugSQLStatements()
@Nonnull public final DBExecutor setDebugSQLStatements(boolean bDebugSQLStatements)
@CodingStyleguideUnaware(value="Needs to be synchronized!") @Nonnull protected final com.helger.commons.state.ESuccess withNewConnectionDo(@Nonnull com.helger.db.jdbc.executor.DBExecutor.IWithConnectionCallback aCB, @Nullable com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
@Nonnull protected final com.helger.commons.state.ESuccess withExistingConnectionDo(@Nonnull Connection aConnection, @Nonnull com.helger.db.jdbc.executor.DBExecutor.IWithConnectionCallback aCB, @Nullable com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
protected static void handleGeneratedKeys(@Nonnull ResultSet aGeneratedKeysRS, @Nonnull IGeneratedKeysCallback aGeneratedKeysCB) throws SQLException
SQLException@Nonnull public final com.helger.commons.state.ESuccess performInTransaction(@Nonnull com.helger.commons.callback.IThrowingRunnable<Exception> aRunnable)
@Nonnull public final com.helger.commons.state.ESuccess performInTransaction(@Nonnull com.helger.commons.callback.IThrowingRunnable<Exception> aRunnable, @Nullable com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
@Nonnull protected final com.helger.commons.state.ESuccess withStatementDo(@Nonnull com.helger.db.jdbc.executor.DBExecutor.IWithStatementCallback aCB, @Nullable IGeneratedKeysCallback aGeneratedKeysCB, @Nullable com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
protected final void withTimingDo(@Nonnull String sDescription, @Nonnull com.helger.commons.callback.IThrowingRunnable<SQLException> aRunnable) throws SQLException
SQLException@Nonnull protected final com.helger.commons.state.ESuccess withPreparedStatementDo(@Nonnull String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP, @Nonnull com.helger.db.jdbc.executor.DBExecutor.IWithPreparedStatementCallback aPSCallback, @Nullable IUpdatedRowCountCallback aUpdatedRowCountCB, @Nullable IGeneratedKeysCallback aGeneratedKeysCB, @Nullable com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
@Nonnull public com.helger.commons.state.ESuccess executeStatement(@Nonnull String sSQL)
@Nonnull public com.helger.commons.state.ESuccess executeStatement(@Nonnull String sSQL, @Nullable IGeneratedKeysCallback aGeneratedKeysCB, @Nullable com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
@Nonnull public com.helger.commons.state.ESuccess executePreparedStatement(@Nonnull String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP)
@Nonnull public com.helger.commons.state.ESuccess executePreparedStatement(@Nonnull String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP, @Nullable IUpdatedRowCountCallback aURWCC, @Nullable IGeneratedKeysCallback aGeneratedKeysCB, @Nullable com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
@Nonnull public Object executePreparedStatementAndGetGeneratedKey(@Nonnull String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP, @Nullable com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
sSQL - The SQL to execute. May not be null.aPSDP - The data provider for the prepared statement. May not be
null.aExtraExCB - An additional exception callback for this execution only.null if the execution failed (see exception handler)
and no key was created, or a non-null key.public long insertOrUpdateOrDelete(@Nonnull String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP)
sSQL - SQL to execute.aPSDP - The prepared statement provider.public long insertOrUpdateOrDelete(@Nonnull String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP, @Nullable IGeneratedKeysCallback aGeneratedKeysCB, @Nullable com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
sSQL - SQL to execute.aPSDP - The prepared statement provider.aGeneratedKeysCB - An optional callback to retrieve eventually generated values. May be
null.aExtraExCB - Per-call Exception callback. May be null.@Nonnull public DBExecutor.CountAndKey insertOrUpdateAndGetGeneratedKey(@Nonnull String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP, @Nullable com.helger.commons.callback.exception.IExceptionCallback<? super Exception> aExtraExCB)
@Nonnegative protected static final long iterateResultSet(@WillClose ResultSet aRS, @Nonnull IResultSetRowCallback aCallback) throws SQLException
aRS - The result set to iterate.aCallback - The callback to be invoked for each row.SQLException - on error@Nonnull public com.helger.commons.state.ESuccess queryAll(@Nonnull @Nonempty String sSQL, @Nonnull IResultSetRowCallback aResultItemCallback)
sSQL - The SQL to query. May neither be null nor empty.aResultItemCallback - The result item callback to be invoked. May not be
null.ESuccess and never null.@Nonnull public com.helger.commons.state.ESuccess queryAll(@Nonnull String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP, @Nonnull IResultSetRowCallback aResultItemCallback)
IPreparedStatementDataProvider.sSQL - The SQL to query. May neither be null nor empty.aPSDP - The data provider for the SQL statement. May not be
null.aResultItemCallback - The result item callback to be invoked. May not be
null.ESuccess and never null.@Nullable public com.helger.commons.collection.impl.ICommonsList<DBResultRow> queryAll(@Nonnull @Nonempty String sSQL)
sSQL - The SQL to query. May neither be null nor empty.null in case of error (see the provided exception
handler) or a non-null but maybe empty list if
querying was successful.@Nullable public com.helger.commons.collection.impl.ICommonsList<DBResultRow> queryAll(@Nonnull @Nonempty String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP)
sSQL - The SQL to query. May neither be null nor empty.aPSDP - The data provider for the SQL statement. May not be
null.null in case of error (see the provided exception
handler) or a non-null but maybe empty list if
querying was successful.@Nonnull public com.helger.commons.state.ESuccess querySingle(@Nonnull @Nonempty String sSQL, @Nonnull Consumer<? super DBResultRow> aConsumer)
sSQL - The SQL to query. May neither be null nor empty.aConsumer - The consumer to be invoked with the result row. May not be
null. This is necessary to differentiate a
null-result was a "not found" or "a DB error". The
consumer is only invoked if no exception occurred.ESuccess and never null.@Nonnull public com.helger.commons.state.ESuccess querySingle(@Nonnull @Nonempty String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP, @Nonnull Consumer<? super DBResultRow> aConsumer)
sSQL - The SQL to query. May neither be null nor empty.aPSDP - The data provider for the SQL statement. May not be
null.aConsumer - The consumer to be invoked with the result row. May not be
null. This is necessary to differentiate a
null-result was a "not found" or "a DB error". The
consumer is only invoked if no exception occurred.ESuccess and never null.@CheckForSigned public long queryCount(@Nonnull String sSQL)
@CheckForSigned public long queryCount(@Nonnull String sSQL, @Nonnull IPreparedStatementDataProvider aPSDP)
Copyright © 2014–2021 Philip Helger. All rights reserved.