aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVitor Mattos <vitor@php.rio>2022-01-03 11:04:05 -0300
committerVitor Mattos <vitor@php.rio>2022-01-03 21:41:38 -0300
commitfb6a9f308d01148a35f8aa7c8cb6b4a5fa5b220f (patch)
tree15fe65abdae05c775dd4c484319c2f7319a88ac0
parent7b9fea85b6e893aca874b7a0a07866443822005a (diff)
downloadnextcloud-server-fb6a9f308d01148a35f8aa7c8cb6b4a5fa5b220f.tar.gz
nextcloud-server-fb6a9f308d01148a35f8aa7c8cb6b4a5fa5b220f.zip
Add unit test
Signed-off-by: Vitor Mattos <vitor@php.rio>
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php9
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php7
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php12
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php6
-rw-r--r--lib/public/DB/QueryBuilder/IFunctionBuilder.php3
-rw-r--r--tests/lib/DB/QueryBuilder/FunctionBuilderTest.php40
6 files changed, 45 insertions, 32 deletions
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
index 3384ad8b42f..6b5fb751fbd 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
@@ -54,14 +54,9 @@ class FunctionBuilder implements IFunctionBuilder {
return new QueryFunction('CONCAT(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
}
- public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction {
- if (is_null($orderBy)) {
- $orderByClause = '';
- } else {
- $orderByClause = ' ORDER BY ' . $orderBy;
- }
+ public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
$separator = $this->connection->quote($separator);
- return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . $orderByClause . ' SEPARATOR ' . $separator . ')');
+ return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ' SEPARATOR ' . $separator . ')');
}
public function substring($input, $start, $length = null): IQueryFunction {
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
index d3553565b7e..efc42e66112 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
@@ -73,11 +73,8 @@ class OCIFunctionBuilder extends FunctionBuilder {
return parent::least($x, $y);
}
- public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction {
- if (is_null($orderBy)) {
- $orderBy = 'NULL';
- }
- $orderByClause = ' WITHIN GROUP(ORDER BY ' . $orderBy . ')';
+ public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
+ $orderByClause = ' WITHIN GROUP(ORDER BY NULL)';
if (is_null($separator)) {
return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr) . $orderByClause . ')');
}
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
index 702b71e12b8..f4c1364fab1 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
@@ -31,15 +31,11 @@ class PgSqlFunctionBuilder extends FunctionBuilder {
return new QueryFunction('(' . $this->helper->quoteColumnName($x) . ' || ' . $this->helper->quoteColumnName($y) . ')');
}
- public function groupConcat($expr, ?string $separator = ',', ?string $orderBy = null): IQueryFunction {
- if (is_null($orderBy)) {
- $orderByClause = '';
- } else {
- $orderByClause = ' ORDER BY ' . $orderBy;
- }
+ public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
if (is_null($separator)) {
- return new QueryFunction('string_agg(' . $this->helper->quoteColumnName($expr) . $orderByClause . ')');
+ return new QueryFunction('string_agg(cast(' . $this->helper->quoteColumnName($expr) . ' AS varchar)');
}
- return new QueryFunction('string_agg(' . $this->helper->quoteColumnName($expr) . ", '$separator'$orderByClause)");
+ $separator = $this->connection->quote($separator);
+ return new QueryFunction('string_agg(cast(' . $this->helper->quoteColumnName($expr) . " AS varchar), $separator)");
}
}
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
index 796495dfd89..74e4e30e7ff 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
@@ -31,9 +31,9 @@ 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 groupConcat($expr, ?string $separator = ','): IQueryFunction {
+ $separator = $this->connection->quote($separator);
+ return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ", $separator)");
}
public function greatest($x, $y): IQueryFunction {
diff --git a/lib/public/DB/QueryBuilder/IFunctionBuilder.php b/lib/public/DB/QueryBuilder/IFunctionBuilder.php
index 466a1c4ddf4..e04a4cbfdba 100644
--- a/lib/public/DB/QueryBuilder/IFunctionBuilder.php
+++ b/lib/public/DB/QueryBuilder/IFunctionBuilder.php
@@ -62,11 +62,10 @@ 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 = ',', ?string $orderBy = null): IQueryFunction;
+ public function groupConcat($expr, ?string $separator = ','): IQueryFunction;
/**
* Takes a substring from the input string
diff --git a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php
index b824b86fecf..938ab6d2b05 100644
--- a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php
+++ b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php
@@ -54,12 +54,38 @@ class FunctionBuilderTest extends TestCase {
$this->assertEquals('foobar', $column);
}
- public function testGroupConcatWithoutSeparatorAndOrder() {
+ protected function clearDummyData() {
+ $delete = $this->connection->getQueryBuilder();
+
+ $delete->delete('appconfig')
+ ->where($delete->expr()->eq('appid', $delete->createNamedParameter('group_concat', IQueryBuilder::PARAM_STR)));
+ $delete->executeStatement();
+ }
+
+ protected function addDummyData() {
+ $this->clearDummyData();
+ $insert = $this->connection->getQueryBuilder();
+
+ $insert->insert('appconfig')
+ ->setValue('appid', $insert->createNamedParameter('group_concat', IQueryBuilder::PARAM_STR))
+ ->setValue('configvalue', $insert->createNamedParameter('unittest', IQueryBuilder::PARAM_STR))
+ ->setValue('configkey', $insert->createParameter('value', IQueryBuilder::PARAM_STR));
+
+ $insert->setParameter('value', '1');
+ $insert->executeStatement();
+ $insert->setParameter('value', '3');
+ $insert->executeStatement();
+ $insert->setParameter('value', '2');
+ $insert->executeStatement();
+ }
+
+ public function testGroupConcatWithoutSeparator() {
+ $this->addDummyData();
$query = $this->connection->getQueryBuilder();
- $query->select($query->func()->groupConcat('appid'));
- $query->from('appconfig')
- ->setMaxResults(1);
+ $query->select($query->func()->groupConcat('configkey'))
+ ->from('appconfig')
+ ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
$result = $query->execute();
$column = $result->fetchOne();
@@ -70,9 +96,9 @@ class FunctionBuilderTest extends TestCase {
public function testGroupConcatWithSeparatorAndOrder() {
$query = $this->connection->getQueryBuilder();
- $query->select($query->func()->groupConcat('appid', '#', 'appid'));
- $query->from('appconfig')
- ->setMaxResults(1);
+ $query->select($query->func()->groupConcat('configkey', '#'))
+ ->from('appconfig')
+ ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
$result = $query->execute();
$column = $result->fetchOne();