KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > ForeignKeyConstraint


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.DatabaseMetaData JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33
34 /**
35  * Represents a foreign key constraint on a table.
36  *
37  * @author Paul Ferraro
38  * @since 1.1
39  */

40 public class ForeignKeyConstraint extends UniqueConstraint
41 {
42     private String JavaDoc foreignSchema;
43     private String JavaDoc foreignTable;
44     private List JavaDoc<String JavaDoc> foreignColumnList = new LinkedList JavaDoc<String JavaDoc>();
45     private int updateRule;
46     private int deleteRule;
47     private int deferrability;
48     
49     /**
50      * Constructs a new ForeignKey.
51      * @param name the name of this constraint
52      * @param schema a schema name, possibly null
53      * @param table a table name
54      */

55     public ForeignKeyConstraint(String JavaDoc name, String JavaDoc schema, String JavaDoc table)
56     {
57         super(name, schema, table);
58     }
59     
60     /**
61      * @return the foreign table of this foreign key
62      */

63     public String JavaDoc getForeignTable()
64     {
65         return this.foreignTable;
66     }
67     
68     /**
69      * @return the foreign schema of this foreign key
70      */

71     public String JavaDoc getForeignSchema()
72     {
73         return this.foreignSchema;
74     }
75     
76     /**
77      * @return the foreign column of this foreign key
78      */

79     public List JavaDoc<String JavaDoc> getForeignColumnList()
80     {
81         return this.foreignColumnList;
82     }
83     
84     /**
85      * @return Returns the deleteRule.
86      */

87     public int getDeleteRule()
88     {
89         return this.deleteRule;
90     }
91
92     /**
93      * @return Returns the updateRule.
94      */

95     public int getUpdateRule()
96     {
97         return this.updateRule;
98     }
99
100     /**
101      * @return Returns the deferrability.
102      */

103     public int getDeferrability()
104     {
105         return this.deferrability;
106     }
107
108     /**
109      * @param deferrability The deferrability to set.
110      */

111     public void setDeferrability(int deferrability)
112     {
113         this.deferrability = deferrability;
114     }
115
116     /**
117      * @param deleteRule The deleteRule to set.
118      */

119     public void setDeleteRule(int deleteRule)
120     {
121         this.deleteRule = deleteRule;
122     }
123
124     /**
125      * @param foreignSchema The foreignSchema to set.
126      */

127     public void setForeignSchema(String JavaDoc foreignSchema)
128     {
129         this.foreignSchema = foreignSchema;
130     }
131
132     /**
133      * @param foreignTable The foreignTable to set.
134      */

135     public void setForeignTable(String JavaDoc foreignTable)
136     {
137         this.foreignTable = foreignTable;
138     }
139
140     /**
141      * @param updateRule The updateRule to set.
142      */

143     public void setUpdateRule(int updateRule)
144     {
145         this.updateRule = updateRule;
146     }
147
148     /**
149      * Collects all foreign keys from the specified tables using the specified connection.
150      * @param connection a database connection
151      * @param schemaMap a map of schema name to list of table names
152      * @return a Collection of ForeignKey objects.
153      * @throws SQLException if a database error occurs
154      */

155     public static Collection JavaDoc<ForeignKeyConstraint> collect(Connection JavaDoc connection, Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> schemaMap) throws SQLException
156     {
157         Map JavaDoc<String JavaDoc, ForeignKeyConstraint> foreignKeyMap = new HashMap JavaDoc<String JavaDoc, ForeignKeyConstraint>();
158         DatabaseMetaData JavaDoc metaData = connection.getMetaData();
159         
160         for (Map.Entry JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> schemaMapEntry: schemaMap.entrySet())
161         {
162             String JavaDoc schema = schemaMapEntry.getKey();
163             List JavaDoc<String JavaDoc> tableList = schemaMapEntry.getValue();
164             
165             for (String JavaDoc table: tableList)
166             {
167                 ResultSet JavaDoc resultSet = metaData.getImportedKeys(null, schema, table);
168                 
169                 while (resultSet.next())
170                 {
171                     String JavaDoc name = resultSet.getString("FK_NAME");
172                     
173                     ForeignKeyConstraint foreignKey = foreignKeyMap.get(name);
174                     
175                     if (foreignKey == null)
176                     {
177                         foreignKey = new ForeignKeyConstraint(name, schema, table);
178                         
179                         foreignKey.foreignSchema = resultSet.getString("PKTABLE_SCHEM");
180                         foreignKey.foreignTable = resultSet.getString("PKTABLE_NAME");
181                         foreignKey.deleteRule = resultSet.getInt("DELETE_RULE");
182                         foreignKey.updateRule = resultSet.getInt("UPDATE_RULE");
183                         foreignKey.deferrability = resultSet.getInt("DEFERRABILITY");
184                         
185                         foreignKeyMap.put(name, foreignKey);
186                     }
187                     
188                     String JavaDoc column = resultSet.getString("FKCOLUMN_NAME");
189                     String JavaDoc foreignColumn = resultSet.getString("PKCOLUMN_NAME");
190         
191                     foreignKey.getColumnList().add(column);
192                     foreignKey.getForeignColumnList().add(foreignColumn);
193                 }
194                 
195                 resultSet.close();
196             }
197         }
198         
199         return foreignKeyMap.values();
200     }
201 }
202
Popular Tags