1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.healthmarketscience.jackcess.impl;
18
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.ArrayList;
22
23 import com.healthmarketscience.jackcess.Column;
24 import com.healthmarketscience.jackcess.Relationship;
25 import com.healthmarketscience.jackcess.Table;
26
27
28
29
30
31
32 public class RelationshipImpl implements Relationship
33 {
34
35
36 public static final int ONE_TO_ONE_FLAG = 0x00000001;
37
38 public static final int NO_REFERENTIAL_INTEGRITY_FLAG = 0x00000002;
39
40 public static final int CASCADE_UPDATES_FLAG = 0x00000100;
41
42 public static final int CASCADE_DELETES_FLAG = 0x00001000;
43
44
45 public static final int CASCADE_NULL_FLAG = 0x00002000;
46
47 public static final int LEFT_OUTER_JOIN_FLAG = 0x01000000;
48
49 public static final int RIGHT_OUTER_JOIN_FLAG = 0x02000000;
50
51
52 private final String _name;
53
54 private final Table _fromTable;
55
56 private final Table _toTable;
57
58
59 private final List<Column> _toColumns;
60
61
62 private final List<Column> _fromColumns;
63
64 private final int _flags;
65
66 public RelationshipImpl(String name, Table../../../../com/healthmarketscience/jackcess/Table.html#Table">Table fromTable, Table toTable, int flags,
67 int numCols)
68 {
69 this(name, fromTable, toTable, flags,
70 Collections.nCopies(numCols, (Column)null),
71 Collections.nCopies(numCols, (Column)null));
72 }
73
74 public RelationshipImpl(String name, Table../../../../com/healthmarketscience/jackcess/Table.html#Table">Table fromTable, Table toTable, int flags,
75 List<? extends Column> fromCols,
76 List<? extends Column> toCols)
77 {
78 _name = name;
79 _fromTable = fromTable;
80 _fromColumns = new ArrayList<Column>(fromCols);
81 _toTable = toTable;
82 _toColumns = new ArrayList<Column>(toCols);
83 _flags = flags;
84 }
85
86 @Override
87 public String getName() {
88 return _name;
89 }
90
91 @Override
92 public Table getFromTable() {
93 return _fromTable;
94 }
95
96 @Override
97 public List<Column> getFromColumns() {
98 return _fromColumns;
99 }
100
101 @Override
102 public Table getToTable() {
103 return _toTable;
104 }
105
106 @Override
107 public List<Column> getToColumns() {
108 return _toColumns;
109 }
110
111 public int getFlags() {
112 return _flags;
113 }
114
115 @Override
116 public boolean isOneToOne() {
117 return hasFlag(ONE_TO_ONE_FLAG);
118 }
119
120 @Override
121 public boolean hasReferentialIntegrity() {
122 return !hasFlag(NO_REFERENTIAL_INTEGRITY_FLAG);
123 }
124
125 @Override
126 public boolean cascadeUpdates() {
127 return hasFlag(CASCADE_UPDATES_FLAG);
128 }
129
130 @Override
131 public boolean cascadeDeletes() {
132 return hasFlag(CASCADE_DELETES_FLAG);
133 }
134
135 @Override
136 public boolean cascadeNullOnDelete() {
137 return hasFlag(CASCADE_NULL_FLAG);
138 }
139
140 @Override
141 public boolean isLeftOuterJoin() {
142 return hasFlag(LEFT_OUTER_JOIN_FLAG);
143 }
144
145 @Override
146 public boolean isRightOuterJoin() {
147 return hasFlag(RIGHT_OUTER_JOIN_FLAG);
148 }
149
150 @Override
151 public JoinType getJoinType() {
152 if(isLeftOuterJoin()) {
153 return JoinType.LEFT_OUTER;
154 } else if(isRightOuterJoin()) {
155 return JoinType.RIGHT_OUTER;
156 }
157 return JoinType.INNER;
158 }
159
160 private boolean hasFlag(int flagMask) {
161 return((getFlags() & flagMask) != 0);
162 }
163
164 @Override
165 public String toString() {
166 return CustomToStringStyle.builder(this)
167 .append("name", _name)
168 .append("fromTable", _fromTable.getName())
169 .append("fromColumns", _fromColumns)
170 .append("toTable", _toTable.getName())
171 .append("toColumns", _toColumns)
172 .append("flags", Integer.toHexString(_flags))
173 .toString();
174 }
175
176 }