From 4e9fa613d531a088d77a96a54c9f85aaf3705492 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sat, 23 Nov 2024 08:35:45 +0700 Subject: [PATCH] Add `QueryBuilder::prepareBinary()` method (#292) --- CHANGELOG.md | 1 + src/Builder/LikeConditionBuilder.php | 2 +- src/Command.php | 3 ++- src/QueryBuilder.php | 7 +++++++ tests/Provider/QueryBuilderProvider.php | 21 ++++++++++++++++++++- tests/QueryBuilderTest.php | 16 +++++++++++++++- 6 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0576078..36e211fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Enh #286: Use constructor to create columns and initialize properties (@Tigrov) - Enh #288: Refactor `Schema::findColumns()` method (@Tigrov) - Enh #289: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov) +- New #292: Override `QueryBuilder::prepareBinary()` method (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/Builder/LikeConditionBuilder.php b/src/Builder/LikeConditionBuilder.php index 97d03765..8652dbfe 100644 --- a/src/Builder/LikeConditionBuilder.php +++ b/src/Builder/LikeConditionBuilder.php @@ -43,7 +43,7 @@ public function build(LikeConditionInterface $expression, array &$params = []): * Different pdo_oci8 versions may or may not implement `PDO::quote()`, so {@see Quoter::quoteValue()} may or * may not quote `\`. */ - $this->escapingReplacements['\\'] = substr((string) $this->queryBuilder->quoter()->quoteValue('\\'), 1, -1); + $this->escapingReplacements['\\'] = substr($this->queryBuilder->quoter()->quoteValue('\\'), 1, -1); } return parent::build($expression, $params); diff --git a/src/Command.php b/src/Command.php index 820d197b..c167667b 100644 --- a/src/Command.php +++ b/src/Command.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Oracle; use PDO; +use Yiisoft\Db\Command\DataType; use Yiisoft\Db\Constant\PhpType; use Yiisoft\Db\Driver\Pdo\AbstractPdoCommand; use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder; @@ -64,7 +65,7 @@ public function insertWithReturningPks(string $table, array $columns): bool|arra $this->setSql($sql)->bindValues($params); $this->prepare(false); - /** @psalm-var array $returnParams */ + /** @psalm-var array $returnParams */ foreach ($returnParams as $name => &$value) { $this->bindParam($name, $value['value'], $value['dataType'], $value['size']); } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 8965a363..72b579f7 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -11,6 +11,8 @@ use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; +use function bin2hex; + /** * Implements the Oracle Server specific query builder. */ @@ -54,4 +56,9 @@ public function __construct(QuoterInterface $quoter, SchemaInterface $schema) parent::__construct($quoter, $schema, $ddlBuilder, $dmlBuilder, $dqlBuilder, $columnDefinitionBuilder); } + + protected function prepareBinary(string $binary): string + { + return "HEXTORAW('" . bin2hex($binary) . "')"; + } } diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 95da6dc4..731a7997 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -317,7 +317,7 @@ public static function buildColumnDefinition(): array $values["defaultValue('')"][0] = "varchar2(255) DEFAULT ''"; $values['defaultValue(null)'][0] = 'varchar2(255) DEFAULT NULL'; $values['defaultValue($expression)'][0] = 'number(10) DEFAULT (1 + 2)'; - $values['notNull()->defaultValue(null)'][0] = 'varchar2(255) NOT NULL'; + $values['defaultValue($emptyExpression)'][0] = 'number(10)'; $values['notNull()'][0] = 'varchar2(255) NOT NULL'; $values['null()'][0] = 'varchar2(255) NULL'; $values['integer()->primaryKey()'][0] = 'number(10) PRIMARY KEY'; @@ -337,4 +337,23 @@ public static function buildColumnDefinition(): array ['number(10) REFERENCES "ref_table" ("id") ON DELETE SET NULL', ColumnBuilder::integer()->reference($referenceSetNull)], ]; } + + public static function prepareParam(): array + { + $values = parent::prepareParam(); + + $values['binary'][0] = "HEXTORAW('737472696e67')"; + + return $values; + } + + public static function prepareValue(): array + { + $values = parent::prepareValue(); + + $values['binary'][0] = "HEXTORAW('737472696e67')"; + $values['paramBinary'][0] = "HEXTORAW('737472696e67')"; + + return $values; + } } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index e8c23a38..75ce0b61 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Oracle\Tests; +use PHPUnit\Framework\Attributes\DataProviderExternal; use Throwable; use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Exception\Exception; @@ -11,6 +12,7 @@ use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\ExpressionInterface; +use Yiisoft\Db\Oracle\Tests\Provider\QueryBuilderProvider; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; @@ -672,9 +674,21 @@ public function testSelectScalar(array|bool|float|int|string $columns, string $e parent::testSelectScalar($columns, $expected); } - /** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\QueryBuilderProvider::buildColumnDefinition() */ + #[DataProviderExternal(QueryBuilderProvider::class, 'buildColumnDefinition')] public function testBuildColumnDefinition(string $expected, ColumnSchemaInterface|string $column): void { parent::testBuildColumnDefinition($expected, $column); } + + #[DataProviderExternal(QueryBuilderProvider::class, 'prepareParam')] + public function testPrepareParam(string $expected, mixed $value, int $type): void + { + parent::testPrepareParam($expected, $value, $type); + } + + #[DataProviderExternal(QueryBuilderProvider::class, 'prepareValue')] + public function testPrepareValue(string $expected, mixed $value): void + { + parent::testPrepareValue($expected, $value); + } }