1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii\db\conditions; |
4
|
|
|
|
5
|
|
|
use yii\db\ExpressionBuilderInterface; |
6
|
|
|
use yii\db\ExpressionBuilderTrait; |
7
|
|
|
use yii\db\ExpressionInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ConjunctionConditionBuilder builds objects of abstract class [[ConjunctionCondition]] |
11
|
|
|
* |
12
|
|
|
* @author Dmytro Naumenko <[email protected]> |
13
|
|
|
* @since 2.0.14 |
14
|
|
|
*/ |
15
|
|
|
class ConjunctionConditionBuilder implements ExpressionBuilderInterface |
16
|
|
|
{ |
17
|
|
|
use ExpressionBuilderTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Method builds the raw SQL from the $expression that will not be additionally |
21
|
|
|
* escaped or quoted. |
22
|
|
|
* |
23
|
|
|
* @param ExpressionInterface|ConjunctionCondition $condition the expression to be built. |
24
|
|
|
* @param array $params the binding parameters. |
25
|
|
|
* @return string the raw SQL that will not be additionally escaped or quoted. |
26
|
|
|
*/ |
27
|
227 |
|
public function build(ExpressionInterface $condition, array &$params = []) |
28
|
|
|
{ |
29
|
227 |
|
$parts = $this->buildExpressionsFrom($condition, $params); |
30
|
|
|
|
31
|
227 |
|
if (empty($parts)) { |
32
|
|
|
return ''; |
33
|
|
|
} |
34
|
|
|
|
35
|
227 |
|
if (count($parts) === 1) { |
36
|
92 |
|
return reset($parts); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
153 |
|
return '(' . implode(") {$condition->getOperator()} (", $parts) . ')'; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Builds expressions, that are stored in $condition |
44
|
|
|
* |
45
|
|
|
* @param ExpressionInterface|ConjunctionCondition $condition the expression to be built. |
46
|
|
|
* @param array $params the binding parameters. |
47
|
|
|
* @return string[] |
48
|
|
|
*/ |
49
|
227 |
|
private function buildExpressionsFrom(ExpressionInterface $condition, &$params = []) |
50
|
|
|
{ |
51
|
227 |
|
$parts = []; |
52
|
227 |
|
foreach ($condition->getExpressions() as $condition) { |
53
|
227 |
|
if (is_array($condition)) { |
54
|
202 |
|
$condition = $this->queryBuilder->buildCondition($condition, $params); |
55
|
|
|
} |
56
|
227 |
|
if ($condition instanceof ExpressionInterface) { |
57
|
9 |
|
$condition = $this->queryBuilder->buildExpression($condition, $params); |
58
|
|
|
} |
59
|
227 |
|
if ($condition !== '') { |
60
|
227 |
|
$parts[] = $condition; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
227 |
|
return $parts; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|