ulmus/src/Repository.php

315 lines
7.3 KiB
PHP
Raw Normal View History

2019-08-21 20:13:00 +00:00
<?php
namespace Ulmus;
use Ulmus\Common\EntityResolver;
class Repository
{
const DEFAULT_ALIAS = "this";
protected ? ConnectionAdapter $adapter;
protected QueryBuilder $queryBuilder;
protected EntityResolver $entityResolver;
public string $alias;
public string $entityClass;
public function __construct(string $entity, string $alias = self::DEFAULT_ALIAS, ConnectionAdapter $adapter = null) {
$this->entityClass = $entity;
$this->alias = $alias;
$this->adapter = $adapter;
$this->queryBuilder = new QueryBuilder( $adapter->adapter() );
2019-08-21 20:13:00 +00:00
$this->entityResolver = Ulmus::resolveEntity($entity);
}
2020-01-31 21:38:48 +00:00
public function loadOne() : ? object
2019-08-21 20:13:00 +00:00
{
2020-01-31 21:38:48 +00:00
return $this->limit(1)->collectionFromQuery()[0] ?? null;
2019-08-21 20:13:00 +00:00
}
2020-01-31 21:38:48 +00:00
public function loadFromPk($value, $primaryKey = "id") : ? object
2019-08-21 20:13:00 +00:00
{
2020-01-31 21:38:48 +00:00
return $this->where($primaryKey, $value)->loadOne();
2019-08-21 20:13:00 +00:00
}
2020-01-31 21:38:48 +00:00
public function loadAll() : EntityCollection
2019-08-21 20:13:00 +00:00
{
2020-01-31 21:38:48 +00:00
return $this->collectionFromQuery();
2019-08-21 20:13:00 +00:00
}
public function loadFromField($field, $value) : EntityCollection
{
return $this->where($field, $value)->collectionFromQuery();
}
public function deleteOne()
{
return $this->limit(1)->deleteSqlQuery()->runQuery();
}
public function deleteAll()
{
return $this->deleteSqlQuery()->runQuery();
}
public function deleteFromPk($value, $primaryKey = "id")
{
return $this->where($primaryKey, $value)->deleteOne();
}
2020-01-31 21:38:48 +00:00
public function save(object $entity) : bool
{
}
public function saveAll(EntityCollection $collection) : bool
{
}
2019-08-21 20:13:00 +00:00
public function yieldAll() : \Generator
{
}
public function select($fields) : self
{
$this->queryBuilder->select($fields);
2019-08-21 20:13:00 +00:00
return $this;
}
public function delete() : self
{
$this->queryBuilder->delete($this->alias);
return $this;
}
public function from(string $table, string $alias, ? string $schema) : self
2019-08-21 20:13:00 +00:00
{
$this->queryBuilder->from($table, $alias, null, $schema);
2019-08-21 20:13:00 +00:00
return $this;
}
public function join(string $type, $table, $field, $value) : self
{
return $this;
}
public function open(string $condition = Query\Where::CONDITION_AND) : self
{
$this->queryBuilder->open($condition);
2019-08-21 20:13:00 +00:00
return $this;
}
public function orOpen() : self
{
return $this->open(Query\Where::CONDITION_OR);
}
public function close() : self
{
$this->queryBuilder->close();
2019-08-21 20:13:00 +00:00
return $this;
}
public function where($field, $value, string $operator = Query\Where::OPERATOR_EQUAL) : self
{
$this->queryBuilder->where($field, $value, $operator, Query\Where::CONDITION_AND);
2019-08-21 20:13:00 +00:00
return $this;
}
public function and($field, $value, string $operator = Query\Where::OPERATOR_EQUAL) : self
{
return $this->where($field, $value, $operator);
}
public function or($field, $value, string $operator = Query\Where::OPERATOR_EQUAL) : self
{
$this->queryBuilder->where($field, $value, $operator, Query\Where::CONDITION_OR);
2019-08-21 20:13:00 +00:00
return $this;
}
public function notWhere(array $condition) : self
{
$this->queryBuilder->where($field, $value, $operator, Query\Where::CONDITION_AND, true);
2019-08-21 20:13:00 +00:00
return $this;
}
public function orNot($field, $value, string $operator = Query\Where::OPERATOR_EQUAL) : self
{
$this->queryBuilder->notWhere($condition, Query\Where::CONDITION_OR, true);
2019-08-21 20:13:00 +00:00
return $this;
}
public function having() : self
{
return $this;
}
public function notHaving() : self
{
return $this;
}
public function in($field, $value, string $operator = Query\Where::OPERATOR_EQUAL) : self
{
$this->queryBuilder->where($field, $value, $operator);
2019-08-21 20:13:00 +00:00
return $this;
}
public function orIn($field, $value, string $operator = Query\Where::OPERATOR_EQUAL) : self
{
$this->queryBuilder->where($field, $value, $operator, Query\Where::CONDITION_OR);
2019-08-21 20:13:00 +00:00
return $this;
}
public function notIn($field, $value) : self
{
$this->queryBuilder->where($field, $value, Query\Where::OPERATOR_NOT_EQUAL);
2019-08-21 20:13:00 +00:00
return $this;
}
public function orNotIn($field, $value, string $operator = Query\Where::OPERATOR_EQUAL) : self
{
return $this->orNot($field, $value, Query\Where::OPERATOR_NOT_EQUAL, Query\Where::CONDITION_OR);
}
public function like($field, $value) : self
{
$this->queryBuilder->where($field, $value, Query\Where::OPERATOR_LIKE, Query\Where::CONDITION_AND);
2019-08-21 20:13:00 +00:00
return $this;
}
public function notLike($field, $value) : self
{
$this->queryBuilder->where($field, $value, Query\Where::OPERATOR_LIKE, Query\Where::CONDITION_AND, true);
2019-08-21 20:13:00 +00:00
return $this;
}
public function match() : self
{
}
public function notMatch() : self
{
}
public function between() : self
{
}
public function notBetween() : self
{
}
public function groupBy() : self
{
#$this->queryBuilder->groupBy();
return $this;
}
public function orderBy(string $field, ? string $direction = null) : self
{
$this->queryBuilder->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;
}
public function commit() : self
{
return $this;
}
public function rollback() : self
{
return $this;
}
protected function collectionFromQuery() : EntityCollection
{
$class = $this->entityClass;
$entityCollection = new EntityCollection();
foreach(Ulmus::iterateQueryBuilder($this->selectSqlQuery()->queryBuilder) as $entityData) {
$entityCollection->append( ( new $class() )->entityFillFromDataset($entityData) );
}
return $entityCollection;
}
public function runQuery()
{
return Ulmus::runQuery($this->queryBuilder);
}
2019-08-21 20:13:00 +00:00
protected function selectSqlQuery() : self
{
if ( ! $this->queryBuilder->has(Query\Select::class) ) {
$this->select("{$this->alias}.*");
}
if ( ! $this->queryBuilder->has(Query\From::class) ) {
$this->from($this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
2019-08-21 20:13:00 +00:00
}
return $this;
}
protected function deleteSqlQuery() : self
{
if ( ! $this->queryBuilder->has(Query\Delete::class) ) {
$this->delete();
}
if ( ! $this->queryBuilder->has(Query\From::class) ) {
$this->from($this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
}
return $this;
}
2019-08-21 20:13:00 +00:00
protected function fromRow($row) : self
{
}
protected function fromCollection($rows) : self
{
}
}