1
0

IQueryBuilder.php 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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\ArrayParameterType;
  9. use Doctrine\DBAL\Connection;
  10. use Doctrine\DBAL\ParameterType;
  11. use Doctrine\DBAL\Types\Types;
  12. use OCP\DB\Exception;
  13. use OCP\DB\IResult;
  14. use OCP\IDBConnection;
  15. /**
  16. * This class provides a wrapper around Doctrine's QueryBuilder
  17. * @since 8.2.0
  18. *
  19. * @psalm-taint-specialize
  20. */
  21. interface IQueryBuilder {
  22. /**
  23. * @since 9.0.0
  24. */
  25. public const PARAM_NULL = ParameterType::NULL;
  26. /**
  27. * @since 9.0.0
  28. */
  29. public const PARAM_BOOL = Types::BOOLEAN;
  30. /**
  31. * @since 9.0.0
  32. */
  33. public const PARAM_INT = ParameterType::INTEGER;
  34. /**
  35. * @since 9.0.0
  36. */
  37. public const PARAM_STR = ParameterType::STRING;
  38. /**
  39. * @since 9.0.0
  40. */
  41. public const PARAM_LOB = ParameterType::LARGE_OBJECT;
  42. /**
  43. * @since 9.0.0
  44. * @deprecated 31.0.0 - use PARAM_DATETIME_MUTABLE instead
  45. */
  46. public const PARAM_DATE = Types::DATETIME_MUTABLE;
  47. /**
  48. * For passing a \DateTime instance when only interested in the time part (without timezone support)
  49. * @since 31.0.0
  50. */
  51. public const PARAM_TIME_MUTABLE = Types::TIME_MUTABLE;
  52. /**
  53. * For passing a \DateTime instance when only interested in the date part (without timezone support)
  54. * @since 31.0.0
  55. */
  56. public const PARAM_DATE_MUTABLE = Types::DATE_MUTABLE;
  57. /**
  58. * For passing a \DateTime instance (without timezone support)
  59. * @since 31.0.0
  60. */
  61. public const PARAM_DATETIME_MUTABLE = Types::DATETIME_MUTABLE;
  62. /**
  63. * For passing a \DateTime instance with timezone support
  64. * @since 31.0.0
  65. */
  66. public const PARAM_DATETIME_TZ_MUTABLE = Types::DATETIMETZ_MUTABLE;
  67. /**
  68. * For passing a \DateTimeImmutable instance when only interested in the time part (without timezone support)
  69. * @since 31.0.0
  70. */
  71. public const PARAM_TIME_IMMUTABLE = Types::TIME_MUTABLE;
  72. /**
  73. * For passing a \DateTime instance when only interested in the date part (without timezone support)
  74. * @since 9.0.0
  75. */
  76. public const PARAM_DATE_IMMUTABLE = Types::DATE_IMMUTABLE;
  77. /**
  78. * For passing a \DateTime instance (without timezone support)
  79. * @since 31.0.0
  80. */
  81. public const PARAM_DATETIME_IMMUTABLE = Types::DATETIME_IMMUTABLE;
  82. /**
  83. * For passing a \DateTime instance with timezone support
  84. * @since 31.0.0
  85. */
  86. public const PARAM_DATETIME_TZ_IMMUTABLE = Types::DATETIMETZ_IMMUTABLE;
  87. /**
  88. * @since 24.0.0
  89. */
  90. public const PARAM_JSON = 'json';
  91. /**
  92. * @since 9.0.0
  93. */
  94. public const PARAM_INT_ARRAY = ArrayParameterType::INTEGER;
  95. /**
  96. * @since 9.0.0
  97. */
  98. public const PARAM_STR_ARRAY = ArrayParameterType::STRING;
  99. /**
  100. * @since 24.0.0 Indicates how many rows can be deleted at once with MySQL
  101. * database server.
  102. */
  103. public const MAX_ROW_DELETION = 100000;
  104. /**
  105. * Enable/disable automatic prefixing of table names with the oc_ prefix
  106. *
  107. * @param bool $enabled If set to true table names will be prefixed with the
  108. * owncloud database prefix automatically.
  109. * @since 8.2.0
  110. */
  111. public function automaticTablePrefix($enabled);
  112. /**
  113. * Gets an ExpressionBuilder used for object-oriented construction of query expressions.
  114. * This producer method is intended for convenient inline usage. Example:
  115. *
  116. * <code>
  117. * $qb = $conn->getQueryBuilder()
  118. * ->select('u')
  119. * ->from('users', 'u')
  120. * ->where($qb->expr()->eq('u.id', 1));
  121. * </code>
  122. *
  123. * For more complex expression construction, consider storing the expression
  124. * builder object in a local variable.
  125. *
  126. * @return \OCP\DB\QueryBuilder\IExpressionBuilder
  127. * @since 8.2.0
  128. */
  129. public function expr();
  130. /**
  131. * Gets an FunctionBuilder used for object-oriented construction of query functions.
  132. * This producer method is intended for convenient inline usage. Example:
  133. *
  134. * <code>
  135. * $qb = $conn->getQueryBuilder()
  136. * ->select('u')
  137. * ->from('users', 'u')
  138. * ->where($qb->fun()->md5('u.id'));
  139. * </code>
  140. *
  141. * For more complex function construction, consider storing the function
  142. * builder object in a local variable.
  143. *
  144. * @return \OCP\DB\QueryBuilder\IFunctionBuilder
  145. * @since 12.0.0
  146. */
  147. public function func();
  148. /**
  149. * Gets the type of the currently built query.
  150. *
  151. * @return integer
  152. * @since 8.2.0
  153. */
  154. public function getType();
  155. /**
  156. * Gets the associated DBAL Connection for this query builder.
  157. *
  158. * @return \OCP\IDBConnection
  159. * @since 8.2.0
  160. */
  161. public function getConnection();
  162. /**
  163. * Gets the state of this query builder instance.
  164. *
  165. * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN.
  166. * @since 8.2.0
  167. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  168. * and we can not fix this in our wrapper.
  169. */
  170. public function getState();
  171. /**
  172. * Executes this query using the bound parameters and their types.
  173. *
  174. * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeStatement}
  175. * for insert, update and delete statements.
  176. *
  177. * Warning: until Nextcloud 20, this method could return a \Doctrine\DBAL\Driver\Statement but since
  178. * that interface changed in a breaking way the adapter \OCP\DB\QueryBuilder\IStatement is returned
  179. * to bridge old code to the new API
  180. *
  181. * @param ?IDBConnection $connection (optional) the connection to run the query against. since 30.0
  182. * @return IResult|int
  183. * @throws Exception since 21.0.0
  184. * @since 8.2.0
  185. * @deprecated 22.0.0 Use executeQuery or executeStatement
  186. */
  187. public function execute(?IDBConnection $connection = null);
  188. /**
  189. * Execute for select statements
  190. *
  191. * @param ?IDBConnection $connection (optional) the connection to run the query against. since 30.0
  192. * @return IResult
  193. * @since 22.0.0
  194. *
  195. * @throws Exception
  196. * @throws \RuntimeException in case of usage with non select query
  197. */
  198. public function executeQuery(?IDBConnection $connection = null): IResult;
  199. /**
  200. * Execute insert, update and delete statements
  201. *
  202. * @param ?IDBConnection $connection (optional) the connection to run the query against. since 30.0
  203. * @return int the number of affected rows
  204. * @since 22.0.0
  205. *
  206. * @throws Exception
  207. * @throws \RuntimeException in case of usage with select query
  208. */
  209. public function executeStatement(?IDBConnection $connection = null): int;
  210. /**
  211. * Gets the complete SQL string formed by the current specifications of this QueryBuilder.
  212. *
  213. * <code>
  214. * $qb = $conn->getQueryBuilder()
  215. * ->select('u')
  216. * ->from('User', 'u')
  217. * echo $qb->getSQL(); // SELECT u FROM User u
  218. * </code>
  219. *
  220. * @return string The SQL query string.
  221. * @since 8.2.0
  222. */
  223. public function getSQL();
  224. /**
  225. * Sets a query parameter for the query being constructed.
  226. *
  227. * <code>
  228. * $qb = $conn->getQueryBuilder()
  229. * ->select('u')
  230. * ->from('users', 'u')
  231. * ->where('u.id = :user_id')
  232. * ->setParameter(':user_id', 1);
  233. * </code>
  234. *
  235. * @param string|integer $key The parameter position or name.
  236. * @param mixed $value The parameter value.
  237. * @param string|null|int $type One of the IQueryBuilder::PARAM_* constants.
  238. *
  239. * @return $this This QueryBuilder instance.
  240. * @since 8.2.0
  241. */
  242. public function setParameter($key, $value, $type = null);
  243. /**
  244. * Sets a collection of query parameters for the query being constructed.
  245. *
  246. * <code>
  247. * $qb = $conn->getQueryBuilder()
  248. * ->select('u')
  249. * ->from('users', 'u')
  250. * ->where('u.id = :user_id1 OR u.id = :user_id2')
  251. * ->setParameters(array(
  252. * ':user_id1' => 1,
  253. * ':user_id2' => 2
  254. * ));
  255. * </code>
  256. *
  257. * @param array $params The query parameters to set.
  258. * @param array $types The query parameters types to set.
  259. *
  260. * @return $this This QueryBuilder instance.
  261. * @since 8.2.0
  262. */
  263. public function setParameters(array $params, array $types = []);
  264. /**
  265. * Gets all defined query parameters for the query being constructed indexed by parameter index or name.
  266. *
  267. * @return array The currently defined query parameters indexed by parameter index or name.
  268. * @since 8.2.0
  269. */
  270. public function getParameters();
  271. /**
  272. * Gets a (previously set) query parameter of the query being constructed.
  273. *
  274. * @param mixed $key The key (index or name) of the bound parameter.
  275. *
  276. * @return mixed The value of the bound parameter.
  277. * @since 8.2.0
  278. */
  279. public function getParameter($key);
  280. /**
  281. * Gets all defined query parameter types for the query being constructed indexed by parameter index or name.
  282. *
  283. * @return array The currently defined query parameter types indexed by parameter index or name.
  284. * @since 8.2.0
  285. */
  286. public function getParameterTypes();
  287. /**
  288. * Gets a (previously set) query parameter type of the query being constructed.
  289. *
  290. * @param mixed $key The key (index or name) of the bound parameter type.
  291. *
  292. * @return mixed The value of the bound parameter type.
  293. * @since 8.2.0
  294. */
  295. public function getParameterType($key);
  296. /**
  297. * Sets the position of the first result to retrieve (the "offset").
  298. *
  299. * @param int $firstResult The first result to return.
  300. *
  301. * @return $this This QueryBuilder instance.
  302. * @since 8.2.0
  303. */
  304. public function setFirstResult($firstResult);
  305. /**
  306. * Gets the position of the first result the query object was set to retrieve (the "offset").
  307. * Returns 0 if {@link setFirstResult} was not applied to this QueryBuilder.
  308. *
  309. * @return int The position of the first result.
  310. * @since 8.2.0
  311. */
  312. public function getFirstResult();
  313. /**
  314. * Sets the maximum number of results to retrieve (the "limit").
  315. *
  316. * @param int|null $maxResults The maximum number of results to retrieve.
  317. *
  318. * @return $this This QueryBuilder instance.
  319. * @since 8.2.0
  320. */
  321. public function setMaxResults($maxResults);
  322. /**
  323. * Gets the maximum number of results the query object was set to retrieve (the "limit").
  324. * Returns NULL if {@link setMaxResults} was not applied to this query builder.
  325. *
  326. * @return int|null The maximum number of results.
  327. * @since 8.2.0
  328. */
  329. public function getMaxResults();
  330. /**
  331. * Specifies an item that is to be returned in the query result.
  332. * Replaces any previously specified selections, if any.
  333. *
  334. * <code>
  335. * $qb = $conn->getQueryBuilder()
  336. * ->select('u.id', 'p.id')
  337. * ->from('users', 'u')
  338. * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');
  339. * </code>
  340. *
  341. * @param mixed ...$selects The selection expressions.
  342. *
  343. * @return $this This QueryBuilder instance.
  344. * @since 8.2.0
  345. *
  346. * @psalm-taint-sink sql $selects
  347. */
  348. public function select(...$selects);
  349. /**
  350. * Specifies an item that is to be returned with a different name in the query result.
  351. *
  352. * <code>
  353. * $qb = $conn->getQueryBuilder()
  354. * ->selectAlias('u.id', 'user_id')
  355. * ->from('users', 'u')
  356. * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');
  357. * </code>
  358. *
  359. * @param mixed $select The selection expressions.
  360. * @param string $alias The column alias used in the constructed query.
  361. *
  362. * @return $this This QueryBuilder instance.
  363. * @since 8.2.1
  364. *
  365. * @psalm-taint-sink sql $select
  366. * @psalm-taint-sink sql $alias
  367. */
  368. public function selectAlias($select, $alias);
  369. /**
  370. * Specifies an item that is to be returned uniquely in the query result.
  371. *
  372. * <code>
  373. * $qb = $conn->getQueryBuilder()
  374. * ->selectDistinct('type')
  375. * ->from('users');
  376. * </code>
  377. *
  378. * @param mixed $select The selection expressions.
  379. *
  380. * @return $this This QueryBuilder instance.
  381. * @since 9.0.0
  382. *
  383. * @psalm-taint-sink sql $select
  384. */
  385. public function selectDistinct($select);
  386. /**
  387. * Adds an item that is to be returned in the query result.
  388. *
  389. * <code>
  390. * $qb = $conn->getQueryBuilder()
  391. * ->select('u.id')
  392. * ->addSelect('p.id')
  393. * ->from('users', 'u')
  394. * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id');
  395. * </code>
  396. *
  397. * @param mixed ...$select The selection expression.
  398. *
  399. * @return $this This QueryBuilder instance.
  400. * @since 8.2.0
  401. *
  402. * @psalm-taint-sink sql $select
  403. */
  404. public function addSelect(...$select);
  405. /**
  406. * Turns the query being built into a bulk delete query that ranges over
  407. * a certain table.
  408. *
  409. * <code>
  410. * $qb = $conn->getQueryBuilder()
  411. * ->delete('users')
  412. * ->where('id = :user_id');
  413. * ->setParameter(':user_id', 1);
  414. * </code>
  415. *
  416. * @param string $delete The table whose rows are subject to the deletion.
  417. * @param string $alias The table alias used in the constructed query.
  418. *
  419. * @return $this This QueryBuilder instance.
  420. * @since 8.2.0
  421. * @since 30.0.0 Alias is deprecated and will no longer be used with the next Doctrine/DBAL update
  422. *
  423. * @psalm-taint-sink sql $delete
  424. */
  425. public function delete($delete = null, $alias = null);
  426. /**
  427. * Turns the query being built into a bulk update query that ranges over
  428. * a certain table
  429. *
  430. * <code>
  431. * $qb = $conn->getQueryBuilder()
  432. * ->update('users')
  433. * ->set('email', ':email')
  434. * ->where('id = :user_id');
  435. * ->setParameter(':user_id', 1);
  436. * </code>
  437. *
  438. * @param string $update The table whose rows are subject to the update.
  439. * @param string $alias The table alias used in the constructed query.
  440. *
  441. * @return $this This QueryBuilder instance.
  442. * @since 8.2.0
  443. * @since 30.0.0 Alias is deprecated and will no longer be used with the next Doctrine/DBAL update
  444. *
  445. * @psalm-taint-sink sql $update
  446. */
  447. public function update($update = null, $alias = null);
  448. /**
  449. * Turns the query being built into an insert query that inserts into
  450. * a certain table
  451. *
  452. * <code>
  453. * $qb = $conn->getQueryBuilder()
  454. * ->insert('users')
  455. * ->values(
  456. * array(
  457. * 'name' => '?',
  458. * 'password' => '?'
  459. * )
  460. * );
  461. * </code>
  462. *
  463. * @param string $insert The table into which the rows should be inserted.
  464. *
  465. * @return $this This QueryBuilder instance.
  466. * @since 8.2.0
  467. *
  468. * @psalm-taint-sink sql $insert
  469. */
  470. public function insert($insert = null);
  471. /**
  472. * Creates and adds a query root corresponding to the table identified by the
  473. * given alias, forming a cartesian product with any existing query roots.
  474. *
  475. * <code>
  476. * $qb = $conn->getQueryBuilder()
  477. * ->select('u.id')
  478. * ->from('users', 'u')
  479. * </code>
  480. *
  481. * @param string|IQueryFunction $from The table.
  482. * @param string|null $alias The alias of the table.
  483. *
  484. * @return $this This QueryBuilder instance.
  485. * @since 8.2.0
  486. *
  487. * @psalm-taint-sink sql $from
  488. */
  489. public function from($from, $alias = null);
  490. /**
  491. * Creates and adds a join to the query.
  492. *
  493. * <code>
  494. * $qb = $conn->getQueryBuilder()
  495. * ->select('u.name')
  496. * ->from('users', 'u')
  497. * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  498. * </code>
  499. *
  500. * @param string $fromAlias The alias that points to a from clause.
  501. * @param string $join The table name to join.
  502. * @param string $alias The alias of the join table.
  503. * @param string|ICompositeExpression|null $condition The condition for the join.
  504. *
  505. * @return $this This QueryBuilder instance.
  506. * @since 8.2.0
  507. *
  508. * @psalm-taint-sink sql $fromAlias
  509. * @psalm-taint-sink sql $join
  510. * @psalm-taint-sink sql $alias
  511. * @psalm-taint-sink sql $condition
  512. */
  513. public function join($fromAlias, $join, $alias, $condition = null);
  514. /**
  515. * Creates and adds a join to the query.
  516. *
  517. * <code>
  518. * $qb = $conn->getQueryBuilder()
  519. * ->select('u.name')
  520. * ->from('users', 'u')
  521. * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  522. * </code>
  523. *
  524. * @param string $fromAlias The alias that points to a from clause.
  525. * @param string $join The table name to join.
  526. * @param string $alias The alias of the join table.
  527. * @param string|ICompositeExpression|null $condition The condition for the join.
  528. *
  529. * @return $this This QueryBuilder instance.
  530. * @since 8.2.0
  531. *
  532. * @psalm-taint-sink sql $fromAlias
  533. * @psalm-taint-sink sql $join
  534. * @psalm-taint-sink sql $alias
  535. * @psalm-taint-sink sql $condition
  536. */
  537. public function innerJoin($fromAlias, $join, $alias, $condition = null);
  538. /**
  539. * Creates and adds a left join to the query.
  540. *
  541. * <code>
  542. * $qb = $conn->getQueryBuilder()
  543. * ->select('u.name')
  544. * ->from('users', 'u')
  545. * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  546. * </code>
  547. *
  548. * @param string $fromAlias The alias that points to a from clause.
  549. * @param string|IQueryFunction $join The table name to join.
  550. * @param string $alias The alias of the join table.
  551. * @param string|ICompositeExpression|null $condition The condition for the join.
  552. *
  553. * @return $this This QueryBuilder instance.
  554. * @since 8.2.0
  555. * @since 30.0.0 Allow passing IQueryFunction as parameter for `$join` to allow join with a sub-query.
  556. *
  557. * @psalm-taint-sink sql $fromAlias
  558. * @psalm-taint-sink sql $join
  559. * @psalm-taint-sink sql $alias
  560. * @psalm-taint-sink sql $condition
  561. */
  562. public function leftJoin($fromAlias, $join, $alias, $condition = null);
  563. /**
  564. * Creates and adds a right join to the query.
  565. *
  566. * <code>
  567. * $qb = $conn->getQueryBuilder()
  568. * ->select('u.name')
  569. * ->from('users', 'u')
  570. * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  571. * </code>
  572. *
  573. * @param string $fromAlias The alias that points to a from clause.
  574. * @param string $join The table name to join.
  575. * @param string $alias The alias of the join table.
  576. * @param string|ICompositeExpression|null $condition The condition for the join.
  577. *
  578. * @return $this This QueryBuilder instance.
  579. * @since 8.2.0
  580. *
  581. * @psalm-taint-sink sql $fromAlias
  582. * @psalm-taint-sink sql $join
  583. * @psalm-taint-sink sql $alias
  584. * @psalm-taint-sink sql $condition
  585. */
  586. public function rightJoin($fromAlias, $join, $alias, $condition = null);
  587. /**
  588. * Sets a new value for a column in a bulk update query.
  589. *
  590. * <code>
  591. * $qb = $conn->getQueryBuilder()
  592. * ->update('users', 'u')
  593. * ->set('u.password', md5('password'))
  594. * ->where('u.id = ?');
  595. * </code>
  596. *
  597. * @param string $key The column to set.
  598. * @param ILiteral|IParameter|IQueryFunction|string $value The value, expression, placeholder, etc.
  599. *
  600. * @return $this This QueryBuilder instance.
  601. * @since 8.2.0
  602. *
  603. * @psalm-taint-sink sql $key
  604. * @psalm-taint-sink sql $value
  605. */
  606. public function set($key, $value);
  607. /**
  608. * Specifies one or more restrictions to the query result.
  609. * Replaces any previously specified restrictions, if any.
  610. *
  611. * <code>
  612. * $qb = $conn->getQueryBuilder()
  613. * ->select('u.name')
  614. * ->from('users', 'u')
  615. * ->where('u.id = ?');
  616. *
  617. * // You can optionally programmatically build and/or expressions
  618. * $qb = $conn->getQueryBuilder();
  619. *
  620. * $or = $qb->expr()->orx(
  621. * $qb->expr()->eq('u.id', 1),
  622. * $qb->expr()->eq('u.id', 2),
  623. * );
  624. *
  625. * $qb->update('users', 'u')
  626. * ->set('u.password', md5('password'))
  627. * ->where($or);
  628. * </code>
  629. *
  630. * @param mixed $predicates The restriction predicates.
  631. *
  632. * @return $this This QueryBuilder instance.
  633. * @since 8.2.0
  634. *
  635. * @psalm-taint-sink sql $predicates
  636. */
  637. public function where(...$predicates);
  638. /**
  639. * Adds one or more restrictions to the query results, forming a logical
  640. * conjunction with any previously specified restrictions.
  641. *
  642. * <code>
  643. * $qb = $conn->getQueryBuilder()
  644. * ->select('u')
  645. * ->from('users', 'u')
  646. * ->where('u.username LIKE ?')
  647. * ->andWhere('u.is_active = 1');
  648. * </code>
  649. *
  650. * @param mixed ...$where The query restrictions.
  651. *
  652. * @return $this This QueryBuilder instance.
  653. *
  654. * @see where()
  655. * @since 8.2.0
  656. *
  657. * @psalm-taint-sink sql $where
  658. */
  659. public function andWhere(...$where);
  660. /**
  661. * Adds one or more restrictions to the query results, forming a logical
  662. * disjunction with any previously specified restrictions.
  663. *
  664. * <code>
  665. * $qb = $conn->getQueryBuilder()
  666. * ->select('u.name')
  667. * ->from('users', 'u')
  668. * ->where('u.id = 1')
  669. * ->orWhere('u.id = 2');
  670. * </code>
  671. *
  672. * @param mixed ...$where The WHERE statement.
  673. *
  674. * @return $this This QueryBuilder instance.
  675. *
  676. * @see where()
  677. * @since 8.2.0
  678. *
  679. * @psalm-taint-sink sql $where
  680. */
  681. public function orWhere(...$where);
  682. /**
  683. * Specifies a grouping over the results of the query.
  684. * Replaces any previously specified groupings, if any.
  685. *
  686. * <code>
  687. * $qb = $conn->getQueryBuilder()
  688. * ->select('u.name')
  689. * ->from('users', 'u')
  690. * ->groupBy('u.id');
  691. * </code>
  692. *
  693. * @param mixed ...$groupBys The grouping expression.
  694. *
  695. * @return $this This QueryBuilder instance.
  696. * @since 8.2.0
  697. *
  698. * @psalm-taint-sink sql $groupBys
  699. */
  700. public function groupBy(...$groupBys);
  701. /**
  702. * Adds a grouping expression to the query.
  703. *
  704. * <code>
  705. * $qb = $conn->getQueryBuilder()
  706. * ->select('u.name')
  707. * ->from('users', 'u')
  708. * ->groupBy('u.lastLogin');
  709. * ->addGroupBy('u.createdAt')
  710. * </code>
  711. *
  712. * @param mixed ...$groupBy The grouping expression.
  713. *
  714. * @return $this This QueryBuilder instance.
  715. * @since 8.2.0
  716. *
  717. * @psalm-taint-sink sql $groupby
  718. */
  719. public function addGroupBy(...$groupBy);
  720. /**
  721. * Sets a value for a column in an insert query.
  722. *
  723. * <code>
  724. * $qb = $conn->getQueryBuilder()
  725. * ->insert('users')
  726. * ->values(
  727. * array(
  728. * 'name' => '?'
  729. * )
  730. * )
  731. * ->setValue('password', '?');
  732. * </code>
  733. *
  734. * @param string $column The column into which the value should be inserted.
  735. * @param IParameter|string $value The value that should be inserted into the column.
  736. *
  737. * @return $this This QueryBuilder instance.
  738. * @since 8.2.0
  739. *
  740. * @psalm-taint-sink sql $column
  741. * @psalm-taint-sink sql $value
  742. */
  743. public function setValue($column, $value);
  744. /**
  745. * Specifies values for an insert query indexed by column names.
  746. * Replaces any previous values, if any.
  747. *
  748. * <code>
  749. * $qb = $conn->getQueryBuilder()
  750. * ->insert('users')
  751. * ->values(
  752. * array(
  753. * 'name' => '?',
  754. * 'password' => '?'
  755. * )
  756. * );
  757. * </code>
  758. *
  759. * @param array $values The values to specify for the insert query indexed by column names.
  760. *
  761. * @return $this This QueryBuilder instance.
  762. * @since 8.2.0
  763. *
  764. * @psalm-taint-sink sql $values
  765. */
  766. public function values(array $values);
  767. /**
  768. * Specifies a restriction over the groups of the query.
  769. * Replaces any previous having restrictions, if any.
  770. *
  771. * @param mixed ...$having The restriction over the groups.
  772. *
  773. * @return $this This QueryBuilder instance.
  774. * @since 8.2.0
  775. *
  776. * @psalm-taint-sink sql $having
  777. */
  778. public function having(...$having);
  779. /**
  780. * Adds a restriction over the groups of the query, forming a logical
  781. * conjunction with any existing having restrictions.
  782. *
  783. * @param mixed ...$having The restriction to append.
  784. *
  785. * @return $this This QueryBuilder instance.
  786. * @since 8.2.0
  787. *
  788. * @psalm-taint-sink sql $andHaving
  789. */
  790. public function andHaving(...$having);
  791. /**
  792. * Adds a restriction over the groups of the query, forming a logical
  793. * disjunction with any existing having restrictions.
  794. *
  795. * @param mixed ...$having The restriction to add.
  796. *
  797. * @return $this This QueryBuilder instance.
  798. * @since 8.2.0
  799. *
  800. * @psalm-taint-sink sql $having
  801. */
  802. public function orHaving(...$having);
  803. /**
  804. * Specifies an ordering for the query results.
  805. * Replaces any previously specified orderings, if any.
  806. *
  807. * @param string|IQueryFunction|ILiteral|IParameter $sort The ordering expression.
  808. * @param string $order The ordering direction.
  809. *
  810. * @return $this This QueryBuilder instance.
  811. * @since 8.2.0
  812. *
  813. * @psalm-taint-sink sql $sort
  814. * @psalm-taint-sink sql $order
  815. */
  816. public function orderBy($sort, $order = null);
  817. /**
  818. * Adds an ordering to the query results.
  819. *
  820. * @param string|ILiteral|IParameter|IQueryFunction $sort The ordering expression.
  821. * @param string $order The ordering direction.
  822. *
  823. * @return $this This QueryBuilder instance.
  824. * @since 8.2.0
  825. *
  826. * @psalm-taint-sink sql $sort
  827. * @psalm-taint-sink sql $order
  828. */
  829. public function addOrderBy($sort, $order = null);
  830. /**
  831. * Gets a query part by its name.
  832. *
  833. * @param string $queryPartName
  834. *
  835. * @return mixed
  836. * @since 8.2.0
  837. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  838. * and we can not fix this in our wrapper. Please track the details you need, outside the object.
  839. */
  840. public function getQueryPart($queryPartName);
  841. /**
  842. * Gets all query parts.
  843. *
  844. * @return array
  845. * @since 8.2.0
  846. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  847. * and we can not fix this in our wrapper. Please track the details you need, outside the object.
  848. */
  849. public function getQueryParts();
  850. /**
  851. * Resets SQL parts.
  852. *
  853. * @param array|null $queryPartNames
  854. *
  855. * @return $this This QueryBuilder instance.
  856. * @since 8.2.0
  857. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  858. * and we can not fix this in our wrapper. Please create a new IQueryBuilder instead.
  859. */
  860. public function resetQueryParts($queryPartNames = null);
  861. /**
  862. * Resets a single SQL part.
  863. *
  864. * @param string $queryPartName
  865. *
  866. * @return $this This QueryBuilder instance.
  867. * @since 8.2.0
  868. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  869. * and we can not fix this in our wrapper. Please create a new IQueryBuilder instead.
  870. */
  871. public function resetQueryPart($queryPartName);
  872. /**
  873. * Creates a new named parameter and bind the value $value to it.
  874. *
  875. * This method provides a shortcut for PDOStatement::bindValue
  876. * when using prepared statements.
  877. *
  878. * The parameter $value specifies the value that you want to bind. If
  879. * $placeholder is not provided bindValue() will automatically create a
  880. * placeholder for you. An automatic placeholder will be of the name
  881. * ':dcValue1', ':dcValue2' etc.
  882. *
  883. * For more information see {@link https://www.php.net/pdostatement-bindparam}
  884. *
  885. * Example:
  886. * <code>
  887. * $value = 2;
  888. * $q->eq( 'id', $q->bindValue( $value ) );
  889. * $stmt = $q->executeQuery(); // executed with 'id = 2'
  890. * </code>
  891. *
  892. * @license New BSD License
  893. * @link http://www.zetacomponents.org
  894. *
  895. * @param mixed $value
  896. * @param self::PARAM_* $type
  897. * @param string $placeHolder The name to bind with. The string must start with a colon ':'.
  898. *
  899. * @return IParameter
  900. * @since 8.2.0
  901. *
  902. * @psalm-taint-escape sql
  903. */
  904. public function createNamedParameter($value, $type = self::PARAM_STR, $placeHolder = null);
  905. /**
  906. * Creates a new positional parameter and bind the given value to it.
  907. *
  908. * Attention: If you are using positional parameters with the query builder you have
  909. * to be very careful to bind all parameters in the order they appear in the SQL
  910. * statement , otherwise they get bound in the wrong order which can lead to serious
  911. * bugs in your code.
  912. *
  913. * Example:
  914. * <code>
  915. * $qb = $conn->getQueryBuilder();
  916. * $qb->select('u.*')
  917. * ->from('users', 'u')
  918. * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR))
  919. * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR))
  920. * </code>
  921. *
  922. * @param mixed $value
  923. * @param self::PARAM_* $type
  924. *
  925. * @return IParameter
  926. * @since 8.2.0
  927. *
  928. * @psalm-taint-escape sql
  929. */
  930. public function createPositionalParameter($value, $type = self::PARAM_STR);
  931. /**
  932. * Creates a new parameter
  933. *
  934. * Example:
  935. * <code>
  936. * $qb = $conn->getQueryBuilder();
  937. * $qb->select('u.*')
  938. * ->from('users', 'u')
  939. * ->where('u.username = ' . $qb->createParameter('name'))
  940. * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR))
  941. * </code>
  942. *
  943. * @param string $name
  944. *
  945. * @return IParameter
  946. * @since 8.2.0
  947. *
  948. * @psalm-taint-escape sql
  949. */
  950. public function createParameter($name);
  951. /**
  952. * Creates a new function
  953. *
  954. * Attention: Column names inside the call have to be quoted before hand
  955. *
  956. * Example:
  957. * <code>
  958. * $qb = $conn->getQueryBuilder();
  959. * $qb->select($qb->createFunction('COUNT(*)'))
  960. * ->from('users', 'u')
  961. * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u
  962. * </code>
  963. * <code>
  964. * $qb = $conn->getQueryBuilder();
  965. * $qb->select($qb->createFunction('COUNT(`column`)'))
  966. * ->from('users', 'u')
  967. * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u
  968. * </code>
  969. *
  970. * @param string $call
  971. *
  972. * @return IQueryFunction
  973. * @since 8.2.0
  974. *
  975. * @psalm-taint-sink sql $call
  976. */
  977. public function createFunction($call);
  978. /**
  979. * Used to get the id of the last inserted element
  980. * @return int
  981. * @throws \BadMethodCallException When being called before an insert query has been run.
  982. * @since 9.0.0
  983. */
  984. public function getLastInsertId(): int;
  985. /**
  986. * Returns the table name quoted and with database prefix as needed by the implementation.
  987. * If a query function is passed the function is casted to string,
  988. * this allows passing functions as sub-queries for join expression.
  989. *
  990. * @param string|IQueryFunction $table
  991. * @return string
  992. * @since 9.0.0
  993. * @since 24.0.0 accepts IQueryFunction as parameter
  994. */
  995. public function getTableName($table);
  996. /**
  997. * Returns the table name with database prefix as needed by the implementation
  998. *
  999. * @param string $table
  1000. * @return string
  1001. * @since 30.0.0
  1002. */
  1003. public function prefixTableName(string $table): string;
  1004. /**
  1005. * Returns the column name quoted and with table alias prefix as needed by the implementation
  1006. *
  1007. * @param string $column
  1008. * @param string $tableAlias
  1009. * @return string
  1010. * @since 9.0.0
  1011. */
  1012. public function getColumnName($column, $tableAlias = '');
  1013. /**
  1014. * Provide a hint for the shard key for queries where this can't be detected otherwise
  1015. *
  1016. * @param string $column
  1017. * @param mixed $value
  1018. * @return $this
  1019. * @since 30.0.0
  1020. */
  1021. public function hintShardKey(string $column, mixed $value, bool $overwrite = false): self;
  1022. /**
  1023. * Set the query to run across all shards if sharding is enabled.
  1024. *
  1025. * @return $this
  1026. * @since 30.0.0
  1027. */
  1028. public function runAcrossAllShards(): self;
  1029. /**
  1030. * Get a list of column names that are expected in the query output
  1031. *
  1032. * @return array
  1033. * @since 30.0.0
  1034. */
  1035. public function getOutputColumns(): array;
  1036. }