1
0

OCIExpressionBuilder.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\DB\QueryBuilder\ExpressionBuilder;
  8. use OC\DB\QueryBuilder\QueryFunction;
  9. use OCP\DB\QueryBuilder\ILiteral;
  10. use OCP\DB\QueryBuilder\IParameter;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\DB\QueryBuilder\IQueryFunction;
  13. class OCIExpressionBuilder extends ExpressionBuilder {
  14. /**
  15. * @param mixed $column
  16. * @param mixed|null $type
  17. * @return array|IQueryFunction|string
  18. */
  19. protected function prepareColumn($column, $type) {
  20. if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) {
  21. $column = $this->castColumn($column, $type);
  22. }
  23. return parent::prepareColumn($column, $type);
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. public function in($x, $y, $type = null): string {
  29. $x = $this->prepareColumn($x, $type);
  30. $y = $this->prepareColumn($y, $type);
  31. return $this->expressionBuilder->in($x, $y);
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function notIn($x, $y, $type = null): string {
  37. $x = $this->prepareColumn($x, $type);
  38. $y = $this->prepareColumn($y, $type);
  39. return $this->expressionBuilder->notIn($x, $y);
  40. }
  41. /**
  42. * Creates a $x = '' statement, because Oracle needs a different check
  43. *
  44. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison.
  45. * @return string
  46. * @since 13.0.0
  47. */
  48. public function emptyString($x): string {
  49. return $this->isNull($x);
  50. }
  51. /**
  52. * Creates a `$x <> ''` statement, because Oracle needs a different check
  53. *
  54. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison.
  55. * @return string
  56. * @since 13.0.0
  57. */
  58. public function nonEmptyString($x): string {
  59. return $this->isNotNull($x);
  60. }
  61. /**
  62. * Returns a IQueryFunction that casts the column to the given type
  63. *
  64. * @param string|IQueryFunction $column
  65. * @param mixed $type One of IQueryBuilder::PARAM_*
  66. * @psalm-param IQueryBuilder::PARAM_* $type
  67. * @return IQueryFunction
  68. */
  69. public function castColumn($column, $type): IQueryFunction {
  70. if ($type === IQueryBuilder::PARAM_STR) {
  71. $column = $this->helper->quoteColumnName($column);
  72. return new QueryFunction('to_char(' . $column . ')');
  73. }
  74. if ($type === IQueryBuilder::PARAM_INT) {
  75. $column = $this->helper->quoteColumnName($column);
  76. return new QueryFunction('to_number(to_char(' . $column . '))');
  77. }
  78. return parent::castColumn($column, $type);
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. public function like($x, $y, $type = null): string {
  84. return parent::like($x, $y, $type) . " ESCAPE '\\'";
  85. }
  86. /**
  87. * @inheritdoc
  88. */
  89. public function iLike($x, $y, $type = null): string {
  90. return $this->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y));
  91. }
  92. }