Skip to content

Commit

Permalink
Merge pull request #198 from nextras/table-modifier
Browse files Browse the repository at this point in the history
Add support for `array{schema, table}` in `%table` modifier
  • Loading branch information
hrach authored Feb 5, 2023
2 parents 457b255 + e61fbae commit 0c62a78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/SqlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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("Modifier %table expects array(table) or array(schema, table), $valueCount values given.");
}
foreach ($value as &$subValue) {
$subValue = $this->identifierToSql($subValue);
}
return implode('.', $value);

case 'column[]':
case '...column[]':
case 'table[]':
Expand Down
7 changes: 6 additions & 1 deletion tests/cases/unit/SqlProcessorTest.scalar.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,16 @@ 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.');

Assert::exception(function () {
$this->parser->processModifier('table', ['a', 'b', 'c']);
}, InvalidArgumentException::class, 'Modifier %table expects array(table) or array(schema, table), 3 values given.');
}


Expand Down

0 comments on commit 0c62a78

Please # to comment.