X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FDatabase%2FDatabase.php;h=671425f9d188c8d19a42f0544b177db7aa9a98d6;hb=e8fee5644b5e746f8c09e604caac3d678f376989;hp=b5aed6c18a5f70f05444c31c0a55e4b0d4be92c7;hpb=335be6f0326e15e80d4470179c69c2a4daae6ae3;p=friendica.git diff --git a/src/Database/Database.php b/src/Database/Database.php index b5aed6c18a..671425f9d1 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -135,12 +135,12 @@ class Database if ($charset) { $connect .= ";charset=" . $charset; } - - if ($socket) { - $connect .= ";$unix_socket=" . $socket; - } - - try { + + 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); $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); @@ -165,10 +165,10 @@ class Database if ($charset) { $this->connection->set_charset($charset); } - - if ($socket) { - $this->connection->set_socket($socket); - } + + if ($socket) { + $this->connection->set_socket($socket); + } } } @@ -1153,7 +1153,7 @@ class Database * * @return boolean Was the command executed successfully? */ - public function transaction() + public function transaction(): bool { if (!$this->performCommit()) { return false; @@ -1284,11 +1284,12 @@ class Database * @param array $fields contains the fields that are updated * @param array $condition condition array with the key values * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate, false = don't update identical fields) + * @param array $params Parameters: "ignore" If set to "true" then the update is done with the ignore parameter * * @return boolean was the update successfull? * @throws \Exception */ - public function update($table, $fields, $condition, $old_fields = []) + public function update($table, $fields, $condition, $old_fields = [], $params = []) { if (empty($table) || empty($fields) || empty($condition)) { $this->logger->info('Table, fields and condition have to be set'); @@ -1325,7 +1326,13 @@ class Database $condition_string = DBA::buildCondition($condition); - $sql = "UPDATE " . $table_string . " SET " + if (!empty($params['ignore'])) { + $ignore = 'IGNORE '; + } else { + $ignore = ''; + } + + $sql = "UPDATE " . $ignore . $table_string . " SET " . implode(" = ?, ", array_map([DBA::class, 'quoteIdentifier'], array_keys($fields))) . " = ?" . $condition_string; @@ -1790,4 +1797,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()); + } + } }