Class Yiisoft\Data\Db\QueryDataReader
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
Public Methods
Method Details
| public mixed __construct ( \ | ||
| $query | \ |
|
| $sort | ?\ |
|
| $offset | integer | |
| $limit | ?int | |
| $countParam | ?string | |
| $filter | \ |
|
| $having | \ |
|
| $batchSize | ?int | |
| $extraFilterHandlers | array | |
| $fieldMapper | array|Yiisoft\ |
|
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 \ |
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 \ |
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 ?\ |
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\ |
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\ |
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 ( \ | ||
| $filter | \ |
|
| return | Yiisoft\ |
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 ( \ | ||
| $having | \ |
|
| return | Yiisoft\ |
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\ |
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\ |
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 ( ?\ | ||
| $sort | ?\ |
|
| return | Yiisoft\ |
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;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.