Opened 3 years ago
Last modified 3 years ago
#59281 new feature request
Support for Database Cursor
| Reported by: | heera | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Database | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
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.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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; } }