Skip to content

Commit 8e5ff7b

Browse files
committed
Removes useless alias
1 parent 1300c0e commit 8e5ff7b

File tree

5 files changed

+9
-93
lines changed

5 files changed

+9
-93
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"symfony/uid": "^7.1",
3535
"symfony/var-dumper": "^7.1",
3636
"symfony/var-exporter": "^7.1",
37-
"tempest/highlight": "^2.11.2",
37+
"tempest/highlight": "^2.11.4",
3838
"vlucas/phpdotenv": "^5.6",
3939
"voku/portable-ascii": "^2.0.3"
4040
},

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ public function execute(mixed ...$bindings): int
4040
return $this->build()->fetchFirst(...$bindings)[$this->count->getKey()];
4141
}
4242

43-
/** @return self<TModelClass> */
44-
public function as(string $alias): self
45-
{
46-
$this->count->alias = $alias;
47-
48-
return $this;
49-
}
50-
5143
/** @return self<TModelClass> */
5244
public function distinct(): self
5345
{

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
final class CountStatement implements QueryStatement
1313
{
14-
public ?string $alias = null;
15-
1614
public bool $distinct = false;
1715

1816
public function __construct(
@@ -25,9 +23,8 @@ public function compile(DatabaseDialect $dialect): string
2523
{
2624
$query = arr([
2725
sprintf(
28-
'SELECT COUNT(%s)%s',
26+
'SELECT COUNT(%s)',
2927
$this->getCountArgument(),
30-
$this->alias ? " AS `{$this->alias}`" : '',
3128
),
3229
sprintf('FROM `%s`', $this->table->name),
3330
]);
@@ -54,10 +51,6 @@ public function getCountArgument(): string
5451

5552
public function getKey(): string
5653
{
57-
if ($this->alias !== null) {
58-
return $this->alias;
59-
}
60-
6154
return "COUNT({$this->getCountArgument()})";
6255
}
6356
}

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,9 @@
33
namespace Tempest\Database\Tests\QueryStatements;
44

55
use PHPUnit\Framework\TestCase;
6-
use Tempest\Database\Builder\FieldDefinition;
76
use Tempest\Database\Builder\TableDefinition;
87
use Tempest\Database\Config\DatabaseDialect;
98
use Tempest\Database\QueryStatements\CountStatement;
10-
use Tempest\Database\QueryStatements\GroupByStatement;
11-
use Tempest\Database\QueryStatements\HavingStatement;
12-
use Tempest\Database\QueryStatements\JoinStatement;
13-
use Tempest\Database\QueryStatements\OrderByStatement;
14-
use Tempest\Database\QueryStatements\SelectStatement;
15-
use Tempest\Database\QueryStatements\WhereStatement;
16-
17-
use function Tempest\Support\arr;
189

1910
final class CountStatementTest extends TestCase
2011
{
@@ -37,19 +28,17 @@ public function test_count_statement(): void
3728
$this->assertSame($expected, $statement->compile(DatabaseDialect::SQLITE));
3829
}
3930

40-
public function test_count_statement_with_alias(): void
31+
public function test_count_statement_with_specified_column(): void
4132
{
4233
$tableDefinition = new TableDefinition('foo', 'bar');
4334

4435
$statement = new CountStatement(
4536
table: $tableDefinition,
46-
column: null,
37+
column: 'foobar',
4738
);
4839

49-
$statement->alias = 'foobar';
50-
5140
$expected = <<<SQL
52-
SELECT COUNT(*) AS `foobar`
41+
SELECT COUNT(`foobar`)
5342
FROM `foo`
5443
SQL;
5544

@@ -58,7 +47,7 @@ public function test_count_statement_with_alias(): void
5847
$this->assertSame($expected, $statement->compile(DatabaseDialect::SQLITE));
5948
}
6049

61-
public function test_count_statement_with_specified_column(): void
50+
public function test_count_statement_with_distinct_specified_column(): void
6251
{
6352
$tableDefinition = new TableDefinition('foo', 'bar');
6453

@@ -67,8 +56,10 @@ public function test_count_statement_with_specified_column(): void
6756
column: 'foobar',
6857
);
6958

59+
$statement->distinct = true;
60+
7061
$expected = <<<SQL
71-
SELECT COUNT(`foobar`)
62+
SELECT COUNT(DISTINCT `foobar`)
7263
FROM `foo`
7364
SQL;
7465

tests/Integration/Database/Builder/CountQueryBuilderTest.php

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,6 @@ public function test_simple_count_query(): void
4040
$this->assertSame(['Timeline Taxi', '1', '2025-01-01'], $bindings);
4141
}
4242

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

98-
public function test_count_query_with_specified_field_and_alias(): void
99-
{
100-
$query = query('chapters')
101-
->count('title')
102-
->as('total')
103-
->build();
104-
105-
$sql = $query->getSql();
106-
107-
$expected = <<<SQL
108-
SELECT COUNT(`title`) AS `total`
109-
FROM `chapters`
110-
SQL;
111-
112-
$this->assertSame($expected, $sql);
113-
}
114-
11573
public function test_count_query_without_specifying_column_cannot_be_distinct(): void
11674
{
11775
$this->expectException(CannotCountDistinctWithoutSpecifyingAColumn::class);
@@ -149,24 +107,6 @@ public function test_count_query_with_distinct_specified_field(): void
149107
$this->assertSame($expected, $sql);
150108
}
151109

152-
public function test_count_query_with_distinct_specified_field_and_alias(): void
153-
{
154-
$query = query('chapters')
155-
->count('title')
156-
->as('total')
157-
->distinct()
158-
->build();
159-
160-
$sql = $query->getSql();
161-
162-
$expected = <<<SQL
163-
SELECT COUNT(DISTINCT `title`) AS `total`
164-
FROM `chapters`
165-
SQL;
166-
167-
$this->assertSame($expected, $sql);
168-
}
169-
170110
public function test_count_from_model(): void
171111
{
172112
$query = query(Author::class)->count()->build();

0 commit comments

Comments
 (0)