001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.dbmaintainer.structure.impl;
017:
018: import java.util.Properties;
019: import java.util.Set;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.unitils.core.dbsupport.DbSupport;
024: import org.unitils.dbmaintainer.structure.ConstraintsDisabler;
025: import org.unitils.dbmaintainer.util.BaseDatabaseTask;
026:
027: /**
028: * Default implementation of {@link ConstraintsDisabler}
029: *
030: * @author Filip Neven
031: * @author Bart Vermeiren
032: * @author Tim Ducheyne
033: */
034: public class DefaultConstraintsDisabler extends BaseDatabaseTask
035: implements ConstraintsDisabler {
036:
037: /* The logger instance for this class */
038: private static Log logger = LogFactory
039: .getLog(DefaultConstraintsDisabler.class);
040:
041: /**
042: * Initializes the disabler.
043: *
044: * @param configuration the config, not null
045: */
046: @Override
047: protected void doInit(Properties configuration) {
048: }
049:
050: /**
051: * Permanently disable every foreign key or not-null constraint
052: */
053: public void disableConstraints() {
054: for (DbSupport dbSupport : dbSupports) {
055: logger.info("Disabling contraints in database schema "
056: + dbSupport.getSchemaName());
057: removeForeignKeyConstraints(dbSupport);
058: removeNotNullConstraints(dbSupport);
059: }
060: }
061:
062: /**
063: * Disables all foreign key constraints
064: *
065: * @param dbSupport The database support, not null
066: */
067: protected void removeForeignKeyConstraints(DbSupport dbSupport) {
068: Set<String> tableNames = dbSupport.getTableNames();
069: for (String tableName : tableNames) {
070: Set<String> constraintNames = dbSupport
071: .getForeignKeyConstraintNames(tableName);
072: for (String constraintName : constraintNames) {
073: dbSupport.removeForeignKeyConstraint(tableName,
074: constraintName);
075: }
076: }
077: }
078:
079: /**
080: * Disables all not-null constraints that are not of primary keys.
081: *
082: * @param dbSupport The database support, not null
083: */
084: protected void removeNotNullConstraints(DbSupport dbSupport) {
085: Set<String> tableNames = dbSupport.getTableNames();
086: for (String tableName : tableNames) {
087: // Retrieve the name of the primary key, since we cannot remove the not-null constraint on this column
088: Set<String> primaryKeyColumnNames = dbSupport
089: .getPrimaryKeyColumnNames(tableName);
090:
091: Set<String> notNullColumnNames = dbSupport
092: .getNotNullColummnNames(tableName);
093: for (String notNullColumnName : notNullColumnNames) {
094: if (primaryKeyColumnNames.contains(notNullColumnName)) {
095: // Do not remove PK constraints
096: continue;
097: }
098: dbSupport.removeNotNullConstraint(tableName,
099: notNullColumnName);
100: }
101: }
102: }
103:
104: }
|