KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > sql > schema > DatabaseTable


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Mathieu Peltier, Sara Bouchenak.
23  */

24
25 package org.objectweb.cjdbc.common.sql.schema;
26
27 import java.io.Serializable JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
33
34 /**
35  * A <code>DatabaseTable</code> represents a database table ! It is just an
36  * array of <code>TableColumns</code> objects.
37  * <p>
38  * Keep it mind that <code>ArrayList</code> is not synchronized...
39  *
40  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
41  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier</a>
42  * @author <a HREF="mailto:Sara.Bouchenak@epfl.ch">Sara Bouchenak</a>
43  * @version 1.0
44  */

45 public class DatabaseTable implements Serializable JavaDoc
46 {
47   private static final long serialVersionUID = 7138810420058450235L;
48
49   /** Database table name. */
50   private String JavaDoc name;
51
52   /** <code>ArrayList</code> of <code>DatabaseColumn</code>. */
53   private ArrayList JavaDoc columns;
54
55   /**
56    * Creates a new <code>DatabaseTable</code> instance.
57    *
58    * @param name table name
59    */

60   public DatabaseTable(String JavaDoc name)
61   {
62     this(name, new ArrayList JavaDoc());
63   }
64
65   /**
66    * Creates a new <code>DatabaseTable</code> instance.
67    *
68    * @param name table name
69    * @param nbOfColumns number of columns
70    */

71   public DatabaseTable(String JavaDoc name, int nbOfColumns)
72   {
73     this(name, new ArrayList JavaDoc(nbOfColumns));
74   }
75
76   /**
77    * Creates a new <code>DatabaseTable</code> instance.
78    *
79    * @param name table name
80    * @param columns columns list
81    */

82   private DatabaseTable(String JavaDoc name, ArrayList JavaDoc columns)
83   {
84     if (name == null)
85       throw new IllegalArgumentException JavaDoc(
86           "Illegal null database table name in DatabaseTable constructor");
87
88     this.name = name;
89     this.columns = columns;
90   }
91
92   /**
93    * Gets the name of the table.
94    *
95    * @return the table name
96    */

97   public String JavaDoc getName()
98   {
99     return name;
100   }
101
102   /**
103    * Adds a <code>DatabaseColumn</code> object to this table.
104    * <p>
105    * Warning! The underlying <code>ArrayList</code> is not synchronized.
106    *
107    * @param column a <code>DatabaseColumn</code> value
108    */

109   public void addColumn(DatabaseColumn column)
110   {
111     columns.add(column);
112   }
113
114   /**
115    * Drops a <code>DatabaseColumn</code> object from this table.
116    * <p>
117    * Warning! The underlying <code>ArrayList</code> is not synchronized.
118    *
119    * @param columnName a <code>String</code> that maps to a
120    * <code>DatabaseColumn</code> value
121    */

122   public void remove(String JavaDoc columnName)
123   {
124     columns.remove(getColumn(columnName));
125   }
126
127   /**
128    * Drops a <code>DatabaseColumn</code> object from this table.
129    * <p>
130    * Warning! The underlying <code>ArrayList</code> is not synchronized.
131    *
132    * @param column a <code>DatabaseColumn</code> value
133    */

134   public void remove(DatabaseColumn column)
135   {
136     columns.remove(column);
137   }
138
139   /**
140    * Merges this table with the given table's columns. All missing columns are
141    * added if no conflict is detected. An exception is thrown if the given table
142    * columns conflicts with this one.
143    *
144    * @param table the table to merge
145    * @throws SQLException if the schemas conflict
146    */

147   public void mergeColumns(DatabaseTable table) throws SQLException JavaDoc
148   {
149     if (table == null)
150       return;
151
152     ArrayList JavaDoc otherColumns = table.getColumns();
153     if (otherColumns == null)
154       return;
155
156     DatabaseColumn c, original;
157     int size = otherColumns.size();
158     for (int i = 0; i < size; i++)
159     {
160       c = (DatabaseColumn) otherColumns.get(i);
161       original = getColumn(c.getName());
162       if (original == null)
163         addColumn(c);
164       else
165       {
166         if (!original.equalsIgnoreType(c))
167           throw new SQLException JavaDoc("Unable to merge table [" + table.getName()
168               + "]: column '" + c.getName() + "' definition mismatch");
169       }
170     }
171   }
172
173   /**
174    * Returns a list of <code>DatabaseColumn</code> objects describing the
175    * columns of this table.
176    * <p>
177    * Warning! The underlying <code>ArrayList</code> is not synchronized.
178    *
179    * @return an <code>ArrayList</code> of <code>DatabaseColumn</code>
180    */

181   public ArrayList JavaDoc getColumns()
182   {
183     return columns;
184   }
185
186   /**
187    * Returns a list of <code>DatabaseColumn</code> objects representing the
188    * unique columns of this table.
189    * <p>
190    * Warning! The underlying <code>ArrayList</code> is not synchronized.
191    *
192    * @return an <code>ArrayList</code> of <code>DatabaseColumn</code>
193    * objects
194    */

195   public ArrayList JavaDoc getUniqueColumns()
196   {
197     ArrayList JavaDoc cols = new ArrayList JavaDoc();
198     Iterator JavaDoc i;
199     DatabaseColumn c;
200
201     for (i = columns.iterator(); i.hasNext();)
202     {
203       c = (DatabaseColumn) i.next();
204       if (c.isUnique())
205         cols.add(c);
206     }
207     return cols;
208   }
209
210   /**
211    * Returns the <code>DatabaseColumn</code> object matching the given column
212    * name or <code>null</code> if not found (the case is ignored).
213    *
214    * @param columnName column name to look for
215    * @return a <code>DatabaseColumn</code> value or <code>null</code>
216    */

217   public DatabaseColumn getColumn(String JavaDoc columnName)
218   {
219     DatabaseColumn c;
220     for (Iterator JavaDoc i = columns.iterator(); i.hasNext();)
221     {
222       c = (DatabaseColumn) i.next();
223       if (columnName.equalsIgnoreCase(c.getName()))
224         return c;
225
226     }
227     return null;
228   }
229
230   /**
231    * Returns the <code>DatabaseColumn</code> object matching the given column
232    * name or <code>null</code> if not found (the case can be enforced).
233    *
234    * @param columnName column name to look for
235    * @param isCaseSensitive true if name matching must be case sensitive
236    * @return a <code>DatabaseColumn</code> value or <code>null</code>
237    */

238   public DatabaseColumn getColumn(String JavaDoc columnName, boolean isCaseSensitive)
239   {
240     if (!isCaseSensitive)
241       return getColumn(columnName);
242
243     DatabaseColumn c;
244     for (Iterator JavaDoc i = columns.iterator(); i.hasNext();)
245     {
246       c = (DatabaseColumn) i.next();
247       if (columnName.equals(c.getName()))
248         return c;
249
250     }
251     return null;
252   }
253
254   /**
255    * Two <code>DatabaseColumn</code> are considered equal if they have the
256    * same name and the same columns.
257    *
258    * @param other the object to compare with
259    * @return <code>true</code> if the tables are equal
260    */

261   public boolean equals(Object JavaDoc other)
262   {
263     if ((other == null) || !(other instanceof DatabaseTable))
264       return false;
265
266     DatabaseTable t = (DatabaseTable) other;
267     return (t.getName().equals(name)) && (t.getColumns().equals(columns));
268   }
269
270   /**
271    * This function is the same as equals but ignores the column type.
272    *
273    * @param other the object to compare with
274    * @return <code>true</code> if the table are equal ignoring the columns
275    * type
276    * @see #equals(Object)
277    */

278   public boolean equalsIgnoreType(Object JavaDoc other)
279   {
280     if ((other == null) || !(other instanceof DatabaseTable))
281       return false;
282
283     DatabaseTable t = (DatabaseTable) other;
284     if (!t.getName().equals(name))
285       return false;
286
287     DatabaseColumn c1, c2;
288     Iterator JavaDoc iter = columns.iterator();
289     while (iter.hasNext())
290     {
291       c1 = (DatabaseColumn) iter.next();
292       c2 = t.getColumn(c1.getName());
293
294       if (!c1.equalsIgnoreType(c2))
295         return false; // Not compatible
296
}
297     return true;
298   }
299
300   /**
301    * Get xml information about this table.
302    *
303    * @return xml formatted information on this database table.
304    */

305   public String JavaDoc getXml()
306   {
307     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
308     info.append("<" + DatabasesXmlTags.ELT_DatabaseTable + " "
309         + DatabasesXmlTags.ATT_tableName + "=\"" + name + "\" "
310         + DatabasesXmlTags.ATT_nbOfColumns + "=\"" + columns.size() + "\">");
311     for (int i = 0; i < columns.size(); i++)
312       info.append(((DatabaseColumn) columns.get(i)).getXml());
313     info.append("</" + DatabasesXmlTags.ELT_DatabaseTable + ">");
314     return info.toString();
315   }
316 }
317
Popular Tags