From d2bd6c2f588ffd8a41ed082e5725536558ba6c6a Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 31 Oct 2023 16:34:45 +0000 Subject: [PATCH 1/5] - minor fixes --- src/Attribute/Property/Field/ForeignKey.php | 2 +- src/EntityTrait.php | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Attribute/Property/Field/ForeignKey.php b/src/Attribute/Property/Field/ForeignKey.php index 11970a9..0c5deeb 100644 --- a/src/Attribute/Property/Field/ForeignKey.php +++ b/src/Attribute/Property/Field/ForeignKey.php @@ -13,7 +13,7 @@ use Ulmus\Attribute\ConstrainActionEnum; class ForeignKey extends PrimaryKey { public function __construct( public ? string $name = null, - public ? string $type = 'bigint', + public ? string $type = null, public null|int|string $length = null, public ? int $precision = null, public array $attributes = [ diff --git a/src/EntityTrait.php b/src/EntityTrait.php index 26a8723..26a2d92 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -112,7 +112,7 @@ trait EntityTrait { $this->entityLoadedDataset = array_change_key_case($dataset, \CASE_LOWER); } elseif ($overwriteDataset) { - $this->entityLoadedDataset = array_change_key_case($dataset, \CASE_LOWER) + $this->entityLoadedDataset; + $this->entityLoadedDataset = iterator_to_array(array_change_key_case($dataset, \CASE_LOWER)) + $this->entityLoadedDataset; } } @@ -261,10 +261,6 @@ trait EntityTrait { #[Ignore] public function __clone() { - foreach($this as $prop) { - - } - if ( null !== $pkField = $this->resolveEntity()->getPrimaryKeyField($this) ) { $key = key($pkField); From 667c1fe9abf12751ab1f55094d69d9043ee80e33 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 31 Oct 2023 18:04:49 +0000 Subject: [PATCH 2/5] - Fixed an alias problem within deleteFromPk() method --- src/EntityTrait.php | 7 +++++-- src/Repository.php | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/EntityTrait.php b/src/EntityTrait.php index 26a8723..ae86450 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -306,9 +306,12 @@ trait EntityTrait { } #[Ignore] - public static function field($name, null|string|bool $alias = Repository::DEFAULT_ALIAS) : EntityField + public static function field($name, null|string|false $alias = Repository::DEFAULT_ALIAS) : EntityField { - return new EntityField(static::class, $name, $alias ? Ulmus::repository(static::class)->adapter->adapter()->escapeIdentifier($alias, Adapter\AdapterInterface::IDENTIFIER_FIELD) : ( $alias === false ? '' : Repository::DEFAULT_ALIAS ), Ulmus::resolveEntity(static::class)); + + $default = ( $alias === false ? '' : Repository::DEFAULT_ALIAS ); # bw compatibility, to be deprecated + + return new EntityField(static::class, $name, $alias ? Ulmus::repository(static::class)->adapter->adapter()->escapeIdentifier($alias, Adapter\AdapterInterface::IDENTIFIER_FIELD) : $default, Ulmus::resolveEntity(static::class)); } #[Ignore] diff --git a/src/Repository.php b/src/Repository.php index 9fc7f2b..1dcb8b4 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -114,7 +114,7 @@ class Repository throw new Exception\EntityPrimaryKeyUnknown("A primary key value has to be defined to delete an item."); } - return (bool) $this->wherePrimaryKey($value)->deleteOne()->rowCount; + return (bool) $this->wherePrimaryKey($value, null)->deleteOne()->rowCount; } public function destroy(object $entity) : bool @@ -599,7 +599,7 @@ class Repository return $this; } - public function wherePrimaryKey(mixed $value) : self + public function wherePrimaryKey(mixed $value, null|string|bool $alias = self::DEFAULT_ALIAS) : self { if ( null === $primaryKeyField = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField() ) { throw new Exception\EntityPrimaryKeyUnknown("Entity has no field containing attributes 'primary_key'"); @@ -607,7 +607,7 @@ class Repository $pkField = key($primaryKeyField); - return $this->where($this->entityClass::field($primaryKeyField[$pkField]->name ?? $pkField), $value); + return $this->where($this->entityClass::field($primaryKeyField[$pkField]->name ?? $pkField, false), $value); } public function withJoin(string|array $fields, array $options = []) : self From fed7d2e302c51aec31eee4a4380d0c244ad1a256 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 1 Nov 2023 09:55:19 -0400 Subject: [PATCH 3/5] - Some minors adjustments to match PHP 8.x coding styles --- src/EntityTrait.php | 10 ++++------ src/Query/Where.php | 8 ++++---- src/QueryBuilder.php | 6 +++--- src/Repository/RelationBuilder.php | 18 ++++++++++++------ 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/EntityTrait.php b/src/EntityTrait.php index b21efaf..fae60c8 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -62,7 +62,7 @@ trait EntityTrait { $data = json_decode($value, true); if (json_last_error() !== \JSON_ERROR_NONE) { - throw new \Exception(sprintf("JSON error while decoding in EntityTrait : '%s' given %s with field %s", json_last_error_msg(), $value, json_encode($field))); + throw new \Exception(sprintf("JSON error while decoding in EntityTrait : '%s' given %s", json_last_error_msg(), $value)); } $this->{$field['name']} = $data; @@ -123,11 +123,9 @@ trait EntityTrait { public function resetVirtualProperties() : self { foreach($this->resolveEntity()->properties as $prop => $property) { - if ( empty($property['builtin']) ) { - foreach($property['tags'] as $tag) { - if ( in_array(strtolower($tag['tag']), [ 'relation', 'join', 'virtual' ] ) ) { - unset($this->$prop); - } + foreach($property['tags'] as $tag) { + if ( in_array(strtolower($tag['tag']), [ 'relation', 'join', 'virtual' ] ) ) { + unset($this->$prop); } } } diff --git a/src/Query/Where.php b/src/Query/Where.php index 6f7e0ec..fd37cc6 100644 --- a/src/Query/Where.php +++ b/src/Query/Where.php @@ -16,7 +16,7 @@ class Where extends Fragment { const COMPARISON_IS = "IS"; const COMPARISON_NULL = "NULL"; - const SQL_TOKEN = "WHERE"; + const SQL_TOKEN = "WHERE"; public int $order = 50; @@ -35,7 +35,7 @@ class Where extends Fragment { $this->parent = $queryBuilder->where ?? null; } - public function add($field, $value, string $operator, string $condition, bool $not = false) : self + public function add($field, mixed $value, string $operator, string $condition, bool $not = false) : self { $this->validateFieldType($field); # $this->validateValueType($value); @@ -73,7 +73,7 @@ class Where extends Fragment { ]); } - protected function whereCondition($field, $value, string $operator = self::OPERATOR_EQUAL, string $condition = self::CONDITION_AND, bool $not = false) { + protected function whereCondition($field, mixed $value, string $operator = self::OPERATOR_EQUAL, string $condition = self::CONDITION_AND, bool $not = false) { return new class($this->queryBuilder, $field, $value, $operator, $condition, $not) { public mixed $value; @@ -123,7 +123,7 @@ class Where extends Fragment { $stack = []; if ($this->value) { - foreach ($this->value as $item) { + foreach($this->value as $item) { $stack[] = $this->filterValue($item); } } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 1f43f81..f91e351 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -198,9 +198,9 @@ class QueryBuilder implements Query\QueryBuilderInterface public function where(string|\Stringable $field, mixed $value, string $operator = Query\Where::OPERATOR_EQUAL, string $condition = Query\Where::CONDITION_AND, bool $not = false) : self { # Empty IN case - #if ( [] === $value ) { - # return $this; - #} + # if ( [] === $value ) { + # return $this; + # } if ( $this->where ?? false ) { $where = $this->where; diff --git a/src/Repository/RelationBuilder.php b/src/Repository/RelationBuilder.php index a9cdc39..796da0d 100644 --- a/src/Repository/RelationBuilder.php +++ b/src/Repository/RelationBuilder.php @@ -47,7 +47,7 @@ class RelationBuilder } } - public function searchRelation(string $name) : object|bool + public function searchRelation(string $name) : mixed { # Resolve relations here if one is called if ( $this->entity->isLoaded() ) { @@ -55,7 +55,13 @@ class RelationBuilder return $dataset; } - return $this->resolveRelation($name) ?: $this->resolveVirtual($name) ?: false; + if ( false !== $value = $this->resolveRelation($name) ) { + return $value; + } + elseif ( false !== $value = $this->resolveVirtual($name) ) { + return $value; + } + } else { if ( $relation = $this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class , Relation::class ] ) ) { @@ -69,7 +75,7 @@ class RelationBuilder return false; } - protected function resolveVirtual(string $name) : bool|object + protected function resolveVirtual(string $name) : mixed { if (null !== ($virtual = $this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Virtual::class, Annotation\Property\Virtual::class ]))) { if ($virtual->closure ?? false) { @@ -82,7 +88,7 @@ class RelationBuilder return false; } - protected function resolveRelation(string $name) : bool|object + protected function resolveRelation(string $name) : mixed { if ( null !== ( $relation = $this->resolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class, Relation::class ] ) ) ) { $this->orders = $this->resolver->searchFieldAnnotationList($name, [ Attribute\Property\OrderBy::class, OrderBy::class ] ); @@ -109,14 +115,14 @@ class RelationBuilder $this->entity->eventExecute(Event\EntityRelationLoadInterface::class, $name, $this->repository); - return call_user_func([ $this->repository, $relation->function() ]); +return call_user_func([ $this->repository, $relation->function() ]); case $relation->isManyToMany(): $this->manyToMany($name, $relation, $relationRelation); $this->entity->eventExecute(Event\EntityRelationLoadInterface::class, $name, $this->repository); - $results = call_user_func([ $this->repository, 'loadAll' ]); + $results = call_user_func([ $this->repository, $relationRelation->function() ]); if ($relation->bridgeField ?? false) { $collection = $relation->bridge::entityCollection(); From d297fd9e86552d002087a85a1c5765098b128677 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 1 Nov 2023 11:25:59 -0400 Subject: [PATCH 4/5] - Fixed a bug which caused a problem using offset without limits --- src/QueryBuilder.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 1f43f81..7eae0e4 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -259,6 +259,11 @@ class QueryBuilder implements Query\QueryBuilderInterface if ( null === $offset = $this->getFragment(Query\Offset::class) ) { $offset = new Query\Offset(); $this->push($offset); + + # A limit is required to match an offset + if ( null === $limit = $this->getFragment(Query\Limit::class) ) { + $this->limit(\PHP_INT_MAX); + } } $offset->set($value); From 07ae8faaacf93fdc29ddf5bf98af136837083552 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 3 Nov 2023 08:25:35 -0400 Subject: [PATCH 5/5] - Worked on SQLite adapter mostly --- src/Adapter/SQLite.php | 8 ++++++++ src/Adapter/SqlAdapterTrait.php | 2 +- src/EntityCollection.php | 2 +- src/EntityTrait.php | 13 ++++++++----- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Adapter/SQLite.php b/src/Adapter/SQLite.php index 3cae7fb..66d441e 100644 --- a/src/Adapter/SQLite.php +++ b/src/Adapter/SQLite.php @@ -163,6 +163,14 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface $pdo->sqliteCreateFunction('length', fn($string) => strlen($string), 1); $pdo->sqliteCreateFunction('lcase', fn($string) => strtolower($string), 1); $pdo->sqliteCreateFunction('ucase', fn($string) => strtoupper($string), 1); + $pdo->sqliteCreateFunction('md5', fn($string) => md5($string), 1); + $pdo->sqliteCreateFunction('sha1', fn($string) => sha1($string), 1); + $pdo->sqliteCreateFunction('sha256', fn($string) => hash('sha256', $string), 1); + $pdo->sqliteCreateFunction('sha512', fn($string) => hash('sha512', $string), 1); + $pdo->sqliteCreateFunction('whirlpool', fn($string) => hash('whirlpool', $string), 1); + $pdo->sqliteCreateFunction('murmur3a', fn($string) => hash('murmur3a', $string), 1); + $pdo->sqliteCreateFunction('murmur3c', fn($string) => hash('murmur3c', $string), 1); + $pdo->sqliteCreateFunction('murmur3f', fn($string) => hash('murmur3f', $string), 1); $pdo->sqliteCreateFunction('left', fn($string, $length) => substr($string, 0, $length), 2); $pdo->sqliteCreateFunction('right', fn($string, $length) => substr($string, -$length), 2); $pdo->sqliteCreateFunction('strcmp', fn($s1, $s2) => strcmp($s1, $s2), 2); diff --git a/src/Adapter/SqlAdapterTrait.php b/src/Adapter/SqlAdapterTrait.php index 6f73d8e..a1860de 100644 --- a/src/Adapter/SqlAdapterTrait.php +++ b/src/Adapter/SqlAdapterTrait.php @@ -134,7 +134,7 @@ trait SqlAdapterTrait case is_object($value): return Ulmus::convertObject($value); - case is_array($value): + case is_array($value): return json_encode($value); case is_bool($value): diff --git a/src/EntityCollection.php b/src/EntityCollection.php index fb412cc..30b1858 100644 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -360,7 +360,7 @@ class EntityCollection extends \ArrayObject implements \JsonSerializable { public function jsonSerialize(): mixed { - return $this->toArray(); + return $this->toArray(true); } public function append($value) : void diff --git a/src/EntityTrait.php b/src/EntityTrait.php index 26a8723..8dfa1bd 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -141,7 +141,7 @@ trait EntityTrait { return $this->entityFillFromDataset($dataset); } - public function entityGetDataset(bool $includeRelations = false, bool $returnSource = false) : array + public function entityGetDataset(bool $includeRelations = false, bool $returnSource = false, bool $rewriteValue = true) : array { if ( $returnSource ) { return $this->entityLoadedDataset; @@ -155,7 +155,10 @@ trait EntityTrait { $annotation = $entityResolver->searchFieldAnnotation($key, [ Attribute\Property\Field::class, Field::class ]); if ( isset($this->$key) ) { - $dataset[$annotation->name ?? $key] = static::repository()->adapter->adapter()->writableValue($this->$key); + $dataset[$annotation->name ?? $key] = $rewriteValue? + static::repository()->adapter->adapter()->writableValue($this->$key) + : + $this->$key; } elseif ( $field['nullable'] ) { $dataset[$annotation->name ?? $key] = null; @@ -165,7 +168,7 @@ trait EntityTrait { # @TODO Must fix recursive bug ! if ($includeRelations) { foreach($entityResolver->properties as $name => $field){ - $relation = $entityResolver->searchFieldAnnotation($key, [ Attribute\Property\Relation::class. Relation::class ] ); + $relation = $entityResolver->searchFieldAnnotation($name, [ Attribute\Property\Relation::class, Relation::class ] ); if ( $relation && isset($this->$name) && ($relation->entity ?? $relation->bridge) !== static::class ) { if ( null !== $value = $this->$name ?? null ) { @@ -192,7 +195,7 @@ trait EntityTrait { #[Ignore] public function toArray($includeRelations = false, array $filterFields = null) : array { - $dataset = $this->entityGetDataset($includeRelations); + $dataset = $this->entityGetDataset($includeRelations, false, false); return $filterFields ? array_intersect_key($dataset, array_flip($filterFields)) : $dataset; } @@ -275,7 +278,7 @@ trait EntityTrait { #[Ignore] public function jsonSerialize() : mixed { - return $this->entityGetDataset(); + return $this->entityGetDataset(true, false, false); } #[Ignore]