Skip to content

Commit

Permalink
Improve the DQL query in case the values are equals
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdz8005 committed Apr 16, 2020
1 parent da3a295 commit 2244c54
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
45 changes: 45 additions & 0 deletions features/doctrine/range_filter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,51 @@ Feature: Range filter on collections
}
"""

@createSchema
Scenario: Get collection filtered by range (between the same values)
Given there are 30 dummy objects with dummyPrice
When I send a "GET" request to "/dummies?dummyPrice[between]=12.99..12.99"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be valid according to this schema:
"""
{
"type": "object",
"properties": {
"@context": {"pattern": "^/contexts/Dummy$"},
"@id": {"pattern": "^/dummies$"},
"@type": {"pattern": "^hydra:Collection$"},
"hydra:member": {
"type": "array",
"items": {
"type": "object",
"properties": {
"@id": {
"oneOf": [
{"pattern": "^/dummies/2$"},
{"pattern": "^/dummies/6$"},
{"pattern": "^/dummies/10$"}
]
}
}
},
"minItems": 3,
"maxItems": 3,
"uniqueItems": true
},
"hydra:totalItems": {"type": "number", "minimum": 8, "maximum": 8},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/dummies\\?dummyPrice%5Bbetween%5D=12.99..12.99"},
"@type": {"pattern": "^hydra:PartialCollectionView$"}
}
}
}
}
"""

Scenario: Filter by range (between) with invalid format
When I send a "GET" request to "/dummies?dummyPrice[between]=9.99..12.99..15.99"
Then the response status code should be 200
Expand Down
5 changes: 5 additions & 0 deletions src/Bridge/Doctrine/MongoDbOdm/Filter/RangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ protected function addMatch(Builder $aggregationBuilder, string $field, string $
return;
}

if ($rangeValue[0] === $rangeValue[1]) {
$aggregationBuilder->match()->field($matchField)->equals($rangeValue[0]);
return;
}

$aggregationBuilder->match()->field($matchField)->gte($rangeValue[0])->lte($rangeValue[1]);

break;
Expand Down
7 changes: 7 additions & 0 deletions src/Bridge/Doctrine/Orm/Filter/RangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterf
return;
}

if ($rangeValue[0] === $rangeValue[1]) {
$queryBuilder
->andWhere(sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
->setParameter($valueParameter, $rangeValue[0]);
return;
}

$queryBuilder
->andWhere(sprintf('%1$s.%2$s BETWEEN :%3$s_1 AND :%3$s_2', $alias, $field, $valueParameter))
->setParameter(sprintf('%s_1', $valueParameter), $rangeValue[0])
Expand Down
8 changes: 8 additions & 0 deletions tests/Bridge/Doctrine/Common/Filter/RangeFilterTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ private function provideApplyTestArguments(): array
],
],
],
'between (same values)' => [
null,
[
'dummyPrice' => [
'between' => '9.99..9.99',
],
],
],
'between (too many operands)' => [
null,
[
Expand Down
9 changes: 9 additions & 0 deletions tests/Bridge/Doctrine/MongoDbOdm/Filter/RangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,15 @@ public function provideApplyTestData(): array
],
],
],
'between (same values)' => [
[
[
'$match' => [
'dummyPrice' => 9.99,
],
],
],
],
'between (too many operands)' => [
[],
],
Expand Down
3 changes: 3 additions & 0 deletions tests/Bridge/Doctrine/Orm/Filter/RangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ public function provideApplyTestData(): array
'between' => [
sprintf('SELECT o FROM %s o WHERE o.dummyPrice BETWEEN :dummyPrice_p1_1 AND :dummyPrice_p1_2', Dummy::class),
],
'between (same values)' => [
sprintf('SELECT o FROM %s o WHERE o.dummyPrice = :dummyPrice_p1', Dummy::class),
],
'between (too many operands)' => [
sprintf('SELECT o FROM %s o', Dummy::class),
],
Expand Down

0 comments on commit 2244c54

Please sign in to comment.