diff --git a/src/Query/Column.php b/src/Query/Column.php index 0c19abf..5210e7b 100644 --- a/src/Query/Column.php +++ b/src/Query/Column.php @@ -194,6 +194,38 @@ public function sumIf($expression = []) return $this; } + /** + * Apply sum function to column. + * + * @param string|Expression|null $columnName + * + * @return $this + */ + public function sum($columnName = null): self + { + if ($columnName !== null) { + $this->name($columnName); + } + + $this->functions[] = ['function' => 'sum']; + + return $this; + } + + /** + * Apply round function to column. + * + * @param int $decimals + * + * @return $this + */ + public function round(int $decimals = 0): self + { + $this->functions[] = ['function' => 'round', 'params' => $decimals]; + + return $this; + } + /** * Apply plus function to column. * diff --git a/src/Query/Traits/ColumnCompiler.php b/src/Query/Traits/ColumnCompiler.php index f7e9354..90997c4 100644 --- a/src/Query/Traits/ColumnCompiler.php +++ b/src/Query/Traits/ColumnCompiler.php @@ -100,6 +100,31 @@ private function sumIf($column, $condition) return "sumIf({$column}, {$condition})"; } + /** + * Compiles sum function on column. + * + * @param $column + * + * @return string + */ + private function sum($column) + { + return "sum({$column})"; + } + + /** + * Compiles round function on column. + * + * @param $column + * @param $decimals + * + * @return string + */ + private function round($column, $decimals) + { + return "round({$column}, {$decimals})"; + } + /** * Compiles distinct function on column. * diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index 8e828b1..5b31290 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -87,6 +87,37 @@ public function testSumIfExpression() ], $functions); } + public function testSum() + { + $column = new Column($this->getBuilder()); + $column->name('column'); + $column->sum(); + + $functions = $column->getFunctions(); + + $this->assertEquals([ + [ + 'function' => 'sum', + ], + ], $functions); + } + + public function testRound() + { + $column = new Column($this->getBuilder()); + $column->name('column'); + $column->round(2); + + $functions = $column->getFunctions(); + + $this->assertEquals([ + [ + 'function' => 'round', + 'params' => 2, + ], + ], $functions); + } + public function testPlus() { $column = new Column($this->getBuilder()); diff --git a/tests/GrammarTest.php b/tests/GrammarTest.php index 70f558e..a1a9f4c 100644 --- a/tests/GrammarTest.php +++ b/tests/GrammarTest.php @@ -175,6 +175,27 @@ public function testCompileSelect() $select = $grammar->compileSelect($builder); $this->assertEquals('SELECT sumIf(`column`, > 10)', $select); + $builder->select(function ($column) { + $column->name('column')->sum(); + }); + + $select = $grammar->compileSelect($builder); + $this->assertEquals('SELECT sum(`column`)', $select); + + $builder->select(function ($column) { + $column->sum('column'); + }); + + $select = $grammar->compileSelect($builder); + $this->assertEquals('SELECT sum(`column`)', $select); + + $builder->select(function ($column) { + $column->sum('column')->round(2); + }); + + $select = $grammar->compileSelect($builder); + $this->assertEquals('SELECT round(sum(`column`), 2)', $select); + $builder->select(function ($column) { $column->name('column')->distinct(); });