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
|
|
|
|