-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Distinct query modifier and CountDistinct operand (#209)
* create Distinct query modifier * create CountDistinct operand * remove not used classes
- Loading branch information
1 parent
162ff2a
commit 83d928e
Showing
6 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Happyr\DoctrineSpecification\Operand; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
|
||
class CountDistinct implements Operand | ||
{ | ||
/** | ||
* @var Operand|string | ||
*/ | ||
private $field; | ||
|
||
/** | ||
* @param Operand|string $field | ||
*/ | ||
public function __construct($field) | ||
{ | ||
$this->field = $field; | ||
} | ||
|
||
/** | ||
* @param QueryBuilder $qb | ||
* @param string $dqlAlias | ||
* | ||
* @return string | ||
*/ | ||
public function transform(QueryBuilder $qb, $dqlAlias) | ||
{ | ||
$field = ArgumentToOperandConverter::toField($this->field); | ||
$field = $field->transform($qb, $dqlAlias); | ||
|
||
return sprintf('COUNT(DISTINCT %s)', $field); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Happyr\DoctrineSpecification\Query; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
|
||
class Distinct implements QueryModifier | ||
{ | ||
/** | ||
* @param QueryBuilder $qb | ||
* @param string $dqlAlias | ||
*/ | ||
public function modify(QueryBuilder $qb, $dqlAlias) | ||
{ | ||
$qb->distinct(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace tests\Happyr\DoctrineSpecification\Operand; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
use Happyr\DoctrineSpecification\Operand\CountDistinct; | ||
use Happyr\DoctrineSpecification\Operand\Operand; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
/** | ||
* @mixin CountDistinct | ||
*/ | ||
class CountDistinctSpec extends ObjectBehavior | ||
{ | ||
private $field = 'foo'; | ||
|
||
public function let() | ||
{ | ||
$this->beConstructedWith($this->field); | ||
} | ||
|
||
public function it_is_a_count_distinct() | ||
{ | ||
$this->shouldBeAnInstanceOf(CountDistinct::class); | ||
} | ||
|
||
public function it_is_a_operand() | ||
{ | ||
$this->shouldBeAnInstanceOf(Operand::class); | ||
} | ||
|
||
public function it_is_transformable(QueryBuilder $qb) | ||
{ | ||
$this->transform($qb, 'a')->shouldReturn('COUNT(DISTINCT a.foo)'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace tests\Happyr\DoctrineSpecification\Query; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
use Happyr\DoctrineSpecification\Query\Distinct; | ||
use Happyr\DoctrineSpecification\Query\QueryModifier; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
/** | ||
* @mixin Distinct | ||
*/ | ||
class DistinctSpec extends ObjectBehavior | ||
{ | ||
public function it_is_a_distinct() | ||
{ | ||
$this->shouldBeAnInstanceOf(Distinct::class); | ||
} | ||
|
||
public function it_is_a_query_modifier() | ||
{ | ||
$this->shouldHaveType(QueryModifier::class); | ||
} | ||
|
||
public function it_add_having(QueryBuilder $qb) | ||
{ | ||
$qb->distinct()->shouldBeCalled(); | ||
$this->modify($qb, 'a'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83d928e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was literally just writing a query to count distinct - so I am very grateful for this update 👍