]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/Database.php
Move relocation feature in its separate class
[friendica.git] / src / Database / Database.php
index 88d8d7d0f6ef87baa78cbc7f63dcd2cceeb65c15..711d546d803a7d23dbdd2ac42490e0aa8e38b8e9 100644 (file)
@@ -1790,4 +1790,32 @@ class Database
        {
                array_walk($arr, [$this, 'escapeArrayCallback'], $add_quotation);
        }
+
+       /**
+        * Replaces a string in the provided fields of the provided table
+        *
+        * @param string $table_name
+        * @param array  $fields List of field names in the provided table
+        * @param string $search
+        * @param string $replace
+        * @throws \Exception
+        */
+       public function replaceInTableFields(string $table_name, array $fields, string $search, string $replace)
+       {
+               $search = $this->escape($search);
+               $replace = $this->escape($replace);
+
+               $upd = [];
+               foreach ($fields as $field) {
+                       $field = DBA::quoteIdentifier($field);
+                       $upd[] = "$field = REPLACE($field, '$search', '$replace')";
+               }
+
+               $upds = implode(', ', $upd);
+
+               $r = $this->e(sprintf("UPDATE %s SET %s;", $table_name, $upds));
+               if (!$this->isResult($r)) {
+                       throw new \RuntimeException("Failed updating `$table_name`: " . $this->errorMessage());
+               }
+       }
 }