|
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
|
|
|
* Returns a IQueryFunction that casts the column to the given type |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $column |
|
144
|
|
|
* @param mixed $type One of IQueryBuilder::PARAM_* |
|
145
|
|
|
* @return IQueryFunction |
|
146
|
|
|
*/ |
|
147
|
|
View Code Duplication |
public function castColumn($column, $type) { |
|
|
|
|
|
|
148
|
|
|
if ($type === IQueryBuilder::PARAM_STR) { |
|
149
|
|
|
$column = $this->helper->quoteColumnName($column); |
|
150
|
|
|
return new QueryFunction('to_char(' . $column . ')'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return parent::castColumn($column, $type); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @inheritdoc |
|
158
|
|
|
*/ |
|
159
|
|
|
public function like($x, $y, $type = null) { |
|
160
|
|
|
return parent::like($x, $y, $type) . " ESCAPE '\\'"; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @inheritdoc |
|
165
|
|
|
*/ |
|
166
|
|
|
public function iLike($x, $y, $type = null) { |
|
167
|
|
|
$x = $this->helper->quoteColumnName($x); |
|
168
|
|
|
$y = $this->helper->quoteColumnName($y); |
|
169
|
|
|
return new QueryFunction('REGEXP_LIKE('.$x.', \'^\' || REPLACE('.$y.', \'%\', \'.*\') || \'$\', \'i\')'); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.