1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* `SET` keyword parser. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SqlParser\Components; |
8
|
|
|
|
9
|
|
|
use SqlParser\Component; |
10
|
|
|
use SqlParser\Parser; |
11
|
|
|
use SqlParser\Token; |
12
|
|
|
use SqlParser\TokensList; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* `SET` keyword parser. |
16
|
|
|
* |
17
|
|
|
* @category Keywords |
18
|
|
|
* |
19
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ |
20
|
|
|
*/ |
21
|
|
|
class SetOperation extends Component |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The name of the column that is being updated. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $column; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The new value. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $value; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Parser $parser the parser that serves as context |
39
|
|
|
* @param TokensList $list the list of tokens that are being parsed |
40
|
|
|
* @param array $options parameters for parsing |
41
|
|
|
* |
42
|
|
|
* @return SetOperation[] |
43
|
|
|
*/ |
44
|
23 |
|
public static function parse(Parser $parser, TokensList $list, array $options = array()) |
45
|
|
|
{ |
46
|
23 |
|
$ret = array(); |
47
|
|
|
|
48
|
23 |
|
$expr = new self(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The state of the parser. |
52
|
|
|
* |
53
|
|
|
* Below are the states of the parser. |
54
|
|
|
* |
55
|
|
|
* 0 -------------------[ column name ]-------------------> 1 |
56
|
|
|
* |
57
|
|
|
* 1 ------------------------[ , ]------------------------> 0 |
58
|
|
|
* 1 ----------------------[ value ]----------------------> 1 |
59
|
|
|
* |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
23 |
|
$state = 0; |
63
|
|
|
|
64
|
23 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
65
|
|
|
/** |
66
|
|
|
* Token parsed at this moment. |
67
|
|
|
* |
68
|
|
|
* @var Token |
69
|
|
|
*/ |
70
|
23 |
|
$token = $list->tokens[$list->idx]; |
71
|
|
|
|
72
|
|
|
// End of statement. |
73
|
23 |
|
if ($token->type === Token::TYPE_DELIMITER) { |
74
|
18 |
|
break; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Skipping whitespaces and comments. |
78
|
15 |
|
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
79
|
15 |
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// No keyword is expected. |
83
|
15 |
|
if (($token->type === Token::TYPE_KEYWORD) |
84
|
15 |
|
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED) |
85
|
15 |
|
&& ($state == 0) |
86
|
15 |
|
) { |
87
|
5 |
|
break; |
88
|
|
|
} |
89
|
|
|
|
90
|
15 |
|
if ($state === 0) { |
91
|
15 |
|
if ($token->token === '=') { |
92
|
15 |
|
$state = 1; |
93
|
15 |
|
} elseif ($token->value !== ',') { |
94
|
15 |
|
$expr->column .= $token->token; |
95
|
15 |
|
} |
96
|
15 |
|
} elseif ($state === 1) { |
97
|
15 |
|
$tmp = Expression::parse( |
98
|
15 |
|
$parser, |
99
|
15 |
|
$list, |
100
|
|
|
array( |
101
|
15 |
|
'breakOnAlias' => true, |
102
|
|
|
) |
103
|
15 |
|
); |
104
|
15 |
|
if ($tmp == null) { |
105
|
1 |
|
$parser->error(__('Missing expression.'), $token); |
106
|
1 |
|
break; |
107
|
|
|
} |
108
|
14 |
|
$expr->column = trim($expr->column); |
109
|
14 |
|
$expr->value = $tmp->expr; |
110
|
14 |
|
$ret[] = $expr; |
111
|
14 |
|
$expr = new self(); |
112
|
14 |
|
$state = 0; |
113
|
14 |
|
} |
114
|
15 |
|
} |
115
|
|
|
|
116
|
23 |
|
--$list->idx; |
117
|
|
|
|
118
|
23 |
|
return $ret; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param SetOperation|SetOperation[] $component the component to be built |
123
|
|
|
* @param array $options parameters for building |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
4 |
|
public static function build($component, array $options = array()) |
128
|
|
|
{ |
129
|
4 |
|
if (is_array($component)) { |
130
|
4 |
|
return implode(', ', $component); |
131
|
|
|
} else { |
132
|
3 |
|
return $component->column . ' = ' . $component->value; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|