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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/*
* Copyright 2013 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit.tickets;
import com.gitblit.utils.StringUtils;
/**
* A Lucene query builder.
*
* @author James Moger
*
*/
public class QueryBuilder {
private final QueryBuilder parent;
private String q;
private transient StringBuilder sb;
private int opCount;
public static QueryBuilder q(String kernel) {
return new QueryBuilder(kernel);
}
private QueryBuilder(QueryBuilder parent) {
this.sb = new StringBuilder();
this.parent = parent;
}
public QueryBuilder() {
this("");
}
public QueryBuilder(String query) {
this.sb = new StringBuilder(query == null ? "" : query);
this.parent = null;
}
public boolean containsField(String field) {
return sb.toString().contains(field + ":");
}
/**
* Creates a new AND subquery. Make sure to call endSubquery to
* get return *this* query.
*
* e.g. field:something AND (subquery)
*
* @return a subquery
*/
public QueryBuilder andSubquery() {
sb.append(" AND (");
return new QueryBuilder(this);
}
/**
* Creates a new OR subquery. Make sure to call endSubquery to
* get return *this* query.
*
* e.g. field:something OR (subquery)
*
* @return a subquery
*/
public QueryBuilder orSubquery() {
sb.append(" OR (");
return new QueryBuilder(this);
}
/**
* Ends a subquery and returns the parent query.
*
* @return the parent query
*/
public QueryBuilder endSubquery() {
this.q = sb.toString().trim();
if (q.length() > 0) {
parent.sb.append(q).append(')');
}
return parent;
}
/**
* Append an OR condition.
*
* @param condition
* @return
*/
public QueryBuilder or(String condition) {
return op(condition, " OR ");
}
/**
* Append an AND condition.
*
* @param condition
* @return
*/
public QueryBuilder and(String condition) {
return op(condition, " AND ");
}
/**
* Append an AND NOT condition.
*
* @param condition
* @return
*/
public QueryBuilder andNot(String condition) {
return op(condition, " AND NOT ");
}
/**
* Nest this query as a subquery.
*
* e.g. field:something AND field2:something else
* ==> (field:something AND field2:something else)
*
* @return this query nested as a subquery
*/
public QueryBuilder toSubquery() {
if (opCount > 1) {
sb.insert(0, '(').append(')');
}
return this;
}
/**
* Nest this query as an AND subquery of the condition
*
* @param condition
* @return the query nested as an AND subquery of the specified condition
*/
public QueryBuilder subqueryOf(String condition) {
if (!StringUtils.isEmpty(condition)) {
toSubquery().and(condition);
}
return this;
}
/**
* Removes a condition from the query.
*
* @param condition
* @return the query
*/
public QueryBuilder remove(String condition) {
int start = sb.indexOf(condition);
if (start == 0) {
// strip first condition
sb.replace(0, condition.length(), "");
} else if (start > 1) {
// locate condition in query
int space1 = sb.lastIndexOf(" ", start - 1);
int space0 = sb.lastIndexOf(" ", space1 - 1);
if (space0 > -1 && space1 > -1) {
String conjunction = sb.substring(space0, space1).trim();
if ("OR".equals(conjunction) || "AND".equals(conjunction)) {
// remove the conjunction
sb.replace(space0, start + condition.length(), "");
} else {
// unknown conjunction
sb.replace(start, start + condition.length(), "");
}
} else {
sb.replace(start, start + condition.length(), "");
}
}
return this;
}
/**
* Generate the return the Lucene query.
*
* @return the generated query
*/
public String build() {
if (parent != null) {
throw new IllegalAccessError("You can not build a subquery! endSubquery() instead!");
}
this.q = sb.toString().trim();
// cleanup paranthesis
while (q.contains("()")) {
q = q.replace("()", "");
}
if (q.length() > 0) {
if (q.charAt(0) == '(' && q.charAt(q.length() - 1) == ')') {
// query is wrapped by unnecessary paranthesis
q = q.substring(1, q.length() - 1);
}
}
if (q.startsWith("AND ")) {
q = q.substring(3).trim();
}
if (q.startsWith("OR ")) {
q = q.substring(2).trim();
}
return q;
}
private QueryBuilder op(String condition, String op) {
opCount++;
if (!StringUtils.isEmpty(condition)) {
if (sb.length() != 0) {
sb.append(op);
}
sb.append(condition);
}
return this;
}
@Override
public String toString() {
return sb.toString().trim();
}
}
|