Merge pull request #325 from acelaya/feature/dql

Feature/dql
This commit is contained in:
Alejandro Celaya 2018-12-19 17:47:10 +01:00 committed by GitHub
commit eac82a602c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 58 deletions

View File

@ -4,6 +4,29 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org).
## [Unreleased]
#### Added
* *Nothing*
#### Changed
* [#320](https://github.com/shlinkio/shlink/issues/320) Replaced query builder by plain DQL for all queries which do not need to be dynamically generated.
#### Deprecated
* *Nothing*
#### Removed
* *Nothing*
#### Fixed
* *Nothing*
## 1.15.1 - 2018-12-16
#### Added

View File

@ -16,12 +16,9 @@ use function key;
class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryInterface
{
/**
* @param int|null $limit
* @param int|null $offset
* @param string|null $searchTerm
* @param array $tags
* @param string[] $tags
* @param string|array|null $orderBy
* @return \Shlinkio\Shlink\Core\Entity\ShortUrl[]
* @return ShortUrl[]
*/
public function findList(
int $limit = null,
@ -51,7 +48,7 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
return $qb->getQuery()->getResult();
}
protected function processOrderByForList(QueryBuilder $qb, $orderBy)
private function processOrderByForList(QueryBuilder $qb, $orderBy): array
{
// Map public field names to column names
$fieldNameMap = [
@ -78,13 +75,6 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
return $qb->getQuery()->getResult();
}
/**
* Counts the number of elements in a list using provided filtering data
*
* @param null|string $searchTerm
* @param array $tags
* @return int
*/
public function countList(string $searchTerm = null, array $tags = []): int
{
$qb = $this->createListQueryBuilder($searchTerm, $tags);
@ -93,12 +83,7 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
return (int) $qb->getQuery()->getSingleScalarResult();
}
/**
* @param null|string $searchTerm
* @param array $tags
* @return QueryBuilder
*/
protected function createListQueryBuilder(string $searchTerm = null, array $tags = []): QueryBuilder
private function createListQueryBuilder(?string $searchTerm = null, array $tags = []): QueryBuilder
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->from(ShortUrl::class, 's');
@ -131,30 +116,25 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
return $qb;
}
/**
* @param string $shortCode
* @return ShortUrl|null
*/
public function findOneByShortCode(string $shortCode): ?ShortUrl
{
$now = Chronos::now();
$dql= <<<DQL
SELECT s
FROM Shlinkio\Shlink\Core\Entity\ShortUrl AS s
WHERE s.shortCode = :shortCode
AND (s.validSince <= :now OR s.validSince IS NULL)
AND (s.validUntil >= :now OR s.validUntil IS NULL)
DQL;
$qb = $this->createQueryBuilder('s');
$qb->where($qb->expr()->eq('s.shortCode', ':shortCode'))
->setParameter('shortCode', $shortCode)
->andWhere($qb->expr()->orX(
$qb->expr()->lte('s.validSince', ':now'),
$qb->expr()->isNull('s.validSince')
))
->andWhere($qb->expr()->orX(
$qb->expr()->gte('s.validUntil', ':now'),
$qb->expr()->isNull('s.validUntil')
))
->setParameter('now', $now)
->setMaxResults(1);
$query = $this->getEntityManager()->createQuery($dql);
$query->setMaxResults(1)
->setParameters([
'shortCode' => $shortCode,
'now' => Chronos::now(),
]);
/** @var ShortUrl|null $result */
$result = $qb->getQuery()->getOneOrNullResult();
$result = $query->getOneOrNullResult();
return $result === null || $result->maxVisitsReached() ? null : $result;
}
}

View File

@ -9,9 +9,5 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepositoryInterface
{
/**
* @param string $shortCode
* @return ShortUrl|null
*/
public function findOneByShortCode(string $shortCode);
public function findOneByShortCode(string $shortCode): ?ShortUrl;
}

View File

@ -8,12 +8,6 @@ use Shlinkio\Shlink\Core\Entity\Tag;
class TagRepository extends EntityRepository implements TagRepositoryInterface
{
/**
* Delete the tags identified by provided names
*
* @param array $names
* @return int The number of affected entries
*/
public function deleteByName(array $names): int
{
if (empty($names)) {

View File

@ -7,11 +7,5 @@ use Doctrine\Common\Persistence\ObjectRepository;
interface TagRepositoryInterface extends ObjectRepository
{
/**
* Delete the tags identified by provided names
*
* @param array $names
* @return int The number of affected entries
*/
public function deleteByName(array $names): int;
}

View File

@ -12,10 +12,10 @@ class VisitRepository extends EntityRepository implements VisitRepositoryInterfa
{
public function findUnlocatedVisits(): iterable
{
$qb = $this->createQueryBuilder('v');
$qb->where($qb->expr()->isNull('v.visitLocation'));
$dql = 'SELECT v FROM Shlinkio\Shlink\Core\Entity\Visit AS v WHERE v.visitLocation IS NULL';
$query = $this->getEntityManager()->createQuery($dql);
return $qb->getQuery()->iterate();
return $query->iterate();
}
/**