Skip to content

Commit b418074

Browse files
committed
Simplify TableIdentifier, separate table identity from table sources
TableIdentifier becomes a final readonly class with just string $table and ?string $schema. Alias, Select, and ExpressionInterface table sources move to the containers that own the context (From, JoinSpec). This eliminates type-checking, getter calls, and instanceof checks from the render path — resolveTable/resolveTableRef replaced by a single renderTableSource method with direct property access. Renderer public API unified to render* naming: renderTableSource, renderResolvedTable (was tablePrefix), renderIdentifiersIn (was quoteIdentifiersIn).
1 parent 8b8f559 commit b418074

25 files changed

+214
-321
lines changed

src/Sql/AbstractSql.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212

1313
abstract class AbstractSql implements SqlInterface
1414
{
15+
private ?AbstractSqlRenderer $renderer = null;
16+
1517
/**
1618
* {@inheritDoc}
1719
*/
1820
#[Override]
1921
public function getSqlString(?PlatformInterface $adapterPlatform = null): string
2022
{
23+
$this->renderer ??= new Sql92Renderer();
24+
2125
return $this->buildSqlString(
22-
(new Sql92Renderer())->init($adapterPlatform ?? new DefaultAdapterPlatform())
26+
$this->renderer->init($adapterPlatform ?? new DefaultAdapterPlatform())
2327
);
2428
}
2529

src/Sql/Ddl/AlterTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function getRawState(?string $key = null): array|string
146146
#[Override]
147147
public function buildSqlString(AbstractSqlRenderer $renderer): string
148148
{
149-
$sql = "ALTER TABLE " . $renderer->resolveTable($this->table) . "\n";
149+
$sql = "ALTER TABLE " . $renderer->renderTableSource($this->table) . "\n";
150150

151151
$clauses = [];
152152

src/Sql/Ddl/CreateTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function buildSqlString(AbstractSqlRenderer $renderer): string
124124
. ($this->isTemporary ? 'TEMPORARY ' : '')
125125
. 'TABLE '
126126
. ($this->ifNotExists ? 'IF NOT EXISTS ' : '')
127-
. $renderer->resolveTable($this->table) . ' (';
127+
. $renderer->renderTableSource($this->table) . ' (';
128128

129129
if ($this->columns) {
130130
$columnSqls = [];

src/Sql/Ddl/DropTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public function buildSqlString(AbstractSqlRenderer $renderer): string
3737
{
3838
return 'DROP TABLE '
3939
. ($this->ifExists ? 'IF EXISTS ' : '')
40-
. $renderer->resolveTable($this->table);
40+
. $renderer->renderTableSource($this->table);
4141
}
4242
}

src/Sql/Join.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ public function toSql(AbstractSqlRenderer $renderer, string $paramPrefix = '', i
125125
$joinSqlParts = [];
126126

127127
foreach ($this->specs as $j => $spec) {
128-
$table = $renderer->resolveTable($spec->table, withAlias: true);
128+
$table = $renderer->renderTableSource($spec->table, $spec->alias);
129129

130130
if (! $spec->isExpressionOn) {
131-
$onClause = $renderer->quoteIdentifiersIn($spec->on);
131+
$onClause = $renderer->renderIdentifiersIn($spec->on);
132132
} else {
133133
$onClause = $renderer->render($spec->on, 'join' . ($j + 1) . 'part');
134134
}

src/Sql/Part/ColumnRef.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,45 @@
1818
{
1919
public ArgumentInterface|ExpressionInterface|string $column;
2020
public bool $isStar;
21-
public ?string $alias;
21+
public ?string $columnAlias;
2222
public bool $containsAlias;
23-
public ?TableIdentifier $table;
23+
public TableIdentifier|Select|ExpressionInterface|null $table;
24+
public ?string $tableAlias;
2425

2526
public function __construct(
2627
int|string $key,
2728
ArgumentInterface|ExpressionInterface|string $column,
28-
?TableIdentifier $table = null,
29+
TableIdentifier|Select|ExpressionInterface|null $table = null,
30+
?string $tableAlias = null,
2931
string $star = Select::SQL_STAR,
3032
) {
31-
$this->table = $table;
32-
$this->isStar = is_string($column) && $column === $star;
33+
$this->table = $table;
34+
$this->tableAlias = $tableAlias;
35+
$this->isStar = is_string($column) && $column === $star;
3336

3437
if ($this->isStar) {
3538
$this->column = '';
36-
$this->alias = null;
39+
$this->columnAlias = null;
3740
$this->containsAlias = false;
3841
return;
3942
}
4043

4144
$this->column = is_string($column) ? new Identifier($column) : $column;
4245

4346
if (is_string($key)) {
44-
$this->alias = $key;
47+
$this->columnAlias = $key;
4548
$this->containsAlias = false;
4649
return;
4750
}
4851

4952
if ($column instanceof ExpressionInterface) {
5053
$this->containsAlias = $column instanceof Expression
5154
&& stripos($column->getExpression(), ' as ') !== false;
52-
$this->alias = null;
55+
$this->columnAlias = null;
5356
return;
5457
}
5558

56-
$this->alias = $column instanceof ArgumentInterface ? $column->getValue() : $column;
59+
$this->columnAlias = $column instanceof ArgumentInterface ? $column->getValue() : $column;
5760
$this->containsAlias = false;
5861
}
5962
}

src/Sql/Part/Columns.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public function toSql(AbstractSqlRenderer $renderer, string $paramPrefix = '', i
4949
$allRefs = $this->joinRefs !== [] ? array_merge($refs, $this->joinRefs) : $refs;
5050

5151
foreach ($allRefs as $ref) {
52-
$prefix = $ref->table !== null ? $renderer->tablePrefix($ref->table) : $fromPrefix;
52+
$prefix = $ref->table !== null
53+
? $renderer->renderResolvedTable($ref->table, $ref->tableAlias)
54+
: $fromPrefix;
5355

5456
if ($ref->isStar) {
5557
$fragments[] = $prefix . '*';
@@ -63,11 +65,11 @@ public function toSql(AbstractSqlRenderer $renderer, string $paramPrefix = '', i
6365
} elseif ($column instanceof ArgumentInterface) {
6466
$columnSql = $prefix . $renderer->renderArgument($column, '', $pi);
6567
} else {
66-
$columnSql = $renderer->render($column, $ref->alias ?? 'column');
68+
$columnSql = $renderer->render($column, $ref->columnAlias ?? 'column');
6769
}
6870

69-
if ($ref->alias !== null) {
70-
$fragments[] = $columnSql . ' AS ' . $platform->quoteIdentifier($ref->alias);
71+
if ($ref->columnAlias !== null) {
72+
$fragments[] = $columnSql . ' AS ' . $platform->quoteIdentifier($ref->columnAlias);
7173
} elseif ($ref->containsAlias) {
7274
$fragments[] = $columnSql;
7375
} else {

src/Sql/Part/From.php

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
use PhpDb\Sql\Select;
1010
use PhpDb\Sql\TableIdentifier;
1111

12+
use function current;
13+
use function is_array;
14+
use function is_string;
15+
use function key;
16+
1217
class From extends AbstractPart
1318
{
14-
private ?TableIdentifier $ref = null;
19+
private TableIdentifier|Select|ExpressionInterface|null $table = null;
1520

16-
private string|array|TableIdentifier|Select|ExpressionInterface|null $raw = null;
21+
private ?string $alias = null;
1722

1823
public function toSql(AbstractSqlRenderer $renderer, string $paramPrefix = '', int &$paramIndex = 0): ?string
1924
{
@@ -23,40 +28,60 @@ public function toSql(AbstractSqlRenderer $renderer, string $paramPrefix = '', i
2328

2429
public function renderTable(AbstractSqlRenderer $renderer): ?string
2530
{
26-
return $this->ref !== null ? $renderer->resolveTable($this->ref, withAlias: true) : null;
31+
return $this->table !== null
32+
? $renderer->renderTableSource($this->table, $this->alias)
33+
: null;
2734
}
2835

2936
public function isEmpty(): bool
3037
{
31-
return $this->ref === null;
38+
return $this->table === null;
3239
}
3340

3441
public function set(string|array|TableIdentifier|Select|ExpressionInterface|null $table): static
3542
{
3643
if ($table === null) {
37-
$this->ref = null;
38-
$this->raw = null;
39-
} elseif ($table instanceof TableIdentifier) {
40-
$this->ref = $table;
41-
$this->raw = $table;
44+
$this->table = null;
45+
$this->alias = null;
46+
return $this;
47+
}
48+
49+
$alias = null;
50+
if (is_array($table)) {
51+
$alias = key($table);
52+
$table = current($table);
53+
if ($table instanceof TableIdentifier) {
54+
$this->table = $table;
55+
$this->alias = $alias;
56+
return $this;
57+
}
58+
}
59+
60+
if (is_string($table)) {
61+
$this->table = new TableIdentifier($table);
4262
} else {
43-
$this->ref = new TableIdentifier($table);
44-
$this->raw = $table;
63+
$this->table = $table;
4564
}
65+
$this->alias = $alias;
4666
return $this;
4767
}
4868

49-
public function get(): string|array|TableIdentifier|Select|ExpressionInterface|null
69+
public function get(): TableIdentifier|Select|ExpressionInterface|null
70+
{
71+
return $this->table;
72+
}
73+
74+
public function getAlias(): ?string
5075
{
51-
return $this->raw;
76+
return $this->alias;
5277
}
5378

5479
public function getQuotedPrefix(AbstractSqlRenderer $renderer): string
5580
{
56-
if ($this->ref === null) {
81+
if ($this->table === null) {
5782
return '';
5883
}
5984

60-
return $renderer->tablePrefix($this->ref);
85+
return $renderer->renderResolvedTable($this->table, $this->alias);
6186
}
6287
}

src/Sql/Part/JoinSpec.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66

77
use PhpDb\Sql\ExpressionInterface;
88
use PhpDb\Sql\Predicate\PredicateInterface;
9+
use PhpDb\Sql\Select;
910
use PhpDb\Sql\TableIdentifier;
1011

1112
final readonly class JoinSpec
1213
{
1314
public bool $isExpressionOn;
1415

1516
public function __construct(
16-
public TableIdentifier $table,
17+
public TableIdentifier|Select|ExpressionInterface $table,
18+
public ?string $alias,
1719
public PredicateInterface|string $on,
1820
public string $type,
1921
) {

src/Sql/Part/Table.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpDb\Sql\Part;
66

77
use PhpDb\Sql\Exception\InvalidArgumentException;
8+
use PhpDb\Sql\ExpressionInterface;
89
use PhpDb\Sql\Join;
910
use PhpDb\Sql\Platform\AbstractSqlRenderer;
1011
use PhpDb\Sql\Predicate\PredicateInterface;
@@ -13,6 +14,7 @@
1314

1415
use function array_shift;
1516
use function count;
17+
use function current;
1618
use function is_array;
1719
use function is_string;
1820
use function key;
@@ -37,7 +39,7 @@ public function setFrom(string|array|TableIdentifier|Select|null $table): static
3739
return $this;
3840
}
3941

40-
public function getFrom(): string|array|TableIdentifier|Select|null
42+
public function getFrom(): TableIdentifier|Select|ExpressionInterface|null
4143
{
4244
return $this->from->get();
4345
}
@@ -89,7 +91,7 @@ public function join(
8991
): static {
9092
$spec = self::createJoinSpec($name, $on, $columns, $type);
9193
$this->join->add($spec);
92-
$this->columns->addJoinRefs(self::buildColumnRefs($columns, $spec->table));
94+
$this->columns->addJoinRefs(self::buildColumnRefs($columns, $spec->table, $spec->alias));
9395
return $this;
9496
}
9597

@@ -109,21 +111,30 @@ public static function createJoinSpec(
109111
$columns = [$columns];
110112
}
111113

112-
$table = $name instanceof TableIdentifier ? $name : new TableIdentifier($name);
114+
$alias = null;
115+
if (is_array($name)) {
116+
$alias = key($name);
117+
$name = current($name);
118+
}
119+
120+
$table = $name instanceof TableIdentifier ? $name : (is_string($name) ? new TableIdentifier($name) : $name);
113121

114-
return new JoinSpec($table, $on, $type);
122+
return new JoinSpec($table, $alias, $on, $type);
115123
}
116124

117125
/** @return ColumnRef[] */
118-
private static function buildColumnRefs(array|string $columns, TableIdentifier $table): array
119-
{
126+
private static function buildColumnRefs(
127+
array|string $columns,
128+
TableIdentifier|Select|ExpressionInterface $table,
129+
?string $alias,
130+
): array {
120131
if (! is_array($columns)) {
121132
$columns = [$columns];
122133
}
123134

124135
$refs = [];
125136
foreach ($columns as $key => $column) {
126-
$refs[] = new ColumnRef($key, $column, $table);
137+
$refs[] = new ColumnRef($key, $column, $table, $alias);
127138
}
128139
return $refs;
129140
}

0 commit comments

Comments
 (0)