Class TopiaSqlQuery<O>
java.lang.Object
org.nuiton.topia.persistence.support.TopiaSqlQuery<O>
- Type Parameters:
O- the type of result data
Wrap a Sql query some object.
Implements the
prepareResult(ResultSet) to transforma result set
row to an object.
You can also do some stuff on the result set just after the query was
executed using method afterExecuteQuery(ResultSet).- Since:
- 2.5
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidA hook to obtain the result set just after the query execute.protected String[]getColumnNames(ResultSet set) Obtain the column names of a given result set using his metadata.protected longFrom a given result set, let's count his number of row.getRowAsMap(String[] columnNames, ResultSet set) Given the column names of the result set, transform the row of the result set to a map with column name as key.This is optional : the instance may be capable to provide an args used for the SQL query.This is optional : the instance may be capable to provide an SQL query.abstract PreparedStatementprepareQuery(Connection connection) Prepare the statement used to do the sql query.abstract OprepareResult(ResultSet set) given a result set, extract the data.static <T> TopiaSqlQuery<T>wrap(String sqlQuery, SqlFunction<ResultSet, T> transformer) Let's you easily create aTopiaSqlQueryusing functional code-style.static <T> TopiaSqlQuery<T>wrap(SqlFunction<Connection, PreparedStatement> preparer, SqlFunction<ResultSet, T> transformer) Let's you easily create aTopiaSqlQueryusing functional code-style.
-
Constructor Details
-
TopiaSqlQuery
public TopiaSqlQuery()
-
-
Method Details
-
prepareQuery
Prepare the statement used to do the sql query.- Parameters:
connection- jdbc connection to use- Returns:
- the statement containing the query to execute
- Throws:
SQLException- if any problem
-
prepareResult
given a result set, extract the data.- Parameters:
set- the result set- Returns:
- the data extracted from the current set, or
null - Throws:
SQLException- if any prob
-
afterExecuteQuery
A hook to obtain the result set just after the query execute.- Parameters:
set- the result set just obtained- Throws:
SQLException- if any prob- Since:
- 2.6.4
-
getColumnNames
Obtain the column names of a given result set using his metadata.- Parameters:
set- the result set to inspect- Returns:
- the column names of the result set
- Throws:
SQLException- if any pb- Since:
- 2.6.4
-
getNbRows
From a given result set, let's count his number of row. Note: the result set must be scrollable to go back to before first row.- Parameters:
set- the result set to inspect- Returns:
- the number of row of the given result set
- Throws:
SQLException- if any pb- Since:
- 2.6.4
-
getRowAsMap
Given the column names of the result set, transform the row of the result set to a map with column name as key.- Parameters:
columnNames- column names of the result setset- the set to inspect- Returns:
- the map for the given row of the result set
- Throws:
SQLException- if any pb- Since:
- 2.6.4
-
wrap
public static <T> TopiaSqlQuery<T> wrap(SqlFunction<Connection, PreparedStatement> preparer, SqlFunction<ResultSet, T> transformer) Let's you easily create aTopiaSqlQueryusing functional code-style.- Type Parameters:
T- the expected return type- Parameters:
preparer- used to create thePreparedStatement. SeeprepareQuery(Connection).transformer- used to transform theResultSetto the expected format. SeeprepareResult(ResultSet).- Returns:
- a new TopiaSqlQuery instance using the given interfaces
- See Also:
-
wrap
Let's you easily create aTopiaSqlQueryusing functional code-style.- Type Parameters:
T- the expected return type- Parameters:
sqlQuery- an SQL query used to create thePreparedStatement. SeeprepareQuery(Connection).transformer- used to transform theResultSetto the expected format. SeeprepareResult(ResultSet).- Returns:
- a new TopiaSqlQuery instance using the given interfaces
- See Also:
-
getSqlQuery
This is optional : the instance may be capable to provide an SQL query. Otherwise this method will return an empty optional.- Returns:
- the SQL query used or
Optional.empty()if the SQL query is not available
-
getSqlArgs
This is optional : the instance may be capable to provide an args used for the SQL query. Otherwise this method will return an empty optional.- Returns:
- the args as map or
Optional.empty()if the SQL args are not available
-