From cd8b6dfd5884af7912e6a919a1e119b2b1af3ddb Mon Sep 17 00:00:00 2001 From: insekticid Date: Wed, 15 Jul 2015 16:54:49 +0200 Subject: [PATCH] Helpers::dumpSql() should parse named params #77 --- src/Database/Helpers.php | 21 +++++++++++++-------- tests/Database/Helpers.dumpSql.phpt | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Database/Helpers.php b/src/Database/Helpers.php index cd80b879f..ee4a3215f 100644 --- a/src/Database/Helpers.php +++ b/src/Database/Helpers.php @@ -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?(?<>|<|>|=)\s?(?\:[^\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 '<binary>'; + return $meta . '<binary>'; } 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 '' . $text . ''; + return $meta . '' . $text . ''; } elseif (is_resource($param)) { $type = get_resource_type($param); if ($type === 'stream') { $info = stream_get_meta_data($param); } - return '<' . htmlSpecialChars($type, ENT_NOQUOTES, 'UTF-8') . ' resource> '; } else { - return htmlspecialchars($param, ENT_NOQUOTES, 'UTF-8'); + return $meta . htmlspecialchars($param, ENT_NOQUOTES, 'UTF-8'); } }, $sql); diff --git a/tests/Database/Helpers.dumpSql.phpt b/tests/Database/Helpers.dumpSql.phpt index 49a33db5d..7c96fe6f7 100644 --- a/tests/Database/Helpers.dumpSql.phpt +++ b/tests/Database/Helpers.dumpSql.phpt @@ -22,6 +22,21 @@ test(function () use ($connection) { // string check "
SELECT id \nFROM author \nWHERE name = 'Alexej Chruščev'
\n", Nette\Database\Helpers::dumpSql('SELECT id FROM author WHERE name = ?', ['Alexej Chruščev'], $connection)); }); +test(function () use ($connection) { // named param + Assert::same( +"
SELECT id \nFROM author \nWHERE name = 'Alexej Chruščev'
\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( +"
SELECT id \nFROM author \nWHERE name <> 'Alexej Chruščev'
\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( +"
SELECT id \nFROM author \nWHERE name > 'Alexej Chruščev'
\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( "
SELECT id \nFROM author \nWHERE name = 'Alexej Ch\'ruščev'
\n", Nette\Database\Helpers::dumpSql('SELECT id FROM author WHERE name = ?', ["Alexej Ch'ruščev"], $connection));