Interface IOrmOperations

All Known Subinterfaces:
IOrm, ITransactionContext
All Known Implementing Classes:
Orm, TestDoubleTransactionContext

public interface IOrmOperations
  • Method Details

    • update

      void update(String sql)
    • update

      UpdateResult update(String sql, Setter setter)
    • updateWithPlaceholders

      default UpdateResult updateWithPlaceholders(String sql, Setter setter, Supplier<List<String>> parametersSupplier)
      New version of update(String, Setter) that avoids expensive regex operations
      Parameters:
      sql - SQL string with placeholders such as ? instead of the usual :paramName tokens
      setter - Setter as usual
      parametersSupplier - Supplier that provides the list of value tokens as they would appear in the SQL string
      Returns:
      The result of the update operation
    • save

      UpdateResult save(String tableName, Setter setter)
    • query

      <T> List<T> query(String sql, Setter setter, IRowMapper<T> rowMapper)
      Queries data from the database and maps it to the list of objects using row mapper
      Type Parameters:
      T - type of the object you wish database rows to be transformed to
      Parameters:
      sql - query to be executed in order to fetch data. Parameters must be parametrized with named parameters like `:my_param`
      setter - setter used to populate parameters in the query
      rowMapper - mapper transforming OrmResultSet to the object of your choice
      Returns:
      list of objects representing the result set rows from the database (list element per result set row)
    • streamQuery

      default <T> Stream<T> streamQuery(String sql, Setter setter, IRowMapper<T> rowMapper)
      New version of query(String, Setter, IRowMapper) that avoids collecting the whole result into a list. Once the returned stream is consumed, this will be closed.
      Type Parameters:
      T - type of the object you wish database rows to be transformed to
      Parameters:
      sql - query to be executed in order to fetch data. Parameters must be parametrized with named parameters like `:my_param`
      setter - setter used to populate parameters in the query
      rowMapper - mapper transforming OrmResultSet to the object of your choice
      Returns:
      stream of objects representing the result set rows from the database (stream element per result set row)
    • queryWithPlaceholders

      default <T> List<T> queryWithPlaceholders(String sql, Setter setter, List<String> parameters, IRowMapper<T> rowMapper)
      New version of query(String, Setter, IRowMapper) that avoids expensive regex operations
      Type Parameters:
      T - Type of the object you wish database rows to be transformed to
      Parameters:
      sql - SQL string with placeholders such as ? instead of the usual :paramName tokens
      setter - Setter as usual
      parameters - List of value tokens as they would appear in the SQL string
      rowMapper - Mapper transforming OrmResultSet to the object of your choice
      Returns:
      List of objects representing the result set rows from the database (list element per result set row)
    • streamQueryWithPlaceholders

      default <T> Stream<T> streamQueryWithPlaceholders(String sql, Setter setter, List<String> parameters, IRowMapper<T> rowMapper)
      New version of streamQuery(String, Setter, IRowMapper) that avoids expensive regex operations. Once the returned stream is consumed, this will be closed.
      Type Parameters:
      T - Type of the object you wish database rows to be transformed to
      Parameters:
      sql - SQL string with placeholders such as ? instead of the usual :paramName tokens
      setter - Setter as usual
      parameters - List of value tokens as they would appear in the SQL string
      rowMapper - Mapper transforming OrmResultSet to the object of your choice
      Returns:
      Stream of objects representing the result set rows from the database (stream element per result set row)
    • querySingle

      <T> Optional<T> querySingle(String sql, Setter setter, IRowMapper<T> rowMapper) throws OrmException.MoreThanOneRowFound
      Queries for a single row from the database and maps it to the object using row mapper. Throws an exception if more than one row has been found. Returns empty optional in case if no rows were found or the row is mapped to null value.
      Type Parameters:
      T - type of the object you wish database rows to be transformed to
      Parameters:
      sql - query to be executed in order to fetch data. Parameters must be parametrized with named parameters like `:my_param`
      setter - setter used to populate parameters in the query
      rowMapper - mapper transforming OrmResultSet to the object of your choice
      Returns:
      Optional of the object representing a row found in the database. Empty if no row were found or row is mapped to null value
      Throws:
      OrmException.MoreThanOneRowFound - in case more than one row can be found for a given query
    • queryOne

      @Deprecated <T> Optional<T> queryOne(String sql, Setter setter, IRowMapper<T> rowMapper) throws OrmException.MoreThanOneRowFound, NullPointerException
      Deprecated.
      Deprecated because it throws a NullPointerException when the result is null instead of returning empty optional.

      Queries for a single row from the database and maps it to the object using row mapper. Throws an exception if more than one row has been found. Returns empty optional in case if no rows were found.

      Type Parameters:
      T - type of the object you wish database rows to be transformed to
      Parameters:
      sql - query to be executed in order to fetch data. Parameters must be parametrized with named parameters like `:my_param`
      setter - setter used to populate parameters in the query
      rowMapper - mapper transforming OrmResultSet to the object of your choice
      Returns:
      Optional of the object representing a row found in the database. Empty if no row were found
      Throws:
      OrmException.MoreThanOneRowFound - in case more than one row can be found for a given query
      NullPointerException - in case more than one row can be found for a given query
    • querySingle

      default <T> Optional<T> querySingle(String sql, IRowMapper<T> rowMapper) throws OrmException.MoreThanOneRowFound
      Overloaded version of querySingle(String, Setter, IRowMapper) Should be used when the SQL is non-parametrized thus Setter is not needed
      Throws:
      OrmException.MoreThanOneRowFound
    • queryOne

      @Deprecated default <T> Optional<T> queryOne(String sql, IRowMapper<T> rowMapper) throws OrmException.MoreThanOneRowFound, NullPointerException
      Deprecated.
      Throws:
      OrmException.MoreThanOneRowFound
      NullPointerException
    • query

      default <T> List<T> query(String sql, IRowMapper<T> rowMapper)
      Overloaded version of query(String, Setter, IRowMapper) Should be used when the SQL is non-parametrized thus Setter is not needed
    • conditionalExecute

      @Deprecated <ReturnType> ReturnType conditionalExecute(Function<IOrmOperations,ReturnType> mySqlFunction, Function<IOrmOperations,ReturnType> sqlServerFunction)
      Deprecated.
      Please avoid using this method, since you will need to update it every time a new database is added.
      Execute the consumer depending on the database type of the connection. This method should be your last resource when running vendor specific code.
      Parameters:
      mySqlFunction - Consumer to be executed if the database is MariaDB or MySQL.
      sqlServerFunction - Consumer to be executed if the database is SQL Server.