mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 20:52:48 +08:00
改进insertall方法的分批处理
This commit is contained in:
@@ -466,6 +466,10 @@ abstract class Connection
|
||||
*/
|
||||
public function getRealSql($sql, array $bind = [])
|
||||
{
|
||||
if (is_array($sql)) {
|
||||
$sql = implode(';', $sql);
|
||||
}
|
||||
|
||||
foreach ($bind as $key => $val) {
|
||||
$value = is_array($val) ? $val[0] : $val;
|
||||
$type = is_array($val) ? $val[1] : PDO::PARAM_STR;
|
||||
@@ -725,7 +729,7 @@ abstract class Connection
|
||||
* @param array $sqlArray SQL批处理指令
|
||||
* @return boolean
|
||||
*/
|
||||
public function batchQuery($sqlArray = [])
|
||||
public function batchQuery($sqlArray = [], $bind = [])
|
||||
{
|
||||
if (!is_array($sqlArray)) {
|
||||
return false;
|
||||
@@ -734,7 +738,7 @@ abstract class Connection
|
||||
$this->startTrans();
|
||||
try {
|
||||
foreach ($sqlArray as $sql) {
|
||||
$this->execute($sql);
|
||||
$this->execute($sql, $bind);
|
||||
}
|
||||
// 提交事务
|
||||
$this->commit();
|
||||
@@ -742,6 +746,7 @@ abstract class Connection
|
||||
$this->rollback();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -312,9 +312,9 @@ class Query
|
||||
* @param array $sql SQL批处理指令
|
||||
* @return boolean
|
||||
*/
|
||||
public function batchQuery($sql = [])
|
||||
public function batchQuery($sql = [], $bind = [])
|
||||
{
|
||||
return $this->connection->batchQuery($sql);
|
||||
return $this->connection->batchQuery($sql, $bind);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2128,24 +2128,37 @@ class Query
|
||||
/**
|
||||
* 批量插入记录
|
||||
* @access public
|
||||
* @param mixed $dataSet 数据集
|
||||
* @param boolean $replace 是否replace
|
||||
* @param mixed $dataSet 数据集
|
||||
* @param boolean $replace 是否replace
|
||||
* @param integer $limit 每次写入数据限制
|
||||
* @return integer|string
|
||||
*/
|
||||
public function insertAll(array $dataSet, $replace = false)
|
||||
public function insertAll(array $dataSet, $replace = false, $limit = null)
|
||||
{
|
||||
// 分析查询表达式
|
||||
$options = $this->parseExpress();
|
||||
if (!is_array(reset($dataSet))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 生成SQL语句
|
||||
$sql = $this->builder->insertAll($dataSet, $options, $replace);
|
||||
if (is_null($limit)) {
|
||||
$sql = $this->builder->insertAll($dataSet, $options, $replace);
|
||||
} else {
|
||||
$array = array_chunk($dataSet, $limit, true);
|
||||
foreach ($array as $item) {
|
||||
$sql[] = $this->builder->insertAll($item, $options, $replace);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取参数绑定
|
||||
$bind = $this->getBind();
|
||||
if ($options['fetch_sql']) {
|
||||
// 获取实际执行的SQL语句
|
||||
return $this->connection->getRealSql($sql, $bind);
|
||||
} elseif (is_array($sql)) {
|
||||
// 执行操作
|
||||
return $this->batchQuery($sql, $bind);
|
||||
} else {
|
||||
// 执行操作
|
||||
return $this->execute($sql, $bind);
|
||||
|
||||
Reference in New Issue
Block a user