Skip to content

Commit

Permalink
Update according to changes in db (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Dec 8, 2024
1 parent 47ded78 commit c80dd31
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Enh #361, #362: Refactor `Schema::findColumns()` method (@Tigrov)
- Enh #363: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)
- Enh #366: Refactor `Quoter::quoteValue()` method (@Tigrov)
- Chg #368: Update `QueryBuilder` constructor (@Tigrov)

## 1.2.0 March 21, 2024

Expand Down
13 changes: 5 additions & 8 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,11 @@ public function createTransaction(): TransactionInterface

public function getQueryBuilder(): QueryBuilderInterface
{
if ($this->queryBuilder === null) {
$this->queryBuilder = new QueryBuilder(
$this->getQuoter(),
$this->getSchema(),
);
}

return $this->queryBuilder;
return $this->queryBuilder ??= new QueryBuilder(
$this->getQuoter(),
$this->getSchema(),
$this->getServerInfo(),
);
}

public function getQuoter(): QuoterInterface
Expand Down
22 changes: 12 additions & 10 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Mysql;

use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Mysql\Column\ColumnDefinitionBuilder;
Expand Down Expand Up @@ -46,15 +47,16 @@ final class QueryBuilder extends AbstractQueryBuilder
PseudoType::UUID_PK => 'binary(16) PRIMARY KEY',
];

public function __construct(
QuoterInterface $quoter,
SchemaInterface $schema,
) {
$ddlBuilder = new DDLQueryBuilder($this, $quoter, $schema);
$dmlBuilder = new DMLQueryBuilder($this, $quoter, $schema);
$dqlBuilder = new DQLQueryBuilder($this, $quoter);
$columnDefinitionBuilder = new ColumnDefinitionBuilder($this);

parent::__construct($quoter, $schema, $ddlBuilder, $dmlBuilder, $dqlBuilder, $columnDefinitionBuilder);
public function __construct(QuoterInterface $quoter, SchemaInterface $schema, ServerInfoInterface $serverInfo)
{
parent::__construct(
$quoter,
$schema,
$serverInfo,
new DDLQueryBuilder($this, $quoter, $schema),
new DMLQueryBuilder($this, $quoter, $schema),
new DQLQueryBuilder($this, $quoter),
new ColumnDefinitionBuilder($this),
);
}
}
2 changes: 1 addition & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected function findColumns(TableSchemaInterface $table): bool
}

$jsonColumns = $this->getJsonColumns($table);
$isMariaDb = str_contains($this->db->getServerVersion(), 'MariaDB');
$isMariaDb = str_contains($this->db->getServerInfo()->getVersion(), 'MariaDB');

