Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Allows developers to traverse their query result via a cursor #13030

Merged
merged 9 commits into from
May 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,34 @@ public function select($query, $bindings = [], $useReadPdo = true)
});
}

/**
* Run a select statement against the database and returns a cursor.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @return mixed
*/
public function cursor($query, $bindings = [], $useReadPdo = true)
{
return $this->run($query, $bindings, function ($me, $query, $bindings) use ($useReadPdo) {
if ($me->pretending()) {
return [];
}

// For select statements, we'll simply execute the query and return an array
// of the database result set. Each element in the array will be a single
// row from the database table, and will either be an array or objects.
$statement = $this->getPdoForSelect($useReadPdo)->prepare($query);

$statement->setFetchMode($me->getFetchMode());

$statement->execute($me->prepareBindings($bindings));

return $statement;
});
}

/**
* Get the PDO connection to use for a select query.
*
Expand Down
27 changes: 27 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,33 @@ public function firstOrFail($columns = ['*'])
throw (new ModelNotFoundException)->setModel(get_class($this->model));
}

/**
* Traverses through a result set using a cursor.
*
* @return void
*/
public function cursor()
{
$builder = $this->applyScopes();

$statement = $builder->query->cursor();

while ($row = $statement->fetch()) {
// On each result set, we will pass them to the callback and then let the
// developer take care of everything within the callback, which allows us to
// keep the memory low for spinning through large result sets for working.

if ($row === false) {
return;
}

//Hydrate and yield an Eloquent Model
$model = $this->model->newFromBuilder($row);

yield $model;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally, the first php yield keyword in laravel! 😀👍

}
}

/**
* Execute the query as a "select" statement.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,18 @@ protected function restoreFieldsForCount()
$this->bindingBackups = [];
}

/**
* Execute the query as a "select" statement.
*
* @return mixed
*/
public function cursor()
{
$results = $this->connection->cursor($this->toSql(), $this->getBindings(), ! $this->useWritePdo);

return $results;
}

/**
* Chunk the results of the query.
*
Expand Down