From 38eab537332d95db545df6f58d97734305d39d69 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 4 Aug 2024 12:44:05 -0400 Subject: [PATCH] Consolidate insert/bulkInsert implementations --- src/Phinx/Db/Adapter/PdoAdapter.php | 18 ++++-- src/Phinx/Db/Adapter/PostgresAdapter.php | 77 +----------------------- 2 files changed, 16 insertions(+), 79 deletions(-) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 7dfbcedf0..8a8722dcd 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -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)) { @@ -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)); } @@ -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) { @@ -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 */ diff --git a/src/Phinx/Db/Adapter/PostgresAdapter.php b/src/Phinx/Db/Adapter/PostgresAdapter.php index 30ed9fca4..c54c39cf5 100644 --- a/src/Phinx/Db/Adapter/PostgresAdapter.php +++ b/src/Phinx/Db/Adapter/PostgresAdapter.php @@ -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 . ' ' : ''; } }