改进sql日志记录中的sql语句组装

This commit is contained in:
thinkphp
2016-01-26 14:02:27 +08:00
parent 64bb1f4b23
commit 189ec6476f

View File

@@ -167,11 +167,9 @@ abstract class Driver
return false; return false;
} }
$this->queryStr = $sql; // 根据参数绑定组装最终的SQL语句
$this->queryStr = $this->getBindSql($sql, $bind);
if ($bind) {
$this->queryStr .= '[ ' . print_r($bind, true) . ' ]';
}
if ($fetch) { if ($fetch) {
return $this->queryStr; return $this->queryStr;
} }
@@ -213,11 +211,9 @@ abstract class Driver
return false; return false;
} }
$this->queryStr = $sql; // 根据参数绑定组装最终的SQL语句
$this->queryStr = $this->getBindSql($sql, $bind);
if ($bind) {
$this->queryStr .= '[ ' . print_r($bind, true) . ' ]';
}
if ($fetch) { if ($fetch) {
return $this->queryStr; 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] 对应命名占位符 * 支持 ['name'=>'value','id'=>123] 对应命名占位符