Class 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 Detail

      • SqlQuery

        public SqlQuery()
    • 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 set
        set - the set to inspect
        Returns:
        the map for the given row of the result set
        Throws:
        SQLException - if any pb
      • 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