Skip to content

Commit

Permalink
Bug fix: order bindings are select bindings (#35)
Browse files Browse the repository at this point in the history
* Move order bindings to select bindings

* Add extra tests

* Small fix

(cherry picked from commit 439135c)
  • Loading branch information
gdebrauwer authored and staudenmeir committed Apr 2, 2021
1 parent 6edf9bf commit c188d86
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Grammars/CompilesGroupLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function compileSelect(Builder $query)
*/
protected function compileGroupLimit(Builder $query)
{
$selectBindings = array_merge($query->getRawBindings()['select'], $query->getRawBindings()['order']);

$query->setBindings($selectBindings, 'select');
$query->setBindings([], 'order');

$limit = (int) $query->groupLimit['value'];

$offset = $query->offset;
Expand Down
40 changes: 40 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public function testGroupLimitMySql()
$expected = 'select * from (select *, row_number() over (partition by `user_id`) as laravel_row from `posts`) as laravel_table where laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());

$builder = $this->getBuilder('MySql');
$builder->getConnection()->getReadPdo()->method('getAttribute')->willReturn('8.0.11');
$builder->from('posts')->groupLimit(10, 'user_id')->offset(1)->where('id', '>', 'where-binding')->orderByRaw('?', ['order-binding']);
$expected = 'select * from (select *, row_number() over (partition by `user_id` order by ?) as laravel_row from `posts` where `id` > ?) as laravel_table where laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());
$this->assertEquals(['order-binding', 'where-binding'], $builder->getBindings());

$builder = $this->getBuilder('MySql');
$builder->getConnection()->getReadPdo()->method('getAttribute')->willReturn('5.7.9');
$builder->from('posts')->groupLimit(10, 'user_id');
Expand All @@ -46,6 +53,13 @@ public function testGroupLimitMySql()
$builder->from('posts')->groupLimit(10, 'user_id')->offset(1);
$expected = 'select laravel_table.*, @laravel_row := if(@laravel_partition = `user_id`, @laravel_row + 1, 1) as laravel_row, @laravel_partition := `user_id` from (select @laravel_row := 0, @laravel_partition := 0) as laravel_vars, (select * from `posts` order by `user_id` asc) as laravel_table having laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());

$builder = $this->getBuilder('MySql');
$builder->getConnection()->getReadPdo()->method('getAttribute')->willReturn('5.7.9');
$builder->from('posts')->groupLimit(10, 'user_id')->offset(1)->where('id', '>', 'where-binding')->orderByRaw('?', ['order-binding']);
$expected = 'select laravel_table.*, @laravel_row := if(@laravel_partition = `user_id`, @laravel_row + 1, 1) as laravel_row, @laravel_partition := `user_id` from (select @laravel_row := 0, @laravel_partition := 0) as laravel_vars, (select * from `posts` where `id` > ? order by `user_id` asc, ?) as laravel_table having laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());
$this->assertEquals(['where-binding', 'order-binding'], $builder->getBindings());
}

public function testGroupLimitMariaDb()
Expand All @@ -67,6 +81,13 @@ public function testGroupLimitMariaDb()
$builder->from('posts')->groupLimit(10, 'user_id')->offset(1);
$expected = 'select * from (select *, row_number() over (partition by `user_id`) as laravel_row from `posts`) as laravel_table where laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());

$builder = $this->getBuilder('MySql');
$builder->getConnection()->getReadPdo()->method('getAttribute')->willReturn('5.5.5-10.3.9-MariaDB');
$builder->from('posts')->groupLimit(10, 'user_id')->offset(1)->where('id', '>', 'where-binding')->orderByRaw('?', ['order-binding']);
$expected = 'select * from (select *, row_number() over (partition by `user_id` order by ?) as laravel_row from `posts` where `id` > ?) as laravel_table where laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());
$this->assertEquals(['order-binding', 'where-binding'], $builder->getBindings());
}

public function testGroupLimitPostgres()
Expand All @@ -85,6 +106,12 @@ public function testGroupLimitPostgres()
$builder->from('posts')->groupLimit(10, 'user_id')->offset(1);
$expected = 'select * from (select *, row_number() over (partition by "user_id") as laravel_row from "posts") as laravel_table where laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());

$builder = $this->getBuilder('Postgres');
$builder->from('posts')->groupLimit(10, 'user_id')->offset(1)->where('id', '>', 'where-binding')->orderByRaw('?', ['order-binding']);
$expected = 'select * from (select *, row_number() over (partition by "user_id" order by ?) as laravel_row from "posts" where "id" > ?) as laravel_table where laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());
$this->assertEquals(['order-binding', 'where-binding'], $builder->getBindings());
}

public function testGroupLimitSQLite()
Expand All @@ -107,6 +134,13 @@ public function testGroupLimitSQLite()
$expected = 'select * from (select *, row_number() over (partition by "user_id") as laravel_row from "posts") as laravel_table where laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());

$builder = $this->getBuilder('SQLite');
$builder->getConnection()->getReadPdo()->method('getAttribute')->willReturn('3.25.0');
$builder->from('posts')->groupLimit(10, 'user_id')->offset(1)->where('id', '>', 'where-binding')->orderByRaw('?', ['order-binding']);
$expected = 'select * from (select *, row_number() over (partition by "user_id" order by ?) as laravel_row from "posts" where "id" > ?) as laravel_table where laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());
$this->assertEquals(['order-binding', 'where-binding'], $builder->getBindings());

$builder = $this->getBuilder('SQLite');
$builder->getConnection()->getReadPdo()->method('getAttribute')->willReturn('3.24.0');
$builder->from('posts')->groupLimit(10, 'user_id');
Expand All @@ -130,6 +164,12 @@ public function testGroupLimitSqlServer()
$builder->from('posts')->groupLimit(10, 'user_id')->offset(1);
$expected = 'select * from (select *, row_number() over (partition by [user_id] order by (select 0)) as laravel_row from [posts]) as laravel_table where laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());

$builder = $this->getBuilder('SqlServer');
$builder->from('posts')->groupLimit(10, 'user_id')->offset(1)->where('id', '>', 'where-binding')->orderByRaw('?', ['order-binding']);
$expected = 'select * from (select *, row_number() over (partition by [user_id] order by ?) as laravel_row from [posts] where [id] > ?) as laravel_table where laravel_row <= 11 and laravel_row > 1 order by laravel_row';
$this->assertEquals($expected, $builder->toSql());
$this->assertEquals(['order-binding', 'where-binding'], $builder->getBindings());
}

protected function getBuilder($database)
Expand Down

0 comments on commit c188d86

Please sign in to comment.