-
Notifications
You must be signed in to change notification settings - Fork 221
/
ForeignKey.java
553 lines (478 loc) · 16.6 KB
/
ForeignKey.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package org.jumpmind.db.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import static org.apache.commons.lang.StringUtils.isNotBlank;
import java.io.Serializable;
import java.sql.DatabaseMetaData;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.collections.set.ListOrderedSet;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* Represents a database foreign key.
*/
public class ForeignKey implements Cloneable, Serializable {
public enum ForeignKeyAction {
UNDEFINED("UNDEFINED"),
CASCADE("CASCADE"),
NOACTION("NO ACTION"),
SETNULL("SET NULL"),
SETDEFAULT("SET DEFAULT"),
RESTRICT("RESTRICT");
private String foreignKeyActionName;
private ForeignKeyAction(String foreignKeyActionName) {
this.foreignKeyActionName = foreignKeyActionName;
}
public String getForeignKeyActionName() {
return foreignKeyActionName;
}
}
private static final long serialVersionUID = 1L;
/** The name of the foreign key, may be <code>null</code>. */
private String name;
/** The target table. */
private Table foreignTable;
/** The name of the foreign table. */
private String foreignTableName;
/** The references between local and remote columns. */
private ListOrderedSet references = new ListOrderedSet();
/** Whether this foreign key has an associated auto-generated index. */
private boolean autoIndexPresent;
private String foreignTableCatalog;
private String foreignTableSchema;
private ForeignKeyAction onDeleteAction = ForeignKeyAction.UNDEFINED;
private ForeignKeyAction onUpdateAction = ForeignKeyAction.UNDEFINED;
/**
* Creates a new foreign key object that has no name.
*/
public ForeignKey() {
this(null);
}
/**
* Creates a new foreign key object.
*
* @param name
* The name of the foreign key
*/
public ForeignKey(String name) {
this(name, null);
}
/**
* Creates a new foreign key object.
*
* @param name
* The name of the foreign key
* @param foreignKeyTableName
* The name of the foreign key table
*/
public ForeignKey(String name, String foreignKeyTableName) {
this.name = name;
this.foreignTableName = foreignKeyTableName;
}
/**
* Returns the name of this foreign key.
*
* @return The name
*/
public String getName() {
return name;
}
/**
* Sets the name of this foreign key.
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns the foreign table.
*
* @return The foreign table
*/
public Table getForeignTable() {
return foreignTable;
}
/**
* Sets the foreign table.
*
* @param foreignTable
* The foreign table
*/
public void setForeignTable(Table foreignTable) {
this.foreignTable = foreignTable;
this.foreignTableName = (foreignTable == null ? null : foreignTable.getName());
this.foreignTableCatalog = (foreignTable == null ? null : foreignTable.getCatalog());
this.foreignTableSchema = (foreignTable == null ? null : foreignTable.getSchema());
}
/**
* Returns the name of the foreign table.
*
* @return The table name
*/
public String getForeignTableName() {
return foreignTableName;
}
/**
* Sets the name of the foreign table. Please note that you should not use
* this method when manually constructing or manipulating the database
* model. Rather utilize the {@link #setForeignTable(Table)} method.
*
* @param foreignTableName
* The table name
*/
public void setForeignTableName(String foreignTableName) {
if ((foreignTable != null) && !foreignTable.getName().equals(foreignTableName)) {
foreignTable = null;
}
this.foreignTableName = foreignTableName;
}
/**
* Returns the number of references.
*
* @return The number of references
*/
public int getReferenceCount() {
return references.size();
}
/**
* Returns the indicated reference.
*
* @param idx
* The index
* @return The reference
*/
public Reference getReference(int idx) {
return (Reference) references.get(idx);
}
/**
* Returns the references.
*
* @return The references
*/
public Reference[] getReferences() {
return (Reference[]) references.toArray(new Reference[references.size()]);
}
/**
* Returns the first reference if it exists.
*
* @return The first reference
*/
public Reference getFirstReference() {
return (Reference) (references.isEmpty() ? null : references.get(0));
}
/**
* Adds a reference, ie. a mapping between a local column (in the table that
* owns this foreign key) and a remote column.
*
* @param reference
* The reference to add
*/
public void addReference(Reference reference) {
if (reference != null) {
for (int idx = 0; idx < references.size(); idx++) {
Reference curRef = getReference(idx);
if (curRef.getSequenceValue() > reference.getSequenceValue()) {
references.add(idx, reference);
return;
}
}
references.add(reference);
}
}
/**
* Removes the given reference.
*
* @param reference
* The reference to remove
*/
public void removeReference(Reference reference) {
if (reference != null) {
references.remove(reference);
}
}
/**
* Removes the indicated reference.
*
* @param idx
* The index of the reference to remove
*/
public void removeReference(int idx) {
references.remove(idx);
}
/**
* Determines whether this foreign key uses the given column as a local
* column in a reference.
*
* @param column
* The column to check
* @return <code>true</code> if a reference uses the column as a local
* column
*/
public boolean hasLocalColumn(Column column) {
for (int idx = 0; idx < getReferenceCount(); idx++) {
if (column.equals(getReference(idx).getLocalColumn())) {
return true;
}
}
return false;
}
/**
* Determines whether this foreign key uses the given column as a foreign
* column in a reference.
*
* @param column
* The column to check
* @return <code>true</code> if a reference uses the column as a foreign
* column
*/
public boolean hasForeignColumn(Column column) {
for (int idx = 0; idx < getReferenceCount(); idx++) {
if (column.equals(getReference(idx).getForeignColumn())) {
return true;
}
}
return false;
}
/**
* Determines whether this foreign key has an auto-generated associated
* index.
*
* @return <code>true</code> if an auto-generated index exists
*/
public boolean isAutoIndexPresent() {
return autoIndexPresent;
}
/**
* Specifies whether this foreign key has an auto-generated associated
* index.
*
* @param autoIndexPresent
* <code>true</code> if an auto-generated index exists
*/
public void setAutoIndexPresent(boolean autoIndexPresent) {
this.autoIndexPresent = autoIndexPresent;
}
/**
* {@inheritDoc}
*/
public Object clone() throws CloneNotSupportedException {
ForeignKey result = (ForeignKey) super.clone();
result.name = name;
result.foreignTableName = foreignTableName;
result.references = new ListOrderedSet();
result.foreignTableCatalog = foreignTableCatalog;
result.foreignTableSchema = foreignTableSchema;
result.onDeleteAction = getOnDeleteAction();
result.onUpdateAction = getOnUpdateAction();
for (Iterator<?> it = references.iterator(); it.hasNext();) {
result.references.add(((Reference) it.next()).clone());
}
return result;
}
/**
* {@inheritDoc}
*/
public boolean equals(Object obj) {
if (obj instanceof ForeignKey) {
ForeignKey otherFk = (ForeignKey) obj;
// Note that this compares case sensitive
// Note also that we can simply compare the references regardless of
// their order
// (which is irrelevant for fks) because they are contained in a set
EqualsBuilder builder = new EqualsBuilder();
if (isCheckName(otherFk)) {
builder.append(name, otherFk.name);
}
builder.append(foreignTableName, otherFk.foreignTableName);
builder.append(getOnDeleteAction(), otherFk.getOnDeleteAction());
builder.append(getOnUpdateAction(), otherFk.getOnUpdateAction());
builder.append(references.size(), otherFk.references.size());
for (int i = 0; i < references.size() && i < otherFk.references.size(); i++) {
builder.append(references.get(i), otherFk.references.get(i));
}
return builder.isEquals();
} else {
return false;
}
}
/**
* Compares this foreign key to the given one while ignoring the case of
* identifiers.
*
* @param otherFk
* The other foreign key
* @return <code>true</code> if this foreign key is equal (ignoring case) to
* the given one
*/
@SuppressWarnings("unchecked")
public boolean equalsIgnoreCase(ForeignKey otherFk) {
boolean checkName = isCheckName(otherFk);
if ((!checkName || name.equalsIgnoreCase(otherFk.name))
&& foreignTableName.equalsIgnoreCase(otherFk.foreignTableName)) {
if(! otherFk.getOnDeleteAction().equals(getOnDeleteAction())) {
return false;
}
if(! otherFk.getOnUpdateAction().equals(getOnUpdateAction())) {
return false;
}
HashSet<Reference> otherRefs = new HashSet<Reference>();
otherRefs.addAll(otherFk.references);
for (Iterator<?> it = references.iterator(); it.hasNext();) {
Reference curLocalRef = (Reference) it.next();
boolean found = false;
for (Iterator<?> otherIt = otherRefs.iterator(); otherIt.hasNext();) {
Reference curOtherRef = (Reference) otherIt.next();
if (curLocalRef.equalsIgnoreCase(curOtherRef)) {
otherIt.remove();
found = true;
break;
}
}
if (!found) {
return false;
}
}
return otherRefs.isEmpty();
} else {
return false;
}
}
private boolean isCheckName(ForeignKey otherFk) {
return name != null && name.length() > 0 && otherFk.name != null
&& otherFk.name.length() > 0;
}
/**
* {@inheritDoc}
*/
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder(17, 37).append(foreignTableName).append(
references);
if (isNotBlank(name)) {
builder.append(name);
}
builder.append(getOnDeleteAction());
builder.append(getOnUpdateAction());
return builder.toHashCode();
}
/**
* {@inheritDoc}
*/
public String toString() {
StringBuffer result = new StringBuffer();
result.append("Foreign key [");
if ((getName() != null) && (getName().length() > 0)) {
result.append("name=");
result.append(getName());
result.append("; ");
}
result.append("foreign table=");
result.append(getForeignTableName());
result.append("; ");
if(! getOnDeleteAction().equals(ForeignKeyAction.UNDEFINED)) {
result.append("ON DELETE " + getOnDeleteAction().getForeignKeyActionName()).append("; ");
}
if(! getOnUpdateAction().equals(ForeignKeyAction.UNDEFINED)) {
result.append("ON UPDATE " + getOnUpdateAction().getForeignKeyActionName()).append(";");
}
result.append(getReferenceCount());
result.append(" references]");
return result.toString();
}
/**
* Returns a verbose string representation of this foreign key.
*
* @return The string representation
*/
public String toVerboseString() {
StringBuffer result = new StringBuffer();
result.append("Foreign key [");
if ((getName() != null) && (getName().length() > 0)) {
result.append("name=");
result.append(getName());
result.append("; ");
}
result.append("foreign table=");
result.append(getForeignTableName());
result.append(";");
if(! getOnDeleteAction().equals(ForeignKeyAction.UNDEFINED)) {
result.append(" ON DELETE " + getOnDeleteAction().getForeignKeyActionName());
}
if(! getOnUpdateAction().equals(ForeignKeyAction.UNDEFINED)) {
result.append(" ON UPDATE " + getOnUpdateAction().getForeignKeyActionName());
}
result.append("] references:");
for (int idx = 0; idx < getReferenceCount(); idx++) {
result.append(" ");
result.append(getReference(idx).toString());
}
return result.toString();
}
public String getForeignTableCatalog() {
return foreignTableCatalog;
}
public void setForeignTableCatalog(String foreignTableCatalog) {
this.foreignTableCatalog = foreignTableCatalog;
}
public String getForeignTableSchema() {
return foreignTableSchema;
}
public void setForeignTableSchema(String foreignTableSchema) {
this.foreignTableSchema = foreignTableSchema;
}
public ForeignKeyAction getOnDeleteAction() {
return onDeleteAction;
}
public void setOnDeleteAction(ForeignKeyAction onDeleteAction) {
this.onDeleteAction = onDeleteAction;
}
public ForeignKeyAction getOnUpdateAction() {
return onUpdateAction;
}
public void setOnUpdateAction(ForeignKeyAction onUpdateAction) {
this.onUpdateAction = onUpdateAction;
}
public static ForeignKeyAction getForeignKeyAction(short importedKeyAction) {
switch(importedKeyAction) {
case DatabaseMetaData.importedKeyCascade:
return ForeignKeyAction.CASCADE;
case DatabaseMetaData.importedKeyNoAction:
return ForeignKeyAction.NOACTION;
case DatabaseMetaData.importedKeyRestrict:
return ForeignKeyAction.RESTRICT;
case DatabaseMetaData.importedKeySetDefault:
return ForeignKeyAction.SETDEFAULT;
case DatabaseMetaData.importedKeySetNull:
return ForeignKeyAction.SETNULL;
default:
return ForeignKeyAction.UNDEFINED;
}
}
public static ForeignKeyAction getForeignKeyActionByForeignKeyActionName(String foreignKeyActionName) throws IllegalArgumentException {
for(ForeignKeyAction action : ForeignKeyAction.values()) {
if(StringUtils.equals(foreignKeyActionName, action.getForeignKeyActionName())) {
return action;
}
}
throw new IllegalArgumentException("Unknown ForeignKeyAction: " + foreignKeyActionName);
}
}