0 follower

Class Yiisoft\Data\Db\QueryDataReader

InheritanceYiisoft\Data\Db\QueryDataReader
ImplementsYiisoft\Data\Db\QueryDataReaderInterface

Method Details

Hide inherited methods

__construct() public method

public __construct( \Yiisoft\Db\Query\QueryInterface $query, \Yiisoft\Data\Reader\Sort|null $sort null, integer $offset 0, integer|null $limit null, string|null $countParam null, \Yiisoft\Data\Reader\FilterInterface $filter = new All(), \Yiisoft\Data\Reader\FilterInterface $having = new All(), integer|null $batchSize null, array $extraFilterHandlers = [], array|Yiisoft\Data\Db\FieldMapper\FieldMapperInterface $fieldMapper = [] ): mixed
$query \Yiisoft\Db\Query\QueryInterface
$sort \Yiisoft\Data\Reader\Sort|null
$offset integer
$limit integer|null
$countParam string|null
$filter \Yiisoft\Data\Reader\FilterInterface
$having \Yiisoft\Data\Reader\FilterInterface
$batchSize integer|null
$extraFilterHandlers array
$fieldMapper array|Yiisoft\Data\Db\FieldMapper\FieldMapperInterface

                public function __construct(
    private readonly QueryInterface $query,
    private ?Sort $sort = null,
    private int $offset = 0,
    private ?int $limit = null,
    private ?string $countParam = null,
    private FilterInterface $filter = new All(),
    private FilterInterface $having = new All(),
    private ?int $batchSize = null,
    array $extraFilterHandlers = [],
    array|FieldMapperInterface $fieldMapper = [],
) {
    $this->fieldMapper = is_array($fieldMapper) ? new ArrayFieldMapper($fieldMapper) : $fieldMapper;
    $this->filterHandler = new FilterHandler(
        [
            new AllHandler(),
            new NoneHandler(),
            new AndXHandler(),
            new OrXHandler(),
            new EqualsHandler(),
            new GreaterThanHandler(),
            new GreaterThanOrEqualHandler(),
            new LessThanHandler(),
            new LessThanOrEqualHandler(),
            new LikeHandler(),
            new InHandler(),
            new ExistsHandler(),
            new NotHandler(),
            new BetweenHandler(),
            new EqualsNullHandler(),
            new EqualsExpressionHandler(),
            ...$extraFilterHandlers,
        ],
        $this->fieldMapper,
    );
}

            
count() public method

public count( ): integer

                final public function count(): int
{
    if ($this->count === null) {
        $q = $this->countParam ?? '*';
        if ($q === '*' && is_array($this->cache) && $this->limit === null && $this->offset === 0) {
            $this->count = count($this->cache);
        } else {
            $query = $this->getPreparedQuery();
            $query->offset(null);
            $query->limit(null);
            $query->orderBy('');
            $count = $query->count($q);
            if (is_string($count)) {
                throw new RuntimeException(
                    sprintf(
                        'Number of records is too large to fit into a PHP integer. Got %s.',
                        $count,
                    ),
                );
            }
            $this->count = $count;
        }
    }
    return $this->count;
}

            
getFilter() public method

public getFilter( ): \Yiisoft\Data\Reader\FilterInterface

                final public function getFilter(): FilterInterface
{
    return $this->filter;
}

            
getIterator() public method

public getIterator( ): Generator

                final public function getIterator(): Generator
{
    if ($this->batchSize !== null) {
        foreach ($this->getPreparedQuery()->batch($this->batchSize) as $data) {
            /** @psalm-var array<TKey, TValue> $data */
            yield from $data;
        }
        /** @infection-ignore-all */
        return;
    }
    if (is_array($this->cache)) {
        yield from $this->cache;
        return;
    }
    /** @psalm-var array<TKey, TValue> */
    $this->cache = $this->getPreparedQuery()->all();
    yield from $this->cache;
}

            
getLimit() public method

public getLimit( ): integer|null

                final public function getLimit(): ?int
{
    return $this->limit;
}

            
getOffset() public method

public getOffset( ): integer

                final public function getOffset(): int
{
    return $this->offset;
}

            
getPreparedQuery() public method

