1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link https://www.yiiframework.com/ |
5
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
6
|
|
|
* @license https://www.yiiframework.com/license/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace yii\db\pgsql; |
10
|
|
|
|
11
|
|
|
use yii\db\ArrayExpression; |
12
|
|
|
use yii\db\ExpressionBuilderInterface; |
13
|
|
|
use yii\db\ExpressionBuilderTrait; |
14
|
|
|
use yii\db\ExpressionInterface; |
15
|
|
|
use yii\db\JsonExpression; |
16
|
|
|
use yii\db\Query; |
17
|
|
|
use yii\helpers\Json; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class JsonExpressionBuilder builds [[JsonExpression]] for PostgreSQL DBMS. |
21
|
|
|
* |
22
|
|
|
* @author Dmytro Naumenko <[email protected]> |
23
|
|
|
* @since 2.0.14 |
24
|
|
|
*/ |
25
|
|
|
class JsonExpressionBuilder implements ExpressionBuilderInterface |
26
|
|
|
{ |
27
|
|
|
use ExpressionBuilderTrait; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
* @param JsonExpression|ExpressionInterface $expression the expression to be built |
33
|
|
|
*/ |
34
|
|
|
public function build(ExpressionInterface $expression, array &$params = []) |
35
|
|
|
{ |
36
|
|
|
$value = $expression->getValue(); |
|
|
|
|
37
|
|
|
|
38
|
|
|
if ($value instanceof Query) { |
39
|
|
|
list ($sql, $params) = $this->queryBuilder->build($value, $params); |
40
|
|
|
return "($sql)" . $this->getTypecast($expression); |
41
|
|
|
} |
42
|
|
|
if ($value instanceof ArrayExpression) { |
43
|
|
|
$placeholder = 'array_to_json(' . $this->queryBuilder->buildExpression($value, $params) . ')'; |
44
|
|
|
} else { |
45
|
|
|
$placeholder = $this->queryBuilder->bindParam(Json::encode($value), $params); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $placeholder . $this->getTypecast($expression); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param JsonExpression $expression |
53
|
|
|
* @return string the typecast expression based on [[type]]. |
54
|
|
|
*/ |
55
|
|
|
protected function getTypecast(JsonExpression $expression) |
56
|
|
|
{ |
57
|
|
|
if ($expression->getType() === null) { |
58
|
|
|
return ''; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return '::' . $expression->getType(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|