Skip to content

Commit fec18d0

Browse files
committed
refactor: OrderBy and OrderByItem got merged into OrderBy
1 parent 9626679 commit fec18d0

File tree

14 files changed

+124
-255
lines changed

14 files changed

+124
-255
lines changed

src/lib/postgresql/src/Flow/PostgreSql/AST/Nodes/OrderByItem.php

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

src/lib/postgresql/src/Flow/PostgreSql/DSL/functions.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
LockingClause,
3232
NullsPosition,
3333
OnConflictClause,
34-
OrderByItem,
34+
OrderBy,
3535
ReturningClause,
3636
SortDirection,
3737
WindowDefinition,
3838
WindowFrame
3939
};
40-
use Flow\PostgreSql\QueryBuilder\Clause\{OrderBy, WithClause};
40+
use Flow\PostgreSql\QueryBuilder\Clause\WithClause;
4141
use Flow\PostgreSql\QueryBuilder\Condition\{
4242
All,
4343
AndCondition,
@@ -1180,7 +1180,7 @@ function binary_expr(Expression $left, string $operator, Expression $right) : Bi
11801180
* @param string $name Function name
11811181
* @param list<Expression> $args Function arguments
11821182
* @param list<Expression> $partitionBy PARTITION BY expressions
1183-
* @param list<OrderBy|OrderByItem> $orderBy ORDER BY items
1183+
* @param list<OrderBy> $orderBy ORDER BY items
11841184
*/
11851185
#[DocumentationDSL(module: Module::PG_QUERY, type: DSLType::HELPER)]
11861186
function window_func(
@@ -1718,26 +1718,26 @@ function order_by(
17181718
Expression $expr,
17191719
SortDirection $direction = SortDirection::ASC,
17201720
NullsPosition $nulls = NullsPosition::DEFAULT,
1721-
) : OrderByItem {
1722-
return new OrderByItem($expr, $direction, $nulls);
1721+
) : OrderBy {
1722+
return new OrderBy($expr, $direction, $nulls);
17231723
}
17241724

17251725
/**
17261726
* Create an ORDER BY item with ASC direction.
17271727
*/
17281728
#[DocumentationDSL(module: Module::PG_QUERY, type: DSLType::HELPER)]
1729-
function asc(Expression $expr, NullsPosition $nulls = NullsPosition::DEFAULT) : OrderByItem
1729+
function asc(Expression $expr, NullsPosition $nulls = NullsPosition::DEFAULT) : OrderBy
17301730
{
1731-
return new OrderByItem($expr, SortDirection::ASC, $nulls);
1731+
return new OrderBy($expr, SortDirection::ASC, $nulls);
17321732
}
17331733

17341734
/**
17351735
* Create an ORDER BY item with DESC direction.
17361736
*/
17371737
#[DocumentationDSL(module: Module::PG_QUERY, type: DSLType::HELPER)]
1738-
function desc(Expression $expr, NullsPosition $nulls = NullsPosition::DEFAULT) : OrderByItem
1738+
function desc(Expression $expr, NullsPosition $nulls = NullsPosition::DEFAULT) : OrderBy
17391739
{
1740-
return new OrderByItem($expr, SortDirection::DESC, $nulls);
1740+
return new OrderBy($expr, SortDirection::DESC, $nulls);
17411741
}
17421742

17431743
/**
@@ -1767,7 +1767,7 @@ function cte(
17671767
*
17681768
* @param string $name Window name
17691769
* @param list<Expression> $partitionBy PARTITION BY expressions
1770-
* @param list<OrderBy|OrderByItem> $orderBy ORDER BY items
1770+
* @param list<OrderBy> $orderBy ORDER BY items
17711771
* @param null|WindowFrame $frame Window frame specification
17721772
*/
17731773
#[DocumentationDSL(module: Module::PG_QUERY, type: DSLType::HELPER)]

src/lib/postgresql/src/Flow/PostgreSql/Extractors/OrderBy.php

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

55
namespace Flow\PostgreSql\Extractors;
66

7-
use Flow\PostgreSql\AST\Nodes\OrderByItem;
87
use Flow\PostgreSql\AST\Visitors\SortByCollector;
98
use Flow\PostgreSql\ParsedQuery;
9+
use Flow\PostgreSql\QueryBuilder\Clause\OrderBy as OrderByClause;
1010

1111
final readonly class OrderBy
1212
{
@@ -15,15 +15,15 @@ public function __construct(private ParsedQuery $query)
1515
}
1616

1717
/**
18-
* @return array<OrderByItem>
18+
* @return array<OrderByClause>
1919
*/
2020
public function all() : array
2121
{
2222
$collector = new SortByCollector();
2323
$this->query->traverse($collector);
2424

2525
return \array_map(
26-
static fn ($sortBy) => new OrderByItem($sortBy),
26+
static fn ($sortBy) => OrderByClause::fromAst($sortBy),
2727
$collector->getSortByClauses()
2828
);
2929
}

src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Clause/OrderBy.php

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,91 @@
55
namespace Flow\PostgreSql\QueryBuilder\Clause;
66

77
use Flow\PostgreSql\Protobuf\AST\{Node, SortBy};
8-
use Flow\PostgreSql\QueryBuilder\Bridge\AstConvertible;
98
use Flow\PostgreSql\QueryBuilder\Exception\InvalidAstException;
10-
use Flow\PostgreSql\QueryBuilder\Expression\{Expression, ExpressionFactory};
9+
use Flow\PostgreSql\QueryBuilder\Expression\{Column, Expression, ExpressionFactory};
1110

1211
/**
13-
* Represents an ORDER BY clause element.
12+
* Represents an ORDER BY item.
1413
*/
15-
final readonly class OrderBy implements AstConvertible
14+
final readonly class OrderBy
1615
{
1716
public function __construct(
1817
private Expression $expression,
1918
private SortDirection $direction = SortDirection::ASC,
20-
private NullsPosition $nullsPosition = NullsPosition::DEFAULT,
19+
private NullsPosition $nulls = NullsPosition::DEFAULT,
2120
) {
2221
}
2322

24-
public static function fromAst(Node $node) : static
23+
public static function fromAst(SortBy $node) : self
2524
{
26-
$sortBy = $node->getSortBy();
27-
28-
if ($sortBy === null) {
29-
throw InvalidAstException::unexpectedNodeType('SortBy', 'unknown');
30-
}
31-
32-
$nodeExpr = $sortBy->getNode();
25+
$nodeExpr = $node->getNode();
3326

3427
if ($nodeExpr === null) {
3528
throw InvalidAstException::missingRequiredField('node', 'SortBy');
3629
}
3730

3831
$expression = ExpressionFactory::fromAst($nodeExpr);
32+
$direction = SortDirection::fromProtobuf($node->getSortbyDir());
33+
$nulls = NullsPosition::fromProtobuf($node->getSortbyNulls());
3934

40-
$direction = SortDirection::fromProtobuf($sortBy->getSortbyDir());
41-
$nullsPosition = NullsPosition::fromProtobuf($sortBy->getSortbyNulls());
35+
return new self($expression, $direction, $nulls);
36+
}
4237

43-
return new self($expression, $direction, $nullsPosition);
38+
public function asc() : self
39+
{
40+
return new self($this->expression, SortDirection::ASC, $this->nulls);
4441
}
4542

46-
public function getDirection() : SortDirection
43+
public function column() : ?string
44+
{
45+
if ($this->expression instanceof Column) {
46+
return $this->expression->columnName();
47+
}
48+
49+
return null;
50+
}
51+
52+
public function desc() : self
53+
{
54+
return new self($this->expression, SortDirection::DESC, $this->nulls);
55+
}
56+
57+
public function direction() : SortDirection
4758
{
4859
return $this->direction;
4960
}
5061

51-
public function getExpression() : Expression
62+
public function expression() : Expression
5263
{
5364
return $this->expression;
5465
}
5566

56-
public function getNullsPosition() : NullsPosition
67+
public function nulls() : NullsPosition
5768
{
58-
return $this->nullsPosition;
69+
return $this->nulls;
5970
}
6071

61-
public function toAst() : Node
72+
public function nullsFirst() : self
6273
{
63-
$sortBy = new SortBy([
64-
'node' => $this->expression->toAst(),
65-
'sortby_dir' => $this->direction->toProtobuf(),
66-
'sortby_nulls' => $this->nullsPosition->toProtobuf(),
67-
]);
74+
return new self($this->expression, $this->direction, NullsPosition::FIRST);
75+
}
6876

69-
return new Node(['sort_by' => $sortBy]);
77+
public function nullsLast() : self
78+
{
79+
return new self($this->expression, $this->direction, NullsPosition::LAST);
7080
}
7181

72-
public function withDirection(SortDirection $direction) : self
82+
public function toAst() : SortBy
7383
{
74-
return new self($this->expression, $direction, $this->nullsPosition);
84+
return new SortBy([
85+
'node' => $this->expression->toAst(),
86+
'sortby_dir' => $this->direction->toProtobuf(),
87+
'sortby_nulls' => $this->nulls->toProtobuf(),
88+
]);
7589
}
7690

77-
public function withNullsPosition(NullsPosition $nullsPosition) : self
91+
public function toNode() : Node
7892
{
79-
return new self($this->expression, $this->direction, $nullsPosition);
93+
return new Node(['sort_by' => $this->toAst()]);
8094
}
8195
}

src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Clause/OrderByItem.php

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

0 commit comments

Comments
 (0)