Skip to content

Commit a283b6d

Browse files
committed
Initial function creation
Created the initial function but came across an issue where the $whereClause doesn't work in the $where method when using an OR combiner, so need to fix that first
1 parent 2f87447 commit a283b6d

File tree

3 files changed

+110
-41
lines changed

3 files changed

+110
-41
lines changed

lib/ezFunctions.php

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ function createCertificate(
113113
*
114114
* @param strings $y, - The right expression.
115115
* @param strings $and, - combine additional expressions with, 'AND','OR', 'NOT', 'AND NOT'.
116+
* @param strings $group, - notes beginning or end of where group, '(',')'.
116117
* @param strings $args - for any extras
117118
*
118-
* function comparison($x, $operator, $y, $and=null, ...$args)
119+
* function comparison($x, $operator, $y, $and=null, $group=null, ...$args)
119120
* {
120-
* return array($x, $operator, $y, $and, ...$args);
121+
* return array($x, $operator, $y, $and, $group, ...$args);
121122
* }
122123
*
123124
* @return array
@@ -126,110 +127,110 @@ function createCertificate(
126127
/**
127128
* Creates an equality comparison expression with the given arguments.
128129
*/
129-
function eq($x, $y, $and = null, ...$args)
130+
function eq($x, $y, $and = null, $group = null, ...$args)
130131
{
131132
$expression = array();
132-
\array_push($expression, $x, \EQ, $y, $and, ...$args);
133+
\array_push($expression, $x, \EQ, $y, $and, $group, ...$args);
133134
return $expression;
134135
}
135136

136137
/**
137138
* Creates a non equality comparison expression with the given arguments.
138139
*/
139-
function neq($x, $y, $and = null, ...$args)
140+
function neq($x, $y, $and = null, $group = null, ...$args)
140141
{
141142
$expression = array();
142-
\array_push($expression, $x, \NEQ, $y, $and, ...$args);
143+
\array_push($expression, $x, \NEQ, $y, $and, $group, ...$args);
143144
return $expression;
144145
}
145146

146147
/**
147148
* Creates the other non equality comparison expression with the given arguments.
148149
*/
149-
function ne($x, $y, $and = null, ...$args)
150+
function ne($x, $y, $and = null, $group = null, ...$args)
150151
{
151152
$expression = array();
152-
\array_push($expression, $x, \NE, $y, $and, ...$args);
153+
\array_push($expression, $x, \NE, $y, $and, $group, ...$args);
153154
return $expression;
154155
}
155156

156157
/**
157158
* Creates a lower-than comparison expression with the given arguments.
158159
*/
159-
function lt($x, $y, $and = null, ...$args)
160+
function lt($x, $y, $and = null, $group = null, ...$args)
160161
{
161162
$expression = array();
162-
\array_push($expression, $x, \LT, $y, $and, ...$args);
163+
\array_push($expression, $x, \LT, $y, $and, $group, ...$args);
163164
return $expression;
164165
}
165166

166167
/**
167168
* Creates a lower-than-equal comparison expression with the given arguments.
168169
*/
169-
function lte($x, $y, $and = null, ...$args)
170+
function lte($x, $y, $and = null, $group = null, ...$args)
170171
{
171172
$expression = array();
172-
\array_push($expression, $x, \LTE, $y, $and, ...$args);
173+
\array_push($expression, $x, \LTE, $y, $and, $group, ...$args);
173174
return $expression;
174175
}
175176

176177
/**
177178
* Creates a greater-than comparison expression with the given arguments.
178179
*/
179-
function gt($x, $y, $and = null, ...$args)
180+
function gt($x, $y, $and = null, $group = null, ...$args)
180181
{
181182
$expression = array();
182-
\array_push($expression, $x, \GT, $y, $and, ...$args);
183+
\array_push($expression, $x, \GT, $y, $and, $group, ...$args);
183184
return $expression;
184185
}
185186

186187
/**
187188
* Creates a greater-than-equal comparison expression with the given arguments.
188189
*/
189-
function gte($x, $y, $and = null, ...$args)
190+
function gte($x, $y, $and = null, $group = null, ...$args)
190191
{
191192
$expression = array();
192-
\array_push($expression, $x, \GTE, $y, $and, ...$args);
193+
\array_push($expression, $x, \GTE, $y, $and, $group, ...$args);
193194
return $expression;
194195
}
195196

196197
/**
197198
* Creates an IS NULL expression with the given arguments.
198199
*/
199-
function isNull($x, $y = 'null', $and = null, ...$args)
200+
function isNull($x, $y = 'null', $and = null, $group = null, ...$args)
200201
{
201202
$expression = array();
202-
\array_push($expression, $x, \_isNULL, $y, $and, ...$args);
203+
\array_push($expression, $x, \_isNULL, $y, $and, $group, ...$args);
203204
return $expression;
204205
}
205206

206207
/**
207208
* Creates an IS NOT NULL expression with the given arguments.
208209
*/
209-
function isNotNull($x, $y = 'null', $and = null, ...$args)
210+
function isNotNull($x, $y = 'null', $and = null, $group = null, ...$args)
210211
{
211212
$expression = array();
212-
\array_push($expression, $x, \_notNULL, $y, $and, ...$args);
213+
\array_push($expression, $x, \_notNULL, $y, $and, $group, ...$args);
213214
return $expression;
214215
}
215216

216217
/**
217218
* Creates a LIKE() comparison expression with the given arguments.
218219
*/
219-
function like($x, $y, $and = null, ...$args)
220+
function like($x, $y, $and = null, $group = null, ...$args)
220221
{
221222
$expression = array();
222-
\array_push($expression, $x, \_LIKE, $y, $and, ...$args);
223+
\array_push($expression, $x, \_LIKE, $y, $and, $group, ...$args);
223224
return $expression;
224225
}
225226

226227
/**
227228
* Creates a NOT LIKE() comparison expression with the given arguments.
228229
*/
229-
function notLike($x, $y, $and = null, ...$args)
230+
function notLike($x, $y, $and = null, $group = null, ...$args)
230231
{
231232
$expression = array();
232-
\array_push($expression, $x, \_notLIKE, $y, $and, ...$args);
233+
\array_push($expression, $x, \_notLIKE, $y, $and, $group, ...$args);
233234
return $expression;
234235
}
235236

@@ -349,6 +350,14 @@ function where(...$args)
349350
: false;
350351
}
351352

353+
function whereGroup(...$args)
354+
{
355+
$ezQuery = \getInstance();
356+
return ($ezQuery instanceof DatabaseInterface)
357+
? $ezQuery->whereGroup(...$args)
358+
: false;
359+
}
360+
352361
function groupBy($groupBy)
353362
{
354363
$ezQuery = \getInstance();
@@ -374,7 +383,7 @@ function innerJoin(
374383
$condition = \EQ
375384
) {
376385
$ezQuery = \getInstance();
377-
return ($ezQuery instanceOf DatabaseInterface)
386+
return ($ezQuery instanceof DatabaseInterface)
378387
? $ezQuery->innerJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
379388
: false;
380389
}
@@ -388,7 +397,7 @@ function leftJoin(
388397
$condition = \EQ
389398
) {
390399
$ezQuery = \getInstance();
391-
return ($ezQuery instanceOf DatabaseInterface)
400+
return ($ezQuery instanceof DatabaseInterface)
392401
? $ezQuery->leftJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
393402
: false;
394403
}
@@ -402,7 +411,7 @@ function rightJoin(
402411
$condition = \EQ
403412
) {
404413
$ezQuery = \getInstance();
405-
return ($ezQuery instanceOf DatabaseInterface)
414+
return ($ezQuery instanceof DatabaseInterface)
406415
? $ezQuery->rightJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
407416
: false;
408417
}
@@ -416,7 +425,7 @@ function fullJoin(
416425
$condition = \EQ
417426
) {
418427
$ezQuery = \getInstance();
419-
return ($ezQuery instanceOf DatabaseInterface)
428+
return ($ezQuery instanceof DatabaseInterface)
420429
? $ezQuery->fullJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition)
421430
: false;
422431
}

lib/ezQuery.php

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class ezQuery implements ezQueryInterface
1919
private $combineWith = null;
2020

2121
public function __construct()
22-
{ }
22+
{
23+
}
2324

2425
public static function clean($string)
2526
{
@@ -282,7 +283,7 @@ public function fullJoin(
282283
* @param string $condition -
283284
*
284285
* @return bool|string JOIN sql statement, false for error
285-
*/
286+
*/
286287
private function joining(
287288
String $type = \_INNER,
288289
string $leftTable = null,
@@ -341,13 +342,16 @@ public function limit($numberOf, $offset = null)
341342
return 'LIMIT ' . $rows . $value;
342343
}
343344

344-
private function conditions($key, $condition, $value, $combine)
345+
private function conditions($key, $condition, $value, $combine, $group)
345346
{
347+
$groupStart = (!empty($group) && $group === '(') ? $group : '';
348+
$groupEnd = (!empty($group) && $group === ')') ? $group : '';
349+
346350
if ($this->isPrepareOn()) {
347-
$this->whereSQL .= "$key $condition " . \_TAG . " $combine ";
351+
$this->whereSQL .= "$groupStart $key $condition " . \_TAG . " $groupEnd $combine ";
348352
$this->addPrepare($value);
349353
} else
350-
$this->whereSQL .= "$key $condition '" . $this->escape($value) . "' $combine ";
354+
$this->whereSQL .= "$groupStart $key $condition '" . $this->escape($value) . "' $groupEnd $combine ";
351355
}
352356

353357
private function conditionBetween($key, $condition, $valueOne, $valueTwo, $combine)
@@ -393,28 +397,31 @@ private function retrieveConditions($whereConditions)
393397
$operator = [];
394398
$extra = [];
395399
$combiner = [];
400+
$group = [];
396401
foreach ($whereConditions as $checkFields) {
397402
$operator[] = (isset($checkFields[1])) ? $checkFields[1] : '';
398403
if (empty($checkFields[1])) {
399404
$this->clearPrepare();
400-
return [[], [], [], []];
405+
return [[], [], [], [], []];
401406
}
402407

403408
if (\strtoupper($checkFields[1]) == 'IN') {
404409
$whereClause[$checkFields[0]] = \array_slice((array) $checkFields, 2);
405410
$combiner[] = \_AND;
411+
$group[] = null;
406412
$extra[] = null;
407413
} else {
408414
$whereClause[(isset($checkFields[0])) ? $checkFields[0] : '1'] = (isset($checkFields[2])) ? $checkFields[2] : '';
409415
$combiner[] = (isset($checkFields[3])) ? $checkFields[3] : \_AND;
410-
$extra[] = (isset($checkFields[4])) ? $checkFields[4] : null;
416+
$group[] = (isset($checkFields[4])) ? $checkFields[4] : null;
417+
$extra[] = (isset($checkFields[5])) ? $checkFields[5] : null;
411418
}
412419
}
413420

414-
return [$operator, $whereClause, $combiner, $extra];
421+
return [$operator, $whereClause, $combiner, $extra, $group];
415422
}
416423

417-
private function processConditions($column, $condition, $value, $valueOrCombine, $extraCombine)
424+
private function processConditions($column, $condition, $value, $valueOrCombine, $extraCombine, $whereGroup)
418425
{
419426
if (!\in_array($condition, \_BOOLEAN_OPERATORS))
420427
return $this->clearPrepare();
@@ -428,12 +435,38 @@ private function processConditions($column, $condition, $value, $valueOrCombine,
428435
} elseif ((($condition == \_LIKE) || ($condition == \_notLIKE)) && !\preg_match('/[_%?]/', $value)) {
429436
return $this->clearPrepare();
430437
} else {
431-
$this->conditions($column, $condition, $value, $valueOrCombine);
438+
$this->conditions($column, $condition, $value, $valueOrCombine, $whereGroup);
432439
}
433440
}
434441

442+
public function whereGroup(...$whereConditions)
443+
{
444+
if (empty($whereConditions))
445+
return false;
446+
447+
$whereOrHaving = ($this->isWhere) ? 'WHERE' : 'HAVING';
448+
449+
if (\is_string($whereConditions[0]) && \strpos($whereConditions[0], $whereOrHaving) !== false)
450+
return $whereConditions[0];
451+
452+
$totalConditions = count($whereConditions) - 1;
453+
454+
if ($totalConditions > 0) {
455+
456+
if (empty($whereConditions[0][4]) || $whereConditions[0][4] !== '(')
457+
$whereConditions[0][4] = '(';
458+
459+
if (empty($whereConditions[$totalConditions][4]) || $whereConditions[$totalConditions][4] !== ')')
460+
$whereConditions[$totalConditions][4] = ')';
461+
}
462+
463+
return $whereConditions;
464+
}
465+
435466
public function where(...$whereConditions)
436467
{
468+
$whereConditions = (!empty($whereConditions[0]) && is_array($whereConditions[0])) ? $whereConditions[0] : $whereConditions;
469+
437470
if (empty($whereConditions))
438471
return false;
439472

@@ -445,7 +478,7 @@ public function where(...$whereConditions)
445478
if (\is_string($whereConditions[0]) && \strpos($whereConditions[0], $whereOrHaving) !== false)
446479
return $whereConditions[0];
447480

448-
list($operator, $whereClause, $combiner, $extra) = $this->retrieveConditions($whereConditions);
481+
list($operator, $whereClause, $combiner, $extra, $group) = $this->retrieveConditions($whereConditions);
449482
if (empty($operator))
450483
return false;
451484

@@ -460,7 +493,7 @@ public function where(...$whereConditions)
460493
if (\in_array(\strtoupper($combine), \_COMBINERS) || isset($extra[$i]))
461494
$this->combineWith = isset($extra[$i]) ? $combine : \strtoupper($combine);
462495

463-
if ($this->processConditions($key, $isCondition, $val, $this->combineWith, $extra[$i]) === false)
496+
if ($this->processConditions($key, $isCondition, $val, $this->combineWith, $extra[$i], $group[$i]) === false)
464497
return false;
465498

466499
$i++;

lib/ezQueryInterface.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,33 @@ public function orderBy($orderBy, $order);
294294
*/
295295
public function limit($numberOf, $offset = null);
296296

297+
/**
298+
* Helper adds WHERE grouping to the conditions
299+
*
300+
* format:
301+
* `whereGroup( comparison(x, y, and) )`
302+
*
303+
* example:
304+
* `whereGroup( eq(key, value, combiner ), eq(key, value, combiner ) );`
305+
*
306+
* @param array $whereConditions - In the following format:
307+
*
308+
* eq('key/Field/Column', $value, _AND), // combine next expression
309+
* neq('key/Field/Column', $value, _OR), // will combine next expression again
310+
* ne('key/Field/Column', $value), // the default is _AND so will combine next expression
311+
* lt('key/Field/Column', $value)
312+
* lte('key/Field/Column', $value)
313+
* gt('key/Field/Column', $value)
314+
* gte('key/Field/Column', $value)
315+
* isNull('key/Field/Column')
316+
* isNotNull('key/Field/Column')
317+
* like('key/Field/Column', '_%')
318+
* notLike('key/Field/Column', '_%')
319+
*
320+
* @return mixed bool/string - WHERE SQL statement, or false on error
321+
*/
322+
public function whereGroup(...$whereConditions);
323+
297324
/**
298325
* Helper returns an WHERE sql clause string.
299326
*
@@ -321,7 +348,7 @@ public function limit($numberOf, $offset = null);
321348
* between('key/Field/Column', $value, $value2)
322349
* notBetween('key/Field/Column', $value, $value2)
323350
*
324-
* @return mixed bool/string - WHERE SQL statement, or false on error
351+
* @return array modified conditions
325352
*/
326353
public function where(...$whereConditions);
327354

0 commit comments

Comments
 (0)