1 19 20 package org.netbeans.modules.dbschema; 21 22 24 public final class ForeignKeyElement extends KeyElement implements ReferenceKey, ColumnPairElementHolder { 25 27 public ForeignKeyElement() { 28 this(new Memory(), null); 29 } 30 31 36 public ForeignKeyElement(Impl impl, TableElement declaringTable) { 37 super(impl, declaringTable); 38 } 39 40 43 final Impl getForeignKeyImpl() { 44 return (Impl)getElementImpl(); 45 } 46 47 50 public TableElement getReferencedTable() { 51 ColumnPairElement[] columnPairs = getColumnPairs(); 52 53 if ((columnPairs != null) && (columnPairs.length > 0)) 54 return columnPairs[0].getReferencedColumn().getDeclaringTable(); 55 56 return null; 57 } 58 59 62 public ColumnElement[] getReferencedColumns() { 63 ColumnPairElement[] columnPairs = getColumnPairs(); 64 int count = ((columnPairs != null) ? columnPairs.length : 0); 65 ColumnElement[] ce = new ColumnElement[count]; 66 67 for (int i = 0; i < count; i++) 68 ce[i] = columnPairs[i].getReferencedColumn(); 69 70 return ce; 71 } 72 73 76 public String getKeyName() { 77 return getName().getFullName(); 78 } 79 80 84 public void setKeyName(String name) throws DBException { 85 setName(DBIdentifier.create(name)); 86 } 87 88 92 public void addColumnPair(ColumnPairElement pair) throws DBException { 93 addColumnPairs(new ColumnPairElement[] {pair}); 94 } 95 96 100 public void addColumnPairs(ColumnPairElement[] pairs) throws DBException { 101 getForeignKeyImpl().changeColumnPairs(pairs, Impl.ADD); 102 } 103 104 108 public void removeColumnPair(ColumnPairElement pair) throws DBException { 109 removeColumnPairs(new ColumnPairElement[] {pair}); 110 } 111 112 116 public void removeColumnPairs(ColumnPairElement[] pairs) throws DBException { 117 getForeignKeyImpl().changeColumnPairs(pairs, Impl.REMOVE); 118 } 119 120 124 public void setColumnPairs(ColumnPairElement[] pairs) throws DBException { 125 getForeignKeyImpl().changeColumnPairs(pairs, Impl.SET); 126 } 127 128 131 public ColumnPairElement[] getColumnPairs() { 132 return getForeignKeyImpl().getColumnPairs(); 133 } 134 135 139 public ColumnPairElement getColumnPair(DBIdentifier name) { 140 return getForeignKeyImpl().getColumnPair(name); 141 } 142 143 146 public ColumnElement[] getLocalColumns() { 147 return getForeignKeyImpl().getColumns(); 148 } 149 150 152 156 public void addColumn (ColumnElement el) throws UnsupportedOperationException { 157 throw new UnsupportedOperationException (); 158 } 159 160 164 public void addColumns (ColumnElement[] els) throws UnsupportedOperationException { 165 throw new UnsupportedOperationException (); 166 } 167 168 172 public void removeColumn (ColumnElement el) throws UnsupportedOperationException { 173 throw new UnsupportedOperationException (); 174 } 175 176 180 public void removeColumns (ColumnElement[] els) throws UnsupportedOperationException { 181 throw new UnsupportedOperationException (); 182 } 183 184 189 public void setColumns (ColumnElement[] els) throws UnsupportedOperationException { 190 throw new UnsupportedOperationException (); 191 } 192 193 195 198 public ColumnElement[] getColumns () { 199 return getForeignKeyImpl().getColumns(); 200 } 201 202 206 public ColumnElement getColumn (DBIdentifier name) { 207 return getForeignKeyImpl().getColumn(name); 208 } 209 210 211 214 public interface Impl extends KeyElement.Impl { 215 219 public void changeColumnPairs(ColumnPairElement[] pairs, int action) throws DBException; 220 221 224 public ColumnPairElement[] getColumnPairs(); 225 226 230 public ColumnPairElement getColumnPair(DBIdentifier name); 231 } 232 233 static class Memory extends KeyElement.Memory implements Impl { 234 235 private DBMemoryCollection.ColumnPair pairs; 236 237 239 Memory() { 240 super(); 241 } 242 243 246 Memory(ForeignKeyElement fk) { 247 super(fk); 248 } 249 250 253 public synchronized ColumnPairElement[] getColumnPairs() { 254 initColumnPairs(); 255 return (ColumnPairElement[]) pairs.getElements(); 256 } 257 258 262 public synchronized ColumnPairElement getColumnPair(DBIdentifier name) { 263 initColumnPairs(); 264 return (ColumnPairElement) pairs.getElement(name); 265 } 266 267 271 public synchronized void changeColumnPairs(ColumnPairElement[] pairs, int action) throws DBException { 272 initColumnPairs(); 273 this.pairs.change(pairs, action); 274 } 275 276 278 void initColumnPairs() { 279 if (pairs == null) 280 pairs = new DBMemoryCollection.ColumnPair(this); 281 } 282 283 } 284 } 285 | Popular Tags |