diff options
author | Robin Appelman <robin@icewind.nl> | 2019-09-04 16:48:02 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2019-09-06 12:28:17 +0200 |
commit | 8ef5a366eccaab850dab4641619c704a0b36f3d9 (patch) | |
tree | 66358c6d83850bc3ef08a65db5730e764779f0ca | |
parent | 6d20876eb2aae5cb2269c7551e7a4f43e7a31222 (diff) | |
download | nextcloud-server-8ef5a366eccaab850dab4641619c704a0b36f3d9.tar.gz nextcloud-server-8ef5a366eccaab850dab4641619c704a0b36f3d9.zip |
add MAX and MIN to functionbuilder
Signed-off-by: Robin Appelman <robin@icewind.nl>
-rw-r--r-- | lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php | 8 | ||||
-rw-r--r-- | lib/public/DB/QueryBuilder/IFunctionBuilder.php | 20 | ||||
-rw-r--r-- | tests/lib/DB/QueryBuilder/FunctionBuilderTest.php | 78 |
3 files changed, 106 insertions, 0 deletions
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php index a9d2f6f9a35..3b67661c8b0 100644 --- a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php +++ b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php @@ -76,4 +76,12 @@ class FunctionBuilder implements IFunctionBuilder { $alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : ''; return new QueryFunction('COUNT(' . $this->helper->quoteColumnName($count) . ')' . $alias); } + + public function max($field) { + return new QueryFunction('MAX(' . $this->helper->quoteColumnName($field) . ')'); + } + + public function min($field) { + return new QueryFunction('MIN(' . $this->helper->quoteColumnName($field) . ')'); + } } diff --git a/lib/public/DB/QueryBuilder/IFunctionBuilder.php b/lib/public/DB/QueryBuilder/IFunctionBuilder.php index 1e17290f145..066be470150 100644 --- a/lib/public/DB/QueryBuilder/IFunctionBuilder.php +++ b/lib/public/DB/QueryBuilder/IFunctionBuilder.php @@ -105,4 +105,24 @@ interface IFunctionBuilder { * @since 14.0.0 */ public function count($count, $alias = ''); + + /** + * Takes the maximum of all rows in a column + * + * @param mixed $field the column to maximum + * + * @return IQueryFunction + * @since 18.0.0 + */ + public function max($field); + + /** + * Takes the minimum of all rows in a column + * + * @param mixed $field the column to minimum + * + * @return IQueryFunction + * @since 18.0.0 + */ + public function min($field); } diff --git a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php index 136650741f5..a8af7f4fe07 100644 --- a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php +++ b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php @@ -120,4 +120,82 @@ class FunctionBuilderTest extends TestCase { $this->assertGreaterThan(1, $query->execute()->fetchColumn()); } + + private function setUpMinMax($value) { + $query = $this->connection->getQueryBuilder(); + + $query->insert('appconfig') + ->values([ + 'appid' => $query->createNamedParameter('minmax'), + 'configkey' => $query->createNamedParameter(uniqid()), + 'configvalue' => $query->createNamedParameter((string)$value), + ]); + $query->execute(); + } + + private function clearMinMax() { + $query = $this->connection->getQueryBuilder(); + + $query->delete('appconfig') + ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax'))); + $query->execute(); + } + + public function testMaxEmpty() { + $this->clearMinMax(); + + $query = $this->connection->getQueryBuilder(); + + $query->select($query->func()->max($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT))); + $query->from('appconfig') + ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax'))) + ->setMaxResults(1); + + $this->assertEquals(null, $query->execute()->fetchColumn()); + } + + public function testMinEmpty() { + $this->clearMinMax(); + + $query = $this->connection->getQueryBuilder(); + + $query->select($query->func()->min($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT))); + $query->from('appconfig') + ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax'))) + ->setMaxResults(1); + + $this->assertEquals(null, $query->execute()->fetchColumn()); + } + + public function testMax() { + $this->clearMinMax(); + $this->setUpMinMax(10); + $this->setUpMinMax(11); + $this->setUpMinMax(20); + + $query = $this->connection->getQueryBuilder(); + + $query->select($query->func()->max($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT))); + $query->from('appconfig') + ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax'))) + ->setMaxResults(1); + + $this->assertEquals(20, $query->execute()->fetchColumn()); + } + + public function testMin() { + $this->clearMinMax(); + $this->setUpMinMax(10); + $this->setUpMinMax(11); + $this->setUpMinMax(20); + + $query = $this->connection->getQueryBuilder(); + + $query->select($query->func()->min($query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT))); + $query->from('appconfig') + ->where($query->expr()->eq('appid', $query->createNamedParameter('minmax'))) + ->setMaxResults(1); + + $this->assertEquals(10, $query->execute()->fetchColumn()); + } } |