1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\Crudites\Grammar\Query; |
4
|
|
|
|
5
|
|
|
use HexMakina\Crudites\Grammar\Predicate\{Predicate, IsNotEmpty, IsEmpty, WithValue, WithValues}; |
|
|
|
|
6
|
|
|
trait ClauseWhere |
7
|
|
|
{ |
8
|
|
|
public static $OP_AND = 'AND'; |
9
|
|
|
|
10
|
|
|
public static $OP_OR = 'OR'; |
11
|
|
|
|
12
|
|
|
public static $WHERE_LIKE_PRE = '%TERM'; |
13
|
|
|
|
14
|
|
|
public static $WHERE_LIKE_POST = 'TERM%'; |
15
|
|
|
|
16
|
|
|
public static $WHERE_LIKE_BOTH = '%TERM%'; |
17
|
|
|
|
18
|
|
|
protected $where; |
19
|
|
|
|
20
|
|
|
abstract public function tableLabel($table_name = null); |
21
|
|
|
|
22
|
|
|
abstract public function backTick($field, $table_name = null); |
23
|
|
|
|
24
|
|
|
abstract public function addBinding($field, $value, $table_name, $bind_label = null): string; |
25
|
|
|
|
26
|
|
|
public function where($where_condition, $bindings = []) |
27
|
|
|
{ |
28
|
|
|
$this->where ??= []; |
29
|
|
|
$this->where[] = sprintf('(%s)', $where_condition); |
30
|
|
|
|
31
|
|
|
if(!empty($bindings)){ |
32
|
|
|
$this->bindings = array_merge($this->bindings, $bindings); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function wherePredicate(Predicate $predicate) |
39
|
|
|
{ |
40
|
|
|
return $this->where($predicate->__toString(), $predicate->getBindings()); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function whereWithBind($where) |
44
|
|
|
{ |
45
|
|
|
$this->where ??= []; |
46
|
|
|
$this->where[] = sprintf('(%s)', $where); |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
public function whereBindField($table_name, $field, $operator, $value, $bind_name = null) |
50
|
|
|
{ |
51
|
|
|
$bind_name = $this->addBinding($field, $value, $table_name, $bind_name); |
52
|
|
|
return $this->whereField($field, sprintf('%s %s', $operator, $bind_name), $table_name); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
public function whereEqualOrNull($field, $value, $table_name = null, $bindname = null) |
57
|
|
|
{ |
58
|
|
|
$bind_name = $this->addBinding($field, $value, $table_name, $bindname); |
59
|
|
|
$field_name = $this->backTick($field, $table_name); |
60
|
|
|
|
61
|
|
|
return $this->where(sprintf('%s = %s OR %s IS NULL', $field_name, $bind_name, $field_name)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function whereIn($field, $values, $table_name = null) |
65
|
|
|
{ |
66
|
|
|
return $this->wherePredicate(new WithValues([$this->tableLabel($table_name), $field], 'IN', $values, 'AWIN')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
public function whereField($field, $condition, $table_name = null) |
71
|
|
|
{ |
72
|
|
|
$table_field = $this->backTick($field, $table_name); |
73
|
|
|
return $this->where(sprintf('%s %s', $table_field, $condition)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function whereIsNull($field, $table_name = null) |
77
|
|
|
{ |
78
|
|
|
return $this->where(new Predicate($this->predicateColumn($field, $table_name), 'IS NULL')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function whereNotEmpty($field, $table_name = null) |
82
|
|
|
{ |
83
|
|
|
return $this->where(new IsNotEmpty($this->predicateColumn($field, $table_name))); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function whereEmpty($field, $table_name = null) |
87
|
|
|
{ |
88
|
|
|
return $this->where(new IsEmpty($this->predicateColumn($field, $table_name))); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function whereFieldsEQ($assoc_data, $table_name = null) |
92
|
|
|
{ |
93
|
|
|
$table_name = $this->tableLabel($table_name); |
94
|
|
|
foreach ($assoc_data as $field => $value) { |
95
|
|
|
$p = new WithValue([$table_name, $field], '=', $value); |
96
|
|
|
$this->wherePredicate($p); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function whereEQ($field, $value, $table_name = null, $bindname = null) |
103
|
|
|
{ |
104
|
|
|
$p = new WithValue($this->predicateColumn($field, $table_name), '=', $value, $bindname); |
105
|
|
|
return $this->wherePredicate($p); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function whereGT($field, $value, $table_name = null, $bindname = null) |
109
|
|
|
{ |
110
|
|
|
$p = new WithValue($this->predicateColumn($field, $table_name), '>', $value, $bindname); |
111
|
|
|
return $this->wherePredicate($p); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function whereLT($field, $value, $table_name = null, $bindname = null) |
115
|
|
|
{ |
116
|
|
|
$p = new WithValue($this->predicateColumn($field, $table_name), '<', $value, $bindname); |
117
|
|
|
return $this->wherePredicate($p); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function predicateColumn($column, $table=null): array |
121
|
|
|
{ |
122
|
|
|
return [$this->tableLabel($table), $column]; |
123
|
|
|
} |
124
|
|
|
public function whereGTE($field, $value, $table_name = null, $bindname = null) |
125
|
|
|
{ |
126
|
|
|
$p = new WithValue($this->predicateColumn($field, $table_name), '>=', $value, $bindname); |
127
|
|
|
return $this->wherePredicate($p); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function whereLTE($field, $value, $table_name = null, $bindname = null) |
131
|
|
|
{ |
132
|
|
|
$p = new WithValue($this->predicateColumn($field, $table_name), '<=', $value, $bindname); |
133
|
|
|
return $this->wherePredicate($p); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function whereNotEQ($field, $value, $table_name = null, $bindname = null) |
137
|
|
|
{ |
138
|
|
|
$p = new WithValue($this->predicateColumn($field, $table_name), '<>', $value, $bindname); |
139
|
|
|
return $this->wherePredicate($p); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function whereLike($field, $prep_value, $table_name = null, $bindname = null) |
143
|
|
|
{ |
144
|
|
|
$p = new WithValue($this->predicateColumn($field, $table_name), 'LIKE', $prep_value, $bindname); |
145
|
|
|
return $this->wherePredicate($p); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function whereNotLike($field, $prep_value, $table_name = null, $bindname = null) |
149
|
|
|
{ |
150
|
|
|
$p = new WithValue($this->predicateColumn($field, $table_name), 'NOT LIKE', $prep_value, $bindname); |
151
|
|
|
return $this->wherePredicate($p); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param array $filters_content with 2 indexes: 'term', the search string, 'fields', the search fields |
156
|
|
|
* @param $search_table String to filter |
157
|
|
|
* @param $filters_operator Object, inclusive or exclusive search |
158
|
|
|
*/ |
159
|
|
|
|
160
|
|
|
// sub array filters[$content] |
161
|
|
|
public function whereFilterContent(array $filters_content, $search_table = null, $filters_operator = null) |
162
|
|
|
{ |
163
|
|
|
if (!isset($filters_content['term'])) { |
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
if (!isset($filters_content['fields'])) { |
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
if ($search_table === null) { |
170
|
|
|
$search_table = $this->tableLabel(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$search_term = trim($filters_content['term']); |
174
|
|
|
if ($search_term === '') { |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$content_wc = []; |
179
|
|
|
foreach ($filters_content['fields'] as $search_field => $search_mode) { |
180
|
|
|
if (is_numeric($search_field)) { |
181
|
|
|
$search_field = $search_mode; |
182
|
|
|
$search_mode = self::$WHERE_LIKE_BOTH; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$search_field = $this->backTick($search_field, $search_table); |
186
|
|
|
|
187
|
|
|
if ($search_mode === self::$OP_EQ) { |
188
|
|
|
$content_wc[] = sprintf('%s = \'%s\' ', $search_field, $search_term); // TODO bindthis |
189
|
|
|
} else // %% |
190
|
|
|
{ |
191
|
|
|
$pattern = str_replace('TERM', $search_term, $search_mode); |
192
|
|
|
$content_wc[] = sprintf(' %s LIKE \'%s\' ', $search_field, $pattern); // TODO bindthis |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (!empty($content_wc)) { |
197
|
|
|
$operator = self::validWhereOperator($filters_operator, self::$OP_OR); |
198
|
|
|
$content_wc = implode(sprintf(' %s ', $operator), $content_wc); |
199
|
|
|
|
200
|
|
|
$this->where(sprintf(' (%s) ', $content_wc)); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// //------------------------------------------------------------ FIELDS |
205
|
|
|
protected static function validWhereOperator($operator, $default) |
206
|
|
|
{ |
207
|
|
|
$operator = strtoupper(sprintf('%s', $operator)); |
208
|
|
|
$choices = [self::$OP_AND, self::$OP_OR]; |
209
|
|
|
|
210
|
|
|
if (in_array($operator, $choices)) { |
211
|
|
|
return $operator; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (in_array($default, $choices)) { |
215
|
|
|
return $default; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
throw new \Exception('ERR_INVALID_QUERY_OPERATOR'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
protected function generateWhere() |
222
|
|
|
{ |
223
|
|
|
if (!empty($this->where)) { |
224
|
|
|
return PHP_EOL . ' WHERE ' . implode(PHP_EOL . ' AND ', $this->where); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths