<commit_before>//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2006-2007 <NAME>, <NAME>
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.samskivert.jdbc;

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

import com.samskivert.util.StringUtil;

import static com.samskivert.Log.log;

/**
 * A superclass to help with the shrinking subset of SQL our supported dialects can agree on,
 * or when there is disagreement, implement the most standard-compliant version and let the
 * dialectal sub-classes override.
 */
public abstract class BaseLiaison implements DatabaseLiaison
{
    public BaseLiaison ()
    {
        super();
    }

    // from DatabaseLiaison
    public boolean tableExists (Connection conn, String name) throws SQLException
    {
        ResultSet rs = conn.getMetaData().getTables(null, null, name, null);
        while (rs.next()) {
            String tname = rs.getString("TABLE_NAME");
            if (name.equals(tname)) {
                return true;
            }
        }
        return false;
    }

    // from DatabaseLiaison
    public boolean tableContainsColumn (Connection conn, String table, String column)
        throws SQLException
    {
        ResultSet rs = conn.getMetaData().getColumns(null, null, table, column);
        while (rs.next()) {
            String tname = rs.getString("TABLE_NAME");
            String cname = rs.getString("COLUMN_NAME");
            if (tname.equals(table) && cname.equals(column)) {
                return true;
            }
        }
        return false;
    }

    // from DatabaseLiaison
    public boolean tableContainsIndex (Connection conn, String table, String index)
        throws SQLException
    {
        ResultSet rs = conn.getMetaData().getIndexInfo(null, null, table, false, true);
        while (rs.next()) {
            String tname = rs.getString("TABLE_NAME");
            String iname = rs.getString("INDEX_NAME");
            if (tname.equals(table) && index.equals(iname)) {
                return true;
            }
        }
        return false;
    }

    // from DatabaseLiaison
    public boolean addIndexToTable (
        Connection conn, String table, String[] columns, String ixName, boolean unique)
        throws SQLException
    {
        if (tableContainsIndex(conn, table, ixName)) {
            return false;
        }
        ixName = (ixName != null ? ixName : StringUtil.join(columns, "_"));

        StringBuilder update = new StringBuilder("CREATE ");
        if (unique) {
            update.append("UNIQUE ");
        }
        update.append("INDEX ").append(indexSQL(ixName)).append(" ON ").
            append(tableSQL(table)).append(" (");
        for (int ii = 0; ii < columns.length; ii ++) {
            if (ii > 0) {
                update.append(", ");
            }
            update.append(columnSQL(columns[ii]));
        }
        update.append(")");

        executeQuery(conn, update.toString());
        log.info("Database index '" + ixName + "' added to table '" + table + "'");
        return true;
    }

    // from DatabaseLiaison
    public void addPrimaryKey (Connection conn, String table, String[] columns)
        throws SQLException
    {
        StringBuilder fields = new StringBuilder("(");
        for (int ii = 0; ii < columns.length; ii ++) {
            if (ii > 0) {
                fields.append(", ");
            }
            fields.append(columnSQL(columns[ii]));
        }
        fields.append(")");
        String update = "ALTER TABLE " + tableSQL(table) + " ADD PRIMARY KEY " + fields.toString();

        executeQuery(conn, update.toString());
        log.info("Primary key " + fields + " added to table '" + table + "'");
    }

    // from DatabaseLiaison
    public void dropIndex (Connection conn, String table, String index) throws SQLException
    {
        executeQuery(conn, "ALTER TABLE " + tableSQL(table) +
                     " DROP CONSTRAINT " + columnSQL(index));
    }

    // from DatabaseLiaison
    public void dropPrimaryKey (Connection conn, String table, String pkName)
        throws SQLException
    {
        dropIndex(conn, table, pkName);
    }

