improve cross database engine compatibility (#756)

This commit is contained in:
Kevin Papst
2019-05-05 03:09:02 +02:00
committed by GitHub
parent aa113cc300
commit 6e224c63ef
6 changed files with 97 additions and 71 deletions

View File

@@ -73,7 +73,7 @@ class Timesheet
* @var User * @var User
* *
* @ORM\ManyToOne(targetEntity="App\Entity\User") * @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(name="user", referencedColumnName="id", onDelete="CASCADE", nullable=false) * @ORM\JoinColumn(name="`user`", referencedColumnName="id", onDelete="CASCADE", nullable=false)
* @Assert\NotNull() * @Assert\NotNull()
*/ */
private $user; private $user;

View File

@@ -48,12 +48,13 @@ class ActivityRepository extends AbstractRepository
->join('t.project', 'p') ->join('t.project', 'p')
->join('p.customer', 'c') ->join('p.customer', 'c')
->andWhere($qb->expr()->isNotNull('t.end')) ->andWhere($qb->expr()->isNotNull('t.end'))
->andWhere('a.visible = 1') ->andWhere($qb->expr()->eq('a.visible', ':visible'))
->andWhere('p.visible = 1') ->andWhere($qb->expr()->eq('p.visible', ':visible'))
->andWhere('c.visible = 1') ->andWhere($qb->expr()->eq('c.visible', ':visible'))
->groupBy('a.id', 'p.id') ->groupBy('a.id', 'p.id')
->orderBy('maxid', 'DESC') ->orderBy('maxid', 'DESC')
->setMaxResults(10) ->setMaxResults(10)
->setParameter('visible', true, \PDO::PARAM_BOOL)
; ;
if (null !== $user) { if (null !== $user) {
@@ -88,10 +89,15 @@ class ActivityRepository extends AbstractRepository
} }
/** /**
* @param null|bool $visible
* @return int * @return int
*/ */
public function countActivity() public function countActivity($visible = null)
{ {
if (null !== $visible) {
return $this->count(['visible' => (bool) $visible]);
}
return $this->count([]); return $this->count([]);
} }
@@ -185,10 +191,10 @@ class ActivityRepository extends AbstractRepository
) )
); );
} }
$qb->setParameter('visible', 1); $qb->setParameter('visible', true, \PDO::PARAM_BOOL);
} elseif (ActivityQuery::SHOW_HIDDEN == $query->getVisibility()) { } elseif (ActivityQuery::SHOW_HIDDEN == $query->getVisibility()) {
$where->add('a.visible = :visible'); $where->add('a.visible = :visible');
$qb->setParameter('visible', 0); $qb->setParameter('visible', false, \PDO::PARAM_BOOL);
} }
if ($query->isGlobalsOnly()) { if ($query->isGlobalsOnly()) {

View File

@@ -20,9 +20,6 @@ use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Pagerfanta\Pagerfanta; use Pagerfanta\Pagerfanta;
/**
* Class CustomerRepository
*/
class CustomerRepository extends AbstractRepository class CustomerRepository extends AbstractRepository
{ {
/** /**
@@ -41,7 +38,7 @@ class CustomerRepository extends AbstractRepository
public function countCustomer($visible = null) public function countCustomer($visible = null)
{ {
if (null !== $visible) { if (null !== $visible) {
return $this->count(['visible' => (int) $visible]); return $this->count(['visible' => (bool) $visible]);
} }
return $this->count([]); return $this->count([]);
@@ -55,34 +52,47 @@ class CustomerRepository extends AbstractRepository
*/ */
public function getCustomerStatistics(Customer $customer) public function getCustomerStatistics(Customer $customer)
{ {
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('COUNT(t.id) as recordAmount')
->addSelect('SUM(t.duration) as recordDuration')
->addSelect('COUNT(DISTINCT(a.id)) as activityAmount')
->addSelect('COUNT(DISTINCT(p.id)) as projectAmount')
->from(Timesheet::class, 't')
->join(Activity::class, 'a')
->join(Project::class, 'p')
->join(Customer::class, 'c')
->andWhere('t.activity = a.id')
->andWhere('t.project = p.id')
->andWhere('p.customer = c.id')
->andWhere('c.id = :customer')
;
$result = $qb->getQuery()->execute(['customer' => $customer], Query::HYDRATE_ARRAY);
$stats = new CustomerStatistic(); $stats = new CustomerStatistic();
$stats->setCount(1);
if (isset($result[0])) { $qb = $this->getEntityManager()->createQueryBuilder();
$dbStats = $result[0]; $qb
->addSelect('COUNT(t.id) as recordAmount')
->addSelect('SUM(t.duration) as recordDuration')
->from(Timesheet::class, 't')
->join(Project::class, 'p', Query\Expr\Join::WITH, 't.project = p.id')
->andWhere('p.customer = :customer')
;
$timesheetResult = $qb->getQuery()->execute(['customer' => $customer], Query::HYDRATE_ARRAY);
$stats->setCount(1); if (isset($timesheetResult[0])) {
$stats->setRecordAmount($dbStats['recordAmount']); $stats->setRecordAmount($timesheetResult[0]['recordAmount']);
$stats->setRecordDuration($dbStats['recordDuration']); $stats->setRecordDuration($timesheetResult[0]['recordDuration']);
$stats->setActivityAmount($dbStats['activityAmount']); }
$stats->setProjectAmount($dbStats['projectAmount']);
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->addSelect('COUNT(a.id) as activityAmount')
->from(Activity::class, 'a')
->join(Project::class, 'p', Query\Expr\Join::WITH, 'a.project = p.id')
->andWhere('a.project = p.id')
->andWhere('p.customer = :customer')
;
$activityResult = $qb->getQuery()->execute(['customer' => $customer], Query::HYDRATE_ARRAY);
if (isset($activityResult[0])) {
$stats->setActivityAmount($activityResult[0]['activityAmount']);
}
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->addSelect('COUNT(p.id) as projectAmount')
->from(Project::class, 'p')
->andWhere('p.customer = :customer')
;
$projectResult = $qb->getQuery()->execute(['customer' => $customer], Query::HYDRATE_ARRAY);
if (isset($projectResult[0])) {
$stats->setProjectAmount($projectResult[0]['projectAmount']);
} }
return $stats; return $stats;
@@ -117,7 +127,8 @@ class CustomerRepository extends AbstractRepository
->orderBy('c.' . $query->getOrderBy(), $query->getOrder()); ->orderBy('c.' . $query->getOrderBy(), $query->getOrder());
if (CustomerQuery::SHOW_VISIBLE == $query->getVisibility()) { if (CustomerQuery::SHOW_VISIBLE == $query->getVisibility()) {
$qb->andWhere('c.visible = 1'); $qb->andWhere($qb->expr()->eq('c.visible', ':visible'));
$qb->setParameter('visible', true, \PDO::PARAM_BOOL);
/** @var Customer $entity */ /** @var Customer $entity */
$entity = $query->getHiddenEntity(); $entity = $query->getHiddenEntity();
@@ -125,7 +136,8 @@ class CustomerRepository extends AbstractRepository
$qb->orWhere('c.id = :customer')->setParameter('customer', $entity); $qb->orWhere('c.id = :customer')->setParameter('customer', $entity);
} }
} elseif (CustomerQuery::SHOW_HIDDEN == $query->getVisibility()) { } elseif (CustomerQuery::SHOW_HIDDEN == $query->getVisibility()) {
$qb->andWhere('c.visible = 0'); $qb->andWhere($qb->expr()->eq('c.visible', ':visible'));
$qb->setParameter('visible', false, \PDO::PARAM_BOOL);
} }
if (!empty($query->getIgnoredEntities())) { if (!empty($query->getIgnoredEntities())) {

View File

@@ -41,42 +41,44 @@ class ProjectRepository extends AbstractRepository
public function countProject($visible = null) public function countProject($visible = null)
{ {
if (null !== $visible) { if (null !== $visible) {
return $this->count(['visible' => (int) $visible]); return $this->count(['visible' => (bool) $visible]);
} }
return $this->count([]); return $this->count([]);
} }
/** public function getProjectStatistics(Project $project): ProjectStatistic
* Retrieves statistics for one project.
*
* @param Project $project
* @return ProjectStatistic
*/
public function getProjectStatistics(Project $project)
{ {
$qb = $this->getEntityManager()->createQueryBuilder(); $qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('COUNT(t.id) as recordAmount') $qb->select('COUNT(t.id) as recordAmount')
->addSelect('SUM(t.duration) as recordDuration') ->addSelect('SUM(t.duration) as recordDuration')
->addSelect('COUNT(DISTINCT(a.id)) as activityAmount') ->from(Timesheet::class, 't')
->from(Activity::class, 'a') ->andWhere('t.project = :project')
->join(Timesheet::class, 't')
->where('t.project = :project')
->andWhere('t.activity = a.id')
; ;
$resultTimesheets = $qb->getQuery()->execute(['project' => $project], Query::HYDRATE_ARRAY);
$result = $qb->getQuery()->execute(['project' => $project], Query::HYDRATE_ARRAY); $qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('COUNT(a.id) as activityAmount')
->from(Activity::class, 'a')
->andWhere('a.project = :project')
;
$resultActivities = $qb->getQuery()->execute(['project' => $project], Query::HYDRATE_ARRAY);
$stats = new ProjectStatistic(); $stats = new ProjectStatistic();
$stats->setCount(1);
if (isset($result[0])) { if (isset($resultTimesheets[0])) {
$dbStats = $result[0]; $resultTimesheets = $resultTimesheets[0];
$stats->setCount(1); $stats->setRecordAmount($resultTimesheets['recordAmount']);
$stats->setRecordAmount($dbStats['recordAmount']); $stats->setRecordDuration($resultTimesheets['recordDuration']);
$stats->setRecordDuration($dbStats['recordDuration']); }
$stats->setActivityAmount($dbStats['activityAmount']);
if (isset($resultActivities[0])) {
$resultActivities = $resultActivities[0];
$stats->setActivityAmount($resultActivities['activityAmount']);
} }
return $stats; return $stats;
@@ -108,7 +110,7 @@ class ProjectRepository extends AbstractRepository
{ {
$qb = $this->getEntityManager()->createQueryBuilder(); $qb = $this->getEntityManager()->createQueryBuilder();
// if we join activities, the maxperpage limit will limit the list // if we join activities, the max-per-page limit will limit the list
// due to the raised amount of rows by projects * activities // due to the raised amount of rows by projects * activities
$qb->select('p', 'c') $qb->select('p', 'c')
->from(Project::class, 'p') ->from(Project::class, 'p')
@@ -117,9 +119,10 @@ class ProjectRepository extends AbstractRepository
if (ProjectQuery::SHOW_VISIBLE == $query->getVisibility()) { if (ProjectQuery::SHOW_VISIBLE == $query->getVisibility()) {
if (!$query->isExclusiveVisibility()) { if (!$query->isExclusiveVisibility()) {
$qb->andWhere('c.visible = 1'); $qb->andWhere($qb->expr()->eq('c.visible', ':visible'));
} }
$qb->andWhere('p.visible = 1'); $qb->andWhere($qb->expr()->eq('p.visible', ':visible'));
$qb->setParameter('visible', true, \PDO::PARAM_BOOL);
$entity = $query->getHiddenEntity(); $entity = $query->getHiddenEntity();
if (null !== $entity) { if (null !== $entity) {
@@ -128,7 +131,8 @@ class ProjectRepository extends AbstractRepository
// TODO check for visibility of customer // TODO check for visibility of customer
} elseif (ProjectQuery::SHOW_HIDDEN == $query->getVisibility()) { } elseif (ProjectQuery::SHOW_HIDDEN == $query->getVisibility()) {
$qb->andWhere('p.visible = 0'); $qb->andWhere($qb->expr()->eq('p.visible', ':visible'));
$qb->setParameter('visible', false, \PDO::PARAM_BOOL);
// TODO check for visibility of customer // TODO check for visibility of customer
} }

View File

@@ -212,13 +212,13 @@ class TimesheetRepository extends AbstractRepository
$qb->select('SUM(t.rate) as rate, SUM(t.duration) as duration, MONTH(t.begin) as month, YEAR(t.begin) as year') $qb->select('SUM(t.rate) as rate, SUM(t.duration) as duration, MONTH(t.begin) as month, YEAR(t.begin) as year')
->from(Timesheet::class, 't') ->from(Timesheet::class, 't')
->where($qb->expr()->gt('t.begin', ':from'))
; ;
if (!empty($begin)) { if (!empty($begin)) {
$qb->where($qb->expr()->gt('t.begin', ':from'));
$qb->setParameter('from', $begin, Type::DATETIME); $qb->setParameter('from', $begin, Type::DATETIME);
} else { } else {
$qb->setParameter('from', 0); $qb->where($qb->expr()->isNotNull('t.begin'));
} }
if (!empty($end)) { if (!empty($end)) {
@@ -274,7 +274,7 @@ class TimesheetRepository extends AbstractRepository
->join('t.activity', 'a') ->join('t.activity', 'a')
->join('t.project', 'p') ->join('t.project', 'p')
->join('p.customer', 'c') ->join('p.customer', 'c')
->where($qb->expr()->gt('t.begin', '0')) ->where($qb->expr()->isNotNull('t.begin'))
->andWhere($qb->expr()->isNull('t.end')) ->andWhere($qb->expr()->isNull('t.end'))
->orderBy('t.begin', 'DESC'); ->orderBy('t.begin', 'DESC');

View File

@@ -13,9 +13,6 @@ use App\Entity\User;
use App\Repository\Query\UserQuery; use App\Repository\Query\UserQuery;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface; use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
/**
* Class UserRepository
*/
class UserRepository extends AbstractRepository implements UserLoaderInterface class UserRepository extends AbstractRepository implements UserLoaderInterface
{ {
/** /**
@@ -41,10 +38,15 @@ class UserRepository extends AbstractRepository implements UserLoaderInterface
} }
/** /**
* @param null|bool $enabled
* @return int * @return int
*/ */
public function countUser() public function countUser($enabled = null)
{ {
if (null !== $enabled) {
return $this->count(['enabled' => (bool) $enabled]);
}
return $this->count([]); return $this->count([]);
} }
@@ -61,9 +63,11 @@ class UserRepository extends AbstractRepository implements UserLoaderInterface
->orderBy('u.' . $query->getOrderBy(), $query->getOrder()); ->orderBy('u.' . $query->getOrderBy(), $query->getOrder());
if (UserQuery::SHOW_VISIBLE == $query->getVisibility()) { if (UserQuery::SHOW_VISIBLE == $query->getVisibility()) {
$qb->andWhere('u.enabled = 1'); $qb->andWhere($qb->expr()->eq('u.enabled', ':enabled'));
$qb->setParameter('enabled', true, \PDO::PARAM_BOOL);
} elseif (UserQuery::SHOW_HIDDEN == $query->getVisibility()) { } elseif (UserQuery::SHOW_HIDDEN == $query->getVisibility()) {
$qb->andWhere('u.enabled = 0'); $qb->andWhere($qb->expr()->eq('u.enabled', ':enabled'));
$qb->setParameter('enabled', false, \PDO::PARAM_BOOL);
} }
if ($query->getRole() !== null) { if ($query->getRole() !== null) {