Initial DB Wrapper

This commit is contained in:
Philipp Holzer 2019-04-14 18:36:21 +02:00
parent e0911efc87
commit 5275cff11a
No known key found for this signature in database
GPG Key ID: 517BE60E2CE5C8A5
11 changed files with 1895 additions and 1340 deletions

View File

@ -10,7 +10,6 @@
use Friendica\Core\Config;
use Friendica\Core\Logger;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Factory;
// Get options
@ -128,7 +127,7 @@ if (!$foreground) {
// fclose(STDOUT); // file descriptors as we
// fclose(STDERR); // are running as a daemon.
DBA::disconnect();
$a->getDatabase()->disconnect();
register_shutdown_function('shutdown');
@ -144,7 +143,7 @@ if (!$foreground) {
file_put_contents($pidfile, $pid);
// We lose the database connection upon forking
Factory\DBFactory::init($a->getConfigCache(), $a->getProfiler(), $_SERVER);
$a->getDatabase()->reconnect();
}
Config::set('system', 'worker_daemon_mode', true);
@ -169,7 +168,7 @@ while (true) {
if ($do_cron) {
// We force a reconnect of the database connection.
// This is done to ensure that the connection don't get lost over time.
DBA::reconnect();
$a->getDatabase()->reconnect();
$last_cron = time();
}

View File

@ -12,6 +12,7 @@ use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Core\Config\Configuration;
use Friendica\Core\Hook;
use Friendica\Core\Theme;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Model\Profile;
use Friendica\Network\HTTPException\InternalServerErrorException;
@ -123,6 +124,19 @@ class App
*/
private $logger;
/**
* @var Database The database
*/
private $database;
/**
* @return Database The database connection of this node
*/
public function getDatabase()
{
return $this->database;
}
/**
* @var Profiler The profiler of this app
*/
@ -238,6 +252,7 @@ class App
* @brief App constructor.
*
* @param Configuration $config The Configuration
* @param Database $database The DB instance
* @param App\Mode $mode The mode of this Friendica app
* @param App\Router $router The router of this Friendica app
* @param BaseURL $baseURL The full base URL of this Friendica app
@ -247,11 +262,12 @@ class App
*
* @throws Exception if the Basepath is not usable
*/
public function __construct(Configuration $config, App\Mode $mode, App\Router $router, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
public function __construct(Configuration $config, Database $database, App\Mode $mode, App\Router $router, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
{
BaseObject::setApp($this);
$this->config = $config;
$this->database = $database;
$this->mode = $mode;
$this->router = $router;
$this->baseURL = $baseURL;

View File

@ -7,6 +7,7 @@ namespace Friendica\Core;
use DOMDocument;
use Exception;
use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\Object\Image;
@ -605,13 +606,15 @@ class Installer
$dbpass = $configCache->get('database', 'password');
$dbdata = $configCache->get('database', 'database');
if (!DBA::connect($configCache, $profiler, new VoidLogger(), $dbhost, $dbuser, $dbpass, $dbdata)) {
$database = new Database($configCache, $profiler, new VoidLogger(), $dbhost, $dbuser, $dbpass, $dbdata);
if (!$database->connect()) {
$this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
return false;
}
if (DBA::connected()) {
if ($database->connected()) {
if (DBStructure::existsTable('user')) {
$this->addCheck(L10n::t('Database already in use.'), false, true, '');
@ -619,6 +622,8 @@ class Installer
}
}
DBA::init($database);
return true;
}

File diff suppressed because it is too large Load Diff

1757
src/Database/Database.php Normal file

File diff suppressed because it is too large Load Diff

View File

@ -16,14 +16,12 @@ class DBFactory
* @param Profiler $profiler The profiler
* @param array $server The $_SERVER variables
*
* @return Database\Database;
*
* @throws \Exception if connection went bad
*/
public static function init(Cache\IConfigCache $configCache, Profiler $profiler, array $server)
public static function create(Cache\IConfigCache $configCache, Profiler $profiler, array $server)
{
if (Database\DBA::connected()) {
return;
}
$db_host = $configCache->get('database', 'hostname');
$db_user = $configCache->get('database', 'username');
$db_pass = $configCache->get('database', 'password');
@ -49,11 +47,17 @@ class DBFactory
$db_data = $server['MYSQL_DATABASE'];
}
if (Database\DBA::connect($configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset)) {
$database = new Database\Database($configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset);
Database\DBA::init($database);
if ($database->connect()) {
// Loads DB_UPDATE_VERSION constant
Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
}
unset($db_host, $db_user, $db_pass, $db_data, $charset);
return $database;
}
}

View File

@ -29,7 +29,7 @@ class DependencyFactory
$configLoader = new Config\ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($configCache, $profiler, $_SERVER);
$database = Factory\DBFactory::create($configCache, $profiler, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
// needed to call PConfig::init()
Factory\ConfigFactory::createPConfig($configCache);
@ -37,6 +37,6 @@ class DependencyFactory
Factory\LoggerFactory::createDev($channel, $config, $profiler);
$baseURL = new BaseURL($config, $_SERVER);
return new App($config, $mode, $router, $baseURL, $logger, $profiler, $isBackend);
return new App($config, $database, $mode, $router, $baseURL, $logger, $profiler, $isBackend);
}
}

View File

@ -6,7 +6,7 @@
namespace Friendica\Test;
use Friendica\App;
use Friendica\Database\DBA;
use Friendica\Database\Database;
use Friendica\Factory;
use Friendica\Util\BasePath;
use Friendica\Util\Config\ConfigFileLoader;
@ -49,8 +49,7 @@ abstract class DatabaseTest extends MockedTest
$profiler = \Mockery::mock(Profiler::class);
DBA::connect(
$config,
$database = new Database($config,
$profiler,
new VoidLogger(),
getenv('MYSQL_HOST'),
@ -58,11 +57,13 @@ abstract class DatabaseTest extends MockedTest
getenv('MYSQL_PASSWORD'),
getenv('MYSQL_DATABASE'));
if (!DBA::connected()) {
$database->connect();
if (!$database->connected()) {
$this->markTestSkipped('Could not connect to the database.');
}
return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
return $this->createDefaultDBConnection($database->getConnection(), getenv('MYSQL_DATABASE'));
}
/**

View File

@ -55,7 +55,7 @@ class ApiTest extends DatabaseTest
$configLoader = new ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($configCache, $profiler, $_SERVER);
Factory\DBFactory::create($configCache, $profiler, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('test', $config, $profiler);

View File

@ -20,7 +20,7 @@ class DBATest extends DatabaseTest
$configLoader = new ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($configCache, $profiler, $_SERVER);
Factory\DBFactory::create($configCache, $profiler, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('test', $config, $profiler);

View File

@ -20,7 +20,7 @@ class DBStructureTest extends DatabaseTest
$configLoader = new ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($configCache, $profiler, $_SERVER);
Factory\DBFactory::create($configCache, $profiler, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache);
Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create('test', $config, $profiler);