Skip to content

Commit de29d0b

Browse files
authored
Support limit and offset in AqlQueryBuilder (#371)
1 parent ee286ad commit de29d0b

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

services/src/main/java/org/jfrog/artifactory/client/aql/AqlQueryBuilder.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class AqlQueryBuilder {
1717
private AqlRootElement root = new AqlRootElement();
1818
private AqlItem sort;
1919
private AqlInclude include;
20+
private Integer limit;
21+
private Integer offset;
2022

2123
public AqlQueryBuilder item(AqlItem item) {
2224
root.putAll(item.value());
@@ -26,9 +28,9 @@ public AqlQueryBuilder item(AqlItem item) {
2628
public AqlQueryBuilder elements(AqlItem... items) {
2729
if (isNotEmpty(items)) {
2830
root.putAll(Arrays.stream(items)
29-
.map(item -> item.value().entrySet())
30-
.flatMap(Collection::stream)
31-
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
31+
.map(item -> item.value().entrySet())
32+
.flatMap(Collection::stream)
33+
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
3234
}
3335
return this;
3436
}
@@ -97,10 +99,21 @@ public AqlQueryBuilder desc(String... by) {
9799
return this;
98100
}
99101

102+
public AqlQueryBuilder limit(int limit) {
103+
this.limit = limit;
104+
return this;
105+
}
106+
107+
public AqlQueryBuilder offset(int offset) {
108+
this.offset = offset;
109+
return this;
110+
}
111+
100112
public String build() {
101113
try {
102114
ObjectMapper mapper = new ObjectMapper();
103-
return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString(mapper);
115+
return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString(
116+
mapper) + getOffsetAsString(mapper) + getLimitAsString(mapper);
104117
} catch (JsonProcessingException e) {
105118
throw new AqlBuilderException("Error serializing object to json: ", e);
106119
}
@@ -114,16 +127,32 @@ private String getIncludeAsString() {
114127
return hasInclude() ? include.toString() : "";
115128
}
116129

130+
private String getOffsetAsString(ObjectMapper mapper) throws JsonProcessingException {
131+
return hasOffset() ? ".offset(" + mapper.writeValueAsString(offset) + ")" : "";
132+
}
133+
134+
private String getLimitAsString(ObjectMapper mapper) throws JsonProcessingException {
135+
return hasLimit() ? ".limit(" + mapper.writeValueAsString(limit) + ")" : "";
136+
}
137+
117138
private String getRootAsString(ObjectMapper mapper) throws JsonProcessingException {
118139
return hasRoot() ? mapper.writeValueAsString(root) : "";
119140
}
120141

142+
private boolean hasInclude() {
143+
return include != null && include.isNotEmpty();
144+
}
145+
121146
private boolean hasSort() {
122147
return sort != null && sort.isNotEmpty();
123148
}
124149

125-
private boolean hasInclude() {
126-
return include != null && include.isNotEmpty();
150+
private boolean hasLimit() {
151+
return limit != null;
152+
}
153+
154+
private boolean hasOffset() {
155+
return offset != null;
127156
}
128157

129158
private boolean hasRoot() {

services/src/test/java/org/jfrog/artifactory/client/aql/AqlQueryBuilderTest.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,31 +144,51 @@ public void orTest() {
144144
+ "]})"));
145145
}
146146

147-
148147
@Test
149148
public void matchTest() {
150149
String result = new AqlQueryBuilder()
151-
.match("repo", "myrepo*")
152-
.build();
150+
.match("repo", "myrepo*")
151+
.build();
153152

154153
assertThat(result, notNullValue());
155154
assertThat(result, is("items.find("
156-
+ "{\"repo\":"
157-
+ "{\"$match\":\"myrepo*\"}"
158-
+ "})"));
155+
+ "{\"repo\":"
156+
+ "{\"$match\":\"myrepo*\"}"
157+
+ "})"));
159158
}
160159

161160
@Test
162161
public void notMatchTest() {
163162
String result = new AqlQueryBuilder()
164-
.notMatch("repo", "myrepo*")
165-
.build();
163+
.notMatch("repo", "myrepo*")
164+
.build();
166165

167166
assertThat(result, notNullValue());
168167
assertThat(result, is("items.find("
169-
+ "{\"repo\":"
170-
+ "{\"$nmatch\":\"myrepo*\"}"
171-
+ "})"));
168+
+ "{\"repo\":"
169+
+ "{\"$nmatch\":\"myrepo*\"}"
170+
+ "})"));
171+
}
172+
173+
174+
@Test
175+
public void limitTest() {
176+
String result = new AqlQueryBuilder()
177+
.limit(123)
178+
.build();
179+
180+
assertThat(result, notNullValue());
181+
assertThat(result, is("items.find().limit(123)"));
182+
}
183+
184+
@Test
185+
public void offsetTest() {
186+
String result = new AqlQueryBuilder()
187+
.offset(123)
188+
.build();
189+
190+
assertThat(result, notNullValue());
191+
assertThat(result, is("items.find().offset(123)"));
172192
}
173193

174194
@Test
@@ -221,6 +241,8 @@ public void variousElements() {
221241
.item(aqlItem("property", "value"))
222242
.include("name", "repo")
223243
.asc("name", "repo")
244+
.offset(1)
245+
.limit(2)
224246
.build();
225247

226248
assertThat(result, notNullValue());
@@ -237,7 +259,9 @@ public void variousElements() {
237259
+ "\"property\":\"value\""
238260
+ "})"
239261
+ ".include(\"name\",\"repo\")"
240-
+ ".sort({\"$asc\":[\"name\",\"repo\"]})"));
262+
+ ".sort({\"$asc\":[\"name\",\"repo\"]})"
263+
+ ".offset(1)"
264+
+ ".limit(2)"));
241265
}
242266

243267
@Test

0 commit comments

Comments
 (0)