CompilesMySqlGroupLimitByMultipleColumnPartition   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 40
c 1
b 0
f 0
dl 0
loc 71
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A compileLegacyGroupLimit() 0 56 4
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;
0 ignored issues
show
Bug introduced by
The property groupLimit does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
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)));
0 ignored issues
show
Bug introduced by
It seems like wrap() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
                /** @scrutinizer ignore-call */ 
44
                $wrappedColumn = $this->wrap(last(explode('.', $column)));
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like compileComponents() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        /** @scrutinizer ignore-call */ 
68
        $components = $this->compileComponents($query);
Loading history...
68
69
        $sql = $this->concatenate($components);
0 ignored issues
show
Bug introduced by
It seems like concatenate() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        /** @scrutinizer ignore-call */ 
70
        $sql = $this->concatenate($components);
Loading history...
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) {
0 ignored issues
show
introduced by
The condition $offset !== null is always true.
Loading history...
80
            $sql .= ' and laravel_row > ' . (int)$offset;
81
        }
82
83
        return $sql . ' order by laravel_row';
84
    }
85
}
86