1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Query\Builder as IlluminateBuilder; |
8
|
|
|
use Illuminate\Database\Query\Expression; |
9
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder; |
10
|
|
|
|
11
|
|
|
trait CompilesUnions |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Compile the "union" queries attached to the main query. |
15
|
|
|
* |
16
|
|
|
* @param Builder $query |
17
|
|
|
* @param string $aql |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
protected function compileUnions(IlluminateBuilder $query, $firstQuery = '') |
21
|
|
|
{ |
22
|
|
|
$unionResultsId = 'union'.$query->getQueryId().'Results'; |
23
|
|
|
$unionDocId = 'union'.$query->getQueryId().'Results'; |
24
|
|
|
|
25
|
|
|
$firstQuery = $this->wrapUnion($firstQuery); |
|
|
|
|
26
|
|
|
$unions = ''; |
27
|
|
|
foreach ($query->unions as $union) { |
28
|
|
|
$prefix = ($unions !== '') ? $unions : $firstQuery; |
29
|
|
|
$unions = $this->compileUnion($union, $prefix); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$aql = 'LET '.$unionResultsId.' = '.$unions |
33
|
|
|
.' FOR '.$unionDocId.'Doc IN '.$unionResultsId; |
34
|
|
|
$aql .= ' RETURN '.$unionDocId.'Doc'; |
35
|
|
|
|
36
|
|
|
// if (! empty($query->unionOrders)) { |
37
|
|
|
// $sql .= ' '.$this->compileOrders($query, $query->unionOrders); |
38
|
|
|
// } |
39
|
|
|
// |
40
|
|
|
// if (isset($query->unionLimit)) { |
41
|
|
|
// $sql .= ' '.$this->compileLimit($query, $query->unionLimit); |
42
|
|
|
// } |
43
|
|
|
// |
44
|
|
|
// if (isset($query->unionOffset)) { |
45
|
|
|
// $sql .= ' '.$this->compileOffset($query, $query->unionOffset); |
46
|
|
|
// } |
47
|
|
|
|
48
|
|
|
return $aql; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Compile a single union statement. |
53
|
|
|
* |
54
|
|
|
* @param array $union |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
protected function compileUnion(array $union, string $aql = '') |
58
|
|
|
{ |
59
|
|
|
$unionType = $union['all'] ? 'UNION' : 'UNION_DISTINCT'; |
60
|
|
|
|
61
|
|
|
return $unionType.'('.$aql.', '.$this->wrapUnion($union['query']->toSql()).')'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Compile the "union" queries attached to the main query. |
66
|
|
|
* |
67
|
|
|
* @param IlluminateBuilder $query |
68
|
|
|
* @param string $aql |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function compileUnionsX(IlluminateBuilder $query, $aql = '') |
72
|
|
|
{ |
73
|
|
|
$unionType = $query->unions[0]['all'] ? 'UNION' : 'UNION_DISTINCT'; |
|
|
|
|
74
|
|
|
|
75
|
|
|
$unions = []; |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
foreach ($query->unions as $union) { |
79
|
|
|
ray('compileUnions', $union['query']); |
80
|
|
|
$unions[] = $this->wrapUnion($union['query']->toSql()); |
81
|
|
|
} |
82
|
|
|
$unions[] = $this->wrapUnion($aql); |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
// if (!empty($query->unionOrders)) { |
86
|
|
|
// $sql .= ' ' . $this->compileOrders($query, $query->unionOrders); |
87
|
|
|
// } |
88
|
|
|
// |
89
|
|
|
// if (isset($query->unionLimit)) { |
90
|
|
|
// $sql .= ' ' . $this->compileLimit($query, $query->unionLimit); |
91
|
|
|
// } |
92
|
|
|
// |
93
|
|
|
// if (isset($query->unionOffset)) { |
94
|
|
|
// $sql .= ' ' . $this->compileOffset($query, $query->unionOffset); |
95
|
|
|
// } |
96
|
|
|
// |
97
|
|
|
// return ltrim($sql); |
98
|
|
|
return $aql; |
99
|
|
|
} |
100
|
|
|
} |