Skip to content

Commit

Permalink
Add QueryBuilder::prepareBinary() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Nov 20, 2024
1 parent 66bcb27 commit ffcb26e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Builder/LikeConditionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function insertWithReturningPks(string $table, array $columns): bool|arra
$this->setSql($sql)->bindValues($params);
$this->prepare(false);

/** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */
/** @psalm-var array<string, array{column: string, value: mixed, dataType: DataType::*, size: int}> $returnParams */
foreach ($returnParams as $name => &$value) {
$this->bindParam($name, $value['value'], $value['dataType'], $value['size']);
}
Expand Down
7 changes: 7 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;

use function bin2hex;

/**
* Implements the Oracle Server specific query builder.
*/
Expand Down Expand Up @@ -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) . "')";
}
}
20 changes: 19 additions & 1 deletion tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ 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['notNull()'][0] = 'varchar2(255) NOT NULL';
$values['null()'][0] = 'varchar2(255) NULL';
$values['integer()->primaryKey()'][0] = 'number(10) PRIMARY KEY';
Expand All @@ -337,4 +336,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;
}
}
16 changes: 15 additions & 1 deletion tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Yiisoft\Db\Oracle\Tests;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Throwable;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit ffcb26e

Please sign in to comment.