From 5c77e4381945300ba9a1086b99fac12ae48c52a8 Mon Sep 17 00:00:00 2001 From: brunoais Date: Tue, 24 Feb 2015 23:03:11 +0000 Subject: [PATCH 01/18] [feature/sql-bool-builder] First working version PHPBB3-13652 --- phpBB/phpbb/db/driver/driver.php | 92 ++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/phpBB/phpbb/db/driver/driver.php b/phpBB/phpbb/db/driver/driver.php index 8d360fc3e2..5822adceab 100644 --- a/phpBB/phpbb/db/driver/driver.php +++ b/phpBB/phpbb/db/driver/driver.php @@ -792,6 +792,98 @@ abstract class driver implements driver_interface return $sql; } + + + protected function _process_boolean_tree_first($operations_ary) + { + if ($operations_ary[0] !== 'AND' && + $operations_ary[0] !== 'OR') + { + $operations_ary = array('AND', $operations_ary); + } + return $this->_process_boolean_tree($operations_ary) . "\n"; + } + + protected function _process_boolean_tree($operations_ary) + { + $operation = array_shift($operations_ary); + + foreach ($operations_ary AS &$condition) + { + switch ($condition[0]) + { + case 'AND': + case 'OR': + + $condition = ' ( ' . $this->_process_boolean_tree($condition) . ') '; + + break; + case 'NOT': + + $condition = ' NOT ' . $this->_process_boolean_tree($condition); + + break; + + default: + + switch (sizeof($condition)) + { + case 3: + + // Typical 3 element clause with {left hand} {operator} {right hand} + switch ($condition[1]) + { + case 'IN': + case 'NOT_IN': + + // As this is used with an IN, assume it is a set of elements for sql_in_set() + $condition = $this->sql_in_set($condition[0], $condition[2], $condition[1] === 'NOT_IN', true); + + break; + + default: + + $condition = implode(' ', $condition); + + break; + } + + break; + + case 5: + + // Subquery with {left hand} {operator} {compare kind} {SELECT Kind } {Sub Query} + + $condition = $condition[0] . ' ' . $condition[1] . ' ' . $condition[2] . ' ( '; + $condition .= $this->sql_build_query($condition[3], $condition[4]); + $condition .= ' )'; + + break; + + default: + // This is an unpredicted clause setup. Just join all elements. + $condition = implode(' ', $condition); + + break; + } + + break; + } + + } + + if($operation === 'NOT') + { + $operations_ary = implode("", $operations_ary); + } + else + { + $operations_ary = implode(" \n $operation ", $operations_ary); + } + + return $operations_ary; + } + /** * {@inheritDoc} From b54adaaabed7546254ff8317763214cce3a40237 Mon Sep 17 00:00:00 2001 From: brunoais Date: Wed, 25 Feb 2015 07:54:31 +0000 Subject: [PATCH 02/18] [feature/sql-bool-builder] Removed non-necessary spaces PHPBB3-13652 --- phpBB/phpbb/db/driver/driver.php | 58 ++++++++++++++++---------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/phpBB/phpbb/db/driver/driver.php b/phpBB/phpbb/db/driver/driver.php index 5822adceab..cdff6bf9de 100644 --- a/phpBB/phpbb/db/driver/driver.php +++ b/phpBB/phpbb/db/driver/driver.php @@ -792,11 +792,11 @@ abstract class driver implements driver_interface return $sql; } - - + + protected function _process_boolean_tree_first($operations_ary) { - if ($operations_ary[0] !== 'AND' && + if ($operations_ary[0] !== 'AND' && $operations_ary[0] !== 'OR') { $operations_ary = array('AND', $operations_ary); @@ -807,71 +807,71 @@ abstract class driver implements driver_interface protected function _process_boolean_tree($operations_ary) { $operation = array_shift($operations_ary); - + foreach ($operations_ary AS &$condition) { switch ($condition[0]) { case 'AND': case 'OR': - + $condition = ' ( ' . $this->_process_boolean_tree($condition) . ') '; - + break; case 'NOT': - + $condition = ' NOT ' . $this->_process_boolean_tree($condition); - + break; - + default: - + switch (sizeof($condition)) { case 3: - + // Typical 3 element clause with {left hand} {operator} {right hand} switch ($condition[1]) - { + { case 'IN': case 'NOT_IN': - + // As this is used with an IN, assume it is a set of elements for sql_in_set() $condition = $this->sql_in_set($condition[0], $condition[2], $condition[1] === 'NOT_IN', true); - + break; - + default: - + $condition = implode(' ', $condition); - + break; } - + break; - + case 5: - + // Subquery with {left hand} {operator} {compare kind} {SELECT Kind } {Sub Query} - + $condition = $condition[0] . ' ' . $condition[1] . ' ' . $condition[2] . ' ( '; $condition .= $this->sql_build_query($condition[3], $condition[4]); $condition .= ' )'; - + break; - + default: // This is an unpredicted clause setup. Just join all elements. $condition = implode(' ', $condition); - + break; } - + break; } - + } - + if($operation === 'NOT') { $operations_ary = implode("", $operations_ary); @@ -880,10 +880,10 @@ abstract class driver implements driver_interface { $operations_ary = implode(" \n $operation ", $operations_ary); } - + return $operations_ary; } - + /** * {@inheritDoc} From 46de94690453e0bd6dfc589682b7c4ede34f1d59 Mon Sep 17 00:00:00 2001 From: brunoais Date: Wed, 25 Feb 2015 07:55:24 +0000 Subject: [PATCH 03/18] [feature/sql-bool-builder] Added code to use this feature for the WHERE clause PHPBB3-13652 --- phpBB/phpbb/db/driver/driver.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/driver/driver.php b/phpBB/phpbb/db/driver/driver.php index cdff6bf9de..0fbbe5ae7c 100644 --- a/phpBB/phpbb/db/driver/driver.php +++ b/phpBB/phpbb/db/driver/driver.php @@ -774,7 +774,18 @@ abstract class driver implements driver_interface if (!empty($array['WHERE'])) { - $sql .= ' WHERE ' . $this->_sql_custom_build('WHERE', $array['WHERE']); + $sql .= ' WHERE '; + + if (is_array($array['WHERE'])) + { + $sql_where = $this->_process_boolean_tree_first($array['WHERE']); + } + else + { + $sql_where = $array['WHERE']; + } + + $sql .= $this->_sql_custom_build('WHERE', $sql_where); } if (!empty($array['GROUP_BY'])) From 5c1850e10e0e31e4f72cc16c657e37bb009659df Mon Sep 17 00:00:00 2001 From: brunoais Date: Wed, 25 Feb 2015 09:09:48 +0000 Subject: [PATCH 04/18] [feature/sql-bool-builder] AS keyword must be lowercase; AS keyword must be lowercase; expected "as" but found "AS" PHPBB3-13652 --- phpBB/phpbb/db/driver/driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/driver/driver.php b/phpBB/phpbb/db/driver/driver.php index 0fbbe5ae7c..f66f10322b 100644 --- a/phpBB/phpbb/db/driver/driver.php +++ b/phpBB/phpbb/db/driver/driver.php @@ -819,7 +819,7 @@ abstract class driver implements driver_interface { $operation = array_shift($operations_ary); - foreach ($operations_ary AS &$condition) + foreach ($operations_ary as &$condition) { switch ($condition[0]) { From 51737be6162584209b483f97daa87ce9d3c039b0 Mon Sep 17 00:00:00 2001 From: brunoais Date: Sat, 28 Feb 2015 18:14:00 +0000 Subject: [PATCH 05/18] [feature/sql-bool-builder] Also use parenthesis for the NOT operator Be on the safe side, also use parenthesis for the NOT operator PHPBB3-13652 --- phpBB/phpbb/db/driver/driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/driver/driver.php b/phpBB/phpbb/db/driver/driver.php index f66f10322b..d2bb5f4f7c 100644 --- a/phpBB/phpbb/db/driver/driver.php +++ b/phpBB/phpbb/db/driver/driver.php @@ -831,7 +831,7 @@ abstract class driver implements driver_interface break; case 'NOT': - $condition = ' NOT ' . $this->_process_boolean_tree($condition); + $condition = ' NOT (' . $this->_process_boolean_tree($condition) . ') '; break; From 5d70f612be113336d6a85146369232c87d1b069b Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 1 Mar 2015 11:39:20 +0000 Subject: [PATCH 06/18] [feature/sql-bool-builder] Explain better the code in the first Explain what that if and check is for in the first method that is called. PHPBB3-13652 --- phpBB/phpbb/db/driver/driver.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpBB/phpbb/db/driver/driver.php b/phpBB/phpbb/db/driver/driver.php index d2bb5f4f7c..cda2f7f86d 100644 --- a/phpBB/phpbb/db/driver/driver.php +++ b/phpBB/phpbb/db/driver/driver.php @@ -807,6 +807,8 @@ abstract class driver implements driver_interface protected function _process_boolean_tree_first($operations_ary) { + // In cases where an array exists but there is no head condition, + // it should be because there's only 1 WHERE clause. This seems the best way to deal with it. if ($operations_ary[0] !== 'AND' && $operations_ary[0] !== 'OR') { From 298d86009ed77a4b75792a6237f987be271ac43c Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 11:40:02 +0000 Subject: [PATCH 07/18] [feature/sql-bool-builder] Added LIKE and NOT_LIKE to the comparations PHPBB3-13652 --- phpBB/phpbb/db/driver/driver.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/phpBB/phpbb/db/driver/driver.php b/phpBB/phpbb/db/driver/driver.php index cda2f7f86d..ce69ef6a52 100644 --- a/phpBB/phpbb/db/driver/driver.php +++ b/phpBB/phpbb/db/driver/driver.php @@ -854,6 +854,18 @@ abstract class driver implements driver_interface break; + case 'LIKE': + + $condition = $condition[0] . ' ' . $this->sql_like_expression($condition[2]) . ' '; + + break; + + case 'NOT_LIKE': + + $condition = $condition[0] . ' ' . $this->sql_not_like_expression($condition[2]) . ' '; + + break; + default: $condition = implode(' ', $condition); From 5bc03c961023e01dfb74eff296b12b6169d705e4 Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 17:16:53 +0000 Subject: [PATCH 08/18] [feature/sql-bool-builder] Prepare testing class PHPBB3-13652 --- tests/dbal/boolean_processor_test.php | 24 +++++++ tests/dbal/fixtures/boolean_processor.xml | 84 +++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 tests/dbal/boolean_processor_test.php create mode 100644 tests/dbal/fixtures/boolean_processor.xml diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php new file mode 100644 index 0000000000..f80e12ad4f --- /dev/null +++ b/tests/dbal/boolean_processor_test.php @@ -0,0 +1,24 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; + +class phpbb_boolean_processor_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/boolean_processor.xml'); + } + +} diff --git a/tests/dbal/fixtures/boolean_processor.xml b/tests/dbal/fixtures/boolean_processor.xml new file mode 100644 index 0000000000..c5da677116 --- /dev/null +++ b/tests/dbal/fixtures/boolean_processor.xml @@ -0,0 +1,84 @@ + + + + ban_id + ban_userid + + 1 + 2 + +
+ + user_id + username + username_clean + user_permissions + user_sig + + 1 + mass email + mass email + + + + + 2 + banned + banned + + + + + 3 + helper + helper + + + + + 4 + GroupBPal + groupbpal + + + + + 5 + GroupBPal2 + groupBPal2 + + + + + 6 + not in group + not in group + + + +
+ + user_id + group_id + + 1 + 1 + + + 2 + 1 + + + 3 + 1 + + + 4 + 2 + + + 5 + 2 + +
+
From d10a0ca1ca48d8d348922a806f14f63360179d50 Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 20:25:55 +0000 Subject: [PATCH 09/18] [feature/sql-bool-builder] test_triple_and_with_is_null PHPBB3-13652 --- tests/dbal/boolean_processor_test.php | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php index f80e12ad4f..652cb1164b 100644 --- a/tests/dbal/boolean_processor_test.php +++ b/tests/dbal/boolean_processor_test.php @@ -21,4 +21,41 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/boolean_processor.xml'); } + public function test_triple_and_with_is_null() + { + $db = $this->new_dbal(); + + $db->sql_return_on_error(true); + + $sql_ary = array( + 'SELECT' => 'u.username', + 'FROM' => array( + 'phpbb_users' => 'u', + 'phpbb_user_group' => 'ug', + ), + 'LEFT_JOIN' => array( + array( + 'FROM' => array( + 'phpbb_banlist' => 'b', + ), + 'ON' => 'u.user_id = b.ban_userid', + ), + ), + 'WHERE' => array('AND', + array('ug.group_id', '=', 1), + array('u.user_id', '=', 'ug.user_id'), + array('b.ban_id', 'IS', NULL), + ), + 'ORDER_BY' => 'u.username', + ); + $sql = $db->sql_build_query('SELECT', $sql_ary); + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array( + array('username' => 'helper'), + array('username' => 'mass email'), + ), $db->sql_fetchrowset($result)); + } } From 5d3b22c21e2c31dce80d3a4c84fd5a0a326f944f Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 20:26:24 +0000 Subject: [PATCH 10/18] [feature/sql-bool-builder] test_double_and_with_not_of_and PHPBB3-13652 --- tests/dbal/boolean_processor_test.php | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php index 652cb1164b..40efbe8d5a 100644 --- a/tests/dbal/boolean_processor_test.php +++ b/tests/dbal/boolean_processor_test.php @@ -21,6 +21,37 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/boolean_processor.xml'); } + public function test_double_and_with_not_of_or() + { + $db = $this->new_dbal(); + + $db->sql_return_on_error(true); + + $sql_ary = array( + 'SELECT' => 'u.user_id', + 'FROM' => array( + 'phpbb_users' => 'u', + 'phpbb_user_group' => 'ug', + ), + 'WHERE' => array('AND', + array('NOT', + array('OR', + array('ug.group_id', '=', 1), + array('ug.group_id', '=', 2), + ), + ), + array('u.user_id', '=', 'ug.user_id'), + ), + 'ORDER_BY' => 'u.user_id', + ); + $sql = $db->sql_build_query('SELECT', $sql_ary); + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array(), $db->sql_fetchrowset($result)); + } + public function test_triple_and_with_is_null() { $db = $this->new_dbal(); From e6eab35a641b8795f8a277e28d3eab0937d64b6d Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 20:29:48 +0000 Subject: [PATCH 11/18] [feature/sql-bool-builder] test_triple_and_with_in PHPBB3-13652 --- tests/dbal/boolean_processor_test.php | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php index 40efbe8d5a..081a5ac64d 100644 --- a/tests/dbal/boolean_processor_test.php +++ b/tests/dbal/boolean_processor_test.php @@ -21,6 +21,37 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/boolean_processor.xml'); } + public function test_triple_and_with_in() + { + $db = $this->new_dbal(); + + $db->sql_return_on_error(true); + + $sql_ary = array( + 'SELECT' => 'u.user_id', + 'FROM' => array( + 'phpbb_users' => 'u', + 'phpbb_user_group' => 'ug', + ), + 'WHERE' => array('AND', + array('ug.user_id', 'IN', array(1, 2, 3, 4)), + array('ug.group_id', '=', 1), + array('u.user_id', '=', 'ug.user_id'), + ), + 'ORDER_BY' => 'u.user_id', + ); + $sql = $db->sql_build_query('SELECT', $sql_ary); + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array( + array('user_id' => '1'), + array('user_id' => '2'), + array('user_id' => '3'), + ), $db->sql_fetchrowset($result), + } + public function test_double_and_with_not_of_or() { $db = $this->new_dbal(); From 5f63d685f6b0235badd7723e477754833595d045 Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 20:30:04 +0000 Subject: [PATCH 12/18] [feature/sql-bool-builder] test_and_of_or_of_and PHPBB3-13652 --- tests/dbal/boolean_processor_test.php | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php index 081a5ac64d..716e524e9b 100644 --- a/tests/dbal/boolean_processor_test.php +++ b/tests/dbal/boolean_processor_test.php @@ -21,6 +21,52 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/boolean_processor.xml'); } + public function test_and_of_or_of_and() + { + $db = $this->new_dbal(); + + $db->sql_return_on_error(true); + + $sql_ary = array( + 'SELECT' => 'u.user_id', + 'FROM' => array( + 'phpbb_users' => 'u', + 'phpbb_user_group' => 'ug', + ), + 'LEFT_JOIN' => array( + array( + 'FROM' => array( + 'phpbb_banlist' => 'b', + ), + 'ON' => 'u.user_id = b.ban_userid', + ), + ), + 'WHERE' => array('AND', + array('OR', + array('AND', + array('ug.user_id', 'IN', array(1, 2, 3, 4)), + array('ug.group_id', '=', 2), + ), + array('AND', + array('ug.group_id', '=', 1), + array('b.ban_id', 'IS NOT', NULL), + ), + ), + array('u.user_id', '=', 'ug.user_id'), + ), + 'ORDER_BY' => 'u.user_id', + ); + $sql = $db->sql_build_query('SELECT', $sql_ary); + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array( + array('user_id' => '2'), + array('user_id' => '4'), + ), $db->sql_fetchrowset($result)); + } + public function test_triple_and_with_in() { $db = $this->new_dbal(); From 1754f1832d563074e767f236cbc315eb41c7b7b9 Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 20:30:35 +0000 Subject: [PATCH 13/18] [feature/sql-bool-builder] test_single_in PHPBB3-13652 --- tests/dbal/boolean_processor_test.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php index 716e524e9b..e615edbdcd 100644 --- a/tests/dbal/boolean_processor_test.php +++ b/tests/dbal/boolean_processor_test.php @@ -21,6 +21,32 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/boolean_processor.xml'); } + public function test_single_in() + { + $db = $this->new_dbal(); + + $db->sql_return_on_error(true); + + $sql_ary = array( + 'SELECT' => 'u.user_id', + 'FROM' => array( + 'phpbb_users' => 'u', + ), + 'WHERE' => array('u.user_id', 'IN', array(3,4,5)), + 'ORDER_BY' => 'u.user_id', + ); + $sql = $db->sql_build_query('SELECT', $sql_ary); + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array( + array('user_id' => '3'), + array('user_id' => '4'), + array('user_id' => '5'), + ), $db->sql_fetchrowset($result)); + } + public function test_and_of_or_of_and() { $db = $this->new_dbal(); From d60d596f25b0c31526f469cf56c4cdb1e3698bda Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 20:31:19 +0000 Subject: [PATCH 14/18] [feature/sql-bool-builder] test_single_not_in PHPBB3-13652 --- tests/dbal/boolean_processor_test.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php index e615edbdcd..a94085b77d 100644 --- a/tests/dbal/boolean_processor_test.php +++ b/tests/dbal/boolean_processor_test.php @@ -21,6 +21,32 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/boolean_processor.xml'); } + public function test_single_not_in() + { + $db = $this->new_dbal(); + + $db->sql_return_on_error(true); + + $sql_ary = array( + 'SELECT' => 'u.user_id', + 'FROM' => array( + 'phpbb_users' => 'u', + ), + 'WHERE' => array('u.user_id', 'NOT_IN', array(3,4,5)), + 'ORDER_BY' => 'u.user_id', + ); + $sql = $db->sql_build_query('SELECT', $sql_ary); + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array( + array('user_id' => '1'), + array('user_id' => '2'), + array('user_id' => '6'), + ), $db->sql_fetchrowset($result)); + } + public function test_single_in() { $db = $this->new_dbal(); From c342531872938797f52f3e6bdfa966d81e3253ce Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 20:31:32 +0000 Subject: [PATCH 15/18] [feature/sql-bool-builder] test_single_like PHPBB3-13652 --- tests/dbal/boolean_processor_test.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php index a94085b77d..8f57a8e452 100644 --- a/tests/dbal/boolean_processor_test.php +++ b/tests/dbal/boolean_processor_test.php @@ -21,6 +21,31 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/boolean_processor.xml'); } + public function test_single_like() + { + $db = $this->new_dbal(); + + $db->sql_return_on_error(true); + + $sql_ary = array( + 'SELECT' => 'u.user_id', + 'FROM' => array( + 'phpbb_users' => 'u', + ), + 'WHERE' => array('u.username_clean', 'LIKE', 'gr' . $db->get_any_char()), + 'ORDER_BY' => 'u.user_id', + ); + $sql = $db->sql_build_query('SELECT', $sql_ary); + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array( + array('user_id' => '4'), + array('user_id' => '5'), + ), $db->sql_fetchrowset($result)); + } + public function test_single_not_in() { $db = $this->new_dbal(); From bc6ea5796dcdabd57ff9e6f7640d724df67254c5 Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 20:31:46 +0000 Subject: [PATCH 16/18] [feature/sql-bool-builder] test_single_not_like PHPBB3-13652 --- tests/dbal/boolean_processor_test.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php index 8f57a8e452..2ba3f6ff22 100644 --- a/tests/dbal/boolean_processor_test.php +++ b/tests/dbal/boolean_processor_test.php @@ -21,6 +21,33 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/boolean_processor.xml'); } + public function test_single_not_like() + { + $db = $this->new_dbal(); + + $db->sql_return_on_error(true); + + $sql_ary = array( + 'SELECT' => 'u.user_id', + 'FROM' => array( + 'phpbb_users' => 'u', + ), + 'WHERE' => array('u.username_clean', 'NOT_LIKE', 'gr' . $db->get_any_char()), + 'ORDER_BY' => 'u.user_id', + ); + $sql = $db->sql_build_query('SELECT', $sql_ary); + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array( + array('user_id' => '1'), + array('user_id' => '2'), + array('user_id' => '3'), + array('user_id' => '6'), + ), $db->sql_fetchrowset($result)); + } + public function test_single_like() { $db = $this->new_dbal(); From 576eaa0cff7a5e051aa672034e596e90f65fc1a9 Mon Sep 17 00:00:00 2001 From: brunoais Date: Mon, 16 Mar 2015 11:31:51 +0000 Subject: [PATCH 17/18] [feature/sql-bool-builder] Adding the IS operator to predicted operators PHPBB3-13652 --- phpBB/phpbb/db/driver/driver.php | 18 ++++++++++++++++++ tests/dbal/boolean_processor_test.php | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/driver/driver.php b/phpBB/phpbb/db/driver/driver.php index ce69ef6a52..4d78c84c8a 100644 --- a/phpBB/phpbb/db/driver/driver.php +++ b/phpBB/phpbb/db/driver/driver.php @@ -866,6 +866,24 @@ abstract class driver implements driver_interface break; + case 'IS_NOT': + + $condition[1] = 'IS NOT'; + + // no break + case 'IS': + + // If the value is NULL, the string of it is the empty string ('') which is not the intended result. + // this should solve that + if ($condition[2] === null) + { + $condition[2] = 'NULL'; + } + + $condition = implode(' ', $condition); + + break; + default: $condition = implode(' ', $condition); diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php index 2ba3f6ff22..5e044797f8 100644 --- a/tests/dbal/boolean_processor_test.php +++ b/tests/dbal/boolean_processor_test.php @@ -153,7 +153,7 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case ), array('AND', array('ug.group_id', '=', 1), - array('b.ban_id', 'IS NOT', NULL), + array('b.ban_id', 'IS_NOT', NULL), ), ), array('u.user_id', '=', 'ug.user_id'), From fe132f19e8d5c821fff936d208fbb981aa6ac92d Mon Sep 17 00:00:00 2001 From: brunoais Date: Sun, 15 Mar 2015 21:01:00 +0000 Subject: [PATCH 18/18] [feature/sql-bool-builder] Improved tests output to show the SQL error. PHPBB3-13652 --- tests/dbal/boolean_processor_test.php | 47 +++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/tests/dbal/boolean_processor_test.php b/tests/dbal/boolean_processor_test.php index 5e044797f8..8662485ac8 100644 --- a/tests/dbal/boolean_processor_test.php +++ b/tests/dbal/boolean_processor_test.php @@ -45,7 +45,11 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case array('user_id' => '2'), array('user_id' => '3'), array('user_id' => '6'), - ), $db->sql_fetchrowset($result)); + ), $db->sql_fetchrowset($result), + ($result === false) ? + "SQL ERROR:
" . var_export($sql, true) . "
" . $db->sql_error() : + var_export($sql, true) . ' ' . var_export($result, true) + ); } public function test_single_like() @@ -70,7 +74,11 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case $this->assertEquals(array( array('user_id' => '4'), array('user_id' => '5'), - ), $db->sql_fetchrowset($result)); + ), $db->sql_fetchrowset($result), + ($result === false) ? + "SQL ERROR:
" . var_export($sql, true) . "
" . $db->sql_error() : + var_export($sql, true) . ' ' . var_export($result, true) + ); } public function test_single_not_in() @@ -96,7 +104,11 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case array('user_id' => '1'), array('user_id' => '2'), array('user_id' => '6'), - ), $db->sql_fetchrowset($result)); + ), $db->sql_fetchrowset($result), + ($result === false) ? + "SQL ERROR:
" . var_export($sql, true) . "
" . $db->sql_error() : + var_export($sql, true) . ' ' . var_export($result, true) + ); } public function test_single_in() @@ -122,7 +134,11 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case array('user_id' => '3'), array('user_id' => '4'), array('user_id' => '5'), - ), $db->sql_fetchrowset($result)); + ), $db->sql_fetchrowset($result), + ($result === false) ? + "SQL ERROR:
" . var_export($sql, true) . "
" . $db->sql_error() : + var_export($sql, true) . ' ' . var_export($result, true) + ); } public function test_and_of_or_of_and() @@ -168,7 +184,11 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case $this->assertEquals(array( array('user_id' => '2'), array('user_id' => '4'), - ), $db->sql_fetchrowset($result)); + ), $db->sql_fetchrowset($result), + ($result === false) ? + "SQL ERROR:
" . var_export($sql, true) . "
" . $db->sql_error() : + var_export($sql, true) . ' ' . var_export($result, true) + ); } public function test_triple_and_with_in() @@ -200,6 +220,11 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case array('user_id' => '2'), array('user_id' => '3'), ), $db->sql_fetchrowset($result), + ($result === false) ? + "SQL ERROR:
" . var_export($sql, true) . "
" . $db->sql_error() : + var_export($sql, true) . ' ' . var_export($result, true) + ); + } public function test_double_and_with_not_of_or() @@ -230,7 +255,11 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case $db->sql_return_on_error(false); - $this->assertEquals(array(), $db->sql_fetchrowset($result)); + $this->assertEquals(array(), $db->sql_fetchrowset($result), + ($result === false) ? + "SQL ERROR:
" . var_export($sql, true) . "
" . $db->sql_error() : + var_export($sql, true) . ' ' . var_export($result, true) + ); } public function test_triple_and_with_is_null() @@ -268,6 +297,10 @@ class phpbb_boolean_processor_test extends phpbb_database_test_case $this->assertEquals(array( array('username' => 'helper'), array('username' => 'mass email'), - ), $db->sql_fetchrowset($result)); + ), $db->sql_fetchrowset($result), + ($result === false) ? + "SQL ERROR:
" . var_export($sql, true) . "
" . $db->sql_error() : + var_export($sql, true) . ' ' . var_export($result, true) + ); } }