Skip to content

Commit b3f29d0

Browse files
authored
Merge pull request #28 from formal-php/multi-order
Add `Select::andOrderBy()`
2 parents 5b04391 + eb1ecec commit b3f29d0

4 files changed

Lines changed: 189 additions & 10 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Added
6+
7+
- `Formal\AccessLayer\Query\Select::andOrderBy()`
8+
39
## 5.3.0 - 2026-09-24
410

5-
## Added
11+
### Added
612

713
- It is now possible to use an empty array when using `Sign::in` in a specification
814

‎properties/Connection.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public static function list(): array
7373
Connection\SelectOffset::class,
7474
Connection\SelectLimit::class,
7575
Connection\SelectOrder::class,
76+
Connection\SelectMultiOrder::class,
7677
Connection\SelectValues::class,
7778
Connection\Update::class,
7879
Connection\UpdateSpecificRow::class,
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Properties\Formal\AccessLayer\Connection;
5+
6+
use Formal\AccessLayer\{
7+
Query\MultipleInsert,
8+
Query\Select,
9+
Query\Select\Direction,
10+
Table\Name,
11+
Table\Column,
12+
Row,
13+
Connection,
14+
};
15+
use Innmind\Specification\{
16+
Comparator,
17+
Sign,
18+
};
19+
use Innmind\Immutable\Sequence;
20+
use Innmind\BlackBox\{
21+
Property,
22+
Set,
23+
Runner\Assert,
24+
};
25+
26+
/**
27+
* @implements Property<Connection>
28+
*/
29+
final class SelectMultiOrder implements Property
30+
{
31+
private string $uuid1;
32+
private string $uuid2;
33+
private string $uuid3;
34+
private string $username;
35+
private int $number;
36+
37+
public function __construct(
38+
string $uuid1,
39+
string $uuid2,
40+
string $uuid3,
41+
string $username,
42+
int $number,
43+
) {
44+
$this->uuid1 = $uuid1;
45+
$this->uuid2 = $uuid2;
46+
$this->uuid3 = $uuid3;
47+
$this->username = $username;
48+
$this->number = $number;
49+
}
50+
51+
public static function any(): Set
52+
{
53+
return Set::compose(
54+
static fn(...$args) => new self(...$args),
55+
Set::uuid(),
56+
Set::uuid(),
57+
Set::uuid(),
58+
Set::strings()
59+
->madeOf(Set::strings()->chars()->ascii())
60+
->between(0, 254),
61+
Set::integers(),
62+
);
63+
}
64+
65+
public function applicableTo(object $connection): bool
66+
{
67+
return true;
68+
}
69+
70+
public function ensureHeldBy(Assert $assert, object $connection): object
71+
{
72+
$insert = MultipleInsert::into(
73+
Name::of('test'),
74+
Column\Name::of('id'),
75+
Column\Name::of('username'),
76+
Column\Name::of('registerNumber'),
77+
);
78+
$_ = $connection($insert(Sequence::of(
79+
Row::of([
80+
'id' => $this->uuid1,
81+
'username' => 'a'.$this->username,
82+
'registerNumber' => $this->number,
83+
]),
84+
Row::of([
85+
'id' => $this->uuid2,
86+
'username' => 'b'.$this->username,
87+
'registerNumber' => $this->number,
88+
]),
89+
Row::of([
90+
'id' => $this->uuid3,
91+
'username' => 'b'.$this->username,
92+
'registerNumber' => $this->number + 1,
93+
]),
94+
)));
95+
96+
$table = Name::of('test');
97+
$select = Select::from($table)
98+
->orderBy(
99+
Column\Name::of('username')->in($table),
100+
Direction::asc,
101+
)
102+
->andOrderBy(
103+
Column\Name::of('registerNumber')->in($table),
104+
Direction::desc,
105+
)
106+
->where(Comparator\Property::of(
107+
'id',
108+
Sign::in,
109+
[$this->uuid1, $this->uuid2, $this->uuid3],
110+
));
111+
$rows = $connection($select);
112+
113+
$assert
114+
->expected([$this->uuid1, $this->uuid3, $this->uuid2])
115+
->same(
116+
$rows
117+
->flatMap(static fn($row) => $row->column('id')->toSequence())
118+
->toList(),
119+
);
120+
121+
$rows = $connection(
122+
$select
123+
->orderBy(
124+
Column\Name::of('username')->in($table),
125+
Direction::desc,
126+
)
127+
->andOrderBy(
128+
Column\Name::of('registerNumber')->in($table),
129+
Direction::asc,
130+
),
131+
);
132+
133+
$assert
134+
->expected([$this->uuid2, $this->uuid3, $this->uuid1])
135+
->same(
136+
$rows
137+
->flatMap(static fn($row) => $row->column('id')->toSequence())
138+
->toList(),
139+
);
140+
141+
return $connection;
142+
}
143+
}

