Skip to content

Commit

Permalink
[10.x] Add selectResultsets to database Connection (#46592)
Browse files Browse the repository at this point in the history
* add `selectResultsets` to database Connection

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
pistvan and taylorotwell authored Mar 29, 2023
1 parent 6510073 commit b760e61
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,39 @@ public function select($query, $bindings = [], $useReadPdo = true)
});
}

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

$statement = $this->prepared(
$this->getPdoForSelect($useReadPdo)->prepare($query)
);

$this->bindValues($statement, $this->prepareBindings($bindings));

$statement->execute();

$sets = [];

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

return $sets;
});
}

/**
* Run a select statement against the database and returns a generator.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ public function testSelectProperlyCallsPDO()
$this->assertIsNumeric($log[0]['time']);
}

public function testSelectResultsetsReturnsMultipleRowset()
{
$pdo = $this->getMockBuilder(DatabaseConnectionTestMockPDO::class)->onlyMethods(['prepare'])->getMock();
$writePdo = $this->getMockBuilder(DatabaseConnectionTestMockPDO::class)->onlyMethods(['prepare'])->getMock();
$writePdo->expects($this->never())->method('prepare');
$statement = $this->getMockBuilder('PDOStatement')
->onlyMethods(['setFetchMode', 'execute', 'fetchAll', 'bindValue', 'nextRowset'])
->getMock();
$statement->expects($this->once())->method('setFetchMode');
$statement->expects($this->once())->method('bindValue')->with(1, 'foo', 2);
$statement->expects($this->once())->method('execute');
$statement->expects($this->atLeastOnce())->method('fetchAll')->willReturn(['boom']);
$statement->expects($this->atLeastOnce())->method('nextRowset')->will($this->returnCallback(function () {
static $i = 1;

return ++$i <= 2;
}));
$pdo->expects($this->once())->method('prepare')->with('CALL a_procedure(?)')->willReturn($statement);
$mock = $this->getMockConnection(['prepareBindings'], $writePdo);
$mock->setReadPdo($pdo);
$mock->expects($this->once())->method('prepareBindings')->with($this->equalTo(['foo']))->willReturn(['foo']);
$results = $mock->selectResultsets('CALL a_procedure(?)', ['foo']);
$this->assertEquals([['boom'], ['boom']], $results);
$log = $mock->getQueryLog();
$this->assertSame('CALL a_procedure(?)', $log[0]['query']);
$this->assertEquals(['foo'], $log[0]['bindings']);
$this->assertIsNumeric($log[0]['time']);
}

public function testInsertCallsTheStatementMethod()
{
$connection = $this->getMockConnection(['statement']);
Expand Down

0 comments on commit b760e61

Please sign in to comment.