1 19 20 package org.netbeans.modules.dbschema; 21 22 import java.util.ArrayList ; 23 import java.util.ResourceBundle ; 24 import java.text.MessageFormat ; 25 26 28 public final class TableElement extends DBElement implements ColumnElementHolder, ColumnPairElementHolder { 29 32 public static final boolean TABLE = true; 33 34 37 public static final boolean VIEW = false; 38 39 40 private transient SchemaElement declaringSchema; 41 42 44 public TableElement() { 45 this(new Memory(), null); 46 } 47 48 52 public TableElement(Impl impl, SchemaElement declaringSchema) { 53 super(impl); 54 this.declaringSchema = declaringSchema; 55 } 56 57 60 final Impl getTableImpl() { 61 return (Impl) getElementImpl(); 62 } 63 64 69 public static TableElement forName(String name, SchemaElement schema) { 70 int pos = name.lastIndexOf("."); 71 if (pos == -1) 72 return null; 73 else 74 name = name.substring(pos + 1); 75 76 TableElement[] tes = schema.getTables(); 77 for (int i = 0; i < tes.length; i++) 78 if (tes[i].getName().getName().trim().equals(name)) 79 return tes[i]; 80 81 return null; 82 } 83 84 88 public static TableElement forName(String name) { 89 int index = name.lastIndexOf("."); 91 if (index == -1) { 92 if (Boolean.getBoolean("netbeans.debug.exceptions")) System.out.println(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("FullyQualifiedName")); return null; 95 } 96 97 SchemaElement se = SchemaElement.forName(name.substring(0, index)); 98 99 if (se == null) 100 return null; 102 else 103 return TableElement.forName(name, se); 104 } 105 106 110 public void setTableOrView(boolean isTable) throws DBException { 111 getTableImpl().setTableOrView(isTable); 112 } 113 114 117 public boolean isTableOrView() { 118 return getTableImpl().isTableOrView(); 119 } 120 121 125 public boolean isTable() { 126 return getTableImpl().isTableOrView(); 127 } 128 129 133 public boolean isView() { 134 return !getTableImpl().isTableOrView(); 135 } 136 137 139 141 142 145 public final SchemaElement getDeclaringSchema() { 146 return declaringSchema; 147 } 148 149 152 public final void setDeclaringSchema(SchemaElement se) { 153 if (declaringSchema == null) 154 declaringSchema = se; 155 } 156 157 159 163 public void addColumn(ColumnElement el) throws DBException { 164 addColumns(new ColumnElement[] {el}); 165 } 166 167 171 public void addColumns(final ColumnElement[] els) throws DBException { 172 for (int i = 0; i < els.length; i++) { 173 if (getColumn(els[i].getName()) != null) 174 throwAddException("FMT_EXC_AddColumn", els[i]); if (els[i].getDeclaringTable() == null) 176 els[i].setDeclaringTable(this); 177 } 178 getTableImpl().changeColumns(els, Impl.ADD); 179 } 180 181 185 public void removeColumn(ColumnElement el) throws DBException { 186 removeColumns(new ColumnElement[] {el}); 187 } 188 189 193 public void removeColumns(final ColumnElement[] els) throws DBException { 194 getTableImpl().changeColumns(els, Impl.REMOVE); 195 } 196 197 202 public void setColumns(ColumnElement[] els) throws DBException { 203 getTableImpl().changeColumns(els, Impl.SET); 204 } 205 206 209 public ColumnElement[] getColumns() { 210 return getTableImpl().getColumns(); 211 } 212 213 217 public ColumnElement getColumn(DBIdentifier name) { 218 return getTableImpl().getColumn(name); 219 } 220 221 223 227 public void addIndex(IndexElement el) throws DBException { 228 addIndexes(new IndexElement[] {el}); 229 } 230 231 235 public void addIndexes(final IndexElement[] els) throws DBException { 236 for (int i = 0; i < els.length; i++) { 237 if (getIndex(els[i].getName()) != null) 238 throwAddException("FMT_EXC_AddColumn", els[i]); if (els[i].getDeclaringTable() == null) 240 els[i].setDeclaringTable(this); 241 } 242 243 getTableImpl().changeIndexes(els, Impl.ADD); 244 } 245 246 250 public void removeIndex(IndexElement el) throws DBException { 251 removeIndexes(new IndexElement[] {el}); 252 } 253 254 258 public void removeIndexes(final IndexElement[] els) throws DBException { 259 getTableImpl().changeIndexes(els, Impl.REMOVE); 260 } 261 262 267 public void setIndexes(IndexElement[] els) throws DBException { 268 getTableImpl().changeIndexes(els, Impl.SET); 269 } 270 271 274 public IndexElement[] getIndexes() { 275 return getTableImpl().getIndexes(); 276 } 277 278 282 public IndexElement getIndex(DBIdentifier name) { 283 return getTableImpl().getIndex(name); 284 } 285 286 288 292 public void addKey(KeyElement el) throws DBException { 293 addKeys(new KeyElement[]{el}); 294 } 295 296 300 public void addKeys(final KeyElement[] els) throws DBException { 301 for (int i = 0; i < els.length; i++) { 302 if (getKey(els[i].getName()) != null) 303 throwAddException("FMT_EXC_AddColumn", els[i]); if (els[i].getDeclaringTable() == null) 305 els[i].setDeclaringTable(this); 306 if (els[i] instanceof UniqueKeyElement) 307 if (((UniqueKeyElement) els[i]).getAssociatedIndex() == null) { 308 IndexElement ie = new IndexElement(); 309 try { 310 ie.setName(els[i].getName()); 311 ie.setColumns(els[i].getColumns()); 312 this.addIndex(ie); 313 } catch (DBException exc) { 314 exc.printStackTrace(); 315 } 316 ((UniqueKeyElement) els[i]).setAssociatedIndex(ie); 317 } 318 } 319 320 getTableImpl().changeKeys(els, Impl.ADD); 321 } 322 323 327 public void removeKey(KeyElement el) throws DBException { 328 removeKeys(new KeyElement[] {el}); 329 } 330 331 335 public void removeKeys(final KeyElement[] els) throws DBException { 336 getTableImpl().changeKeys(els, Impl.REMOVE); 337 } 338 339 344 345 public void setKeys(KeyElement[] els) throws DBException { 346 getTableImpl().changeKeys(els, Impl.SET); 347 } 348 349 352 public KeyElement[] getKeys() { 353 return getTableImpl().getKeys(); 354 } 355 356 360 public KeyElement getKey(DBIdentifier name) { 361 return getTableImpl().getKey(name); 362 } 363 364 366 370 private ArrayList getKeys(Class subtype) { 371 KeyElement[] keys = getKeys(); 372 373 if (keys == null) 374 return null; 375 376 int i, count = keys.length; 377 ArrayList subKeys = new ArrayList (count); 378 379 for (i = 0; i < count; i++) { 380 KeyElement key = keys[i]; 381 382 if (subtype.isInstance(key)) 383 subKeys.add(key); 384 } 385 386 return subKeys; 387 } 388 389 392 public ForeignKeyElement[] getForeignKeys() { 393 ArrayList keys = getKeys(ForeignKeyElement.class); 394 395 if (keys == null) 396 return null; 397 398 int count = keys.size(); 399 400 return ((ForeignKeyElement[]) keys.toArray(new ForeignKeyElement[count])); 401 } 402 403 407 public ForeignKeyElement getForeignKey(DBIdentifier name) { 408 ForeignKeyElement[] fks = getForeignKeys(); 409 int i, count = fks.length; 410 411 for (i = 0; i < count; i++) { 412 ForeignKeyElement fk = fks[i]; 413 414 if (name.equals(fk.getName())) 415 return fk; 416 } 417 418 return null; 419 } 420 421 424 public UniqueKeyElement[] getUniqueKeys() { 425 ArrayList keys = getKeys(UniqueKeyElement.class); 426 427 if (keys == null) 428 return null; 429 430 return ((UniqueKeyElement[]) keys.toArray(new UniqueKeyElement[keys.size()])); 431 } 432 433 437 public UniqueKeyElement getUniqueKey(DBIdentifier name) { 438 UniqueKeyElement[] uks = getUniqueKeys(); 439 int i, count = uks.length; 440 441 for (i = 0; i < count; i++) { 442 UniqueKeyElement uk = uks[i]; 443 444 if (name.equals(uk.getName())) 445 return uk; 446 } 447 448 return null; 449 } 450 451 454 public UniqueKeyElement getPrimaryKey() { 455 UniqueKeyElement[] uks = getUniqueKeys(); 456 457 if (uks == null) 458 return null; 459 460 for (int i = 0; i < uks.length; i++) { 461 UniqueKeyElement uk = uks[i]; 462 463 if (uk.isPrimaryKey()) 464 return uk; 465 } 466 467 return null; 468 } 469 470 472 478 private void throwAddException(String formatKey, DBMemberElement element) throws DBException { 479 String msg = element.getName().getName(); throw new DBException(msg); 482 } 483 484 487 public String toString() { 488 if (getName() != null) 489 return getName().toString(); 490 else 491 return null; 492 } 493 494 498 public void addColumnPair(ColumnPairElement pair) throws DBException { 499 addColumnPairs(new ColumnPairElement[] {pair}); 500 } 501 502 506 public void addColumnPairs(ColumnPairElement[] pairs) throws DBException { 507 for (int i = 0; i < pairs.length; i++) { 508 if (getColumnPair(pairs[i].getName()) != null) 509 throwAddException("FMT_EXC_AddColumn", pairs[i]); if (pairs[i].getDeclaringTable() == null) 511 pairs[i].setDeclaringTable(this); 512 } 513 getTableImpl().changeColumnPairs(pairs, Impl.ADD); 514 } 515 516 520 public void removeColumnPair(ColumnPairElement pair) throws DBException { 521 removeColumnPairs(new ColumnPairElement[] {pair}); 522 } 523 524 528 public void removeColumnPairs(ColumnPairElement[] pairs) throws DBException { 529 getTableImpl().changeColumnPairs(pairs, Impl.REMOVE); 530 } 531 532 537 public void setColumnPairs(ColumnPairElement[] pairs) throws DBException { 538 getTableImpl().changeColumnPairs(pairs, Impl.SET); 539 } 540 541 544 public ColumnPairElement[] getColumnPairs() { 545 return getTableImpl().getColumnPairs(); 546 } 547 548 552 public ColumnPairElement getColumnPair(DBIdentifier name) { 553 return getTableImpl().getColumnPair(name); 554 } 555 556 560 public DBMemberElement getMember(DBIdentifier name) { 561 int index = name.getName().indexOf(";"); 563 if (index == -1) 564 return getColumn(name); 566 else 567 return getColumnPair(name); 569 } 570 571 574 public static interface Impl extends DBElement.Impl { 575 579 public void setTableOrView(boolean isTable) throws DBException; 580 581 584 public boolean isTableOrView(); 585 586 591 public void changeColumns(ColumnElement[] elems, int action) throws DBException; 592 593 596 public ColumnElement[] getColumns(); 597 598 602 public ColumnElement getColumn(DBIdentifier name); 603 604 609 public void changeIndexes(IndexElement[] elems, int action) throws DBException; 610 611 614 public IndexElement[] getIndexes(); 615 616 620 public IndexElement getIndex(DBIdentifier name); 621 622 627 public void changeKeys(KeyElement[] elems, int action) throws DBException; 628 629 632 public KeyElement[] getKeys(); 633 634 638 public KeyElement getKey(DBIdentifier name); 639 640 645 public void changeColumnPairs(ColumnPairElement[] pairs, int action) throws DBException; 646 647 650 public ColumnPairElement[] getColumnPairs(); 651 652 656 public ColumnPairElement getColumnPair(DBIdentifier name); 657 } 658 659 661 static final class Memory extends DBElement.Memory implements Impl { 662 663 private boolean _isTable; 664 665 666 private DBMemoryCollection.Column columns; 667 668 669 private DBMemoryCollection.Index indexes; 670 671 672 private DBMemoryCollection.Key keys; 673 674 675 private DBMemoryCollection.ColumnPair pairs; 676 677 679 public Memory() { 680 super(); 681 _isTable = true; 682 } 683 684 687 public Memory(TableElement el) { 688 super(el); 689 _isTable = el.isTableOrView(); 690 } 691 692 696 public void setTableOrView(boolean isTable) { 697 boolean old = _isTable; 698 699 _isTable = isTable; 700 firePropertyChange(PROP_TABLE_OR_VIEW, Boolean.valueOf(old), Boolean.valueOf(isTable)); 701 } 702 703 706 public boolean isTableOrView() { 707 return _isTable; 708 } 709 710 715 public synchronized void changeColumns(ColumnElement[] elems, int action) throws DBException { 716 initColumns(); 717 columns.change(elems, action); 718 } 719 720 723 public synchronized ColumnElement[] getColumns() { 724 initColumns(); 725 return (ColumnElement[]) columns.getElements(); 726 } 727 728 732 public synchronized ColumnElement getColumn(DBIdentifier name) { 733 initColumns(); 734 return (ColumnElement) columns.getElement(name); 735 } 736 737 739 void initColumns() { 740 if (columns == null) 741 columns = new DBMemoryCollection.Column(this); 742 } 743 744 749 public synchronized void changeIndexes(IndexElement[] elems, int action) throws DBException { 750 initIndexes(); 751 indexes.change(elems, action); 752 } 753 754 757 public synchronized IndexElement[] getIndexes() { 758 initIndexes(); 759 return (IndexElement[]) indexes.getElements(); 760 } 761 762 766 public synchronized IndexElement getIndex(DBIdentifier name) { 767 initIndexes(); 768 return (IndexElement) indexes.getElement(name); 769 } 770 771 773 void initIndexes() { 774 if (indexes == null) 775 indexes = new DBMemoryCollection.Index(this); 776 } 777 778 783 public synchronized void changeKeys(KeyElement[] elems, int action) throws DBException { 784 initKeys(); 785 keys.change(elems, action); 786 } 787 788 791 public synchronized KeyElement[] getKeys() { 792 initKeys(); 793 return (KeyElement[]) keys.getElements(); 794 } 795 796 800 public synchronized KeyElement getKey(DBIdentifier name) { 801 initKeys(); 802 return (KeyElement) keys.getElement(name); 803 } 804 805 807 void initKeys() { 808 if (keys == null) 809 keys = new DBMemoryCollection.Key(this); 810 } 811 812 815 final TableElement getTableElement() { 816 return (TableElement) _element; 817 } 818 819 822 public synchronized ColumnPairElement[] getColumnPairs() { 823 initColumnPairs(); 824 return (ColumnPairElement[]) pairs.getElements(); 825 } 826 827 831 public synchronized ColumnPairElement getColumnPair(DBIdentifier name) { 832 initColumnPairs(); 833 return (ColumnPairElement) pairs.getElement(name); 834 } 835 836 841 public synchronized void changeColumnPairs(ColumnPairElement[] pairs, int action) throws DBException { 842 initColumnPairs(); 843 this.pairs.change(pairs, action); 844 } 845 846 848 void initColumnPairs() { 849 if (pairs == null) 850 pairs = new DBMemoryCollection.ColumnPair(this); 851 } 852 } 853 } 854 | Popular Tags |