KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > ForeignKey


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: ForeignKey.java,v 1.5 2003/02/25 06:55:15 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.util.ArrayList JavaDoc;
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 JavaDoc refColumns = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc toString()
98     {
99         StringBuffer JavaDoc s = new StringBuffer JavaDoc("FOREIGN KEY ")
100                                   .append(getColumnList(columns))
101                                   .append(" REFERENCES ")
102                                   .append(refTable.getName());
103
104         /*
105          * We only need to append the referenced column list if we're
106          * referencing something other than the primary key. However, some
107          * broken DBMS's (MySQL, I'm looking at you) fail if it's not included.
108          */

109         //if (!refColumns.equals(refTable.getExpectedPrimaryKey().getColumns()))
110
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