1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.query.internal; |
21 |
|
|
22 |
|
import org.apache.commons.lang3.StringUtils; |
23 |
|
import org.slf4j.Logger; |
24 |
|
import org.slf4j.LoggerFactory; |
25 |
|
import org.xwiki.component.manager.ComponentLookupException; |
26 |
|
import org.xwiki.component.manager.ComponentManager; |
27 |
|
import org.xwiki.query.Query; |
28 |
|
import org.xwiki.query.QueryException; |
29 |
|
import org.xwiki.query.QueryFilter; |
30 |
|
import org.xwiki.query.QueryManager; |
31 |
|
import org.xwiki.query.SecureQuery; |
32 |
|
|
33 |
|
import java.util.List; |
34 |
|
import java.util.Map; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
@version |
40 |
|
@since |
41 |
|
|
|
|
| 66.7% |
Uncovered Elements: 29 (87) |
Complexity: 30 |
Complexity Density: 0.55 |
|
42 |
|
public class ScriptQuery implements SecureQuery |
43 |
|
{ |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(ScriptQuery.class); |
48 |
|
|
49 |
|
|
50 |
|
@link |
51 |
|
|
52 |
|
private ComponentManager componentManager; |
53 |
|
|
54 |
|
|
55 |
|
@link |
56 |
|
|
57 |
|
private Query query; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@param |
63 |
|
@param |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
65 |
363 |
public ScriptQuery(Query query, ComponentManager cm)... |
66 |
|
{ |
67 |
363 |
this.query = query; |
68 |
363 |
this.componentManager = cm; |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@param |
75 |
|
@return |
76 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
77 |
288 |
public Query addFilter(String filter)... |
78 |
|
{ |
79 |
288 |
if (!StringUtils.isBlank(filter)) { |
80 |
288 |
try { |
81 |
288 |
QueryFilter queryFilter = this.componentManager.getInstance(QueryFilter.class, filter); |
82 |
288 |
addFilter(queryFilter); |
83 |
|
} catch (ComponentLookupException e) { |
84 |
|
|
85 |
0 |
LOGGER.warn("Failed to load QueryFilter with component hint [{}]", filter); |
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
288 |
return this; |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
@link |
95 |
|
|
96 |
|
@return |
97 |
|
|
|
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 2 |
Complexity Density: 0.12 |
|
98 |
79 |
public long count()... |
99 |
|
{ |
100 |
79 |
long result = -1; |
101 |
|
|
102 |
79 |
try { |
103 |
|
|
104 |
79 |
QueryManager queryManager = (QueryManager) this.componentManager.getInstance(QueryManager.class); |
105 |
79 |
Query countQuery = queryManager.createQuery(getStatement(), getLanguage()); |
106 |
79 |
countQuery.setWiki(getWiki()); |
107 |
79 |
for (Map.Entry<Integer, Object> entry : getPositionalParameters().entrySet()) { |
108 |
139 |
countQuery.bindValue(entry.getKey(), entry.getValue()); |
109 |
|
} |
110 |
79 |
for (Map.Entry<String, Object> entry : getNamedParameters().entrySet()) { |
111 |
14 |
countQuery.bindValue(entry.getKey(), entry.getValue()); |
112 |
|
} |
113 |
79 |
for (QueryFilter filter : getFilters()) { |
114 |
102 |
countQuery.addFilter(filter); |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
79 |
countQuery.addFilter(this.componentManager.<QueryFilter>getInstance(QueryFilter.class, "count")); |
119 |
|
|
120 |
|
|
121 |
79 |
List<Long> results = countQuery.execute(); |
122 |
79 |
result = results.get(0); |
123 |
|
} catch (Exception e) { |
124 |
0 |
LOGGER.warn("Failed to create count query for query [{}]", getStatement()); |
125 |
0 |
e.printStackTrace(); |
126 |
|
} |
127 |
|
|
128 |
79 |
return result; |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
79 |
@Override... |
132 |
|
public String getStatement() |
133 |
|
{ |
134 |
79 |
return this.query.getStatement(); |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
137 |
79 |
@Override... |
138 |
|
public String getLanguage() |
139 |
|
{ |
140 |
79 |
return this.query.getLanguage(); |
141 |
|
} |
142 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
143 |
0 |
@Override... |
144 |
|
public boolean isNamed() |
145 |
|
{ |
146 |
0 |
return this.query.isNamed(); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
149 |
13 |
@Override... |
150 |
|
public Query setWiki(String wiki) |
151 |
|
{ |
152 |
13 |
this.query.setWiki(wiki); |
153 |
13 |
return this; |
154 |
|
} |
155 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
156 |
79 |
@Override... |
157 |
|
public String getWiki() |
158 |
|
{ |
159 |
79 |
return this.query.getWiki(); |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
162 |
38 |
@Override... |
163 |
|
public Query bindValue(String var, Object val) |
164 |
|
{ |
165 |
38 |
this.query.bindValue(var, val); |
166 |
38 |
return this; |
167 |
|
} |
168 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
169 |
0 |
@Override... |
170 |
|
public Query bindValue(int index, Object val) |
171 |
|
{ |
172 |
0 |
this.query.bindValue(index, val); |
173 |
0 |
return this; |
174 |
|
} |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
176 |
275 |
@Override... |
177 |
|
public Query bindValues(List<Object> values) |
178 |
|
{ |
179 |
275 |
this.query.bindValues(values); |
180 |
275 |
return this; |
181 |
|
} |
182 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
183 |
79 |
@Override... |
184 |
|
public Map<String, Object> getNamedParameters() |
185 |
|
{ |
186 |
79 |
return this.query.getNamedParameters(); |
187 |
|
} |
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
189 |
79 |
@Override... |
190 |
|
public Map<Integer, Object> getPositionalParameters() |
191 |
|
{ |
192 |
79 |
return this.query.getPositionalParameters(); |
193 |
|
} |
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
195 |
288 |
@Override... |
196 |
|
public Query addFilter(QueryFilter filter) |
197 |
|
{ |
198 |
288 |
this.query.addFilter(filter); |
199 |
288 |
return this; |
200 |
|
} |
201 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
202 |
79 |
@Override... |
203 |
|
public List<QueryFilter> getFilters() |
204 |
|
{ |
205 |
79 |
return this.query.getFilters(); |
206 |
|
} |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
208 |
76 |
@Override... |
209 |
|
public Query setLimit(int limit) |
210 |
|
{ |
211 |
76 |
this.query.setLimit(limit); |
212 |
76 |
return this; |
213 |
|
} |
214 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
215 |
76 |
@Override... |
216 |
|
public Query setOffset(int offset) |
217 |
|
{ |
218 |
76 |
this.query.setOffset(offset); |
219 |
76 |
return this; |
220 |
|
} |
221 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
222 |
0 |
@Override... |
223 |
|
public int getLimit() |
224 |
|
{ |
225 |
0 |
return this.query.getLimit(); |
226 |
|
} |
227 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
228 |
0 |
@Override... |
229 |
|
public int getOffset() |
230 |
|
{ |
231 |
0 |
return this.query.getOffset(); |
232 |
|
} |
233 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
234 |
337 |
@Override... |
235 |
|
public <T> List<T> execute() throws QueryException |
236 |
|
{ |
237 |
337 |
return this.query.execute(); |
238 |
|
} |
239 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
240 |
0 |
@Override... |
241 |
|
public boolean isCurrentAuthorChecked() |
242 |
|
{ |
243 |
0 |
return this.query instanceof SecureQuery ? ((SecureQuery) this.query).isCurrentAuthorChecked() : true; |
244 |
|
} |
245 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
246 |
0 |
@Override... |
247 |
|
public SecureQuery checkCurrentAuthor(boolean checkCurrentAuthor) |
248 |
|
{ |
249 |
|
|
250 |
|
|
251 |
0 |
return this; |
252 |
|
} |
253 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
254 |
0 |
@Override... |
255 |
|
public boolean isCurrentUserChecked() |
256 |
|
{ |
257 |
0 |
return this.query instanceof SecureQuery ? ((SecureQuery) this.query).isCurrentAuthorChecked() : false; |
258 |
|
} |
259 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
260 |
0 |
@Override... |
261 |
|
public SecureQuery checkCurrentUser(boolean checkCurrentUser) |
262 |
|
{ |
263 |
0 |
if (this.query instanceof SecureQuery) { |
264 |
0 |
((SecureQuery) this.query).isCurrentAuthorChecked(); |
265 |
|
} |
266 |
|
|
267 |
0 |
return this; |
268 |
|
} |
269 |
|
} |