IQueryBuilder.php 28 KB

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