Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class AqlQueryBuilder {
private AqlRootElement root = new AqlRootElement();
private AqlItem sort;
private AqlInclude include;
private Integer limit;
private Integer offset;

public AqlQueryBuilder item(AqlItem item) {
root.putAll(item.value());
Expand All @@ -26,9 +28,9 @@ public AqlQueryBuilder item(AqlItem item) {
public AqlQueryBuilder elements(AqlItem... items) {
if (isNotEmpty(items)) {
root.putAll(Arrays.stream(items)
.map(item -> item.value().entrySet())
.flatMap(Collection::stream)
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
.map(item -> item.value().entrySet())
.flatMap(Collection::stream)
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
return this;
}
Expand Down Expand Up @@ -97,10 +99,21 @@ public AqlQueryBuilder desc(String... by) {
return this;
}

public AqlQueryBuilder limit(int limit) {
this.limit = limit;
return this;
}

public AqlQueryBuilder offset(int offset) {
this.offset = offset;
return this;
}

public String build() {
try {
ObjectMapper mapper = new ObjectMapper();
return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString(mapper);
return "items.find(" + getRootAsString(mapper) + ")" + getIncludeAsString() + getSortAsString(
mapper) + getOffsetAsString(mapper) + getLimitAsString(mapper);
} catch (JsonProcessingException e) {
throw new AqlBuilderException("Error serializing object to json: ", e);
}
Expand All @@ -114,16 +127,32 @@ private String getIncludeAsString() {
return hasInclude() ? include.toString() : "";
}

private String getOffsetAsString(ObjectMapper mapper) throws JsonProcessingException {
return hasOffset() ? ".offset(" + mapper.writeValueAsString(offset) + ")" : "";
}

private String getLimitAsString(ObjectMapper mapper) throws JsonProcessingException {
return hasLimit() ? ".limit(" + mapper.writeValueAsString(limit) + ")" : "";
}

private String getRootAsString(ObjectMapper mapper) throws JsonProcessingException {
return hasRoot() ? mapper.writeValueAsString(root) : "";
}

private boolean hasInclude() {
return include != null && include.isNotEmpty();
}

private boolean hasSort() {
return sort != null && sort.isNotEmpty();
}

private boolean hasInclude() {
return include != null && include.isNotEmpty();
private boolean hasLimit() {
return limit != null;
}

private boolean hasOffset() {
return offset != null;
}

private boolean hasRoot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,31 +144,51 @@ public void orTest() {
+ "]})"));
}


@Test
public void matchTest() {
String result = new AqlQueryBuilder()
.match("repo", "myrepo*")
.build();
.match("repo", "myrepo*")
.build();

assertThat(result, notNullValue());
assertThat(result, is("items.find("
+ "{\"repo\":"
+ "{\"$match\":\"myrepo*\"}"
+ "})"));
+ "{\"repo\":"
+ "{\"$match\":\"myrepo*\"}"
+ "})"));
}

@Test
public void notMatchTest() {
String result = new AqlQueryBuilder()
.notMatch("repo", "myrepo*")
.build();
.notMatch("repo", "myrepo*")
.build();

assertThat(result, notNullValue());
assertThat(result, is("items.find("
+ "{\"repo\":"
+ "{\"$nmatch\":\"myrepo*\"}"
+ "})"));
+ "{\"repo\":"
+ "{\"$nmatch\":\"myrepo*\"}"
+ "})"));
}


@Test
public void limitTest() {
String result = new AqlQueryBuilder()
.limit(123)
.build();

assertThat(result, notNullValue());
assertThat(result, is("items.find().limit(123)"));
}

@Test
public void offsetTest() {
String result = new AqlQueryBuilder()
.offset(123)
.build();

assertThat(result, notNullValue());
assertThat(result, is("items.find().offset(123)"));
}

@Test
Expand Down Expand Up @@ -221,6 +241,8 @@ public void variousElements() {
.item(aqlItem("property", "value"))
.include("name", "repo")
.asc("name", "repo")
.offset(1)
.limit(2)
.build();

assertThat(result, notNullValue());
Expand All @@ -237,7 +259,9 @@ public void variousElements() {
+ "\"property\":\"value\""
+ "})"
+ ".include(\"name\",\"repo\")"
+ ".sort({\"$asc\":[\"name\",\"repo\"]})"));
+ ".sort({\"$asc\":[\"name\",\"repo\"]})"
+ ".offset(1)"
+ ".limit(2)"));
}

@Test
Expand Down