Package com.blackrook.sql
Class SQLAbstractDAO
java.lang.Object
com.blackrook.sql.SQLAbstractDAO
Abstract DAO class.
Contains convenience methods for database pool access.
- Since:
- 1.3.0
- Author:
- Matthew Tropiano
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
A single criterion.class
Exception thrown when a Data Access Object fails at storage or retrieval unexpectedly.class
Exception thrown when a Data Access Object fails at storage or retrieval unexpectedly.static enum
A criterion operator.static class
A single ordering criterion.static class
Query string builder. -
Constructor Summary
ModifierConstructorDescriptionprotected
SQLAbstractDAO
(SQLPool pool) Creates this DAO attached to the provided pool, no connection acquisition timeout.protected
SQLAbstractDAO
(SQLPool pool, long acquireTimeout) Creates this DAO attached to the provided pool, no connection acquisition timeout. -
Method Summary
Modifier and TypeMethodDescriptionprotected SQLAbstractDAO.QueryStringBuilder
buildQuery
(String queryFragment) Creates a query builder that is pre-populated with a query fragment.protected <R> R
call
(SQLConnectionFunction<R> handler) Gets a connection and performs a function on it, returning the result.protected static SQLAbstractDAO.Criterion
column
(String columnName, SQLAbstractDAO.Operator operator, Object value) Creates a column operation criterion.protected static SQLAbstractDAO.Criterion
Creates a column "in" criterion.protected static SQLAbstractDAO.Criterion
columnIsNull
(String columnName) Creates a null column criterion.protected static SQLAbstractDAO.Criterion
columnNotIn
(String columnName, Object... values) Creates a column "not in" criterion.protected Object
id
(SQLConnectionFunction<SQLResult> handler) Gets a connection and performs a function on it, and if the affected row count is 1, return the id.protected Object[]
ids
(SQLConnectionFunction<SQLResult> handler) Gets a connection and performs a function on it, and returns the generated ids.protected static Object[]
parameters
(SQLAbstractDAO.Criterion... criteria) Extracts the values from a set of criteria (to be used as parameters).protected int
rowCount
(SQLConnectionFunction<SQLResult> handler) Gets a connection and performs a function on it, returning the altered row count.protected void
setAcquireTimeout
(long acquireTimeout) Sets this DAO's connection acquisition timeout.protected <R> R
transaction
(SQLConnection.TransactionLevel level, SQLTransactionFunction<R> handler) Gets a connection and performs a function on it, returning the result.protected boolean
updated
(SQLConnectionFunction<SQLResult> handler) Gets a connection and performs a function on it, and if the affected row count is 1, return true.protected <R> R
value
(SQLConnectionFunction<SQLRow> handler, Function<SQLRow, R> extractor) Lifts a single value from a queriedSQLRow
and returns it, returning null if the row is null.valueCaseInsenstiveMap
(SQLConnectionFunction<SQLResult> handler, BiConsumer<SQLRow, SortedMap<String, V>> extractor) Lifts a map of values from a queriedSQLResult
and returns it as an immutable map of case-insensitive string key to value.protected <R> List<R>
valueList
(SQLConnectionFunction<SQLResult> handler, Function<SQLRow, R> extractor) Lifts a list of single values from a queriedSQLResult
and returns it as an immutable list.protected <K,
V> Map<K, V> valueMap
(SQLConnectionFunction<SQLResult> handler, BiConsumer<SQLRow, Map<K, V>> extractor) Lifts a map of values from a queriedSQLResult
and returns it as an immutable map of key to value.protected <R> Set<R>
valueSet
(SQLConnectionFunction<SQLResult> handler, Function<SQLRow, R> extractor) Lifts a list of single values from a queriedSQLResult
and returns it as an immutable set of unique values.protected <K,
V> SortedMap<K, V> valueSortedMap
(SQLConnectionFunction<SQLResult> handler, BiConsumer<SQLRow, SortedMap<K, V>> extractor) Lifts a sorted map of values from a queriedSQLResult
and returns it as an immutable map of key to value.protected <R> SortedSet<R>
valueSortedSet
(SQLConnectionFunction<SQLResult> handler, Function<SQLRow, R> extractor) Lifts a list of single values from a queriedSQLResult
and returns it as an immutable sorted set of unique values.
-
Constructor Details
-
SQLAbstractDAO
Creates this DAO attached to the provided pool, no connection acquisition timeout.- Parameters:
pool
- the pool to acquire connections from.
-
SQLAbstractDAO
Creates this DAO attached to the provided pool, no connection acquisition timeout.- Parameters:
pool
- the pool to acquire connections from.acquireTimeout
- the connection acquisition timeout in milliseconds.
-
-
Method Details
-
setAcquireTimeout
protected void setAcquireTimeout(long acquireTimeout) Sets this DAO's connection acquisition timeout.- Parameters:
acquireTimeout
- the new timeout in milliseconds.- See Also:
-
buildQuery
Creates a query builder that is pre-populated with a query fragment.- Parameters:
queryFragment
- the query resource name.- Returns:
- a new query builder.
-
call
Gets a connection and performs a function on it, returning the result.- Type Parameters:
R
- the return type.- Parameters:
handler
- the connection handler function.- Returns:
- the return object.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
transaction
protected <R> R transaction(SQLConnection.TransactionLevel level, SQLTransactionFunction<R> handler) Gets a connection and performs a function on it, returning the result.- Type Parameters:
R
- the return type.- Parameters:
level
- the transaction level.handler
- the connection handler function.- Returns:
- the return object.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
rowCount
Gets a connection and performs a function on it, returning the altered row count.- Parameters:
handler
- the connection handler function.- Returns:
- the row count.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
updated
Gets a connection and performs a function on it, and if the affected row count is 1, return true.- Parameters:
handler
- the connection handler function.- Returns:
- true if the row count is 1.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
id
Gets a connection and performs a function on it, and if the affected row count is 1, return the id.- Parameters:
handler
- the connection handler function.- Returns:
- the returned id, or null if row count isn't 1.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
ids
Gets a connection and performs a function on it, and returns the generated ids.- Parameters:
handler
- the connection handler function.- Returns:
- the array of returned id objects.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
value
Lifts a single value from a queriedSQLRow
and returns it, returning null if the row is null. If the row is null, the extractor is not called.- Type Parameters:
R
- the return type.- Parameters:
handler
- the handler function for returning a row from a query.extractor
- the value returner function (called if row is not null).- Returns:
- the value returned, or
null
if the row is null. - Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
valueList
protected <R> List<R> valueList(SQLConnectionFunction<SQLResult> handler, Function<SQLRow, R> extractor) Lifts a list of single values from a queriedSQLResult
and returns it as an immutable list. If the result has zero rows, the extractor is never called.- Type Parameters:
R
- the return type.- Parameters:
handler
- the handler function for returning a row from a query.extractor
- the function to call per row for conversion.- Returns:
- the List generated.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
valueSet
protected <R> Set<R> valueSet(SQLConnectionFunction<SQLResult> handler, Function<SQLRow, R> extractor) Lifts a list of single values from a queriedSQLResult
and returns it as an immutable set of unique values. If the result has zero rows, the extractor is never called.- Type Parameters:
R
- the return type.- Parameters:
handler
- the handler function for returning a row from a query.extractor
- the value returner function (called if row is not null).- Returns:
- the Set generated.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
valueSortedSet
protected <R> SortedSet<R> valueSortedSet(SQLConnectionFunction<SQLResult> handler, Function<SQLRow, R> extractor) Lifts a list of single values from a queriedSQLResult
and returns it as an immutable sorted set of unique values. If the result has zero rows, the extractor is never called.- Type Parameters:
R
- the return type.- Parameters:
handler
- the handler function for returning a row from a query.extractor
- the value returner function (called if row is not null).- Returns:
- the Set generated.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
valueMap
protected <K,V> Map<K,V> valueMap(SQLConnectionFunction<SQLResult> handler, BiConsumer<SQLRow, Map<K, V>> extractor) Lifts a map of values from a queriedSQLResult
and returns it as an immutable map of key to value. If the result has zero rows, the extractor is never called.- Type Parameters:
K
- the key type.V
- the value type.- Parameters:
handler
- the handler function for returning a row from a query.extractor
- the function for adding the map entries from a row (second parameter is the Map to add to).- Returns:
- the Map generated.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
valueSortedMap
protected <K,V> SortedMap<K,V> valueSortedMap(SQLConnectionFunction<SQLResult> handler, BiConsumer<SQLRow, SortedMap<K, V>> extractor) Lifts a sorted map of values from a queriedSQLResult
and returns it as an immutable map of key to value. If the result has zero rows, the extractor is never called.- Type Parameters:
K
- the key type.V
- the value type.- Parameters:
handler
- the handler function for returning a row from a query.extractor
- the function for adding the map entries from a row (second parameter is the SortedMap to add to).- Returns:
- the Map generated.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
valueCaseInsenstiveMap
protected <V> SortedMap<String,V> valueCaseInsenstiveMap(SQLConnectionFunction<SQLResult> handler, BiConsumer<SQLRow, SortedMap<String, V>> extractor) Lifts a map of values from a queriedSQLResult
and returns it as an immutable map of case-insensitive string key to value. If the result has zero rows, the extractor is never called.- Type Parameters:
V
- the value type.- Parameters:
handler
- the handler function for returning a row from a query.extractor
- the function for adding the map entries from a row (second parameter is the SortedMap to add to).- Returns:
- the Map generated.
- Throws:
SQLAbstractDAO.DataAccessTimeoutException
- if a connection timeout occurs.SQLAbstractDAO.DataAccessFailureException
- if any other exception occurs.
-
columnIsNull
Creates a null column criterion.- Parameters:
columnName
- the column name.- Returns:
- the created criterion.
-
columnIn
Creates a column "in" criterion.- Parameters:
columnName
- the column name.values
- the values.- Returns:
- the created criterion.
-
columnNotIn
Creates a column "not in" criterion.- Parameters:
columnName
- the column name.values
- the values.- Returns:
- the created criterion.
-
column
protected static SQLAbstractDAO.Criterion column(String columnName, SQLAbstractDAO.Operator operator, Object value) Creates a column operation criterion.- Parameters:
columnName
- the column name.operator
- the operator.value
- the value.- Returns:
- the created criterion.
-
parameters
Extracts the values from a set of criteria (to be used as parameters).- Parameters:
criteria
- the list of criteria.- Returns:
- the array of parameters.
-