diff --git a/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php index 71234023bddb..6f5e9c30c6a0 100755 --- a/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php @@ -6,6 +6,25 @@ class SQLiteGrammar 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. * @@ -17,6 +36,36 @@ class SQLiteGrammar extends Grammar '&', '|', '<<', '>>', ]; + /** + * Compile a select query into SQL. + * + * @param \Illuminate\Database\Query\Builder $query + * @return string + */ + public function compileSelect(Builder $query) + { + $sql = parent::compileSelect($query); + + if ($query->unions) { + $sql = 'select * from ('.$sql.') '.$this->compileUnions($query); + } + + return $sql; + } + + /** + * Compile a single union statement. + * + * @param array $union + * @return string + */ + protected function compileUnion(array $union) + { + $conjuction = $union['all'] ? ' union all ' : ' union '; + + return $conjuction.'select * from ('.$union['query']->toSql().')'; + } + /** * Compile a "where date" clause. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 1378ae11c03d..1f81d2a26e1d 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -459,6 +459,13 @@ public function testUnions() $builder->select('a')->from('t1')->where('a', 10)->where('b', 1)->union($union)->orderBy('a')->limit(10); $this->assertEquals($expectedSql, $builder->toSql()); $this->assertEquals([0 => 10, 1 => 1, 2 => 11, 3 => 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); + $builder->union($this->getSQLiteBuilder()->select('name')->from('users')->where('id', '=', 2)); + $this->assertEquals($expectedSql, $builder->toSql()); + $this->assertEquals([0 => 1, 1 => 2], $builder->getBindings()); } public function testUnionAlls()