Failed Conditions
Pull Request — experimental/3.1 (#2185)
by chihiro
71:30
created

Extract::getSql()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 26

Duplication

Lines 13
Ratio 40.63 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 6
nop 1
dl 13
loc 32
ccs 4
cts 5
cp 0.8
crap 4.128
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2016 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Doctrine\ORM\Query;
25
26
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
27
use Doctrine\ORM\Query\Lexer;
28
use Doctrine\ORM\Query\Parser;
29
use Doctrine\ORM\Query\SqlWalker;
30
31
/**
32
 * EXTRACT (field FROM [type] source)
33
 *  field:
34
 *      単位指定子
35
 *          - YEAR
36
 *          - MONTH
37
 *          - DAY
38
 *          - HOUR
39
 *          - MINUTE
40
 *          - SECOND
41
 *          - WEEK
42
 *  type:
43
 *      日付/時刻データ型
44
 *      sourceが文字列の場合のみ必須
45
 *          - TIMESTAMP
46
 *          - DATE
47
 *          - TIME
48
 *  source:
49
 *      日付/時刻データ型、もしくは日付/時刻を表す文字列
50
 *      文字列の場合はtypeが必須
51
 *
52
 * @package Eccube\Doctrine\ORM\Query
53
 */
54
class Extract extends FunctionNode
55
{
56
    protected $field;
57
    protected $type;
58
    protected $source;
59
60
    protected $formats = array(
61
        'YEAR'      => '%Y',
62
        'MONTH'     => '%m',
63
        'DAY'       => '%d',
64
        'HOUR'      => '%H',
65
        'MINUTE'    => '%M',
66
        'SECOND'    => '%S',
67
        'WEEK'      => '%W',
68
    );
69
70
    protected $dateTimeTypes = array(
71
        'TIMESTAMP',
72
        'DATE',
73
        'TIME',
74
    );
75
76 1
    public function parse(Parser $parser)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
77
    {
78 1
        $lexer = $parser->getLexer();
79 1
        $parser->match(Lexer::T_IDENTIFIER);
80 1
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
81
82 1
        $upperField = strtoupper($lexer->lookahead['value']);
83 1 View Code Duplication
        if ($lexer->lookahead['type'] !== Lexer::T_IDENTIFIER || !isset($this->formats[$upperField])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
            $parser->syntaxError(implode('/', array_keys($this->formats)));
85
        }
86
87 1
        $parser->match(Lexer::T_IDENTIFIER);
88 1
        $this->field = $upperField;
89 1
        $parser->match(Lexer::T_FROM);
90
91 1
        $next = $lexer->glimpse();
92 1
        if (isset($next['type']) && $next['type'] === Lexer::T_STRING) {
93
            $upperType = strtoupper($lexer->lookahead['value']);
94 View Code Duplication
            if ($lexer->lookahead['type'] !== Lexer::T_IDENTIFIER || !in_array($upperType, $this->dateTimeTypes, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
                $parser->syntaxError(implode('/', $this->dateTimeTypes));
96
            }
97
            $parser->match(Lexer::T_IDENTIFIER);
98
            $this->type = $upperType;
99
        }
100
101 1
        $this->source = $parser->ArithmeticPrimary();
102 1
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
103
    }
104
105 1
    public function getSql(SqlWalker $sqlWalker)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
106
    {
107 1
        $driver = $sqlWalker->getConnection()->getDriver()->getName();
108 1
        // UTCとの時差(秒数)
109
        $diff = intval(date('Z'));
110
        $second = abs($diff);
111 1
        $op = ($diff === $second) ? '+' : '-';
112
113
        switch ($driver) {
114
            case 'pdo_sqlite':
115
                $sql = sprintf(
116
                    "CAST(STRFTIME('%s', DATETIME(%s, '${op}{$second} SECONDS')) AS INTEGER)",
117
                    $this->formats[$this->field],
118
                    $this->source->dispatch($sqlWalker));
119
                break;
120 View Code Duplication
            case 'pdo_pgsql':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
                $sql = sprintf(
122
                    "EXTRACT(%s FROM %s %s $op INTERVAL '$second SECONDS')",
123
                    $this->field,
124
                    (string)$this->type,
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
125
                    $this->source->dispatch($sqlWalker));
126
                break;
127 View Code Duplication
            default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
                $sql = sprintf(
129
                    "EXTRACT(%s FROM %s %s $op INTERVAL $second SECOND)",
130
                    $this->field,
131
                    (string)$this->type,
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
132
                    $this->source->dispatch($sqlWalker));
133
        }
134
135
        return $sql;
136
    }
137
}