org.unitils.dbmaintainer.clean.impl
Class DefaultDBClearer

java.lang.Object
  extended by org.unitils.dbmaintainer.util.BaseDatabaseAccessor
      extended by org.unitils.dbmaintainer.clean.impl.DefaultDBClearer
All Implemented Interfaces:
DBClearer, DatabaseAccessing

public class DefaultDBClearer
extends BaseDatabaseAccessor
implements DBClearer

Implementation of DBClearer. This implementation individually drops every table, view, constraint, trigger and sequence in the database. A list of tables, views, ... that should be preserverd can be specified using the property PROPKEY_PRESERVE_TABLES.

NOTE: FK constraints give problems in MySQL and Derby The cascade in drop table A cascade; does not work in MySQL-5.0 The DBMaintainer will first remove all constraints before calling the db clearer

Author:
Tim Ducheyne, Filip Neven

Field Summary
protected  Map<String,Set<String>> materializedViewsToPreserve
          Names of materialized views that should not be dropped per schema.
static String PROPKEY_PRESERVE_MATERIALIZED_VIEWS
          The key of the property that specifies which materialized views should not be dropped
static String PROPKEY_PRESERVE_SCHEMAS
          The key of the property that specifies of which schemas nothing should be dropped
static String PROPKEY_PRESERVE_SEQUENCES
          The key of the property that specifies which sequences should not be dropped
static String PROPKEY_PRESERVE_SYNONYMS
          The key of the property that specifies which synonyms should not be dropped
static String PROPKEY_PRESERVE_TABLES
          The key of the property that specifies which tables should not be dropped
static String PROPKEY_PRESERVE_TRIGGERS
          The key of the property that specifies which triggers should not be dropped
static String PROPKEY_PRESERVE_TYPES
          The key of the property that specifies which types should not be dropped
static String PROPKEY_PRESERVE_VIEWS
          The key of the property that specifies which views should not be dropped
static String PROPKEY_VERSION_TABLE_NAME
          The key of the property that specifies the name of the datase table in which the DB version is stored.
protected  Set<String> schemasToPreserve
          Names of schemas that should left untouched.
protected  Map<String,Set<String>> sequencesToPreserve
          Names of sequences that should not be dropped per schema.
protected  Map<String,Set<String>> synonymsToPreserve
          Names of synonyms that should not be dropped per schema.
protected  Map<String,Set<String>> tablesToPreserve
          Names of tables that should not be dropped per schema.
protected  Map<String,Set<String>> triggersToPreserve
          Names of triggers that should not be dropped per schema.
protected  Map<String,Set<String>> typesToPreserve
          Names of types that should not be dropped per schema.
protected  Map<String,Set<String>> viewsToPreserve
          Names of views that should not be dropped per schema.
 
Fields inherited from class org.unitils.dbmaintainer.util.BaseDatabaseAccessor
configuration, dbSupports, defaultDbSupport, sqlHandler
 
Constructor Summary
DefaultDBClearer()
           
 
Method Summary
 void clearSchemas()
          Clears the database schemas.
protected  void doInit(Properties configuration)
          Initializes the the DBClearer.
protected  void dropMaterializedViews(DbSupport dbSupport)
          Drops all materialized views.
protected  void dropSequences(DbSupport dbSupport)
          Drops all sequences
protected  void dropSynonyms(DbSupport dbSupport)
          Drops all synonyms
protected  void dropTables(DbSupport dbSupport)
          Drops all tables.
protected  void dropTriggers(DbSupport dbSupport)
          Drops all triggers
protected  void dropTypes(DbSupport dbSupport)
          Drops all types.
protected  void dropViews(DbSupport dbSupport)
          Drops all views.
protected  Map<String,Set<String>> getItemsToPreserve(String propertyName)
          Gets the list of items to preserve per schema.
protected  Map<String,Set<String>> getMaterializedViewsToPreserve()
          Gets the list of all materialized views to preserve per schema.
protected  Set<String> getSchemasToPreserve(Properties configuration)
          Gets the list of all schemas to preserve.
protected  Map<String,Set<String>> getSequencesToPreserve()
          Gets the list of all sequences to preserve per schema.
protected  Map<String,Set<String>> getSynonymsToPreserve()
          Gets the list of all synonym to preserve per schema.
protected  Map<String,Set<String>> getTablesToPreserve()
          Gets the list of all tables to preserve per schema.
protected  Map<String,Set<String>> getTriggersToPreserve()
          Gets the list of all triggers to preserve per schema.
protected  Map<String,Set<String>> getTypesToPreserve()
          Gets the list of all types to preserve per schema.
