From 2139a0fe9d859ae7a61b641e3958bfaf0c0ccadf Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Sun, 5 Feb 2023 15:48:39 +0100 Subject: [PATCH] add support for array{schema, table} in %table modifier --- src/SqlProcessor.php | 12 +++++++++++- tests/cases/unit/SqlProcessorTest.scalar.phpt | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/SqlProcessor.php b/src/SqlProcessor.php index 1442ce19..a7f4dccc 100644 --- a/src/SqlProcessor.php +++ b/src/SqlProcessor.php @@ -42,7 +42,7 @@ class SqlProcessor 'multiOr' => [false, false, 'array'], // SQL constructs - 'table' => [false, true, 'string'], + 'table' => [false, true, 'string|array'], 'column' => [false, true, 'string'], 'values' => [false, true, 'array'], 'set' => [false, false, 'array'], @@ -338,6 +338,16 @@ public function processModifier(string $type, mixed $value): string return $this->platform->formatJson($value); // normal + case 'table': + $valueCount = count($value); + if ($valueCount == 0 || $valueCount > 2) { + throw new InvalidArgumentException("%table modifier expects array(table) or array(schema, table), got $valueCount values."); + } + foreach ($value as &$subValue) { + $subValue = $this->identifierToSql($subValue); + } + return implode('.', $value); + case 'column[]': case '...column[]': case 'table[]': diff --git a/tests/cases/unit/SqlProcessorTest.scalar.phpt b/tests/cases/unit/SqlProcessorTest.scalar.phpt index 575b1d66..b061f69e 100644 --- a/tests/cases/unit/SqlProcessorTest.scalar.phpt +++ b/tests/cases/unit/SqlProcessorTest.scalar.phpt @@ -143,11 +143,12 @@ class SqlProcessorScalarTest extends TestCase $this->platform->shouldReceive('formatIdentifier')->once()->with('b')->andReturn('B'); $this->platform->shouldReceive('formatIdentifier')->once()->with('c')->andReturn('C'); Assert::same('A, B, C', $this->parser->processModifier('table[]', ['a', 'b', 'c'])); + Assert::same('A.B', $this->parser->processModifier('table', ['a', 'b'])); Assert::exception(function () { // test break to process non-string values $this->parser->processModifier('table[]', [1]); - }, InvalidArgumentException::class, 'Modifier %table expects value to be string, integer given.'); + }, InvalidArgumentException::class, 'Modifier %table expects value to be string|array, integer given.'); }