1
0

IQueryBuilder.php 27 KB

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