Skip to content

Commit

Permalink
Add sum, round functions for Column (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalcrazz authored Jan 17, 2021
1 parent 1d315f5 commit 5441f01
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Query/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
25 changes: 25 additions & 0 deletions src/Query/Traits/ColumnCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
21 changes: 21 additions & 0 deletions tests/GrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit 5441f01

Please sign in to comment.