Renaming Connection to Driver

This commit is contained in:
Philipp Holzer 2019-04-11 08:32:07 +02:00
parent 957d41b58f
commit 2a6513436c
No known key found for this signature in database
GPG Key ID: 517BE60E2CE5C8A5
10 changed files with 75 additions and 74 deletions

View File

@ -1,13 +0,0 @@
<?php
namespace Friendica\Database\Connection;
use Exception;
/**
* A DB connection exception
*/
class ConnectionException extends Exception
{
}

View File

@ -4,7 +4,7 @@ namespace Friendica\Database;
use Friendica\Core\Config\Cache\IConfigCache; use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Database\Connection\IConnection; use Friendica\Database\Driver\IDriver;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use mysqli_result; use mysqli_result;
@ -71,7 +71,7 @@ class DBA
*/ */
public static function disconnect() public static function disconnect()
{ {
self::$db->getConnection()->disconnect(); self::$db->getDriver()->disconnect();
} }
/** /**
@ -79,16 +79,17 @@ class DBA
*/ */
public static function reconnect() public static function reconnect()
{ {
return self::$db->getConnection()->reconnect(); return self::$db->getDriver()->reconnect();
} }
/** /**
* Return the database object. * Return the database object.
* @return IConnection *
* @return IDriver
*/ */
public static function getConnection() public static function getConnection()
{ {
return self::$db->getConnection(); return self::$db->getDriver();
} }
/** /**
@ -101,7 +102,7 @@ class DBA
*/ */
public static function serverInfo() 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) public static function escape($sql)
{ {
return self::$db->getConnection()->escape($sql); return self::$db->getDriver()->escape($sql);
} }
public static function connected() public static function connected()
{ {
return self::$db->getConnection()->isConnected(true); return self::$db->getDriver()->isConnected(true);
} }
/** /**

View File

@ -4,10 +4,9 @@ namespace Friendica\Database;
use Friendica\Core\Config\Cache\IConfigCache; use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\Connection\ConnectionException; use Friendica\Database\Driver\DriverException;
use Friendica\Database\Connection\IConnection; use Friendica\Database\Driver\IDriver;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Logger\VoidLogger;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -27,9 +26,10 @@ class Database implements IDatabase, IDatabaseLock
/** /**
* The connection instance of the database * The connection instance of the database
* @var IConnection *
* @var IDriver
*/ */
private $connection; private $driver;
/** /**
* The logger instance * The logger instance
@ -49,14 +49,12 @@ class Database implements IDatabase, IDatabaseLock
*/ */
private $dbRelation; 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->configCache = $configCache;
$this->profiler = $profiler; $this->profiler = $profiler;
$this->connection = $connection; $this->driver = $connection;
$this->connection->connect(); $this->driver->connect();
} }
/** /**
@ -70,9 +68,9 @@ class Database implements IDatabase, IDatabaseLock
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getConnection() public function getDriver()
{ {
return $this->connection; return $this->driver;
} }
/** /**
@ -97,7 +95,7 @@ class Database implements IDatabase, IDatabaseLock
return false; return false;
} }
$ret = $this->connection->closeStatement($stmt); $ret = $this->driver->closeStatement($stmt);
$this->profiler->saveTimestamp($stamp1, 'database', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
@ -109,8 +107,8 @@ class Database implements IDatabase, IDatabaseLock
*/ */
public function transaction() public function transaction()
{ {
if (!$this->connection->performCommit() || if (!$this->driver->performCommit() ||
!$this->connection->startTransaction()) { !$this->driver->startTransaction()) {
return false; return false;
} }
@ -140,7 +138,7 @@ class Database implements IDatabase, IDatabaseLock
return false; return false;
} }
$columns = $this->getConnection()->fetch($stmt); $columns = $this->getDriver()->fetch($stmt);
$this->profiler->saveTimestamp($stamp1, 'database', System::callstack()); $this->profiler->saveTimestamp($stamp1, 'database', System::callstack());
@ -152,7 +150,7 @@ class Database implements IDatabase, IDatabaseLock
*/ */
public function commit() public function commit()
{ {
$conn = $this->connection; $conn = $this->driver;
if (!$conn->performCommit()) { if (!$conn->performCommit()) {
return false; return false;
@ -167,7 +165,7 @@ class Database implements IDatabase, IDatabaseLock
*/ */
public function rollback() public function rollback()
{ {
$conn = $this->connection; $conn = $this->driver;
$ret = $conn->rollbackTransaction(); $ret = $conn->rollbackTransaction();
@ -186,7 +184,7 @@ class Database implements IDatabase, IDatabaseLock
*/ */
public function delete($table, array $conditions, $cascade = true, array &$callstack = []) public function delete($table, array $conditions, $cascade = true, array &$callstack = [])
{ {
$conn = $this->connection; $conn = $this->driver;
$logger = $this->logger; $logger = $this->logger;
if (empty($table) || empty($conditions)) { if (empty($table) || empty($conditions)) {
@ -321,7 +319,7 @@ class Database implements IDatabase, IDatabaseLock
*/ */
public function update($table, array $fields, array $condition, $old_fields = []) public function update($table, array $fields, array $condition, $old_fields = [])
{ {
$conn = $this->connection; $conn = $this->driver;
$logger = $this->logger; $logger = $this->logger;
if (empty($table) || empty($fields) || empty($condition)) { 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 = []) public function select($table, array $fields = [], array $condition = [], array $params = [])
{ {
$conn = $this->connection; $conn = $this->driver;
if ($table == '') { if ($table == '') {
return false; return false;
@ -438,7 +436,7 @@ class Database implements IDatabase, IDatabaseLock
$logger = $this->logger; $logger = $this->logger;
$profiler = $this->profiler; $profiler = $this->profiler;
$config = $this->configCache; $config = $this->configCache;
$conn = $this->connection; $conn = $this->driver;
$stamp1 = microtime(true); $stamp1 = microtime(true);
@ -492,7 +490,7 @@ class Database implements IDatabase, IDatabaseLock
$retval = $conn->executePrepared($sql, $args); $retval = $conn->executePrepared($sql, $args);
self::$affected_rows = $conn->getNumRows($retval); self::$affected_rows = $conn->getNumRows($retval);
} catch (ConnectionException $exception) { } catch (DriverException $exception) {
// We are having an own error logging in the function "e" // We are having an own error logging in the function "e"
if (($exception->getCode() != 0) && !$called_from_e) { if (($exception->getCode() != 0) && !$called_from_e) {
// We have to preserve the error code, somewhere in the logging it get lost // 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) public function execute($sql)
{ {
$conn = $this->connection; $conn = $this->driver;
$profiler = $this->profiler; $profiler = $this->profiler;
$logger = $this->logger; $logger = $this->logger;
@ -577,7 +575,7 @@ class Database implements IDatabase, IDatabaseLock
} }
$this->close($stmt); $this->close($stmt);
} catch (ConnectionException $exception) { } catch (DriverException $exception) {
$errorno = $exception->getCode(); $errorno = $exception->getCode();
} }
@ -768,7 +766,7 @@ class Database implements IDatabase, IDatabaseLock
*/ */
private function escapeArrayCallback(&$value, $key, $add_quotation) private function escapeArrayCallback(&$value, $key, $add_quotation)
{ {
$conn = $this->connection; $conn = $this->driver;
if (!$add_quotation) { if (!$add_quotation) {
if (is_bool($value)) { if (is_bool($value)) {

View File

@ -1,8 +1,8 @@
<?php <?php
namespace Friendica\Database\Connection; namespace Friendica\Database\Driver;
abstract class AbstractConnection implements IConnection abstract class AbstractDriver implements IDriver
{ {
/** /**
* The connection state of the database * The connection state of the database

View File

@ -0,0 +1,13 @@
<?php
namespace Friendica\Database\Driver;
use Exception;
/**
* A DB connection exception
*/
class DriverException extends Exception
{
}

View File

@ -1,10 +1,10 @@
<?php <?php
namespace Friendica\Database\Connection; namespace Friendica\Database\Driver;
interface IConnection interface IDriver
{ {
/** /**
* Connecting to the current database * Connecting to the current database
@ -97,7 +97,7 @@ interface IConnection
* *
* @return object * @return object
* *
* @throws ConnectionException In case the execution doesn't work * @throws DriverException In case the execution doesn't work
*/ */
function executePrepared($sql, array $args = []); function executePrepared($sql, array $args = []);
@ -108,7 +108,7 @@ interface IConnection
* *
* @return int * @return int
* *
* @throws ConnectionException In case the statement isn't valid * @throws DriverException In case the statement isn't valid
*/ */
function getNumRows($stmt); function getNumRows($stmt);
} }

View File

@ -1,13 +1,13 @@
<?php <?php
namespace Friendica\Database\Connection; namespace Friendica\Database\Driver;
use Friendica\Database\Database; use Friendica\Database\Database;
use mysqli; use mysqli;
use mysqli_result; use mysqli_result;
use mysqli_stmt; use mysqli_stmt;
class MysqlIConnection extends AbstractConnection implements IConnection class MySQLiDriver extends AbstractDriver implements IDriver
{ {
/** /**
* The connection to the database * The connection to the database
@ -138,7 +138,7 @@ class MysqlIConnection extends AbstractConnection implements IConnection
* *
* @param mysqli_stmt|mysqli_result * @param mysqli_stmt|mysqli_result
* *
* @throws ConnectionException In case of a wrong statement * @throws DriverException In case of a wrong statement
*/ */
public function fetch($stmt) public function fetch($stmt)
{ {
@ -174,7 +174,7 @@ class MysqlIConnection extends AbstractConnection implements IConnection
} }
} }
throw new ConnectionException('Wrong statement for this connection'); throw new DriverException('Wrong statement for this connection');
} }
/** /**
@ -193,7 +193,7 @@ class MysqlIConnection extends AbstractConnection implements IConnection
$retval = $this->connection->query(Database::replaceParameters($sql, $args)); $retval = $this->connection->query(Database::replaceParameters($sql, $args));
if ($this->connection->errno) { if ($this->connection->errno) {
throw new ConnectionException($this->connection->error, $this->connection->errno); throw new DriverException($this->connection->error, $this->connection->errno);
} }
return $retval; return $retval;
@ -202,7 +202,7 @@ class MysqlIConnection extends AbstractConnection implements IConnection
$stmt = $this->connection->stmt_init(); $stmt = $this->connection->stmt_init();
if (!$stmt->prepare($sql)) { if (!$stmt->prepare($sql)) {
throw new ConnectionException($this->connection->error, $this->connection->errno); throw new DriverException($this->connection->error, $this->connection->errno);
} }
$param_types = ''; $param_types = '';
@ -226,7 +226,7 @@ class MysqlIConnection extends AbstractConnection implements IConnection
} }
if (!$stmt->execute()) { if (!$stmt->execute()) {
throw new ConnectionException($this->connection->error, $this->connection->errno); throw new DriverException($this->connection->error, $this->connection->errno);
} else { } else {
$stmt->store_result(); $stmt->store_result();
return $stmt; return $stmt;
@ -253,7 +253,7 @@ class MysqlIConnection extends AbstractConnection implements IConnection
return $stmt->num_rows; return $stmt->num_rows;
} }
} else { } else {
throw new ConnectionException('Wrong statement for this connection'); throw new DriverException('Wrong statement for this connection');
} }
} }

View File

@ -1,11 +1,11 @@
<?php <?php
namespace Friendica\Database\Connection; namespace Friendica\Database\Driver;
use PDO; use PDO;
use PDOStatement; use PDOStatement;
class PDOConnection extends AbstractConnection implements IConnection class PDODriver extends AbstractDriver implements IDriver
{ {
/** /**
* The connection to the database * The connection to the database
@ -145,14 +145,14 @@ class PDOConnection extends AbstractConnection implements IConnection
if (count($args) == 0) { if (count($args) == 0) {
if (!$retval = $this->connection->query($sql)) { if (!$retval = $this->connection->query($sql)) {
$errorInfo = $this->connection->errorInfo(); $errorInfo = $this->connection->errorInfo();
throw new ConnectionException($errorInfo[2], $errorInfo[1]); throw new DriverException($errorInfo[2], $errorInfo[1]);
} }
return $retval; return $retval;
} }
if (!$stmt = $this->connection->prepare($sql)) { if (!$stmt = $this->connection->prepare($sql)) {
$errorInfo = $this->connection->errorInfo(); $errorInfo = $this->connection->errorInfo();
throw new ConnectionException($errorInfo[2], $errorInfo[1]); throw new DriverException($errorInfo[2], $errorInfo[1]);
} }
foreach ($args AS $param => $value) { foreach ($args AS $param => $value) {
@ -166,7 +166,7 @@ class PDOConnection extends AbstractConnection implements IConnection
if (!$stmt->execute()) { if (!$stmt->execute()) {
$errorInfo = $stmt->errorInfo(); $errorInfo = $stmt->errorInfo();
throw new ConnectionException($errorInfo[2], $errorInfo[1]); throw new DriverException($errorInfo[2], $errorInfo[1]);
} else { } else {
return $stmt; return $stmt;
} }
@ -182,7 +182,7 @@ class PDOConnection extends AbstractConnection implements IConnection
if ($stmt instanceof PDOStatement) { if ($stmt instanceof PDOStatement) {
return $stmt->rowCount(); return $stmt->rowCount();
} else { } else {
throw new ConnectionException('Wrong statement for this connection'); throw new DriverException('Wrong statement for this connection');
} }
} }

View File

@ -2,7 +2,7 @@
namespace Friendica\Database; namespace Friendica\Database;
use Friendica\Database\Connection\IConnection; use Friendica\Database\Driver\IDriver;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
interface IDatabase interface IDatabase
@ -15,9 +15,10 @@ interface IDatabase
/** /**
* Returns the current connection of the database * Returns the current connection of the database
* @return IConnection *
* @return IDriver
*/ */
function getConnection(); function getDriver();
/** /**
* Returns the selected database name * Returns the selected database name

View File

@ -4,6 +4,7 @@ namespace Friendica\Factory;
use Friendica\Core\Config\Cache; use Friendica\Core\Config\Cache;
use Friendica\Database; use Friendica\Database;
use Friendica\Util\Logger\VoidLogger;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use PDO; use PDO;
@ -51,18 +52,18 @@ class DBFactory
} }
if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) { 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')) { } 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 { } else {
throw new \Exception('Missing connection environment'); throw new \Exception('Missing connection environment');
} }
unset($db_host, $db_user, $db_pass, $db_data, $charset); 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 // Loads DB_UPDATE_VERSION constant
Database\DBStructure::definition($basePath, false); Database\DBStructure::definition($basePath, false);
} }