1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| /**
* SQL 日志转换为字符串
*
* @param string $sql SQL预处理
* @param string $bindings 绑定的参数
* @param string $time 执行时间
* @return string 返回一个解析完成的sql (带时间)
*/
function laravel_sql_parse($sql, $bindings, $time = null)
{
$bindings = array_map(function($bind){
if ($bind instanceof \DateTime) {
$bind = $bind->format('Y-m-d H:i:s');
}
return is_numeric($bind) ? $bind : "'{$bind}'";
}, $bindings);
array_unshift($bindings, str_replace(['%', '?'], ['%%', '%s'], $sql));
$sql_str = count($bindings) == 1 ? current($bindings) : call_user_func_array("sprintf", $bindings);
return $sql_str . ( is_null($time) ? "" : " [{$time}ms]");
}
|