batch “replace into” for CodeIgniter Active Record

丶灬走出姿态 提交于 2021-01-28 14:57:22

问题


CodeIgniter offers a great function to update in batch called update_batch()

http://www.codeigniter.com/user_guide/database/query_builder.html?highlight=update_batch#CI_DB_query_builder::update_batch

It also offers a function replace() to execute replace into mysql queries

http://www.codeigniter.com/user_guide/database/query_builder.html?highlight=replace#CI_DB_query_builder::replace

Is there any way to have something like replace_batch() or equivalent? I am using a loop with a replace inside, but figured a function would be better


回答1:


If you want to clone my repository I have implemented. I tried to get the solution into the main branch but it seems like narfbg is adamantly against it. Maybe some more participation from the community could get it implemented.

/**
 * 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);
}

Above is the exact same code as on Github just so the solution is indexable back to stackoverflow.com. But unfortunately my reply on stackoverflow is limited to 30k characters so I cannot paste in the entire commit which also includes changes to db drivers.



来源:https://stackoverflow.com/questions/32645278/batch-replace-into-for-codeigniter-active-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!