Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сhange CountDistinct to Count platform function #285

Merged
merged 1 commit into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,41 @@
* The `Satisfiable` interface was added.
* The `Specification` interface was extends `Satisfiable` interface.
* The `BaseSpecification` class implement `Satisfiable` interface.
* The `Happyr\DoctrineSpecification\Operand\CountDistinct` class was removed, use
`Happyr\DoctrineSpecification\Operand\PlatformFunction\Count` instead.
* The `Spec::countDistinct()` method was removed, use `Spec::COUNT()` instead.

Before:

```php
new CountDistinct('field_name');
Spec::countDistinct('field_name');
```

After:

```php
new Count('field_name', true);
Spec::COUNT('field_name', true);
```

* The `COUNT` function as argument to `Spec::fun()` is not longer supported, use `Spec::COUNT()` instead.
* The `COUNT` function as argument to `Happyr\DoctrineSpecification\Operand\PlatformFunction` is not longer supported,
use `Spec::COUNT()` instead.

Before:

```php
new PlatformFunction('COUNT', 'field_name');
Spec::fun('COUNT', 'field_name');
```

After:

```php
new Count('field_name');
Spec::COUNT('field_name');
```

# Upgrade from 1.0 to 1.1

Expand Down
2 changes: 0 additions & 2 deletions src/Operand/PlatformFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\BitAndExecutor;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\BitOrExecutor;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\ConcatExecutor;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CountExecutor;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CurrentDateExecutor;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CurrentTimeExecutor;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Executor\CurrentTimestampExecutor;
Expand Down Expand Up @@ -72,7 +71,6 @@ final class PlatformFunction implements Operand
'MAX' => MaxExecutor::class,
'AVG' => AvgExecutor::class,
'SUM' => SumExecutor::class,
'COUNT' => CountExecutor::class,
// Datetime functions
'CURRENT_DATE' => CurrentDateExecutor::class,
'CURRENT_TIME' => CurrentTimeExecutor::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,33 @@
* file that was distributed with this source code.
*/

namespace Happyr\DoctrineSpecification\Operand;
namespace Happyr\DoctrineSpecification\Operand\PlatformFunction;

use Doctrine\ORM\QueryBuilder;
use Happyr\DoctrineSpecification\Exception\OperandNotExecuteException;
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter;
use Happyr\DoctrineSpecification\Operand\Operand;

final class CountDistinct implements Operand
final class Count implements Operand
{
/**
* @var Operand|string
*/
private $field;

/**
* @var bool
*/
private $distinct;

/**
* @param Operand|string $field
* @param bool $distinct
*/
public function __construct($field)
public function __construct($field, bool $distinct = false)
{
$this->field = $field;
$this->distinct = $distinct;
}

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

return sprintf('COUNT(DISTINCT %s)', $field);
$expression = '';

if ($this->distinct) {
$expression = 'DISTINCT ';
}

return sprintf('COUNT(%s%s)', $expression, $field);
}

/**
Expand Down
30 changes: 0 additions & 30 deletions src/Operand/PlatformFunction/Executor/CountExecutor.php

This file was deleted.

36 changes: 18 additions & 18 deletions src/Spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
use Happyr\DoctrineSpecification\Logic\OrX;
use Happyr\DoctrineSpecification\Operand\Addition;
use Happyr\DoctrineSpecification\Operand\Alias;
use Happyr\DoctrineSpecification\Operand\CountDistinct;
use Happyr\DoctrineSpecification\Operand\Division;
use Happyr\DoctrineSpecification\Operand\Field;
use Happyr\DoctrineSpecification\Operand\LikePattern;
use Happyr\DoctrineSpecification\Operand\Multiplication;
use Happyr\DoctrineSpecification\Operand\Operand;
use Happyr\DoctrineSpecification\Operand\PlatformFunction;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Count;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\DateAdd;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\DateSub;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Trim;
Expand Down Expand Up @@ -91,7 +91,6 @@
* @method static PlatformFunction MAX($a)
* @method static PlatformFunction AVG($a)
* @method static PlatformFunction SUM($a)
* @method static PlatformFunction COUNT($a)
* @method static PlatformFunction CURRENT_DATE() Return the current date
* @method static PlatformFunction CURRENT_TIME() Returns the current time
* @method static PlatformFunction CURRENT_TIMESTAMP() Returns a timestamp of the current date and time.
Expand Down Expand Up @@ -578,16 +577,6 @@ public static function likePattern(string $value, string $format = LikePattern::
return new LikePattern($value, $format);
}

/**
* @param Operand|string $field
*
* @return CountDistinct
*/
public static function countDistinct($field): CountDistinct
{
return new CountDistinct($field);
}

