Fix groupConcat and ordering on Oracle

Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
Vitor Mattos 2022-01-14 15:56:47 -03:00
parent f7cd995612
commit f071b4dfbb
No known key found for this signature in database
GPG key ID: B7AB4B76A7CA7318
6 changed files with 12 additions and 10 deletions

View 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'

View 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 . ')');
}

View 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);
}

View 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)) {

View 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 . ')');
}

View 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