From 987ce5ce52862c2b417f79fe76255fa52a65039d Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 9 Nov 2023 13:52:12 +0000 Subject: [PATCH] Added ~OrNull methods to IDbResult. --- VERSION | 2 +- src/Data/DbResultTrait.php | 32 ++++++++++++++++++++++++++++++ src/Data/IDbResult.php | 24 ++++++++++++++++++++++ src/Data/MariaDB/MariaDBResult.php | 15 +++----------- src/Data/NullDb/NullDbResult.php | 12 +++++++++++ src/Data/SQLite/SQLiteResult.php | 15 +++----------- 6 files changed, 75 insertions(+), 25 deletions(-) create mode 100644 src/Data/DbResultTrait.php diff --git a/VERSION b/VERSION index c35fa3f..b6c00dc 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2311.91336 +0.2311.91351 diff --git a/src/Data/DbResultTrait.php b/src/Data/DbResultTrait.php new file mode 100644 index 0000000..faa2dd5 --- /dev/null +++ b/src/Data/DbResultTrait.php @@ -0,0 +1,32 @@ +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); + } +} diff --git a/src/Data/IDbResult.php b/src/Data/IDbResult.php index 219adf6..2a6dcf2 100644 --- a/src/Data/IDbResult.php +++ b/src/Data/IDbResult.php @@ -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. * diff --git a/src/Data/MariaDB/MariaDBResult.php b/src/Data/MariaDB/MariaDBResult.php index b1fa6b6..f7001c5 100644 --- a/src/Data/MariaDB/MariaDBResult.php +++ b/src/Data/MariaDB/MariaDBResult.php @@ -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; diff --git a/src/Data/NullDb/NullDbResult.php b/src/Data/NullDb/NullDbResult.php index 01733be..fa36cd1 100644 --- a/src/Data/NullDb/NullDbResult.php +++ b/src/Data/NullDb/NullDbResult.php @@ -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; } diff --git a/src/Data/SQLite/SQLiteResult.php b/src/Data/SQLite/SQLiteResult.php index 92b4011..3fba230 100644 --- a/src/Data/SQLite/SQLiteResult.php +++ b/src/Data/SQLite/SQLiteResult.php @@ -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.