Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support limits #180

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
}
],
"require": {
"illuminate/database": ">=5.6 <12.0"
"php": "^8.0",
"illuminate/database": "^11.0",
"laravel/serializable-closure": "^1.3|^2.0"
Copy link

@parallels999 parallels999 Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, the Laravel version constraint in your composer.json file would restrict compatibility with those versions.

Remove this line

-"php": "^8.0",
-"illuminate/database": "^11.0",
-"laravel/serializable-closure": "^1.3|^2.0"
+"illuminate/database": ">=5.6 <12.0"

Also merge it with master

},
"require-dev": {
"ext-sqlite3": "*",
Expand Down
18 changes: 17 additions & 1 deletion src/Compoships.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
namespace Awobaz\Compoships;

use Awobaz\Compoships\Database\Eloquent\Concerns\HasRelationships;
use Awobaz\Compoships\Database\Grammar\MySqlGrammar;
use Awobaz\Compoships\Database\Grammar\PostgresGrammar;
use Awobaz\Compoships\Database\Grammar\SQLiteGrammar;
use Awobaz\Compoships\Database\Grammar\SqlServerGrammar;
use Awobaz\Compoships\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Str;
use RuntimeException;

trait Compoships
{
Expand Down Expand Up @@ -45,6 +50,17 @@ protected function newBaseQueryBuilder()
{
$connection = $this->getConnection();

return new QueryBuilder($connection, $connection->getQueryGrammar(), $connection->getPostProcessor());
$grammar = match ($connection->getDriverName()) {
'mysql' => new MySqlGrammar(),
'pgsql' => new PostgresGrammar(),
'sqlite' => new SQLiteGrammar(),
'sqlsrv' => new SqlServerGrammar(),
default => throw new RuntimeException('This database is not supported.'),
};

$grammar->setConnection($connection);
$grammar = $connection->withTablePrefix($grammar);

return new QueryBuilder($connection, $grammar, $connection->getPostProcessor());
}
}
19 changes: 19 additions & 0 deletions src/Database/Grammar/Concerns/CompileRowNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Awobaz\Compoships\Database\Grammar\Concerns;

trait CompileRowNumber
{
protected function compileRowNumber($partition, $orders)
{
if (!is_array($partition)) {
return parent::compileRowNumber($partition, $orders);
}

$columns = implode(', ', array_map(fn ($p) => $this->wrap($p), $partition));

$over = trim('partition by '.$columns.' '.$orders);

return ', row_number() over ('.$over.') as '.$this->wrap('laravel_row');
}
}
11 changes: 11 additions & 0 deletions src/Database/Grammar/MySqlGrammar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Awobaz\Compoships\Database\Grammar;

use Awobaz\Compoships\Database\Grammar\Concerns\CompileRowNumber;
use Illuminate\Database\Query\Grammars\MySqlGrammar as BaseMySqlGrammar;

class MySqlGrammar extends BaseMySqlGrammar
{
use CompileRowNumber;
}
11 changes: 11 additions & 0 deletions src/Database/Grammar/PostgresGrammar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Awobaz\Compoships\Database\Grammar;

use Awobaz\Compoships\Database\Grammar\Concerns\CompileRowNumber;
use Illuminate\Database\Query\Grammars\PostgresGrammar as BasePostgresGrammar;

class PostgresGrammar extends BasePostgresGrammar
{
use CompileRowNumber;
}
11 changes: 11 additions & 0 deletions src/Database/Grammar/SQLiteGrammar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Awobaz\Compoships\Database\Grammar;

use Awobaz\Compoships\Database\Grammar\Concerns\CompileRowNumber;
use Illuminate\Database\Query\Grammars\SQLiteGrammar as BaseSQLiteGrammar;

class SQLiteGrammar extends BaseSQLiteGrammar
{
use CompileRowNumber;
}
11 changes: 11 additions & 0 deletions src/Database/Grammar/SqlServerGrammar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Awobaz\Compoships\Database\Grammar;

use Awobaz\Compoships\Database\Grammar\Concerns\CompileRowNumber;
use Illuminate\Database\Query\Grammars\SqlServerGrammar as BaseSqlServerGrammar;

class SqlServerGrammar extends BaseSqlServerGrammar
{
use CompileRowNumber;
}
43 changes: 43 additions & 0 deletions tests/Unit/LimitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Awobaz\Compoships\Tests\Unit;

use Awobaz\Compoships\Tests\Models\User;
use Awobaz\Compoships\Tests\TestCase\TestCase;
use Illuminate\Database\Eloquent\Model;

class LimitTest extends TestCase
{
/**
* @covers \Awobaz\Compoships\Database\Grammar\MySqlGrammar
*/
public function test_relation_limit()
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure these changes will work with previous versions of laravel

Add something like this

if (getLaravelVersion() < 11.0) {
    $this->markTestIncomplete('This test is broken on laravel 10.x and earlier!');
}

if (getLaravelVersion() < 8.0) {
$this->markTestIncomplete('This test is broken on laravel 7.x and earlier!');
}

Model::unguard();

$user = User::create([
'booking_id' => '123',
]);

for ($i = 0; $i < 5; $i++) {
$user->allocations()->create([
'booking_id' => '123',
]);
}

$allocations = $user->allocations()->get();
$this->assertCount(5, $allocations);

$user = $user->fresh();
$user->load([
'allocations' => fn ($query) => $query->limit(4),
]);
$this->assertCount(4, $user->allocations);

$user = $user->fresh();
$user->load([
'allocations' => fn ($query) => $query->limit(2),
]);
$this->assertCount(2, $user->allocations);
}
}