Class Xact

java.lang.Object
org.reldb.wrapd.sqldb.Xact

public class Xact
extends java.lang.Object
Transaction wrapper that allows database query and update operations to be invoked transactionally in an ergonomic fashion.
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected java.sql.Connection connection
    Connection to database this transaction is running in.
    protected Database database
    Database upon which this transaction will run.
  • Method Summary

    Modifier and Type Method Description
    TupleTypeGenerator.GenerateResult createTupleFromQuery​(java.lang.String codeDirectory, java.lang.String packageSpec, java.lang.String tupleClassName, java.lang.String query, java.lang.Object... parms)
    Use a parametric SELECT query to generate a corresponding Tuple-derived class to represent future evaluations of the same query or similar queries.
    TupleTypeGenerator.GenerateResult createTupleFromQueryAll​(java.lang.String codeDirectory, java.lang.String packageSpec, java.lang.String tupleClassName, java.lang.String query)
    Use a SELECT query to generate a corresponding Tuple-derived class to represent future evaluations of the same query or similar queries.
    TupleTypeGenerator.GenerateResult createTupleFromQueryAllForUpdate​(java.lang.String codeDirectory, java.lang.String packageSpec, java.lang.String tupleClassName, java.lang.String tableName, java.lang.String query)
    Use a SELECT query to generate a corresponding UpdatableTuple-derived class to represent future evaluations of the same query or similar queries.
    TupleTypeGenerator.GenerateResult createTupleFromQueryForUpdate​(java.lang.String codeDirectory, java.lang.String packageSpec, java.lang.String tupleClassName, java.lang.String tableName, java.lang.String query, java.lang.Object... parms)
    Use a parametric SELECT query to generate a corresponding UpdatableTuple-derived class to represent future evaluations of the same query or similar queries.
    java.lang.String[] getKeyColumnNamesFor​(java.lang.String tableName)
    Get primary key for a given table.
    java.util.List<UpdatableTuple.FieldGetFailure> insert​(UpdatableTuple tuple, java.lang.String tableName)
    Insert specified Tuple.
    <T> Response<T> processPreparedStatement​(Database.PreparedStatementUser<T> preparedStatementUser, java.lang.String query, java.lang.Object... parms)
    Use a prepared statement.
    <T extends Tuple>
    java.util.stream.Stream<T>
    query​(java.lang.String query, java.lang.Class<T> tupleClass, java.lang.Object... parms)
    Obtain a stream of Tuple derivatives from a query evaluation.
    <T> T query​(java.lang.String query, Database.ResultSetReceiver<T> receiver, java.lang.Object... parms)
    Issue a parametric SELECT query with '?' substitutions, process it, and return the result
    <T extends Tuple>
    java.util.stream.Stream<T>
    query​(Query<T> query)
    Obtain a stream of Tuple derivatives from a query evaluation.
    <T extends Tuple>
    java.util.stream.Stream<T>
    queryAll​(java.lang.String query, java.lang.Class<T> tupleClass)
    Obtain a stream of Tuple derivatives from a query evaluation.
    <T> T queryAll​(java.lang.String query, Database.ResultSetReceiver<T> receiver)
    Issue a SELECT query, process it, and return the result
    <T extends Tuple>
    java.util.stream.Stream<T>
    queryAll​(Query<T> query)
    Obtain a stream of Tuple derivatives from a query evaluation.
    <T extends UpdatableTuple>
    java.util.stream.Stream<T>
    queryAllForUpdate​(java.lang.String query, java.lang.Class<T> tupleClass)
    Obtain a stream of UpdatableTuple derivatives from a query evaluation for possible update.
    <T extends UpdatableTuple>
    java.util.stream.Stream<T>
    queryAllForUpdate​(Query<T> query)
    Obtain a stream of Tuple derivatives from a query evaluation for possible update.
    <T extends UpdatableTuple>
    java.util.stream.Stream<T>
    queryForUpdate​(java.lang.String query, java.lang.Class<T> tupleClass, java.lang.Object... parms)
    Obtain a stream of UpdatableTuple derivatives from a query evaluation for possible update.
    <T extends UpdatableTuple>
    java.util.stream.Stream<T>
    queryForUpdate​(Query<T> query)
    Obtain a stream of UpdatableTuple derivatives from a query evaluation for possible update.
    boolean update​(java.lang.String query, java.lang.Object... parms)
    Issue a parametric update query with '?' substitutions.
    org.reldb.toolbox.types.Pair<java.util.List<UpdatableTuple.FieldGetFailure>,​java.util.List<UpdatableTuple.FieldGetFailure>> update​(UpdatableTuple tuple, java.lang.String tableName)
    Update specified tuple.
    boolean update​(Update update)
    Issue an update query.
    boolean updateAll​(java.lang.String sqlStatement)
    Issue an update query.
    boolean updateAll​(Update update)
    Issue an update query.
    <T> T usePreparedStatement​(Database.PreparedStatementUser<T> preparedStatementUser, java.lang.String query, java.lang.Object... parms)
    Use a prepared statement.
    java.util.Optional<?> valueOf​(java.lang.String query, java.lang.Object... parms)
    Issue a parametric SELECT query with '?' substitutions and obtain a value for the first row in the first column.
    java.util.Optional<?> valueOf​(Query<? extends Tuple> query)
    Issue a parametric Query with '?' substitutions and obtain a value for the first row in the first column.
    java.util.Optional<?> valueOfAll​(java.lang.String query)
    Issue a SELECT query and obtain a value for the first row in the first column.
    java.util.Optional<?> valueOfAll​(Query<? extends Tuple> query)
    Issue a Query and obtain a value for the first row in the first column.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • database

      protected final Database database
      Database upon which this transaction will run.
    • connection

      protected final java.sql.Connection connection
      Connection to database this transaction is running in.
  • Method Details

    • updateAll

      public boolean updateAll​(java.lang.String sqlStatement) throws java.sql.SQLException
      Issue an update query.
      Parameters:
      sqlStatement - String SQL query.
      Returns:
      True if a ResultSet is returned, false otherwise.
      Throws:
      java.sql.SQLException - Error.
    • valueOfAll

      public java.util.Optional<?> valueOfAll​(java.lang.String query) throws java.sql.SQLException
      Issue a SELECT query and obtain a value for the first row in the first column. Intended to obtain a single value.
      Parameters:
      query - SELECT query that returns a single column, or multiple columns but only the first is used to obtain the value.
      Returns:
      Value of first column of first row in result.
      Throws:
      java.sql.SQLException - Error.
    • valueOfAll

      public java.util.Optional<?> valueOfAll​(Query<? extends Tuple> query) throws java.sql.SQLException
      Issue a Query and obtain a value for the first row in the first column. Intended to obtain a single value.
      Parameters:
      query - Query that returns a single column, or multiple columns but only the first is used to obtain the value.
      Returns:
      Value of first column of first row in result.
      Throws:
      java.sql.SQLException - Error.
    • valueOf

      public java.util.Optional<?> valueOf​(java.lang.String query, java.lang.Object... parms) throws java.sql.SQLException
      Issue a parametric SELECT query with '?' substitutions and obtain a value for the first row in the first column. Intended to obtain a single value.
      Parameters:
      query - SELECT query that returns a single column, or multiple columns but only the first is used to obtain the value.
      parms - Parameter arguments.
      Returns:
      Value of first column of first row in result.
      Throws:
      java.sql.SQLException - Error.
    • valueOf

      public java.util.Optional<?> valueOf​(Query<? extends Tuple> query) throws java.sql.SQLException
      Issue a parametric Query with '?' substitutions and obtain a value for the first row in the first column. Intended to obtain a single value.
      Parameters:
      query - Query that returns a single column, or multiple columns but only the first is used to obtain the value.
      Returns:
      Value of first column of first row in result.
      Throws:
      java.sql.SQLException - Error.
    • queryAll

      public <T> T queryAll​(java.lang.String query, Database.ResultSetReceiver<T> receiver) throws java.sql.SQLException
      Issue a SELECT query, process it, and return the result
      Type Parameters:
      T - Return type.
      Parameters:
      query - Query text.
      receiver - ResultSet receiver lambda.
      Returns:
      Return value.
      Throws:
      java.sql.SQLException - Error.
    • queryAll

      public <T extends Tuple> java.util.stream.Stream<T> queryAll​(java.lang.String query, java.lang.Class<T> tupleClass) throws java.sql.SQLException
      Obtain a stream of Tuple derivatives from a query evaluation.
      Type Parameters:
      T - T extends Tuple.
      Parameters:
      query - Query string.
      tupleClass - Tuple derivative that represents rows in the ResultSet returned from evaluating the query
      Returns:
      Stream<T> Result stream.
      Throws:
      java.sql.SQLException - Error.
    • queryAll

      public <T extends Tuple> java.util.stream.Stream<T> queryAll​(Query<T> query) throws java.sql.SQLException
      Obtain a stream of Tuple derivatives from a query evaluation.
      Type Parameters:
      T - T extends Tuple.
      Parameters:
      query - A Query.
      Returns:
      Stream<T> Result stream.
      Throws:
      java.sql.SQLException - Error.
    • queryAllForUpdate

      public <T extends UpdatableTuple> java.util.stream.Stream<T> queryAllForUpdate​(java.lang.String query, java.lang.Class<T> tupleClass) throws java.sql.SQLException
      Obtain a stream of UpdatableTuple derivatives from a query evaluation for possible update.
      Type Parameters:
      T - T extends UpdatableTuple.
      Parameters:
      query - Query string.
      tupleClass - Tuple derivative that represents rows in the ResultSet returned from evaluating the query.
      Returns:
      Stream<T> Result stream.
      Throws:
      java.sql.SQLException - Error.
    • queryAllForUpdate

      public <T extends UpdatableTuple> java.util.stream.Stream<T> queryAllForUpdate​(Query<T> query) throws java.sql.SQLException
      Obtain a stream of Tuple derivatives from a query evaluation for possible update.
      Type Parameters:
      T - T extends UpdatableTuple.
      Parameters:
      query - A Query.
      Returns:
      Stream<T> Result stream.
      Throws:
      java.sql.SQLException - Error.
    • query

      public <T> T query​(java.lang.String query, Database.ResultSetReceiver<T> receiver, java.lang.Object... parms) throws java.sql.SQLException
      Issue a parametric SELECT query with '?' substitutions, process it, and return the result
      Type Parameters:
      T - Return type
      Parameters:
      query - SQL query.
      receiver - ResultSet receiver lambda.
      parms - Parameter arguments.
      Returns:
      Return value.
      Throws:
      java.sql.SQLException - Error.
    • query

      public <T extends Tuple> java.util.stream.Stream<T> query​(java.lang.String query, java.lang.Class<T> tupleClass, java.lang.Object... parms) throws java.sql.SQLException
      Obtain a stream of Tuple derivatives from a query evaluation.
      Type Parameters:
      T - T extends Tuple.
      Parameters:
      query - SQL query string
      tupleClass - Tuple derivative that represents rows in the ResultSet returned from evaluating the query
      parms - Parameter arguments to parametric query.
      Returns:
      Stream<T> Result stream
      Throws:
      java.sql.SQLException - Error
    • query

      public <T extends Tuple> java.util.stream.Stream<T> query​(Query<T> query) throws java.sql.SQLException
      Obtain a stream of Tuple derivatives from a query evaluation.
      Type Parameters:
      T - T extends Tuple.
      Parameters:
      query - A Query
      Returns:
      Stream<T> Result stream.
      Throws:
      java.sql.SQLException - Error.
    • queryForUpdate

      public <T extends UpdatableTuple> java.util.stream.Stream<T> queryForUpdate​(java.lang.String query, java.lang.Class<T> tupleClass, java.lang.Object... parms) throws java.sql.SQLException
      Obtain a stream of UpdatableTuple derivatives from a query evaluation for possible update.
      Type Parameters:
      T - T extends UpdatableTuple.
      Parameters:
      query - SQL query string.
      tupleClass - Tuple derivative that represents rows in the ResultSet returned from evaluating the query
      parms - Parameter arguments to parametric query.
      Returns:
      Stream<T> Result stream
      Throws:
      java.sql.SQLException - Error
    • queryForUpdate

      public <T extends UpdatableTuple> java.util.stream.Stream<T> queryForUpdate​(Query<T> query) throws java.sql.SQLException
      Obtain a stream of UpdatableTuple derivatives from a query evaluation for possible update.
      Type Parameters:
      T - T extends UpdatableTuple.
      Parameters:
      query - A Query.
      Returns:
      Stream<T> Result stream.
      Throws:
      java.sql.SQLException - Error.
    • insert

      public java.util.List<UpdatableTuple.FieldGetFailure> insert​(UpdatableTuple tuple, java.lang.String tableName) throws java.sql.SQLException
      Insert specified Tuple.
      Parameters:
      tuple - UpdatableTuple to insert.
      tableName - Table name.
      Returns:
      List of failures to retrieve one or more fields. Empty if all fields retrieved.
      Throws:
      java.sql.SQLException - Failure.
    • update

      public boolean update​(java.lang.String query, java.lang.Object... parms) throws java.sql.SQLException
      Issue a parametric update query with '?' substitutions.
      Parameters:
      query - SQL query.
      parms - Parameter arguments.
      Returns:
      True if a ResultSet is returned, false otherwise.
      Throws:
      java.sql.SQLException - Error.
    • updateAll

      public boolean updateAll​(Update update) throws java.sql.SQLException
      Issue an update query.
      Parameters:
      update - Update query specification.
      Returns:
      True if a ResultSet is returned, false otherwise.
      Throws:
      java.sql.SQLException - Error.
    • update

      public boolean update​(Update update) throws java.sql.SQLException
      Issue an update query.
      Parameters:
      update - Parametric update query specification.
      Returns:
      True if a ResultSet is returned, false otherwise.
      Throws:
      java.sql.SQLException - Error.
    • update

      public org.reldb.toolbox.types.Pair<java.util.List<UpdatableTuple.FieldGetFailure>,​java.util.List<UpdatableTuple.FieldGetFailure>> update​(UpdatableTuple tuple, java.lang.String tableName) throws java.sql.SQLException
      Update specified tuple.
      Parameters:
      tuple - UpdatableTuple to update.
      tableName - Table name.
      Returns:
      Return a pair of List<FieldGetFailure> where the left item is the new field get failures, and the right item is the original (backup) field get failures. Both lists in the Pair are empty if successful.
      Throws:
      java.sql.SQLException - Failure.
    • createTupleFromQuery

      public TupleTypeGenerator.GenerateResult createTupleFromQuery​(java.lang.String codeDirectory, java.lang.String packageSpec, java.lang.String tupleClassName, java.lang.String query, java.lang.Object... parms) throws java.sql.SQLException
      Use a parametric SELECT query to generate a corresponding Tuple-derived class to represent future evaluations of the same query or similar queries.
      Parameters:
      codeDirectory - Directory in which compiled Tuple-derived source and .class will be generated.
      packageSpec - The package, in dotted notation, to which the Tuple belongs.
      tupleClassName - Name of desired Tuple-derived class.
      query - Query to be evaluated.
      parms - Parameter arguments which positionally match to '?' in the query.
      Returns:
      Result of code generation.
      Throws:
      java.sql.SQLException - Error.
    • createTupleFromQueryAll

      public TupleTypeGenerator.GenerateResult createTupleFromQueryAll​(java.lang.String codeDirectory, java.lang.String packageSpec, java.lang.String tupleClassName, java.lang.String query) throws java.sql.SQLException
      Use a SELECT query to generate a corresponding Tuple-derived class to represent future evaluations of the same query or similar queries.
      Parameters:
      codeDirectory - Directory in which compiled Tuple-derived source and .class will be generated.
      packageSpec - The package, in dotted notation, to which the Tuple belongs.
      tupleClassName - Name of desired Tuple-derived class.
      query - Query to be evaluated.
      Returns:
      Result of code generation.
      Throws:
      java.sql.SQLException - Error.
    • createTupleFromQueryForUpdate

      public TupleTypeGenerator.GenerateResult createTupleFromQueryForUpdate​(java.lang.String codeDirectory, java.lang.String packageSpec, java.lang.String tupleClassName, java.lang.String tableName, java.lang.String query, java.lang.Object... parms) throws java.sql.SQLException
      Use a parametric SELECT query to generate a corresponding UpdatableTuple-derived class to represent future evaluations of the same query or similar queries.
      Parameters:
      codeDirectory - Directory in which compiled UpdatableTuple-derived source and .class will be generated.
      packageSpec - The package, in dotted notation, to which the Tuple belongs.
      tupleClassName - Name of desired UpdatableTuple-derived class.
      tableName - Name of table this Tuple maps to. Null if not mapped to a table.
      query - Query to be evaluated.
      parms - Parameter arguments which positionally match to '?' in the query.
      Returns:
      Result of code generation.
      Throws:
      java.sql.SQLException - Error.
    • createTupleFromQueryAllForUpdate

      public TupleTypeGenerator.GenerateResult createTupleFromQueryAllForUpdate​(java.lang.String codeDirectory, java.lang.String packageSpec, java.lang.String tupleClassName, java.lang.String tableName, java.lang.String query) throws java.sql.SQLException
      Use a SELECT query to generate a corresponding UpdatableTuple-derived class to represent future evaluations of the same query or similar queries.
      Parameters:
      codeDirectory - Directory in which compiled UpdatableTuple-derived source and .class will be generated.
      packageSpec - The package, in dotted notation, to which the Tuple belongs.
      tupleClassName - Name of desired UpdatableTuple-derived class.
      tableName - Name of table this Tuple maps to. Null if not mapped to a table.
      query - Query to be evaluated.
      Returns:
      Result of code generation.
      Throws:
      java.sql.SQLException - Error.
    • getKeyColumnNamesFor

      public java.lang.String[] getKeyColumnNamesFor​(java.lang.String tableName) throws java.sql.SQLException
      Get primary key for a given table.
      Parameters:
      tableName - Table name.
      Returns:
      Array of column names comprising the primary key.
      Throws:
      java.sql.SQLException - Error.
    • processPreparedStatement

      public <T> Response<T> processPreparedStatement​(Database.PreparedStatementUser<T> preparedStatementUser, java.lang.String query, java.lang.Object... parms) throws java.sql.SQLException
      Use a prepared statement.
      Type Parameters:
      T - The type of return value from use of connection.
      Parameters:
      preparedStatementUser - Instance of PreparedStatementUser, usually as a lambda expression.
      query - SQL text.
      parms - Parameter arguments to prepared statement.
      Returns:
      A PreparedStatementUseResult<T> containing either a T (indicating success) or a SQLException.
      Throws:
      java.sql.SQLException - Error
    • usePreparedStatement

      public <T> T usePreparedStatement​(Database.PreparedStatementUser<T> preparedStatementUser, java.lang.String query, java.lang.Object... parms) throws java.sql.SQLException
      Use a prepared statement.
      Type Parameters:
      T - Type of return value from user of connection.
      Parameters:
      preparedStatementUser - Instance of PreparedStatementUser, usually as a lambda expression.
      query - SQL text.
      parms - Parameter arguments to prepared statement.
      Returns:
      A value of type T as a result of using a PreparedStatement.
      Throws:
      java.sql.SQLException - Error