Skip to content

Commit 32bf6b4

Browse files
committed
Add alias
1 parent cea03e1 commit 32bf6b4

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

src/Tempest/Database/src/Builder/QueryBuilders/CountQueryBuilder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ public function __construct(string|object $model, ?string $column = null)
3636

3737
public function execute(mixed ...$bindings): int
3838
{
39-
$key = "COUNT({$this->count->countArgument})";
39+
return $this->build()->fetchFirst(...$bindings)[$this->count->getKey()];
40+
}
4041

41-
return $this->build()->fetchFirst(...$bindings)[$key];
42+
/** @return self<TModelClass> */
43+
public function as(string $alias): self
44+
{
45+
$this->count->alias = $alias;
46+
47+
return $this;
4248
}
4349

4450
/** @return self<TModelClass> */

src/Tempest/Database/src/QueryStatements/CountStatement.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ final class CountStatement implements QueryStatement
1313
{
1414
public readonly string $countArgument;
1515

16+
public ?string $alias = null;
17+
1618
public function __construct(
1719
public readonly TableDefinition $table,
1820
public ?string $column = null,
@@ -26,7 +28,11 @@ public function __construct(
2628
public function compile(DatabaseDialect $dialect): string
2729
{
2830
$query = arr([
29-
sprintf('SELECT COUNT(%s)', $this->countArgument),
31+
sprintf(
32+
'SELECT COUNT(%s)%s',
33+
$this->countArgument,
34+
$this->alias ? " AS `{$this->alias}`" : '',
35+
),
3036
sprintf('FROM `%s`', $this->table->name),
3137
]);
3238

@@ -38,4 +44,13 @@ public function compile(DatabaseDialect $dialect): string
3844

3945
return $query->implode(PHP_EOL);
4046
}
47+
48+
public function getKey(): string
49+
{
50+
if ($this->alias !== null) {
51+
return $this->alias;
52+
}
53+
54+
return "COUNT({$this->countArgument})";
55+
}
4156
}

src/Tempest/Database/tests/QueryStatements/CountStatementTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ public function test_count_statement(): void
3737
$this->assertSame($expected, $statement->compile(DatabaseDialect::SQLITE));
3838
}
3939

40+
public function test_count_statement_with_alias(): void
41+
{
42+
$tableDefinition = new TableDefinition('foo', 'bar');
43+
44+
$statement = new CountStatement(
45+
table: $tableDefinition,
46+
column: null,
47+
);
48+
49+
$statement->alias = 'foobar';
50+
51+
$expected = <<<SQL
52+
SELECT COUNT(*) AS `foobar`
53+
FROM `foo`
54+
SQL;
55+
56+
$this->assertSame($expected, $statement->compile(DatabaseDialect::MYSQL));
57+
$this->assertSame($expected, $statement->compile(DatabaseDialect::POSTGRESQL));
58+
$this->assertSame($expected, $statement->compile(DatabaseDialect::SQLITE));
59+
}
60+
4061
public function test_count_statement_with_specified_column(): void
4162
{
4263
$tableDefinition = new TableDefinition('foo', 'bar');

tests/Integration/Database/Builder/CountQueryBuilderTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
final class CountQueryBuilderTest extends FrameworkIntegrationTestCase
1717
{
18-
public function test_count_query(): void
18+
public function test_simple_count_query(): void
1919
{
2020
$query = query('chapters')
2121
->count()
@@ -39,6 +39,31 @@ public function test_count_query(): void
3939
$this->assertSame(['Timeline Taxi', '1', '2025-01-01'], $bindings);
4040
}
4141

42+
public function test_count_query_with_alias(): void
43+
{
44+
$query = query('chapters')
45+
->count()
46+
->as('total')
47+
->where('`title` = ?', 'Timeline Taxi')
48+
->andWhere('`index` <> ?', '1')
49+
->orWhere('`createdAt` > ?', '2025-01-01')
50+
->build();
51+
52+
$expected = <<<SQL
53+
SELECT COUNT(*) AS `total`
54+
FROM `chapters`
55+
WHERE `title` = ?
56+
AND `index` <> ?
57+
OR `createdAt` > ?
58+
SQL;
59+
60+
$sql = $query->getSql();
61+
$bindings = $query->bindings;
62+
63+
$this->assertSame($expected, $sql);
64+
$this->assertSame(['Timeline Taxi', '1', '2025-01-01'], $bindings);
65+
}
66+
4267
public function test_count_query_with_specified_field(): void
4368
{
4469
$query = query('chapters')->count('title')->build();

0 commit comments

Comments
 (0)