Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Apr 14, 2024
1 parent 705b317 commit 605b5c4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
38 changes: 27 additions & 11 deletions src/QueryBuilder/AbstractDMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use function array_combine;
use function array_diff;
use function array_fill_keys;
use function array_filter;
use function array_key_exists;
use function array_keys;
Expand Down Expand Up @@ -63,15 +64,11 @@ public function __construct(

public function batchInsert(string $table, array $columns, iterable $rows, array &$params = []): string
{
if (empty($rows)) {
return '';
}

while ($rows instanceof IteratorAggregate) {
$rows = $rows->getIterator();
if (!is_array($rows)) {
$rows = $this->prepareTraversable($rows);
}

if ($rows instanceof Iterator && !$rows->valid()) {
if (empty($rows)) {
return '';
}

Expand Down Expand Up @@ -139,6 +136,26 @@ public function upsert(
throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');
}

/**
* Prepare traversable for batch insert.
*
* @param Traversable $rows The rows to be batch inserted into the table.
*
* @return Iterator|array The prepared rows.

Check failure on line 144 in src/QueryBuilder/AbstractDMLQueryBuilder.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidReturnType

src/QueryBuilder/AbstractDMLQueryBuilder.php:144:16: InvalidReturnType: The declared return type 'Iterator|array<array-key, mixed>' for Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder::prepareTraversable is incorrect, got 'iterable<mixed, mixed>' (see https://psalm.dev/011)
*/
protected function prepareTraversable(Traversable $rows): Iterator|array
{
while ($rows instanceof IteratorAggregate) {
$rows = $rows->getIterator();
}

if ($rows instanceof Iterator && !$rows->valid()) {
return [];
}

return $rows;

Check failure on line 156 in src/QueryBuilder/AbstractDMLQueryBuilder.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

LessSpecificReturnStatement

src/QueryBuilder/AbstractDMLQueryBuilder.php:156:16: LessSpecificReturnStatement: The type 'Traversable<mixed, mixed>' is more general than the declared return type 'Iterator|array<array-key, mixed>' for Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder::prepareTraversable (see https://psalm.dev/129)
}

/**
* Prepare values for batch insert.
*
Expand All @@ -156,17 +173,16 @@ protected function prepareBatchInsertValues(string $table, iterable $rows, array
$values = [];
/** @var string[] $columnNames */
$columnNames = array_values($columns);
$columnKeys = array_combine($columnNames, $columnNames);
$columnNulls = array_fill_keys($columnNames, 'NULL');
$columnKeys = array_fill_keys($columnNames, false);
$columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? [];

foreach ($rows as $row) {
$i = 0;
$placeholders = $columnNulls;
$placeholders = $columnKeys;

/** @var int|string $key */
foreach ($row as $key => $value) {
$columnName = $columns[$key] ?? $columnKeys[$key] ?? $columnNames[$i] ?? $i;
$columnName = $columns[$key] ?? (isset($columnKeys[$key]) ? $key : $columnNames[$i] ?? $i);

if (isset($columnSchemas[$columnName])) {
$value = $columnSchemas[$columnName]->dbTypecast($value);
Expand Down
12 changes: 0 additions & 12 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,6 @@ public static function batchInsert(): array
':qp3' => 1,
],
],
'skipped columns' => [
'customer',
['email', 'name', 'address'],
'values' => [['email' => 'hello@localhost', 'address' => 'Earth, Solar System']],
'expected' => DbHelper::replaceQuotes(
<<<SQL
INSERT INTO [[customer]] ([[email]], [[name]], [[address]]) VALUES (:qp0, NULL, :qp1)
SQL,
static::$driverName,
),
'expectedParams' => [':qp0' => 'hello@localhost', ':qp1' => 'Earth, Solar System'],
],
];
}

Expand Down

0 comments on commit 605b5c4

Please sign in to comment.