From dd5b42d40f4692a5f1e48d9aa296c45c644d6f6b Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 18 Oct 2024 14:00:24 +0700 Subject: [PATCH 1/5] Fix `DMLQueryBuilder::insertBatch()` method --- src/DMLQueryBuilder.php | 8 +++----- tests/Provider/CommandProvider.php | 10 ++++------ tests/Provider/QueryBuilderProvider.php | 15 +++------------ 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/DMLQueryBuilder.php b/src/DMLQueryBuilder.php index b9c028d..81ca3dd 100644 --- a/src/DMLQueryBuilder.php +++ b/src/DMLQueryBuilder.php @@ -37,17 +37,15 @@ public function insertBatch(string $table, iterable $rows, array $columns = [], return ''; } - $tableAndColumns = ' INTO ' . $this->quoter->quoteTableName($table); + $query = 'INSERT INTO ' . $this->quoter->quoteTableName($table); if (count($columns) > 0) { $quotedColumnNames = array_map($this->quoter->quoteColumnName(...), $columns); - $tableAndColumns .= ' (' . implode(', ', $quotedColumnNames) . ')'; + $query .= ' (' . implode(', ', $quotedColumnNames) . ')'; } - $tableAndColumns .= ' VALUES '; - - return 'INSERT ALL' . $tableAndColumns . implode($tableAndColumns, $values) . ' SELECT 1 FROM SYS.DUAL'; + return $query . "\nSELECT " . implode(" FROM DUAL UNION ALL\nSELECT ", $values) . ' FROM DUAL'; } public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index 32353a9..248f854 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -24,13 +24,11 @@ public static function batchInsert(): array { $batchInsert = parent::batchInsert(); - $batchInsert['multirow']['expected'] = << [ + ':qp3' => '1', + ':qp7' => '0', + ], 'issue11242' => [ ':qp3' => '1', ], diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index bb9a6a0..2201d39 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -48,18 +48,9 @@ public static function batchInsert(): array { $batchInsert = parent::batchInsert(); - DbHelper::changeSqlForOracleBatchInsert($batchInsert['simple']['expected']); - DbHelper::changeSqlForOracleBatchInsert($batchInsert['escape-danger-chars']['expected']); - DbHelper::changeSqlForOracleBatchInsert($batchInsert['customer3']['expected']); - DbHelper::changeSqlForOracleBatchInsert($batchInsert['bool-false, bool2-null']['expected']); - - $batchInsert['wrong']['expected'] = << $value) { + DbHelper::changeSqlForOracleBatchInsert($batchInsert[$key]['expected']); + } $batchInsert['bool-false, bool2-null']['expectedParams'][':qp0'] = '0'; $batchInsert['bool-false, time-now()']['expectedParams'][':qp0'] = '0'; From 935de16f627d6e3e034eb33ed21a640c11defd81 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 18 Oct 2024 14:14:00 +0700 Subject: [PATCH 2/5] Add line to CHANGELOG.md [skip ci] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddb324c..ba24a87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - New #280: Realize `ColumnBuilder` class (@Tigrov) - Enh #281: Update according changes in `ColumnSchemaInterface` (@Tigrov) - New #282: Add `ColumnDefinitionBuilder` class (@Tigrov) +- Bug #285: Fix `DMLQueryBuilder::insertBatch()` method (@Tigrov) ## 1.3.0 March 21, 2024 From 4919f02d66b14bd2ac9e0ac5fcb70ee3013cf2d5 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 18 Oct 2024 14:38:49 +0700 Subject: [PATCH 3/5] Add test --- tests/CommandTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 7062677..09e0230 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -74,6 +74,35 @@ public function testBatchInsert( parent::testBatchInsert($table, $values, $columns, $expected, $expectedParams, $insertedRow); } + /** @link https://github.com/yiisoft/db-oracle/issues/284 */ + public function testBatchInsertWithAutoincrement() + { + $db = $this->getConnection(); + $command = $db->createCommand(); + + try { + $command->dropTable('test_batch_autoincrement')->execute(); + } catch (Exception) { + } + + $command->createTable('test_batch_autoincrement', [ + 'id' => PseudoType::PK, + 'name' => ColumnType::STRING + ])->execute(); + + $command->insertBatch('test_batch_autoincrement', [['name' => 'John'], ['name' => 'Emma']])->execute(); + + $this->assertSame( + [ + ['id' => '1', 'name' => 'John'], + ['id' => '2', 'name' => 'Emma'], + ], + (new Query($db))->from('test_batch_autoincrement')->all() + ); + + $db->close(); + } + /** * @throws Exception * @throws InvalidConfigException From 3e91c803fe58fdfbd880b8087a75e3ed3bba0067 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 18 Oct 2024 07:39:05 +0000 Subject: [PATCH 4/5] Apply fixes from StyleCI --- tests/CommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 09e0230..4cd366f 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -87,7 +87,7 @@ public function testBatchInsertWithAutoincrement() $command->createTable('test_batch_autoincrement', [ 'id' => PseudoType::PK, - 'name' => ColumnType::STRING + 'name' => ColumnType::STRING, ])->execute(); $command->insertBatch('test_batch_autoincrement', [['name' => 'John'], ['name' => 'Emma']])->execute(); From 75424e59a49de5f445eee0e4cf538ddd4872b16f Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 18 Oct 2024 14:40:11 +0700 Subject: [PATCH 5/5] Improve test --- tests/CommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 09e0230..7b1ac25 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -75,7 +75,7 @@ public function testBatchInsert( } /** @link https://github.com/yiisoft/db-oracle/issues/284 */ - public function testBatchInsertWithAutoincrement() + public function testBatchInsertWithAutoincrement(): void { $db = $this->getConnection(); $command = $db->createCommand();