IExpressionBuilder.php 13 KB

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