]> source.dussan.org Git - nextcloud-server.git/commitdiff
add MAX and MIN to functionbuilder 17006/head
authorRobin Appelman <robin@icewind.nl>
Wed, 4 Sep 2019 14:48:02 +0000 (16:48 +0200)
committerRobin Appelman <robin@icewind.nl>
Fri, 6 Sep 2019 10:28:17 +0000 (12:28 +0200)
Signed-off-by: Robin Appelman <robin@icewind.nl>
lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
lib/public/DB/QueryBuilder/IFunctionBuilder.php
tests/lib/DB/QueryBuilder/FunctionBuilderTest.php

index a9d2f6f9a3564decf0ea813741ab4e8c5c02fc59..3b67661c8b0529bbaeeaea39d0701f7e64a25d0a 100644 (file)
@@ -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) . ')');
+       }
 }
index 1e17290f1458bf222601ca5293e4528f41e11fee..066be470150d235ba1e8ed6d17c600e9c62ca167 100644 (file)
@@ -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);
 }
index 136650741f551fad9cf46c9d20b5a4652bb1bc42..a8af7f4fe07a38a13756c009c5c11b44d4bf043d 100644 (file)
@@ -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());
+       }
 }