This commit is contained in:
Yuri Kuznetsov
2023-02-17 16:28:31 +02:00
parent 9b63470e9a
commit da7fc9d6a0
12 changed files with 65 additions and 87 deletions

View File

@@ -36,14 +36,12 @@ use Espo\ORM\Query\Select;
*/
class CollectionFactory
{
protected EntityManager $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function __construct(protected EntityManager $entityManager)
{}
/**
* Create.
*
* @param array<Entity|array<string,mixed>> $dataList
* @return EntityCollection<Entity>
*/
@@ -53,6 +51,8 @@ class CollectionFactory
}
/**
* Create from an SQL.
*
* @return SthCollection<Entity>
*/
public function createFromSql(string $entityType, string $sql): SthCollection
@@ -61,6 +61,8 @@ class CollectionFactory
}
/**
* Create from a query.
*
* @return SthCollection<Entity>
*/
public function createFromQuery(Select $query): SthCollection
@@ -69,6 +71,8 @@ class CollectionFactory
}
/**
* Create EntityCollection from SthCollection.
*
* @template TEntity of Entity
* @param SthCollection<TEntity> $sthCollection
* @return EntityCollection<TEntity>

View File

@@ -33,6 +33,4 @@ namespace Espo\ORM;
* Definitions.
*/
class Defs extends Defs\Defs
{
}
{}

View File

@@ -36,15 +36,16 @@ use SeekableIterator;
use RuntimeException;
use OutOfBoundsException;
use InvalidArgumentException;
use stdClass;
/**
* A standard collection of entities. It allocates a memory for all entities.
*
* @template TEntity of Entity
* @implements Iterator<int,TEntity>
* @implements Iterator<int, TEntity>
* @implements Collection<TEntity>
* @implements ArrayAccess<int,TEntity>
* @implements SeekableIterator<int,TEntity>
* @implements ArrayAccess<int, TEntity>
* @implements SeekableIterator<int, TEntity>
*/
class EntityCollection implements Collection, Iterator, Countable, ArrayAccess, SeekableIterator
{
@@ -56,7 +57,7 @@ class EntityCollection implements Collection, Iterator, Countable, ArrayAccess,
protected array $dataList = [];
/**
* @param array<TEntity|array<string,mixed>> $dataList
* @param array<TEntity|array<string, mixed>> $dataList
*/
public function __construct(
array $dataList = [],
@@ -237,7 +238,7 @@ class EntityCollection implements Collection, Iterator, Countable, ArrayAccess,
}
/**
* @param array<string,mixed> $dataArray
* @param array<string, mixed> $dataArray
* @return TEntity
*/
protected function buildEntityFromArray(array $dataArray): Entity
@@ -304,7 +305,7 @@ class EntityCollection implements Collection, Iterator, Countable, ArrayAccess,
/**
* Whether a collection contains a specific item.
*
* @param TEntity|array<string,mixed> $value
* @param TEntity|array<string, mixed> $value
*/
public function contains($value): bool
{
@@ -316,7 +317,7 @@ class EntityCollection implements Collection, Iterator, Countable, ArrayAccess,
}
/**
* @param TEntity|array<string,mixed> $value
* @param TEntity|array<string, mixed> $value
* @return false|int
*/
public function indexOf($value)
@@ -361,7 +362,7 @@ class EntityCollection implements Collection, Iterator, Countable, ArrayAccess,
/**
* @deprecated As of v6.0. Use `getValueMapList`.
* @return array<array<string, mixed>>|\stdClass[]
* @return array<array<string, mixed>>|stdClass[]
*/
public function toArray(bool $itemsAsObjects = false): array
{
@@ -381,9 +382,12 @@ class EntityCollection implements Collection, Iterator, Countable, ArrayAccess,
return $arr;
}
/**
* {@inheritDoc}
*/
public function getValueMapList(): array
{
/** @var \stdClass[] */
/** @var stdClass[] */
return $this->toArray(true);
}
@@ -404,6 +408,8 @@ class EntityCollection implements Collection, Iterator, Countable, ArrayAccess,
}
/**
* Create from SthCollection.
*
* @param SthCollection<TEntity> $sthCollection
* @return self<TEntity>
*/
@@ -417,7 +423,6 @@ class EntityCollection implements Collection, Iterator, Countable, ArrayAccess,
/** @var self<TEntity> $obj */
$obj = new EntityCollection($entityList, $sthCollection->getEntityType());
$obj->setAsFetched();
return $obj;

View File

@@ -34,9 +34,7 @@ namespace Espo\ORM;
*/
class EventDispatcher
{
/**
* @var array{'metadataUpdate': callable[]}
*/
/** @var array{'metadataUpdate': callable[]} */
private $data;
private const METADATA_UPDATE = 'metadataUpdate';

View File

@@ -214,7 +214,7 @@ class BaseMapper implements RDBMapper
}
/**
* Select entities from DB by a SQL query.
* Select entities from DB by aт SQL query.
*
* @return SthCollection<Entity>
*/

View File

