Class SQLHelper

java.lang.Object
org.dbunit.util.SQLHelper

public class SQLHelper extends Object
Helper for SQL-related stuff.
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 Details

    • DB_PRODUCT_SYBASE

      public static final String DB_PRODUCT_SYBASE
      The database product name reported by Sybase JDBC drivers.
      See Also:
  • Method Details

    • getPrimaryKeyColumn

      public static String getPrimaryKeyColumn(Connection conn, String table) throws SQLException
      Gets the primary column for a table.
      Parameters:
      conn - connection with the database
      table - 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

      public static void close(ResultSet rs, Statement stmt) throws SQLException
      Close a result set and a prepared statement, checking for null references.
      Parameters:
      rs - result set to be closed
      stmt - prepared statement to be closed
      Throws:
      SQLException - exception raised in either close() method
    • close

      public static void close(Statement stmt) throws SQLException
      Close a SQL statement, checking for null references.
      Parameters:
      stmt - statement to be closed
      Throws:
      SQLException - exception raised while closing the statement
    • close

      public static void close(ResultSet resultSet) throws SQLException
      Closes the given result set in a null-safe way
      Parameters:
      resultSet -
      Throws:
      SQLException
    • schemaExists

      public static boolean schemaExists(Connection connection, String schema) throws SQLException
      Returns true if the given schema exists for the given connection.
      Parameters:
      connection - The connection to a database
      schema - The schema to be searched
      Returns:
      Returns true if 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
      Checks if the given table exists.
      Parameters:
      metaData - The database meta data
      schema - The schema in which the table should be searched. If null the schema is not used to narrow the table name.
      tableName - The table name to be searched
      Returns:
      Returns true if the given table exists in the given schema. Else returns false.
      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

      public static String getDatabaseInfo(DatabaseMetaData metaData)
      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 information
      outputStream - The stream to which the information is printed
      Throws:
      SQLException
    • isSybaseDb

      public static boolean isSybaseDb(DatabaseMetaData metaData) throws SQLException
      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:
      true if 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
      Utility method to create a Column object from a SQL ResultSet object.
      Parameters:
      resultSet - A result set produced via DatabaseMetaData.getColumns(String, String, String, String)
      dataTypeFactory - The factory used to lookup the DataType for this column
      datatypeWarning - Whether or not a warning should be printed if the column could not be created because of an unknown datatype.
      Returns:
      The Column or null if the column could not be initialized because of an unknown datatype.
      Throws:
      SQLException
      DataTypeException
      Since:
      2.4.0
    • matches

      public static boolean matches(ResultSet resultSet, String schema, String table, boolean caseSensitive) throws SQLException
      Checks if the given resultSet matches the given schema and table name. The comparison is case sensitive.
      Parameters:
      resultSet - A result set produced via DatabaseMetaData.getColumns(String, String, String, String)
      schema - The name of the schema to check. If null it is ignored in the comparison
      table - The name of the table to check. If null it is ignored in the comparison
      caseSensitive - Whether or not the comparison should be case sensitive or not
      Returns:
      true if the column metadata of the given resultSet matches 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
      Checks if the given resultSet matches the given schema and table name. The comparison is case sensitive.
      Parameters:
      resultSet - A result set produced via DatabaseMetaData.getColumns(String, String, String, String)
      catalog - The name of the catalog to check. If null it is ignored in the comparison
      schema - The name of the schema to check. If null it is ignored in the comparison
      table - The name of the table to check. If null it is ignored in the comparison
      column - The name of the column to check. If null it is ignored in the comparison
      caseSensitive - Whether or not the comparison should be case sensitive or not
      Returns:
      true if the column metadata of the given resultSet matches the given schema and table parameters.
      Throws:
      SQLException
      Since:
      2.4.0
    • areEqualIgnoreNull

      public static final boolean areEqualIgnoreNull(String value1, String value2, boolean caseSensitive)
      Compares the given values and returns true if they are equal. If the first value is null or empty String it always returns true which is the way of ignoring nulls for this specific case.
      Parameters:
      value1 - The first value to compare. Is ignored if null or empty String
      value2 - The second value to be compared
      Returns:
      true if both values are equal or if the first value is null or empty string.
      Since:
      2.4.4
    • correctCase

      public static final String correctCase(String databaseIdentifier, Connection connection)
      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 property
      newValue - The new value of a property
      message - The message to be logged
      source - 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 property
      newValue - The new value of a property
      message - The message to be logged
      source - The class which invokes this method - used for enriching the log message
      Since:
      2.4.8