protected  Map<String,Set<String>> getViewsToPreserve()
          Gets the list of all views to preserve per schema.
protected  boolean isItemToPreserve(String item, Set<String> itemsToPreserve)
          Checks whether the given item is one of the items to preserve.
protected  boolean itemToPreserveExists(String itemToPreserve, Set<String> items)
          Checks whether the given item to preserve is one of the items.
 
Methods inherited from class org.unitils.dbmaintainer.util.BaseDatabaseAccessor
getDbSupport, init
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.unitils.dbmaintainer.util.DatabaseAccessing
init
 

Field Detail

PROPKEY_PRESERVE_SCHEMAS

public static final String PROPKEY_PRESERVE_SCHEMAS
The key of the property that specifies of which schemas nothing should be dropped

See Also:
Constant Field Values

PROPKEY_PRESERVE_TABLES

public static final String PROPKEY_PRESERVE_TABLES
The key of the property that specifies which tables should not be dropped

See Also:
Constant Field Values

PROPKEY_PRESERVE_VIEWS

public static final String PROPKEY_PRESERVE_VIEWS
The key of the property that specifies which views should not be dropped

See Also:
Constant Field Values

PROPKEY_PRESERVE_MATERIALIZED_VIEWS

public static final String PROPKEY_PRESERVE_MATERIALIZED_VIEWS
The key of the property that specifies which materialized views should not be dropped

See Also:
Constant Field Values

PROPKEY_PRESERVE_SYNONYMS

public static final String PROPKEY_PRESERVE_SYNONYMS
The key of the property that specifies which synonyms should not be dropped

See Also:
Constant Field Values

PROPKEY_PRESERVE_SEQUENCES

public static final String PROPKEY_PRESERVE_SEQUENCES
The key of the property that specifies which sequences should not be dropped

See Also:
Constant Field Values

PROPKEY_PRESERVE_TRIGGERS

public static final String PROPKEY_PRESERVE_TRIGGERS
The key of the property that specifies which triggers should not be dropped

See Also:
Constant Field Values

PROPKEY_PRESERVE_TYPES

public static final String PROPKEY_PRESERVE_TYPES
The key of the property that specifies which types should not be dropped

See Also:
Constant Field Values

PROPKEY_VERSION_TABLE_NAME

public static final String PROPKEY_VERSION_TABLE_NAME
The key of the property that specifies the name of the datase table in which the DB version is stored. This table should not be deleted

See Also:
Constant Field Values

schemasToPreserve

protected Set<String> schemasToPreserve
Names of schemas that should left untouched.


tablesToPreserve

protected Map<String,Set<String>> tablesToPreserve
Names of tables that should not be dropped per schema.


viewsToPreserve

protected Map<String,Set<String>> viewsToPreserve
Names of views that should not be dropped per schema.


materializedViewsToPreserve

protected Map<String,Set<String>> materializedViewsToPreserve
Names of materialized views that should not be dropped per schema.


synonymsToPreserve

protected Map<String,Set<String>> synonymsToPreserve
Names of synonyms that should not be dropped per schema.


sequencesToPreserve

protected Map<String,Set<String>> sequencesToPreserve
Names of sequences that should not be dropped per schema.


triggersToPreserve

protected Map<String,Set<String>> triggersToPreserve
Names of triggers that should not be dropped per schema.


typesToPreserve

protected Map<String,Set<String>> typesToPreserve
Names of types that should not be dropped per schema.

Constructor Detail

DefaultDBClearer

public DefaultDBClearer()
Method Detail

doInit

protected void doInit(Properties configuration)
Initializes the the DBClearer. The list of database items that should be preserved is retrieved from the given Configuration object.

Overrides:
doInit in class BaseDatabaseAccessor
Parameters:
configuration - the config, not null

clearSchemas

public void clearSchemas()
Clears the database schemas. This means, all the tables, views, constraints, triggers and sequences are dropped, so that the database schema is empty. The database items that are configured as items to preserve, are left untouched.

Specified by:
clearSchemas in interface DBClearer

dropTables

protected void dropTables(DbSupport dbSupport)
Drops all tables.

Parameters:
dbSupport - The database support, not null

dropViews

protected void dropViews(DbSupport dbSupport)
Drops all views.

Parameters:
dbSupport - The database support, not null

dropMaterializedViews

protected void dropMaterializedViews(DbSupport dbSupport)
Drops all materialized views.

Parameters:
dbSupport - The database support, not null

dropSynonyms

protected void dropSynonyms(DbSupport dbSupport)
Drops all synonyms

