1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import com.healthmarketscience.jackcess.impl.DatabaseImpl;
24 import com.healthmarketscience.jackcess.impl.RelationshipCreator;
25 import com.healthmarketscience.jackcess.impl.RelationshipImpl;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class RelationshipBuilder
50 {
51 private static final int JOIN_FLAGS =
52 RelationshipImpl.LEFT_OUTER_JOIN_FLAG |
53 RelationshipImpl.RIGHT_OUTER_JOIN_FLAG;
54
55
56 private int _flags = RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG;
57 private String _fromTable;
58 private String _toTable;
59 private List<String> _fromCols = new ArrayList<String>();
60 private List<String> _toCols = new ArrayList<String>();
61 private String _name = null;
62
63 public RelationshipBuilder(Table../../../com/healthmarketscience/jackcess/Table.html#Table">Table fromTable, Table toTable) {
64 this(fromTable.getName(), toTable.getName());
65 }
66
67 public RelationshipBuilder(String fromTable, String toTable) {
68 _fromTable = fromTable;
69 _toTable = toTable;
70 }
71
72
73
74
75 public RelationshipBuilder addColumns(String fromCol, String toCol) {
76 _fromCols.add(fromCol);
77 _toCols.add(toCol);
78 return this;
79 }
80
81
82
83
84 public RelationshipBuilder addColumns(Column"../../../com/healthmarketscience/jackcess/Column.html#Column">Column fromCol, Column toCol) {
85 return addColumns(fromCol.getName(), toCol.getName());
86 }
87
88
89
90
91
92
93
94 public RelationshipBuilder setReferentialIntegrity() {
95 return clearFlag(RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG);
96 }
97
98
99
100
101
102
103 public RelationshipBuilder setCascadeDeletes() {
104 return setFlag(RelationshipImpl.CASCADE_DELETES_FLAG);
105 }
106
107
108
109
110
111
112 public RelationshipBuilder setCascadeUpdates() {
113 return setFlag(RelationshipImpl.CASCADE_UPDATES_FLAG);
114 }
115
116
117
118
119
120
121
122 public RelationshipBuilder setCascadeNullOnDelete() {
123 return setFlag(RelationshipImpl.CASCADE_NULL_FLAG);
124 }
125
126
127
128
129 public RelationshipBuilder setJoinType(Relationship.JoinType joinType) {
130 clearFlag(JOIN_FLAGS);
131 switch(joinType) {
132 case INNER:
133
134 break;
135 case LEFT_OUTER:
136 _flags |= RelationshipImpl.LEFT_OUTER_JOIN_FLAG;
137 break;
138 case RIGHT_OUTER:
139 _flags |= RelationshipImpl.RIGHT_OUTER_JOIN_FLAG;
140 break;
141 default:
142 throw new RuntimeException("unexpected join type " + joinType);
143 }
144 return this;
145 }
146
147
148
149
150
151
152 public RelationshipBuilder setName(String relationshipName) {
153 _name = relationshipName;
154 return this;
155 }
156
157 public boolean hasReferentialIntegrity() {
158 return !hasFlag(RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG);
159 }
160
161 public int getFlags() {
162 return _flags;
163 }
164
165 public String getFromTable() {
166 return _fromTable;
167 }
168
169 public String getToTable() {
170 return _toTable;
171 }
172
173 public List<String> getFromColumns() {
174 return _fromCols;
175 }
176
177 public List<String> getToColumns() {
178 return _toCols;
179 }
180
181 public String getName() {
182 return _name;
183 }
184
185
186
187
188
189 public Relationship toRelationship(Database db)
190 throws IOException
191 {
192 return new RelationshipCreator((DatabaseImpl)db).createRelationship(this);
193 }
194
195 private RelationshipBuilder setFlag(int flagMask) {
196 _flags |= flagMask;
197 return this;
198 }
199
200 private RelationshipBuilder clearFlag(int flagMask) {
201 _flags &= ~flagMask;
202 return this;
203 }
204
205 private boolean hasFlag(int flagMask) {
206 return((_flags & flagMask) != 0);
207 }
208
209 }