1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\ResourceBundle\Doctrine\Functions; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query\Lexer; |
6
|
|
|
use Doctrine\ORM\Query\AST\Functions\FunctionNode; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* DateFormat. |
10
|
|
|
* |
11
|
|
|
* Allows Doctrine 2.0 Query Language to execute a MySQL DATE_FORMAT function |
12
|
|
|
* You must boostrap this function in your ORM as a DQLFunction. |
13
|
|
|
* |
14
|
|
|
* |
15
|
|
|
* DATE_FORMAT(TIMESTAMP,'%format') : @link http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format |
16
|
|
|
* |
17
|
|
|
* |
18
|
|
|
* PLEASE REMEMBER TO CHECK YOUR NAMESPACE |
19
|
|
|
* |
20
|
|
|
* @link labs.ultravioletdesign.co.uk |
21
|
|
|
* |
22
|
|
|
* @author Rob Squires <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class DateFormat extends FunctionNode |
25
|
|
|
{ |
26
|
|
|
/* |
27
|
|
|
* holds the timestamp of the DATE_FORMAT DQL statement |
28
|
|
|
* @var mixed |
29
|
|
|
*/ |
30
|
|
|
protected $dateExpression; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* holds the '%format' parameter of the DATE_FORMAT DQL statement. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $formatChar; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* getSql - allows ORM to inject a DATE_FORMAT() statement into an SQL string being constructed. |
41
|
|
|
* |
42
|
|
|
* @param \Doctrine\ORM\Query\SqlWalker $sqlWalker |
43
|
|
|
*/ |
44
|
|
|
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) |
45
|
|
|
{ |
46
|
|
|
return 'DATE_FORMAT('. |
47
|
|
|
$sqlWalker->walkArithmeticExpression($this->dateExpression). |
48
|
|
|
','. |
49
|
|
|
$sqlWalker->walkStringPrimary($this->formatChar). |
50
|
|
|
')'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* parse - allows DQL to breakdown the DQL string into a processable structure. |
55
|
|
|
* |
56
|
|
|
* @param \Doctrine\ORM\Query\Parser $parser |
57
|
|
|
*/ |
58
|
|
|
public function parse(\Doctrine\ORM\Query\Parser $parser) |
59
|
|
|
{ |
60
|
|
|
$parser->match(Lexer::T_IDENTIFIER); |
61
|
|
|
$parser->match(Lexer::T_OPEN_PARENTHESIS); |
62
|
|
|
|
63
|
|
|
$this->dateExpression = $parser->ArithmeticExpression(); |
64
|
|
|
$parser->match(Lexer::T_COMMA); |
65
|
|
|
|
66
|
|
|
$this->formatChar = $parser->StringPrimary(); |
67
|
|
|
$parser->match(Lexer::T_CLOSE_PARENTHESIS); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|