IQueryBuilder.php 27 KB

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