From ee6f00ee6a014e34008c5b9ae3e599d2739219c7 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 18 Oct 2024 15:03:12 +0700 Subject: [PATCH] Update DMLQueryBuilder::insertBatch() method (#889) --- CHANGELOG.md | 1 + src/QueryBuilder/AbstractDMLQueryBuilder.php | 4 ++-- tests/Provider/QueryBuilderProvider.php | 4 ++-- tests/Support/DbHelper.php | 14 +++++++++++++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd645674b..5f7a5239d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ - New #882: Move `ArrayColumnSchema` and `StructuredColumnSchema` classes from `db-pgsql` package (@Tigrov) - New #883: Add `ColumnDefinitionBuilder` class and `QueryBuilderInterface::buildColumnDefinition()` method (@Tigrov) - Enh #885: Refactor `AbstractDsn` class (@Tigrov) +- Chg #889: Update `AbstractDMLQueryBuilder::insertBatch()` method (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/QueryBuilder/AbstractDMLQueryBuilder.php b/src/QueryBuilder/AbstractDMLQueryBuilder.php index 7097b0f4a..3b3905ec1 100644 --- a/src/QueryBuilder/AbstractDMLQueryBuilder.php +++ b/src/QueryBuilder/AbstractDMLQueryBuilder.php @@ -96,7 +96,7 @@ public function insertBatch(string $table, iterable $rows, array $columns = [], $query .= ' (' . implode(', ', $quotedColumnNames) . ')'; } - return $query . ' VALUES ' . implode(', ', $values); + return $query . ' VALUES (' . implode('), (', $values) . ')'; } public function delete(string $table, array|string $condition, array &$params): string @@ -209,7 +209,7 @@ protected function prepareBatchInsertValues(string $table, iterable $rows, array ++$i; } - $values[] = '(' . implode(', ', $placeholders) . ')'; + $values[] = implode(', ', $placeholders); } return $values; diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 85df631c5..63b22d580 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -172,7 +172,7 @@ public static function batchInsert(): array 'customer', [], ['address'], - '', + 'expected' => '', ], 'customer3' => [ 'customer', @@ -242,7 +242,7 @@ public static function batchInsert(): array } })(), [], - '', + 'expected' => '', ], 'empty columns and non-exists table' => [ 'non_exists_table', diff --git a/tests/Support/DbHelper.php b/tests/Support/DbHelper.php index 8aeab5d9c..18970d50f 100644 --- a/tests/Support/DbHelper.php +++ b/tests/Support/DbHelper.php @@ -21,7 +21,19 @@ final class DbHelper { public static function changeSqlForOracleBatchInsert(string &$str): void { - $str = str_replace('INSERT INTO', 'INSERT ALL INTO', $str) . ' SELECT 1 FROM SYS.DUAL'; + if (empty($str)) { + return; + } + + $str = str_replace( + ' VALUES (', + "\nSELECT ", + str_replace( + '), (', + " FROM DUAL UNION ALL\nSELECT ", + substr($str, 0, -1) + ) + ) . ' FROM DUAL'; } public static function getPsrCache(): CacheInterface