]> 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 effed6e5e9be972ff4e38c75b6e4492d3282af87..711d546d803a7d23dbdd2ac42490e0aa8e38b8e9 100644 (file)
@@ -114,7 +114,7 @@ class Database
                $pass    = trim($this->configCache->get('database', 'password'));
                $db      = trim($this->configCache->get('database', 'database'));
                $charset = trim($this->configCache->get('database', 'charset'));
-                $socket  = trim($this->configCache->get('database', 'socket')); 
+               $socket  = trim($this->configCache->get('database', 'socket')); 
 
                if (!(strlen($server) && strlen($user))) {
                        return false;
@@ -126,7 +126,7 @@ class Database
 
                if (!$this->configCache->get('database', 'disable_pdo') && class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
                        $this->driver = self::PDO;
-                       $connect      = "mysql:host=" . $server . ";dbname=" . $db . ";unix_socket=" . $socket;
+                       $connect      = "mysql:host=" . $server . ";dbname=" . $db;
 
                        if ($port > 0) {
                                $connect .= ";port=" . $port;
@@ -136,6 +136,10 @@ class Database
                                $connect .= ";charset=" . $charset;
                        }
 
+                       if ($socket) {
+                               $connect .= ";$unix_socket=" . $socket;
+                       }
+
                        try {
                                $this->connection = @new PDO($connect, $user, $pass, [PDO::ATTR_PERSISTENT => $persistent]);
                                $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->pdo_emulate_prepares);
@@ -150,9 +154,9 @@ class Database
                        $this->driver = self::MYSQLI;
 
                        if ($port > 0) {
-                               $this->connection = @new mysqli($server, $user, $pass, $db, $port, $socket);
+                               $this->connection = @new mysqli($server, $user, $pass, $db, $port);
                        } else {
-                               $this->connection = @new mysqli($server, $user, $pass, $db, $socket);
+                               $this->connection = @new mysqli($server, $user, $pass, $db);
                        }
 
                        if (!mysqli_connect_errno()) {
@@ -161,6 +165,11 @@ class Database
                                if ($charset) {
                                        $this->connection->set_charset($charset);
                                }
+
+                               if ($socket) {
+                                       $this->connection->set_socket($socket);
+                               }
+
                        }
                }
 
@@ -1781,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());
+               }
+       }
 }