Skip to content

Commit eae55bc

Browse files
authoredAug 4, 2024
Consolidate insert/bulkInsert implementations (#2300)
1 parent 1dbfdc1 commit eae55bc

File tree

2 files changed

+16
-79
lines changed

2 files changed

+16
-79
lines changed
 

‎src/Phinx/Db/Adapter/PdoAdapter.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public function insert(Table $table, array $row): void
324324
$this->quoteTableName($table->getName())
325325
);
326326
$columns = array_keys($row);
327-
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $columns)) . ')';
327+
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $columns)) . ') ' . $this->getInsertOverride() . 'VALUES ';
328328

329329
foreach ($row as $column => $value) {
330330
if (is_bool($value)) {
@@ -333,10 +333,10 @@ public function insert(Table $table, array $row): void
333333
}
334334

335335
if ($this->isDryRunEnabled()) {
336-
$sql .= ' VALUES (' . implode(', ', array_map([$this, 'quoteValue'], $row)) . ');';
336+
$sql .= '(' . implode(', ', array_map([$this, 'quoteValue'], $row)) . ');';
337337
$this->output->writeln($sql);
338338
} else {
339-
$sql .= ' VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')';
339+
$sql .= '(' . implode(', ', array_fill(0, count($columns), '?')) . ')';
340340
$stmt = $this->getConnection()->prepare($sql);
341341
$stmt->execute(array_values($row));
342342
}
@@ -383,7 +383,7 @@ public function bulkinsert(Table $table, array $rows): void
383383
);
384384
$current = current($rows);
385385
$keys = array_keys($current);
386-
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $keys)) . ') VALUES ';
386+
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $keys)) . ') ' . $this->getInsertOverride() . 'VALUES ';
387387

388388
if ($this->isDryRunEnabled()) {
389389
$values = array_map(function ($row) {
@@ -414,6 +414,16 @@ public function bulkinsert(Table $table, array $rows): void
414414
}
415415
}
416416

417+
/**
418+
* Returns override clause for insert operations, to be befort `VALUES` keyword.
419+
*
420+
* @return string
421+
*/
422+
protected function getInsertOverride(): string
423+
{
424+
return '';
425+
}
426+
417427
/**
418428
* @inheritDoc
419429
*/

‎src/Phinx/Db/Adapter/PostgresAdapter.php

+2-75
Original file line numberDiff line numberDiff line change
@@ -1598,81 +1598,8 @@ public function setSearchPath(): void
15981598
/**
15991599
* @inheritDoc
16001600
*/
1601-
public function insert(Table $table, array $row): void
1601+
protected function getInsertOverride(): string
16021602
{
1603-
$sql = sprintf(
1604-
'INSERT INTO %s ',
1605-
$this->quoteTableName($table->getName())
1606-
);
1607-
$columns = array_keys($row);
1608-
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $columns)) . ')';
1609-
1610-
foreach ($row as $column => $value) {
1611-
if (is_bool($value)) {
1612-
$row[$column] = $this->castToBool($value);
1613-
}
1614-
}
1615-
1616-
$override = '';
1617-
if ($this->useIdentity) {
1618-
$override = self::OVERRIDE_SYSTEM_VALUE . ' ';
1619-
}
1620-
1621-
if ($this->isDryRunEnabled()) {
1622-
$sql .= ' ' . $override . 'VALUES (' . implode(', ', array_map([$this, 'quoteValue'], $row)) . ');';
1623-
$this->output->writeln($sql);
1624-
} else {
1625-
$sql .= ' ' . $override . 'VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')';
1626-
$stmt = $this->getConnection()->prepare($sql);
1627-
$stmt->execute(array_values($row));
1628-
}
1629-
}
1630-
1631-
/**
1632-
* @inheritDoc
1633-
*/
1634-
public function bulkinsert(Table $table, array $rows): void
1635-
{
1636-
$sql = sprintf(
1637-
'INSERT INTO %s ',
1638-
$this->quoteTableName($table->getName())
1639-
);
1640-
$current = current($rows);
1641-
$keys = array_keys($current);
1642-
1643-
$override = '';
1644-
if ($this->useIdentity) {
1645-
$override = self::OVERRIDE_SYSTEM_VALUE . ' ';
1646-
}
1647-
1648-
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $keys)) . ') ' . $override . 'VALUES ';
1649-
1650-
if ($this->isDryRunEnabled()) {
1651-
$values = array_map(function ($row) {
1652-
return '(' . implode(', ', array_map([$this, 'quoteValue'], $row)) . ')';
1653-
}, $rows);
1654-
$sql .= implode(', ', $values) . ';';
1655-
$this->output->writeln($sql);
1656-
} else {
1657-
$count_keys = count($keys);
1658-
$query = '(' . implode(', ', array_fill(0, $count_keys, '?')) . ')';
1659-
$count_vars = count($rows);
1660-
$queries = array_fill(0, $count_vars, $query);
1661-
$sql .= implode(',', $queries);
1662-
$stmt = $this->getConnection()->prepare($sql);
1663-
$vals = [];
1664-
1665-
foreach ($rows as $row) {
1666-
foreach ($row as $v) {
1667-
if (is_bool($v)) {
1668-
$vals[] = $this->castToBool($v);
1669-
} else {
1670-
$vals[] = $v;
1671-
}
1672-
}
1673-
}
1674-
1675-
$stmt->execute($vals);
1676-
}
1603+
return $this->useIdentity ? self::OVERRIDE_SYSTEM_VALUE . ' ' : '';
16771604
}
16781605
}

0 commit comments

Comments
 (0)