• 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

80.0
/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\Exceptions\CannotCountDistinctWithoutSpecifyingAColumn;
10
use Tempest\Database\Query;
11
use Tempest\Database\QueryStatements\CountStatement;
12
use Tempest\Database\QueryStatements\WhereStatement;
13
use Tempest\Support\Conditions\HasConditions;
14

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

22
    private ?ModelDefinition $modelDefinition;
23

24
    private CountStatement $count;
25

26
    private array $bindings = [];
27

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

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

NEW
38
    public function execute(mixed ...$bindings): int
×
39
    {
NEW
40
        return $this->build()->fetchFirst(...$bindings)[$this->count->getKey()];
×
41
    }
42

43
    /** @return self<TModelClass> */
44
    public function distinct(): self
3✔
45
    {
46
        if ($this->count->column === null || $this->count->column === '*') {
3✔
47
            throw new CannotCountDistinctWithoutSpecifyingAColumn();
2✔
48
        }
49

50
        $this->count->distinct = true;
1✔
51

52
        return $this;
1✔
53
    }
54

55
    /** @return self<TModelClass> */
56
    public function where(string $where, mixed ...$bindings): self
2✔
57
    {
58
        $this->count->where[] = new WhereStatement($where);
2✔
59

60
        $this->bind(...$bindings);
2✔
61

62
        return $this;
2✔
63
    }
64

65
    public function andWhere(string $where, mixed ...$bindings): self
2✔
66
    {
67
        return $this->where("AND {$where}", ...$bindings);
2✔
68
    }
69

70
    public function orWhere(string $where, mixed ...$bindings): self
2✔
71
    {
72
        return $this->where("OR {$where}", ...$bindings);
2✔
73
    }
74

75
    /** @return self<TModelClass> */
NEW
76
    public function whereField(string $field, mixed $value): self
×
77
    {
NEW
78
        $field = $this->modelDefinition->getFieldDefinition($field);
×
79

NEW
80
        return $this->where("{$field} = :{$field->name}", ...[$field->name => $value]);
×
81
    }
82

83
    /** @return self<TModelClass> */
84
    public function bind(mixed ...$bindings): self
2✔
85
    {
86
        $this->bindings = [...$this->bindings, ...$bindings];
2✔
87

88
        return $this;
2✔
89
    }
90

NEW
91
    public function toSql(): string
×
92
    {
NEW
93
        return $this->build()->getSql();
×
94
    }
95

96
    public function build(array $bindings = []): Query
6✔
97
    {
98
        return new Query($this->count, [...$this->bindings, ...$bindings]);
6✔
99
    }
100

101
    private function resolveTable(string|object $model): TableDefinition
8✔
102
    {
103
        if ($this->modelDefinition === null) {
8✔
104
            return new TableDefinition($model);
7✔
105
        }
106

107
        return $this->modelDefinition->getTableDefinition();
1✔
108
    }
109
}
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