Skip to content

Commit

Permalink
Update batchInsert()` according to main PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jan 4, 2024
1 parent 6ca55a7 commit 1e4a62d
Showing 1 changed file with 11 additions and 34 deletions.
45 changes: 11 additions & 34 deletions src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,24 @@ public function batchInsert(string $table, array $columns, iterable $rows, array
return '';
}

$values = [];
$columns = $this->getNormalizeColumnNames('', $columns);
$columnNames = array_values($columns);
$columnKeys = array_fill_keys($columnNames, false);
$columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? [];

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

foreach ($row as $key => $value) {
/** @psalm-suppress MixedArrayTypeCoercion */
$columnName = $columns[$key] ?? (isset($columnKeys[$key]) ? $key : $columnNames[$i] ?? $i);
/** @psalm-suppress MixedArrayTypeCoercion */
if (isset($columnSchemas[$columnName])) {
$value = $columnSchemas[$columnName]->dbTypecast($value);
}

if ($value instanceof ExpressionInterface) {
$placeholders[$columnName] = $this->queryBuilder->buildExpression($value, $params);
} else {
$placeholders[$columnName] = $this->queryBuilder->bindParam($value, $params);
}

++$i;
}

$values[] = '(' . implode(', ', $placeholders) . ')';
}
$columns = $this->extractColumnNames($columns, $columnSchemas, $rows);
$values = $this->prepareBatchInsertValues($columns, $columnSchemas, $rows, $params);

if (empty($values)) {
return '';
}

$columnNames = array_map(
[$this->quoter, 'quoteColumnName'],
$columnNames,
);
$tableAndColumns = ' INTO ' . $this->quoter->quoteTableName($table);

if (count($columns) > 0) {
$quotedColumnNames = array_map(
[$this->quoter, 'quoteColumnName'],
$columns,
);

$tableAndColumns = ' INTO ' . $this->quoter->quoteTableName($table)
. ' (' . implode(', ', $columnNames) . ') VALUES ';
$tableAndColumns .= ' (' . implode(', ', $quotedColumnNames) . ') VALUES ';
}

return 'INSERT ALL ' . $tableAndColumns . implode($tableAndColumns, $values) . ' SELECT 1 FROM SYS.DUAL';
}
Expand Down

0 comments on commit 1e4a62d

Please sign in to comment.