FunctionBuilder.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Connection;
  8. use OC\DB\QueryBuilder\QueryFunction;
  9. use OC\DB\QueryBuilder\QuoteHelper;
  10. use OCP\DB\QueryBuilder\IFunctionBuilder;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\DB\QueryBuilder\IQueryFunction;
  13. use OCP\IDBConnection;
  14. class FunctionBuilder implements IFunctionBuilder {
  15. /** @var IDBConnection|Connection */
  16. protected $connection;
  17. /** @var IQueryBuilder */
  18. protected $queryBuilder;
  19. /** @var QuoteHelper */
  20. protected $helper;
  21. public function __construct(IDBConnection $connection, IQueryBuilder $queryBuilder, QuoteHelper $helper) {
  22. $this->connection = $connection;
  23. $this->queryBuilder = $queryBuilder;
  24. $this->helper = $helper;
  25. }
  26. public function md5($input): IQueryFunction {
  27. return new QueryFunction('MD5(' . $this->helper->quoteColumnName($input) . ')');
  28. }
  29. public function concat($x, ...$expr): IQueryFunction {
  30. $args = func_get_args();
  31. $list = [];
  32. foreach ($args as $item) {
  33. $list[] = $this->helper->quoteColumnName($item);
  34. }
  35. return new QueryFunction(sprintf('CONCAT(%s)', implode(', ', $list)));
  36. }
  37. public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
  38. $separator = $this->connection->quote($separator);
  39. return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ' SEPARATOR ' . $separator . ')');
  40. }
  41. public function substring($input, $start, $length = null): IQueryFunction {
  42. if ($length) {
  43. return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ', ' . $this->helper->quoteColumnName($length) . ')');
  44. } else {
  45. return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ')');
  46. }
  47. }
  48. public function sum($field): IQueryFunction {
  49. return new QueryFunction('SUM(' . $this->helper->quoteColumnName($field) . ')');
  50. }
  51. public function lower($field): IQueryFunction {
  52. return new QueryFunction('LOWER(' . $this->helper->quoteColumnName($field) . ')');
  53. }
  54. public function add($x, $y): IQueryFunction {
  55. return new QueryFunction($this->helper->quoteColumnName($x) . ' + ' . $this->helper->quoteColumnName($y));
  56. }
  57. public function subtract($x, $y): IQueryFunction {
  58. return new QueryFunction($this->helper->quoteColumnName($x) . ' - ' . $this->helper->quoteColumnName($y));
  59. }
  60. public function count($count = '', $alias = ''): IQueryFunction {
  61. $alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
  62. $quotedName = $count === '' ? '*' : $this->helper->quoteColumnName($count);
  63. return new QueryFunction('COUNT(' . $quotedName . ')' . $alias);
  64. }
  65. public function octetLength($field, $alias = ''): IQueryFunction {
  66. $alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
  67. $quotedName = $this->helper->quoteColumnName($field);
  68. return new QueryFunction('OCTET_LENGTH(' . $quotedName . ')' . $alias);
  69. }
  70. public function charLength($field, $alias = ''): IQueryFunction {
  71. $alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
  72. $quotedName = $this->helper->quoteColumnName($field);
  73. return new QueryFunction('CHAR_LENGTH(' . $quotedName . ')' . $alias);
  74. }
  75. public function max($field): IQueryFunction {
  76. return new QueryFunction('MAX(' . $this->helper->quoteColumnName($field) . ')');
  77. }
  78. public function min($field): IQueryFunction {
  79. return new QueryFunction('MIN(' . $this->helper->quoteColumnName($field) . ')');
  80. }
  81. public function greatest($x, $y): IQueryFunction {
  82. return new QueryFunction('GREATEST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
  83. }
  84. public function least($x, $y): IQueryFunction {
  85. return new QueryFunction('LEAST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
  86. }
  87. }