KickJava   Java API By Example, From Geeks To Geeks.

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


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): ______________________________________.
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
31 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
32
33 /**
34  * A <code>DatabaseSchema</code> describes all the tables and columns of a
35  * database.
36  *
37  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
39  * @version 1.0
40  */

41 public class DatabaseSchema implements Serializable JavaDoc
42 {
43   private static final long serialVersionUID = 1105453274994163661L;
44
45   /** <code>ArrayList</code> of <code>DatabaseTables</code>. */
46   private ArrayList JavaDoc tables;
47   /** <code>ArrayList</code> of <code>DatabaseProcedures</code>. */
48   private ArrayList JavaDoc procedures;
49
50   /**
51    * Creates a new <code>DatabaseSchema</code> instance.
52    */

53   public DatabaseSchema()
54   {
55     tables = new ArrayList JavaDoc();
56     procedures = new ArrayList JavaDoc();
57   }
58
59   /**
60    * Creates a new <code>DatabaseSchema</code> instance with a specified
61    * number of tables.
62    *
63    * @param nbOfTables an <code>int</code> value
64    */

65   public DatabaseSchema(int nbOfTables)
66   {
67     tables = new ArrayList JavaDoc(nbOfTables);
68     procedures = new ArrayList JavaDoc();
69   }
70
71   /**
72    * Creates a new <code>DatabaseSchema</code> instance from an existing
73    * database schema (the schema is cloned).
74    *
75    * @param schema the existing database schema
76    */

77   public DatabaseSchema(DatabaseSchema schema)
78   {
79     if (schema == null)
80       throw new IllegalArgumentException JavaDoc(
81           "Illegal null database schema in DatabaseSchema(DatabaseSchema) constructor");
82
83     tables = new ArrayList JavaDoc(schema.getTables());
84     procedures = new ArrayList JavaDoc(schema.getProcedures());
85   }
86
87   /**
88    * Adds a <code>DatabaseTable</code> describing a table of the database.
89    *
90    * @param table the table to add
91    */

92   public void addTable(DatabaseTable table)
93   {
94     if (table == null)
95       throw new IllegalArgumentException JavaDoc(
96           "Illegal null database table in addTable(DatabaseTable) method");
97     tables.add(table);
98   }
99
100   /**
101    * Adds a <code>DatabaseProcedure</code> describing a procedure of the
102    * database.
103    *
104    * @param procedure the procedure to add
105    */

106   public void addProcedure(DatabaseProcedure procedure)
107   {
108     if (procedure == null)
109       throw new IllegalArgumentException JavaDoc(
110           "Illegal null database table in addTable(DatabaseTable) method");
111     procedures.add(procedure);
112   }
113
114   /**
115    * Removes a <code>DatabaseTable</code> describing a table of the database.
116    *
117    * @param table the table to remove
118    * @return true if the table was successfully removed
119    */

120   public boolean removeTable(DatabaseTable table)
121   {
122     if (table == null)
123       throw new IllegalArgumentException JavaDoc(
124           "Illegal null database table in removeTable(DatabaseTable) method");
125     return tables.remove(table);
126   }
127
128   /**
129    * removes a <code>DatabaseProcedure</code> describing a procedure of the
130    * database.
131    *
132    * @param procedure to remove
133    * @return true if the procedure was successfully removed
134    */

135   public boolean removeProcedure(DatabaseProcedure procedure)
136   {
137     if (procedure == null)
138       throw new IllegalArgumentException JavaDoc(
139           "Illegal null database procedure in removeProcedure(DatabaseProcedure) method");
140     return procedures.remove(procedure);
141   }
142
143   /**
144    * Merges the given schema with the current one. All missing tables or columns
145    * are added if no conflict is detected. An exception is thrown if the given
146    * schema definition conflicts with the current one.
147    *
148    * @param databaseSchema the schema to merge
149    * @throws SQLException if the schemas conflict
150    */

151   public void mergeSchema(DatabaseSchema databaseSchema) throws SQLException JavaDoc
152   {
153     if (databaseSchema == null)
154       throw new IllegalArgumentException JavaDoc(
155           "Illegal null database schema in mergeSchema(DatabaseSchema) method");
156
157     ArrayList JavaDoc otherTables = databaseSchema.getTables();
158     if (otherTables.size() == 0)
159       return;
160
161     DatabaseTable table, originalTable;
162     int size = otherTables.size();
163     for (int i = 0; i < size; i++)
164     {
165       table = (DatabaseTable) otherTables.get(i);
166       originalTable = getTable(table.getName());
167       if (originalTable == null)
168         addTable(table);
169       else
170         originalTable.mergeColumns(table);
171     }
172
173     ArrayList JavaDoc otherProcedures = databaseSchema.getProcedures();
174     if (otherProcedures.size() == 0)
175       return;
176
177     DatabaseProcedure procedure, originalProcedure;
178     int sizep = otherProcedures.size();
179     for (int i = 0; i < sizep; i++)
180     {
181       procedure = (DatabaseProcedure) otherProcedures.get(i);
182       originalProcedure = getProcedure(procedure.getName());
183       if (originalProcedure == null)
184         addProcedure(procedure);
185       else
186         originalProcedure.mergeParameters(procedure);
187     }
188   }
189
190   /**
191    * Returns an <code>ArrayList</code> of <code>DatabaseTable</code> objects
192    * describing the database.
193    *
194    * @return an <code>ArrayList</code> of <code>DatabaseTable</code>
195    */

196   public ArrayList JavaDoc getTables()
197   {
198     return tables;
199   }
200
201   /**
202    * Returns an <code>ArrayList</code> of <code>DatabaseProcedure</code>
203    * objects describing the database.
204    *
205    * @return an <code>ArrayList</code> of <code>DatabaseProcedure</code>
206    */

207   public ArrayList JavaDoc getProcedures()
208   {
209     return procedures;
210   }
211
212   /**
213    * Returns the <code>DatabaseTable</code> object matching the given table
214    * name or <code>null</code> if not found.
215    *
216    * @param tableName the table name to look for
217    * @return a <code>DatabaseTable</code> value or null
218    */

219   public DatabaseTable getTable(String JavaDoc tableName)
220   {
221     if (tableName == null)
222       return null;
223
224     DatabaseTable t;
225     int size = tables.size();
226     for (int i = 0; i < size; i++)
227     {
228       t = (DatabaseTable) tables.get(i);
229       if (tableName.compareTo(t.getName()) == 0)
230         return t;
231     }
232     return null;
233   }
234
235   /**
236    * Returns the <code>DatabaseProcedure</code> object matching the given
237    * procedure name or <code>null</code> if not found.
238    *
239    * @param procedureName the procedure name to look for
240    * @return a <code>DatabaseProcedure</code> value or null
241    */

242   public DatabaseProcedure getProcedure(String JavaDoc procedureName)
243   {
244     if (procedureName == null)
245       return null;
246
247     DatabaseProcedure t;
248     int size = procedures.size();
249     for (int i = 0; i < size; i++)
250     {
251       t = (DatabaseProcedure) procedures.get(i);
252       if (procedureName.compareTo(t.getName()) == 0)
253         return t;
254     }
255     return null;
256   }
257
258   /**
259    * Returns the <code>DatabaseTable</code> object matching the given table
260    * name or <code>null</code> if not found. An extra boolean indicates if
261    * table name matching is case sensitive or not.
262    *
263    * @param tableName the table name to look for
264    * @param isCaseSensitive true if name matching must be case sensitive
265    * @return a <code>DatabaseTable</code> value or null
266    */

267   public DatabaseTable getTable(String JavaDoc tableName, boolean isCaseSensitive)
268   {
269     if (isCaseSensitive)
270       return getTable(tableName);
271
272     if (tableName == null)
273       return null;
274
275     DatabaseTable t;
276     int size = tables.size();
277     for (int i = 0; i < size; i++)
278     {
279       t = (DatabaseTable) tables.get(i);
280       if (tableName.equalsIgnoreCase(t.getName()))
281         return t;
282     }
283     return null;
284   }
285
286   /**
287    * Returns <code>true</code> if the given <code>TableName</code> is found
288    * in this schema.
289    *
290    * @param tableName the name of the table you are looking for
291    * @return <code>true</code> if the table has been found
292    */

293   public boolean hasTable(String JavaDoc tableName)
294   {
295     if (tableName == null)
296       return false;
297
298     DatabaseTable t;
299     int size = tables.size();
300     for (int i = 0; i < size; i++)
301     {
302       t = (DatabaseTable) tables.get(i);
303       if (tableName.equals(t.getName()))
304         return true;
305     }
306     return false;
307   }
308
309   /**
310    * Returns <code>true</code> if the given <code>ProcedureName</code> is
311    * found in this schema.
312    *
313    * @param procedureName the name of the procedure you are looking for
314    * @return <code>true</code> if the procedure has been found
315    */

316   public boolean hasProcedure(String JavaDoc procedureName)
317   {
318     if (procedureName == null)
319       return false;
320
321     DatabaseProcedure t;
322     int size = procedures.size();
323     for (int i = 0; i < size; i++)
324     {
325       t = (DatabaseProcedure) procedures.get(i);
326       if (procedureName.equals(t.getName()))
327         return true;
328     }
329     return false;
330   }
331
332   /**
333    * Checks if this <code>DatabaseSchema</code> is a compatible subset of a
334    * given schema. It means that all tables in this schema must be present with
335    * the same definition in the other schema.
336    *
337    * @param other the object to compare with
338    * @return <code>true</code> if the two schemas are compatible
339    */

340   public boolean isCompatibleSubset(DatabaseSchema other)
341   {
342     if (other == null)
343       return false;
344
345     DatabaseTable table, otherTable;
346     int size = tables.size();
347     for (int i = 0; i < size; i++)
348     {
349       // Parse all tables
350
table = (DatabaseTable) tables.get(i);
351       otherTable = other.getTable(table.getName());
352       if (otherTable == null)
353         return false; // Not present
354
else if (!table.equalsIgnoreType(otherTable))
355         return false; // Not compatible
356
}
357     DatabaseProcedure procedure, otherProcedure;
358     int sizep = procedures.size();
359     for (int i = 0; i < sizep; i++)
360     {
361       // Parse all procedures
362
procedure = (DatabaseProcedure) procedures.get(i);
363       otherProcedure = other.getProcedure(procedure.getName());
364       if (otherProcedure == null)
365         return false; // Not present
366
else if (!procedure.equals(otherProcedure))
367         return false; // Not compatible
368
}
369     return true; // Ok, all tables passed the test
370
}
371
372   /**
373    * Checks if this <code>DatabaseSchema</code> is compatible with the given
374    * schema. It means that all tables in this schema that are common with the
375    * other schema must be identical.
376    *
377    * @param other the object to compare with
378    * @return <code>true</code> if the two schemas are compatible
379    */

380   public boolean isCompatibleWith(DatabaseSchema other)
381   {
382     DatabaseTable table, otherTable;
383     int size = tables.size();
384     for (int i = 0; i < size; i++)
385     { // Parse all tables
386
table = (DatabaseTable) tables.get(i);
387       otherTable = other.getTable(table.getName());
388       if (otherTable == null)
389         continue; // Not present in other schema
390
else if (!table.equalsIgnoreType(otherTable))
391         return false; // Not compatible
392
}
393     DatabaseProcedure procedure, otherProcedure;
394     int sizep = procedures.size();
395     for (int i = 0; i < sizep; i++)
396     { // Parse all procedures
397
procedure = (DatabaseProcedure) procedures.get(i);
398       otherProcedure = other.getProcedure(procedure.getName());
399       if (otherProcedure == null)
400         continue; // Not present
401
else if (!procedure.equals(otherProcedure))
402         return false; // Not compatible
403
}
404     return true; // Ok, all tables passed the test
405
}
406
407   /**
408    * Two <code>DatabaseSchema</code> are considered equal if they have the
409    * same tables and the same procedures.
410    *
411    * @param other the object to compare with
412    * @return <code>true</code> if the schemas are equals
413    */

414   public boolean equals(Object JavaDoc other)
415   {
416     boolean equal = true;
417     if ((other == null) || !(other instanceof DatabaseSchema))
418       return false;
419     if (tables == null)
420       equal &= ((DatabaseSchema) other).getTables() == null;
421     else
422       equal &= tables.equals(((DatabaseSchema) other).getTables());
423     if (procedures == null)
424       equal &= ((DatabaseSchema) other).getProcedures() == null;
425     else
426       equal &= procedures.equals(((DatabaseSchema) other).getProcedures());
427     return equal;
428   }
429
430   /**
431    * Get xml information about this schema.
432    *
433    * @return xml formatted information on this database schema.
434    */

435   public String JavaDoc getXml()
436   {
437     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
438     info.append("<" + DatabasesXmlTags.ELT_DatabaseStaticSchema + ">");
439     for (int i = 0; i < procedures.size(); i++)
440       info.append(((DatabaseProcedure) procedures.get(i)).getXml());
441     for (int i = 0; i < tables.size(); i++)
442       info.append(((DatabaseTable) tables.get(i)).getXml());
443     info.append("</" + DatabasesXmlTags.ELT_DatabaseStaticSchema + ">");
444     return info.toString();
445   }
446
447 }
Popular Tags