‎src/Query/Select.php‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class Select implements Builder
3030
* @param Sequence<Join> $joins
3131
* @param Sequence<Column\Name|Column\Name\Namespaced|Column\Name\Aliased|Row\Value> $columns
3232
* @param Maybe<non-empty-string> $count
33-
* @param ?array{Column\Name|Column\Name\Namespaced|Column\Name\Aliased, Direction} $orderBy
33+
* @param list<array{Column\Name|Column\Name\Namespaced|Column\Name\Aliased, Direction}> $orderBy
3434
* @param ?positive-int $limit
3535
* @param ?positive-int $offset
3636
*/
@@ -41,7 +41,7 @@ private function __construct(
4141
private Sequence $columns,
4242
private Maybe $count,
4343
private Where $where,
44-
private ?array $orderBy,
44+
private array $orderBy,
4545
private ?int $limit,
4646
private ?int $offset,
4747
) {
@@ -62,7 +62,7 @@ public static function from(Name|Name\Aliased $table): self
6262
Sequence::of(),
6363
$count,
6464
Where::everything(),
65-
null,
65+
[],
6666
null,
6767
null,
6868
);
@@ -92,7 +92,7 @@ public static function lazily(Name|Name\Aliased $table): self
9292
Sequence::of(),
9393
$count,
9494
Where::everything(),
95-
null,
95+
[],
9696
null,
9797
null,
9898
);
@@ -180,7 +180,27 @@ public function orderBy(
180180
$this->columns,
181181
$this->count,
182182
$this->where,
183-
[$column, $direction],
183+
[[$column, $direction]],
184+
$this->limit,
185+
$this->offset,
186+
);
187+
}
188+
189+
public function andOrderBy(
190+
Column\Name|Column\Name\Namespaced|Column\Name\Aliased $column,
191+
Direction $direction,
192+
): self {
193+
$orderBy = $this->orderBy;
194+
$orderBy[] = [$column, $direction];
195+
196+
return new self(
197+
$this->table,
198+
$this->lazy,
199+
$this->joins,
200+
$this->columns,
201+
$this->count,
202+
$this->where,
203+
$orderBy,
184204
$this->limit,
185205
$this->offset,
186206
);
@@ -228,11 +248,20 @@ public function normalize(Driver $driver): Query
228248
->toString(),
229249
$where,
230250
match ($this->orderBy) {
231-
null => '',
251+
[] => '',
232252
default => \sprintf(
233-
' ORDER BY %s %s',
234-
$this->orderBy[0]->sql($driver),
235-
$this->orderBy[1]->sql(),
253+
' ORDER BY %s',
254+
\implode(
255+
', ',
256+
\array_map(
257+
static fn($pair) => \sprintf(
258+
'%s %s',
259+
$pair[0]->sql($driver),
260+
$pair[1]->sql(),
261+
),
262+
$this->orderBy,
263+
),
264+
),
236265
),
237266
},
238267
match ($this->limit) {

0 commit comments

Comments
 (0)