Class MockQueryDslAccessor

java.lang.Object
com.atlassian.jira.database.MockQueryDslAccessor
All Implemented Interfaces:
QueryDslAccessor

public class MockQueryDslAccessor extends Object implements QueryDslAccessor
Since:
v7.0.6
  • Constructor Details

    • MockQueryDslAccessor

      public MockQueryDslAccessor()
  • Method Details

    • executeQuery

      public <T> T executeQuery(@Nonnull QueryCallback<T> callback)
      Description copied from interface: QueryDslAccessor
      Executes SQL statements as defined in the callback function and returns the results.

      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.

      Important: This method will NOT put the new connection into the OfBiz ThreadLocal transaction if there wasn't one there already. See QueryDslAccessor.withLegacyOfBizTransaction() instead.

      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:

      Example Usage:

          WorklogDTO worklogDTO = queryDslAccessor.executeQuery(
                  dbConnection -> dbConnection.newSqlQuery()
                          .from(WORKLOG)
                          .where(WORKLOG.id.eq(worklog.getId()))
                          .singleResult(WORKLOG));
       
      Specified by:
      executeQuery in interface QueryDslAccessor
      Type Parameters:
      T - type of results
      Parameters:
      callback - the callback function that runs the query
      Returns:
      results of the callback function
      See Also:
    • execute

      public void execute(@Nonnull SqlCallback callback)
      Description copied from interface: QueryDslAccessor
      Executes SQL statements as defined in the callback function.

      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:

      Example Usage:

           queryDslAccessor.execute(dbConnection -> {
                   dbConnection.update(QIssueLink.ISSUE_LINK)
                           .set(QIssueLink.ISSUE_LINK.sequence, newSeq)
                           .where(QIssueLink.ISSUE_LINK.id.eq(issueLinkId))
                           .execute();
           });
       
      Specified by:
      execute in interface QueryDslAccessor
      Parameters:
      callback - the callback function that runs the query
      See Also:
    • withNewConnection

      public ConnectionProvider withNewConnection()
      Description copied from interface: QueryDslAccessor
      Get a connection provider that will always borrow a new DB connection from the connection pool.

      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 PoolableConnectionFactory for details.)

      • You can set autocommit(false) and then commit() to use a transaction.
      • To cause a rollback you can either call rollback() explicitly, or throw a RuntimeException.
      Note that this is very different to the behaviour of the default methods in this accessor where the transaction is completely managed for you.

      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();
           });
       
      Specified by:
      withNewConnection in interface QueryDslAccessor
      Returns:
      a provider that allows you to run QueryDsl queries against a freshly borrowed DB connection.
      See Also:
    • withLegacyOfBizTransaction

      public ConnectionProvider withLegacyOfBizTransaction()
      Description copied from interface: QueryDslAccessor
      Get a connection provider that will ensure that there is a Connection in OfBiz's Transaction ThreadLocal and then use that.

      This method is mostly useful if you want to call a mixture of OfBiz and Querydsl methods within your callback and ensure that the OfBiz code participates in the same transaction as the Querydsl code. In other words, its a handy workaround for dealing with legacy code, but the preferred long-term solution would usually be to convert the OfBiz code to Querydsl.

      If there is already an OfBiz ThreadLocal transaction in play, then this will be passed to the callback function. If not, then a new OfBiz ThreadLocal transaction is started and it's connection is passed to the callback function. This method will attempt to run the callback within an existing OfBiz transaction if one is running within this thread. If not, it will start a new OfBiz ThreadLocal transaction which will be committed or rolled back once the callback terminates.

      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, some specific Connection methods are illegal to call and will cause a RuntimeException including:

      Example Usage:

        queryDslAccessor.withLegacyOfBizTransaction().execute(dbConnection -> {
            doQueryDslStuffWith(dbConnection);
            callLegacyCodeThatUsesOfBiz();
        });
       
      Specified by:
      withLegacyOfBizTransaction in interface QueryDslAccessor
      Returns:
      a provider that allows you to run QueryDsl queries and also have Ofbiz participate in the same Transaction.
      See Also:
    • withDbConnection

      public DbConnection withDbConnection(Connection connection)
      Description copied from interface: QueryDslAccessor
      Wrap an existing connection in a DbConnection to allow the use of QueryDsl against it.
      Specified by:
      withDbConnection in interface QueryDslAccessor
      Parameters:
      connection - an already established connection.
      Returns:
      a db connection that allows you to run QueryDsl queries against a predefined DB connection.
    • getConnection

      @Nonnull public DuckTypeConnection getConnection()
    • setQueryResults

      public void setQueryResults(String sql, Iterable<ResultRow> expectedResults)
    • setQueryResults

      public void setQueryResults(String sql, ResultRow expectedResult)
    • setUpdateResults

      public void setUpdateResults(String sql, Supplier<RuntimeException> exFactory)
    • reset

      public void reset()
    • onSqlListener

      public void onSqlListener(String sql, Runnable action)
      Require MockQueryDslAccessor to invoke some action, when specified sql is being executed.
      Parameters:
      sql - SQL query we listen on
      action - Action to be invoked when the sql is run
    • setQueryEphemeralResults

      public 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.
    • setUpdateResults

      public void setUpdateResults(String sql, int rowCount)
    • getRanQueries

      public Set<String> getRanQueries()
    • assertAllExpectedStatementsWereRun

      public void assertAllExpectedStatementsWereRun()
    • getMockDelegatorInterface

      public org.ofbiz.core.entity.DelegatorInterface getMockDelegatorInterface()
    • setDefaultQueryResult

      public void setDefaultQueryResult(@Nonnull Iterable<ResultRow> defaultQueryResult)
      Set default answer for queries that do not have answer defined by 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
      Parameters:
      defaultQueryResult - result for queries that do not have result defined explicitly
    • setDefaultUpdateResult

      public void setDefaultUpdateResult(int defaultUpdateResult)
      Set default answer for update queries that do not have answer defined by 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
      Parameters:
      defaultUpdateResult - result for queries that do not have result defined explicitly
    • toString

      public String toString()
      Overrides:
      toString in class Object