Skip to content

Commit 51becbe

Browse files
authored
Fix DMLQueryBuilder::insertBatch() method (#285)
1 parent b6d0334 commit 51becbe

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- New #280: Realize `ColumnBuilder` class (@Tigrov)
1717
- Enh #281: Update according changes in `ColumnSchemaInterface` (@Tigrov)
1818
- New #282: Add `ColumnDefinitionBuilder` class (@Tigrov)
19+
- Bug #285: Fix `DMLQueryBuilder::insertBatch()` method (@Tigrov)
1920
- Enh #283: Refactor `Dsn` class (@Tigrov)
2021

2122
## 1.3.0 March 21, 2024

src/DMLQueryBuilder.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@ public function insertBatch(string $table, iterable $rows, array $columns = [],
3737
return '';
3838
}
3939

40-
$tableAndColumns = ' INTO ' . $this->quoter->quoteTableName($table);
40+
$query = 'INSERT INTO ' . $this->quoter->quoteTableName($table);
4141

4242
if (count($columns) > 0) {
4343
$quotedColumnNames = array_map($this->quoter->quoteColumnName(...), $columns);
4444

45-
$tableAndColumns .= ' (' . implode(', ', $quotedColumnNames) . ')';
45+
$query .= ' (' . implode(', ', $quotedColumnNames) . ')';
4646
}
4747

48-
$tableAndColumns .= ' VALUES ';
49-
50-
return 'INSERT ALL' . $tableAndColumns . implode($tableAndColumns, $values) . ' SELECT 1 FROM SYS.DUAL';
48+
return $query . "\nSELECT " . implode(" FROM DUAL UNION ALL\nSELECT ", $values) . ' FROM DUAL';
5149
}
5250

5351
public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string

tests/CommandTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,35 @@ public function testBatchInsert(
7474
parent::testBatchInsert($table, $values, $columns, $expected, $expectedParams, $insertedRow);
7575
}
7676

77+
/** @link https://github.com/yiisoft/db-oracle/issues/284 */
78+
public function testBatchInsertWithAutoincrement(): void
79+
{
80+
$db = $this->getConnection();
81+
$command = $db->createCommand();
82+
83+
try {
84+
$command->dropTable('test_batch_autoincrement')->execute();
85+
} catch (Exception) {
86+
}
87+
88+
$command->createTable('test_batch_autoincrement', [
89+
'id' => PseudoType::PK,
90+
'name' => ColumnType::STRING,
91+
])->execute();
92+
93+
$command->insertBatch('test_batch_autoincrement', [['name' => 'John'], ['name' => 'Emma']])->execute();
94+
95+
$this->assertSame(
96+
[
97+
['id' => '1', 'name' => 'John'],
98+
['id' => '2', 'name' => 'Emma'],
99+
],
100+
(new Query($db))->from('test_batch_autoincrement')->all()
101+
);
102+
103+
$db->close();
104+
}
105+
77106
/**
78107
* @throws Exception
79108
* @throws InvalidConfigException

tests/Provider/CommandProvider.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ public static function batchInsert(): array
2424
{
2525
$batchInsert = parent::batchInsert();
2626

27-
$batchInsert['multirow']['expected'] = <<<SQL
28-
INSERT ALL INTO "type" ("int_col", "float_col", "char_col", "bool_col") VALUES (:qp0, :qp1, :qp2, :qp3) INTO "type" ("int_col", "float_col", "char_col", "bool_col") VALUES (:qp4, :qp5, :qp6, :qp7) SELECT 1 FROM SYS.DUAL
29-
SQL;
30-
$batchInsert['multirow']['expectedParams'][':qp3'] = '1';
31-
$batchInsert['multirow']['expectedParams'][':qp7'] = '0';
32-
3327
$replaceParams = [
28+
'multirow' => [
29+
':qp3' => '1',
30+
':qp7' => '0',
31+
],
3432
'issue11242' => [
3533
':qp3' => '1',
3634
],

tests/Provider/QueryBuilderProvider.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,9 @@ public static function batchInsert(): array
4848
{
4949
$batchInsert = parent::batchInsert();
5050

51-
DbHelper::changeSqlForOracleBatchInsert($batchInsert['simple']['expected']);
52-
DbHelper::changeSqlForOracleBatchInsert($batchInsert['escape-danger-chars']['expected']);
53-
DbHelper::changeSqlForOracleBatchInsert($batchInsert['customer3']['expected']);
54-
DbHelper::changeSqlForOracleBatchInsert($batchInsert['bool-false, bool2-null']['expected']);
55-
56-
$batchInsert['wrong']['expected'] = <<<SQL
57-
INSERT ALL INTO {{%type}} ("float_col", "time") VALUES (:qp0, now()) INTO {{%type}} ("float_col", "time") VALUES (:qp1, now()) SELECT 1 FROM SYS.DUAL
58-
SQL;
59-
60-
DbHelper::changeSqlForOracleBatchInsert($batchInsert['bool-false, time-now()']['expected']);
61-
DbHelper::changeSqlForOracleBatchInsert($batchInsert['column table names are not checked']['expected']);
62-
DbHelper::changeSqlForOracleBatchInsert($batchInsert['empty columns and non-exists table']['expected']);
51+
foreach ($batchInsert as $key => $value) {
52+
DbHelper::changeSqlForOracleBatchInsert($batchInsert[$key]['expected']);
53+
}
6354

6455
$batchInsert['bool-false, bool2-null']['expectedParams'][':qp0'] = '0';
6556
$batchInsert['bool-false, time-now()']['expectedParams'][':qp0'] = '0';

0 commit comments

Comments
 (0)