Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/database/src/Builder/QueryBuilders/SelectQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Tempest\Database\OnDatabase;
use Tempest\Database\Query;
use Tempest\Database\QueryStatements\FieldStatement;
use Tempest\Database\QueryStatements\GroupByStatement;
use Tempest\Database\QueryStatements\HasWhereStatements;
use Tempest\Database\QueryStatements\HavingStatement;
use Tempest\Database\QueryStatements\JoinStatement;
use Tempest\Database\QueryStatements\OrderByStatement;
use Tempest\Database\QueryStatements\RawStatement;
Expand Down Expand Up @@ -123,6 +125,24 @@ public function orderBy(string $statement): self
return $this;
}

/** @return self<TModelClass> */
public function groupBy(string $statement): self
{
$this->select->groupBy[] = new GroupByStatement($statement);

return $this;
}

/** @return self<TModelClass> */
public function having(string $statement, mixed ...$bindings): self
{
$this->select->having[] = new HavingStatement($statement);

$this->bind(...$bindings);

return $this;
}

/** @return self<TModelClass> */
public function limit(int $limit): self
{
Expand Down
12 changes: 6 additions & 6 deletions packages/database/src/QueryStatements/SelectStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ public function compile(DatabaseDialect $dialect): string
->implode(PHP_EOL);
}

if ($this->orderBy->isNotEmpty()) {
$query[] = 'ORDER BY ' . $this->orderBy
->map(fn (OrderByStatement $orderBy) => $orderBy->compile($dialect))
->implode(', ');
}

if ($this->groupBy->isNotEmpty()) {
$query[] = 'GROUP BY ' . $this->groupBy
->map(fn (GroupByStatement $groupBy) => $groupBy->compile($dialect))
Expand All @@ -72,6 +66,12 @@ public function compile(DatabaseDialect $dialect): string
->implode(PHP_EOL);
}

if ($this->orderBy->isNotEmpty()) {
$query[] = 'ORDER BY ' . $this->orderBy
->map(fn (OrderByStatement $orderBy) => $orderBy->compile($dialect))
->implode(', ');
}

if ($this->limit !== null) {
$query[] = 'LIMIT ' . $this->limit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function test_select(): void
FROM `foo` AS `bar`
INNER JOIN foo ON bar.id = foo.id
WHERE `foo` = "bar"
ORDER BY `foo` DESC
GROUP BY `foo`
HAVING `foo` = "bar"
ORDER BY `foo` DESC
LIMIT 10
OFFSET 100
SQL;
Expand Down
32 changes: 32 additions & 0 deletions tests/Integration/Database/Builder/SelectQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,38 @@ public function test_eager_loads_combined_with_manual_loads(): void
SQL, $query);
}

public function test_group_by(): void
{
$sql = query('authors')
->select()
->groupBy('name')
->toSql();

$expected = <<<SQL
SELECT *
FROM authors
GROUP BY name
SQL;

$this->assertSameWithoutBackticks($expected, $sql);
}

public function test_having(): void
{
$sql = query('authors')
->select()
->having('name = ?', 'Brent')
->toSql();

$expected = <<<SQL
SELECT *
FROM authors
HAVING name = ?
SQL;

$this->assertSameWithoutBackticks($expected, $sql);
}

private function seed(): void
{
$this->migrate(
Expand Down