PgSqlFunctionBuilder.php 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\DB\QueryBuilder\FunctionBuilder;
  7. use OC\DB\QueryBuilder\QueryFunction;
  8. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\DB\QueryBuilder\IQueryFunction;
  10. class PgSqlFunctionBuilder extends FunctionBuilder {
  11. public function concat($x, ...$expr): IQueryFunction {
  12. $args = func_get_args();
  13. $list = [];
  14. foreach ($args as $item) {
  15. $list[] = $this->queryBuilder->expr()->castColumn($item, IQueryBuilder::PARAM_STR);
  16. }
  17. return new QueryFunction(sprintf('(%s)', implode(' || ', $list)));
  18. }
  19. public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
  20. $castedExpression = $this->queryBuilder->expr()->castColumn($expr, IQueryBuilder::PARAM_STR);
  21. if (is_null($separator)) {
  22. return new QueryFunction('string_agg(' . $castedExpression . ')');
  23. }
  24. $separator = $this->connection->quote($separator);
  25. return new QueryFunction('string_agg(' . $castedExpression . ', ' . $separator . ')');
  26. }
  27. }