From 3687788ac26bf4e4a65321688c7ec349b0ec0e73 Mon Sep 17 00:00:00 2001 From: David Rodrigues Date: Sun, 16 Feb 2020 15:30:24 -0300 Subject: [PATCH 1/4] Illuminate\Database\Query\Builder: added support to groupByRaw($sql, array $bindings = []) [fixes #1112]. --- src/Illuminate/Database/Query/Builder.php | 30 ++++++++++++++++----- tests/Database/DatabaseQueryBuilderTest.php | 9 +++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 216c6c1d9021..a41373beaedd 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -53,13 +53,14 @@ class Builder * @var array */ public $bindings = [ - 'select' => [], - 'from' => [], - 'join' => [], - 'where' => [], - 'having' => [], - 'order' => [], - 'union' => [], + 'select' => [], + 'from' => [], + 'join' => [], + 'where' => [], + 'groupBy' => [], + 'having' => [], + 'order' => [], + 'union' => [], 'unionOrder' => [], ]; @@ -1685,6 +1686,21 @@ public function groupBy(...$groups) return $this; } + /** + * Add a raw groupBy clause to the query. + * + * @param string $sql + * @param array $bindings + */ + public function groupByRaw($sql, array $bindings = []): self + { + $this->groups[] = new Expression($sql); + + $this->addBinding($bindings, 'groupBy'); + + return $this; + } + /** * Add a "having" clause to the query. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index eabf5cfad591..83763281745b 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -1045,6 +1045,15 @@ public function testGroupBys() $builder = $this->getBuilder(); $builder->select('*')->from('users')->groupBy(new Raw('DATE(created_at)')); $this->assertSame('select * from "users" group by DATE(created_at)', $builder->toSql()); + + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->groupByRaw('DATE(created_at), ? DESC', ['foo']); + $this->assertSame('select * from "users" group by DATE(created_at), ? DESC', $builder->toSql()); + $this->assertEquals(['foo'], $builder->getBindings()); + + $builder = $this->getBuilder(); + $builder->havingRaw('?', ['havingRawBinding'])->groupByRaw('?', ['groupByRawBinding'])->whereRaw('?', ['whereRawBinding']); + $this->assertEquals(['whereRawBinding', 'groupByRawBinding', 'havingRawBinding'], $builder->getBindings()); } public function testOrderBys() From 55a78c3cd1cc1766c50021435890e82a59e28624 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 17 Feb 2020 08:51:45 +0000 Subject: [PATCH 2/4] Update Builder.php --- src/Illuminate/Database/Query/Builder.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index a41373beaedd..178fa6a73034 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -53,14 +53,14 @@ class Builder * @var array */ public $bindings = [ - 'select' => [], - 'from' => [], - 'join' => [], - 'where' => [], - 'groupBy' => [], - 'having' => [], - 'order' => [], - 'union' => [], + 'select' => [], + 'from' => [], + 'join' => [], + 'where' => [], + 'groupBy' => [], + 'having' => [], + 'order' => [], + 'union' => [], 'unionOrder' => [], ]; @@ -1690,9 +1690,10 @@ public function groupBy(...$groups) * Add a raw groupBy clause to the query. * * @param string $sql - * @param array $bindings + * @param array $bindings + * @return $this */ - public function groupByRaw($sql, array $bindings = []): self + public function groupByRaw($sql, array $bindings = []) { $this->groups[] = new Expression($sql); From 6db8f40adf04dfa2dfba081b7ca2c2e475fe1ef4 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 17 Feb 2020 08:52:36 +0000 Subject: [PATCH 3/4] Update Builder.php --- src/Illuminate/Database/Query/Builder.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 178fa6a73034..5eeba0e8708b 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -53,14 +53,14 @@ class Builder * @var array */ public $bindings = [ - 'select' => [], - 'from' => [], - 'join' => [], - 'where' => [], - 'groupBy' => [], - 'having' => [], - 'order' => [], - 'union' => [], + 'select' => [], + 'from' => [], + 'join' => [], + 'where' => [], + 'groupBy' => [], + 'having' => [], + 'order' => [], + 'union' => [], 'unionOrder' => [], ]; From df1b482600c8eea10019268721620440d519a04c Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 17 Feb 2020 14:20:00 +0000 Subject: [PATCH 4/4] Update Builder.php --- src/Illuminate/Database/Query/Builder.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 5eeba0e8708b..1b9fe51b9655 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -53,14 +53,14 @@ class Builder * @var array */ public $bindings = [ - 'select' => [], - 'from' => [], - 'join' => [], - 'where' => [], - 'groupBy' => [], - 'having' => [], - 'order' => [], - 'union' => [], + 'select' => [], + 'from' => [], + 'join' => [], + 'where' => [], + 'groupBy' => [], + 'having' => [], + 'order' => [], + 'union' => [], 'unionOrder' => [], ];