KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2003 (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: ForeignKeyInfo.java,v 1.2 2003/02/05 18:15:20 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.io.StringWriter JavaDoc;
15 import java.sql.DatabaseMetaData JavaDoc;
16 import java.sql.ResultSet JavaDoc;
17 import java.sql.SQLException JavaDoc;
18 import java.sql.Types JavaDoc;
19 import javax.jdo.JDOFatalDataStoreException;
20
21
22 /**
23  * Represents the metadata of a specific foreign key column. This class is
24  * basically a data structure that makes accessing the JDBC foreign key metadata
25  * easier. Each of the items returned by
26  * {@link DatabaseMetaData#getImportedKeys(String,String,String)} or
27  * {@link DatabaseMetaData#getExportedKeys(String,String,String)}
28  * is represented by a public field in this class.
29  *
30  * Subclasses of ForeignKeyInfo can be created on a per-DBMS basis to supply
31  * missing metadata or correct faulty metadata obtained from that DBMS's JDBC
32  * driver(s).
33  *
34  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
35  * @version $Revision: 1.2 $
36  *
37  * @see StoreManager#getForeignKeyInfo
38  * @see DatabaseAdapter#newForeignKeyInfo
39  */

40
41 class ForeignKeyInfo
42 {
43     /**
44      * The primary key table catalog, which may be <tt>null</tt>.
45      */

46     public String JavaDoc pkTableCat;
47
48     /**
49      * The primary key table schema, which may be <tt>null</tt>.
50      */

51     public String JavaDoc pkTableSchem;
52
53     /**
54      * The primary key table name.
55      */

56     public String JavaDoc pkTableName;
57
58     /**
59      * The primary key column name.
60      */

61     public String JavaDoc pkColumnName;
62
63     /**
64      * The foreign key table catalog, which may be <tt>null</tt>.
65      */

66     public String JavaDoc fkTableCat;
67
68     /**
69      * The foreign key table schema, which may be <tt>null</tt>.
70      */

71     public String JavaDoc fkTableSchem;
72
73     /**
74      * The foreign key table name.
75      */

76     public String JavaDoc fkTableName;
77
78     /**
79      * The foreign key column name.
80      */

81     public String JavaDoc fkColumnName;
82
83     /**
84      * The sequence number within the foreign key, base 1.
85      */

86     public short keySeq;
87
88     /**
89      * What happens to a foreign key when the primary key is updated.
90      *
91      * @see DatabaseMetaData#importedKeyNoAction
92      * @see DatabaseMetaData#importedKeyCascade
93      * @see DatabaseMetaData#importedKeySetNull
94      * @see DatabaseMetaData#importedKeySetDefault
95      * @see DatabaseMetaData#importedKeyRestrict
96      */

97     public short updateRule;
98
99     /**
100      * What happens to a foreign key when the primary key is deleted.
101      *
102      * @see DatabaseMetaData#importedKeyNoAction
103      * @see DatabaseMetaData#importedKeyCascade
104      * @see DatabaseMetaData#importedKeySetNull
105      * @see DatabaseMetaData#importedKeySetDefault
106      * @see DatabaseMetaData#importedKeyRestrict
107      */

108     public short deleteRule;
109
110     /**
111      * The foreign key name.
112      */

113     public String JavaDoc fkName;
114
115     /**
116      * The primary key name.
117      */

118     public String JavaDoc pkName;
119
120     /**
121      * Indicates whether the evaluation of the foreign key constraint can be
122      * deferred until commit time.
123      *
124      * @see DatabaseMetaData#importedKeyInitiallyDeferred
125      * @see DatabaseMetaData#importedKeyInitiallyImmediate
126      * @see DatabaseMetaData#importedKeyNotDeferrable
127      */

128     public short deferrability;
129
130     private int hash = 0;
131
132
133     /**
134      * Constructs a foreign key information object from the current row of the
135      * given result set.
136      * The {@link ResultSet} object passed must have been obtained from a call
137      * to DatabaseMetaData.getImportedKeys() or
138      * DatabaseMetaData.getImportedKeys().
139      *
140      * <p>This method only retrieves the values from the current row; the caller
141      * is required to advance to the next row with {@link ResultSet#next}.
142      *
143      * @param rs The result set returned from DatabaseMetaData.get??portedKeys().
144      *
145      * @exception JDOFatalDataStoreException
146      * if a column of foreign key information could not be retrieved from the
147      * result set.
148      */

149
150     public ForeignKeyInfo(ResultSet JavaDoc rs) throws JDOFatalDataStoreException
151     {
152         try
153         {
154             pkTableCat = rs.getString(1);
155             pkTableSchem = rs.getString(2);
156             pkTableName = rs.getString(3);
157             pkColumnName = rs.getString(4);
158             fkTableCat = rs.getString(5);
159             fkTableSchem = rs.getString(6);
160             fkTableName = rs.getString(7);
161             fkColumnName = rs.getString(8);
162             keySeq = rs.getShort(9);
163             updateRule = rs.getShort(10);
164             deleteRule = rs.getShort(11);
165             fkName = rs.getString(12);
166             pkName = rs.getString(13);
167             deferrability = rs.getShort(14);
168         }
169         catch (SQLException JavaDoc e)
170         {
171             throw new JDOFatalDataStoreException("Can't read JDBC metadata from result set", e);
172         }
173     }
174
175
176     /**
177      * Constructs a foreign key information object from its individual
178      * attributes.
179      *
180      * <p>This can be useful to subclasses and/or custom DatabaseAdapters that
181      * need to modify and/or correct the metadata returned by the JDBC driver.
182      */

183
184     public ForeignKeyInfo(String JavaDoc pkTableCat,
185                           String JavaDoc pkTableSchem,
186                           String JavaDoc pkTableName,
187                           String JavaDoc pkColumnName,
188                           String JavaDoc fkTableCat,
189                           String JavaDoc fkTableSchem,
190                           String JavaDoc fkTableName,
191                           String JavaDoc fkColumnName,
192                           short keySeq,
193                           short updateRule,
194                           short deleteRule,
195                           String JavaDoc fkName,
196                           String JavaDoc pkName,
197                           short deferrability)
198     {
199         this.pkTableCat = pkTableCat;
200         this.pkTableSchem = pkTableSchem;
201         this.pkTableName = pkTableName;
202         this.pkColumnName = pkColumnName;
203         this.fkTableCat = fkTableCat;
204         this.fkTableSchem = fkTableSchem;
205         this.fkTableName = fkTableName;
206         this.fkColumnName = fkColumnName;
207         this.keySeq = keySeq;
208         this.updateRule = updateRule;
209         this.deleteRule = deleteRule;
210         this.fkName = fkName;
211         this.pkName = pkName;
212         this.deferrability = deferrability;
213     }
214
215
216     /**
217      * Indicates whether some object is "equal to" this one.
218      * Two <tt>ForeignKeyInfo</tt> objects are considered equal if their
219      * catalog, schema, table, and column names, both primary and foreign, are
220      * all equal.
221      *
222      * @param obj the reference object with which to compare
223      *
224      * @return <tt>true</tt> if this object is equal to the obj argument;
225      * <tt>false</tt> otherwise.
226      */

227
228     public final boolean equals(Object JavaDoc obj)
229     {
230         if (obj == this)
231             return true;
232
233         if (!(obj instanceof ForeignKeyInfo))
234             return false;
235
236         ForeignKeyInfo ci = (ForeignKeyInfo)obj;
237
238         return (pkTableCat == null ? ci.pkTableCat == null : pkTableCat.equals(ci.pkTableCat))
239             && (pkTableSchem == null ? ci.pkTableSchem == null : pkTableSchem.equals(ci.pkTableSchem))
240             && pkTableName.equals(ci.pkTableName)
241             && pkColumnName.equals(ci.pkColumnName)
242             && (fkTableCat == null ? ci.fkTableCat == null : fkTableCat.equals(ci.fkTableCat))
243             && (fkTableSchem == null ? ci.fkTableSchem == null : fkTableSchem.equals(ci.fkTableSchem))
244             && fkTableName.equals(ci.fkTableName)
245             && fkColumnName.equals(ci.fkColumnName);
246     }
247
248
249     /**
250      * Returns a hash code value for this object.
251      *
252      * @return a hash code value for this object.
253      */

254
255     public final int hashCode()
256     {
257         if (hash == 0)
258         {
259             hash = (pkTableCat == null ? 0 : pkTableCat.hashCode())
260                  ^ (pkTableSchem == null ? 0 : pkTableSchem.hashCode())
261                  ^ pkTableName.hashCode()
262                  ^ pkColumnName.hashCode()
263                  ^ (fkTableCat == null ? 0 : fkTableCat.hashCode())
264                  ^ (fkTableSchem == null ? 0 : fkTableSchem.hashCode())
265                  ^ fkTableName.hashCode()
266                  ^ fkColumnName.hashCode();
267         }
268
269         return hash;
270     }
271
272
273     /**
274      * Returns the string representation of this object.
275      *
276      * @return string representation of this object.
277      */

278
279     public String JavaDoc toString()
280     {
281         StringWriter JavaDoc sw = new StringWriter JavaDoc();
282         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
283
284         pw.println(this.getClass().getName());
285         pw.print(" pkTableCat = "); pw.println(pkTableCat);
286         pw.print(" pkTableSchem = "); pw.println(pkTableSchem);
287         pw.print(" pkTableName = "); pw.println(pkTableName);
288         pw.print(" pkColumnName = "); pw.println(pkColumnName);
289         pw.print(" fkTableCat = "); pw.println(fkTableCat);
290         pw.print(" fkTableSchem = "); pw.println(fkTableSchem);
291         pw.print(" fkTableName = "); pw.println(fkTableName);
292         pw.print(" fkColumnName = "); pw.println(fkColumnName);
293         pw.print(" keySeq = "); pw.println(keySeq);
294         pw.print(" updateRule = "); pw.println(updateRule);
295         pw.print(" deleteRule = "); pw.println(deleteRule);
296         pw.print(" fkName = "); pw.println(fkName);
297         pw.print(" pkName = "); pw.println(pkName);
298         pw.print(" deferrability = "); pw.println(deferrability);
299         pw.close();
300
301         return sw.toString();
302     }
303 }
304
Popular Tags