0 follower

Class Yiisoft\Data\Db\QueryDataReader

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

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Db\Query\QueryInterface $query, ?\Yiisoft\Data\Reader\Sort $sort null, integer $offset 0, ?int $limit null, ?string $countParam null, \Yiisoft\Data\Reader\FilterInterface $filter = new All(), \Yiisoft\Data\Reader\FilterInterface $having = new All(), ?int $batchSize null, array $extraFilterHandlers = [], array|Yiisoft\Data\Db\FieldMapper\FieldMapperInterface $fieldMapper = [] )
$query \Yiisoft\Db\Query\QueryInterface
$sort ?\Yiisoft\Data\Reader\Sort
$offset integer
$limit ?int
$countParam ?string
$filter \Yiisoft\Data\Reader\FilterInterface
$having \Yiisoft\Data\Reader\FilterInterface
$batchSize ?int
$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 integer count ( )

                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 \Yiisoft\Data\Reader\FilterInterface getFilter ( )

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

            
getIterator() public method

public Generator getIterator ( )

                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 ?int getLimit ( )

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

            
getOffset() public method

public integer getOffset ( )

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

            
getPreparedQuery() public method

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

                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 ?\Yiisoft\Data\Reader\Sort getSort ( )

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

            
read() public method

public Generator read ( )

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

            
readOne() public method

public array|object|null readOne ( )

                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 static withBatchSize ( ?int $batchSize )
$batchSize ?int
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 static withCountParam ( ?string $countParam )
$countParam ?string
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 static withFilter ( \Yiisoft\Data\Reader\FilterInterface $filter )
$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 static withHaving ( \Yiisoft\Data\Reader\FilterInterface $having )
$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 static withLimit ( ?int $limit )
$limit ?int
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 static withOffset ( integer $offset )
$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 static withSort ( ?\Yiisoft\Data\Reader\Sort $sort )
$sort ?\Yiisoft\Data\Reader\Sort
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;
}