Class TopiaSqlQuery<O>

java.lang.Object
org.nuiton.topia.persistence.support.TopiaSqlQuery<O>
Type Parameters:
O - the type of result data

public abstract class TopiaSqlQuery<O> extends Object
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 Details

    • TopiaSqlQuery

      public TopiaSqlQuery()
  • Method Details

    • 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
      Since:
      2.6.4
    • 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
      Since:
      2.6.4
    • 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
      Since:
      2.6.4
    • 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 set
      set - 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 a TopiaSqlQuery using functional code-style.
      Type Parameters:
      T - the expected return type
      Parameters:
      preparer - used to create the PreparedStatement. See prepareQuery(Connection).
      transformer - used to transform the ResultSet to the expected format. See prepareResult(ResultSet).
      Returns:
      a new TopiaSqlQuery instance using the given interfaces
      See Also:
    • wrap

      public static <T> TopiaSqlQuery<T> wrap(String sqlQuery, SqlFunction<ResultSet,T> transformer)
      Let's you easily create a TopiaSqlQuery using functional code-style.
      Type Parameters:
      T - the expected return type
      Parameters:
      sqlQuery - an SQL query used to create the PreparedStatement. See prepareQuery(Connection).
      transformer - used to transform the ResultSet to the expected format. See prepareResult(ResultSet).
      Returns:
      a new TopiaSqlQuery instance using the given interfaces
      See Also:
    • 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