QueryBuilder.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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($column, '.')) {
  525. [, $column] = explode('.', $column);
  526. }
  527. $this->selectedColumns[] = $column;
  528. }
  529. }
  530. }
  531. public function getOutputColumns(): array {
  532. return array_unique(array_map(function (string $column) {
  533. if (str_contains($column, '.')) {
  534. [, $column] = explode('.', $column);
  535. return $column;
  536. } else {
  537. return $column;
  538. }
  539. }, $this->selectedColumns));
  540. }
  541. /**
  542. * Turns the query being built into a bulk delete query that ranges over
  543. * a certain table.
  544. *
  545. * <code>
  546. * $qb = $conn->getQueryBuilder()
  547. * ->delete('users', 'u')
  548. * ->where('u.id = :user_id');
  549. * ->setParameter(':user_id', 1);
  550. * </code>
  551. *
  552. * @param string $delete The table whose rows are subject to the deletion.
  553. * @param string $alias The table alias used in the constructed query.
  554. *
  555. * @return $this This QueryBuilder instance.
  556. * @since 30.0.0 Alias is deprecated and will no longer be used with the next Doctrine/DBAL update
  557. */
  558. public function delete($delete = null, $alias = null) {
  559. if ($alias !== null) {
  560. $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')]);
  561. }
  562. $this->queryBuilder->delete(
  563. $this->getTableName($delete),
  564. $alias
  565. );
  566. return $this;
  567. }
  568. /**
  569. * Turns the query being built into a bulk update query that ranges over
  570. * a certain table
  571. *
  572. * <code>
  573. * $qb = $conn->getQueryBuilder()
  574. * ->update('users', 'u')
  575. * ->set('u.password', md5('password'))
  576. * ->where('u.id = ?');
  577. * </code>
  578. *
  579. * @param string $update The table whose rows are subject to the update.
  580. * @param string $alias The table alias used in the constructed query.
  581. *
  582. * @return $this This QueryBuilder instance.
  583. * @since 30.0.0 Alias is deprecated and will no longer be used with the next Doctrine/DBAL update
  584. */
  585. public function update($update = null, $alias = null) {
  586. if ($alias !== null) {
  587. $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')]);
  588. }
  589. $this->queryBuilder->update(
  590. $this->getTableName($update),
  591. $alias
  592. );
  593. return $this;
  594. }
  595. /**
  596. * Turns the query being built into an insert query that inserts into
  597. * a certain table
  598. *
  599. * <code>
  600. * $qb = $conn->getQueryBuilder()
  601. * ->insert('users')
  602. * ->values(
  603. * array(
  604. * 'name' => '?',
  605. * 'password' => '?'
  606. * )
  607. * );
  608. * </code>
  609. *
  610. * @param string $insert The table into which the rows should be inserted.
  611. *
  612. * @return $this This QueryBuilder instance.
  613. */
  614. public function insert($insert = null) {
  615. $this->queryBuilder->insert(
  616. $this->getTableName($insert)
  617. );
  618. $this->lastInsertedTable = $insert;
  619. return $this;
  620. }
  621. /**
  622. * Creates and adds a query root corresponding to the table identified by the
  623. * given alias, forming a cartesian product with any existing query roots.
  624. *
  625. * <code>
  626. * $qb = $conn->getQueryBuilder()
  627. * ->select('u.id')
  628. * ->from('users', 'u')
  629. * </code>
  630. *
  631. * @param string|IQueryFunction $from The table.
  632. * @param string|null $alias The alias of the table.
  633. *
  634. * @return $this This QueryBuilder instance.
  635. */
  636. public function from($from, $alias = null) {
  637. $this->queryBuilder->from(
  638. $this->getTableName($from),
  639. $this->quoteAlias($alias)
  640. );
  641. return $this;
  642. }
  643. /**
  644. * Creates and adds a join to the query.
  645. *
  646. * <code>
  647. * $qb = $conn->getQueryBuilder()
  648. * ->select('u.name')
  649. * ->from('users', 'u')
  650. * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  651. * </code>
  652. *
  653. * @param string $fromAlias The alias that points to a from clause.
  654. * @param string $join The table name to join.
  655. * @param string $alias The alias of the join table.
  656. * @param string|ICompositeExpression|null $condition The condition for the join.
  657. *
  658. * @return $this This QueryBuilder instance.
  659. */
  660. public function join($fromAlias, $join, $alias, $condition = null) {
  661. $this->queryBuilder->join(
  662. $this->quoteAlias($fromAlias),
  663. $this->getTableName($join),
  664. $this->quoteAlias($alias),
  665. $condition
  666. );
  667. return $this;
  668. }
  669. /**
  670. * Creates and adds a join to the query.
  671. *
  672. * <code>
  673. * $qb = $conn->getQueryBuilder()
  674. * ->select('u.name')
  675. * ->from('users', 'u')
  676. * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  677. * </code>
  678. *
  679. * @param string $fromAlias The alias that points to a from clause.
  680. * @param string $join The table name to join.
  681. * @param string $alias The alias of the join table.
  682. * @param string|ICompositeExpression|null $condition The condition for the join.
  683. *
  684. * @return $this This QueryBuilder instance.
  685. */
  686. public function innerJoin($fromAlias, $join, $alias, $condition = null) {
  687. $this->queryBuilder->innerJoin(
  688. $this->quoteAlias($fromAlias),
  689. $this->getTableName($join),
  690. $this->quoteAlias($alias),
  691. $condition
  692. );
  693. return $this;
  694. }
  695. /**
  696. * Creates and adds a left join to the query.
  697. *
  698. * <code>
  699. * $qb = $conn->getQueryBuilder()
  700. * ->select('u.name')
  701. * ->from('users', 'u')
  702. * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  703. * </code>
  704. *
  705. * @param string $fromAlias The alias that points to a from clause.
  706. * @param string|IQueryFunction $join The table name or sub-query to join.
  707. * @param string $alias The alias of the join table.
  708. * @param string|ICompositeExpression|null $condition The condition for the join.
  709. *
  710. * @return $this This QueryBuilder instance.
  711. */
  712. public function leftJoin($fromAlias, $join, $alias, $condition = null) {
  713. $this->queryBuilder->leftJoin(
  714. $this->quoteAlias($fromAlias),
  715. $this->getTableName($join),
  716. $this->quoteAlias($alias),
  717. $condition
  718. );
  719. return $this;
  720. }
  721. /**
  722. * Creates and adds a right join to the query.
  723. *
  724. * <code>
  725. * $qb = $conn->getQueryBuilder()
  726. * ->select('u.name')
  727. * ->from('users', 'u')
  728. * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
  729. * </code>
  730. *
  731. * @param string $fromAlias The alias that points to a from clause.
  732. * @param string $join The table name to join.
  733. * @param string $alias The alias of the join table.
  734. * @param string|ICompositeExpression|null $condition The condition for the join.
  735. *
  736. * @return $this This QueryBuilder instance.
  737. */
  738. public function rightJoin($fromAlias, $join, $alias, $condition = null) {
  739. $this->queryBuilder->rightJoin(
  740. $this->quoteAlias($fromAlias),
  741. $this->getTableName($join),
  742. $this->quoteAlias($alias),
  743. $condition
  744. );
  745. return $this;
  746. }
  747. /**
  748. * Sets a new value for a column in a bulk update query.
  749. *
  750. * <code>
  751. * $qb = $conn->getQueryBuilder()
  752. * ->update('users', 'u')
  753. * ->set('u.password', md5('password'))
  754. * ->where('u.id = ?');
  755. * </code>
  756. *
  757. * @param string $key The column to set.
  758. * @param ILiteral|IParameter|IQueryFunction|string $value The value, expression, placeholder, etc.
  759. *
  760. * @return $this This QueryBuilder instance.
  761. */
  762. public function set($key, $value) {
  763. $this->queryBuilder->set(
  764. $this->helper->quoteColumnName($key),
  765. $this->helper->quoteColumnName($value)
  766. );
  767. return $this;
  768. }
  769. /**
  770. * Specifies one or more restrictions to the query result.
  771. * Replaces any previously specified restrictions, if any.
  772. *
  773. * <code>
  774. * $qb = $conn->getQueryBuilder()
  775. * ->select('u.name')
  776. * ->from('users', 'u')
  777. * ->where('u.id = ?');
  778. *
  779. * // You can optionally programmatically build and/or expressions
  780. * $qb = $conn->getQueryBuilder();
  781. *
  782. * $or = $qb->expr()->orx(
  783. * $qb->expr()->eq('u.id', 1),
  784. * $qb->expr()->eq('u.id', 2),
  785. * );
  786. *
  787. * $qb->update('users', 'u')
  788. * ->set('u.password', md5('password'))
  789. * ->where($or);
  790. * </code>
  791. *
  792. * @param mixed ...$predicates The restriction predicates.
  793. *
  794. * @return $this This QueryBuilder instance.
  795. */
  796. public function where(...$predicates) {
  797. if ($this->nonEmptyWhere && $this->systemConfig->getValue('debug', false)) {
  798. // Only logging a warning, not throwing for now.
  799. $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.');
  800. $this->logger->warning($e->getMessage(), ['exception' => $e]);
  801. }
  802. $this->nonEmptyWhere = true;
  803. call_user_func_array(
  804. [$this->queryBuilder, 'where'],
  805. $predicates
  806. );
  807. return $this;
  808. }
  809. /**
  810. * Adds one or more restrictions to the query results, forming a logical
  811. * conjunction with any previously specified restrictions.
  812. *
  813. * <code>
  814. * $qb = $conn->getQueryBuilder()
  815. * ->select('u')
  816. * ->from('users', 'u')
  817. * ->where('u.username LIKE ?')
  818. * ->andWhere('u.is_active = 1');
  819. * </code>
  820. *
  821. * @param mixed ...$where The query restrictions.
  822. *
  823. * @return $this This QueryBuilder instance.
  824. *
  825. * @see where()
  826. */
  827. public function andWhere(...$where) {
  828. $this->nonEmptyWhere = true;
  829. call_user_func_array(
  830. [$this->queryBuilder, 'andWhere'],
  831. $where
  832. );
  833. return $this;
  834. }
  835. /**
  836. * Adds one or more restrictions to the query results, forming a logical
  837. * disjunction with any previously specified restrictions.
  838. *
  839. * <code>
  840. * $qb = $conn->getQueryBuilder()
  841. * ->select('u.name')
  842. * ->from('users', 'u')
  843. * ->where('u.id = 1')
  844. * ->orWhere('u.id = 2');
  845. * </code>
  846. *
  847. * @param mixed ...$where The WHERE statement.
  848. *
  849. * @return $this This QueryBuilder instance.
  850. *
  851. * @see where()
  852. */
  853. public function orWhere(...$where) {
  854. $this->nonEmptyWhere = true;
  855. call_user_func_array(
  856. [$this->queryBuilder, 'orWhere'],
  857. $where
  858. );
  859. return $this;
  860. }
  861. /**
  862. * Specifies a grouping over the results of the query.
  863. * Replaces any previously specified groupings, if any.
  864. *
  865. * <code>
  866. * $qb = $conn->getQueryBuilder()
  867. * ->select('u.name')
  868. * ->from('users', 'u')
  869. * ->groupBy('u.id');
  870. * </code>
  871. *
  872. * @param mixed ...$groupBys The grouping expression.
  873. *
  874. * @return $this This QueryBuilder instance.
  875. */
  876. public function groupBy(...$groupBys) {
  877. if (count($groupBys) === 1 && is_array($groupBys[0])) {
  878. $groupBys = $groupBys[0];
  879. }
  880. call_user_func_array(
  881. [$this->queryBuilder, 'groupBy'],
  882. $this->helper->quoteColumnNames($groupBys)
  883. );
  884. return $this;
  885. }
  886. /**
  887. * Adds a grouping expression to the query.
  888. *
  889. * <code>
  890. * $qb = $conn->getQueryBuilder()
  891. * ->select('u.name')
  892. * ->from('users', 'u')
  893. * ->groupBy('u.lastLogin');
  894. * ->addGroupBy('u.createdAt')
  895. * </code>
  896. *
  897. * @param mixed ...$groupBy The grouping expression.
  898. *
  899. * @return $this This QueryBuilder instance.
  900. */
  901. public function addGroupBy(...$groupBy) {
  902. call_user_func_array(
  903. [$this->queryBuilder, 'addGroupBy'],
  904. $this->helper->quoteColumnNames($groupBy)
  905. );
  906. return $this;
  907. }
  908. /**
  909. * Sets a value for a column in an insert query.
  910. *
  911. * <code>
  912. * $qb = $conn->getQueryBuilder()
  913. * ->insert('users')
  914. * ->values(
  915. * array(
  916. * 'name' => '?'
  917. * )
  918. * )
  919. * ->setValue('password', '?');
  920. * </code>
  921. *
  922. * @param string $column The column into which the value should be inserted.
  923. * @param IParameter|string $value The value that should be inserted into the column.
  924. *
  925. * @return $this This QueryBuilder instance.
  926. */
  927. public function setValue($column, $value) {
  928. $this->queryBuilder->setValue(
  929. $this->helper->quoteColumnName($column),
  930. (string)$value
  931. );
  932. return $this;
  933. }
  934. /**
  935. * Specifies values for an insert query indexed by column names.
  936. * Replaces any previous values, if any.
  937. *
  938. * <code>
  939. * $qb = $conn->getQueryBuilder()
  940. * ->insert('users')
  941. * ->values(
  942. * array(
  943. * 'name' => '?',
  944. * 'password' => '?'
  945. * )
  946. * );
  947. * </code>
  948. *
  949. * @param array $values The values to specify for the insert query indexed by column names.
  950. *
  951. * @return $this This QueryBuilder instance.
  952. */
  953. public function values(array $values) {
  954. $quotedValues = [];
  955. foreach ($values as $key => $value) {
  956. $quotedValues[$this->helper->quoteColumnName($key)] = $value;
  957. }
  958. $this->queryBuilder->values($quotedValues);
  959. return $this;
  960. }
  961. /**
  962. * Specifies a restriction over the groups of the query.
  963. * Replaces any previous having restrictions, if any.
  964. *
  965. * @param mixed ...$having The restriction over the groups.
  966. *
  967. * @return $this This QueryBuilder instance.
  968. */
  969. public function having(...$having) {
  970. call_user_func_array(
  971. [$this->queryBuilder, 'having'],
  972. $having
  973. );
  974. return $this;
  975. }
  976. /**
  977. * Adds a restriction over the groups of the query, forming a logical
  978. * conjunction with any existing having restrictions.
  979. *
  980. * @param mixed ...$having The restriction to append.
  981. *
  982. * @return $this This QueryBuilder instance.
  983. */
  984. public function andHaving(...$having) {
  985. call_user_func_array(
  986. [$this->queryBuilder, 'andHaving'],
  987. $having
  988. );
  989. return $this;
  990. }
  991. /**
  992. * Adds a restriction over the groups of the query, forming a logical
  993. * disjunction with any existing having restrictions.
  994. *
  995. * @param mixed ...$having The restriction to add.
  996. *
  997. * @return $this This QueryBuilder instance.
  998. */
  999. public function orHaving(...$having) {
  1000. call_user_func_array(
  1001. [$this->queryBuilder, 'orHaving'],
  1002. $having
  1003. );
  1004. return $this;
  1005. }
  1006. /**
  1007. * Specifies an ordering for the query results.
  1008. * Replaces any previously specified orderings, if any.
  1009. *
  1010. * @param string|IQueryFunction|ILiteral|IParameter $sort The ordering expression.
  1011. * @param string $order The ordering direction.
  1012. *
  1013. * @return $this This QueryBuilder instance.
  1014. */
  1015. public function orderBy($sort, $order = null) {
  1016. $this->queryBuilder->orderBy(
  1017. $this->helper->quoteColumnName($sort),
  1018. $order
  1019. );
  1020. return $this;
  1021. }
  1022. /**
  1023. * Adds an ordering to the query results.
  1024. *
  1025. * @param string|ILiteral|IParameter|IQueryFunction $sort The ordering expression.
  1026. * @param string $order The ordering direction.
  1027. *
  1028. * @return $this This QueryBuilder instance.
  1029. */
  1030. public function addOrderBy($sort, $order = null) {
  1031. $this->queryBuilder->addOrderBy(
  1032. $this->helper->quoteColumnName($sort),
  1033. $order
  1034. );
  1035. return $this;
  1036. }
  1037. /**
  1038. * Gets a query part by its name.
  1039. *
  1040. * @param string $queryPartName
  1041. *
  1042. * @return mixed
  1043. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  1044. * and we can not fix this in our wrapper. Please track the details you need, outside the object.
  1045. */
  1046. public function getQueryPart($queryPartName) {
  1047. $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]);
  1048. return $this->queryBuilder->getQueryPart($queryPartName);
  1049. }
  1050. /**
  1051. * Gets all query parts.
  1052. *
  1053. * @return array
  1054. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  1055. * and we can not fix this in our wrapper. Please track the details you need, outside the object.
  1056. */
  1057. public function getQueryParts() {
  1058. $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]);
  1059. return $this->queryBuilder->getQueryParts();
  1060. }
  1061. /**
  1062. * Resets SQL parts.
  1063. *
  1064. * @param array|null $queryPartNames
  1065. *
  1066. * @return $this This QueryBuilder instance.
  1067. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  1068. * and we can not fix this in our wrapper. Please create a new IQueryBuilder instead.
  1069. */
  1070. public function resetQueryParts($queryPartNames = null) {
  1071. $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]);
  1072. $this->queryBuilder->resetQueryParts($queryPartNames);
  1073. return $this;
  1074. }
  1075. /**
  1076. * Resets a single SQL part.
  1077. *
  1078. * @param string $queryPartName
  1079. *
  1080. * @return $this This QueryBuilder instance.
  1081. * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update
  1082. * and we can not fix this in our wrapper. Please create a new IQueryBuilder instead.
  1083. */
  1084. public function resetQueryPart($queryPartName) {
  1085. $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]);
  1086. $this->queryBuilder->resetQueryPart($queryPartName);
  1087. return $this;
  1088. }
  1089. /**
  1090. * Creates a new named parameter and bind the value $value to it.
  1091. *
  1092. * This method provides a shortcut for PDOStatement::bindValue
  1093. * when using prepared statements.
  1094. *
  1095. * The parameter $value specifies the value that you want to bind. If
  1096. * $placeholder is not provided bindValue() will automatically create a
  1097. * placeholder for you. An automatic placeholder will be of the name
  1098. * ':dcValue1', ':dcValue2' etc.
  1099. *
  1100. * For more information see {@link https://www.php.net/pdostatement-bindparam}
  1101. *
  1102. * Example:
  1103. * <code>
  1104. * $value = 2;
  1105. * $q->eq( 'id', $q->bindValue( $value ) );
  1106. * $stmt = $q->executeQuery(); // executed with 'id = 2'
  1107. * </code>
  1108. *
  1109. * @license New BSD License
  1110. * @link http://www.zetacomponents.org
  1111. *
  1112. * @param mixed $value
  1113. * @param IQueryBuilder::PARAM_* $type
  1114. * @param string $placeHolder The name to bind with. The string must start with a colon ':'.
  1115. *
  1116. * @return IParameter the placeholder name used.
  1117. */
  1118. public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) {
  1119. return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder));
  1120. }
  1121. /**
  1122. * Creates a new positional parameter and bind the given value to it.
  1123. *
  1124. * Attention: If you are using positional parameters with the query builder you have
  1125. * to be very careful to bind all parameters in the order they appear in the SQL
  1126. * statement , otherwise they get bound in the wrong order which can lead to serious
  1127. * bugs in your code.
  1128. *
  1129. * Example:
  1130. * <code>
  1131. * $qb = $conn->getQueryBuilder();
  1132. * $qb->select('u.*')
  1133. * ->from('users', 'u')
  1134. * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR))
  1135. * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR))
  1136. * </code>
  1137. *
  1138. * @param mixed $value
  1139. * @param IQueryBuilder::PARAM_* $type
  1140. *
  1141. * @return IParameter
  1142. */
  1143. public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) {
  1144. return new Parameter($this->queryBuilder->createPositionalParameter($value, $type));
  1145. }
  1146. /**
  1147. * Creates a new parameter
  1148. *
  1149. * Example:
  1150. * <code>
  1151. * $qb = $conn->getQueryBuilder();
  1152. * $qb->select('u.*')
  1153. * ->from('users', 'u')
  1154. * ->where('u.username = ' . $qb->createParameter('name'))
  1155. * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR))
  1156. * </code>
  1157. *
  1158. * @param string $name
  1159. *
  1160. * @return IParameter
  1161. */
  1162. public function createParameter($name) {
  1163. return new Parameter(':' . $name);
  1164. }
  1165. /**
  1166. * Creates a new function
  1167. *
  1168. * Attention: Column names inside the call have to be quoted before hand
  1169. *
  1170. * Example:
  1171. * <code>
  1172. * $qb = $conn->getQueryBuilder();
  1173. * $qb->select($qb->createFunction('COUNT(*)'))
  1174. * ->from('users', 'u')
  1175. * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u
  1176. * </code>
  1177. * <code>
  1178. * $qb = $conn->getQueryBuilder();
  1179. * $qb->select($qb->createFunction('COUNT(`column`)'))
  1180. * ->from('users', 'u')
  1181. * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u
  1182. * </code>
  1183. *
  1184. * @param string $call
  1185. *
  1186. * @return IQueryFunction
  1187. */
  1188. public function createFunction($call) {
  1189. return new QueryFunction($call);
  1190. }
  1191. /**
  1192. * Used to get the id of the last inserted element
  1193. * @return int
  1194. * @throws \BadMethodCallException When being called before an insert query has been run.
  1195. */
  1196. public function getLastInsertId(): int {
  1197. if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) {
  1198. // lastInsertId() needs the prefix but no quotes
  1199. $table = $this->prefixTableName($this->lastInsertedTable);
  1200. return $this->connection->lastInsertId($table);
  1201. }
  1202. throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.');
  1203. }
  1204. /**
  1205. * Returns the table name quoted and with database prefix as needed by the implementation
  1206. *
  1207. * @param string|IQueryFunction $table
  1208. * @return string
  1209. */
  1210. public function getTableName($table) {
  1211. if ($table instanceof IQueryFunction) {
  1212. return (string)$table;
  1213. }
  1214. $table = $this->prefixTableName($table);
  1215. return $this->helper->quoteColumnName($table);
  1216. }
  1217. /**
  1218. * Returns the table name with database prefix as needed by the implementation
  1219. *
  1220. * @param string $table
  1221. * @return string
  1222. */
  1223. public function prefixTableName(string $table): string {
  1224. if ($this->automaticTablePrefix === false || str_starts_with($table, '*PREFIX*')) {
  1225. return $table;
  1226. }
  1227. return '*PREFIX*' . $table;
  1228. }
  1229. /**
  1230. * Returns the column name quoted and with table alias prefix as needed by the implementation
  1231. *
  1232. * @param string $column
  1233. * @param string $tableAlias
  1234. * @return string
  1235. */
  1236. public function getColumnName($column, $tableAlias = '') {
  1237. if ($tableAlias !== '') {
  1238. $tableAlias .= '.';
  1239. }
  1240. return $this->helper->quoteColumnName($tableAlias . $column);
  1241. }
  1242. /**
  1243. * Returns the column name quoted and with table alias prefix as needed by the implementation
  1244. *
  1245. * @param string $alias
  1246. * @return string
  1247. */
  1248. public function quoteAlias($alias) {
  1249. if ($alias === '' || $alias === null) {
  1250. return $alias;
  1251. }
  1252. return $this->helper->quoteColumnName($alias);
  1253. }
  1254. public function escapeLikeParameter(string $parameter): string {
  1255. return $this->connection->escapeLikeParameter($parameter);
  1256. }
  1257. public function hintShardKey(string $column, mixed $value, bool $overwrite = false): self {
  1258. return $this;
  1259. }
  1260. public function runAcrossAllShards(): self {
  1261. // noop
  1262. return $this;
  1263. }
  1264. }