Browse Source

removed unnecessary code duplication

tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
e9e59483f4
  1. 58
      extensions/sphinx/Query.php
  2. 58
      framework/db/Query.php
  3. 70
      framework/db/QueryTrait.php

58
extensions/sphinx/Query.php

@ -800,62 +800,4 @@ class Query extends Component implements QueryInterface
->callSnippets($from, $source, $match, $this->snippetOptions)
->queryColumn();
}
/**
* Returns new condition with empty (null, empty string, blank string, or empty array) parameters removed
*
* @param array $condition original condition
* @return array condition with empty parameters removed
*/
protected function filterCondition($condition)
{
if (is_array($condition) && isset($condition[0])) {
$operator = strtoupper($condition[0]);
switch ($operator) {
case 'NOT':
case 'AND':
case 'OR':
for ($i = 1, $operandsCount = count($condition); $i < $operandsCount; $i++) {
$subCondition = $this->filterCondition($condition[$i]);
if ($this->isParameterNotEmpty($subCondition)) {
$condition[$i] = $subCondition;
} else {
unset($condition[$i]);
}
}
$operandsCount = count($condition) - 1;
if ($operator === 'NOT' && $operandsCount === 0) {
$condition = [];
} else {
// reindex array
array_splice($condition, 0, 0);
if ($operandsCount === 1) {
$condition = $condition[1];
}
}
break;
case 'IN':
case 'NOT IN':
case 'LIKE':
case 'OR LIKE':
case 'NOT LIKE':
case 'OR NOT LIKE':
if (!$this->isParameterNotEmpty($condition[2])) {
$condition = [];
}
break;
case 'BETWEEN':
case 'NOT BETWEEN':
if (!$this->isParameterNotEmpty($condition[2]) && !$this->isParameterNotEmpty($condition[3])) {
$condition = [];
}
break;
}
} else {
$condition = $this->filterHashCondition($condition);
}
return $condition;
}
}

58
framework/db/Query.php

@ -888,62 +888,4 @@ class Query extends Component implements QueryInterface
return $this;
}
/**
* Returns new condition with empty (null, empty string, blank string, or empty array) parameters removed
*
* @param array $condition original condition
* @return array condition with empty parameters removed
*/
protected function filterCondition($condition)
{
if (is_array($condition) && isset($condition[0])) {
$operator = strtoupper($condition[0]);
switch ($operator) {
case 'NOT':
case 'AND':
case 'OR':
for ($i = 1, $operandsCount = count($condition); $i < $operandsCount; $i++) {
$subCondition = $this->filterCondition($condition[$i]);
if ($this->isParameterNotEmpty($subCondition)) {
$condition[$i] = $subCondition;
} else {
unset($condition[$i]);
}
}
$operandsCount = count($condition) - 1;
if ($operator === 'NOT' && $operandsCount === 0) {
$condition = [];
} else {
// reindex array
array_splice($condition, 0, 0);
if ($operandsCount === 1) {
$condition = $condition[1];
}
}
break;
case 'IN':
case 'NOT IN':
case 'LIKE':
case 'OR LIKE':
case 'NOT LIKE':
case 'OR NOT LIKE':
if (!$this->isParameterNotEmpty($condition[2])) {
$condition = [];
}
break;
case 'BETWEEN':
case 'NOT BETWEEN':
if (!$this->isParameterNotEmpty($condition[2]) && !$this->isParameterNotEmpty($condition[3])) {
$condition = [];
}
break;
}
} else {
$condition = $this->filterHashCondition($condition);
}
return $condition;
}
}

70
framework/db/QueryTrait.php

@ -188,14 +188,63 @@ trait QueryTrait
*
* @param array $condition original condition
* @return array condition with [[isParameterNotEmpty|empty parameters]] removed.
* @throws NotSupportedException if the condition format is not supported
*/
protected function filterCondition($condition)
{
if (is_array($condition) && !isset($condition[0])) {
return $this->filterHashCondition($condition);
if (is_array($condition) && isset($condition[0])) {
$operator = strtoupper($condition[0]);
switch ($operator) {
case 'NOT':
case 'AND':
case 'OR':
for ($i = 1, $operandsCount = count($condition); $i < $operandsCount; $i++) {
$subCondition = $this->filterCondition($condition[$i]);
if ($this->isParameterNotEmpty($subCondition)) {
$condition[$i] = $subCondition;
} else {
unset($condition[$i]);
}
}
$operandsCount = count($condition) - 1;
if ($operator === 'NOT' && $operandsCount === 0) {
$condition = [];
} else {
// reindex array
array_splice($condition, 0, 0);
if ($operandsCount === 1) {
$condition = $condition[1];
}
}
break;
case 'IN':
case 'NOT IN':
case 'LIKE':
case 'OR LIKE':
case 'NOT LIKE':
case 'OR NOT LIKE':
if (!$this->isParameterNotEmpty($condition[2])) {
$condition = [];
}
break;
case 'BETWEEN':
case 'NOT BETWEEN':
if (!$this->isParameterNotEmpty($condition[2]) && !$this->isParameterNotEmpty($condition[3])) {
$condition = [];
}
break;
default:
throw new NotSupportedException("filterWhere() does not support the '$operator' operator.");
}
} elseif (is_array($condition)) {
// hash format: 'column1' => 'value1', 'column2' => 'value2', ...
return array_filter($condition, [$this, 'isParameterNotEmpty']);
} else {
throw new NotSupportedException('filterWhere() only supports hash condition format.');
throw new NotSupportedException("filterWhere() does not support plain string conditions use where() instead.");
}
return $condition;
}
/**
@ -220,21 +269,6 @@ trait QueryTrait
}
/**
* Returns a new hash condition without [[isParameterNotEmpty|empty parameters]].
*
* @param array $condition original condition
* @return array condition without [[isParameterNotEmpty|empty parameters]].
*/
protected function filterHashCondition($condition)
{
if (is_array($condition) && !isset($condition[0])) {
// hash format: 'column1' => 'value1', 'column2' => 'value2', ...
return array_filter($condition, [$this, 'isParameterNotEmpty']);
}
return $condition;
}
/**
* Sets the ORDER BY part of the query.
* @param string|array $columns the columns (and the directions) to be ordered by.
* Columns can be specified in either a string (e.g. "id ASC, name DESC") or an array

Loading…
Cancel
Save