Skip to content

Commit 0460ce2

Browse files
committed
updates, refactor where clause, reduce NPATH complexity for code coverage
1 parent 30dde6b commit 0460ce2

File tree

5 files changed

+86
-48
lines changed

5 files changed

+86
-48
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ prepareOff(); // When off shortcut SQL Methods calls will use vendors escape rou
125125
// the last arguments of _AND, _OR, _NOT, _andNOT will combine expressions
126126
eq('key/Field/Column', $value, _AND), // combine next expression
127127
neq('key/Field/Column', $value, _OR), // will combine next expression again
128-
ne('key/Field/Column', $value)
128+
ne('key/Field/Column', $value), // the default is _AND so will combine next expression
129129
lt('key/Field/Column', $value)
130130
lte('key/Field/Column', $value)
131131
gt('key/Field/Column', $value)
@@ -164,7 +164,7 @@ $db->insert('profile', $values);
164164
$db->insert('profile', ['name' => 'john john', 'email' => 'john@email', 'phone' => 123456]);
165165

166166
// returns result set given the table name, column fields, and ...conditions
167-
$result = $db->selecting('profile', 'phone', eq('email', $email, _AND), neq('id', 1));
167+
$result = $db->selecting('profile', 'phone', eq('email', $email), between('id', 1, $values));
168168

