Skip to content

Commit 09ba680

Browse files
committed
Restore to post-ColumnRef/OrderSpec state
Reverts the temporary baseline test commit. This is the state after polymorphic argument rendering with SqlFragment fluent chain intact. Signed-off-by: Simon Mundy <simon.mundy@peptolab.com>
1 parent 9642955 commit 09ba680

File tree

5 files changed

+58
-95
lines changed

5 files changed

+58
-95
lines changed

src/Sql/Part/ColumnRef.php

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,34 @@
44

55
namespace PhpDb\Sql\Part;
66

7-
use PhpDb\Sql\Argument\Identifier;
8-
use PhpDb\Sql\Argument\Literal;
9-
use PhpDb\Sql\Argument\Select as SelectArgument;
10-
use PhpDb\Sql\ArgumentInterface;
117
use PhpDb\Sql\Expression;
128
use PhpDb\Sql\ExpressionInterface;
139
use PhpDb\Sql\Select;
1410

1511
use function is_string;
1612
use function stripos;
1713

18-
/**
19-
* Normalized column reference for SELECT column lists.
20-
* Type discrimination happens at construction time, not at render time.
21-
*
22-
* Argument types determine rendering:
23-
* - Literal → star column (*), prefixed, no alias
24-
* - Identifier → column name, prefixed, alias handling
25-
* - SelectArgument → expression/subselect, alias handling
26-
*
27-
* Alias rules:
28-
* - $alias is set → always render AS $alias
29-
* - $containsAlias → expression already has AS in its spec, no extra alias
30-
* - expression w/o alias → auto-generate alias as Expression{N} at render time
31-
* - string w/o alias → $alias is set to the column name itself (auto-alias)
32-
*/
3314
final readonly class ColumnRef
3415
{
35-
public ArgumentInterface $arg;
16+
public ExpressionInterface|string $column;
17+
public bool $isStar;
3618
public ?string $alias;
3719
public bool $containsAlias;
3820

39-
/**
40-
* @param int|string $key Array key (int = auto-alias, string = explicit alias)
41-
* @param ExpressionInterface|string $column The column value
42-
* @param string $star The SQL_STAR constant value
43-
*/
4421
public function __construct(
4522
int|string $key,
4623
ExpressionInterface|string $column,
4724
string $star = Select::SQL_STAR,
4825
) {
49-
if ($column === $star) {
50-
$this->arg = new Literal('*');
26+
$this->column = $column;
27+
$this->isStar = ($column === $star);
28+
29+
if ($this->isStar) {
5130
$this->alias = null;
5231
$this->containsAlias = false;
5332
return;
5433
}
5534

56-
if ($column instanceof ExpressionInterface) {
57-
$this->arg = new SelectArgument($column);
58-
} else {
59-
$this->arg = new Identifier($column);
60-
}
61-
6235
if (is_string($key)) {
6336
$this->alias = $key;
6437
$this->containsAlias = false;

src/Sql/Part/Columns.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
namespace PhpDb\Sql\Part;
66

7-
use PhpDb\Sql\Argument\Identifier;
8-
use PhpDb\Sql\Argument\Literal;
97
use PhpDb\Sql\ExpressionInterface;
108
use PhpDb\Sql\Select;
119

1210
use function count;
1311
use function current;
12+
use function explode;
1413
use function implode;
1514
use function is_array;
1615
use function is_numeric;
16+
use function is_string;
1717
use function key;
1818

1919
/**
@@ -51,29 +51,27 @@ public function toSql(SqlProcessor $processor): ?string
5151
$refs = $this->columnRefs;
5252
$fromPrefix = $this->fromTablePrefix;
5353

54-
if (isset($refs[0]) && ! isset($refs[1]) && $refs[0]->arg instanceof Literal && $this->joinSpecs === []) {
55-
return $fromPrefix . $refs[0]->arg->getValue();
54+
if (isset($refs[0]) && ! isset($refs[1]) && $refs[0]->isStar && $this->joinSpecs === []) {
55+
return $fromPrefix . '*';
5656
}
5757

5858
$fragments = [];
5959
$exprCounter = 1;
6060
$platform = $processor->platform;
6161

6262
foreach ($refs as $ref) {
63-
$arg = $ref->arg;
64-
65-
if ($arg instanceof Literal) {
66-
$fragments[] = $fromPrefix . $arg->getValue();
63+
if ($ref->isStar) {
64+
$fragments[] = $fromPrefix . '*';
6765
continue;
6866
}
6967

70-
if ($arg instanceof Identifier) {
71-
$columnSql = $fromPrefix . $platform->quoteIdentifier($arg->segments[0], $arg->segments[1] ?? null);
68+
$column = $ref->column;
69+
70+
if (is_string($column)) {
71+
$parts = explode('.', $column, 2);
72+
$columnSql = $fromPrefix . $platform->quoteIdentifier($parts[0], $parts[1] ?? null);
7273
} else {
73-
$columnSql = $processor->renderExpression(
74-
$arg->getValue(),
75-
$ref->alias ?? 'column',
76-
);
74+
$columnSql = $processor->renderExpression($column, $ref->alias ?? 'column');
7775
}
7876

7977
if ($ref->alias !== null) {
@@ -93,20 +91,18 @@ public function toSql(SqlProcessor $processor): ?string
9391
}
9492
$joinPrefix = $processor->resolveTable($spec->alias ?? $spec->table) . $separator;
9593
foreach ($spec->columnRefs as $ref) {
96-
$arg = $ref->arg;
97-
98-
if ($arg instanceof Literal) {
99-
$fragments[] = $joinPrefix . $arg->getValue();
94+
if ($ref->isStar) {
95+
$fragments[] = $joinPrefix . '*';
10096
continue;
10197
}
10298

103-
if ($arg instanceof Identifier) {
104-
$columnSql = $joinPrefix . $platform->quoteIdentifier($arg->segments[0], $arg->segments[1] ?? null);
99+
$column = $ref->column;
100+
101+
if (is_string($column)) {
102+
$parts = explode('.', $column, 2);
103+
$columnSql = $joinPrefix . $platform->quoteIdentifier($parts[0], $parts[1] ?? null);
105104
} else {
106-
$columnSql = $processor->renderExpression(
107-
$arg->getValue(),
108-
$ref->alias ?? 'column',
109-
);
105+
$columnSql = $processor->renderExpression($column, $ref->alias ?? 'column');
110106
}
111107

112108
if ($ref->alias !== null) {

src/Sql/Part/GroupBy.php

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

55
namespace PhpDb\Sql\Part;
66

7-
use PhpDb\Sql\ArgumentType;
8-
use ValueError;
7+
use PhpDb\Sql\ExpressionInterface;
98

9+
use function explode;
1010
use function implode;
1111
use function is_array;
12+
use function is_string;
1213

1314
/**
1415
* Holds and renders GROUP BY clause.
@@ -25,15 +26,18 @@ public function toSql(SqlProcessor $processor): ?string
2526
return null;
2627
}
2728

28-
$groups = [];
29-
$pi = 0;
29+
$platform = $processor->platform;
30+
$groups = [];
31+
3032
foreach ($this->group as $ref) {
31-
$groups[] = match ($ref->arg->getType()) {
32-
ArgumentType::Identifier => $ref->arg->render($processor, '', $pi),
33-
ArgumentType::Select => $processor->renderExpression($ref->arg->getValue()),
34-
ArgumentType::Literal => $ref->arg->getValue(),
35-
default => throw new ValueError('Unexpected ArgumentType: ' . $ref->arg->getType()->name),
36-
};
33+
$column = $ref->column;
34+
35+
if (is_string($column)) {
36+
$parts = explode('.', $column, 2);
37+
$groups[] = $platform->quoteIdentifier($parts[0], $parts[1] ?? null);
38+
} else {
39+
$groups[] = $processor->renderExpression($column);
40+
}
3741
}
3842

3943
return 'GROUP BY ' . implode(', ', $groups);
@@ -67,7 +71,7 @@ public function get(): ?array
6771

6872
$result = [];
6973
foreach ($this->group as $ref) {
70-
$result[] = $ref->arg->getValue();
74+
$result[] = $ref->column;
7175
}
7276
return $result;
7377
}

src/Sql/Part/OrderBy.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace PhpDb\Sql\Part;
66

7-
use PhpDb\Sql\ArgumentType;
87
use PhpDb\Sql\ExpressionInterface;
9-
use ValueError;
108

119
use function explode;
1210
use function implode;
@@ -33,15 +31,19 @@ public function toSql(SqlProcessor $processor): ?string
3331
return null;
3432
}
3533

36-
$orders = [];
37-
$pi = 0;
34+
$platform = $processor->platform;
35+
$orders = [];
36+
3837
foreach ($this->order as $spec) {
39-
$orders[] = match ($spec->column->getType()) {
40-
ArgumentType::Select => $processor->renderExpression($spec->column->getValue()),
41-
ArgumentType::Identifier => $spec->column->render($processor, '', $pi)
42-
. ' ' . $spec->direction,
43-
default => throw new ValueError('Unexpected ArgumentType: ' . $spec->column->getType()->name),
44-
};
38+
$column = $spec->column;
39+
40+
if (is_string($column)) {
41+
$parts = explode('.', $column, 2);
42+
$orders[] = $platform->quoteIdentifier($parts[0], $parts[1] ?? null)
43+
. ' ' . $spec->direction;
44+
} else {
45+
$orders[] = $processor->renderExpression($column);
46+
}
4547
}
4648

4749
return 'ORDER BY ' . implode(', ', $orders);
@@ -86,10 +88,10 @@ public function get(): array
8688
{
8789
$result = [];
8890
foreach ($this->order as $spec) {
89-
if ($spec->column->getType() === ArgumentType::Select) {
90-
$result[] = $spec->column->getValue();
91+
if ($spec->column instanceof ExpressionInterface) {
92+
$result[] = $spec->column;
9193
} else {
92-
$result[] = $spec->column->getValue() . ' ' . $spec->direction;
94+
$result[] = $spec->column . ' ' . $spec->direction;
9395
}
9496
}
9597
return $result;

src/Sql/Part/OrderSpec.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,24 @@
44

55
namespace PhpDb\Sql\Part;
66

7-
use PhpDb\Sql\Argument\Identifier;
8-
use PhpDb\Sql\Argument\Select as SelectArgument;
9-
use PhpDb\Sql\ArgumentInterface;
107
use PhpDb\Sql\ExpressionInterface;
118

129
use function strcasecmp;
1310
use function trim;
1411

15-
/**
16-
* Normalized order-by item with column (as ArgumentInterface) and direction.
17-
* Parsing of "column DESC" strings and direction normalization happens at construction time.
18-
*/
1912
final readonly class OrderSpec
2013
{
2114
public const ORDER_ASCENDING = 'ASC';
2215
public const ORDER_DESCENDING = 'DESC';
2316

24-
public ArgumentInterface $column;
17+
public ExpressionInterface|string $column;
2518
public string $direction;
2619

2720
public function __construct(
2821
ExpressionInterface|string $column,
2922
string $direction = self::ORDER_ASCENDING,
3023
) {
31-
if ($column instanceof ExpressionInterface) {
32-
$this->column = new SelectArgument($column);
33-
} else {
34-
$this->column = new Identifier($column);
35-
}
36-
24+
$this->column = $column;
3725
$this->direction = strcasecmp(trim($direction), self::ORDER_DESCENDING) === 0
3826
? self::ORDER_DESCENDING
3927
: self::ORDER_ASCENDING;

0 commit comments

Comments
 (0)