entityClass = $entity; $this->alias = $alias; $this->entityResolver = Ulmus::resolveEntity($entity); $this->adapter = $adapter ?? $this->entityResolver->databaseAdapter() ?? Ulmus::$defaultAdapter; $this->queryBuilder = new QueryBuilder(); } public function __clone() { #$this->queryBuilder = clone $this->queryBuilder; } public function loadOne() : ? object { return $this->limit(1)->collectionFromQuery()[0] ?? null; } public function loadOneFromField($field, $value) : ? object { return $this->where($field, $value)->loadOne(); } public function loadFromPk($value, /* ? stringable */ $primaryKey = null) : ? object { return $primaryKey ? $this->loadOneFromField($primaryKey, $value) : $this->wherePrimaryKey($value)->loadOne(); } public function loadAll() : EntityCollection { return $this->collectionFromQuery(); } public function loadFromField($field, $value) : EntityCollection { return $this->where($field, $value)->collectionFromQuery(); } public function count() : int { if ( null !== $select = $this->queryBuilder->getFragment(Query\Select::class) ) { $this->queryBuilder->removeFragment($select); } if ( $this->queryBuilder->getFragment(Query\GroupBy::class) ) { $this->select( "DISTINCT COUNT(*) OVER ()" ); } else { $this->select(Common\Sql::function("COUNT", "*")); } $this->selectSqlQuery(); $this->finalizeQuery(); return Ulmus::runSelectQuery($this->queryBuilder, $this->adapter)->fetchColumn(0); } protected function deleteOne() { return $this->limit(1)->deleteSqlQuery()->runQuery(); } protected function deleteAll() { return $this->deleteSqlQuery()->runQuery(); } public function deleteFromPk($value) : bool { if ( $value !== 0 && empty($value) ) { throw new Exception\EntityPrimaryKeyUnknown("A primary key value has to be defined to delete an item."); } return (bool) $this->wherePrimaryKey($value)->deleteOne()->rowCount(); } public function destroy(object $entity) : bool { if ( ! $this->matchEntity($entity) ) { throw new \Exception("Your entity class `" . get_class($entity) . "` cannot match entity type of repository `{$this->entityClass}`"); } $primaryKeyDefinition = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField(); if ( $primaryKeyDefinition === null ) { throw new \Exception(sprintf("No primary key found for entity %s", $this->entityClass)); } else { $pkField = key($primaryKeyDefinition); return $this->deleteFromPk($entity->$pkField); } return false; } public function destroyAll(EntityCollection $collection) : void { foreach($collection as $entity) { $this->destroy($entity); } } public function save(object $entity) : bool { if ( ! $this->matchEntity($entity) ) { throw new \Exception("Your entity class `" . get_class($entity) . "` cannot match entity type of repository `{$this->entityClass}`"); } $dataset = $entity->toArray(); $primaryKeyDefinition = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField(); if ( ! $entity->isLoaded() ) { $statement = $this->insertSqlQuery($dataset)->runQuery(); if ( ( 0 !== $statement->lastInsertId ) && ( null !== $primaryKeyDefinition )) { $pkField = key($primaryKeyDefinition); $dataset[$pkField] = $statement->lastInsertId; } $entity->entityFillFromDataset($dataset, true); return true; } else { if ( $primaryKeyDefinition === null ) { throw new \Exception(sprintf("No primary key found for entity %s", $this->entityClass)); } if ( [] !== $diff = $this->generateDatasetDiff($entity) ) { $pkField = key($primaryKeyDefinition); $pkFieldName = $primaryKeyDefinition[$pkField]->name ?? $pkField; $this->where($pkFieldName, $dataset[$pkFieldName]); $update = $this->updateSqlQuery($diff)->runQuery(); $entity->entityFillFromDataset($dataset); return $update ? (bool) $update->rowCount() : false; } } return false; } public function saveAll(EntityCollection $collection) : void { foreach($collection as $entity) { $this->save($entity); } } public function truncate(? string $table = null, ? string $alias = null, ? string $schema = null) : self { $schema = $schema ?: $this->entityResolver->schemaName(); $this->queryBuilder->truncate($this->escapeTable($table ?: $this->entityResolver->tableName()), $alias ?: $this->alias, $this->escapeDatabase($this->adapter->adapter()->database), $schema ? $this->escapeSchema($schema) : null); $this->finalizeQuery(); $result = Ulmus::runSelectQuery($this->queryBuilder, $this->adapter); return $this; } public function generateDatasetDiff(object $entity) : array { return array_diff_assoc( array_change_key_case($entity->toArray()), array_change_key_case($entity->entityGetDataset(false, true)) ); } public function yield() : \Generator { $class = $this->entityClass; $this->selectSqlQuery(); $this->finalizeQuery(); foreach(Ulmus::iterateQueryBuilder($this->queryBuilder, $this->adapter) as $entityData) { yield ( new $class() )->entityFillFromDataset($entityData); } } public function select(/*array|Stringable*/ $fields) : self { $this->queryBuilder->select($fields); return $this; } public function insert(array $fieldlist, string $table, string $alias, ? string $schema) : self { $this->queryBuilder->insert($fieldlist, $this->escapeTable($table), $alias, $schema); return $this; } public function values(array $dataset) : self { $this->queryBuilder->values($dataset); return $this; } public function update(string $table, string $alias, ? string $schema) : self { $this->queryBuilder->update($this->escapeTable($table), $alias, $schema); return $this; } public function set(array $dataset) : self { $this->queryBuilder->set($dataset); return $this; } public function delete() : self { $this->queryBuilder->delete($this->alias); return $this; } public function from(string $table, ? string $alias, ? string $schema) : self { $this->queryBuilder->from($this->escapeTable($table), $alias, $this->escapeDatabase($this->adapter->adapter()->database), $schema ? $this->escapeSchema($schema) : null); return $this; } public function join(string $type, $table, $field, $value, ? string $alias = null, ? callable $callback = null) : self { $join = $this->queryBuilder->withJoin($type, $this->escapeTable($table), $field, $value, false, $alias); if ( $callback ) { $callback($join); } return $this; } public function outerJoin(string $type, $table, $field, $value, ? string $alias = null, ? callable $callback = null) : self { $join = $this->queryBuilder->withJoin($type, $this->escapeTable($table), $field, $value, true, $alias); if ( $callback ) { $callback($join); } return $this; } public function match() : self { } public function notMatch() : self { } public function between() : self { } public function notBetween() : self { } public function groupBy($field) : self { $this->queryBuilder->groupBy($field); return $this; } public function groups(array $groups) : self { foreach($groups as $field ) { $this->groupBy($field); } return $this; } public function orderBy($field, ? string $direction = null) : self { $this->queryBuilder->orderBy($field, $direction); 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) { $this->orderBy($field, $direction); } return $this; } public function limit(int $value) : self { $this->queryBuilder->limit($value); return $this; } public function offset(int $value) : self { $this->queryBuilder->offset($value); return $this; } /* @TODO */ public function commit() : self { return $this; } /* @TODO */ public function rollback() : self { return $this; } public function wherePrimaryKey($value) : self { if ( null === $primaryKeyField = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField() ) { throw new Exception\EntityPrimaryKeyUnknown("Entity has no field containing attributes 'primary_key'"); } $pkField = key($primaryKeyField); return $this->where($primaryKeyField[$pkField]->name ?? $pkField, $value); } public function withJoin(/*stringable|array*/ $fields) : self { $resolvedEntity = Ulmus::resolveEntity($this->entityClass); if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) { $this->select("{$this->alias}.*"); } foreach((array) $fields as $item) { if ( null !== $join = $resolvedEntity->searchFieldAnnotation($item, new Annotation\Property\Join) ) { $alias = $join->alias ?? $item; $entity = $join->entity ?? $resolvedEntity->properties[$item]['type']; foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) { $this->select("$alias.$key as {$alias}\${$field['name']}"); } $key = is_string($join->key) ? $this->entityClass::field($join->key) : $join->key; $foreignKey = is_string($join->foreignKey) ? $entity::field($join->foreignKey, $alias) : $join->foreignKey; $this->join($join->type, $entity::resolveEntity()->tableName(), $key, $foreignKey, $alias); } else { throw new \Exception("You referenced field `$item` which do not exist or do not contain a valid @Join annotation."); } } return $this; } public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest) : self { $likes = $searchRequest->likes(); $wheres = $searchRequest->wheres(); $groups = $searchRequest->groups(); $searchRequest->filter( $this ) ->wheres($wheres, Query\Where::OPERATOR_EQUAL, Query\Where::CONDITION_AND) ->likes($likes, Query\Where::CONDITION_OR) ->groups($groups); $searchRequest->count = $searchRequest->skipCount ? 0 : (clone $this)->count(); return $searchRequest ->filter($this) ->wheres($wheres, Query\Where::OPERATOR_EQUAL, Query\Where::CONDITION_AND) ->likes($likes, Query\Where::CONDITION_OR) ->groups($groups) ->orders($searchRequest->orders()) ->offset($searchRequest->offset()) ->limit($searchRequest->limit()); } public function collectionFromQuery(? string $entityClass = null) : EntityCollection { $class = $entityClass ?: $this->entityClass; $entityCollection = $class::entityCollection(); $this->selectSqlQuery(); $this->finalizeQuery(); foreach(Ulmus::iterateQueryBuilder($this->queryBuilder, $this->adapter) as $entityData) { $entityCollection->append( ( new $class() )->entityFillFromDataset($entityData) ); } $this->eventExecute(Event\Repository\CollectionFromQueryInterface::class, $entityCollection); return $entityCollection; } public function arrayFromQuery() : array { $this->selectSqlQuery(); $this->finalizeQuery(); return Ulmus::datasetQueryBuilder($this->queryBuilder, $this->adapter); } public function runQuery() : ? \PDOStatement { $this->finalizeQuery(); 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) ) { $this->insert(array_keys($dataset), $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName()); } $this->values($dataset); return $this; } protected function updateSqlQuery(array $dataset) : self { if ( null === $this->queryBuilder->getFragment(Query\Update::class) ) { $this->update($this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName()); } $this->set($dataset); return $this; } protected function selectSqlQuery() : self { if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) { $this->select("{$this->alias}.*"); } if ( null === $this->queryBuilder->getFragment(Query\From::class) ) { $this->from($this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName()); } return $this; } protected function deleteSqlQuery() : self { if ( null === $this->queryBuilder->getFragment(Query\Delete::class) ) { $this->delete(); } if ( null === $this->queryBuilder->getFragment(Query\From::class) ) { $this->from($this->entityResolver->tableName(), null, $this->entityResolver->schemaName()); } return $this; } protected function fromRow($row) : self { } protected function fromCollection($rows) : self { } public function instanciateEntityCollection(...$arguments) : EntityCollection { return $this->entityClass::entityCollection(...$arguments); } public function escapeTable(string $identifier) : string { return $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\AdapterInterface::IDENTIFIER_TABLE); } public function escapeDatabase(string $identifier) : string { return $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\AdapterInterface::IDENTIFIER_DATABASE); } public function escapeSchema(string $identifier) : string { return $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\AdapterInterface::IDENTIFIER_SCHEMA); } protected function matchEntity(object $entity) { return get_class($entity) === $this->entityClass; } protected function finalizeQuery() : void {} }