1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess.impl.query;
18
19 import java.util.List;
20
21 import static com.healthmarketscience.jackcess.impl.query.QueryFormat.*;
22 import com.healthmarketscience.jackcess.query.UnionQuery;
23
24
25
26
27
28
29
30
31 public class UnionQueryImpl extends QueryImpl implements UnionQuery
32 {
33 public UnionQueryImpl(String name, List<Row> rows, int objectId,
34 int objectFlag) {
35 super(name, rows, objectId, objectFlag, Type.UNION);
36 }
37
38 @Override
39 public String getUnionType() {
40 return(hasFlag(UNION_FLAG) ? DEFAULT_TYPE : "ALL");
41 }
42
43 @Override
44 public String getUnionString1() {
45 return getUnionString(UNION_PART1);
46 }
47
48 @Override
49 public String getUnionString2() {
50 return getUnionString(UNION_PART2);
51 }
52
53 @Override
54 public List<String> getOrderings() {
55 return super.getOrderings();
56 }
57
58 private String getUnionString(String id) {
59 for(Row row : getTableRows()) {
60 if(id.equals(row.name2)) {
61 return cleanUnionString(row.expression);
62 }
63 }
64 throw new IllegalStateException(
65 "Could not find union query with id " + id);
66 }
67
68 @Override
69 protected void toSQLString(StringBuilder builder)
70 {
71 builder.append(getUnionString1()).append(NEWLINE)
72 .append("UNION ");
73 String unionType = getUnionType();
74 if(!DEFAULT_TYPE.equals(unionType)) {
75 builder.append(unionType).append(' ');
76 }
77 builder.append(getUnionString2());
78 List<String> orderings = getOrderings();
79 if(!orderings.isEmpty()) {
80 builder.append(NEWLINE).append("ORDER BY ").append(orderings);
81 }
82 }
83
84 private static String cleanUnionString(String str)
85 {
86 return str.trim().replaceAll("[\r\n]+", NEWLINE);
87 }
88
89 }