From 3587e89482284d81213eda21dd62415698185ab7 Mon Sep 17 00:00:00 2001 From: Philipp Date: Tue, 6 Oct 2020 20:03:38 +0200 Subject: [PATCH] Introduce a "DatabaseException" class for fatal exceptions (used in testmode to throw an exception in case of DB errors) --- src/Database/Database.php | 36 ++++++--------------------- src/Database/DatabaseException.php | 39 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 src/Database/DatabaseException.php diff --git a/src/Database/Database.php b/src/Database/Database.php index 80fd02dc0d..e25323b382 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -21,10 +21,8 @@ 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; @@ -75,7 +73,6 @@ class Database $this->profiler = $profiler; $this->logger = $logger; - $this->readServerVariables($server); $this->connect(); if ($this->isConnected()) { @@ -84,30 +81,6 @@ class Database } } - 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()) { @@ -124,6 +97,11 @@ class Database 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')); @@ -658,7 +636,7 @@ class Database $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', [ @@ -761,7 +739,7 @@ class Database $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', [ diff --git a/src/Database/DatabaseException.php b/src/Database/DatabaseException.php new file mode 100644 index 0000000000..8bf5d8a6c2 --- /dev/null +++ b/src/Database/DatabaseException.php @@ -0,0 +1,39 @@ +query = $query; + } + + /** + * {@inheritDoc} + */ + public function __toString() + { + return sprintf('Database error %d "%s" at "%s"', $this->message, $this->code, $this->query); + } +} -- 2.20.1