mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
改进insertall方法的分批处理
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2128,24 +2128,37 @@ 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语句
|
||||||
$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();
|
$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);
|
||||||
|
|||||||
Reference in New Issue
Block a user