Skip to content

Commit db62367

Browse files
committed
Add support for left and right joins to QueryBuilder.
1 parent 8e2babc commit db62367

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

httprpc-client/src/main/java/org/httprpc/sql/QueryBuilder.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,46 @@ public QueryBuilder join(String table) {
139139
return this;
140140
}
141141

142+
/**
143+
* Appends a "left join" clause to a query.
144+
*
145+
* @param table
146+
* The table name.
147+
*
148+
* @return
149+
* The {@link QueryBuilder} instance.
150+
*/
151+
public QueryBuilder leftJoin(String table) {
152+
if (table == null) {
153+
throw new IllegalArgumentException();
154+
}
155+
156+
sqlBuilder.append(" left join ");
157+
sqlBuilder.append(table);
158+
159+
return this;
160+
}
161+
162+
/**
163+
* Appends a "right join" clause to a query.
164+
*
165+
* @param table
166+
* The table name.
167+
*
168+
* @return
169+
* The {@link QueryBuilder} instance.
170+
*/
171+
public QueryBuilder rightJoin(String table) {
172+
if (table == null) {
173+
throw new IllegalArgumentException();
174+
}
175+
176+
sqlBuilder.append(" right join ");
177+
sqlBuilder.append(table);
178+
179+
return this;
180+
}
181+
142182
/**
143183
* Appends an "on" clause to a query.
144184
*

httprpc-client/src/test/java/org/httprpc/sql/QueryBuilderTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ public void testSelect() {
2424
String sql = QueryBuilder.select("a", "b", "c")
2525
.from("A")
2626
.join("B").on("A.id = B.id and x = 50")
27-
.join("C").on("B.id = C.id")
27+
.leftJoin("C").on("B.id = C.id")
28+
.rightJoin("D").on("C.id = D.id")
2829
.where("(a > 10 or b < 200) and c = :c")
2930
.orderBy("a", "b").toString();
3031

3132
assertEquals("select a, b, c from A "
3233
+ "join B on A.id = B.id and x = 50 "
33-
+ "join C on B.id = C.id "
34+
+ "left join C on B.id = C.id "
35+
+ "right join D on C.id = D.id "
3436
+ "where (a > 10 or b < 200) and c = :c "
3537
+ "order by a, b", sql);
3638
}

0 commit comments

Comments
 (0)