org.unitils.core.dbsupport
Class DbSupport

java.lang.Object
  extended by org.unitils.core.dbsupport.DbSupport
Direct Known Subclasses:
Db2DbSupport, DerbyDbSupport, HsqldbDbSupport, MsSqlDbSupport, MySqlDbSupport, OracleDbSupport, PostgreSqlDbSupport

public abstract class DbSupport
extends Object

Helper class that implements a number of common operations on a database schema. Operations that can be implemented using general JDBC or ANSI SQL constructs, are impelemented in this base abstract class. Operations that are DBMS specific are abstract, and their implementation is left to DBMS specific subclasses.

Author:
Filip Neven, Tim Ducheyne, Frederick Beernaert

Field Summary
static String PROPKEY_IDENTIFIER_QUOTE_STRING
          Property key for the default identifier quote string (empty value for not supported, auto)
static String PROPKEY_STORED_IDENTIFIER_CASE
          Property key for the default identifier casing (lower_case, upper_case, mixed_case, auto)
 
Constructor Summary
protected DbSupport(String databaseDialect)
          Creates a new, unconfigured instance.
 
Method Summary
abstract  void disableReferentialConstraints()
          Disables all referential constraints (e.g. foreign keys) on all table in the schema
abstract  void disableValueConstraints()
          Disables all value constraints (e.g. not null) on all tables in the schema
 void dropMaterializedView(String viewName)
          Removes the materialized view with the given name from the database Note: the view name is surrounded with quotes, making it case-sensitive.
 void dropSequence(String sequenceName)
          Drops the sequence with the given name from the database Note: the sequence name is surrounded with quotes, making it case-sensitive.
 void dropSynonym(String synonymName)
          Removes the synonym with the given name from the database Note: the synonym name is surrounded with quotes, making it case-sensitive.
 void dropTable(String tableName)
          Removes the table with the given name from the database.
 void dropTrigger(String triggerName)
          Drops the trigger with the given name from the database Note: the trigger name is surrounded with quotes, making it case-sensitive.
 void dropType(String typeName)
          Drops the type with the given name from the database Note: the type name is surrounded with quotes, making it case-sensitive.
 void dropView(String viewName)
          Removes the view with the given name from the database Note: the view name is surrounded with quotes, making it case-sensitive.
abstract  Set<String> getColumnNames(String tableName)
          Gets the names of all columns of the given table.
 String getDatabaseDialect()
          Gets the database dialect.
 String getIdentifierQuoteString()
          Gets the identifier quote string.
 Set<String> getIdentityColumnNames(String tableName)
          Gets the names of all identity columns of the given table.
 String getLongDataType()
          Gets the column type suitable to store values of the Java java.lang.Long type.
 Set<String> getMaterializedViewNames()
          Retrieves the names of all materialized views in the database schema.
 String getSchemaName()
          Gets the schema name.
 Set<String> getSequenceNames()
          Retrieves the names of all sequences in the database schema.
 long getSequenceValue(String sequenceName)
          Returns the value of the sequence with the given name.
 SQLHandler getSQLHandler()
          Gets the sql handler.
 StoredIdentifierCase getStoredIdentifierCase()
          Gets the stored identifier case.
 Set<String> getSynonymNames()
          Retrieves the names of all synonyms in the database schema.
abstract  Set<String> getTableNames()
          Returns the names of all tables in the database.
 String getTextDataType(int length)
          Gets the column type suitable to store text values.
 Set<String> getTriggerNames()
          Retrieves the names of all triggers in the database schema.
 Set<String> getTypeNames()
          Retrieves the names of all types in the database schema.
abstract  Set<String> getViewNames()
          Retrieves the names of all the views in the database schema.
 void incrementIdentityColumnToValue(String tableName, String identityColumnName, long identityValue)
          Increments the identity value for the specified identity column on the specified table to the given value.
 void incrementSequenceToValue(String sequenceName, long newSequenceValue)
          Sets the next value of the sequence with the given sequence name to the given sequence value.
 void init(Properties configuration, SQLHandler sqlHandler, String schemaName)
          Initializes this DbSupport object with the given schemaName and dataSource.
 String qualified(String databaseObjectName)
          Qualifies the given database object name with the name of the database schema.
 String quoted(String databaseObjectName)
          Put quotes around the given databaseObjectName, if the underlying DBMS supports quoted database object names.
 boolean supportsCascade()
          Indicates whether the underlying DBMS supports the cascade option for dropping tables and views.
 boolean supportsIdentityColumns()
          Indicates whether the underlying DBMS supports identity columns
 boolean supportsMaterializedViews()
          Indicates whether the underlying DBMS supports materialized views
 boolean supportsSequences()
          Indicates whether the underlying DBMS supports sequences
 boolean supportsSynonyms()
          Indicates whether the underlying DBMS supports synonyms
 boolean supportsTriggers()
          Indicates whether the underlying DBMS supports triggers
 boolean supportsTypes()
          Indicates whether the underlying DBMS supports database types
 String toCorrectCaseIdentifier(String identifier)
          Converts the given identifier to uppercase/lowercase depending on the DBMS.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPKEY_STORED_IDENTIFIER_CASE

