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

Batch insert with empty columns #795

Merged
merged 15 commits into from
Jan 9, 2024
Merged
135 changes: 101 additions & 34 deletions src/QueryBuilder/AbstractDMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\QueryBuilder;

use JsonException;
use Traversable;
use Yiisoft\Db\Constraint\Constraint;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
Expand All @@ -20,16 +21,24 @@
use function array_diff;
use function array_fill_keys;
use function array_filter;
use function array_key_exists;
use function array_keys;
use function array_map;
use function array_merge;
use function array_slice;
use function array_unique;
use function array_values;
use function count;
use function get_object_vars;
use function implode;
use function in_array;
use function is_array;
use function is_object;
use function is_string;
use function iterator_to_array;
use function json_encode;
use function preg_match;
use function reset;
use function sort;

/**
Expand All @@ -55,47 +64,26 @@
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,
);
$query = 'INSERT INTO ' . $this->quoter->quoteTableName($table);

return 'INSERT INTO ' . $this->quoter->quoteTableName($table)
. ' (' . implode(', ', $columnNames) . ') VALUES ' . implode(', ', $values);
if (count($columns) > 0) {
$quotedColumnNames = array_map(
[$this->quoter, 'quoteColumnName'],
$columns,
);

$query .= ' (' . implode(', ', $quotedColumnNames) . ')';
}

return $query . ' VALUES ' . implode(', ', $values);
}

public function delete(string $table, array|string $condition, array &$params): string
Expand Down Expand Up @@ -144,6 +132,85 @@
throw new NotSupportedException(__METHOD__ . ' is not supported by this DBMS.');
}

/**
* Prepare values for batch insert.
*
* @param string[] $columns The column names.
* @param ColumnSchemaInterface[] $columnSchemas The column schemas.
* @param iterable $rows The rows to be batch inserted into the table.
* @param array $params The binding parameters that will be generated by this method.
*
* @return string[] The values.
*/
protected function prepareBatchInsertValues(array $columns, array $columnSchemas, iterable $rows, array &$params): array
{
$values = [];
/** @var string[] $columnNames */
$columnNames = array_values($columns);
$columnKeys = array_fill_keys($columnNames, false);

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

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

if (isset($columnSchemas[$columnName])) {
$value = $columnSchemas[$columnName]->dbTypecast($value);

Check warning on line 161 in src/QueryBuilder/AbstractDMLQueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractDMLQueryBuilder.php#L161

Added line #L161 was not covered by tests
}

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

++$i;
}

$values[] = '(' . implode(', ', $placeholders) . ')';
}

return $values;
}

/**
* Extract column names from columns and rows.
*
* @param string[] $columns The column names.
* @param ColumnSchemaInterface[] $columnSchemas The column schemas.
* @param iterable $rows The rows to be batch inserted into the table.
*
* @return string[] The column names.
*/
protected function extractColumnNames(array $columns, array $columnSchemas, iterable $rows): array
{
$columns = $this->getNormalizeColumnNames('', $columns);

if ($columns !== [] || !is_array($rows)) {
return $columns;
}

$row = reset($rows);
$row = match (true) {
is_array($row) => $row,
$row instanceof Traversable => iterator_to_array($row),
is_object($row) => get_object_vars($row),
default => [],
};

if (!array_key_exists(0, $row)) {
$columnNames = array_keys($row);

Check warning on line 205 in src/QueryBuilder/AbstractDMLQueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractDMLQueryBuilder.php#L205

Added line #L205 was not covered by tests
} else {
$columnNames = array_slice(array_keys($columnSchemas), 0, count($row));
}

/** @var string[] $columnNames */
return array_combine($columnNames, $columnNames);
}

