]> git.mxchange.org Git - friendica.git/commitdiff
Added support for the "replace" database command
authorMichael <heluecht@pirati.ca>
Wed, 26 Aug 2020 20:16:57 +0000 (20:16 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 26 Aug 2020 20:16:57 +0000 (20:16 +0000)
src/Database/DBA.php
src/Database/Database.php

index 46bd871b4a748838beeaf04f706b204f6369f6b2..9debdf02a53cfb8512c1964f30d849197e0defb6 100644 (file)
@@ -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
         *
index 4b96205a9adbb8c3bafde329a9b64e7ee76b43d5..609b86d0e5dae85c3a8f7ef316ac150da8e6dbe4 100644 (file)
@@ -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
         *