Package org.dbunit.util
Class SQLHelper
java.lang.Object
org.dbunit.util.SQLHelper
Helper for SQL-related stuff.
TODO: testcases, also think about refactoring so that methods are not static anymore (for better extensibility)
TODO: testcases, also think about refactoring so that methods are not static anymore (for better extensibility)
- Since:
- Nov 5, 2005
- Version:
- $Revision$
- Author:
- Felipe Leme (dbunit@felipeal.net)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe database product name reported by Sybase JDBC drivers. -
Method Summary
Modifier and TypeMethodDescriptionstatic final booleanareEqualIgnoreNull(String value1, String value2, boolean caseSensitive) Compares the given values and returns true if they are equal.static voidCloses the given result set in a null-safe waystatic voidClose a result set and a prepared statement, checking for null references.static voidClose a SQL statement, checking for null references.static final StringcorrectCase(String databaseIdentifier, Connection connection) Corrects the case of the given String according to the way in which the database stores metadata.static final StringcorrectCase(String databaseIdentifier, DatabaseMetaData databaseMetaData) Corrects the case of the given String according to the way in which the database stores metadata.static final ColumncreateColumn(ResultSet resultSet, IDataTypeFactory dataTypeFactory, boolean datatypeWarning) static StringgetDatabaseInfo(DatabaseMetaData metaData) Returns the database and JDBC driver information as pretty formatted stringstatic StringgetPrimaryKeyColumn(Connection conn, String table) Gets the primary column for a table.static booleanisSybaseDb(DatabaseMetaData metaData) Detects whether or not the given metadata describes the connection to a Sybase database or not.static final voidlogDebugIfValueChanged(String oldValue, String newValue, String message, Class source) Checks whether two given values are unequal and if so print a log message (level DEBUG)static final voidlogInfoIfValueChanged(String oldValue, String newValue, String message, Class source) Checks whether two given values are unequal and if so print a log message (level DEBUG)static booleanDeprecated.static booleanmatches(ResultSet resultSet, String catalog, String schema, String table, String column, boolean caseSensitive) Deprecated.static voidprintAllTables(DatabaseMetaData metaData, PrintStream outputStream) Utility method for debugging to print all tables of the given metadata on the given streamstatic voidprintDatabaseInfo(DatabaseMetaData metaData, PrintStream outputStream) Prints the database and JDBC driver information to the given output streamstatic booleanschemaExists(Connection connection, String schema) Returnstrueif the given schema exists for the given connection.static booleantableExists(DatabaseMetaData metaData, String schema, String tableName) Deprecated.since 2.4.5 - useIMetadataHandler.tableExists(DatabaseMetaData, String, String)
-
Field Details
-
DB_PRODUCT_SYBASE
The database product name reported by Sybase JDBC drivers.- See Also:
-
-
Method Details
-
getPrimaryKeyColumn
Gets the primary column for a table.- Parameters:
conn- connection with the databasetable- table name- Returns:
- name of primary column for a table (assuming it's just 1 column).
- Throws:
SQLException- raised while getting the meta data
-
close
Close a result set and a prepared statement, checking for null references.- Parameters:
rs- result set to be closedstmt- prepared statement to be closed- Throws:
SQLException- exception raised in either close() method
-
close
Close a SQL statement, checking for null references.- Parameters:
stmt- statement to be closed- Throws:
SQLException- exception raised while closing the statement
-
close
Closes the given result set in a null-safe way- Parameters:
resultSet-- Throws:
SQLException
-
schemaExists
Returnstrueif the given schema exists for the given connection.- Parameters:
connection- The connection to a databaseschema- The schema to be searched- Returns:
- Returns
trueif the given schema exists for the given connection. - Throws:
SQLException- Since:
- 2.3.0
-
tableExists
public static boolean tableExists(DatabaseMetaData metaData, String schema, String tableName) throws SQLException Deprecated.since 2.4.5 - useIMetadataHandler.tableExists(DatabaseMetaData, String, String)Checks if the given table exists.- Parameters:
metaData- The database meta dataschema- The schema in which the table should be searched. Ifnullthe schema is not used to narrow the table name.tableName- The table name to be searched- Returns:
- Returns
trueif the given table exists in the given schema. Else returnsfalse. - Throws:
SQLException- Since:
- 2.3.0
-
printAllTables
public static void printAllTables(DatabaseMetaData metaData, PrintStream outputStream) throws SQLException Utility method for debugging to print all tables of the given metadata on the given stream- Parameters:
metaData-outputStream-- Throws:
SQLException
-
getDatabaseInfo
Returns the database and JDBC driver information as pretty formatted string- Parameters:
metaData- The JDBC database metadata needed to retrieve database information- Returns:
- The database information as formatted string
-
printDatabaseInfo
public static void printDatabaseInfo(DatabaseMetaData metaData, PrintStream outputStream) throws SQLException Prints the database and JDBC driver information to the given output stream- Parameters:
metaData- The JDBC database metadata needed to retrieve database informationoutputStream- The stream to which the information is printed- Throws:
SQLException
-
isSybaseDb
Detects whether or not the given metadata describes the connection to a Sybase database or not.- Parameters:
metaData- The metadata to be checked whether it is a Sybase connection- Returns:
trueif and only if the given metadata belongs to a Sybase database.- Throws:
SQLException
-
createColumn
public static final Column createColumn(ResultSet resultSet, IDataTypeFactory dataTypeFactory, boolean datatypeWarning) throws SQLException, DataTypeException - Parameters:
resultSet- A result set produced viaDatabaseMetaData.getColumns(String, String, String, String)dataTypeFactory- The factory used to lookup theDataTypefor this columndatatypeWarning- Whether or not a warning should be printed if the column could not be created because of an unknown datatype.- Returns:
- The
Columnornullif the column could not be initialized because of an unknown datatype. - Throws:
SQLExceptionDataTypeException- Since:
- 2.4.0
-
matches
public static boolean matches(ResultSet resultSet, String schema, String table, boolean caseSensitive) throws SQLException Deprecated.Checks if the givenresultSetmatches the given schema and table name. The comparison is case sensitive.- Parameters:
resultSet- A result set produced viaDatabaseMetaData.getColumns(String, String, String, String)schema- The name of the schema to check. Ifnullit is ignored in the comparisontable- The name of the table to check. Ifnullit is ignored in the comparisoncaseSensitive- Whether or not the comparison should be case sensitive or not- Returns:
trueif the column metadata of the givenresultSetmatches the given schema and table parameters.- Throws:
SQLException- Since:
- 2.4.0
-
matches
public static boolean matches(ResultSet resultSet, String catalog, String schema, String table, String column, boolean caseSensitive) throws SQLException Deprecated.Checks if the givenresultSetmatches the given schema and table name. The comparison is case sensitive.- Parameters:
resultSet- A result set produced viaDatabaseMetaData.getColumns(String, String, String, String)catalog- The name of the catalog to check. Ifnullit is ignored in the comparisonschema- The name of the schema to check. Ifnullit is ignored in the comparisontable- The name of the table to check. Ifnullit is ignored in the comparisoncolumn- The name of the column to check. Ifnullit is ignored in the comparisoncaseSensitive- Whether or not the comparison should be case sensitive or not- Returns:
trueif the column metadata of the givenresultSetmatches the given schema and table parameters.- Throws:
SQLException- Since:
- 2.4.0
-
areEqualIgnoreNull
Compares the given values and returns true if they are equal. If the first value isnullor empty String it always returnstruewhich is the way of ignoringnulls for this specific case.- Parameters:
value1- The first value to compare. Is ignored if null or empty Stringvalue2- The second value to be compared- Returns:
trueif both values are equal or if the first value isnullor empty string.- Since:
- 2.4.4
-
correctCase
Corrects the case of the given String according to the way in which the database stores metadata.- Parameters:
databaseIdentifier- A database identifier such as a table name or a schema name for which the case should be corrected.connection- The connection used to lookup the database metadata. This is needed to determine the way in which the database stores its metadata.- Returns:
- The database identifier in the correct case for the RDBMS
- Since:
- 2.4.4
-
correctCase
public static final String correctCase(String databaseIdentifier, DatabaseMetaData databaseMetaData) Corrects the case of the given String according to the way in which the database stores metadata.- Parameters:
databaseIdentifier- A database identifier such as a table name or a schema name for which the case should be corrected.databaseMetaData- The database metadata needed to determine the way in which the database stores its metadata.- Returns:
- The database identifier in the correct case for the RDBMS
- Since:
- 2.4.4
-
logInfoIfValueChanged
public static final void logInfoIfValueChanged(String oldValue, String newValue, String message, Class source) Checks whether two given values are unequal and if so print a log message (level DEBUG)- Parameters:
oldValue- The old value of a propertynewValue- The new value of a propertymessage- The message to be loggedsource- The class which invokes this method - used for enriching the log message- Since:
- 2.4.4
-
logDebugIfValueChanged
public static final void logDebugIfValueChanged(String oldValue, String newValue, String message, Class source) Checks whether two given values are unequal and if so print a log message (level DEBUG)- Parameters:
oldValue- The old value of a propertynewValue- The new value of a propertymessage- The message to be loggedsource- The class which invokes this method - used for enriching the log message- Since:
- 2.4.8
-
IMetadataHandler.matches(ResultSet, String, String, String, String, boolean)