Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Batch-Ersetzung in für CodeIgniter Active Record

Wenn Sie mein Repository klonen möchten habe ich umgesetzt. Ich habe es versucht um die Lösung in den Hauptzweig zu bringen, aber es scheint, als wäre narfbg entschieden dagegen. Vielleicht könnte es durch mehr Beteiligung der Community umgesetzt werden.

/**
 * Replace_Batch
 *
 * Compiles batch insert strings replacing any existing rows and runs the queries
 *
 * @param   string  $table  Table to replace insert into
 * @param   array   $set    An associative array of insert values
 * @param   bool    $escape Whether to escape values and identifiers
 * @return  int Number of rows inserted or FALSE on failure
 */
public function replace_batch($table, $set = NULL, $escape = NULL, $batch_size = 100)
{
    if ($set === NULL)
    {
        if (empty($this->qb_set))
        {
            return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
        }
    }
    else
    {
        if (empty($set))
        {
            return ($this->db_debug) ? $this->display_error('replace_batch() called with no data') : FALSE;
        }

        $this->set_insert_batch($set, '', $escape);
    }

    if (strlen($table) === 0)
    {
        if ( ! isset($this->qb_from[0]))
        {
            return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
        }

        $table = $this->qb_from[0];
    }

    // Batch this baby
    $affected_rows = 0;
    for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
    {
        if ($this->query($this->_replace_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, $batch_size))))
        {
            $affected_rows += $this->affected_rows();
        }
    }

    $this->_reset_write();
    return $affected_rows;
}

// --------------------------------------------------------------------

/**
 * Replace batch statement
 *
 * Generates a platform-specific insert string from the supplied data.
 *
 * @param   string  $table  Table name
 * @param   array   $keys   INSERT keys
 * @param   array   $values INSERT values
 * @return  string
 */
protected function _replace_batch($table, $keys, $values)
{
    return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
}

Oben ist genau derselbe Code wie auf Github, nur damit die Lösung zurück zu stackoverflow.com indexierbar ist. Aber leider ist meine Antwort auf Stackoverflow auf 30.000 Zeichen begrenzt, sodass ich nicht den gesamten Commit einfügen kann, der auch Änderungen an DB-Treibern enthält.