Class Yiisoft\Data\Db\QueryDataReader
| Inheritance | Yiisoft\Data\Db\QueryDataReader |
|---|---|
| Implements | Yiisoft\Data\Db\QueryDataReaderInterface |
Public Methods
Method Details
| 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,
);
}
| 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;
}
| public \Yiisoft\Data\Reader\FilterInterface getFilter ( ) |
final public function getFilter(): FilterInterface
{
return $this->filter;
}
| 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;
}
| 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;
}
| public ?\Yiisoft\Data\Reader\Sort getSort ( ) |
final public function getSort(): ?Sort
{
return $this->sort;
}
| 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();
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
Signup or Login in order to comment.