From dd239af81051788db32829780702a79ec0080db8 Mon Sep 17 00:00:00 2001 From: yuri Date: Thu, 9 May 2019 11:40:01 +0300 Subject: [PATCH] orm metadata index --- application/Espo/ORM/DB/Mapper.php | 6 ++++- application/Espo/ORM/DB/Query/Base.php | 27 ++++++++++++++++++----- application/Espo/ORM/EntityManager.php | 4 ++-- application/Espo/SelectManagers/Email.php | 6 ++++- tests/unit/Espo/ORM/DB/MapperTest.php | 6 +++-- tests/unit/Espo/ORM/DB/QueryTest.php | 4 +++- 6 files changed, 40 insertions(+), 13 deletions(-) diff --git a/application/Espo/ORM/DB/Mapper.php b/application/Espo/ORM/DB/Mapper.php index 65bcc62a75..12a153438f 100644 --- a/application/Espo/ORM/DB/Mapper.php +++ b/application/Espo/ORM/DB/Mapper.php @@ -32,6 +32,7 @@ namespace Espo\ORM\DB; use Espo\ORM\Entity; use Espo\ORM\IEntity; use Espo\ORM\EntityFactory; +use Espo\ORM\Metadata; use PDO; /** @@ -47,6 +48,8 @@ abstract class Mapper implements IMapper protected $query; + protected $metadata; + protected $fieldsMapCache = []; protected $aliasesCache = []; @@ -55,10 +58,11 @@ abstract class Mapper implements IMapper protected $collectionClass = "\\Espo\\ORM\\EntityCollection"; - public function __construct(PDO $pdo, \Espo\ORM\EntityFactory $entityFactory, Query\Base $query) { + public function __construct(PDO $pdo, \Espo\ORM\EntityFactory $entityFactory, Query\Base $query, Metadata $metadata) { $this->pdo = $pdo; $this->query = $query; $this->entityFactory = $entityFactory; + $this->metadata = $metadata; } public function selectById(IEntity $entity, $id, ?array $params = null) : ?IEntity diff --git a/application/Espo/ORM/DB/Query/Base.php b/application/Espo/ORM/DB/Query/Base.php index ff22ff3f76..91b585680f 100644 --- a/application/Espo/ORM/DB/Query/Base.php +++ b/application/Espo/ORM/DB/Query/Base.php @@ -32,6 +32,7 @@ namespace Espo\ORM\DB\Query; use Espo\ORM\Entity; use Espo\ORM\IEntity; use Espo\ORM\EntityFactory; +use Espo\ORM\Metadata; use PDO; abstract class Base @@ -242,16 +243,19 @@ abstract class Base protected $pdo; + protected $metadata; + protected $fieldsMapCache = []; protected $aliasesCache = []; protected $seedCache = []; - public function __construct(PDO $pdo, EntityFactory $entityFactory) + public function __construct(PDO $pdo, EntityFactory $entityFactory, Metadata $metadata = null) { $this->entityFactory = $entityFactory; $this->pdo = $pdo; + $this->metadata = $metadata; } protected function getSeed($entityType) @@ -378,6 +382,17 @@ abstract class Base $groupByPart = implode(', ', $arr); } + $indexKeyList = null; + if (!empty($params['useIndexList']) && $this->metadata) { + $indexKeyList = []; + foreach ($params['useIndexList'] as $indexName) { + $indexKey = $this->metadata->get($entityType, ['indexes', $indexName, 'key']); + if ($indexKey) { + $indexKeyList[] = $indexKey; + } + } + } + if (!empty($params['aggregation'])) { $sql = $this->composeSelectQuery( $this->toDb($entityType), @@ -391,7 +406,7 @@ abstract class Base $params['aggregation'], $groupByPart, $havingPart, - $params['useIndexList'] + $indexKeyList ); if ($params['aggregation'] === 'COUNT' && $groupByPart && $havingPart) { $sql = "SELECT COUNT(*) AS `AggregateValue` FROM ({$sql}) AS `countAlias`"; @@ -411,7 +426,7 @@ abstract class Base null, $groupByPart, $havingPart, - $params['useIndexList'] + $indexKeyList ); return $sql; } @@ -1835,7 +1850,7 @@ abstract class Base $aggregation = false, $groupBy = null, $having = null, - $useIndexList = null + $indexKeyList = null ) { $sql = "SELECT"; @@ -1846,8 +1861,8 @@ abstract class Base $sql .= " {$select} FROM `{$table}`"; - if (!empty($useIndexList)) { - foreach ($useIndexList as $index) { + if (!empty($indexKeyList)) { + foreach ($indexKeyList as $index) { $sql .= " USE INDEX (`".$this->sanitizeIndexName($index)."`)"; } } diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index 84a59e7706..7d1d43e967 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -97,7 +97,7 @@ class EntityManager if (empty($this->query)) { $platform = $this->params['platform']; $className = '\\Espo\\ORM\\DB\\Query\\' . ucfirst($platform); - $this->query = new $className($this->getPDO(), $this->entityFactory); + $this->query = new $className($this->getPDO(), $this->entityFactory, $this->metadata); } return $this->query; } @@ -125,7 +125,7 @@ class EntityManager } if (empty($this->mappers[$className])) { - $this->mappers[$className] = new $className($this->getPDO(), $this->entityFactory, $this->getQuery()); + $this->mappers[$className] = new $className($this->getPDO(), $this->entityFactory, $this->getQuery(), $this->metadata); } return $this->mappers[$className]; } diff --git a/application/Espo/SelectManagers/Email.php b/application/Espo/SelectManagers/Email.php index 97171f0cc8..0776101b55 100644 --- a/application/Espo/SelectManagers/Email.php +++ b/application/Espo/SelectManagers/Email.php @@ -59,10 +59,14 @@ class Email extends \Espo\Core\SelectManagers\Base $skipIndex = true; } if (!$skipIndex) { - $result['useIndexList'] = ['IDX_DATE_SENT']; + $result['useIndexList'] = ['dateSent']; } } + if ($folderId === 'drafts') { + $result['useIndexList'] = ['createdById']; + } + if ($folderId !== 'drafts') { $this->addUsersJoin($result); } diff --git a/tests/unit/Espo/ORM/DB/MapperTest.php b/tests/unit/Espo/ORM/DB/MapperTest.php index 683e0403e9..e2efa58a3c 100644 --- a/tests/unit/Espo/ORM/DB/MapperTest.php +++ b/tests/unit/Espo/ORM/DB/MapperTest.php @@ -81,9 +81,11 @@ class DBMapperTest extends \PHPUnit\Framework\TestCase return new $className([], $entityManager); })); - $this->query = new Query($this->pdo, $this->entityFactory); + $this->metadata = $this->getMockBuilder('\\Espo\\ORM\\Metadata')->disableOriginalConstructor()->getMock(); - $this->db = new MysqlMapper($this->pdo, $this->entityFactory, $this->query); + $this->query = new Query($this->pdo, $this->entityFactory, $this->metadata); + + $this->db = new MysqlMapper($this->pdo, $this->entityFactory, $this->query, $this->metadata); $this->db->setReturnCollection(true); $this->post = new \Espo\Entities\Post([], $entityManager); diff --git a/tests/unit/Espo/ORM/DB/QueryTest.php b/tests/unit/Espo/ORM/DB/QueryTest.php index 35479bca69..a3faac03da 100644 --- a/tests/unit/Espo/ORM/DB/QueryTest.php +++ b/tests/unit/Espo/ORM/DB/QueryTest.php @@ -68,7 +68,9 @@ class QueryTest extends \PHPUnit\Framework\TestCase return new $className(); })); - $this->query = new Query($this->pdo, $this->entityFactory); + $this->metadata = $this->getMockBuilder('\\Espo\\ORM\\Metadata')->disableOriginalConstructor()->getMock(); + + $this->query = new Query($this->pdo, $this->entityFactory, $this->metadata); $this->post = new \Espo\Entities\Post(); $this->comment = new \Espo\Entities\Comment();