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

tempestphp / tempest-framework / 14661590915

25 Apr 2025 09:37AM UTC coverage: 80.232% (+0.01%) from 80.219%
14661590915

Pull #1174

github

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

42 of 50 new or added lines in 3 files covered. (84.0%)

11827 of 14741 relevant lines covered (80.23%)

106.41 hits per line

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

74.19
/src/Tempest/Database/src/Builder/QueryBuilders/CountQueryBuilder.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Database\Builder\QueryBuilders;
6

7
use Tempest\Database\Builder\ModelDefinition;
8
use Tempest\Database\Builder\TableDefinition;
9
use Tempest\Database\Query;
10
use Tempest\Database\QueryStatements\CountStatement;
11
use Tempest\Database\QueryStatements\WhereStatement;
12
use Tempest\Support\Conditions\HasConditions;
13

14
/**
15
 * @template TModelClass of object
16
 */
17
final class CountQueryBuilder
18
{
19
    use HasConditions;
20

21
    private ?ModelDefinition $modelDefinition;
22

23
    private CountStatement $count;
24

25
    private array $bindings = [];
26

27
    public function __construct(string|object $model, ?string $column = null)
4✔
28
    {
29
        $this->modelDefinition = ModelDefinition::tryFrom($model);
4✔
30

31
        $this->count = new CountStatement(
4✔
32
            table: $this->resolveTable($model),
4✔
33
            column: $column,
4✔
34
        );
4✔
35
    }
36

NEW
37
    public function execute(mixed ...$bindings): int
×
38
    {
NEW
39
        $key = "COUNT({$this->count->countArgument})";
×
40

NEW
41
        return $this->build()->fetchFirst(...$bindings)[$key];
×
42
    }
43

44
    /** @return self<TModelClass> */
45
    public function where(string $where, mixed ...$bindings): self
2✔
46
    {
47
        $this->count->where[] = new WhereStatement($where);
2✔
48

49
        $this->bind(...$bindings);
2✔
50

51
        return $this;
2✔
52
    }
53

54
    public function andWhere(string $where, mixed ...$bindings): self
2✔
55
    {
56
        return $this->where("AND {$where}", ...$bindings);
2✔
57
    }
58

59
    public function orWhere(string $where, mixed ...$bindings): self
2✔
60
    {
61
        return $this->where("OR {$where}", ...$bindings);
2✔
62
    }
63

64
    /** @return self<TModelClass> */
NEW
65
    public function whereField(string $field, mixed $value): self
×
66
    {
NEW
67
        $field = $this->modelDefinition->getFieldDefinition($field);
×
68

NEW
69
        return $this->where("{$field} = :{$field->name}", ...[$field->name => $value]);
×
70
    }
71

72
    /** @return self<TModelClass> */
73
    public function bind(mixed ...$bindings): self
2✔
74
    {
75
        $this->bindings = [...$this->bindings, ...$bindings];
2✔
76

77
        return $this;
2✔
78
    }
79

NEW
80
    public function toSql(): string
×
81
    {
NEW
82
        return $this->build()->getSql();
×
83
    }
84

85
    public function build(array $bindings = []): Query
4✔
86
    {
87
        return new Query($this->count, [...$this->bindings, ...$bindings]);
4✔
88
    }
89

90
    private function resolveTable(string|object $model): TableDefinition
4✔
91
    {
92
        if ($this->modelDefinition === null) {
4✔
93
            return new TableDefinition($model);
3✔
94
        }
95

96
        return $this->modelDefinition->getTableDefinition();
1✔
97
    }
98
}
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