$connect .= ";charset=" . $charset;
}
- if ($socket) {
- $connect .= ";$unix_socket=" . $socket;
- }
+ if ($socket) {
+ $connect .= ";$unix_socket=" . $socket;
+ }
- try {
+ 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);
if ($charset) {
$this->connection->set_charset($charset);
}
-
- if ($socket) {
- $this->connection->set_socket($socket);
- }
+
+ if ($socket) {
+ $this->connection->set_socket($socket);
+ }
}
}
{
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());
+ }
+ }
}