OCIExpressionBuilder.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\DB\QueryBuilder\ExpressionBuilder;
  25. use OC\DB\QueryBuilder\QueryFunction;
  26. use OCP\DB\QueryBuilder\ILiteral;
  27. use OCP\DB\QueryBuilder\IParameter;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\DB\QueryBuilder\IQueryFunction;
  30. class OCIExpressionBuilder extends ExpressionBuilder {
  31. /**
  32. * @param mixed $column
  33. * @param mixed|null $type
  34. * @return array|IQueryFunction|string
  35. */
  36. protected function prepareColumn($column, $type) {
  37. if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) {
  38. $column = $this->castColumn($column, $type);
  39. } else {
  40. $column = $this->helper->quoteColumnNames($column);
  41. }
  42. return $column;
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function comparison($x, $operator, $y, $type = null) {
  48. $x = $this->prepareColumn($x, $type);
  49. $y = $this->prepareColumn($y, $type);
  50. return $this->expressionBuilder->comparison($x, $operator, $y);
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function eq($x, $y, $type = null) {
  56. $x = $this->prepareColumn($x, $type);
  57. $y = $this->prepareColumn($y, $type);
  58. return $this->expressionBuilder->eq($x, $y);
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function neq($x, $y, $type = null) {
  64. $x = $this->prepareColumn($x, $type);
  65. $y = $this->prepareColumn($y, $type);
  66. return $this->expressionBuilder->neq($x, $y);
  67. }
  68. /**
  69. * @inheritdoc
  70. */
  71. public function lt($x, $y, $type = null) {
  72. $x = $this->prepareColumn($x, $type);
  73. $y = $this->prepareColumn($y, $type);
  74. return $this->expressionBuilder->lt($x, $y);
  75. }
  76. /**
  77. * @inheritdoc
  78. */
  79. public function lte($x, $y, $type = null) {
  80. $x = $this->prepareColumn($x, $type);
  81. $y = $this->prepareColumn($y, $type);
  82. return $this->expressionBuilder->lte($x, $y);
  83. }
  84. /**
  85. * @inheritdoc
  86. */
  87. public function gt($x, $y, $type = null) {
  88. $x = $this->prepareColumn($x, $type);
  89. $y = $this->prepareColumn($y, $type);
  90. return $this->expressionBuilder->gt($x, $y);
  91. }
  92. /**
  93. * @inheritdoc
  94. */
  95. public function gte($x, $y, $type = null) {
  96. $x = $this->prepareColumn($x, $type);
  97. $y = $this->prepareColumn($y, $type);
  98. return $this->expressionBuilder->gte($x, $y);
  99. }
  100. /**
  101. * @inheritdoc
  102. */
  103. public function in($x, $y, $type = null) {
  104. $x = $this->prepareColumn($x, $type);
  105. $y = $this->prepareColumn($y, $type);
  106. return $this->expressionBuilder->in($x, $y);
  107. }
  108. /**
  109. * @inheritdoc
  110. */
  111. public function notIn($x, $y, $type = null) {
  112. $x = $this->prepareColumn($x, $type);
  113. $y = $this->prepareColumn($y, $type);
  114. return $this->expressionBuilder->notIn($x, $y);
  115. }
  116. /**
  117. * Creates a $x = '' statement, because Oracle needs a different check
  118. *
  119. * @param string $x The field in string format to be inspected by the comparison.
  120. * @return string
  121. * @since 13.0.0
  122. */
  123. public function emptyString($x) {
  124. return $this->isNull($x);
  125. }
  126. /**
  127. * Creates a `$x <> ''` statement, because Oracle needs a different check
  128. *
  129. * @param string $x The field in string format to be inspected by the comparison.
  130. * @return string
  131. * @since 13.0.0
  132. */
  133. public function nonEmptyString($x) {
  134. return $this->isNotNull($x);
  135. }
  136. /**
  137. * Returns a IQueryFunction that casts the column to the given type
  138. *
  139. * @param string $column
  140. * @param mixed $type One of IQueryBuilder::PARAM_*
  141. * @return IQueryFunction
  142. */
  143. public function castColumn($column, $type) {
  144. if ($type === IQueryBuilder::PARAM_STR) {
  145. $column = $this->helper->quoteColumnName($column);
  146. return new QueryFunction('to_char(' . $column . ')');
  147. }
  148. return parent::castColumn($column, $type);
  149. }
  150. /**
  151. * @inheritdoc
  152. */
  153. public function like($x, $y, $type = null) {
  154. return parent::like($x, $y, $type) . " ESCAPE '\\'";
  155. }
  156. /**
  157. * @inheritdoc
  158. */
  159. public function iLike($x, $y, $type = null) {
  160. $x = $this->helper->quoteColumnName($x);
  161. $y = $this->helper->quoteColumnName($y);
  162. return new QueryFunction('REGEXP_LIKE(' . $x . ', \'^\' || REPLACE(REPLACE(' . $y . ', \'%\', \'.*\'), \'_\', \'.\') || \'$\', \'i\')');
  163. }
  164. }