Skip to content

Commit 0435c5b

Browse files
authored
Add ColumnName and Value expressions. Remove BetweenColumns condition (#1024)
1 parent df87261 commit 0435c5b

File tree

17 files changed

+136
-360
lines changed

17 files changed

+136
-360
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,12 @@
121121
- Enh #1016: Refactor `AbstractDMLQueryBuilder::getTableUniqueColumnNames()` method (@Tigrov)
122122
- New #1015: Add mode parameter to `Like` condition (@vjik)
123123
- Chg #1019: Split `In` condition to `In` and `NotIn` (@vjik)
124-
- Chg #1018: Split `BetweenColumns` condition to `BetweenColumns` and `NotBetweenColumns` (@vjik)
124+
- Chg #1018, #1024: Remove `BetweenColumns` in favor `Between` with `ColumnName` usage (@vjik)
125125
- Chg #1017: Split `Between` condition to `Between` and `NotBetween` (@vjik)
126126
- New #1020: Support column's collation (@Tigrov)
127127
- Chg #1021: Move conjunction type from operator string value to `Like` condition constructor parameter (@vjik)
128128
- Chg #1023: Split `Like` condition to `Like` and `NotLike` (@vjik)
129+
- New #1024: Add `ColumnName` and `Value` expressions (@vjik)
129130

130131
## 1.3.0 March 21, 2024
131132

UPGRADE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,4 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace
268268
- Remove `AbstractDsn` and `AbstractDsnSocket` classes and `DsnInterface` interface;
269269
- Remove `Hash` condition;
270270
- Remove `AbstractTableSchema` and add `TableSchema` instead;
271+
- Remove `BetweenColumns` condition;

docs/guide/en/query/where.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,17 @@ Operand 1 should be the column name, and operand 2 and 3 should be the starting
139139

140140
For example, `['between', 'id', 1,10]` will generate `id BETWEEN 1 AND 10`.
141141

142-
In case you need to build a condition where value is between two columns `(like 11 BETWEEN min_id AND max_id)`,
142+
You can also use `Yiisoft\Db\Expression\ColumnName` and `Yiisoft\Db\Expression\Value` expressions:
143143

144-
you should use `Yiisoft\Db\QueryBuilder\Condition\BetweenColumnsCondition`.
144+
```php
145+
use Yiisoft\Db\Expression\ColumnName;
146+
use Yiisoft\Db\Expression\Value;
147+
148+
['between', new Value('2025-08-11'), new ColumnName('start_date'), new ColumnName('end_date')]
149+
```
150+
151+
The `ColumnName` expression ensures proper quoting of column names, while `Value` expressions ensure proper parameter
152+
binding for values.
145153

146154
### not between
147155

docs/guide/pt-BR/query/where.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ O operando 1 deve ser o nome da coluna e os operandos 2 e 3 devem ser os valores
139139

140140
Por exemplo, `['between', 'id', 1,10]` irá gerar `id BETWEEN 1 AND 10`.
141141

142-
Caso você precise construir uma condição onde o valor esteja entre duas colunas `(like 11 BETWEEN min_id AND max_id)`,
143-
144-
você deve usar `Yiisoft\Db\QueryBuilder\Condition\BetweenColumnsCondition`.
145-
146142
### not between
147143

148144
Semelhante a `between` exceto que `BETWEEN` é substituído por `NOT BETWEEN` na condição gerada.

src/Expression/ColumnName.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Expression;
6+
7+
/**
8+
* Represents a column name expression for SQL queries.
9+
*
10+
* This class encapsulates a column name that will be properly quoted when building SQL queries.
11+
*/
12+
final class ColumnName implements ExpressionInterface
13+
{
14+
/**
15+
* @param string $name The column name.
16+
*/
17+
public function __construct(
18+
public readonly string $name,
19+
) {
20+
}
21+
}

src/Expression/ColumnNameBuilder.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Expression;
6+
7+
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
8+
9+
/**
10+
* Builder for {@see ColumnName} expressions.
11+
*
12+
* This builder takes {@see ColumnName} expressions and converts them into properly quoted column names suitable for
13+
* inclusion in SQL statements using the database-specific quoting rules.
14+
*
15+
* @implements ExpressionBuilderInterface<ColumnName>
16+
*/
17+
final class ColumnNameBuilder implements ExpressionBuilderInterface
18+
{
19+
/**
20+
* @param QueryBuilderInterface $queryBuilder The query builder instance.
21+
*/
22+
public function __construct(
23+
private readonly QueryBuilderInterface $queryBuilder,
24+
) {
25+
}
26+
27+
public function build(ExpressionInterface $expression, array &$params = []): string
28+
{
29+
return $this->queryBuilder->getQuoter()->quoteColumnName($expression->name);
30+
}
31+
}

src/Expression/Value.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Expression;
6+
7+
/**
8+
* Represents a value for SQL queries.
9+
*
10+
* This class encapsulates any type of value that needs to be properly converted and bound as a parameter in SQL
11+
* statements.
12+
*/
13+
final class Value implements ExpressionInterface
14+
{
15+
/**
16+
* @param mixed $value The value to be used in the SQL query.
17+
*/
18+
public function __construct(
19+
public readonly mixed $value,
20+
) {
21+
}
22+
}

src/Expression/ValueBuilder.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Expression;
6+
7+
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
8+
9+
/**
10+
* Builder for {@see Value} expressions that converts values into SQL parameters.
11+
*
12+
* This builder takes {@see Value} expressions and converts them into properly formatted SQL parameter placeholders
13+
* while adding the actual values to the parameters array for safe binding during query execution.
14+
*
15+
* @implements ExpressionBuilderInterface<Value>
16+
*/
17+
final class ValueBuilder implements ExpressionBuilderInterface
18+
{
19+
/**
20+
* @param QueryBuilderInterface $queryBuilder The query builder instance.
21+
*/
22+
public function __construct(
23+
private readonly QueryBuilderInterface $queryBuilder,
24+
) {
25+
}
26+
27+
public function build(ExpressionInterface $expression, array &$params = []): string
28+
{
29+
return $this->queryBuilder->buildValue($expression->value, $params);
30+
}
31+
}

src/QueryBuilder/AbstractDQLQueryBuilder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Yiisoft\Db\Exception\NotSupportedException;
1313
use Yiisoft\Db\Expression\ArrayExpression;
1414
use Yiisoft\Db\Expression\ArrayExpressionBuilder;
15+
use Yiisoft\Db\Expression\ColumnName;
16+
use Yiisoft\Db\Expression\ColumnNameBuilder;
1517
use Yiisoft\Db\Expression\Expression;
1618
use Yiisoft\Db\Expression\ExpressionBuilder;
1719
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
@@ -22,6 +24,8 @@
2224
use Yiisoft\Db\Expression\CaseExpressionBuilder;
2325
use Yiisoft\Db\Expression\StructuredExpression;
2426
use Yiisoft\Db\Expression\StructuredExpressionBuilder;
27+
use Yiisoft\Db\Expression\Value;
28+
use Yiisoft\Db\Expression\ValueBuilder;
2529
use Yiisoft\Db\QueryBuilder\Condition\ConditionInterface;
2630
use Yiisoft\Db\QueryBuilder\Condition\Simple;
2731
use Yiisoft\Db\Query\Query;
@@ -558,12 +562,12 @@ protected function defaultExpressionBuilders(): array
558562
Condition\Equals::class => Condition\Builder\EqualsBuilder::class,
559563
Condition\Exists::class => Condition\Builder\ExistsBuilder::class,
560564
Simple::class => Condition\Builder\SimpleBuilder::class,
561-
Condition\BetweenColumns::class => Condition\Builder\BetweenColumnsBuilder::class,
562-
Condition\NotBetweenColumns::class => Condition\Builder\BetweenColumnsBuilder::class,
563565
JsonExpression::class => JsonExpressionBuilder::class,
564566
ArrayExpression::class => ArrayExpressionBuilder::class,
565567
StructuredExpression::class => StructuredExpressionBuilder::class,
566568
CaseExpression::class => CaseExpressionBuilder::class,
569+
ColumnName::class => ColumnNameBuilder::class,
570+
Value::class => ValueBuilder::class,
567571
];
568572
}
569573

src/QueryBuilder/Condition/AbstractBetweenColumns.php

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)