Package net.solarnetwork.node.dao.jdbc
Class JdbcUtils
java.lang.Object
net.solarnetwork.node.dao.jdbc.JdbcUtils
Utilities to help with JDBC.
- Since:
- 1.17
- Version:
- 1.2
- Author:
- matt
-
Method Summary
Modifier and TypeMethodDescriptionstatic Map<String,ColumnCsvMetaData> columnCsvMetaDataForDatabaseMetaData(DatabaseMetaData meta, String tableName) Get a mapping of JDBC column names to associated column metadata from a JDBCDatabaseMetaDataobject.csvColumnIndexMapping(String[] header) Get a mapping of CVS column names to their associated position in an array.static org.supercsv.cellprocessor.ift.CellProcessor[]Get a set ofCellProcessorfor formatting ResultSet data as CSV strings.static String[]getBatchSqlResource(org.springframework.core.io.Resource sqlResource) Get batch SQL statements, split into multiple statements on the ; character.static StringgetSqlResource(String classPathResource, Class<?> resourceClass, String prefix, Map<String, String> sqlResourceCache) Load a classpath SQL resource into a String.static StringgetSqlResource(org.springframework.core.io.Resource resource) Load a SQL resource into a String.static StringinsertSqlForColumnCsvMetaData(String tableName, Map<String, ColumnCsvMetaData> columnMetaData) Get a SQL string for inserting into a table using column metadata.static org.supercsv.cellprocessor.ift.CellProcessor[]parsingCellProcessorsForCsvColumns(String[] csvColumns, Map<String, ColumnCsvMetaData> columnMetaData) Get a set ofCellProcessorfor parsing CSV strings into JDBC column objects.
-
Method Details
-
formattingProcessorsForResultSetMetaData
public static org.supercsv.cellprocessor.ift.CellProcessor[] formattingProcessorsForResultSetMetaData(ResultSetMetaData meta) throws SQLException Get a set ofCellProcessorfor 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 ofCellProcessorfor 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 viacolumnCsvMetaDataForDatabaseMetaData(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 JDBCDatabaseMetaDataobject.- 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. fromcolumnCsvMetaDataForDatabaseMetaData(DatabaseMetaData, String).- Returns:
- The SQL statement.
-
csvColumnIndexMapping
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
prefixvalue and-and theclassPathResourcecombined with a.sqlsuffix. 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-andclassPathResourceagain to try to find a match, until there is no prefix left and just theclassPathResourceitself is tried.This method will cache the SQL resource in the given
Mapfor quick future access.- Parameters:
classPathResource- the classpath resource to load as a SQL stringresourceClass- the class to load the resource forprefix- a dash-delimited prefix to add to the resource namesqlResourceCache- 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
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
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
-