Skip to content

Commit

Permalink
Refactor insertBatch(), add getRawTableName() to QuoterInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 5, 2024
1 parent ce1ddb5 commit 466b4d0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
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

0 comments on commit 466b4d0

Please sign in to comment.