/**
  * Returns the total amount of elements in the repository given the restrictions provided by the Filter object.
  *
  * @param Filter|null $filter
  *
  * @return int
  */
 public function count(Filter $filter = null) : int
 {
     $query = $this->queryBuilder();
     $query->select(['COUNT(' . $this->mapping->identity() . ') AS total'])->from($this->mapping->name())->execute()->fetch(PDO::FETCH_ASSOC);
     if ($filter) {
         SqlFilter::filter($query, $filter, $this->mapping);
     }
     return (int) $query->execute()->fetch(PDO::FETCH_ASSOC)['total'];
 }
 /**
  * Returns all instances of the type meeting $distinctFields values.
  *
  * @param Fields      $distinctFields
  * @param Filter|null $filter
  * @param Sort|null   $sort
  *
  * @return array
  */
 public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null) : array
 {
     $query = $this->queryBuilder();
     $query->select($distinctFields ? 'DISTINCT ' . implode(', ', $this->getColumns($distinctFields)) : ['DISTINCT *'])->from($this->mapping->name());
     if ($filter) {
         SqlFilter::filter($query, $filter, $this->mapping);
     }
     if ($sort) {
         SqlSorter::sort($query, $sort, $this->mapping);
     }
     return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
 }
 /**
  * Returns a Page of entities meeting the paging restriction provided in the Pageable object.
  *
  * @param Pageable $pageable
  *
  * @return Page
  */
 public function findAll(Pageable $pageable = null) : Page
 {
     $query = $this->queryBuilder();
     if ($pageable) {
         $columns = $this->getPageColumns($pageable);
         $query->select($columns)->from($this->mapping->name());
         if ($filter = $pageable->filters()) {
             SqlFilter::filter($query, $filter, $this->mapping);
         }
         if ($sort = $pageable->sortings()) {
             SqlSorter::sort($query, $sort, $this->mapping);
         }
         $total = $this->count($pageable->filters());
         return new ResultPage($query->getConnection()->executeQuery(sprintf($query->getSQL() . ' LIMIT %s, %s', (int) ($pageable->offset() - $pageable->pageSize()), (int) $pageable->pageSize()), $query->getParameters())->fetchAll(PDO::FETCH_ASSOC), $total, $pageable->pageNumber(), ceil($total / ($pageable->pageSize() > 0) ? $pageable->pageSize() : 1));
     }
     $query->select('*')->from($this->mapping->name());
     return new ResultPage($query->getConnection()->executeQuery($query->getSQL(), $query->getParameters())->fetchAll(PDO::FETCH_ASSOC), $this->count(), 1, 1);
 }
 /**
  * Removes all elements in the repository given the restrictions provided by the Filter object.
  * If $filter is null, all the repository data will be deleted.
  *
  * @param Filter $filter
  */
 public function removeAll(Filter $filter = null)
 {
     $query = $this->queryBuilder();
     $query->delete($this->mapping->name());
     if ($filter) {
         SqlFilter::filter($query, $filter, $this->mapping);
     }
     $query->execute();
 }