Skip to content

Commit aa9bb90

Browse files
committed
Allows set table alias in JOIN clauses
1 parent 09d261b commit aa9bb90

File tree

2 files changed

+101
-62
lines changed

2 files changed

+101
-62
lines changed

src/Manipulation/Traits/Join.php

Lines changed: 92 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function hasFrom(string $clause = null) : bool
8585
/**
8686
* Adds a JOIN clause with "$type JOIN $table $clause $conditional".
8787
*
88-
* @param Closure|string $table Table factor
88+
* @param array<string,Closure|string>|Closure|string $table Table factor
8989
* @param string $type JOIN type. One of: `CROSS`, `INNER`, `LEFT`, `LEFT OUTER`,
9090
* `RIGHT`, `RIGHT OUTER`, `NATURAL`, `NATURAL LEFT`, `NATURAL LEFT OUTER`,
9191
* `NATURAL RIGHT`, `NATURAL RIGHT OUTER` or empty (same as `INNER`)
@@ -97,7 +97,7 @@ protected function hasFrom(string $clause = null) : bool
9797
* @return static
9898
*/
9999
public function join(
100-
Closure | string $table,
100+
array | Closure | string $table,
101101
string $type = '',
102102
string $clause = null,
103103
array | Closure $conditional = null
@@ -108,261 +108,291 @@ public function join(
108108
/**
109109
* Adds a JOIN clause with "JOIN $table ON $conditional".
110110
*
111-
* @param Closure|string $table Table factor
111+
* @param array<string,Closure|string>|Closure|string $table Table factor
112112
* @param Closure $conditional Conditional expression
113113
*
114114
* @return static
115115
*/
116-
public function joinOn(Closure | string $table, Closure $conditional) : static
117-
{
116+
public function joinOn(
117+
array | Closure | string $table,
118+
Closure $conditional
119+
) : static {
118120
return $this->setJoin($table, '', 'ON', $conditional);
119121
}
120122

121123
/**
122124
* Adds a JOIN clause with "JOIN $table USING ...$columns".
123125
*
124-
* @param Closure|string $table Table factor
126+
* @param array<string,Closure|string>|Closure|string $table Table factor
125127
* @param Closure|string ...$columns Columns list
126128
*
127129
* @return static
128130
*/
129-
public function joinUsing(Closure | string $table, Closure | string ...$columns) : static
130-
{
131+
public function joinUsing(
132+
array | Closure | string $table,
133+
Closure | string ...$columns
134+
) : static {
131135
return $this->setJoin($table, '', 'USING', $columns);
132136
}
133137

134138
/**
135139
* Adds a JOIN clause with "INNER JOIN $table ON $conditional".
136140
*
137-
* @param Closure|string $table Table factor
141+
* @param array<string,Closure|string>|Closure|string $table Table factor
138142
* @param Closure $conditional Conditional expression
139143
*
140144
* @return static
141145
*/
142-
public function innerJoinOn(Closure | string $table, Closure $conditional) : static
143-
{
146+
public function innerJoinOn(
147+
array | Closure | string $table,
148+
Closure $conditional
149+
) : static {
144150
return $this->setJoin($table, 'INNER', 'ON', $conditional);
145151
}
146152

147153
/**
148154
* Adds a JOIN clause with "INNER JOIN $table USING ...$columns".
149155
*
150-
* @param Closure|string $table Table factor
156+
* @param array<string,Closure|string>|Closure|string $table Table factor
151157
* @param Closure|string ...$columns Columns list
152158
*
153159
* @return static
154160
*/
155-
public function innerJoinUsing(Closure | string $table, Closure | string ...$columns) : static
156-
{
161+
public function innerJoinUsing(
162+
array | Closure | string $table,
163+
Closure | string ...$columns
164+
) : static {
157165
return $this->setJoin($table, 'INNER', 'USING', $columns);
158166
}
159167

160168
/**
161169
* Adds a JOIN clause with "CROSS JOIN $table".
162170
*
163-
* @param Closure|string $table Table factor
171+
* @param array<string,Closure|string>|Closure|string $table Table factor
164172
*
165173
* @return static
166174
*/
167-
public function crossJoin(Closure | string $table) : static
175+
public function crossJoin(array | Closure | string $table) : static
168176
{
169177
return $this->setJoin($table, 'CROSS');
170178
}
171179

172180
/**
173181
* Adds a JOIN clause with "CROSS JOIN $table ON $conditional".
174182
*
175-
* @param Closure|string $table Table factor
183+
* @param array<string,Closure|string>|Closure|string $table Table factor
176184
* @param Closure $conditional Conditional expression
177185
*
178186
* @return static
179187
*/
180-
public function crossJoinOn(Closure | string $table, Closure $conditional) : static
181-
{
188+
public function crossJoinOn(
189+
array | Closure | string $table,
190+
Closure $conditional
191+
) : static {
182192
return $this->setJoin($table, 'CROSS', 'ON', $conditional);
183193
}
184194

185195
/**
186196
* Adds a JOIN clause with "CROSS JOIN $table USING ...$columns".
187197
*
188-
* @param Closure|string $table Table factor
198+
* @param array<string,Closure|string>|Closure|string $table Table factor
189199
* @param Closure|string ...$columns Columns list
190200
*
191201
* @return static
192202
*/
193-
public function crossJoinUsing(Closure | string $table, Closure | string ...$columns) : static
194-
{
203+
public function crossJoinUsing(
204+
array | Closure | string $table,
205+
Closure | string ...$columns
206+
) : static {
195207
return $this->setJoin($table, 'CROSS', 'USING', $columns);
196208
}
197209

198210
/**
199211
* Adds a JOIN clause with "LEFT JOIN $table ON $conditional".
200212
*
201-
* @param Closure|string $table Table factor
213+
* @param array<string,Closure|string>|Closure|string $table Table factor
202214
* @param Closure $conditional Conditional expression
203215
*
204216
* @return static
205217
*/
206-
public function leftJoinOn(Closure | string $table, Closure $conditional) : static
207-
{
218+
public function leftJoinOn(
219+
array | Closure | string $table,
220+
Closure $conditional
221+
) : static {
208222
return $this->setJoin($table, 'LEFT', 'ON', $conditional);
209223
}
210224

211225
/**
212226
* Adds a JOIN clause with "LEFT JOIN $table USING ...$columns".
213227
*
214-
* @param Closure|string $table Table factor
228+
* @param array<string,Closure|string>|Closure|string $table Table factor
215229
* @param Closure|string ...$columns Columns list
216230
*
217231
* @return static
218232
*/
219-
public function leftJoinUsing(Closure | string $table, Closure | string ...$columns) : static
220-
{
233+
public function leftJoinUsing(
234+
array | Closure | string $table,
235+
Closure | string ...$columns
236+
) : static {
221237
return $this->setJoin($table, 'LEFT', 'USING', $columns);
222238
}
223239

224240
/**
225241
* Adds a JOIN clause with "LEFT OUTER JOIN $table ON $conditional".
226242
*
227-
* @param Closure|string $table Table factor
243+
* @param array<string,Closure|string>|Closure|string $table Table factor
228244
* @param Closure $conditional Conditional expression
229245
*
230246
* @return static
231247
*/
232-
public function leftOuterJoinOn(Closure | string $table, Closure $conditional) : static
233-
{
248+
public function leftOuterJoinOn(
249+
array | Closure | string $table,
250+
Closure $conditional
251+
) : static {
234252
return $this->setJoin($table, 'LEFT OUTER', 'ON', $conditional);
235253
}
236254

237255
/**
238256
* Adds a JOIN clause with "LEFT OUTER JOIN $table USING ...$columns".
239257
*
240-
* @param Closure|string $table Table factor
258+
* @param array<string,Closure|string>|Closure|string $table Table factor
241259
* @param Closure|string ...$columns Columns list
242260
*
243261
* @return static
244262
*/
245-
public function leftOuterJoinUsing(Closure | string $table, Closure | string ...$columns) : static
246-
{
263+
public function leftOuterJoinUsing(
264+
array | Closure | string $table,
265+
Closure | string ...$columns
266+
) : static {
247267
return $this->setJoin($table, 'LEFT OUTER', 'USING', $columns);
248268
}
249269

250270
/**
251271
* Adds a JOIN clause with "RIGHT JOIN $table ON $conditional".
252272
*
253-
* @param Closure|string $table Table factor
273+
* @param array<string,Closure|string>|Closure|string $table Table factor
254274
* @param Closure $conditional Conditional expression
255275
*
256276
* @return static
257277
*/
258-
public function rightJoinOn(Closure | string $table, Closure $conditional) : static
259-
{
278+
public function rightJoinOn(
279+
array | Closure | string $table,
280+
Closure $conditional
281+
) : static {
260282
return $this->setJoin($table, 'RIGHT', 'ON', $conditional);
261283
}
262284

263285
/**
264286
* Adds a JOIN clause with "RIGHT JOIN $table USING ...$columns".
265287
*
266-
* @param Closure|string $table Table factor
288+
* @param array<string,Closure|string>|Closure|string $table Table factor
267289
* @param Closure|string ...$columns Columns list
268290
*
269291
* @return static
270292
*/
271-
public function rightJoinUsing(Closure | string $table, Closure | string ...$columns) : static
272-
{
293+
public function rightJoinUsing(
294+
array | Closure | string $table,
295+
Closure | string ...$columns
296+
) : static {
273297
return $this->setJoin($table, 'RIGHT', 'USING', $columns);
274298
}
275299

276300
/**
277301
* Adds a JOIN clause with "RIGHT OUTER JOIN $table ON $conditional".
278302
*
279-
* @param Closure|string $table Table factor
303+
* @param array<string,Closure|string>|Closure|string $table Table factor
280304
* @param Closure $conditional Conditional expression
281305
*
282306
* @return static
283307
*/
284-
public function rightOuterJoinOn(Closure | string $table, Closure $conditional) : static
285-
{
308+
public function rightOuterJoinOn(
309+
array | Closure | string $table,
310+
Closure $conditional
311+
) : static {
286312
return $this->setJoin($table, 'RIGHT OUTER', 'ON', $conditional);
287313
}
288314

289315
/**
290316
* Adds a JOIN clause with "RIGHT OUTER JOIN $table USING ...$columns".
291317
*
292-
* @param Closure|string $table Table factor
318+
* @param array<string,Closure|string>|Closure|string $table Table factor
293319
* @param Closure|string ...$columns Columns list
294320
*
295321
* @return static
296322
*/
297-
public function rightOuterJoinUsing(Closure | string $table, Closure | string ...$columns) : static
298-
{
323+
public function rightOuterJoinUsing(
324+
array | Closure | string $table,
325+
Closure | string ...$columns
326+
) : static {
299327
return $this->setJoin($table, 'RIGHT OUTER', 'USING', $columns);
300328
}
301329

302330
/**
303331
* Adds a JOIN clause with "NATURAL JOIN $table".
304332
*
305-
* @param Closure|string $table Table factor
333+
* @param array<string,Closure|string>|Closure|string $table Table factor
306334
*
307335
* @return static
308336
*/
309-
public function naturalJoin(Closure | string $table) : static
337+
public function naturalJoin(array | Closure | string $table) : static
310338
{
311339
return $this->setJoin($table, 'NATURAL');
312340
}
313341

314342
/**
315343
* Adds a JOIN clause with "NATURAL LEFT JOIN $table".
316344
*
317-
* @param Closure|string $table Table factor
345+
* @param array<string,Closure|string>|Closure|string $table Table factor
318346
*
319347
* @return static
320348
*/
321-
public function naturalLeftJoin(Closure | string $table) : static
349+
public function naturalLeftJoin(array | Closure | string $table) : static
322350
{
323351
return $this->setJoin($table, 'NATURAL LEFT');
324352
}
325353

326354
/**
327355
* Adds a JOIN clause with "NATURAL LEFT OUTER JOIN $table".
328356
*
329-
* @param Closure|string $table Table factor
357+
* @param array<string,Closure|string>|Closure|string $table Table factor
330358
*
331359
* @return static
332360
*/
333-
public function naturalLeftOuterJoin(Closure | string $table) : static
334-
{
361+
public function naturalLeftOuterJoin(
362+
array | Closure | string $table
363+
) : static {
335364
return $this->setJoin($table, 'NATURAL LEFT OUTER');
336365
}
337366

338367
/**
339368
* Adds a JOIN clause with "NATURAL RIGHT JOIN $table".
340369
*
341-
* @param Closure|string $table Table factor
370+
* @param array<string,Closure|string>|Closure|string $table Table factor
342371
*
343372
* @return static
344373
*/
345-
public function naturalRightJoin(Closure | string $table) : static
374+
public function naturalRightJoin(array | Closure | string $table) : static
346375
{
347376
return $this->setJoin($table, 'NATURAL RIGHT');
348377
}
349378

350379
/**
351380
* Adds a JOIN clause with "NATURAL RIGHT OUTER JOIN $table".
352381
*
353-
* @param Closure|string $table Table factor
382+
* @param array<string,Closure|string>|Closure|string $table Table factor
354383
*
355384
* @return static
356385
*/
357-
public function naturalRightOuterJoin(Closure | string $table) : static
358-
{
386+
public function naturalRightOuterJoin(
387+
array | Closure | string $table
388+
) : static {
359389
return $this->setJoin($table, 'NATURAL RIGHT OUTER');
360390
}
361391

362392
/**
363393
* Sets the JOIN clause.
364394
*
365-
* @param Closure|string $table The table factor
395+
* @param array<string,Closure|string>|Closure|string $table The table factor
366396
* @param string $type ``, `CROSS`, `INNER`, `LEFT`, `LEFT OUTER`, `RIGHT`,
367397
* `RIGHT OUTER`, `NATURAL`, `NATURAL LEFT`, `NATURAL LEFT OUTER`, `NATURAL RIGHT`
368398
* or `NATURAL RIGHT OUTER`
@@ -372,7 +402,7 @@ public function naturalRightOuterJoin(Closure | string $table) : static
372402
* @return static
373403
*/
374404
private function setJoin(
375-
Closure | string $table,
405+
array | Closure | string $table,
376406
string $type,
377407
string $clause = null,
378408
Closure | array $expression = null
@@ -422,15 +452,15 @@ protected function renderJoin() : ?string
422452
* @param string $type ``, `CROSS`,`INNER`, `LEFT`, `LEFT OUTER`, `RIGHT`,
423453
* `RIGHT OUTER`, `NATURAL`, `NATURAL LEFT`, `NATURAL LEFT OUTER`, `NATURAL RIGHT`
424454
* or `NATURAL RIGHT OUTER`
425-
* @param string $table The table name
455+
* @param array<string,Closure|string>|Closure|string $table The table name
426456
* @param string|null $clause `ON`, `USING` or null for none
427457
* @param array<Closure|string>|Closure|null $expression Column(s) or subquery(ies)
428458
*
429459
* @return string The JOIN conditional part
430460
*/
431461
private function renderJoinConditional(
432462
string $type,
433-
string $table,
463+
array | Closure | string $table,
434464
?string $clause,
435465
Closure | array | null $expression
436466
) : string {

0 commit comments

Comments
 (0)