Skip to content

Commit

Permalink
Fix #14
Browse files Browse the repository at this point in the history
  • Loading branch information
filisko committed Jun 17, 2021
1 parent 3a3ce5f commit 519c6e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,43 @@ public function execute($params = null)
$result = parent::execute($params);

$this->pdo->addLog(
$this->getStatementWithBindingsIn($this->bindings, $this->queryString),
$this->produceStatementWithBindingsInForLogging($this->bindings, $this->queryString),
microtime(true) - $start
);

return $result;
}

private function getStatementWithBindingsIn(array $bindings, string $query)
private function produceStatementWithBindingsInForLogging(array $bindings, string $query)
{
$indexed = ($bindings == array_values($bindings));

foreach ($bindings as $param => $value) {
$value = is_numeric($value) || $value === null ? $value : $this->pdo->quote($value);
$value = is_null($value) ? 'null' : $value;
$valueForPresentation = $this->translateValueForPresentationInsideStatement($value);

if ($indexed) {
$query = preg_replace('/\?/', (string)$value, $query, 1);
$query = preg_replace('/\?/', (string)$valueForPresentation, $query, 1);
} else {
$query = str_replace(":$param", (string)$value, $query);
$query = str_replace(":$param", (string)$valueForPresentation, $query);
}
}

return $query;
}

private function translateValueForPresentationInsideStatement(mixed $value): mixed
{
$result = $value;

if ($value === null) {
$result = 'null';
} elseif (is_string($value)) {
$result = $this->pdo->quote($value);
} elseif (is_bool($value) && $value === false) {
$result = 'false';
} elseif (is_bool($value) && $value === true) {
$result = 'true';
}
return $result;
}
}
5 changes: 5 additions & 0 deletions tests/PDOStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public function dataForTestAddValuesToQuery()
'SELECT * FROM users WHERE `name` = ?',
"SELECT * FROM users WHERE `name` = null"
],
[
[false, true],
'SELECT * FROM users WHERE `name` = ? OR `name` = ?',
"SELECT * FROM users WHERE `name` = false OR `name` = true"
],
[
[1],
'SELECT * FROM users WHERE `id` = ?',
Expand Down

0 comments on commit 519c6e6

Please sign in to comment.