mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
96 lines
2.6 KiB
PHP
96 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\MainBundle\Doctrine\DQL;
|
|
|
|
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
|
|
use Doctrine\ORM\Query\AST\PathExpression;
|
|
use Doctrine\ORM\Query\Lexer;
|
|
use Doctrine\ORM\Query\Parser;
|
|
|
|
/**
|
|
* DQL function for OVERLAPS function in postgresql
|
|
*
|
|
* If a value is null in period start, it will be replaced by -infinity.
|
|
* If a value is null in period end, it will be replaced by infinity
|
|
*/
|
|
class OverlapsI extends FunctionNode
|
|
{
|
|
private $firstPeriodStart;
|
|
|
|
private $firstPeriodEnd;
|
|
|
|
private $secondPeriodStart;
|
|
|
|
private $secondPeriodEnd;
|
|
|
|
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
|
|
{
|
|
return sprintf(
|
|
'(%s, %s) OVERLAPS (%s, %s)',
|
|
$this->makeCase($sqlWalker, $this->firstPeriodStart, 'start'),
|
|
$this->makeCase($sqlWalker, $this->firstPeriodEnd, 'end'),
|
|
$this->makeCase($sqlWalker, $this->secondPeriodStart, 'start'),
|
|
$this->makeCase($sqlWalker, $this->secondPeriodEnd, 'end')
|
|
);
|
|
}
|
|
|
|
protected function makeCase($sqlWalker, $part, string $position): string
|
|
{
|
|
switch ($position) {
|
|
case 'start' :
|
|
$p = '-infinity';
|
|
break;
|
|
case 'end':
|
|
$p = 'infinity';
|
|
break;
|
|
default:
|
|
throw new \Exception('Unexpected position value.');
|
|
}
|
|
|
|
if ($part instanceof PathExpression) {
|
|
return sprintf(
|
|
"CASE WHEN %s IS NOT NULL THEN %s ELSE '%s'::date END",
|
|
$part->dispatch($sqlWalker),
|
|
$part->dispatch($sqlWalker),
|
|
$p
|
|
);
|
|
}
|
|
|
|
return sprintf(
|
|
"CASE WHEN %s::date IS NOT NULL THEN %s::date ELSE '%s'::date END",
|
|
$part->dispatch($sqlWalker),
|
|
$part->dispatch($sqlWalker),
|
|
$p
|
|
);
|
|
}
|
|
|
|
public function parse(Parser $parser): void
|
|
{
|
|
$parser->match(Lexer::T_IDENTIFIER);
|
|
|
|
$parser->match(Lexer::T_OPEN_PARENTHESIS);
|
|
|
|
$this->firstPeriodStart = $parser->StringPrimary();
|
|
|
|
$parser->match(Lexer::T_COMMA);
|
|
|
|
$this->firstPeriodEnd = $parser->StringPrimary();
|
|
|
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
|
|
|
|
$parser->match(Lexer::T_COMMA);
|
|
|
|
$parser->match(Lexer::T_OPEN_PARENTHESIS);
|
|
|
|
$this->secondPeriodStart = $parser->StringPrimary();
|
|
|
|
$parser->match(Lexer::T_COMMA);
|
|
|
|
$this->secondPeriodEnd = $parser->StringPrimary();
|
|
|
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
|
|
}
|
|
}
|