1 10 11 package com.triactive.jdo.store; 12 13 import java.util.ArrayList ; 14 import javax.jdo.JDOFatalInternalException; 15 16 17 class ForeignKey extends Key 18 { 19 private final boolean initiallyDeferred; 20 private BaseTable refTable; 21 private DatabaseAdapter dba; 22 23 private ArrayList refColumns = new ArrayList (); 24 25 26 public ForeignKey(boolean initiallyDeferred) 27 { 28 super(null); 29 30 this.initiallyDeferred = initiallyDeferred; 31 this.refTable = null; 32 this.dba = null; 33 } 34 35 36 public ForeignKey(Column col, ClassBaseTable refTable, boolean initiallyDeferred) 37 { 38 super((BaseTable)col.getTable()); 39 this.initiallyDeferred = initiallyDeferred; 40 this.refTable = refTable; 41 this.dba = table.getStoreManager().getDatabaseAdapter(); 42 43 setColumn(0, col, refTable.getIDMapping().getColumn()); 44 } 45 46 47 public void addColumn(Column col, Column refCol) 48 { 49 setColumn(columns.size(), col, refCol); 50 } 51 52 53 public void setColumn(int seq, Column col, Column refCol) 54 { 55 if (table == null) 56 { 57 table = (BaseTable)col.getTable(); 58 refTable = (BaseTable)refCol.getTable(); 59 dba = table.getStoreManager().getDatabaseAdapter(); 60 } 61 else 62 { 63 if (!table.equals(col.getTable())) 64 throw new JDOFatalInternalException("Cannot add " + col + " as FK column for " + table); 65 if (!refTable.equals(refCol.getTable())) 66 throw new JDOFatalInternalException("Cannot add " + refCol + " as referenced FK column for " + refTable); 67 } 68 69 setMinSize(columns, seq + 1); 70 setMinSize(refColumns, seq + 1); 71 72 columns.set(seq, col); 73 refColumns.set(seq, refCol); 74 } 75 76 77 public int hashCode() 78 { 79 return columns.hashCode() ^ refColumns.hashCode(); 80 } 81 82 83 public boolean equals(Object o) 84 { 85 if (o == this) 86 return true; 87 88 if (!(o instanceof ForeignKey)) 89 return false; 90 91 ForeignKey fk = (ForeignKey)o; 92 93 return columns.equals(fk.columns) && refColumns.equals(fk.refColumns); 94 } 95 96 97 public String toString() 98 { 99 StringBuffer s = new StringBuffer ("FOREIGN KEY ") 100 .append(getColumnList(columns)) 101 .append(" REFERENCES ") 102 .append(refTable.getName()); 103 104 109 s.append(' ').append(getColumnList(refColumns)); 111 112 if (initiallyDeferred) 113 { 114 if (dba.supportsDeferredConstraints()) 115 s.append(" INITIALLY DEFERRED"); 116 } 117 118 return s.toString(); 119 } 120 } 121 | Popular Tags |