From 402fbe8be5064539cd464fc7bd67a58a7da1b29e Mon Sep 17 00:00:00 2001 From: Zoozy Date: Thu, 12 Nov 2015 18:35:59 +0800 Subject: [PATCH 1/3] LogFormatter convertParams fix for passing nested array as params --- src/Loggers/Formatters/ReplaceQueryParams.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Loggers/Formatters/ReplaceQueryParams.php b/src/Loggers/Formatters/ReplaceQueryParams.php index 8ad95b17..3657c12f 100644 --- a/src/Loggers/Formatters/ReplaceQueryParams.php +++ b/src/Loggers/Formatters/ReplaceQueryParams.php @@ -42,7 +42,11 @@ protected function convertParam($param) } } } elseif (is_array($param)) { - $param = implode(',', $param); + if (count($param) === count($param, COUNT_RECURSIVE)) { + $param = implode(',', $param); + } else { + $param = json_encode($param); + } } return (string) e($param); From b23321c6998c36640e0f8ad661ef3a8f7a8e0330 Mon Sep 17 00:00:00 2001 From: Zoozy Date: Thu, 12 Nov 2015 20:24:36 +0800 Subject: [PATCH 2/3] LogFormatter convertParams fix for passing array and nested array as params --- src/Loggers/Formatters/ReplaceQueryParams.php | 20 ++++++++++++----- .../Formatters/ReplaceQueryParamsTest.php | 22 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/Loggers/Formatters/ReplaceQueryParams.php b/src/Loggers/Formatters/ReplaceQueryParams.php index 3657c12f..f306f8e9 100644 --- a/src/Loggers/Formatters/ReplaceQueryParams.php +++ b/src/Loggers/Formatters/ReplaceQueryParams.php @@ -18,7 +18,7 @@ public function format($sql, array $params = null) if (is_array($params)) { foreach ($params as $param) { $param = $this->convertParam($param); - $sql = preg_replace('/\?/', "\"$param\"", $sql, 1); + $sql = preg_replace('/\?/', "$param", $sql, 1); } } @@ -42,13 +42,23 @@ protected function convertParam($param) } } } elseif (is_array($param)) { - if (count($param) === count($param, COUNT_RECURSIVE)) { - $param = implode(',', $param); + if (count($param) !== count($param, COUNT_RECURSIVE)) { + $param = json_encode($param, JSON_UNESCAPED_UNICODE); } else { - $param = json_encode($param); + $param = implode( + ', ', + array_map( + function ($part) { + return '"' . (string) $part . '"'; + }, + $param + ) + ); + + return '(' . $param . ')'; } } - return (string) e($param); + return '"' . (string) $param . '"'; } } diff --git a/tests/Loggers/Formatters/ReplaceQueryParamsTest.php b/tests/Loggers/Formatters/ReplaceQueryParamsTest.php index 93356427..f0071acb 100644 --- a/tests/Loggers/Formatters/ReplaceQueryParamsTest.php +++ b/tests/Loggers/Formatters/ReplaceQueryParamsTest.php @@ -73,6 +73,28 @@ public function test_can_replace_datetime_objects() ); } + public function test_can_replace_array_param() + { + $sql = 'SELECT * FROM table WHERE column IN ?'; + $params = [['value1', 'value2']]; + + $this->assertEquals( + 'SELECT * FROM table WHERE column IN ("value1", "value2")', + $this->formatter->format($sql, $params) + ); + } + + public function test_can_replace_nested_array_param() + { + $sql = 'SELECT * FROM table WHERE column = ?'; + $params = [['key1' => 'value1', 'key2' => ['key21' => 'value22']]]; + + $this->assertEquals( + 'SELECT * FROM table WHERE column = "' . json_encode(reset($params), JSON_UNESCAPED_UNICODE) . '"', + $this->formatter->format($sql, $params) + ); + } + protected function tearDown() { m::close(); From 656005d7ba4332170915d724b200e0129981edb0 Mon Sep 17 00:00:00 2001 From: Zoozy Date: Fri, 13 Nov 2015 10:13:24 +0800 Subject: [PATCH 3/3] convert normal param using laravel e helper --- src/Loggers/Formatters/ReplaceQueryParams.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Loggers/Formatters/ReplaceQueryParams.php b/src/Loggers/Formatters/ReplaceQueryParams.php index f306f8e9..4e446e51 100644 --- a/src/Loggers/Formatters/ReplaceQueryParams.php +++ b/src/Loggers/Formatters/ReplaceQueryParams.php @@ -57,6 +57,8 @@ function ($part) { return '(' . $param . ')'; } + } else { + $param = e($param); } return '"' . (string) $param . '"';