|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\QueryBuilder\Conditions\Builder; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
8
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
|
9
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
10
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
|
11
|
|
|
use Yiisoft\Db\Expression\ExpressionBuilderInterface; |
|
12
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
|
13
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
|
14
|
|
|
use Yiisoft\Db\QueryBuilder\Conditions\Interface\BetweenColumnsConditionInterface; |
|
15
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
|
16
|
|
|
|
|
17
|
|
|
use function str_contains; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class BetweenColumnsConditionBuilder builds objects of {@see BetweenColumnsCondition}. |
|
21
|
|
|
*/ |
|
22
|
|
|
class BetweenColumnsConditionBuilder implements ExpressionBuilderInterface |
|
23
|
|
|
{ |
|
24
|
|
|
public function __construct(private QueryBuilderInterface $queryBuilder) |
|
25
|
|
|
{ |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function build(BetweenColumnsConditionInterface $expression, array &$params = []): string |
|
32
|
|
|
{ |
|
33
|
|
|
$operator = $expression->getOperator(); |
|
34
|
|
|
$startColumn = $this->escapeColumnName($expression->getIntervalStartColumn(), $params); |
|
35
|
|
|
$endColumn = $this->escapeColumnName($expression->getIntervalEndColumn(), $params); |
|
36
|
|
|
$value = $this->createPlaceholder($expression->getValue(), $params); |
|
37
|
|
|
return "$value $operator $startColumn AND $endColumn"; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Attaches $value to $params array and returns placeholder. |
|
42
|
|
|
* |
|
43
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function createPlaceholder(mixed $value, array &$params): string |
|
46
|
|
|
{ |
|
47
|
|
|
if ($value instanceof ExpressionInterface) { |
|
48
|
|
|
return $this->queryBuilder->buildExpression($value, $params); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->queryBuilder->bindParam($value, $params); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Prepares column name to be used in SQL statement. |
|
56
|
|
|
* |
|
57
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function escapeColumnName( |
|
60
|
|
|
ExpressionInterface|QueryInterface|string $columnName, |
|
61
|
|
|
array &$params = [] |
|
62
|
|
|
): string { |
|
63
|
|
|
if ($columnName instanceof QueryInterface) { |
|
64
|
|
|
[$sql, $params] = $this->queryBuilder->build($columnName, $params); |
|
65
|
|
|
return "($sql)"; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($columnName instanceof ExpressionInterface) { |
|
69
|
|
|
return $this->queryBuilder->buildExpression($columnName, $params); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (!str_contains($columnName, '(')) { |
|
73
|
|
|
return $this->queryBuilder->quoter()->quoteColumnName($columnName); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $columnName; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|