Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpers::dumpSql() should parse named params #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 13 additions & 8 deletions src/Database/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,36 @@ public static function dumpSql($sql, array $params = NULL, Connection $connectio
}, $sql);

// parameters
$sql = preg_replace_callback('#\?#', function () use ($params, $connection) {
static $i = 0;
if (!isset($params[$i])) {
$sql = preg_replace_callback('#\?|\s?(?<meta>&lt;&gt;|&lt;|&gt;|=)\s?(?<param>\:[^\s\)]+)#', function ($matches) use ($params, $connection) {
static $i = 0;$meta = null;
if (isset($matches['param']) && isset($params[$matches['param']])) {
$param = $params[$matches['param']];
$meta = ' ' . $matches['meta'] . ' ';
} elseif (isset($params[$i])) {
$param = $params[$i++];
} else {
return '?';
}
$param = $params[$i++];

if (is_string($param) && (preg_match('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u', $param) || preg_last_error())) {
return '<i title="Length ' . strlen($param) . ' bytes">&lt;binary&gt;</i>';
return $meta . '<i title="Length ' . strlen($param) . ' bytes">&lt;binary&gt;</i>';

} elseif (is_string($param)) {
$length = Nette\Utils\Strings::length($param);
$truncated = Nette\Utils\Strings::truncate($param, self::$maxLength);
$text = htmlspecialchars($connection ? $connection->quote($truncated) : '\'' . $truncated . '\'', ENT_NOQUOTES, 'UTF-8');
return '<span title="Length ' . $length . ' characters">' . $text . '</span>';
return $meta . '<span title="Length ' . $length . ' characters">' . $text . '</span>';

} elseif (is_resource($param)) {
$type = get_resource_type($param);
if ($type === 'stream') {
$info = stream_get_meta_data($param);
}
return '<i' . (isset($info['uri']) ? ' title="' . htmlspecialchars($info['uri'], ENT_NOQUOTES, 'UTF-8') . '"' : NULL)
return $meta . '<i' . (isset($info['uri']) ? ' title="' . htmlspecialchars($info['uri'], ENT_NOQUOTES, 'UTF-8') . '"' : NULL)
. '>&lt;' . htmlSpecialChars($type, ENT_NOQUOTES, 'UTF-8') . ' resource&gt;</i> ';

} else {
return htmlspecialchars($param, ENT_NOQUOTES, 'UTF-8');
return $meta . htmlspecialchars($param, ENT_NOQUOTES, 'UTF-8');
}
}, $sql);

Expand Down
15 changes: 15 additions & 0 deletions tests/Database/Helpers.dumpSql.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ test(function () use ($connection) { // string check
"<pre class=\"dump\"><strong style=\"color:blue\">SELECT</strong> id \n<strong style=\"color:blue\">FROM</strong> author \n<strong style=\"color:blue\">WHERE</strong> name = <span title=\"Length 15 characters\">'Alexej Chruščev'</span></pre>\n", Nette\Database\Helpers::dumpSql('SELECT id FROM author WHERE name = ?', ['Alexej Chruščev'], $connection));
});

test(function () use ($connection) { // named param
Assert::same(
"<pre class=\"dump\"><strong style=\"color:blue\">SELECT</strong> id \n<strong style=\"color:blue\">FROM</strong> author \n<strong style=\"color:blue\">WHERE</strong> name = <span title=\"Length 15 characters\">'Alexej Chruščev'</span></pre>\n", Nette\Database\Helpers::dumpSql('SELECT id FROM author WHERE name = :name', [':name' => 'Alexej Chruščev'], $connection));
});

test(function () use ($connection) { // named param with compare
Assert::same(
"<pre class=\"dump\"><strong style=\"color:blue\">SELECT</strong> id \n<strong style=\"color:blue\">FROM</strong> author \n<strong style=\"color:blue\">WHERE</strong> name &lt;&gt; <span title=\"Length 15 characters\">'Alexej Chruščev'</span></pre>\n", Nette\Database\Helpers::dumpSql('SELECT id FROM author WHERE name <> :name', [':name' => 'Alexej Chruščev'], $connection));
});

test(function () use ($connection) { // named param with compare
Assert::same(
"<pre class=\"dump\"><strong style=\"color:blue\">SELECT</strong> id \n<strong style=\"color:blue\">FROM</strong> author \n<strong style=\"color:blue\">WHERE</strong> name &gt; <span title=\"Length 15 characters\">'Alexej Chruščev'</span></pre>\n", Nette\Database\Helpers::dumpSql('SELECT id FROM author WHERE name > :name', [':name' => 'Alexej Chruščev'], $connection));
});

test(function () use ($connection) { // string check with \'
Assert::same(
"<pre class=\"dump\"><strong style=\"color:blue\">SELECT</strong> id \n<strong style=\"color:blue\">FROM</strong> author \n<strong style=\"color:blue\">WHERE</strong> name = <span title=\"Length 16 characters\">'Alexej Ch\'ruščev'</span></pre>\n", Nette\Database\Helpers::dumpSql('SELECT id FROM author WHERE name = ?', ["Alexej Ch'ruščev"], $connection));
Expand Down