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