From 2e15b5b271af942e07a2c1eb6e45c2bded01f913 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 28 Aug 2020 14:15:31 +0300 Subject: [PATCH] orm fixes --- application/Espo/ORM/EntityManager.php | 37 ++++++++++++++++++---- application/Espo/ORM/Locker/BaseLocker.php | 2 +- application/Espo/ORM/Mapper/BaseMapper.php | 15 +++++---- application/Espo/ORM/Mapper/Mapper.php | 26 +++------------ 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index 7ac7204fee..add66de40d 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -30,11 +30,13 @@ namespace Espo\ORM; use Espo\ORM\{ - Mapper\Mapper, QueryComposer\QueryComposer, + Mapper\Mapper, + Mapper\BaseMapper, Repository\RepositoryFactory, Repository\Repository, Locker\Locker, + Locker\BaseLocker, }; use PDO; @@ -90,10 +92,13 @@ class EntityManager if (empty($this->params['driver'])) { throw new Exception('No database driver specified.'); } + $driver = $this->params['driver']; + if (empty($this->driverPlatformMap[$driver])) { throw new Exception("Database driver '{$driver}' is not supported."); } + $this->params['platform'] = $this->driverPlatformMap[$this->params['driver']]; } @@ -144,6 +149,10 @@ class EntityManager if (!$className) { $platform = $this->params['platform']; $className = 'Espo\\ORM\\Locker\\' . ucfirst($platform) . 'Locker'; + + if (!class_exists($className)) { + $className = BaseLocker::class; + } } if (!$className || !class_exists($className)) { @@ -181,13 +190,15 @@ class EntityManager { $className = null; - switch ($name) { - case 'RDB': - $platform = $this->params['platform']; - $className = 'Espo\\ORM\\Mapper\\' . ucfirst($platform) . 'Mapper'; - break; + $classNameMap = $this->params['mapperClassNameMap'] ?? []; + + $className = $classNameMap[$name] ?? null; + + if (!$className && $name === 'RDB') { + $className = $this->getRDBMapperClassName(); } + if (!$className || !class_exists($className)) { throw new RuntimeException("Mapper '{$name}' does not exist."); } @@ -195,6 +206,19 @@ class EntityManager return $className; } + protected function getRDBMapperClassName() : string + { + $platform = $this->params['platform']; + + $className = 'Espo\\ORM\\Mapper\\' . ucfirst($platform) . 'Mapper'; + + if (!class_exists($className)) { + $className = BaseMapper::class; + } + + return $className; + } + /** * Get a Mapper. */ @@ -227,6 +251,7 @@ class EntityManager $platform = strtolower($params['platform']); $options = []; + if (isset($params['sslCA'])) { $options[PDO::MYSQL_ATTR_SSL_CA] = $params['sslCA']; } diff --git a/application/Espo/ORM/Locker/BaseLocker.php b/application/Espo/ORM/Locker/BaseLocker.php index a9a3a2f600..f9e67f2418 100644 --- a/application/Espo/ORM/Locker/BaseLocker.php +++ b/application/Espo/ORM/Locker/BaseLocker.php @@ -38,7 +38,7 @@ use Espo\ORM\{ use PDO; use RuntimeException; -abstract class BaseLocker implements Locker +class BaseLocker implements Locker { protected $pdo; protected $queryComposer; diff --git a/application/Espo/ORM/Mapper/BaseMapper.php b/application/Espo/ORM/Mapper/BaseMapper.php index 74a770c6de..410246d014 100644 --- a/application/Espo/ORM/Mapper/BaseMapper.php +++ b/application/Espo/ORM/Mapper/BaseMapper.php @@ -88,7 +88,7 @@ abstract class BaseMapper implements Mapper } /** - * Get the first entity from DB. + * {@inheritdoc} */ public function selectOne(Select $select) : ?Entity { @@ -112,7 +112,7 @@ abstract class BaseMapper implements Mapper } /** - * Get a number of entities in DB. + * {@inheritdoc} */ public function count(Select $select) : int { @@ -158,9 +158,10 @@ abstract class BaseMapper implements Mapper } /** - * Select enities from DB. + * {@inheritdoc} + * @todo Change return type to SthCollection once PHP 7.4 is a min version. */ - public function select(Select $select) : SthCollection + public function select(Select $select) : Collection { $entityType = $select->getFrom(); @@ -1199,7 +1200,7 @@ abstract class BaseMapper implements Mapper } /** - * Mass insert collection into DB. + * {@inheritdoc} */ public function massInsert(Collection $collection) { @@ -1286,7 +1287,7 @@ abstract class BaseMapper implements Mapper } /** - * Update an entity in DB. + * {@inheritdoc} */ public function update(Entity $entity) { @@ -1375,7 +1376,7 @@ abstract class BaseMapper implements Mapper } /** - * Mark an entity as deleted in DB. + * {@inheritdoc} */ public function delete(Entity $entity) : bool { diff --git a/application/Espo/ORM/Mapper/Mapper.php b/application/Espo/ORM/Mapper/Mapper.php index 9cd8d5c00e..4537e2a9e8 100644 --- a/application/Espo/ORM/Mapper/Mapper.php +++ b/application/Espo/ORM/Mapper/Mapper.php @@ -38,43 +38,27 @@ use Espo\ORM\{ interface Mapper { /** - * Get the first entity from DB. + * Get a first entity from DB. */ public function selectOne(Select $select) : ?Entity; /** - * Select a list of entities according to given parameters. + * Select enities from DB. */ public function select(Select $select) : Collection; /** - * Returns count of records according to given parameters. - * - * @return Record count. + * Get a number of records in DB. */ public function count(Select $select) : int; - /** - * Selects related entity or list of entities. - * - * @return List of entities or one entity. - */ - public function selectRelated(Entity $entity, string $relationName, ?Select $select = null); - - /** - * Returns count of related records according to given parameters. - * - * @return A number of records. - */ - public function countRelated(Entity $entity, string $relationName, ?Select $select = null) : int; - /** * Insert an entity into DB. */ public function insert(Entity $entity); /** - * Insert an entity collaction. + * Insert a collection into DB. */ public function massInsert(Collection $collection); @@ -84,7 +68,7 @@ interface Mapper public function update(Entity $entity); /** - * Delete an entity (mark as deleted). + * Mark an entity as deleted in DB. */ public function delete(Entity $entity); }