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

Revert "[12.x] Query builder PDO fetch modes" #54709

Merged
merged 1 commit into from
Feb 19, 2025
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
19 changes: 8 additions & 11 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,11 @@ public function selectFromWriteConnection($query, $bindings = [])
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @param array $fetchUsing
* @return array
*/
public function select($query, $bindings = [], $useReadPdo = true, array $fetchUsing = [])
public function select($query, $bindings = [], $useReadPdo = true)
{
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo, $fetchUsing) {
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
if ($this->pretending()) {
return [];
}
Expand All @@ -410,7 +409,7 @@ public function select($query, $bindings = [], $useReadPdo = true, array $fetchU

$statement->execute();

return $statement->fetchAll(...$fetchUsing);
return $statement->fetchAll();
});
}

Expand All @@ -420,12 +419,11 @@ public function select($query, $bindings = [], $useReadPdo = true, array $fetchU
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @param array $fetchUsing
* @return array
*/
public function selectResultSets($query, $bindings = [], $useReadPdo = true, array $fetchUsing = [])
public function selectResultSets($query, $bindings = [], $useReadPdo = true)
{
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo, $fetchUsing) {
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
if ($this->pretending()) {
return [];
}
Expand All @@ -441,7 +439,7 @@ public function selectResultSets($query, $bindings = [], $useReadPdo = true, arr
$sets = [];

do {
$sets[] = $statement->fetchAll(...$fetchUsing);
$sets[] = $statement->fetchAll();
} while ($statement->nextRowset());

return $sets;
Expand All @@ -454,10 +452,9 @@ public function selectResultSets($query, $bindings = [], $useReadPdo = true, arr
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @param array $fetchUsing
* @return \Generator<int, \stdClass>
*/
public function cursor($query, $bindings = [], $useReadPdo = true, array $fetchUsing = [])
public function cursor($query, $bindings = [], $useReadPdo = true)
{
$statement = $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
if ($this->pretending()) {
Expand All @@ -482,7 +479,7 @@ public function cursor($query, $bindings = [], $useReadPdo = true, array $fetchU
return $statement;
});

while ($record = $statement->fetch(...$fetchUsing)) {
while ($record = $statement->fetch()) {
yield $record;
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/Illuminate/Database/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,19 @@ public function scalar($query, $bindings = [], $useReadPdo = true);
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @param array $fetchUsing
* @return array
*/
public function select($query, $bindings = [], $useReadPdo = true, array $fetchUsing = []);
public function select($query, $bindings = [], $useReadPdo = true);

/**
* Run a select statement against the database and returns a generator.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @param array $fetchUsing
* @return \Generator
*/
public function cursor($query, $bindings = [], $useReadPdo = true, array $fetchUsing = []);
public function cursor($query, $bindings = [], $useReadPdo = true);

/**
* Run an insert statement against the database.
Expand Down
75 changes: 37 additions & 38 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,6 @@ class Builder implements BuilderContract
*/
public $useWritePdo = false;

/**
* The custom arguments for the PDOStatement::fetchAll / fetch functions.
*
* @var array
*/
public array $fetchUsing = [];

/**
* Create a new query builder instance.
*
Expand Down Expand Up @@ -3094,13 +3087,9 @@ public function soleValue($column)
*/
public function get($columns = ['*'])
{
$original = $this->columns;

$this->columns ??= Arr::wrap($columns);

$items = new Collection($this->processor->processSelect($this, $this->runSelect()));

$this->columns = $original;
$items = new Collection($this->onceWithColumns(Arr::wrap($columns), function () {
return $this->processor->processSelect($this, $this->runSelect());
}));

return $this->applyAfterQueryCallbacks(
isset($this->groupLimit) ? $this->withoutGroupLimitKeys($items) : $items
Expand All @@ -3115,7 +3104,7 @@ public function get($columns = ['*'])
protected function runSelect()
{
return $this->connection->select(
$this->toSql(), $this->getBindings(), ! $this->useWritePdo, $this->fetchUsing
$this->toSql(), $this->getBindings(), ! $this->useWritePdo
);
}

Expand Down Expand Up @@ -3333,7 +3322,7 @@ public function cursor()

return (new LazyCollection(function () {
yield from $this->connection->cursor(
$this->toSql(), $this->getBindings(), ! $this->useWritePdo, $this->fetchUsing
$this->toSql(), $this->getBindings(), ! $this->useWritePdo
);
}))->map(function ($item) {
return $this->applyAfterQueryCallbacks(new Collection([$item]))->first();
Expand Down Expand Up @@ -3363,18 +3352,17 @@ protected function enforceOrderBy()
*/
public function pluck($column, $key = null)
{
$original = $this->columns;

// First, we will need to select the results of the query accounting for the
// given columns / key. Once we have the results, we will be able to take
// the results and get the exact data that was requested for the query.
$this->columns = is_null($key) || $key === $column
? [$column]
: [$column, $key];

$queryResult = $this->processor->processSelect($this, $this->runSelect());

$this->columns = $original;
$queryResult = $this->onceWithColumns(
is_null($key) || $key === $column ? [$column] : [$column, $key],
function () {
return $this->processor->processSelect(
$this, $this->runSelect()
);
}
);

if (empty($queryResult)) {
return new Collection;
Expand Down Expand Up @@ -3668,6 +3656,30 @@ protected function setAggregate($function, $columns)
return $this;
}

/**
* Execute the given callback while selecting the given columns.
*
* After running the callback, the columns are reset to the original value.
*
* @param array $columns
* @param callable $callback
* @return mixed
*/
protected function onceWithColumns($columns, $callback)
{
$original = $this->columns;

if (is_null($original)) {
$this->columns = $columns;
}

$result = $callback();

$this->columns = $original;

return $result;
}

/**
* Insert new records into the database.
*
Expand Down Expand Up @@ -4281,19 +4293,6 @@ public function useWritePdo()
return $this;
}

/**
* Specify arguments for the PDOStatement::fetchAll / fetch functions.
*
* @param mixed ...$fetchUsing
* @return $this
*/
public function fetchUsing(...$fetchUsing)
{
$this->fetchUsing = $fetchUsing;

return $this;
}

/**
* Determine if the value is a query builder instance or a Closure.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testCreateOrFirstMethodAssociatesExistingRelated(): void

$source->getConnection()
->expects('select')
->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true, [])
->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true)
->andReturn([[
'id' => 456,
'attr' => 'foo',
Expand Down Expand Up @@ -128,7 +128,6 @@ public function testFirstOrCreateMethodRetrievesExistingRelatedAlreadyAssociated
'select "related_table".*, "pivot_table"."source_id" as "pivot_source_id", "pivot_table"."related_id" as "pivot_related_id" from "related_table" inner join "pivot_table" on "related_table"."id" = "pivot_table"."related_id" where "pivot_table"."source_id" = ? and ("attr" = ?) limit 1',
[123, 'foo'],
true,
[],
)
->andReturn([[
'id' => 456,
Expand Down Expand Up @@ -177,7 +176,7 @@ public function testCreateOrFirstMethodRetrievesExistingRelatedAssociatedJustNow

$source->getConnection()
->expects('select')
->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true, [])
->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true)
->andReturn([[
'id' => 456,
'attr' => 'foo',
Expand All @@ -200,7 +199,6 @@ public function testCreateOrFirstMethodRetrievesExistingRelatedAssociatedJustNow
'select "related_table".*, "pivot_table"."source_id" as "pivot_source_id", "pivot_table"."related_id" as "pivot_related_id" from "related_table" inner join "pivot_table" on "related_table"."id" = "pivot_table"."related_id" where "pivot_table"."source_id" = ? and ("attr" = ?) limit 1',
[123, 'foo'],
false,
[],
)
->andReturn([[
'id' => 456,
Expand Down Expand Up @@ -245,7 +243,6 @@ public function testFirstOrCreateMethodRetrievesExistingRelatedAndAssociatesIt()
'select "related_table".*, "pivot_table"."source_id" as "pivot_source_id", "pivot_table"."related_id" as "pivot_related_id" from "related_table" inner join "pivot_table" on "related_table"."id" = "pivot_table"."related_id" where "pivot_table"."source_id" = ? and ("attr" = ?) limit 1',
[123, 'foo'],
true,
[],
)
->andReturn([]);

Expand All @@ -255,7 +252,6 @@ public function testFirstOrCreateMethodRetrievesExistingRelatedAndAssociatesIt()
'select * from "related_table" where ("attr" = ?) limit 1',
['foo'],
true,
[],
)
->andReturn([[
'id' => 456,
Expand Down Expand Up @@ -330,7 +326,6 @@ protected function newBelongsToMany(Builder $query, Model $parent, $table, $fore
'select "related_table".*, "pivot_table"."source_id" as "pivot_source_id", "pivot_table"."related_id" as "pivot_related_id" from "related_table" inner join "pivot_table" on "related_table"."id" = "pivot_table"."related_id" where "pivot_table"."source_id" = ? and ("attr" = ?) limit 1',
[123, 'foo'],
true,
[],
)
->andReturn([]);

Expand All @@ -340,7 +335,6 @@ protected function newBelongsToMany(Builder $query, Model $parent, $table, $fore
'select * from "related_table" where ("attr" = ?) limit 1',
['foo'],
true,
[],
)
->andReturn([]);

Expand Down
Loading
Loading