类 yii\db\mssql\conditions\LikeConditionBuilder
公共方法
方法 | 描述 | 定义于 |
---|---|---|
__construct() | ExpressionBuilderTrait 构造函数。 | yii\db\ExpressionBuilderTrait |
build() | 该方法从 $expression 中构建原始 SQL,该 SQL 将不会被额外转义或引用。 | yii\db\conditions\LikeConditionBuilder |
属性详情
字符到 LIKE 条件中替换值的映射。默认情况下,它被配置为使用 `\` 来转义 `%`、`_` 和 `\`。
protected array $escapingReplacements = [
'%' => '[%]',
'_' => '[_]',
'[' => '[[]',
']' => '[]]',
'\\' => '[\]',
]
'%' => '[%]',
'_' => '[_]',
'[' => '[[]',
']' => '[]]',
'\\' => '[\]',
]
方法详情
定义于: yii\db\ExpressionBuilderTrait::__construct()
ExpressionBuilderTrait 构造函数。
public void __construct ( yii\db\QueryBuilder $queryBuilder ) | ||
$queryBuilder | yii\db\QueryBuilder |
public function __construct(QueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
}
定义于: yii\db\conditions\LikeConditionBuilder::build()
该方法从 $expression 中构建原始 SQL,该 SQL 将不会被额外转义或引用。
public string build ( yii\db\ExpressionInterface $expression, array &$params = [] ) | ||
$expression | yii\db\ExpressionInterface|yii\db\conditions\LikeCondition |
要构建的表达式。 |
$params | array |
绑定参数。 |
返回 | string |
不会被额外转义或引用的原始 SQL。 |
---|
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((string)$value, $escape) . '%'), $params);
}
$parts[] = "{$column} {$operator} {$phName}{$escapeSql}";
}
return implode($andor, $parts);
}
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];
}
注册 或 登录 以便评论。