From e151d70f62fdc3b07769dabbf7873084394a15fd Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 19 May 2016 12:38:11 +0800 Subject: [PATCH] =?UTF-8?q?query=E7=B1=BB=E5=A2=9E=E5=8A=A0sequence?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20=E7=94=A8=E4=BA=8E=E8=AE=BE=E7=BD=AE=20?= =?UTF-8?q?=E8=87=AA=E5=A2=9E=E5=BA=8F=E5=88=97=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 5 +++-- library/think/db/Query.php | 14 +++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 22eebb89..dfa9ca49 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -367,11 +367,12 @@ abstract class Connection * @param array $bind 参数绑定 * @param boolean $fetch 不执行只是获取SQL * @param boolean $getLastInsID 是否获取自增ID + * @param string $sequence 自增序列名 * @return int * @throws DbBindParamException * @throws PDOException */ - public function execute($sql, $bind = [], $fetch = false, $getLastInsID = false) + public function execute($sql, $bind = [], $fetch = false, $getLastInsID = false, $sequence = null) { $this->initConnect(true); if (!$this->linkID) { @@ -403,7 +404,7 @@ abstract class Connection $this->numRows = $this->PDOStatement->rowCount(); if (preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $sql)) { - $this->lastInsID = $this->linkID->lastInsertId(); + $this->lastInsID = $this->linkID->lastInsertId($sequence); if ($getLastInsID) { return $this->lastInsID; } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index f327f78d..8bc4ea7b 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1059,6 +1059,18 @@ class Query return $this; } + /** + * 设置自增序列名 + * @access public + * @param string $sequence 自增序列名 + * @return $this + */ + public function sequence($sequence = null) + { + $this->options['sequence'] = $sequence; + return $this; + } + /** * 获取数据表信息 * @access public @@ -1312,7 +1324,7 @@ class Query // 生成SQL语句 $sql = $this->builder()->insert($data, $options, $replace); // 执行操作 - return $this->execute($sql, $this->getBind(), $options['fetch_sql'], $getLastInsID); + return $this->execute($sql, $this->getBind(), $options['fetch_sql'], $getLastInsID, isset($options['sequence']) ? $options['sequence'] : null); } /**