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 javax.inject.Inject; |
23 |
|
|
24 |
|
import org.slf4j.Logger; |
25 |
|
import org.xwiki.query.Query; |
26 |
|
import org.xwiki.query.QueryFilter; |
27 |
|
|
28 |
|
|
29 |
|
@link |
30 |
|
|
31 |
|
@version |
32 |
|
@since |
33 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (36) |
Complexity: 10 |
Complexity Density: 0.45 |
|
34 |
|
public abstract class AbstractWhereQueryFilter implements QueryFilter |
35 |
|
{ |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
private static final String WHERE = " where "; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@Inject |
45 |
|
private Logger logger; |
46 |
|
|
47 |
|
|
48 |
|
@param |
49 |
|
@return |
50 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
51 |
876 |
protected boolean isFilterable(String statement)... |
52 |
|
{ |
53 |
|
|
54 |
876 |
return statement.indexOf("xwikidocument as doc") > -1 || statement.indexOf("xwikidocument doc") > -1; |
55 |
|
} |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
@param |
61 |
|
@param |
62 |
|
@param |
63 |
|
@return |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (33) |
Complexity: 9 |
Complexity Density: 0.43 |
|
65 |
903 |
protected String insertWhereClause(String whereClause, String statement, String language)... |
66 |
|
{ |
67 |
903 |
String result = statement.trim(); |
68 |
903 |
String lowerStatement = result.toLowerCase(); |
69 |
903 |
String original = result; |
70 |
|
|
71 |
903 |
if (Query.HQL.equals(language) && isFilterable(lowerStatement)) { |
72 |
|
|
73 |
899 |
int whereIdx = lowerStatement.indexOf(WHERE); |
74 |
899 |
int orderByIdx = Math.min(lowerStatement.indexOf(" order by "), Integer.MAX_VALUE); |
75 |
899 |
int groupByIdx = Math.min(lowerStatement.indexOf(" group by "), Integer.MAX_VALUE); |
76 |
|
|
77 |
899 |
orderByIdx = orderByIdx < 0 ? Integer.MAX_VALUE : orderByIdx; |
78 |
899 |
groupByIdx = groupByIdx < 0 ? Integer.MAX_VALUE : groupByIdx; |
79 |
|
|
80 |
899 |
int orderOrGroupByIdx = Math.min(orderByIdx, groupByIdx); |
81 |
|
|
82 |
899 |
if (whereIdx >= 0) { |
83 |
|
|
84 |
|
|
85 |
848 |
whereIdx = whereIdx + WHERE.length(); |
86 |
848 |
int whereEndIdx = Math.min(orderOrGroupByIdx, lowerStatement.length()); |
87 |
848 |
result = result.substring(0, whereEndIdx) + ")" + result.substring(whereEndIdx); |
88 |
848 |
result = |
89 |
|
result.substring(0, whereIdx) + whereClause + " and (" + result.substring(whereIdx); |
90 |
|
} else { |
91 |
|
|
92 |
51 |
if (orderOrGroupByIdx > 0 && orderOrGroupByIdx < Integer.MAX_VALUE) { |
93 |
|
|
94 |
49 |
result = result.substring(0, orderOrGroupByIdx) + WHERE + whereClause |
95 |
|
+ result.substring(orderOrGroupByIdx); |
96 |
|
} else { |
97 |
|
|
98 |
2 |
result = result + WHERE + whereClause; |
99 |
|
} |
100 |
|
|
101 |
|
} |
102 |
|
} |
103 |
|
|
104 |
903 |
if (!original.equals(result)) { |
105 |
899 |
logger.debug("Query [{}] has been transformed into [{}]", original, result); |
106 |
|
} |
107 |
|
|
108 |
903 |
return result; |
109 |
|
} |
110 |
|
} |