Skip to content

Commit

Permalink
Merge branch '5.7' into 5.8
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 20, 2019
2 parents e51d1f4 + c821cbf commit d90611b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@

class PostgresGrammar extends Grammar
{
/**
* The components that make up a select clause.
*
* @var array
*/
protected $selectComponents = [
'aggregate',
'columns',
'from',
'joins',
'wheres',
'groups',
'havings',
'orders',
'limit',
'offset',
'lock',
];

/**
* All of the available clause operators.
*
Expand Down Expand Up @@ -85,6 +104,40 @@ protected function dateBasedWhere($type, Builder $query, $where)
return 'extract('.$type.' from '.$this->wrap($where['column']).') '.$where['operator'].' '.$value;
}

/**
* Compile a select query into SQL.
*
* @param \Illuminate\Database\Query\Builder $query
* @return string
*/
public function compileSelect(Builder $query)
{
if ($query->unions && $query->aggregate) {
return $this->compileUnionAggregate($query);
}

$sql = parent::compileSelect($query);

if ($query->unions) {
$sql = '('.$sql.') '.$this->compileUnions($query);
}

return $sql;
}

/**
* Compile a single union statement.
*
* @param array $union
* @return string
*/
protected function compileUnion(array $union)
{
$conjunction = $union['all'] ? ' union all ' : ' union ';

return $conjunction.'('.$union['query']->toSql().')';
}

/**
* Compile a "JSON contains" statement into SQL.
*
Expand Down
30 changes: 29 additions & 1 deletion tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,13 @@ public function testUnions()
$this->assertEquals($expectedSql, $builder->toSql());
$this->assertEquals([0 => 10, 1 => 1, 2 => 11, 3 => 2], $builder->getBindings());

$builder = $this->getPostgresBuilder();
$expectedSql = '(select "name" from "users" where "id" = ?) union (select "name" from "users" where "id" = ?)';
$builder->select('name')->from('users')->where('id', '=', 1);
$builder->union($this->getPostgresBuilder()->select('name')->from('users')->where('id', '=', 2));
$this->assertEquals($expectedSql, $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());

$builder = $this->getSQLiteBuilder();
$expectedSql = 'select * from (select "name" from "users" where "id" = ?) union select * from (select "name" from "users" where "id" = ?)';
$builder->select('name')->from('users')->where('id', '=', 1);
Expand All @@ -795,6 +802,13 @@ public function testUnionAlls()
$builder->unionAll($this->getBuilder()->select('*')->from('users')->where('id', '=', 2));
$this->assertEquals('select * from "users" where "id" = ? union all select * from "users" where "id" = ?', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());

$expectedSql = '(select * from "users" where "id" = ?) union all (select * from "users" where "id" = ?)';
$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('id', '=', 1);
$builder->unionAll($this->getBuilder()->select('*')->from('users')->where('id', '=', 2));
$this->assertEquals($expectedSql, $builder->toSql());
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
}

public function testMultipleUnions()
Expand Down Expand Up @@ -834,6 +848,20 @@ public function testUnionLimitsAndOffsets()
$builder->union($this->getBuilder()->select('*')->from('dogs'));
$builder->skip(5)->take(10);
$this->assertEquals('select * from "users" union select * from "dogs" limit 10 offset 5', $builder->toSql());

$expectedSql = '(select * from "users") union (select * from "dogs") limit 10 offset 5';
$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users');
$builder->union($this->getBuilder()->select('*')->from('dogs'));
$builder->skip(5)->take(10);
$this->assertEquals($expectedSql, $builder->toSql());

$expectedSql = '(select * from "users" limit 11) union (select * from "dogs" limit 22) limit 10 offset 5';
$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->limit(11);
$builder->union($this->getBuilder()->select('*')->from('dogs')->limit(22));
$builder->skip(5)->take(10);
$this->assertEquals($expectedSql, $builder->toSql());
}

public function testUnionWithJoin()
Expand Down Expand Up @@ -881,7 +909,7 @@ public function testUnionAggregate()
$builder->getProcessor()->shouldReceive('processSelect')->once();
$builder->from('posts')->select('id')->union($this->getMySqlBuilder()->from('videos')->select('id'))->count();

$expected = 'select count(*) as aggregate from (select * from "posts" union select * from "videos") as "temp_table"';
$expected = 'select count(*) as aggregate from ((select * from "posts") union (select * from "videos")) as "temp_table"';
$builder = $this->getPostgresBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with($expected, [], true);
$builder->getProcessor()->shouldReceive('processSelect')->once();
Expand Down

0 comments on commit d90611b

Please sign in to comment.