1 24 25 package org.continuent.sequoia.controller.sql.schema; 26 27 import java.io.Serializable ; 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 32 import org.continuent.sequoia.controller.locks.TransactionLogicalLock; 33 34 45 public class DatabaseTable implements Serializable 46 { 47 private static final long serialVersionUID = 7138810420058450235L; 48 49 50 private String schema; 51 52 53 private String name; 54 55 56 private ArrayList columns; 57 58 59 private transient TransactionLogicalLock lock = new TransactionLogicalLock(); 60 61 66 private ArrayList dependingTables; 67 68 73 public DatabaseTable(String name) 74 { 75 this(name, new ArrayList ()); 76 } 77 78 84 public DatabaseTable(String name, int nbOfColumns) 85 { 86 this(name, new ArrayList (nbOfColumns)); 87 } 88 89 95 public DatabaseTable(DatabaseTable dt) 96 { 97 this(dt.getName(), dt.getColumns()); 98 if (dt.getDependingTables() != null) 99 for (Iterator i = dt.getDependingTables().iterator(); i.hasNext();) 100 addDependingTable((String ) i.next()); 101 } 102 103 109 private DatabaseTable(String name, ArrayList columns) 110 { 111 if (name == null) 112 throw new IllegalArgumentException ( 113 "Illegal null database table name in DatabaseTable constructor"); 114 115 this.name = name; 116 this.columns = columns; 117 } 118 119 128 public synchronized void addDependingTable(String tableName) 129 { 130 if (dependingTables == null) 131 dependingTables = new ArrayList (); 132 if (!dependingTables.contains(tableName)) 133 dependingTables.add(tableName); 134 } 135 136 143 public void addColumn(DatabaseColumn column) 144 { 145 columns.add(column); 146 } 147 148 156 public ArrayList getColumns() 157 { 158 return columns; 159 } 160 161 168 public DatabaseColumn getColumn(String columnName) 169 { 170 DatabaseColumn c; 171 for (Iterator i = columns.iterator(); i.hasNext();) 172 { 173 c = (DatabaseColumn) i.next(); 174 if (columnName.equalsIgnoreCase(c.getName())) 175 return c; 176 177 } 178 return null; 179 } 180 181 189 public DatabaseColumn getColumn(String columnName, boolean isCaseSensitive) 190 { 191 if (!isCaseSensitive) 192 return getColumn(columnName); 193 194 DatabaseColumn c; 195 for (Iterator i = columns.iterator(); i.hasNext();) 196 { 197 c = (DatabaseColumn) i.next(); 198 if (columnName.equals(c.getName())) 199 return c; 200 201 } 202 return null; 203 } 204 205 210 public ArrayList getDependingTables() 211 { 212 return dependingTables; 213 } 214 215 220 public TransactionLogicalLock getLock() 221 { 222 return lock; 223 } 224 225 230 void setLock(DatabaseTable oldTable) 231 { 232 lock = oldTable.lock; 233 } 234 235 240 public String getName() 241 { 242 return name; 243 } 244 245 250 public String getSchema() 251 { 252 return schema; 253 } 254 255 260 public void setSchema(String schema) 261 { 262 this.schema = schema; 263 } 264 265 274 public ArrayList getUniqueColumns() 275 { 276 ArrayList cols = new ArrayList (); 277 Iterator i; 278 DatabaseColumn c; 279 280 for (i = columns.iterator(); i.hasNext();) 281 { 282 c = (DatabaseColumn) i.next(); 283 if (c.isUnique()) 284 cols.add(c); 285 } 286 return cols; 287 } 288 289 299 public void merge(DatabaseTable table) throws SQLException 300 { 301 if (table == null) 302 return; 303 304 if (dependingTables == null) 305 dependingTables = table.getDependingTables(); 306 else 307 { 308 ArrayList otherDependingTables = table.getDependingTables(); 309 if (otherDependingTables != null) 310 for (Iterator iter = otherDependingTables.iterator(); iter.hasNext();) 311 addDependingTable((String ) iter.next()); 312 } 313 314 ArrayList otherColumns = table.getColumns(); 315 if (otherColumns == null) 316 return; 317 318 DatabaseColumn c, original; 319 int size = otherColumns.size(); 320 for (int i = 0; i < size; i++) 321 { 322 c = (DatabaseColumn) otherColumns.get(i); 323 original = getColumn(c.getName()); 324 if (original == null) 325 addColumn(c); 326 else 327 { 328 if (!original.equalsIgnoreType(c)) 329 throw new SQLException ("Unable to merge table [" + table.getName() 330 + "]: column '" + c.getName() + "' definition mismatch"); 331 } 332 } 333 } 334 335 343 public void removeColumn(String columnName) 344 { 345 columns.remove(getColumn(columnName)); 346 } 347 348 355 public void removeColumn(DatabaseColumn column) 356 { 357 columns.remove(column); 358 } 359 360 370 public void updateColumns(DatabaseTable table) 371 { 372 if (table == null) 373 return; 374 375 ArrayList otherColumns = table.getColumns(); 376 377 for (Iterator iter = columns.iterator(); iter.hasNext();) 379 { 380 DatabaseColumn c = (DatabaseColumn) iter.next(); 381 if (!otherColumns.contains(c)) 382 iter.remove(); 383 } 384 385 int size = otherColumns.size(); 387 for (int i = 0; i < size; i++) 388 { 389 DatabaseColumn c = (DatabaseColumn) otherColumns.get(i); 390 DatabaseColumn originalColumn = getColumn(c.getName()); 391 if (originalColumn == null) 392 addColumn(c); 393 else 394 { 395 originalColumn.setType(c.getType()); 396 originalColumn.setIsUnique(c.isUnique()); 397 } 398 } 399 } 400 401 408 public boolean equals(Object other) 409 { 410 if ((other == null) || !(other instanceof DatabaseTable)) 411 return false; 412 413 DatabaseTable t = (DatabaseTable) other; 414 415 if (!name.equals(t.getName())) 417 return false; 418 419 if (t.getSchema() == null) 421 { 422 if (schema != null) 423 return false; 424 } 425 else 426 { 427 if (!t.getSchema().equals(schema)) 428 return false; 429 } 430 if (t.getColumns() == null) 432 return columns == null; 433 else 434 return t.getColumns().equals(columns); 435 } 436 437 445 public boolean equalsIgnoreType(Object other) 446 { 447 if ((other == null) || !(other instanceof DatabaseTable)) 448 return false; 449 450 DatabaseTable t = (DatabaseTable) other; 451 if (!name.equals(t.getName())) 453 return false; 454 455 if (t.getSchema() == null) 457 { 458 if (schema != null) 459 return false; 460 } 461 else 462 { 463 if (!t.getSchema().equals(schema)) 464 return false; 465 } 466 467 DatabaseColumn c1, c2; 468 Iterator iter = columns.iterator(); 469 while (iter.hasNext()) 470 { 471 c1 = (DatabaseColumn) iter.next(); 472 c2 = t.getColumn(c1.getName()); 473 474 if (!c1.equalsIgnoreType(c2)) 475 return false; } 477 return true; 478 } 479 480 483 public String toString() 484 { 485 return name + "(" + columns + ")"; 486 } 487 } 488 | Popular Tags |