Completed
Pull Request — master (#5713)
by Ondřej
14:07
created

DateAddFunction::getSql()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.3906
Metric Value
dl 0
loc 32
ccs 15
cts 20
cp 0.75
rs 8.439
cc 5
eloc 21
nc 5
nop 1
crap 5.3906
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Query\AST\Functions;
21
22
use Doctrine\ORM\Query\Lexer;
23
use Doctrine\ORM\Query\SqlWalker;
24
use Doctrine\ORM\Query\Parser;
25
use Doctrine\ORM\Query\QueryException;
26
27
/**
28
 * "DATE_ADD" "(" ArithmeticPrimary "," ArithmeticPrimary "," StringPrimary ")"
29
 *
30
 *
31
 * @link    www.doctrine-project.org
32
 * @since   2.0
33
 * @author  Guilherme Blanco <[email protected]>
34
 * @author  Benjamin Eberlei <[email protected]>
35
 */
36
class DateAddFunction extends FunctionNode
37
{
38
    public $firstDateExpression = null;
39
    public $intervalExpression = null;
40
    public $unit = null;
41
42
    /**
43
     * @override
44
     */
45 3
    public function getSql(SqlWalker $sqlWalker)
46
    {
47 3
        switch (strtolower($this->unit->value)) {
48 3
            case 'second':
49 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddSecondsExpression(
50 1
                    $this->firstDateExpression->dispatch($sqlWalker),
51 1
                    $this->intervalExpression->dispatch($sqlWalker)
52
                );
53
54 2
            case 'hour':
55
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddHourExpression(
56
                    $this->firstDateExpression->dispatch($sqlWalker),
57
                    $this->intervalExpression->dispatch($sqlWalker)
58
                );
59 2
            case 'day':
60 2
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddDaysExpression(
61 2
                    $this->firstDateExpression->dispatch($sqlWalker),
62 2
                    $this->intervalExpression->dispatch($sqlWalker)
63
                );
64
65 1
            case 'month':
66 1
                return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMonthExpression(
67 1
                    $this->firstDateExpression->dispatch($sqlWalker),
68 1
                    $this->intervalExpression->dispatch($sqlWalker)
69
                );
70
71
            default:
72
                throw QueryException::semanticalError(
73
                    'DATE_ADD() only supports units of type second, hour, day and month.'
74
                );
75
        }
76
    }
77
78
    /**
79
     * @override
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