-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 💡 Split into traits and drop stale versions support
- Loading branch information
Showing
17 changed files
with
234 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/vendor/ | ||
/.idea/ | ||
.php_cs.cache | ||
.phpunit.result.cache | ||
/.phpunit.cache/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
bootstrap="vendor/autoload.php" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false"> | ||
<coverage> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix="Test.php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Database/Query/Concerns/BuildsGroupLimitQueriesByMultipleColumnPartition.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Mpyw\ComposhipsEagerLimit\Database\Query\Concerns; | ||
|
||
use Staudenmeir\EloquentEagerLimit\Traits\BuildsGroupLimitQueries; | ||
|
||
/** | ||
* Trait BuildsGroupLimitQueriesByMultipleColumnPartition | ||
* | ||
* @mixin \Illuminate\Database\Query\Builder | ||
*/ | ||
trait BuildsGroupLimitQueriesByMultipleColumnPartition | ||
{ | ||
use BuildsGroupLimitQueries; | ||
|
||
/** | ||
* Execute the query as a "select" statement. | ||
* | ||
* @param array $columns | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
public function get($columns = ['*']) | ||
{ | ||
$items = parent::get($columns); | ||
|
||
if (!$this->groupLimit) { | ||
return $items; | ||
} | ||
|
||
$keys = ['laravel_row']; | ||
|
||
if (is_array($this->groupLimit['column'])) { | ||
foreach ($this->groupLimit['column'] as $i => $column) { | ||
$keys[] = "@laravel_partition_$i := " . $this->grammar->wrap(last(explode('.', $column))); | ||
$keys[] = "@laravel_partition_$i := " . $this->grammar->wrap('pivot_' . last(explode('.', $column))); | ||
} | ||
} else { | ||
$keys[] = '@laravel_partition := ' . $this->grammar->wrap(last(explode('.', $this->groupLimit['column']))); | ||
$keys[] = '@laravel_partition := ' . $this->grammar->wrap('pivot_' . last(explode('.', $this->groupLimit['column']))); | ||
} | ||
|
||
foreach ($items as $item) { | ||
foreach ($keys as $key) { | ||
unset($item->$key); | ||
} | ||
} | ||
|
||
return $items; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...rammar/Concerns/CompilesGroupLimitByMultipleColumnPartitionWithInternalRenameAsParent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Mpyw\ComposhipsEagerLimit\Database\Query\Grammar\Concerns; | ||
|
||
/** | ||
* Trait CompilesGroupLimitByMultipleColumnPartitionWithInternalRenameAsParent | ||
* | ||
* @internal | ||
*/ | ||
trait CompilesGroupLimitByMultipleColumnPartitionWithInternalRenameAsParent | ||
{ | ||
use CompilesGroupLimitByMultipleColumnPartition { | ||
compileRowNumber as compileRowNumberParent; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/Database/Query/Grammar/Concerns/CompilesMySqlGroupLimitByMultipleColumnPartition.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Mpyw\ComposhipsEagerLimit\Database\Query\Grammar\Concerns; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
use Staudenmeir\EloquentEagerLimit\Grammars\Traits\CompilesMySqlGroupLimit; | ||
|
||
/** | ||
* Trait CompilesMySqlGroupLimitByMultipleColumnPartition | ||
* | ||
* @mixin \Illuminate\Database\Query\Grammars\MySqlGrammar | ||
*/ | ||
trait CompilesMySqlGroupLimitByMultipleColumnPartition | ||
{ | ||
use CompilesMySqlGroupLimit, CompilesGroupLimitByMultipleColumnPartition { | ||
CompilesGroupLimitByMultipleColumnPartition::compileRowNumber insteadof CompilesMySqlGroupLimit; | ||
CompilesMySqlGroupLimit::compileGroupLimit insteadof CompilesGroupLimitByMultipleColumnPartition; | ||
} | ||
|
||
/** | ||
* Compile a group limit clause for MySQL < 8.0. | ||
* | ||
* Derived from https://softonsofa.com/tweaking-eloquent-relations-how-to-get-n-related-models-per-parent/. | ||
* | ||
* @param \Illuminate\Database\Query\Builder|\Mpyw\ComposhipsEagerLimit\Database\Query\Builder $query | ||
* @return string | ||
*/ | ||
protected function compileLegacyGroupLimit(Builder $query) | ||
{ | ||
$limit = (int)$query->groupLimit['value'] + (int)$query->offset; | ||
$offset = $query->offset; | ||
|
||
$query->offset = null; | ||
$query->orders = (array)$query->orders; | ||
|
||
$partitionExpressions = []; | ||
$partitionAssignments = []; | ||
$partitionInitializations = []; | ||
$partitionOrders = []; | ||
|
||
if (is_array($query->groupLimit['column'])) { | ||
foreach ($query->groupLimit['column'] as $i => $column) { | ||
$wrappedColumn = $this->wrap(last(explode('.', $column))); | ||
|
||
$partitionExpressions[] = "@laravel_partition_$i = $wrappedColumn"; | ||
$partitionAssignments[] = "@laravel_partition_$i := $wrappedColumn"; | ||
$partitionInitializations[] = "@laravel_partition_$i := 0"; | ||
$partitionOrders[] = ['column' => $column, 'direction' => 'asc']; | ||
} | ||
} else { | ||
$wrappedColumn = $this->wrap(last(explode('.', $query->groupLimit['column']))); | ||
|
||
$partitionExpressions[] = "@laravel_partition = $wrappedColumn"; | ||
$partitionAssignments[] = "@laravel_partition := $wrappedColumn"; | ||
$partitionInitializations[] = '@laravel_partition := 0'; | ||
$partitionOrders[] = ['column' => $query->groupLimit['column'], 'direction' => 'asc']; | ||
} | ||
|
||
$partition = sprintf( | ||
', @laravel_row := if(%s, @laravel_row + 1, 1) as laravel_row, %s', | ||
implode(' and ', $partitionExpressions), | ||
implode(', ', $partitionAssignments) | ||
); | ||
|
||
array_splice($query->orders, 0, 0, $partitionOrders); | ||
|
||
$components = $this->compileComponents($query); | ||
|
||
$sql = $this->concatenate($components); | ||
|
||
$from = sprintf( | ||
'(select @laravel_row := 0, %s) as laravel_vars, (%s) as laravel_table', | ||
implode(', ', $partitionInitializations), | ||
$sql | ||
); | ||
|
||
$sql = 'select laravel_table.*' . $partition . ' from ' . $from . ' having laravel_row <= ' . $limit; | ||
|
||
if ($offset !== null) { | ||
$sql .= ' and laravel_row > ' . (int)$offset; | ||
} | ||
|
||
return $sql . ' order by laravel_row'; | ||
} | ||
} |
Oops, something went wrong.