Page MenuHomeDevCentral

No OneTemporary

diff --git a/database/src/Result/DatabaseResult.php b/database/src/Result/DatabaseResult.php
index bca93d3..11fb80d 100644
--- a/database/src/Result/DatabaseResult.php
+++ b/database/src/Result/DatabaseResult.php
@@ -1,22 +1,41 @@
<?php
namespace Keruald\Database\Result;
use IteratorAggregate;
/**
* Represents a database result
*/
abstract class DatabaseResult implements IteratorAggregate {
/**
* Gets number of rows in result
*/
public abstract function numRows () : int;
/**
* Fetches a row of the result
* @return array|null An array if there is still a row to read; null if not.
*/
public abstract function fetchRow () : ?array;
+
+ /**
+ * Fetches the value from the first column of the first row in the result.
+ *
+ * This is typically used for queries that return a single scalar value
+ * such as SELECT count(*).
+ *
+ * @return mixed|null The value, or null if no more rows.
+ */
+ public function fetchScalar () : mixed {
+ $row = $this->fetchRow();
+
+ if (!$row) {
+ return null;
+ }
+
+ $key = array_key_first($row);
+ return $row[$key];
+ }
}
diff --git a/database/tests/Result/EmptyDatabaseResultTest.php b/database/tests/Result/EmptyDatabaseResultTest.php
index a918752..bdf71c4 100644
--- a/database/tests/Result/EmptyDatabaseResultTest.php
+++ b/database/tests/Result/EmptyDatabaseResultTest.php
@@ -1,30 +1,34 @@
<?php
namespace Keruald\Database\Tests\Result;
use Keruald\Database\Result\EmptyDatabaseResult;
use PHPUnit\Framework\TestCase;
class EmptyDatabaseResultTest extends TestCase {
private EmptyDatabaseResult $result;
protected function setUp () : void {
$this->result = new EmptyDatabaseResult();
}
public function testNumRows () : void {
$this->assertSame(0, $this->result->numRows());
}
public function testFetchRow () : void {
$this->assertEmpty($this->result->fetchRow());
}
+ public function testFetchScalar () : void {
+ $this->assertNull($this->result->fetchScalar());
+ }
+
public function testGetIterator () : void {
$actual = iterator_to_array($this->result->getIterator());
$this->assertSame([], $actual);
}
}
diff --git a/database/tests/Result/MockDatabaseResultTest.php b/database/tests/Result/MockDatabaseResultTest.php
index 6a2a777..e97b3c9 100644
--- a/database/tests/Result/MockDatabaseResultTest.php
+++ b/database/tests/Result/MockDatabaseResultTest.php
@@ -1,40 +1,44 @@
<?php
namespace Keruald\Database\Tests\Result;
use Keruald\Database\Result\MockDatabaseResult;
use PHPUnit\Framework\TestCase;
class MockDatabaseResultTest extends TestCase {
const RESULT = [
[ "name" => "strawberry", "color" => "red" ],
[ "name" => "blueberry", "color" => "violet" ],
];
private MockDatabaseResult $result;
protected function setUp () : void {
$this->result = new MockDatabaseResult(self::RESULT);
}
public function testFetchRow () {
$this->assertEquals(
[ "name" => "strawberry", "color" => "red" ],
$this->result->fetchRow(),
);
}
+ public function testFetchScalar () {
+ $this->assertEquals("strawberry", $this->result->fetchScalar());
+ }
+
public function testNumRows () {
$this->assertEquals(2, $this->result->numRows());
}
public function testGetIterator () {
$i = 0;
foreach ($this->result as $row) {
$this->assertEquals(self::RESULT[$i], $row);
$i++;
}
}
}
diff --git a/database/tests/Result/MySQLiDatabaseResultTest.php b/database/tests/Result/MySQLiDatabaseResultTest.php
index 1365e0c..a96ef94 100644
--- a/database/tests/Result/MySQLiDatabaseResultTest.php
+++ b/database/tests/Result/MySQLiDatabaseResultTest.php
@@ -1,55 +1,59 @@
<?php
namespace Keruald\Database\Tests\Result;
use Keruald\Database\Engines\MySQLiEngine;
use Keruald\Database\Result\MySQLiDatabaseResult;
use PHPUnit\Framework\TestCase;
class MySQLiDatabaseResultTest extends TestCase {
private MySQLiDatabaseResult $result;
protected function setUp () : void {
$db = new MySQLiEngine('localhost', '', '', 'test_keruald_db');
$db->setFetchMode(MYSQLI_BOTH);
$sql = "SELECT id, name, category FROM ships";
$this->result = $db->query($sql);
}
public function provideExpectedData () : array {
$data = [
// MYSQLI_NUM data
["1", "So Much For Subtlety", "GSV"],
["2", "Unfortunate Conflict Of Evidence", "GSV"],
["3", "Just Read The Instructions", "GCU"],
["4", "Just Another Victim Of The Ambient Morality", "GCU"],
];
return array_map(function ($row) {
// MYSQLI_ASSOC data
return $row + [
"id" => $row[0],
"name" => $row[1],
"category" => $row[2],
];
}, $data);
}
public function testGetIterator () {
$actual = iterator_to_array($this->result->getIterator());
$this->assertEquals($this->provideExpectedData(), $actual);
}
public function testFetchRow () {
$expected = $this->provideExpectedData()[0];
$this->assertEquals($expected, $this->result->fetchRow());
}
+ public function testFetchScalar () {
+ $this->assertEquals("1", $this->result->fetchScalar());
+ }
+
public function testNumRows () {
$this->assertEquals(4, $this->result->numRows());
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sat, Mar 21, 04:43 (14 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3546421
Default Alt Text
(5 KB)

Event Timeline