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;
|
2020-02-01 03:01:56 +00:00
|
|
|
$this->adapter = $adapter ?: Ulmus::$defaultAdapter;
|
|
|
|
$this->queryBuilder = new QueryBuilder( $this->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();
|
|
|
|
}
|
2020-02-01 03:01:56 +00:00
|
|
|
|
|
|
|
public function count() : int
|
|
|
|
{
|
2020-02-07 21:36:38 +00:00
|
|
|
$this->select("count(*) as totalItem")->selectSqlQuery();
|
|
|
|
|
|
|
|
$this->finalizeQuery();
|
2020-02-01 03:01:56 +00:00
|
|
|
|
2020-02-07 21:36:38 +00:00
|
|
|
foreach(Ulmus::iterateQueryBuilder($this->queryBuilder) as $entityData) {
|
2020-02-01 03:01:56 +00:00
|
|
|
return $entityData['totalItem'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
|
2019-12-19 14:54:33 +00:00
|
|
|
public function deleteOne()
|
2020-02-07 21:36:38 +00:00
|
|
|
{
|
2019-12-19 14:54:33 +00:00
|
|
|
return $this->limit(1)->deleteSqlQuery()->runQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteAll()
|
|
|
|
{
|
|
|
|
return $this->deleteSqlQuery()->runQuery();
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:36:38 +00:00
|
|
|
public function deleteFromPk($value) : bool
|
2020-02-06 04:41:57 +00:00
|
|
|
{
|
2020-02-07 21:36:38 +00:00
|
|
|
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();
|
2019-12-19 14:54:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 21:38:48 +00:00
|
|
|
public function save(object $entity) : bool
|
2020-02-06 04:41:57 +00:00
|
|
|
{
|
2020-02-05 21:19:57 +00:00
|
|
|
$dataset = $entity->toArray();
|
2020-02-07 21:36:38 +00:00
|
|
|
|
2020-02-06 04:41:57 +00:00
|
|
|
$primaryKeyDefinition = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField();
|
|
|
|
|
2020-02-05 21:19:57 +00:00
|
|
|
if ( ! $entity->isLoaded() ) {
|
2020-02-06 04:41:57 +00:00
|
|
|
$statement = $this->insertSqlQuery($dataset)->runQuery();
|
|
|
|
|
|
|
|
if ( ( 0 !== $statement->lastInsertId ) &&
|
|
|
|
( null !== $primaryKeyDefinition )) {
|
|
|
|
$pkField = key($primaryKeyDefinition);
|
|
|
|
$entity->$pkField = $statement->lastInsertId;
|
|
|
|
}
|
2020-02-07 21:36:38 +00:00
|
|
|
|
|
|
|
return true;
|
2020-02-05 21:19:57 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-02-07 21:36:38 +00:00
|
|
|
if ( $primaryKeyDefinition === null ) {
|
2020-02-06 04:41:57 +00:00
|
|
|
throw new \Exception(sprintf("No primary key found for entity %s", $this->entityClass));
|
|
|
|
}
|
|
|
|
|
|
|
|
$pkField = key($primaryKeyDefinition);
|
|
|
|
$pkFieldName = $primaryKeyDefinition[$pkField]->name ?? $pkField;
|
|
|
|
$this->where($pkFieldName, $dataset[$pkFieldName]);
|
2020-02-07 21:36:38 +00:00
|
|
|
|
|
|
|
$diff = array_diff_assoc($dataset, $entity->entityGetDataset(true));
|
|
|
|
|
|
|
|
var_dump( "<pre>", $diff, $dataset, $entity->entityGetDataset(true) );
|
|
|
|
|
|
|
|
if ( [] !== $diff ) {
|
|
|
|
$update = $this->updateSqlQuery($diff)->runQuery();
|
|
|
|
|
|
|
|
return (bool) $update->rowCount();
|
|
|
|
}
|
2020-02-05 21:19:57 +00:00
|
|
|
}
|
2020-01-31 21:38:48 +00:00
|
|
|
|
2020-02-05 21:19:57 +00:00
|
|
|
return false;
|
2020-01-31 21:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function saveAll(EntityCollection $collection) : bool
|
|
|
|
{
|
2020-02-05 21:19:57 +00:00
|
|
|
foreach($collection as $entity) {
|
|
|
|
$this->save($entity);
|
|
|
|
}
|
2020-01-31 21:38:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
public function yieldAll() : \Generator
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function select($fields) : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->select($fields);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-02-05 21:19:57 +00:00
|
|
|
public function insert(array $fieldlist, string $table, string $alias, ? string $schema) : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->insert($fieldlist, $table, $alias, $schema);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function values(array $dataset) : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->values($dataset);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-02-06 04:41:57 +00:00
|
|
|
public function update(array $fieldlist, string $table, string $alias, ? string $schema) : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->update($table, $alias, $schema);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set(array $dataset) : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->set($dataset);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-12-19 14:54:33 +00:00
|
|
|
public function delete() : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->delete($this->alias);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
2019-12-19 14:54:33 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:36:38 +00:00
|
|
|
public function from(string $table, ? string $alias, ? string $schema) : self
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
2020-01-29 21:11:16 +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);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
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();
|
2020-01-29 21:11:16 +00:00
|
|
|
|
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);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
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);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-02-06 04:41:57 +00:00
|
|
|
public function notWhere($field, $value, string $operator = Query\Where::OPERATOR_NOT_EQUAL) : self
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
|
|
|
$this->queryBuilder->where($field, $value, $operator, Query\Where::CONDITION_AND, true);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
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);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
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);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
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);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
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);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
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);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
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);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:36:38 +00:00
|
|
|
public function orderBy($field, ? string $direction = null) : self
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
|
|
|
$this->queryBuilder->orderBy($field, $direction);
|
2020-02-06 04:41:57 +00:00
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function limit(int $value) : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->limit($value);
|
2020-02-06 04:41:57 +00:00
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offset(int $value) : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->offset($value);
|
2020-02-06 04:41:57 +00:00
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function commit() : self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rollback() : self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-02-05 21:19:57 +00:00
|
|
|
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'");
|
|
|
|
}
|
2020-02-07 21:36:38 +00:00
|
|
|
|
|
|
|
$pkField = key($primaryKeyField);
|
|
|
|
|
|
|
|
return $this->where($primaryKeyField[$pkField]->name ?? $pkField, $value);
|
2020-02-05 21:19:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
protected function collectionFromQuery() : EntityCollection
|
|
|
|
{
|
2020-02-07 21:36:38 +00:00
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
$class = $this->entityClass;
|
|
|
|
|
|
|
|
$entityCollection = new EntityCollection();
|
2020-02-07 21:36:38 +00:00
|
|
|
|
|
|
|
$this->selectSqlQuery();
|
|
|
|
|
|
|
|
$this->finalizeQuery();
|
2019-08-21 20:13:00 +00:00
|
|
|
|
2020-02-07 21:36:38 +00:00
|
|
|
foreach(Ulmus::iterateQueryBuilder($this->queryBuilder) as $entityData) {
|
2019-08-21 20:13:00 +00:00
|
|
|
$entityCollection->append( ( new $class() )->entityFillFromDataset($entityData) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entityCollection;
|
|
|
|
}
|
|
|
|
|
2020-02-06 04:41:57 +00:00
|
|
|
public function runQuery() : \PDOStatement
|
2019-12-19 14:54:33 +00:00
|
|
|
{
|
2020-02-07 21:36:38 +00:00
|
|
|
$this->finalizeQuery();
|
|
|
|
|
2019-12-19 14:54:33 +00:00
|
|
|
return Ulmus::runQuery($this->queryBuilder);
|
|
|
|
}
|
|
|
|
|
2020-02-05 21:19:57 +00:00
|
|
|
protected function insertSqlQuery(array $dataset) : self
|
|
|
|
{
|
2020-02-07 21:36:38 +00:00
|
|
|
if ( null === $this->queryBuilder->getFragment(Query\Insert::class) ) {
|
2020-02-06 04:41:57 +00:00
|
|
|
$this->insert(array_keys($dataset), $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
2020-02-05 21:19:57 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 04:41:57 +00:00
|
|
|
$this->values($dataset);
|
2020-02-05 21:19:57 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function updateSqlQuery(array $dataset) : self
|
|
|
|
{
|
2020-02-07 21:36:38 +00:00
|
|
|
if ( null === $this->queryBuilder->getFragment(Query\Update::class) ) {
|
2020-02-06 04:41:57 +00:00
|
|
|
$this->update($dataset, $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
2020-02-05 21:19:57 +00:00
|
|
|
}
|
2020-02-06 04:41:57 +00:00
|
|
|
|
|
|
|
$this->set($dataset);
|
2020-02-05 21:19:57 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
protected function selectSqlQuery() : self
|
|
|
|
{
|
2020-02-07 21:36:38 +00:00
|
|
|
if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) {
|
2019-08-21 20:13:00 +00:00
|
|
|
$this->select("{$this->alias}.*");
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:36:38 +00:00
|
|
|
if ( null === $this->queryBuilder->getFragment(Query\From::class) ) {
|
2020-01-29 21:11:16 +00:00
|
|
|
$this->from($this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
2019-08-21 20:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-12-19 14:54:33 +00:00
|
|
|
protected function deleteSqlQuery() : self
|
|
|
|
{
|
2020-02-07 21:36:38 +00:00
|
|
|
if ( null === $this->queryBuilder->getFragment(Query\Delete::class) ) {
|
2019-12-19 14:54:33 +00:00
|
|
|
$this->delete();
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:36:38 +00:00
|
|
|
if ( null === $this->queryBuilder->getFragment(Query\From::class) ) {
|
|
|
|
$this->from($this->entityResolver->tableName(), null, $this->entityResolver->schemaName());
|
2019-12-19 14:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
protected function fromRow($row) : self
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function fromCollection($rows) : self
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2020-02-03 16:13:26 +00:00
|
|
|
|
|
|
|
public function table()
|
|
|
|
{
|
|
|
|
return "REFLECT TABLE";
|
|
|
|
}
|
2020-02-07 21:36:38 +00:00
|
|
|
|
|
|
|
protected function finalizeQuery() : void {}
|
2019-08-21 20:13:00 +00:00
|
|
|
}
|