Skip to content

Commit

Permalink
Add QueryBuilder::prepareBinary() method (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Nov 23, 2024
1 parent aa65e6e commit 3b65eae
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Enh #365: Refactor `Dsn` class (@Tigrov)
- Enh #366: Use constructor to create columns and initialize properties (@Tigrov)
- Enh #370: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)
- New #373: Override `QueryBuilder::prepareBinary()` method (@Tigrov)

## 1.3.0 March 21, 2024

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 PostgreSQL Server specific query builder.
*/
Expand Down Expand Up @@ -57,4 +59,9 @@ public function __construct(QuoterInterface $quoter, SchemaInterface $schema)

parent::__construct($quoter, $schema, $ddlBuilder, $dmlBuilder, $dqlBuilder, $columnDefinitionBuilder);
}

protected function prepareBinary(string $binary): string
{
return "'\x" . bin2hex($binary) . "'::bytea";
}
}
19 changes: 19 additions & 0 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,23 @@ public static function buildColumnDefinition(): array

return $values;
}

public static function prepareParam(): array
{
$values = parent::prepareParam();

$values['binary'][0] = "'\\x737472696e67'::bytea";

return $values;
}

public static function prepareValue(): array
{
$values = parent::prepareValue();

$values['binary'][0] = "'\\x737472696e67'::bytea";
$values['paramBinary'][0] = "'\\x737472696e67'::bytea";

return $values;
}
}
16 changes: 15 additions & 1 deletion tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Pgsql\Tests;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Throwable;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface;
Expand All @@ -14,6 +15,7 @@
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Pgsql\Column;
use Yiisoft\Db\Pgsql\Tests\Provider\QueryBuilderProvider;
use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Query\QueryInterface;
Expand Down Expand Up @@ -787,9 +789,21 @@ public function testOverlapsConditionOperator(iterable|ExpressionInterface $valu
$db->close();
}

/** @dataProvider \Yiisoft\Db\Pgsql\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 3b65eae

Please sign in to comment.