2019-08-21 20:13:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Ulmus;
|
|
|
|
|
|
|
|
use Ulmus\Common\EntityResolver;
|
|
|
|
|
|
|
|
class Repository
|
|
|
|
{
|
2020-10-06 15:00:12 +00:00
|
|
|
use EventTrait, Repository\ConditionTrait;
|
2020-05-20 19:34:50 +00:00
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
const DEFAULT_ALIAS = "this";
|
|
|
|
|
2020-04-09 17:20:07 +00:00
|
|
|
public ? ConnectionAdapter $adapter;
|
2019-08-21 20:13:00 +00:00
|
|
|
|
|
|
|
protected QueryBuilder $queryBuilder;
|
|
|
|
|
|
|
|
protected EntityResolver $entityResolver;
|
|
|
|
|
|
|
|
public string $alias;
|
|
|
|
|
|
|
|
public string $entityClass;
|
2020-05-20 19:34:50 +00:00
|
|
|
|
|
|
|
public array $events = [];
|
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
public function __construct(string $entity, string $alias = self::DEFAULT_ALIAS, ConnectionAdapter $adapter = null) {
|
|
|
|
$this->entityClass = $entity;
|
|
|
|
$this->alias = $alias;
|
|
|
|
$this->entityResolver = Ulmus::resolveEntity($entity);
|
2020-02-13 03:56:53 +00:00
|
|
|
$this->adapter = $adapter ?? $this->entityResolver->databaseAdapter() ?? Ulmus::$defaultAdapter;
|
2020-10-06 15:00:12 +00:00
|
|
|
$this->queryBuilder = new QueryBuilder();
|
2019-08-21 20:13:00 +00:00
|
|
|
}
|
2020-11-27 17:09:15 +00:00
|
|
|
|
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
#$this->queryBuilder = clone $this->queryBuilder;
|
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
|
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-02-17 13:23:41 +00:00
|
|
|
public function loadOneFromField($field, $value) : ? object
|
|
|
|
{
|
|
|
|
return $this->where($field, $value)->loadOne();
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:34:50 +00:00
|
|
|
public function loadFromPk($value, /* ? stringable */ $primaryKey = null) : ? object
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
2020-05-20 19:34:50 +00:00
|
|
|
return $primaryKey ? $this->loadOneFromField($primaryKey, $value) : $this->wherePrimaryKey($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-05-20 19:34:50 +00:00
|
|
|
if ( null !== $select = $this->queryBuilder->getFragment(Query\Select::class) ) {
|
|
|
|
$this->queryBuilder->removeFragment($select);
|
|
|
|
}
|
|
|
|
|
2020-10-06 15:00:12 +00:00
|
|
|
if ( $this->queryBuilder->getFragment(Query\GroupBy::class) ) {
|
|
|
|
$this->select( "DISTINCT COUNT(*) OVER ()" );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->select(Common\Sql::function("COUNT", "*"));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->selectSqlQuery();
|
2020-02-07 21:36:38 +00:00
|
|
|
|
|
|
|
$this->finalizeQuery();
|
2020-02-01 03:01:56 +00:00
|
|
|
|
2020-04-09 13:50:09 +00:00
|
|
|
return Ulmus::runSelectQuery($this->queryBuilder, $this->adapter)->fetchColumn(0);
|
2020-02-01 03:01:56 +00:00
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
|
2020-06-11 12:47:15 +00:00
|
|
|
protected function deleteOne()
|
2020-02-07 21:36:38 +00:00
|
|
|
{
|
2019-12-19 14:54:33 +00:00
|
|
|
return $this->limit(1)->deleteSqlQuery()->runQuery();
|
|
|
|
}
|
|
|
|
|
2020-06-11 12:47:15 +00:00
|
|
|
protected function deleteAll()
|
2019-12-19 14:54:33 +00:00
|
|
|
{
|
|
|
|
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-05-20 19:34:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 21:38:48 +00:00
|
|
|
public function save(object $entity) : bool
|
2020-02-06 04:41:57 +00:00
|
|
|
{
|
2020-05-20 19:34:50 +00:00
|
|
|
if ( ! $this->matchEntity($entity) ) {
|
|
|
|
throw new \Exception("Your entity class `" . get_class($entity) . "` cannot match entity type of repository `{$this->entityClass}`");
|
|
|
|
}
|
|
|
|
|
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);
|
2020-11-27 17:09:15 +00:00
|
|
|
$dataset[$pkField] = $statement->lastInsertId;
|
2020-02-06 04:41:57 +00:00
|
|
|
}
|
2020-02-07 21:36:38 +00:00
|
|
|
|
2020-11-27 17:09:15 +00:00
|
|
|
$entity->entityFillFromDataset($dataset, true);
|
|
|
|
|
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));
|
|
|
|
}
|
2020-02-17 13:23:41 +00:00
|
|
|
|
2020-03-19 13:55:00 +00:00
|
|
|
if ( [] !== $diff = $this->generateDatasetDiff($entity) ) {
|
2020-02-17 13:23:41 +00:00
|
|
|
$pkField = key($primaryKeyDefinition);
|
|
|
|
$pkFieldName = $primaryKeyDefinition[$pkField]->name ?? $pkField;
|
|
|
|
$this->where($pkFieldName, $dataset[$pkFieldName]);
|
|
|
|
|
2020-02-07 21:36:38 +00:00
|
|
|
$update = $this->updateSqlQuery($diff)->runQuery();
|
2020-11-27 17:09:15 +00:00
|
|
|
$entity->entityFillFromDataset($dataset);
|
2020-02-07 21:36:38 +00:00
|
|
|
|
2020-02-17 13:23:41 +00:00
|
|
|
return $update ? (bool) $update->rowCount() : false;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-02-17 13:23:41 +00:00
|
|
|
public function saveAll(EntityCollection $collection) : void
|
2020-01-31 21:38:48 +00:00
|
|
|
{
|
2020-02-05 21:19:57 +00:00
|
|
|
foreach($collection as $entity) {
|
|
|
|
$this->save($entity);
|
|
|
|
}
|
2020-01-31 21:38:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 15:00:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-19 13:55:00 +00:00
|
|
|
public function generateDatasetDiff(object $entity) : array
|
|
|
|
{
|
2020-05-20 19:34:50 +00:00
|
|
|
return array_diff_assoc( array_change_key_case($entity->toArray()), array_change_key_case($entity->entityGetDataset(false, true)) );
|
2020-03-19 13:55:00 +00:00
|
|
|
}
|
2020-10-06 15:00:12 +00:00
|
|
|
|
|
|
|
public function yield() : \Generator
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
2020-10-06 15:00:12 +00:00
|
|
|
$class = $this->entityClass;
|
|
|
|
|
|
|
|
$this->selectSqlQuery();
|
|
|
|
|
|
|
|
$this->finalizeQuery();
|
2019-08-21 20:13:00 +00:00
|
|
|
|
2020-10-06 15:00:12 +00:00
|
|
|
foreach(Ulmus::iterateQueryBuilder($this->queryBuilder, $this->adapter) as $entityData) {
|
|
|
|
yield ( new $class() )->entityFillFromDataset($entityData);
|
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 17:09:15 +00:00
|
|
|
public function select(/*array|Stringable*/ $fields) : self
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
{
|
2020-04-09 17:20:07 +00:00
|
|
|
$this->queryBuilder->insert($fieldlist, $this->escapeTable($table), $alias, $schema);
|
2020-02-05 21:19:57 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function values(array $dataset) : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->values($dataset);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-04-09 13:50:09 +00:00
|
|
|
public function update(string $table, string $alias, ? string $schema) : self
|
2020-02-06 04:41:57 +00:00
|
|
|
{
|
2020-04-09 17:20:07 +00:00
|
|
|
$this->queryBuilder->update($this->escapeTable($table), $alias, $schema);
|
2020-02-06 04:41:57 +00:00
|
|
|
|
|
|
|
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-04-09 17:20:07 +00:00
|
|
|
$this->queryBuilder->from($this->escapeTable($table), $alias, $this->escapeDatabase($this->adapter->adapter()->database), $schema ? $this->escapeSchema($schema) : null);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-10-06 15:00:12 +00:00
|
|
|
public function join(string $type, $table, $field, $value, ? string $alias = null, ? callable $callback = null) : self
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
2020-10-06 15:00:12 +00:00
|
|
|
$join = $this->queryBuilder->withJoin($type, $this->escapeTable($table), $field, $value, false, $alias);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
2020-10-06 15:00:12 +00:00
|
|
|
if ( $callback ) {
|
|
|
|
$callback($join);
|
2020-03-29 02:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
|
2020-10-06 15:00:12 +00:00
|
|
|
public function outerJoin(string $type, $table, $field, $value, ? string $alias = null, ? callable $callback = null) : self
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
2020-10-06 15:00:12 +00:00
|
|
|
$join = $this->queryBuilder->withJoin($type, $this->escapeTable($table), $field, $value, true, $alias);
|
2020-01-29 21:11:16 +00:00
|
|
|
|
2020-10-06 15:00:12 +00:00
|
|
|
if ( $callback ) {
|
|
|
|
$callback($join);
|
2020-03-29 02:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
public function match() : self
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function notMatch() : self
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function between() : self
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function notBetween() : self
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2020-10-06 15:00:12 +00:00
|
|
|
|
|
|
|
public function groupBy($field) : self
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
2020-10-06 15:00:12 +00:00
|
|
|
$this->queryBuilder->groupBy($field);
|
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-03-29 02:13:29 +00:00
|
|
|
public function groups(array $groups) : self
|
|
|
|
{
|
2020-10-06 15:00:12 +00:00
|
|
|
foreach($groups as $field ) {
|
|
|
|
$this->groupBy($field);
|
|
|
|
}
|
|
|
|
|
2020-03-29 02:13:29 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-27 17:09:15 +00:00
|
|
|
# @UNTESTED
|
|
|
|
public function randomizeOrder() : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->orderBy(Common\Sql::function('RAND', Sql::identifier('CURDATE()+0')));
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-03-29 02:13:29 +00:00
|
|
|
public function orders(array $orderList) : self
|
|
|
|
{
|
|
|
|
foreach($orderList as $field => $direction) {
|
2020-10-06 15:00:12 +00:00
|
|
|
$this->orderBy($field, $direction);
|
2020-03-29 02:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:34:50 +00:00
|
|
|
|
|
|
|
/* @TODO */
|
2019-08-21 20:13:00 +00:00
|
|
|
public function commit() : self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:34:50 +00:00
|
|
|
/* @TODO */
|
2019-08-21 20:13:00 +00:00
|
|
|
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
|
|
|
}
|
2020-04-09 14:00:48 +00:00
|
|
|
|
2020-11-27 17:09:15 +00:00
|
|
|
public function withJoin(/*stringable|array*/ $fields) : self
|
2020-05-20 19:34:50 +00:00
|
|
|
{
|
|
|
|
$resolvedEntity = Ulmus::resolveEntity($this->entityClass);
|
2020-10-06 15:00:12 +00:00
|
|
|
|
|
|
|
if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) {
|
|
|
|
$this->select("{$this->alias}.*");
|
|
|
|
}
|
2020-05-20 19:34:50 +00:00
|
|
|
|
|
|
|
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'];
|
|
|
|
|
2020-11-27 17:09:15 +00:00
|
|
|
foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
|
2020-05-20 19:34:50 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2020-03-29 02:13:29 +00:00
|
|
|
public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest) : self
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
2020-10-06 15:00:12 +00:00
|
|
|
$likes = $searchRequest->likes();
|
|
|
|
$wheres = $searchRequest->wheres();
|
|
|
|
$groups = $searchRequest->groups();
|
|
|
|
|
2020-11-27 17:09:15 +00:00
|
|
|
$searchRequest->filter( $this )
|
2020-10-06 15:00:12 +00:00
|
|
|
->wheres($wheres, Query\Where::OPERATOR_EQUAL, Query\Where::CONDITION_AND)
|
|
|
|
->likes($likes, Query\Where::CONDITION_OR)
|
2020-11-27 17:09:15 +00:00
|
|
|
->groups($groups);
|
|
|
|
|
|
|
|
$searchRequest->count = $searchRequest->skipCount ? 0 : (clone $this)->count();
|
2020-02-07 21:36:38 +00:00
|
|
|
|
2020-11-27 17:09:15 +00:00
|
|
|
return $searchRequest
|
|
|
|
->filter($this)
|
2020-10-06 15:00:12 +00:00
|
|
|
->wheres($wheres, Query\Where::OPERATOR_EQUAL, Query\Where::CONDITION_AND)
|
|
|
|
->likes($likes, Query\Where::CONDITION_OR)
|
|
|
|
->groups($groups)
|
2020-11-27 17:09:15 +00:00
|
|
|
->orders($searchRequest->orders())
|
2020-03-29 02:13:29 +00:00
|
|
|
->offset($searchRequest->offset())
|
|
|
|
->limit($searchRequest->limit());
|
|
|
|
}
|
2020-05-20 19:34:50 +00:00
|
|
|
|
2020-04-09 13:50:09 +00:00
|
|
|
public function collectionFromQuery(? string $entityClass = null) : EntityCollection
|
2019-08-21 20:13:00 +00:00
|
|
|
{
|
2020-04-09 13:50:09 +00:00
|
|
|
$class = $entityClass ?: $this->entityClass;
|
2020-10-06 15:00:12 +00:00
|
|
|
|
|
|
|
$entityCollection = $class::entityCollection();
|
2020-02-07 21:36:38 +00:00
|
|
|
|
|
|
|
$this->selectSqlQuery();
|
|
|
|
|
|
|
|
$this->finalizeQuery();
|
2019-08-21 20:13:00 +00:00
|
|
|
|
2020-02-13 03:56:53 +00:00
|
|
|
foreach(Ulmus::iterateQueryBuilder($this->queryBuilder, $this->adapter) as $entityData) {
|
2019-08-21 20:13:00 +00:00
|
|
|
$entityCollection->append( ( new $class() )->entityFillFromDataset($entityData) );
|
|
|
|
}
|
2020-05-20 19:34:50 +00:00
|
|
|
|
2020-10-06 15:00:12 +00:00
|
|
|
$this->eventExecute(Event\Repository\CollectionFromQueryInterface::class, $entityCollection);
|
2019-08-21 20:13:00 +00:00
|
|
|
|
|
|
|
return $entityCollection;
|
|
|
|
}
|
2020-05-20 19:34:50 +00:00
|
|
|
|
|
|
|
public function arrayFromQuery() : array
|
|
|
|
{
|
|
|
|
$this->selectSqlQuery();
|
|
|
|
|
|
|
|
$this->finalizeQuery();
|
|
|
|
|
|
|
|
return Ulmus::datasetQueryBuilder($this->queryBuilder, $this->adapter);
|
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
|
2020-02-17 13:23:41 +00:00
|
|
|
public function runQuery() : ? \PDOStatement
|
2019-12-19 14:54:33 +00:00
|
|
|
{
|
2020-02-07 21:36:38 +00:00
|
|
|
$this->finalizeQuery();
|
|
|
|
|
2020-02-13 03:56:53 +00:00
|
|
|
return Ulmus::runQuery($this->queryBuilder, $this->adapter);
|
2019-12-19 14:54:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 17:09:15 +00:00
|
|
|
public function resetQuery() : self
|
|
|
|
{
|
|
|
|
$this->queryBuilder->reset();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
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-04-09 13:50:09 +00:00
|
|
|
$this->update($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
|
|
|
|
2020-11-27 17:09:15 +00:00
|
|
|
public function instanciateEntityCollection(...$arguments) : EntityCollection
|
2020-02-03 16:13:26 +00:00
|
|
|
{
|
2020-11-27 17:09:15 +00:00
|
|
|
return $this->entityClass::entityCollection(...$arguments);
|
2020-02-03 16:13:26 +00:00
|
|
|
}
|
2020-02-07 21:36:38 +00:00
|
|
|
|
2020-04-09 17:20:07 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:34:50 +00:00
|
|
|
protected function matchEntity(object $entity) {
|
|
|
|
return get_class($entity) === $this->entityClass;
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:36:38 +00:00
|
|
|
protected function finalizeQuery() : void {}
|
2019-08-21 20:13:00 +00:00
|
|
|
}
|