From e873f909fda84b37e6bd9dec9f2f0b45fbd3cb5e Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 4 Oct 2023 23:57:30 +0000 Subject: [PATCH] add exists functionality (similar to count but just checks existence) --- .../Database/Mapper/DataMapperFactory.php | 14 ++++++++ DataStorage/Database/Mapper/MapperType.php | 2 ++ DataStorage/Database/Mapper/ReadMapper.php | 32 ++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/DataStorage/Database/Mapper/DataMapperFactory.php b/DataStorage/Database/Mapper/DataMapperFactory.php index 18988b8bd..8eb0bd878 100755 --- a/DataStorage/Database/Mapper/DataMapperFactory.php +++ b/DataStorage/Database/Mapper/DataMapperFactory.php @@ -254,6 +254,20 @@ class DataMapperFactory return (new ReadMapper(new static(), $db ?? self::$db))->count(); } + /** + * Create read mapper + * + * @param ConnectionAbstract $db Database connection + * + * @return ReadMapper + * + * @since 1.0.0 + */ + public static function exists(ConnectionAbstract $db = null) : ReadMapper + { + return (new ReadMapper(new static(), $db ?? self::$db))->exists(); + } + /** * Create read mapper * diff --git a/DataStorage/Database/Mapper/MapperType.php b/DataStorage/Database/Mapper/MapperType.php index 90ab70552..b25f24598 100755 --- a/DataStorage/Database/Mapper/MapperType.php +++ b/DataStorage/Database/Mapper/MapperType.php @@ -38,6 +38,8 @@ abstract class MapperType extends Enum public const COUNT_MODELS = 12; + public const MODEL_EXISTS = 13; + // -------------------------------------------- // public const CREATE = 1001; diff --git a/DataStorage/Database/Mapper/ReadMapper.php b/DataStorage/Database/Mapper/ReadMapper.php index e67844987..39b2a604c 100755 --- a/DataStorage/Database/Mapper/ReadMapper.php +++ b/DataStorage/Database/Mapper/ReadMapper.php @@ -102,6 +102,20 @@ final class ReadMapper extends DataMapperAbstract return $this; } + /** + * Create count mapper + * + * @return self + * + * @since 1.0.0 + */ + public function exists() : self + { + $this->type = MapperType::MODEL_EXISTS; + + return $this; + } + /** * Create random mapper * @@ -158,6 +172,8 @@ final class ReadMapper extends DataMapperAbstract return $this->executeGetRaw(); case MapperType::COUNT_MODELS: return $this->executeCount(); + case MapperType::MODEL_EXISTS: + return $this->executeExists(); default: return null; } @@ -227,7 +243,7 @@ final class ReadMapper extends DataMapperAbstract try { $results = false; - $sth = $this->db->con->prepare($a = $query->toSql()); + $sth = $this->db->con->prepare($query->toSql()); if ($sth !== false) { $sth->execute(); $results = $sth->fetchAll(\PDO::FETCH_ASSOC); @@ -285,6 +301,20 @@ final class ReadMapper extends DataMapperAbstract return (int) $query->execute()?->fetchColumn(); } + /** + * Check if any element exists + * + * @return int + * + * @since 1.0.0 + */ + public function executeExists() : bool + { + $query = $this->getQuery(null, ['1']); + + return ($query->execute()?->fetchColumn() ?? 0) > 0; + } + /** * Get random object *