Skip to content

Commit 5bddbb3

Browse files
authored
Refactoring (#393)
1 parent a46aa0a commit 5bddbb3

9 files changed

+31
-45
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Chg #388: Change supported PHP versions to `8.1 - 8.4` (@Tigrov)
3535
- Enh #388: Minor refactoring (@Tigrov)
3636
- Chg #390: Remove `yiisoft/json` dependency (@Tigrov)
37+
- Enh #393: Refactor according changes in `db` package (@Tigrov)
3738
- New #391: Add `caseSensitive` option to like condition (@vjik)
3839

3940
## 1.3.0 March 21, 2024

src/Builder/ArrayExpressionBuilder.php

-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ private function getColumn(ArrayExpression $expression): AbstractArrayColumn|nul
128128
} elseif ($type !== ColumnType::ARRAY) {
129129
$column = $this
130130
->queryBuilder
131-
->getSchema()
132131
->getColumnFactory()
133132
->fromDefinition($type);
134133

@@ -142,7 +141,6 @@ private function getColumn(ArrayExpression $expression): AbstractArrayColumn|nul
142141
/** @var AbstractArrayColumn */
143142
return $this
144143
->queryBuilder
145-
->getSchema()
146144
->getColumnFactory()
147145
->fromType(ColumnType::ARRAY, $info);
148146
}

src/Connection.php

+10-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection;
88
use Yiisoft\Db\Driver\Pdo\PdoCommandInterface;
99
use Yiisoft\Db\Exception\InvalidArgumentException;
10+
use Yiisoft\Db\Pgsql\Column\ColumnFactory;
1011
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
12+
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
1113
use Yiisoft\Db\Schema\Quoter;
1214
use Yiisoft\Db\Schema\QuoterInterface;
1315
use Yiisoft\Db\Schema\SchemaInterface;
@@ -44,6 +46,11 @@ public function createTransaction(): TransactionInterface
4446
return new Transaction($this);
4547
}
4648

49+
public function getColumnFactory(): ColumnFactoryInterface
50+
{
51+
return new ColumnFactory();
52+
}
53+
4754
public function getLastInsertID(?string $sequenceName = null): string
4855
{
4956
if ($sequenceName === null) {
@@ -55,28 +62,16 @@ public function getLastInsertID(?string $sequenceName = null): string
5562

5663
public function getQueryBuilder(): QueryBuilderInterface
5764
{
58-
return $this->queryBuilder ??= new QueryBuilder(
59-
$this->getQuoter(),
60-
$this->getSchema(),
61-
$this->getServerInfo(),
62-
);
65+
return $this->queryBuilder ??= new QueryBuilder($this);
6366
}
6467

6568
public function getQuoter(): QuoterInterface
6669
{
67-
if ($this->quoter === null) {
68-
$this->quoter = new Quoter('"', '"', $this->getTablePrefix());
69-
}
70-
71-
return $this->quoter;
70+
return $this->quoter ??= new Quoter('"', '"', $this->getTablePrefix());
7271
}
7372

7473
public function getSchema(): SchemaInterface
7574
{
76-
if ($this->schema === null) {
77-
$this->schema = new Schema($this, $this->schemaCache);
78-
}
79-
80-
return $this->schema;
75+
return $this->schema ??= new Schema($this, $this->schemaCache);
8176
}
8277
}

src/DDLQueryBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function alterColumn(string $table, string $column, ColumnInterface|strin
4141
return "ALTER TABLE $tableName ALTER COLUMN $columnName $type";
4242
}
4343

44-
$type = $this->schema->getColumnFactory()->fromDefinition($type);
44+
$type = $this->queryBuilder->getColumnFactory()->fromDefinition($type);
4545
}
4646

4747
$columnDefinitionBuilder = $this->queryBuilder->getColumnDefinitionBuilder();

src/QueryBuilder.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
namespace Yiisoft\Db\Pgsql;
66

7-
use Yiisoft\Db\Connection\ServerInfoInterface;
7+
use Yiisoft\Db\Connection\ConnectionInterface;
88
use Yiisoft\Db\Pgsql\Column\ColumnDefinitionBuilder;
99
use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder;
10-
use Yiisoft\Db\Schema\QuoterInterface;
11-
use Yiisoft\Db\Schema\SchemaInterface;
1210

1311
use function bin2hex;
1412

