From 51becbe9ee6f0539e2f548f2402b0d7c75fe08c7 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Fri, 18 Oct 2024 15:03:15 +0700 Subject: [PATCH] Fix `DMLQueryBuilder::insertBatch()` method (#285) --- CHANGELOG.md | 1 + src/DMLQueryBuilder.php | 8 +++---- tests/CommandTest.php | 29 +++++++++++++++++++++++++ tests/Provider/CommandProvider.php | 10 ++++----- tests/Provider/QueryBuilderProvider.php | 15 +++---------- 5 files changed, 40 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c3cc90..60c3ae3 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) - Enh #283: Refactor `Dsn` class (@Tigrov) ## 1.3.0 March 21, 2024 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/CommandTest.php b/tests/CommandTest.php index 7062677..89a1374 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(): void + { + $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 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';