1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
9
|
|
|
use Illuminate\Database\Query\Expression; |
10
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder; |
11
|
|
|
use LaravelFreelancerNL\Aranguent\Query\JoinClause; |
12
|
|
|
use LogicException; |
13
|
|
|
|
14
|
|
|
trait BuildsJoins |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Add a right join to the query. |
18
|
|
|
* |
19
|
|
|
* @param \Illuminate\Contracts\Database\Query\Expression|string $table |
20
|
|
|
* @param \Closure|string $first |
21
|
|
|
* @param string|null $operator |
22
|
|
|
* @param \Illuminate\Contracts\Database\Query\Expression|string|null $second |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
|
|
public function rightJoin($table, $first, $operator = null, $second = null) |
26
|
|
|
{ |
27
|
|
|
// TODO: right outer joins should be doable by basically transforming them to a left outer join. |
28
|
|
|
// The right join would need to be checked before generating the FOR clause on the first called collection |
29
|
|
|
// with the filter after that. |
30
|
|
|
// FOR x IN rightJoinCollection |
31
|
|
|
// FOR x IN firstCollection |
32
|
|
|
// FILTER ... |
33
|
|
|
throw new LogicException('This database driver does not support the rightJoin method.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add a subquery right join to the query. |
38
|
|
|
* |
39
|
|
|
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query |
40
|
|
|
* @param string $as |
41
|
|
|
* @param \Closure|string $first |
42
|
|
|
* @param string|null $operator |
43
|
|
|
* @param \Illuminate\Contracts\Database\Query\Expression|string|null $second |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
|
public function rightJoinSub($query, $as, $first, $operator = null, $second = null) |
47
|
|
|
{ |
48
|
|
|
throw new LogicException('This database driver does not support the rightJoinSub method.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add a "right join where" clause to the query. |
53
|
|
|
* |
54
|
|
|
* @param \Illuminate\Contracts\Database\Query\Expression|string $table |
55
|
|
|
* @param \Closure|string $first |
56
|
|
|
* @param string $operator |
57
|
|
|
* @param \Illuminate\Contracts\Database\Query\Expression|string $second |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function rightJoinWhere($table, $first, $operator, $second) |
61
|
|
|
{ |
62
|
|
|
throw new LogicException('This database driver does not support the rightJoinWhere method.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add a join clause to the query. |
68
|
|
|
* |
69
|
|
|
* The boolean argument flag is part of this method's API in Laravel. |
70
|
|
|
* |
71
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
72
|
|
|
* |
73
|
|
|
* @param mixed $table |
74
|
|
|
* @param \Closure|string $first |
75
|
|
|
* @param string|null $operator |
76
|
|
|
* @param string|null $second |
77
|
|
|
* @param string $type |
78
|
|
|
* @param bool $where |
79
|
|
|
*/ |
80
|
|
|
public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false): Builder |
81
|
|
|
{ |
82
|
|
|
$join = $this->newJoinClause($this, $type, $table); |
|
|
|
|
83
|
|
|
|
84
|
|
|
// If the first "column" of the join is really a Closure instance the developer |
85
|
|
|
// is trying to build a join with a complex "on" clause containing more than |
86
|
|
|
// one condition, so we'll add the join and call a Closure with the query. |
87
|
|
|
if ($first instanceof Closure) { |
88
|
|
|
$first($join); |
89
|
|
|
|
90
|
|
|
$this->joins[] = $join; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
if (!$first instanceof Closure) { |
93
|
|
|
// If the column is simply a string, we can assume the join simply has a basic |
94
|
|
|
// "on" clause with a single condition. So we will just build the join with |
95
|
|
|
// this simple join clauses attached to it. There is not a join callback. |
96
|
|
|
|
97
|
|
|
//where and on are the same for aql |
98
|
|
|
$method = $where ? 'where' : 'on'; |
99
|
|
|
|
100
|
|
|
$this->joins[] = $join->$method($first, $operator, $second); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add a subquery join clause to the query. |
108
|
|
|
* |
109
|
|
|
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query |
110
|
|
|
* @param string $as |
111
|
|
|
* @param \Closure|string $first |
112
|
|
|
* @param string|null $operator |
113
|
|
|
* @param string|null $second |
114
|
|
|
* @param string $type |
115
|
|
|
* @param bool $where |
116
|
|
|
* @return $this |
117
|
|
|
* |
118
|
|
|
* @throws \InvalidArgumentException |
119
|
|
|
*/ |
120
|
|
|
public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false) |
121
|
|
|
{ |
122
|
|
|
$query->importTableAliases($this); |
|
|
|
|
123
|
|
|
$query->importTableAliases([$as => $as]); |
124
|
|
|
|
125
|
|
|
[$query, $bindings] = $this->createSub($query); |
126
|
|
|
|
127
|
|
|
$this->bindings['join'] = array_merge($this->bindings['join'], $bindings); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $this->join(new Expression('(' . $query . ') as ' . $as), $first, $operator, $second, $type, $where); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get a new join clause. |
135
|
|
|
* |
136
|
|
|
* @param string $type |
137
|
|
|
* @param string $table |
138
|
|
|
*/ |
139
|
|
|
protected function newJoinClause(IlluminateQueryBuilder $parentQuery, $type, $table): JoinClause |
140
|
|
|
{ |
141
|
|
|
return new JoinClause($parentQuery, $type, $table); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Creates a subquery and parse it. |
146
|
|
|
* |
147
|
|
|
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
protected function createSub($query) |
151
|
|
|
{ |
152
|
|
|
// If the given query is a Closure, we will execute it while passing in a new |
153
|
|
|
// query instance to the Closure. This will give the developer a chance to |
154
|
|
|
// format and work with the query before we cast it to a raw SQL string. |
155
|
|
|
if ($query instanceof Closure) { |
156
|
|
|
$callback = $query; |
157
|
|
|
|
158
|
|
|
$callback($query = $this->forSubQuery()); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->parseSub($query); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|