Add unit test and orderBy parameter

Signed-off-by: Vitor Mattos <vitor@php.rio>
pull/30393/head
Vitor Mattos 2 years ago
parent 79b3df00f8
commit 7b9fea85b6
No known key found for this signature in database
GPG Key ID: B7AB4B76A7CA7318

@ -27,17 +27,22 @@ use OC\DB\QueryBuilder\QueryFunction;
use OC\DB\QueryBuilder\QuoteHelper;
use OCP\DB\QueryBuilder\IFunctionBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
use OCP\IDBConnection;
class FunctionBuilder implements IFunctionBuilder {
/** @var QuoteHelper */
protected $helper;
/** @var IDBConnection */
protected $connection;
/**
* ExpressionBuilder constructor.
*
* @param QuoteHelper $helper
*/
public function __construct(QuoteHelper $helper) {
public function __construct(IDBConnection $connection, QuoteHelper $helper) {
$this->connection = $connection;
$this->helper = $helper;
}
@ -49,8 +54,14 @@ 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 groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction {
if (is_null($orderBy)) {
$orderByClause = '';
} else {
$orderByClause = ' ORDER BY ' . $orderBy;
}
$separator = $this->connection->quote($separator);
return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . $orderByClause . ' SEPARATOR ' . $separator . ')');
}
public function substring($input, $start, $length = null): IQueryFunction {

@ -73,10 +73,14 @@ class OCIFunctionBuilder extends FunctionBuilder {
return parent::least($x, $y);
}
public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction {
if (is_null($orderBy)) {
$orderBy = 'NULL';
}
$orderByClause = ' WITHIN GROUP(ORDER BY ' . $orderBy . ')';
if (is_null($separator)) {
return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr));
return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr) . $orderByClause . ')');
}
return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr) . ", '$separator')");
return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr) . ", '$separator')$orderByClause");
}
}

@ -31,10 +31,15 @@ class PgSqlFunctionBuilder extends FunctionBuilder {
return new QueryFunction('(' . $this->helper->quoteColumnName($x) . ' || ' . $this->helper->quoteColumnName($y) . ')');
}
public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction {
if (is_null($orderBy)) {
$orderByClause = '';
} else {
$orderByClause = ' ORDER BY ' . $orderBy;
}
if (is_null($separator)) {
return new QueryFunction('string_agg(' . $this->helper->quoteColumnName($expr));
return new QueryFunction('string_agg(' . $this->helper->quoteColumnName($expr) . $orderByClause . ')');
}
return new QueryFunction('string_agg(' . $this->helper->quoteColumnName($expr) . ", '$separator')");
return new QueryFunction('string_agg(' . $this->helper->quoteColumnName($expr) . ", '$separator'$orderByClause)");
}
}

@ -31,6 +31,11 @@ class SqliteFunctionBuilder extends FunctionBuilder {
return new QueryFunction('(' . $this->helper->quoteColumnName($x) . ' || ' . $this->helper->quoteColumnName($y) . ')');
}
public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction {
$separator = $this->helper->quoteColumnName($separator);
return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . "$separator)");
}
public function greatest($x, $y): IQueryFunction {
return new QueryFunction('MAX(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
}

@ -155,16 +155,16 @@ class QueryBuilder implements IQueryBuilder {
*/
public function func() {
if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
return new OCIFunctionBuilder($this->helper);
return new OCIFunctionBuilder($this->connection, $this->helper);
}
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
return new SqliteFunctionBuilder($this->helper);
return new SqliteFunctionBuilder($this->connection, $this->helper);
}
if ($this->connection->getDatabasePlatform() instanceof PostgreSQL94Platform) {
return new PgSqlFunctionBuilder($this->helper);
return new PgSqlFunctionBuilder($this->connection, $this->helper);
}
return new FunctionBuilder($this->helper);
return new FunctionBuilder($this->connection, $this->helper);
}
/**

@ -62,10 +62,11 @@ interface IFunctionBuilder {
*
* @param string|ILiteral|IParameter|IQueryFunction $expr The expression to group
* @param string|null $separator The separator
* @param string|null $orderBy Optional SQL expression (and direction) to order the grouped rows by.
* @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

@ -54,6 +54,32 @@ class FunctionBuilderTest extends TestCase {
$this->assertEquals('foobar', $column);
}
public function testGroupConcatWithoutSeparatorAndOrder() {
$query = $this->connection->getQueryBuilder();
$query->select($query->func()->groupConcat('appid'));
$query->from('appconfig')
->setMaxResults(1);
$result = $query->execute();
$column = $result->fetchOne();
$result->closeCursor();
$this->assertGreaterThan(1, str_getcsv($column, ','));
}
public function testGroupConcatWithSeparatorAndOrder() {
$query = $this->connection->getQueryBuilder();
$query->select($query->func()->groupConcat('appid', '#', 'appid'));
$query->from('appconfig')
->setMaxResults(1);
$result = $query->execute();
$column = $result->fetchOne();
$result->closeCursor();
$this->assertGreaterThan(1, str_getcsv($column, '#', 'appid'));
}
public function testMd5() {
$query = $this->connection->getQueryBuilder();

Loading…
Cancel
Save