IExpressionBuilder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 Lukas Reschke <lukas@statuscode.ch>
  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 OCP\DB\QueryBuilder;
  28. use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
  29. /**
  30. * This class provides a wrapper around Doctrine's ExpressionBuilder
  31. * @since 8.2.0
  32. *
  33. * @psalm-taint-specialize
  34. */
  35. interface IExpressionBuilder {
  36. /**
  37. * @since 9.0.0
  38. */
  39. public const EQ = ExpressionBuilder::EQ;
  40. /**
  41. * @since 9.0.0
  42. */
  43. public const NEQ = ExpressionBuilder::NEQ;
  44. /**
  45. * @since 9.0.0
  46. */
  47. public const LT = ExpressionBuilder::LT;
  48. /**
  49. * @since 9.0.0
  50. */
  51. public const LTE = ExpressionBuilder::LTE;
  52. /**
  53. * @since 9.0.0
  54. */
  55. public const GT = ExpressionBuilder::GT;
  56. /**
  57. * @since 9.0.0
  58. */
  59. public const GTE = ExpressionBuilder::GTE;
  60. /**
  61. * Creates a conjunction of the given boolean expressions.
  62. *
  63. * Example:
  64. *
  65. * [php]
  66. * // (u.type = ?) AND (u.role = ?)
  67. * $expr->andX('u.type = ?', 'u.role = ?'));
  68. *
  69. * @param mixed ...$x Optional clause. Defaults = null, but requires
  70. * at least one defined when converting to string.
  71. *
  72. * @return \OCP\DB\QueryBuilder\ICompositeExpression
  73. * @since 8.2.0
  74. *
  75. * @psalm-taint-sink sql $x
  76. */
  77. public function andX(...$x): ICompositeExpression;
  78. /**
  79. * Creates a disjunction of the given boolean expressions.
  80. *
  81. * Example:
  82. *
  83. * [php]
  84. * // (u.type = ?) OR (u.role = ?)
  85. * $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?'));
  86. *
  87. * @param mixed ...$x Optional clause. Defaults = null, but requires
  88. * at least one defined when converting to string.
  89. *
  90. * @return \OCP\DB\QueryBuilder\ICompositeExpression
  91. * @since 8.2.0
  92. *
  93. * @psalm-taint-sink sql $x
  94. */
  95. public function orX(...$x): ICompositeExpression;
  96. /**
  97. * Creates a comparison expression.
  98. *
  99. * @param mixed $x The left expression.
  100. * @param string $operator One of the IExpressionBuilder::* constants.
  101. * @param mixed $y The right expression.
  102. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  103. * required when comparing text fields for oci compatibility
  104. *
  105. * @return string
  106. * @since 8.2.0 - Parameter $type was added in 9.0.0
  107. *
  108. * @psalm-taint-sink sql $x
  109. * @psalm-taint-sink sql $operator
  110. * @psalm-taint-sink sql $y
  111. * @psalm-taint-sink sql $type
  112. */
  113. public function comparison($x, string $operator, $y, $type = null): string;
  114. /**
  115. * Creates an equality comparison expression with the given arguments.
  116. *
  117. * First argument is considered the left expression and the second is the right expression.
  118. * When converted to string, it will generated a <left expr> = <right expr>. Example:
  119. *
  120. * [php]
  121. * // u.id = ?
  122. * $expr->eq('u.id', '?');
  123. *
  124. * @param mixed $x The left expression.
  125. * @param mixed $y The right expression.
  126. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  127. * required when comparing text fields for oci compatibility
  128. *
  129. * @return string
  130. * @since 8.2.0 - Parameter $type was added in 9.0.0
  131. *
  132. * @psalm-taint-sink sql $x
  133. * @psalm-taint-sink sql $y
  134. * @psalm-taint-sink sql $type
  135. */
  136. public function eq($x, $y, $type = null): string;
  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. * @since 8.2.0 - Parameter $type was added in 9.0.0
  153. *
  154. * @psalm-taint-sink sql $x
  155. * @psalm-taint-sink sql $y
  156. * @psalm-taint-sink sql $type
  157. */
  158. public function neq($x, $y, $type = null): string;
  159. /**
  160. * Creates a lower-than comparison expression with the given arguments.
  161. * First argument is considered the left expression and the second is the right expression.
  162. * When converted to string, it will generated a <left expr> < <right expr>. Example:
  163. *
  164. * [php]
  165. * // u.id < ?
  166. * $q->where($q->expr()->lt('u.id', '?'));
  167. *
  168. * @param mixed $x The left expression.
  169. * @param mixed $y The right expression.
  170. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  171. * required when comparing text fields for oci compatibility
  172. *
  173. * @return string
  174. * @since 8.2.0 - Parameter $type was added in 9.0.0
  175. *
  176. * @psalm-taint-sink sql $x
  177. * @psalm-taint-sink sql $y
  178. * @psalm-taint-sink sql $type
  179. */
  180. public function lt($x, $y, $type = null): string;
  181. /**
  182. * Creates a lower-than-equal comparison expression with the given arguments.
  183. * First argument is considered the left expression and the second is the right expression.
  184. * When converted to string, it will generated a <left expr> <= <right expr>. Example:
  185. *
  186. * [php]
  187. * // u.id <= ?
  188. * $q->where($q->expr()->lte('u.id', '?'));
  189. *
  190. * @param mixed $x The left expression.
  191. * @param mixed $y The right expression.
  192. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  193. * required when comparing text fields for oci compatibility
  194. *
  195. * @return string
  196. * @since 8.2.0 - Parameter $type was added in 9.0.0
  197. *
  198. * @psalm-taint-sink sql $x
  199. * @psalm-taint-sink sql $y
  200. * @psalm-taint-sink sql $type
  201. */
  202. public function lte($x, $y, $type = null): string;
  203. /**
  204. * Creates a greater-than comparison expression with the given arguments.
  205. * First argument is considered the left expression and the second is the right expression.
  206. * When converted to string, it will generated a <left expr> > <right expr>. Example:
  207. *
  208. * [php]
  209. * // u.id > ?
  210. * $q->where($q->expr()->gt('u.id', '?'));
  211. *
  212. * @param mixed $x The left expression.
  213. * @param mixed $y The right expression.
  214. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  215. * required when comparing text fields for oci compatibility
  216. *
  217. * @return string
  218. * @since 8.2.0 - Parameter $type was added in 9.0.0
  219. *
  220. * @psalm-taint-sink sql $x
  221. * @psalm-taint-sink sql $y
  222. * @psalm-taint-sink sql $type
  223. */
  224. public function gt($x, $y, $type = null): string;
  225. /**
  226. * Creates a greater-than-equal comparison expression with the given arguments.
  227. * First argument is considered the left expression and the second is the right expression.
  228. * When converted to string, it will generated a <left expr> >= <right expr>. Example:
  229. *
  230. * [php]
  231. * // u.id >= ?
  232. * $q->where($q->expr()->gte('u.id', '?'));
  233. *
  234. * @param mixed $x The left expression.
  235. * @param mixed $y The right expression.
  236. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  237. * required when comparing text fields for oci compatibility
  238. *
  239. * @return string
  240. * @since 8.2.0 - Parameter $type was added in 9.0.0
  241. *
  242. * @psalm-taint-sink sql $x
  243. * @psalm-taint-sink sql $y
  244. * @psalm-taint-sink sql $type
  245. */
  246. public function gte($x, $y, $type = null): string;
  247. /**
  248. * Creates an IS NULL expression with the given arguments.
  249. *
  250. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be restricted by IS NULL.
  251. *
  252. * @return string
  253. * @since 8.2.0
  254. *
  255. * @psalm-taint-sink sql $x
  256. */
  257. public function isNull($x): string;
  258. /**
  259. * Creates an IS NOT NULL expression with the given arguments.
  260. *
  261. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be restricted by IS NOT NULL.
  262. *
  263. * @return string
  264. * @since 8.2.0
  265. *
  266. * @psalm-taint-sink sql $x
  267. */
  268. public function isNotNull($x): string;
  269. /**
  270. * Creates a LIKE() comparison expression with the given arguments.
  271. *
  272. * @param ILiteral|IParameter|IQueryFunction|string $x Field in string format to be inspected by LIKE() comparison.
  273. * @param mixed $y Argument to be used in LIKE() comparison.
  274. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  275. * required when comparing text fields for oci compatibility
  276. *
  277. * @return string
  278. * @since 8.2.0 - Parameter $type was added in 9.0.0
  279. *
  280. * @psalm-taint-sink sql $x
  281. * @psalm-taint-sink sql $y
  282. * @psalm-taint-sink sql $type
  283. */
  284. public function like($x, $y, $type = null): string;
  285. /**
  286. * Creates a NOT LIKE() comparison expression with the given arguments.
  287. *
  288. * @param ILiteral|IParameter|IQueryFunction|string $x Field in string format to be inspected by NOT LIKE() comparison.
  289. * @param mixed $y Argument to be used in NOT LIKE() comparison.
  290. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  291. * required when comparing text fields for oci compatibility
  292. *
  293. * @return string
  294. * @since 8.2.0 - Parameter $type was added in 9.0.0
  295. *
  296. * @psalm-taint-sink sql $x
  297. * @psalm-taint-sink sql $y
  298. * @psalm-taint-sink sql $type
  299. */
  300. public function notLike($x, $y, $type = null): string;
  301. /**
  302. * Creates a ILIKE() comparison expression with the given arguments.
  303. *
  304. * @param string $x Field in string format to be inspected by ILIKE() comparison.
  305. * @param mixed $y Argument to be used in ILIKE() comparison.
  306. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  307. * required when comparing text fields for oci compatibility
  308. *
  309. * @return string
  310. * @since 9.0.0
  311. *
  312. * @psalm-taint-sink sql $x
  313. * @psalm-taint-sink sql $y
  314. * @psalm-taint-sink sql $type
  315. */
  316. public function iLike($x, $y, $type = null): string;
  317. /**
  318. * Creates a IN () comparison expression with the given arguments.
  319. *
  320. * @param ILiteral|IParameter|IQueryFunction|string $x The field in string format to be inspected by IN() comparison.
  321. * @param ILiteral|IParameter|IQueryFunction|string|array $y The placeholder or the array of values to be used by IN() comparison.
  322. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  323. * required when comparing text fields for oci compatibility
  324. *
  325. * @return string
  326. * @since 8.2.0 - Parameter $type was added in 9.0.0
  327. *
  328. * @psalm-taint-sink sql $x
  329. * @psalm-taint-sink sql $y
  330. * @psalm-taint-sink sql $type
  331. */
  332. public function in($x, $y, $type = null): string;
  333. /**
  334. * Creates a NOT IN () comparison expression with the given arguments.
  335. *
  336. * @param ILiteral|IParameter|IQueryFunction|string $x The field in string format to be inspected by NOT IN() comparison.
  337. * @param ILiteral|IParameter|IQueryFunction|string|array $y The placeholder or the array of values to be used by NOT IN() comparison.
  338. * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants
  339. * required when comparing text fields for oci compatibility
  340. *
  341. * @return string
  342. * @since 8.2.0 - Parameter $type was added in 9.0.0
  343. *
  344. * @psalm-taint-sink sql $x
  345. * @psalm-taint-sink sql $y
  346. * @psalm-taint-sink sql $type
  347. */
  348. public function notIn($x, $y, $type = null): string;
  349. /**
  350. * Creates a $x = '' statement, because Oracle needs a different check
  351. *
  352. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison.
  353. * @return string
  354. * @since 13.0.0
  355. *
  356. * @psalm-taint-sink sql $x
  357. */
  358. public function emptyString($x): string;
  359. /**
  360. * Creates a `$x <> ''` statement, because Oracle needs a different check
  361. *
  362. * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison.
  363. * @return string
  364. * @since 13.0.0
  365. *
  366. * @psalm-taint-sink sql $x
  367. */
  368. public function nonEmptyString($x): string;
  369. /**
  370. * Creates a bitwise AND comparison
  371. *
  372. * @param string|ILiteral $x The field or value to check
  373. * @param int $y Bitmap that must be set
  374. * @return IQueryFunction
  375. * @since 12.0.0
  376. *
  377. * @psalm-taint-sink sql $x
  378. * @psalm-taint-sink sql $y
  379. */
  380. public function bitwiseAnd($x, int $y): IQueryFunction;
  381. /**
  382. * Creates a bitwise OR comparison
  383. *
  384. * @param string|ILiteral $x The field or value to check
  385. * @param int $y Bitmap that must be set
  386. * @return IQueryFunction
  387. * @since 12.0.0
  388. *
  389. * @psalm-taint-sink sql $x
  390. * @psalm-taint-sink sql $y
  391. */
  392. public function bitwiseOr($x, int $y): IQueryFunction;
  393. /**
  394. * Quotes a given input parameter.
  395. *
  396. * @param mixed $input The parameter to be quoted.
  397. * @param int $type One of the IQueryBuilder::PARAM_* constants
  398. *
  399. * @return ILiteral
  400. * @since 8.2.0
  401. *
  402. * @psalm-taint-sink sql $input
  403. * @psalm-taint-sink sql $type
  404. */
  405. public function literal($input, $type = IQueryBuilder::PARAM_STR): ILiteral;
  406. /**
  407. * Returns a IQueryFunction that casts the column to the given type
  408. *
  409. * @param string|IQueryFunction $column
  410. * @param mixed $type One of IQueryBuilder::PARAM_*
  411. * @psalm-param IQueryBuilder::PARAM_* $type
  412. * @return IQueryFunction
  413. * @since 9.0.0
  414. *
  415. * @psalm-taint-sink sql $column
  416. * @psalm-taint-sink sql $type
  417. */
  418. public function castColumn($column, $type): IQueryFunction;
  419. }