diff --git a/src/Database/Connection/ConnectionException.php b/src/Database/Connection/ConnectionException.php deleted file mode 100644 index 9cae961c10..0000000000 --- a/src/Database/Connection/ConnectionException.php +++ /dev/null @@ -1,13 +0,0 @@ -getConnection()->disconnect(); + self::$db->getDriver()->disconnect(); } /** @@ -79,16 +79,17 @@ class DBA */ public static function reconnect() { - return self::$db->getConnection()->reconnect(); + return self::$db->getDriver()->reconnect(); } /** * Return the database object. - * @return IConnection + * + * @return IDriver */ public static function getConnection() { - return self::$db->getConnection(); + return self::$db->getDriver(); } /** @@ -101,7 +102,7 @@ class DBA */ public static function serverInfo() { - return self::$db->getConnection()->getServerInfo(); + return self::$db->getDriver()->getServerInfo(); } /** @@ -174,12 +175,12 @@ class DBA public static function escape($sql) { - return self::$db->getConnection()->escape($sql); + return self::$db->getDriver()->escape($sql); } public static function connected() { - return self::$db->getConnection()->isConnected(true); + return self::$db->getDriver()->isConnected(true); } /** diff --git a/src/Database/Database.php b/src/Database/Database.php index 44c0ee499d..f0c8d775d7 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4,10 +4,9 @@ namespace Friendica\Database; use Friendica\Core\Config\Cache\IConfigCache; use Friendica\Core\System; -use Friendica\Database\Connection\ConnectionException; -use Friendica\Database\Connection\IConnection; +use Friendica\Database\Driver\DriverException; +use Friendica\Database\Driver\IDriver; use Friendica\Util\DateTimeFormat; -use Friendica\Util\Logger\VoidLogger; use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; @@ -27,9 +26,10 @@ class Database implements IDatabase, IDatabaseLock /** * The connection instance of the database - * @var IConnection + * + * @var IDriver */ - private $connection; + private $driver; /** * The logger instance @@ -49,14 +49,12 @@ class Database implements IDatabase, IDatabaseLock */ private $dbRelation; - public function __construct(IConnection $connection, IConfigCache $configCache, Profiler $profiler) + public function __construct(IDriver $connection, IConfigCache $configCache, Profiler $profiler, LoggerInterface $logger) { - // At startup, the logger isn't set - $this->logger = new VoidLogger(); $this->configCache = $configCache; $this->profiler = $profiler; - $this->connection = $connection; - $this->connection->connect(); + $this->driver = $connection; + $this->driver->connect(); } /** @@ -70,9 +68,9 @@ class Database implements IDatabase, IDatabaseLock /** * {@inheritDoc} */ - public function getConnection() + public function getDriver() { - return $this->connection; + return $this->driver; } /** @@ -97,7 +95,7 @@ class Database implements IDatabase, IDatabaseLock return false; } - $ret = $this->connection->closeStatement($stmt); + $ret = $this->driver->closeStatement($stmt); $this->profiler->saveTimestamp($stamp1, 'database', System::callstack()); @@ -109,8 +107,8 @@ class Database implements IDatabase, IDatabaseLock */ public function transaction() { - if (!$this->connection->performCommit() || - !$this->connection->startTransaction()) { + if (!$this->driver->performCommit() || + !$this->driver->startTransaction()) { return false; } @@ -140,7 +138,7 @@ class Database implements IDatabase, IDatabaseLock return false; } - $columns = $this->getConnection()->fetch($stmt); + $columns = $this->getDriver()->fetch($stmt); $this->profiler->saveTimestamp($stamp1, 'database', System::callstack()); @@ -152,7 +150,7 @@ class Database implements IDatabase, IDatabaseLock */ public function commit() { - $conn = $this->connection; + $conn = $this->driver; if (!$conn->performCommit()) { return false; @@ -167,7 +165,7 @@ class Database implements IDatabase, IDatabaseLock */ public function rollback() { - $conn = $this->connection; + $conn = $this->driver; $ret = $conn->rollbackTransaction(); @@ -186,7 +184,7 @@ class Database implements IDatabase, IDatabaseLock */ public function delete($table, array $conditions, $cascade = true, array &$callstack = []) { - $conn = $this->connection; + $conn = $this->driver; $logger = $this->logger; if (empty($table) || empty($conditions)) { @@ -321,7 +319,7 @@ class Database implements IDatabase, IDatabaseLock */ public function update($table, array $fields, array $condition, $old_fields = []) { - $conn = $this->connection; + $conn = $this->driver; $logger = $this->logger; if (empty($table) || empty($fields) || empty($condition)) { @@ -378,7 +376,7 @@ class Database implements IDatabase, IDatabaseLock */ public function select($table, array $fields = [], array $condition = [], array $params = []) { - $conn = $this->connection; + $conn = $this->driver; if ($table == '') { return false; @@ -438,7 +436,7 @@ class Database implements IDatabase, IDatabaseLock $logger = $this->logger; $profiler = $this->profiler; $config = $this->configCache; - $conn = $this->connection; + $conn = $this->driver; $stamp1 = microtime(true); @@ -492,7 +490,7 @@ class Database implements IDatabase, IDatabaseLock $retval = $conn->executePrepared($sql, $args); self::$affected_rows = $conn->getNumRows($retval); - } catch (ConnectionException $exception) { + } catch (DriverException $exception) { // We are having an own error logging in the function "e" if (($exception->getCode() != 0) && !$called_from_e) { // We have to preserve the error code, somewhere in the logging it get lost @@ -552,7 +550,7 @@ class Database implements IDatabase, IDatabaseLock */ public function execute($sql) { - $conn = $this->connection; + $conn = $this->driver; $profiler = $this->profiler; $logger = $this->logger; @@ -577,7 +575,7 @@ class Database implements IDatabase, IDatabaseLock } $this->close($stmt); - } catch (ConnectionException $exception) { + } catch (DriverException $exception) { $errorno = $exception->getCode(); } @@ -768,7 +766,7 @@ class Database implements IDatabase, IDatabaseLock */ private function escapeArrayCallback(&$value, $key, $add_quotation) { - $conn = $this->connection; + $conn = $this->driver; if (!$add_quotation) { if (is_bool($value)) { diff --git a/src/Database/Connection/AbstractConnection.php b/src/Database/Driver/AbstractDriver.php similarity index 92% rename from src/Database/Connection/AbstractConnection.php rename to src/Database/Driver/AbstractDriver.php index 1f229bd079..ba16dcd680 100644 --- a/src/Database/Connection/AbstractConnection.php +++ b/src/Database/Driver/AbstractDriver.php @@ -1,8 +1,8 @@ connection->query(Database::replaceParameters($sql, $args)); if ($this->connection->errno) { - throw new ConnectionException($this->connection->error, $this->connection->errno); + throw new DriverException($this->connection->error, $this->connection->errno); } return $retval; @@ -202,7 +202,7 @@ class MysqlIConnection extends AbstractConnection implements IConnection $stmt = $this->connection->stmt_init(); if (!$stmt->prepare($sql)) { - throw new ConnectionException($this->connection->error, $this->connection->errno); + throw new DriverException($this->connection->error, $this->connection->errno); } $param_types = ''; @@ -226,7 +226,7 @@ class MysqlIConnection extends AbstractConnection implements IConnection } if (!$stmt->execute()) { - throw new ConnectionException($this->connection->error, $this->connection->errno); + throw new DriverException($this->connection->error, $this->connection->errno); } else { $stmt->store_result(); return $stmt; @@ -253,7 +253,7 @@ class MysqlIConnection extends AbstractConnection implements IConnection return $stmt->num_rows; } } else { - throw new ConnectionException('Wrong statement for this connection'); + throw new DriverException('Wrong statement for this connection'); } } diff --git a/src/Database/Connection/PDOConnection.php b/src/Database/Driver/PDODriver.php similarity index 89% rename from src/Database/Connection/PDOConnection.php rename to src/Database/Driver/PDODriver.php index 52a4b12e8c..823dbda434 100644 --- a/src/Database/Connection/PDOConnection.php +++ b/src/Database/Driver/PDODriver.php @@ -1,11 +1,11 @@ connection->query($sql)) { $errorInfo = $this->connection->errorInfo(); - throw new ConnectionException($errorInfo[2], $errorInfo[1]); + throw new DriverException($errorInfo[2], $errorInfo[1]); } return $retval; } if (!$stmt = $this->connection->prepare($sql)) { $errorInfo = $this->connection->errorInfo(); - throw new ConnectionException($errorInfo[2], $errorInfo[1]); + throw new DriverException($errorInfo[2], $errorInfo[1]); } foreach ($args AS $param => $value) { @@ -166,7 +166,7 @@ class PDOConnection extends AbstractConnection implements IConnection if (!$stmt->execute()) { $errorInfo = $stmt->errorInfo(); - throw new ConnectionException($errorInfo[2], $errorInfo[1]); + throw new DriverException($errorInfo[2], $errorInfo[1]); } else { return $stmt; } @@ -182,7 +182,7 @@ class PDOConnection extends AbstractConnection implements IConnection if ($stmt instanceof PDOStatement) { return $stmt->rowCount(); } else { - throw new ConnectionException('Wrong statement for this connection'); + throw new DriverException('Wrong statement for this connection'); } } diff --git a/src/Database/IDatabase.php b/src/Database/IDatabase.php index 1e641c0df3..d5dbbe3db7 100644 --- a/src/Database/IDatabase.php +++ b/src/Database/IDatabase.php @@ -2,7 +2,7 @@ namespace Friendica\Database; -use Friendica\Database\Connection\IConnection; +use Friendica\Database\Driver\IDriver; use Psr\Log\LoggerInterface; interface IDatabase @@ -15,9 +15,10 @@ interface IDatabase /** * Returns the current connection of the database - * @return IConnection + * + * @return IDriver */ - function getConnection(); + function getDriver(); /** * Returns the selected database name diff --git a/src/Factory/DBFactory.php b/src/Factory/DBFactory.php index 733dc67cdd..7cbe87af04 100644 --- a/src/Factory/DBFactory.php +++ b/src/Factory/DBFactory.php @@ -4,6 +4,7 @@ namespace Friendica\Factory; use Friendica\Core\Config\Cache; use Friendica\Database; +use Friendica\Util\Logger\VoidLogger; use Friendica\Util\Profiler; use PDO; @@ -51,18 +52,18 @@ class DBFactory } if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) { - $dbConnection = new Database\Connection\PDOConnection($db_host, $db_user, $db_pass, $db_data, $charset); + $driver = new Database\Driver\PDODriver($db_host, $db_user, $db_pass, $db_data, $charset); } elseif (class_exists('\mysqli')) { - $dbConnection = new Database\Connection\MysqlIConnection($db_host, $db_user, $db_pass, $db_data, $charset); + $driver = new Database\Driver\MySQLiDriver($db_host, $db_user, $db_pass, $db_data, $charset); } else { throw new \Exception('Missing connection environment'); } unset($db_host, $db_user, $db_pass, $db_data, $charset); - $database = new Database\Database($dbConnection, $configCache, $profiler); + $database = new Database\Database($driver, $configCache, $profiler, new VoidLogger()); - if ($dbConnection->isConnected()) { + if ($driver->isConnected()) { // Loads DB_UPDATE_VERSION constant Database\DBStructure::definition($basePath, false); }