Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

DatabaseTable.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet.
00022  * Contributor(s): Mathieu Peltier, Sara Bouchenak.
00023  */
00024 
00025 package org.objectweb.cjdbc.common.sql.schema;
00026 
00027 import java.io.Serializable;
00028 import java.sql.SQLException;
00029 import java.util.ArrayList;
00030 import java.util.Iterator;
00031 
00032 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00033 
00034 /**
00035  * A <code>DatabaseTable</code> represents a database table ! It is just an
00036  * array of <code>TableColumns</code> objects.
00037  * <p>
00038  * Keep it mind that <code>ArrayList</code> is not synchronized...
00039  * 
00040  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
00041  * @author <a href="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier</a>
00042  * @author <a href="mailto:Sara.Bouchenak@epfl.ch">Sara Bouchenak</a>
00043  * @version 1.0
00044  */
00045 public class DatabaseTable implements Serializable
00046 {
00047   /** Database table name. */
00048   private String name;
00049 
00050   /** <code>ArrayList</code> of <code>DatabaseColumn</code>. */
00051   private ArrayList columns;
00052 
00053   /**
00054    * Creates a new <code>DatabaseTable</code> instance.
00055    * 
00056    * @param name table name
00057    */
00058   public DatabaseTable(String name)
00059   {
00060     this(name, new ArrayList());
00061   }
00062 
00063   /**
00064    * Creates a new <code>DatabaseTable</code> instance.
00065    * 
00066    * @param name table name
00067    * @param nbOfColumns number of columns
00068    */
00069   public DatabaseTable(String name, int nbOfColumns)
00070   {
00071     this(name, new ArrayList(nbOfColumns));
00072   }
00073 
00074   /**
00075    * Creates a new <code>DatabaseTable</code> instance.
00076    * 
00077    * @param name table name
00078    * @param columns columns list
00079    */
00080   private DatabaseTable(String name, ArrayList columns)
00081   {
00082     if (name == null)
00083       throw new IllegalArgumentException("Illegal null database table name in DatabaseTable constructor");
00084 
00085     this.name = name;
00086     this.columns = columns;
00087   }
00088 
00089   /**
00090    * Gets the name of the table.
00091    * 
00092    * @return the table name
00093    */
00094   public String getName()
00095   {
00096     return name;
00097   }
00098 
00099   /**
00100    * Adds a <code>DatabaseColumn</code> object to this table.
00101    * <p>
00102    * Warning! The underlying <code>ArrayList</code> is not synchronized.
00103    * 
00104    * @param column a <code>DatabaseColumn</code> value
00105    */
00106   public void addColumn(DatabaseColumn column)
00107   {
00108     columns.add(column);
00109   }
00110   
00111   /**
00112    * Drops a <code>DatabaseColumn</code> object from this table.
00113    * <p>
00114    * Warning! The underlying <code>ArrayList</code> is not synchronized.
00115    * 
00116    * @param columnName a <code>String</code> that maps to a <code>DatabaseColumn</code> value
00117    */
00118   public void remove(String columnName)
00119   {
00120     columns.remove(getColumn(columnName));
00121   }
00122   
00123   /**
00124    * Drops a <code>DatabaseColumn</code> object from this table.
00125    * <p>
00126    * Warning! The underlying <code>ArrayList</code> is not synchronized.
00127    * 
00128    * @param column a <code>DatabaseColumn</code> value
00129    */
00130   public void remove(DatabaseColumn column)
00131   {
00132     columns.remove(column);
00133   }
00134 
00135   /**
00136    * Merges this table with the given table's columns. All missing columns are
00137    * added if no conflict is detected. An exception is thrown if the given
00138    * table columns conflicts with this one.
00139    * 
00140    * @param table the table to merge
00141    * @throws SQLException if the schemas conflict
00142    */
00143   public void mergeColumns(DatabaseTable table) throws SQLException
00144   {
00145     if (table == null)
00146       return;
00147 
00148     ArrayList otherColumns = table.getColumns();
00149     if (otherColumns == null)
00150       return;
00151 
00152     DatabaseColumn c, original;
00153     int size = otherColumns.size();
00154     for (int i = 0; i < size; i++)
00155     {
00156       c = (DatabaseColumn) otherColumns.get(i);
00157       original = getColumn(c.getName());
00158       if (original == null)
00159         addColumn(c);
00160       else
00161       {
00162         if (!original.equalsIgnoreType(c))
00163           throw new SQLException(
00164             "Unable to merge table ["+table.getName()+"]: column '"
00165               + c.getName()
00166               + "' definition mismatch");
00167       }
00168     }
00169   }
00170 
00171   /**
00172    * Returns a list of <code>DatabaseColumn</code> objects describing the
00173    * columns of this table.
00174    * <p>
00175    * Warning! The underlying <code>ArrayList</code> is not synchronized.
00176    * 
00177    * @return an <code>ArrayList</code> of <code>DatabaseColumn</code>
00178    */
00179   public ArrayList getColumns()
00180   {
00181     return columns;
00182   }
00183 
00184   /**
00185    * Returns a list of <code>DatabaseColumn</code> objects representing the
00186    * unique columns of this table.
00187    * <p>
00188    * Warning! The underlying <code>ArrayList</code> is not synchronized.
00189    * 
00190    * @return an <code>ArrayList</code> of <code>DatabaseColumn</code>
00191    *         objects
00192    */
00193   public ArrayList getUniqueColumns()
00194   {
00195     ArrayList cols = new ArrayList();
00196     Iterator i;
00197     DatabaseColumn c;
00198 
00199     for (i = columns.iterator(); i.hasNext();)
00200     {
00201       c = (DatabaseColumn) i.next();
00202       if (c.isUnique())
00203         cols.add(c);
00204     }
00205     return cols;
00206   }
00207 
00208   /**
00209    * Returns the <code>DatabaseColumn</code> object matching the given column
00210    * name or <code>null</code> if not found (the case is ignored).
00211    * 
00212    * @param columnName column name to look for
00213    * @return a <code>DatabaseColumn</code> value or <code>null</code>
00214    */
00215   public DatabaseColumn getColumn(String columnName)
00216   {
00217     DatabaseColumn c;
00218     for (Iterator i = columns.iterator(); i.hasNext();)
00219     {
00220       c = (DatabaseColumn) i.next();
00221       if (columnName.equalsIgnoreCase(c.getName()))
00222         return c;
00223 
00224     }
00225     return null;
00226   }
00227 
00228   /**
00229    * Returns the <code>DatabaseColumn</code> object matching the given column
00230    * name or <code>null</code> if not found (the case can be enforced).
00231    * 
00232    * @param columnName column name to look for
00233    * @param isCaseSensitive true if name matching must be case sensitive
00234    * @return a <code>DatabaseColumn</code> value or <code>null</code>
00235    */
00236   public DatabaseColumn getColumn(String columnName, boolean isCaseSensitive)
00237   {
00238     if (!isCaseSensitive)
00239       return getColumn(columnName);
00240 
00241     DatabaseColumn c;
00242     for (Iterator i = columns.iterator(); i.hasNext();)
00243     {
00244       c = (DatabaseColumn) i.next();
00245       if (columnName.equals(c.getName()))
00246         return c;
00247 
00248     }
00249     return null;
00250   }
00251 
00252   /**
00253    * Two <code>DatabaseColumn</code> are considered equal if they have the
00254    * same name and the same columns.
00255    * 
00256    * @param other the object to compare with
00257    * @return <code>true</code> if the tables are equal
00258    */
00259   public boolean equals(Object other)
00260   {
00261     if ((other == null) || !(other instanceof DatabaseTable))
00262       return false;
00263 
00264     DatabaseTable t = (DatabaseTable) other;
00265     return (t.getName().equals(name)) && (t.getColumns().equals(columns));
00266   }
00267 
00268   /**
00269    * This function is the same as equals but ignores the column type.
00270    * 
00271    * @param other the object to compare with
00272    * @return <code>true</code> if the table are equal ignoring the columns
00273    *         type
00274    * @see #equals(Object)
00275    */
00276   public boolean equalsIgnoreType(Object other)
00277   {
00278     if ((other == null) || !(other instanceof DatabaseTable))
00279       return false;
00280 
00281     DatabaseTable t = (DatabaseTable) other;
00282     if (!t.getName().equals(name))
00283       return false;
00284 
00285     DatabaseColumn c1, c2;
00286     Iterator iter = columns.iterator();
00287     while (iter.hasNext())
00288     {
00289       c1 = (DatabaseColumn) iter.next();
00290       c2 = t.getColumn(c1.getName());
00291 
00292       if (!c1.equalsIgnoreType(c2))
00293         return false; // Not compatible
00294     }
00295     return true;
00296   }
00297 
00298   /**
00299    * Get xml information about this table.
00300    * 
00301    * @return xml formatted information on this database table.
00302    */
00303   public String getXml()
00304   {
00305     StringBuffer info = new StringBuffer();
00306     info.append(
00307       "<"
00308         + DatabasesXmlTags.ELT_DatabaseTable
00309         + " "
00310         + DatabasesXmlTags.ATT_tableName
00311         + "=\""
00312         + name
00313         + "\" "
00314         + DatabasesXmlTags.ATT_nbOfColumns
00315         + "=\""
00316         + columns.size()
00317         + "\">");
00318     for (int i = 0; i < columns.size(); i++)
00319       info.append(((DatabaseColumn) columns.get(i)).getXml());
00320     info.append("</" + DatabasesXmlTags.ELT_DatabaseTable + ">");
00321     return info.toString();
00322   }
00323 }

Generated on Mon Apr 11 22:01:31 2005 for C-JDBC by  doxygen 1.3.9.1