// Arithmetic operands

/**
Expand Down Expand Up @@ -637,15 +626,14 @@ public static function div($field, $value): Division
// Platform functions

/**
* Trim the string by the given trim char, defaults to whitespaces.
* @param Operand|string $field
* @param bool $distinct
*
* @param Operand|string $string
* @param string $mode
* @param string $characters
* @return Count
*/
public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim
public static function COUNT($field, bool $distinct = false): Count
{
return new Trim($string, $mode, $characters);
return new Count($field, $distinct);
}

/**
Expand Down Expand Up @@ -676,6 +664,18 @@ public static function DATE_SUB($date, $value, string $unit): DateSub
return new DateSub($date, $value, $unit);
}

/**
* Trim the string by the given trim char, defaults to whitespaces.
*
* @param Operand|string $string
* @param string $mode
* @param string $characters
*/
public static function TRIM($string, string $mode = Trim::BOTH, string $characters = ''): Trim
{
return new Trim($string, $mode, $characters);
}

/**
* Call DQL function.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
* file that was distributed with this source code.
*/

namespace tests\Happyr\DoctrineSpecification\Operand;
namespace tests\Happyr\DoctrineSpecification\Operand\PlatformFunction;

use Doctrine\ORM\QueryBuilder;
use Happyr\DoctrineSpecification\Exception\OperandNotExecuteException;
use Happyr\DoctrineSpecification\Operand\CountDistinct;
use Happyr\DoctrineSpecification\Operand\Operand;
use Happyr\DoctrineSpecification\Operand\PlatformFunction\Count;
use PhpSpec\ObjectBehavior;

/**
* @mixin CountDistinct
* @mixin Count
*/
final class CountDistinctSpec extends ObjectBehavior
final class CountSpec extends ObjectBehavior
{
private $field = 'foo';

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

public function it_is_a_count_distinct(): void
{
$this->shouldBeAnInstanceOf(CountDistinct::class);
$this->shouldBeAnInstanceOf(Count::class);
}

public function it_is_a_operand(): void
Expand All @@ -44,6 +44,13 @@ public function it_is_a_operand(): void

public function it_is_transformable(QueryBuilder $qb): void
{
$this->transform($qb, 'a')->shouldReturn('COUNT(a.foo)');
}

public function it_is_transformable_distinct(QueryBuilder $qb): void
{
$this->beConstructedWith($this->field, true);

$this->transform($qb, 'a')->shouldReturn('COUNT(DISTINCT a.foo)');
}

Expand Down
30 changes: 0 additions & 30 deletions tests/Operand/PlatformFunction/Executor/CountExecutorSpec.php

This file was deleted.

6 changes: 0 additions & 6 deletions tests/SpecSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Happyr\DoctrineSpecification\Logic\LogicX;
use Happyr\DoctrineSpecification\Operand\Addition;
use Happyr\DoctrineSpecification\Operand\Alias;
use Happyr\DoctrineSpecification\Operand\CountDistinct;
use Happyr\DoctrineSpecification\Operand\Division;
use Happyr\DoctrineSpecification\Operand\Multiplication;
use Happyr\DoctrineSpecification\Operand\PlatformFunction;
Expand Down Expand Up @@ -47,11 +46,6 @@ public function it_creates_distinct(): void
$this->distinct()->shouldReturnAnInstanceOf(Distinct::class);
}

public function it_creates_count_distinct(): void
{
$this->countDistinct('foo')->shouldReturnAnInstanceOf(CountDistinct::class);
}

public function it_creates_add_operand(): void
{
$this->add('foo', 'bar')->shouldReturnAnInstanceOf(Addition::class);
Expand Down