001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Emmanuel Cecchet.
020: * Contributor(s): ______________________________________.
021: */package org.continuent.sequoia.controller.cache.result.schema;
022:
023: import java.sql.SQLException;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Iterator;
027:
028: import org.continuent.sequoia.common.sql.schema.DatabaseSchema;
029: import org.continuent.sequoia.common.sql.schema.DatabaseTable;
030:
031: /**
032: * A <code>CacheDatabaseSchema</code> describes all the tables and columns of
033: * a database and its associated cache entries.
034: *
035: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
036: * @version 1.0
037: */
038: public class CacheDatabaseSchema {
039: /** Database tables. */
040: private ArrayList tables;
041:
042: /**
043: * Creates a new <code>CacheDatabaseSchema</code> instance by cloning an
044: * existing <code>DatabaseSchema</code>.
045: *
046: * @param dbs the <code>DatabaseSchema</code> to clone
047: */
048: public CacheDatabaseSchema(DatabaseSchema dbs) {
049: if (dbs == null) {
050: tables = new ArrayList();
051: return;
052: }
053:
054: // Clone the tables
055: Collection origTables = dbs.getTables().values();
056: int size = origTables.size();
057: tables = new ArrayList(size);
058: for (Iterator iter = origTables.iterator(); iter.hasNext();)
059: for (int i = 0; i < size; i++)
060: tables.add(new CacheDatabaseTable((DatabaseTable) iter
061: .next()));
062: }
063:
064: /**
065: * Adds a <code>CacheDatabaseTable</code> describing a table of the
066: * database.
067: *
068: * @param table the table to add
069: */
070: public void addTable(CacheDatabaseTable table) {
071: tables.add(table);
072: }
073:
074: /**
075: * Removes a <code>CacheDatabaseTable</code> describing a table of the
076: * database.
077: *
078: * @param table the table to remove
079: */
080: public void removeTable(CacheDatabaseTable table) {
081: tables.remove(table);
082: }
083:
084: /**
085: * Merge the given schema with the current one. All missing tables or columns
086: * are added if no conflict is detected. An exception is thrown if the given
087: * schema definition conflicts with the current one.
088: *
089: * @param databaseSchema the schema to merge
090: * @throws SQLException if the schemas conflict
091: */
092: public void mergeSchema(CacheDatabaseSchema databaseSchema)
093: throws SQLException {
094: if (databaseSchema == null)
095: return;
096:
097: ArrayList otherTables = databaseSchema.getTables();
098: if (otherTables == null)
099: return;
100:
101: int size = otherTables.size();
102: for (int i = 0; i < size; i++) {
103: CacheDatabaseTable t = (CacheDatabaseTable) otherTables
104: .get(i);
105: CacheDatabaseTable original = getTable(t.getName());
106: if (original == null)
107: addTable(t);
108: else
109: original.mergeColumns(t);
110: }
111: }
112:
113: /**
114: * Returns an <code>ArrayList</code> of <code>CacheDatabaseTable</code>
115: * objects describing the database.
116: *
117: * @return an <code>ArrayList</code> of <code>CacheDatabaseTable</code>
118: */
119: public ArrayList getTables() {
120: return tables;
121: }
122:
123: /**
124: * Returns the <code>CacheDatabaseTable</code> object matching the given
125: * table name or <code>null</code> if not found.
126: *
127: * @param tableName the table name to look for
128: * @return a <code>CacheDatabaseTable</code> value or null
129: */
130: public CacheDatabaseTable getTable(String tableName) {
131: if (tableName == null)
132: return null;
133:
134: int size = tables.size();
135: for (int i = 0; i < size; i++) {
136: CacheDatabaseTable t = (CacheDatabaseTable) tables.get(i);
137: if (t.getName().compareTo(tableName) == 0)
138: return t;
139: }
140: return null;
141: }
142:
143: /**
144: * Returns <code>true</code> if the given <code>TableName</code> is found
145: * in this schema.
146: *
147: * @param tableName the name of the table you are looking for
148: * @return <code>true</code> if the table has been found
149: */
150: public boolean hasTable(String tableName) {
151: int size = tables.size();
152: for (int i = 0; i < size; i++) {
153: CacheDatabaseTable t = (CacheDatabaseTable) tables.get(i);
154: if (tableName.equals(t.getName()))
155: return true;
156: }
157: return false;
158: }
159:
160: /**
161: * Two <code>CacheDatabaseSchema</code> are equals if they have the same
162: * tables.
163: *
164: * @param other the object to compare with
165: * @return true if the 2 objects are the same.
166: */
167: public boolean equals(Object other) {
168: if (!(other instanceof CacheDatabaseSchema))
169: return false;
170:
171: if (tables == null)
172: return ((CacheDatabaseSchema) other).getTables() == null;
173: else
174: return tables.equals(((CacheDatabaseSchema) other)
175: .getTables());
176: }
177:
178: /**
179: * Returns information about the database schema.
180: *
181: * @param longFormat <code>true</code> for a long format, false for a short
182: * summary
183: * @return a <code>String</code> value
184: */
185: public String getInformation(boolean longFormat) {
186: String result = "";
187: int size = tables.size();
188: for (int i = 0; i < size; i++) {
189: CacheDatabaseTable t = (CacheDatabaseTable) tables.get(i);
190: result += t.getInformation(longFormat) + "\n";
191: }
192: return result;
193: }
194:
195: }
|