Skip to content

Commit

Permalink
add case statement to sql function builder
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Apr 28, 2022
1 parent c0931f4 commit 38646ee
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,15 @@ public function greatest($x, $y): IQueryFunction {
public function least($x, $y): IQueryFunction {
return new QueryFunction('LEAST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
}

public function case(array $whens, $else): IQueryFunction {
if (count($whens) < 1) {
return new QueryFunction($this->helper->quoteColumnName($else));
}

$whenParts = array_map(function (array $when) {
return 'WHEN ' . $this->helper->quoteColumnName($when['when']) . ' THEN ' . $this->helper->quoteColumnName($when['then']);
}, $whens);
return new QueryFunction('CASE ' . implode(' ', $whenParts) . ' ELSE ' . $this->helper->quoteColumnName($else) . ' END');
}
}
12 changes: 12 additions & 0 deletions lib/public/DB/QueryBuilder/IFunctionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,16 @@ public function greatest($x, $y): IQueryFunction;
* @since 18.0.0
*/
public function least($x, $y): IQueryFunction;

/**
* Takes the minimum of multiple values
*
* If you want to get the minimum value of all rows in a column, use `min` instead
*
* @param array<array{"when": string|ILiteral|IParameter|IQueryFunction, "then": string|ILiteral|IParameter|IQueryFunction}> $whens
* @param string|ILiteral|IParameter|IQueryFunction $else
* @return IQueryFunction
* @since 18.0.0
*/
public function case(array $whens, $else): IQueryFunction;
}
17 changes: 17 additions & 0 deletions tests/lib/DB/QueryBuilder/FunctionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,21 @@ public function testLeast() {
$result->closeCursor();
$this->assertEquals(1, $row);
}

public function testCase() {
$query = $this->connection->getQueryBuilder();

$query->select($query->func()->case([
['when' => $query->expr()->gt($query->expr()->literal(1, IQueryBuilder::PARAM_INT), $query->expr()->literal(2, IQueryBuilder::PARAM_INT)), 'then' => $query->expr()->literal('first')],
['when' => $query->expr()->lt($query->expr()->literal(1, IQueryBuilder::PARAM_INT), $query->expr()->literal(2, IQueryBuilder::PARAM_INT)), 'then' => $query->expr()->literal('second')],
['when' => $query->expr()->eq($query->expr()->literal(1, IQueryBuilder::PARAM_INT), $query->expr()->literal(2, IQueryBuilder::PARAM_INT)), 'then' => $query->expr()->literal('third')],
], $query->createNamedParameter('else')));
$query->from('appconfig')
->setMaxResults(1);

$result = $query->execute();
$row = $result->fetchOne();
$result->closeCursor();
$this->assertEquals('second', $row);
}
}

0 comments on commit 38646ee

Please sign in to comment.