Class yii\db\mssql\conditions\LikeConditionBuilder

Inheritanceyii\db\mssql\conditions\LikeConditionBuilder » yii\db\conditions\LikeConditionBuilder
Implementsyii\db\ExpressionBuilderInterface
Uses Traitsyii\db\ExpressionBuilderTrait
Source Code https://github.com/yiisoft/yii2/blob/master/framework/db/mssql/conditions/LikeConditionBuilder.php

Protected Properties

Hide inherited properties

Property Type Description Defined By
$escapeCharacter string|null Character used to escape special characters in LIKE conditions. yii\db\conditions\LikeConditionBuilder
$escapingReplacements array Map of chars to their replacements in LIKE conditions. yii\db\mssql\conditions\LikeConditionBuilder
$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\conditions\LikeConditionBuilder

Protected Methods

Hide inherited methods

Method Description Defined By
parseOperator() yii\db\conditions\LikeConditionBuilder

Property Details

Hide inherited properties

$escapingReplacements protected property

Map of chars to their replacements in LIKE conditions. By default it's configured to escape %, _ and \ with \.

protected array $escapingReplacements = [
    
'%' => '[%]',
    
'_' => '[_]',
    
'[' => '[[]',
    
']' => '[]]',
    
'\\' => '[\]',
]

Method Details

Hide inherited methods

__construct() public method

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

ExpressionBuilderTrait constructor.

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

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

            
build() public method

Defined in: yii\db\conditions\LikeConditionBuilder::build()

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

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

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 = [])
{
    $operator = strtoupper($expression->getOperator());
    $column = $expression->getColumn();
    $values = $expression->getValue();
    $escape = $expression->getEscapingReplacements();
    if ($escape === null || $escape === []) {
        $escape = $this->escapingReplacements;
    }
    list($andor, $not, $operator) = $this->parseOperator($operator);
    if (!is_array($values)) {
        $values = [$values];
    }
    if (empty($values)) {
        return $not ? '' : '0=1';
    }
    if ($column instanceof ExpressionInterface) {
        $column = $this->queryBuilder->buildExpression($column, $params);
    } elseif (is_string($column) && strpos($column, '(') === false) {
        $column = $this->queryBuilder->db->quoteColumnName($column);
    }
    $escapeSql = $this->getEscapeSql();
    $parts = [];
    foreach ($values as $value) {
        if ($value instanceof ExpressionInterface) {
            $phName = $this->queryBuilder->buildExpression($value, $params);
        } else {
            $phName = $this->queryBuilder->bindParam(empty($escape) ? $value : ('%' . strtr($value, $escape) . '%'), $params);
        }
        $parts[] = "{$column} {$operator} {$phName}{$escapeSql}";
    }
    return implode($andor, $parts);
}

            
parseOperator() protected method
protected array parseOperator ( $operator )
$operator string

                protected function parseOperator($operator)
{
    if (!preg_match('/^(AND |OR |)(((NOT |))I?LIKE)/', $operator, $matches)) {
        throw new InvalidArgumentException("Invalid operator '$operator'.");
    }
    $andor = ' ' . (!empty($matches[1]) ? $matches[1] : 'AND ');
    $not = !empty($matches[3]);
    $operator = $matches[2];
    return [$andor, $not, $operator];
}