-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for OVERLAPS for date intervals (#105)
- Loading branch information
Showing
5 changed files
with
94 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
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
25 changes: 25 additions & 0 deletions
25
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/DateOverlaps.php
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
||
/** | ||
* Implementation of PostgreSql check if left date interval overlaps with right interval. | ||
* | ||
* @see https://www.postgresql.org/docs/9.6/functions-datetime.html | ||
* @since 1.7.0 | ||
* | ||
* @author Ramil Gallyamov <[email protected]> | ||
*/ | ||
class DateOverlaps extends BaseFunction | ||
{ | ||
protected function customiseFunction(): void | ||
{ | ||
$this->setFunctionPrototype('(%s, %s) OVERLAPS (%s, %s)'); | ||
$this->addNodeMapping('StringExpression'); | ||
$this->addNodeMapping('StringExpression'); | ||
$this->addNodeMapping('StringExpression'); | ||
$this->addNodeMapping('StringExpression'); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/MartinGeorgiev/Doctrine/Fixtures/Entity/ContainsDates.php
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\MartinGeorgiev\Doctrine\Fixtures\Entity; | ||
|
||
/** | ||
* @Entity | ||
*/ | ||
class ContainsDates extends Entity | ||
{ | ||
/** | ||
* @var \DateTimeImmutable | ||
* | ||
* @Column(type="date_immutable") | ||
*/ | ||
public $date1; | ||
|
||
/** | ||
* @var \DateTimeImmutable | ||
* | ||
* @Column(type="date_immutable") | ||
*/ | ||
public $date2; | ||
} |
40 changes: 40 additions & 0 deletions
40
tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/DateOverlapsTest.php
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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
||
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\DateOverlaps; | ||
use Tests\MartinGeorgiev\Doctrine\Fixtures\Entity\ContainsDates; | ||
|
||
class DateOverlapsTest extends TestCase | ||
{ | ||
protected function getStringFunctions(): array | ||
{ | ||
return [ | ||
'DATE_OVERLAPS' => DateOverlaps::class, | ||
]; | ||
} | ||
|
||
protected function getExpectedSqlStatements(): array | ||
{ | ||
return [ | ||
"SELECT c0_.date1 AS date1_0 FROM ContainsDates c0_ WHERE (c0_.date1, c0_.date2) OVERLAPS ('2001-12-21', '2001-12-25') = 1", | ||
"SELECT c0_.date1 AS date1_0 FROM ContainsDates c0_ WHERE (c0_.date1, COALESCE(c0_.date2, CURRENT_DATE)) OVERLAPS ('2001-12-21', '2001-12-25') = 1", | ||
]; | ||
} | ||
|
||
protected function getDqlStatements(): array | ||
{ | ||
return [ | ||
\sprintf( | ||
"SELECT e.date1 FROM %s e WHERE DATE_OVERLAPS(e.date1, e.date2, '2001-12-21', '2001-12-25')=TRUE", | ||
ContainsDates::class | ||
), | ||
\sprintf( | ||
"SELECT e.date1 FROM %s e WHERE DATE_OVERLAPS(e.date1, COALESCE(e.date2, CURRENT_DATE()), '2001-12-21', '2001-12-25')=TRUE", | ||
ContainsDates::class | ||
), | ||
]; | ||
} | ||
} |