|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpyw\ComposhipsEagerLimit\Database\Query\Grammar\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder; |
|
6
|
|
|
use Staudenmeir\EloquentEagerLimit\Grammars\Traits\CompilesMySqlGroupLimit; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Trait CompilesMySqlGroupLimitByMultipleColumnPartition |
|
10
|
|
|
* |
|
11
|
|
|
* @mixin \Illuminate\Database\Query\Grammars\MySqlGrammar |
|
12
|
|
|
*/ |
|
13
|
|
|
trait CompilesMySqlGroupLimitByMultipleColumnPartition |
|
14
|
|
|
{ |
|
15
|
|
|
use CompilesMySqlGroupLimit, CompilesGroupLimitByMultipleColumnPartition { |
|
16
|
|
|
CompilesGroupLimitByMultipleColumnPartition::compileRowNumber insteadof CompilesMySqlGroupLimit; |
|
17
|
|
|
CompilesMySqlGroupLimit::compileGroupLimit insteadof CompilesGroupLimitByMultipleColumnPartition; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Compile a group limit clause for MySQL < 8.0. |
|
22
|
|
|
* |
|
23
|
|
|
* Derived from https://softonsofa.com/tweaking-eloquent-relations-how-to-get-n-related-models-per-parent/. |
|
24
|
|
|
* |
|
25
|
|
|
* @param \Illuminate\Database\Query\Builder|\Mpyw\ComposhipsEagerLimit\Database\Query\Builder $query |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function compileLegacyGroupLimit(Builder $query) |
|
29
|
|
|
{ |
|
30
|
|
|
$limit = (int)$query->groupLimit['value'] + (int)$query->offset; |
|
|
|
|
|
|
31
|
|
|
$offset = $query->offset; |
|
32
|
|
|
|
|
33
|
|
|
$query->offset = null; |
|
34
|
|
|
$query->orders = (array)$query->orders; |
|
35
|
|
|
|
|
36
|
|
|
$partitionExpressions = []; |
|
37
|
|
|
$partitionAssignments = []; |
|
38
|
|
|
$partitionInitializations = []; |
|
39
|
|
|
$partitionOrders = []; |
|
40
|
|
|
|
|
41
|
|
|
if (is_array($query->groupLimit['column'])) { |
|
42
|
|
|
foreach ($query->groupLimit['column'] as $i => $column) { |
|
43
|
|
|
$wrappedColumn = $this->wrap(last(explode('.', $column))); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$partitionExpressions[] = "@laravel_partition_$i = $wrappedColumn"; |
|
46
|
|
|
$partitionAssignments[] = "@laravel_partition_$i := $wrappedColumn"; |
|
47
|
|
|
$partitionInitializations[] = "@laravel_partition_$i := 0"; |
|
48
|
|
|
$partitionOrders[] = ['column' => $column, 'direction' => 'asc']; |
|
49
|
|
|
} |
|
50
|
|
|
} else { |
|
51
|
|
|
$wrappedColumn = $this->wrap(last(explode('.', $query->groupLimit['column']))); |
|
52
|
|
|
|
|
53
|
|
|
$partitionExpressions[] = "@laravel_partition = $wrappedColumn"; |
|
54
|
|
|
$partitionAssignments[] = "@laravel_partition := $wrappedColumn"; |
|
55
|
|
|
$partitionInitializations[] = '@laravel_partition := 0'; |
|
56
|
|
|
$partitionOrders[] = ['column' => $query->groupLimit['column'], 'direction' => 'asc']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$partition = sprintf( |
|
60
|
|
|
', @laravel_row := if(%s, @laravel_row + 1, 1) as laravel_row, %s', |
|
61
|
|
|
implode(' and ', $partitionExpressions), |
|
62
|
|
|
implode(', ', $partitionAssignments) |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
array_splice($query->orders, 0, 0, $partitionOrders); |
|
66
|
|
|
|
|
67
|
|
|
$components = $this->compileComponents($query); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$sql = $this->concatenate($components); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$from = sprintf( |
|
72
|
|
|
'(select @laravel_row := 0, %s) as laravel_vars, (%s) as laravel_table', |
|
73
|
|
|
implode(', ', $partitionInitializations), |
|
74
|
|
|
$sql |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$sql = 'select laravel_table.*' . $partition . ' from ' . $from . ' having laravel_row <= ' . $limit; |
|
78
|
|
|
|
|
79
|
|
|
if ($offset !== null) { |
|
|
|
|
|
|
80
|
|
|
$sql .= ' and laravel_row > ' . (int)$offset; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $sql . ' order by laravel_row'; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|