aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2022-01-04 08:50:10 +0100
committerJoas Schilling <coding@schilljs.com>2022-01-04 09:15:17 +0100
commit46ad6fa3c7ff8a2c6ab0d7ff583cab19ba04b146 (patch)
treedaea2f7b774846d187bd8f255851c7dcec96f1cb
parente3e73a12ef42b45f773da7e18090938ed3ff1ef3 (diff)
downloadnextcloud-server-46ad6fa3c7ff8a2c6ab0d7ff583cab19ba04b146.tar.gz
nextcloud-server-46ad6fa3c7ff8a2c6ab0d7ff583cab19ba04b146.zip
Add a test with integer
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php18
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php6
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php8
-rw-r--r--lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php2
-rw-r--r--lib/private/DB/QueryBuilder/QueryBuilder.php8
-rw-r--r--lib/public/DB/QueryBuilder/IFunctionBuilder.php2
-rw-r--r--tests/lib/DB/QueryBuilder/FunctionBuilderTest.php94
7 files changed, 117 insertions, 21 deletions
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
index 6b5fb751fbd..37518bda3ec 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/FunctionBuilder.php
@@ -26,23 +26,23 @@ namespace OC\DB\QueryBuilder\FunctionBuilder;
use OC\DB\QueryBuilder\QueryFunction;
use OC\DB\QueryBuilder\QuoteHelper;
use OCP\DB\QueryBuilder\IFunctionBuilder;
+use OCP\DB\QueryBuilder\IQueryBuilder;
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(IDBConnection $connection, QuoteHelper $helper) {
+ /** @var IQueryBuilder */
+ protected $queryBuilder;
+
+ /** @var QuoteHelper */
+ protected $helper;
+
+ public function __construct(IDBConnection $connection, IQueryBuilder $queryBuilder, QuoteHelper $helper) {
$this->connection = $connection;
+ $this->queryBuilder = $queryBuilder;
$this->helper = $helper;
}
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
index 45a1e1c1ff6..a3e62e6d330 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/OCIFunctionBuilder.php
@@ -26,6 +26,7 @@ namespace OC\DB\QueryBuilder\FunctionBuilder;
use OC\DB\QueryBuilder\QueryFunction;
use OCP\DB\QueryBuilder\ILiteral;
use OCP\DB\QueryBuilder\IParameter;
+use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
class OCIFunctionBuilder extends FunctionBuilder {
@@ -78,6 +79,11 @@ class OCIFunctionBuilder extends FunctionBuilder {
if (is_null($separator)) {
return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr) . ')' . $orderByClause);
}
+
+ if ($separator === '\'') {
+ $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 f4c1364fab1..0b22775acc8 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/PgSqlFunctionBuilder.php
@@ -24,6 +24,7 @@
namespace OC\DB\QueryBuilder\FunctionBuilder;
use OC\DB\QueryBuilder\QueryFunction;
+use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
class PgSqlFunctionBuilder extends FunctionBuilder {
@@ -32,10 +33,13 @@ class PgSqlFunctionBuilder extends FunctionBuilder {
}
public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
+ $castedExpression = $this->queryBuilder->expr()->castColumn($expr, IQueryBuilder::PARAM_STR);
+
if (is_null($separator)) {
- return new QueryFunction('string_agg(cast(' . $this->helper->quoteColumnName($expr) . ' AS varchar)');
+ return new QueryFunction('string_agg(' . $castedExpression . ')');
}
+
$separator = $this->connection->quote($separator);
- return new QueryFunction('string_agg(cast(' . $this->helper->quoteColumnName($expr) . " AS varchar), $separator)");
+ return new QueryFunction('string_agg(' . $castedExpression . ', ' . $separator . ')');
}
}
diff --git a/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php b/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
index 74e4e30e7ff..1067f8f0925 100644
--- a/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
+++ b/lib/private/DB/QueryBuilder/FunctionBuilder/SqliteFunctionBuilder.php
@@ -33,7 +33,7 @@ class SqliteFunctionBuilder extends FunctionBuilder {
public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
$separator = $this->connection->quote($separator);
- return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ", $separator)");
+ return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ', ' . $separator . ')');
}
public function greatest($x, $y): IQueryFunction {
diff --git a/lib/private/DB/QueryBuilder/QueryBuilder.php b/lib/private/DB/QueryBuilder/QueryBuilder.php
index e247d1a8c00..a362ff8016e 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->connection, $this->helper);
+ return new OCIFunctionBuilder($this->connection, $this, $this->helper);
}
if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
- return new SqliteFunctionBuilder($this->connection, $this->helper);
+ return new SqliteFunctionBuilder($this->connection, $this, $this->helper);
}
if ($this->connection->getDatabasePlatform() instanceof PostgreSQL94Platform) {
- return new PgSqlFunctionBuilder($this->connection, $this->helper);
+ return new PgSqlFunctionBuilder($this->connection, $this, $this->helper);
}
- return new FunctionBuilder($this->connection, $this->helper);
+ return new FunctionBuilder($this->connection, $this, $this->helper);
}
/**
diff --git a/lib/public/DB/QueryBuilder/IFunctionBuilder.php b/lib/public/DB/QueryBuilder/IFunctionBuilder.php
index e04a4cbfdba..49be92d7852 100644
--- a/lib/public/DB/QueryBuilder/IFunctionBuilder.php
+++ b/lib/public/DB/QueryBuilder/IFunctionBuilder.php
@@ -60,7 +60,7 @@ interface IFunctionBuilder {
*
* groupConcat('column', ';') -- with different separator
*
- * @param string|ILiteral|IParameter|IQueryFunction $expr The expression to group
+ * @param string|IQueryFunction $expr The expression to group
* @param string|null $separator The separator
* @return IQueryFunction
* @since 24.0.0
diff --git a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php
index a0ae05d0149..7ff26f7e144 100644
--- a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php
+++ b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php
@@ -54,7 +54,7 @@ class FunctionBuilderTest extends TestCase {
$this->assertEquals('foobar', $column);
}
- protected function clearDummyData() {
+ protected function clearDummyData(): void {
$delete = $this->connection->getQueryBuilder();
$delete->delete('appconfig')
@@ -62,7 +62,7 @@ class FunctionBuilderTest extends TestCase {
$delete->executeStatement();
}
- protected function addDummyData() {
+ protected function addDummyData(): void {
$this->clearDummyData();
$insert = $this->connection->getQueryBuilder();
@@ -79,7 +79,7 @@ class FunctionBuilderTest extends TestCase {
$insert->executeStatement();
}
- public function testGroupConcatWithoutSeparator() {
+ public function testGroupConcatWithoutSeparator(): void {
$this->addDummyData();
$query = $this->connection->getQueryBuilder();
@@ -90,12 +90,13 @@ class FunctionBuilderTest extends TestCase {
$result = $query->execute();
$column = $result->fetchOne();
$result->closeCursor();
+ $this->assertEquals('1,2,3', $column);
$this->assertStringContainsString(',', $column);
$actual = explode(',', $column);
$this->assertEqualsCanonicalizing([1,2,3], $actual);
}
- public function testGroupConcatWithSeparator() {
+ public function testGroupConcatWithSeparator(): void {
$this->addDummyData();
$query = $this->connection->getQueryBuilder();
@@ -111,6 +112,91 @@ class FunctionBuilderTest extends TestCase {
$this->assertEqualsCanonicalizing([1,2,3], $actual);
}
+ public function testGroupConcatWithSingleQuoteSeparator(): void {
+ $this->addDummyData();
+ $query = $this->connection->getQueryBuilder();
+
+ $query->select($query->func()->groupConcat('configkey', '\''))
+ ->from('appconfig')
+ ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
+
+ $result = $query->execute();
+ $column = $result->fetchOne();
+ $result->closeCursor();
+ $this->assertEquals('1\'2\'3', $column);
+ }
+
+ public function testGroupConcatWithDoubleQuoteSeparator(): void {
+ $this->addDummyData();
+ $query = $this->connection->getQueryBuilder();
+
+ $query->select($query->func()->groupConcat('configkey', '"'))
+ ->from('appconfig')
+ ->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat')));
+
+ $result = $query->execute();
+ $column = $result->fetchOne();
+ $result->closeCursor();
+ $this->assertEquals('1"2"3', $column);
+ }
+
+ protected function clearIntDummyData(): void {
+ $delete = $this->connection->getQueryBuilder();
+
+ $delete->delete('systemtag')
+ ->where($delete->expr()->eq('name', $delete->createNamedParameter('group_concat')));
+ $delete->executeStatement();
+ }
+
+ protected function addIntDummyData(): void {
+ $this->clearIntDummyData();
+ $insert = $this->connection->getQueryBuilder();
+
+ $insert->insert('systemtag')
+ ->setValue('name', $insert->createNamedParameter('group_concat'))
+ ->setValue('visibility', $insert->createNamedParameter(1))
+ ->setValue('editable', $insert->createParameter('value'));
+
+ $insert->setParameter('value', 1);
+ $insert->executeStatement();
+ $insert->setParameter('value', 2);
+ $insert->executeStatement();
+ $insert->setParameter('value', 3);
+ $insert->executeStatement();
+ }
+
+ public function testIntGroupConcatWithoutSeparator(): void {
+ $this->addIntDummyData();
+ $query = $this->connection->getQueryBuilder();
+
+ $query->select($query->func()->groupConcat('editable'))
+ ->from('systemtag')
+ ->where($query->expr()->eq('name', $query->createNamedParameter('group_concat')));
+
+ $result = $query->execute();
+ $column = $result->fetchOne();
+ $result->closeCursor();
+ $this->assertStringContainsString(',', $column);
+ $actual = explode(',', $column);
+ $this->assertEqualsCanonicalizing([1,2,3], $actual);
+ }
+
+ public function testIntGroupConcatWithSeparator(): void {
+ $this->addIntDummyData();
+ $query = $this->connection->getQueryBuilder();
+
+ $query->select($query->func()->groupConcat('editable', '#'))
+ ->from('systemtag')
+ ->where($query->expr()->eq('name', $query->createNamedParameter('group_concat')));
+
+ $result = $query->execute();
+ $column = $result->fetchOne();
+ $result->closeCursor();
+ $this->assertStringContainsString('#', $column);
+ $actual = explode('#', $column);
+ $this->assertEqualsCanonicalizing([1,2,3], $actual);
+ }
+
public function testMd5() {
$query = $this->connection->getQueryBuilder();