Skip to content

Commit

Permalink
#60: FIX: Make TableQueryBuilder's where(field) and and(field)
Browse files Browse the repository at this point in the history
…have equivalent meaning (#62)

This way, `where(field)` does not replace the whole filter expression
with itself, but gracefully adds query conditions instead, e.g.:
```java
return db.computeInstances().query()
     .where("id.project").eq(project)
     .where("id.name").eq(name)
     .limit(10)
     .find();
```
  • Loading branch information
nvamelichev authored Apr 17, 2024
1 parent 138ef3c commit 3b42668
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public FilterBuilder<T> generated(boolean generated) {
}

public FieldBuilder where(@NonNull String fieldPath) {
return new FieldBuilder(ModelField.of(schema.getField(fieldPath)), generated, e -> e);
return and(fieldPath);
}

public FieldBuilder and(@NonNull String fieldPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,76 @@ public void listByNamesWithUnderscores() {
});
}

@Test
public void whereAndEquivalence1() {
Project p1 = new Project(new Project.Id("uuid002"), "AAA");
Project inOutput = new Project(new Project.Id("uuid333"), "WWW");
Project p2 = new Project(new Project.Id("uuid777"), "XXX");
db.tx(() -> db.projects().insert(p1, p2, inOutput));

assertThat(db.tx(() -> db.projects().query()
.and("id").in(p1.getId(), inOutput.getId())
.where("name").in(p2.getName())
.find()
)).isEmpty();
}

@Test
public void whereAndEquivalence2() {
Project p1 = new Project(new Project.Id("uuid002"), "AAA");
Project inOutput = new Project(new Project.Id("uuid333"), "WWW");
Project p2 = new Project(new Project.Id("uuid777"), "XXX");
db.tx(() -> db.projects().insert(p1, p2, inOutput));

assertThat(db.tx(() -> db.projects().query()
.where("id").in(p1.getId(), inOutput.getId())
.where("name").in(p2.getName())
.find()
)).isEmpty();
}

@Test
public void whereAndEquivalence3() {
Project p1 = new Project(new Project.Id("uuid002"), "AAA");
Project inOutput = new Project(new Project.Id("uuid333"), "WWW");
Project p2 = new Project(new Project.Id("uuid777"), "XXX");
db.tx(() -> db.projects().insert(p1, p2, inOutput));

assertThat(db.tx(() -> db.projects().query()
.and("id").in(p1.getId(), inOutput.getId())
.and("name").in(p2.getName())
.find()
)).isEmpty();
}

@Test
public void whereAndEquivalenceWithOr1() {
Project p1 = new Project(new Project.Id("uuid002"), "AAA");
Project inOutput = new Project(new Project.Id("uuid333"), "WWW");
Project p2 = new Project(new Project.Id("uuid777"), "XXX");
db.tx(() -> db.projects().insert(p1, p2, inOutput));

assertThat(db.tx(() -> db.projects().query()
.or("name").eq(p1.getName()) // funny way to write WHERE name='...'
.where("id").eq(p2.getId()) // funny way to write ...AND id='...'
.find()
)).isEmpty();
}

@Test
public void whereAndEquivalenceWithOr2() {
Project p1 = new Project(new Project.Id("uuid002"), "AAA");
Project inOutput = new Project(new Project.Id("uuid333"), "WWW");
Project p2 = new Project(new Project.Id("uuid777"), "XXX");
db.tx(() -> db.projects().insert(p1, p2, inOutput));

assertThat(db.tx(() -> db.projects().query()
.or("name").eq(p1.getName()) // funny way to write WHERE name='...'
.and("id").eq(p2.getId()) // funny way to write ...AND id='...'
.find()
)).isEmpty();
}

protected <T extends Entity<T>> TableQueryBuilder<T> getQueryBuilder(@NonNull Table<T> table) {
return new TableQueryBuilder<>(table);
}
Expand Down

0 comments on commit 3b42668

Please sign in to comment.