Class CommonJdbcUtils
java.lang.Object
net.solarnetwork.central.common.dao.jdbc.sql.CommonJdbcUtils
Common JDBC utilities.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TarrayValue(Object o) Get an array value from a SQL array instance.static LongexecuteCountQuery(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> voidexecuteStreamingQuery(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> voidexecuteStreamingQuery(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> TGet an array result column value.getCodedValueSet(ResultSet rs, int colNum, Class<T> clazz) Get a set of enumerated coded values from a JDBC array.static InstantgetTimestampInstant(ResultSet rs, int column) Get an Timestamp column value as an Instant.static UUIDGet a UUID column value.static LongupdateWithGeneratedLong(org.springframework.jdbc.core.JdbcOperations jdbcTemplate, org.springframework.jdbc.core.PreparedStatementCreator sql, String keyColumnName) Perform an update operation and extract a generatedLongkey.
-
Method Details
-
getArray
Get an array result column value.- Type Parameters:
T- the expected array type- Parameters:
rs- the result setcolNum- the column number- Returns:
- the array
- Throws:
SQLException- if any SQL error occursClassCastException- if a casting error occurs
-
arrayValue
Get an array value from a SQL array instance.- Type Parameters:
T- the expected array type, e.g.Long[].class- Parameters:
o- theArrayinstance- Returns:
- the array value, or null if
ois null or not aArray - Throws:
ClassCastException- if a casting error occurs
-
getUuid
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 calltoString()on the column value and parse that as a UUID.- Parameters:
rs- the result set to read fromcolumn- the column number to get as a UUID- Returns:
- the UUID, or null if the column value is null
- Throws:
SQLException- if an error occursIllegalArgumentException- if the column value is non-null but does not conform to the string representation as described inUUID.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 usecreator- the statement creator; if implementsCountPreparedStatementCreatorProviderthenCountPreparedStatementCreatorProvider.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 typeK- the filter result key type- Parameters:
jdbcTemplate- the JDBC template to usefilter- the pagination criteriasql- the SQL to executemapper- 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 operationsprocessor- the stream processorsql- the prepared statement creatormapper- 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 IOExceptionExecute a streaming query.- Type Parameters:
T- the entity type- Parameters:
jdbcOps- the JDBC operationsprocessor- the stream processorsql- the prepared statement creatormapper- the row mappertotalResultCount- 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 generatedLongkey.- Parameters:
jdbcTemplate- the JDBC template to usesql- the SQL to executekeyColumnName- 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
Longinstance - 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 setcolNum- the column numberclazz- the enum class- Throws:
SQLException- if any SQL error occurs- Since:
- 1.1
-
getTimestampInstant
Get an Timestamp column value as an Instant.- Parameters:
rs- the result set to read fromcolumn- 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
-