1
0

ExtendedQueryBuilder.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl>
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\DB\QueryBuilder;
  8. use OC\DB\Exceptions\DbalException;
  9. use OCP\DB\IResult;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IDBConnection;
  12. /**
  13. * Base class for creating classes that extend the builtin query builder
  14. */
  15. abstract class ExtendedQueryBuilder implements IQueryBuilder {
  16. public function __construct(
  17. protected IQueryBuilder $builder,
  18. ) {
  19. }
  20. public function automaticTablePrefix($enabled) {
  21. $this->builder->automaticTablePrefix($enabled);
  22. return $this;
  23. }
  24. public function expr() {
  25. return $this->builder->expr();
  26. }
  27. public function func() {
  28. return $this->builder->func();
  29. }
  30. public function getType() {
  31. return $this->builder->getType();
  32. }
  33. public function getConnection() {
  34. return $this->builder->getConnection();
  35. }
  36. public function getState() {
  37. return $this->builder->getState();
  38. }
  39. public function execute(?IDBConnection $connection = null) {
  40. try {
  41. if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::SELECT) {
  42. return $this->executeQuery($connection);
  43. } else {
  44. return $this->executeStatement($connection);
  45. }
  46. } catch (DBALException $e) {
  47. // `IQueryBuilder->execute` never wrapped the exception, but `executeQuery` and `executeStatement` do
  48. /** @var \Doctrine\DBAL\Exception $previous */
  49. $previous = $e->getPrevious();
  50. throw $previous;
  51. }
  52. }
  53. public function getSQL() {
  54. return $this->builder->getSQL();
  55. }
  56. public function setParameter($key, $value, $type = null) {
  57. $this->builder->setParameter($key, $value, $type);
  58. return $this;
  59. }
  60. public function setParameters(array $params, array $types = []) {
  61. $this->builder->setParameters($params, $types);
  62. return $this;
  63. }
  64. public function getParameters() {
  65. return $this->builder->getParameters();
  66. }
  67. public function getParameter($key) {
  68. return $this->builder->getParameter($key);
  69. }
  70. public function getParameterTypes() {
  71. return $this->builder->getParameterTypes();
  72. }
  73. public function getParameterType($key) {
  74. return $this->builder->getParameterType($key);
  75. }
  76. public function setFirstResult($firstResult) {
  77. $this->builder->setFirstResult($firstResult);
  78. return $this;
  79. }
  80. public function getFirstResult() {
  81. return $this->builder->getFirstResult();
  82. }
  83. public function setMaxResults($maxResults) {
  84. $this->builder->setMaxResults($maxResults);
  85. return $this;
  86. }
  87. public function getMaxResults() {
  88. return $this->builder->getMaxResults();
  89. }
  90. public function select(...$selects) {
  91. $this->builder->select(...$selects);
  92. return $this;
  93. }
  94. public function selectAlias($select, $alias) {
  95. $this->builder->selectAlias($select, $alias);
  96. return $this;
  97. }
  98. public function selectDistinct($select) {
  99. $this->builder->selectDistinct($select);
  100. return $this;
  101. }
  102. public function addSelect(...$select) {
  103. $this->builder->addSelect(...$select);
  104. return $this;
  105. }
  106. public function delete($delete = null, $alias = null) {
  107. $this->builder->delete($delete, $alias);
  108. return $this;
  109. }
  110. public function update($update = null, $alias = null) {
  111. $this->builder->update($update, $alias);
  112. return $this;
  113. }
  114. public function insert($insert = null) {
  115. $this->builder->insert($insert);
  116. return $this;
  117. }
  118. public function from($from, $alias = null) {
  119. $this->builder->from($from, $alias);
  120. return $this;
  121. }
  122. public function join($fromAlias, $join, $alias, $condition = null) {
  123. $this->builder->join($fromAlias, $join, $alias, $condition);
  124. return $this;
  125. }
  126. public function innerJoin($fromAlias, $join, $alias, $condition = null) {
  127. $this->builder->innerJoin($fromAlias, $join, $alias, $condition);
  128. return $this;
  129. }
  130. public function leftJoin($fromAlias, $join, $alias, $condition = null) {
  131. $this->builder->leftJoin($fromAlias, $join, $alias, $condition);
  132. return $this;
  133. }
  134. public function rightJoin($fromAlias, $join, $alias, $condition = null) {
  135. $this->builder->rightJoin($fromAlias, $join, $alias, $condition);
  136. return $this;
  137. }
  138. public function set($key, $value) {
  139. $this->builder->set($key, $value);
  140. return $this;
  141. }
  142. public function where(...$predicates) {
  143. $this->builder->where(...$predicates);
  144. return $this;
  145. }
  146. public function andWhere(...$where) {
  147. $this->builder->andWhere(...$where);
  148. return $this;
  149. }
  150. public function orWhere(...$where) {
  151. $this->builder->orWhere(...$where);
  152. return $this;
  153. }
  154. public function groupBy(...$groupBys) {
  155. $this->builder->groupBy(...$groupBys);
  156. return $this;
  157. }
  158. public function addGroupBy(...$groupBy) {
  159. $this->builder->addGroupBy(...$groupBy);
  160. return $this;
  161. }
  162. public function setValue($column, $value) {
  163. $this->builder->setValue($column, $value);
  164. return $this;
  165. }
  166. public function values(array $values) {
  167. $this->builder->values($values);
  168. return $this;
  169. }
  170. public function having(...$having) {
  171. $this->builder->having(...$having);
  172. return $this;
  173. }
  174. public function andHaving(...$having) {
  175. $this->builder->andHaving(...$having);
  176. return $this;
  177. }
  178. public function orHaving(...$having) {
  179. $this->builder->orHaving(...$having);
  180. return $this;
  181. }
  182. public function orderBy($sort, $order = null) {
  183. $this->builder->orderBy($sort, $order);
  184. return $this;
  185. }
  186. public function addOrderBy($sort, $order = null) {
  187. $this->builder->addOrderBy($sort, $order);
  188. return $this;
  189. }
  190. public function getQueryPart($queryPartName) {
  191. return $this->builder->getQueryPart($queryPartName);
  192. }
  193. public function getQueryParts() {
  194. return $this->builder->getQueryParts();
  195. }
  196. public function resetQueryParts($queryPartNames = null) {
  197. $this->builder->resetQueryParts($queryPartNames);
  198. return $this;
  199. }
  200. public function resetQueryPart($queryPartName) {
  201. $this->builder->resetQueryPart($queryPartName);
  202. return $this;
  203. }
  204. public function createNamedParameter($value, $type = self::PARAM_STR, $placeHolder = null) {
  205. return $this->builder->createNamedParameter($value, $type, $placeHolder);
  206. }
  207. public function createPositionalParameter($value, $type = self::PARAM_STR) {
  208. return $this->builder->createPositionalParameter($value, $type);
  209. }
  210. public function createParameter($name) {
  211. return $this->builder->createParameter($name);
  212. }
  213. public function createFunction($call) {
  214. return $this->builder->createFunction($call);
  215. }
  216. public function getLastInsertId(): int {
  217. return $this->builder->getLastInsertId();
  218. }
  219. public function getTableName($table) {
  220. return $this->builder->getTableName($table);
  221. }
  222. public function getColumnName($column, $tableAlias = '') {
  223. return $this->builder->getColumnName($column, $tableAlias);
  224. }
  225. public function executeQuery(?IDBConnection $connection = null): IResult {
  226. return $this->builder->executeQuery($connection);
  227. }
  228. public function executeStatement(?IDBConnection $connection = null): int {
  229. return $this->builder->executeStatement($connection);
  230. }
  231. public function hintShardKey(string $column, mixed $value, bool $overwrite = false): self {
  232. $this->builder->hintShardKey($column, $value, $overwrite);
  233. return $this;
  234. }
  235. public function runAcrossAllShards(): self {
  236. $this->builder->runAcrossAllShards();
  237. return $this;
  238. }
  239. public function getOutputColumns(): array {
  240. return $this->builder->getOutputColumns();
  241. }
  242. public function prefixTableName(string $table): string {
  243. return $this->builder->prefixTableName($table);
  244. }
  245. }