Added ~OrNull methods to IDbResult.

This commit is contained in:
Pachira 2023-11-09 13:52:12 +00:00
parent 96700f7044
commit 987ce5ce52
6 changed files with 75 additions and 25 deletions

View file

@ -1 +1 @@
0.2311.91336
0.2311.91351

View file

@ -0,0 +1,32 @@
<?php
// DbResultTrait.php
// Created: 2023-11-09
// Updated: 2023-11-09
namespace Index\Data;
trait DbResultTrait {
public function getString(int|string $index): string {
return (string)$this->getValue($index);
}
public function getStringOrNull(int|string $index): ?string {
return $this->isNull($index) ? null : (string)$this->getValue($index);
}
public function getInteger(int|string $index): int {
return (int)$this->getValue($index);
}
public function getIntegerOrNull(int|string $index): ?int {
return $this->isNull($index) ? null : (int)$this->getValue($index);
}
public function getFloat(int|string $index): float {
return (float)$this->getValue($index);
}
public function getFloatOrNull(int|string $index): ?float {
return $this->isNull($index) ? null : (float)$this->getValue($index);
}
}

View file

@ -43,6 +43,14 @@ interface IDbResult extends ICloseable {
*/
function getString(int|string $index): string;
/**
* Checks if the field is null, then gets the value from the target index cast as a native string.
*
* @param int|string $index Target index.
* @return ?string Returns a string of the value or null.
*/
function getStringOrNull(int|string $index): ?string;
/**
* Gets the value from the target index cast as an integer.
*
@ -51,6 +59,14 @@ interface IDbResult extends ICloseable {
*/
function getInteger(int|string $index): int;
/**
* Checks if the field is null, then gets the value from the target index cast as an integer.
*
* @param int|string $index Target index.
* @return int Returns the value cast to an integer or null.
*/
function getIntegerOrNull(int|string $index): ?int;
/**
* Gets the value from the target index cast as a floating point number.
*
@ -59,6 +75,14 @@ interface IDbResult extends ICloseable {
*/
function getFloat(int|string $index): float;
/**
* Checks if the field is null, then gets the value from the target index cast as a floating point number.
*
* @param int|string $index Target index.
* @return float Returns the value cast to a floating point number or null.
*/
function getFloatOrNull(int|string $index): ?float;
/**
* Gets the value from the target index as a Stream.
*

View file

@ -8,6 +8,7 @@ namespace Index\Data\MariaDB;
use mysqli_result;
use mysqli_stmt;
use InvalidArgumentException;
use Index\Data\DbResultTrait;
use Index\Data\IDbResult;
use Index\IO\Stream;
use Index\IO\TempFileStream;
@ -16,6 +17,8 @@ use Index\IO\TempFileStream;
* Represents a MariaDB/MySQL database result.
*/
abstract class MariaDBResult implements IDbResult {
use DbResultTrait;
protected mysqli_stmt|mysqli_result $result;
protected array $currentRow = [];
@ -67,18 +70,6 @@ abstract class MariaDBResult implements IDbResult {
return $this->currentRow[$index] ?? null;
}
public function getString(int|string $index): string {
return (string)$this->getValue($index);
}
public function getInteger(int|string $index): int {
return (int)$this->getValue($index);
}
public function getFloat(int|string $index): float {
return (float)$this->getValue($index);
}
public function getStream(int|string $index): ?Stream {
if($this->isNull($index))
return null;

View file

@ -28,14 +28,26 @@ class NullDbResult implements IDbResult {
return '';
}
public function getStringOrNull(int|string $index): ?string {
return null;
}
public function getInteger(int|string $index): int {
return 0;
}
public function getIntegerOrNull(int|string $index): ?int {
return null;
}
public function getFloat(int|string $index): float {
return 0.0;
}
public function getFloatOrNull(int|string $index): ?float {
return null;
}
public function getStream(int|string $index): ?Stream {
return null;
}

View file

@ -7,6 +7,7 @@ namespace Index\Data\SQLite;
use SQLite3Result;
use InvalidArgumentException;
use Index\Data\DbResultTrait;
use Index\Data\IDbResult;
use Index\IO\Stream;
use Index\IO\TempFileStream;
@ -15,6 +16,8 @@ use Index\IO\TempFileStream;
* Represents an SQLite result set.
*/
class SQLiteResult implements IDbResult {
use DbResultTrait;
private SQLite3Result $result;
private array $currentRow = [];
@ -53,18 +56,6 @@ class SQLiteResult implements IDbResult {
return $this->currentRow[$index] ?? null;
}
public function getString(int|string $index): string {
return (string)$this->getValue($index);
}
public function getInteger(int|string $index): int {
return (int)$this->getValue($index);
}
public function getFloat(int|string $index): float {
return (float)$this->getValue($index);
}
/**
* Gets the value from the target index as a Stream.
* If you're aware that you're using SQLite it may make more sense to use SQLiteConnection::getBlobStream instead.