insertall支持replace

This commit is contained in:
thinkphp
2017-05-21 00:35:25 +08:00
parent 7deea9dd07
commit 253f2bfc7b
2 changed files with 8 additions and 5 deletions

View File

@@ -27,7 +27,7 @@ abstract class Builder
// SQL表达式 // SQL表达式
protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%LOCK%%COMMENT%'; protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%LOCK%%COMMENT%';
protected $insertSql = '%INSERT% INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%'; protected $insertSql = '%INSERT% INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%';
protected $insertAllSql = 'INSERT INTO %TABLE% (%FIELD%) %DATA% %COMMENT%'; protected $insertAllSql = '%INSERT% INTO %TABLE% (%FIELD%) %DATA% %COMMENT%';
protected $updateSql = 'UPDATE %TABLE% SET %SET% %JOIN% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%'; protected $updateSql = 'UPDATE %TABLE% SET %SET% %JOIN% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%';
protected $deleteSql = 'DELETE FROM %TABLE% %USING% %JOIN% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%'; protected $deleteSql = 'DELETE FROM %TABLE% %USING% %JOIN% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%';
@@ -719,9 +719,10 @@ abstract class Builder
* @access public * @access public
* @param array $dataSet 数据集 * @param array $dataSet 数据集
* @param array $options 表达式 * @param array $options 表达式
* @param bool $replace 是否replace
* @return string * @return string
*/ */
public function insertAll($dataSet, $options) public function insertAll($dataSet, $options, $replace = false)
{ {
// 获取合法的字段 // 获取合法的字段
if ('*' == $options['field']) { if ('*' == $options['field']) {
@@ -754,8 +755,9 @@ abstract class Builder
} }
$fields = array_map([$this, 'parseKey'], array_keys(reset($dataSet))); $fields = array_map([$this, 'parseKey'], array_keys(reset($dataSet)));
$sql = str_replace( $sql = str_replace(
['%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'], ['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
[ [
$replace ? 'REPLACE' : 'INSERT',
$this->parseTable($options['table'], $options), $this->parseTable($options['table'], $options),
implode(' , ', $fields), implode(' , ', $fields),
implode(' UNION ALL ', $values), implode(' UNION ALL ', $values),

View File

@@ -2117,9 +2117,10 @@ class Query
* 批量插入记录 * 批量插入记录
* @access public * @access public
* @param mixed $dataSet 数据集 * @param mixed $dataSet 数据集
* @param boolean $replace 是否replace
* @return integer|string * @return integer|string
*/ */
public function insertAll(array $dataSet) public function insertAll(array $dataSet, $replace = false)
{ {
// 分析查询表达式 // 分析查询表达式
$options = $this->parseExpress(); $options = $this->parseExpress();
@@ -2127,7 +2128,7 @@ class Query
return false; return false;
} }
// 生成SQL语句 // 生成SQL语句
$sql = $this->builder->insertAll($dataSet, $options); $sql = $this->builder->insertAll($dataSet, $options, $replace);
// 获取参数绑定 // 获取参数绑定
$bind = $this->getBind(); $bind = $this->getBind();
if ($options['fetch_sql']) { if ($options['fetch_sql']) {