1
0

QueryBuilder.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\DB\QueryBuilder;
  8. use Doctrine\DBAL\Query\QueryException;
  9. use OC\DB\ConnectionAdapter;
  10. use OC\DB\Exceptions\DbalException;
  11. use OC\DB\QueryBuilder\ExpressionBuilder\MySqlExpressionBuilder;
  12. use OC\DB\QueryBuilder\ExpressionBuilder\OCIExpressionBuilder;
  13. use OC\DB\QueryBuilder\ExpressionBuilder\PgSqlExpressionBuilder;
  14. use OC\DB\QueryBuilder\ExpressionBuilder\SqliteExpressionBuilder;
  15. use OC\DB\QueryBuilder\FunctionBuilder\FunctionBuilder;
  16. use OC\DB\QueryBuilder\FunctionBuilder\OCIFunctionBuilder;
  17. use OC\DB\QueryBuilder\FunctionBuilder\PgSqlFunctionBuilder;
  18. use OC\DB\QueryBuilder\FunctionBuilder\SqliteFunctionBuilder;
  19. use OC\SystemConfig;
  20. use OCP\DB\IResult;
  21. use OCP\DB\QueryBuilder\ICompositeExpression;
  22. use OCP\DB\QueryBuilder\ILiteral;
  23. use OCP\DB\QueryBuilder\IParameter;
  24. use OCP\DB\QueryBuilder\IQueryBuilder;
  25. use OCP\DB\QueryBuilder\IQueryFunction;
  26. use OCP\IDBConnection;
  27. use Psr\Log\LoggerInterface;
  28. class QueryBuilder implements IQueryBuilder {
  29. /** @var ConnectionAdapter */
  30. private $connection;
  31. /** @var SystemConfig */
  32. private $systemConfig;
  33. private LoggerInterface $logger;
  34. /** @var \Doctrine\DBAL\Query\QueryBuilder */
  35. private $queryBuilder;
  36. /** @var QuoteHelper */
  37. private $helper;
  38. /** @var bool */
  39. private $automaticTablePrefix = true;
  40. private bool $nonEmptyWhere = false;
  41. /** @var string */
  42. protected $lastInsertedTable;
  43. private array $selectedColumns = [];
  44. /**
  45. * Initializes a new QueryBuilder.
  46. *
  47. * @param ConnectionAdapter $connection
  48. * @param SystemConfig $systemConfig
  49. */
  50. public function __construct(ConnectionAdapter $connection, SystemConfig $systemConfig, LoggerInterface $logger) {
  51. $this->connection = $connection;
  52. $this->systemConfig = $systemConfig;
  53. $this->logger = $logger;
  54. $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection->getInner());
  55. $this->helper = new QuoteHelper();
  56. }
  57. /**
  58. * Enable/disable automatic prefixing of table names with the oc_ prefix
  59. *
  60. * @param bool $enabled If set to true table names will be prefixed with the
  61. * owncloud database prefix automatically.
  62. * @since 8.2.0
  63. */
  64. public function automaticTablePrefix($enabled) {
  65. $this->automaticTablePrefix = (bool) $enabled;
  66. }
  67. /**
  68. * Gets an ExpressionBuilder used for object-oriented construction of query expressions.
  69. * This producer method is intended for convenient inline usage. Example:
  70. *
  71. * <code>
  72. * $qb = $conn->getQueryBuilder()
  73. * ->select('u')
  74. * ->from('users', 'u')
  75. * ->where($qb->expr()->eq('u.id', 1));
  76. * </code>
  77. *
  78. * For more complex expression construction, consider storing the expression
  79. * builder object in a local variable.
  80. *
  81. * @return \OCP\DB\QueryBuilder\IExpressionBuilder
  82. */
  83. public function expr() {
  84. return match($this->connection->getDatabaseProvider()) {
  85. IDBConnection::PLATFORM_ORACLE => new OCIExpressionBuilder($this->connection, $this, $this->logger),
  86. IDBConnection::PLATFORM_POSTGRES => new PgSqlExpressionBuilder($this->connection, $this, $this->logger),
  87. IDBConnection::PLATFORM_MYSQL => new MySqlExpressionBuilder($this->connection, $this, $this->logger),
  88. IDBConnection::PLATFORM_SQLITE => new SqliteExpressionBuilder($this->connection, $this, $this->logger),
  89. };
  90. }
  91. /**
  92. * Gets an FunctionBuilder used for object-oriented construction of query functions.
  93. * This producer method is intended for convenient inline usage. Example:
  94. *
  95. * <code>
  96. * $qb = $conn->getQueryBuilder()
  97. * ->select('u')
  98. * ->from('users', 'u')
  99. * ->where($qb->fun()->md5('u.id'));
  100. * </code>
  101. *
  102. * For more complex function construction, consider storing the function
  103. * builder object in a local variable.
  104. *
  105. * @return \OCP\DB\QueryBuilder\IFunctionBuilder
  106. */
  107. public function func() {
  108. return match($this->connection->getDatabaseProvider()) {
  109. IDBConnection::PLATFORM_ORACLE => new OCIFunctionBuilder($this->connection, $this, $this->helper),
  110. IDBConnection::PLATFORM_POSTGRES => new PgSqlFunctionBuilder($this->connection, $this, $this->helper),
  111. IDBConnection::PLATFORM_MYSQL => new FunctionBuilder($this->connection, $this, $this->helper),
  112. IDBConnection::PLATFORM_SQLITE => new SqliteFunctionBuilder($this->connection, $this, $this->helper),
  113. };
  114. }
  115. /**
  116. * Gets the type of the currently built query.
  117. *
  118. * @return integer
  119. */
  120. public function getType() {
  121. return $this->queryBuilder->getType();
  122. }
  123. /**
  124. * Gets the associated DBAL Connection for this query builder.
  125. *
  126. * @return \OCP\IDBConnection
  127. */
  128. public function getConnection() {
  129. return $this->connection;
  130. }
  131. /**
  132. * Gets the state of this query builder instance.
  133. *
  134. * @return int Always returns 0 which is former `QueryBuilder::STATE_DIRTY`
  135. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  136. * and we can not fix this in our wrapper.
  137. */
  138. public function getState() {
  139. $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]);
  140. return $this->queryBuilder->getState();
  141. }
  142. private function prepareForExecute() {
  143. if ($this->systemConfig->getValue('log_query', false)) {
  144. try {
  145. $params = [];
  146. foreach ($this->getParameters() as $placeholder => $value) {
  147. if ($value instanceof \DateTime) {
  148. $params[] = $placeholder . ' => DateTime:\'' . $value->format('c') . '\'';
  149. } elseif (is_array($value)) {
  150. $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')';
  151. } else {
  152. $params[] = $placeholder . ' => \'' . $value . '\'';
  153. }
  154. }
  155. if (empty($params)) {
  156. $this->logger->debug('DB QueryBuilder: \'{query}\'', [
  157. 'query' => $this->getSQL(),
  158. 'app' => 'core',
  159. ]);
  160. } else {
  161. $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [
  162. 'query' => $this->getSQL(),
  163. 'params' => implode(', ', $params),
  164. 'app' => 'core',
  165. ]);
  166. }
  167. } catch (\Error $e) {
  168. // likely an error during conversion of $value to string
  169. $this->logger->error('DB QueryBuilder: error trying to log SQL query', ['exception' => $e]);
  170. }
  171. }
  172. // if (!empty($this->getQueryPart('select'))) {
  173. // $select = $this->getQueryPart('select');
  174. // $hasSelectAll = array_filter($select, static function ($s) {
  175. // return $s === '*';
  176. // });
  177. // $hasSelectSpecific = array_filter($select, static function ($s) {
  178. // return $s !== '*';
  179. // });
  180. // if (empty($hasSelectAll) === empty($hasSelectSpecific)) {
  181. // $exception = new QueryException('Query is selecting * and specific values in the same query. This is not supported in Oracle.');
  182. // $this->logger->error($exception->getMessage(), [
  183. // 'query' => $this->getSQL(),
  184. // 'app' => 'core',
  185. // 'exception' => $exception,
  186. // ]);
  187. // }
  188. // }
  189. $tooLongOutputColumns = [];
  190. foreach ($this->getOutputColumns() as $column) {
  191. if (strlen($column) > 30) {
  192. $tooLongOutputColumns[] = $column;
  193. }
  194. }
  195. if (!empty($tooLongOutputColumns)) {
  196. $exception = new QueryException('More than 30 characters for an output column name are not allowed on Oracle.');
  197. $this->logger->error($exception->getMessage(), [
  198. 'query' => $this->getSQL(),
  199. 'columns' => $tooLongOutputColumns,
  200. 'app' => 'core',
  201. 'exception' => $exception,
  202. ]);
  203. }
  204. $numberOfParameters = 0;
  205. $hasTooLargeArrayParameter = false;
  206. foreach ($this->getParameters() as $parameter) {
  207. if (is_array($parameter)) {
  208. $count = count($parameter);
  209. $numberOfParameters += $count;
  210. $hasTooLargeArrayParameter = $hasTooLargeArrayParameter || ($count > 1000);
  211. }
  212. }
  213. if ($hasTooLargeArrayParameter) {
  214. $exception = new QueryException('More than 1000 expressions in a list are not allowed on Oracle.');
  215. $this->logger->error($exception->getMessage(), [
  216. 'query' => $this->getSQL(),
  217. 'app' => 'core',
  218. 'exception' => $exception,
  219. ]);
  220. }
  221. if ($numberOfParameters > 65535) {
  222. $exception = new QueryException('The number of parameters must not exceed 65535. Restriction by PostgreSQL.');
  223. $this->logger->error($exception->getMessage(), [
  224. 'query' => $this->getSQL(),
  225. 'app' => 'core',
  226. 'exception' => $exception,
  227. ]);
  228. }
  229. }
  230. /**
  231. * Executes this query using the bound parameters and their types.
  232. *
  233. * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate}
  234. * for insert, update and delete statements.
  235. *
  236. * @return IResult|int
  237. */
  238. public function execute(?IDBConnection $connection = null) {
  239. try {
  240. if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::SELECT) {
  241. return $this->executeQuery($connection);
  242. } else {
  243. return $this->executeStatement($connection);
  244. }
  245. } catch (DBALException $e) {
  246. // `IQueryBuilder->execute` never wrapped the exception, but `executeQuery` and `executeStatement` do
  247. /** @var \Doctrine\DBAL\Exception $previous */
  248. $previous = $e->getPrevious();
  249. throw $previous;
  250. }
  251. }
  252. public function executeQuery(?IDBConnection $connection = null): IResult {
  253. if ($this->getType() !== \Doctrine\DBAL\Query\QueryBuilder::SELECT) {
  254. throw new \RuntimeException('Invalid query type, expected SELECT query');
  255. }
  256. $this->prepareForExecute();
  257. if (!$connection) {
  258. $connection = $this->connection;
  259. }
  260. return $connection->executeQuery(
  261. $this->getSQL(),
  262. $this->getParameters(),
  263. $this->getParameterTypes(),
  264. );
  265. }
  266. public function executeStatement(?IDBConnection $connection = null): int {
  267. if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::SELECT) {
  268. throw new \RuntimeException('Invalid query type, expected INSERT, DELETE or UPDATE statement');
  269. }
  270. $this->prepareForExecute();
  271. if (!$connection) {
  272. $connection = $this->connection;
  273. }
  274. return $connection->executeStatement(
  275. $this->getSQL(),
  276. $this->getParameters(),
  277. $this->getParameterTypes(),
  278. );
  279. }
  280. /**
  281. * Gets the complete SQL string formed by the current specifications of this QueryBuilder.
  282. *
  283. * <code>
  284. * $qb = $conn->getQueryBuilder()
  285. * ->select('u')
  286. * ->from('User', 'u')
  287. * echo $qb->getSQL(); // SELECT u FROM User u
  288. * </code>
  289. *
  290. * @return string The SQL query string.
  291. */
  292. public function getSQL() {
  293. return $this->queryBuilder->getSQL();
  294. }
  295. /**
  296. * Sets a query parameter for the query being constructed.
  297. *
  298. * <code>
  299. * $qb = $conn->getQueryBuilder()
  300. * ->select('u')
  301. * ->from('users', 'u')
  302. * ->where('u.id = :user_id')
  303. * ->setParameter(':user_id', 1);
  304. * </code>
  305. *
  306. * @param string|integer $key The parameter position or name.
  307. * @param mixed $value The parameter value.
  308. * @param string|null|int $type One of the IQueryBuilder::PARAM_* constants.
  309. *
  310. * @return $this This QueryBuilder instance.
  311. */
  312. public function setParameter($key, $value, $type = null) {
  313. $this->queryBuilder->setParameter($key, $value, $type);
  314. return $this;
  315. }
  316. /**
  317. * Sets a collection of query parameters for the query being constructed.
  318. *
  319. * <code>
  320. * $qb = $conn->getQueryBuilder()
  321. * ->select('u')
  322. * ->from('users', 'u')
  323. * ->where('u.id = :user_id1 OR u.id = :user_id2')
  324. * ->setParameters(array(
  325. * ':user_id1' => 1,
  326. * ':user_id2' => 2
  327. * ));
  328. * </code>
  329. *
  330. * @param array $params The query parameters to set.
  331. * @param array $types The query parameters types to set.
  332. *
  333. * @return $this This QueryBuilder instance.
  334. */
  335. public function setParameters(array $params, array $types = []) {
  336. $this->queryBuilder->setParameters($params, $types);
  337. return $this;
  338. }
  339. /**
  340. * Gets all defined query parameters for the query being constructed indexed by parameter index or name.
  341. *
  342. * @return array The currently defined query parameters indexed by parameter index or name.
  343. */
  344. public function getParameters() {
  345. return $this->queryBuilder->getParameters();
  346. }
  347. /**
  348. * Gets a (previously set) query parameter of the query being constructed.
  349. *
  350. * @param mixed $key The key (index or name) of the bound parameter.
  351. *
  352. * @return mixed The value of the bound parameter.
  353. */
  354. public function getParameter($key) {
  355. return $this->queryBuilder->getParameter($key);
  356. }
  357. /**
  358. * Gets all defined query parameter types for the query being constructed indexed by parameter index or name.
  359. *
  360. * @return array The currently defined query parameter types indexed by parameter index or name.
  361. */
  362. public function getParameterTypes() {
  363. return $this->queryBuilder->getParameterTypes();
  364. }
  365. /**
  366. * Gets a (previously set) query parameter type of the query being constructed.
  367. *
  368. * @param mixed $key The key (index or name) of the bound parameter type.
  369. *
  370. * @return mixed The value of the bound parameter type.
  371. */
  372. public function getParameterType($key) {
  373. return $this->queryBuilder->getParameterType($key);
  374. }
  375. /**
  376. * Sets the position of the first result to retrieve (the "offset").
  377. *
  378. * @param int $firstResult The first result to return.
  379. *
  380. * @return $this This QueryBuilder instance.
  381. */
  382. public function setFirstResult($firstResult) {
  383. $this->queryBuilder->setFirstResult((int) $firstResult);
  384. return $this;
  385. }
  386. /**
  387. * Gets the position of the first result the query object was set to retrieve (the "offset").
  388. * Returns 0 if {@link setFirstResult} was not applied to this QueryBuilder.
  389. *
  390. * @return int The position of the first result.
  391. */
  392. public function getFirstResult() {
  393. return $this->queryBuilder->getFirstResult();
  394. }
  395. /**
  396. * Sets the maximum number of results to retrieve (the "limit").
  397. *
  398. * NOTE: Setting max results to "0" will cause mixed behaviour. While most
  399. * of the databases will just return an empty result set, Oracle will return
  400. * all entries.
  401. *
  402. * @param int|null $maxResults The maximum number of results to retrieve.
  403. *
  404. * @return $this This QueryBuilder instance.
  405. */
  406. public function setMaxResults($maxResults) {
  407. if ($maxResults === null) {
  408. $this->queryBuilder->setMaxResults($maxResults);
  409. } else {
  410. $this->queryBuilder->setMaxResults((int) $maxResults);
  411. }
  412. return $this;
  413. }
  414. /**
  415. * Gets the maximum number of results the query object was set to retrieve (the "limit").
  416. * Returns NULL if {@link setMaxResults} was not applied to this query builder.
  417. *
  418. * @return int|null The maximum number of results.
  419. */
  420. public function getMaxResults() {
  421. return $this->queryBuilder->getMaxResults();
  422. }
  423. /**
  424. * Specifies an item that is to be returned in the query result.
  425. * Replaces any previously specified selections, if any.
  426. *
  427. * <code>
  428. * $qb = $conn->getQueryBuilder()
  429. * ->select('u.id', 'p.id')
  430. * ->from('users', 'u')
  431. * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');
  432. * </code>
  433. *
  434. * @param mixed ...$selects The selection expressions.
  435. *
  436. * '@return $this This QueryBuilder instance.
  437. */
  438. public function select(...$selects) {
  439. if (count($selects) === 1 && is_array($selects[0])) {
  440. $selects = $selects[0];
  441. }
  442. $this->addOutputColumns($selects);
  443. $this->queryBuilder->select(
  444. $this->helper->quoteColumnNames($selects)
  445. );
  446. return $this;
  447. }
  448. /**
  449. * Specifies an item that is to be returned with a different name in the query result.
  450. *
  451. * <code>
  452. * $qb = $conn->getQueryBuilder()
  453. * ->selectAlias('u.id', 'user_id')
  454. * ->from('users', 'u')
  455. * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');
  456. * </code>
  457. *
  458. * @param mixed $select The selection expressions.
  459. * @param string $alias The column alias used in the constructed query.
  460. *
  461. * @return $this This QueryBuilder instance.
  462. */
  463. public function selectAlias($select, $alias) {
  464. $this->queryBuilder->addSelect(
  465. $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias)
  466. );
  467. $this->addOutputColumns([$alias]);
  468. return $this;
  469. }
  470. /**
  471. * Specifies an item that is to be returned uniquely in the query result.
  472. *
  473. * <code>
  474. * $qb = $conn->getQueryBuilder()
  475. * ->selectDistinct('type')
  476. * ->from('users');
  477. * </code>
  478. *
  479. * @param mixed $select The selection expressions.
  480. *
  481. * @return $this This QueryBuilder instance.
  482. */
  483. public function selectDistinct($select) {
  484. if (!is_array($select)) {
  485. $select = [$select];
  486. }
  487. $this->addOutputColumns($select);
  488. $quotedSelect = $this->helper->quoteColumnNames($select);
  489. $this->queryBuilder->addSelect(
  490. 'DISTINCT ' . implode(', ', $quotedSelect)
  491. );
  492. return $this;
  493. }
  494. /**
  495. * Adds an item that is to be returned in the query result.
  496. *
  497. * <code>
  498. * $qb = $conn->getQueryBuilder()
  499. * ->select('u.id')
  500. * ->addSelect('p.id')
  501. * ->from('users', 'u')
  502. * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id');
  503. * </code>
  504. *
  505. * @param mixed ...$selects The selection expression.
  506. *
  507. * @return $this This QueryBuilder instance.
  508. */
  509. public function addSelect(...$selects) {
  510. if (count($selects) === 1 && is_array($selects[0])) {
  511. $selects = $selects[0];
  512. }
  513. $this->addOutputColumns($selects);
  514. $this->queryBuilder->addSelect(
  515. $this->helper->quoteColumnNames($selects)
  516. );
  517. return $this;
  518. }
  519. private function addOutputColumns(array $columns): void {
  520. foreach ($columns as $column) {
  521. if (is_array($column)) {
  522. $this->addOutputColumns($column);
  523. } elseif (is_string($column) && !str_contains($column, '*')) {
  524. if (str_contains(strtolower($column), ' as ')) {
  525. [, $column] = preg_split('/ as /i', $column);
  526. }
  527. if (str_contains($column, '.')) {
  528. [, $column] = explode('.', $column);
  529. }
  530. $this->selectedColumns[] = $column;
  531. }
  532. }
  533. }
  534. public function getOutputColumns(): array {
  535. return array_unique($this->selectedColumns);
  536. }
  537. /**
  538. * Turns the query being built into a bulk delete query that ranges over
  539. * a certain table.
  540. *
  541. * <code>
  542. * $qb = $conn->getQueryBuilder()
  543. * ->delete('users', 'u')
  544. * ->where('u.id = :user_id');
  545. * ->setParameter(':user_id', 1);
  546. * </code>
  547. *
  548. * @param string $delete The table whose rows are subject to the deletion.
  549. * @param string $alias The table alias used in the constructed query.
  550. *
  551. * @return $this This QueryBuilder instance.
  552. * @since 30.0.0 Alias is deprecated and will no longer be used with the next Doctrine/DBAL update
  553. */
  554. public function delete($delete = null, $alias = null) {
  555. if ($alias !== null) {
  556. $this->logger->debug('DELETE queries with alias are no longer supported and the provided alias is ignored', ['exception' => new \InvalidArgumentException('Table alias provided for DELETE query')]);
  557. }
  558. $this->queryBuilder->delete(
  559. $this->getTableName($delete),
  560. $alias
  561. );
  562. return $this;
  563. }
  564. /**
  565. * Turns the query being built into a bulk update query that ranges over
  566. * a certain table
  567. *
  568. * <code>
  569. * $qb = $conn->getQueryBuilder()
  570. * ->update('users', 'u')
  571. * ->set('u.password', md5('password'))
  572. * ->where('u.id = ?');
  573. * </code>
  574. *
  575. * @param string $update The table whose rows are subject to the update.
  576. * @param string $alias The table alias used in the constructed query.
  577. *
  578. * @return $this This QueryBuilder instance.
  579. * @since 30.0.0 Alias is deprecated and will no longer be used with the next Doctrine/DBAL update
  580. */
  581. public function update($update = null, $alias = null) {
  582. if ($alias !== null) {
  583. $this->logger->debug('UPDATE queries with alias are no longer supported and the provided alias is ignored', ['exception' => new \InvalidArgumentException('Table alias provided for UPDATE query')]);
  584. }
  585. $this->queryBuilder->update(
  586. $this->getTableName($update),
  587. $alias
  588. );
  589. return $this;
  590. }
  591. /**
  592. * Turns the query being built into an insert query that inserts into
  593. * a certain table
  594. *
  595. * <code>
  596. * $qb = $conn->getQueryBuilder()
  597. * ->insert('users')
  598. * ->values(
  599. * array(
  600. * 'name' => '?',
  601. * 'password' => '?'
  602. * )
  603. * );
  604. * </code>
  605. *
  606. * @param string $insert The table into which the rows should be inserted.
  607. *
  608. * @return $this This QueryBuilder instance.
  609. */
  610. public function insert($insert = null) {
  611. $this->queryBuilder->insert(
  612. $this->getTableName($insert)
  613. );
  614. $this->lastInsertedTable = $insert;
  615. return $this;
  616. }
  617. /**
  618. * Creates and adds a query root corresponding to the table identified by the
  619. * given alias, forming a cartesian product with any existing query roots.
  620. *
  621. * <code>
  622. * $qb = $conn->getQueryBuilder()
  623. * ->select('u.id')
  624. * ->from('users', 'u')
  625. * </code>
  626. *
  627. * @param string|IQueryFunction $from The table.
  628. * @param string|null $alias The alias of the table.
  629. *
  630. * @return $this This QueryBuilder instance.
  631. */
  632. public function from($from, $alias = null) {
  633. $this->queryBuilder->from(
  634. $this->getTableName($from),
  635. $this->quoteAlias($alias)
  636. );
  637. return $this;
  638. }
  639. /**
  640. * Creates and adds a join to the query.
  641. *
  642. * <code>
  643. * $qb = $conn->getQueryBuilder()
  644. * ->select('u.name')
  645. * ->from('users', 'u')
  646. * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  647. * </code>
  648. *
  649. * @param string $fromAlias The alias that points to a from clause.
  650. * @param string $join The table name to join.
  651. * @param string $alias The alias of the join table.
  652. * @param string|ICompositeExpression|null $condition The condition for the join.
  653. *
  654. * @return $this This QueryBuilder instance.
  655. */
  656. public function join($fromAlias, $join, $alias, $condition = null) {
  657. $this->queryBuilder->join(
  658. $this->quoteAlias($fromAlias),
  659. $this->getTableName($join),
  660. $this->quoteAlias($alias),
  661. $condition
  662. );
  663. return $this;
  664. }
  665. /**
  666. * Creates and adds a join to the query.
  667. *
  668. * <code>
  669. * $qb = $conn->getQueryBuilder()
  670. * ->select('u.name')
  671. * ->from('users', 'u')
  672. * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  673. * </code>
  674. *
  675. * @param string $fromAlias The alias that points to a from clause.
  676. * @param string $join The table name to join.
  677. * @param string $alias The alias of the join table.
  678. * @param string|ICompositeExpression|null $condition The condition for the join.
  679. *
  680. * @return $this This QueryBuilder instance.
  681. */
  682. public function innerJoin($fromAlias, $join, $alias, $condition = null) {
  683. $this->queryBuilder->innerJoin(
  684. $this->quoteAlias($fromAlias),
  685. $this->getTableName($join),
  686. $this->quoteAlias($alias),
  687. $condition
  688. );
  689. return $this;
  690. }
  691. /**
  692. * Creates and adds a left join to the query.
  693. *
  694. * <code>
  695. * $qb = $conn->getQueryBuilder()
  696. * ->select('u.name')
  697. * ->from('users', 'u')
  698. * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  699. * </code>
  700. *
  701. * @param string $fromAlias The alias that points to a from clause.
  702. * @param string|IQueryFunction $join The table name or sub-query to join.
  703. * @param string $alias The alias of the join table.
  704. * @param string|ICompositeExpression|null $condition The condition for the join.
  705. *
  706. * @return $this This QueryBuilder instance.
  707. */
  708. public function leftJoin($fromAlias, $join, $alias, $condition = null) {
  709. $this->queryBuilder->leftJoin(
  710. $this->quoteAlias($fromAlias),
  711. $this->getTableName($join),
  712. $this->quoteAlias($alias),
  713. $condition
  714. );
  715. return $this;
  716. }
  717. /**
  718. * Creates and adds a right join to the query.
  719. *
  720. * <code>
  721. * $qb = $conn->getQueryBuilder()
  722. * ->select('u.name')
  723. * ->from('users', 'u')
  724. * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  725. * </code>
  726. *
  727. * @param string $fromAlias The alias that points to a from clause.
  728. * @param string $join The table name to join.
  729. * @param string $alias The alias of the join table.
  730. * @param string|ICompositeExpression|null $condition The condition for the join.
  731. *
  732. * @return $this This QueryBuilder instance.
  733. */
  734. public function rightJoin($fromAlias, $join, $alias, $condition = null) {
  735. $this->queryBuilder->rightJoin(
  736. $this->quoteAlias($fromAlias),
  737. $this->getTableName($join),
  738. $this->quoteAlias($alias),
  739. $condition
  740. );
  741. return $this;
  742. }
  743. /**
  744. * Sets a new value for a column in a bulk update query.
  745. *
  746. * <code>
  747. * $qb = $conn->getQueryBuilder()
  748. * ->update('users', 'u')
  749. * ->set('u.password', md5('password'))
  750. * ->where('u.id = ?');
  751. * </code>
  752. *
  753. * @param string $key The column to set.
  754. * @param ILiteral|IParameter|IQueryFunction|string $value The value, expression, placeholder, etc.
  755. *
  756. * @return $this This QueryBuilder instance.
  757. */
  758. public function set($key, $value) {
  759. $this->queryBuilder->set(
  760. $this->helper->quoteColumnName($key),
  761. $this->helper->quoteColumnName($value)
  762. );
  763. return $this;
  764. }
  765. /**
  766. * Specifies one or more restrictions to the query result.
  767. * Replaces any previously specified restrictions, if any.
  768. *
  769. * <code>
  770. * $qb = $conn->getQueryBuilder()
  771. * ->select('u.name')
  772. * ->from('users', 'u')
  773. * ->where('u.id = ?');
  774. *
  775. * // You can optionally programmatically build and/or expressions
  776. * $qb = $conn->getQueryBuilder();
  777. *
  778. * $or = $qb->expr()->orx(
  779. * $qb->expr()->eq('u.id', 1),
  780. * $qb->expr()->eq('u.id', 2),
  781. * );
  782. *
  783. * $qb->update('users', 'u')
  784. * ->set('u.password', md5('password'))
  785. * ->where($or);
  786. * </code>
  787. *
  788. * @param mixed ...$predicates The restriction predicates.
  789. *
  790. * @return $this This QueryBuilder instance.
  791. */
  792. public function where(...$predicates) {
  793. if ($this->nonEmptyWhere && $this->systemConfig->getValue('debug', false)) {
  794. // Only logging a warning, not throwing for now.
  795. $e = new QueryException('Using where() on non-empty WHERE part, please verify it is intentional to not call andWhere() or orWhere() instead. Otherwise consider creating a new query builder object or call resetQueryPart(\'where\') first.');
  796. $this->logger->warning($e->getMessage(), ['exception' => $e]);
  797. }
  798. $this->nonEmptyWhere = true;
  799. call_user_func_array(
  800. [$this->queryBuilder, 'where'],
  801. $predicates
  802. );
  803. return $this;
  804. }
  805. /**
  806. * Adds one or more restrictions to the query results, forming a logical
  807. * conjunction with any previously specified restrictions.
  808. *
  809. * <code>
  810. * $qb = $conn->getQueryBuilder()
  811. * ->select('u')
  812. * ->from('users', 'u')
  813. * ->where('u.username LIKE ?')
  814. * ->andWhere('u.is_active = 1');
  815. * </code>
  816. *
  817. * @param mixed ...$where The query restrictions.
  818. *
  819. * @return $this This QueryBuilder instance.
  820. *
  821. * @see where()
  822. */
  823. public function andWhere(...$where) {
  824. $this->nonEmptyWhere = true;
  825. call_user_func_array(
  826. [$this->queryBuilder, 'andWhere'],
  827. $where
  828. );
  829. return $this;
  830. }
  831. /**
  832. * Adds one or more restrictions to the query results, forming a logical
  833. * disjunction with any previously specified restrictions.
  834. *
  835. * <code>
  836. * $qb = $conn->getQueryBuilder()
  837. * ->select('u.name')
  838. * ->from('users', 'u')
  839. * ->where('u.id = 1')
  840. * ->orWhere('u.id = 2');
  841. * </code>
  842. *
  843. * @param mixed ...$where The WHERE statement.
  844. *
  845. * @return $this This QueryBuilder instance.
  846. *
  847. * @see where()
  848. */
  849. public function orWhere(...$where) {
  850. $this->nonEmptyWhere = true;
  851. call_user_func_array(
  852. [$this->queryBuilder, 'orWhere'],
  853. $where
  854. );
  855. return $this;
  856. }
  857. /**
  858. * Specifies a grouping over the results of the query.
  859. * Replaces any previously specified groupings, if any.
  860. *
  861. * <code>
  862. * $qb = $conn->getQueryBuilder()
  863. * ->select('u.name')
  864. * ->from('users', 'u')
  865. * ->groupBy('u.id');
  866. * </code>
  867. *
  868. * @param mixed ...$groupBys The grouping expression.
  869. *
  870. * @return $this This QueryBuilder instance.
  871. */
  872. public function groupBy(...$groupBys) {
  873. if (count($groupBys) === 1 && is_array($groupBys[0])) {
  874. $groupBys = $groupBys[0];
  875. }
  876. call_user_func_array(
  877. [$this->queryBuilder, 'groupBy'],
  878. $this->helper->quoteColumnNames($groupBys)
  879. );
  880. return $this;
  881. }
  882. /**
  883. * Adds a grouping expression to the query.
  884. *
  885. * <code>
  886. * $qb = $conn->getQueryBuilder()
  887. * ->select('u.name')
  888. * ->from('users', 'u')
  889. * ->groupBy('u.lastLogin');
  890. * ->addGroupBy('u.createdAt')
  891. * </code>
  892. *
  893. * @param mixed ...$groupBy The grouping expression.
  894. *
  895. * @return $this This QueryBuilder instance.
  896. */
  897. public function addGroupBy(...$groupBy) {
  898. call_user_func_array(
  899. [$this->queryBuilder, 'addGroupBy'],
  900. $this->helper->quoteColumnNames($groupBy)
  901. );
  902. return $this;
  903. }
  904. /**
  905. * Sets a value for a column in an insert query.
  906. *
  907. * <code>
  908. * $qb = $conn->getQueryBuilder()
  909. * ->insert('users')
  910. * ->values(
  911. * array(
  912. * 'name' => '?'
  913. * )
  914. * )
  915. * ->setValue('password', '?');
  916. * </code>
  917. *
  918. * @param string $column The column into which the value should be inserted.
  919. * @param IParameter|string $value The value that should be inserted into the column.
  920. *
  921. * @return $this This QueryBuilder instance.
  922. */
  923. public function setValue($column, $value) {
  924. $this->queryBuilder->setValue(
  925. $this->helper->quoteColumnName($column),
  926. (string) $value
  927. );
  928. return $this;
  929. }
  930. /**
  931. * Specifies values for an insert query indexed by column names.
  932. * Replaces any previous values, if any.
  933. *
  934. * <code>
  935. * $qb = $conn->getQueryBuilder()
  936. * ->insert('users')
  937. * ->values(
  938. * array(
  939. * 'name' => '?',
  940. * 'password' => '?'
  941. * )
  942. * );
  943. * </code>
  944. *
  945. * @param array $values The values to specify for the insert query indexed by column names.
  946. *
  947. * @return $this This QueryBuilder instance.
  948. */
  949. public function values(array $values) {
  950. $quotedValues = [];
  951. foreach ($values as $key => $value) {
  952. $quotedValues[$this->helper->quoteColumnName($key)] = $value;
  953. }
  954. $this->queryBuilder->values($quotedValues);
  955. return $this;
  956. }
  957. /**
  958. * Specifies a restriction over the groups of the query.
  959. * Replaces any previous having restrictions, if any.
  960. *
  961. * @param mixed ...$having The restriction over the groups.
  962. *
  963. * @return $this This QueryBuilder instance.
  964. */
  965. public function having(...$having) {
  966. call_user_func_array(
  967. [$this->queryBuilder, 'having'],
  968. $having
  969. );
  970. return $this;
  971. }
  972. /**
  973. * Adds a restriction over the groups of the query, forming a logical
  974. * conjunction with any existing having restrictions.
  975. *
  976. * @param mixed ...$having The restriction to append.
  977. *
  978. * @return $this This QueryBuilder instance.
  979. */
  980. public function andHaving(...$having) {
  981. call_user_func_array(
  982. [$this->queryBuilder, 'andHaving'],
  983. $having
  984. );
  985. return $this;
  986. }
  987. /**
  988. * Adds a restriction over the groups of the query, forming a logical
  989. * disjunction with any existing having restrictions.
  990. *
  991. * @param mixed ...$having The restriction to add.
  992. *
  993. * @return $this This QueryBuilder instance.
  994. */
  995. public function orHaving(...$having) {
  996. call_user_func_array(
  997. [$this->queryBuilder, 'orHaving'],
  998. $having
  999. );
  1000. return $this;
  1001. }
  1002. /**
  1003. * Specifies an ordering for the query results.
  1004. * Replaces any previously specified orderings, if any.
  1005. *
  1006. * @param string|IQueryFunction|ILiteral|IParameter $sort The ordering expression.
  1007. * @param string $order The ordering direction.
  1008. *
  1009. * @return $this This QueryBuilder instance.
  1010. */
  1011. public function orderBy($sort, $order = null) {
  1012. $this->queryBuilder->orderBy(
  1013. $this->helper->quoteColumnName($sort),
  1014. $order
  1015. );
  1016. return $this;
  1017. }
  1018. /**
  1019. * Adds an ordering to the query results.
  1020. *
  1021. * @param string|ILiteral|IParameter|IQueryFunction $sort The ordering expression.
  1022. * @param string $order The ordering direction.
  1023. *
  1024. * @return $this This QueryBuilder instance.
  1025. */
  1026. public function addOrderBy($sort, $order = null) {
  1027. $this->queryBuilder->addOrderBy(
  1028. $this->helper->quoteColumnName($sort),
  1029. $order
  1030. );
  1031. return $this;
  1032. }
  1033. /**
  1034. * Gets a query part by its name.
  1035. *
  1036. * @param string $queryPartName
  1037. *
  1038. * @return mixed
  1039. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  1040. * and we can not fix this in our wrapper. Please track the details you need, outside the object.
  1041. */
  1042. public function getQueryPart($queryPartName) {
  1043. $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]);
  1044. return $this->queryBuilder->getQueryPart($queryPartName);
  1045. }
  1046. /**
  1047. * Gets all query parts.
  1048. *
  1049. * @return array
  1050. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  1051. * and we can not fix this in our wrapper. Please track the details you need, outside the object.
  1052. */
  1053. public function getQueryParts() {
  1054. $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]);
  1055. return $this->queryBuilder->getQueryParts();
  1056. }
  1057. /**
  1058. * Resets SQL parts.
  1059. *
  1060. * @param array|null $queryPartNames
  1061. *
  1062. * @return $this This QueryBuilder instance.
  1063. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  1064. * and we can not fix this in our wrapper. Please create a new IQueryBuilder instead.
  1065. */
  1066. public function resetQueryParts($queryPartNames = null) {
  1067. $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]);
  1068. $this->queryBuilder->resetQueryParts($queryPartNames);
  1069. return $this;
  1070. }
  1071. /**
  1072. * Resets a single SQL part.
  1073. *
  1074. * @param string $queryPartName
  1075. *
  1076. * @return $this This QueryBuilder instance.
  1077. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  1078. * and we can not fix this in our wrapper. Please create a new IQueryBuilder instead.
  1079. */
  1080. public function resetQueryPart($queryPartName) {
  1081. $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]);
  1082. $this->queryBuilder->resetQueryPart($queryPartName);
  1083. return $this;
  1084. }
  1085. /**
  1086. * Creates a new named parameter and bind the value $value to it.
  1087. *
  1088. * This method provides a shortcut for PDOStatement::bindValue
  1089. * when using prepared statements.
  1090. *
  1091. * The parameter $value specifies the value that you want to bind. If
  1092. * $placeholder is not provided bindValue() will automatically create a
  1093. * placeholder for you. An automatic placeholder will be of the name
  1094. * ':dcValue1', ':dcValue2' etc.
  1095. *
  1096. * For more information see {@link https://www.php.net/pdostatement-bindparam}
  1097. *
  1098. * Example:
  1099. * <code>
  1100. * $value = 2;
  1101. * $q->eq( 'id', $q->bindValue( $value ) );
  1102. * $stmt = $q->executeQuery(); // executed with 'id = 2'
  1103. * </code>
  1104. *
  1105. * @license New BSD License
  1106. * @link http://www.zetacomponents.org
  1107. *
  1108. * @param mixed $value
  1109. * @param IQueryBuilder::PARAM_* $type
  1110. * @param string $placeHolder The name to bind with. The string must start with a colon ':'.
  1111. *
  1112. * @return IParameter the placeholder name used.
  1113. */
  1114. public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) {
  1115. return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder));
  1116. }
  1117. /**
  1118. * Creates a new positional parameter and bind the given value to it.
  1119. *
  1120. * Attention: If you are using positional parameters with the query builder you have
  1121. * to be very careful to bind all parameters in the order they appear in the SQL
  1122. * statement , otherwise they get bound in the wrong order which can lead to serious
  1123. * bugs in your code.
  1124. *
  1125. * Example:
  1126. * <code>
  1127. * $qb = $conn->getQueryBuilder();
  1128. * $qb->select('u.*')
  1129. * ->from('users', 'u')
  1130. * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR))
  1131. * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR))
  1132. * </code>
  1133. *
  1134. * @param mixed $value
  1135. * @param IQueryBuilder::PARAM_* $type
  1136. *
  1137. * @return IParameter
  1138. */
  1139. public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) {
  1140. return new Parameter($this->queryBuilder->createPositionalParameter($value, $type));
  1141. }
  1142. /**
  1143. * Creates a new parameter
  1144. *
  1145. * Example:
  1146. * <code>
  1147. * $qb = $conn->getQueryBuilder();
  1148. * $qb->select('u.*')
  1149. * ->from('users', 'u')
  1150. * ->where('u.username = ' . $qb->createParameter('name'))
  1151. * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR))
  1152. * </code>
  1153. *
  1154. * @param string $name
  1155. *
  1156. * @return IParameter
  1157. */
  1158. public function createParameter($name) {
  1159. return new Parameter(':' . $name);
  1160. }
  1161. /**
  1162. * Creates a new function
  1163. *
  1164. * Attention: Column names inside the call have to be quoted before hand
  1165. *
  1166. * Example:
  1167. * <code>
  1168. * $qb = $conn->getQueryBuilder();
  1169. * $qb->select($qb->createFunction('COUNT(*)'))
  1170. * ->from('users', 'u')
  1171. * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u
  1172. * </code>
  1173. * <code>
  1174. * $qb = $conn->getQueryBuilder();
  1175. * $qb->select($qb->createFunction('COUNT(`column`)'))
  1176. * ->from('users', 'u')
  1177. * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u
  1178. * </code>
  1179. *
  1180. * @param string $call
  1181. *
  1182. * @return IQueryFunction
  1183. */
  1184. public function createFunction($call) {
  1185. return new QueryFunction($call);
  1186. }
  1187. /**
  1188. * Used to get the id of the last inserted element
  1189. * @return int
  1190. * @throws \BadMethodCallException When being called before an insert query has been run.
  1191. */
  1192. public function getLastInsertId(): int {
  1193. if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) {
  1194. // lastInsertId() needs the prefix but no quotes
  1195. $table = $this->prefixTableName($this->lastInsertedTable);
  1196. return $this->connection->lastInsertId($table);
  1197. }
  1198. throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.');
  1199. }
  1200. /**
  1201. * Returns the table name quoted and with database prefix as needed by the implementation
  1202. *
  1203. * @param string|IQueryFunction $table
  1204. * @return string
  1205. */
  1206. public function getTableName($table) {
  1207. if ($table instanceof IQueryFunction) {
  1208. return (string) $table;
  1209. }
  1210. $table = $this->prefixTableName($table);
  1211. return $this->helper->quoteColumnName($table);
  1212. }
  1213. /**
  1214. * Returns the table name with database prefix as needed by the implementation
  1215. *
  1216. * @param string $table
  1217. * @return string
  1218. */
  1219. public function prefixTableName(string $table): string {
  1220. if ($this->automaticTablePrefix === false || str_starts_with($table, '*PREFIX*')) {
  1221. return $table;
  1222. }
  1223. return '*PREFIX*' . $table;
  1224. }
  1225. /**
  1226. * Returns the column name quoted and with table alias prefix as needed by the implementation
  1227. *
  1228. * @param string $column
  1229. * @param string $tableAlias
  1230. * @return string
  1231. */
  1232. public function getColumnName($column, $tableAlias = '') {
  1233. if ($tableAlias !== '') {
  1234. $tableAlias .= '.';
  1235. }
  1236. return $this->helper->quoteColumnName($tableAlias . $column);
  1237. }
  1238. /**
  1239. * Returns the column name quoted and with table alias prefix as needed by the implementation
  1240. *
  1241. * @param string $alias
  1242. * @return string
  1243. */
  1244. public function quoteAlias($alias) {
  1245. if ($alias === '' || $alias === null) {
  1246. return $alias;
  1247. }
  1248. return $this->helper->quoteColumnName($alias);
  1249. }
  1250. public function hintShardKey(string $column, mixed $value, bool $overwrite = false): self {
  1251. return $this;
  1252. }
  1253. public function runAcrossAllShards(): self {
  1254. // noop
  1255. return $this;
  1256. }
  1257. }