Skip to content

Commit 54f29a2

Browse files
committed
Unify table references under expanded TableIdentifier
Centralise all table storage and rendering into TableIdentifier, eliminating TableRef and JoinTableType which duplicated alias-extraction and type-dispatch logic across From, JoinSpec, and Join. TableIdentifier now accepts string|array|Select|ExpressionInterface tables, optional schema and alias, and provides resolveTable(), resolveTableWithAlias(), and getQuotedPrefix() for rendering. - Expand TableIdentifier with alias, array unwrapping, and rendering methods - Simplify From to store TableIdentifier, delegate rendering - Simplify JoinSpec to store TableIdentifier, drop tableType/alias fields - Replace Join::toSql() 4-case match with resolveTableWithAlias() - Replace Columns join prefix logic with getQuotedPrefix() - Simplify SqlProcessor::resolveTable() to delegate to TableIdentifier - Switch Insert/InsertSelect/InsertValues to From::renderTable() - Update SqlFragment::wrap() to support no-arg mode - Guard Select::buildSqlString wrap() to only wrap on combine - Delete TableRef and JoinTableType
1 parent d8d4748 commit 54f29a2

File tree

14 files changed

+139
-167
lines changed

14 files changed

+139
-167
lines changed

src/Sql/Insert.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function buildSqlString(
158158
$processor->setParamPrefix($this->processInfo['paramPrefix']);
159159

160160
$keyword = $this->getStatementKeyword();
161-
$tableSql = $processor->resolveTable($this->table->get());
161+
$tableSql = $this->table->renderTable($processor);
162162

163163
if ($this->select !== null) {
164164
$selectSql = $processor->processSubSelect($this->select);

src/Sql/Join.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Override;
1010
use PhpDb\Sql\Part\AbstractPart;
1111
use PhpDb\Sql\Part\JoinSpec;
12-
use PhpDb\Sql\Part\JoinTableType;
1312
use PhpDb\Sql\Part\SqlProcessor;
1413
use ReturnTypeWillChange;
1514

@@ -36,7 +35,8 @@
3635
*/
3736
class Join extends AbstractPart implements Iterator, Countable
3837
{
39-
private const IDENTIFIER_PATTERN = '/\b(?!(?:AS|AND|OR|BETWEEN)\b)([a-zA-Z_]\w*+(?:\.[a-zA-Z_]\w*+)*)(?!\s*\()/i';
38+
private const IDENTIFIER_PATTERN
39+
= '/\b(?!(?:AS|AND|OR|BETWEEN)\b)([a-zA-Z_]\w*+(?:\.[a-zA-Z_]\w*+)*)(?!\s*\()/i';
4040

4141
final public const JOIN_INNER = 'INNER';
4242

@@ -179,17 +179,7 @@ public function toSql(SqlProcessor $processor, string $paramPrefix = '', int &$p
179179
$joinSqlParts = [];
180180

181181
foreach ($this->specs as $j => $spec) {
182-
$joinName = match ($spec->tableType) {
183-
JoinTableType::Identifier => $platform->quoteIdentifier($spec->table),
184-
JoinTableType::TableIdentifier => $processor->resolveTable($spec->table),
185-
JoinTableType::Expression => $spec->table->getExpression(), // @phpstan-ignore method.nonObject
186-
JoinTableType::Select => '(' . $processor->processSubSelect($spec->table) . ')',
187-
};
188-
$quotedAlias = $spec->alias !== null
189-
? $platform->quoteIdentifier($spec->alias)
190-
: null;
191-
192-
$renderedTable = $processor->renderTable($joinName, $quotedAlias);
182+
$renderedTable = $spec->table->resolveTableWithAlias($processor);
193183

194184
if ($spec->isExpressionOn) {
195185
$onClause = $processor->renderExpression(

src/Sql/Part/Columns.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ public function toSql(SqlProcessor $processor, string $paramPrefix = '', int &$p
6969
}
7070

7171
if ($this->joinSpecs !== []) {
72-
$separator = $processor->identifierSeparator;
7372
foreach ($this->joinSpecs as $spec) {
7473
if ($spec->columnRefs === []) {
7574
continue;
7675
}
77-
$joinPrefix = $processor->resolveTable($spec->alias ?? $spec->table) . $separator;
76+
$joinPrefix = $spec->table->getQuotedPrefix($processor);
7877
foreach ($spec->columnRefs as $ref) {
7978
if ($ref->isStar) {
8079
$fragments[] = $joinPrefix . '*';

src/Sql/Part/From.php

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
namespace PhpDb\Sql\Part;
66

7+
use PhpDb\Sql\ExpressionInterface;
78
use PhpDb\Sql\Select;
89
use PhpDb\Sql\TableIdentifier;
910

1011
class From extends AbstractPart
1112
{
12-
private ?TableRef $ref = null;
13+
private ?TableIdentifier $ref = null;
1314

14-
private ?string $resolvedTable = null;
15+
private string|array|TableIdentifier|Select|ExpressionInterface|null $raw = null;
1516

1617
public function toSql(SqlProcessor $processor, string $paramPrefix = '', int &$paramIndex = 0): ?string
1718
{
@@ -21,47 +22,32 @@ public function toSql(SqlProcessor $processor, string $paramPrefix = '', int &$p
2122

2223
public function renderTable(SqlProcessor $processor): ?string
2324
{
24-
if ($this->ref === null) {
25-
return null;
26-
}
27-
28-
$resolved = $this->resolvedTable ?? $processor->resolveTable($this->ref->table);
29-
30-
if ($this->ref->alias !== null) {
31-
$quotedAlias = $processor->platform->quoteIdentifier($this->ref->alias);
32-
$resolved = $processor->renderTable($resolved, $quotedAlias);
33-
}
34-
35-
return $resolved;
25+
return $this->ref?->resolveTableWithAlias($processor);
3626
}
3727

3828
public function isEmpty(): bool
3929
{
4030
return $this->ref === null;
4131
}
4232

43-
public function set(string|array|TableIdentifier|Select|null $table): static
33+
public function set(string|array|TableIdentifier|Select|ExpressionInterface|null $table): static
4434
{
4535
if ($table === null) {
4636
$this->ref = null;
37+
$this->raw = null;
38+
} elseif ($table instanceof TableIdentifier) {
39+
$this->ref = $table;
40+
$this->raw = $table;
4741
} else {
48-
$this->ref = new TableRef($table);
42+
$this->ref = new TableIdentifier($table);
43+
$this->raw = $table;
4944
}
50-
$this->resolvedTable = null;
5145
return $this;
5246
}
5347

54-
public function get(): string|array|TableIdentifier|Select|null
48+
public function get(): string|array|TableIdentifier|Select|ExpressionInterface|null
5549
{
56-
if ($this->ref === null) {
57-
return null;
58-
}
59-
60-
if ($this->ref->alias !== null) {
61-
return [$this->ref->alias => $this->ref->table];
62-
}
63-
64-
return $this->ref->table;
50+
return $this->raw;
6551
}
6652

6753
public function getQuotedPrefix(SqlProcessor $processor): string
@@ -70,16 +56,6 @@ public function getQuotedPrefix(SqlProcessor $processor): string
7056
return '';
7157
}
7258

73-
if ($this->ref->alias !== null) {
74-
return $processor->platform->quoteIdentifier($this->ref->alias)
75-
. $processor->identifierSeparator;
76-
}
77-
78-
$this->resolvedTable = $processor->resolveTable($this->ref->table);
79-
if ($this->resolvedTable) {
80-
return $this->resolvedTable . $processor->identifierSeparator;
81-
}
82-
83-
return '';
59+
return $this->ref->getQuotedPrefix($processor);
8460
}
8561
}

src/Sql/Part/InsertSelect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function toSql(SqlProcessor $processor, string $paramPrefix = '', int &$p
3737
}
3838
$columnsSql = implode(', ', $columns);
3939

40-
$tableSql = $processor->resolveTable($this->table->get());
40+
$tableSql = $this->table->renderTable($processor);
4141

4242
return $this->keyword . ' ' . $tableSql
4343
. ' ' . ($columnsSql ? "({$columnsSql})" : '')

src/Sql/Part/InsertValues.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function toSql(SqlProcessor $processor, string $paramPrefix = '', int &$p
6060
};
6161
}
6262

63-
$tableSql = $processor->resolveTable($this->table->get());
63+
$tableSql = $this->table->renderTable($processor);
6464

6565
return $this->keyword . ' ' . $tableSql
6666
. ' (' . implode(', ', $columns) . ')'

src/Sql/Part/JoinSpec.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,13 @@
44

55
namespace PhpDb\Sql\Part;
66

7-
use PhpDb\Sql\Exception;
8-
use PhpDb\Sql\Expression;
97
use PhpDb\Sql\ExpressionInterface;
108
use PhpDb\Sql\Predicate\PredicateInterface;
11-
use PhpDb\Sql\Select;
129
use PhpDb\Sql\TableIdentifier;
1310

14-
use function current;
15-
use function get_debug_type;
16-
use function is_array;
17-
use function is_string;
18-
use function key;
19-
use function sprintf;
20-
2111
final readonly class JoinSpec
2212
{
23-
public string|TableIdentifier|Select|ExpressionInterface $table;
24-
public JoinTableType $tableType;
25-
public ?string $alias;
13+
public TableIdentifier $table;
2614
public PredicateInterface|string $on;
2715
public bool $isExpressionOn;
2816
public string $type;
@@ -39,26 +27,12 @@ public function __construct(array $join)
3927
$this->raw = $join;
4028
$name = $join['name'];
4129

42-
if (is_array($name)) {
43-
$this->alias = key($name);
44-
$table = current($name);
30+
if ($name instanceof TableIdentifier) {
31+
$this->table = $name;
4532
} else {
46-
$this->alias = null;
47-
$table = $name;
33+
$this->table = new TableIdentifier($name);
4834
}
4935

50-
$this->table = $table;
51-
$this->tableType = match (true) {
52-
$table instanceof Expression => JoinTableType::Expression,
53-
$table instanceof TableIdentifier => JoinTableType::TableIdentifier,
54-
$table instanceof Select => JoinTableType::Select,
55-
is_string($table) => JoinTableType::Identifier,
56-
default => throw new Exception\InvalidArgumentException(sprintf(
57-
'Join name expected to be Expression|TableIdentifier|Select|string, "%s" given',
58-
get_debug_type($table)
59-
)),
60-
};
61-
6236
$on = $join['on'];
6337
$this->on = $on;
6438
$this->isExpressionOn = $on instanceof ExpressionInterface;

src/Sql/Part/JoinTableType.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Sql/Part/SqlFragment.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ public function part(?string $part): self
2525
return $this;
2626
}
2727

28-
public function wrap(?string $suffix): self
28+
public function wrap(?string $suffix = null): self
2929
{
30+
if ($this->parts === []) {
31+
return $this;
32+
}
33+
$this->parts = ['( ' . implode(' ', $this->parts) . ' )'];
3034
if ($suffix !== null) {
31-
$this->parts = ['( ' . implode(' ', $this->parts) . ' )', $suffix];
35+
$this->parts[] = $suffix;
3236
}
3337
return $this;
3438
}

src/Sql/Part/SqlProcessor.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PhpDb\Sql\Select;
1515
use PhpDb\Sql\TableIdentifier;
1616

17-
use function is_string;
1817
use function str_replace;
1918

2019
class SqlProcessor
@@ -89,26 +88,15 @@ public function processSubSelect(Select $subselect): string
8988

9089
public function resolveTable(Select|string|TableIdentifier|null $table): string|null
9190
{
92-
if (is_string($table)) {
93-
return $this->platform->quoteIdentifier($table);
91+
if ($table === null || $table === '') {
92+
return $table;
9493
}
9594

96-
$schema = null;
9795
if ($table instanceof TableIdentifier) {
98-
[$table, $schema] = $table->getTableAndSchema();
96+
return $table->resolveTable($this);
9997
}
10098

101-
if ($table instanceof Select) {
102-
$table = '(' . $this->processSubSelect($table) . ')';
103-
} elseif ($table) {
104-
$table = $this->platform->quoteIdentifier($table);
105-
}
106-
107-
if ($schema && $table) {
108-
$table = $this->platform->quoteIdentifier($schema) . $this->identifierSeparator . $table;
109-
}
110-
111-
return $table;
99+
return (new TableIdentifier($table))->resolveTable($this);
112100
}
113101

114102
public function renderTable(string $table, ?string $alias = null): string

0 commit comments

Comments
 (0)