Package org.dalesbred

Class DatabaseAccess

java.lang.Object
org.dalesbred.DatabaseAccess
Direct Known Subclasses:
Database, DatabaseConnection

public abstract class DatabaseAccess extends Object
Abstract base class for database access, providing all query and update operations independently of how connections and transactions are managed.

The two concrete subclasses are:

  • Field Details

    • dialect

      @NotNull protected final @NotNull Dialect dialect
      The dialect that the database uses
    • instantiatorRegistry

      @NotNull protected final @NotNull InstantiatorProvider instantiatorRegistry
      Contains the instantiators and data-converters
  • Method Details

    • executeQuery

      public <T> T executeQuery(@NotNull @NotNull ResultSetProcessor<T> processor, @NotNull @NotNull SqlQuery query)
      Executes a query and processes the results with given ResultSetProcessor. All other findXXX-methods are just convenience methods for this one.
    • executeQuery

      public <T> T executeQuery(@NotNull @NotNull ResultSetProcessor<T> processor, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      Executes a query and processes the results with given ResultSetProcessor.
      See Also:
    • findAll

      @NotNull public <T> @NotNull List<T> findAll(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @NotNull SqlQuery query)
      Executes a query and processes each row of the result with given RowMapper to produce a list of results.
    • findAll

      @NotNull public <T> @NotNull List<T> findAll(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      Executes a query and processes each row of the result with given RowMapper to produce a list of results.
    • findAll

      @NotNull public <T> @NotNull List<T> findAll(@NotNull @NotNull Class<T> cl, @NotNull @NotNull SqlQuery query)
      Executes a query and converts the results to instances of given class using default mechanisms.
    • findAll

      @NotNull public <T> @NotNull List<T> findAll(@NotNull @NotNull Class<T> cl, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      Executes a query and converts the results to instances of given class using default mechanisms.
    • findUnique

      public <T> T findUnique(@NotNull @NotNull RowMapper<T> mapper, @NotNull @NotNull SqlQuery query)
      Finds a unique result from database, using given RowMapper to convert the row.
      Throws:
      NonUniqueResultException - if there is more then one row
      EmptyResultException - if there are no rows
    • findUnique

      public <T> T findUnique(@NotNull @NotNull RowMapper<T> mapper, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      Finds a unique result from database, using given RowMapper to convert the row.
      Throws:
      NonUniqueResultException - if there is more then one row
      EmptyResultException - if there are no rows
    • findUnique

      public <T> T findUnique(@NotNull @NotNull Class<T> cl, @NotNull @NotNull SqlQuery query)
      Finds a unique result from database, converting the database row to given class using default mechanisms.
      Throws:
      NonUniqueResultException - if there is more then one row
      EmptyResultException - if there are no rows
    • findUnique

      public <T> T findUnique(@NotNull @NotNull Class<T> cl, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      Finds a unique result from database, converting the database row to given class using default mechanisms.
      Throws:
      NonUniqueResultException - if there is more then one row
      EmptyResultException - if there are no rows
    • findOptional

      @NotNull public <T> @NotNull Optional<T> findOptional(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @NotNull SqlQuery query)
      Find a unique result from database, using given RowMapper to convert row. Returns empty if there are no results or if single null result is returned.
      Throws:
      NonUniqueResultException - if there are multiple result rows
    • findOptional

      @NotNull public <T> @NotNull Optional<T> findOptional(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      Find a unique result from database, using given RowMapper to convert row. Returns empty if there are no results or if single null result is returned.
      Throws:
      NonUniqueResultException - if there are multiple result rows
    • findOptional

      @NotNull public <T> @NotNull Optional<T> findOptional(@NotNull @NotNull Class<T> cl, @NotNull @NotNull SqlQuery query)
      Finds a unique result from database, converting the database row to given class using default mechanisms. Returns empty if there are no results or if single null result is returned.
      Throws:
      NonUniqueResultException - if there are multiple result rows
    • findOptional

      @NotNull public <T> @NotNull Optional<T> findOptional(@NotNull @NotNull Class<T> cl, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      Finds a unique result from database, converting the database row to given class using default mechanisms. Returns empty if there are no results or if single null result is returned.
      Throws:
      NonUniqueResultException - if there are multiple result rows
    • findOptionalInt

      @NotNull public @NotNull OptionalInt findOptionalInt(@NotNull @Language("SQL") @NotNull String sql, Object... args)
      Finds a unique result from database, converting the database row to int using default mechanisms. Returns empty if there are no results or if single null result is returned.
      Throws:
      NonUniqueResultException - if there are multiple result rows
    • findOptionalInt

      @NotNull public @NotNull OptionalInt findOptionalInt(@NotNull @NotNull SqlQuery query)
      Finds a unique result from database, converting the database row to int using default mechanisms. Returns empty if there are no results or if single null result is returned.
      Throws:
      NonUniqueResultException - if there are multiple result rows
    • findOptionalLong

      @NotNull public @NotNull OptionalLong findOptionalLong(@NotNull @Language("SQL") @NotNull String sql, Object... args)
      Finds a unique result from database, converting the database row to long using default mechanisms. Returns empty if there are no results or if single null result is returned.
      Throws:
      NonUniqueResultException - if there are multiple result rows
    • findOptionalLong

      @NotNull public @NotNull OptionalLong findOptionalLong(@NotNull @NotNull SqlQuery query)
      Finds a unique result from database, converting the database row to long using default mechanisms. Returns empty if there are no results or if single null result is returned.
      Throws:
      NonUniqueResultException - if there are multiple result rows
    • findOptionalDouble

      @NotNull public @NotNull OptionalDouble findOptionalDouble(@NotNull @Language("SQL") @NotNull String sql, Object... args)
      Finds a unique result from database, converting the database row to double using default mechanisms. Returns empty if there are no results or if single null result is returned.
      Throws:
      NonUniqueResultException - if there are multiple result rows
    • findOptionalDouble

      @NotNull public @NotNull OptionalDouble findOptionalDouble(@NotNull @NotNull SqlQuery query)
      Finds a unique result from database, converting the database row to double using default mechanisms. Returns empty if there are no results or if single null result is returned.
      Throws:
      NonUniqueResultException - if there are multiple result rows
    • findUniqueOrNull

      @Nullable public <T> T findUniqueOrNull(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @NotNull SqlQuery query)
      Alias for findOptional(rowMapper, query).orElse(null).
    • findUniqueOrNull

      @Nullable public <T> T findUniqueOrNull(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      Alias for {findUniqueOrNull(rowMapper, SqlQuery.query(sql, args))}.
    • findUniqueOrNull

      @Nullable public <T> T findUniqueOrNull(@NotNull @NotNull Class<T> cl, @NotNull @NotNull SqlQuery query)
      Alias for findOptional(cl, query).orElse(null).
    • findUniqueOrNull

      @Nullable public <T> T findUniqueOrNull(@NotNull @NotNull Class<T> cl, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      Alias for findOptional(cl, sql, args).orElse(null).
    • findUniqueBoolean

      public boolean findUniqueBoolean(@NotNull @NotNull SqlQuery query)
      A convenience method for retrieving a single non-null boolean.
      Throws:
      NonUniqueResultException - if there is more then one row
      EmptyResultException - if there are no rows
    • findUniqueBoolean

      public boolean findUniqueBoolean(@NotNull @Language("SQL") @NotNull String sql, Object... args)
      A convenience method for retrieving a single non-null boolean.
      Throws:
      NonUniqueResultException - if there is more then one row
      EmptyResultException - if there are no rows
    • findUniqueInt

      public int findUniqueInt(@NotNull @NotNull SqlQuery query)
      A convenience method for retrieving a single non-null integer.
      Throws:
      NonUniqueResultException - if there is more then one row
      EmptyResultException - if there are no rows
    • findUniqueInt

      public int findUniqueInt(@NotNull @Language("SQL") @NotNull String sql, Object... args)
      A convenience method for retrieving a single non-null integer.
      Throws:
      NonUniqueResultException - if there is more than one row
      EmptyResultException - if there are no rows
    • findUniqueLong

      public long findUniqueLong(@NotNull @NotNull SqlQuery query)
      A convenience method for retrieving a single non-null long.
      Throws:
      NonUniqueResultException - if there is more then one row
      EmptyResultException - if there are no rows
    • findUniqueLong

      public long findUniqueLong(@NotNull @Language("SQL") @NotNull String sql, Object... args)
      A convenience method for retrieving a single non-null long.
      Throws:
      NonUniqueResultException - if there is more then one row
      EmptyResultException - if there are no rows
    • findMap

      @NotNull public <K, V> @NotNull Map<K,V> findMap(@NotNull @NotNull Class<K> keyType, @NotNull @NotNull Class<V> valueType, @NotNull @NotNull SqlQuery query)
      Executes a query that returns at least two values and creates a map from the results, using the first value as the key and rest of the values for instantiating V.

      If the keys of the result are not distinct, the result contains the last binding of given key.

    • findMap

      @NotNull public <K, V> @NotNull Map<K,V> findMap(@NotNull @NotNull Class<K> keyType, @NotNull @NotNull Class<V> valueType, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      Executes a query that returns at least two values and creates a map from the results, using the first value as the key and rest of the values for instantiating V.

      If the keys of the result are not distinct, the result contains the last binding of given key.

    • findTable

      @NotNull public @NotNull ResultTable findTable(@NotNull @NotNull SqlQuery query)
      Executes a query and creates a ResultTable from the results.
    • findTable

      @NotNull public @NotNull ResultTable findTable(@NotNull @Language("SQL") @NotNull String sql, Object... args)
      Executes a query and creates a ResultTable from the results.
    • update

      public int update(@NotNull @NotNull SqlQuery query)
      Executes an update against the database and returns the amount of affected rows.
    • update

      public int update(@NotNull @Language("SQL") @NotNull String sql, Object... args)
      Executes an update against the database and returns the amount of affected rows.
    • updateUnique

      public void updateUnique(@NotNull @NotNull SqlQuery query)
      Execute an update against the database and assert that a single row will be modified.
      Throws:
      NonUniqueUpdateException - if zero or more then one rows were updated
    • updateUnique

      public void updateUnique(@NotNull @Language("SQL") @NotNull String sql, Object... args)
      Execute an update against the database and assert that a single row will be modified.
      Throws:
      NonUniqueUpdateException - if zero or more then one rows were updated
    • updateAndProcessGeneratedKeys

      public <T> T updateAndProcessGeneratedKeys(@NotNull @NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull @NotNull List<String> columnNames, @NotNull @NotNull SqlQuery query)
      Executes an update against the database and return generated keys as extracted by generatedKeysProcessor.
      Parameters:
      generatedKeysProcessor - processor for handling the generated keys
      columnNames - names of columns that contain the generated keys. Can be empty, in which case the returned columns depend on the database
      query - to execute
      Returns:
      Result of processing the results with generatedKeysProcessor.
    • updateAndProcessGeneratedKeys

      public <T> T updateAndProcessGeneratedKeys(@NotNull @NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull @NotNull List<String> columnNames, @NotNull @Language("SQL") @NotNull String sql, Object... args)
      See Also:
    • updateBatch

      public int[] updateBatch(@Language("SQL") @NotNull @NotNull String sql, @NotNull @NotNull List<? extends List<?>> argumentLists)
      Executes a batch update against the database, returning an array of modification counts for each argument list.
    • updateBatchAndProcessGeneratedKeys

      public <T> T updateBatchAndProcessGeneratedKeys(@NotNull @NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull @NotNull List<String> columnNames, @NotNull @Language("SQL") @NotNull String sql, @NotNull @NotNull List<? extends List<?>> argumentLists)
      Executes batch of updates against the database and return generated keys as extracted by generatedKeysProcessor.
      Parameters:
      generatedKeysProcessor - processor for handling the generated keys
      columnNames - names of columns that contain the generated keys. Can be empty, in which case the returned columns depend on the database
      sql - to execute
      argumentLists - List of argument lists for items of batch
      Returns:
      Result of processing the results with generatedKeysProcessor.
    • getDefaultTimeout

      @Nullable public @Nullable Duration getDefaultTimeout()
      Returns the default query timeout, or null if none is set.
    • setDefaultTimeout

      public void setDefaultTimeout(@NotNull @NotNull Duration timeout)
      Sets a default timeout applied to all queries unless overridden on the SqlQuery itself or via JDBC connection parameters.
      Throws:
      IllegalArgumentException - if timeout is negative
      See Also:
    • withCurrentTransaction

      protected abstract <T> T withCurrentTransaction(@NotNull @NotNull SqlQuery query, @NotNull @NotNull TransactionCallback<T> callback)
    • bindQueryParameters

      protected void bindQueryParameters(@NotNull @NotNull PreparedStatement ps, @NotNull @NotNull SqlQuery query) throws SQLException
      Throws:
      SQLException
    • bindArguments

      protected void bindArguments(@NotNull @NotNull PreparedStatement ps, @NotNull @NotNull Iterable<?> args) throws SQLException
      Throws:
      SQLException
    • prepareStatementFromQuery

      protected void prepareStatementFromQuery(@NotNull @NotNull PreparedStatement ps, @NotNull @NotNull SqlQuery query) throws SQLException
      Throws:
      SQLException