|
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
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
3 |
View Code Duplication |
public function getSql(SqlWalker $sqlWalker) |
|
47
|
|
|
{ |
|
48
|
3 |
|
switch (strtolower($this->unit->value)) { |
|
49
|
3 |
|
case 'second': |
|
50
|
1 |
|
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddSecondsExpression( |
|
51
|
1 |
|
$this->firstDateExpression->dispatch($sqlWalker), |
|
52
|
1 |
|
$this->intervalExpression->dispatch($sqlWalker) |
|
53
|
|
|
); |
|
54
|
|
|
case 'minute': |
|
55
|
2 |
|
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMinutesExpression( |
|
56
|
|
|
$this->firstDateExpression->dispatch($sqlWalker), |
|
57
|
|
|
$this->intervalExpression->dispatch($sqlWalker) |
|
58
|
|
|
); |
|
59
|
|
|
case 'hour': |
|
60
|
2 |
|
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddHourExpression( |
|
61
|
2 |
|
$this->firstDateExpression->dispatch($sqlWalker), |
|
62
|
2 |
|
$this->intervalExpression->dispatch($sqlWalker) |
|
63
|
2 |
|
); |
|
64
|
|
|
case 'day': |
|
65
|
|
|
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddDaysExpression( |
|
66
|
1 |
|
$this->firstDateExpression->dispatch($sqlWalker), |
|
67
|
1 |
|
$this->intervalExpression->dispatch($sqlWalker) |
|
68
|
1 |
|
); |
|
69
|
1 |
|
case 'week': |
|
70
|
|
|
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddWeeksExpression( |
|
71
|
|
|
$this->firstDateExpression->dispatch($sqlWalker), |
|
72
|
|
|
$this->intervalExpression->dispatch($sqlWalker) |
|
73
|
|
|
); |
|
74
|
|
|
case 'month': |
|
75
|
|
|
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMonthExpression( |
|
76
|
|
|
$this->firstDateExpression->dispatch($sqlWalker), |
|
77
|
|
|
$this->intervalExpression->dispatch($sqlWalker) |
|
78
|
|
|
); |
|
79
|
|
|
case 'year': |
|
80
|
|
|
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddYearsExpression( |
|
81
|
|
|
$this->firstDateExpression->dispatch($sqlWalker), |
|
82
|
|
|
$this->intervalExpression->dispatch($sqlWalker) |
|
83
|
4 |
|
); |
|
84
|
|
|
|
|
85
|
4 |
|
default: |
|
86
|
4 |
|
throw QueryException::semanticalError( |
|
87
|
|
|
'DATE_ADD() only supports units of type second, minute, hour, day, week, month and year.' |
|
88
|
4 |
|
); |
|
89
|
4 |
|
} |
|
90
|
4 |
|
} |
|
91
|
4 |
|
|
|
92
|
4 |
|
/** |
|
93
|
|
|
* @override |
|
94
|
4 |
|
* @inheritdoc |
|
95
|
4 |
|
*/ |
|
96
|
|
|
public function parse(Parser $parser) |
|
97
|
|
|
{ |
|
98
|
|
|
$parser->match(Lexer::T_IDENTIFIER); |
|
99
|
|
|
$parser->match(Lexer::T_OPEN_PARENTHESIS); |
|
100
|
|
|
|
|
101
|
|
|
$this->firstDateExpression = $parser->ArithmeticPrimary(); |
|
102
|
|
|
$parser->match(Lexer::T_COMMA); |
|
103
|
|
|
$this->intervalExpression = $parser->ArithmeticPrimary(); |
|
104
|
|
|
$parser->match(Lexer::T_COMMA); |
|
105
|
|
|
$this->unit = $parser->StringPrimary(); |
|
106
|
|
|
|
|
107
|
|
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|