-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d1e3aa
commit a8104ae
Showing
31 changed files
with
439 additions
and
381 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
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
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
2 changes: 1 addition & 1 deletion
2
src/Grammars/CompilesGroupLimit.php → src/Grammars/Traits/CompilesGroupLimit.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
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,89 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\EloquentEagerLimit\Grammars\Traits; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Support\Str; | ||
use PDO; | ||
|
||
trait CompilesMySqlGroupLimit | ||
{ | ||
use CompilesGroupLimit { | ||
compileGroupLimit as compileGroupLimitParent; | ||
} | ||
|
||
/** | ||
* Determine whether to use a group limit clause for MySQL < 8.0. | ||
* | ||
* @param \Illuminate\Database\Query\Builder $query | ||
* @return bool | ||
*/ | ||
public function useLegacyGroupLimit(Builder $query) | ||
{ | ||
$version = $query->getConnection()->getReadPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); | ||
|
||
return version_compare($version, '8.0.11') < 0 && !Str::contains($version, 'MariaDB'); | ||
} | ||
|
||
/** | ||
* Compile a group limit clause. | ||
* | ||
* @param \Illuminate\Database\Query\Builder $query | ||
* @return string | ||
*/ | ||
protected function compileGroupLimit(Builder $query) | ||
{ | ||
return $this->useLegacyGroupLimit($query) | ||
? $this->compileLegacyGroupLimit($query) | ||
: $this->compileGroupLimitParent($query); | ||
} | ||
|
||
/** | ||
* 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 $query | ||
* @return string | ||
*/ | ||
protected function compileLegacyGroupLimit(Builder $query) | ||
{ | ||
$limit = (int) $query->groupLimit['value']; | ||
|
||
$offset = $query->offset; | ||
|
||
if (isset($offset)) { | ||
$limit += (int) $offset; | ||
|
||
$query->offset = null; | ||
} | ||
|
||
$column = last(explode('.', $query->groupLimit['column'])); | ||
|
||
$column = $this->wrap($column); | ||
|
||
$partition = ', @laravel_row := if(@laravel_partition = '.$column.', @laravel_row + 1, 1) as laravel_row'; | ||
|
||
$partition .= ', @laravel_partition := '.$column; | ||
|
||
$orders = (array) $query->orders; | ||
|
||
array_unshift($orders, ['column' => $query->groupLimit['column'], 'direction' => 'asc']); | ||
|
||
$query->orders = $orders; | ||
|
||
$components = $this->compileComponents($query); | ||
|
||
$sql = $this->concatenate($components); | ||
|
||
$from = '(select @laravel_row := 0, @laravel_partition := 0) as laravel_vars, ('.$sql.') as laravel_table'; | ||
|
||
$sql = 'select laravel_table.*'.$partition.' from '.$from.' having laravel_row <= '.$limit; | ||
|
||
if (isset($offset)) { | ||
$sql .= ' and laravel_row > '.(int) $offset; | ||
} | ||
|
||
return $sql.' order by laravel_row'; | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\EloquentEagerLimit\Grammars\Traits; | ||
|
||
trait CompilesPostgresGroupLimit | ||
{ | ||
use CompilesGroupLimit; | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\EloquentEagerLimit\Grammars\Traits; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
use PDO; | ||
|
||
trait CompilesSQLiteGroupLimit | ||
{ | ||
use CompilesGroupLimit { | ||
compileGroupLimit as compileGroupLimitParent; | ||
} | ||
|
||
/** | ||
* Compile a group limit clause. | ||
* | ||
* @param \Illuminate\Database\Query\Builder $query | ||
* @return string | ||
*/ | ||
protected function compileGroupLimit(Builder $query) | ||
{ | ||
$version = $query->getConnection()->getReadPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); | ||
|
||
if (version_compare($version, '3.25.0') >= 0) { | ||
return $this->compileGroupLimitParent($query); | ||
} | ||
|
||
$query->groupLimit = null; | ||
|
||
return $this->compileSelect($query); | ||
} | ||
} |
Oops, something went wrong.