QueryBuilder.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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 escapeLikeParameter(string $parameter): string {
  1251. return $this->connection->escapeLikeParameter($parameter);
  1252. }
  1253. public function hintShardKey(string $column, mixed $value, bool $overwrite = false): self {
  1254. return $this;
  1255. }
  1256. public function runAcrossAllShards(): self {
  1257. // noop
  1258. return $this;
  1259. }
  1260. }