IQueryBuilder.php 26 KB

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