Skip to content

Commit 49df3e4

Browse files
authored
Merge pull request #51 from jeffgbutler/master
Add support for Union All
2 parents 5ac7c3d + fb82006 commit 49df3e4

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ public SelectDSL<R> orderBy(SortSpecification...columns) {
124124

125125
public UnionBuilder union() {
126126
selectDSL.addQueryExpression(buildModel());
127-
return new UnionBuilder();
127+
return new UnionBuilder("union"); //$NON-NLS-1$
128+
}
129+
130+
public UnionBuilder unionAll() {
131+
selectDSL.addQueryExpression(buildModel());
132+
return new UnionBuilder("union all"); //$NON-NLS-1$
128133
}
129134

130135
protected QueryExpressionModel buildModel() {
@@ -206,7 +211,13 @@ private <T> QueryExpressionWhereBuilder(BindableColumn<T> column, VisitableCondi
206211
public UnionBuilder union() {
207212
whereModel = buildWhereModel();
208213
selectDSL.addQueryExpression(buildModel());
209-
return new UnionBuilder();
214+
return new UnionBuilder("union"); //$NON-NLS-1$
215+
}
216+
217+
public UnionBuilder unionAll() {
218+
whereModel = buildWhereModel();
219+
selectDSL.addQueryExpression(buildModel());
220+
return new UnionBuilder("union all"); //$NON-NLS-1$
210221
}
211222

212223
public SelectDSL<R> orderBy(SortSpecification...columns) {
@@ -388,17 +399,23 @@ public R build() {
388399
}
389400

390401
public class UnionBuilder {
402+
private String connector;
403+
404+
public UnionBuilder(String connector) {
405+
this.connector = Objects.requireNonNull(connector);
406+
}
407+
391408
public FromGatherer<R> select(BasicColumn...selectList) {
392409
return new FromGathererBuilder<R>()
393-
.withConnector("union") //$NON-NLS-1$
410+
.withConnector(connector)
394411
.withSelectList(selectList)
395412
.withSelectDSL(selectDSL)
396413
.build();
397414
}
398415

399416
public FromGatherer<R> selectDistinct(BasicColumn...selectList) {
400417
return new FromGathererBuilder<R>()
401-
.withConnector("union") //$NON-NLS-1$
418+
.withConnector(connector)
402419
.isDistinct()
403420
.withSelectList(selectList)
404421
.withSelectDSL(selectDSL)

src/site/markdown/docs/select.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ select statements, but purposely does not cover every possibility.
66
In general, the following are supported:
77

88
1. The typical parts of a select statement including SELECT, DISTINCT, FROM, JOIN, WHERE, GROUP BY, UNION,
9-
ORDER BY
9+
UNION ALL, ORDER BY
1010
2. Tables can be aliased per select statement
1111
3. Columns can be aliased per select statement
1212
4. Some support for aggregates (avg, min, max, sum)
@@ -70,7 +70,7 @@ The library supports four join types:
7070
4. `.fullJoin(...)` is a FULL OUTER join
7171

7272
## Union Queries
73-
The library supports the generation of UNION queries. Foe example:
73+
The library supports the generation of UNION and UNION ALL queries. For example:
7474

7575
```java
7676
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)

src/test/java/examples/animal/data/AnimalDataTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,36 @@ public void testUnionSelectWithoutWhere() {
287287
}
288288
}
289289

290+
@Test
291+
public void testUnionAllSelectWithoutWhere() {
292+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
293+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
294+
295+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
296+
.from(animalData)
297+
.unionAll()
298+
.selectDistinct(id, animalName, bodyWeight, brainWeight)
299+
.from(animalData)
300+
.orderBy(id)
301+
.build()
302+
.render(RenderingStrategy.MYBATIS3);
303+
304+
String expected = "select id, animal_name, body_weight, brain_weight "
305+
+ "from AnimalData "
306+
+ "union all "
307+
+ "select distinct id, animal_name, body_weight, brain_weight "
308+
+ "from AnimalData "
309+
+ "order by id";
310+
311+
List<AnimalData> animals = mapper.selectMany(selectStatement);
312+
assertAll(
313+
() -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected),
314+
() -> assertThat(animals.size()).isEqualTo(130),
315+
() -> assertThat(selectStatement.getParameters().size()).isEqualTo(0)
316+
);
317+
}
318+
}
319+
290320
@Test
291321
public void testUnionSelectWithTableAliases() {
292322
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
@@ -324,6 +354,43 @@ public void testUnionSelectWithTableAliases() {
324354
}
325355
}
326356

357+
@Test
358+
public void testUnionAllSelectWithTableAliases() {
359+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
360+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
361+
362+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
363+
.from(animalData, "a")
364+
.where(id, isLessThan(20))
365+
.unionAll()
366+
.select(id, animalName, bodyWeight, brainWeight)
367+
.from(animalData, "b")
368+
.where(id, isGreaterThan(40))
369+
.orderBy(id)
370+
.build()
371+
.render(RenderingStrategy.MYBATIS3);
372+
373+
String expected = "select a.id, a.animal_name, a.body_weight, a.brain_weight "
374+
+ "from AnimalData a "
375+
+ "where a.id < #{parameters.p1,jdbcType=INTEGER} "
376+
+ "union all "
377+
+ "select b.id, b.animal_name, b.body_weight, b.brain_weight "
378+
+ "from AnimalData b "
379+
+ "where b.id > #{parameters.p2,jdbcType=INTEGER} "
380+
+ "order by id";
381+
382+
List<AnimalData> animals = mapper.selectMany(selectStatement);
383+
384+
assertAll(
385+
() -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expected),
386+
() -> assertThat(animals.size()).isEqualTo(44),
387+
() -> assertThat(selectStatement.getParameters().size()).isEqualTo(2),
388+
() -> assertThat(selectStatement.getParameters().get("p1")).isEqualTo(20),
389+
() -> assertThat(selectStatement.getParameters().get("p2")).isEqualTo(40)
390+
);
391+
}
392+
}
393+
327394
@Test
328395
public void testUnionSelectWithTableAndColumnAliases() {
329396
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

0 commit comments

Comments
 (0)