Skip to content

Commit cd51bcf

Browse files
authored
fix(database): multiple select fields in one statement (#1603)
1 parent a9776c4 commit cd51bcf

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/database/src/QueryStatements/SelectStatement.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Tempest\Database\QueryStatement;
99
use Tempest\Support\Arr\ImmutableArray;
1010

11+
use function Tempest\Support\str;
12+
1113
final class SelectStatement implements QueryStatement, HasWhereStatements
1214
{
1315
public function __construct(
@@ -46,6 +48,13 @@ public function compile(DatabaseDialect $dialect): string
4648
$columns = $this->fields->isEmpty()
4749
? '*'
4850
: $this->fields
51+
->flatMap(function (string|Stringable|FieldStatement $field) {
52+
if ($field instanceof FieldStatement) {
53+
return $field;
54+
}
55+
56+
return str($field)->explode(',')->toArray();
57+
})
4958
->map(function (string|Stringable|FieldStatement $field) use ($dialect) {
5059
if (! $field instanceof FieldStatement) {
5160
$field = new FieldStatement($field);

tests/Integration/Database/Builder/SelectQueryBuilderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ public function test_select_query(): void
4545
$this->assertSame(['Timeline Taxi', '1', '2025-01-01'], $bindings);
4646
}
4747

48+
public function test_select_multiple_fields_as_one(): void
49+
{
50+
$this->assertSameWithoutBackticks(
51+
'SELECT title AS t, index AS i FROM chapters',
52+
query('chapters')
53+
->select('title AS t, index AS i')
54+
->build()
55+
->compile(),
56+
);
57+
58+
$this->assertSameWithoutBackticks(
59+
'SELECT title, index FROM chapters',
60+
query('chapters')
61+
->select('title, index')
62+
->build()
63+
->compile(),
64+
);
65+
}
66+
4867
public function test_select_without_any_fields_specified(): void
4968
{
5069
$query = query('chapters')->select()->build();

0 commit comments

Comments
 (0)