Skip to content

Commit a132cc1

Browse files
change CountDistinct to Count platform function
1 parent e3b060e commit a132cc1

File tree

8 files changed

+84
-95
lines changed

8 files changed

+84
-95
lines changed

UPGRADE.md

+35
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,41 @@
255255
* The `Satisfiable` interface was added.
256256
* The `Specification` interface was extends `Satisfiable` interface.
257257
* The `BaseSpecification` class implement `Satisfiable` interface.
258+
* The `Happyr\DoctrineSpecification\Operand\CountDistinct` class was removed, use
259+
`Happyr\DoctrineSpecification\Operand\PlatformFunction\Count` instead.
260+
* The `Spec::countDistinct()` method was removed, use `Spec::COUNT()` instead.
261+
262+
Before:
263+
264+
```php
265+
new CountDistinct('field_name');
266+
Spec::countDistinct('field_name');
267+
```
268+
269+
After:
270+
271+
```php
272+
new Count('field_name', true);
273+
Spec::COUNT('field_name', true);
274+
```
275+
276+
* The `COUNT` function as argument to `Spec::fun()` is not longer supported, use `Spec::COUNT()` instead.
277+
* The `COUNT` function as argument to `Happyr\DoctrineSpecification\Operand\PlatformFunction` is not longer supported,
278+
use `Spec::COUNT()` instead.
279+
280+
Before:
281+
282+
```php
283+
new PlatformFunction('COUNT', 'field_name');
284+
Spec::fun('COUNT', 'field_name');
285+
```
286+
287+
After:
288+
289+
```php
290+
new Count('field_name');
291+
Spec::COUNT('field_name');
292+
```
258293

259294
# Upgrade from 1.0 to 1.1
260295

src/Operand/PlatformFunction.php

-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\BitAndExecutor;
2323
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\BitOrExecutor;
2424
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\ConcatExecutor;
25-
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CountExecutor;
2625
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CurrentDateExecutor;
2726
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CurrentTimeExecutor;
2827
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CurrentTimestampExecutor;
@@ -72,7 +71,6 @@ final class PlatformFunction implements Operand
7271
'MAX' => MaxExecutor::class,
7372
'AVG' => AvgExecutor::class,
7473
'SUM' => SumExecutor::class,
75-
'COUNT' => CountExecutor::class,
7674
// Datetime functions
7775
'CURRENT_DATE' => CurrentDateExecutor::class,
7876
'CURRENT_TIME' => CurrentTimeExecutor::class,

src/Operand/CountDistinct.php renamed to src/Operand/PlatformFunction/Count.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,33 @@
1212
* file that was distributed with this source code.
1313
*/
1414

15-
namespace Happyr\DoctrineSpecification\Operand;
15+
namespace Happyr\DoctrineSpecification\Operand\PlatformFunction;
1616

1717
use Doctrine\ORM\QueryBuilder;
1818
use Happyr\DoctrineSpecification\Exception\OperandNotExecuteException;
19+
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter;
20+
use Happyr\DoctrineSpecification\Operand\Operand;
1921

