aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/DB/QueryBuilder/QuoteHelperTest.php
blob: 12984bac3e06e4f6fac940c10b283782214f33d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
 * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-only
 */

namespace Test\DB\QueryBuilder;

use OC\DB\QueryBuilder\Literal;
use OC\DB\QueryBuilder\Parameter;
use OC\DB\QueryBuilder\QuoteHelper;
use OCP\DB\QueryBuilder\ILiteral;
use OCP\DB\QueryBuilder\IParameter;

class QuoteHelperTest extends \Test\TestCase {
	/** @var QuoteHelper */
	protected $helper;

	protected function setUp(): void {
		parent::setUp();

		$this->helper = new QuoteHelper();
	}

	public function dataQuoteColumnName() {
		return [
			['column', '`column`'],
			[new Literal('literal'), 'literal'],
			[new Literal(1), '1'],
			[new Parameter(':param'), ':param'],

			// (string) 'null' is Doctrines way to set columns to null
			// See https://github.com/owncloud/core/issues/19314
			['null', 'null'],
		];
	}

	/**
	 * @dataProvider dataQuoteColumnName
	 * @param mixed $input
	 * @param string $expected
	 */
	public function testQuoteColumnName($input, $expected) {
		$this->assertSame(
			$expected,
			$this->helper->quoteColumnName($input)
		);
	}

	public function dataQuoteColumnNames() {
		return [
			// Single case
			['d.column', '`d`.`column`'],
			['column', '`column`'],
			[new Literal('literal'), 'literal'],
			[new Literal(1), '1'],
			[new Parameter(':param'), ':param'],

			// Array case
			[['column'], ['`column`']],
			[[new Literal('literal')], ['literal']],
			[[new Literal(1)], ['1']],
			[[new Parameter(':param')], [':param']],

			// Array mixed cases
			[['column1', 'column2'], ['`column1`', '`column2`']],
			[['column', new Literal('literal')], ['`column`', 'literal']],
			[['column', new Literal(1)], ['`column`', '1']],
			[['column', new Parameter(':param')], ['`column`', ':param']],
		];
	}

	/**
	 * @dataProvider dataQuoteColumnNames
	 * @param mixed $input
	 * @param string $expected
	 */
	public function testQuoteColumnNames($input, $expected) {
		$this->assertSame(
			$expected,
			$this->helper->quoteColumnNames($input)
		);
	}

	/**
	 * @param array|string|ILiteral|IParameter $strings string, Literal or Parameter
	 * @return array|string
	 */
	public function quoteColumnNames($strings) {
		if (!is_array($strings)) {
			return $this->quoteColumnName($strings);
		}

		$return = [];
		foreach ($strings as $string) {
			$return[] = $this->quoteColumnName($string);
		}

		return $return;
	}

	/**
	 * @param string|ILiteral|IParameter $string string, Literal or Parameter
	 * @return string
	 */
	public function quoteColumnName($string) {
		if ($string instanceof IParameter) {
			return $string->getName();
		}

		if ($string instanceof ILiteral) {
			return $string->getLiteral();
		}

		if ($string === null) {
			return $string;
		}

		if (!is_string($string)) {
			throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed');
		}

		if (substr_count($string, '.')) {
			[$alias, $columnName] = explode('.', $string);
			return '`' . $alias . '`.`' . $columnName . '`';
		}

		return '`' . $string . '`';
	}
}