Skip to content

Commit 3d559b6

Browse files
committed
cursor方法返回值改为lazyCollection对象
1 parent c8d60e4 commit 3d559b6

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/db/Query.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,17 @@ public function getPdo(): PDOStatement
507507
*
508508
* @param bool $unbuffered 是否开启无缓冲查询(仅限mysql)
509509
*
510-
* @return \Generator
510+
* @return LazyCollection
511511
*/
512-
public function cursor(bool $unbuffered = false)
512+
public function cursor(bool $unbuffered = false): LazyCollection
513513
{
514514
$connection = clone $this->connection;
515515

516-
return $connection->cursor($this, $unbuffered);
516+
return new LazyCollection(function () use ($connection, $unbuffered) {
517+
yield from $connection->cursor($this, $unbuffered);
518+
});
517519
}
518-
520+
519521
/**
520522
* 流式处理查询结果
521523
*
@@ -527,10 +529,11 @@ public function cursor(bool $unbuffered = false)
527529
public function stream(callable $callback, bool $unbuffered = false): int
528530
{
529531
$count = 0;
530-
foreach ($this->cursor($unbuffered) as $item) {
531-
$callback($item);
532-
$count++;
533-
}
532+
$this->cursor($unbuffered)
533+
->each(function ($item) use ($callback, &$count) {
534+
$callback($item);
535+
$count++;
536+
});
534537
return $count;
535538
}
536539

@@ -610,7 +613,7 @@ public function chunk(int $count, callable $callback, string | array | null $col
610613
* @param string $order 字段排序
611614
* @return LazyCollection
612615
*/
613-
public function lazy(int $count = 1000, ?string $column = null, string $order = 'desc')
616+
public function lazy(int $count = 1000, ?string $column = null, string $order = 'desc'): LazyCollection
614617
{
615618
if ($count < 1) {
616619
throw new Exception('The chunk size should be at least 1');

tests/orm/StreamQueryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use think\facade\Db;
77
use think\DbManager;
8+
use think\model\LazyCollection;
89

910
class StreamQueryTest extends TestCase
1011
{
@@ -55,7 +56,7 @@ public function testBasicCursor()
5556
{
5657
$cursor = Db::table('user')->cursor();
5758

58-
$this->assertInstanceOf(\Generator::class, $cursor);
59+
$this->assertInstanceOf(LazyCollection::class, $cursor);
5960

6061
$count = 0;
6162
foreach ($cursor as $user) {

0 commit comments

Comments
 (0)