From 3eeccddd3c20deafb75c42e0fbdfd96a495a5dbe Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 29 Jan 2016 21:48:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/driver/Oracle.php | 28 +++++++++++++++++----------- library/think/db/driver/Sqlsrv.php | 7 ++++--- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/library/think/db/driver/Oracle.php b/library/think/db/driver/Oracle.php index dccb815c..5fc94eda 100644 --- a/library/think/db/driver/Oracle.php +++ b/library/think/db/driver/Oracle.php @@ -11,6 +11,7 @@ namespace think\db\driver; +use think\Config; use think\Db; use think\db\Driver; @@ -41,23 +42,26 @@ class Oracle extends Driver /** * 执行语句 * @access public - * @param string $str sql指令 + * @param string $sql sql指令 + * @param array $bind 参数绑定 + * @param boolean $fetch 不执行只是获取SQL * @return integer */ - public function execute($str, $bind = []) + public function execute($sql, $bind = [], $fetch = false) { $this->initConnect(true); if (!$this->linkID) { return false; } - $this->queryStr = $str; - if (!empty($bind)) { - $this->queryStr .= '[ ' . print_r($bind, true) . ' ]'; + // 根据参数绑定组装最终的SQL语句 + $this->queryStr = $this->getBindSql($sql, $bind); + if ($fetch) { + return $this->queryStr; } $flag = false; - if (preg_match("/^\s*(INSERT\s+INTO)\s+(\w+)\s+/i", $str, $match)) { - $this->table = C("DB_SEQUENCE_PREFIX") . str_ireplace(C("DB_PREFIX"), "", $match[2]); + if (preg_match("/^\s*(INSERT\s+INTO)\s+(\w+)\s+/i", $sql, $match)) { + $this->table = Config::get("db_sequence_prefix") . str_ireplace(Config::get("database.prefix"), "", $match[2]); $flag = (boolean) $this->query("SELECT * FROM user_sequences WHERE sequence_name='" . strtoupper($this->table) . "'"); } //释放前次的查询结果 @@ -69,16 +73,18 @@ class Oracle extends Driver try { // 记录开始执行时间 $this->debug(true); - $this->PDOStatement = $this->linkID->prepare($str); - $result = $this->PDOStatement->execute($bind); + $this->PDOStatement = $this->linkID->prepare($sql); + // 参数绑定操作 + $this->bindValue($bind); + $result = $this->PDOStatement->execute(); $this->debug(false); $this->numRows = $this->PDOStatement->rowCount(); - if ($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) { + if ($flag || preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $sql)) { $this->lastInsID = $this->linkID->lastInsertId(); } return $this->numRows; } catch (\PDOException $e) { - throw new Exception($e->getMessage()); + throw new Exception($this->getError()); } } diff --git a/library/think/db/driver/Sqlsrv.php b/library/think/db/driver/Sqlsrv.php index 470f5dac..e28df391 100644 --- a/library/think/db/driver/Sqlsrv.php +++ b/library/think/db/driver/Sqlsrv.php @@ -135,7 +135,6 @@ class Sqlsrv extends Driver } else { $limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND ' . $limit[0] . ")"; } - return 'WHERE ' . $limitStr; } @@ -149,13 +148,14 @@ class Sqlsrv extends Driver public function update($data, $options) { $this->model = $options['model']; + $this->bind = array_merge($this->bind, !empty($options['bind']) ? $options['bind'] : []); $sql = 'UPDATE ' . $this->parseTable($options['table']) . $this->parseSet($data) . $this->parseWhere(!empty($options['where']) ? $options['where'] : '') . $this->parseLock(isset($options['lock']) ? $options['lock'] : false) . $this->parseComment(!empty($options['comment']) ? $options['comment'] : ''); - return $this->execute($sql, $this->parseBind(!empty($options['bind']) ? $options['bind'] : [])); + return $this->execute($sql, $this->getBindParams(true), !empty($options['fetch_sql']) ? true : false); } /** @@ -167,12 +167,13 @@ class Sqlsrv extends Driver public function delete($options = []) { $this->model = $options['model']; + $this->bind = array_merge($this->bind, !empty($options['bind']) ? $options['bind'] : []); $sql = 'DELETE FROM ' . $this->parseTable($options['table']) . $this->parseWhere(!empty($options['where']) ? $options['where'] : '') . $this->parseLock(isset($options['lock']) ? $options['lock'] : false) . $this->parseComment(!empty($options['comment']) ? $options['comment'] : ''); - return $this->execute($sql, $this->parseBind(!empty($options['bind']) ? $options['bind'] : [])); + return $this->execute($sql, $this->getBindParams(true), !empty($options['fetch_sql']) ? true : false); } }