Skip to content

Commit 405131e

Browse files
committed
Add support for subqueries.
1 parent c138132 commit 405131e

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,9 @@ String sql = QueryBuilder.select("name", "species", "sex", "birth")
798798
.where("owner = :owner").toString();
799799
```
800800

801-
Insert, update, and delete operations are also supported. In general, string values provided to insert and update statements via the `values()` and `set()` methods are wrapped in single quotes, and any embdedded single quotes are replaced with two successive single quotes. However, any string that starts with ":" or is equal to "?" is assumed to be a parameter reference and is not escaped.
801+
Insert, update, and delete operations are also supported. In general, string values provided to the `insertInto()` and `set()` methods are wrapped in single quotes, and any embdedded single quotes are replaced with two successive single quotes. However, any string that starts with ":" or is equal to "?" is assumed to be a parameter reference and is not escaped.
802+
803+
If an instance of `QueryBuilder` is passed to `insertInto()` or `set()`, it is considered a subquery and wrapped in parentheses.
802804

803805
## ElementAdapter
804806
The `ElementAdapter` class provides access to the contents of an XML DOM `Element` via the `Map` interface. The resulting map can then be transformed to another representation via a template document or accessed via a strongly typed interface proxy, as described earlier.

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,15 @@ public static QueryBuilder insertInto(String table, Map<String, ?> values) {
267267
sqlBuilder.append(", ");
268268
}
269269

270-
sqlBuilder.append(encode(values.get(columns.get(i))));
270+
Object value = values.get(columns.get(i));
271+
272+
if (value instanceof QueryBuilder) {
273+
sqlBuilder.append("(");
274+
sqlBuilder.append(value.toString());
275+
sqlBuilder.append(")");
276+
} else {
277+
sqlBuilder.append(encode(value));
278+
}
271279
}
272280

273281
sqlBuilder.append(")");
@@ -322,7 +330,16 @@ public QueryBuilder set(Map<String, ?> values) {
322330

323331
sqlBuilder.append(entry.getKey());
324332
sqlBuilder.append(" = ");
325-
sqlBuilder.append(encode(entry.getValue()));
333+
334+
Object value = entry.getValue();
335+
336+
if (value instanceof QueryBuilder) {
337+
sqlBuilder.append("(");
338+
sqlBuilder.append(value.toString());
339+
sqlBuilder.append(")");
340+
} else {
341+
sqlBuilder.append(encode(value));
342+
}
326343

327344
i++;
328345
}
@@ -372,7 +389,6 @@ private static String encode(Object value) {
372389

373390
return stringBuilder.toString();
374391
}
375-
376392
} else {
377393
return String.valueOf(value);
378394
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ public void testInsertInto() {
5050
entry("b", true),
5151
entry("c", "hello"),
5252
entry("d", ":d"),
53-
entry("e", "?")
53+
entry("e", "?"),
54+
entry("f", QueryBuilder.select("f").from("F"))
5455
)).toString();
5556

56-
assertEquals("insert into A (a, b, c, d, e) values (1, true, 'hello', :d, ?)", sql);
57+
assertEquals("insert into A (a, b, c, d, e, f) values (1, true, 'hello', :d, ?, (select f from F))", sql);
5758
}
5859

5960
@Test
@@ -63,10 +64,11 @@ public void testUpdate() {
6364
entry("b", true),
6465
entry("c", "hello"),
6566
entry("d", ":d"),
66-
entry("e", "?")
67+
entry("e", "?"),
68+
entry("f", QueryBuilder.select("f").from("F"))
6769
)).where("a is not null").toString();
6870

69-
assertEquals("update A set a = 1, b = true, c = 'hello', d = :d, e = ? where a is not null", sql);
71+
assertEquals("update A set a = 1, b = true, c = 'hello', d = :d, e = ?, f = (select f from F) where a is not null", sql);
7072
}
7173

7274
@Test

0 commit comments

Comments
 (0)