[英]batch “replace into” for CodeIgniter Active Record
CodeIgniter offers a great function to update in batch called update_batch()
CodeIgniter提供了一个很棒的批量更新功能,称为
update_batch()
http://www.codeigniter.com/user_guide/database/query_builder.html?highlight=update_batch#CI_DB_query_builder::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 它还提供了函数
replace()
来执行replace into
mysql查询的replace into
http://www.codeigniter.com/user_guide/database/query_builder.html?highlight=replace#CI_DB_query_builder::replace 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? 有什么办法可以
replace_batch()
或类似的东西吗? I am using a loop with a replace inside, but figured a function would be better 我正在使用带有替换内部的循环,但想出一个功能会更好
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.
我试图将解决方案带入主分支,但narfbg似乎坚决反对。 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. 上面的代码与Github上的代码完全相同,只是该解决方案可索引到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.
但是不幸的是,我对stackoverflow的答复仅限于3万个字符,因此我无法粘贴整个提交,其中也包括对数据库驱动程序的更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.