Skip to content

Commit 9294cfd

Browse files
authored
Support build and prepare enum values (#995)
1 parent d72d4d7 commit 9294cfd

4 files changed

Lines changed: 27 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
- Chg #889: Update `AbstractDMLQueryBuilder::insertBatch()` method (@Tigrov)
5353
- Enh #890: Add properties of `AbstractColumnSchema` class to constructor (@Tigrov)
5454
- New #899: Add `ColumnSchemaInterface::hasDefaultValue()` and `ColumnSchemaInterface::null()` methods (@Tigrov)
55-
- New #902, #983: Add `QueryBuilderInterface::prepareParam()` and `QueryBuilderInterface::prepareValue()` methods (@Tigrov)
55+
- New #902, #982, #983, #995: Add `prepareParam()` and `prepareValue()` methods in `QueryBuilderInterface` (@Tigrov)
5656
- Enh #902: Refactor `Quoter::quoteValue()` method (@Tigrov)
5757
- New #906: Add `ServerInfoInterface` and its implementation (@Tigrov)
5858
- Enh #905: Use `AbstractColumnDefinitionBuilder` to generate table column SQL representation (@Tigrov)
@@ -97,7 +97,7 @@
9797
- Chg #980: Add constructor with DB connection to `AbstractCommand` (@vjik)
9898
- Enh #979: Allow `ExpressionInterface` for column definitions when create table (@Tigrov)
9999
- Enh #981: Refactor constraints (@Tigrov)
100-
- Enh #982: Reduce binding parameters (@Tigrov)
100+
- Enh #982, #995: Reduce binding parameters, add `QueryBuilderInterface::buildValue()` method (@Tigrov)
101101
- New #984: Add `createQuery()` and `select()` methods to `ConnectionInterface` (@Tigrov)
102102
- Chg #985: Rename `insertWithReturningPks()` to `insertReturningPks()` in `CommandInterface` and `DMLQueryBuilderInterface` (@Tigrov)
103103
- Enh #992: Add optional type casting to `DataReaderInterface` using columns (@Tigrov)

src/QueryBuilder/AbstractQueryBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Db\QueryBuilder;
66

7+
use BackedEnum;
78
use Iterator;
89
use JsonSerializable;
910
use Stringable;
@@ -281,6 +282,9 @@ public function buildValue(mixed $value, array &$params): string
281282
$value instanceof ParamInterface => $this->bindParam($value, $params),
282283
$value instanceof ExpressionInterface => $this->buildExpression($value, $params),
283284
$value instanceof Stringable => $this->bindParam(new Param((string) $value, DataType::STRING), $params),
285+
$value instanceof BackedEnum => is_string($value->value)
286+
? $this->bindParam(new Param($value->value, DataType::STRING), $params)
287+
: (string) $value->value,
284288
$value instanceof Iterator && $value->key() === 0 => $this->buildExpression(new ArrayExpression($value), $params),
285289
$value instanceof Traversable => $this->buildExpression(new JsonExpression($value), $params),
286290
$value instanceof JsonSerializable => $this->buildExpression(new JsonExpression($value), $params),
@@ -473,6 +477,9 @@ public function prepareValue(mixed $value): string
473477
$this->buildExpression($value, $params),
474478
array_map($this->prepareValue(...), $params),
475479
),
480+
$value instanceof BackedEnum => is_string($value->value)
481+
? $this->db->getQuoter()->quoteValue($value->value)
482+
: (string) $value->value,
476483
$value instanceof Iterator && $value->key() === 0 => $this->replacePlaceholders(
477484
$this->buildExpression(new ArrayExpression($value), $params),
478485
array_map($this->prepareValue(...), $params),

tests/AbstractQueryBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2502,7 +2502,7 @@ public function testPrepareValue(string $expected, mixed $value): void
25022502
}
25032503

