Skip to content

Commit

Permalink
Merge branch '2.0' into 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent4vx committed Feb 2, 2023
2 parents bea3509 + 86c38ea commit 5ec6934
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Query/Compiler/SqlCompiler.php
Expand Up @@ -436,7 +436,7 @@ protected function compileExpressionColumn(CompilableClause $query, $column, $al

$column = $query->preprocessor()->field($column);

if (strpos($column, '*') !== false) {
if ($column[-1] === '*') {
return $column;
}

Expand Down
13 changes: 8 additions & 5 deletions src/Query/Expression/Aggregate.php
Expand Up @@ -46,10 +46,13 @@ public function __construct(string $attribute)
*/
final public function build(CompilableClause $query, object $compiler)
{
return $this->expression(
$compiler->platform()->grammar(),
$compiler->quoteIdentifier($query, $query->preprocessor()->field($this->attribute))
);
$attribute = $this->attribute;

if ($attribute !== '*') {
$attribute = $compiler->quoteIdentifier($query, $query->preprocessor()->field($attribute));
}

return $this->expression($compiler->platform()->grammar(), $attribute);
}

/**
Expand Down Expand Up @@ -120,7 +123,7 @@ protected function expression(AbstractPlatform $platform, string $attribute): st
*
* @return self
*/
public static function count(string $attribute): self
public static function count(string $attribute = '*'): self
{
return new class ($attribute) extends Aggregate {
protected function expression(AbstractPlatform $platform, string $attribute): string
Expand Down
16 changes: 16 additions & 0 deletions tests/Query/Expression/AggregateTest.php
Expand Up @@ -100,6 +100,22 @@ public function test_count()

$this->assertEquals('SELECT COUNT(t0.id_) as a FROM faction_ t0 WHERE t0.enabled_ = ?', $query->toSql());
$this->assertEquals(3, $query->execute()->current()['a']);

$query = Faction::select(['a' => Aggregate::count('id')]);
$query->useQuoteIdentifier(true);
$this->assertEquals('SELECT COUNT("t0"."id_") as "a" FROM "faction_" "t0" WHERE "t0"."enabled_" = ?', $query->toSql());
}

/**
*
*/
public function test_count_wildcard()
{
$query = Faction::select(['a' => Aggregate::count()]);
$query->useQuoteIdentifier(true);

$this->assertEquals('SELECT COUNT(*) as "a" FROM "faction_" "t0" WHERE "t0"."enabled_" = ?', $query->toSql());
$this->assertEquals(3, $query->execute()->current()['a']);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Query/QueryTest.php
Expand Up @@ -1600,4 +1600,14 @@ public function test_join_with_fk_which_is_a_callable_string_should_be_used_as_s
$query->toSql()
);
}

public function test_count_alias()
{
$query = $this->query()->addSelect(['count' => 'COUNT(*)']);

$this->assertEquals(
'SELECT COUNT(*) as count FROM test_',
$query->toSql()
);
}
}

0 comments on commit 5ec6934

Please sign in to comment.