mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-03 22:32:40 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			49 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/**
 | 
						|
 * @link http://www.yiiframework.com/
 | 
						|
 * @copyright Copyright (c) 2008 Yii Software LLC
 | 
						|
 * @license http://www.yiiframework.com/license/
 | 
						|
 */
 | 
						|
 | 
						|
namespace yii\db\mysql;
 | 
						|
 | 
						|
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 MySQL DBMS.
 | 
						|
 *
 | 
						|
 * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
 | 
						|
 * @since 2.0.14
 | 
						|
 */
 | 
						|
class JsonExpressionBuilder implements ExpressionBuilderInterface
 | 
						|
{
 | 
						|
    use ExpressionBuilderTrait;
 | 
						|
 | 
						|
    const PARAM_PREFIX = ':qp';
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * {@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)";
 | 
						|
        }
 | 
						|
 | 
						|
        $placeholder = static::PARAM_PREFIX . count($params);
 | 
						|
        $params[$placeholder] = Json::encode($value);
 | 
						|
 | 
						|
        return "CAST($placeholder AS JSON)";
 | 
						|
    }
 | 
						|
}
 |