IExpressionBuilder.php 13 KB

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