]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix groupConcat and ordering on Oracle
authorVitor Mattos <vitor@php.rio>
Fri, 14 Jan 2022 18:56:47 +0000 (15:56 -0300)
committerVitor Mattos <vitor@php.rio>
Fri, 21 Jan 2022 11:39:39 +0000 (08:39 -0300)
Signed-off-by: Vitor Mattos <vitor@php.rio>
lib/private/Comments/Manager.php
lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
lib/public/DB/QueryBuilder/IFunctionBuilder.php

index 2e9acb6ca24d324eca37e77abb4b71af530773e1..410fcf26f4d2ec35995ec0541080b5840edc7118 100644 (file)
@@ -1210,20 +1210,21 @@ class Manager implements ICommentsManager {
                                        $totalQuery->expr()->literal('":'),
                                        $totalQuery->func()->count('id')
                                ),
-                               'total'
+                               'colonseparatedvalue'
                        )
+                       ->selectAlias($totalQuery->func()->count('id'), 'total')
                        ->from('reactions', 'r')
                        ->where($totalQuery->expr()->eq('r.parent_id', $qb->createNamedParameter($parentId)))
                        ->groupBy('r.reaction')
-                       ->orderBy($totalQuery->func()->count('id'), 'DESC')
+                       ->orderBy('total', 'DESC')
                        ->setMaxResults(200);
 
                $jsonQuery = $this->dbConn->getQueryBuilder();
                $jsonQuery
                        ->selectAlias(
-                               $totalQuery->func()->concat(
+                               $jsonQuery->func()->concat(
                                        $jsonQuery->expr()->literal('{'),
-                                       $jsonQuery->func()->groupConcat('total'),
+                                       $jsonQuery->func()->groupConcat('colonseparatedvalue', ',', $jsonQuery->getColumnName('total') . ' DESC'),
                                        $jsonQuery->expr()->literal('}')
                                ),
                                'json'
index 03630ea14a370f3db4e66f1c7eaab97f14e49231..ba59f42ce5dc9c51eb8610c76403410943c64859 100644 (file)
@@ -59,7 +59,7 @@ class FunctionBuilder implements IFunctionBuilder {
                return new QueryFunction(sprintf('CONCAT(%s)', implode(', ', $list)));
        }
 
-       public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
+       public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction {
                $separator = $this->connection->quote($separator);
                return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ' SEPARATOR ' . $separator . ')');
        }
index 43ecf599eba0cc665d4db1624acbdeb6617b8448..77f14a2dda474f13d075ad370e3c8c2f4b0c0463 100644 (file)
@@ -82,8 +82,8 @@ class OCIFunctionBuilder extends FunctionBuilder {
                return new QueryFunction(sprintf('(%s)', implode(' || ', $list)));
        }
 
-       public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
-               $orderByClause = ' WITHIN GROUP(ORDER BY NULL)';
+       public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = 'NULL'): IQueryFunction {
+               $orderByClause = ' WITHIN GROUP(ORDER BY ' . $orderBy . ')';
                if (is_null($separator)) {
                        return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr) . ')' . $orderByClause);
                }
index 444f6aa83a4e4df5a2376d6fe14d91137a6705df..3a1d9a56734c7b260a04d328e2e310f9f52d75de 100644 (file)
@@ -37,7 +37,7 @@ class PgSqlFunctionBuilder extends FunctionBuilder {
                return new QueryFunction(sprintf('(%s)', implode(' || ', $list)));
        }
 
-       public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
+       public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction {
                $castedExpression = $this->queryBuilder->expr()->castColumn($expr, IQueryBuilder::PARAM_STR);
 
                if (is_null($separator)) {
index fe700075a82c308ac9b3f1c0bc2316522f2561f1..cfcabc9eb1ad4a6ef0332d8625532547704c7f8d 100644 (file)
@@ -36,7 +36,7 @@ class SqliteFunctionBuilder extends FunctionBuilder {
                return new QueryFunction(sprintf('(%s)', implode(' || ', $list)));
        }
 
-       public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
+       public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction {
                $separator = $this->connection->quote($separator);
                return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ', ' . $separator . ')');
        }
index 04c5cbd07bde6b7227105cffff22555bd8bf85c9..9b0c7254250e119238dcaea3c28569db5b7ab116 100644 (file)
@@ -62,10 +62,11 @@ interface IFunctionBuilder {
         *
         * @param string|IQueryFunction $expr The expression to group
         * @param string|null $separator The separator
+        * @param string|null $orderBy Option only used to make compatible with Oracle database if is necessary use order. The default value is null and the Oracle don't will respect the order by of query
         * @return IQueryFunction
         * @since 24.0.0
         */
-       public function groupConcat($expr, ?string $separator = ','): IQueryFunction;
+       public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction;
 
        /**
         * Takes a substring from the input string