Class CommonSqlUtils

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

public final class CommonSqlUtils extends Object
Common SQL utilities for SolarNetwork.
  • Field Details

    • SQL_COMMENT

      public static final Pattern SQL_COMMENT
      Regex for a line starting with a -- SQL style comment character.
    • WHERE_COMPONENT_PREFIX_LENGTH

      public static final int WHERE_COMPONENT_PREFIX_LENGTH
      The number of characters to drop from the start of a leading where clause.
      See Also:
  • Constructor Details

    • CommonSqlUtils

      public CommonSqlUtils()
  • Method Details

    • prepareOptimizedArrayParameter

      public static int prepareOptimizedArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, Integer[] value) throws SQLException
      Prepare a SQL statement INTEGER array parameter, optimized to a non-array parameter if the array holds a single object.

      The parameter will not be set if value is null.

      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.4
    • prepareOptimizedArrayParameter

      public static int prepareOptimizedArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, Long[] value) throws SQLException
      Prepare a SQL statement BIGINT array parameter, optimized to a non-array parameter if the array holds a single object.

      The parameter will not be set if value is null.

      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
    • prepareOptimizedArrayParameter

      public static int prepareOptimizedArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, Long[] value, boolean setNull) throws SQLException
      Prepare a SQL statement BIGINT array parameter, optimized to a non-array parameter if the array holds a single object.
      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      setNull - true to set a NULL parameter if value is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.2
    • prepareOptimizedArrayParameter

      public static int prepareOptimizedArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, String[] value) throws SQLException
      Prepare a SQL statement TEXT array parameter, optimized to a non-array parameter if the array holds a single object.

      The parameter will not be set if value is null.

      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
    • prepareOptimizedArrayParameter

      public static int prepareOptimizedArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, String[] value, boolean setNull) throws SQLException
      Prepare a SQL statement TEXT array parameter, optimized to a non-array parameter if the array holds a single object.
      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      setNull - true to set a NULL parameter if value is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.2
    • prepareOptimizedArrayParameter

      public static int prepareOptimizedArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, UUID[] value) throws SQLException
      Prepare a SQL statement UUID array parameter, optimized to a non-array parameter if the array holds a single object.

      The parameter will not be set if value is null.

      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.6
    • prepareOptimizedArrayParameter

      public static int prepareOptimizedArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, UUID[] value, boolean setNull) throws SQLException
      Prepare a SQL statement UUID array parameter, optimized to a non-array parameter if the array holds a single object.
      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      setNull - true to set a NULL parameter if value is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.6
    • prepareOptimizedArrayParameter

      public static int prepareOptimizedArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, String arrayType, Object[] value) throws SQLException
      Prepare a SQL statement array parameter, optimized to a non-array parameter if the array holds a single object.

      The parameter will not be set if value is null.

      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      arrayType - the SQL array type to use
      value - the array value
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
    • prepareOptimizedArrayParameter

      public static int prepareOptimizedArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, String arrayType, Object[] value, boolean setNull) throws SQLException
      Prepare a SQL statement array parameter, optimized to a non-array parameter if the array holds a single object.
      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      arrayType - the SQL array type to use
      value - the array value
      setNull - true to set a NULL parameter if value is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.2
    • prepareArrayParameter

      public static int prepareArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, Integer[] value) throws SQLException
      Prepare a SQL statement INTEGER array parameter.

      The parameter will not be set if value is null.

      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.1
    • prepareArrayParameter

      public static int prepareArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, Integer[] value, boolean setNull) throws SQLException
      Prepare a SQL statement INTEGER array parameter.
      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      setNull - true to set a NULL parameter if value is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.2
    • prepareArrayParameter

      public static int prepareArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, Long[] value) throws SQLException
      Prepare a SQL statement BIGINT array parameter.

      The parameter will not be set if value is null.

      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.1
    • prepareArrayParameter

      public static int prepareArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, Long[] value, boolean setNull) throws SQLException
      Prepare a SQL statement BIGINT array parameter.
      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      setNull - true to set a NULL parameter if value is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.2
    • prepareArrayParameter

      public static int prepareArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, String[] value) throws SQLException
      Prepare a SQL statement TEXT array parameter.

      The parameter will not be set if value is null.

      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.1
    • prepareArrayParameter

      public static int prepareArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, String[] value, boolean setNull) throws SQLException
      Prepare a SQL statement TEXT array parameter.
      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      setNull - true to set a NULL parameter if value is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.2
    • prepareArrayParameter

      public static int prepareArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, UUID[] value) throws SQLException
      Prepare a SQL statement UUID array parameter.

      The parameter will not be set if value is null.

      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.6
    • prepareArrayParameter

      public static int prepareArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, UUID[] value, boolean setNull) throws SQLException
      Prepare a SQL statement UUID array parameter.
      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the array value
      setNull - true to set a NULL parameter if value is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.6
    • prepareArrayParameter

      public static int prepareArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, String arrayType, Object[] value) throws SQLException
      Prepare a SQL statement array parameter.

      The parameter will not be set if value is null.

      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      arrayType - the SQL array type to use
      value - the array value
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.1
    • prepareArrayParameter

      public static int prepareArrayParameter(Connection con, PreparedStatement stmt, int parameterOffset, String arrayType, Object[] value, boolean setNull) throws SQLException
      Prepare a SQL statement array parameter.
      Parameters:
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      arrayType - the SQL array type to use
      value - the array value
      setNull - true to set a NULL parameter if value is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.2
    • forUpdate

      public static void forUpdate(boolean skipLocked, StringBuilder buf)
      Generate SQL FOR UPDATE SKIP LOCKED criteria to support locking.
      Parameters:
      skipLocked - true to include the SKIP LOCKED clause
      buf - the buffer to append the SQL to
    • forUpdate

      public static void forUpdate(boolean skipLocked, String[] tableNames, StringBuilder buf)
      Generate SQL FOR UPDATE SKIP LOCKED criteria to support locking.
      Parameters:
      skipLocked - true to include the SKIP LOCKED clause
      tableNames - explicit table names to lock, or null for default (all referenced tables)
      buf - the buffer to append the SQL to
      Since:
      2.2
    • limitOffset

      public static int limitOffset(net.solarnetwork.dao.PaginationCriteria filter, StringBuilder buf)
      Generate SQL LIMIT x OFFSET y criteria to support pagination.

      The buffer is populated with a pattern of \nLIMIT ? OFFSET ?.

      Parameters:
      filter - the search criteria
      buf - the buffer to append the SQL to
      Returns:
      the number of JDBC query parameters generated
    • prepareLimitOffset

      public static int prepareLimitOffset(net.solarnetwork.dao.PaginationCriteria filter, Connection con, PreparedStatement stmt, int parameterOffset) throws SQLException
      Prepare a SQL query limit/offset.
      Parameters:
      filter - the search criteria
      con - the JDBC connection
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.1
      See Also:
    • wrappedCountQuery

      public static String wrappedCountQuery(String sql)
      Wrap a SQL query with a SELECT COUNT(*) clause.
      Parameters:
      sql - the SQL query to wrap
      Returns:
      the wrapped query
      Since:
      2.1
    • limitOffsetLiteral

      public static void limitOffsetLiteral(net.solarnetwork.dao.PaginationCriteria filter, StringBuilder buf)
      Generate SQL LIMIT x OFFSET y criteria to support pagination where the limit and offset are generated as literal values.

      The buffer is populated with a pattern of \nLIMIT x OFFSET y.

      Parameters:
      filter - the search criteria
      buf - the buffer to append the SQL to
    • createPreparedStatement

      public static PreparedStatement createPreparedStatement(Connection con, String sql, boolean forUpdate) throws SQLException
      Create a PreparedStatement with settings appropriate for updating or not.
      Parameters:
      con - the connection
      sql - the SQL statement
      forUpdate - true to use a scroll-insensitive, updatable
      invalid reference
      ResetSet
      , or false for a forward-only, read-only one
      Returns:
      the prepared statement
      Throws:
      SQLException - if any SQL error occurs
    • whereArrayColContains

      public static int whereArrayColContains(Object[] array, String colName, StringBuilder buf)
      Generate SQL WHERE criteria for an array column containment clause.
      Parameters:
      array - the array value to match
      colName - the array SQL column name
      buf - the buffer to append the SQL to
      Returns:
      the number of JDBC query parameters generated
      Since:
      2.7
      See Also:
    • whereOptimizedArrayContains

      public static int whereOptimizedArrayContains(Object[] array, String colName, StringBuilder buf)
      Generate SQL WHERE criteria for an array containment clause.

      If array contains exactly one value, the generated SQL will use a simple equality comparison. Otherwise an ANY() comparison will be generated.

      Parameters:
      array - the array value to match
      colName - the array SQL column name
      buf - the buffer to append the SQL to
      Returns:
      the number of JDBC query parameters generated
      Since:
      2.1
      See Also:
    • whereDateRange

      public static int whereDateRange(net.solarnetwork.dao.DateRangeCriteria filter, String colName, StringBuilder buf)
      Generate SQL WHERE criteria for a date range.

      The buffer is populated with a pattern of \tAND c = ?\n for each clause. The leading tab and AND and space characters are not stripped.

      Parameters:
      filter - the search criteria
      buf - the buffer to append the SQL to
      column - the date column name to use, e.g. solardatum.ts
      Returns:
      the number of JDBC query parameters generated
      Since:
      2.1
    • prepareDateRange

      public static int prepareDateRange(net.solarnetwork.dao.DateRangeCriteria filter, PreparedStatement stmt, int parameterOffset) throws SQLException
      Prepare a SQL query date range filter.
      Parameters:
      filter - the search criteria
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.1
    • prepareJsonString

      public static int prepareJsonString(Object data, PreparedStatement stmt, int parameterOffset, boolean setNull) throws SQLException
      Prepare a SQL query JSON string parameter.
      Parameters:
      data - the JSON data; will be serialized via JsonUtils.getJSONString(Object, String)
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      setNull - true to set a NULL parameter if data serializes to null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.2
    • prepareCodedValue

      public static int prepareCodedValue(PreparedStatement stmt, int parameterOffset, net.solarnetwork.domain.CodedValue value, net.solarnetwork.domain.CodedValue defaultValue, boolean setNull) throws SQLException
      Prepare a SQL query CodedValue parameter as a INTEGER.
      Parameters:
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the value to set
      defaultValue - the value to set if value is null
      setNull - true to set a NULL parameter if value is null and defaultValue is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
    • prepareCodedValueChar

      public static int prepareCodedValueChar(PreparedStatement stmt, int parameterOffset, net.solarnetwork.domain.CodedValue value, net.solarnetwork.domain.CodedValue defaultValue, boolean setNull) throws SQLException
      Prepare a SQL query CodedValue parameter as a CHARACTER.
      Parameters:
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      value - the value to set
      defaultValue - the value to set if value is null
      setNull - true to set a NULL parameter if value is null and defaultValue is null, or false to skip the parameter
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.5
    • prepareCodedValuesArray

      public static int prepareCodedValuesArray(Connection con, PreparedStatement stmt, int parameterOffset, Collection<? extends net.solarnetwork.domain.CodedValue> values, boolean setNull) throws SQLException
      Prepare a SQL query CodedValue parameter as a INTEGER.
      Parameters:
      stmt - the JDBC statement
      parameterOffset - the zero-based starting JDBC statement parameter offset
      setNull - true to set a NULL parameter if value is null and defaultValue is null, or false to skip the parameter
      value - the value to set
      defaultValue - the value to set if value is null
      Returns:
      the new JDBC statement parameter offset
      Throws:
      SQLException - if any SQL error occurs
      Since:
      2.2
    • orderBySorts

      public static int orderBySorts(Iterable<net.solarnetwork.domain.SortDescriptor> sorts, Map<String,String> sortKeyMapping, StringBuilder buf)
      Generate SQL ORDER BY criteria for a set of SortDescriptor.

      The buffer is populated with a pattern of , key for each key. The leading comma and space characters are not stripped, but the returned value indicates the number of characters to trim from the results if needed.

      Parameters:
      sorts - the sorts
      sortKeyMapping - the mapping of sort keys to SQL sort names
      buf - the buffer to append the SQL to
      Returns:
      the number of leading "joining" characters added to buf; will either be 0 or 2
      Since:
      2.3