namespace Friendica\Database;
-use Exception;
use Friendica\Core\Config\Cache;
use Friendica\Core\System;
-use Friendica\DI;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Profiler;
$this->profiler = $profiler;
$this->logger = $logger;
- $this->readServerVariables($server);
$this->connect();
if ($this->isConnected()) {
}
}
- private function readServerVariables(array $server)
- {
- // Use environment variables for mysql if they are set beforehand
- if (!empty($server['MYSQL_HOST'])
- && (!empty($server['MYSQL_USERNAME']) || !empty($server['MYSQL_USER']))
- && $server['MYSQL_PASSWORD'] !== false
- && !empty($server['MYSQL_DATABASE']))
- {
- $db_host = $server['MYSQL_HOST'];
- if (!empty($server['MYSQL_PORT'])) {
- $db_host .= ':' . $server['MYSQL_PORT'];
- }
- $this->configCache->set('database', 'hostname', $db_host);
- unset($db_host);
- if (!empty($server['MYSQL_USERNAME'])) {
- $this->configCache->set('database', 'username', $server['MYSQL_USERNAME']);
- } else {
- $this->configCache->set('database', 'username', $server['MYSQL_USER']);
- }
- $this->configCache->set('database', 'password', (string) $server['MYSQL_PASSWORD']);
- $this->configCache->set('database', 'database', $server['MYSQL_DATABASE']);
- }
- }
-
public function connect()
{
if (!is_null($this->connection) && $this->connected()) {
if (count($serverdata) > 1) {
$port = trim($serverdata[1]);
}
+
+ if (!empty(trim($this->configCache->get('database', 'port')))) {
+ $port = trim(trim($this->configCache->get('database', 'port')));
+ }
+
$server = trim($server);
$user = trim($this->configCache->get('database', 'username'));
$pass = trim($this->configCache->get('database', 'password'));
$errorno = $this->errorno;
if ($this->testmode) {
- throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $args)));
+ throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $args));
}
$this->logger->error('DB Error', [
$errorno = $this->errorno;
if ($this->testmode) {
- throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $params)));
+ throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $params));
}
$this->logger->error('DB Error', [
--- /dev/null
+<?php
+declare(strict_types=1);
+
+namespace Friendica\Database;
+
+use Exception;
+use Throwable;
+
+/**
+ * A database fatal exception, which shouldn't occur
+ */
+class DatabaseException extends Exception
+{
+ protected $query;
+
+ /**
+ * Construct the exception. Note: The message is NOT binary safe.
+ *
+ * @link https://php.net/manual/en/exception.construct.php
+ *
+ * @param string $message The Database error message.
+ * @param int $code The Database error code.
+ * @param string $query The Database error query.
+ * @param Throwable $previous [optional] The previous throwable used for the exception chaining.
+ */
+ public function __construct(string $message, int $code, string $query, Throwable $previous = null)
+ {
+ parent::__construct($message, $code, $previous);
+ $this->query = $query;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function __toString()
+ {
+ return sprintf('Database error %d "%s" at "%s"', $this->message, $this->code, $this->query);
+ }
+}