From 00b0c0f229edd1bfd19dab4aaf15d028e0b93183 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 12 Jun 2016 22:47:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=20=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=E6=9F=A5=E8=AF=A2=E6=96=B9=E6=B3=95=E7=9A=84SQL?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 8 ++- library/think/db/Connection.php | 26 +++------- library/think/db/Query.php | 71 +++++++++++++++++---------- library/think/db/connector/Oracle.php | 8 ++- 4 files changed, 57 insertions(+), 56 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index c1ab43c8..b6be17b2 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -292,8 +292,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected function writeTransform($value, $type) { if(is_array($type)){ - $param = $type[1]; - $type = $type[0]; + list($type,$param) = $type; }elseif (strpos($type, ':')) { list($type, $param) = explode(':', $type, 2); } @@ -328,7 +327,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess case 'array': $value = (array) $value; case 'json': - $option = !empty($param) ? (intval)$param : JSON_UNESCAPED_UNICODE; + $option = !empty($param) ? (int)$param : JSON_UNESCAPED_UNICODE; $value = json_encode($value, $option); break; case 'serialize': @@ -374,8 +373,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected function readTransform($value, $type) { if(is_array($type)){ - $param = $type[1]; - $type = $type[0]; + list($type,$param) = $type; }elseif (strpos($type, ':')) { list($type, $param) = explode(':', $type, 2); } diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index f77df8b7..ab18a719 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -28,11 +28,6 @@ abstract class Connection /** @var PDOStatement PDO操作实例 */ protected $PDOStatement; - // 当前操作的数据表名 - protected $table = ''; - // 当前操作的数据对象名 - protected $name = ''; - /** @var string 当前SQL指令 */ protected $queryStr = ''; // 最后插入ID @@ -322,26 +317,21 @@ abstract class Connection * @access public * @param string $sql sql指令 * @param array $bind 参数绑定 - * @param boolean $fetch 不执行只是获取SQL * @param boolean $master 是否在主服务器读操作 * @param bool|string $class 指定返回的数据集对象 * @return mixed * @throws BindParamException * @throws PDOException */ - public function query($sql, $bind = [], $fetch = false, $master = false, $class = false) + public function query($sql, $bind = [], $master = false, $class = false) { $this->initConnect($master); if (!$this->linkID) { return false; } - // 根据参数绑定组装最终的SQL语句 - $this->queryStr = $this->getBindSql($sql, $bind); - - if ($fetch) { - return $this->queryStr; - } + $this->queryStr = $this->getRealSql($sql, $bind); + //释放前次的查询结果 if (!empty($this->PDOStatement)) { $this->free(); @@ -371,25 +361,21 @@ abstract class Connection * @access public * @param string $sql sql指令 * @param array $bind 参数绑定 - * @param boolean $fetch 不执行只是获取SQL * @param boolean $getLastInsID 是否获取自增ID * @param string $sequence 自增序列名 * @return int * @throws BindParamException * @throws PDOException */ - public function execute($sql, $bind = [], $fetch = false, $getLastInsID = false, $sequence = null) + public function execute($sql, $bind = [], $getLastInsID = false, $sequence = null) { $this->initConnect(true); if (!$this->linkID) { return false; } // 根据参数绑定组装最终的SQL语句 - $this->queryStr = $this->getBindSql($sql, $bind); + $this->queryStr = $this->getRealSql($sql, $bind); - if ($fetch) { - return $this->queryStr; - } //释放前次的查询结果 if (!empty($this->PDOStatement)) { $this->free(); @@ -428,7 +414,7 @@ abstract class Connection * @param array $bind 参数绑定列表 * @return string */ - protected function getBindSql($sql, array $bind = []) + public function getRealSql($sql, array $bind = []) { if ($bind) { foreach ($bind as $key => $val) { diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 1ecc72d1..4eee12ca 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1521,7 +1521,7 @@ class Query * @param boolean $replace 是否replace * @param boolean $getLastInsID 是否获取自增ID * @param string $sequence 自增序列名 - * @return integer + * @return integer|string */ public function insert(array $data, $replace = false, $getLastInsID = false, $sequence = null) { @@ -1529,9 +1529,13 @@ class Query $options = $this->parseExpress(); // 生成SQL语句 $sql = $this->builder()->insert($data, $options, $replace); + if($options['fetch_sql']){ + // 获取实际执行的SQL语句 + return $this->connection->getRealSql($sql,$this->bind); + } $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); // 执行操作 - return $this->execute($sql, $this->getBind(), $options['fetch_sql'], $getLastInsID, $sequence); + return $this->execute($sql, $this->getBind(), $getLastInsID, $sequence); } /** @@ -1540,7 +1544,7 @@ class Query * @param mixed $data 数据 * @param boolean $replace 是否replace * @param string $sequence 自增序列名 - * @return integer + * @return integer|string */ public function insertGetId(array $data, $replace = false, $sequence = null) { @@ -1551,7 +1555,7 @@ class Query * 批量插入记录 * @access public * @param mixed $dataSet 数据集 - * @return integer + * @return integer|string */ public function insertAll(array $dataSet) { @@ -1562,8 +1566,13 @@ class Query } // 生成SQL语句 $sql = $this->builder()->insertAll($dataSet, $options); - // 执行操作 - return $this->execute($sql, $this->getBind(), $options['fetch_sql']); + if($options['fetch_sql']){ + // 获取实际执行的SQL语句 + return $this->connection->getRealSql($sql,$this->bind); + }else{ + // 执行操作 + return $this->execute($sql, $this->getBind()); + } } /** @@ -1571,7 +1580,7 @@ class Query * @access public * @param string $fields 要插入的数据表字段名 * @param string $table 要插入的数据表名 - * @return int + * @return integer|string * @throws PDOException */ public function selectInsert($fields, $table) @@ -1580,15 +1589,20 @@ class Query $options = $this->parseExpress(); // 生成SQL语句 $sql = $this->builder()->selectInsert($fields, $table, $options); - // 执行操作 - return $this->execute($sql, $this->getBind(), $options['fetch_sql']); + if($options['fetch_sql']){ + // 获取实际执行的SQL语句 + return $this->connection->getRealSql($sql,$this->bind); + }else{ + // 执行操作 + return $this->execute($sql, $this->getBind()); + } } /** * 更新记录 * @access public * @param mixed $data 数据 - * @return int + * @return integer|string * @throws Exception * @throws PDOException */ @@ -1622,11 +1636,13 @@ class Query } // 生成UPDATE SQL语句 $sql = $this->builder()->update($data, $options); - if ('' == $sql) { - return 0; + if($options['fetch_sql']){ + // 获取实际执行的SQL语句 + return $this->connection->getRealSql($sql,$this->bind); + }else{ + // 执行操作 + return '' == $sql ? 0 : $this->execute($sql, $this->getBind()); } - // 执行操作 - return $this->execute($sql, $this->getBind(), $options['fetch_sql']); } /** @@ -1666,13 +1682,13 @@ class Query if (!$resultSet) { // 生成查询SQL $sql = $this->builder()->select($options); + if($options['fetch_sql']){ + // 获取实际执行的SQL语句 + return $this->connection->getRealSql($sql,$this->bind); + } // 执行查询操作 - $resultSet = $this->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_class']); + $resultSet = $this->query($sql, $this->getBind(), $options['master'], $options['fetch_class']); - if (is_string($resultSet)) { - // 返回SQL - return $resultSet; - } if ($resultSet instanceof \PDOStatement) { // 返回PDOStatement对象 return $resultSet; @@ -1747,13 +1763,12 @@ class Query if (!$result) { // 生成查询SQL $sql = $this->builder()->select($options); + if($options['fetch_sql']){ + // 获取实际执行的SQL语句 + return $this->connection->getRealSql($sql,$this->bind); + } // 执行查询 - $result = $this->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_class']); - - if (is_string($result)) { - // 返回SQL - return $result; - } + $result = $this->query($sql, $this->getBind(), $options['master'], $options['fetch_class']); if ($result instanceof \PDOStatement) { // 返回PDOStatement对象 @@ -1902,8 +1917,12 @@ class Query } // 生成删除SQL语句 $sql = $this->builder()->delete($options); + if($options['fetch_sql']){ + // 获取实际执行的SQL语句 + return $this->getRealSql($sql,$this->bind); + } // 执行操作 - return $this->execute($sql, $this->getBind(), $options['fetch_sql']); + return $this->execute($sql, $this->getBind()); } /** diff --git a/library/think/db/connector/Oracle.php b/library/think/db/connector/Oracle.php index a9b7e102..1e2a4372 100644 --- a/library/think/db/connector/Oracle.php +++ b/library/think/db/connector/Oracle.php @@ -46,14 +46,13 @@ class Oracle extends Connection * @access public * @param string $sql sql指令 * @param array $bind 参数绑定 - * @param boolean $fetch 不执行只是获取SQL * @param boolean $getLastInsID 是否获取自增ID * @param string $sequence 序列名 * @return integer * @throws \Exception * @throws \think\Exception */ - public function execute($sql, $bind = [], $fetch = false, $getLastInsID = false, $sequence = null) + public function execute($sql, $bind = [], $getLastInsID = false, $sequence = null) { $this->initConnect(true); if (!$this->linkID) { @@ -62,9 +61,7 @@ class Oracle extends Connection // 根据参数绑定组装最终的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", $sql, $match)) { if(is_null($sequence)){ @@ -72,6 +69,7 @@ class Oracle extends Connection } $flag = (boolean) $this->query("SELECT * FROM all_sequences WHERE sequence_name='" . strtoupper($sequence) . "'"); } + //释放前次的查询结果 if (!empty($this->PDOStatement)) { $this->free();