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

tempestphp / tempest-framework / 14662303859

25 Apr 2025 10:16AM UTC coverage: 80.225% (+0.006%) from 80.219%
14662303859

Pull #1174

github

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

49 of 60 new or added lines in 3 files covered. (81.67%)

11834 of 14751 relevant lines covered (80.23%)

106.45 hits per line

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

78.79
/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)
5✔
28
    {
29
        $this->modelDefinition = ModelDefinition::tryFrom($model);
5✔
30

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

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

42
    /** @return self<TModelClass> */
43
    public function as(string $alias): self
1✔
44
    {
45
        $this->count->alias = $alias;
1✔
46

47
        return $this;
1✔
48
    }
49

50
    /** @return self<TModelClass> */
51
    public function where(string $where, mixed ...$bindings): self
3✔
52
    {
53
        $this->count->where[] = new WhereStatement($where);
3✔
54

55
        $this->bind(...$bindings);
3✔
56

57
        return $this;
3✔
58
    }
59

60
    public function andWhere(string $where, mixed ...$bindings): self
3✔
61
    {
62
        return $this->where("AND {$where}", ...$bindings);
3✔
63
    }
64

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

70
    /** @return self<TModelClass> */
NEW
71
    public function whereField(string $field, mixed $value): self
×
72
    {
NEW
73
        $field = $this->modelDefinition->getFieldDefinition($field);
×
74

NEW
75
        return $this->where("{$field} = :{$field->name}", ...[$field->name => $value]);
×
76
    }
77

78
    /** @return self<TModelClass> */
79
    public function bind(mixed ...$bindings): self
3✔
80
    {
81
        $this->bindings = [...$this->bindings, ...$bindings];
3✔
82

83
        return $this;
3✔
84
    }
85

NEW
86
    public function toSql(): string
×
87
    {
NEW
88
        return $this->build()->getSql();
×
89
    }
90

91
    public function build(array $bindings = []): Query
5✔
92
    {
93
        return new Query($this->count, [...$this->bindings, ...$bindings]);
5✔
94
    }
95

96
    private function resolveTable(string|object $model): TableDefinition
5✔
97
    {
98
        if ($this->modelDefinition === null) {
5✔
99
            return new TableDefinition($model);
4✔
100
        }
101

102
        return $this->modelDefinition->getTableDefinition();
1✔
103
    }
104
}
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