Failed Conditions
Pull Request — develop (#6873)
by
unknown
112:44 queued 47:41
created

DateAddFunction::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Query\AST\Functions;
6
7
use Doctrine\ORM\Query\Lexer;
8
use Doctrine\ORM\Query\SqlWalker;
9
use Doctrine\ORM\Query\Parser;
10
use Doctrine\ORM\Query\QueryException;
11
12
/**
13
 * "DATE_ADD" "(" ArithmeticPrimary "," ArithmeticPrimary "," StringPrimary ")"
14
 *
15
 *
16
 * @link    www.doctrine-project.org
17
 * @since   2.0
18
 * @author  Guilherme Blanco <[email protected]>
19
 * @author  Benjamin Eberlei <[email protected]>
20
 */
21
class DateAddFunction extends FunctionNode
22
{
23
    public $firstDateExpression = null;
24
    public $intervalExpression = null;
25
    public $unit = null;
26
27
    /**
28
     * @override
29
     * @inheritdoc
30
     */
31 View Code Duplication
    public function getSql(SqlWalker $sqlWalker)
32
    {
33
        switch (strtolower($this->unit->value)) {
34
            case 'second':
35
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddSecondsExpression(
36
                    $this->firstDateExpression->dispatch($sqlWalker),
37
                    $this->intervalExpression->dispatch($sqlWalker)
38
                );
39
            case 'minute':
40
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMinutesExpression(
41
                    $this->firstDateExpression->dispatch($sqlWalker),
42
                    $this->intervalExpression->dispatch($sqlWalker)
43
                );
44
            case 'hour':
45 3
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddHourExpression(
46
                    $this->firstDateExpression->dispatch($sqlWalker),
47 3
                    $this->intervalExpression->dispatch($sqlWalker)
48 3
                );
49 1
            case 'day':
50 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddDaysExpression(
51 1
                    $this->firstDateExpression->dispatch($sqlWalker),
52
                    $this->intervalExpression->dispatch($sqlWalker)
53
                );
54 2
            case 'week':
55
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddWeeksExpression(
56
                    $this->firstDateExpression->dispatch($sqlWalker),
57
                    $this->intervalExpression->dispatch($sqlWalker)
58
                );
59 2
            case 'month':
60 2
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMonthExpression(
61 2
                    $this->firstDateExpression->dispatch($sqlWalker),
62 2
                    $this->intervalExpression->dispatch($sqlWalker)
63
                );
64
            case 'year':
65 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddYearsExpression(
66 1
                    $this->firstDateExpression->dispatch($sqlWalker),
67 1
                    $this->intervalExpression->dispatch($sqlWalker)
68 1
                );
69
70
            default:
71
                throw QueryException::semanticalError(
72
                    'DATE_ADD() only supports units of type second, minute, hour, day, week, month and year.'
73
                );
74
        }
75
    }
76
77
    /**
78
     * @override
79
     * @inheritdoc
80
     */
81 4
    public function parse(Parser $parser)
82
    {
83 4
        $parser->match(Lexer::T_IDENTIFIER);
84 4
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
85
86 4
        $this->firstDateExpression = $parser->ArithmeticPrimary();
87 4
        $parser->match(Lexer::T_COMMA);
88 4
        $this->intervalExpression = $parser->ArithmeticPrimary();
89 4
        $parser->match(Lexer::T_COMMA);
90 4
        $this->unit = $parser->StringPrimary();
91
92 4
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
93 4
    }
94
}
95