OCIFunctionBuilder.php 3.4 KB

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