summaryrefslogtreecommitdiffstats
path: root/lib
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 12:45:00 -0300
commit7b9fea85b6e893aca874b7a0a07866443822005a (patch)
treec5f96ba7800a8ae60edcb3921fc83d01ebedffd8 /lib
parent79b3df00f82f3d0eee397f5389d23c8af1727afd (diff)
downloadnextcloud-server-7b9fea85b6e893aca874b7a0a07866443822005a.tar.gz
nextcloud-server-7b9fea85b6e893aca874b7a0a07866443822005a.zip
Add unit test and orderBy parameter
Signed-off-by: Vitor Mattos <vitor@php.rio>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php17
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php10
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php11
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php5
-rw-r--r--lib/private/DB/QueryBuilder/QueryBuilder.php8
-rw-r--r--lib/public/DB/QueryBuilder/IFunctionBuilder.php3
6 files changed, 40 insertions, 14 deletions
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
index 88290237a90..3384ad8b42f 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
@@ -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 {
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
index af72d85d511..d3553565b7e 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
@@ -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");
}
}
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
index a3a25b7e293..702b71e12b8 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
@@ -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)");
}
}
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
index 19cff52546b..796495dfd89 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
@@ -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) . ')');
}
diff --git a/lib/private/DB/QueryBuilder/QueryBuilder.php b/lib/private/DB/QueryBuilder/QueryBuilder.php
index 89265c74fae..e247d1a8c00 100644
--- a/lib/private/DB/QueryBuilder/QueryBuilder.php
+++ b/lib/private/DB/QueryBuilder/QueryBuilder.php
@@ -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);
}
/**
diff --git a/lib/public/DB/QueryBuilder/IFunctionBuilder.php b/lib/public/DB/QueryBuilder/IFunctionBuilder.php
index e04a4cbfdba..466a1c4ddf4 100644
--- a/lib/public/DB/QueryBuilder/IFunctionBuilder.php
+++ b/lib/public/DB/QueryBuilder/IFunctionBuilder.php
@@ -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