[feature/sql-bool-builder] Changing syntax pt3. Don't use magic numbers

PHPBB3-13652
This commit is contained in:
brunoais 2015-11-11 08:35:28 +00:00
parent 062352e312
commit 9725f5757e

View file

@ -66,6 +66,15 @@ abstract class driver implements driver_interface
*/ */
var $sql_server_version = false; var $sql_server_version = false;
const LOGICAL_OP = 0;
const STATEMENTS = 1;
const LEFT_STMT = 0;
const COMPARE_OP = 1;
const RIGHT_STMT = 2;
const SUBQUERY_OP = 3;
const SUBQUERY_SELECT_TYPE = 4;
const SUBQUERY_BUILD = 5;
/** /**
* Constructor * Constructor
*/ */
@ -809,8 +818,8 @@ abstract class driver implements driver_interface
{ {
// In cases where an array exists but there is no head condition, // 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. // it should be because there's only 1 WHERE clause. This seems the best way to deal with it.
if ($operations_ary[0] !== 'AND' && if ($operations_ary[self::LOGICAL_OP] !== 'AND' &&
$operations_ary[0] !== 'OR') $operations_ary[self::LOGICAL_OP] !== 'OR')
{ {
$operations_ary = array('AND', array($operations_ary)); $operations_ary = array('AND', array($operations_ary));
} }
@ -819,11 +828,11 @@ abstract class driver implements driver_interface
protected function _process_boolean_tree($operations_ary) protected function _process_boolean_tree($operations_ary)
{ {
$operation = $operations_ary[0]; $operation = $operations_ary[self::LOGICAL_OP];
foreach ($operations_ary[1] as &$condition) foreach ($operations_ary[1] as &$condition)
{ {
switch ($condition[0]) switch ($condition[self::LOGICAL_OP])
{ {
case 'AND': case 'AND':
case 'OR': case 'OR':
@ -844,40 +853,40 @@ abstract class driver implements driver_interface
case 3: case 3:
// Typical 3 element clause with {left hand} {operator} {right hand} // Typical 3 element clause with {left hand} {operator} {right hand}
switch ($condition[1]) switch ($condition[self::COMPARE_OP])
{ {
case 'IN': case 'IN':
case 'NOT_IN': case 'NOT_IN':
// As this is used with an IN, assume it is a set of elements for sql_in_set() // 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); $condition = $this->sql_in_set($condition[self::LEFT_STMT], $condition[self::RIGHT_STMT], $condition[Cself::OMPARE_OP] === 'NOT_IN', true);
break; break;
case 'LIKE': case 'LIKE':
$condition = $condition[0] . ' ' . $this->sql_like_expression($condition[2]) . ' '; $condition = $condition[self::LEFT_STMT] . ' ' . $this->sql_like_expression($condition[self::RIGHT_STMT]) . ' ';
break; break;
case 'NOT_LIKE': case 'NOT_LIKE':
$condition = $condition[0] . ' ' . $this->sql_not_like_expression($condition[2]) . ' '; $condition = $condition[self::LEFT_STMT] . ' ' . $this->sql_not_like_expression($condition[self::RIGHT_STMT]) . ' ';
break; break;
case 'IS_NOT': case 'IS_NOT':
$condition[1] = 'IS NOT'; $condition[self::COMPARE_OP] = 'IS NOT';
// no break // no break
case 'IS': case 'IS':
// If the value is NULL, the string of it is the empty string ('') which is not the intended result. // If the value is NULL, the string of it is the empty string ('') which is not the intended result.
// this should solve that // this should solve that
if ($condition[2] === null) if ($condition[self::RIGHT_STMT] === null)
{ {
$condition[2] = 'NULL'; $condition[self::RIGHT_STMT] = 'NULL';
} }
$condition = implode(' ', $condition); $condition = implode(' ', $condition);
@ -897,8 +906,8 @@ abstract class driver implements driver_interface
// Subquery with {left hand} {operator} {compare kind} {SELECT Kind } {Sub Query} // Subquery with {left hand} {operator} {compare kind} {SELECT Kind } {Sub Query}
$condition = $condition[0] . ' ' . $condition[1] . ' ' . $condition[2] . ' ( '; $condition = $condition[self::LEFT_STMT] . ' ' . $condition[self::COMPARE_OP] . ' ' . $condition[self::SUBQUERY_OP] . ' ( ';
$condition .= $this->sql_build_query($condition[3], $condition[4]); $condition .= $this->sql_build_query($condition[self::SUBQUERY_SELECT_TYPE], $condition[self::SUBQUERY_BUILD]);
$condition .= ' )'; $condition .= ' )';
break; break;
@ -917,11 +926,11 @@ abstract class driver implements driver_interface
if ($operation === 'NOT') if ($operation === 'NOT')
{ {
$operations_ary = implode("", $operations_ary[1]); $operations_ary = implode("", $operations_ary[self::STATEMENTS]);
} }
else else
{ {
$operations_ary = implode(" \n $operation ", $operations_ary[1]); $operations_ary = implode(" \n $operation ", $operations_ary[self::STATEMENTS]);
} }
return $operations_ary; return $operations_ary;