<commit_before>package org.intermine.sql.writebatch;

/*
 * Copyright (C) 2002-2004 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.intermine.util.DatabaseUtil;

import org.apache.log4j.Logger;

/**
 * An implementation of the BatchWriter interface that uses simple JDBC addBatch() and
 * executeBatch() methods.
 *
 * @author <NAME>
 */
public class BatchWriterSimpleImpl implements BatchWriter
{
    private static final Logger LOG = Logger.getLogger(BatchWriterSimpleImpl.class);

    protected Connection con;
    protected Statement simpleBatch;
    protected int simpleBatchSize;
    protected List retval;

    /**
     * @see BatchWriter#write
     */
    public List write(Connection con, Map tables) throws SQLException {
        retval = new ArrayList();
        this.con = con;
        simpleBatch = con.createStatement();
        simpleBatchSize = 0;
        Iterator tableIter = tables.entrySet().iterator();
        while (tableIter.hasNext()) {
            Map.Entry tableEntry = (Map.Entry) tableIter.next();
            String name = (String) tableEntry.getKey();
            Table table = (Table) tableEntry.getValue();
            if (table instanceof TableBatch) {
                doDeletes(name, (TableBatch) table);
                doInserts(name, (TableBatch) table);
            } else {
                doIndirectionDeletes(name, (IndirectionTableBatch) table);
                doIndirectionInserts(name, (IndirectionTableBatch) table);
            }
            table.clear();
        }
        if (simpleBatchSize > 0) {
            retval.add(new FlushJobStatementBatchImpl(simpleBatch));
        }
        simpleBatch = null;
        return retval;
    }

    /**
     * Performs all the inserts for the given table name and table batch.
     *
     * @param name the name of the table
     * @param table the table batch
     * @throws SQLException if an error occurs
     */
    protected void doInserts(String name, TableBatch table) throws SQLException {
        String colNames[] = table.getColNames();
        if ((colNames != null) && (!table.getIdsToInsert().isEmpty())) {
            StringBuffer preambleBuffer = new StringBuffer("INSERT INTO ").append(name)
                .append(" (");
            for (int i = 0; i < colNames.length; i++) {
                if (i > 0) {
                    preambleBuffer.append(", ");
                }
                preambleBuffer.append(colNames[i]);
            }
            preambleBuffer.append(") VALUES (");
            String preamble = preambleBuffer.toString();
            Iterator insertIter = table.getIdsToInsert().entrySet().iterator();
            while (insertIter.hasNext()) {
                Map.Entry insertEntry = (Map.Entry) insertIter.next();
                Object inserts = insertEntry.getValue();
                if (inserts instanceof Object[]) {
                    addToSimpleBatch(insertString(preamble, colNames.length, (Object[]) inserts));
                } else {
                    Iterator iter = ((List) inserts).iterator();
                    while (iter.hasNext()) {
                        Object values[] = (Object[]) iter.next();
                        addToSimpleBatch(insertString(preamble, colNames.length, values));
                    }
                }
            }
        }
    }

    private static String insertString(String preamble, int colCount, Object values[]) {
        StringBuffer sqlBuffer = new StringBuffer((int) (TableBatch.sizeOfArray(values)
                    * 1.01 + 1000)).append(preamble);
        for (int i = 0; i < colCount; i++) {
            if (i > 0) {
                sqlBuffer.append(", ");
            }
            sqlBuffer.append(DatabaseUtil.objectToString(values[i]));
        }
        sqlBuffer.append(")");
        return sqlBuffer.toString();
    }

    /**
     * Performs all the deletes for the given table name and table batch.
     *
     * @param name the name of the table
     * @param table the table batch
     * @throws SQLException if an error occurs
     */
    protected void doDeletes(String name, TableBatch table) throws SQLException {
        String idField = table.getIdField();
        if ((idField != null) && (!table.getIdsToDelete().isEmpty())) {
            StringBuffer sqlBuffer = new StringBuffer("DELETE FROM ").append(name).append(" WHERE ")
                .append(idField).append(" IN (");
            boolean needComma = false;
            Iterator iter = table.getIdsToDelete().iterator();
            while (iter.hasNext()) {
                Object idValue = iter.next();
                if (needComma) {
                    sqlBuffer.append(", ");
                }
                needComma = true;
                sqlBuffer.append(DatabaseUtil.objectToString(idValue));
            }
            sqlBuffer.append(")");
            addToSimpleBatch(sqlBuffer.toString());
        }
    }