@@ -17,12 +15,13 @@
1715
*/
1816
final class QueryBuilder extends AbstractQueryBuilder
1917
{
20-
public function __construct(QuoterInterface $quoter, SchemaInterface $schema, ServerInfoInterface $serverInfo)
18+
public function __construct(ConnectionInterface $db)
2119
{
20+
$quoter = $db->getQuoter();
21+
$schema = $db->getSchema();
22+
2223
parent::__construct(
23-
$quoter,
24-
$schema,
25-
$serverInfo,
24+
$db,
2625
new DDLQueryBuilder($this, $quoter, $schema),
2726
new DMLQueryBuilder($this, $quoter, $schema),
2827
new DQLQueryBuilder($this, $quoter),

src/Schema.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
use Yiisoft\Db\Exception\InvalidConfigException;
1919
use Yiisoft\Db\Exception\NotSupportedException;
2020
use Yiisoft\Db\Helper\DbArrayHelper;
21-
use Yiisoft\Db\Pgsql\Column\ColumnFactory;
2221
use Yiisoft\Db\Pgsql\Column\SequenceColumnInterface;
23-
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
2422
use Yiisoft\Db\Schema\Column\ColumnInterface;
2523
use Yiisoft\Db\Schema\TableSchemaInterface;
2624

@@ -94,11 +92,6 @@ final class Schema extends AbstractPdoSchema
9492
*/
9593
protected string|null $defaultSchema = 'public';
9694

97-
public function getColumnFactory(): ColumnFactoryInterface
98-
{
99-
return new ColumnFactory();
100-
}
101-
10295
/**
10396
* Resolves the table name and schema name (if any).
10497
*
@@ -715,7 +708,7 @@ protected function findColumns(TableSchemaInterface $table): bool
715708
*/
716709
private function loadColumn(array $info): ColumnInterface
717710
{
718-
$columnFactory = $this->getColumnFactory();
711+
$columnFactory = $this->db->getColumnFactory();
719712
$dbType = $info['data_type'];
720713

721714
if (!in_array($info['type_scheme'], [$this->defaultSchema, 'pg_catalog'], true)) {

tests/ColumnFactoryTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testFromDbType(string $dbType, string $expectedType, string $exp
2525
parent::testFromDbType($dbType, $expectedType, $expectedInstanceOf);
2626

2727
$db = $this->getConnection();
28-
$columnFactory = $db->getSchema()->getColumnFactory();
28+
$columnFactory = $db->getColumnFactory();
2929

3030
// For array type
3131
$column = $columnFactory->fromType(ColumnType::ARRAY, ['dbType' => $dbType]);
@@ -56,7 +56,7 @@ public function testFromType(string $type, string $expectedType, string $expecte
5656
parent::testFromType($type, $expectedType, $expectedInstanceOf);
5757

5858
$db = $this->getConnection();
59-
$columnFactory = $db->getSchema()->getColumnFactory();
59+
$columnFactory = $db->getColumnFactory();
6060

6161
// For array type
6262
$column = $columnFactory->fromType(ColumnType::ARRAY, ['column' => $columnFactory->fromType($type)]);

tests/ConnectionTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Yiisoft\Db\Exception\Exception;
1111
use Yiisoft\Db\Exception\InvalidConfigException;
1212
use Yiisoft\Db\Exception\NotSupportedException;
13+
use Yiisoft\Db\Pgsql\Column\ColumnFactory;
1314
use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
1415
use Yiisoft\Db\Tests\Common\CommonConnectionTest;
1516
use Yiisoft\Db\Transaction\TransactionInterface;
@@ -133,4 +134,13 @@ static function (ConnectionInterface $db) {
133134

134135
$db->close();
135136
}
137+
138+
public function testGetColumnFactory(): void
139+
{
140+
$db = $this->getConnection();
141+
142+
$this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory());
143+
144+
$db->close();
145+
}
136146
}

tests/SchemaTest.php

-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Yiisoft\Db\Exception\InvalidConfigException;
1616
use Yiisoft\Db\Exception\NotSupportedException;
1717
use Yiisoft\Db\Expression\Expression;
18-
use Yiisoft\Db\Pgsql\Column\ColumnFactory;
1918
use Yiisoft\Db\Pgsql\Schema;
2019
use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
2120
use Yiisoft\Db\Schema\SchemaInterface;
@@ -637,13 +636,4 @@ public function testTableIndexes(): void
637636

638637
$db->close();
639638
}
640-
641-
public function testGetColumnFactory(): void
642-
{
643-
$db = $this->getConnection();
644-
645-
$this->assertInstanceOf(ColumnFactory::class, $db->getSchema()->getColumnFactory());
646-
647-
$db->close();
648-
}
649639
}

0 commit comments

Comments
 (0)