make db connection throw less exceptions

This commit is contained in:
Dennis Eichhorn 2020-06-07 22:24:24 +02:00
parent a0bb408b6a
commit 3db14eb5b6
9 changed files with 89 additions and 83 deletions

View File

@ -68,7 +68,10 @@ final class MysqlConnection extends ConnectionAbstract
|| !DatabaseType::isValidValue($this->dbdata['db'])
) {
$this->status = DatabaseStatus::FAILURE;
throw new InvalidConnectionConfigException((string) \json_encode($this->dbdata));
$this->dbdata['password'] = '****';
//throw new InvalidConnectionConfigException((string) \json_encode($this->dbdata));
return;
}
$this->close();
@ -82,7 +85,6 @@ final class MysqlConnection extends ConnectionAbstract
} catch (\PDOException $e) {
unset($this->con);
$this->status = DatabaseStatus::MISSING_DATABASE;
throw new InvalidConnectionConfigException((string) \json_encode($this->dbdata));
} finally {
$this->dbdata['password'] = '****';
}

View File

@ -68,7 +68,10 @@ final class PostgresConnection extends ConnectionAbstract
|| !DatabaseType::isValidValue($this->dbdata['db'])
) {
$this->status = DatabaseStatus::FAILURE;
throw new InvalidConnectionConfigException((string) \json_encode($this->dbdata));
$this->dbdata['password'] = '****';
//throw new InvalidConnectionConfigException((string) \json_encode($this->dbdata));
return;
}
$this->close();

View File

@ -74,7 +74,10 @@ final class SQLiteConnection extends ConnectionAbstract
|| !DatabaseType::isValidValue($this->dbdata['db'])
) {
$this->status = DatabaseStatus::FAILURE;
throw new InvalidConnectionConfigException((string) \json_encode($this->dbdata));
$this->dbdata['password'] = '****';
//throw new InvalidConnectionConfigException((string) \json_encode($this->dbdata));
return;
}
$this->close();

View File

@ -68,7 +68,10 @@ final class SqlServerConnection extends ConnectionAbstract
|| !DatabaseType::isValidValue($this->dbdata['db'])
) {
$this->status = DatabaseStatus::FAILURE;
throw new InvalidConnectionConfigException((string) \json_encode($this->dbdata));
$this->dbdata['password'] = '****';
//throw new InvalidConnectionConfigException((string) \json_encode($this->dbdata));
return;
}
$this->close();

View File

