Skip to content

Commit

Permalink
#17 Implement cursor function, disable WP display error
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitriBouteille committed Mar 9, 2024
1 parent 2f30c17 commit cfa679a
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/Orm/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,14 @@ public function select($query, $bindings = [], $useReadPdo = true): array
}

/**
* Run a select statement against the database and returns a generator.
* TODO: Implement cursor and all the related sub-methods.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @return \Generator
* @inheritDoc
*/
public function cursor($query, $bindings = [], $useReadPdo = true)
public function cursor($query, $bindings = [], $useReadPdo = true): \Generator
{

$results = $this->select($query, $bindings, $useReadPdo);
foreach ($results as $result) {
yield $result;
}
}

/**
Expand Down Expand Up @@ -327,6 +324,7 @@ public function commit(): void
if ($this->transactionCount < 1) {
return;
}

$transaction = $this->unprepared('COMMIT;');
if ($transaction) {
$this->transactionCount--;
Expand All @@ -341,6 +339,7 @@ public function rollBack(): void
if ($this->transactionCount < 1) {
return;
}

$transaction = $this->unprepared('ROLLBACK;');
if ($transaction) {
$this->transactionCount--;
Expand All @@ -358,6 +357,7 @@ public function transactionLevel(): int
/**
* @inheritDoc
* @throws WpOrmException
* @see https://laravel.com/docs/10.x/eloquent#pruning-models
*/
public function pretend(\Closure $callback): array
{
Expand Down Expand Up @@ -465,7 +465,14 @@ protected function run(string $query, array $binding, \Closure $callback): mixed
protected function runQueryCallback(string $query, array $bindings, \Closure $callback): mixed
{
try {
// Disable display WP error and save previous state
$suppressionError = $this->db->suppress_errors();

$result = $callback($query, $bindings);

// Restore the state
$this->db->suppress_errors($suppressionError);

if ($result === false || $this->lastRequestHasError()) {
throw new \Exception($this->db->last_error);
}
Expand Down

0 comments on commit cfa679a

Please sign in to comment.