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

tempestphp / tempest-framework / 14698093036

26 Apr 2025 06:23AM UTC coverage: 80.248% (+0.03%) from 80.219%
14698093036

push

github

web-flow
feat(database): add `Count` query builder and statement (#1174)

58 of 67 new or added lines in 4 files covered. (86.57%)

11843 of 14758 relevant lines covered (80.25%)

106.71 hits per line

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

92.0
/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 bool $distinct = false;
15

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

22
    public function compile(DatabaseDialect $dialect): string
9✔
23
    {
24
        $query = arr([
9✔
25
            sprintf(
9✔
26
                'SELECT COUNT(%s)',
9✔
27
                $this->getCountArgument(),
9✔
28
            ),
9✔
29
            sprintf('FROM `%s`', $this->table->name),
9✔
30
        ]);
9✔
31

32
        if ($this->where->isNotEmpty()) {
9✔
33
            $query[] = 'WHERE ' . $this->where
2✔
34
                ->map(fn (WhereStatement $where) => $where->compile($dialect))
2✔
35
                ->implode(PHP_EOL);
2✔
36
        }
37

38
        return $query->implode(PHP_EOL);
9✔
39
    }
40

41
    public function getCountArgument(): string
9✔
42
    {
43
        return $this->column === null || $this->column === '*'
9✔
44
            ? '*'
5✔
45
            : sprintf(
9✔
46
                '%s`%s`',
9✔
47
                $this->distinct ? 'DISTINCT ' : '',
9✔
48
                $this->column,
9✔
49
            );
9✔
50
    }
51

NEW
52
    public function getKey(): string
×
53
    {
NEW
54
        return "COUNT({$this->getCountArgument()})";
×
55
    }
56
}
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

© 2025 Coveralls, Inc