/**
* Prepare select-subQuery and field names for `INSERT INTO ... SELECT` SQL statement.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/AbstractQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function testBatchInsert(
string $expected,
array $expectedParams = [],
): void {
$db = $this->getConnection();
$db = $this->getConnection(true);
$qb = $db->getQueryBuilder();

$params = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/Db/QueryBuilder/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testAddDefaultValue(): void
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::batchInsert
* @dataProvider \Yiisoft\Db\Tests\Db\QueryBuilderProvider::batchInsert
*/
public function testBatchInsert(
string $table,
Expand Down
20 changes: 20 additions & 0 deletions tests/Db/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Tests\Db;

use Yiisoft\Db\Tests\Provider\QueryBuilderProvider as BaseQueryBuilderProvider;

class QueryBuilderProvider extends BaseQueryBuilderProvider
{
public static function batchInsert(): array
{
$result = parent::batchInsert();

$result['customer3']['expected'] = 'INSERT INTO [customer] VALUES (:qp0)';
$result['customer3']['expectedParams'] = [':qp0' => 'no columns passed'];

return $result;
}
}
69 changes: 69 additions & 0 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Tests\Provider;

use ArrayIterator;
use Yiisoft\Db\Command\DataType;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Expression\Expression;
Expand Down Expand Up @@ -427,6 +428,74 @@ public static function batchInsert(): array
':qp3' => 2.0,
],
],
'empty columns and associative values' => [
'type',
[],
'values' => [['int_col' => '1.0', 'float_col' => '2', 'char_col' => 10, 'bool_col' => 1]],
'expected' => DbHelper::replaceQuotes(
<<<SQL
INSERT INTO [[type]] ([[int_col]], [[float_col]], [[char_col]], [[bool_col]]) VALUES (:qp0, :qp1, :qp2, :qp3)
SQL,
static::$driverName,
),
'expectedParams' => [
':qp0' => 1,
':qp1' => 2.0,
':qp2' => '10',
':qp3' => true,
],
],
'empty columns and objects' => [
'type',
[],
'values' => [(object)['int_col' => '1.0', 'float_col' => '2', 'char_col' => 10, 'bool_col' => 1]],
'expected' => DbHelper::replaceQuotes(
<<<SQL
INSERT INTO [[type]] ([[int_col]], [[float_col]], [[char_col]], [[bool_col]]) VALUES (:qp0, :qp1, :qp2, :qp3)
SQL,
static::$driverName,
),
'expectedParams' => [
':qp0' => 1,
':qp1' => 2.0,
':qp2' => '10',
':qp3' => true,
],
],
'empty columns and Traversable' => [
'type',
[],
'values' => [new ArrayIterator(['int_col' => '1.0', 'float_col' => '2', 'char_col' => 10, 'bool_col' => 1])],
'expected' => DbHelper::replaceQuotes(
<<<SQL
INSERT INTO [[type]] ([[int_col]], [[float_col]], [[char_col]], [[bool_col]]) VALUES (:qp0, :qp1, :qp2, :qp3)
SQL,
static::$driverName,
),
'expectedParams' => [
':qp0' => 1,
':qp1' => 2.0,
':qp2' => '10',
':qp3' => true,
],
],
'empty columns and indexed values' => [
'type',
[],
'values' => [['1.0', '2', 10, 1]],
'expected' => DbHelper::replaceQuotes(
<<<SQL
INSERT INTO [[type]] ([[int_col]], [[float_col]], [[char_col]], [[bool_col]]) VALUES (:qp0, :qp1, :qp2, :qp3)
SQL,
static::$driverName,
),
'expectedParams' => [
':qp0' => 1,
':qp1' => 2.0,
':qp2' => '10',
':qp3' => true,
],
],
];
}

Expand Down
21 changes: 19 additions & 2 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ public static function batchInsert(): array
[['no columns passed']],
'expected' => DbHelper::replaceQuotes(
<<<SQL
INSERT INTO [[customer]] () VALUES (:qp0)
INSERT INTO [[customer]] ([[id]]) VALUES (:qp0)
Tigrov marked this conversation as resolved.
Show resolved Hide resolved
SQL,
static::$driverName,
),
'expectedParams' => [':qp0' => 'no columns passed'],
'expectedParams' => [':qp0' => 0],
Tigrov marked this conversation as resolved.
Show resolved Hide resolved
],
'bool-false, bool2-null' => [
'type',
Expand Down Expand Up @@ -235,6 +235,23 @@ public static function batchInsert(): array
})(),
'',
],
'empty columns and non-exists table' => [
'non_exists_table',
[],
'values' => [['1.0', '2', 10, 1]],
'expected' => DbHelper::replaceQuotes(
<<<SQL
INSERT INTO [[non_exists_table]] VALUES (:qp0, :qp1, :qp2, :qp3)
SQL,
static::$driverName,
),
'expectedParams' => [
':qp0' => '1.0',
':qp1' => '2',
':qp2' => 10,
':qp3' => 1,
],
],
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Support/DbHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class DbHelper
{
public static function changeSqlForOracleBatchInsert(string &$str): void
{
$str = str_replace('INSERT INTO', 'INSERT ALL INTO', $str) . ' SELECT 1 FROM SYS.DUAL';
$str = str_replace('INSERT INTO', 'INSERT ALL INTO', $str) . ' SELECT 1 FROM SYS.DUAL';
}

public static function getPsrCache(): CacheInterface
Expand Down
Loading