public static final String PROPKEY_STORED_IDENTIFIER_CASE
Property key for the default identifier casing (lower_case, upper_case, mixed_case, auto)

See Also:
Constant Field Values

PROPKEY_IDENTIFIER_QUOTE_STRING

public static final String PROPKEY_IDENTIFIER_QUOTE_STRING
Property key for the default identifier quote string (empty value for not supported, auto)

See Also:
Constant Field Values
Constructor Detail

DbSupport

protected DbSupport(String databaseDialect)
Creates a new, unconfigured instance. To have a instance that can be used, the init(java.util.Properties, org.unitils.core.dbsupport.SQLHandler, java.lang.String) method must be called first.

Parameters:
databaseDialect - The name of the DBMS implementation that is supported by this implementation, not null
Method Detail

init

public void init(Properties configuration,
                 SQLHandler sqlHandler,
                 String schemaName)
Initializes this DbSupport object with the given schemaName and dataSource. If the storedIdentifierCase or identifierQuoteString is set to null, the metadata of the connection will be used to determine the correct value.

Parameters:
configuration - The config, not null
sqlHandler - The sql handler, not null
schemaName - The name of the database schema

getDatabaseDialect

public String getDatabaseDialect()
Gets the database dialect.

Returns:
the supported dialect, not null

getSchemaName

public String getSchemaName()
Gets the schema name.

Returns:
the schema name, not null

getIdentifierQuoteString

public String getIdentifierQuoteString()
Gets the identifier quote string.

Returns:
the quote string, null if not supported

getStoredIdentifierCase

public StoredIdentifierCase getStoredIdentifierCase()
Gets the stored identifier case.

Returns:
the case, not null

getSQLHandler

public SQLHandler getSQLHandler()
Gets the sql handler.

Returns:
the data source, not null

getTableNames

public abstract Set<String> getTableNames()
Returns the names of all tables in the database.

Returns:
The names of all tables in the database

getColumnNames

public abstract Set<String> getColumnNames(String tableName)
Gets the names of all columns of the given table.

Parameters:
tableName - The table, not null
Returns:
The names of the columns of the table with the given name

getViewNames

public abstract Set<String> getViewNames()
Retrieves the names of all the views in the database schema.

Returns:
The names of all views in the database

getMaterializedViewNames

public Set<String> getMaterializedViewNames()
Retrieves the names of all materialized views in the database schema.

Returns:
The names of all materialized views in the database

getSynonymNames

public Set<String> getSynonymNames()
Retrieves the names of all synonyms in the database schema.

Returns:
The names of all synonyms in the database

getSequenceNames

public Set<String> getSequenceNames()
Retrieves the names of all sequences in the database schema.

Returns:
The names of all sequences in the database, not null

getTriggerNames

public Set<String> getTriggerNames()
Retrieves the names of all triggers in the database schema.

Returns:
The names of all triggers in the database, not null

getTypeNames

public Set<String> getTypeNames()
Retrieves the names of all types in the database schema.

Returns:
The names of all types in the database, not null

dropTable

public void dropTable(String tableName)
Removes the table with the given name from the database. Note: the table name is surrounded with quotes, making it case-sensitive.

Parameters:
tableName - The table to drop (case-sensitive), not null

dropView

public void dropView(String viewName)
Removes the view with the given name from the database Note: the view name is surrounded with quotes, making it case-sensitive.

Parameters:
viewName - The view to drop (case-sensitive), not null

dropMaterializedView

public void dropMaterializedView(String viewName)
Removes the materialized view with the given name from the database Note: the view name is surrounded with quotes, making it case-sensitive.

Parameters:
viewName - The view to drop (case-sensitive), not null

dropSynonym

public void dropSynonym(String synonymName)
Removes the synonym with the given name from the database Note: the synonym name is surrounded with quotes, making it case-sensitive.

Parameters:
synonymName - The synonym to drop (case-sensitive), not null

dropSequence

public void dropSequence(String sequenceName)
Drops the sequence with the given name from the database Note: the sequence name is surrounded with quotes, making it case-sensitive.

Parameters:
sequenceName - The sequence to drop (case-sensitive), not null

dropTrigger

