Skip to content

Commit

Permalink
optimize ::any() and ::none() for the sql adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Feb 10, 2024
1 parent 7dfb8a9 commit 9a552fc
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Adapter/SQL/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,24 @@ public function size(Specification $specification = null): int

public function any(Specification $specification = null): bool
{
return $this->size($specification) !== 0;
$count = $this
->mainTable
->count($specification)
->limit(1);

return ($this->connection)($count)
->first()
->flatMap(static fn($row) => $row->column('count'))
->filter(\is_numeric(...))
->map(static fn($count) => (int) $count)
->match(
static fn($count) => $count !== 0,
static fn() => false,
);
}

public function none(Specification $specification = null): bool
{
return $this->size($specification) === 0;
return !$this->any($specification);
}
}

0 comments on commit 9a552fc

Please sign in to comment.