    // from DatabaseLiaison
    public boolean addColumn (
        Connection conn, String table, String column, String definition, boolean check)
        throws SQLException
    {
        if (check && tableContainsColumn(conn, table, column)) {
            return false;
        }

        executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " ADD COLUMN " +
                     columnSQL(column) + " " + definition);
        log.info("Database column '" + column + "' added to table '" + table + "'.");
        return true;
    }

    // from DatabaseLiaison
    public boolean addColumn (Connection conn, String table, String column,
                              ColumnDefinition newColumnDef, boolean check)
        throws SQLException
    {
        if (check && tableContainsColumn(conn, table, column)) {<commit_msg>Indexes are not really constraints. Primary key dropping is its own thing, too.<commit_after>//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2006-2007 <NAME>, <NAME>
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.samskivert.jdbc;

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

import com.samskivert.util.StringUtil;

import static com.samskivert.Log.log;

/**
 * A superclass to help with the shrinking subset of SQL our supported dialects can agree on,
 * or when there is disagreement, implement the most standard-compliant version and let the
 * dialectal sub-classes override.
 */
public abstract class BaseLiaison implements DatabaseLiaison
{
    public BaseLiaison ()
    {
        super();
    }

    // from DatabaseLiaison
    public boolean tableExists (Connection conn, String name) throws SQLException
    {
        ResultSet rs = conn.getMetaData().getTables(null, null, name, null);
        while (rs.next()) {
            String tname = rs.getString("TABLE_NAME");
            if (name.equals(tname)) {
                return true;
            }
        }
        return false;
    }

    // from DatabaseLiaison
    public boolean tableContainsColumn (Connection conn, String table, String column)
        throws SQLException
    {
        ResultSet rs = conn.getMetaData().getColumns(null, null, table, column);
        while (rs.next()) {
            String tname = rs.getString("TABLE_NAME");
            String cname = rs.getString("COLUMN_NAME");
            if (tname.equals(table) && cname.equals(column)) {
                return true;
            }
        }
        return false;
    }

    // from DatabaseLiaison
    public boolean tableContainsIndex (Connection conn, String table, String index)
        throws SQLException
    {
        ResultSet rs = conn.getMetaData().getIndexInfo(null, null, table, false, true);
        while (rs.next()) {
            String tname = rs.getString("TABLE_NAME");
            String iname = rs.getString("INDEX_NAME");
            if (tname.equals(table) && index.equals(iname)) {
                return true;
            }
        }
        return false;
    }

    // from DatabaseLiaison
    public boolean addIndexToTable (
        Connection conn, String table, String[] columns, String ixName, boolean unique)
        throws SQLException
    {
        if (tableContainsIndex(conn, table, ixName)) {
            return false;
        }
        ixName = (ixName != null ? ixName : StringUtil.join(columns, "_"));

        StringBuilder update = new StringBuilder("CREATE ");
        if (unique) {
            update.append("UNIQUE ");
        }
        update.append("INDEX ").append(indexSQL(ixName)).append(" ON ").
            append(tableSQL(table)).append(" (");
        for (int ii = 0; ii < columns.length; ii ++) {
            if (ii > 0) {
                update.append(", ");
            }
            update.append(columnSQL(columns[ii]));
        }
        update.append(")");

        executeQuery(conn, update.toString());
        log.info("Database index '" + ixName + "' added to table '" + table + "'");
        return true;
    }

    // from DatabaseLiaison
    public void addPrimaryKey (Connection conn, String table, String[] columns)
        throws SQLException
    {
        StringBuilder fields = new StringBuilder("(");
        for (int ii = 0; ii < columns.length; ii ++) {
            if (ii > 0) {
                fields.append(", ");
            }
            fields.append(columnSQL(columns[ii]));
        }
        fields.append(")");
        String update = "ALTER TABLE " + tableSQL(table) + " ADD PRIMARY KEY " + fields.toString();

        executeQuery(conn, update.toString());
        log.info("Primary key " + fields + " added to table '" + table + "'");
    }

    // from DatabaseLiaison
    public void dropIndex (Connection conn, String table, String index) throws SQLException
    {
        executeQuery(conn, "DROP INDEX " + columnSQL(index));
    }

    // from DatabaseLiaison
    public void dropPrimaryKey (Connection conn, String table, String pkName)
        throws SQLException
    {
        executeQuery(conn, "ALTER TABLE " + tableSQL(table) +
            " DROP CONSTRAINT " + columnSQL(pkName));
    }

    // from DatabaseLiaison
    public boolean addColumn (
        Connection conn, String table, String column, String definition, boolean check)
        throws SQLException
    {
        if (check && tableContainsColumn(conn, table, column)) {
            return false;
        }

        executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " ADD COLUMN " +
                     columnSQL(column) + " " + definition);
        log.info("Database column '" + column + "' added to table '" + table + "'.");
        return true;
    }

    // from DatabaseLiaison
    public boolean addColumn (Connection conn, String table, String column,
                              ColumnDefinition newColumnDef, boolean check)
        throws SQLException
    {
        if (check && tableContainsColumn(conn, table, column)) {
{'fraction_non_alphanumeric': 0.1063811489508084, 'fraction_numerical': 0.004815961472308222, 'mean_word_length': 2.293401302747097, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4432162', 'n_tokens_mistral': 3106, 'n_tokens_neox': 3018, 'n_words': 999}