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 authored May 6, 2024
1 parent ce1ddb5 commit d594a8b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
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
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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 d594a8b

Please sign in to comment.