Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
118 / 118 |
|
100.00% |
31 / 31 |
CRAP | |
100.00% |
1 / 1 |
| Join | |
100.00% |
118 / 118 |
|
100.00% |
31 / 31 |
52 | |
100.00% |
1 / 1 |
| from | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| renderFrom | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| hasFrom | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| join | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| joinOn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| joinUsing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| innerJoinOn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| innerJoinUsing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| crossJoin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| crossJoinOn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| crossJoinUsing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| leftJoinOn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| leftJoinUsing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| leftOuterJoinOn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| leftOuterJoinUsing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| rightJoinOn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| rightJoinUsing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| rightOuterJoinOn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| rightOuterJoinUsing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| naturalJoin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| naturalLeftJoin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| naturalLeftOuterJoin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| naturalRightJoin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| naturalRightOuterJoin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setJoin | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| renderJoin | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
| renderJoinConditional | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| renderJoinType | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
2 | |||
| checkNaturalJoinType | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| renderJoinConditionClause | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| renderJoinConditionExpression | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework Database Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\Database\Manipulation\Traits; |
| 11 | |
| 12 | use Closure; |
| 13 | use InvalidArgumentException; |
| 14 | use LogicException; |
| 15 | |
| 16 | /** |
| 17 | * Trait Join. |
| 18 | * |
| 19 | * @see https://mariadb.com/kb/en/joins/ |
| 20 | * |
| 21 | * @package database |
| 22 | * |
| 23 | * @todo STRAIGHT_JOIN - https://mariadb.com/kb/en/index-hints-how-to-force-query-plans/ |
| 24 | */ |
| 25 | trait Join |
| 26 | { |
| 27 | /** |
| 28 | * Sets the FROM clause. |
| 29 | * |
| 30 | * @param array<string,Closure|string>|Closure|string $reference Table reference |
| 31 | * @param array<string,Closure|string>|Closure|string ...$references Table references |
| 32 | * |
| 33 | * @see https://mariadb.com/kb/en/join-syntax/ |
| 34 | * |
| 35 | * @return static |
| 36 | */ |
| 37 | public function from( |
| 38 | array | Closure | string $reference, |
| 39 | array | Closure | string ...$references |
| 40 | ) : static { |
| 41 | $this->sql['from'] = []; |
| 42 | foreach ([$reference, ...$references] as $reference) { |
| 43 | $this->sql['from'][] = $reference; |
| 44 | } |
| 45 | return $this; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Renders the FROM clause. |
| 50 | * |
| 51 | * @return string|null The FROM clause or null if it was not set |
| 52 | */ |
| 53 | protected function renderFrom() : ?string |
| 54 | { |
| 55 | if ( ! isset($this->sql['from'])) { |
| 56 | return null; |
| 57 | } |
| 58 | $tables = []; |
| 59 | foreach ($this->sql['from'] as $table) { |
| 60 | $tables[] = $this->renderAliasedIdentifier($table); |
| 61 | } |
| 62 | return ' FROM ' . \implode(', ', $tables); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Tells if the FROM clause was set. |
| 67 | * |
| 68 | * @param string|null $clause A clause where FROM is required |
| 69 | * |
| 70 | * @throws LogicException if FROM is not set, but is required for some other clause |
| 71 | * |
| 72 | * @return bool True if it has FROM, otherwise false |
| 73 | */ |
| 74 | protected function hasFrom(string $clause = null) : bool |
| 75 | { |
| 76 | if (isset($this->sql['from'])) { |
| 77 | return true; |
| 78 | } |
| 79 | if ($clause === null) { |
| 80 | return false; |
| 81 | } |
| 82 | throw new LogicException("Clause {$clause} only works with FROM"); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Adds a JOIN clause with "$type JOIN $table $clause $conditional". |
| 87 | * |
| 88 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 89 | * @param string $type JOIN type. One of: `CROSS`, `INNER`, `LEFT`, `LEFT OUTER`, |
| 90 | * `RIGHT`, `RIGHT OUTER`, `NATURAL`, `NATURAL LEFT`, `NATURAL LEFT OUTER`, |
| 91 | * `NATURAL RIGHT`, `NATURAL RIGHT OUTER` or empty (same as `INNER`) |
| 92 | * @param string|null $clause Condition clause. Null if it has a NATURAL type, |
| 93 | * otherwise `ON` or `USING` |
| 94 | * @param array<int,Closure|string>|Closure|null $conditional A conditional |
| 95 | * expression as Closure or the columns list as array |
| 96 | * |
| 97 | * @return static |
| 98 | */ |
| 99 | public function join( |
| 100 | array | Closure | string $table, |
| 101 | string $type = '', |
| 102 | string $clause = null, |
| 103 | array | Closure $conditional = null |
| 104 | ) : static { |
| 105 | return $this->setJoin($table, $type, $clause, $conditional); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Adds a JOIN clause with "JOIN $table ON $conditional". |
| 110 | * |
| 111 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 112 | * @param Closure $conditional Conditional expression |
| 113 | * |
| 114 | * @return static |
| 115 | */ |
| 116 | public function joinOn( |
| 117 | array | Closure | string $table, |
| 118 | Closure $conditional |
| 119 | ) : static { |
| 120 | return $this->setJoin($table, '', 'ON', $conditional); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Adds a JOIN clause with "JOIN $table USING ...$columns". |
| 125 | * |
| 126 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 127 | * @param Closure|string ...$columns Columns list |
| 128 | * |
| 129 | * @return static |
| 130 | */ |
| 131 | public function joinUsing( |
| 132 | array | Closure | string $table, |
| 133 | Closure | string ...$columns |
| 134 | ) : static { |
| 135 | return $this->setJoin($table, '', 'USING', $columns); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Adds a JOIN clause with "INNER JOIN $table ON $conditional". |
| 140 | * |
| 141 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 142 | * @param Closure $conditional Conditional expression |
| 143 | * |
| 144 | * @return static |
| 145 | */ |
| 146 | public function innerJoinOn( |
| 147 | array | Closure | string $table, |
| 148 | Closure $conditional |
| 149 | ) : static { |
| 150 | return $this->setJoin($table, 'INNER', 'ON', $conditional); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Adds a JOIN clause with "INNER JOIN $table USING ...$columns". |
| 155 | * |
| 156 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 157 | * @param Closure|string ...$columns Columns list |
| 158 | * |
| 159 | * @return static |
| 160 | */ |
| 161 | public function innerJoinUsing( |
| 162 | array | Closure | string $table, |
| 163 | Closure | string ...$columns |
| 164 | ) : static { |
| 165 | return $this->setJoin($table, 'INNER', 'USING', $columns); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Adds a JOIN clause with "CROSS JOIN $table". |
| 170 | * |
| 171 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 172 | * |
| 173 | * @return static |
| 174 | */ |
| 175 | public function crossJoin(array | Closure | string $table) : static |
| 176 | { |
| 177 | return $this->setJoin($table, 'CROSS'); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Adds a JOIN clause with "CROSS JOIN $table ON $conditional". |
| 182 | * |
| 183 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 184 | * @param Closure $conditional Conditional expression |
| 185 | * |
| 186 | * @return static |
| 187 | */ |
| 188 | public function crossJoinOn( |
| 189 | array | Closure | string $table, |
| 190 | Closure $conditional |
| 191 | ) : static { |
| 192 | return $this->setJoin($table, 'CROSS', 'ON', $conditional); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Adds a JOIN clause with "CROSS JOIN $table USING ...$columns". |
| 197 | * |
| 198 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 199 | * @param Closure|string ...$columns Columns list |
| 200 | * |
| 201 | * @return static |
| 202 | */ |
| 203 | public function crossJoinUsing( |
| 204 | array | Closure | string $table, |
| 205 | Closure | string ...$columns |
| 206 | ) : static { |
| 207 | return $this->setJoin($table, 'CROSS', 'USING', $columns); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Adds a JOIN clause with "LEFT JOIN $table ON $conditional". |
| 212 | * |
| 213 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 214 | * @param Closure $conditional Conditional expression |
| 215 | * |
| 216 | * @return static |
| 217 | */ |
| 218 | public function leftJoinOn( |
| 219 | array | Closure | string $table, |
| 220 | Closure $conditional |
| 221 | ) : static { |
| 222 | return $this->setJoin($table, 'LEFT', 'ON', $conditional); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Adds a JOIN clause with "LEFT JOIN $table USING ...$columns". |
| 227 | * |
| 228 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 229 | * @param Closure|string ...$columns Columns list |
| 230 | * |
| 231 | * @return static |
| 232 | */ |
| 233 | public function leftJoinUsing( |
| 234 | array | Closure | string $table, |
| 235 | Closure | string ...$columns |
| 236 | ) : static { |
| 237 | return $this->setJoin($table, 'LEFT', 'USING', $columns); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Adds a JOIN clause with "LEFT OUTER JOIN $table ON $conditional". |
| 242 | * |
| 243 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 244 | * @param Closure $conditional Conditional expression |
| 245 | * |
| 246 | * @return static |
| 247 | */ |
| 248 | public function leftOuterJoinOn( |
| 249 | array | Closure | string $table, |
| 250 | Closure $conditional |
| 251 | ) : static { |
| 252 | return $this->setJoin($table, 'LEFT OUTER', 'ON', $conditional); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Adds a JOIN clause with "LEFT OUTER JOIN $table USING ...$columns". |
| 257 | * |
| 258 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 259 | * @param Closure|string ...$columns Columns list |
| 260 | * |
| 261 | * @return static |
| 262 | */ |
| 263 | public function leftOuterJoinUsing( |
| 264 | array | Closure | string $table, |
| 265 | Closure | string ...$columns |
| 266 | ) : static { |
| 267 | return $this->setJoin($table, 'LEFT OUTER', 'USING', $columns); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Adds a JOIN clause with "RIGHT JOIN $table ON $conditional". |
| 272 | * |
| 273 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 274 | * @param Closure $conditional Conditional expression |
| 275 | * |
| 276 | * @return static |
| 277 | */ |
| 278 | public function rightJoinOn( |
| 279 | array | Closure | string $table, |
| 280 | Closure $conditional |
| 281 | ) : static { |
| 282 | return $this->setJoin($table, 'RIGHT', 'ON', $conditional); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Adds a JOIN clause with "RIGHT JOIN $table USING ...$columns". |
| 287 | * |
| 288 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 289 | * @param Closure|string ...$columns Columns list |
| 290 | * |
| 291 | * @return static |
| 292 | */ |
| 293 | public function rightJoinUsing( |
| 294 | array | Closure | string $table, |
| 295 | Closure | string ...$columns |
| 296 | ) : static { |
| 297 | return $this->setJoin($table, 'RIGHT', 'USING', $columns); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Adds a JOIN clause with "RIGHT OUTER JOIN $table ON $conditional". |
| 302 | * |
| 303 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 304 | * @param Closure $conditional Conditional expression |
| 305 | * |
| 306 | * @return static |
| 307 | */ |
| 308 | public function rightOuterJoinOn( |
| 309 | array | Closure | string $table, |
| 310 | Closure $conditional |
| 311 | ) : static { |
| 312 | return $this->setJoin($table, 'RIGHT OUTER', 'ON', $conditional); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Adds a JOIN clause with "RIGHT OUTER JOIN $table USING ...$columns". |
| 317 | * |
| 318 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 319 | * @param Closure|string ...$columns Columns list |
| 320 | * |
| 321 | * @return static |
| 322 | */ |
| 323 | public function rightOuterJoinUsing( |
| 324 | array | Closure | string $table, |
| 325 | Closure | string ...$columns |
| 326 | ) : static { |
| 327 | return $this->setJoin($table, 'RIGHT OUTER', 'USING', $columns); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Adds a JOIN clause with "NATURAL JOIN $table". |
| 332 | * |
| 333 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 334 | * |
| 335 | * @return static |
| 336 | */ |
| 337 | public function naturalJoin(array | Closure | string $table) : static |
| 338 | { |
| 339 | return $this->setJoin($table, 'NATURAL'); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Adds a JOIN clause with "NATURAL LEFT JOIN $table". |
| 344 | * |
| 345 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 346 | * |
| 347 | * @return static |
| 348 | */ |
| 349 | public function naturalLeftJoin(array | Closure | string $table) : static |
| 350 | { |
| 351 | return $this->setJoin($table, 'NATURAL LEFT'); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Adds a JOIN clause with "NATURAL LEFT OUTER JOIN $table". |
| 356 | * |
| 357 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 358 | * |
| 359 | * @return static |
| 360 | */ |
| 361 | public function naturalLeftOuterJoin( |
| 362 | array | Closure | string $table |
| 363 | ) : static { |
| 364 | return $this->setJoin($table, 'NATURAL LEFT OUTER'); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Adds a JOIN clause with "NATURAL RIGHT JOIN $table". |
| 369 | * |
| 370 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 371 | * |
| 372 | * @return static |
| 373 | */ |
| 374 | public function naturalRightJoin(array | Closure | string $table) : static |
| 375 | { |
| 376 | return $this->setJoin($table, 'NATURAL RIGHT'); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Adds a JOIN clause with "NATURAL RIGHT OUTER JOIN $table". |
| 381 | * |
| 382 | * @param array<string,Closure|string>|Closure|string $table Table factor |
| 383 | * |
| 384 | * @return static |
| 385 | */ |
| 386 | public function naturalRightOuterJoin( |
| 387 | array | Closure | string $table |
| 388 | ) : static { |
| 389 | return $this->setJoin($table, 'NATURAL RIGHT OUTER'); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Sets the JOIN clause. |
| 394 | * |
| 395 | * @param array<string,Closure|string>|Closure|string $table The table factor |
| 396 | * @param string $type ``, `CROSS`, `INNER`, `LEFT`, `LEFT OUTER`, `RIGHT`, |
| 397 | * `RIGHT OUTER`, `NATURAL`, `NATURAL LEFT`, `NATURAL LEFT OUTER`, `NATURAL RIGHT` |
| 398 | * or `NATURAL RIGHT OUTER` |
| 399 | * @param string|null $clause `ON`, `USING` or null for none |
| 400 | * @param array<Closure|string>|Closure|null $expression Column(s) or subquery(ies) |
| 401 | * |
| 402 | * @return static |
| 403 | */ |
| 404 | private function setJoin( |
| 405 | array | Closure | string $table, |
| 406 | string $type, |
| 407 | string $clause = null, |
| 408 | Closure | array $expression = null |
| 409 | ) : static { |
| 410 | $this->sql['join'][] = [ |
| 411 | 'type' => $type, |
| 412 | 'table' => $table, |
| 413 | 'clause' => $clause, |
| 414 | 'expression' => $expression, |
| 415 | ]; |
| 416 | return $this; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Renders the JOIN clause. |
| 421 | * |
| 422 | * @return string|null The JOIN clause or null if it was not set |
| 423 | */ |
| 424 | protected function renderJoin() : ?string |
| 425 | { |
| 426 | if ( ! isset($this->sql['join'])) { |
| 427 | return null; |
| 428 | } |
| 429 | $result = ''; |
| 430 | foreach ($this->sql['join'] as $index => $join) { |
| 431 | $type = $this->renderJoinType($join['type']); |
| 432 | $conditional = $this->renderJoinConditional( |
| 433 | $type, |
| 434 | $join['table'], |
| 435 | $join['clause'], |
| 436 | $join['expression'] |
| 437 | ); |
| 438 | if ($type) { |
| 439 | $type .= ' '; |
| 440 | } |
| 441 | if ($index > 0) { |
| 442 | $result .= \PHP_EOL; |
| 443 | } |
| 444 | $result .= " {$type}JOIN {$conditional}"; |
| 445 | } |
| 446 | return $result; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Renders the JOIN conditional part. |
| 451 | * |
| 452 | * @param string $type ``, `CROSS`,`INNER`, `LEFT`, `LEFT OUTER`, `RIGHT`, |
| 453 | * `RIGHT OUTER`, `NATURAL`, `NATURAL LEFT`, `NATURAL LEFT OUTER`, `NATURAL RIGHT` |
| 454 | * or `NATURAL RIGHT OUTER` |
| 455 | * @param array<string,Closure|string>|Closure|string $table The table name |
| 456 | * @param string|null $clause `ON`, `USING` or null for none |
| 457 | * @param array<Closure|string>|Closure|null $expression Column(s) or subquery(ies) |
| 458 | * |
| 459 | * @return string The JOIN conditional part |
| 460 | */ |
| 461 | private function renderJoinConditional( |
| 462 | string $type, |
| 463 | array | Closure | string $table, |
| 464 | ?string $clause, |
| 465 | Closure | array | null $expression |
| 466 | ) : string { |
| 467 | $table = $this->renderAliasedIdentifier($table); |
| 468 | $isNatural = $this->checkNaturalJoinType($type, $clause, $expression); |
| 469 | if ($isNatural) { |
| 470 | return $table; |
| 471 | } |
| 472 | $conditional = ''; |
| 473 | $clause = $this->renderJoinConditionClause($clause); |
| 474 | if ($clause) { |
| 475 | $conditional .= ' ' . $clause; |
| 476 | } |
| 477 | $expression = $this->renderJoinConditionExpression($clause, $expression); |
| 478 | if ($expression) { |
| 479 | $conditional .= ' ' . $expression; |
| 480 | } |
| 481 | return $table . $conditional; |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Validates and renders the JOIN type. |
| 486 | * |
| 487 | * @param string $type ``, `CROSS`,`INNER`, `LEFT`, `LEFT OUTER`, `RIGHT`, |
| 488 | * `RIGHT OUTER`, `NATURAL`, `NATURAL LEFT`, `NATURAL LEFT OUTER`, `NATURAL RIGHT` |
| 489 | * or `NATURAL RIGHT OUTER` |
| 490 | * |
| 491 | * @throws InvalidArgumentException for invalid type |
| 492 | * |
| 493 | * @return string The input ype |
| 494 | */ |
| 495 | private function renderJoinType(string $type) : string |
| 496 | { |
| 497 | $result = \strtoupper($type); |
| 498 | if (\in_array($result, [ |
| 499 | '', |
| 500 | 'CROSS', |
| 501 | 'INNER', |
| 502 | 'LEFT', |
| 503 | 'LEFT OUTER', |
| 504 | 'RIGHT', |
| 505 | 'RIGHT OUTER', |
| 506 | 'NATURAL', |
| 507 | 'NATURAL LEFT', |
| 508 | 'NATURAL LEFT OUTER', |
| 509 | 'NATURAL RIGHT', |
| 510 | 'NATURAL RIGHT OUTER', |
| 511 | ], true)) { |
| 512 | return $result; |
| 513 | } |
| 514 | throw new InvalidArgumentException("Invalid JOIN type: {$type}"); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Check if a JOIN type belongs to the NATURAL group. |
| 519 | * |
| 520 | * @param string $type `NATURAL`, `NATURAL LEFT`, `NATURAL LEFT OUTER`, |
| 521 | * `NATURAL RIGHT`, `NATURAL RIGHT OUTER` or any other non-natural |
| 522 | * @param string|null $clause Must be null if type is natural |
| 523 | * @param array<Closure|string>|Closure|null $expression Must be null if type is natural |
| 524 | * |
| 525 | * @throws InvalidArgumentException if $type is natural and has clause or expression |
| 526 | * |
| 527 | * @return bool True if the type is natural, otherwise false |
| 528 | */ |
| 529 | private function checkNaturalJoinType( |
| 530 | string $type, |
| 531 | ?string $clause, |
| 532 | Closure | array | null $expression |
| 533 | ) : bool { |
| 534 | if (\in_array($type, [ |
| 535 | 'NATURAL', |
| 536 | 'NATURAL LEFT', |
| 537 | 'NATURAL LEFT OUTER', |
| 538 | 'NATURAL RIGHT', |
| 539 | 'NATURAL RIGHT OUTER', |
| 540 | ], true)) { |
| 541 | if ($clause !== null || $expression !== null) { |
| 542 | throw new InvalidArgumentException( |
| 543 | "{$type} JOIN has not condition" |
| 544 | ); |
| 545 | } |
| 546 | return true; |
| 547 | } |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Validates and renders the JOIN condition clause. |
| 553 | * |
| 554 | * @param string|null $clause `ON`, `USING` or null for none |
| 555 | * |
| 556 | * @throws InvalidArgumentException for invalid condition clause |
| 557 | * |
| 558 | * @return string|null The condition clause or none |
| 559 | */ |
| 560 | private function renderJoinConditionClause(?string $clause) : ?string |
| 561 | { |
| 562 | if ($clause === null) { |
| 563 | return null; |
| 564 | } |
| 565 | $result = \strtoupper($clause); |
| 566 | if (\in_array($result, [ |
| 567 | 'ON', |
| 568 | 'USING', |
| 569 | ], true)) { |
| 570 | return $result; |
| 571 | } |
| 572 | throw new InvalidArgumentException("Invalid JOIN condition clause: {$clause}"); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Renders the JOIN condition expression. |
| 577 | * |
| 578 | * @param string|null $clause `ON`or null |
| 579 | * @param array<Closure|string>|Closure|null $expression Column(s) or subquery(ies) |
| 580 | * |
| 581 | * @return string|null The condition or null if $clause is null |
| 582 | */ |
| 583 | private function renderJoinConditionExpression( |
| 584 | ?string $clause, |
| 585 | Closure | array | null $expression |
| 586 | ) : ?string { |
| 587 | if ($clause === null) { |
| 588 | return null; |
| 589 | } |
| 590 | if ($clause === 'ON') { |
| 591 | // @phpstan-ignore-next-line |
| 592 | return $this->subquery($expression); |
| 593 | } |
| 594 | // @phpstan-ignore-next-line |
| 595 | foreach ($expression as &$column) { |
| 596 | $column = $this->renderIdentifier($column); |
| 597 | } |
| 598 | // @phpstan-ignore-next-line |
| 599 | return '(' . \implode(', ', $expression) . ')'; |
| 600 | } |
| 601 | } |