java.lang.Object
org.dalesbred.DatabaseAccess
- Direct Known Subclasses:
Database,DatabaseConnection
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:
Database– the standard entry point; manages a connection pool or data source and handles transactions automaticallyDatabaseConnection– wraps a single caller-owned JDBC connection obtained fromDatabaseSource.openConnection()
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final @NotNull DialectThe dialect that the database usesprotected final @NotNull InstantiatorProviderContains the instantiators and data-converters -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbindArguments(@NotNull PreparedStatement ps, @NotNull Iterable<?> args) protected voidbindQueryParameters(@NotNull PreparedStatement ps, @NotNull SqlQuery query) <T> TexecuteQuery(@NotNull ResultSetProcessor<T> processor, @NotNull String sql, Object... args) Executes a query and processes the results with givenResultSetProcessor.<T> TexecuteQuery(@NotNull ResultSetProcessor<T> processor, @NotNull SqlQuery query) Executes a query and processes the results with givenResultSetProcessor.<T> @NotNull List<T> Executes a query and converts the results to instances of given class using default mechanisms.<T> @NotNull List<T> Executes a query and converts the results to instances of given class using default mechanisms.<T> @NotNull List<T> Executes a query and processes each row of the result with givenRowMapperto produce a list of results.<T> @NotNull List<T> Executes a query and processes each row of the result with givenRowMapperto produce a list of results.<K,V> @NotNull Map <K, V> findMap(@NotNull Class<K> keyType, @NotNull Class<V> valueType, @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 instantiatingV.<K,V> @NotNull Map <K, V> 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 instantiatingV.<T> @NotNull Optional<T> findOptional(@NotNull Class<T> cl, @NotNull String sql, Object... args) Finds a unique result from database, converting the database row to given class using default mechanisms.<T> @NotNull Optional<T> findOptional(@NotNull Class<T> cl, @NotNull SqlQuery query) Finds a unique result from database, converting the database row to given class using default mechanisms.<T> @NotNull Optional<T> findOptional(@NotNull RowMapper<T> rowMapper, @NotNull String sql, Object... args) Find a unique result from database, using givenRowMapperto convert row.<T> @NotNull Optional<T> findOptional(@NotNull RowMapper<T> rowMapper, @NotNull SqlQuery query) Find a unique result from database, using givenRowMapperto convert row.@NotNull OptionalDoublefindOptionalDouble(@NotNull String sql, Object... args) Finds a unique result from database, converting the database row to double using default mechanisms.@NotNull OptionalDoublefindOptionalDouble(@NotNull SqlQuery query) Finds a unique result from database, converting the database row to double using default mechanisms.@NotNull OptionalIntfindOptionalInt(@NotNull String sql, Object... args) Finds a unique result from database, converting the database row to int using default mechanisms.@NotNull OptionalIntfindOptionalInt(@NotNull SqlQuery query) Finds a unique result from database, converting the database row to int using default mechanisms.@NotNull OptionalLongfindOptionalLong(@NotNull String sql, Object... args) Finds a unique result from database, converting the database row to long using default mechanisms.@NotNull OptionalLongfindOptionalLong(@NotNull SqlQuery query) Finds a unique result from database, converting the database row to long using default mechanisms.@NotNull ResultTableExecutes a query and creates aResultTablefrom the results.@NotNull ResultTableExecutes a query and creates aResultTablefrom the results.<T> TfindUnique(@NotNull Class<T> cl, @NotNull String sql, Object... args) Finds a unique result from database, converting the database row to given class using default mechanisms.<T> TfindUnique(@NotNull Class<T> cl, @NotNull SqlQuery query) Finds a unique result from database, converting the database row to given class using default mechanisms.<T> TfindUnique(@NotNull RowMapper<T> mapper, @NotNull String sql, Object... args) Finds a unique result from database, using givenRowMapperto convert the row.<T> TfindUnique(@NotNull RowMapper<T> mapper, @NotNull SqlQuery query) Finds a unique result from database, using givenRowMapperto convert the row.booleanfindUniqueBoolean(@NotNull String sql, Object... args) A convenience method for retrieving a single non-null boolean.booleanfindUniqueBoolean(@NotNull SqlQuery query) A convenience method for retrieving a single non-null boolean.intfindUniqueInt(@NotNull String sql, Object... args) A convenience method for retrieving a single non-null integer.intfindUniqueInt(@NotNull SqlQuery query) A convenience method for retrieving a single non-null integer.longfindUniqueLong(@NotNull String sql, Object... args) A convenience method for retrieving a single non-null long.longfindUniqueLong(@NotNull SqlQuery query) A convenience method for retrieving a single non-null long.<T> TfindUniqueOrNull(@NotNull Class<T> cl, @NotNull String sql, Object... args) Alias forfindOptional(cl, sql, args).orElse(null).<T> TfindUniqueOrNull(@NotNull Class<T> cl, @NotNull SqlQuery query) Alias forfindOptional(cl, query).orElse(null).<T> TfindUniqueOrNull(@NotNull RowMapper<T> rowMapper, @NotNull String sql, Object... args) Alias for {findUniqueOrNull(rowMapper, SqlQuery.query(sql, args))}.<T> TfindUniqueOrNull(@NotNull RowMapper<T> rowMapper, @NotNull SqlQuery query) Alias forfindOptional(rowMapper, query).orElse(null).@Nullable DurationReturns the default query timeout, ornullif none is set.protected voidprepareStatementFromQuery(@NotNull PreparedStatement ps, @NotNull SqlQuery query) voidsetDefaultTimeout(@NotNull Duration timeout) Sets a default timeout applied to all queries unless overridden on theSqlQueryitself or via JDBC connection parameters.intExecutes an update against the database and returns the amount of affected rows.intExecutes an update against the database and returns the amount of affected rows.<T> TupdateAndProcessGeneratedKeys(@NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull List<String> columnNames, @NotNull String sql, Object... args) <T> TupdateAndProcessGeneratedKeys(@NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull List<String> columnNames, @NotNull SqlQuery query) Executes an update against the database and return generated keys as extracted by generatedKeysProcessor.int[]updateBatch(@NotNull String sql, @NotNull List<? extends List<?>> argumentLists) Executes a batch update against the database, returning an array of modification counts for each argument list.<T> TupdateBatchAndProcessGeneratedKeys(@NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull List<String> columnNames, @NotNull String sql, @NotNull List<? extends List<?>> argumentLists) Executes batch of updates against the database and return generated keys as extracted by generatedKeysProcessor.voidupdateUnique(@NotNull String sql, Object... args) Execute an update against the database and assert that a single row will be modified.voidupdateUnique(@NotNull SqlQuery query) Execute an update against the database and assert that a single row will be modified.protected abstract <T> TwithCurrentTransaction(@NotNull SqlQuery query, @NotNull TransactionCallback<T> callback)
-
Field Details
-
dialect
The dialect that the database uses -
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 givenResultSetProcessor. 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 givenResultSetProcessor.- 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 givenRowMapperto 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 givenRowMapperto 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
Finds a unique result from database, using givenRowMapperto convert the row.- Throws:
NonUniqueResultException- if there is more then one rowEmptyResultException- 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 givenRowMapperto convert the row.- Throws:
NonUniqueResultException- if there is more then one rowEmptyResultException- if there are no rows
-
findUnique
Finds a unique result from database, converting the database row to given class using default mechanisms.- Throws:
NonUniqueResultException- if there is more then one rowEmptyResultException- 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 rowEmptyResultException- 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 givenRowMapperto 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 givenRowMapperto 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
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
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
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 forfindOptional(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 forfindOptional(cl, query).orElse(null). -
findUniqueOrNull
@Nullable public <T> T findUniqueOrNull(@NotNull @NotNull Class<T> cl, @NotNull @Language("SQL") @NotNull String sql, Object... args) Alias forfindOptional(cl, sql, args).orElse(null). -
findUniqueBoolean
A convenience method for retrieving a single non-null boolean.- Throws:
NonUniqueResultException- if there is more then one rowEmptyResultException- if there are no rows
-
findUniqueBoolean
A convenience method for retrieving a single non-null boolean.- Throws:
NonUniqueResultException- if there is more then one rowEmptyResultException- if there are no rows
-
findUniqueInt
A convenience method for retrieving a single non-null integer.- Throws:
NonUniqueResultException- if there is more then one rowEmptyResultException- if there are no rows
-
findUniqueInt
A convenience method for retrieving a single non-null integer.- Throws:
NonUniqueResultException- if there is more than one rowEmptyResultException- if there are no rows
-
findUniqueLong
A convenience method for retrieving a single non-null long.- Throws:
NonUniqueResultException- if there is more then one rowEmptyResultException- if there are no rows
-
findUniqueLong
A convenience method for retrieving a single non-null long.- Throws:
NonUniqueResultException- if there is more then one rowEmptyResultException- 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 instantiatingV.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 instantiatingV.If the keys of the result are not distinct, the result contains the last binding of given key.
-
findTable
Executes a query and creates aResultTablefrom the results. -
findTable
@NotNull public @NotNull ResultTable findTable(@NotNull @Language("SQL") @NotNull String sql, Object... args) Executes a query and creates aResultTablefrom the results. -
update
Executes an update against the database and returns the amount of affected rows. -
update
Executes an update against the database and returns the amount of affected rows. -
updateUnique
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
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 keyscolumnNames- names of columns that contain the generated keys. Can be empty, in which case the returned columns depend on the databasequery- 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 keyscolumnNames- names of columns that contain the generated keys. Can be empty, in which case the returned columns depend on the databasesql- to executeargumentLists- List of argument lists for items of batch- Returns:
- Result of processing the results with
generatedKeysProcessor.
-
getDefaultTimeout
Returns the default query timeout, ornullif none is set. -
setDefaultTimeout
Sets a default timeout applied to all queries unless overridden on theSqlQueryitself 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
-