1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* `JOIN` keyword parser. |
5
|
|
|
* |
6
|
|
|
* @package SqlParser |
7
|
|
|
* @subpackage Components |
8
|
|
|
*/ |
9
|
|
|
namespace SqlParser\Components; |
10
|
|
|
|
11
|
|
|
use SqlParser\Component; |
12
|
|
|
use SqlParser\Parser; |
13
|
|
|
use SqlParser\Token; |
14
|
|
|
use SqlParser\TokensList; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* `JOIN` keyword parser. |
18
|
|
|
* |
19
|
|
|
* @category Keywords |
20
|
|
|
* @package SqlParser |
21
|
|
|
* @subpackage Components |
22
|
|
|
* @author Dan Ungureanu <[email protected]> |
23
|
|
|
* @license http://opensource.org/licenses/GPL-2.0 GNU Public License |
24
|
|
|
*/ |
25
|
|
|
class JoinKeyword extends Component |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Types of join. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
public static $JOINS = array( |
34
|
|
|
'FULL JOIN' => 'FULL', |
35
|
|
|
'INNER JOIN' => 'INNER', |
36
|
|
|
'JOIN' => 'JOIN', |
37
|
|
|
'LEFT JOIN' => 'LEFT', |
38
|
|
|
'LEFT OUTER JOIN' => 'LEFT', |
39
|
|
|
'RIGHT JOIN' => 'RIGHT', |
40
|
|
|
'RIGHT OUTER JOIN' => 'RIGHT', |
41
|
|
|
'STRAIGHT_JOIN' => 'STRAIGHT', |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Type of this join. |
46
|
|
|
* |
47
|
|
|
* @see static::$JOINS |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
public $type; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Join expression. |
54
|
|
|
* |
55
|
|
|
* @var Expression |
56
|
|
|
*/ |
57
|
|
|
public $expr; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Join conditions. |
61
|
|
|
* |
62
|
|
|
* @var Condition[] |
63
|
|
|
*/ |
64
|
|
|
public $on; |
65
|
|
|
/** |
66
|
|
|
* Columns in Using clause |
67
|
|
|
* |
68
|
|
|
* @var ArrayObj |
69
|
|
|
*/ |
70
|
|
|
public $using; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Parser $parser The parser that serves as context. |
74
|
|
|
* @param TokensList $list The list of tokens that are being parsed. |
75
|
|
|
* @param array $options Parameters for parsing. |
76
|
|
|
* |
77
|
|
|
* @return JoinKeyword[] |
78
|
|
|
*/ |
79
|
6 |
|
public static function parse(Parser $parser, TokensList $list, array $options = array()) |
80
|
|
|
{ |
81
|
6 |
|
$ret = array(); |
82
|
|
|
|
83
|
6 |
|
$expr = new JoinKeyword(); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The state of the parser. |
87
|
|
|
* |
88
|
|
|
* Below are the states of the parser. |
89
|
|
|
* |
90
|
|
|
* 0 -----------------------[ JOIN ]----------------------> 1 |
91
|
|
|
* |
92
|
|
|
* 1 -----------------------[ expr ]----------------------> 2 |
93
|
|
|
* |
94
|
|
|
* 2 ------------------------[ ON ]-----------------------> 3 |
95
|
|
|
* 2 -----------------------[ USING ]---------------------> 4 |
96
|
|
|
* |
97
|
|
|
* 3 --------------------[ conditions ]-------------------> 0 |
98
|
|
|
* |
99
|
|
|
* 4 ----------------------[ columns ]--------------------> 0 |
100
|
|
|
* |
101
|
|
|
* @var int $state |
102
|
|
|
*/ |
103
|
6 |
|
$state = 0; |
104
|
|
|
|
105
|
|
|
// By design, the parser will parse first token after the keyword. |
106
|
|
|
// In this case, the keyword must be analyzed too, in order to determine |
107
|
|
|
// the type of this join. |
108
|
6 |
|
if ($list->idx > 0) { |
109
|
4 |
|
--$list->idx; |
110
|
4 |
|
} |
111
|
|
|
|
112
|
6 |
|
for (; $list->idx < $list->count; ++$list->idx) { |
113
|
|
|
/** |
114
|
|
|
* Token parsed at this moment. |
115
|
|
|
* |
116
|
|
|
* @var Token $token |
117
|
|
|
*/ |
118
|
6 |
|
$token = $list->tokens[$list->idx]; |
119
|
|
|
|
120
|
|
|
// End of statement. |
121
|
6 |
|
if ($token->type === Token::TYPE_DELIMITER) { |
122
|
5 |
|
break; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Skipping whitespaces and comments. |
126
|
6 |
|
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) { |
127
|
6 |
|
continue; |
128
|
|
|
} |
129
|
|
|
|
130
|
6 |
|
if ($state === 0) { |
131
|
6 |
|
if (($token->type === Token::TYPE_KEYWORD) |
132
|
6 |
|
&& (!empty(static::$JOINS[$token->value])) |
133
|
6 |
|
) { |
134
|
6 |
|
$expr->type = static::$JOINS[$token->value]; |
135
|
6 |
|
$state = 1; |
|
|
|
|
136
|
6 |
|
} else { |
137
|
1 |
|
break; |
138
|
|
|
} |
139
|
6 |
|
} elseif ($state === 1) { |
140
|
6 |
|
$expr->expr = Expression::parse($parser, $list, array('field' => 'table')); |
141
|
6 |
|
$state = 2; |
|
|
|
|
142
|
6 |
View Code Duplication |
} elseif ($state === 2) { |
|
|
|
|
143
|
5 |
|
if ($token->type === Token::TYPE_KEYWORD) { |
144
|
5 |
|
if ($token->value === 'ON') { |
145
|
5 |
|
$state = 3; |
146
|
5 |
|
} elseif ($token->value === 'USING') { |
147
|
|
|
$state = 4; |
148
|
|
|
} |
149
|
5 |
|
} |
150
|
5 |
|
} elseif ($state === 3) { |
151
|
5 |
|
$expr->on = Condition::parse($parser, $list); |
152
|
5 |
|
$ret[] = $expr; |
|
|
|
|
153
|
5 |
|
$expr = new JoinKeyword(); |
|
|
|
|
154
|
5 |
|
$state = 0; |
|
|
|
|
155
|
5 |
|
} elseif ($state === 4) { |
156
|
|
|
$expr->using = ArrayObj::parse($parser, $list); |
|
|
|
|
157
|
|
|
$ret[] = $expr; |
|
|
|
|
158
|
|
|
$expr = new JoinKeyword(); |
|
|
|
|
159
|
|
|
$state = 0; |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
6 |
|
} |
163
|
|
|
|
164
|
6 |
|
if (!empty($expr->type)) { |
165
|
1 |
|
$ret[] = $expr; |
166
|
1 |
|
} |
167
|
|
|
|
168
|
6 |
|
--$list->idx; |
169
|
6 |
|
return $ret; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param JoinKeyword[] $component The component to be built. |
174
|
|
|
* @param array $options Parameters for building. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
2 |
|
public static function build($component, array $options = array()) |
179
|
|
|
{ |
180
|
2 |
|
$ret = array(); |
181
|
2 |
|
foreach ($component as $c) { |
182
|
2 |
|
$ret[] = array_search($c->type, static::$JOINS) . ' ' . $c->expr |
183
|
2 |
|
. (! empty($c->on) |
184
|
2 |
|
? ' ON ' . Condition::build($c->on) |
185
|
2 |
|
: ' USING ' . ArrayObj::build($c->using)); |
186
|
2 |
|
} |
187
|
2 |
|
return implode(' ', $ret); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.