改进insertall方法的分批处理

This commit is contained in:
thinkphp
2017-11-01 18:10:49 +08:00
parent 9db0007e36
commit b0f2e21cca
2 changed files with 26 additions and 8 deletions

View File

@@ -466,6 +466,10 @@ abstract class Connection
*/ */
public function getRealSql($sql, array $bind = []) public function getRealSql($sql, array $bind = [])
{ {
if (is_array($sql)) {
$sql = implode(';', $sql);
}
foreach ($bind as $key => $val) { foreach ($bind as $key => $val) {
$value = is_array($val) ? $val[0] : $val; $value = is_array($val) ? $val[0] : $val;
$type = is_array($val) ? $val[1] : PDO::PARAM_STR; $type = is_array($val) ? $val[1] : PDO::PARAM_STR;
@@ -725,7 +729,7 @@ abstract class Connection
* @param array $sqlArray SQL批处理指令 * @param array $sqlArray SQL批处理指令
* @return boolean * @return boolean
*/ */
public function batchQuery($sqlArray = []) public function batchQuery($sqlArray = [], $bind = [])
{ {
if (!is_array($sqlArray)) { if (!is_array($sqlArray)) {
return false; return false;
@@ -734,7 +738,7 @@ abstract class Connection
$this->startTrans(); $this->startTrans();
try { try {
foreach ($sqlArray as $sql) { foreach ($sqlArray as $sql) {
$this->execute($sql); $this->execute($sql, $bind);
} }
// 提交事务 // 提交事务
$this->commit(); $this->commit();
@@ -742,6 +746,7 @@ abstract class Connection
$this->rollback(); $this->rollback();
throw $e; throw $e;
} }
return true; return true;
} }

View File

@@ -312,9 +312,9 @@ class Query
* @param array $sql SQL批处理指令 * @param array $sql SQL批处理指令
* @return boolean * @return boolean
*/ */
public function batchQuery($sql = []) public function batchQuery($sql = [], $bind = [])
{ {
return $this->connection->batchQuery($sql); return $this->connection->batchQuery($sql, $bind);
} }
/** /**
@@ -2130,22 +2130,35 @@ class Query
* @access public * @access public
* @param mixed $dataSet 数据集 * @param mixed $dataSet 数据集
* @param boolean $replace 是否replace * @param boolean $replace 是否replace
* @param integer $limit 每次写入数据限制
* @return integer|string * @return integer|string
*/ */
public function insertAll(array $dataSet, $replace = false) public function insertAll(array $dataSet, $replace = false, $limit = null)
{ {
// 分析查询表达式 // 分析查询表达式
$options = $this->parseExpress(); $options = $this->parseExpress();
if (!is_array(reset($dataSet))) { if (!is_array(reset($dataSet))) {
return false; return false;
} }
// 生成SQL语句 // 生成SQL语句
if (is_null($limit)) {
$sql = $this->builder->insertAll($dataSet, $options, $replace); $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(); $bind = $this->getBind();
if ($options['fetch_sql']) { if ($options['fetch_sql']) {
// 获取实际执行的SQL语句 // 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $bind); return $this->connection->getRealSql($sql, $bind);
} elseif (is_array($sql)) {
// 执行操作
return $this->batchQuery($sql, $bind);
} else { } else {
// 执行操作 // 执行操作
return $this->execute($sql, $bind); return $this->execute($sql, $bind);