    /**
     * Performs all the delete operations for the given IndirectionTableBatch and name.
     *
     * @param name the name of the table
     * @param table the IndirectionTableBatch
     * @throws SQLException if an error occurs
     */
    protected void doIndirectionDeletes(String name,
            IndirectionTableBatch table) throws SQLException {
        Set rows = new CombinedSet(table.getRowsToDelete(), table.getRowsToInsert());
        if (!rows.isEmpty()) {
            StringBuffer sql = new StringBuffer("DELETE FROM ").append(name).append(" WHERE (");
            boolean needComma = false;
            Iterator dIter = rows.iterator();
            while (dIter.hasNext()) {
                Row row = (Row) dIter.next();
                if (needComma) {
                    sql.append(" OR ");
                }
                sql.append("(").append(table.getLeftColName()).append(" = ")
                    .append(row.getLeft()).append(" AND ").append(table.getRightColName()).append(" = ")
                    .append(row.getRight()).append(")");
                needComma = true;
            }<commit_msg>Checkstyle fix<commit_after>package org.intermine.sql.writebatch;

/*
 * Copyright (C) 2002-2004 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.intermine.util.DatabaseUtil;

import org.apache.log4j.Logger;

/**
 * An implementation of the BatchWriter interface that uses simple JDBC addBatch() and
 * executeBatch() methods.
 *
 * @author <NAME>
 */
public class BatchWriterSimpleImpl implements BatchWriter
{
    private static final Logger LOG = Logger.getLogger(BatchWriterSimpleImpl.class);

    protected Connection con;
    protected Statement simpleBatch;
    protected int simpleBatchSize;
    protected List retval;

    /**
     * @see BatchWriter#write
     */
    public List write(Connection con, Map tables) throws SQLException {
        retval = new ArrayList();
        this.con = con;
        simpleBatch = con.createStatement();
        simpleBatchSize = 0;
        Iterator tableIter = tables.entrySet().iterator();
        while (tableIter.hasNext()) {
            Map.Entry tableEntry = (Map.Entry) tableIter.next();
            String name = (String) tableEntry.getKey();
            Table table = (Table) tableEntry.getValue();
            if (table instanceof TableBatch) {
                doDeletes(name, (TableBatch) table);
                doInserts(name, (TableBatch) table);
            } else {
                doIndirectionDeletes(name, (IndirectionTableBatch) table);
                doIndirectionInserts(name, (IndirectionTableBatch) table);
            }
            table.clear();
        }
        if (simpleBatchSize > 0) {
            retval.add(new FlushJobStatementBatchImpl(simpleBatch));
        }
        simpleBatch = null;
        return retval;
    }

    /**
     * Performs all the inserts for the given table name and table batch.
     *
     * @param name the name of the table
     * @param table the table batch
     * @throws SQLException if an error occurs
     */
    protected void doInserts(String name, TableBatch table) throws SQLException {
        String colNames[] = table.getColNames();
        if ((colNames != null) && (!table.getIdsToInsert().isEmpty())) {
            StringBuffer preambleBuffer = new StringBuffer("INSERT INTO ").append(name)
                .append(" (");
            for (int i = 0; i < colNames.length; i++) {
                if (i > 0) {
                    preambleBuffer.append(", ");
                }
                preambleBuffer.append(colNames[i]);
            }
            preambleBuffer.append(") VALUES (");
            String preamble = preambleBuffer.toString();
            Iterator insertIter = table.getIdsToInsert().entrySet().iterator();
            while (insertIter.hasNext()) {
                Map.Entry insertEntry = (Map.Entry) insertIter.next();
                Object inserts = insertEntry.getValue();
                if (inserts instanceof Object[]) {
                    addToSimpleBatch(insertString(preamble, colNames.length, (Object[]) inserts));
                } else {
                    Iterator iter = ((List) inserts).iterator();
                    while (iter.hasNext()) {
                        Object values[] = (Object[]) iter.next();
                        addToSimpleBatch(insertString(preamble, colNames.length, values));
                    }
                }
            }
        }
    }

    private static String insertString(String preamble, int colCount, Object values[]) {
        StringBuffer sqlBuffer = new StringBuffer((int) (TableBatch.sizeOfArray(values)
                    * 1.01 + 1000)).append(preamble);
        for (int i = 0; i < colCount; i++) {
            if (i > 0) {
                sqlBuffer.append(", ");
            }
            sqlBuffer.append(DatabaseUtil.objectToString(values[i]));
        }
        sqlBuffer.append(")");
        return sqlBuffer.toString();
    }

    /**
     * Performs all the deletes for the given table name and table batch.
     *
     * @param name the name of the table
     * @param table the table batch
     * @throws SQLException if an error occurs
     */
    protected void doDeletes(String name, TableBatch table) throws SQLException {
        String idField = table.getIdField();
        if ((idField != null) && (!table.getIdsToDelete().isEmpty())) {
            StringBuffer sqlBuffer = new StringBuffer("DELETE FROM ").append(name).append(" WHERE ")
                .append(idField).append(" IN (");
            boolean needComma = false;
            Iterator iter = table.getIdsToDelete().iterator();
            while (iter.hasNext()) {
                Object idValue = iter.next();
                if (needComma) {
                    sqlBuffer.append(", ");
                }
                needComma = true;
                sqlBuffer.append(DatabaseUtil.objectToString(idValue));
            }
            sqlBuffer.append(")");
            addToSimpleBatch(sqlBuffer.toString());
        }
    }

    /**
     * Performs all the delete operations for the given IndirectionTableBatch and name.
     *
     * @param name the name of the table
     * @param table the IndirectionTableBatch
     * @throws SQLException if an error occurs
     */
    protected void doIndirectionDeletes(String name,
            IndirectionTableBatch table) throws SQLException {
        Set rows = new CombinedSet(table.getRowsToDelete(), table.getRowsToInsert());
        if (!rows.isEmpty()) {
            StringBuffer sql = new StringBuffer("DELETE FROM ").append(name).append(" WHERE (");
            boolean needComma = false;
            Iterator dIter = rows.iterator();
            while (dIter.hasNext()) {
                Row row = (Row) dIter.next();
                if (needComma) {
                    sql.append(" OR ");
                }
                sql.append("(").append(table.getLeftColName()).append(" = ")
                    .append(row.getLeft()).append(" AND ").append(table.getRightColName())
                    .append(" = ").append(row.getRight()).append(")");
                needComma = true;
            }
{'fraction_non_alphanumeric': 0.11113673045884252, 'fraction_numerical': 0.003381753900545692, 'mean_word_length': 2.031686859273066, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '260812', 'n_tokens_mistral': 3438, 'n_tokens_neox': 3288, 'n_words': 890}