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

CacheDatabaseSchema.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): ______________________________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.cache.result.schema;
00026 
00027 import java.io.Serializable;
00028 import java.sql.SQLException;
00029 import java.util.ArrayList;
00030 
00031 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
00032 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
00033 
00034 /**
00035  * A <code>CacheDatabaseSchema</code> describes all the tables and columns of
00036  * a database and its associated cache entries.
00037  * 
00038  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
00039  * @version 1.0
00040  */
00041 public class CacheDatabaseSchema implements Serializable
00042 {
00043   /** Database tables. */
00044   private ArrayList tables;
00045 
00046   /**
00047    * Creates a new <code>CacheDatabaseSchema</code> instance by cloning an
00048    * existing <code>DatabaseSchema</code>.
00049    * 
00050    * @param dbs the <code>DatabaseSchema</code> to clone
00051    */
00052   public CacheDatabaseSchema(DatabaseSchema dbs)
00053   {
00054     if (dbs == null)
00055     {
00056       tables = new ArrayList();
00057       return;
00058     }
00059 
00060     // Clone the tables
00061     ArrayList origTables = dbs.getTables();
00062     int size = origTables.size();
00063     tables = new ArrayList(size);
00064     for (int i = 0; i < size; i++)
00065       tables.add(new CacheDatabaseTable((DatabaseTable) origTables.get(i)));
00066   }
00067 
00068   /**
00069    * Adds a <code>CacheDatabaseTable</code> describing a table of the
00070    * database.
00071    * 
00072    * @param table the table to add
00073    */
00074   public void addTable(CacheDatabaseTable table)
00075   {
00076     tables.add(table);
00077   }
00078 
00079   /**
00080    * Removes a <code>CacheDatabaseTable</code> describing a table of the
00081    * database.
00082    * 
00083    * @param table the table to remove
00084    */
00085   public void removeTable(CacheDatabaseTable table)
00086   {
00087     tables.remove(table);
00088   }
00089 
00090   /**
00091    * Merge the given schema with the current one. All missing tables or columns
00092    * are added if no conflict is detected. An exception is thrown if the given
00093    * schema definition conflicts with the current one.
00094    * 
00095    * @param databaseSchema the schema to merge
00096    * @throws SQLException if the schemas conflict
00097    */
00098   public void mergeSchema(CacheDatabaseSchema databaseSchema)
00099     throws SQLException
00100   {
00101     if (databaseSchema == null)
00102       return;
00103 
00104     ArrayList otherTables = databaseSchema.getTables();
00105     if (otherTables == null)
00106       return;
00107 
00108     int size = otherTables.size();
00109     for (int i = 0; i < size; i++)
00110     {
00111       CacheDatabaseTable t = (CacheDatabaseTable) otherTables.get(i);
00112       CacheDatabaseTable original = getTable(t.getName());
00113       if (original == null)
00114         addTable(t);
00115       else
00116         original.mergeColumns(t);
00117     }
00118   }
00119 
00120   /**
00121    * Returns an <code>ArrayList</code> of <code>CacheDatabaseTable</code>
00122    * objects describing the database.
00123    * 
00124    * @return an <code>ArrayList</code> of <code>CacheDatabaseTable</code>
00125    */
00126   public ArrayList getTables()
00127   {
00128     return tables;
00129   }
00130 
00131   /**
00132    * Returns the <code>CacheDatabaseTable</code> object matching the given
00133    * table name or <code>null</code> if not found.
00134    * 
00135    * @param tableName the table name to look for
00136    * @return a <code>CacheDatabaseTable</code> value or null
00137    */
00138   public CacheDatabaseTable getTable(String tableName)
00139   {
00140     int size = tables.size();
00141     for (int i = 0; i < size; i++)
00142     {
00143       CacheDatabaseTable t = (CacheDatabaseTable) tables.get(i);
00144       if (t.getName().compareTo(tableName) == 0)
00145         return t;
00146     }
00147     return null;
00148   }
00149 
00150   /**
00151    * Returns <code>true</code> if the given <code>TableName</code> is found
00152    * in this schema.
00153    * 
00154    * @param tableName the name of the table you are looking for
00155    * @return <code>true</code> if the table has been found
00156    */
00157   public boolean hasTable(String tableName)
00158   {
00159     int size = tables.size();
00160     for (int i = 0; i < size; i++)
00161     {
00162       CacheDatabaseTable t = (CacheDatabaseTable) tables.get(i);
00163       if (tableName.equals(t.getName()))
00164         return true;
00165     }
00166     return false;
00167   }
00168 
00169   /**
00170    * Two <code>CacheDatabaseSchema</code> are equals if they have the same
00171    * tables.
00172    * 
00173    * @param other the object to compare with
00174    * @return true if the 2 objects are the same.
00175    */
00176   public boolean equals(Object other)
00177   {
00178     if (!(other instanceof CacheDatabaseSchema))
00179       return false;
00180 
00181     if (tables == null)
00182       return ((CacheDatabaseSchema) other).getTables() == null;
00183     else
00184       return tables.equals(((CacheDatabaseSchema) other).getTables());
00185   }
00186 
00187   /**
00188    * Returns information about the database schema.
00189    * 
00190    * @param longFormat <code>true</code> for a long format, false for a short
00191    *          summary
00192    * @return a <code>String</code> value
00193    */
00194   public String getInformation(boolean longFormat)
00195   {
00196     String result = "";
00197     int size = tables.size();
00198     for (int i = 0; i < size; i++)
00199     {
00200       CacheDatabaseTable t = (CacheDatabaseTable) tables.get(i);
00201       result += t.getInformation(longFormat) + "\n";
00202     }
00203     return result;
00204   }
00205 
00206 }

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