-
Notifications
You must be signed in to change notification settings - Fork 430
/
Cast.php
76 lines (61 loc) · 2.27 KB
/
Cast.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace DoctrineExtensions\Query\Mysql;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\AST\Literal;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\TokenType;
use function assert;
use function implode;
use function sprintf;
/**
* "CAST" "(" "$fieldIdentifierExpression" "AS" "$castingTypeExpression" ")"
*
* @link https://dev.mysql.com/doc/refman/en/cast-functions.html#function_cast
*
* @example SELECT CAST(foo.bar AS SIGNED) FROM dual;
*/
class Cast extends FunctionNode
{
/** @var Node */
protected $fieldIdentifierExpression;
/** @var string */
protected $castingTypeExpression;
/** @throws QueryException */
public function parse(Parser $parser): void
{
$parser->match(TokenType::T_IDENTIFIER);
$parser->match(TokenType::T_OPEN_PARENTHESIS);
$this->fieldIdentifierExpression = $parser->SimpleArithmeticExpression();
$parser->match(TokenType::T_AS);
$parser->match(TokenType::T_IDENTIFIER);
$type = $parser->getLexer()->token->value;
if ($parser->getLexer()->isNextToken(TokenType::T_OPEN_PARENTHESIS)) {
$parser->match(TokenType::T_OPEN_PARENTHESIS);
$parameter = $parser->Literal();
assert($parameter instanceof Literal);
$parameters = [$parameter->value];
if ($parser->getLexer()->isNextToken(TokenType::T_COMMA)) {
while ($parser->getLexer()->isNextToken(TokenType::T_COMMA)) {
$parser->match(TokenType::T_COMMA);
$parameter = $parser->Literal();
$parameters[] = $parameter->value;
}
}
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
$type .= '(' . implode(', ', $parameters) . ')';
}
$this->castingTypeExpression = $type;
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker): string
{
return sprintf(
'CAST(%s AS %s)',
$sqlWalker->walkSimpleArithmeticExpression($this->fieldIdentifierExpression),
$this->castingTypeExpression
);
}
}