All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.akquinet.jbosscc.guttenbase.configuration.impl.OracleTargetDatabaseConfiguration Maven / Gradle / Ivy

The newest version!
package de.akquinet.jbosscc.guttenbase.configuration.impl;

import de.akquinet.jbosscc.guttenbase.hints.TableOrderHint;
import de.akquinet.jbosscc.guttenbase.meta.TableMetaData;
import de.akquinet.jbosscc.guttenbase.repository.ConnectorRepository;
import de.akquinet.jbosscc.guttenbase.tools.ScriptExecutorTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Implementation for Oracle data base.
 *
 * 

* © 2012-2034 akquinet tech@spree *

* * @author M. Dahm */ public class OracleTargetDatabaseConfiguration extends DefaultTargetDatabaseConfiguration { private static final Logger LOG = LoggerFactory.getLogger(OracleTargetDatabaseConfiguration.class); public OracleTargetDatabaseConfiguration(final ConnectorRepository connectorRepository) { super(connectorRepository); } /** * {@inheritDoc} */ @Override public void initializeTargetConnection(final Connection connection, final String connectorId) throws SQLException { if (connection.getAutoCommit()) { connection.setAutoCommit(false); } setReferentialIntegrity(connection, connectorId, getTableMetaData(connectorId), false); } /** * {@inheritDoc} */ @Override public void finalizeTargetConnection(final Connection connection, final String connectorId) throws SQLException { setReferentialIntegrity(connection, connectorId, getTableMetaData(connectorId), true); } private List getTableMetaData(final String connectorId) { return TableOrderHint.getSortedTables(_connectorRepository, connectorId); } private void setReferentialIntegrity(final Connection connection, final String connectorId, final List tableMetaDatas, final boolean enable) throws SQLException { final String tablesList = createTablesList(tableMetaDatas); if (!"".equals(tablesList)) { final String schema = _connectorRepository.getConnectionInfo(connectorId).getSchema(); /* I want to disable all constraints in tables that reference the tables that I will update. */ final List> foreignKeyNames = new ScriptExecutorTool(_connectorRepository).executeQuery(connectorId, "SELECT DISTINCT AC.OWNER, AC.TABLE_NAME, AC.CONSTRAINT_NAME FROM ALL_CONSTRAINTS AC, ALL_CONS_COLUMNS ACC " + "WHERE AC.CONSTRAINT_TYPE = 'R' "// + "AND ACC.TABLE_NAME IN (" + tablesList + ") "// + "AND ACC.OWNER = '" + schema + "' "// + "AND ACC.CONSTRAINT_NAME = AC.R_CONSTRAINT_NAME "// + "AND ACC.OWNER = AC.R_OWNER"); // memorize any problems that occur during constraint handling final StringBuilder problems = new StringBuilder(); for (final Map fkMap : foreignKeyNames) { final String tableName = fkMap.get("TABLE_NAME").toString(); final String constraintName = fkMap.get("CONSTRAINT_NAME").toString(); final String owner = fkMap.get("OWNER").toString(); final String sql = "ALTER TABLE " + owner + "." + tableName + (enable ? " ENABLE " : " DISABLE ") + "CONSTRAINT " + constraintName; try { executeSQL(connection, sql); } catch (final SQLException e) { LOG.error("Unable to handle constraint: " + sql, e); problems.append("Unable to handle constraint: ").append(sql).append("->").append(e.getMessage()).append(":").append(e.getSQLState()); } } if (problems.length() > 0) { // if there has been a problem with any constraint, now throw the exception throw new SQLException("Constraint problems occurred: " + problems); } } } private static String createTablesList(final List tableMetaDatas) { final StringBuilder tablesBuilder = new StringBuilder(); for (final TableMetaData tableMetaData : tableMetaDatas) { tablesBuilder.append("'").append(tableMetaData.getTableName()).append("'").append(", "); } if (tablesBuilder.length() > 2) { tablesBuilder.setLength(tablesBuilder.length() - 2); } return tablesBuilder.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy