Class JdbcUtils

java.lang.Object
net.solarnetwork.node.dao.jdbc.JdbcUtils

public abstract class JdbcUtils extends Object
Utilities to help with JDBC.
Since:
1.17
Version:
1.2
Author:
matt
  • Method Details

    • formattingProcessorsForResultSetMetaData

      public static org.supercsv.cellprocessor.ift.CellProcessor[] formattingProcessorsForResultSetMetaData(ResultSetMetaData meta) throws SQLException
      Get a set of CellProcessor for formatting ResultSet data as CSV strings.
      Parameters:
      meta - The metadata.
      Returns:
      The processors.
      Throws:
      SQLException - If any SQL error occurs.
    • parsingCellProcessorsForCsvColumns

      public static org.supercsv.cellprocessor.ift.CellProcessor[] parsingCellProcessorsForCsvColumns(String[] csvColumns, Map<String,ColumnCsvMetaData> columnMetaData)
      Get a set of CellProcessor for parsing CSV strings into JDBC column objects.
      Parameters:
      csvColumns - The parsed CSV column names (i.e. from the header row).
      columnMetaData - JDBC column metadata (i.e. extracted from JDBC via columnCsvMetaDataForDatabaseMetaData(DatabaseMetaData, String))
      Returns:
      The cell processors.
    • columnCsvMetaDataForDatabaseMetaData

      public static Map<String,ColumnCsvMetaData> columnCsvMetaDataForDatabaseMetaData(DatabaseMetaData meta, String tableName) throws SQLException
      Get a mapping of JDBC column names to associated column metadata from a JDBC DatabaseMetaData object.
      Parameters:
      meta - The database metadata to read from.
      tableName - The table name to get column metadata for.
      Returns:
      The metadata.
      Throws:
      SQLException - If any SQL error occurs.
    • insertSqlForColumnCsvMetaData

      public static String insertSqlForColumnCsvMetaData(String tableName, Map<String,ColumnCsvMetaData> columnMetaData)
      Get a SQL string for inserting into a table using column metadata.
      Parameters:
      tableName - The table name to insert into.
      columnMetaData - The column metadata of that table, i.e. from columnCsvMetaDataForDatabaseMetaData(DatabaseMetaData, String).
      Returns:
      The SQL statement.
    • csvColumnIndexMapping

      public static Map<String,Integer> csvColumnIndexMapping(String[] header)
      Get a mapping of CVS column names to their associated position in an array.
      Parameters:
      header - The parsed CVS column header row.
      Returns:
      The mapping of headers. The iteration order preserves the order of the array.
    • getSqlResource

      public static String getSqlResource(String classPathResource, Class<?> resourceClass, String prefix, Map<String,String> sqlResourceCache)
      Load a classpath SQL resource into a String.

      The classpath resource is taken as the prefix value and - and the classPathResource combined with a .sql suffix. If that resource is not found, then the prefix is split into components separated by a - character, and the last component is dropped and then combined with - and classPathResource again to try to find a match, until there is no prefix left and just the classPathResource itself is tried.

      This method will cache the SQL resource in the given Map for quick future access.

      Parameters:
      classPathResource - the classpath resource to load as a SQL string
      resourceClass - the class to load the resource for
      prefix - a dash-delimited prefix to add to the resource name
      sqlResourceCache - a cache to use for the loaded resource
      Returns:
      the SQL as a string
      Throws:
      RuntimeException - if the SQL resource cannot be loaded
      Since:
      1.2
    • getSqlResource

      public static String getSqlResource(org.springframework.core.io.Resource resource)
      Load a SQL resource into a String.
      Parameters:
      resource - the SQL resource to load
      Returns:
      the String
      Throws:
      RuntimeException - if the resource cannot be loaded
      Since:
      1.2
    • getBatchSqlResource

      public static String[] getBatchSqlResource(org.springframework.core.io.Resource sqlResource)
      Get batch SQL statements, split into multiple statements on the ; character.
      Parameters:
      sqlResource - the SQL resource to load
      Returns:
      the split SQL statements
      Throws:
      RuntimeException - if the resource cannot be loaded
      Since:
      1.2