From 5a46a06289ed985491a0e3b6c57083f227a21ba0 Mon Sep 17 00:00:00 2001 From: ay Date: Mon, 26 Jun 2017 12:44:43 +0300 Subject: [PATCH] added fixes to getCountQuery --- src/Integrations/Laravel/Builder.php | 9 +++++++-- src/Query/BaseBuilder.php | 19 ++++++++++++++----- src/Query/Builder.php | 9 +++++++-- tests/BuilderTest.php | 4 ++-- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/Integrations/Laravel/Builder.php b/src/Integrations/Laravel/Builder.php index 77bd509..3db4225 100644 --- a/src/Integrations/Laravel/Builder.php +++ b/src/Integrations/Laravel/Builder.php @@ -52,9 +52,14 @@ public function get() */ public function count($column = '*') { - $result = $this->getCountQuery($column)->get(); + $builder = $this->getCountQuery($column); + $result = $builder->get(); - return $result[0]['count'] ?? 0; + if (!empty($this->groups)) { + return count($result); + } else { + return $result[0]['count'] ?? 0; + } } /** diff --git a/src/Query/BaseBuilder.php b/src/Query/BaseBuilder.php index 13479cc..e726963 100644 --- a/src/Query/BaseBuilder.php +++ b/src/Query/BaseBuilder.php @@ -145,14 +145,23 @@ public function select(...$columns) return $this; } + /** + * Returns query for count total rows without limit + * + * @param string $column + * + * @return static + */ public function getCountQuery($column = '*') { - $builder = $this->cloneWithout(['columns'])->select(raw('count('.$column.') as `count`')); + $without = ['columns' => [], 'limit' => null]; - if (empty($builder->groups)) { - $builder->orders = []; + if (empty($this->groups)) { + $without['orders'] = []; } + $builder = $this->cloneWithout($without)->select(raw('count('.$column.') as `count`')); + return $builder; } @@ -165,8 +174,8 @@ public function getCountQuery($column = '*') public function cloneWithout(array $except) { return tap(clone $this, function ($clone) use ($except) { - foreach ($except as $property) { - $clone->{$property} = null; + foreach ($except as $property => $value) { + $clone->{$property} = $value; } }); } diff --git a/src/Query/Builder.php b/src/Query/Builder.php index e34333b..e3adab2 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -50,9 +50,14 @@ public function get() */ public function count($column = '*') { - $result = $this->getCountQuery($column)->get(); + $builder = $this->getCountQuery($column); + $result = $builder->get(); - return $result[0]['count'] ?? 0; + if (!empty($this->groups)) { + return count($result); + } else { + return $result[0]['count'] ?? 0; + } } /** diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 99566b1..2786769 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -662,10 +662,10 @@ public function test_where_dict() public function test_count() { - $builder = $this->getBuilder()->from('table')->select('column1', 'column2', 'column3')->orderBy('column1')->getCountQuery('*'); + $builder = $this->getBuilder()->from('table')->select('column1', 'column2', 'column3')->orderBy('column1')->limit(10)->getCountQuery('*'); $this->assertEquals('SELECT count(*) as `count` FROM `table`', $builder->toSql()); - $builder = $this->getBuilder()->from('table')->select('column1', 'column2', 'column3')->groupBy('column2')->orderBy('column1')->getCountQuery('*'); + $builder = $this->getBuilder()->from('table')->select('column1', 'column2', 'column3')->groupBy('column2')->orderBy('column1')->limit(10)->getCountQuery('*'); $this->assertEquals('SELECT count(*) as `count` FROM `table` GROUP BY `column2` ORDER BY `column1` ASC', $builder->toSql()); }