JIT db connection implemented

This commit is contained in:
Dennis Eichhorn 2020-09-12 23:13:58 +02:00
parent 0a11f2d36b
commit c15c9b8b09
11 changed files with 55 additions and 29 deletions

View File

@ -45,7 +45,7 @@ abstract class ConnectionAbstract implements ConnectionInterface
* @var null|\PDO
* @since 1.0.0
*/
public ?\PDO $con;
protected ?\PDO $con;
/**
* Database data.
@ -186,4 +186,22 @@ abstract class ConnectionAbstract implements ConnectionInterface
$this->con = null;
$this->status = DatabaseStatus::CLOSED;
}
/**
* Get values
*
* @param string $name Variable name
*
* @return mixed Returns the value of the connection
*
* @since 1.0.0
*/
public function __get($name)
{
if ($name === 'con' && !isset($this->con)) {
$this->connect($this->dbdata);
}
return isset($this->{$name}) ? $this->{$name} : null;
}
}

View File

@ -48,13 +48,7 @@ final class MysqlConnection extends ConnectionAbstract
$this->grammar = new MysqlGrammar();
$this->schemaGrammar = new MysqlSchemaGrammar();
/**
* @todo Orange-Management/phpOMS#219
* Don't automatically connect to the database during initialization. This should be done in a separate step.
* This also requires to adjust some other framework code which currently expects the database connection to be established after initialization.
* Sometimes DB connections may not be needed and should only be connected to once required.
*/
$this->connect($dbdata);
$this->dbdata = $dbdata;
}
/**

View File

@ -48,13 +48,7 @@ final class PostgresConnection extends ConnectionAbstract
$this->grammar = new PostgresGrammar();
$this->schemaGrammar = new PostgresSchemaGrammar();
/**
* @todo Orange-Management/phpOMS#219
* Don't automatically connect to the database during initialization. This should be done in a separate step.
* This also requires to adjust some other framework code which currently expects the database connection to be established after initialization.
* Sometimes DB connections may not be needed and should only be connected to once required.
*/
$this->connect($dbdata);
$this->dbdata = $dbdata;
}
/**

View File

@ -48,13 +48,7 @@ final class SQLiteConnection extends ConnectionAbstract
$this->grammar = new SQLiteGrammar();
$this->schemaGrammar = new SQLiteSchemaGrammar();
/**
* @todo Orange-Management/phpOMS#219
* Don't automatically connect to the database during initialization. This should be done in a separate step.
* This also requires to adjust some other framework code which currently expects the database connection to be established after initialization.
* Sometimes DB connections may not be needed and should only be connected to once required.
*/
$this->connect($dbdata);
$this->dbdata = $dbdata;
}
/**

View File

@ -48,13 +48,7 @@ final class SqlServerConnection extends ConnectionAbstract
$this->grammar = new MysqlGrammar();
$this->schemaGrammar = new MysqlSchemaGrammar();
/**
* @todo Orange-Management/phpOMS#219
* Don't automatically connect to the database during initialization. This should be done in a separate step.
* This also requires to adjust some other framework code which currently expects the database connection to be established after initialization.
* Sometimes DB connections may not be needed and should only be connected to once required.
*/
$this->connect($dbdata);
$this->dbdata = $dbdata;
}
/**

View File

@ -135,6 +135,8 @@ final class SocketRouter implements RouterInterface
}
}
}
$bound[] = ['dest' => $d['dest']];
}
}

View File

@ -42,6 +42,7 @@ class MysqlConnectionTest extends \PHPUnit\Framework\TestCase
public function testConnect() : void
{
$mysql = new MysqlConnection($GLOBALS['CONFIG']['db']['core']['masters']['admin']);
$mysql->connect();
self::assertEquals(DatabaseStatus::OK, $mysql->getStatus());
self::assertEquals($GLOBALS['CONFIG']['db']['core']['masters']['admin']['database'], $mysql->getDatabase());
@ -62,6 +63,7 @@ class MysqlConnectionTest extends \PHPUnit\Framework\TestCase
unset($db['db']);
$mysql = new MysqlConnection($db);
$mysql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
@ -76,6 +78,7 @@ class MysqlConnectionTest extends \PHPUnit\Framework\TestCase
unset($db['host']);
$mysql = new MysqlConnection($db);
$mysql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
@ -90,6 +93,7 @@ class MysqlConnectionTest extends \PHPUnit\Framework\TestCase
unset($db['port']);
$mysql = new MysqlConnection($db);
$mysql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
@ -104,6 +108,7 @@ class MysqlConnectionTest extends \PHPUnit\Framework\TestCase
unset($db['database']);
$mysql = new MysqlConnection($db);
$mysql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
@ -118,6 +123,7 @@ class MysqlConnectionTest extends \PHPUnit\Framework\TestCase
unset($db['login']);
$mysql = new MysqlConnection($db);
$mysql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
@ -132,6 +138,7 @@ class MysqlConnectionTest extends \PHPUnit\Framework\TestCase
unset($db['password']);
$mysql = new MysqlConnection($db);
$mysql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
@ -146,6 +153,7 @@ class MysqlConnectionTest extends \PHPUnit\Framework\TestCase
$db['db'] = 'invalid';
$mysql = new MysqlConnection($db);
$mysql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
@ -160,6 +168,7 @@ class MysqlConnectionTest extends \PHPUnit\Framework\TestCase
$db['database'] = 'invalid';
$mysql = new MysqlConnection($db);
$mysql->connect();
self::assertEquals(DatabaseStatus::MISSING_DATABASE, $mysql->getStatus());
}
}

View File

@ -41,6 +41,8 @@ class PostgresConnectionTest extends \PHPUnit\Framework\TestCase
public function testConnect() : void
{
$psql = new PostgresConnection($GLOBALS['CONFIG']['db']['core']['postgresql']['admin']);
$psql->connect();
self::assertEquals(DatabaseStatus::OK, $psql->getStatus());
self::assertEquals($GLOBALS['CONFIG']['db']['core']['postgresql']['admin']['database'], $psql->getDatabase());
self::assertEquals($GLOBALS['CONFIG']['db']['core']['postgresql']['admin']['host'], $psql->getHost());
@ -59,6 +61,7 @@ class PostgresConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['db']);
$psql = new PostgresConnection($db);
$psql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
@ -72,6 +75,7 @@ class PostgresConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['host']);
$psql = new PostgresConnection($db);
$psql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
@ -85,6 +89,7 @@ class PostgresConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['port']);
$psql = new PostgresConnection($db);
$psql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
@ -98,6 +103,7 @@ class PostgresConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['database']);
$psql = new PostgresConnection($db);
$psql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
@ -111,6 +117,7 @@ class PostgresConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['login']);
$psql = new PostgresConnection($db);
$psql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
@ -124,6 +131,7 @@ class PostgresConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['password']);
$psql = new PostgresConnection($db);
$psql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
@ -137,6 +145,7 @@ class PostgresConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
$db['db'] = 'invalid';
$psql = new PostgresConnection($db);
$psql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
}

View File

@ -41,6 +41,7 @@ class SQLiteConnectionTest extends \PHPUnit\Framework\TestCase
public function testConnect() : void
{
$sqlite = new SQLiteConnection($GLOBALS['CONFIG']['db']['core']['sqlite']['admin']);
$sqlite->connect();
self::assertEquals(DatabaseStatus::OK, $sqlite->getStatus());
self::assertEquals($GLOBALS['CONFIG']['db']['core']['sqlite']['admin']['database'], $sqlite->getDatabase());
self::assertInstanceOf('\phpOMS\DataStorage\Database\Query\Grammar\SQLiteGrammar', $sqlite->getGrammar());
@ -57,6 +58,7 @@ class SQLiteConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['sqlite']['admin'];
unset($db['db']);
$sqlite = new SQLiteConnection($db);
$sqlite->connect();
self::assertEquals(DatabaseStatus::FAILURE, $sqlite->getStatus());
}
@ -70,6 +72,7 @@ class SQLiteConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['sqlite']['admin'];
unset($db['database']);
$sqlite = new SQLiteConnection($db);
$sqlite->connect();
self::assertEquals(DatabaseStatus::FAILURE, $sqlite->getStatus());
}

View File

@ -42,6 +42,7 @@ class SqlServerConnectionTest extends \PHPUnit\Framework\TestCase
public function testConnect() : void
{
$ssql = new SqlServerConnection($GLOBALS['CONFIG']['db']['core']['mssql']['admin']);
$ssql->connect();
self::assertEquals(DatabaseStatus::OK, $ssql->getStatus());
self::assertEquals($GLOBALS['CONFIG']['db']['core']['mssql']['admin']['database'], $ssql->getDatabase());
self::assertEquals($GLOBALS['CONFIG']['db']['core']['mssql']['admin']['host'], $ssql->getHost());
@ -60,6 +61,7 @@ class SqlServerConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['db']);
$ssql = new SqlServerConnection($db);
$ssql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
@ -86,6 +88,7 @@ class SqlServerConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['port']);
$ssql = new SqlServerConnection($db);
$ssql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
@ -99,6 +102,7 @@ class SqlServerConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['database']);
$ssql = new SqlServerConnection($db);
$ssql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
@ -112,6 +116,7 @@ class SqlServerConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['login']);
$ssql = new SqlServerConnection($db);
$ssql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
@ -125,6 +130,7 @@ class SqlServerConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['password']);
$ssql = new SqlServerConnection($db);
$ssql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
@ -138,6 +144,7 @@ class SqlServerConnectionTest extends \PHPUnit\Framework\TestCase
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
$db['db'] = 'invalid';
$ssql = new SqlServerConnection($db);
$ssql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
@ -151,6 +158,7 @@ class SqlServerConnectionTest extends \PHPUnit\Framework\TestCase
$db['database'] = 'invalid';
$ssql = new SqlServerConnection($db);
$ssql->connect();
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
}

View File

@ -49,6 +49,7 @@ class DatabasePoolTest extends \PHPUnit\Framework\TestCase
{
/** @var array $CONFIG */
self::assertTrue($this->dbPool->create('core', $GLOBALS['CONFIG']['db']['core']['masters']['admin']));
$this->dbPool->get()->connect();
self::assertEquals($this->dbPool->get()->getStatus(), DatabaseStatus::OK);
}