001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: ForeignKey.java,v 1.5 2003/02/25 06:55:15 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import java.util.ArrayList;
014: import javax.jdo.JDOFatalInternalException;
015:
016: class ForeignKey extends Key {
017: private final boolean initiallyDeferred;
018: private BaseTable refTable;
019: private DatabaseAdapter dba;
020:
021: private ArrayList refColumns = new ArrayList();
022:
023: public ForeignKey(boolean initiallyDeferred) {
024: super (null);
025:
026: this .initiallyDeferred = initiallyDeferred;
027: this .refTable = null;
028: this .dba = null;
029: }
030:
031: public ForeignKey(Column col, ClassBaseTable refTable,
032: boolean initiallyDeferred) {
033: super ((BaseTable) col.getTable());
034: this .initiallyDeferred = initiallyDeferred;
035: this .refTable = refTable;
036: this .dba = table.getStoreManager().getDatabaseAdapter();
037:
038: setColumn(0, col, refTable.getIDMapping().getColumn());
039: }
040:
041: public void addColumn(Column col, Column refCol) {
042: setColumn(columns.size(), col, refCol);
043: }
044:
045: public void setColumn(int seq, Column col, Column refCol) {
046: if (table == null) {
047: table = (BaseTable) col.getTable();
048: refTable = (BaseTable) refCol.getTable();
049: dba = table.getStoreManager().getDatabaseAdapter();
050: } else {
051: if (!table.equals(col.getTable()))
052: throw new JDOFatalInternalException("Cannot add " + col
053: + " as FK column for " + table);
054: if (!refTable.equals(refCol.getTable()))
055: throw new JDOFatalInternalException("Cannot add "
056: + refCol + " as referenced FK column for "
057: + refTable);
058: }
059:
060: setMinSize(columns, seq + 1);
061: setMinSize(refColumns, seq + 1);
062:
063: columns.set(seq, col);
064: refColumns.set(seq, refCol);
065: }
066:
067: public int hashCode() {
068: return columns.hashCode() ^ refColumns.hashCode();
069: }
070:
071: public boolean equals(Object o) {
072: if (o == this )
073: return true;
074:
075: if (!(o instanceof ForeignKey))
076: return false;
077:
078: ForeignKey fk = (ForeignKey) o;
079:
080: return columns.equals(fk.columns)
081: && refColumns.equals(fk.refColumns);
082: }
083:
084: public String toString() {
085: StringBuffer s = new StringBuffer("FOREIGN KEY ").append(
086: getColumnList(columns)).append(" REFERENCES ").append(
087: refTable.getName());
088:
089: /*
090: * We only need to append the referenced column list if we're
091: * referencing something other than the primary key. However, some
092: * broken DBMS's (MySQL, I'm looking at you) fail if it's not included.
093: */
094: //if (!refColumns.equals(refTable.getExpectedPrimaryKey().getColumns()))
095: s.append(' ').append(getColumnList(refColumns));
096:
097: if (initiallyDeferred) {
098: if (dba.supportsDeferredConstraints())
099: s.append(" INITIALLY DEFERRED");
100: }
101:
102: return s.toString();
103: }
104: }
|