Parameters:
dbSupport - The database support, not null

dropSequences

protected void dropSequences(DbSupport dbSupport)
Drops all sequences

Parameters:
dbSupport - The database support, not null

dropTriggers

protected void dropTriggers(DbSupport dbSupport)
Drops all triggers

Parameters:
dbSupport - The database support, not null

dropTypes

protected void dropTypes(DbSupport dbSupport)
Drops all types.

Parameters:
dbSupport - The database support, not null

getSchemasToPreserve

protected Set<String> getSchemasToPreserve(Properties configuration)
Gets the list of all schemas to preserve. The case is corrected if necesary. Quoting a schema name makes it case sensitive.

If a schema name is not defined in the unitils configuration, a UnitilsException is thrown.

Parameters:
configuration - The unitils configuration, not null
Returns:
The schemas to preserve, not null

getTablesToPreserve

protected Map<String,Set<String>> getTablesToPreserve()
Gets the list of all tables to preserve per schema. The case is corrected if necesary. Quoting a table name makes it case sensitive. If no schema is specified, the tables will be added to the default schema name set.

If a table to preserve does not exist, a UnitilsException is thrown.

The db version table is also added as a table to preserve, but will not be checked on existence.

Returns:
The tables to preserve per schema, not null

getViewsToPreserve

protected Map<String,Set<String>> getViewsToPreserve()
Gets the list of all views to preserve per schema. The case is corrected if necesary. Quoting a view name makes it case sensitive. If no schema is specified, the view will be added to the default schema name set.

If a view to preserve does not exist, a UnitilsException is thrown.

Returns:
The views to preserve per schema, not null

getMaterializedViewsToPreserve

protected Map<String,Set<String>> getMaterializedViewsToPreserve()
Gets the list of all materialized views to preserve per schema. The case is corrected if necesary. Quoting a view name makes it case sensitive. If no schema is specified, the view will be added to the default schema name set.

If a view to preserve does not exist, a UnitilsException is thrown.

Returns:
The materialized views to preserve per schema, not null

getSequencesToPreserve

protected Map<String,Set<String>> getSequencesToPreserve()
Gets the list of all sequences to preserve per schema. The case is corrected if necesary. Quoting a sequence name makes it case sensitive. If no schema is specified, the sequence will be added to the default schema name set.

If a sequence to preserve does not exist, a UnitilsException is thrown.

Returns:
The sequences to preserve per schema, not null

getSynonymsToPreserve

protected Map<String,Set<String>> getSynonymsToPreserve()
Gets the list of all synonym to preserve per schema. The case is corrected if necesary. Quoting a synonym name makes it case sensitive. If no schema is specified, the synonym will be added to the default schema name set.

If a synonym to preserve does not exist, a UnitilsException is thrown.

Returns:
The synonym to preserve per schema, not null

getTriggersToPreserve

protected Map<String,Set<String>> getTriggersToPreserve()
Gets the list of all triggers to preserve per schema. The case is corrected if necesary. Quoting a trigger name makes it case sensitive. If no schema is specified, the trigger will be added to the default schema name set.

If a trigger to preserve does not exist, a UnitilsException is thrown.

Returns:
The trigger to preserve per schema, not null

getTypesToPreserve

protected Map<String,Set<String>> getTypesToPreserve()
Gets the list of all types to preserve per schema. The case is corrected if necesary. Quoting a type name makes it case sensitive. If no schema is specified, the type will be added to the default schema name set.

If a type to preserve does not exist, a UnitilsException is thrown.

Returns:
The type to preserve per schema, not null

isItemToPreserve

protected boolean isItemToPreserve(String item,
                                   Set<String> itemsToPreserve)
Checks whether the given item is one of the items to preserve. This also handles identifiers that are stored in mixed case.

Parameters:
item - The item, not null
itemsToPreserve - The items to preserve
Returns:
True if item to preserve

itemToPreserveExists

protected boolean itemToPreserveExists(String itemToPreserve,
                                       Set<String> items)
Checks whether the given item to preserve is one of the items. This also handles identifiers that are stored in mixed case.

Parameters:
itemToPreserve - The item to preserve, not null
items - The items, not null
Returns:
True if on of the items

getItemsToPreserve

protected Map<String,Set<String>> getItemsToPreserve(String propertyName)
Gets the list of items to preserve per schema. The case is corrected if necesary. Quoting an identifier makes it case sensitive. If no schema is specified, the identifiers will be added to the default schema name set.

Parameters:
propertyName - The name of the property that defines the items, not null
Returns:
The set of items per schema name, not null


Copyright © 2011. All Rights Reserved.