KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > cache > result > schema > CacheDatabaseSchema


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Emmanuel Cecchet.
20  * Contributor(s): ______________________________________.
21  */

22
23 package org.continuent.sequoia.controller.cache.result.schema;
24
25 import java.sql.SQLException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.continuent.sequoia.controller.sql.schema.DatabaseSchema;
31 import org.continuent.sequoia.controller.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
Collection JavaDoc origTables = dbs.getTables().values();
61     int size = origTables.size();
62     tables = new ArrayList JavaDoc(size);
63     for (Iterator JavaDoc iter = origTables.iterator(); iter.hasNext();)
64       for (int i = 0; i < size; i++)
65         tables.add(new CacheDatabaseTable((DatabaseTable) iter.next()));
66   }
67
68   /**
69    * Adds a <code>CacheDatabaseTable</code> describing a table of the
70    * database.
71    *
72    * @param table the table to add
73    */

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

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

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

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

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

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

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

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