Skip to content

Commit

Permalink
Update DMLQueryBuilder::insertBatch() method (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 18, 2024
1 parent 8abb33a commit ee6f00e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/QueryBuilder/AbstractDMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -209,7 +209,7 @@ protected function prepareBatchInsertValues(string $table, iterable $rows, array
++$i;
}

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

return $values;
Expand Down
4 changes: 2 additions & 2 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static function batchInsert(): array
'customer',
[],
['address'],
'',
'expected' => '',
],
'customer3' => [
'customer',
Expand Down Expand Up @@ -242,7 +242,7 @@ public static function batchInsert(): array
}
})(),
[],
'',
'expected' => '',
],
'empty columns and non-exists table' => [
'non_exists_table',
Expand Down
14 changes: 13 additions & 1 deletion tests/Support/DbHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ee6f00e

Please sign in to comment.