Add group_concat aggregator function

Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
Vitor Mattos 2021-12-21 12:18:15 -03:00
parent 29dffd7e7f
commit 79b3df00f8
No known key found for this signature in database
GPG key ID: B7AB4B76A7CA7318
4 changed files with 34 additions and 0 deletions

View file

@ -49,6 +49,10 @@ class FunctionBuilder implements IFunctionBuilder {
return new QueryFunction('CONCAT(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
}
public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ')');
}
public function substring($input, $start, $length = null): IQueryFunction {
if ($length) {
return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ', ' . $this->helper->quoteColumnName($length) . ')');

View file

@ -72,4 +72,11 @@ class OCIFunctionBuilder extends FunctionBuilder {
return parent::least($x, $y);
}
public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
if (is_null($separator)) {
return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr));
}
return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr) . ", '$separator')");
}
}

View file

@ -30,4 +30,11 @@ class PgSqlFunctionBuilder extends FunctionBuilder {
public function concat($x, $y): IQueryFunction {
return new QueryFunction('(' . $this->helper->quoteColumnName($x) . ' || ' . $this->helper->quoteColumnName($y) . ')');
}
public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
if (is_null($separator)) {
return new QueryFunction('string_agg(' . $this->helper->quoteColumnName($expr));
}
return new QueryFunction('string_agg(' . $this->helper->quoteColumnName($expr) . ", '$separator')");
}
}

View file

@ -51,6 +51,22 @@ interface IFunctionBuilder {
*/
public function concat($x, $y): IQueryFunction;
/**
* Returns a string which is the concatenation of all non-NULL values of X
*
* Usage examples:
*
* groupConcat('column') -- with comma as separator (default separator)
*
* groupConcat('column', ';') -- with different separator
*
* @param string|ILiteral|IParameter|IQueryFunction $expr The expression to group
* @param string|null $separator The separator
* @return IQueryFunction
* @since 24.0.0
*/
public function groupConcat($expr, ?string $separator = ','): IQueryFunction;
/**
* Takes a substring from the input string
*