FunctionBuilder.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\DB\QueryBuilder\FunctionBuilder;
  25. use OC\DB\QueryBuilder\QueryFunction;
  26. use OC\DB\QueryBuilder\QuoteHelper;
  27. use OCP\DB\QueryBuilder\IFunctionBuilder;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\DB\QueryBuilder\IQueryFunction;
  30. use OCP\IDBConnection;
  31. class FunctionBuilder implements IFunctionBuilder {
  32. /** @var IDBConnection */
  33. protected $connection;
  34. /** @var IQueryBuilder */
  35. protected $queryBuilder;
  36. /** @var QuoteHelper */
  37. protected $helper;
  38. public function __construct(IDBConnection $connection, IQueryBuilder $queryBuilder, QuoteHelper $helper) {
  39. $this->connection = $connection;
  40. $this->queryBuilder = $queryBuilder;
  41. $this->helper = $helper;
  42. }
  43. public function md5($input): IQueryFunction {
  44. return new QueryFunction('MD5(' . $this->helper->quoteColumnName($input) . ')');
  45. }
  46. public function concat($x, ...$expr): IQueryFunction {
  47. $args = func_get_args();
  48. $list = [];
  49. foreach ($args as $item) {
  50. $list[] = $this->helper->quoteColumnName($item);
  51. }
  52. return new QueryFunction(sprintf('CONCAT(%s)', implode(', ', $list)));
  53. }
  54. public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
  55. $separator = $this->connection->quote($separator);
  56. return new QueryFunction('GROUP_CONCAT(' . $this->helper->quoteColumnName($expr) . ' SEPARATOR ' . $separator . ')');
  57. }
  58. public function substring($input, $start, $length = null): IQueryFunction {
  59. if ($length) {
  60. return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ', ' . $this->helper->quoteColumnName($length) . ')');
  61. } else {
  62. return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ')');
  63. }
  64. }
  65. public function sum($field): IQueryFunction {
  66. return new QueryFunction('SUM(' . $this->helper->quoteColumnName($field) . ')');
  67. }
  68. public function lower($field): IQueryFunction {
  69. return new QueryFunction('LOWER(' . $this->helper->quoteColumnName($field) . ')');
  70. }
  71. public function add($x, $y): IQueryFunction {
  72. return new QueryFunction($this->helper->quoteColumnName($x) . ' + ' . $this->helper->quoteColumnName($y));
  73. }
  74. public function subtract($x, $y): IQueryFunction {
  75. return new QueryFunction($this->helper->quoteColumnName($x) . ' - ' . $this->helper->quoteColumnName($y));
  76. }
  77. public function count($count = '', $alias = ''): IQueryFunction {
  78. $alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
  79. $quotedName = $count === '' ? '*' : $this->helper->quoteColumnName($count);
  80. return new QueryFunction('COUNT(' . $quotedName . ')' . $alias);
  81. }
  82. public function octetLength($field, $alias = ''): IQueryFunction {
  83. $alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
  84. $quotedName = $this->helper->quoteColumnName($field);
  85. return new QueryFunction('OCTET_LENGTH(' . $quotedName . ')' . $alias);
  86. }
  87. public function charLength($field, $alias = ''): IQueryFunction {
  88. $alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
  89. $quotedName = $this->helper->quoteColumnName($field);
  90. return new QueryFunction('CHAR_LENGTH(' . $quotedName . ')' . $alias);
  91. }
  92. public function max($field): IQueryFunction {
  93. return new QueryFunction('MAX(' . $this->helper->quoteColumnName($field) . ')');
  94. }
  95. public function min($field): IQueryFunction {
  96. return new QueryFunction('MIN(' . $this->helper->quoteColumnName($field) . ')');
  97. }
  98. public function greatest($x, $y): IQueryFunction {
  99. return new QueryFunction('GREATEST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
  100. }
  101. public function least($x, $y): IQueryFunction {
  102. return new QueryFunction('LEAST(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')');
  103. }
  104. }