From 189ec6476f1a52c7cc4b9af52bc8c0a24dff2af3 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 26 Jan 2016 14:02:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bsql=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E4=B8=AD=E7=9A=84sql=E8=AF=AD=E5=8F=A5?= =?UTF-8?q?=E7=BB=84=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Driver.php | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/library/think/db/Driver.php b/library/think/db/Driver.php index 5b56b6fe..4c70d834 100644 --- a/library/think/db/Driver.php +++ b/library/think/db/Driver.php @@ -167,11 +167,9 @@ abstract class Driver return false; } - $this->queryStr = $sql; + // 根据参数绑定组装最终的SQL语句 + $this->queryStr = $this->getBindSql($sql, $bind); - if ($bind) { - $this->queryStr .= '[ ' . print_r($bind, true) . ' ]'; - } if ($fetch) { return $this->queryStr; } @@ -213,11 +211,9 @@ abstract class Driver return false; } - $this->queryStr = $sql; + // 根据参数绑定组装最终的SQL语句 + $this->queryStr = $this->getBindSql($sql, $bind); - if ($bind) { - $this->queryStr .= '[ ' . print_r($bind, true) . ' ]'; - } if ($fetch) { return $this->queryStr; } @@ -249,6 +245,27 @@ abstract class Driver } } + /** + * 组装最终的SQL语句 便于调试 + * @access public + * @param string $sql 带参数绑定的sql语句 + * @param array $bind 参数绑定列表 + * @return string + */ + protected function getBindSql($sql, array $bind = []) + { + if ($bind) { + foreach ($bind as $key => $val) { + $val = is_array($val) ? $val[0] : $val; + // 判断占位符 + $sql = is_numeric($key) ? + substr_replace($sql, $val, strpos($sql, '?'), 1) : + str_replace(':' . $key, $val, $sql); + } + } + return $sql; + } + /** * 参数绑定 * 支持 ['name'=>'value','id'=>123] 对应命名占位符