orm metadata index

This commit is contained in:
yuri
2019-05-09 11:40:01 +03:00
parent 52536f9803
commit dd239af810
6 changed files with 40 additions and 13 deletions

View File

@@ -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

View File

@@ -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)."`)";
}
}

View File

@@ -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];
}

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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();