diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bffa6bf4..4a058bb0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Bug #828: Fix `float` type when use `AbstractCommand::getRawSql()` method (@Tigrov) - Enh #829: Rename `batchInsert()` to `insertBatch()` in `DMLQueryBuilderInterface` and `CommandInterface` and change parameters from `$table, $columns, $rows` to `$table, $rows, $columns = []` (@Tigrov) +- Enh #834: Refactor `AbstractCommand::insertBatch()`, add `Quoter::getRawTableName()` to `QuoterInterface` (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/UPGRADE.md b/UPGRADE.md index 6da8f2b95..00ed580ab 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -54,3 +54,7 @@ $values = [ /** @var ConnectionInterface $db */ $db->createCommand()->insertBatch('user', $values)->execute(); ``` + +### New methods in `QuoterInterface` + +* `QuoterInterface::getRawTableName()` - returns the raw table name without quotes. diff --git a/src/Command/AbstractCommand.php b/src/Command/AbstractCommand.php index 0114d801d..c9f1e505f 100644 --- a/src/Command/AbstractCommand.php +++ b/src/Command/AbstractCommand.php @@ -212,14 +212,7 @@ public function batchInsert(string $table, array $columns, iterable $rows): stat public function insertBatch(string $table, iterable $rows, array $columns = []): static { - $table = $this->getQueryBuilder()->quoter()->quoteSql($table); - - /** @psalm-var string[] $columns */ - foreach ($columns as &$column) { - $column = $this->getQueryBuilder()->quoter()->quoteSql($column); - } - - unset($column); + $table = $this->getQueryBuilder()->quoter()->getRawTableName($table); $params = []; $sql = $this->getQueryBuilder()->insertBatch($table, $rows, $columns, $params); diff --git a/src/Schema/Quoter.php b/src/Schema/Quoter.php index 8314c1499..18f78aa2e 100644 --- a/src/Schema/Quoter.php +++ b/src/Schema/Quoter.php @@ -83,16 +83,6 @@ public function cleanUpTableNames(array $tableNames): array return $cleanedUpTableNames; } - /** - * Returns the actual name of a given table name. - * - * This method will strip off curly brackets from the given table name and replace the percentage character '%' with - * {@see ConnectionInterface::tablePrefix}. - * - * @param string $name The table name to convert. - * - * @return string The real name of the given table name. - */ public function getRawTableName(string $name): string { if (str_contains($name, '{{')) { diff --git a/src/Schema/QuoterInterface.php b/src/Schema/QuoterInterface.php index f51e446cc..9435c434c 100644 --- a/src/Schema/QuoterInterface.php +++ b/src/Schema/QuoterInterface.php @@ -26,6 +26,18 @@ interface QuoterInterface */ public function cleanUpTableNames(array $tableNames): array; + /** + * Returns the actual name of a given table name. + * + * This method will strip off curly brackets from the given table name and replace the percentage character '%' with + * {@see ConnectionInterface::tablePrefix}. + * + * @param string $name The table name to convert. + * + * @return string The real name of the given table name. + */ + public function getRawTableName(string $name): string; + /** * Splits full table name into parts. * diff --git a/tests/Db/Helper/DbArrayHelperTest.php b/tests/Db/Helper/DbArrayHelperTest.php index 3a87586e4..0f7039fe1 100644 --- a/tests/Db/Helper/DbArrayHelperTest.php +++ b/tests/Db/Helper/DbArrayHelperTest.php @@ -33,20 +33,20 @@ public function testPopulate(array $rows): void * @dataProvider \Yiisoft\Db\Tests\Provider\PopulateProvider::populateWithIncorrectIndexBy * @dataProvider \Yiisoft\Db\Tests\Provider\PopulateProvider::populateWithIndexByClosure */ - public function testPopulateWithIndexBy(Closure|string|null $indexBy, array $rows, array $populated): void + public function testPopulateWithIndexBy(Closure|string|null $indexBy, array $rows, array $expected): void { - $this->assertSame($populated, DbArrayHelper::populate($rows, $indexBy)); + $this->assertSame($expected, DbArrayHelper::populate($rows, $indexBy)); } /** * @dataProvider \Yiisoft\Db\Tests\Provider\PopulateProvider::populateWithIndexBy */ - public function testPopulateWithIndexByWithObject(Closure|string|null $indexBy, array $rows, array $expectedPopulated): void + public function testPopulateWithIndexByWithObject(Closure|string|null $indexBy, array $rows, array $expected): void { $rows = json_decode(json_encode($rows)); $populated = json_decode(json_encode(DbArrayHelper::populate($rows, $indexBy)), true); - $this->assertSame($expectedPopulated, $populated); + $this->assertSame($expected, $populated); } /**