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

letsdrink / ouzo / 6036930958

31 Aug 2023 11:53AM UTC coverage: 82.907% (-3.1%) from 86.01%
6036930958

push

github

web-flow
Add string space const (#318)

4802 of 5792 relevant lines covered (82.91%)

32.99 hits per line

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

86.79
/src/Ouzo/Core/Db/Query.php
1
<?php
2
/*
3
 * Copyright (c) Ouzo contributors, https://github.com/letsdrink/ouzo
4
 * This file is made available under the MIT License (view the LICENSE file for more information).
5
 */
6

7
namespace Ouzo\Db;
8

9
use Ouzo\Db\WhereClause\WhereClause;
10
use Ouzo\DbException;
11
use Ouzo\Restriction\Restriction;
12
use PDO;
13

14
class Query
15
{
16
    public Query|string|null $table = null;
17
    public ?string $aliasTable = null;
18
    public bool $distinct = false;
19
    /** @var string[] */
20
    public ?array $selectColumns = [];
21
    public int $selectType = PDO::FETCH_ASSOC;
22
    public string|array|null $order = null;
23
    public ?int $limit = null;
24
    public ?int $offset = null;
25
    public array $updateAttributes = [];
26
    public array $upsertConflictColumns = [];
27
    /** @var WhereClause[] */
28
    public array $whereClauses = [];
29
    /** @var JoinClause[] */
30
    public array $joinClauses = [];
31
    /** @var JoinClause[] */
32
    public array $usingClauses = [];
33
    public ?int $type;
34
    public array $options = [];
35
    public string|array|null $groupBy = null;
36
    public bool $lockForUpdate = false;
37
    public ?string $comment = null;
38
    public array $distinctOnColumns = [];
39

40
    public function __construct(?int $type = null)
41
    {
42
        $this->type = $type ? $type : QueryType::$SELECT;
696✔
43
    }
44

45
    public static function newInstance(?int $type = null): static
46
    {
47
        return new Query($type);
461✔
48
    }
49

50
    public static function insert(array $attributes): static
51
    {
52
        return Query::newInstance(QueryType::$INSERT)->attributes($attributes);
457✔
53
    }
54

55
    public static function insertOrDoNoting(array $attributes): static
56
    {
57
        return Query::newInstance(QueryType::$INSERT_OR_DO_NOTHING)->attributes($attributes);
1✔
58
    }
59

60
    public static function update(array $attributes): static
61
    {
62
        return Query::newInstance(QueryType::$UPDATE)->attributes($attributes);
36✔
63
    }
64

65
    public static function upsert(array $attributes): static
66
    {
67
        return Query::newInstance(QueryType::$UPSERT)->attributes($attributes);
8✔
68
    }
69

70
    public static function select(?array $selectColumns = null): static
71
    {
72
        $query = new Query();
46✔
73
        $query->selectColumns = $selectColumns;
46✔
74
        return $query;
46✔
75
    }
76

77
    public static function selectDistinct(?array $selectColumns = null): static
78
    {
79
        $query = self::select($selectColumns);
3✔
80
        $query->distinct = true;
3✔
81
        return $query;
3✔
82
    }
83

84
    public static function count(): static
85
    {
86
        return new Query(QueryType::$COUNT);
6✔
87
    }
88

89
    public static function delete(): static
90
    {
91
        return new Query(QueryType::$DELETE);
3✔
92
    }
93

94
    public function attributes(array $attributes): static
95
    {
96
        $this->updateAttributes = $attributes;
461✔
97
        return $this;
461✔
98
    }
99

100
    public function table(string|Query $table): static
101
    {
102
        $this->table = $table;
488✔
103
        return $this;
488✔
104
    }
105

106
    public function into(string $table): static
107
    {
108
        return $this->table($table);
457✔
109
    }
110

111
    public function from(string|Query $table, ?string $alias = null): static
112
    {
113
        $this->aliasTable = $alias;
21✔
114
        return $this->table($table);
21✔
115
    }
116

117
    /** @param string|string[]|null $order */
118
    public function order(array|string|null $order): static
119
    {
120
        $this->order = $order;
6✔
121
        return $this;
6✔
122
    }
123

124
    public function limit(int $limit): static
125
    {
126
        $this->limit = $limit;
9✔
127
        return $this;
9✔
128
    }
129

130
    public function offset(int $offset): static
131
    {
132
        $this->offset = $offset;
9✔
133
        return $this;
9✔
134
    }
135

136
    public function distinctOn(array $columns): static
137
    {
138
        if ($this->distinct) {
4✔
139
            throw new DbException('Cannot use DISTINCT together with DISTINCT ON.');
×
140
        }
141
        $this->distinctOnColumns = $columns;
4✔
142
        return $this;
4✔
143
    }
144

145
    public function where(array|string|WhereClause $where = '', mixed $whereValues = null): static
146
    {
147
        $this->validateParameters($where);
452✔
148
        $this->whereClauses[] = WhereClause::create($where, $whereValues);
449✔
149
        return $this;
449✔
150
    }
151

152
    public function addUsing(JoinClause $usingClause): static
153
    {
154
        $this->usingClauses[] = $usingClause;
8✔
155
        return $this;
8✔
156
    }
157

158
    public function join(string $joinTable, string $joinKey, string $idName, string|array|null $alias = null, string $type = 'LEFT', array $on = []): static
159
    {
160
        $onClauses = [WhereClause::create($on)];
24✔
161
        $this->joinClauses[] = new JoinClause($joinTable, $joinKey, $idName, $this->aliasTable ?: $this->table, $alias, $type, $onClauses);
24✔
162
        return $this;
24✔
163
    }
164

165
    public function addJoin(JoinClause $join): static
166
    {
167
        $this->joinClauses[] = $join;
84✔
168
        return $this;
84✔
169
    }
170

171
    public function groupBy(string $groupBy): static
172
    {
173
        $this->groupBy = $groupBy;
15✔
174
        return $this;
15✔
175
    }
176

177
    public function lockForUpdate(): static
178
    {
179
        $this->lockForUpdate = true;
2✔
180
        return $this;
2✔
181
    }
182

183
    public function comment(string $comment): static
184
    {
185
        $this->comment = $comment;
381✔
186
        return $this;
381✔
187
    }
188

189
    public function onConflict(array $upsertConflictColumns = []): static
190
    {
191
        $this->upsertConflictColumns = $upsertConflictColumns;
8✔
192
        return $this;
8✔
193
    }
194

195
    private function validateParameters(mixed $where): void
196
    {
197
        if (is_array($where)) {
452✔
198
            foreach ($where as $key => $value) {
338✔
199
                if (is_object($value) && !($value instanceof Restriction)) {
338✔
200
                    throw new DbException("Cannot bind object as a parameter for `{$key}`.");
3✔
201
                }
202
            }
203
        }
204
    }
205
}
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