1
0

QuoteHelperTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\DB\QueryBuilder;
  8. use OC\DB\QueryBuilder\Literal;
  9. use OC\DB\QueryBuilder\Parameter;
  10. use OC\DB\QueryBuilder\QuoteHelper;
  11. use OCP\DB\QueryBuilder\ILiteral;
  12. use OCP\DB\QueryBuilder\IParameter;
  13. class QuoteHelperTest extends \Test\TestCase {
  14. /** @var QuoteHelper */
  15. protected $helper;
  16. protected function setUp(): void {
  17. parent::setUp();
  18. $this->helper = new QuoteHelper();
  19. }
  20. public function dataQuoteColumnName() {
  21. return [
  22. ['column', '`column`'],
  23. [new Literal('literal'), 'literal'],
  24. [new Literal(1), '1'],
  25. [new Parameter(':param'), ':param'],
  26. // (string) 'null' is Doctrines way to set columns to null
  27. // See https://github.com/owncloud/core/issues/19314
  28. ['null', 'null'],
  29. ];
  30. }
  31. /**
  32. * @dataProvider dataQuoteColumnName
  33. * @param mixed $input
  34. * @param string $expected
  35. */
  36. public function testQuoteColumnName($input, $expected): void {
  37. $this->assertSame(
  38. $expected,
  39. $this->helper->quoteColumnName($input)
  40. );
  41. }
  42. public function dataQuoteColumnNames() {
  43. return [
  44. // Single case
  45. ['d.column', '`d`.`column`'],
  46. ['column', '`column`'],
  47. [new Literal('literal'), 'literal'],
  48. [new Literal(1), '1'],
  49. [new Parameter(':param'), ':param'],
  50. // Array case
  51. [['column'], ['`column`']],
  52. [[new Literal('literal')], ['literal']],
  53. [[new Literal(1)], ['1']],
  54. [[new Parameter(':param')], [':param']],
  55. // Array mixed cases
  56. [['column1', 'column2'], ['`column1`', '`column2`']],
  57. [['column', new Literal('literal')], ['`column`', 'literal']],
  58. [['column', new Literal(1)], ['`column`', '1']],
  59. [['column', new Parameter(':param')], ['`column`', ':param']],
  60. ];
  61. }
  62. /**
  63. * @dataProvider dataQuoteColumnNames
  64. * @param mixed $input
  65. * @param string $expected
  66. */
  67. public function testQuoteColumnNames($input, $expected): void {
  68. $this->assertSame(
  69. $expected,
  70. $this->helper->quoteColumnNames($input)
  71. );
  72. }
  73. /**
  74. * @param array|string|ILiteral|IParameter $strings string, Literal or Parameter
  75. * @return array|string
  76. */
  77. public function quoteColumnNames($strings) {
  78. if (!is_array($strings)) {
  79. return $this->quoteColumnName($strings);
  80. }
  81. $return = [];
  82. foreach ($strings as $string) {
  83. $return[] = $this->quoteColumnName($string);
  84. }
  85. return $return;
  86. }
  87. /**
  88. * @param string|ILiteral|IParameter $string string, Literal or Parameter
  89. * @return string
  90. */
  91. public function quoteColumnName($string) {
  92. if ($string instanceof IParameter) {
  93. return $string->getName();
  94. }
  95. if ($string instanceof ILiteral) {
  96. return $string->getLiteral();
  97. }
  98. if ($string === null) {
  99. return $string;
  100. }
  101. if (!is_string($string)) {
  102. throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed');
  103. }
  104. if (substr_count($string, '.')) {
  105. [$alias, $columnName] = explode('.', $string);
  106. return '`' . $alias . '`.`' . $columnName . '`';
  107. }
  108. return '`' . $string . '`';
  109. }
  110. }