MySqlExpressionBuilder.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\DB\QueryBuilder\ExpressionBuilder;
  27. use OC\DB\ConnectionAdapter;
  28. use OC\DB\QueryBuilder\QueryFunction;
  29. use OCP\DB\QueryBuilder\IQueryBuilder;
  30. use OCP\DB\QueryBuilder\IQueryFunction;
  31. class MySqlExpressionBuilder extends ExpressionBuilder {
  32. /** @var string */
  33. protected $collation;
  34. /**
  35. * @param ConnectionAdapter $connection
  36. * @param IQueryBuilder $queryBuilder
  37. */
  38. public function __construct(ConnectionAdapter $connection, IQueryBuilder $queryBuilder) {
  39. parent::__construct($connection, $queryBuilder);
  40. $params = $connection->getInner()->getParams();
  41. $this->collation = $params['collation'] ?? (($params['charset'] ?? 'utf8') . '_general_ci');
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function iLike($x, $y, $type = null): string {
  47. $x = $this->helper->quoteColumnName($x);
  48. $y = $this->helper->quoteColumnName($y);
  49. return $this->expressionBuilder->comparison($x, ' COLLATE ' . $this->collation . ' LIKE', $y);
  50. }
  51. /**
  52. * Returns a IQueryFunction that casts the column to the given type
  53. *
  54. * @param string|IQueryFunction $column
  55. * @param mixed $type One of IQueryBuilder::PARAM_*
  56. * @psalm-param IQueryBuilder::PARAM_* $type
  57. * @return IQueryFunction
  58. */
  59. public function castColumn($column, $type): IQueryFunction {
  60. switch ($type) {
  61. case IQueryBuilder::PARAM_STR:
  62. return new QueryFunction('CAST(' . $this->helper->quoteColumnName($column) . ' AS CHAR)');
  63. default:
  64. return parent::castColumn($column, $type);
  65. }
  66. }
  67. }