批量“替换成"用于 CodeIgniter 活动记录

批量“替换成

问题描述:

CodeIgniter 提供了一个很棒的批量更新函数,叫做 update_batch()

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

它还提供了一个函数replace()来执行replace into mysql 查询

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

有什么方法可以使用 replace_batch() 或类似的东西吗?我正在使用内部带有替换的循环,但认为函数会更好

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

如果你想克隆 我的仓库 我已经实施了.我尝试将解决方案放入主分支,但似乎 narfbg 是坚定的反对.也许社区的更多参与可以使其实施.

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

以上是与 Github 上完全相同的代码,因此该解决方案可索引回 *.com.但不幸的是,我对 * 的回复仅限于 30k 个字符,因此我无法粘贴整个提交,其中还包括对数据库驱动程序的更改.

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