[wp-trac] [WordPress Trac] #59281: Support for Database Cursor

WordPress Trac noreply at wordpress.org
Mon Sep 4 19:40:06 UTC 2023


#59281: Support for Database Cursor
-----------------------------+------------------------------
 Reporter:  heera            |       Owner:  (none)
     Type:  feature request  |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  General          |     Version:
 Severity:  normal           |  Resolution:
 Keywords:                   |     Focuses:
-----------------------------+------------------------------

Comment (by heera):

 Replying to [ticket:59281 heera]:
 > Is it possible to do a cursor query using the `$wpdb` instance or could
 it be added, which will return a generator (yield)?

 Right now I'm using something like the following to achieve the goal, is
 it safe/reliable:



 {{{
 public function cursor($query, $bindings = [], $useReadPdo = true)
 {
     // When the underlying driver is not the mysqli.
     // it's not a pure cursor just mimicked like one.
     if (!$this->wpdb->dbh instanceof \mysqli) {
         foreach ($this->select($query, $bindings) as $row) {
             yield $row;
         }
         return;
     }

     // The underlying driver is the mysqli
     $statement = $this->wpdb->dbh->prepare(
         $this->bindParamsForSqli($query, $bindings)
     );


     $bindings && $statement->bind_param(
         str_repeat('s', count($bindings)),
         ...$bindings
     );

     if ($statement->execute()) {
         $result = $statement->get_result();

         while ($row = $result->fetch_assoc()) yield $row;
     }
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59281#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list