Skip to content

Commit 38646ee

Browse files
committed
add case statement to sql function builder
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent c0931f4 commit 38646ee

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,15 @@ public function greatest($x, $y): IQueryFunction {
121121
public function least($x, $y): IQueryFunction {
122122
return new QueryFunction('LEAST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
123123
}
124+
125+
public function case(array $whens, $else): IQueryFunction {
126+
if (count($whens) < 1) {
127+
return new QueryFunction($this->helper->quoteColumnName($else));
128+
}
129+
130+
$whenParts = array_map(function (array $when) {
131+
return 'WHEN ' . $this->helper->quoteColumnName($when['when']) . ' THEN ' . $this->helper->quoteColumnName($when['then']);
132+
}, $whens);
133+
return new QueryFunction('CASE ' . implode(' ', $whenParts) . ' ELSE ' . $this->helper->quoteColumnName($else) . ' END');
134+
}
124135
}

lib/public/DB/QueryBuilder/IFunctionBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,16 @@ public function greatest($x, $y): IQueryFunction;
188188
* @since 18.0.0
189189
*/
190190
public function least($x, $y): IQueryFunction;
191+
192+
/**
193+
* Takes the minimum of multiple values
194+
*
195+
* If you want to get the minimum value of all rows in a column, use `min` instead
196+
*
197+
* @param array<array{"when": string|ILiteral|IParameter|IQueryFunction, "then": string|ILiteral|IParameter|IQueryFunction}> $whens
198+
* @param string|ILiteral|IParameter|IQueryFunction $else
199+
* @return IQueryFunction
200+
* @since 18.0.0
201+
*/
202+
public function case(array $whens, $else): IQueryFunction;
191203
}

tests/lib/DB/QueryBuilder/FunctionBuilderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,4 +501,21 @@ public function testLeast() {
501501
$result->closeCursor();
502502
$this->assertEquals(1, $row);
503503
}
504+
505+
public function testCase() {
506+
$query = $this->connection->getQueryBuilder();
507+
508+
$query->select($query->func()->case([
509+
['when' => $query->expr()->gt($query->expr()->literal(1, IQueryBuilder::PARAM_INT), $query->expr()->literal(2, IQueryBuilder::PARAM_INT)), 'then' => $query->expr()->literal('first')],
510+
['when' => $query->expr()->lt($query->expr()->literal(1, IQueryBuilder::PARAM_INT), $query->expr()->literal(2, IQueryBuilder::PARAM_INT)), 'then' => $query->expr()->literal('second')],
511+
['when' => $query->expr()->eq($query->expr()->literal(1, IQueryBuilder::PARAM_INT), $query->expr()->literal(2, IQueryBuilder::PARAM_INT)), 'then' => $query->expr()->literal('third')],
512+
], $query->createNamedParameter('else')));
513+
$query->from('appconfig')
514+
->setMaxResults(1);
515+
516+
$result = $query->execute();
517+
$row = $result->fetchOne();
518+
$result->closeCursor();
519+
$this->assertEquals('second', $row);
520+
}
504521
}

0 commit comments

Comments
 (0)