1 24 25 package org.objectweb.cjdbc.common.sql.schema; 26 27 import java.io.Serializable ; 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 31 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 32 33 41 public class DatabaseSchema implements Serializable 42 { 43 private static final long serialVersionUID = 1105453274994163661L; 44 45 46 private ArrayList tables; 47 48 private ArrayList procedures; 49 50 53 public DatabaseSchema() 54 { 55 tables = new ArrayList (); 56 procedures = new ArrayList (); 57 } 58 59 65 public DatabaseSchema(int nbOfTables) 66 { 67 tables = new ArrayList (nbOfTables); 68 procedures = new ArrayList (); 69 } 70 71 77 public DatabaseSchema(DatabaseSchema schema) 78 { 79 if (schema == null) 80 throw new IllegalArgumentException ( 81 "Illegal null database schema in DatabaseSchema(DatabaseSchema) constructor"); 82 83 tables = new ArrayList (schema.getTables()); 84 procedures = new ArrayList (schema.getProcedures()); 85 } 86 87 92 public void addTable(DatabaseTable table) 93 { 94 if (table == null) 95 throw new IllegalArgumentException ( 96 "Illegal null database table in addTable(DatabaseTable) method"); 97 tables.add(table); 98 } 99 100 106 public void addProcedure(DatabaseProcedure procedure) 107 { 108 if (procedure == null) 109 throw new IllegalArgumentException ( 110 "Illegal null database table in addTable(DatabaseTable) method"); 111 procedures.add(procedure); 112 } 113 114 120 public boolean removeTable(DatabaseTable table) 121 { 122 if (table == null) 123 throw new IllegalArgumentException ( 124 "Illegal null database table in removeTable(DatabaseTable) method"); 125 return tables.remove(table); 126 } 127 128 135 public boolean removeProcedure(DatabaseProcedure procedure) 136 { 137 if (procedure == null) 138 throw new IllegalArgumentException ( 139 "Illegal null database procedure in removeProcedure(DatabaseProcedure) method"); 140 return procedures.remove(procedure); 141 } 142 143 151 public void mergeSchema(DatabaseSchema databaseSchema) throws SQLException 152 { 153 if (databaseSchema == null) 154 throw new IllegalArgumentException ( 155 "Illegal null database schema in mergeSchema(DatabaseSchema) method"); 156 157 ArrayList 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 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 196 public ArrayList getTables() 197 { 198 return tables; 199 } 200 201 207 public ArrayList getProcedures() 208 { 209 return procedures; 210 } 211 212 219 public DatabaseTable getTable(String 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 242 public DatabaseProcedure getProcedure(String 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 267 public DatabaseTable getTable(String 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 293 public boolean hasTable(String 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 316 public boolean hasProcedure(String 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 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 table = (DatabaseTable) tables.get(i); 351 otherTable = other.getTable(table.getName()); 352 if (otherTable == null) 353 return false; else if (!table.equalsIgnoreType(otherTable)) 355 return false; } 357 DatabaseProcedure procedure, otherProcedure; 358 int sizep = procedures.size(); 359 for (int i = 0; i < sizep; i++) 360 { 361 procedure = (DatabaseProcedure) procedures.get(i); 363 otherProcedure = other.getProcedure(procedure.getName()); 364 if (otherProcedure == null) 365 return false; else if (!procedure.equals(otherProcedure)) 367 return false; } 369 return true; } 371 372 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 { table = (DatabaseTable) tables.get(i); 387 otherTable = other.getTable(table.getName()); 388 if (otherTable == null) 389 continue; else if (!table.equalsIgnoreType(otherTable)) 391 return false; } 393 DatabaseProcedure procedure, otherProcedure; 394 int sizep = procedures.size(); 395 for (int i = 0; i < sizep; i++) 396 { procedure = (DatabaseProcedure) procedures.get(i); 398 otherProcedure = other.getProcedure(procedure.getName()); 399 if (otherProcedure == null) 400 continue; else if (!procedure.equals(otherProcedure)) 402 return false; } 404 return true; } 406 407 414 public boolean equals(Object 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 435 public String getXml() 436 { 437 StringBuffer info = new StringBuffer (); 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 |