Skip to content

Commit 009a75d

Browse files
committed
Testing phase
Methods and functions complete but failed testing in phpunit so needing to fix
1 parent a283b6d commit 009a75d

3 files changed

Lines changed: 51 additions & 13 deletions

File tree

lib/ezFunctions.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,19 @@ function replace($table = '', $keyValue)
494494
: false;
495495
}
496496

497+
function flattenWhereConditions($whereConditions)
498+
{
499+
$whereConditionsReturn = [];
500+
foreach ($whereConditions as $whereCondition) {
501+
if (!empty($whereCondition[0]) && is_array($whereCondition[0])) {
502+
$whereConditionsReturn = array_merge($whereConditionsReturn, flattenWhereConditions($whereCondition));
503+
} else {
504+
$whereConditionsReturn[] = $whereCondition;
505+
}
506+
}
507+
return $whereConditionsReturn;
508+
}
509+
497510
function ezFunctions()
498511
{
499512
return true;

lib/ezQuery.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ private function conditionIs($key, $condition, $combine)
393393

394394
private function retrieveConditions($whereConditions)
395395
{
396-
$whereClause = [];
396+
$whereConditions = flattenWhereConditions($whereConditions);
397+
$whereKey = [];
398+
$whereValue = [];
397399
$operator = [];
398400
$extra = [];
399401
$combiner = [];
@@ -402,23 +404,27 @@ private function retrieveConditions($whereConditions)
402404
$operator[] = (isset($checkFields[1])) ? $checkFields[1] : '';
403405
if (empty($checkFields[1])) {
404406
$this->clearPrepare();
405-
return [[], [], [], [], []];
407+
return [[], [], [], [], [], []];
406408
}
407409

408410
if (\strtoupper($checkFields[1]) == 'IN') {
409-
$whereClause[$checkFields[0]] = \array_slice((array) $checkFields, 2);
411+
$whereKey[] = $checkFields[0];
412+
$whereValue[] = \array_slice((array) $checkFields, 2);
410413
$combiner[] = \_AND;
411414
$group[] = null;
412415
$extra[] = null;
413416
} else {
414-
$whereClause[(isset($checkFields[0])) ? $checkFields[0] : '1'] = (isset($checkFields[2])) ? $checkFields[2] : '';
415-
$combiner[] = (isset($checkFields[3])) ? $checkFields[3] : \_AND;
416-
$group[] = (isset($checkFields[4])) ? $checkFields[4] : null;
417-
$extra[] = (isset($checkFields[5])) ? $checkFields[5] : null;
417+
if (!empty($checkFields[0])) {
418+
$whereKey[] = $checkFields[0];
419+
$whereValue[] = (isset($checkFields[2])) ? $checkFields[2] : '';
420+
$combiner[] = (isset($checkFields[3])) ? $checkFields[3] : \_AND;
421+
$group[] = (isset($checkFields[4])) ? $checkFields[4] : null;
422+
$extra[] = (isset($checkFields[5])) ? $checkFields[5] : null;
423+
}
418424
}
419425
}
420426

421-
return [$operator, $whereClause, $combiner, $extra, $group];
427+
return [$operator, $whereKey, $whereValue, $combiner, $extra, $group];
422428
}
423429

424430
private function processConditions($column, $condition, $value, $valueOrCombine, $extraCombine, $whereGroup)
@@ -465,7 +471,6 @@ public function whereGroup(...$whereConditions)
465471

466472
public function where(...$whereConditions)
467473
{
468-
$whereConditions = (!empty($whereConditions[0]) && is_array($whereConditions[0])) ? $whereConditions[0] : $whereConditions;
469474

470475
if (empty($whereConditions))
471476
return false;
@@ -478,22 +483,22 @@ public function where(...$whereConditions)
478483
if (\is_string($whereConditions[0]) && \strpos($whereConditions[0], $whereOrHaving) !== false)
479484
return $whereConditions[0];
480485

481-
list($operator, $whereClause, $combiner, $extra, $group) = $this->retrieveConditions($whereConditions);
486+
list($operator, $whereKeys, $whereValues, $combiner, $extra, $group) = $this->retrieveConditions($whereConditions);
482487
if (empty($operator))
483488
return false;
484489

485490
$where = '1';
486-
if (!isset($whereClause['1'])) {
491+
if (!empty($whereKeys)) {
487492
$this->whereSQL = '';
488493
$i = 0;
489-
foreach ($whereClause as $key => $val) {
494+
foreach ($whereKeys as $key) {
490495
$isCondition = \strtoupper($operator[$i]);
491496
$combine = $combiner[$i];
492497
$this->combineWith = \_AND;
493498
if (\in_array(\strtoupper($combine), \_COMBINERS) || isset($extra[$i]))
494499
$this->combineWith = isset($extra[$i]) ? $combine : \strtoupper($combine);
495500

496-
if ($this->processConditions($key, $isCondition, $val, $this->combineWith, $extra[$i], $group[$i]) === false)
501+
if ($this->processConditions($key, $isCondition, $whereValues[$i], $this->combineWith, $extra[$i], $group[$i]) === false)
497502
return false;
498503

499504
$i++;

tests/pdo/pdo_mysqlTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,26 @@ public function testSelecting()
265265
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
266266
}
267267

268+
public function testWhereGroup()
269+
{
270+
$this->assertTrue($this->object->connect('mysql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD));
271+
$this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), active tinyint(1), PRIMARY KEY (ID))');
272+
$this->object->insert('unit_test', array('id' => '1', 'test_key' => 'testing 1', 'active' => 1));
273+
$this->object->insert('unit_test', array('id' => '2', 'test_key' => 'testing 2', 'active' => 0));
274+
$this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1));
275+
$this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1));
276+
277+
$result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%', _OR))));
278+
$i = 1;
279+
foreach ($result as $row) {
280+
$this->assertEquals($i, $row->id);
281+
$this->assertEquals('testing ' . $i, $row->test_key);
282+
$i = $i + 2;
283+
}
284+
285+
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
286+
}
287+
268288
public function testJoins()
269289
{
270290
$this->assertTrue($this->object->connect('mysql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD));

0 commit comments

Comments
 (0)