foreach ($columns as $info) {
$info = array_change_key_case($info);
Expand Down
4 changes: 2 additions & 2 deletions tests/JsonCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testInsertAndSelect(): void

$command = $db->createCommand();
$command->insert('storage', ['data' => ['a' => 1, 'b' => 2]])->execute();
$rowExpected = match (str_contains($db->getServerVersion(), 'MariaDB')) {
$rowExpected = match (str_contains($db->getServerInfo()->getVersion(), 'MariaDB')) {
true => '{"a":1,"b":2}',
default => '{"a": 1, "b": 2}',
};
Expand All @@ -65,7 +65,7 @@ public function testInsertJsonExpressionAndSelect(): void

$command = $db->createCommand();
$command->insert('storage', ['data' => new JsonExpression(['a' => 1, 'b' => 2])])->execute();
$rowExpected = match (str_contains($db->getServerVersion(), 'MariaDB')) {
$rowExpected = match (str_contains($db->getServerInfo()->getVersion(), 'MariaDB')) {
true => '{"a":1,"b":2}',
default => '{"a": 1, "b": 2}',
};
Expand Down
25 changes: 15 additions & 10 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ public function testAddcheck(): void
public function testAddCommentOnColumn(): void
{
$db = $this->getConnection(true);
$serverVersion = $db->getServerInfo()->getVersion();

$qb = $db->getQueryBuilder();
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int NOT NULL AUTO_INCREMENT COMMENT 'Primary key.'
SQL;

if (!str_contains($db->getServerVersion(), 'MariaDB') && version_compare($db->getServerVersion(), '8', '<')) {
if (!str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '8', '<')) {
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key.'
SQL;
} elseif (str_contains($db->getServerVersion(), 'MariaDB')) {
} elseif (str_contains($serverVersion, 'MariaDB')) {
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key.'
SQL;
Expand Down Expand Up @@ -344,17 +345,18 @@ public function testDefaultValues(): void
public function testDropCommentFromColumn(): void
{
$db = $this->getConnection(true);
$serverVersion = $db->getServerInfo()->getVersion();

$qb = $db->getQueryBuilder();
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int NOT NULL AUTO_INCREMENT COMMENT ''
SQL;

if (!str_contains($db->getServerVersion(), 'MariaDB') && version_compare($db->getServerVersion(), '8', '<')) {
if (!str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '8', '<')) {
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ''
SQL;
} elseif (str_contains($db->getServerVersion(), 'MariaDB')) {
} elseif (str_contains($serverVersion, 'MariaDB')) {
$sql = <<<SQL
ALTER TABLE `customer` CHANGE `id` `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ''
SQL;
Expand Down Expand Up @@ -666,10 +668,11 @@ public function testSelectScalar(array|bool|float|int|string $columns, string $e
public function testJsonOverlapsConditionBuilder(): void
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (str_contains($db->getServerVersion(), 'MariaDB') && version_compare($db->getServerVersion(), '10.9', '<')) {
if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) {
self::markTestSkipped('MariaDB < 10.9 does not support JSON_OVERLAPS() function.');
} elseif (version_compare($db->getServerVersion(), '8', '<')) {
} elseif (version_compare($serverVersion, '8', '<')) {
self::markTestSkipped('MySQL < 8 does not support JSON_OVERLAPS() function.');
}

Expand All @@ -695,10 +698,11 @@ public function testJsonOverlapsConditionBuilder(): void
public function testJsonOverlapsCondition(iterable|ExpressionInterface $values, int $expectedCount): void
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (str_contains($db->getServerVersion(), 'MariaDB') && version_compare($db->getServerVersion(), '10.9', '<')) {
if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) {
self::markTestSkipped('MariaDB < 10.9 does not support JSON_OVERLAPS() function.');
} elseif (version_compare($db->getServerVersion(), '8', '<')) {
} elseif (version_compare($serverVersion, '8', '<')) {
self::markTestSkipped('MySQL < 8 does not support JSON_OVERLAPS() function.');
}

Expand All @@ -716,10 +720,11 @@ public function testJsonOverlapsCondition(iterable|ExpressionInterface $values,
public function testJsonOverlapsConditionOperator(iterable|ExpressionInterface $values, int $expectedCount): void
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (str_contains($db->getServerVersion(), 'MariaDB') && version_compare($db->getServerVersion(), '10.9', '<')) {
if (str_contains($serverVersion, 'MariaDB') && version_compare($serverVersion, '10.9', '<')) {
self::markTestSkipped('MariaDB < 10.9 does not support JSON_OVERLAPS() function.');
} elseif (version_compare($db->getServerVersion(), '8', '<')) {
} elseif (version_compare($serverVersion, '8', '<')) {
self::markTestSkipped('MySQL < 8 does not support JSON_OVERLAPS() function.');
}

Expand Down
10 changes: 6 additions & 4 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ public function testLimitOffsetWithExpression(): void
public function testWithQuery()
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (
!str_contains($db->getServerVersion(), 'MariaDB')
&& version_compare($db->getServerVersion(), '8.0.0', '<')
!str_contains($serverVersion, 'MariaDB')
&& version_compare($serverVersion, '8.0.0', '<')
) {
self::markTestSkipped('CTE not supported in MySQL versions below 8.0.0');
}
Expand All @@ -99,10 +100,11 @@ public function testWithQuery()
public function testWithQueryRecursive()
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (
!str_contains($db->getServerVersion(), 'MariaDB')
&& version_compare($db->getServerVersion(), '8.0.0', '<')
!str_contains($serverVersion, 'MariaDB')
&& version_compare($serverVersion, '8.0.0', '<')
) {
self::markTestSkipped('CTE not supported in MySQL versions below 8.0.0');
}
Expand Down
12 changes: 7 additions & 5 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ final class SchemaTest extends CommonSchemaTest
public function testColumnSchema(array $columns, string $tableName): void
{
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

if (
version_compare($db->getServerVersion(), '8.0.17', '>') &&
!str_contains($db->getServerVersion(), 'MariaDB')
version_compare($serverVersion, '8.0.17', '>') &&
!str_contains($serverVersion, 'MariaDB')
) {
if ($tableName === 'type') {
$columns['int_col']['size'] = null;
Expand Down Expand Up @@ -95,10 +96,11 @@ public function testDefaultValueDatetimeColumn(): void
{
$tableName = '{{%datetime_test}}';
$db = $this->getConnection();
$serverVersion = $db->getServerInfo()->getVersion();

$oldMySQL = !(
version_compare($db->getServerVersion(), '8.0.0', '>') &&
!str_contains($db->getServerVersion(), 'MariaDB')
version_compare($serverVersion, '8.0.0', '>') &&
!str_contains($serverVersion, 'MariaDB')
);

$columnsData = [
Expand Down Expand Up @@ -280,7 +282,7 @@ public function testGetViewNames(): void
$schema = $db->getSchema();
$views = $schema->getViewNames();

$viewExpected = match (str_contains($db->getServerVersion(), 'MariaDB')) {
$viewExpected = match (str_contains($db->getServerInfo()->getVersion(), 'MariaDB')) {
true => ['animal_view', 'user'],
default => ['animal_view'],
};
Expand Down

0 comments on commit c80dd31

Please sign in to comment.