169169
foreach ($result as $row) {
170170
echo $row->phone;

lib/ezFunctions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ function notIn($x, $y, $and = null, ...$args)
261261
function between($x, $y, $y2, ...$args)
262262
{
263263
$expression = array();
264-
\array_push($expression, $x, \_BETWEEN,$y, $y2, ...$args);
264+
\array_push($expression, $x, \_BETWEEN,$y, $y2, \_AND, ...$args);
265265
return $expression;
266266
}
267267

lib/ezQuery.php

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class ezQuery implements ezQueryInterface
1515
private $fromTable = null;
1616
private $isWhere = true;
1717
private $isInto = false;
18+
private $whereSQL = null;
19+
private $combineWith = null;
1820

1921
public function __construct()
2022
{
@@ -292,6 +294,52 @@ public function limit($numberOf, $offset = null)
292294
return 'LIMIT '.$rows.$value;
293295
}
294296

297+
private function conditions($key, $condition, $value, $combine)
298+
{
299+
if ($this->isPrepareOn()) {
300+
$this->whereSQL .= "$key $condition ".\_TAG." $combine ";
301+
$this->addPrepare($value);
302+
} else
303+
$this->whereSQL .= "$key $condition '".$this->escape($value)."' $combine ";
304+
}
305+
306+
private function conditionBetween($key, $condition, $valueOne, $valueTwo, $combine)
307+
{
308+
$_valueTwo = $this->escape($valueTwo);
309+
$andCombineWith = \_AND;
310+
if (\in_array(\strtoupper($combine), \_COMBINERS))
311+
$andCombineWith = \strtoupper($combine);
312+
313+
if ($this->isPrepareOn()) {
314+
$this->whereSQL .= "$key ".$condition.' '.\_TAG." AND ".\_TAG." $andCombineWith ";
315+
$this->addPrepare($valueOne);
316+
$this->addPrepare($valueTwo);
317+
} else
318+
$this->whereSQL .= "$key $condition '".$this->escape($valueOne)."' AND '".$_valueTwo."' $andCombineWith ";
319+
320+
$this->combineWith = $andCombineWith;
321+
}
322+
323+
private function conditionIn($key, $condition, $valueRow, $combine)
324+
{
325+
$value = '';
326+
foreach ($valueRow as $splitValue) {
327+
if ($this->isPrepareOn()) {
328+
$value .= \_TAG.', ';
329+
$this->addPrepare($splitValue);
330+
} else
331+
$value .= "'".$this->escape($splitValue)."', ";
332+
}
333+
334+
$this->whereSQL .= "$key $condition ( ".\rtrim($value, ', ')." ) $combine ";
335+
}
336+
337+
private function conditionIs($key, $condition, $combine)
338+
{
339+
$isCondition = (($condition == 'IS') || ($condition == 'IS NOT')) ? $condition : 'IS';
340+
$this->whereSQL .= "$key $isCondition NULL $combine ";
341+
}
342+
295343
public function where( ...$whereConditions)
296344
{
297345
if (empty($whereConditions))
@@ -304,7 +352,7 @@ public function where( ...$whereConditions)
304352
$operator = [];
305353
$extra = [];
306354
$combiner = [];
307-
$combineWith = '';
355+
$this->combineWith = '';
308356

309357
$storeWhereConditions = $whereConditions;
310358
if (\is_string($whereConditions[0])) {
@@ -336,63 +384,41 @@ public function where( ...$whereConditions)
336384

337385
$where = '1';
338386
if (! isset($whereClause['1'])) {
339-
$where = '';
387+
$this->whereSQL = '';
340388
$i = 0;
341389
foreach($whereClause as $key => $val) {
342390
$isCondition = \strtoupper($operator[$i]);
343391
$combine = $combiner[$i];
344-
$combineWith = \_AND;
345-
if ( \in_array(\strtoupper($combine), \_COMBINERS) || isset($extra[$i]))
346-
$combineWith = (isset($extra[$i])) ? $combine : \strtoupper($combine);
392+
$this->combineWith = \_AND;
393+
if ( \in_array(\strtoupper($combine), \_COMBINERS) || isset($extra[$i]))
394+
$this->combineWith = isset($extra[$i]) ? $combine : \strtoupper($combine);
347395

348396
if (! \in_array( $isCondition, \_BOOLEAN_OPERATORS))
349397
return $this->clearPrepare();
350398

351399
if (($isCondition == \_BETWEEN) || ($isCondition == \_notBETWEEN)) {
352-
$value = $this->escape($combineWith);
353-
$myCombineWith = \_AND;
354-
if (\in_array(\strtoupper($extra[$i]), \_COMBINERS))
355-
$myCombineWith = \strtoupper($extra[$i]);
356-
357-
if ($this->isPrepareOn()) {
358-
$where .= "$key ".$isCondition.' '.\_TAG." AND ".\_TAG." $myCombineWith ";
359-
$this->addPrepare($val);
360-
$this->addPrepare($combineWith);
361-
} else
362-
$where .= "$key ".$isCondition." '".$this->escape($val)."' AND '".$value."' $myCombineWith ";
363-
364-
$combineWith = $myCombineWith;
400+
$this->conditionBetween($key, $isCondition, $val, $this->combineWith, $extra[$i]);
365401
} elseif ($isCondition == \_IN) {
366-
$value = '';
367-
foreach ($val as $inValues) {
368-
if ($this->isPrepareOn()) {
369-
$value .= \_TAG.', ';
370-
$this->addPrepare($inValues);
371-
} else
372-
$value .= "'".$this->escape($inValues)."', ";
373-
}
374-
$where .= "$key ".$isCondition." ( ".\rtrim($value, ', ')." ) $combineWith ";
402+
$this->conditionIn($key, $isCondition, $val, $this->combineWith);
375403
} elseif (((\strtolower($val) == 'null') || ($isCondition == 'IS') || ($isCondition == 'IS NOT'))) {
376-
$isCondition = (($isCondition == 'IS') || ($isCondition == 'IS NOT')) ? $isCondition : 'IS';
377-
$where .= "$key ".$isCondition." NULL $combineWith ";
404+
$this->conditionIs($key, $isCondition, $this->combineWith);
378405
} elseif ((($isCondition == \_LIKE) || ($isCondition == \_notLIKE)) && ! \preg_match('/[_%?]/', $val)) {
379406
return $this->clearPrepare();
380407
} else {
381-
if ($this->isPrepareOn()) {
382-
$where .= "$key ".$isCondition.' '.\_TAG." $combineWith ";
383-
$this->addPrepare($val);
384-
} else
385-
$where .= "$key ".$isCondition." '".$this->escape($val)."' $combineWith ";
386-
}
408+
$this->conditions($key, $isCondition, $val, $this->combineWith);
409+
}
410+
387411
$i++;
388412
}
389-
$where = \rtrim($where, " $combineWith ");
413+
414+
$where = \rtrim($this->whereSQL, " $this->combineWith ");
415+
$this->whereSQL = null;
390416
}
391417

392418
if (($this->isPrepareOn()) && !empty($this->prepareValues()) && ($where != '1'))
393-
return " $whereOrHaving ".$where.' ';
419+
return " $whereOrHaving $where ";
394420

395-
return ($where != '1') ? " $whereOrHaving ".$where.' ' : ' ' ;
421+
return ($where != '1') ? " $whereOrHaving $where " : ' ' ;
396422
}
397423

398424
public function selecting(string $table = null, $columnFields = '*', ...$conditions)

tests/ezQueryTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function testHaving()
5454

5555
/**
5656
* @covers ezsql\ezQuery::where
57+
* @covers ezsql\ezQuery::conditionIs
58+
* @covers ezsql\ezQuery::conditionBetween
59+
* @covers ezsql\ezQuery::conditions
60+
* @covers ezsql\ezQuery::conditionIn
5761
* @covers ezsql\ezQuery::isPrepareOn
5862
*/
5963
public function testWhere()
@@ -71,7 +75,7 @@ public function testWhere()
7175
$this->assertContains(')', $expect);
7276

7377
$this->assertContains('AND', $this->object->where(
74-
array('where_test', '=', 'testing 1', 'bad'),
78+
array('where_test', '=', 'testing 1'),
7579
array('test_like', _LIKE, '_good'))
7680
);
7781

@@ -83,6 +87,10 @@ public function testWhere()
8387
/**
8488
* @covers ezsql\ezQuery::prepareOn
8589
* @covers ezsql\ezQuery::where
90+
* @covers ezsql\ezQuery::conditionIs
91+
* @covers ezsql\ezQuery::conditionBetween
92+
* @covers ezsql\ezQuery::conditions
93+
* @covers ezsql\ezQuery::conditionIn
8694
*/
8795
public function testPrepareOn()
8896
{
@@ -116,7 +124,7 @@ public function testAddPrepare()
116124
{
117125
$this->object->prepareOn();
118126
$expect = $this->object->where(
119-
eq('where_test', 'testing 1', _AND),
127+
eq('where_test', 'testing 1'),
120128
neq('some_key', 'other', _OR),
121129
like('other_key', '%any')
122130
);

tests/mysqli/mysqliTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,21 +470,25 @@ public function testInsert_select()
470470

471471
/**
472472
* @covers ezsql\ezQuery::where
473+
* @covers ezsql\ezQuery::conditionIs
474+
* @covers ezsql\ezQuery::conditionBetween
475+
* @covers ezsql\ezQuery::conditions
476+
* @covers ezsql\ezQuery::conditionIn
473477
* @covers \where
474478
*/
475479
public function testWhere()
476480
{
477481
$this->object->prepareOff();
478482
$expect = where(
479-
between('where_test','testing 1','testing 2','bad'),
480-
like('test_null','null')
483+
between('where_test', 'testing 1', 'testing 2'),
484+
like('test_null', 'null')
481485
);
482-
486+
483487
$this->assertContains('WHERE where_test BETWEEN \'testing 1\' AND \'testing 2\' AND test_null IS NULL', $expect);
484488

485489
$this->assertFalse(where(
486-
array('where_test','bad','testing 1','or'),
487-
array('test_null','like','null')
490+
array('where_test', 'bad', 'testing 1', 'or'),
491+
array('test_null', 'like', 'null')
488492
));
489493

490494
$this->object->prepareOn();

0 commit comments

Comments
 (0)