KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > cache > result > schema > CacheDatabaseSchema


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 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): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.controller.cache.result.schema;
26
27 import java.sql.SQLException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
31 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
32
33 /**
34  * A <code>CacheDatabaseSchema</code> describes all the tables and columns of
35  * a database and its associated cache entries.
36  *
37  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
38  * @version 1.0
39  */

40 public class CacheDatabaseSchema
41 {
42   /** Database tables. */
43   private ArrayList JavaDoc tables;
44
45   /**
46    * Creates a new <code>CacheDatabaseSchema</code> instance by cloning an
47    * existing <code>DatabaseSchema</code>.
48    *
49    * @param dbs the <code>DatabaseSchema</code> to clone
50    */

51   public CacheDatabaseSchema(DatabaseSchema dbs)
52   {
53     if (dbs == null)
54     {
55       tables = new ArrayList JavaDoc();
56       return;
57     }
58
59     // Clone the tables
60
ArrayList JavaDoc origTables = dbs.getTables();
61     int size = origTables.size();
62     tables = new ArrayList JavaDoc(size);
63     for (int i = 0; i < size; i++)
64       tables.add(new CacheDatabaseTable((DatabaseTable) origTables.get(i)));
65   }
66
67   /**
68    * Adds a <code>CacheDatabaseTable</code> describing a table of the
69    * database.
70    *
71    * @param table the table to add
72    */

73   public void addTable(CacheDatabaseTable table)
74   {
75     tables.add(table);
76   }
77
78   /**
79    * Removes a <code>CacheDatabaseTable</code> describing a table of the
80    * database.
81    *
82    * @param table the table to remove
83    */

84   public void removeTable(CacheDatabaseTable table)
85   {
86     tables.remove(table);
87   }
88
89   /**
90    * Merge the given schema with the current one. All missing tables or columns
91    * are added if no conflict is detected. An exception is thrown if the given
92    * schema definition conflicts with the current one.
93    *
94    * @param databaseSchema the schema to merge
95    * @throws SQLException if the schemas conflict
96    */

97   public void mergeSchema(CacheDatabaseSchema databaseSchema)
98       throws SQLException JavaDoc
99   {
100     if (databaseSchema == null)
101       return;
102
103     ArrayList JavaDoc otherTables = databaseSchema.getTables();
104     if (otherTables == null)
105       return;
106
107     int size = otherTables.size();
108     for (int i = 0; i < size; i++)
109     {
110       CacheDatabaseTable t = (CacheDatabaseTable) otherTables.get(i);
111       CacheDatabaseTable original = getTable(t.getName());
112       if (original == null)
113         addTable(t);
114       else
115         original.mergeColumns(t);
116     }
117   }
118
119   /**
120    * Returns an <code>ArrayList</code> of <code>CacheDatabaseTable</code>
121    * objects describing the database.
122    *
123    * @return an <code>ArrayList</code> of <code>CacheDatabaseTable</code>
124    */

125   public ArrayList JavaDoc getTables()
126   {
127     return tables;
128   }
129
130   /**
131    * Returns the <code>CacheDatabaseTable</code> object matching the given
132    * table name or <code>null</code> if not found.
133    *
134    * @param tableName the table name to look for
135    * @return a <code>CacheDatabaseTable</code> value or null
136    */

137   public CacheDatabaseTable getTable(String JavaDoc tableName)
138   {
139     int size = tables.size();
140     for (int i = 0; i < size; i++)
141     {
142       CacheDatabaseTable t = (CacheDatabaseTable) tables.get(i);
143       if (t.getName().compareTo(tableName) == 0)
144         return t;
145     }
146     return null;
147   }
148
149   /**
150    * Returns <code>true</code> if the given <code>TableName</code> is found
151    * in this schema.
152    *
153    * @param tableName the name of the table you are looking for
154    * @return <code>true</code> if the table has been found
155    */

156   public boolean hasTable(String JavaDoc tableName)
157   {
158     int size = tables.size();
159     for (int i = 0; i < size; i++)
160     {
161       CacheDatabaseTable t = (CacheDatabaseTable) tables.get(i);
162       if (tableName.equals(t.getName()))
163         return true;
164     }
165     return false;
166   }
167
168   /**
169    * Two <code>CacheDatabaseSchema</code> are equals if they have the same
170    * tables.
171    *
172    * @param other the object to compare with
173    * @return true if the 2 objects are the same.
174    */

175   public boolean equals(Object JavaDoc other)
176   {
177     if (!(other instanceof CacheDatabaseSchema))
178       return false;
179
180     if (tables == null)
181       return ((CacheDatabaseSchema) other).getTables() == null;
182     else
183       return tables.equals(((CacheDatabaseSchema) other).getTables());
184   }
185
186   /**
187    * Returns information about the database schema.
188    *
189    * @param longFormat <code>true</code> for a long format, false for a short
190    * summary
191    * @return a <code>String</code> value
192    */

193   public String JavaDoc getInformation(boolean longFormat)
194   {
195     String JavaDoc result = "";
196     int size = tables.size();
197     for (int i = 0; i < size; i++)
198     {
199       CacheDatabaseTable t = (CacheDatabaseTable) tables.get(i);
200       result += t.getInformation(longFormat) + "\n";
201     }
202     return result;
203   }
204
205 }
206
Popular Tags