From 5178c2fbd0fc6ac874df2b7848279b0c752d327b Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 11 Nov 2021 12:47:43 +0200 Subject: [PATCH] orm pdo refactoring --- .../Espo/Core/ORM/EntityManagerFactory.php | 4 ++ .../Espo/ORM/PDO/DefaultPDOProvider.php | 38 ++-------- application/Espo/ORM/PDO/Options.php | 71 +++++++++++++++++++ 3 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 application/Espo/ORM/PDO/Options.php diff --git a/application/Espo/Core/ORM/EntityManagerFactory.php b/application/Espo/Core/ORM/EntityManagerFactory.php index 4f96cf8e31..047bb497ca 100644 --- a/application/Espo/Core/ORM/EntityManagerFactory.php +++ b/application/Espo/Core/ORM/EntityManagerFactory.php @@ -145,6 +145,10 @@ class EntityManagerFactory ->withSslCipher($config->get('database.sslCipher')) ->withSslVerifyDisabled($config->get('database.sslVerifyDisabled') ?? false); + if (!$databaseParams->getName()) { + throw new RuntimeException('No database name specified.'); + } + if (!$databaseParams->getPlatform()) { $driver = $config->get('database.driver'); diff --git a/application/Espo/ORM/PDO/DefaultPDOProvider.php b/application/Espo/ORM/PDO/DefaultPDOProvider.php index c94b2ae54a..ef144cebf3 100644 --- a/application/Espo/ORM/PDO/DefaultPDOProvider.php +++ b/application/Espo/ORM/PDO/DefaultPDOProvider.php @@ -73,49 +73,21 @@ class DefaultPDOProvider implements PDOProvider throw new RuntimeException("No 'host' parameter."); } - if (!$dbname) { - throw new RuntimeException("No 'dbname' parameter."); - } - - $dsn = - $platform . ':' . - 'host=' . $host; + $dsn = $platform . ':' . 'host=' . $host; if ($port) { $dsn .= ';' . 'port=' . (string) $port; } - $dsn .= ';' . 'dbname=' . $dbname; + if ($dbname) { + $dsn .= ';' . 'dbname=' . $dbname; + } if ($charset) { $dsn .= ';' . 'charset=' . $charset; } - $options = []; - - if ($this->databaseParams->getSslCa()) { - $options[PDO::MYSQL_ATTR_SSL_CA] = $this->databaseParams->getSslCa(); - } - - if ($this->databaseParams->getSslCert()) { - $options[PDO::MYSQL_ATTR_SSL_CERT] = $this->databaseParams->getSslCert(); - } - - if ($this->databaseParams->getSslKey()) { - $options[PDO::MYSQL_ATTR_SSL_KEY] = $this->databaseParams->getSslKey(); - } - - if ($this->databaseParams->getSslCaPath()) { - $options[PDO::MYSQL_ATTR_SSL_CAPATH] = $this->databaseParams->getSslCaPath(); - } - - if ($this->databaseParams->getSslCipher()) { - $options[PDO::MYSQL_ATTR_SSL_CIPHER] = $this->databaseParams->getSslCipher(); - } - - if ($this->databaseParams->isSslVerifyDisabled()) { - $options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = false; - } + $options = Options::getOptionsFromDatabaseParams($this->databaseParams); $this->pdo = new PDO($dsn, $username, $password, $options); diff --git a/application/Espo/ORM/PDO/Options.php b/application/Espo/ORM/PDO/Options.php new file mode 100644 index 0000000000..cf8fe56180 --- /dev/null +++ b/application/Espo/ORM/PDO/Options.php @@ -0,0 +1,71 @@ + + */ + public static function getOptionsFromDatabaseParams(DatabaseParams $databaseParams): array + { + $options = []; + + if ($databaseParams->getSslCa()) { + $options[PDO::MYSQL_ATTR_SSL_CA] = $databaseParams->getSslCa(); + } + + if ($databaseParams->getSslCert()) { + $options[PDO::MYSQL_ATTR_SSL_CERT] = $databaseParams->getSslCert(); + } + + if ($databaseParams->getSslKey()) { + $options[PDO::MYSQL_ATTR_SSL_KEY] = $databaseParams->getSslKey(); + } + + if ($databaseParams->getSslCaPath()) { + $options[PDO::MYSQL_ATTR_SSL_CAPATH] = $databaseParams->getSslCaPath(); + } + + if ($databaseParams->getSslCipher()) { + $options[PDO::MYSQL_ATTR_SSL_CIPHER] = $databaseParams->getSslCipher(); + } + + if ($databaseParams->isSslVerifyDisabled()) { + $options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = false; + } + + return $options; + } +}