<commit_before>/*
 * Copyright (C) 2006, Ordina
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package be.ordina.unitils.db.constraints;

import be.ordina.unitils.db.handler.StatementHandler;
import be.ordina.unitils.db.handler.StatementHandlerException;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OracleConstraintsDisabler implements ConstraintsDisabler {

    /**
     * SQL statement to select the database constraints
     */
    private static final String DISABLE_CONSTRAINTS_SQL = "select table_name, constraint_name "
        + " from user_constraints ";    

    /**
     * The DataSource
     */
    private DataSource dataSource;

    /**
     * The StatementHandler
     */
    private StatementHandler statementHandler;

    /**
     * @see ConstraintsDisabler#init(javax.sql.DataSource, be.ordina.unitils.db.handler.StatementHandler)
     */
    public void init(DataSource dataSource, StatementHandler statementHandler) {
        this.dataSource = dataSource;
        this.statementHandler = statementHandler;
    }

    /**
     * @see ConstraintsDisabler#enableConstraints()
     */
    public void enableConstraints() {
        generateConstraintsScript("enable");
    }

    /**
     * @see ConstraintsDisabler#disableConstraints()
     */
    public void disableConstraints() {
        generateConstraintsScript("disable");
    }

    private void generateConstraintsScript(String enableDisable) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            connection = dataSource.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(DISABLE_CONSTRAINTS_SQL);
            output(resultSet, enableDisable);
        } catch (SQLException e) {
            throw new RuntimeException("Error while disabling constraints", e);
        } catch (StatementHandlerException e) {
            throw new RuntimeException("Error while disabling constraints", e);
        } finally {
            DbUtils.closeQuietly(connection, statement, resultSet);
        }
    }

    private void output(ResultSet resultSet, String enableDisable) throws SQLException, StatementHandlerException {
        while (resultSet.next()) {
            StringBuffer buf = new StringBuffer();
            buf.append("alter table ");
            buf.append(resultSet.getString("table_name"));
            buf.append(" ");
            buf.append(enableDisable);
            buf.append(" constraint ");
            buf.append(resultSet.getString("constraint_name"));
            buf.append(";\n");
            statementHandler.handle(buf.toString());
        }
    }

}<commit_msg>Removed disabling of Primary keys because of constraint dependency problem (foreign keys)<commit_after>/*
 * Copyright (C) 2006, Ordina
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package be.ordina.unitils.db.constraints;

import be.ordina.unitils.db.handler.StatementHandler;
import be.ordina.unitils.db.handler.StatementHandlerException;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OracleConstraintsDisabler implements ConstraintsDisabler {

    /**
     * SQL statement to select the database constraints
     */
    private static final String DISABLE_CONSTRAINTS_SQL = "select table_name, constraint_name "
        + " from user_constraints where constraint_type <> 'P'";    

    /**
     * The DataSource
     */
    private DataSource dataSource;

    /**
     * The StatementHandler
     */
    private StatementHandler statementHandler;

    /**
     * @see ConstraintsDisabler#init(javax.sql.DataSource, be.ordina.unitils.db.handler.StatementHandler)
     */
    public void init(DataSource dataSource, StatementHandler statementHandler) {
        this.dataSource = dataSource;
        this.statementHandler = statementHandler;
    }

    /**
     * @see ConstraintsDisabler#enableConstraints()
     */
    public void enableConstraints() {
        generateConstraintsScript("enable");
    }

    /**
     * @see ConstraintsDisabler#disableConstraints()
     */
    public void disableConstraints() {
        generateConstraintsScript("disable");
    }

    private void generateConstraintsScript(String enableDisable) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            connection = dataSource.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(DISABLE_CONSTRAINTS_SQL);
            output(resultSet, enableDisable);
        } catch (SQLException e) {
            throw new RuntimeException("Error while disabling constraints", e);
        } catch (StatementHandlerException e) {
            throw new RuntimeException("Error while disabling constraints", e);
        } finally {
            DbUtils.closeQuietly(connection, statement, resultSet);
        }
    }

    private void output(ResultSet resultSet, String enableDisable) throws SQLException, StatementHandlerException {
        while (resultSet.next()) {
            StringBuffer buf = new StringBuffer();
            buf.append("alter table ");
            buf.append(resultSet.getString("table_name"));
            buf.append(" ");
            buf.append(enableDisable);
            buf.append(" constraint ");
            buf.append(resultSet.getString("constraint_name"));            
            statementHandler.handle(buf.toString());
        }
    }

}
{'fraction_non_alphanumeric': 0.0957573692281479, 'fraction_numerical': 0.0013630942238882263, 'mean_word_length': 2.8216145833333335, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2370507', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1456, 'n_words': 335}