Make WordPress Core

Opened 9 months ago

Last modified 8 months ago

#59281 new feature request

Support for Database Cursor

Reported by: heera's profile heera 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)

#1 in reply to: ↑ description @heera
9 months ago

Replying to 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;
    }
}

#2 @sabernhardt
8 months ago

  • Component changed from General to Database
Note: See TracTickets for help on using tickets.