Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QueryBuilder::prepareBinary() method #328

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Enh #323: Refactor `Dsn` class (@Tigrov)
- Enh #324: Set more specific result type in `Connection` methods `createCommand()` and `createTransaction()` (@vjik)
- Enh #326: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)
- New #328: Override `QueryBuilder::prepareBinary()` method (@Tigrov)

## 1.2.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\SchemaInterface;
use Yiisoft\Db\Sqlite\Column\ColumnDefinitionBuilder;

use function bin2hex;

/**
* Implements the SQLite Server specific query builder.
*/
Expand Down Expand Up @@ -55,4 +57,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) . "'";
}
}
4 changes: 2 additions & 2 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ private function getPragmaForeignKeyList(string $tableName): array
private function getPragmaIndexInfo(string $name): array
{
$column = $this->db
->createCommand('PRAGMA INDEX_INFO(' . (string) $this->db->getQuoter()->quoteValue($name) . ')')
->createCommand('PRAGMA INDEX_INFO(' . $this->db->getQuoter()->quoteValue($name) . ')')
->queryAll();
$column = array_map(array_change_key_case(...), $column);
DbArrayHelper::multisort($column, 'seqno');
Expand All @@ -593,7 +593,7 @@ private function getPragmaIndexList(string $tableName): array
{
/** @psalm-var IndexListInfo[] */
return $this->db
->createCommand('PRAGMA INDEX_LIST(' . (string) $this->db->getQuoter()->quoteValue($tableName) . ')')
->createCommand('PRAGMA INDEX_LIST(' . $this->db->getQuoter()->quoteValue($tableName) . ')')
->queryAll();
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,23 @@ public static function buildColumnDefinition(): array

return $values;
}

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

$values['binary'][0] = "x'737472696e67'";

return $values;
}

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

$values['binary'][0] = "x'737472696e67'";
$values['paramBinary'][0] = "x'737472696e67'";

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

use JsonException;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
Expand All @@ -18,6 +19,7 @@
use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Sqlite\Column;
use Yiisoft\Db\Sqlite\Tests\Provider\QueryBuilderProvider;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest;

Expand Down Expand Up @@ -837,9 +839,21 @@ public function testJsonOverlapsConditionOperator(iterable|ExpressionInterface $
$db->close();
}

/** @dataProvider \Yiisoft\Db\Sqlite\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);
}
}