|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Query |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Query\Traits\Clause; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Query\Interfaces\StatementInterface; |
|
18
|
|
|
use Phossa2\Query\Interfaces\ExpressionInterface; |
|
19
|
|
|
use Phossa2\Query\Interfaces\Clause\JoinInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* JoinTrait |
|
23
|
|
|
* |
|
24
|
|
|
* Implementation of JoinInterface |
|
25
|
|
|
* |
|
26
|
|
|
* @package Phossa2\Query |
|
27
|
|
|
* @author Hong Zhang <[email protected]> |
|
28
|
|
|
* @see JoinInterface |
|
29
|
|
|
* @version 2.0.0 |
|
30
|
|
|
* @since 2.0.0 added |
|
31
|
|
|
*/ |
|
32
|
|
|
trait JoinTrait |
|
33
|
|
|
{ |
|
34
|
|
|
use AbstractTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritDoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function join($secondTable, $onClause = '') |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->realJoin('INNER JOIN', $secondTable, $onClause); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritDoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function leftJoin($secondTable, $onClause = '') |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->realJoin('LEFT JOIN', $secondTable, $onClause); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritDoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function leftOuterJoin($secondTable, $onClause = '') |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->realJoin('LEFT OUTER JOIN', $secondTable, $onClause); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function rightJoin($secondTable, $onClause = '') |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->realJoin('RIGHT JOIN', $secondTable, $onClause); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritDoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function rightOuterJoin($secondTable, $onClause = '') |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->realJoin('RIGHT OUTER JOIN', $secondTable, $onClause); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritDoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function outerJoin($secondTable, $onClause = '') |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->realJoin('OUTER JOIN', $secondTable, $onClause); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritDoc} |
|
86
|
|
|
*/ |
|
87
|
|
|
public function crossJoin($secondTable, $onClause = '') |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->realJoin('CROSS JOIN', $secondTable, $onClause); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritDoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function joinRaw( |
|
96
|
|
|
/*# string */ $joinType, |
|
97
|
|
|
/*# string */ $rawString, |
|
98
|
|
|
array $params = []) |
|
99
|
|
|
{ |
|
100
|
|
|
$rawString = $this->positionedParam($rawString, $params); |
|
101
|
|
|
return $this->realJoin(strtoupper($joinType), $rawString, '', true); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* The real join |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $joinType |
|
108
|
|
|
* @param string|string[]|SelectStatementInterface $secondTable |
|
109
|
|
|
* @param string|string[]|ExpressionInterface $onClause |
|
110
|
|
|
* @param bool $rawMode |
|
111
|
|
|
* @return $this |
|
112
|
|
|
* @access protected |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function realJoin( |
|
115
|
|
|
/*# string */ $joinType, |
|
116
|
|
|
$secondTable, |
|
117
|
|
|
$onClause = '', |
|
118
|
|
|
/*# bool */ $rawMode = false |
|
119
|
|
|
) { |
|
120
|
|
|
$alias = 0; // no alias |
|
121
|
|
|
list($secondTable, $alias) = $this->fixJoinTable($secondTable); |
|
122
|
|
|
|
|
123
|
|
|
if ($rawMode || '' === $onClause || $this->isRaw($onClause, false)) { |
|
124
|
|
|
$rawMode = true; |
|
125
|
|
|
} else { |
|
126
|
|
|
$onClause = $this->fixOnClause($onClause); |
|
127
|
|
|
} |
|
128
|
|
|
$clause = &$this->getClause('JOIN'); |
|
129
|
|
|
$clause[] = [$rawMode, $joinType, $secondTable, $alias, $onClause]; |
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Fix join table |
|
135
|
|
|
* |
|
136
|
|
|
* @param string|string[]|SelectStatementInterface $table |
|
137
|
|
|
* @return array [table, alias] |
|
138
|
|
|
* @access protected |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function fixJoinTable($table) |
|
141
|
|
|
{ |
|
142
|
|
|
if (is_array($table)) { |
|
143
|
|
|
return $table; |
|
144
|
|
|
} elseif (is_object($table) && $table instanceof StatementInterface) { |
|
145
|
|
|
return [$table, uniqid()]; |
|
146
|
|
|
} else { |
|
147
|
|
|
return [$table, 0]; // alias set 0 |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Fix 'ON' clause |
|
153
|
|
|
* |
|
154
|
|
|
* @param mixed $onClause |
|
155
|
|
|
* @return array|ExpressionInterface |
|
156
|
|
|
* @access protected |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function fixOnClause($onClause) |
|
159
|
|
|
{ |
|
160
|
|
|
if (is_string($onClause)) { |
|
161
|
|
|
return [$onClause, '=', $onClause]; |
|
162
|
|
|
} elseif (is_array($onClause) && !isset($onClause[2])) { |
|
163
|
|
|
return [$onClause[0], '=', $onClause[1]]; |
|
164
|
|
|
} else { |
|
165
|
|
|
return $onClause; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Build join |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $prefix |
|
173
|
|
|
* @param array $settings |
|
174
|
|
|
* @return string |
|
175
|
|
|
* @access protected |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function buildJoin( |
|
178
|
|
|
/*# string */ $prefix, |
|
179
|
|
|
array $settings |
|
180
|
|
|
)/*# : string */ { |
|
181
|
|
|
$string = ''; |
|
182
|
|
|
$clause = &$this->getClause('JOIN'); |
|
183
|
|
|
foreach ($clause as $cls) { |
|
184
|
|
|
$result = []; |
|
185
|
|
|
$prefix = $cls[1]; // join type |
|
186
|
|
|
$result[] = $this->buildJoinTable($cls, $settings); // join table |
|
187
|
|
|
if (!empty($cls[4])) { |
|
188
|
|
|
$result[] = $this->buildJoinOn($cls, $settings); |
|
189
|
|
|
} |
|
190
|
|
|
$string .= $this->joinClause($prefix, '', $result, $settings); |
|
191
|
|
|
} |
|
192
|
|
|
return $string; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Build TABLE part |
|
197
|
|
|
* |
|
198
|
|
|
* @param array $cls |
|
199
|
|
|
* @param array $settings |
|
200
|
|
|
* @return string |
|
201
|
|
|
* @access protected |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function buildJoinTable(array $cls, array $settings)/*# : string */ |
|
204
|
|
|
{ |
|
205
|
|
|
$table = $cls[2]; |
|
206
|
|
|
$alias = $cls[3]; |
|
207
|
|
|
return $this->quoteItem($table, $settings) . $this->quoteAlias($alias, $settings); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Build ON part |
|
212
|
|
|
* |
|
213
|
|
|
* @param array $cls |
|
214
|
|
|
* @param array $settings |
|
215
|
|
|
* @return string |
|
216
|
|
|
* @access protected |
|
217
|
|
|
*/ |
|
218
|
|
|
protected function buildJoinOn(array $cls, array $settings)/*# : string */ |
|
219
|
|
|
{ |
|
220
|
|
|
$res = ['ON']; |
|
221
|
|
|
$on = $cls[4]; |
|
222
|
|
|
if (is_string($on)) { // ON string |
|
223
|
|
|
$res[] = $on; |
|
224
|
|
|
} elseif (is_object($on)) { // ON is an object |
|
225
|
|
|
$res[] = $this->quoteItem($on, $settings); |
|
226
|
|
|
} else { // common on |
|
227
|
|
|
$res[] = $this->quote( // left |
|
228
|
|
|
$this->getFirstTableAlias($cls, $on[0]) . $on[0], $settings); |
|
229
|
|
|
$res[] = $on[1]; // operator |
|
230
|
|
|
$res[] = $this->quote( // right |
|
231
|
|
|
$this->getSecondTableAlias($cls, $on[2]) . $on[2], $settings); |
|
232
|
|
|
} |
|
233
|
|
|
return join(' ', $res); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get first table alias |
|
238
|
|
|
* |
|
239
|
|
|
* @param array $cls |
|
240
|
|
|
* @param string $left left part of eq |
|
241
|
|
|
* @return string |
|
242
|
|
|
* @access protected |
|
243
|
|
|
*/ |
|
244
|
|
|
protected function getFirstTableAlias( |
|
245
|
|
|
array $cls, |
|
|
|
|
|
|
246
|
|
|
/*# string */ $left |
|
247
|
|
|
)/*# : string */ { |
|
248
|
|
|
if (false !== strpos($left, '.')) { // alias exists |
|
249
|
|
|
return ''; |
|
250
|
|
|
} else { // prepend first table alias |
|
251
|
|
|
$tables = &$this->getClause('TABLE'); |
|
252
|
|
|
reset($tables); |
|
253
|
|
|
$alias = key($tables); |
|
254
|
|
|
return (is_int($alias) ? $tables[$alias][0] : $alias) . '.'; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Get second table alias |
|
260
|
|
|
* |
|
261
|
|
|
* @param array $cls |
|
262
|
|
|
* @param string $right right part of eq |
|
263
|
|
|
* @return string |
|
264
|
|
|
* @access protected |
|
265
|
|
|
*/ |
|
266
|
|
|
protected function getSecondTableAlias( |
|
267
|
|
|
array $cls, |
|
268
|
|
|
/*# string */ $right |
|
269
|
|
|
)/*# : string */ { |
|
270
|
|
|
if (false !== strpos($right, '.')) { |
|
271
|
|
|
return ''; |
|
272
|
|
|
} else { |
|
273
|
|
|
$alias = $cls[3]; |
|
274
|
|
|
if (!is_string($alias)) { |
|
275
|
|
|
$alias = $cls[2]; |
|
276
|
|
|
} |
|
277
|
|
|
return $alias . '.'; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.