|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of UnderQuery package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Beniamin Jonatan Šimko |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Phuria\UnderQuery\QueryBuilder\Clause; |
|
13
|
|
|
|
|
14
|
|
|
use Phuria\UnderQuery\JoinType; |
|
15
|
|
|
use Phuria\UnderQuery\Language\Expression\RelativeClause; |
|
16
|
|
|
use Phuria\UnderQuery\Table\AbstractTable; |
|
17
|
|
|
use Phuria\UnderQuery\Table\JoinMetadata; |
|
18
|
|
|
use Phuria\UnderQuery\Table\TableInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Beniamin Jonatan Šimko <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
trait JoinTrait |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var array $joinTables |
|
27
|
|
|
*/ |
|
28
|
|
|
private $joinTables = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param mixed $table |
|
32
|
|
|
* @param string|null $alias |
|
33
|
|
|
* |
|
34
|
|
|
* @return TableInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract public function createTable($table, $alias = null); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $joinType |
|
40
|
|
|
* @param mixed $table |
|
41
|
|
|
* @param string|null $alias |
|
42
|
|
|
* @param string|null $joinOn |
|
43
|
|
|
* |
|
44
|
|
|
* @return TableInterface |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function doJoin($joinType, $table, $alias = null, $joinOn = null) |
|
47
|
|
|
{ |
|
48
|
3 |
|
$this->joinTables[] = $tableObject = $this->createTable($table, $alias); |
|
49
|
|
|
|
|
50
|
3 |
|
$joinMetadata = new JoinMetadata(); |
|
51
|
3 |
|
$joinMetadata->setJoinType($joinType); |
|
52
|
|
|
|
|
53
|
3 |
|
if ($joinOn) { |
|
|
|
|
|
|
54
|
1 |
|
$relativeOn = new RelativeClause($tableObject, $joinOn, RelativeClause::SELF_DIRECTIVE); |
|
55
|
1 |
|
$joinMetadata->setJoinOn($relativeOn); |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
$tableObject->setJoinMetadata($joinMetadata); |
|
59
|
3 |
|
is_callable($table) && $table($tableObject, $joinMetadata); |
|
60
|
|
|
|
|
61
|
3 |
|
return $tableObject; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param mixed $table |
|
66
|
|
|
* @param string|null $alias |
|
67
|
|
|
* @param string|null $joinOn |
|
68
|
|
|
* |
|
69
|
|
|
* @return TableInterface |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function join($table, $alias = null, $joinOn = null) |
|
72
|
|
|
{ |
|
73
|
3 |
|
return $this->doJoin(JoinType::JOIN, $table, $alias, $joinOn); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param mixed $table |
|
78
|
|
|
* @param string|null $alias |
|
79
|
|
|
* @param string|null $joinOn |
|
80
|
|
|
* |
|
81
|
|
|
* @return AbstractTable |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function straightJoin($table, $alias = null, $joinOn = null) |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->doJoin(JoinType::STRAIGHT_JOIN, $table, $alias, $joinOn); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param mixed $table |
|
90
|
|
|
* @param string|null $alias |
|
91
|
|
|
* @param string|null $joinOn |
|
92
|
|
|
* |
|
93
|
|
|
* @return AbstractTable |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function crossJoin($table, $alias = null, $joinOn = null) |
|
96
|
|
|
{ |
|
97
|
1 |
|
return $this->doJoin(JoinType::CROSS_JOIN, $table, $alias, $joinOn); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param mixed $table |
|
102
|
|
|
* @param string|null $alias |
|
103
|
|
|
* @param string|null $joinOn |
|
104
|
|
|
* |
|
105
|
|
|
* @return AbstractTable |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function leftJoin($table, $alias = null, $joinOn = null) |
|
108
|
|
|
{ |
|
109
|
1 |
|
return $this->doJoin(JoinType::LEFT_JOIN, $table, $alias, $joinOn); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param mixed $table |
|
114
|
|
|
* @param string|null $alias |
|
115
|
|
|
* @param string|null $joinOn |
|
116
|
|
|
* |
|
117
|
|
|
* @return AbstractTable |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function rightJoin($table, $alias = null, $joinOn = null) |
|
120
|
|
|
{ |
|
121
|
1 |
|
return $this->doJoin(JoinType::RIGHT_JOIN, $table, $alias, $joinOn); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param mixed $table |
|
126
|
|
|
* @param string|null $alias |
|
127
|
|
|
* @param string|null $joinOn |
|
128
|
|
|
* |
|
129
|
|
|
* @return AbstractTable |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function innerJoin($table, $alias = null, $joinOn = null) |
|
132
|
|
|
{ |
|
133
|
1 |
|
return $this->doJoin(JoinType::INNER_JOIN, $table, $alias, $joinOn); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function getJoinTables() |
|
140
|
|
|
{ |
|
141
|
1 |
|
return $this->joinTables; |
|
142
|
|
|
} |
|
143
|
|
|
} |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: