public class MockQueryDslAccessor extends Object implements QueryDslAccessor
Constructor and Description |
---|
MockQueryDslAccessor() |
Modifier and Type | Method and Description |
---|---|
void |
assertAllExpectedStatementsWereRun() |
void |
execute(SqlCallback callback)
Executes SQL statements as defined in the callback function.
|
<T> T |
executeQuery(QueryCallback<T> callback)
Executes SQL statements as defined in the callback function and returns the results.
|
DuckTypeConnection |
getConnection() |
void |
setDefaultQueryResult(Iterable<ResultRow> defaultQueryResult)
Set default answer for queries that do not have answer defined by
setQueryResults(String, Iterable) or
setQueryEphemeralResults(String, Iterable, Iterable) . |
void |
setDefaultUpdateResult(int defaultUpdateResult)
Set default answer for update queries that do not have answer defined by
setUpdateResults(String, int) . |
void |
setQueryEphemeralResults(String sql,
Iterable<ResultRow> firstResults,
Iterable<ResultRow> laterResults)
Configures the accessor to return `firstResults` the first time `executeQuery(sql)` is called, and `laterResults`
on all subsequent queries.
|
void |
setQueryResults(String sql,
Iterable<ResultRow> expectedResults) |
void |
setUpdateResults(String sql,
int rowCount) |
ConnectionProvider |
withNewConnection()
Get a connection provider that will always borrow a new DB connection from the connection pool.
|
public <T> T executeQuery(@Nonnull QueryCallback<T> callback)
QueryDslAccessor
This method is mostly useful for running SELECT statements and returning the given results in some form.
This method will attempt to run the callback within an existing OfBiz transaction if one is running within this thread. If not, it will borrow a new connection, start a transaction and manage that transaction for you. That is, the connection will always be in autocommit=false, and the commit or rollback will be completely managed for you. If you want to rollback the transaction then throw a RuntimeException. The transaction will be marked as "rollback required" such that the code that started the transaction will eventually call rollback on the Connection and the exception will be propagated. If the callback returns normally, then this indicates that the underlying transaction is allowed to be committed at the appropriate time.
Because the connection (borrowing and returning from the pool) and its transaction are managed for you,
then specific Connection
methods are illegal to call and will cause a RuntimeException including:
Connection.setAutoCommit(boolean)
autocommit is always falseConnection.commit()
Connection.rollback()
Connection.close()
Example Usage:
WorklogDTO worklogDTO = queryDslAccessor.executeQuery( dbConnection -> dbConnection.newSqlQuery() .from(WORKLOG) .where(WORKLOG.id.eq(worklog.getId())) .singleResult(WORKLOG));
executeQuery
in interface QueryDslAccessor
T
- type of resultscallback
- the callback function that runs the queryQueryDslAccessor.execute(SqlCallback)
,
QueryDslAccessor.withNewConnection()
public void execute(@Nonnull SqlCallback callback)
QueryDslAccessor
This method does not return results and is mostly useful for running INSERT, UPDATE, and DELETE operations.
This method will attempt to run the callback within an existing OfBiz transaction if one is running within this thread. If not, it will borrow a new connection, start a transaction and manage that transaction for you. That is, the connection will always be in autocommit=false, and the commit or rollback will be completely managed for you. If you want to rollback the transaction then throw a RuntimeException. The transaction will be marked as "rollback required" such that the code that started the transaction will eventually call rollback on the Connection and the exception will be propagated. If the callback returns normally, then this indicates that the underlying transaction is allowed to be committed at the appropriate time.
Because the connection (borrowing and returning from the pool) and its transaction are managed for you,
then specific Connection
methods are illegal to call and will cause a RuntimeException including:
Connection.setAutoCommit(boolean)
autocommit is always falseConnection.commit()
Connection.rollback()
Connection.close()
Example Usage:
queryDslAccessor.execute(dbConnection -> { dbConnection.update(QIssueLink.ISSUE_LINK) .set(QIssueLink.ISSUE_LINK.sequence, newSeq) .where(QIssueLink.ISSUE_LINK.id.eq(issueLinkId)) .execute(); });
execute
in interface QueryDslAccessor
callback
- the callback function that runs the queryQueryDslAccessor.executeQuery(QueryCallback)
,
QueryDslAccessor.withNewConnection()
public ConnectionProvider withNewConnection()
QueryDslAccessor
Important:
This method will borrow a new connection from the pool, pass it to the callback function and then return it to
the pool after the callback has completed.
Even if OfBiz is currently running in a ThreadLocal transaction, this will retrieve a fresh connection from
the pool.
If you want to run in an existing OfBiz transaction then see instead QueryDslAccessor.executeQuery(QueryCallback)
or
QueryDslAccessor.execute(SqlCallback)
The connection will have the default auto-commit value as defined by the JIRA connection pool.
As at JIRA 7.0 this means autocommit == true.
(See org.apache.commons.dbcp.PoolableConnectionFactory#activateObject(Object)
for details.)
autocommit(false)
and then commit()
to use a transaction.rollback()
explicitly, or throw a RuntimeException.Example Usage:
queryDslAccessor.withNewConnection().execute(dbConnection -> { dbConnection.update(QIssueLink.ISSUE_LINK) .set(QIssueLink.ISSUE_LINK.sequence, newSeq) .where(QIssueLink.ISSUE_LINK.id.eq(issueLinkId)) .execute(); });
withNewConnection
in interface QueryDslAccessor
QueryDslAccessor.executeQuery(QueryCallback)
,
QueryDslAccessor.execute(SqlCallback)
@Nonnull public DuckTypeConnection getConnection()
public void setQueryEphemeralResults(String sql, Iterable<ResultRow> firstResults, Iterable<ResultRow> laterResults)
public void setUpdateResults(String sql, int rowCount)
public void assertAllExpectedStatementsWereRun()
public void setDefaultQueryResult(@Nonnull Iterable<ResultRow> defaultQueryResult)
setQueryResults(String, Iterable)
or
setQueryEphemeralResults(String, Iterable, Iterable)
. If no default answer is set querying DB with
query that doesn't have answer defined by setQueryResult()
will result in AssertionError
defaultQueryResult
- result for queries that do not have result defined explicitlypublic void setDefaultUpdateResult(int defaultUpdateResult)
setUpdateResults(String, int)
. If no default answer is set querying DB with query
that doesn't have answer defined by setUpdateResults()
will result in AssertionError
defaultUpdateResult
- result for queries that do not have result defined explicitlyCopyright © 2002-2016 Atlassian. All Rights Reserved.