From a283b6dcc2189507f631137848c68885cf928b14 Mon Sep 17 00:00:00 2001 From: Damien Date: Tue, 21 Apr 2020 15:37:10 +1200 Subject: [PATCH 01/10] 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 --- lib/ezFunctions.php | 65 +++++++++++++++++++++++----------------- lib/ezQuery.php | 57 +++++++++++++++++++++++++++-------- lib/ezQueryInterface.php | 29 +++++++++++++++++- 3 files changed, 110 insertions(+), 41 deletions(-) diff --git a/lib/ezFunctions.php b/lib/ezFunctions.php index 3c335de..03cfdcf 100644 --- a/lib/ezFunctions.php +++ b/lib/ezFunctions.php @@ -113,11 +113,12 @@ function createCertificate( * * @param strings $y, - The right expression. * @param strings $and, - combine additional expressions with, 'AND','OR', 'NOT', 'AND NOT'. + * @param strings $group, - notes beginning or end of where group, '(',')'. * @param strings $args - for any extras * - * function comparison($x, $operator, $y, $and=null, ...$args) + * function comparison($x, $operator, $y, $and=null, $group=null, ...$args) * { - * return array($x, $operator, $y, $and, ...$args); + * return array($x, $operator, $y, $and, $group, ...$args); * } * * @return array @@ -126,110 +127,110 @@ function createCertificate( /** * Creates an equality comparison expression with the given arguments. */ - function eq($x, $y, $and = null, ...$args) + function eq($x, $y, $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \EQ, $y, $and, ...$args); + \array_push($expression, $x, \EQ, $y, $and, $group, ...$args); return $expression; } /** * Creates a non equality comparison expression with the given arguments. */ - function neq($x, $y, $and = null, ...$args) + function neq($x, $y, $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \NEQ, $y, $and, ...$args); + \array_push($expression, $x, \NEQ, $y, $and, $group, ...$args); return $expression; } /** * Creates the other non equality comparison expression with the given arguments. */ - function ne($x, $y, $and = null, ...$args) + function ne($x, $y, $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \NE, $y, $and, ...$args); + \array_push($expression, $x, \NE, $y, $and, $group, ...$args); return $expression; } /** * Creates a lower-than comparison expression with the given arguments. */ - function lt($x, $y, $and = null, ...$args) + function lt($x, $y, $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \LT, $y, $and, ...$args); + \array_push($expression, $x, \LT, $y, $and, $group, ...$args); return $expression; } /** * Creates a lower-than-equal comparison expression with the given arguments. */ - function lte($x, $y, $and = null, ...$args) + function lte($x, $y, $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \LTE, $y, $and, ...$args); + \array_push($expression, $x, \LTE, $y, $and, $group, ...$args); return $expression; } /** * Creates a greater-than comparison expression with the given arguments. */ - function gt($x, $y, $and = null, ...$args) + function gt($x, $y, $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \GT, $y, $and, ...$args); + \array_push($expression, $x, \GT, $y, $and, $group, ...$args); return $expression; } /** * Creates a greater-than-equal comparison expression with the given arguments. */ - function gte($x, $y, $and = null, ...$args) + function gte($x, $y, $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \GTE, $y, $and, ...$args); + \array_push($expression, $x, \GTE, $y, $and, $group, ...$args); return $expression; } /** * Creates an IS NULL expression with the given arguments. */ - function isNull($x, $y = 'null', $and = null, ...$args) + function isNull($x, $y = 'null', $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \_isNULL, $y, $and, ...$args); + \array_push($expression, $x, \_isNULL, $y, $and, $group, ...$args); return $expression; } /** * Creates an IS NOT NULL expression with the given arguments. */ - function isNotNull($x, $y = 'null', $and = null, ...$args) + function isNotNull($x, $y = 'null', $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \_notNULL, $y, $and, ...$args); + \array_push($expression, $x, \_notNULL, $y, $and, $group, ...$args); return $expression; } /** * Creates a LIKE() comparison expression with the given arguments. */ - function like($x, $y, $and = null, ...$args) + function like($x, $y, $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \_LIKE, $y, $and, ...$args); + \array_push($expression, $x, \_LIKE, $y, $and, $group, ...$args); return $expression; } /** * Creates a NOT LIKE() comparison expression with the given arguments. */ - function notLike($x, $y, $and = null, ...$args) + function notLike($x, $y, $and = null, $group = null, ...$args) { $expression = array(); - \array_push($expression, $x, \_notLIKE, $y, $and, ...$args); + \array_push($expression, $x, \_notLIKE, $y, $and, $group, ...$args); return $expression; } @@ -349,6 +350,14 @@ function where(...$args) : false; } + function whereGroup(...$args) + { + $ezQuery = \getInstance(); + return ($ezQuery instanceof DatabaseInterface) + ? $ezQuery->whereGroup(...$args) + : false; + } + function groupBy($groupBy) { $ezQuery = \getInstance(); @@ -374,7 +383,7 @@ function innerJoin( $condition = \EQ ) { $ezQuery = \getInstance(); - return ($ezQuery instanceOf DatabaseInterface) + return ($ezQuery instanceof DatabaseInterface) ? $ezQuery->innerJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition) : false; } @@ -388,7 +397,7 @@ function leftJoin( $condition = \EQ ) { $ezQuery = \getInstance(); - return ($ezQuery instanceOf DatabaseInterface) + return ($ezQuery instanceof DatabaseInterface) ? $ezQuery->leftJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition) : false; } @@ -402,7 +411,7 @@ function rightJoin( $condition = \EQ ) { $ezQuery = \getInstance(); - return ($ezQuery instanceOf DatabaseInterface) + return ($ezQuery instanceof DatabaseInterface) ? $ezQuery->rightJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition) : false; } @@ -416,7 +425,7 @@ function fullJoin( $condition = \EQ ) { $ezQuery = \getInstance(); - return ($ezQuery instanceOf DatabaseInterface) + return ($ezQuery instanceof DatabaseInterface) ? $ezQuery->fullJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition) : false; } diff --git a/lib/ezQuery.php b/lib/ezQuery.php index d0a3661..8d705fe 100644 --- a/lib/ezQuery.php +++ b/lib/ezQuery.php @@ -19,7 +19,8 @@ class ezQuery implements ezQueryInterface private $combineWith = null; public function __construct() - { } + { + } public static function clean($string) { @@ -282,7 +283,7 @@ public function fullJoin( * @param string $condition - * * @return bool|string JOIN sql statement, false for error - */ + */ private function joining( String $type = \_INNER, string $leftTable = null, @@ -341,13 +342,16 @@ public function limit($numberOf, $offset = null) return 'LIMIT ' . $rows . $value; } - private function conditions($key, $condition, $value, $combine) + private function conditions($key, $condition, $value, $combine, $group) { + $groupStart = (!empty($group) && $group === '(') ? $group : ''; + $groupEnd = (!empty($group) && $group === ')') ? $group : ''; + if ($this->isPrepareOn()) { - $this->whereSQL .= "$key $condition " . \_TAG . " $combine "; + $this->whereSQL .= "$groupStart $key $condition " . \_TAG . " $groupEnd $combine "; $this->addPrepare($value); } else - $this->whereSQL .= "$key $condition '" . $this->escape($value) . "' $combine "; + $this->whereSQL .= "$groupStart $key $condition '" . $this->escape($value) . "' $groupEnd $combine "; } private function conditionBetween($key, $condition, $valueOne, $valueTwo, $combine) @@ -393,28 +397,31 @@ private function retrieveConditions($whereConditions) $operator = []; $extra = []; $combiner = []; + $group = []; foreach ($whereConditions as $checkFields) { $operator[] = (isset($checkFields[1])) ? $checkFields[1] : ''; if (empty($checkFields[1])) { $this->clearPrepare(); - return [[], [], [], []]; + return [[], [], [], [], []]; } if (\strtoupper($checkFields[1]) == 'IN') { $whereClause[$checkFields[0]] = \array_slice((array) $checkFields, 2); $combiner[] = \_AND; + $group[] = null; $extra[] = null; } else { $whereClause[(isset($checkFields[0])) ? $checkFields[0] : '1'] = (isset($checkFields[2])) ? $checkFields[2] : ''; $combiner[] = (isset($checkFields[3])) ? $checkFields[3] : \_AND; - $extra[] = (isset($checkFields[4])) ? $checkFields[4] : null; + $group[] = (isset($checkFields[4])) ? $checkFields[4] : null; + $extra[] = (isset($checkFields[5])) ? $checkFields[5] : null; } } - return [$operator, $whereClause, $combiner, $extra]; + return [$operator, $whereClause, $combiner, $extra, $group]; } - private function processConditions($column, $condition, $value, $valueOrCombine, $extraCombine) + private function processConditions($column, $condition, $value, $valueOrCombine, $extraCombine, $whereGroup) { if (!\in_array($condition, \_BOOLEAN_OPERATORS)) return $this->clearPrepare(); @@ -428,12 +435,38 @@ private function processConditions($column, $condition, $value, $valueOrCombine, } elseif ((($condition == \_LIKE) || ($condition == \_notLIKE)) && !\preg_match('/[_%?]/', $value)) { return $this->clearPrepare(); } else { - $this->conditions($column, $condition, $value, $valueOrCombine); + $this->conditions($column, $condition, $value, $valueOrCombine, $whereGroup); } } + public function whereGroup(...$whereConditions) + { + if (empty($whereConditions)) + return false; + + $whereOrHaving = ($this->isWhere) ? 'WHERE' : 'HAVING'; + + if (\is_string($whereConditions[0]) && \strpos($whereConditions[0], $whereOrHaving) !== false) + return $whereConditions[0]; + + $totalConditions = count($whereConditions) - 1; + + if ($totalConditions > 0) { + + if (empty($whereConditions[0][4]) || $whereConditions[0][4] !== '(') + $whereConditions[0][4] = '('; + + if (empty($whereConditions[$totalConditions][4]) || $whereConditions[$totalConditions][4] !== ')') + $whereConditions[$totalConditions][4] = ')'; + } + + return $whereConditions; + } + public function where(...$whereConditions) { + $whereConditions = (!empty($whereConditions[0]) && is_array($whereConditions[0])) ? $whereConditions[0] : $whereConditions; + if (empty($whereConditions)) return false; @@ -445,7 +478,7 @@ public function where(...$whereConditions) if (\is_string($whereConditions[0]) && \strpos($whereConditions[0], $whereOrHaving) !== false) return $whereConditions[0]; - list($operator, $whereClause, $combiner, $extra) = $this->retrieveConditions($whereConditions); + list($operator, $whereClause, $combiner, $extra, $group) = $this->retrieveConditions($whereConditions); if (empty($operator)) return false; @@ -460,7 +493,7 @@ public function where(...$whereConditions) if (\in_array(\strtoupper($combine), \_COMBINERS) || isset($extra[$i])) $this->combineWith = isset($extra[$i]) ? $combine : \strtoupper($combine); - if ($this->processConditions($key, $isCondition, $val, $this->combineWith, $extra[$i]) === false) + if ($this->processConditions($key, $isCondition, $val, $this->combineWith, $extra[$i], $group[$i]) === false) return false; $i++; diff --git a/lib/ezQueryInterface.php b/lib/ezQueryInterface.php index 08bfe5f..e5517d3 100644 --- a/lib/ezQueryInterface.php +++ b/lib/ezQueryInterface.php @@ -294,6 +294,33 @@ public function orderBy($orderBy, $order); */ public function limit($numberOf, $offset = null); + /** + * Helper adds WHERE grouping to the conditions + * + * format: + * `whereGroup( comparison(x, y, and) )` + * + * example: + * `whereGroup( eq(key, value, combiner ), eq(key, value, combiner ) );` + * + * @param array $whereConditions - In the following format: + * + * eq('key/Field/Column', $value, _AND), // combine next expression + * neq('key/Field/Column', $value, _OR), // will combine next expression again + * ne('key/Field/Column', $value), // the default is _AND so will combine next expression + * lt('key/Field/Column', $value) + * lte('key/Field/Column', $value) + * gt('key/Field/Column', $value) + * gte('key/Field/Column', $value) + * isNull('key/Field/Column') + * isNotNull('key/Field/Column') + * like('key/Field/Column', '_%') + * notLike('key/Field/Column', '_%') + * + * @return mixed bool/string - WHERE SQL statement, or false on error + */ + public function whereGroup(...$whereConditions); + /** * Helper returns an WHERE sql clause string. * @@ -321,7 +348,7 @@ public function limit($numberOf, $offset = null); * between('key/Field/Column', $value, $value2) * notBetween('key/Field/Column', $value, $value2) * - * @return mixed bool/string - WHERE SQL statement, or false on error + * @return array modified conditions */ public function where(...$whereConditions); From 009a75dbf594f4a08a68c01fedd678ab9792576c Mon Sep 17 00:00:00 2001 From: Damien Date: Tue, 21 Apr 2020 17:52:18 +1200 Subject: [PATCH 02/10] Testing phase Methods and functions complete but failed testing in phpunit so needing to fix --- lib/ezFunctions.php | 13 +++++++++++++ lib/ezQuery.php | 31 ++++++++++++++++++------------- tests/pdo/pdo_mysqlTest.php | 20 ++++++++++++++++++++ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/lib/ezFunctions.php b/lib/ezFunctions.php index 03cfdcf..96cebd0 100644 --- a/lib/ezFunctions.php +++ b/lib/ezFunctions.php @@ -494,6 +494,19 @@ function replace($table = '', $keyValue) : false; } + function flattenWhereConditions($whereConditions) + { + $whereConditionsReturn = []; + foreach ($whereConditions as $whereCondition) { + if (!empty($whereCondition[0]) && is_array($whereCondition[0])) { + $whereConditionsReturn = array_merge($whereConditionsReturn, flattenWhereConditions($whereCondition)); + } else { + $whereConditionsReturn[] = $whereCondition; + } + } + return $whereConditionsReturn; + } + function ezFunctions() { return true; diff --git a/lib/ezQuery.php b/lib/ezQuery.php index 8d705fe..e7df71a 100644 --- a/lib/ezQuery.php +++ b/lib/ezQuery.php @@ -393,7 +393,9 @@ private function conditionIs($key, $condition, $combine) private function retrieveConditions($whereConditions) { - $whereClause = []; + $whereConditions = flattenWhereConditions($whereConditions); + $whereKey = []; + $whereValue = []; $operator = []; $extra = []; $combiner = []; @@ -402,23 +404,27 @@ private function retrieveConditions($whereConditions) $operator[] = (isset($checkFields[1])) ? $checkFields[1] : ''; if (empty($checkFields[1])) { $this->clearPrepare(); - return [[], [], [], [], []]; + return [[], [], [], [], [], []]; } if (\strtoupper($checkFields[1]) == 'IN') { - $whereClause[$checkFields[0]] = \array_slice((array) $checkFields, 2); + $whereKey[] = $checkFields[0]; + $whereValue[] = \array_slice((array) $checkFields, 2); $combiner[] = \_AND; $group[] = null; $extra[] = null; } else { - $whereClause[(isset($checkFields[0])) ? $checkFields[0] : '1'] = (isset($checkFields[2])) ? $checkFields[2] : ''; - $combiner[] = (isset($checkFields[3])) ? $checkFields[3] : \_AND; - $group[] = (isset($checkFields[4])) ? $checkFields[4] : null; - $extra[] = (isset($checkFields[5])) ? $checkFields[5] : null; + if (!empty($checkFields[0])) { + $whereKey[] = $checkFields[0]; + $whereValue[] = (isset($checkFields[2])) ? $checkFields[2] : ''; + $combiner[] = (isset($checkFields[3])) ? $checkFields[3] : \_AND; + $group[] = (isset($checkFields[4])) ? $checkFields[4] : null; + $extra[] = (isset($checkFields[5])) ? $checkFields[5] : null; + } } } - return [$operator, $whereClause, $combiner, $extra, $group]; + return [$operator, $whereKey, $whereValue, $combiner, $extra, $group]; } private function processConditions($column, $condition, $value, $valueOrCombine, $extraCombine, $whereGroup) @@ -465,7 +471,6 @@ public function whereGroup(...$whereConditions) public function where(...$whereConditions) { - $whereConditions = (!empty($whereConditions[0]) && is_array($whereConditions[0])) ? $whereConditions[0] : $whereConditions; if (empty($whereConditions)) return false; @@ -478,22 +483,22 @@ public function where(...$whereConditions) if (\is_string($whereConditions[0]) && \strpos($whereConditions[0], $whereOrHaving) !== false) return $whereConditions[0]; - list($operator, $whereClause, $combiner, $extra, $group) = $this->retrieveConditions($whereConditions); + list($operator, $whereKeys, $whereValues, $combiner, $extra, $group) = $this->retrieveConditions($whereConditions); if (empty($operator)) return false; $where = '1'; - if (!isset($whereClause['1'])) { + if (!empty($whereKeys)) { $this->whereSQL = ''; $i = 0; - foreach ($whereClause as $key => $val) { + foreach ($whereKeys as $key) { $isCondition = \strtoupper($operator[$i]); $combine = $combiner[$i]; $this->combineWith = \_AND; if (\in_array(\strtoupper($combine), \_COMBINERS) || isset($extra[$i])) $this->combineWith = isset($extra[$i]) ? $combine : \strtoupper($combine); - if ($this->processConditions($key, $isCondition, $val, $this->combineWith, $extra[$i], $group[$i]) === false) + if ($this->processConditions($key, $isCondition, $whereValues[$i], $this->combineWith, $extra[$i], $group[$i]) === false) return false; $i++; diff --git a/tests/pdo/pdo_mysqlTest.php b/tests/pdo/pdo_mysqlTest.php index dc36492..50cc04d 100644 --- a/tests/pdo/pdo_mysqlTest.php +++ b/tests/pdo/pdo_mysqlTest.php @@ -265,6 +265,26 @@ public function testSelecting() $this->assertEquals(0, $this->object->query('DROP TABLE unit_test')); } + public function testWhereGroup() + { + $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)); + $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), active tinyint(1), PRIMARY KEY (ID))'); + $this->object->insert('unit_test', array('id' => '1', 'test_key' => 'testing 1', 'active' => 1)); + $this->object->insert('unit_test', array('id' => '2', 'test_key' => 'testing 2', 'active' => 0)); + $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); + $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); + + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%', _OR)))); + $i = 1; + foreach ($result as $row) { + $this->assertEquals($i, $row->id); + $this->assertEquals('testing ' . $i, $row->test_key); + $i = $i + 2; + } + + $this->assertEquals(0, $this->object->query('DROP TABLE unit_test')); + } + public function testJoins() { $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)); From b50ca9d255ca86a30ce48fee52bd12132c34d575 Mon Sep 17 00:00:00 2001 From: Damien Date: Tue, 21 Apr 2020 18:09:19 +1200 Subject: [PATCH 03/10] Tests completed PHPUnit tests succeeded. --- lib/ezFunctions.php | 4 ++-- tests/pdo/pdo_pgsqlTest.php | 18 ++++++++++++++++++ tests/pdo/pdo_sqliteTest.php | 20 ++++++++++++++++++++ tests/pdo/pdo_sqlsrvTest.php | 18 ++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/ezFunctions.php b/lib/ezFunctions.php index 96cebd0..7da5199 100644 --- a/lib/ezFunctions.php +++ b/lib/ezFunctions.php @@ -260,7 +260,7 @@ function notIn($x, $y, ...$args) function between($x, $y, $y2, ...$args) { $expression = array(); - \array_push($expression, $x, \_BETWEEN, $y, $y2, \_AND, ...$args); + \array_push($expression, $x, \_BETWEEN, $y, $y2, null, \_AND, ...$args); return $expression; } @@ -270,7 +270,7 @@ function between($x, $y, $y2, ...$args) function notBetween($x, $y, $y2, ...$args) { $expression = array(); - \array_push($expression, $x, \_notBETWEEN, $y, $y2, \_AND, ...$args); + \array_push($expression, $x, \_notBETWEEN, $y, $y2, null, \_AND, ...$args); return $expression; } diff --git a/tests/pdo/pdo_pgsqlTest.php b/tests/pdo/pdo_pgsqlTest.php index 414b6f0..117a839 100644 --- a/tests/pdo/pdo_pgsqlTest.php +++ b/tests/pdo/pdo_pgsqlTest.php @@ -189,6 +189,24 @@ public function testSelecting() } } + public function testWhereGroup() + { + $this->assertTrue($this->object->connect('pgsql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD)); + $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), active tinyint(1), PRIMARY KEY (ID))'); + $this->object->insert('unit_test', array('id' => '1', 'test_key' => 'testing 1', 'active' => 1)); + $this->object->insert('unit_test', array('id' => '2', 'test_key' => 'testing 2', 'active' => 0)); + $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); + $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); + + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%', _OR)))); + $i = 1; + foreach ($result as $row) { + $this->assertEquals($i, $row->id); + $this->assertEquals('testing ' . $i, $row->test_key); + $i = $i + 2; + } + } + public function testJoins() { $this->assertTrue($this->object->connect('pgsql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD)); diff --git a/tests/pdo/pdo_sqliteTest.php b/tests/pdo/pdo_sqliteTest.php index ea639af..abe98d7 100644 --- a/tests/pdo/pdo_sqliteTest.php +++ b/tests/pdo/pdo_sqliteTest.php @@ -216,6 +216,26 @@ public function testSelecting() $this->assertEquals(1, $this->object->query('DROP TABLE unit_test')); } + public function testWhereGroup() + { + $this->assertTrue($this->object->connect('sqlite:' . self::TEST_SQLITE_DB, '', '', array(), true)); + $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), active tinyint(1), PRIMARY KEY (ID))'); + $this->object->insert('unit_test', array('id' => '1', 'test_key' => 'testing 1', 'active' => 1)); + $this->object->insert('unit_test', array('id' => '2', 'test_key' => 'testing 2', 'active' => 0)); + $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); + $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); + + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%', _OR)))); + $i = 1; + foreach ($result as $row) { + $this->assertEquals($i, $row->id); + $this->assertEquals('testing ' . $i, $row->test_key); + $i = $i + 2; + } + + $this->assertEquals(0, $this->object->query('DROP TABLE unit_test')); + } + public function testJoins() { $this->assertTrue($this->object->connect('sqlite:' . self::TEST_SQLITE_DB, '', '', array(), true)); diff --git a/tests/pdo/pdo_sqlsrvTest.php b/tests/pdo/pdo_sqlsrvTest.php index f3ed02b..24499e1 100644 --- a/tests/pdo/pdo_sqlsrvTest.php +++ b/tests/pdo/pdo_sqlsrvTest.php @@ -193,6 +193,24 @@ public function testSelecting() } } + public function testWhereGroup() + { + $this->assertTrue($this->object->connect('sqlsrv:Server=' . self::TEST_DB_HOST . ';Database=' . self::TEST_DB_NAME, self::TEST_DB_USER, self::TEST_DB_PASSWORD)); + $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), active tinyint(1), PRIMARY KEY (ID))'); + $this->object->insert('unit_test', array('id' => '1', 'test_key' => 'testing 1', 'active' => 1)); + $this->object->insert('unit_test', array('id' => '2', 'test_key' => 'testing 2', 'active' => 0)); + $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); + $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); + + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%', _OR)))); + $i = 1; + foreach ($result as $row) { + $this->assertEquals($i, $row->id); + $this->assertEquals('testing ' . $i, $row->test_key); + $i = $i + 2; + } + } + public function testJoins() { $this->assertTrue($this->object->connect('sqlsrv:Server=' . self::TEST_DB_HOST . ';Database=' . self::TEST_DB_NAME, self::TEST_DB_USER, self::TEST_DB_PASSWORD)); From 6ef9eff13ad8f3b14d7372754ab4f93ea10ac3c3 Mon Sep 17 00:00:00 2001 From: Damien Date: Tue, 21 Apr 2020 18:16:14 +1200 Subject: [PATCH 04/10] Cleaned up trailing in tests Cleaned up trailing combiner in tests --- tests/pdo/pdo_mysqlTest.php | 2 +- tests/pdo/pdo_pgsqlTest.php | 2 +- tests/pdo/pdo_sqliteTest.php | 2 +- tests/pdo/pdo_sqlsrvTest.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/pdo/pdo_mysqlTest.php b/tests/pdo/pdo_mysqlTest.php index 50cc04d..fd0af43 100644 --- a/tests/pdo/pdo_mysqlTest.php +++ b/tests/pdo/pdo_mysqlTest.php @@ -274,7 +274,7 @@ public function testWhereGroup() $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); - $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%', _OR)))); + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%')))); $i = 1; foreach ($result as $row) { $this->assertEquals($i, $row->id); diff --git a/tests/pdo/pdo_pgsqlTest.php b/tests/pdo/pdo_pgsqlTest.php index 117a839..812573d 100644 --- a/tests/pdo/pdo_pgsqlTest.php +++ b/tests/pdo/pdo_pgsqlTest.php @@ -198,7 +198,7 @@ public function testWhereGroup() $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); - $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%', _OR)))); + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%')))); $i = 1; foreach ($result as $row) { $this->assertEquals($i, $row->id); diff --git a/tests/pdo/pdo_sqliteTest.php b/tests/pdo/pdo_sqliteTest.php index abe98d7..ee1338c 100644 --- a/tests/pdo/pdo_sqliteTest.php +++ b/tests/pdo/pdo_sqliteTest.php @@ -225,7 +225,7 @@ public function testWhereGroup() $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); - $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%', _OR)))); + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%')))); $i = 1; foreach ($result as $row) { $this->assertEquals($i, $row->id); diff --git a/tests/pdo/pdo_sqlsrvTest.php b/tests/pdo/pdo_sqlsrvTest.php index 24499e1..5de13ee 100644 --- a/tests/pdo/pdo_sqlsrvTest.php +++ b/tests/pdo/pdo_sqlsrvTest.php @@ -202,7 +202,7 @@ public function testWhereGroup() $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); - $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%', _OR)))); + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%')))); $i = 1; foreach ($result as $row) { $this->assertEquals($i, $row->id); From cc7a8cdb18a25696a1c7b21e0290cc79c90c4b38 Mon Sep 17 00:00:00 2001 From: Damien Date: Wed, 22 Apr 2020 13:02:57 +1200 Subject: [PATCH 05/10] Changed from breaking changes to user $args Changed from breaking changes to use the existing extra `$args`. All existing PHPUnit tests run without failing. --- lib/ezFunctions.php | 53 ++++++++++++++++++------------------ lib/ezQuery.php | 31 ++++++++++----------- tests/pdo/pdo_sqliteTest.php | 2 +- 3 files changed, 41 insertions(+), 45 deletions(-) diff --git a/lib/ezFunctions.php b/lib/ezFunctions.php index 7da5199..561c23c 100644 --- a/lib/ezFunctions.php +++ b/lib/ezFunctions.php @@ -113,12 +113,11 @@ function createCertificate( * * @param strings $y, - The right expression. * @param strings $and, - combine additional expressions with, 'AND','OR', 'NOT', 'AND NOT'. - * @param strings $group, - notes beginning or end of where group, '(',')'. * @param strings $args - for any extras * - * function comparison($x, $operator, $y, $and=null, $group=null, ...$args) + * function comparison($x, $operator, $y, $and=null, ...$args) * { - * return array($x, $operator, $y, $and, $group, ...$args); + * return array($x, $operator, $y, $and, ...$args); * } * * @return array @@ -127,110 +126,110 @@ function createCertificate( /** * Creates an equality comparison expression with the given arguments. */ - function eq($x, $y, $and = null, $group = null, ...$args) + function eq($x, $y, $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \EQ, $y, $and, $group, ...$args); + \array_push($expression, $x, \EQ, $y, $and, ...$args); return $expression; } /** * Creates a non equality comparison expression with the given arguments. */ - function neq($x, $y, $and = null, $group = null, ...$args) + function neq($x, $y, $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \NEQ, $y, $and, $group, ...$args); + \array_push($expression, $x, \NEQ, $y, $and, ...$args); return $expression; } /** * Creates the other non equality comparison expression with the given arguments. */ - function ne($x, $y, $and = null, $group = null, ...$args) + function ne($x, $y, $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \NE, $y, $and, $group, ...$args); + \array_push($expression, $x, \NE, $y, $and, ...$args); return $expression; } /** * Creates a lower-than comparison expression with the given arguments. */ - function lt($x, $y, $and = null, $group = null, ...$args) + function lt($x, $y, $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \LT, $y, $and, $group, ...$args); + \array_push($expression, $x, \LT, $y, $and, ...$args); return $expression; } /** * Creates a lower-than-equal comparison expression with the given arguments. */ - function lte($x, $y, $and = null, $group = null, ...$args) + function lte($x, $y, $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \LTE, $y, $and, $group, ...$args); + \array_push($expression, $x, \LTE, $y, $and, ...$args); return $expression; } /** * Creates a greater-than comparison expression with the given arguments. */ - function gt($x, $y, $and = null, $group = null, ...$args) + function gt($x, $y, $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \GT, $y, $and, $group, ...$args); + \array_push($expression, $x, \GT, $y, $and, ...$args); return $expression; } /** * Creates a greater-than-equal comparison expression with the given arguments. */ - function gte($x, $y, $and = null, $group = null, ...$args) + function gte($x, $y, $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \GTE, $y, $and, $group, ...$args); + \array_push($expression, $x, \GTE, $y, $and, ...$args); return $expression; } /** * Creates an IS NULL expression with the given arguments. */ - function isNull($x, $y = 'null', $and = null, $group = null, ...$args) + function isNull($x, $y = 'null', $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \_isNULL, $y, $and, $group, ...$args); + \array_push($expression, $x, \_isNULL, $y, $and, ...$args); return $expression; } /** * Creates an IS NOT NULL expression with the given arguments. */ - function isNotNull($x, $y = 'null', $and = null, $group = null, ...$args) + function isNotNull($x, $y = 'null', $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \_notNULL, $y, $and, $group, ...$args); + \array_push($expression, $x, \_notNULL, $y, $and, ...$args); return $expression; } /** * Creates a LIKE() comparison expression with the given arguments. */ - function like($x, $y, $and = null, $group = null, ...$args) + function like($x, $y, $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \_LIKE, $y, $and, $group, ...$args); + \array_push($expression, $x, \_LIKE, $y, $and, ...$args); return $expression; } /** * Creates a NOT LIKE() comparison expression with the given arguments. */ - function notLike($x, $y, $and = null, $group = null, ...$args) + function notLike($x, $y, $and = null, ...$args) { $expression = array(); - \array_push($expression, $x, \_notLIKE, $y, $and, $group, ...$args); + \array_push($expression, $x, \_notLIKE, $y, $and, ...$args); return $expression; } @@ -260,7 +259,7 @@ function notIn($x, $y, ...$args) function between($x, $y, $y2, ...$args) { $expression = array(); - \array_push($expression, $x, \_BETWEEN, $y, $y2, null, \_AND, ...$args); + \array_push($expression, $x, \_BETWEEN, $y, $y2, \_AND, ...$args); return $expression; } @@ -270,7 +269,7 @@ function between($x, $y, $y2, ...$args) function notBetween($x, $y, $y2, ...$args) { $expression = array(); - \array_push($expression, $x, \_notBETWEEN, $y, $y2, null, \_AND, ...$args); + \array_push($expression, $x, \_notBETWEEN, $y, $y2, \_AND, ...$args); return $expression; } diff --git a/lib/ezQuery.php b/lib/ezQuery.php index e7df71a..55e0767 100644 --- a/lib/ezQuery.php +++ b/lib/ezQuery.php @@ -342,10 +342,10 @@ public function limit($numberOf, $offset = null) return 'LIMIT ' . $rows . $value; } - private function conditions($key, $condition, $value, $combine, $group) + private function conditions($key, $condition, $value, $combine, $extra) { - $groupStart = (!empty($group) && $group === '(') ? $group : ''; - $groupEnd = (!empty($group) && $group === ')') ? $group : ''; + $groupStart = (!empty($extra) && $extra === '(') ? $extra : ''; + $groupEnd = (!empty($extra) && $extra === ')') ? $extra : ''; if ($this->isPrepareOn()) { $this->whereSQL .= "$groupStart $key $condition " . \_TAG . " $groupEnd $combine "; @@ -399,35 +399,32 @@ private function retrieveConditions($whereConditions) $operator = []; $extra = []; $combiner = []; - $group = []; foreach ($whereConditions as $checkFields) { $operator[] = (isset($checkFields[1])) ? $checkFields[1] : ''; if (empty($checkFields[1])) { $this->clearPrepare(); - return [[], [], [], [], [], []]; + return [[], [], [], [], []]; } if (\strtoupper($checkFields[1]) == 'IN') { $whereKey[] = $checkFields[0]; $whereValue[] = \array_slice((array) $checkFields, 2); $combiner[] = \_AND; - $group[] = null; $extra[] = null; } else { if (!empty($checkFields[0])) { $whereKey[] = $checkFields[0]; $whereValue[] = (isset($checkFields[2])) ? $checkFields[2] : ''; $combiner[] = (isset($checkFields[3])) ? $checkFields[3] : \_AND; - $group[] = (isset($checkFields[4])) ? $checkFields[4] : null; - $extra[] = (isset($checkFields[5])) ? $checkFields[5] : null; + $extra[] = (isset($checkFields[4])) ? $checkFields[4] : null; } } } - return [$operator, $whereKey, $whereValue, $combiner, $extra, $group]; + return [$operator, $whereKey, $whereValue, $combiner, $extra]; } - private function processConditions($column, $condition, $value, $valueOrCombine, $extraCombine, $whereGroup) + private function processConditions($column, $condition, $value, $valueOrCombine, $extraCombine) { if (!\in_array($condition, \_BOOLEAN_OPERATORS)) return $this->clearPrepare(); @@ -441,7 +438,7 @@ private function processConditions($column, $condition, $value, $valueOrCombine, } elseif ((($condition == \_LIKE) || ($condition == \_notLIKE)) && !\preg_match('/[_%?]/', $value)) { return $this->clearPrepare(); } else { - $this->conditions($column, $condition, $value, $valueOrCombine, $whereGroup); + $this->conditions($column, $condition, $value, $valueOrCombine, $extraCombine); } } @@ -459,11 +456,11 @@ public function whereGroup(...$whereConditions) if ($totalConditions > 0) { - if (empty($whereConditions[0][4]) || $whereConditions[0][4] !== '(') - $whereConditions[0][4] = '('; + if (!in_array('(', $whereConditions[0])) + $whereConditions[0][count($whereConditions[0])] = '('; - if (empty($whereConditions[$totalConditions][4]) || $whereConditions[$totalConditions][4] !== ')') - $whereConditions[$totalConditions][4] = ')'; + if (!in_array(')', $whereConditions[$totalConditions])) + $whereConditions[$totalConditions][count($whereConditions[$totalConditions])] = ')'; } return $whereConditions; @@ -483,7 +480,7 @@ public function where(...$whereConditions) if (\is_string($whereConditions[0]) && \strpos($whereConditions[0], $whereOrHaving) !== false) return $whereConditions[0]; - list($operator, $whereKeys, $whereValues, $combiner, $extra, $group) = $this->retrieveConditions($whereConditions); + list($operator, $whereKeys, $whereValues, $combiner, $extra) = $this->retrieveConditions($whereConditions); if (empty($operator)) return false; @@ -498,7 +495,7 @@ public function where(...$whereConditions) if (\in_array(\strtoupper($combine), \_COMBINERS) || isset($extra[$i])) $this->combineWith = isset($extra[$i]) ? $combine : \strtoupper($combine); - if ($this->processConditions($key, $isCondition, $whereValues[$i], $this->combineWith, $extra[$i], $group[$i]) === false) + if ($this->processConditions($key, $isCondition, $whereValues[$i], $this->combineWith, $extra[$i]) === false) return false; $i++; diff --git a/tests/pdo/pdo_sqliteTest.php b/tests/pdo/pdo_sqliteTest.php index ee1338c..12a77b3 100644 --- a/tests/pdo/pdo_sqliteTest.php +++ b/tests/pdo/pdo_sqliteTest.php @@ -233,7 +233,7 @@ public function testWhereGroup() $i = $i + 2; } - $this->assertEquals(0, $this->object->query('DROP TABLE unit_test')); + $this->assertEquals(1, $this->object->query('DROP TABLE unit_test')); } public function testJoins() From fd67871dc57de363eec2d8338bc750173c65d298 Mon Sep 17 00:00:00 2001 From: Damien Date: Wed, 22 Apr 2020 13:05:20 +1200 Subject: [PATCH 06/10] Fixed incorrect return change Incorrectly made a modification to the return of the `where` method instead of the `whereGroup` method --- lib/ezQueryInterface.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ezQueryInterface.php b/lib/ezQueryInterface.php index e5517d3..a9c4acb 100644 --- a/lib/ezQueryInterface.php +++ b/lib/ezQueryInterface.php @@ -317,7 +317,7 @@ public function limit($numberOf, $offset = null); * like('key/Field/Column', '_%') * notLike('key/Field/Column', '_%') * - * @return mixed bool/string - WHERE SQL statement, or false on error + * @return array modified conditions */ public function whereGroup(...$whereConditions); @@ -348,7 +348,7 @@ public function whereGroup(...$whereConditions); * between('key/Field/Column', $value, $value2) * notBetween('key/Field/Column', $value, $value2) * - * @return array modified conditions + * @return mixed bool/string - WHERE SQL statement, or false on error */ public function where(...$whereConditions); From b35291aae60a281b623e820cf5de9053c1911b8a Mon Sep 17 00:00:00 2001 From: Damien Date: Thu, 23 Apr 2020 11:52:32 +1200 Subject: [PATCH 07/10] Synced with master for conflicts Synced with master branch to reduce conflicts --- lib/ezQuery.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ezQuery.php b/lib/ezQuery.php index d66cbc1..dc1278e 100644 --- a/lib/ezQuery.php +++ b/lib/ezQuery.php @@ -393,6 +393,7 @@ private function conditionIs($key, $condition, $combine) private function retrieveConditions($whereConditions) { + $whereConditions = flattenWhereConditions($whereConditions); $whereKey = []; $whereValue = []; $operator = []; From 1637812e9eb751a2b793efa2ff13034d01a678df Mon Sep 17 00:00:00 2001 From: Damien Date: Thu, 23 Apr 2020 11:53:20 +1200 Subject: [PATCH 08/10] Synced with master for conflicts Synced with master branch to reduce conflicts --- lib/ezQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ezQuery.php b/lib/ezQuery.php index dc1278e..55e0767 100644 --- a/lib/ezQuery.php +++ b/lib/ezQuery.php @@ -412,7 +412,7 @@ private function retrieveConditions($whereConditions) $combiner[] = \_AND; $extra[] = null; } else { - if (isset($checkFields[0])) { + if (!empty($checkFields[0])) { $whereKey[] = $checkFields[0]; $whereValue[] = (isset($checkFields[2])) ? $checkFields[2] : ''; $combiner[] = (isset($checkFields[3])) ? $checkFields[3] : \_AND; From f990c889701d9a249f2e4ab23080f73b9cea2ab1 Mon Sep 17 00:00:00 2001 From: Damien Date: Fri, 24 Apr 2020 10:13:44 +1200 Subject: [PATCH 09/10] Changed to grouping Changed the method from `whereGroup` to `grouping` and moved the flatten function to be a private method in the `ezQuery` file as per @techno-express recommendations. --- lib/ezFunctions.php | 17 ++--------------- lib/ezQuery.php | 17 +++++++++++++++-- lib/ezQueryInterface.php | 6 +++--- tests/pdo/pdo_mysqlTest.php | 4 ++-- tests/pdo/pdo_pgsqlTest.php | 4 ++-- tests/pdo/pdo_sqliteTest.php | 4 ++-- tests/pdo/pdo_sqlsrvTest.php | 4 ++-- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/ezFunctions.php b/lib/ezFunctions.php index 561c23c..b99fc9a 100644 --- a/lib/ezFunctions.php +++ b/lib/ezFunctions.php @@ -349,11 +349,11 @@ function where(...$args) : false; } - function whereGroup(...$args) + function grouping(...$args) { $ezQuery = \getInstance(); return ($ezQuery instanceof DatabaseInterface) - ? $ezQuery->whereGroup(...$args) + ? $ezQuery->grouping(...$args) : false; } @@ -493,19 +493,6 @@ function replace($table = '', $keyValue) : false; } - function flattenWhereConditions($whereConditions) - { - $whereConditionsReturn = []; - foreach ($whereConditions as $whereCondition) { - if (!empty($whereCondition[0]) && is_array($whereCondition[0])) { - $whereConditionsReturn = array_merge($whereConditionsReturn, flattenWhereConditions($whereCondition)); - } else { - $whereConditionsReturn[] = $whereCondition; - } - } - return $whereConditionsReturn; - } - function ezFunctions() { return true; diff --git a/lib/ezQuery.php b/lib/ezQuery.php index 55e0767..7c02ac7 100644 --- a/lib/ezQuery.php +++ b/lib/ezQuery.php @@ -391,9 +391,22 @@ private function conditionIs($key, $condition, $combine) $this->whereSQL .= "$key $isCondition NULL $combine "; } + private function flattenWhereConditions($whereConditions) + { + $whereConditionsReturn = []; + foreach ($whereConditions as $whereCondition) { + if (!empty($whereCondition[0]) && is_array($whereCondition[0])) { + $whereConditionsReturn = array_merge($whereConditionsReturn, $this->flattenWhereConditions($whereCondition)); + } else { + $whereConditionsReturn[] = $whereCondition; + } + } + return $whereConditionsReturn; + } + private function retrieveConditions($whereConditions) { - $whereConditions = flattenWhereConditions($whereConditions); + $whereConditions = $this->flattenWhereConditions($whereConditions); $whereKey = []; $whereValue = []; $operator = []; @@ -442,7 +455,7 @@ private function processConditions($column, $condition, $value, $valueOrCombine, } } - public function whereGroup(...$whereConditions) + public function grouping(...$whereConditions) { if (empty($whereConditions)) return false; diff --git a/lib/ezQueryInterface.php b/lib/ezQueryInterface.php index a9c4acb..5007d1d 100644 --- a/lib/ezQueryInterface.php +++ b/lib/ezQueryInterface.php @@ -298,10 +298,10 @@ public function limit($numberOf, $offset = null); * Helper adds WHERE grouping to the conditions * * format: - * `whereGroup( comparison(x, y, and) )` + * `grouping( comparison(x, y, and) )` * * example: - * `whereGroup( eq(key, value, combiner ), eq(key, value, combiner ) );` + * `grouping( eq(key, value, combiner ), eq(key, value, combiner ) );` * * @param array $whereConditions - In the following format: * @@ -319,7 +319,7 @@ public function limit($numberOf, $offset = null); * * @return array modified conditions */ - public function whereGroup(...$whereConditions); + public function grouping(...$whereConditions); /** * Helper returns an WHERE sql clause string. diff --git a/tests/pdo/pdo_mysqlTest.php b/tests/pdo/pdo_mysqlTest.php index fd0af43..a46f2a2 100644 --- a/tests/pdo/pdo_mysqlTest.php +++ b/tests/pdo/pdo_mysqlTest.php @@ -265,7 +265,7 @@ public function testSelecting() $this->assertEquals(0, $this->object->query('DROP TABLE unit_test')); } - public function testWhereGroup() + public function testWhereGrouping() { $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)); $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), active tinyint(1), PRIMARY KEY (ID))'); @@ -274,7 +274,7 @@ public function testWhereGroup() $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); - $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%')))); + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), grouping(like('test_key', '%1%', _OR), like('test_key', '%3%')))); $i = 1; foreach ($result as $row) { $this->assertEquals($i, $row->id); diff --git a/tests/pdo/pdo_pgsqlTest.php b/tests/pdo/pdo_pgsqlTest.php index 812573d..58a91eb 100644 --- a/tests/pdo/pdo_pgsqlTest.php +++ b/tests/pdo/pdo_pgsqlTest.php @@ -189,7 +189,7 @@ public function testSelecting() } } - public function testWhereGroup() + public function testWhereGrouping() { $this->assertTrue($this->object->connect('pgsql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD)); $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), active tinyint(1), PRIMARY KEY (ID))'); @@ -198,7 +198,7 @@ public function testWhereGroup() $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); - $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%')))); + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), grouping(like('test_key', '%1%', _OR), like('test_key', '%3%')))); $i = 1; foreach ($result as $row) { $this->assertEquals($i, $row->id); diff --git a/tests/pdo/pdo_sqliteTest.php b/tests/pdo/pdo_sqliteTest.php index 12a77b3..2cc1348 100644 --- a/tests/pdo/pdo_sqliteTest.php +++ b/tests/pdo/pdo_sqliteTest.php @@ -216,7 +216,7 @@ public function testSelecting() $this->assertEquals(1, $this->object->query('DROP TABLE unit_test')); } - public function testWhereGroup() + public function testWhereGrouping() { $this->assertTrue($this->object->connect('sqlite:' . self::TEST_SQLITE_DB, '', '', array(), true)); $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), active tinyint(1), PRIMARY KEY (ID))'); @@ -225,7 +225,7 @@ public function testWhereGroup() $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); - $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%')))); + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), grouping(like('test_key', '%1%', _OR), like('test_key', '%3%')))); $i = 1; foreach ($result as $row) { $this->assertEquals($i, $row->id); diff --git a/tests/pdo/pdo_sqlsrvTest.php b/tests/pdo/pdo_sqlsrvTest.php index 5de13ee..de8e82b 100644 --- a/tests/pdo/pdo_sqlsrvTest.php +++ b/tests/pdo/pdo_sqlsrvTest.php @@ -193,7 +193,7 @@ public function testSelecting() } } - public function testWhereGroup() + public function testWhereGrouping() { $this->assertTrue($this->object->connect('sqlsrv:Server=' . self::TEST_DB_HOST . ';Database=' . self::TEST_DB_NAME, self::TEST_DB_USER, self::TEST_DB_PASSWORD)); $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), active tinyint(1), PRIMARY KEY (ID))'); @@ -202,7 +202,7 @@ public function testWhereGroup() $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3', 'active' => 1)); $this->object->insert('unit_test', array('id' => '4', 'test_key' => 'testing 4', 'active' => 1)); - $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), whereGroup(like('test_key', '%1%', _OR), like('test_key', '%3%')))); + $result = $this->object->selecting('unit_test', '*', where(eq('active', '1'), grouping(like('test_key', '%1%', _OR), like('test_key', '%3%')))); $i = 1; foreach ($result as $row) { $this->assertEquals($i, $row->id); From 8b83dd7bc50cf8f75d622628f0aa15c1ca205704 Mon Sep 17 00:00:00 2001 From: Damien Date: Fri, 24 Apr 2020 10:22:01 +1200 Subject: [PATCH 10/10] Added brief example to the readme Added a brief example in the readme file underneath the shortcut methods. Will add a detailed example in the wiki once approved. --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index f86235d..526839b 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,15 @@ notBetween('column', $value, $value2) // $value will protected by either using escape or prepare statement ``` +```php +// To allow simple grouping of basic $whereConditions, +// wrap the following around a group of the above comparison +// expressions within the where( ...$whereConditions) clause +grouping( eq(key, value, combiner ), eq(key, value, combiner ) ) +// The above will wrap beginning and end grouping in a where statement +// where required to break down your where clause. +``` + ```php // Supply the the whole query string, and placing '?' within // With the same number of arguments in an array.