From 212e06b87b157af3f8a5718b48761a97b0adf6f2 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 26 Aug 2020 20:16:57 +0000 Subject: [PATCH 1/3] Added support for the "replace" database command --- src/Database/DBA.php | 14 ++++++++++++++ src/Database/Database.php | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Database/DBA.php b/src/Database/DBA.php index 46bd871b4..9debdf02a 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -292,6 +292,20 @@ class DBA return DI::dba()->insert($table, $param, $on_duplicate_update); } + /** + * Replace a row of a table + * + * @param string|array $table Table name or array [schema => table] + * @param array $param parameter array + * + * @return boolean was the insert successful? + * @throws \Exception + */ + public static function replace($table, $param) + { + return DI::dba()->replace($table, $param); + } + /** * Fetch the id of the last insert command * diff --git a/src/Database/Database.php b/src/Database/Database.php index 4b96205a9..609b86d0e 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1006,6 +1006,33 @@ class Database return $this->e($sql, $param); } + /** + * Replace a row of a table + * + * @param string|array $table Table name or array [schema => table] + * @param array $param parameter array + * + * @return boolean was the insert successful? + * @throws \Exception + */ + public function replace($table, array $param) + { + if (empty($table) || empty($param)) { + $this->logger->info('Table and fields have to be set'); + return false; + } + + $table_string = DBA::buildTableString($table); + + $fields_string = implode(', ', array_map([DBA::class, 'quoteIdentifier'], array_keys($param))); + + $values_string = substr(str_repeat("?, ", count($param)), 0, -2); + + $sql = "REPLACE " . $table_string . " (" . $fields_string . ") VALUES (" . $values_string . ")"; + + return $this->e($sql, $param); + } + /** * Fetch the id of the last insert command * From bc98f86afea9d7218a94e1f450e4e88477af20cb Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Wed, 26 Aug 2020 22:44:51 +0200 Subject: [PATCH 2/3] Update src/Database/DBA.php Co-authored-by: Hypolite Petovan --- src/Database/DBA.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Database/DBA.php b/src/Database/DBA.php index 9debdf02a..273c87690 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -293,7 +293,8 @@ class DBA } /** - * Replace a row of a table + * Inserts a row with the provided data in the provided table. + * If the data corresponds to an existing row through a UNIQUE or PRIMARY index constraints, it updates the row instead. * * @param string|array $table Table name or array [schema => table] * @param array $param parameter array From e71545800544514a92df0d9b468e4c3119fd2155 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Wed, 26 Aug 2020 22:45:00 +0200 Subject: [PATCH 3/3] Update src/Database/Database.php Co-authored-by: Hypolite Petovan --- src/Database/Database.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 609b86d0e..d155a9276 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1007,7 +1007,8 @@ class Database } /** - * Replace a row of a table + * Inserts a row with the provided data in the provided table. + * If the data corresponds to an existing row through a UNIQUE or PRIMARY index constraints, it updates the row instead. * * @param string|array $table Table name or array [schema => table] * @param array $param parameter array