KickJava   Java API By Example, From Geeks To Geeks.

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


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.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Represents a unique constraint on a table.
34  *
35  * @author Paul Ferraro
36  * @since 1.1
37  */

38 public class UniqueConstraint implements Comparable JavaDoc<UniqueConstraint>
39 {
40     private String JavaDoc name;
41     private String JavaDoc schema;
42     private String JavaDoc table;
43     private List JavaDoc<String JavaDoc> columnList = new LinkedList JavaDoc<String JavaDoc>();
44         
45     /**
46      * Constructs a new UniqueConstraint.
47      * @param name
48      * @param schema
49      * @param table
50      */

51     public UniqueConstraint(String JavaDoc name, String JavaDoc schema, String JavaDoc table)
52     {
53         this.name = name;
54         this.schema = schema;
55         this.table = table;
56     }
57     
58     /**
59      * @return the list of columns in this unique constraint
60      */

61     public List JavaDoc<String JavaDoc> getColumnList()
62     {
63         return this.columnList;
64     }
65     
66     /**
67      * @return the name of this constraint
68      */

69     public String JavaDoc getName()
70     {
71         return this.name;
72     }
73     
74     /**
75      * @return the schema of this constraint
76      */

77     public String JavaDoc getSchema()
78     {
79         return this.schema;
80     }
81     
82     /**
83      * @return the table of this constraint
84      */

85     public String JavaDoc getTable()
86     {
87         return this.table;
88     }
89     
90     /**
91      * @see java.lang.Object#equals(java.lang.Object)
92      */

93     @Override JavaDoc
94     public boolean equals(Object JavaDoc object)
95     {
96         UniqueConstraint key = (UniqueConstraint) object;
97         
98         return (key != null) && (key.name != null) && key.name.equals(this.name);
99     }
100     
101     /**
102      * @see java.lang.Object#hashCode()
103      */

104     @Override JavaDoc
105     public int hashCode()
106     {
107         return this.name.hashCode();
108     }
109
110     /**
111      * @see java.lang.Comparable#compareTo(java.lang.Object)
112      */

113     public int compareTo(UniqueConstraint constraint)
114     {
115         return this.name.compareTo(constraint.name);
116     }
117     
118     /**
119      * Collects all foreign keys from the specified tables using the specified connection.
120      * @param connection a database connection
121      * @param schema a schema name
122      * @param table a table name
123      * @param primaryKeyName the name of the primary key of this table
124      * @return a Collection of ForeignKey objects.
125      * @throws SQLException if a database error occurs
126      */

127     public static Collection JavaDoc<UniqueConstraint> collect(Connection JavaDoc connection, String JavaDoc schema, String JavaDoc table, String JavaDoc primaryKeyName) throws SQLException
128     {
129         Map JavaDoc<String JavaDoc, UniqueConstraint> keyMap = new HashMap JavaDoc<String JavaDoc, UniqueConstraint>();
130         
131         ResultSet JavaDoc resultSet = connection.getMetaData().getIndexInfo(null, schema, table, true, false);
132         
133         while (resultSet.next())
134         {
135             String JavaDoc name = resultSet.getString("INDEX_NAME");
136             
137             if ((name == null) || name.equals(primaryKeyName)) continue;
138             
139             UniqueConstraint key = keyMap.get(name);
140             
141             if (key == null)
142             {
143                 key = new UniqueConstraint(name, schema, table);
144                 
145                 keyMap.put(name, key);
146             }
147             
148             String JavaDoc column = resultSet.getString("COLUMN_NAME");
149             
150             key.getColumnList().add(column);
151         }
152         
153         resultSet.close();
154         
155         return keyMap.values();
156     }
157 }
158
Popular Tags