@ -2960,7 +2960,7 @@ class DataMapperAbstract implements DataMapperInterface
$cachedTables = []; // used by conditionals
foreach (static::$hasMany as $member => $value) {
if ($value['writeonly'] ?? false === true || isset($value['column'])) {
if ($value['writeonly'] ?? false === true || isset($value['column'])) { // @todo: conflict with getQuery()???!?!?!?!
continue;
}
@ -3080,7 +3080,7 @@ class DataMapperAbstract implements DataMapperInterface
// get HasManyQuery (but only for elements which have a 'column' defined)
if ($depth > 1 && $relations === RelationType::ALL) {
foreach (static::$hasMany as $member => $rel) {
if (isset($rel['self']) || !isset($rel['column'])) {
if (isset($rel['self']) || !isset($rel['column'])) { // @todo: conflict with getHasMany()???!?!?!?!
continue;
}
@ -3363,6 +3363,7 @@ INSERT INTO `tag_l11n` (`tag_l11n_id`, `tag_l11n_tag`, `tag_l11n_title`, `tag_l1
*/
/* C1: conditional values with priorities
https://dbfiddle.uk/?rdbms=mariadb_10.4&fiddle=54359372c3481cfee85f423af76665e4
SELECT
`tag_3`.`tag_id` as tag_id_3, `tag_3`.`tag_bla` as tag_bla_3,
`tag_l11n_2`.`tag_l11n_title` as tag_l11n_title_2, `tag_l11n_2`.`tag_l11n_language`
@ -3390,3 +3391,23 @@ ORDER BY
`tag_3`.`tag_id` ASC
LIMIT 25;
*/
/* C2: try this
SELECT
`tag_3`.`tag_id` as tag_id_3,
COALESCE(`tag_l11n_2`.`tag_l11n_title`, `tag_l11n_3`.`tag_l11n_title`, `tag_l11n_4`.`tag_l11n_title`) as tag_l11n_title_2
FROM
`tag` as tag_3
LEFT JOIN
`tag_l11n` as tag_l11n_2 ON `tag_3`.`tag_id` = `tag_l11n_2`.`tag_l11n_tag`
AND `tag_l11n_2`.`tag_l11n_language` = 'it'
LEFT JOIN
`tag_l11n` as tag_l11n_3 ON `tag_3`.`tag_id` = `tag_l11n_3`.`tag_l11n_tag`
AND `tag_l11n_3`.`tag_l11n_language` = 'en'
LEFT JOIN
`tag_l11n` as tag_l11n_4 ON `tag_3`.`tag_id` = `tag_l11n_4`.`tag_l11n_tag`
AND `tag_l11n_4`.`tag_l11n_language` NOT IN ('en', 'it')
ORDER BY
`tag_3`.`tag_id` ASC
LIMIT 25;
*/

View File

@ -52,122 +52,114 @@ class MysqlConnectionTest extends \PHPUnit\Framework\TestCase
}
/**
* @testdox A missing database type throws a InvalidConnectionConfigException
* @testdox A missing database type returns a failure
* @covers phpOMS\DataStorage\Database\Connection\MysqlConnection
* @group framework
*/
public function testInvalidDatabaseType() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['masters']['admin'];
unset($db['db']);
$mysql = new MysqlConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
/**
* @testdox A missing database host throws a InvalidConnectionConfigException
* @testdox A missing database host returns a failure
* @covers phpOMS\DataStorage\Database\Connection\MysqlConnection
* @group framework
*/
public function testInvalidHost() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['masters']['admin'];
unset($db['host']);
$mysql = new MysqlConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
/**
* @testdox A missing database port throws a InvalidConnectionConfigException
* @testdox A missing database port returns a failure
* @covers phpOMS\DataStorage\Database\Connection\MysqlConnection
* @group framework
*/
public function testInvalidPort() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['masters']['admin'];
unset($db['port']);
$mysql = new MysqlConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
/**
* @testdox A missing database throws a InvalidConnectionConfigException
* @testdox A missing database returns a failure
* @covers phpOMS\DataStorage\Database\Connection\MysqlConnection
* @group framework
*/
public function testInvalidDatabase() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['masters']['admin'];
unset($db['database']);
$mysql = new MysqlConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
/**
* @testdox A missing database login throws a InvalidConnectionConfigException
* @testdox A missing database login returns a failure
* @covers phpOMS\DataStorage\Database\Connection\MysqlConnection
* @group framework
*/
public function testInvalidLogin() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['masters']['admin'];
unset($db['login']);
$mysql = new MysqlConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
/**
* @testdox A missing database password throws a InvalidConnectionConfigException
* @testdox A missing database password returns a failure
* @covers phpOMS\DataStorage\Database\Connection\MysqlConnection
* @group framework
*/
public function testInvalidPassword() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['masters']['admin'];
unset($db['password']);
$mysql = new MysqlConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
/**
* @testdox A invalid database type throws a InvalidConnectionConfigException
* @testdox A invalid database type returns a failure
* @covers phpOMS\DataStorage\Database\Connection\MysqlConnection
* @group framework
*/
public function testInvalidDatabaseTypeName() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['masters']['admin'];
$db['db'] = 'invalid';
$mysql = new MysqlConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
/**
* @testdox A invalid database throws a InvalidConnectionConfigException
* @testdox A invalid database returns a failure
* @covers phpOMS\DataStorage\Database\Connection\MysqlConnection
* @group framework
*/
public function testInvalidDatabaseName() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['masters']['admin'];
$db['database'] = 'invalid';
$mysql = new MysqlConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $mysql->getStatus());
}
}

View File

@ -50,100 +50,93 @@ class PostgresConnectionTest extends \PHPUnit\Framework\TestCase
}
/**
* @testdox A missing database type throws a InvalidConnectionConfigException
* @testdox A missing database type returns a failure
* @covers phpOMS\DataStorage\Database\Connection\PostgresConnection
* @group framework
*/
public function testInvalidDatabaseType() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['db']);
$psql = new PostgresConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
/**
* @testdox A missing database host throws a InvalidConnectionConfigException
* @testdox A missing database host returns a failure
* @covers phpOMS\DataStorage\Database\Connection\PostgresConnection
* @group framework
*/
public function testInvalidHost() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['host']);
$psql = new PostgresConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
/**
* @testdox A missing database port throws a InvalidConnectionConfigException
* @testdox A missing database port returns a failure
* @covers phpOMS\DataStorage\Database\Connection\PostgresConnection
* @group framework
*/
public function testInvalidPort() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['port']);
$psql = new PostgresConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
/**
* @testdox A missing database throws a InvalidConnectionConfigException
* @testdox A missing database returns a failure
* @covers phpOMS\DataStorage\Database\Connection\PostgresConnection
* @group framework
*/
public function testInvalidDatabase() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['database']);
$psql = new PostgresConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
/**
* @testdox A missing database login throws a InvalidConnectionConfigException
* @testdox A missing database login returns a failure
* @covers phpOMS\DataStorage\Database\Connection\PostgresConnection
* @group framework
*/
public function testInvalidLogin() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['login']);
$psql = new PostgresConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
/**
* @testdox A missing database password throws a InvalidConnectionConfigException
* @testdox A missing database password returns a failure
* @covers phpOMS\DataStorage\Database\Connection\PostgresConnection
* @group framework
*/
public function testInvalidPassword() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
unset($db['password']);
$psql = new PostgresConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
/**
* @testdox A invalid database type throws a InvalidConnectionConfigException
* @testdox A invalid database returns a failure
* @covers phpOMS\DataStorage\Database\Connection\PostgresConnection
* @group framework
*/
public function testInvalidDatabaseTypeName() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['postgresql']['admin'];
$db['db'] = 'invalid';
$psql = new PostgresConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $psql->getStatus());
}
}

View File

@ -48,31 +48,29 @@ class SQLiteConnectionTest extends \PHPUnit\Framework\TestCase
}
/**
* @testdox A missing database type throws a InvalidConnectionConfigException
* @testdox A missing database type returns a failure
* @covers phpOMS\DataStorage\Database\Connection\SQLiteConnection
* @group framework
*/
public function testInvalidDatabaseType() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['sqlite']['admin'];
unset($db['db']);
$sqlite = new SQLiteConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $sqlite->getStatus());
}
/**
* @testdox A missing database throws a InvalidConnectionConfigException
* @testdox A missing database returns a failure
* @covers phpOMS\DataStorage\Database\Connection\SQLiteConnection
* @group framework
*/
public function testInvalidDatabase() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['sqlite']['admin'];
unset($db['database']);
$sqlite = new SQLiteConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $sqlite->getStatus());
}
public static function tearDownAfterClass() : void

View File

@ -51,115 +51,106 @@ class SqlServerConnectionTest extends \PHPUnit\Framework\TestCase
}
/**
* @testdox A missing database type throws a InvalidConnectionConfigException
* @testdox A missing database type returns a failure
* @covers phpOMS\DataStorage\Database\Connection\SqlServerConnection
* @group framework
*/
public function testInvalidDatabaseType() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['db']);
$ssql = new SqlServerConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
/**
* @testdox A missing database host throws a InvalidConnectionConfigException
* @testdox A missing database host returns a failure
* @covers phpOMS\DataStorage\Database\Connection\SqlServerConnection
* @group framework
*/
public function testInvalidHost() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['host']);
$ssql = new SqlServerConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
/**
* @testdox A missing database port throws a InvalidConnectionConfigException
* @testdox A missing database port returns a failure
* @covers phpOMS\DataStorage\Database\Connection\SqlServerConnection
* @group framework
*/
public function testInvalidPort() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['port']);
$ssql = new SqlServerConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
/**
* @testdox A missing database throws a InvalidConnectionConfigException
* @testdox A missing database returns a failure
* @covers phpOMS\DataStorage\Database\Connection\SqlServerConnection
* @group framework
*/
public function testInvalidDatabase() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['database']);
$ssql = new SqlServerConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
/**
* @testdox A missing database login throws a InvalidConnectionConfigException
* @testdox A missing database login returns a failure
* @covers phpOMS\DataStorage\Database\Connection\SqlServerConnection
* @group framework
*/
public function testInvalidLogin() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['login']);
$ssql = new SqlServerConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
/**
* @testdox A missing database password throws a InvalidConnectionConfigException
* @testdox A missing database password returns a failure
* @covers phpOMS\DataStorage\Database\Connection\SqlServerConnection
* @group framework
*/
public function testInvalidPassword() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
unset($db['password']);
$ssql = new SqlServerConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
/**
* @testdox A invalid database type throws a InvalidConnectionConfigException
* @testdox A invalid database type returns a failure
* @covers phpOMS\DataStorage\Database\Connection\SqlServerConnection
* @group framework
*/
public function testInvalidDatabaseTypeName() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
$db['db'] = 'invalid';
$ssql = new SqlServerConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
/**
* @testdox A invalid database throws a InvalidConnectionConfigException
* @testdox A invalid database returns a failure
* @covers phpOMS\DataStorage\Database\Connection\SqlServerConnection
* @group framework
*/
public function testInvalidDatabaseName() : void
{
self::expectException(\phpOMS\DataStorage\Database\Exception\InvalidConnectionConfigException::class);
$db = $GLOBALS['CONFIG']['db']['core']['mssql']['admin'];
$db['database'] = 'invalid';
$ssql = new SqlServerConnection($db);
self::assertEquals(DatabaseStatus::FAILURE, $ssql->getStatus());
}
}