public void dropTrigger(String triggerName)
Drops the trigger with the given name from the database Note: the trigger name is surrounded with quotes, making it case-sensitive.

Parameters:
triggerName - The trigger to drop (case-sensitive), not null

dropType

public void dropType(String typeName)
Drops the type with the given name from the database Note: the type name is surrounded with quotes, making it case-sensitive.

Parameters:
typeName - The type to drop (case-sensitive), not null

disableReferentialConstraints

public abstract void disableReferentialConstraints()
Disables all referential constraints (e.g. foreign keys) on all table in the schema


disableValueConstraints

public abstract void disableValueConstraints()
Disables all value constraints (e.g. not null) on all tables in the schema


getSequenceValue

public long getSequenceValue(String sequenceName)
Returns the value of the sequence with the given name.

Note: this can have the side-effect of increasing the sequence value.

Parameters:
sequenceName - The sequence, not null
Returns:
The value of the sequence with the given name

incrementSequenceToValue

public void incrementSequenceToValue(String sequenceName,
                                     long newSequenceValue)
Sets the next value of the sequence with the given sequence name to the given sequence value.

Parameters:
sequenceName - The sequence, not null
newSequenceValue - The value to set

getIdentityColumnNames

public Set<String> getIdentityColumnNames(String tableName)
Gets the names of all identity columns of the given table.

Parameters:
tableName - The table, not null
Returns:
The names of the identity columns of the table with the given name

incrementIdentityColumnToValue

public void incrementIdentityColumnToValue(String tableName,
                                           String identityColumnName,
                                           long identityValue)
Increments the identity value for the specified identity column on the specified table to the given value. If there is no identity specified on the given primary key, the method silently finishes without effect.

Parameters:
tableName - The table with the identity column, not null
identityColumnName - The column, not null
identityValue - The new value

getLongDataType

public String getLongDataType()
Gets the column type suitable to store values of the Java java.lang.Long type.

Returns:
The column type

getTextDataType

public String getTextDataType(int length)
Gets the column type suitable to store text values.

Parameters:
length - The nr of characters.
Returns:
The column type, not null

qualified

public String qualified(String databaseObjectName)
Qualifies the given database object name with the name of the database schema. Quotes are put around both schemaname and object name. If the schemaName is not supplied, the database object is returned surrounded with quotes. If the DBMS doesn't support quoted database object names, no quotes are put around neither schema name nor database object name.

Parameters:
databaseObjectName - The database object name to be qualified
Returns:
The qualified database object name

quoted

public String quoted(String databaseObjectName)
Put quotes around the given databaseObjectName, if the underlying DBMS supports quoted database object names. If not, the databaseObjectName is returned unchanged.

Parameters:
databaseObjectName - The name, not null
Returns:
Quoted version of the given databaseObjectName, if supported by the underlying DBMS

toCorrectCaseIdentifier

public String toCorrectCaseIdentifier(String identifier)
Converts the given identifier to uppercase/lowercase depending on the DBMS. If a value is surrounded with double quotes (") and the DBMS supports quoted database object names, the case is left untouched and the double quotes are stripped. These values are treated as case sensitive names.

Identifiers can be prefixed with schema names. These schema names will be converted in the same way as described above. Quoting the schema name will make it case sensitive. Examples:

mySchema.myTable -> MYSCHEMA.MYTABLE "mySchema".myTable -> mySchema.MYTABLE "mySchema"."myTable" -> mySchema.myTable

Parameters:
identifier - The identifier, not null
Returns:
The name converted to correct case if needed, not null

supportsSynonyms

public boolean supportsSynonyms()
Indicates whether the underlying DBMS supports synonyms

Returns:
True if synonyms are supported, false otherwise

supportsSequences

public boolean supportsSequences()
Indicates whether the underlying DBMS supports sequences

Returns:
True if sequences are supported, false otherwise

supportsTriggers

public boolean supportsTriggers()
Indicates whether the underlying DBMS supports triggers

Returns:
True if triggers are supported, false otherwise

supportsTypes

public boolean supportsTypes()
Indicates whether the underlying DBMS supports database types

Returns:
True if types are supported, false otherwise

supportsIdentityColumns

public boolean supportsIdentityColumns()
Indicates whether the underlying DBMS supports identity columns

Returns:
True if identity is supported, false otherwise

supportsMaterializedViews

public boolean supportsMaterializedViews()
Indicates whether the underlying DBMS supports materialized views

Returns:
True if materialized views are supported, false otherwise

supportsCascade

public boolean supportsCascade()
Indicates whether the underlying DBMS supports the cascade option for dropping tables and views.

Returns:
True if cascade is supported, false otherwise


Copyright © 2011. All Rights Reserved.