diff --git a/src/SqlProcessor.php b/src/SqlProcessor.php index 78362d1f..9673a252 100644 --- a/src/SqlProcessor.php +++ b/src/SqlProcessor.php @@ -72,7 +72,7 @@ public function process(array $args) $i = $j; $fragments[] = preg_replace_callback( - '#%(\??+\w++(?:\[\]){0,2}+)|\[(.+?)\]#S', // %modifier | [identifier] + '#%(\??+\w++(?:\[\]){0,2}+)|(%%)|\[(.+?)\]#S', // %modifier | %% | [identifier] function ($matches) use ($args, &$j, $last) { if ($matches[1] !== '') { if ($j === $last) { @@ -80,11 +80,14 @@ function ($matches) use ($args, &$j, $last) { } return $this->processModifier($matches[1], $args[++$j]); - } elseif (!ctype_digit($matches[2])) { - return $this->identifiers->{$matches[2]}; + } elseif ($matches[2] !== '') { + return '%'; + + } elseif (!ctype_digit($matches[3])) { + return $this->identifiers->{$matches[3]}; } else { - return "[$matches[2]]"; + return "[$matches[3]]"; } }, $args[$i] diff --git a/tests/cases/unit/SqlProcessorTest.process.phpt b/tests/cases/unit/SqlProcessorTest.process.phpt index 6202f5b6..41280615 100644 --- a/tests/cases/unit/SqlProcessorTest.process.phpt +++ b/tests/cases/unit/SqlProcessorTest.process.phpt @@ -61,6 +61,15 @@ class SqlProcessorProcessTest extends TestCase } + public function testEscape() + { + Assert::same( + 'SELECT DATE_FORMAT(publishedDate, "%Y") AS year FROM foo', + $this->parser->process(['SELECT DATE_FORMAT(publishedDate, "%%Y") AS year FROM foo']) + ); + } + + public function testWrongArguments() { Assert::throws(function() {