01: package simpleorm.data;
02:
03: /**
04: * Reprents a foreign key between two SFieldMetas.<p>
05: *
06: * Unlike SimpleORM, they are locical, not conceptual -- ie. the foreign key
07: * columns need to be manually declared in the referencing table.
08: * This is cruder, but simpler.
09: */
10: public class DForeignKeyMeta {
11: DRecordMeta refingRecord;
12: DRecordMeta refedRecord;
13: DReference[] refs;
14: String name; // tableName_# -- does not change and short for dbs with 20 char idents.
15:
16: public static class DReference {
17: DFieldMeta refing, refed;
18:
19: public DReference(DFieldMeta refing, DFieldMeta refed) {
20: this .refing = refing;
21: this .refed = refed;
22: }
23:
24: public DFieldMeta getRefing() {
25: return refing;
26: }
27:
28: public DFieldMeta getRefed() {
29: return refed;
30: }
31: }
32:
33: DForeignKeyMeta(DRecordMeta refedRecord, DFieldMeta[] refingFields) {
34: this .refedRecord = refedRecord;
35: refs = new DReference[refingFields.length];
36: int rx = 0;
37: for (DFieldMeta refing : refingFields) {
38: if (refingRecord == null)
39: refingRecord = refing.getRecordMeta();
40: else if (refingRecord != refing.getRecordMeta())
41: throw new DException("Inconsistent Foreign key fields "
42: + refingRecord + refing.getRecordMeta());
43: DFieldMeta refed = refedRecord.primaryKeyField(rx);
44: if (!refed.getType().equals(refing.getType()))
45: throw new DException("Inconsistent FKey types "
46: + refing + refed);
47: refs[rx] = new DReference(refing, refed);
48: rx++;
49: }
50: }
51:
52: public DRecordMeta getRefingRecord() {
53: return refingRecord;
54: }
55:
56: public DRecordMeta getRefedRecord() {
57: return refedRecord;
58: }
59:
60: public DReference[] getRefs() {
61: return refs;
62: }
63:
64: public String getName() {
65: return name;
66: }
67: }
|