Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function orderBy(string $field, Direction $direction = Direction::ASC): s
return $this->orderByRaw($field);
}

$this->select->orderBy[] = new OrderByStatement("`{$field}` {$direction->value}");
$this->select->orderBy[] = new OrderByStatement(field: $field, direction: $direction);

return $this;
}
Expand All @@ -241,7 +241,7 @@ public function orderBy(string $field, Direction $direction = Direction::ASC): s
*/
public function orderByRaw(string $statement): self
{
$this->select->orderBy[] = new OrderByStatement($statement);
$this->select->orderBy[] = new RawStatement($statement);

return $this;
}
Expand Down
13 changes: 11 additions & 2 deletions packages/database/src/QueryStatements/OrderByStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
namespace Tempest\Database\QueryStatements;

use Tempest\Database\Config\DatabaseDialect;
use Tempest\Database\Direction;
use Tempest\Database\QueryStatement;

use function Tempest\Support\str;

final readonly class OrderByStatement implements QueryStatement
{
public function __construct(
private string $orderBy,
private string $field,
private Direction $direction = Direction::ASC,
) {}

public function compile(DatabaseDialect $dialect): string
{
return $this->orderBy;
$quoted = str($this->field)
->explode(separator: '.')
->map($dialect->quoteIdentifier(...))
->implode(glue: '.');

return "{$quoted} {$this->direction->value}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function compile(DatabaseDialect $dialect): string

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Tempest\Database\Builder\FieldDefinition;
use Tempest\Database\Builder\TableDefinition;
use Tempest\Database\Config\DatabaseDialect;
use Tempest\Database\Direction;
use Tempest\Database\QueryStatements\GroupByStatement;
use Tempest\Database\QueryStatements\HavingStatement;
use Tempest\Database\QueryStatements\JoinStatement;
Expand All @@ -26,7 +27,7 @@ public function test_select(): void
fields: arr(['`a`', 'b', 'c', new FieldDefinition($tableDefinition, 'd', 'd_alias')]),
join: arr(new JoinStatement('INNER JOIN foo ON bar.id = foo.id')),
where: arr(new WhereStatement('`foo` = "bar"')),
orderBy: arr(new OrderByStatement('`foo` DESC')),
orderBy: arr(new OrderByStatement(field: 'foo', direction: Direction::DESC)),
groupBy: arr(new GroupByStatement('`foo`')),
having: arr(new HavingStatement('`foo` = "bar"')),
limit: 10,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Database\QueryStatements;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use Tempest\Database\Config\DatabaseDialect;
use Tempest\Database\Direction;
use Tempest\Database\QueryStatements\OrderByStatement;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

/**
* @internal
*/
final class OrderByStatementTest extends FrameworkIntegrationTestCase
{
#[Test]
#[TestWith([DatabaseDialect::MYSQL, '`books`.`title` ASC'])]
#[TestWith([DatabaseDialect::SQLITE, '`books`.`title` ASC'])]
#[TestWith([DatabaseDialect::POSTGRESQL, '"books"."title" ASC'])]
public function qualified_field_is_quoted_per_dialect(DatabaseDialect $dialect, string $expected): void
{
$statement = new OrderByStatement(field: 'books.title', direction: Direction::ASC);

$this->assertSame(expected: $expected, actual: $statement->compile(dialect: $dialect));
}

#[Test]
#[TestWith([DatabaseDialect::MYSQL, '`title` DESC'])]
#[TestWith([DatabaseDialect::SQLITE, '`title` DESC'])]
#[TestWith([DatabaseDialect::POSTGRESQL, '"title" DESC'])]
public function bare_column_is_quoted_per_dialect(DatabaseDialect $dialect, string $expected): void
{
$statement = new OrderByStatement(field: 'title', direction: Direction::DESC);

$this->assertSame(expected: $expected, actual: $statement->compile(dialect: $dialect));
}
}
Loading