public getPreparedQuery( ): \Yiisoft\Db\Query\QueryInterface

                final public function getPreparedQuery(): QueryInterface
{
    $query = (clone $this->query)
        ->andWhere(
            $this->filterHandler->handle($this->filter),
        )
        ->andHaving(
            $this->filterHandler->handle($this->having),
        );
    if ($this->limit !== null) {
        $query->limit($this->limit);
    }
    $query->offset($this->offset);
    if ($this->sort !== null) {
        $query->addOrderBy(
            $this->convertSortToOrderBy($this->sort),
        );
    }
    return $query;
}

            
getSort() public method

public getSort( ): \Yiisoft\Data\Reader\Sort|null

                final public function getSort(): ?Sort
{
    return $this->sort;
}

            
read() public method

public read( ): Generator

                final public function read(): Generator
{
    return $this->getIterator();
}

            
readOne() public method

public readOne( ): array|object|null

                final public function readOne(): array|object|null
{
    if (is_array($this->cache)) {
        $key = array_key_first($this->cache);
        return $key === null ? null : $this->cache[$key];
    }
    return $this->withLimit(1)->getIterator()->current();
}

            
withBatchSize() public method

public withBatchSize( integer|null $batchSize ): Yiisoft\Data\Db\QueryDataReader
$batchSize integer|null
return Yiisoft\Data\Db\QueryDataReader

The new instance with the specified batch size.

                final public function withBatchSize(?int $batchSize): static
{
    if ($batchSize !== null && $batchSize < 1) {
        throw new InvalidArgumentException('$batchSize cannot be less than 1.');
    }
    $new = clone $this;
    $new->batchSize = $batchSize;
    return $new;
}

            
withCountParam() public method

public withCountParam( string|null $countParam ): Yiisoft\Data\Db\QueryDataReader
$countParam string|null
return Yiisoft\Data\Db\QueryDataReader

The new instance with the specified count parameter.

                final public function withCountParam(?string $countParam): static
{
    if ($this->countParam === $countParam) {
        return $this;
    }
    $new = clone $this;
    $new->count = null;
    $new->countParam = $countParam;
    return $new;
}

            
withFilter() public method

public withFilter( \Yiisoft\Data\Reader\FilterInterface $filter ): Yiisoft\Data\Db\QueryDataReader
$filter \Yiisoft\Data\Reader\FilterInterface
return Yiisoft\Data\Db\QueryDataReader

The new instance with the specified filter.

                final public function withFilter(FilterInterface $filter): static
{
    $new = clone $this;
    $new->filter = $filter;
    $new->count = $new->cache = null;
    return $new;
}

            
withHaving() public method

public withHaving( \Yiisoft\Data\Reader\FilterInterface $having ): Yiisoft\Data\Db\QueryDataReader
$having \Yiisoft\Data\Reader\FilterInterface
return Yiisoft\Data\Db\QueryDataReader

The new instance with the specified having condition.

                final public function withHaving(FilterInterface $having): static
{
    $new = clone $this;
    $new->having = $having;
    $new->count = $new->cache = null;
    return $new;
}

            
withLimit() public method

public withLimit( integer|null $limit ): Yiisoft\Data\Db\QueryDataReader
$limit integer|null
return Yiisoft\Data\Db\QueryDataReader

The new instance with the specified limit.

                final public function withLimit(?int $limit): static
{
    if ($limit < 0) {
        throw new InvalidArgumentException('$limit must not be less than 0.');
    }
    $new = clone $this;
    $new->cache = null;
    $new->limit = $limit;
    return $new;
}

            
withOffset() public method

public withOffset( integer $offset ): Yiisoft\Data\Db\QueryDataReader
$offset integer
return Yiisoft\Data\Db\QueryDataReader

The new instance with the specified offset.

                final public function withOffset(int $offset): static
{
    $new = clone $this;
    $new->cache = null;
    $new->offset = $offset;
    return $new;
}

            
withSort() public method

public withSort( \Yiisoft\Data\Reader\Sort|null $sort ): Yiisoft\Data\Db\QueryDataReader
$sort \Yiisoft\Data\Reader\Sort|null
return Yiisoft\Data\Db\QueryDataReader

The new instance with the specified sort.

                final public function withSort(?Sort $sort): static
{
    $new = clone $this;
    $new->cache = null;
    $new->sort = $sort;
    return $new;
}