|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2021 Aleksandar Panic |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace ArekX\PQL\Sql\Query\Traits; |
|
19
|
|
|
|
|
20
|
|
|
use ArekX\PQL\Contracts\StructuredQuery; |
|
21
|
|
|
|
|
22
|
|
|
trait JoinTrait |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Add an INNER type join to the query. |
|
26
|
|
|
* |
|
27
|
|
|
* This function is a helper function for join with type set to `inner`. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array|StructuredQuery|string $withSource Source to join with |
|
30
|
|
|
* @param array|StructuredQuery|string|null $condition Condition to join on |
|
31
|
|
|
* @return $this |
|
32
|
|
|
* @see JoinTrait::join() |
|
33
|
|
|
*/ |
|
34
|
6 |
|
public function innerJoin( |
|
35
|
|
|
StructuredQuery|array|string $withSource, |
|
36
|
|
|
StructuredQuery|array|string|null $condition = null |
|
37
|
|
|
): static |
|
38
|
|
|
{ |
|
39
|
6 |
|
return $this->join('inner', $withSource, $condition); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Add a join part to the query for a specific type of join |
|
44
|
|
|
* source to join with and a condition of the join. |
|
45
|
|
|
* |
|
46
|
|
|
* If $withSource is passed as an associative array it will be parsed as |
|
47
|
|
|
* an array where key is the AS alias and value is the name of the table |
|
48
|
|
|
* or a sub-query if StructuredQuery is passed. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $type Type of the join, types depend on the types that driver supports. |
|
51
|
|
|
* @param array|StructuredQuery|string $withSource Source to join with. |
|
52
|
|
|
* @param StructuredQuery|array|string|null $condition Condition to be set for the join part. |
|
53
|
|
|
* @return $this |
|
54
|
|
|
* @see ConditionTrait::where() for $condition format |
|
55
|
|
|
*/ |
|
56
|
21 |
|
public function join( |
|
57
|
|
|
string $type, |
|
58
|
|
|
StructuredQuery|array|string $withSource, |
|
59
|
|
|
StructuredQuery|array|string|null $condition = null |
|
60
|
|
|
): static |
|
61
|
|
|
{ |
|
62
|
21 |
|
$this->append('join', [$type, $withSource, $condition]); |
|
63
|
21 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Add an LEFT type join to the query. |
|
68
|
|
|
* |
|
69
|
|
|
* This function is a helper function for join with type set to `left`. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array|StructuredQuery|string $withSource Source to join with |
|
72
|
|
|
* @param array|StructuredQuery|string|null $condition Condition to join on |
|
73
|
|
|
* @return $this |
|
74
|
|
|
* @see JoinTrait::join() |
|
75
|
|
|
*/ |
|
76
|
6 |
|
public function leftJoin( |
|
77
|
|
|
StructuredQuery|array|string $withSource, |
|
78
|
|
|
StructuredQuery|array|string|null $condition |
|
79
|
|
|
): static |
|
80
|
|
|
{ |
|
81
|
6 |
|
return $this->join('left', $withSource, $condition); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add an RIGHT type join to the query. |
|
86
|
|
|
* |
|
87
|
|
|
* This function is a helper function for join with type set to `right`. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array|StructuredQuery|string $withSource Source to join with |
|
90
|
|
|
* @param array|StructuredQuery|string|null $condition Condition to join on |
|
91
|
|
|
* @return $this |
|
92
|
|
|
* @see JoinTrait::join() |
|
93
|
|
|
*/ |
|
94
|
6 |
|
public function rightJoin( |
|
95
|
|
|
StructuredQuery|array|string $withSource, |
|
96
|
|
|
StructuredQuery|array|string|null $condition |
|
97
|
|
|
): static |
|
98
|
|
|
{ |
|
99
|
6 |
|
return $this->join('right', $withSource, $condition); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Add an FULL OUTER type join to the query. |
|
104
|
|
|
* |
|
105
|
|
|
* This function is a helper function for join with type set to `full outer`. |
|
106
|
|
|
* |
|
107
|
|
|
* @param array|StructuredQuery|string $withSource Source to join with |
|
108
|
|
|
* @param array|StructuredQuery|string|null $condition Condition to join on |
|
109
|
|
|
* @return $this |
|
110
|
|
|
* @see JoinTrait::join() |
|
111
|
|
|
*/ |
|
112
|
6 |
|
public function fullOuterJoin( |
|
113
|
|
|
StructuredQuery|array|string $withSource, |
|
114
|
|
|
StructuredQuery|array|string|null $condition |
|
115
|
|
|
): static |
|
116
|
|
|
{ |
|
117
|
6 |
|
return $this->join('full outer', $withSource, $condition); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|