MySqlExpressionBuilder.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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\ExpressionBuilder;
  8. use OC\DB\ConnectionAdapter;
  9. use OC\DB\QueryBuilder\QueryFunction;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\DB\QueryBuilder\IQueryFunction;
  12. use Psr\Log\LoggerInterface;
  13. class MySqlExpressionBuilder extends ExpressionBuilder {
  14. protected string $collation;
  15. public function __construct(ConnectionAdapter $connection, IQueryBuilder $queryBuilder, LoggerInterface $logger) {
  16. parent::__construct($connection, $queryBuilder, $logger);
  17. $params = $connection->getInner()->getParams();
  18. $this->collation = $params['collation'] ?? (($params['charset'] ?? 'utf8') . '_general_ci');
  19. }
  20. /**
  21. * @inheritdoc
  22. */
  23. public function iLike($x, $y, $type = null): string {
  24. $x = $this->helper->quoteColumnName($x);
  25. $y = $this->helper->quoteColumnName($y);
  26. return $this->expressionBuilder->comparison($x, ' COLLATE ' . $this->collation . ' LIKE', $y);
  27. }
  28. /**
  29. * Returns a IQueryFunction that casts the column to the given type
  30. *
  31. * @param string|IQueryFunction $column
  32. * @param mixed $type One of IQueryBuilder::PARAM_*
  33. * @psalm-param IQueryBuilder::PARAM_* $type
  34. * @return IQueryFunction
  35. */
  36. public function castColumn($column, $type): IQueryFunction {
  37. switch ($type) {
  38. case IQueryBuilder::PARAM_STR:
  39. return new QueryFunction('CAST(' . $this->helper->quoteColumnName($column) . ' AS CHAR)');
  40. default:
  41. return parent::castColumn($column, $type);
  42. }
  43. }
  44. }