From 8e91c3a41858de38d00e6db02872d32504e93b04 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 31 Jan 2020 22:01:56 -0500 Subject: [PATCH] - Bugfixes done on MySQL adapter caused by some changes while adding Mssql. - WIP on Repository and QueryBuilder --- src/Adapter/MariaDB.php | 4 +--- src/Adapter/MySQL.php | 20 ++++++++------------ src/Annotation/Property/Field/Id.php | 3 ++- src/Common/PdoObject.php | 4 ++-- src/Exception/EmptyDatasetException.php | 5 +++++ src/QueryBuilder.php | 2 +- src/Repository.php | 15 +++++++++++++-- 7 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 src/Exception/EmptyDatasetException.php diff --git a/src/Adapter/MariaDB.php b/src/Adapter/MariaDB.php index 5d8495b..2875b39 100644 --- a/src/Adapter/MariaDB.php +++ b/src/Adapter/MariaDB.php @@ -2,6 +2,4 @@ namespace Ulmus\Adapter; -class MariaDB extends MySQL { - -} +class MariaDB extends MySQL {} diff --git a/src/Adapter/MySQL.php b/src/Adapter/MySQL.php index fe857e6..926a482 100644 --- a/src/Adapter/MySQL.php +++ b/src/Adapter/MySQL.php @@ -16,11 +16,11 @@ class MySQL implements AdapterInterface { public string $password; - public string $charset; + public string $charset = "utf8mb4"; - public string $unixSocket; + public ? string $socket; - public int $port; + public int $port = 3306; public function __construct( ?string $hostname = null, @@ -74,8 +74,8 @@ class MySQL implements AdapterInterface { public function buildDataSourceName() : string { - if ( false !== ($this->unixSocket ?? false) ) { - + if ( false !== ($this->socket ?? false) ) { + $parts[] = "unix_socket={$this->socket}"; } else { $parts[] = "host={$this->hostname}"; @@ -87,10 +87,6 @@ class MySQL implements AdapterInterface { $parts[] = "dbname={$this->database}"; - if ( $this->socket ?? false ) { - $parts[] = "socket={$this->socket}"; - } - if ( $this->charset ?? false ) { $parts[] = "charset={$this->charset}"; } @@ -100,11 +96,11 @@ class MySQL implements AdapterInterface { public function setup(array $configuration) : void { - $configuration = array_change_key_case($configuration, \CASE_LOWER); + $connection = array_change_key_case($configuration, \CASE_LOWER); # Either Unix Socket is used, or Host / Port - if ( false !== ( $this->unixSocket = $connection['unix_socket'] ?? false ) ) { - if ( false === ( $this->hostname = $connection['host'] ?? false ) ) { + if ( null === ( $this->socket = $connection['socket'] ?? null ) ) { + if ( "" === ( $this->hostname = $connection['host'] ?? "" ) ) { throw new AdapterConfigurationException("Your `host` name is missing from your configuration array"); } diff --git a/src/Annotation/Property/Field/Id.php b/src/Annotation/Property/Field/Id.php index e7f51d2..029277a 100644 --- a/src/Annotation/Property/Field/Id.php +++ b/src/Annotation/Property/Field/Id.php @@ -7,9 +7,10 @@ class Id extends \Ulmus\Annotation\Property\Field { public function __construct() { $this->nullable = false; - $this->type = "int"; $this->attributes['unsigned'] = true; $this->attributes['primary_key'] = true; + + parent::__construct('int'); } } diff --git a/src/Common/PdoObject.php b/src/Common/PdoObject.php index 306e933..f564f70 100644 --- a/src/Common/PdoObject.php +++ b/src/Common/PdoObject.php @@ -8,8 +8,8 @@ class PdoObject extends PDO { public function select(string $sql, array $parameters = []) : PDOStatement { - #dump($sql, $parameters); - +# var_dump($sql, $parameters); + # die(); try { if ( false !== ( $statement = $this->prepare($sql) ) ) { $statement = $this->execute($statement, $parameters, false); diff --git a/src/Exception/EmptyDatasetException.php b/src/Exception/EmptyDatasetException.php new file mode 100644 index 0000000..37e8b8a --- /dev/null +++ b/src/Exception/EmptyDatasetException.php @@ -0,0 +1,5 @@ +entityClass = $entity; $this->alias = $alias; - $this->adapter = $adapter; - $this->queryBuilder = new QueryBuilder( $adapter->adapter() ); + $this->adapter = $adapter ?: Ulmus::$defaultAdapter; + $this->queryBuilder = new QueryBuilder( $this->adapter->adapter() ); $this->entityResolver = Ulmus::resolveEntity($entity); } @@ -45,6 +45,17 @@ class Repository { return $this->where($field, $value)->collectionFromQuery(); } + + public function count() : int + { + $this->select("count(*) as totalItem"); + + foreach(Ulmus::iterateQueryBuilder($this->selectSqlQuery()->queryBuilder) as $entityData) { + return $entityData['totalItem']; + } + + return 0; + } public function deleteOne() {