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

Refactor insertBatch(), add getRawTableName() to QuoterInterface #834

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -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

Expand Down
9 changes: 1 addition & 8 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 0 additions & 10 deletions src/Schema/Quoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '{{')) {
Expand Down
12 changes: 12 additions & 0 deletions src/Schema/QuoterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 4 additions & 4 deletions tests/Db/Helper/DbArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down