diff options
author | Robin Appelman <robin@icewind.nl> | 2022-04-28 15:16:07 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2022-06-02 15:52:12 +0200 |
commit | 2a68819a67045d87a369a8a6413f153b3b2bea5f (patch) | |
tree | 13978f741bb54c603b9085318612c9ea021aaa6c | |
parent | 813b50ed428a8bc36817d19c84444e96dbe3b668 (diff) | |
download | nextcloud-server-2a68819a67045d87a369a8a6413f153b3b2bea5f.tar.gz nextcloud-server-2a68819a67045d87a369a8a6413f153b3b2bea5f.zip |
add case statement to sql function builder
Signed-off-by: Robin Appelman <robin@icewind.nl>
-rw-r--r-- | lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php | 11 | ||||
-rw-r--r-- | lib/public/DB/QueryBuilder/IFunctionBuilder.php | 12 | ||||
-rw-r--r-- | tests/lib/DB/QueryBuilder/FunctionBuilderTest.php | 17 |
3 files changed, 40 insertions, 0 deletions
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php index e0a7549a0ad..408a879d624 100644 --- a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php +++ b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php @@ -121,4 +121,15 @@ class FunctionBuilder implements IFunctionBuilder { 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'); + } } diff --git a/lib/public/DB/QueryBuilder/IFunctionBuilder.php b/lib/public/DB/QueryBuilder/IFunctionBuilder.php index d4edc8ea9f8..811e8d06aaf 100644 --- a/lib/public/DB/QueryBuilder/IFunctionBuilder.php +++ b/lib/public/DB/QueryBuilder/IFunctionBuilder.php @@ -188,4 +188,16 @@ interface IFunctionBuilder { * @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; } diff --git a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php index 08392b09d8d..0ea6e69c956 100644 --- a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php +++ b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php @@ -501,4 +501,21 @@ class FunctionBuilderTest extends TestCase { $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); + } } |