Add unit test

Signed-off-by: Vitor Mattos <vitor@php.rio>
pull/30393/head
Vitor Mattos 4 years ago
parent 7b9fea85b6
commit fb6a9f308d
No known key found for this signature in database
GPG Key ID: B7AB4B76A7CA7318
  1. 9
      lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
  2. 7
      lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
  3. 12
      lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
  4. 6
      lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
  5. 3
      lib/public/DB/QueryBuilder/IFunctionBuilder.php
  6. 40
      tests/lib/DB/QueryBuilder/FunctionBuilderTest.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 {

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

@ -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)");
}
}

@ -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 {

@ -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

@ -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();

Loading…
Cancel
Save