@@ -29,10 +29,7 @@
namespace Espo\ORM;
use Espo\ORM\{
Defs\DefsData,
Defs,
};
use Espo\ORM\Defs\DefsData;
use InvalidArgumentException;
@@ -41,29 +38,20 @@ use InvalidArgumentException;
*/
class Metadata
{
/**
* @var array<string,mixed>
*/
/** @var array<string, mixed> */
private array $data;
private Defs $defs;
private DefsData $defsData;
private MetadataDataProvider $dataProvider;
private EventDispatcher $eventDispatcher;
public function __construct(MetadataDataProvider $dataProvider, ?EventDispatcher $eventDispatcher = null)
{
public function __construct(
private MetadataDataProvider $dataProvider,
?EventDispatcher $eventDispatcher = null
) {
$this->data = $dataProvider->get();
$this->dataProvider = $dataProvider;
$this->defsData = new DefsData($this);
$this->defs = new Defs($this->defsData);
$this->eventDispatcher = $eventDispatcher ?? new EventDispatcher();
}
@@ -92,7 +80,7 @@ class Metadata
*
* @param string $entityType An entity type.
* @param string[]|string|null $key A Key.
* @param mixed $default A default value. *
* @param mixed $default A default value.
* @return mixed
*/
public function get(string $entityType, $key = null, $default = null)

View File

@@ -3666,7 +3666,7 @@ abstract class BaseQueryComposer implements QueryComposer
}
/**
* Add a LIMIT part to a SQL query.
* Add a LIMIT part to an SQL query.
*/
abstract protected function limit(string $sql, ?int $offset = null, ?int $limit = null): string;
}

View File

@@ -39,14 +39,10 @@ use PDOStatement;
*/
class QueryExecutor
{
private SqlExecutor $sqlExecutor;
private QueryComposerWrapper $queryComposer;
public function __construct(SqlExecutor $sqlExecutor, QueryComposerWrapper $queryComposer)
{
$this->sqlExecutor = $sqlExecutor;
$this->queryComposer = $queryComposer;
}
public function __construct(
private SqlExecutor $sqlExecutor,
private QueryComposerWrapper $queryComposer
) {}
/**
* Execute a query.

View File

@@ -278,7 +278,7 @@ class RDBRepository implements Repository
}
/**
* Find records by a SQL query.
* Find records by an SQL query.
*
* @return SthCollection<TEntity>
*/

View File

@@ -32,7 +32,6 @@ namespace Espo\ORM;
use PDO;
use PDOStatement;
use PDOException;
use Exception;
use RuntimeException;
@@ -41,14 +40,10 @@ use RuntimeException;
*/
class SqlExecutor
{
private $pdo;
private const MAX_ATTEMPT_COUNT = 4;
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
public function __construct(private PDO $pdo)
{}
/**
* Execute a query.
@@ -73,9 +68,7 @@ class SqlExecutor
$counter--;
if ($counter === 0 || !$this->isExceptionIsDeadlock($e)) {
/**
* @var PDOException $e
*/
/** @var PDOException $e */
throw $e;
}

View File

@@ -34,6 +34,7 @@ use Espo\ORM\QueryComposer\QueryComposer as QueryComposer;
use IteratorAggregate;
use Countable;
use stdClass;
use Traversable;
use PDO;
use PDOStatement;
@@ -52,20 +53,13 @@ use RuntimeException;
*/
class SthCollection implements Collection, IteratorAggregate, Countable
{
private EntityManager $entityManager;
private string $entityType;
private ?SelectQuery $query = null;
private ?PDOStatement $sth = null;
private ?string $sql = null;
private function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
private function __construct(private EntityManager $entityManager)
{}
private function getQueryComposer(): QueryComposer
{
@@ -134,7 +128,7 @@ class SthCollection implements Collection, IteratorAggregate, Countable
}
/**
* @return array<string,mixed>
* @return array<string, mixed>
*/
private function fetchRow()
{
@@ -145,6 +139,9 @@ class SthCollection implements Collection, IteratorAggregate, Countable
return $this->sth->fetch(PDO::FETCH_ASSOC);
}
/**
* Get count. Can be slow. Use EntityCollection if you need count.
*/
public function count(): int
{
$this->executeQueryIfNotExecuted();
@@ -158,16 +155,15 @@ class SthCollection implements Collection, IteratorAggregate, Countable
return $rowCount;
}
return count($this->getValueMapList());
return iterator_count($this);
}
protected function prepareEntity(Entity $entity): void
{
}
{}
/**
* @deprecated As of v6.0. Use `getValueMapList`.
* @return array<int,array<string,mixed>>|\stdClass[]
* @return array<int, array<string,mixed>>|stdClass[]
*/
public function toArray(bool $itemsAsObjects = false): array
{
@@ -190,15 +186,18 @@ class SthCollection implements Collection, IteratorAggregate, Countable
return $arr;
}
/**
* {@inheritDoc}
*/
public function getValueMapList(): array
{
/** @var \stdClass[] */
/** @var stdClass[] */
return $this->toArray(true);
}
/**
* Whether Is fetched from DB. SthCollection is always fetched.
* Whether is fetched from DB. SthCollection is always fetched.
*/
public function isFetched(): bool
{
@@ -214,6 +213,8 @@ class SthCollection implements Collection, IteratorAggregate, Countable
}
/**
* Create from a query.
*
* @return self<Entity>
*/
public static function fromQuery(SelectQuery $query, EntityManager $entityManager): self
@@ -234,6 +235,8 @@ class SthCollection implements Collection, IteratorAggregate, Countable
}
/**
* Create from an SQL.
*
* @return self<Entity>
*/
public static function fromSql(string $entityType, string $sql, EntityManager $entityManager): self

View File

@@ -41,15 +41,8 @@ class TransactionManager
{
private int $level = 0;
private PDO $pdo;
private QueryComposer $queryComposer;
public function __construct(PDO $pdo, QueryComposer $queryComposer)
{
$this->pdo = $pdo;
$this->queryComposer = $queryComposer;
}
public function __construct(private PDO $pdo, private QueryComposer $queryComposer)
{}
/**
* Whether a transaction is started.