forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasWhereQueryBuilderMethods.php
More file actions
171 lines (140 loc) · 5.36 KB
/
HasWhereQueryBuilderMethods.php
File metadata and controls
171 lines (140 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace Tempest\Database\Builder\QueryBuilders;
use Closure;
use Tempest\Database\Builder\WhereOperator;
use Tempest\Database\QueryStatements\WhereExistsStatement;
use Tempest\Database\QueryStatements\WhereGroupStatement;
use Tempest\Database\QueryStatements\WhereStatement;
use function Tempest\Support\str;
trait HasWhereQueryBuilderMethods
{
use HasConvenientWhereMethods;
use HasWhereRelationMethods;
protected function appendWhere(WhereStatement|WhereGroupStatement|WhereExistsStatement $where): void
{
$this->wheres->offsetSet(null, $where);
}
/**
* Adds a SQL `WHERE` condition to the query. If the `$statement` looks like raw SQL, the method will assume it is and call `whereRaw`. Otherwise, `whereField` will be called.
*
* **Example**
* ```php
* ->where('price > ?', $value); // calls `whereRaw`
* ->where('price', $value); // calls `whereField`
* ```
*/
public function where(string $statement, mixed ...$bindings): self
{
if ($this->looksLikeWhereRawStatement($statement, $bindings)) {
return $this->whereRaw($statement, ...$bindings);
}
return $this->whereField($statement, value: $bindings[0], operator: $bindings[1] ?? WhereOperator::EQUALS);
}
/**
* Adds a where condition to the query.
*/
public function whereField(string $field, mixed $value, string|WhereOperator $operator = WhereOperator::EQUALS): self
{
$operator = WhereOperator::fromOperator($operator);
$fieldDefinition = $this->model->getFieldDefinition($field);
$condition = $this->buildCondition((string) $fieldDefinition, $operator, $value);
if ($this->wheres->isNotEmpty()) {
return $this->andWhere($field, $value, $operator);
}
$this->appendWhere(new WhereStatement($condition['sql']));
$this->bind(...$condition['bindings']);
return $this;
}
/**
* Adds an `AND WHERE` condition to the query.
*/
public function andWhere(string $field, mixed $value, WhereOperator $operator = WhereOperator::EQUALS): self
{
$operator = WhereOperator::fromOperator($operator);
$fieldDefinition = $this->model->getFieldDefinition($field);
$condition = $this->buildCondition((string) $fieldDefinition, $operator, $value);
$this->appendWhere(new WhereStatement("AND {$condition['sql']}"));
$this->bind(...$condition['bindings']);
return $this;
}
/**
* Adds an `OR WHERE` condition to the query.
*/
public function orWhere(string $field, mixed $value, WhereOperator $operator = WhereOperator::EQUALS): self
{
$operator = WhereOperator::fromOperator($operator);
$fieldDefinition = $this->model->getFieldDefinition($field);
$condition = $this->buildCondition((string) $fieldDefinition, $operator, $value);
$this->appendWhere(new WhereStatement("OR {$condition['sql']}"));
$this->bind(...$condition['bindings']);
return $this;
}
/**
* Adds a raw SQL `WHERE` condition to the query.
*/
public function whereRaw(string $statement, mixed ...$bindings): self
{
if ($this->wheres->isNotEmpty() && ! str($statement)->trim()->startsWith(['AND', 'OR'])) {
return $this->andWhereRaw($statement, ...$bindings);
}
$this->appendWhere(new WhereStatement($statement));
$this->bind(...$bindings);
return $this;
}
/**
* Adds a raw SQL `AND WHERE` condition to the query.
*/
public function andWhereRaw(string $rawCondition, mixed ...$bindings): self
{
$this->appendWhere(new WhereStatement("AND {$rawCondition}"));
$this->bind(...$bindings);
return $this;
}
/**
* Adds a raw SQL `OR WHERE` condition to the query.
*/
public function orWhereRaw(string $rawCondition, mixed ...$bindings): self
{
$this->appendWhere(new WhereStatement("OR {$rawCondition}"));
$this->bind(...$bindings);
return $this;
}
/**
* Adds a grouped where statement. The callback accepts a builder, which may be used to add more nested `WHERE` statements.
*
*/
public function whereGroup(Closure $callback): self
{
/** @var WhereGroupBuilder<TModel> $groupBuilder */
$groupBuilder = new WhereGroupBuilder($this->model);
$callback($groupBuilder);
$group = $groupBuilder->build();
if (! $group->conditions->isEmpty()) {
$this->appendWhere($group);
$this->bind(...$groupBuilder->getBindings());
}
return $this;
}
/**
* Adds a grouped `AND WHERE` statement. The callback accepts a builder, which may be used to add more nested `WHERE` statements.
*
*/
public function andWhereGroup(Closure $callback): self
{
if ($this->wheres->isNotEmpty()) {
$this->appendWhere(new WhereStatement('AND'));
}
return $this->whereGroup($callback);
}
/**
* Adds a grouped `OR WHERE` statement. The callback accepts a builder, which may be used to add more nested `WHERE` statements.
*
*/
public function orWhereGroup(Closure $callback): self
{
if ($this->wheres->isNotEmpty()) {
$this->appendWhere(new WhereStatement('OR'));
}
return $this->whereGroup($callback);
}
}