Opened 2 years ago
Last modified 2 years ago
#59281 new feature request
Support for Database Cursor
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Database | Keywords: | |
| Focuses: | Cc: |
Description
Is it possible to do a cursor query using the $wpdb instance or could it be added, which will return a generator (yield)?
Change History (2)
Note: See
TracTickets for help on using
tickets.
Replying to heera:
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; } }