Skip to content

Commit ad719b5

Browse files
authored
Revert "[12.x] Query builder PDO fetch modes (#54443)" (#54709)
This reverts commit ca7ca2c.
1 parent 6f0c2d0 commit ad719b5

9 files changed

+110
-179
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,11 @@ public function selectFromWriteConnection($query, $bindings = [])
389389
* @param string $query
390390
* @param array $bindings
391391
* @param bool $useReadPdo
392-
* @param array $fetchUsing
393392
* @return array
394393
*/
395-
public function select($query, $bindings = [], $useReadPdo = true, array $fetchUsing = [])
394+
public function select($query, $bindings = [], $useReadPdo = true)
396395
{
397-
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo, $fetchUsing) {
396+
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
398397
if ($this->pretending()) {
399398
return [];
400399
}
@@ -410,7 +409,7 @@ public function select($query, $bindings = [], $useReadPdo = true, array $fetchU
410409

411410
$statement->execute();
412411

413-
return $statement->fetchAll(...$fetchUsing);
412+
return $statement->fetchAll();
414413
});
415414
}
416415

@@ -420,12 +419,11 @@ public function select($query, $bindings = [], $useReadPdo = true, array $fetchU
420419
* @param string $query
421420
* @param array $bindings
422421
* @param bool $useReadPdo
423-
* @param array $fetchUsing
424422
* @return array
425423
*/
426-
public function selectResultSets($query, $bindings = [], $useReadPdo = true, array $fetchUsing = [])
424+
public function selectResultSets($query, $bindings = [], $useReadPdo = true)
427425
{
428-
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo, $fetchUsing) {
426+
return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
429427
if ($this->pretending()) {
430428
return [];
431429
}
@@ -441,7 +439,7 @@ public function selectResultSets($query, $bindings = [], $useReadPdo = true, arr
441439
$sets = [];
442440

443441
do {
444-
$sets[] = $statement->fetchAll(...$fetchUsing);
442+
$sets[] = $statement->fetchAll();
445443
} while ($statement->nextRowset());
446444

447445
return $sets;
@@ -454,10 +452,9 @@ public function selectResultSets($query, $bindings = [], $useReadPdo = true, arr
454452
* @param string $query
455453
* @param array $bindings
456454
* @param bool $useReadPdo
457-
* @param array $fetchUsing
458455
* @return \Generator<int, \stdClass>
459456
*/
460-
public function cursor($query, $bindings = [], $useReadPdo = true, array $fetchUsing = [])
457+
public function cursor($query, $bindings = [], $useReadPdo = true)
461458
{
462459
$statement = $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
463460
if ($this->pretending()) {
@@ -482,7 +479,7 @@ public function cursor($query, $bindings = [], $useReadPdo = true, array $fetchU
482479
return $statement;
483480
});
484481

485-
while ($record = $statement->fetch(...$fetchUsing)) {
482+
while ($record = $statement->fetch()) {
486483
yield $record;
487484
}
488485
}

src/Illuminate/Database/ConnectionInterface.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,19 @@ public function scalar($query, $bindings = [], $useReadPdo = true);
5151
* @param string $query
5252
* @param array $bindings
5353
* @param bool $useReadPdo
54-
* @param array $fetchUsing
5554
* @return array
5655
*/
57-
public function select($query, $bindings = [], $useReadPdo = true, array $fetchUsing = []);
56+
public function select($query, $bindings = [], $useReadPdo = true);
5857

5958
/**
6059
* Run a select statement against the database and returns a generator.
6160
*
6261
* @param string $query
6362
* @param array $bindings
6463
* @param bool $useReadPdo
65-
* @param array $fetchUsing
6664
* @return \Generator
6765
*/
68-
public function cursor($query, $bindings = [], $useReadPdo = true, array $fetchUsing = []);
66+
public function cursor($query, $bindings = [], $useReadPdo = true);
6967

7068
/**
7169
* Run an insert statement against the database.

src/Illuminate/Database/Query/Builder.php

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,6 @@ class Builder implements BuilderContract
249249
*/
250250
public $useWritePdo = false;
251251

252-
/**
253-
* The custom arguments for the PDOStatement::fetchAll / fetch functions.
254-
*
255-
* @var array
256-
*/
257-
public array $fetchUsing = [];
258-
259252
/**
260253
* Create a new query builder instance.
261254
*
@@ -3094,13 +3087,9 @@ public function soleValue($column)
30943087
*/
30953088
public function get($columns = ['*'])
30963089
{
3097-
$original = $this->columns;
3098-
3099-
$this->columns ??= Arr::wrap($columns);
3100-
3101-
$items = new Collection($this->processor->processSelect($this, $this->runSelect()));
3102-
3103-
$this->columns = $original;
3090+
$items = new Collection($this->onceWithColumns(Arr::wrap($columns), function () {
3091+
return $this->processor->processSelect($this, $this->runSelect());
3092+
}));
31043093

31053094
return $this->applyAfterQueryCallbacks(
31063095
isset($this->groupLimit) ? $this->withoutGroupLimitKeys($items) : $items
@@ -3115,7 +3104,7 @@ public function get($columns = ['*'])
31153104
protected function runSelect()
31163105
{
31173106
return $this->connection->select(
3118-
$this->toSql(), $this->getBindings(), ! $this->useWritePdo, $this->fetchUsing
3107+
$this->toSql(), $this->getBindings(), ! $this->useWritePdo
31193108
);
31203109
}
31213110

@@ -3333,7 +3322,7 @@ public function cursor()
33333322

33343323
return (new LazyCollection(function () {
33353324
yield from $this->connection->cursor(
3336-
$this->toSql(), $this->getBindings(), ! $this->useWritePdo, $this->fetchUsing
3325+
$this->toSql(), $this->getBindings(), ! $this->useWritePdo
33373326
);
33383327
}))->map(function ($item) {
33393328
return $this->applyAfterQueryCallbacks(new Collection([$item]))->first();
@@ -3363,18 +3352,17 @@ protected function enforceOrderBy()
33633352
*/
33643353
public function pluck($column, $key = null)
33653354
{
3366-
$original = $this->columns;
3367-
33683355
// First, we will need to select the results of the query accounting for the
33693356
// given columns / key. Once we have the results, we will be able to take
33703357
// the results and get the exact data that was requested for the query.
3371-
$this->columns = is_null($key) || $key === $column
3372-
? [$column]
3373-
: [$column, $key];
3374-
3375-
$queryResult = $this->processor->processSelect($this, $this->runSelect());
3376-
3377-
$this->columns = $original;
3358+
$queryResult = $this->onceWithColumns(
3359+
is_null($key) || $key === $column ? [$column] : [$column, $key],
3360+
function () {
3361+
return $this->processor->processSelect(
3362+
$this, $this->runSelect()
3363+
);
3364+
}
3365+
);
33783366

33793367
if (empty($queryResult)) {
33803368
return new Collection;
@@ -3668,6 +3656,30 @@ protected function setAggregate($function, $columns)
36683656
return $this;
36693657
}
36703658

3659+
/**
3660+
* Execute the given callback while selecting the given columns.
3661+
*
3662+
* After running the callback, the columns are reset to the original value.
3663+
*
3664+
* @param array $columns
3665+
* @param callable $callback
3666+
* @return mixed
3667+
*/
3668+
protected function onceWithColumns($columns, $callback)
3669+
{
3670+
$original = $this->columns;
3671+
3672+
if (is_null($original)) {
3673+
$this->columns = $columns;
3674+
}
3675+
3676+
$result = $callback();
3677+
3678+
$this->columns = $original;
3679+
3680+
return $result;
3681+
}
3682+
36713683
/**
36723684
* Insert new records into the database.
36733685
*
@@ -4281,19 +4293,6 @@ public function useWritePdo()
42814293
return $this;
42824294
}
42834295

4284-
/**
4285-
* Specify arguments for the PDOStatement::fetchAll / fetch functions.
4286-
*
4287-
* @param mixed ...$fetchUsing
4288-
* @return $this
4289-
*/
4290-
public function fetchUsing(...$fetchUsing)
4291-
{
4292-
$this->fetchUsing = $fetchUsing;
4293-
4294-
return $this;
4295-
}
4296-
42974296
/**
42984297
* Determine if the value is a query builder instance or a Closure.
42994298
*

tests/Database/DatabaseEloquentBelongsToManyCreateOrFirstTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testCreateOrFirstMethodAssociatesExistingRelated(): void
8484

8585
$source->getConnection()
8686
->expects('select')
87-
->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true, [])
87+
->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true)
8888
->andReturn([[
8989
'id' => 456,
9090
'attr' => 'foo',
@@ -128,7 +128,6 @@ public function testFirstOrCreateMethodRetrievesExistingRelatedAlreadyAssociated
128128
'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',
129129
[123, 'foo'],
130130
true,
131-
[],
132131
)
133132
->andReturn([[
134133
'id' => 456,
@@ -177,7 +176,7 @@ public function testCreateOrFirstMethodRetrievesExistingRelatedAssociatedJustNow
177176

178177
$source->getConnection()
179178
->expects('select')
180-
->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true, [])
179+
->with('select * from "related_table" where ("attr" = ?) limit 1', ['foo'], true)
181180
->andReturn([[
182181
'id' => 456,
183182
'attr' => 'foo',
@@ -200,7 +199,6 @@ public function testCreateOrFirstMethodRetrievesExistingRelatedAssociatedJustNow
200199
'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',
201200
[123, 'foo'],
202201
false,
203-
[],
204202
)
205203
->andReturn([[
206204
'id' => 456,
@@ -245,7 +243,6 @@ public function testFirstOrCreateMethodRetrievesExistingRelatedAndAssociatesIt()
245243
'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',
246244
[123, 'foo'],
247245
true,
248-
[],
249246
)
250247
->andReturn([]);
251248

@@ -255,7 +252,6 @@ public function testFirstOrCreateMethodRetrievesExistingRelatedAndAssociatesIt()
255252
'select * from "related_table" where ("attr" = ?) limit 1',
256253
['foo'],
257254
true,
258-
[],
259255
)
260256
->andReturn([[
261257
'id' => 456,
@@ -330,7 +326,6 @@ protected function newBelongsToMany(Builder $query, Model $parent, $table, $fore
330326
'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',
331327
[123, 'foo'],
332328
true,
333-
[],
334329
)
335330
->andReturn([]);
336331

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

0 commit comments

Comments
 (0)