Compare commits

..

No commits in common. "f74d1907d94facc29ba227044e9ec9aad80a8431" and "bcbf65e0b3b4ee9bb7139bf8634f3c20275bbb68" have entirely different histories.

9 changed files with 48 additions and 212 deletions

View File

@ -7,15 +7,17 @@ class Field implements \Ulmus\Annotation\Annotation {
public string $type;
public string $name;
# public string $column;
public int $length;
public int $precision;
public array $attributes = [];
public bool $nullable = false;
public bool $nullable;
public function __construct(? string $type = null, ? int $length = null)
{
if ( $type !== null ) {

View File

@ -1,7 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class Virtual extends Field {
public Closure $closure;
}

View File

@ -6,7 +6,6 @@ use Ulmus\Ulmus,
Ulmus\Annotation\Annotation,
Ulmus\Annotation\Classes\Table,
Ulmus\Annotation\Property\Field,
Ulmus\Annotation\Property\Virtual,
Ulmus\Annotation\Property\Relation;
class EntityResolver {
@ -52,17 +51,13 @@ class EntityResolver {
return null;
}
public function fieldList($fieldKey = self::KEY_ENTITY_NAME, bool $skipVirtual = false) : array
public function fieldList($fieldKey = self::KEY_ENTITY_NAME) : array
{
$fieldList = [];
foreach($this->properties as $item) {
foreach($item['tags'] ?? [] as $tag) {
if ( $tag['object'] instanceof Field ) {
if ( $skipVirtual && ($tag['object'] instanceof Virtual )) {
break;
}
switch($fieldKey) {
case static::KEY_ENTITY_NAME:
$key = $item['name'];

View File

@ -6,13 +6,10 @@ use PDO,
PDOStatement;
class PdoObject extends PDO {
public static ? string $dump = null;
public function select(string $sql, array $parameters = []): PDOStatement
{
static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]);
//dump($sql, $parameters);
try {
if (false !== ( $statement = $this->prepare($sql) )) {
$statement = $this->execute($statement, $parameters, false);
@ -28,8 +25,6 @@ class PdoObject extends PDO {
public function runQuery(string $sql, array $parameters = []): ? PDOStatement
{
static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]);
try {
if (false !== ( $statement = $this->prepare($sql) )) {
return $this->execute($statement, $parameters, true);
@ -68,9 +63,7 @@ class PdoObject extends PDO {
throw $e;
}
catch (\Throwable $e) {
if ( function_exists("debogueur") ) {
debogueur($statement, $parameters, $commit);
}
debogueur($statement, $parameters, $commit);
throw $e;
}

View File

@ -8,7 +8,7 @@ class EntityCollection extends \ArrayObject {
public ? string $entityClass = null;
public function filters(Callable $callback, bool $yieldValueOnly = false) : Generator
public function filters(Callable $callback) : Generator
{
$idx = 0;
@ -16,41 +16,31 @@ class EntityCollection extends \ArrayObject {
if ( $callback($item, $key, $idx) ) {
$idx++;
if ( $yieldValueOnly ) {
yield $item;
}
else {
yield $key => $item;
}
yield $key => $item;
}
}
}
public function filtersCollection(Callable $callback, bool $yieldValueOnly = false, bool $replaceCollection = false) : self
public function filtersCollection(Callable $callback) : self
{
$collection = new static();
foreach($this->filters($callback, $yieldValueOnly) as $item) {
foreach($this->filters($callback) as $item) {
$collection->append($item);
}
if ($replaceCollection) {
$this->exchangeArray(array_values($collection->getArrayCopy()));
return $this;
}
else {
return $collection;
}
return $collection;
}
public function iterate(Callable $callback) : self
public function iterate(Callable $callback) : array
{
$results = [];
foreach($this as $item) {
$callback($item);
$results[] = $callback($item);
}
return $this;
return $results;
}
public function removeOne($value, string $field, bool $strict = true) : ? object
@ -78,13 +68,6 @@ class EntityCollection extends \ArrayObject {
return $removed;
}
public function search($value, string $field, bool $strict = true) : Generator
{
foreach($this->filters(fn($v) => $strict ? $v->$field === $value : $v->$field == $value) as $key => $item) {
yield $key => $item;
}
}
public function searchOne($value, string $field, bool $strict = true) : ? object
{
# Returning first value only
@ -95,83 +78,24 @@ class EntityCollection extends \ArrayObject {
return null;
}
public function searchAll($value, string $field, bool $strict = true) : self
public function search($value, string $field, bool $strict = true) : Generator
{
$obj = new static();
foreach($this->search($value, $field, $strict) as $item) {
$obj->append($item);
foreach($this->filters(fn($v) => $strict ? $v->$field === $value : $v->$field == $value) as $key => $item) {
yield $key => $item;
}
return $obj;
}
public function column($field, bool $unique = false) : array
public function column($field) : array
{
$list = [];
foreach($this as $item) {
if ( is_callable($field) ) {
$value = call_user_func_array($field, [ $item ]);
}
else {
$value = $item->$field;
}
if ($unique && in_array($value, $list)) {
break;
}
$list[] = $value;
$list[] = $item->$field;
}
return $list;
}
public function unique(/*stringable|callable */ $field, bool $strict = false) : self
{
$list = [];
$obj = new static();
foreach($this as $item) {
if ( $field === null) {
$value = $this;
}
if ( is_callable($field) ) {
$value = call_user_func_array($field, [ $item ]);
}
else {
$value = $item->$field;
}
if ( ! in_array($value, $list, $strict) ) {
$list[] = $value;
$obj->append($item);
}
}
return $obj;
}
public function first() : ? object
{
foreach($this as $item) {
return $item;
}
return null;
}
public function last() : ? object
{
foreach($this as $item) {
$return = $item;
}
return $return ?? null;
}
public function buildArray(string $keyColumn, /* string|callable|null */ $value = null) : array
{
$list = [];
@ -219,7 +143,7 @@ class EntityCollection extends \ArrayObject {
return $list;
}
public function fromArray(array $datasets, ? string /*stringable*/ $entityClass = null) : self
public function fromArray(array $datasets, ? string $entityClass = null) : self
{
if ( ! ($this->entityClass || $entityClass) ) {
throw new \Exception("An entity class name must be provided to be instanciated and populated before insertion into this collection.");
@ -244,31 +168,4 @@ class EntityCollection extends \ArrayObject {
return $this;
}
public function replaceWith( /*array|EntityCollection*/ $datasets ) : self
{
if ( is_object($datasets) ) {
$datasets = $datasets->getArrayCopy();
}
$this->exchangeArray( $datasets );
return $this;
}
public function randomize() : self
{
$arr = $this->getArrayCopy();
shuffle($arr);
$this->exchangeArray($arr);
return $this;
}
public function sort(callable $callback, $function = "uasort") : self
{
call_user_func_array([ $this, $function ], [ $callback ]);
return $this;
}
}

View File

@ -8,7 +8,7 @@ use Ulmus\Repository,
Ulmus\Common\EntityField;
use Ulmus\Annotation\Classes\{ Method, Table, Collation, };
use Ulmus\Annotation\Property\{ Field, Relation, OrderBy, Where, Join, Virtual };
use Ulmus\Annotation\Property\{ Field, Relation, OrderBy, Where, Join };
use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, };
trait EntityTrait {
@ -70,7 +70,7 @@ trait EntityTrait {
$repository = $baseEntity->repository();
foreach($where as $condition) {
$repository->where($condition->field, is_callable($condition->value) ? $f = call_user_func_array($condition->value, [ $this ]) : $condition->value, $condition->operator);
$repository->where($condition->field, $condition->value, $condition->operator);
}
foreach($order as $item) {
@ -81,11 +81,10 @@ trait EntityTrait {
}
switch( $relationType ) {
case 'onetoone':
if ($relation->foreignKey) {
$repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity->field($relation->foreignKey), is_callable($field) ? $field($this) : $this->$field );
}
case 'onetoone':
$repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity->field($relation->foreignKey), $this->$field );
$repository->limit(1);
$this->eventExecute(Event\EntityRelationLoadInterface::class, $name, $repository);
$result = call_user_func([$repository, $relation->function]);
@ -97,9 +96,7 @@ trait EntityTrait {
return $this->$name = $result[0];
case 'onetomany':
if ($relation->foreignKey) {
$repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity->field($relation->foreignKey), is_callable($field) ? $field($this) : $this->$field );
}
$repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity->field($relation->foreignKey), $this->$field );
$this->eventExecute(Event\EntityRelationLoadInterface::class, $name, $repository);
return $this->$name = call_user_func([$repository, $relation->function]);
@ -186,12 +183,12 @@ trait EntityTrait {
/**
* @Ignore
*/
public function entityFillFromDataset(iterable $dataset, bool $isLoadedDataset = false) : self
public function entityFillFromDataset(iterable $dataset) : self
{
$loaded = $this->isLoaded();
$entityResolver = $this->resolveEntity();
foreach($dataset as $key => $value) {
$field = $entityResolver->field(strtolower($key), EntityResolver::KEY_COLUMN_NAME, false) ?? null;
@ -235,7 +232,7 @@ trait EntityTrait {
}
# Keeping original data to diff on UPDATE query
if ( ! $loaded || $isLoadedDataset ) {
if ( ! $loaded ) {
#if ( $field !== null ) {
# $annotation = $entityResolver->searchFieldAnnotation($field['name'], new Field() );
# $this->entityLoadedDataset[$annotation ? $annotation->name : $field['name']] = $dataset; # <--------- THIS TO FIX !!!!!!
@ -283,7 +280,7 @@ trait EntityTrait {
$entityResolver = $this->resolveEntity();
foreach($entityResolver->fieldList(Common\EntityResolver::KEY_ENTITY_NAME, true) as $key => $field) {
foreach($entityResolver->fieldList() as $key => $field) {
$annotation = $entityResolver->searchFieldAnnotation($key, new Field() );
if ( isset($this->$key) ) {
@ -307,6 +304,7 @@ trait EntityTrait {
}
}
# @TODO Must fix recursive bug !
if ($includeRelations) {
foreach($entityResolver->properties as $name => $field){
$relation = $entityResolver->searchFieldAnnotation($name, new Relation() );
@ -317,13 +315,13 @@ trait EntityTrait {
$list = [];
foreach($value as $entity) {
$list[] = $entity->entityGetDataset(false);
$list[] = $entity->entityGetDataset(true);
}
$dataset[$name] = $list;
}
elseif ( is_object($value) ) {
$dataset[$name] = $value->entityGetDataset(false);
$dataset[$name] = $value->entityGetDataset(true);
}
}
}

View File

@ -36,7 +36,7 @@ class Where extends Fragment {
$this->condition = $condition;
$this->parent = $queryBuilder->where ?? null;
}
public function add($field, $value, string $operator, string $condition, bool $not = false) : self
{
$this->conditionList[] = [

View File

@ -29,23 +29,6 @@ class QueryBuilder
protected array $queryStack = [];
public function __clone()
{
if ($this->where ?? false) {
#$this->where = clone $this->where;
#$this->where->queryBuilder = $this;
}
if ($this->having ?? false) {
#$this->having = clone $this->having;
#$this->having->queryBuiler = $this;
}
if ($this->parent ?? false) {
#$this->parent = clone $this->parent;
}
}
public function select($field) : self
{
if ( null !== ( $select = $this->getFragment(Query\Select::class) ) ) {

View File

@ -29,11 +29,6 @@ class Repository
$this->adapter = $adapter ?? $this->entityResolver->databaseAdapter();
$this->queryBuilder = new QueryBuilder();
}
public function __clone()
{
#$this->queryBuilder = clone $this->queryBuilder;
}
public function loadOne() : ? object
{
@ -143,10 +138,10 @@ class Repository
( null !== $primaryKeyDefinition )) {
$pkField = key($primaryKeyDefinition);
$dataset[$pkField] = $statement->lastInsertId;
$entity->$pkField = $statement->lastInsertId;
}
$entity->entityFillFromDataset($dataset, true);
return true;
}
else {
if ( $primaryKeyDefinition === null ) {
@ -160,8 +155,6 @@ class Repository
$update = $this->updateSqlQuery($diff)->runQuery();
$entity->entityFillFromDataset($dataset);
return $update ? (bool) $update->rowCount() : false;
}
}
@ -212,7 +205,7 @@ class Repository
}
}
public function select(/*array|Stringable*/ $fields) : self
public function select($fields) : self
{
$this->queryBuilder->select($fields);
@ -325,14 +318,6 @@ class Repository
return $this;
}
# @UNTESTED
public function randomizeOrder() : self
{
$this->queryBuilder->orderBy(Common\Sql::function('RAND', Sql::identifier('CURDATE()+0')));
return $this;
}
public function orders(array $orderList) : self
{
foreach($orderList as $field => $direction) {
@ -378,7 +363,6 @@ class Repository
return $this->where($primaryKeyField[$pkField]->name ?? $pkField, $value);
}
public function withJoin(/*string|array*/ $fields) : self
{
@ -389,10 +373,9 @@ class Repository
foreach((array) $fields as $item) {
if ( null !== $join = $this->entityResolver->searchFieldAnnotation($item, new Annotation\Property\Join) ) {
$alias = $join->alias ?? $item;
$entity = $join->entity ?? $this->entityResolver->properties[$item]['type'];
foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME) as $key => $field) {
$this->select("$alias.$key as {$alias}\${$field['name']}");
}
@ -409,7 +392,6 @@ class Repository
return $this;
}
public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest) : self
{
$searchRequest->count = $searchRequest->filter( clone $this )
@ -461,14 +443,7 @@ class Repository
return Ulmus::runQuery($this->queryBuilder, $this->adapter);
}
public function resetQuery() : self
{
$this->queryBuilder->reset();
return $this;
}
protected function insertSqlQuery(array $dataset) : self
{
if ( null === $this->queryBuilder->getFragment(Query\Insert::class) ) {
@ -550,10 +525,10 @@ class Repository
return $result;
}
public function instanciateEntityCollection(...$arguments) : EntityCollection
public function instanciateEntityCollection() : EntityCollection
{
return $this->entityClass::entityCollection(...$arguments);
return $this->entityClass::entityCollection();
}
public function instanciateEntity() : object