1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Extension; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Query\Clause; |
6
|
|
|
use Bdf\Prime\Query\Compiler\CompilerState; |
7
|
|
|
use Bdf\Prime\Query\Contract\Joinable; |
8
|
|
|
use Bdf\Prime\Query\JoinClause; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait for join() method |
12
|
|
|
* |
13
|
|
|
* @see Joinable |
14
|
|
|
* @psalm-require-implements Joinable |
15
|
|
|
* |
16
|
|
|
* @property CompilerState $compilerState |
17
|
|
|
*/ |
18
|
|
|
trait SimpleJoinTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @see Joinable::join() |
24
|
|
|
*/ |
25
|
112 |
|
public function join($table, $key, ?string $operator = null, $foreign = null, string $type = Joinable::INNER_JOIN) |
26
|
|
|
{ |
27
|
112 |
|
$this->compilerState->invalidate('joins'); |
28
|
|
|
|
29
|
112 |
|
$alias = null; |
30
|
112 |
|
if (is_array($table)) { |
31
|
111 |
|
$alias = $table[1]; |
32
|
111 |
|
$table = $table[0]; |
33
|
|
|
} |
34
|
|
|
|
35
|
112 |
|
$join = new JoinClause(); |
36
|
|
|
|
37
|
112 |
|
if (is_string($key)) { |
38
|
109 |
|
$join->on($key, $operator, $foreign); |
39
|
|
|
} else { |
40
|
3 |
|
$key($join); |
41
|
|
|
} |
42
|
|
|
|
43
|
112 |
|
$this->addStatement('joins', [ |
44
|
112 |
|
'type' => strtoupper($type), // TODO remove strtoupper |
45
|
112 |
|
'table' => $table, |
46
|
112 |
|
'alias' => $alias, |
47
|
112 |
|
'on' => $join->clauses(), |
48
|
112 |
|
]); |
49
|
|
|
|
50
|
112 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
* |
56
|
|
|
* @see Joinable::leftJoin() |
57
|
|
|
*/ |
58
|
|
|
public function leftJoin($table, $key, ?string $operator = null, $foreign = null) |
59
|
|
|
{ |
60
|
|
|
return $this->join($table, $key, $operator, $foreign, Joinable::LEFT_JOIN); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
* |
66
|
|
|
* @see Joinable::rightJoin() |
67
|
|
|
*/ |
68
|
|
|
public function rightJoin($table, $key, ?string $operator = null, $foreign = null) |
69
|
|
|
{ |
70
|
|
|
return $this->join($table, $key, $operator, $foreign, Joinable::RIGHT_JOIN); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
* |
76
|
|
|
* @see Clause::addStatement() |
77
|
|
|
*/ |
78
|
|
|
abstract public function addStatement(string $name, $values): void; |
79
|
|
|
} |
80
|
|
|
|