Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Loggers/Formatters/ReplaceQueryParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -42,9 +42,25 @@ protected function convertParam($param)
}
}
} elseif (is_array($param)) {
$param = implode(',', $param);
if (count($param) !== count($param, COUNT_RECURSIVE)) {
$param = json_encode($param, JSON_UNESCAPED_UNICODE);
} else {
$param = implode(
', ',
array_map(
function ($part) {
return '"' . (string) $part . '"';
},
$param
)
);

return '(' . $param . ')';
}
} else {
$param = e($param);
}

return (string) e($param);
return '"' . (string) $param . '"';
}
}
22 changes: 22 additions & 0 deletions tests/Loggers/Formatters/ReplaceQueryParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down