• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 14662931729

25 Apr 2025 10:52AM UTC coverage: 80.242% (+0.02%) from 80.219%
14662931729

Pull #1174

github

web-flow
Merge 1300c0e23 into be8067395
Pull Request #1174: feat(database): add `Count` query builder and statement

62 of 73 new or added lines in 4 files covered. (84.93%)

11847 of 14764 relevant lines covered (80.24%)

106.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/src/Tempest/Database/src/QueryStatements/CountStatement.php
1
<?php
2

3
namespace Tempest\Database\QueryStatements;
4

5
use Tempest\Database\Builder\TableDefinition;
6
use Tempest\Database\Config\DatabaseDialect;
7
use Tempest\Database\QueryStatement;
8
use Tempest\Support\Arr\ImmutableArray;
9

10
use function Tempest\Support\arr;
11

12
final class CountStatement implements QueryStatement
13
{
14
    public ?string $alias = null;
15

16
    public bool $distinct = false;
17

18
    public function __construct(
14✔
19
        public readonly TableDefinition $table,
20
        public ?string $column = null,
21
        public ImmutableArray $where = new ImmutableArray(),
22
    ) {}
14✔
23

24
    public function compile(DatabaseDialect $dialect): string
12✔
25
    {
26
        $query = arr([
12✔
27
            sprintf(
12✔
28
                'SELECT COUNT(%s)%s',
12✔
29
                $this->getCountArgument(),
12✔
30
                $this->alias ? " AS `{$this->alias}`" : '',
12✔
31
            ),
12✔
32
            sprintf('FROM `%s`', $this->table->name),
12✔
33
        ]);
12✔
34

35
        if ($this->where->isNotEmpty()) {
12✔
36
            $query[] = 'WHERE ' . $this->where
3✔
37
                ->map(fn (WhereStatement $where) => $where->compile($dialect))
3✔
38
                ->implode(PHP_EOL);
3✔
39
        }
40

41
        return $query->implode(PHP_EOL);
12✔
42
    }
43

44
    public function getCountArgument(): string
12✔
45
    {
46
        return $this->column === null || $this->column === '*'
12✔
47
            ? '*'
7✔
48
            : sprintf(
12✔
49
                '%s`%s`',
12✔
50
                $this->distinct ? 'DISTINCT ' : '',
12✔
51
                $this->column,
12✔
52
            );
12✔
53
    }
54

NEW
55
    public function getKey(): string
×
56
    {
NEW
57
        if ($this->alias !== null) {
×
NEW
58
            return $this->alias;
×
59
        }
60

NEW
61
        return "COUNT({$this->getCountArgument()})";
×
62
    }
63
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc