Package io.ultreia.java4all.util.sql
Class SqlQuery<O>
- java.lang.Object
-
- io.ultreia.java4all.util.sql.SqlQuery<O>
-
- Type Parameters:
O- the type of result data
public abstract class SqlQuery<O> extends Object
Wrap a Sql query some object.Implements the
prepareResult(ResultSet)to transform a 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:
- 1.1.1
- Author:
- Tony Chemit - dev@tchemit.fr
-
-
Constructor Summary
Constructors Constructor Description SqlQuery()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidafterExecuteQuery(ResultSet set)A 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 longgetNbRows(ResultSet set)From a given result set, let's count his number of row.protected Map<String,Object>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.Optional<List<?>>getSqlArgs()This is optional : the instance may be capable to provide an args used for the SQL query.Optional<String>getSqlQuery()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> SqlQuery<T>wrap(SqlFunction<Connection,PreparedStatement> preparer, SqlFunction<ResultSet,T> transformer)Let's you easily create aSqlQueryusing functional code-style.static <T> SqlQuery<T>wrap(String sqlQuery, SqlFunction<ResultSet,T> transformer)Let's you easily create aSqlQueryusing functional code-style.
-
-
-
Method Detail
-
prepareQuery
public abstract PreparedStatement prepareQuery(Connection connection) throws SQLException
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
public abstract O prepareResult(ResultSet set) throws SQLException
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
public void afterExecuteQuery(ResultSet set) throws SQLException
A hook to obtain the result set just after the query execute.- Parameters:
set- the result set just obtained- Throws:
SQLException- if any prob
-
getColumnNames
protected String[] getColumnNames(ResultSet set) throws SQLException
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
-
getNbRows
protected long getNbRows(ResultSet set) throws SQLException
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
-
getRowAsMap
protected Map<String,Object> getRowAsMap(String[] columnNames, ResultSet set) throws SQLException
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
-
wrap
public static <T> SqlQuery<T> wrap(SqlFunction<Connection,PreparedStatement> preparer, SqlFunction<ResultSet,T> transformer)
Let's you easily create aSqlQueryusing 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 SqlQuery instance using the given interfaces
- See Also:
prepareQuery(Connection),prepareResult(ResultSet)
-
wrap
public static <T> SqlQuery<T> wrap(String sqlQuery, SqlFunction<ResultSet,T> transformer)
Let's you easily create aSqlQueryusing 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 SqlQuery instance using the given interfaces
- See Also:
prepareQuery(Connection),prepareResult(ResultSet)
-
getSqlQuery
public Optional<String> 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
public Optional<List<?>> 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
-
-