OCIExpressionBuilder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\DB\QueryBuilder\ExpressionBuilder;
  26. use OC\DB\QueryBuilder\QueryFunction;
  27. use OCP\DB\QueryBuilder\ILiteral;
  28. use OCP\DB\QueryBuilder\IParameter;
  29. use OCP\DB\QueryBuilder\IQueryBuilder;
  30. use OCP\DB\QueryBuilder\IQueryFunction;
  31. class OCIExpressionBuilder extends ExpressionBuilder {
  32. /**
  33. * @param mixed $column
  34. * @param mixed|null $type
  35. * @return array|IQueryFunction|string
  36. */
  37. protected function prepareColumn($column, $type) {
  38. if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) {
  39. $column = $this->castColumn($column, $type);
  40. } else {
  41. $column = $this->helper->quoteColumnNames($column);
  42. }
  43. return $column;
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. public function comparison($x, string $operator, $y, $type = null): string {
  49. $x = $this->prepareColumn($x, $type);
  50. $y = $this->prepareColumn($y, $type);
  51. return $this->expressionBuilder->comparison($x, $operator, $y);
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. public function eq($x, $y, $type = null): string {
  57. $x = $this->prepareColumn($x, $type);
  58. $y = $this->prepareColumn($y, $type);
  59. return $this->expressionBuilder->eq($x, $y);
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function neq($x, $y, $type = null): string {
  65. $x = $this->prepareColumn($x, $type);
  66. $y = $this->prepareColumn($y, $type);
  67. return $this->expressionBuilder->neq($x, $y);
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. public function lt($x, $y, $type = null): string {
  73. $x = $this->prepareColumn($x, $type);
  74. $y = $this->prepareColumn($y, $type);
  75. return $this->expressionBuilder->lt($x, $y);
  76. }
  77. /**
  78. * @inheritdoc
  79. */
  80. public function lte($x, $y, $type = null): string {
  81. $x = $this->prepareColumn($x, $type);
  82. $y = $this->prepareColumn($y, $type);
  83. return $this->expressionBuilder->lte($x, $y);
  84. }
  85. /**
  86. * @inheritdoc
  87. */
  88. public function gt($x, $y, $type = null): string {
  89. $x = $this->prepareColumn($x, $type);
  90. $y = $this->prepareColumn($y, $type);
  91. return $this->expressionBuilder->gt($x, $y);
  92. }
  93. /**
  94. * @inheritdoc
  95. */
  96. public function gte($x, $y, $type = null): string {
  97. $x = $this->prepareColumn($x, $type);
  98. $y = $this->prepareColumn($y, $type);
  99. return $this->expressionBuilder->gte($x, $y);
  100. }
  101. /**
  102. * @inheritdoc
  103. */
  104. public function in($x, $y, $type = null): string {
  105. $x = $this->prepareColumn($x, $type);
  106. $y = $this->prepareColumn($y, $type);
  107. return $this->expressionBuilder->in($x, $y);
  108. }
  109. /**
  110. * @inheritdoc
  111. */
  112. public function notIn($x, $y, $type = null): string {
  113. $x = $this->prepareColumn($x, $type);
  114. $y = $this->prepareColumn($y, $type);
  115. return $this->expressionBuilder->notIn($x, $y);
  116. }
  117. /**
  118. * Creates a $x = '' statement, because Oracle needs a different check
  119. *
  120. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison.
  121. * @return string
  122. * @since 13.0.0
  123. */
  124. public function emptyString($x): string {
  125. return $this->isNull($x);
  126. }
  127. /**
  128. * Creates a `$x <> ''` statement, because Oracle needs a different check
  129. *
  130. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison.
  131. * @return string
  132. * @since 13.0.0
  133. */
  134. public function nonEmptyString($x): string {
  135. return $this->isNotNull($x);
  136. }
  137. /**
  138. * Returns a IQueryFunction that casts the column to the given type
  139. *
  140. * @param string|IQueryFunction $column
  141. * @param mixed $type One of IQueryBuilder::PARAM_*
  142. * @psalm-param IQueryBuilder::PARAM_* $type
  143. * @return IQueryFunction
  144. */
  145. public function castColumn($column, $type): IQueryFunction {
  146. if ($type === IQueryBuilder::PARAM_STR) {
  147. $column = $this->helper->quoteColumnName($column);
  148. return new QueryFunction('to_char(' . $column . ')');
  149. }
  150. if ($type === IQueryBuilder::PARAM_INT) {
  151. $column = $this->helper->quoteColumnName($column);
  152. return new QueryFunction('to_number(to_char(' . $column . '))');
  153. }
  154. return parent::castColumn($column, $type);
  155. }
  156. /**
  157. * @inheritdoc
  158. */
  159. public function like($x, $y, $type = null): string {
  160. return parent::like($x, $y, $type) . " ESCAPE '\\'";
  161. }
  162. /**
  163. * @inheritdoc
  164. */
  165. public function iLike($x, $y, $type = null): string {
  166. return $this->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y));
  167. }
  168. }