From 5d288412ba5795058405cc58466558cad728402d Mon Sep 17 00:00:00 2001 From: Carlos Date: Tue, 2 Sep 2025 14:17:21 -0300 Subject: [PATCH] add: 'lazy' method --- src/ResultSet.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/ResultSet.php b/src/ResultSet.php index ca0bc8f..23a323a 100644 --- a/src/ResultSet.php +++ b/src/ResultSet.php @@ -85,6 +85,24 @@ public function allGroup($uniq = false, $callable = null) return $this->statement->fetchAll($fetchStyle | PDO::FETCH_FUNC, $callable); } + /** + * Return a generator that fetches each row at a time, reducing memory load. + * + * @param int $fetchStyle (optional) PDO fetch style + * + * @return iterable + */ + public function lazy($fetchStyle = 0) + { + $generator = function() use (&$fetchStyle) { + while($row = $this->statement->fetch($fetchStyle)) { + yield $row; + } + }; + + return $generator(); + } + /** * Fetch first result *