Files
yii2/framework/db/pgsql/JsonExpressionBuilder.php
Vladimir Reznichenko 1a74b3d4f8 [minor] SCA with Php Inspections (EA Ultimate) (#15871)
* Php Inspections (EA Ultimate): use type casting where applicable

* Php Inspections (EA Ultimate): use constants where applicable

* Php Inspections (EA Ultimate): CS

* Php Inspections (EA Ultimate): address some of one-time used variables

* Php Inspections (EA Ultimate): address some of performance-related findings

* Php Inspections (EA Ultimate): address some of performance-related findings

* Php Inspections (EA Ultimate): revert a constant usage

* Php Inspections (EA Ultimate): revert sequential assignments

* Php Inspections (EA Ultimate): build is green again

* Php Inspections (EA Ultimate): revert array_merge tweaks

* Php Inspections (EA Ultimate): revert BC-incompatible one-time used variable tweak

* Update description [skip ci]

* Php Inspections (EA Ultimate): CS
2018-03-12 01:37:19 +03:00

63 lines
1.7 KiB
PHP

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\pgsql;
use yii\db\ArrayExpression;
use yii\db\ExpressionBuilderInterface;
use yii\db\ExpressionBuilderTrait;
use yii\db\ExpressionInterface;
use yii\db\JsonExpression;
use yii\db\Query;
use yii\helpers\Json;
/**
* Class JsonExpressionBuilder builds [[JsonExpression]] for PostgreSQL DBMS.
*
* @author Dmytro Naumenko <d.naumenko.a@gmail.com>
* @since 2.0.14
*/
class JsonExpressionBuilder implements ExpressionBuilderInterface
{
use ExpressionBuilderTrait;
/**
* {@inheritdoc}
* @param JsonExpression|ExpressionInterface $expression the expression to be built
*/
public function build(ExpressionInterface $expression, array &$params = [])
{
$value = $expression->getValue();
if ($value instanceof Query) {
list ($sql, $params) = $this->queryBuilder->build($value, $params);
return "($sql)" . $this->getTypecast($expression);
}
if ($value instanceof ArrayExpression) {
$placeholder = 'array_to_json(' . $this->queryBuilder->buildExpression($value, $params) . ')';
} else {
$placeholder = $this->queryBuilder->bindParam(Json::encode($value), $params);
}
return $placeholder . $this->getTypecast($expression);
}
/**
* @param JsonExpression $expression
* @return string the typecast expression based on [[type]].
*/
protected function getTypecast(JsonExpression $expression)
{
if ($expression->getType() === null) {
return '';
}
return '::' . $expression->getType();
}
}