Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"illuminate/database": ">=5.6 <12.0"
"illuminate/database": ">=5.6 <13.0"
},
"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;
}
47 changes: 47 additions & 0 deletions tests/Unit/LimitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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()
{
if (getLaravelVersion() < 11.0) {
$this->markTestIncomplete('This test will not work on laravel 10.x or 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);
}
}