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\Logger;
use Friendica\Database\Connection\IConnection;
use Friendica\Database\Driver\IDriver;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Profiler;
use mysqli_result;
@ -71,7 +71,7 @@ class DBA
*/
public static function disconnect()
{
self::$db->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);
}
/**

View File

@ -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)) {

View File

@ -1,8 +1,8 @@
<?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

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
namespace Friendica\Database\Connection;
namespace Friendica\Database\Driver;
interface IConnection
interface IDriver
{
/**
* Connecting to the current database
@ -97,7 +97,7 @@ interface IConnection
*
* @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 = []);
@ -108,7 +108,7 @@ interface IConnection
*
* @return int
*
* @throws ConnectionException In case the statement isn't valid
* @throws DriverException In case the statement isn't valid
*/
function getNumRows($stmt);
}

View File

@ -1,13 +1,13 @@
<?php
namespace Friendica\Database\Connection;
namespace Friendica\Database\Driver;
use Friendica\Database\Database;
use mysqli;
use mysqli_result;
use mysqli_stmt;
class MysqlIConnection extends AbstractConnection implements IConnection
class MySQLiDriver extends AbstractDriver implements IDriver
{
/**
* The connection to the database
@ -138,7 +138,7 @@ class MysqlIConnection extends AbstractConnection implements IConnection
*
* @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)
{
@ -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));
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');
}
}

View File

@ -1,11 +1,11 @@
<?php
namespace Friendica\Database\Connection;
namespace Friendica\Database\Driver;
use PDO;
use PDOStatement;
class PDOConnection extends AbstractConnection implements IConnection
class PDODriver extends AbstractDriver implements IDriver
{
/**
* The connection to the database
@ -145,14 +145,14 @@ class PDOConnection extends AbstractConnection implements IConnection
if (count($args) == 0) {
if (!$retval = $this->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');
}
}

View File

@ -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

View File

@ -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);
}