diff --git a/CHANGELOG.md b/CHANGELOG.md index 476a533d8..b256afb5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ - New #379: Add parameters `$ifExists` and `$cascade` to `CommandInterface::dropTable()` and `DDLQueryBuilderInterface::dropTable()` methods (@vjik) - Chg #382: Remove `yiisoft/json` dependency (@Tigrov) +- Enh #384: Refactor according changes in `db` package (@Tigrov) - New #383: Add `caseSensitive` option to like condition (@vjik) ## 1.2.0 March 21, 2024 diff --git a/src/Column/ColumnDefinitionBuilder.php b/src/Column/ColumnDefinitionBuilder.php index 431f51895..9c9dfd9ab 100644 --- a/src/Column/ColumnDefinitionBuilder.php +++ b/src/Column/ColumnDefinitionBuilder.php @@ -65,7 +65,7 @@ protected function buildComment(ColumnInterface $column): string { $comment = $column->getComment(); - return $comment === null ? '' : ' COMMENT ' . $this->queryBuilder->quoter()->quoteValue($comment); + return $comment === null ? '' : ' COMMENT ' . $this->queryBuilder->getQuoter()->quoteValue($comment); } protected function getDbType(ColumnInterface $column): string diff --git a/src/Connection.php b/src/Connection.php index ca1527f0c..df31886ca 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -8,7 +8,9 @@ use Throwable; use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection; use Yiisoft\Db\Driver\Pdo\PdoCommandInterface; +use Yiisoft\Db\Mysql\Column\ColumnFactory; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; +use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Transaction\TransactionInterface; @@ -63,30 +65,23 @@ public function createTransaction(): TransactionInterface return new Transaction($this); } + public function getColumnFactory(): ColumnFactoryInterface + { + return new ColumnFactory(); + } + public function getQueryBuilder(): QueryBuilderInterface { - return $this->queryBuilder ??= new QueryBuilder( - $this->getQuoter(), - $this->getSchema(), - $this->getServerInfo(), - ); + return $this->queryBuilder ??= new QueryBuilder($this); } public function getQuoter(): QuoterInterface { - if ($this->quoter === null) { - $this->quoter = new Quoter('`', '`', $this->getTablePrefix()); - } - - return $this->quoter; + return $this->quoter ??= new Quoter('`', '`', $this->getTablePrefix()); } public function getSchema(): SchemaInterface { - if ($this->schema === null) { - $this->schema = new Schema($this, $this->schemaCache); - } - - return $this->schema; + return $this->schema ??= new Schema($this, $this->schemaCache); } } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 0cbdd963c..1d7a086d4 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -4,23 +4,22 @@ namespace Yiisoft\Db\Mysql; -use Yiisoft\Db\Connection\ServerInfoInterface; +use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Mysql\Column\ColumnDefinitionBuilder; use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder; -use Yiisoft\Db\Schema\QuoterInterface; -use Yiisoft\Db\Schema\SchemaInterface; /** * Implements MySQL, MariaDB specific query builder. */ final class QueryBuilder extends AbstractQueryBuilder { - public function __construct(QuoterInterface $quoter, SchemaInterface $schema, ServerInfoInterface $serverInfo) + public function __construct(ConnectionInterface $db) { + $quoter = $db->getQuoter(); + $schema = $db->getSchema(); + parent::__construct( - $quoter, - $schema, - $serverInfo, + $db, new DDLQueryBuilder($this, $quoter, $schema), new DMLQueryBuilder($this, $quoter, $schema), new DQLQueryBuilder($this, $quoter), diff --git a/src/Schema.php b/src/Schema.php index cb0725f43..aa54b63eb 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -15,8 +15,6 @@ use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Helper\DbArrayHelper; -use Yiisoft\Db\Mysql\Column\ColumnFactory; -use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; use Yiisoft\Db\Schema\Column\ColumnInterface; use Yiisoft\Db\Schema\TableSchemaInterface; @@ -73,11 +71,6 @@ */ final class Schema extends AbstractPdoSchema { - public function getColumnFactory(): ColumnFactoryInterface - { - return new ColumnFactory(); - } - /** * Returns all unique indexes for the given table. * @@ -405,7 +398,7 @@ private function loadColumn(array $info): ColumnInterface { $extra = trim(str_ireplace('auto_increment', '', $info['extra'], $autoIncrement)); - $column = $this->getColumnFactory()->fromDefinition($info['column_type'], [ + $column = $this->db->getColumnFactory()->fromDefinition($info['column_type'], [ 'autoIncrement' => $autoIncrement > 0, 'comment' => $info['column_comment'], 'defaultValueRaw' => $info['column_default'], diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index 4f6f32e1d..7bc5d1703 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -52,7 +52,7 @@ public function testFromTypeDefaultValueRaw(string $type, string|null $defaultVa public function testExpressionDefaultValueRaw(): void { $db = $this->getConnection(); - $columnFactory = $db->getSchema()->getColumnFactory(); + $columnFactory = $db->getColumnFactory(); $column = $columnFactory->fromType(ColumnType::DATETIME, ['defaultValueRaw' => 'now()', 'extra' => 'DEFAULT_GENERATED']); diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index a10d3a9b9..f4cb4dd0b 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -11,6 +11,7 @@ use Yiisoft\Db\Exception\IntegrityException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; +use Yiisoft\Db\Mysql\Column\ColumnFactory; use Yiisoft\Db\Mysql\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest; use Yiisoft\Db\Transaction\TransactionInterface; @@ -158,4 +159,13 @@ public function testNotRestartConnectionOnTimeoutInTransaction(): void $db->close(); } + + public function testGetColumnFactory(): void + { + $db = $this->getConnection(); + + $this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory()); + + $db->close(); + } } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index e37d4367b..eaee2c355 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -14,7 +14,6 @@ use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Mysql\Column\ColumnBuilder; -use Yiisoft\Db\Mysql\Column\ColumnFactory; use Yiisoft\Db\Mysql\Schema; use Yiisoft\Db\Mysql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; @@ -493,13 +492,4 @@ public function testInsertDefaultValues() $db->close(); } - - public function testGetColumnFactory(): void - { - $db = $this->getConnection(); - - $this->assertInstanceOf(ColumnFactory::class, $db->getSchema()->getColumnFactory()); - - $db->close(); - } }