laravel cursor的底层实现
admin
2023-06-13 18:22:21
0
foreach (BillInfo::where('id', '>', 0)->orderBy('id', 'desc')->limit(10000)->cursor() as $value) {
   print_r($value);
}


Illuminate\Database\Query\Builder.php;
/**
* Get a generator for the given query.
*
* @return \Generator
*/
public function cursor()
{
   if (is_null($this->columns)) {
       $this->columns = ['*'];
   }

   return $this->connection->cursor(
       $this->toSql(), $this->getBindings(), ! $this->useWritePdo
   );
}


Illuminate\Database\Connection.php;
/**
* Run a select statement against the database and returns a generator.
*
* @param  string  $query
* @param  array  $bindings
* @param  bool  $useReadPdo
* @return \Generator
*/
public function cursor($query, $bindings = [], $useReadPdo = true)
{
   $statement = $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
       if ($this->pretending()) {
           return [];
       }

       // First we will create a statement for the query. Then, we will set the fetch
       // mode and prepare the bindings for the query. Once that's done we will be
       // ready to execute the query against the database and return the cursor.
       $statement = $this->prepared($this->getPdoForSelect($useReadPdo)
                         ->prepare($query));

       $this->bindValues(
           $statement, $this->prepareBindings($bindings)
       );

       // Next, we'll execute the query against the database and return the statement
       // so we can return the cursor. The cursor will use a PHP generator to give
       // back one row at a time without using a bunch of memory to render them.
       $statement->execute();

       return $statement;
   });

   while ($record = $statement->fetch()) {
       yield $record;
   }
}

相关内容

热门资讯

重庆永川遭遇特大暴雨,67岁会... 5月26日上午11点多,潇湘晨报·晨视频记者从重庆市委宣传部获悉,在重庆永川区茶山竹海街道大桥村特大...
陈克明为“手擀”商标道歉,40... 极目新闻评论员 屈旌一袋挂面能藏着多少的心眼?陈克明以40年商誉为代价,给出了一个令人唏嘘的答案。近...
荆州鸿创机械取得折弯机用挡料机... 国家知识产权局信息显示,荆州鸿创机械制造有限公司取得一项名为“一种折弯机用挡料机构”的专利,授权公告...
领雁科技数智渠道5.0:助力银... 5月23日,信华信集团举办三十周年庆典,同期召开“智敬未来·深度经营——领雁AI+金融科技创新产品发...
视频丨香港各界:神舟二十三号成... 神舟二十三号载人飞船发射取得圆满成功,引发香港社会各界热烈反响。在接受总台记者采访时,不少人表示深受...
算法1年翻倍,芯片2年翻倍?重... 新智元报道 【新智元导读】NBER重磅论文证明:AI研发的自我加速反馈环强度远超所有科技领域,算...
伊朗外交部发言人否认收取海峡“... 【文/观察者网 齐倩】美媒近期频繁放风称,美伊即将达成协议。但伊朗方面表示,双方仍就一、两项条款存在...
“永川兄弟妯娌4人被洪水卷走”... 极目新闻记者 谢茂 邓波5月23日夜间至24日凌晨,重庆市永川区突发瞬时极端特大暴雨引发山洪和地灾,...
朱媛媛,提名最佳女配角 5月26日上午,第31届上海电视节白玉兰奖电视剧类别中国电视剧单元入围名单揭晓。其中,朱媛媛凭借遗作...
“迄今为止最严厉表态”!加总理... 【环球网报道】据英国广播公司(BBC)、美国《纽约时报》等媒体报道,加拿大总理卡尼当地时间25日表示...