Skip to content

Commit

Permalink
Consolidate insert/bulkInsert implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin committed Aug 4, 2024
1 parent 1dbfdc1 commit 73dcd4b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 79 deletions.
18 changes: 14 additions & 4 deletions src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public function insert(Table $table, array $row): void
$this->quoteTableName($table->getName())
);
$columns = array_keys($row);
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $columns)) . ')';
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $columns)) . ') ' . $this->getInsertOverride() . 'VALUES';

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

if ($this->isDryRunEnabled()) {
$sql .= ' VALUES (' . implode(', ', array_map([$this, 'quoteValue'], $row)) . ');';
$sql .= '(' . implode(', ', array_map([$this, 'quoteValue'], $row)) . ');';
$this->output->writeln($sql);
} else {
$sql .= ' VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')';
$sql .= '(' . implode(', ', array_fill(0, count($columns), '?')) . ')';
$stmt = $this->getConnection()->prepare($sql);
$stmt->execute(array_values($row));
}
Expand Down Expand Up @@ -383,7 +383,7 @@ public function bulkinsert(Table $table, array $rows): void
);
$current = current($rows);
$keys = array_keys($current);
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $keys)) . ') VALUES ';
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $keys)) . ') ' . $this->getInsertOverride() . 'VALUES ';

if ($this->isDryRunEnabled()) {
$values = array_map(function ($row) {
Expand Down Expand Up @@ -414,6 +414,16 @@ public function bulkinsert(Table $table, array $rows): void
}
}

/**
* Returns override clause for insert operations, to be befort `VALUES` keyword.
*
* @return string
*/
protected function getInsertOverride(): string
{
return '';
}

/**
* @inheritDoc
*/
Expand Down
77 changes: 2 additions & 75 deletions src/Phinx/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1598,81 +1598,8 @@ public function setSearchPath(): void
/**
* @inheritDoc
*/
public function insert(Table $table, array $row): void
protected function getInsertOverride(): string
{
$sql = sprintf(
'INSERT INTO %s ',
$this->quoteTableName($table->getName())
);
$columns = array_keys($row);
$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $columns)) . ')';

foreach ($row as $column => $value) {
if (is_bool($value)) {
$row[$column] = $this->castToBool($value);
}
}

$override = '';
if ($this->useIdentity) {
$override = self::OVERRIDE_SYSTEM_VALUE . ' ';
}

if ($this->isDryRunEnabled()) {
$sql .= ' ' . $override . 'VALUES (' . implode(', ', array_map([$this, 'quoteValue'], $row)) . ');';
$this->output->writeln($sql);
} else {
$sql .= ' ' . $override . 'VALUES (' . implode(', ', array_fill(0, count($columns), '?')) . ')';
$stmt = $this->getConnection()->prepare($sql);
$stmt->execute(array_values($row));
}
}

/**
* @inheritDoc
*/
public function bulkinsert(Table $table, array $rows): void
{
$sql = sprintf(
'INSERT INTO %s ',
$this->quoteTableName($table->getName())
);
$current = current($rows);
$keys = array_keys($current);

$override = '';
if ($this->useIdentity) {
$override = self::OVERRIDE_SYSTEM_VALUE . ' ';
}

$sql .= '(' . implode(', ', array_map([$this, 'quoteColumnName'], $keys)) . ') ' . $override . 'VALUES ';

if ($this->isDryRunEnabled()) {
$values = array_map(function ($row) {
return '(' . implode(', ', array_map([$this, 'quoteValue'], $row)) . ')';
}, $rows);
$sql .= implode(', ', $values) . ';';
$this->output->writeln($sql);
} else {
$count_keys = count($keys);
$query = '(' . implode(', ', array_fill(0, $count_keys, '?')) . ')';
$count_vars = count($rows);
$queries = array_fill(0, $count_vars, $query);
$sql .= implode(',', $queries);
$stmt = $this->getConnection()->prepare($sql);
$vals = [];

foreach ($rows as $row) {
foreach ($row as $v) {
if (is_bool($v)) {
$vals[] = $this->castToBool($v);
} else {
$vals[] = $v;
}
}
}

$stmt->execute($vals);
}
return $this->useIdentity ? self::OVERRIDE_SYSTEM_VALUE . ' ' : '';
}
}

0 comments on commit 73dcd4b

Please sign in to comment.