1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
9
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Grammar; |
12
|
|
|
|
13
|
|
|
trait BuildsSubqueries |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* IN SQL subqueries in selects and where's need to return a single value, |
17
|
|
|
* whereas subqueries in joins return an object. This variable lets the |
18
|
|
|
* compiler know how to return a single value. |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
1 |
|
public bool $returnSingleValue = false; |
23
|
|
|
|
24
|
1 |
|
/** |
25
|
|
|
* Creates a subquery and parse it. |
26
|
|
|
* |
27
|
1 |
|
* @param \Closure|IlluminateQueryBuilder|IlluminateEloquentBuilder|string $query |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
1 |
|
protected function createSub($query, bool $returnSingleValue = false) |
31
|
|
|
{ |
32
|
|
|
// If the given query is a Closure, we will execute it while passing in a new |
33
|
1 |
|
// query instance to the Closure. This will give the developer a chance to |
34
|
|
|
// format and work with the query before we cast it to a raw SQL string. |
35
|
1 |
|
if ($query instanceof Closure) { |
36
|
|
|
$callback = $query; |
37
|
|
|
|
38
|
|
|
$callback($query = $this->forSubQuery()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($returnSingleValue) { |
42
|
|
|
$query->returnSingleValue = $returnSingleValue; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->parseSub($query); |
46
|
2 |
|
} |
47
|
|
|
|
48
|
2 |
|
/** |
49
|
1 |
|
* Create a new query instance for sub-query. |
50
|
1 |
|
* |
51
|
|
|
* @return \Illuminate\Database\Query\Builder |
52
|
1 |
|
*/ |
53
|
|
|
protected function forSubQuery() |
54
|
|
|
{ |
55
|
1 |
|
return $this->newQuery(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function hasLimitOfOne(IlluminateQueryBuilder $query) |
59
|
|
|
{ |
60
|
1 |
|
if (isset($query->limit) && $query->limit == 1) { |
61
|
1 |
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Parse the subquery into AQL and bindings. |
69
|
|
|
* |
70
|
|
|
* @param mixed $query |
71
|
|
|
* @return array |
72
|
|
|
* |
73
|
|
|
* @throws \InvalidArgumentException |
74
|
|
|
*/ |
75
|
|
|
protected function parseSub($query) |
76
|
|
|
{ |
77
|
|
|
if ($query instanceof self || $query instanceof EloquentBuilder || $query instanceof Relation) { |
78
|
|
|
$this->exchangeTableAliases($query); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->importBindings($query); |
|
|
|
|
81
|
|
|
|
82
|
|
|
assert($this->grammar instanceof Grammar); |
83
|
|
|
$queryString = $this->grammar->wrapSubquery($query->toSql()); |
|
|
|
|
84
|
|
|
|
85
|
|
|
if ($this->hasLimitOfOne($query)) { |
|
|
|
|
86
|
|
|
$queryString = 'FIRST(' . $queryString . ')'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return [$queryString, $query->getBindings()]; |
|
|
|
|
90
|
|
|
} elseif (is_string($query)) { |
91
|
|
|
return [$query, []]; |
92
|
|
|
} else { |
93
|
|
|
throw new InvalidArgumentException( |
94
|
|
|
'A subquery must be a query builder instance, a Relation, a Closure, or a string.' |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|