IQueryBuilder.php 27 KB

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