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

[9.x] Added a way to retrieve the first column of the first row from a query #41858

Merged
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
27 changes: 27 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,33 @@ public function selectOne($query, $bindings = [], $useReadPdo = true)
return array_shift($records);
}

/**
* Run a select statement and return the first column of the first row.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @return mixed
*
* @throws MultipleColumnsSelectedException
*/
public function scalar($query, $bindings = [], $useReadPdo = true)
{
$record = $this->selectOne($query, $bindings, $useReadPdo);

if (is_null($record)) {
return null;
}

$record = (array) $record;

if (count($record) > 1) {
throw new MultipleColumnsSelectedException;
}

return reset($record);
}

/**
* Run a select statement against the database.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/MultipleColumnsSelectedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Database;

use RuntimeException;

class MultipleColumnsSelectedException extends RuntimeException
{
//
}
23 changes: 23 additions & 0 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Database\Events\TransactionBeginning;
use Illuminate\Database\Events\TransactionCommitted;
use Illuminate\Database\Events\TransactionRolledBack;
use Illuminate\Database\MultipleColumnsSelectedException;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Grammars\Grammar;
use Illuminate\Database\Query\Processors\Processor;
Expand Down Expand Up @@ -56,6 +57,28 @@ public function testSelectOneCallsSelectAndReturnsSingleResult()
$this->assertSame('foo', $connection->selectOne('foo', ['bar' => 'baz']));
}

public function testScalarCallsSelectOneAndReturnsSingleResult()
{
$connection = $this->getMockConnection(['selectOne']);
$connection->expects($this->once())->method('selectOne')->with('select count(*) from tbl')->willReturn((object) ['count(*)' => 5]);
$this->assertSame(5, $connection->scalar('select count(*) from tbl'));
}

public function testScalarThrowsExceptionIfMultipleColumnsAreSelected()
{
$connection = $this->getMockConnection(['selectOne']);
$connection->expects($this->once())->method('selectOne')->with('select a, b from tbl')->willReturn((object) ['a' => 'a', 'b' => 'b']);
$this->expectException(MultipleColumnsSelectedException::class);
$connection->scalar('select a, b from tbl');
}

public function testScalarReturnsNullIfUnderlyingSelectReturnsNoRows()
{
$connection = $this->getMockConnection(['selectOne']);
$connection->expects($this->once())->method('selectOne')->with('select foo from tbl where 0=1')->willReturn(null);
$this->assertNull($connection->scalar('select foo from tbl where 0=1'));
}

public function testSelectProperlyCallsPDO()
{
$pdo = $this->getMockBuilder(DatabaseConnectionTestMockPDO::class)->onlyMethods(['prepare'])->getMock();
Expand Down