ExpressionBuilder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\DB\QueryBuilder\ExpressionBuilder;
  28. use Doctrine\DBAL\Query\Expression\ExpressionBuilder as DoctrineExpressionBuilder;
  29. use OC\DB\ConnectionAdapter;
  30. use OC\DB\QueryBuilder\CompositeExpression;
  31. use OC\DB\QueryBuilder\FunctionBuilder\FunctionBuilder;
  32. use OC\DB\QueryBuilder\Literal;
  33. use OC\DB\QueryBuilder\QueryFunction;
  34. use OC\DB\QueryBuilder\QuoteHelper;
  35. use OCP\DB\QueryBuilder\ICompositeExpression;
  36. use OCP\DB\QueryBuilder\IExpressionBuilder;
  37. use OCP\DB\QueryBuilder\ILiteral;
  38. use OCP\DB\QueryBuilder\IParameter;
  39. use OCP\DB\QueryBuilder\IQueryBuilder;
  40. use OCP\DB\QueryBuilder\IQueryFunction;
  41. use OCP\IDBConnection;
  42. class ExpressionBuilder implements IExpressionBuilder {
  43. /** @var \Doctrine\DBAL\Query\Expression\ExpressionBuilder */
  44. protected $expressionBuilder;
  45. /** @var QuoteHelper */
  46. protected $helper;
  47. /** @var IDBConnection */
  48. protected $connection;
  49. /** @var FunctionBuilder */
  50. protected $functionBuilder;
  51. /**
  52. * Initializes a new <tt>ExpressionBuilder</tt>.
  53. *
  54. * @param ConnectionAdapter $connection
  55. * @param IQueryBuilder $queryBuilder
  56. */
  57. public function __construct(ConnectionAdapter $connection, IQueryBuilder $queryBuilder) {
  58. $this->connection = $connection;
  59. $this->helper = new QuoteHelper();
  60. $this->expressionBuilder = new DoctrineExpressionBuilder($connection->getInner());
  61. $this->functionBuilder = $queryBuilder->func();
  62. }
  63. /**
  64. * Creates a conjunction of the given boolean expressions.
  65. *
  66. * Example:
  67. *
  68. * [php]
  69. * // (u.type = ?) AND (u.role = ?)
  70. * $expr->andX('u.type = ?', 'u.role = ?'));
  71. *
  72. * @param mixed ...$x Optional clause. Defaults = null, but requires
  73. * at least one defined when converting to string.
  74. *
  75. * @return \OCP\DB\QueryBuilder\ICompositeExpression
  76. */
  77. public function andX(...$x): ICompositeExpression {
  78. $compositeExpression = call_user_func_array([$this->expressionBuilder, 'andX'], $x);
  79. return new CompositeExpression($compositeExpression);
  80. }
  81. /**
  82. * Creates a disjunction of the given boolean expressions.
  83. *
  84. * Example:
  85. *
  86. * [php]
  87. * // (u.type = ?) OR (u.role = ?)
  88. * $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?'));
  89. *
  90. * @param mixed ...$x Optional clause. Defaults = null, but requires
  91. * at least one defined when converting to string.
  92. *
  93. * @return \OCP\DB\QueryBuilder\ICompositeExpression
  94. */
  95. public function orX(...$x): ICompositeExpression {
  96. $compositeExpression = call_user_func_array([$this->expressionBuilder, 'orX'], $x);
  97. return new CompositeExpression($compositeExpression);
  98. }
  99. /**
  100. * Creates a comparison expression.
  101. *
  102. * @param mixed $x The left expression.
  103. * @param string $operator One of the IExpressionBuilder::* constants.
  104. * @param mixed $y The right expression.
  105. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  106. * required when comparing text fields for oci compatibility
  107. *
  108. * @return string
  109. */
  110. public function comparison($x, string $operator, $y, $type = null): string {
  111. $x = $this->prepareColumn($x, $type);
  112. $y = $this->prepareColumn($y, $type);
  113. return $this->expressionBuilder->comparison($x, $operator, $y);
  114. }
  115. /**
  116. * Creates an equality comparison expression with the given arguments.
  117. *
  118. * First argument is considered the left expression and the second is the right expression.
  119. * When converted to string, it will generated a <left expr> = <right expr>. Example:
  120. *
  121. * [php]
  122. * // u.id = ?
  123. * $expr->eq('u.id', '?');
  124. *
  125. * @param mixed $x The left expression.
  126. * @param mixed $y The right expression.
  127. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  128. * required when comparing text fields for oci compatibility
  129. *
  130. * @return string
  131. */
  132. public function eq($x, $y, $type = null): string {
  133. $x = $this->prepareColumn($x, $type);
  134. $y = $this->prepareColumn($y, $type);
  135. return $this->expressionBuilder->eq($x, $y);
  136. }
  137. /**
  138. * Creates a non equality comparison expression with the given arguments.
  139. * First argument is considered the left expression and the second is the right expression.
  140. * When converted to string, it will generated a <left expr> <> <right expr>. Example:
  141. *
  142. * [php]
  143. * // u.id <> 1
  144. * $q->where($q->expr()->neq('u.id', '1'));
  145. *
  146. * @param mixed $x The left expression.
  147. * @param mixed $y The right expression.
  148. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  149. * required when comparing text fields for oci compatibility
  150. *
  151. * @return string
  152. */
  153. public function neq($x, $y, $type = null): string {
  154. $x = $this->prepareColumn($x, $type);
  155. $y = $this->prepareColumn($y, $type);
  156. return $this->expressionBuilder->neq($x, $y);
  157. }
  158. /**
  159. * Creates a lower-than comparison expression with the given arguments.
  160. * First argument is considered the left expression and the second is the right expression.
  161. * When converted to string, it will generated a <left expr> < <right expr>. Example:
  162. *
  163. * [php]
  164. * // u.id < ?
  165. * $q->where($q->expr()->lt('u.id', '?'));
  166. *
  167. * @param mixed $x The left expression.
  168. * @param mixed $y The right expression.
  169. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  170. * required when comparing text fields for oci compatibility
  171. *
  172. * @return string
  173. */
  174. public function lt($x, $y, $type = null): string {
  175. $x = $this->prepareColumn($x, $type);
  176. $y = $this->prepareColumn($y, $type);
  177. return $this->expressionBuilder->lt($x, $y);
  178. }
  179. /**
  180. * Creates a lower-than-equal comparison expression with the given arguments.
  181. * First argument is considered the left expression and the second is the right expression.
  182. * When converted to string, it will generated a <left expr> <= <right expr>. Example:
  183. *
  184. * [php]
  185. * // u.id <= ?
  186. * $q->where($q->expr()->lte('u.id', '?'));
  187. *
  188. * @param mixed $x The left expression.
  189. * @param mixed $y The right expression.
  190. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  191. * required when comparing text fields for oci compatibility
  192. *
  193. * @return string
  194. */
  195. public function lte($x, $y, $type = null): string {
  196. $x = $this->prepareColumn($x, $type);
  197. $y = $this->prepareColumn($y, $type);
  198. return $this->expressionBuilder->lte($x, $y);
  199. }
  200. /**
  201. * Creates a greater-than comparison expression with the given arguments.
  202. * First argument is considered the left expression and the second is the right expression.
  203. * When converted to string, it will generated a <left expr> > <right expr>. Example:
  204. *
  205. * [php]
  206. * // u.id > ?
  207. * $q->where($q->expr()->gt('u.id', '?'));
  208. *
  209. * @param mixed $x The left expression.
  210. * @param mixed $y The right expression.
  211. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  212. * required when comparing text fields for oci compatibility
  213. *
  214. * @return string
  215. */
  216. public function gt($x, $y, $type = null): string {
  217. $x = $this->prepareColumn($x, $type);
  218. $y = $this->prepareColumn($y, $type);
  219. return $this->expressionBuilder->gt($x, $y);
  220. }
  221. /**
  222. * Creates a greater-than-equal comparison expression with the given arguments.
  223. * First argument is considered the left expression and the second is the right expression.
  224. * When converted to string, it will generated a <left expr> >= <right expr>. Example:
  225. *
  226. * [php]
  227. * // u.id >= ?
  228. * $q->where($q->expr()->gte('u.id', '?'));
  229. *
  230. * @param mixed $x The left expression.
  231. * @param mixed $y The right expression.
  232. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  233. * required when comparing text fields for oci compatibility
  234. *
  235. * @return string
  236. */
  237. public function gte($x, $y, $type = null): string {
  238. $x = $this->prepareColumn($x, $type);
  239. $y = $this->prepareColumn($y, $type);
  240. return $this->expressionBuilder->gte($x, $y);
  241. }
  242. /**
  243. * Creates an IS NULL expression with the given arguments.
  244. *
  245. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be restricted by IS NULL.
  246. *
  247. * @return string
  248. */
  249. public function isNull($x): string {
  250. $x = $this->helper->quoteColumnName($x);
  251. return $this->expressionBuilder->isNull($x);
  252. }
  253. /**
  254. * Creates an IS NOT NULL expression with the given arguments.
  255. *
  256. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be restricted by IS NOT NULL.
  257. *
  258. * @return string
  259. */
  260. public function isNotNull($x): string {
  261. $x = $this->helper->quoteColumnName($x);
  262. return $this->expressionBuilder->isNotNull($x);
  263. }
  264. /**
  265. * Creates a LIKE() comparison expression with the given arguments.
  266. *
  267. * @param ILiteral|IParameter|IQueryFunction|string $x Field in string format to be inspected by LIKE() comparison.
  268. * @param mixed $y Argument to be used in LIKE() comparison.
  269. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  270. * required when comparing text fields for oci compatibility
  271. *
  272. * @return string
  273. */
  274. public function like($x, $y, $type = null): string {
  275. $x = $this->helper->quoteColumnName($x);
  276. $y = $this->helper->quoteColumnName($y);
  277. return $this->expressionBuilder->like($x, $y);
  278. }
  279. /**
  280. * Creates a ILIKE() comparison expression with the given arguments.
  281. *
  282. * @param string $x Field in string format to be inspected by ILIKE() comparison.
  283. * @param mixed $y Argument to be used in ILIKE() comparison.
  284. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  285. * required when comparing text fields for oci compatibility
  286. *
  287. * @return string
  288. * @since 9.0.0
  289. */
  290. public function iLike($x, $y, $type = null): string {
  291. return $this->expressionBuilder->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y));
  292. }
  293. /**
  294. * Creates a NOT LIKE() comparison expression with the given arguments.
  295. *
  296. * @param ILiteral|IParameter|IQueryFunction|string $x Field in string format to be inspected by NOT LIKE() comparison.
  297. * @param mixed $y Argument to be used in NOT LIKE() comparison.
  298. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  299. * required when comparing text fields for oci compatibility
  300. *
  301. * @return string
  302. */
  303. public function notLike($x, $y, $type = null): string {
  304. $x = $this->helper->quoteColumnName($x);
  305. $y = $this->helper->quoteColumnName($y);
  306. return $this->expressionBuilder->notLike($x, $y);
  307. }
  308. /**
  309. * Creates a IN () comparison expression with the given arguments.
  310. *
  311. * @param ILiteral|IParameter|IQueryFunction|string $x The field in string format to be inspected by IN() comparison.
  312. * @param ILiteral|IParameter|IQueryFunction|string|array $y The placeholder or the array of values to be used by IN() comparison.
  313. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  314. * required when comparing text fields for oci compatibility
  315. *
  316. * @return string
  317. */
  318. public function in($x, $y, $type = null): string {
  319. $x = $this->helper->quoteColumnName($x);
  320. $y = $this->helper->quoteColumnNames($y);
  321. return $this->expressionBuilder->in($x, $y);
  322. }
  323. /**
  324. * Creates a NOT IN () comparison expression with the given arguments.
  325. *
  326. * @param ILiteral|IParameter|IQueryFunction|string $x The field in string format to be inspected by NOT IN() comparison.
  327. * @param ILiteral|IParameter|IQueryFunction|string|array $y The placeholder or the array of values to be used by NOT IN() comparison.
  328. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  329. * required when comparing text fields for oci compatibility
  330. *
  331. * @return string
  332. */
  333. public function notIn($x, $y, $type = null): string {
  334. $x = $this->helper->quoteColumnName($x);
  335. $y = $this->helper->quoteColumnNames($y);
  336. return $this->expressionBuilder->notIn($x, $y);
  337. }
  338. /**
  339. * Creates a $x = '' statement, because Oracle needs a different check
  340. *
  341. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison.
  342. * @return string
  343. * @since 13.0.0
  344. */
  345. public function emptyString($x): string {
  346. return $this->eq($x, $this->literal('', IQueryBuilder::PARAM_STR));
  347. }
  348. /**
  349. * Creates a `$x <> ''` statement, because Oracle needs a different check
  350. *
  351. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison.
  352. * @return string
  353. * @since 13.0.0
  354. */
  355. public function nonEmptyString($x): string {
  356. return $this->neq($x, $this->literal('', IQueryBuilder::PARAM_STR));
  357. }
  358. /**
  359. * Binary AND Operator copies a bit to the result if it exists in both operands.
  360. *
  361. * @param string|ILiteral $x The field or value to check
  362. * @param int $y Bitmap that must be set
  363. * @return IQueryFunction
  364. * @since 12.0.0
  365. */
  366. public function bitwiseAnd($x, int $y): IQueryFunction {
  367. return new QueryFunction($this->connection->getDatabasePlatform()->getBitAndComparisonExpression(
  368. $this->helper->quoteColumnName($x),
  369. $y
  370. ));
  371. }
  372. /**
  373. * Binary OR Operator copies a bit if it exists in either operand.
  374. *
  375. * @param string|ILiteral $x The field or value to check
  376. * @param int $y Bitmap that must be set
  377. * @return IQueryFunction
  378. * @since 12.0.0
  379. */
  380. public function bitwiseOr($x, int $y): IQueryFunction {
  381. return new QueryFunction($this->connection->getDatabasePlatform()->getBitOrComparisonExpression(
  382. $this->helper->quoteColumnName($x),
  383. $y
  384. ));
  385. }
  386. /**
  387. * Quotes a given input parameter.
  388. *
  389. * @param mixed $input The parameter to be quoted.
  390. * @param int $type One of the IQueryBuilder::PARAM_* constants
  391. *
  392. * @return ILiteral
  393. */
  394. public function literal($input, $type = IQueryBuilder::PARAM_STR): ILiteral {
  395. return new Literal($this->expressionBuilder->literal($input, $type));
  396. }
  397. /**
  398. * Returns a IQueryFunction that casts the column to the given type
  399. *
  400. * @param string|IQueryFunction $column
  401. * @param mixed $type One of IQueryBuilder::PARAM_*
  402. * @psalm-param IQueryBuilder::PARAM_* $type
  403. * @return IQueryFunction
  404. */
  405. public function castColumn($column, $type): IQueryFunction {
  406. return new QueryFunction(
  407. $this->helper->quoteColumnName($column)
  408. );
  409. }
  410. /**
  411. * @param mixed $column
  412. * @param mixed|null $type
  413. * @return array|IQueryFunction|string
  414. */
  415. protected function prepareColumn($column, $type) {
  416. return $this->helper->quoteColumnNames($column);
  417. }
  418. }