0

Class yii\db\ExpressionBuilder

Inheritanceyii\db\ExpressionBuilder
Implementsyii\db\ExpressionBuilderInterface
Uses Traitsyii\db\ExpressionBuilderTrait
Available since version2.0.14
Source Code https://github.com/yiisoft/yii2/blob/master/framework/db/ExpressionBuilder.php

Class ExpressionBuilder builds objects of yii\db\Expression class.

Protected Properties

Hide inherited properties

Property Type Description Defined By
$queryBuilder yii\db\QueryBuilder yii\db\ExpressionBuilderTrait

Public Methods

Hide inherited methods

Method Description Defined By
__construct() ExpressionBuilderTrait constructor. yii\db\ExpressionBuilderTrait
build() Method builds the raw SQL from the $expression that will not be additionally escaped or quoted. yii\db\ExpressionBuilder

Method Details

Hide inherited methods

__construct() public method

Defined in: yii\db\ExpressionBuilderTrait::__construct()

ExpressionBuilderTrait constructor.

public mixed __construct ( yii\db\QueryBuilder $queryBuilder )
$queryBuilder yii\db\QueryBuilder

                public function __construct(QueryBuilder $queryBuilder)
{
    $this->queryBuilder = $queryBuilder;
}

            
build() public method

Method builds the raw SQL from the $expression that will not be additionally escaped or quoted.

public string build ( yii\db\Expression|yii\db\ExpressionInterface $expression, array &$params = [] )
$expression yii\db\Expression|yii\db\ExpressionInterface

The expression to be built

$params array

The binding parameters.

return string

The raw SQL that will not be additionally escaped or quoted.

                public function build(ExpressionInterface $expression, array &$params = [])
{
    $newSql = $expression->__toString();
    if ($expression->params === []) {
        return $newSql;
    }
    $newParams = $expression->params;
    $duplicateKeys = array_filter(
        $newParams,
        static function ($key) use ($params) {
            $keyWithoutColon = ltrim((string)$key, ':');
            $keyWithColon = ':' . $keyWithoutColon;
            // the key could already exist with or without the leading colon, so look for both
            return (array_key_exists($keyWithoutColon, $params))
                || (array_key_exists($keyWithColon, $params));
        },
        ARRAY_FILTER_USE_KEY
    );
    foreach (array_keys($duplicateKeys) as $duplicateKey) {
        $duplicateKeyWithoutColon = ltrim((string)$duplicateKey, ':');
        $duplicateKeyWithColon = ':' . $duplicateKeyWithoutColon;
        // use an arbitrary key to avoid clashing
        $suffix = count($params);
        do {
            $newKeyWithoutColon = $duplicateKeyWithoutColon . $suffix++;
            $newKey = ':' . $newKeyWithoutColon;
        } while (
            array_key_exists($newKey, $params)
            || array_key_exists($newKey, $newParams)
            || array_key_exists($newKeyWithoutColon, $params)
            || array_key_exists($newKeyWithoutColon, $newParams)
        );
        $newParams[$newKey] = $newParams[$duplicateKey];
        $pattern = '~'
            . "('(?:''|\\\\'|[^'])*'"                                    // single-quoted string
            . '|"(?:""|\\\\"|[^"])*"'                                    // double-quoted string / identifier
            . '|`(?:``|[^`])*`'                                          // backtick identifier
            . '|\[(?:\]\]|[^\]])*\]'                                 // bracket-quoted identifier
            . '|(?<dollar>\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$).*?\k<dollar>' // PostgreSQL dollar-quoted string
            . '|--[^\r\n]*'                                              // line comment
            . '|/\*.*?\*/'                                               // block comment
            . ')(*SKIP)(*F)'
            . '|(?<!:)' . preg_quote($duplicateKeyWithColon, '~') . '(?![A-Za-z0-9_])'
            . '~s';
        $newSql = preg_replace($pattern, $newKey, $newSql);
        unset($newParams[$duplicateKey]);
    }
    $params = array_merge($params, $newParams);
    return $newSql;
}