Class CommonJdbcUtils

java.lang.Object
net.solarnetwork.central.common.dao.jdbc.sql.CommonJdbcUtils

public final class CommonJdbcUtils extends Object
Common JDBC utilities.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    Get an array value from a SQL array instance.
    static Long
    executeCountQuery(org.springframework.jdbc.core.JdbcOperations jdbcTemplate, org.springframework.jdbc.core.PreparedStatementCreator creator)
    Execute a query for a count result.
    static <M extends net.solarnetwork.domain.Identity<K>, K>
    net.solarnetwork.dao.FilterResults<M,K>
    executeFilterQuery(org.springframework.jdbc.core.JdbcOperations jdbcTemplate, net.solarnetwork.dao.PaginationCriteria filter, org.springframework.jdbc.core.PreparedStatementCreator sql, org.springframework.jdbc.core.RowMapper<M> mapper)
    Standardized utility to execute a filter based query.
    static <T> void
    executeStreamingQuery(org.springframework.jdbc.core.JdbcOperations jdbcOps, FilteredResultsProcessor<T> processor, org.springframework.jdbc.core.PreparedStatementCreator sql, org.springframework.jdbc.core.RowMapper<T> mapper)
    Execute a streaming query.
    static <T> void
    executeStreamingQuery(org.springframework.jdbc.core.JdbcOperations jdbcOps, FilteredResultsProcessor<T> processor, org.springframework.jdbc.core.PreparedStatementCreator sql, org.springframework.jdbc.core.RowMapper<T> mapper, Long totalResultCount, Integer startingOffset, Integer expectedResultCount, Map<String,?> attributes)
    Execute a streaming query.
    static <T> T
    getArray(ResultSet rs, int colNum)
    Get an array result column value.
    static <T extends Enum<T> & net.solarnetwork.domain.CodedValue>
    Set<T>
    getCodedValueSet(ResultSet rs, int colNum, Class<T> clazz)
    Get a set of enumerated coded values from a JDBC array.
    static Instant
    getTimestampInstant(ResultSet rs, int column)
    Get an Timestamp column value as an Instant.
    static UUID
    getUuid(ResultSet rs, int column)
    Get a UUID column value.
    static Long
    updateWithGeneratedLong(org.springframework.jdbc.core.JdbcOperations jdbcTemplate, org.springframework.jdbc.core.PreparedStatementCreator sql, String keyColumnName)
    Perform an update operation and extract a generated Long key.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getArray

      public static <T> T getArray(ResultSet rs, int colNum) throws SQLException
      Get an array result column value.
      Type Parameters:
      T - the expected array type
      Parameters:
      rs - the result set
      colNum - the column number
      Returns:
      the array
      Throws:
      SQLException - if any SQL error occurs
      ClassCastException - if a casting error occurs
    • arrayValue

      public static <T> T arrayValue(Object o)
      Get an array value from a SQL array instance.
      Type Parameters:
      T - the expected array type, e.g. Long[].class
      Parameters:
      o - the Array instance
      Returns:
      the array value, or null if o is null or not a Array
      Throws:
      ClassCastException - if a casting error occurs
    • getUuid

      public static UUID getUuid(ResultSet rs, int column) throws SQLException
      Get a UUID column value.

      This method can be more efficient than calling ResultSet.getString(int) if the JDBC driver returns a UUID instance natively. Otherwise this method will call toString() on the column value and parse that as a UUID.

      Parameters:
      rs - the result set to read from
      column - the column number to get as a UUID
      Returns:
      the UUID, or null if the column value is null
      Throws:
      SQLException - if an error occurs
      IllegalArgumentException - if the column value is non-null but does not conform to the string representation as described in UUID.toString()
    • executeCountQuery

      public static Long executeCountQuery(org.springframework.jdbc.core.JdbcOperations jdbcTemplate, org.springframework.jdbc.core.PreparedStatementCreator creator)
      Execute a query for a count result.
      Parameters:
      jdbcTemplate - the JDBC template to use
      creator - the statement creator; if implements CountPreparedStatementCreatorProvider then CountPreparedStatementCreatorProvider.countPreparedStatementCreator() will be used
      Returns:
      the result, or null if no result count is available
    • executeFilterQuery

      public static <M extends net.solarnetwork.domain.Identity<K>, K> net.solarnetwork.dao.FilterResults<M,K> executeFilterQuery(org.springframework.jdbc.core.JdbcOperations jdbcTemplate, net.solarnetwork.dao.PaginationCriteria filter, org.springframework.jdbc.core.PreparedStatementCreator sql, org.springframework.jdbc.core.RowMapper<M> mapper)
      Standardized utility to execute a filter based query.
      Type Parameters:
      M - the filter result type
      K - the filter result key type
      Parameters:
      jdbcTemplate - the JDBC template to use
      filter - the pagination criteria
      sql - the SQL to execute
      mapper - the row mapper to use
      Returns:
      the results, never null
    • executeStreamingQuery

      public static <T> void executeStreamingQuery(org.springframework.jdbc.core.JdbcOperations jdbcOps, FilteredResultsProcessor<T> processor, org.springframework.jdbc.core.PreparedStatementCreator sql, org.springframework.jdbc.core.RowMapper<T> mapper) throws IOException
      Execute a streaming query.
      Type Parameters:
      T - the entity type
      Parameters:
      jdbcOps - the JDBC operations
      processor - the stream processor
      sql - the prepared statement creator
      mapper - the row mapper
      Throws:
      IOException - if any IO error occurs
      Since:
      1.2
    • executeStreamingQuery

      public static <T> void executeStreamingQuery(org.springframework.jdbc.core.JdbcOperations jdbcOps, FilteredResultsProcessor<T> processor, org.springframework.jdbc.core.PreparedStatementCreator sql, org.springframework.jdbc.core.RowMapper<T> mapper, Long totalResultCount, Integer startingOffset, Integer expectedResultCount, Map<String,?> attributes) throws IOException
      Execute a streaming query.
      Type Parameters:
      T - the entity type
      Parameters:
      jdbcOps - the JDBC operations
      processor - the stream processor
      sql - the prepared statement creator
      mapper - the row mapper
      totalResultCount - the total result count (or null)
      startingOffset - the starting offset (or null)
      expectedResultCount - the expected result count (or null)
      attributes - the attributes (or null)
      Throws:
      IOException - if any IO error occurs
      Since:
      1.2
    • updateWithGeneratedLong

      public static Long updateWithGeneratedLong(org.springframework.jdbc.core.JdbcOperations jdbcTemplate, org.springframework.jdbc.core.PreparedStatementCreator sql, String keyColumnName)
      Perform an update operation and extract a generated Long key.
      Parameters:
      jdbcTemplate - the JDBC template to use
      sql - the SQL to execute
      keyColumnName - the name of the generated key column to extract
      Returns:
      the generated key value, or null if the key is not returned or is not a Long instance
      Since:
      1.1
    • getCodedValueSet

      public static <T extends Enum<T> & net.solarnetwork.domain.CodedValue> Set<T> getCodedValueSet(ResultSet rs, int colNum, Class<T> clazz) throws SQLException
      Get a set of enumerated coded values from a JDBC array.
      Type Parameters:
      T - the coded value type
      Parameters:
      rs - the result set
      colNum - the column number
      clazz - the enum class
      Throws:
      SQLException - if any SQL error occurs
      Since:
      1.1
    • getTimestampInstant

      public static Instant getTimestampInstant(ResultSet rs, int column) throws SQLException
      Get an Timestamp column value as an Instant.
      Parameters:
      rs - the result set to read from
      column - the column number to get as a UUID
      Returns:
      the instant, or null if the column value is null
      Throws:
      SQLException - if an error occurs
      Since:
      1.3