Skip to content

Commit

Permalink
Update QueryBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
gk-brown committed Jan 28, 2025
1 parent ec54c2e commit cd6036f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ Primary and foreign key annotations associated with the [`Actor`](kilo-test/src/
select actor.* from actor
join film_actor on actor.actor_id = film_actor.actor_id
where film_actor.film_id = :filmID
order by actor.last_name asc, actor.first_name asc
order by last_name asc, first_name asc
```

Insert, update, and delete operations are also supported. See the [pet](kilo-test/src/main/java/org/httprpc/kilo/test/PetService.java), [catalog](kilo-test/src/main/java/org/httprpc/kilo/test/CatalogService.java), and [film](kilo-test/src/main/java/org/httprpc/kilo/test/FilmService.java) service examples for more information.
Expand Down
34 changes: 30 additions & 4 deletions kilo-client/src/main/java/org/httprpc/kilo/sql/QueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,28 @@ private static String getForeignKeyColumnName(Class<?> from, Class<?> to) {
throw new UnsupportedOperationException("Foreign key is not defined.");
}

/**
* Appends a "union" operation.
*
* @param queryBuilder
* A "select" query.
*
* @return
* The {@link QueryBuilder} instance.
*/
public QueryBuilder union(QueryBuilder queryBuilder) {
if (queryBuilder == null) {
throw new IllegalArgumentException();
}

sqlBuilder.append(" union ");
sqlBuilder.append(queryBuilder);

parameters.addAll(queryBuilder.parameters);

return this;
}

/**
* Creates an "insert" query.
*
Expand Down Expand Up @@ -1040,6 +1062,10 @@ private QueryBuilder filterByIndex(String operator, String... keys) {
* The {@link QueryBuilder} instance.
*/
public QueryBuilder filterByExists(QueryBuilder queryBuilder) {
if (queryBuilder == null) {
throw new IllegalArgumentException();
}

sqlBuilder.append(" ");
sqlBuilder.append(filterCount == 0 ? WHERE : AND);
sqlBuilder.append(" exists (");
Expand All @@ -1063,6 +1089,10 @@ public QueryBuilder filterByExists(QueryBuilder queryBuilder) {
* The {@link QueryBuilder} instance.
*/
public QueryBuilder filterByNotExists(QueryBuilder queryBuilder) {
if (queryBuilder == null) {
throw new IllegalArgumentException();
}

sqlBuilder.append(" ");
sqlBuilder.append(filterCount == 0 ? WHERE : AND);
sqlBuilder.append(" not exists (");
Expand All @@ -1088,8 +1118,6 @@ public QueryBuilder filterByNotExists(QueryBuilder queryBuilder) {
public QueryBuilder ordered(boolean ascending) {
var first = types.getFirst();

var tableName = getTableName(first);

sqlBuilder.append(" order by ");

var i = 0;
Expand All @@ -1099,8 +1127,6 @@ public QueryBuilder ordered(boolean ascending) {
sqlBuilder.append(", ");
}

sqlBuilder.append(tableName);
sqlBuilder.append(".");
sqlBuilder.append(indexColumnName);
sqlBuilder.append(" ");
sqlBuilder.append(ascending ? "asc" : "desc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public void testSelectIHGF() {
+ "join H on I.h = H.h "
+ "join G on H.g = G.g "
+ "where G.f = ? "
+ "order by I.t asc, I.u asc", queryBuilder.toString());
+ "order by t asc, u asc", queryBuilder.toString());

assertEquals(listOf("f"), getParameters(queryBuilder));
}
Expand All @@ -332,7 +332,7 @@ public void testSelectIJK() {
+ "join J on I.i = J.i "
+ "join K on J.i = K.i "
+ "where I.i = ? "
+ "order by I.t desc, I.u desc", queryBuilder.toString());
+ "order by t desc, u desc", queryBuilder.toString());

assertEquals(listOf("i"), getParameters(queryBuilder));
}
Expand Down Expand Up @@ -431,6 +431,14 @@ public void testNotExists() {
assertEquals(listOf("b"), getParameters(queryBuilder));
}

@Test
public void testUnion() {
var queryBuilder = QueryBuilder.select(A.class).filterByPrimaryKey("a1").union(QueryBuilder.select(A.class).filterByPrimaryKey("a2"));

assertEquals("select A.a, A.b, A.c, A.d as x from A where A.a = ? union select A.a, A.b, A.c, A.d as x from A where A.a = ?", queryBuilder.toString());
assertEquals(listOf("a1", "a2"), getParameters(queryBuilder));
}

@Test
public void testInsert() {
var queryBuilder = QueryBuilder.insert(A.class);
Expand Down

0 comments on commit cd6036f

Please sign in to comment.