<commit_before><?php

namespace JarJak;

use Exception;

/**
 * Separated class to ease testing and wrapping prepared SQLs into your own DB Abstraction
 *
 * @package DBALManager
 * @author  <NAME> <<EMAIL>>
 */
class SqlPreparator
{
    /**
     * prepares "INSERT...ON DUPLICATE KEY UPDATE" sql statement by array of parameters
     *
     * @param string $table           table name
     * @param array  $values          values
     * @param array  $ignoreForUpdate columns that should not be updated
     *
     * @return array [sql, params] prepared SQL and params
     *
     * @link http://stackoverflow.com/questions/778534/mysql-on-duplicate-key-last-insert-id
     */
    public static function prepareInsertOrUpdate($table, array $values, array $ignoreForUpdate = [])
    {
        $cols = [];
        $params = [];
        $marks = [];

        foreach ($values as $k => $v) {
            $cols[] = $k;
            $params[] = $v;
            $marks[] = '?';
        }

        $cols = static::escapeSqlWords($cols);

        $sql = "INSERT INTO " . static::escapeSqlWords($table) . " (";
        $sql .= implode(', ', $cols);
        $sql .= ") VALUES (";
        $sql .= implode(', ', $marks);
        $sql .= ") ON DUPLICATE KEY UPDATE ";

        $updateArray = [];
        foreach ($cols as $col) {
            if (!in_array($col, $ignoreForUpdate)) {
                $updateArray[] = "$col=VALUES($col)";
            }
        }
        $sql .= implode(', ', $updateArray);
        $sql .= ", id=LAST_INSERT_ID(id)";

        return ['sql' => $sql, 'params' => $params];
    }

    /**
     * prepares "INSERT...ON DUPLICATE KEY UPDATE" sql statement by multi array of values
     * to be used for bulk inserts
     *
     * @param string $table           table name
     * @param array  $rows            2-dimensional array of values to insert
     * @param array  $ignoreForUpdate columns that should not be updated
     *
     * @return array [sql, params] prepared SQL and params<commit_msg>Fix insertOrUpdateByArray to properly ignore columns on update<commit_after><?php

namespace JarJak;

use Exception;

/**
 * Separated class to ease testing and wrapping prepared SQLs into your own DB Abstraction
 *
 * @package DBALManager
 * @author  <NAME> <<EMAIL>>
 */
class SqlPreparator
{
    /**
     * prepares "INSERT...ON DUPLICATE KEY UPDATE" sql statement by array of parameters
     *
     * @param string $table           table name
     * @param array  $values          values
     * @param array  $ignoreForUpdate columns that should not be updated
     *
     * @return array [sql, params] prepared SQL and params
     *
     * @link http://stackoverflow.com/questions/778534/mysql-on-duplicate-key-last-insert-id
     */
    public static function prepareInsertOrUpdate($table, array $values, array $ignoreForUpdate = [])
    {
        $cols = [];
        $params = [];
        $marks = [];

        foreach ($values as $k => $v) {
            $cols[] = $k;
            $params[] = $v;
            $marks[] = '?';
        }

        $cols = static::escapeSqlWords($cols);
        $ignoreForUpdate = static::escapeSqlWords($ignoreForUpdate);

        $sql = "INSERT INTO " . static::escapeSqlWords($table) . " (";
        $sql .= implode(', ', $cols);
        $sql .= ") VALUES (";
        $sql .= implode(', ', $marks);
        $sql .= ") ON DUPLICATE KEY UPDATE ";

        $updateArray = [];
        foreach ($cols as $col) {
            if (!in_array($col, $ignoreForUpdate)) {
                $updateArray[] = "$col=VALUES($col)";
            }
        }
        $sql .= implode(', ', $updateArray);
        $sql .= ", id=LAST_INSERT_ID(id)";

        return ['sql' => $sql, 'params' => $params];
    }

    /**
     * prepares "INSERT...ON DUPLICATE KEY UPDATE" sql statement by multi array of values
     * to be used for bulk inserts
     *
     * @param string $table           table name
     * @param array  $rows            2-dimensional array of values to insert
     * @param array  $ignoreForUpdate columns that should not be updated
     *
     * @return array [sql, params] prepared SQL and params
{'fraction_non_alphanumeric': 0.1365830115830116, 'fraction_numerical': 0.0033783783783783786, 'mean_word_length': 2.0955937266616878, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '5602535', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1184, 'n_words': 356}