Skip to content

Commit

Permalink
Add support for OVERLAPS for date intervals (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
gallyamow authored Feb 10, 2022
1 parent e2d08cb commit 6b515bf
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/AVAILABLE-FUNCTIONS-AND-OPERATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/INTEGRATING-WITH-SYMFONY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 tests/MartinGeorgiev/Doctrine/Fixtures/Entity/ContainsDates.php
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;
}
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
),
];
}
}

0 comments on commit 6b515bf

Please sign in to comment.