Completed
Push — master ( ca3aef...5b2d6b )
by Morris
15:49
created

OCIExpressionBuilder   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 160
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 8
loc 160
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 3

15 Methods

Rating   Name   Duplication   Size   Complexity  
B prepareColumn() 0 8 5
A comparison() 0 6 1
A eq() 0 6 1
A neq() 0 6 1
A lt() 0 6 1
A lte() 0 6 1
A gt() 0 6 1
A gte() 0 6 1
A in() 0 6 1
A notIn() 0 6 1
A castColumn() 8 8 2
A like() 0 3 1
A iLike() 0 5 1
A emptyString() 0 3 1
A nonEmptyString() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\DB\QueryBuilder\ExpressionBuilder;
26
27
28
use OC\DB\QueryBuilder\QueryFunction;
29
use OCP\DB\QueryBuilder\ILiteral;
30
use OCP\DB\QueryBuilder\IParameter;
31
use OCP\DB\QueryBuilder\IQueryBuilder;
32
use OCP\DB\QueryBuilder\IQueryFunction;
33
34
class OCIExpressionBuilder extends ExpressionBuilder {
35
36
	/**
37
	 * @param mixed $column
38
	 * @param mixed|null $type
39
	 * @return array|IQueryFunction|string
40
	 */
41
	protected function prepareColumn($column, $type) {
42
		if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) {
43
			$column = $this->castColumn($column, $type);
44
		} else {
45
			$column = $this->helper->quoteColumnNames($column);
46
		}
47
		return $column;
48
	}
49
50
	/**
51
	 * @inheritdoc
52
	 */
53
	public function comparison($x, $operator, $y, $type = null) {
54
		$x = $this->prepareColumn($x, $type);
55
		$y = $this->prepareColumn($y, $type);
56
57
		return $this->expressionBuilder->comparison($x, $operator, $y);
58
	}
59
60
	/**
61
	 * @inheritdoc
62
	 */
63
	public function eq($x, $y, $type = null) {
64
		$x = $this->prepareColumn($x, $type);
65
		$y = $this->prepareColumn($y, $type);
66
67
		return $this->expressionBuilder->eq($x, $y);
68
	}
69
70
	/**
71
	 * @inheritdoc
72
	 */
73
	public function neq($x, $y, $type = null) {
74
		$x = $this->prepareColumn($x, $type);
75
		$y = $this->prepareColumn($y, $type);
76
77
		return $this->expressionBuilder->neq($x, $y);
78
	}
79
80
	/**
81
	 * @inheritdoc
82
	 */
83
	public function lt($x, $y, $type = null) {
84
		$x = $this->prepareColumn($x, $type);
85
		$y = $this->prepareColumn($y, $type);
86
87
		return $this->expressionBuilder->lt($x, $y);
88
	}
89
90
	/**
91
	 * @inheritdoc
92
	 */
93
	public function lte($x, $y, $type = null) {
94
		$x = $this->prepareColumn($x, $type);
95
		$y = $this->prepareColumn($y, $type);
96
97
		return $this->expressionBuilder->lte($x, $y);
98
	}
99
100
	/**
101
	 * @inheritdoc
102
	 */
103
	public function gt($x, $y, $type = null) {
104
		$x = $this->prepareColumn($x, $type);
105
		$y = $this->prepareColumn($y, $type);
106
107
		return $this->expressionBuilder->gt($x, $y);
108
	}
109
110
	/**
111
	 * @inheritdoc
112
	 */
113
	public function gte($x, $y, $type = null) {
114
		$x = $this->prepareColumn($x, $type);
115
		$y = $this->prepareColumn($y, $type);
116
117
		return $this->expressionBuilder->gte($x, $y);
118
	}
119
120
	/**
121
	 * @inheritdoc
122
	 */
123
	public function in($x, $y, $type = null) {
124
		$x = $this->prepareColumn($x, $type);
125
		$y = $this->prepareColumn($y, $type);
126
127
		return $this->expressionBuilder->in($x, $y);
128
	}
129
130
	/**
131
	 * @inheritdoc
132
	 */
133
	public function notIn($x, $y, $type = null) {
134
		$x = $this->prepareColumn($x, $type);
135
		$y = $this->prepareColumn($y, $type);
136
137
		return $this->expressionBuilder->notIn($x, $y);
138
	}
139
140
	/**
141
	 * Creates a $x = '' statement, because Oracle needs a different check
142
	 *
143
	 * @param string $x The field in string format to be inspected by the comparison.
144
	 * @return string
145
	 * @since 13.0.0
146
	 */
147
	public function emptyString($x) {
148
		return $this->isNull($x);
149
	}
150
151
	/**
152
	 * Creates a `$x <> ''` statement, because Oracle needs a different check
153
	 *
154
	 * @param string $x The field in string format to be inspected by the comparison.
155
	 * @return string
156
	 * @since 13.0.0
157
	 */
158
	public function nonEmptyString($x) {
159
		return $this->isNotNull($x);
160
	}
161
162
	/**
163
	 * Returns a IQueryFunction that casts the column to the given type
164
	 *
165
	 * @param string $column
166
	 * @param mixed $type One of IQueryBuilder::PARAM_*
167
	 * @return IQueryFunction
168
	 */
169 View Code Duplication
	public function castColumn($column, $type) {
170
		if ($type === IQueryBuilder::PARAM_STR) {
171
			$column = $this->helper->quoteColumnName($column);
172
			return new QueryFunction('to_char(' . $column . ')');
173
		}
174
175
		return parent::castColumn($column, $type);
176
	}
177
178
	/**
179
	 * @inheritdoc
180
	 */
181
	public function like($x, $y, $type = null) {
182
		return parent::like($x, $y, $type) . " ESCAPE '\\'";
183
	}
184
185
	/**
186
	 * @inheritdoc
187
	 */
188
	public function iLike($x, $y, $type = null) {
189
		$x = $this->helper->quoteColumnName($x);
190
		$y = $this->helper->quoteColumnName($y);
191
		return new QueryFunction('REGEXP_LIKE(' . $x . ', \'^\' || REPLACE(REPLACE(' . $y . ', \'%\', \'.*\'), \'_\', \'.\') || \'$\', \'i\')');
192
	}
193
}
194