diff --git a/docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md b/docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md index 479f2f94..d7ba43a7 100644 --- a/docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md +++ b/docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md @@ -63,6 +63,7 @@ | tsmatch | TSMATCH | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Tsmatch` | | unaccent | UNACCENT | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Unaccent` | | unnest | UNNEST | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Unnest` | +| overlaps | DATE_OVERLAPS | `MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\DateOverlaps` | # Bonus helpers diff --git a/docs/INTEGRATING-WITH-SYMFONY.md b/docs/INTEGRATING-WITH-SYMFONY.md index 4b6fcfef..ccae72cb 100644 --- a/docs/INTEGRATING-WITH-SYMFONY.md +++ b/docs/INTEGRATING-WITH-SYMFONY.md @@ -127,6 +127,9 @@ doctrine: TO_TSVECTOR: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ToTsvector TSMATCH: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Tsmatch + # date specific functions + DATE_OVERLAPS: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\DateOverlaps + # other operators ILIKE: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Ilike SIMILAR_TO: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\SimilarTo diff --git a/src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/DateOverlaps.php b/src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/DateOverlaps.php new file mode 100644 index 00000000..f1c64eb7 --- /dev/null +++ b/src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/DateOverlaps.php @@ -0,0 +1,25 @@ + + */ +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'); + } +} diff --git a/tests/MartinGeorgiev/Doctrine/Fixtures/Entity/ContainsDates.php b/tests/MartinGeorgiev/Doctrine/Fixtures/Entity/ContainsDates.php new file mode 100644 index 00000000..337f5847 --- /dev/null +++ b/tests/MartinGeorgiev/Doctrine/Fixtures/Entity/ContainsDates.php @@ -0,0 +1,25 @@ + 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 + ), + ]; + } +}