Skip to content

Commit

Permalink
Merge pull request #15 from robsontenorio/fix-belongsToMany-pagination
Browse files Browse the repository at this point in the history
Fix "top all" strategy with custom select
  • Loading branch information
jeandormehl authored Nov 11, 2019
2 parents 82f49cc + 7ed7d9a commit 8ff4b92
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Cache/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ protected function compileAnsiOffset(Builder $query, $components)
$components['orders'] = 'order by 1';
}

$components['columns'] = $this->compileOver('');
$components['columns'] = $this->compileOver($this->columnize($query->columns));
$sql = $this->concatenate($components);

return $this->compileTableExpression($sql, $query);
}

protected function compileOver($orderings)
{
return 'select top all *';
return "select top all {$orderings}";
}

protected function compileTableExpression($sql, $query)
Expand Down
132 changes: 132 additions & 0 deletions tests/CacheQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,25 @@ public function testUnions()
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
}

public function testUnionsWithCustomSelect()
{
$builder = $this->getBuilder();

$builder->select(['name', 'email'])
->from('users')
->where('id', '=', 1);

$builder->union(
$this->getBuilder()->select(['name', 'email'])->from('users')->where('id', '=', 2)
);

$this->assertEquals(
'select * from (select name, email from users where id = ?) as temp_table union select * from (select name, email from users where id = ?) as temp_table',
$builder->toSql()
);
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
}

public function testUnionAlls()
{
$builder = $this->getBuilder();
Expand All @@ -273,6 +292,25 @@ public function testUnionAlls()
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
}

public function testUnionAllsWithCustomSelect()
{
$builder = $this->getBuilder();

$builder->select(['name', 'email'])
->from('users')
->where('id', '=', 1);

$builder->unionAll(
$this->getBuilder()->select(['name', 'email'])->from('users')->where('id', '=', 2)
);

$this->assertEquals(
'select * from (select name, email from users where id = ?) as temp_table union all select * from (select name, email from users where id = ?) as temp_table',
$builder->toSql()
);
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
}

public function testMultipleUnions()
{
$builder = $this->getBuilder();
Expand All @@ -294,6 +332,27 @@ public function testMultipleUnions()
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());
}

public function testMultipleUnionsWithCustomSelect()
{
$builder = $this->getBuilder();

$builder->select(['name', 'email'])->from('users')->where('id', '=', 1);

$builder->union(
$this->getBuilder()->select(['name', 'email'])->from('users')->where('id', '=', 2)
);

$builder->union(
$this->getBuilder()->select(['name', 'email'])->from('users')->where('id', '=', 3)
);

$this->assertEquals(
'select * from (select name, email from users where id = ?) as temp_table union select * from (select name, email from users where id = ?) as temp_table union select * from (select name, email from users where id = ?) as temp_table',
$builder->toSql()
);
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());
}

public function testMultipleUnionAlls()
{
$builder = $this->getBuilder();
Expand All @@ -314,6 +373,26 @@ public function testMultipleUnionAlls()
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());
}

public function testMultipleUnionAllsWithCustomSelect()
{
$builder = $this->getBuilder();

$builder->select(['name', 'email'])->from('users')->where('id', '=', 1);

$builder->unionAll(
$this->getBuilder()->select(['name', 'email'])->from('users')->where('id', '=', 2)
);
$builder->unionAll(
$this->getBuilder()->select(['name', 'email'])->from('users')->where('id', '=', 3)
);

$this->assertEquals(
'select * from (select name, email from users where id = ?) as temp_table union all select * from (select name, email from users where id = ?) as temp_table union all select * from (select name, email from users where id = ?) as temp_table',
$builder->toSql()
);
$this->assertEquals([0 => 1, 1 => 2, 2 => 3], $builder->getBindings());
}

public function testSubSelectWhereIns()
{
$builder = $this->getBuilder();
Expand Down Expand Up @@ -496,6 +575,20 @@ public function testOffset()
);
}

public function testOffsetWithCustomSelect()
{
$builder = $this->getBuilder();
$connection = $builder->getConnection();

$connection->shouldReceive('getConfig')->andReturn('');
$builder->select(['name', 'email'])->from('users')->offset(10);

$this->assertEquals(
'select *, %vid from (select top all name, email from users order by 1) where %vid >= 11',
$builder->toSql()
);
}

public function testLimitsAndOffsets()
{
$builder = $this->getBuilder();
Expand Down Expand Up @@ -551,6 +644,20 @@ public function testLimitsAndOffsets()
);
}

public function testLimitsAndOffsetsWithCustomSelect()
{
$builder = $this->getBuilder();
$connection = $builder->getConnection();

$connection->shouldReceive('getConfig')->andReturn('');
$builder->select(['name', 'email'])->from('users')->offset(5)->limit(10);

$this->assertEquals(
'select *, %vid from (select top all name, email from users order by 1) where %vid between 6 and 15',
$builder->toSql()
);
}

public function testLimitAndOffsetToPaginateOne()
{
$builder = $this->getBuilder();
Expand All @@ -576,6 +683,31 @@ public function testLimitAndOffsetToPaginateOne()
);
}

public function testLimitAndOffsetToPaginateOneWithCustomSelect()
{
$builder = $this->getBuilder();
$connection = $builder->getConnection();

$connection->shouldReceive('getConfig')->andReturn('');
$builder->select(['name', 'email'])->from('users')->offset(0)->limit(1);

$this->assertEquals(
'select top 1 name, email from users',
$builder->toSql()
);

$builder = $this->getBuilder();
$connection = $builder->getConnection();

$connection->shouldReceive('getConfig')->andReturn('');
$builder->select(['name', 'email'])->from('users')->offset(1)->limit(1);

$this->assertEquals(
'select *, %vid from (select top all name, email from users order by 1) where %vid between 2 and 2',
$builder->toSql()
);
}

public function testWhereShortcut()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit 8ff4b92

Please sign in to comment.