20-
final class CountDistinct implements Operand
22+
final class Count implements Operand
2123
{
2224
/**
2325
* @var Operand|string
2426
*/
2527
private $field;
2628

29+
/**
30+
* @var bool
31+
*/
32+
private $distinct;
33+
2734
/**
2835
* @param Operand|string $field
36+
* @param bool $distinct
2937
*/
30-
public function __construct($field)
38+
public function __construct($field, bool $distinct = false)
3139
{
3240
$this->field = $field;
41+
$this->distinct = $distinct;
3342
}
3443

3544
/**
@@ -43,7 +52,13 @@ public function transform(QueryBuilder $qb, string $context): string
4352
$field = ArgumentToOperandConverter::toField($this->field);
4453
$field = $field->transform($qb, $context);
4554

46-
return sprintf('COUNT(DISTINCT %s)', $field);
55+
$expression = '';
56+
57+
if ($this->distinct) {
58+
$expression = 'DISTINCT ';
59+
}
60+
61+
return sprintf('COUNT(%s%s)', $expression, $field);
4762
}
4863

4964
/**

src/Operand/PlatformFunction/Executor/CountExecutor.php

-30
This file was deleted.

src/Spec.php

+18-18
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
use Happyr\DoctrineSpecification\Logic\OrX;
3333
use Happyr\DoctrineSpecification\Operand\Addition;
3434
use Happyr\DoctrineSpecification\Operand\Alias;
35-
use Happyr\DoctrineSpecification\Operand\CountDistinct;
3635
use Happyr\DoctrineSpecification\Operand\Division;
3736
use Happyr\DoctrineSpecification\Operand\Field;
3837
use Happyr\DoctrineSpecification\Operand\LikePattern;
3938
use Happyr\DoctrineSpecification\Operand\Multiplication;
4039
use Happyr\DoctrineSpecification\Operand\Operand;
4140
use Happyr\DoctrineSpecification\Operand\PlatformFunction;
41+
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Count;
4242
use Happyr\DoctrineSpecification\Operand\PlatformFunction\DateAdd;
4343
use Happyr\DoctrineSpecification\Operand\PlatformFunction\DateSub;
4444
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Trim;
@@ -91,7 +91,6 @@
9191
* @method static PlatformFunction MAX($a)
9292
* @method static PlatformFunction AVG($a)
9393
* @method static PlatformFunction SUM($a)
94-
* @method static PlatformFunction COUNT($a)
9594
* @method static PlatformFunction CURRENT_DATE() Return the current date
9695
* @method static PlatformFunction CURRENT_TIME() Returns the current time
9796
* @method static PlatformFunction CURRENT_TIMESTAMP() Returns a timestamp of the current date and time.
@@ -578,16 +577,6 @@ public static function likePattern(string $value, string $format = LikePattern::
578577
return new LikePattern($value, $format);
579578
}
580579

581-
/**
582-
* @param Operand|string $field
583-
*
584-
* @return CountDistinct
585-
*/
586-
public static function countDistinct($field): CountDistinct
587-
{
588-
return new CountDistinct($field);
589-
}
590-
591580
// Arithmetic operands
592581

593582
/**
@@ -637,15 +626,14 @@ public static function div($field, $value): Division
637626
// Platform functions
638627

639628
/**
640-
* Trim the string by the given trim char, defaults to whitespaces.
629+
* @param Operand|string $field
630+
* @param bool $distinct
641631
*
642-
* @param Operand|string $string
643-
* @param string $mode
644-
* @param string $characters
632+
* @return Count
645633
*/
646-
public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim
634+
public static function COUNT($field, bool $distinct = false): Count
647635
{
648-
return new Trim($string, $mode, $characters);
636+
return new Count($field, $distinct);
649637
}
650638

651639
/**
@@ -676,6 +664,18 @@ public static function DATE_SUB($date, $value, string $unit): DateSub
676664
return new DateSub($date, $value, $unit);
677665
}
678666

667+
/**
668+
* Trim the string by the given trim char, defaults to whitespaces.
669+
*
670+
* @param Operand|string $string
671+
* @param string $mode
672+
* @param string $characters
673+
*/
674+
public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim
675+
{
676+
return new Trim($string, $mode, $characters);
677+
}
678+
679679
/**
680680
* Call DQL function.
681681
*

tests/Operand/CountDistinctSpec.php renamed to tests/Operand/PlatformFunction/CountSpec.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
* file that was distributed with this source code.
1313
*/
1414

15-
namespace tests\Happyr\DoctrineSpecification\Operand;
15+
namespace tests\Happyr\DoctrineSpecification\Operand\PlatformFunction;
1616

1717
use Doctrine\ORM\QueryBuilder;
1818
use Happyr\DoctrineSpecification\Exception\OperandNotExecuteException;
19-
use Happyr\DoctrineSpecification\Operand\CountDistinct;
2019
use Happyr\DoctrineSpecification\Operand\Operand;
20+
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Count;
2121
use PhpSpec\ObjectBehavior;
2222

2323
/**
24-
* @mixin CountDistinct
24+
* @mixin Count
2525
*/
26-
final class CountDistinctSpec extends ObjectBehavior
26+
final class CountSpec extends ObjectBehavior
2727
{
2828
private $field = 'foo';
2929

@@ -34,7 +34,7 @@ public function let(): void
3434

3535
public function it_is_a_count_distinct(): void
3636
{
37-
$this->shouldBeAnInstanceOf(CountDistinct::class);
37+
$this->shouldBeAnInstanceOf(Count::class);
3838
}
3939

4040
public function it_is_a_operand(): void
@@ -44,6 +44,13 @@ public function it_is_a_operand(): void
4444

4545
public function it_is_transformable(QueryBuilder $qb): void
4646
{
47+
$this->transform($qb, 'a')->shouldReturn('COUNT(a.foo)');
48+
}
49+
50+
public function it_is_transformable_distinct(QueryBuilder $qb): void
51+
{
52+
$this->beConstructedWith($this->field, true);
53+
4754
$this->transform($qb, 'a')->shouldReturn('COUNT(DISTINCT a.foo)');
4855
}
4956

tests/Operand/PlatformFunction/Executor/CountExecutorSpec.php

-30
This file was deleted.

tests/SpecSpec.php

-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Happyr\DoctrineSpecification\Logic\LogicX;
1919
use Happyr\DoctrineSpecification\Operand\Addition;
2020
use Happyr\DoctrineSpecification\Operand\Alias;
21-
use Happyr\DoctrineSpecification\Operand\CountDistinct;
2221
use Happyr\DoctrineSpecification\Operand\Division;
2322
use Happyr\DoctrineSpecification\Operand\Multiplication;
2423
use Happyr\DoctrineSpecification\Operand\PlatformFunction;
@@ -47,11 +46,6 @@ public function it_creates_distinct(): void
4746
$this->distinct()->shouldReturnAnInstanceOf(Distinct::class);
4847
}
4948

50-
public function it_creates_count_distinct(): void
51-
{
52-
$this->countDistinct('foo')->shouldReturnAnInstanceOf(CountDistinct::class);
53-
}
54-
5549
public function it_creates_add_operand(): void
5650
{
5751
$this->add('foo', 'bar')->shouldReturnAnInstanceOf(Addition::class);

0 commit comments

Comments
 (0)