Skip to content

Commit

Permalink
Added a way to retrieve the first column of the first row from a quer…
Browse files Browse the repository at this point in the history
…y. (#41858)

* Added a way to retrieve the first column of the first row from a query result.

* styleci fixes
  • Loading branch information
peterlupu authored Apr 6, 2022
1 parent 76a158e commit 1a43d11
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
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

0 comments on commit 1a43d11

Please sign in to comment.