1
0

SqliteExpressionBuilder.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\DB\QueryBuilder\ExpressionBuilder;
  7. use OC\DB\QueryBuilder\QueryFunction;
  8. use OCP\DB\QueryBuilder\ILiteral;
  9. use OCP\DB\QueryBuilder\IParameter;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\DB\QueryBuilder\IQueryFunction;
  12. class SqliteExpressionBuilder extends ExpressionBuilder {
  13. /**
  14. * @inheritdoc
  15. */
  16. public function like($x, $y, $type = null): string {
  17. return parent::like($x, $y, $type) . " ESCAPE '\\'";
  18. }
  19. public function iLike($x, $y, $type = null): string {
  20. return $this->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y), $type);
  21. }
  22. /**
  23. * @param mixed $column
  24. * @param mixed|null $type
  25. * @return array|IQueryFunction|string
  26. */
  27. protected function prepareColumn($column, $type) {
  28. if ($type !== null
  29. && !is_array($column)
  30. && !($column instanceof IParameter)
  31. && !($column instanceof ILiteral)
  32. && (str_starts_with($type, 'date') || str_starts_with($type, 'time'))) {
  33. return $this->castColumn($column, $type);
  34. }
  35. return parent::prepareColumn($column, $type);
  36. }
  37. /**
  38. * Returns a IQueryFunction that casts the column to the given type
  39. *
  40. * @param string $column
  41. * @param mixed $type One of IQueryBuilder::PARAM_*
  42. * @return IQueryFunction
  43. */
  44. public function castColumn($column, $type): IQueryFunction {
  45. switch ($type) {
  46. case IQueryBuilder::PARAM_DATE_MUTABLE:
  47. case IQueryBuilder::PARAM_DATE_IMMUTABLE:
  48. $column = $this->helper->quoteColumnName($column);
  49. return new QueryFunction('DATE(' . $column . ')');
  50. case IQueryBuilder::PARAM_DATETIME_MUTABLE:
  51. case IQueryBuilder::PARAM_DATETIME_IMMUTABLE:
  52. case IQueryBuilder::PARAM_DATETIME_TZ_MUTABLE:
  53. case IQueryBuilder::PARAM_DATETIME_TZ_IMMUTABLE:
  54. $column = $this->helper->quoteColumnName($column);
  55. return new QueryFunction('DATETIME(' . $column . ')');
  56. case IQueryBuilder::PARAM_TIME_MUTABLE:
  57. case IQueryBuilder::PARAM_TIME_IMMUTABLE:
  58. $column = $this->helper->quoteColumnName($column);
  59. return new QueryFunction('TIME(' . $column . ')');
  60. }
  61. return parent::castColumn($column, $type);
  62. }
  63. }