Completed
Push — master ( 4509c7...06ffd8 )
by Luís
14s
created

DateSubFunction::getSql()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 9.423

Importance

Changes 0
Metric Value
cc 8
eloc 32
nc 8
nop 1
dl 0
loc 42
ccs 23
cts 32
cp 0.7188
crap 9.423
rs 5.3846
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\QueryException;
8
use Doctrine\ORM\Query\SqlWalker;
9
10
/**
11
 * "DATE_SUB(date1, interval, unit)"
12
 */
13
class DateSubFunction extends DateAddFunction
14
{
15
    /**
16
     * @override
17
     * @inheritdoc
18
     */
19 7
    public function getSql(SqlWalker $sqlWalker)
20
    {
21 7
        switch (strtolower($this->unit->value)) {
22
            case 'second':
23 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubSecondsExpression(
24 1
                    $this->firstDateExpression->dispatch($sqlWalker),
25 1
                    $this->intervalExpression->dispatch($sqlWalker)
26
                );
27
            case 'minute':
28 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubMinutesExpression(
29 1
                    $this->firstDateExpression->dispatch($sqlWalker),
30 1
                    $this->intervalExpression->dispatch($sqlWalker)
31
                );
32
            case 'hour':
33 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubHourExpression(
34 1
                    $this->firstDateExpression->dispatch($sqlWalker),
35 1
                    $this->intervalExpression->dispatch($sqlWalker)
36
                );
37
            case 'day':
38 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubDaysExpression(
39 1
                    $this->firstDateExpression->dispatch($sqlWalker),
40 1
                    $this->intervalExpression->dispatch($sqlWalker)
41
                );
42
            case 'week':
43 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubWeeksExpression(
44 1
                    $this->firstDateExpression->dispatch($sqlWalker),
45 1
                    $this->intervalExpression->dispatch($sqlWalker)
46
                );
47
            case 'month':
48 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubMonthExpression(
49 1
                    $this->firstDateExpression->dispatch($sqlWalker),
50 1
                    $this->intervalExpression->dispatch($sqlWalker)
51
                );
52
            case 'year':
53 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateSubYearsExpression(
54 1
                    $this->firstDateExpression->dispatch($sqlWalker),
55 1
                    $this->intervalExpression->dispatch($sqlWalker)
56
                );
57
58
            default:
59
                throw QueryException::semanticalError(
60
                    'DATE_SUB() only supports units of type second, minute, hour, day, week, month and year.'
61
                );
62
        }
63
    }
64
}
65