1
0

OCIFunctionBuilder.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 OCP\DB\QueryBuilder\ILiteral;
  27. use OCP\DB\QueryBuilder\IParameter;
  28. use OCP\DB\QueryBuilder\IQueryFunction;
  29. class OCIFunctionBuilder extends FunctionBuilder {
  30. public function md5($input): IQueryFunction {
  31. return new QueryFunction('LOWER(DBMS_OBFUSCATION_TOOLKIT.md5 (input => UTL_RAW.cast_to_raw(' . $this->helper->quoteColumnName($input) .')))');
  32. }
  33. /**
  34. * As per https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions060.htm
  35. * Oracle uses the first value to cast the rest or the values. So when the
  36. * first value is a literal, plain value or column, instead of doing the
  37. * math, it will cast the expression to int and continue with a "0". So when
  38. * the second parameter is a function or column, we have to put that as
  39. * first parameter.
  40. *
  41. * @param string|ILiteral|IParameter|IQueryFunction $x
  42. * @param string|ILiteral|IParameter|IQueryFunction $y
  43. * @return IQueryFunction
  44. */
  45. public function greatest($x, $y): IQueryFunction {
  46. if (is_string($y) || $y instanceof IQueryFunction) {
  47. return parent::greatest($y, $x);
  48. }
  49. return parent::greatest($x, $y);
  50. }
  51. /**
  52. * As per https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions060.htm
  53. * Oracle uses the first value to cast the rest or the values. So when the
  54. * first value is a literal, plain value or column, instead of doing the
  55. * math, it will cast the expression to int and continue with a "0". So when
  56. * the second parameter is a function or column, we have to put that as
  57. * first parameter.
  58. *
  59. * @param string|ILiteral|IParameter|IQueryFunction $x
  60. * @param string|ILiteral|IParameter|IQueryFunction $y
  61. * @return IQueryFunction
  62. */
  63. public function least($x, $y): IQueryFunction {
  64. if (is_string($y) || $y instanceof IQueryFunction) {
  65. return parent::least($y, $x);
  66. }
  67. return parent::least($x, $y);
  68. }
  69. public function concat($x, ...$expr): IQueryFunction {
  70. $args = func_get_args();
  71. $list = [];
  72. foreach ($args as $item) {
  73. $list[] = $this->helper->quoteColumnName($item);
  74. }
  75. return new QueryFunction(sprintf('(%s)', implode(' || ', $list)));
  76. }
  77. public function groupConcat($expr, ?string $separator = ','): IQueryFunction {
  78. $orderByClause = ' WITHIN GROUP(ORDER BY NULL)';
  79. if (is_null($separator)) {
  80. return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr) . ')' . $orderByClause);
  81. }
  82. $separator = $this->connection->quote($separator);
  83. return new QueryFunction('LISTAGG(' . $this->helper->quoteColumnName($expr) . ', ' . $separator . ')' . $orderByClause);
  84. }
  85. public function octetLength($field, $alias = ''): IQueryFunction {
  86. $alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
  87. $quotedName = $this->helper->quoteColumnName($field);
  88. return new QueryFunction('LENGTHB(' . $quotedName . ')' . $alias);
  89. }
  90. public function charLength($field, $alias = ''): IQueryFunction {
  91. $alias = $alias ? (' AS ' . $this->helper->quoteColumnName($alias)) : '';
  92. $quotedName = $this->helper->quoteColumnName($field);
  93. return new QueryFunction('LENGTH(' . $quotedName . ')' . $alias);
  94. }
  95. }