1
0

QuoteHelperTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\DB\QueryBuilder;
  22. use OC\DB\QueryBuilder\Literal;
  23. use OC\DB\QueryBuilder\Parameter;
  24. use OC\DB\QueryBuilder\QuoteHelper;
  25. use OCP\DB\QueryBuilder\ILiteral;
  26. use OCP\DB\QueryBuilder\IParameter;
  27. class QuoteHelperTest extends \Test\TestCase {
  28. /** @var QuoteHelper */
  29. protected $helper;
  30. protected function setUp() {
  31. parent::setUp();
  32. $this->helper = new QuoteHelper();
  33. }
  34. public function dataQuoteColumnName() {
  35. return [
  36. ['column', '`column`'],
  37. [new Literal('literal'), 'literal'],
  38. [new Literal(1), '1'],
  39. [new Parameter(':param'), ':param'],
  40. // (string) 'null' is Doctrines way to set columns to null
  41. // See https://github.com/owncloud/core/issues/19314
  42. ['null', 'null'],
  43. ];
  44. }
  45. /**
  46. * @dataProvider dataQuoteColumnName
  47. * @param mixed $input
  48. * @param string $expected
  49. */
  50. public function testQuoteColumnName($input, $expected) {
  51. $this->assertSame(
  52. $expected,
  53. $this->helper->quoteColumnName($input)
  54. );
  55. }
  56. public function dataQuoteColumnNames() {
  57. return [
  58. // Single case
  59. ['d.column', '`d`.`column`'],
  60. ['column', '`column`'],
  61. [new Literal('literal'), 'literal'],
  62. [new Literal(1), '1'],
  63. [new Parameter(':param'), ':param'],
  64. // Array case
  65. [['column'], ['`column`']],
  66. [[new Literal('literal')], ['literal']],
  67. [[new Literal(1)], ['1']],
  68. [[new Parameter(':param')], [':param']],
  69. // Array mixed cases
  70. [['column1', 'column2'], ['`column1`', '`column2`']],
  71. [['column', new Literal('literal')], ['`column`', 'literal']],
  72. [['column', new Literal(1)], ['`column`', '1']],
  73. [['column', new Parameter(':param')], ['`column`', ':param']],
  74. ];
  75. }
  76. /**
  77. * @dataProvider dataQuoteColumnNames
  78. * @param mixed $input
  79. * @param string $expected
  80. */
  81. public function testQuoteColumnNames($input, $expected) {
  82. $this->assertSame(
  83. $expected,
  84. $this->helper->quoteColumnNames($input)
  85. );
  86. }
  87. /**
  88. * @param array|string|ILiteral|IParameter $strings string, Literal or Parameter
  89. * @return array|string
  90. */
  91. public function quoteColumnNames($strings) {
  92. if (!is_array($strings)) {
  93. return $this->quoteColumnName($strings);
  94. }
  95. $return = [];
  96. foreach ($strings as $string) {
  97. $return[] = $this->quoteColumnName($string);
  98. }
  99. return $return;
  100. }
  101. /**
  102. * @param string|ILiteral|IParameter $string string, Literal or Parameter
  103. * @return string
  104. */
  105. public function quoteColumnName($string) {
  106. if ($string instanceof IParameter) {
  107. return $string->getName();
  108. }
  109. if ($string instanceof ILiteral) {
  110. return $string->getLiteral();
  111. }
  112. if ($string === null) {
  113. return $string;
  114. }
  115. if (!is_string($string)) {
  116. throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed');
  117. }
  118. if (substr_count($string, '.')) {
  119. list($alias, $columnName) = explode('.', $string);
  120. return '`' . $alias . '`.`' . $columnName . '`';
  121. }
  122. return '`' . $string . '`';
  123. }
  124. }