25042504
#[DataProviderExternal(QueryBuilderProvider::class, 'buildValue')]
2505-
public function testBuildValue(mixed $value, string $expected, array $expectedParams): void
2505+
public function testBuildValue(mixed $value, string $expected, array $expectedParams = []): void
25062506
{
25072507
$db = $this->getConnection();
25082508
$qb = $db->getQueryBuilder();

tests/Provider/QueryBuilderProvider.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
2323
use Yiisoft\Db\Schema\Column\ColumnBuilder;
2424
use Yiisoft\Db\Tests\Support\DbHelper;
25+
use Yiisoft\Db\Tests\Support\IntEnum;
2526
use Yiisoft\Db\Tests\Support\JsonSerializableObject;
2627
use Yiisoft\Db\Tests\Support\Stringable;
28+
use Yiisoft\Db\Tests\Support\StringEnum;
2729
use Yiisoft\Db\Tests\Support\TestTrait;
2830
use Yiisoft\Db\Tests\Support\TraversableObject;
2931

@@ -1753,6 +1755,8 @@ public static function prepareParam(): array
17531755
'expression' => ['(1 + 2)', new Expression('(1 + 2)'), DataType::STRING],
17541756
'expression with params' => ['(1 + 2)', new Expression('(:a + :b)', [':a' => 1, 'b' => 2]), DataType::STRING],
17551757
'Stringable' => ["'string'", new Stringable('string'), DataType::STRING],
1758+
'StringEnum' => ["'one'", StringEnum::ONE, DataType::STRING],
1759+
'IntEnum' => ['1', IntEnum::ONE, DataType::STRING],
17561760
];
17571761
}
17581762

@@ -1773,6 +1777,8 @@ public static function prepareValue(): array
17731777
'expression' => ['(1 + 2)', new Expression('(1 + 2)')],
17741778
'expression with params' => ['(1 + 2)', new Expression('(:a + :b)', [':a' => 1, 'b' => 2])],
17751779
'Stringable' => ["'string'", new Stringable('string')],
1780+
'StringEnum' => ["'one'", StringEnum::ONE],
1781+
'IntEnum' => ['1', IntEnum::ONE],
17761782
'array' => ['\'["a","b","c"]\'', ['a', 'b', 'c']],
17771783
'json' => ['\'{"a":1,"b":2}\'', ['a' => 1, 'b' => 2]],
17781784
'Iterator' => ['\'["a","b","c"]\'', new ArrayIterator(['a', 'b', 'c'])],
@@ -1784,11 +1790,11 @@ public static function prepareValue(): array
17841790
public static function buildValue(): array
17851791
{
17861792
return [
1787-
'null' => [null, 'NULL', []],
1788-
'true' => [true, 'TRUE', []],
1789-
'false' => [false, 'FALSE', []],
1790-
'integer' => [1, '1', []],
1791-
'float' => [1.1, '1.1', []],
1793+
'null' => [null, 'NULL'],
1794+
'true' => [true, 'TRUE'],
1795+
'false' => [false, 'FALSE'],
1796+
'integer' => [1, '1'],
1797+
'float' => [1.1, '1.1'],
17921798
'string' => [
17931799
'string',
17941800
':qp0',
@@ -1817,7 +1823,6 @@ public static function buildValue(): array
18171823
'expression' => [
18181824
new Expression('(1 + 2)'),
18191825
'(1 + 2)',
1820-
[],
18211826
],
18221827
'expression with params' => [
18231828
new Expression('(:a + :b)', [':a' => 1, 'b' => 2]),
@@ -1829,6 +1834,12 @@ public static function buildValue(): array
18291834
':qp0',
18301835
[':qp0' => new Param('string', DataType::STRING)],
18311836
],
1837+
'StringEnum' => [
1838+
StringEnum::ONE,
1839+
':qp0',
1840+
[':qp0' => new Param('one', DataType::STRING)],
1841+
],
1842+
'IntEnum' => [IntEnum::ONE, '1'],
18321843
'array' => [
18331844
['a', 'b', 'c'],
18341845
':qp0',

0 commit comments

Comments
 (0)