1 65 66 67 package org.hsqldb.util; 68 69 import java.applet.Applet ; 70 import java.util.Enumeration ; 71 import java.util.Vector ; 72 73 import java.awt.BorderLayout ; 74 import java.awt.Button ; 75 import java.awt.Checkbox ; 76 import java.awt.Component ; 77 import java.awt.Dimension ; 78 import java.awt.FileDialog ; 79 import java.awt.Font ; 80 import java.awt.Frame ; 81 import java.awt.GridLayout ; 82 import java.awt.Image ; 83 import java.awt.ItemSelectable ; 84 import java.awt.Label ; 85 import java.awt.Menu ; 86 import java.awt.MenuBar ; 87 import java.awt.MenuItem ; 88 import java.awt.Panel ; 89 import java.awt.SystemColor ; 90 import java.awt.TextField ; 91 import java.awt.Toolkit ; 92 import java.awt.event.ActionEvent ; 93 import java.awt.event.ActionListener ; 94 import java.awt.event.ItemEvent ; 95 import java.awt.event.ItemListener ; 96 import java.awt.event.WindowEvent ; 97 import java.awt.event.WindowListener ; 98 import java.awt.image.MemoryImageSource ; 99 100 111 119 public class Transfer extends Applet 120 implements WindowListener , ActionListener , ItemListener , Traceable { 121 122 Frame fMain; 123 Image imgEmpty; 124 DataAccessPoint sourceDb; 125 DataAccessPoint targetDb; 126 TransferTable tCurrent; 127 int iMaxRows; 128 int iSelectionStep; 129 Vector tTable; 130 java.awt.List lTable; 131 String [] sSourceSchemas; 132 String sSourceCatalog, sDestSchema, sDestCatalog; 133 TextField tSourceTable, tDestTable, tDestDropIndex, tDestCreateIndex; 134 TextField tDestDrop, tDestCreate, tDestDelete, tDestAlter; 135 TextField tSourceSelect, tDestInsert; 136 Checkbox cTransfer, cDrop, cCreate, cDelete, cInsert, cAlter; 137 Checkbox cCreateIndex, cDropIndex; 138 Checkbox cFKForced, cIdxForced; 139 Button bStart, bContinue; 140 TextField tMessage; 141 int iTransferMode; 142 static boolean bMustExit; 143 int CurrentTransfer, CurrentAlter; 144 static final int SELECT_SOURCE_CATALOG = 1; 145 static final int SELECT_SOURCE_SCHEMA = 2; 146 static final int SELECT_DEST_CATALOG = 3; 147 static final int SELECT_DEST_SCHEMA = 4; 148 static final int SELECT_SOURCE_TABLES = 5; 149 static final int TRFM_TRANSFER = 1; 150 static final int TRFM_DUMP = 2; 151 static final int TRFM_RESTORE = 3; 152 153 159 public void trace(String s) { 160 161 if ((s != null) &&!s.equals("")) { 162 tMessage.setText(s); 163 164 if (TRACE) { 165 System.out.println(s); 166 } 167 } 168 } 169 170 174 public void init() { 175 176 Transfer m = new Transfer(); 177 178 m._main(null); 179 } 180 181 185 public static void work(String [] arg) { 186 187 Transfer m = new Transfer(); 188 189 m._main(arg); 190 } 191 192 198 public static void main(String [] arg) { 199 200 System.getProperties().put("sun.java2d.noddraw", "true"); 201 202 bMustExit = true; 203 204 work(arg); 205 } 206 207 private boolean CatalogToSelect() { 208 209 Vector result = null; 210 211 try { 212 lTable.removeAll(); 213 214 if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) { 215 result = sourceDb.getCatalog(); 216 } else if (iSelectionStep == Transfer.SELECT_DEST_CATALOG) { 217 result = targetDb.getCatalog(); 218 } else { 219 Exit(); 220 } 221 222 if (result.size() > 1) { 223 lTable.setMultipleMode(true); 224 225 if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) { 226 bStart.setLabel("Select Catalog: Source"); 227 } else { 228 bStart.setLabel("Select Catalog: Destination"); 229 } 230 231 bStart.invalidate(); 232 bStart.setEnabled(true); 233 234 for (Enumeration e = 235 result.elements(); e.hasMoreElements(); ) { 236 lTable.add(e.nextElement().toString()); 237 } 238 239 lTable.repaint(); 240 trace("Select correct Catalog"); 241 } else { 242 if (result.size() == 1) { 243 if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) { 244 sSourceCatalog = (String ) result.firstElement(); 245 sSourceSchemas = null; 246 } else { 247 sDestCatalog = (String ) result.firstElement(); 248 sDestSchema = null; 249 } 250 } else { 251 if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) { 252 sSourceCatalog = null; 253 sSourceSchemas = null; 254 } else { 255 sDestCatalog = null; 256 sDestSchema = null; 257 } 258 } 259 260 if ((iSelectionStep == Transfer.SELECT_DEST_CATALOG) 261 && (sDestCatalog != null)) { 262 try { 263 targetDb.setCatalog(sDestCatalog); 264 } catch (Exception ex) { 265 trace("Catalog " + sSourceCatalog 266 + " could not be selected in the target database"); 267 268 sSourceCatalog = null; 269 } 270 } 271 272 iSelectionStep++; 273 274 ProcessNextStep(); 275 276 return false; 277 } 278 } catch (Exception exp) { 279 lTable.removeAll(); 280 trace("Exception reading catalog: " + exp); 281 exp.printStackTrace(); 282 } 283 284 return (lTable.getItemCount() > 0); 285 } 286 287 private boolean SchemaToSelect() { 288 289 Vector result = null; 290 291 try { 292 lTable.removeAll(); 293 294 if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) { 295 result = sourceDb.getSchemas(); 296 } else if (iSelectionStep == Transfer.SELECT_DEST_SCHEMA) { 297 result = targetDb.getSchemas(); 298 } else { 299 Exit(); 300 } 301 302 if (result.size() > 1) { 303 lTable.setMultipleMode(true); 304 305 if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) { 306 bStart.setLabel("Select Schema: Source"); 307 } else { 308 bStart.setLabel("Select Schema: Destination"); 309 } 310 311 bStart.invalidate(); 312 bStart.setEnabled(true); 313 314 for (Enumeration e = 315 result.elements(); e.hasMoreElements(); ) { 316 lTable.add(e.nextElement().toString()); 317 } 318 319 lTable.repaint(); 320 trace("Select correct Schema or load Settings file"); 321 } else { 322 if (result.size() == 1) { 323 if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) { 324 sSourceSchemas = new String [1]; 325 sSourceSchemas[0] = (String ) result.firstElement(); 326 } else { 327 sDestSchema = (String ) result.firstElement(); 328 } 329 } else { 330 if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) { 331 sSourceSchemas = null; 332 } else { 333 sDestSchema = null; 334 } 335 } 336 337 if (iTransferMode == TRFM_DUMP) { 338 iSelectionStep = Transfer.SELECT_SOURCE_TABLES; 339 } else { 340 iSelectionStep++; 341 } 342 343 ProcessNextStep(); 344 345 return false; 346 } 347 } catch (Exception exp) { 348 lTable.removeAll(); 349 trace("Exception reading schemas: " + exp); 350 exp.printStackTrace(); 351 } 352 353 return (lTable.getItemCount() > 0); 354 } 355 356 360 void _main(String [] arg) { 361 362 365 iTransferMode = TRFM_TRANSFER; 366 367 if ((arg != null) && (arg.length > 0)) { 368 if ((arg[0].toLowerCase().equals("-r")) 369 || (arg[0].toLowerCase().equals("--restore"))) { 370 iTransferMode = TRFM_RESTORE; 371 } else if ((arg[0].toLowerCase().equals("-d")) 372 || (arg[0].toLowerCase().equals("--dump"))) { 373 iTransferMode = TRFM_DUMP; 374 } 375 } 376 377 fMain = new Frame ("HSQL Transfer Tool"); 378 imgEmpty = createImage(new MemoryImageSource (2, 2, new int[4 * 4], 2, 379 2)); 380 381 fMain.setIconImage(imgEmpty); 382 fMain.addWindowListener(this); 383 fMain.setSize(640, 480); 384 fMain.add("Center", this); 385 386 MenuBar bar = new MenuBar (); 387 String [] extras = { 388 "Insert 10 rows only", "Insert 1000 rows only", "Insert all rows", 389 "-", "Load Settings...", "Save Settings...", "-", "Exit" 390 }; 391 Menu menu = new Menu ("Options"); 392 393 addMenuItems(menu, extras); 394 bar.add(menu); 395 fMain.setMenuBar(bar); 396 initGUI(); 397 398 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 399 Dimension size = fMain.getSize(); 400 401 if (d.width >= 640) { 403 fMain.setLocation((d.width - size.width) / 2, 404 (d.height - size.height) / 2); 405 } else { 406 fMain.setLocation(0, 0); 407 fMain.setSize(d); 408 } 409 410 fMain.setVisible(true); 411 412 CurrentTransfer = CurrentAlter = 0; 413 414 try { 415 if ((iTransferMode == TRFM_DUMP) 416 || (iTransferMode == TRFM_TRANSFER)) { 417 sourceDb = new TransferDb( 418 ConnectionDialog.createConnection( 419 fMain, "Source Database"), this); 420 421 if (!sourceDb.isConnected()) { 422 Exit(); 423 424 return; 425 } 426 } else { 427 FileDialog f = new FileDialog (fMain, "Restore FileName", 428 FileDialog.LOAD); 429 430 f.show(); 431 432 String sFileName = f.getFile(); 433 String Path = f.getDirectory(); 434 435 if ((sFileName == null) || (sFileName.equals(""))) { 436 Exit(); 437 438 return; 439 } else { 440 sourceDb = new TransferSQLText(Path + sFileName, this); 441 } 442 } 443 444 if ((iTransferMode == TRFM_RESTORE) 445 || (iTransferMode == TRFM_TRANSFER)) { 446 targetDb = new TransferDb( 447 ConnectionDialog.createConnection( 448 fMain, "Target Database"), this); 449 450 if (!targetDb.isConnected()) { 451 Exit(); 452 453 return; 454 } 455 } else { 456 FileDialog f = new FileDialog (fMain, "Dump FileName", 457 FileDialog.SAVE); 458 459 f.show(); 460 461 String sFileName = f.getFile(); 462 String Path = f.getDirectory(); 463 464 if ((sFileName == null) || (sFileName.equals(""))) { 465 Exit(); 466 467 return; 468 } else { 469 targetDb = new TransferSQLText(Path + sFileName, this); 470 } 471 } 472 } catch (Exception e) { 473 Exit(); 474 e.printStackTrace(); 475 476 return; 477 } 478 479 if ((iTransferMode == TRFM_DUMP) 480 || (iTransferMode == TRFM_TRANSFER)) { 481 iSelectionStep = SELECT_SOURCE_CATALOG; 482 sSourceCatalog = null; 483 } else { 484 iSelectionStep = SELECT_DEST_CATALOG; 485 sDestCatalog = null; 486 } 487 488 ProcessNextStep(); 489 fMain.show(); 490 491 return; 492 } 493 494 private void RefreshMainDisplay() { 495 496 lTable.removeAll(); 497 lTable.repaint(); 498 499 try { 500 tTable = sourceDb.getTables(sSourceCatalog, sSourceSchemas); 501 502 for (int i = 0; i < tTable.size(); i++) { 503 TransferTable t = (TransferTable) tTable.elementAt(i); 504 505 t.setDest(sDestSchema, targetDb); 506 t.extractTableStructure(sourceDb, targetDb); 507 lTable.add(t.Stmts.sSourceTable); 508 lTable.select(i); 509 displayTable(t); 510 } 511 512 bStart.setEnabled(true); 513 514 if (iTransferMode == TRFM_TRANSFER) { 515 trace("Edit definitions and press [Start Transfer]"); 516 } else if (iTransferMode == TRFM_DUMP) { 517 trace("Edit definitions and press [Start Dump]"); 518 } 519 } catch (Exception e) { 520 trace("Exception reading source tables: " + e); 521 e.printStackTrace(); 522 } 523 524 fMain.show(); 525 } 526 527 534 private void addMenuItems(Menu f, String [] m) { 535 536 for (int i = 0; i < m.length; i++) { 537 if (m[i].equals("-")) { 538 f.addSeparator(); 539 } else { 540 MenuItem item = new MenuItem (m[i]); 541 542 item.addActionListener(this); 543 f.add(item); 544 } 545 } 546 } 547 548 554 public void itemStateChanged(ItemEvent e) { 555 556 ItemSelectable item = e.getItemSelectable(); 557 558 if (item == lTable) { 559 if (iSelectionStep == SELECT_SOURCE_TABLES) { 560 String table = lTable.getSelectedItem(); 561 int selected = ((Integer ) e.getItem()).intValue(); 562 563 for (int i = 0; i < tTable.size(); i++) { 564 TransferTable t = (TransferTable) tTable.elementAt(i); 565 566 if (t == null) { 567 continue; 568 } 569 570 if (i == selected) { 571 saveTable(); 572 displayTable(t); 573 updateEnabled(true); 574 } 575 } 576 } 577 } else { 578 579 saveTable(); 581 updateEnabled(true); 582 } 583 } 584 585 589 private void saveTable() { 590 591 if (tCurrent == null) { 592 return; 593 } 594 595 TransferTable t = tCurrent; 596 597 t.Stmts.sSourceTable = tSourceTable.getText(); 598 t.Stmts.sDestTable = tDestTable.getText(); 599 t.Stmts.sDestDrop = tDestDrop.getText(); 600 t.Stmts.sDestCreateIndex = tDestCreateIndex.getText(); 601 t.Stmts.sDestDropIndex = tDestDropIndex.getText(); 602 t.Stmts.sDestCreate = tDestCreate.getText(); 603 t.Stmts.sDestDelete = tDestDelete.getText(); 604 t.Stmts.sSourceSelect = tSourceSelect.getText(); 605 t.Stmts.sDestInsert = tDestInsert.getText(); 606 t.Stmts.sDestAlter = tDestAlter.getText(); 607 608 t.Stmts.bTransfer = cTransfer.getState(); 610 t.Stmts.bDrop = cDrop.getState(); 611 t.Stmts.bCreate = cCreate.getState(); 612 t.Stmts.bDelete = cDelete.getState(); 613 t.Stmts.bInsert = cInsert.getState(); 614 t.Stmts.bAlter = cAlter.getState(); 615 t.Stmts.bCreateIndex = cCreateIndex.getState(); 616 t.Stmts.bDropIndex = cDropIndex.getState(); 617 618 if (!t.Stmts.bTransfer) { 619 t.Stmts.bInsert = false; 620 621 cInsert.setState(false); 622 } 623 624 boolean reparsetable = ((t.Stmts.bFKForced != cFKForced.getState()) 625 || (t.Stmts.bIdxForced 626 != cIdxForced.getState())); 627 628 t.Stmts.bFKForced = cFKForced.getState(); 629 t.Stmts.bIdxForced = cIdxForced.getState(); 630 631 if (reparsetable) { 632 try { 633 sourceDb.getTableStructure(t, targetDb); 634 } catch (Exception e) { 635 trace("Exception reading source tables: " + e); 636 e.printStackTrace(); 637 } 638 } 639 } 640 641 647 private void displayTable(TransferTable t) { 648 649 tCurrent = t; 650 651 if (t == null) { 652 return; 653 } 654 655 tSourceTable.setText(t.Stmts.sSourceTable); 656 tDestTable.setText(t.Stmts.sDestTable); 657 tDestDrop.setText(t.Stmts.sDestDrop); 658 tDestCreateIndex.setText(t.Stmts.sDestCreateIndex); 659 tDestDropIndex.setText(t.Stmts.sDestDropIndex); 660 tDestCreate.setText(t.Stmts.sDestCreate); 661 tDestDelete.setText(t.Stmts.sDestDelete); 662 tSourceSelect.setText(t.Stmts.sSourceSelect); 663 tDestInsert.setText(t.Stmts.sDestInsert); 664 tDestAlter.setText(t.Stmts.sDestAlter); 665 cTransfer.setState(t.Stmts.bTransfer); 666 cDrop.setState(t.Stmts.bDrop); 667 cCreate.setState(t.Stmts.bCreate); 668 cDropIndex.setState(t.Stmts.bDropIndex); 669 cCreateIndex.setState(t.Stmts.bCreateIndex); 670 cDelete.setState(t.Stmts.bDelete); 671 cInsert.setState(t.Stmts.bInsert); 672 cAlter.setState(t.Stmts.bAlter); 673 cFKForced.setState(t.Stmts.bFKForced); 674 cIdxForced.setState(t.Stmts.bIdxForced); 675 } 676 677 683 private void updateEnabled(boolean and) { 684 685 boolean b = cTransfer.getState(); 686 687 tDestTable.setEnabled(and && b); 688 tDestDrop.setEnabled(and && b && cDrop.getState()); 689 tDestCreate.setEnabled(and && b && cCreate.getState()); 690 tDestDelete.setEnabled(and && b && cDelete.getState()); 691 tDestCreateIndex.setEnabled(and && b && cCreateIndex.getState()); 692 tDestDropIndex.setEnabled(and && b && cDropIndex.getState()); 693 tSourceSelect.setEnabled(and && b); 694 tDestInsert.setEnabled(and && b && cInsert.getState()); 695 tDestAlter.setEnabled(and && b && cAlter.getState()); 696 cDrop.setEnabled(and && b); 697 cCreate.setEnabled(and && b); 698 cDelete.setEnabled(and && b); 699 cCreateIndex.setEnabled(and && b); 700 cDropIndex.setEnabled(and && b); 701 cInsert.setEnabled(and && b); 702 cAlter.setEnabled(and && b); 703 cFKForced.setEnabled(cAlter.getState()); 704 cIdxForced.setEnabled(cCreateIndex.getState()); 705 bStart.setEnabled(and); 706 707 if (iTransferMode == TRFM_TRANSFER) { 708 bContinue.setEnabled(and); 709 } 710 } 711 712 715 private void ProcessNextStep() { 716 717 switch (iSelectionStep) { 718 719 case SELECT_SOURCE_CATALOG : 720 case SELECT_DEST_CATALOG : 721 if (CatalogToSelect()) { 722 fMain.show(); 723 724 return; 725 } 726 break; 727 728 case SELECT_DEST_SCHEMA : 729 case SELECT_SOURCE_SCHEMA : 730 if (SchemaToSelect()) { 731 fMain.show(); 732 733 return; 734 } 735 break; 736 737 case SELECT_SOURCE_TABLES : 738 if (iTransferMode == TRFM_TRANSFER) { 739 bStart.setLabel("Start Transfer"); 740 } else if (iTransferMode == TRFM_DUMP) { 741 bStart.setLabel("Start Dump"); 742 } else if (iTransferMode == TRFM_RESTORE) { 743 bStart.setLabel("Start Restore"); 744 } 745 746 bStart.invalidate(); 747 bStart.setEnabled(false); 748 lTable.setMultipleMode(false); 749 RefreshMainDisplay(); 750 break; 751 752 default : 753 break; 754 } 755 } 756 757 763 public void actionPerformed(ActionEvent ev) { 764 765 if (ev.getSource() instanceof TextField ) { 766 saveTable(); 767 768 return; 769 } 770 771 String s = ev.getActionCommand(); 772 MenuItem i = new MenuItem (); 773 774 if (s == null) { 775 if (ev.getSource() instanceof MenuItem ) { 776 i = (MenuItem ) ev.getSource(); 777 s = i.getLabel(); 778 } 779 } 780 781 if (s == null) {} 782 783 if (s.equals("Start Transfer") || s.equals("ReStart Transfer")) { 784 bStart.setLabel("ReStart Transfer"); 785 bStart.invalidate(); 786 787 CurrentTransfer = 0; 788 CurrentAlter = 0; 789 790 transfer(); 791 } else if (s.equals("Continue Transfer")) { 792 transfer(); 793 } else if (s.equals("Start Dump") || s.equals("Start Restore")) { 794 CurrentTransfer = 0; 795 CurrentAlter = 0; 796 797 transfer(); 798 } else if (s.equals("Quit")) { 799 Exit(); 800 } else if (s.indexOf("Select Schema") >= 0) { 801 String [] selection = lTable.getSelectedItems(); 802 803 if ((selection == null) || (selection.length == 0)) { 804 return; 805 } 806 807 if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) { 808 sSourceSchemas = selection; 809 } else { 810 sDestSchema = selection[0]; 811 } 812 813 if (iTransferMode == TRFM_DUMP) { 814 iSelectionStep = Transfer.SELECT_SOURCE_TABLES; 815 } else { 816 iSelectionStep++; 817 } 818 819 ProcessNextStep(); 820 } else if (s.indexOf("Select Catalog") >= 0) { 821 String selection = lTable.getSelectedItem(); 822 823 if ((selection == null) || (selection.equals(""))) { 824 return; 825 } 826 827 if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) { 828 sSourceCatalog = selection; 829 sSourceSchemas = null; 830 } else { 831 sDestCatalog = selection; 832 sDestSchema = null; 833 834 try { 835 targetDb.setCatalog(sDestCatalog); 836 } catch (Exception ex) { 837 trace("Catalog " + sDestCatalog 838 + " could not be selected in the target database"); 839 840 sDestCatalog = null; 841 } 842 } 843 844 iSelectionStep++; 845 846 ProcessNextStep(); 847 } else if (s.equals("Insert 10 rows only")) { 848 iMaxRows = 10; 849 } else if (s.equals("Insert 1000 rows only")) { 850 iMaxRows = 1000; 851 } else if (s.equals("Insert all rows")) { 852 iMaxRows = 0; 853 } else if (s.equals("Load Settings...")) { 854 FileDialog f = new FileDialog (fMain, "Load Settings", 855 FileDialog.LOAD); 856 857 f.show(); 858 859 String file = f.getDirectory() + f.getFile(); 860 861 if (file != null) { 862 LoadPrefs(file); 863 displayTable(tCurrent); 864 } 865 } else if (s.equals("Save Settings...")) { 866 FileDialog f = new FileDialog (fMain, "Save Settings", 867 FileDialog.SAVE); 868 869 f.show(); 870 871 String file = f.getDirectory() + f.getFile(); 872 873 if (file != null) { 874 SavePrefs(file); 875 } 876 } else if (s.equals("Exit")) { 877 windowClosing(null); 878 } 879 } 880 881 887 public void windowActivated(WindowEvent e) {} 888 889 895 public void windowDeactivated(WindowEvent e) {} 896 897 903 public void windowClosed(WindowEvent e) {} 904 905 private void cleanup() { 906 907 try { 908 if (sourceDb != null) { 909 sourceDb.close(); 910 } 911 912 if (targetDb != null) { 913 targetDb.close(); 914 } 915 } catch (Exception e) {} 916 } 917 918 924 public void windowClosing(WindowEvent ev) { 925 926 fMain.dispose(); 927 928 if (bMustExit) { 929 System.exit(0); 930 } 931 } 932 933 939 public void windowDeiconified(WindowEvent e) {} 940 941 947 public void windowIconified(WindowEvent e) {} 948 949 955 public void windowOpened(WindowEvent e) {} 956 957 961 private void initGUI() { 962 963 Font fFont = new Font ("Dialog", Font.PLAIN, 12); 964 965 setLayout(new BorderLayout ()); 966 967 Panel p = new Panel (); 968 969 p.setBackground(SystemColor.control); 970 p.setLayout(new GridLayout (16, 1)); 971 972 tSourceTable = new TextField (); 973 974 tSourceTable.setEnabled(false); 975 976 tDestTable = new TextField (); 977 978 tDestTable.addActionListener(this); 979 980 tDestDrop = new TextField (); 981 982 tDestDrop.addActionListener(this); 983 984 tDestCreate = new TextField (); 985 986 tDestCreate.addActionListener(this); 987 988 tDestDelete = new TextField (); 989 990 tDestDelete.addActionListener(this); 991 992 tDestCreateIndex = new TextField (); 993 994 tDestCreateIndex.addActionListener(this); 995 996 tDestDropIndex = new TextField (); 997 998 tDestDropIndex.addActionListener(this); 999 1000 tSourceSelect = new TextField (); 1001 1002 tSourceSelect.addActionListener(this); 1003 1004 tDestInsert = new TextField (); 1005 1006 tDestInsert.addActionListener(this); 1007 1008 tDestAlter = new TextField (); 1009 1010 tDestAlter.addActionListener(this); 1011 1012 cTransfer = new Checkbox ("Transfer to destination table", true); 1013 1014 cTransfer.addItemListener(this); 1015 1016 cDrop = new Checkbox ("Drop destination table (ignore error)", true); 1017 1018 cDrop.addItemListener(this); 1019 1020 cCreate = new Checkbox ("Create destination table", true); 1021 1022 cCreate.addItemListener(this); 1023 1024 cDropIndex = new Checkbox ("Drop destination index (ignore error)", 1025 true); 1026 1027 cDropIndex.addItemListener(this); 1028 1029 cIdxForced = new Checkbox ("force Idx_ prefix for indexes names", 1030 false); 1031 1032 cIdxForced.addItemListener(this); 1033 1034 cCreateIndex = new Checkbox ("Create destination index", true); 1035 1036 cCreateIndex.addItemListener(this); 1037 1038 cDelete = new Checkbox ("Delete rows in destination table", true); 1039 1040 cDelete.addItemListener(this); 1041 1042 cInsert = new Checkbox ("Insert into destination", true); 1043 1044 cInsert.addItemListener(this); 1045 1046 cFKForced = new Checkbox ("force FK_ prefix for foreign key names", 1047 false); 1048 1049 cFKForced.addItemListener(this); 1050 1051 cAlter = new Checkbox ("Alter destination table", true); 1052 1053 cAlter.addItemListener(this); 1054 p.add(createLabel("Source table")); 1055 p.add(tSourceTable); 1056 p.add(cTransfer); 1057 p.add(tDestTable); 1058 p.add(cDrop); 1059 p.add(tDestDrop); 1060 p.add(cCreate); 1061 p.add(tDestCreate); 1062 p.add(cDropIndex); 1063 p.add(tDestDropIndex); 1064 p.add(cCreateIndex); 1065 p.add(tDestCreateIndex); 1066 p.add(cDelete); 1067 p.add(tDestDelete); 1068 p.add(cAlter); 1069 p.add(tDestAlter); 1070 p.add(createLabel("Select source records")); 1071 p.add(tSourceSelect); 1072 p.add(cInsert); 1073 p.add(tDestInsert); 1074 p.add(createLabel("")); 1075 p.add(createLabel("")); 1076 p.add(cIdxForced); 1077 p.add(cFKForced); 1078 p.add(createLabel("")); 1079 p.add(createLabel("")); 1080 1081 if (iTransferMode == TRFM_TRANSFER) { 1082 bStart = new Button ("Start Transfer"); 1083 bContinue = new Button ("Continue Transfer"); 1084 1085 bContinue.setEnabled(false); 1086 } else if (iTransferMode == Transfer.TRFM_DUMP) { 1087 bStart = new Button ("Start Dump"); 1088 } else if (iTransferMode == Transfer.TRFM_RESTORE) { 1089 bStart = new Button ("Start Restore"); 1090 } 1091 1092 bStart.addActionListener(this); 1093 p.add(bStart); 1094 1095 if (iTransferMode == TRFM_TRANSFER) { 1096 bContinue.addActionListener(this); 1097 p.add(bContinue); 1098 } 1099 1100 bStart.setEnabled(false); 1101 fMain.add("Center", createBorderPanel(p)); 1102 1103 lTable = new java.awt.List (10); 1104 1105 lTable.addItemListener(this); 1106 fMain.add("West", createBorderPanel(lTable)); 1107 1108 tMessage = new TextField (); 1109 1110 Panel pMessage = createBorderPanel(tMessage); 1111 1112 fMain.add("South", pMessage); 1113 } 1114 1115 1123 private Panel createBorderPanel(Component center) { 1124 1125 Panel p = new Panel (); 1126 1127 p.setBackground(SystemColor.control); 1128 p.setLayout(new BorderLayout ()); 1129 p.add("Center", center); 1130 p.add("South", createLabel("")); 1131 p.add("East", createLabel("")); 1132 p.add("West", createLabel("")); 1133 p.setBackground(SystemColor.control); 1134 1135 return p; 1136 } 1137 1138 1146 private Label createLabel(String s) { 1147 1148 Label l = new Label (s); 1149 1150 l.setBackground(SystemColor.control); 1151 1152 return l; 1153 } 1154 1155 private void SavePrefs(String f) { 1156 saveTable(); 1157 TransferCommon.savePrefs(f, sourceDb, targetDb, this, tTable); 1158 } 1159 1160 private void LoadPrefs(String f) { 1161 1162 TransferTable t; 1163 1164 trace("Parsing Settings file"); 1165 bStart.setEnabled(false); 1166 1167 if (iTransferMode == TRFM_TRANSFER) { 1168 bContinue.setEnabled(false); 1169 } 1170 1171 tTable = TransferCommon.loadPrefs(f, sourceDb, targetDb, this); 1172 iSelectionStep = SELECT_SOURCE_TABLES; 1173 1174 lTable.removeAll(); 1175 1176 for (int i = 0; i < tTable.size(); i++) { 1177 t = (TransferTable) tTable.elementAt(i); 1178 1179 lTable.add(t.Stmts.sSourceTable); 1180 } 1181 1182 t = (TransferTable) tTable.elementAt(0); 1183 1184 displayTable(t); 1185 lTable.select(0); 1186 updateEnabled(true); 1187 lTable.invalidate(); 1188 1189 if (iTransferMode == TRFM_TRANSFER) { 1190 bStart.setLabel("Start Transfer"); 1191 trace("Edit definitions and press [Start Transfer]"); 1192 } else if (iTransferMode == TRFM_DUMP) { 1193 bStart.setLabel("Start Dump"); 1194 trace("Edit definitions and press [Start Dump]"); 1195 } else if (iTransferMode == TRFM_RESTORE) { 1196 bStart.setLabel("Start Restore"); 1197 trace("Edit definitions and press [Start Restore]"); 1198 } 1199 1200 bStart.invalidate(); 1201 1202 if (iTransferMode == TRFM_TRANSFER) { 1203 bContinue.setEnabled(false); 1204 } 1205 } 1206 1207 1211 private void transfer() { 1212 1213 saveTable(); 1214 updateEnabled(false); 1215 trace("Start Transfer"); 1216 1217 int TransferIndex = CurrentTransfer; 1218 int AlterIndex = CurrentAlter; 1219 TransferTable t = null; 1220 long startTime, stopTime; 1221 1222 startTime = System.currentTimeMillis(); 1223 1224 try { 1225 for (int i = TransferIndex; i < tTable.size(); i++) { 1226 CurrentTransfer = i; 1227 t = (TransferTable) tTable.elementAt(i); 1228 1229 lTable.select(i); 1230 displayTable(t); 1231 t.transferStructure(); 1232 t.transferData(iMaxRows); 1233 } 1234 1235 for (int i = AlterIndex; i < tTable.size(); i++) { 1236 CurrentAlter = i; 1237 t = (TransferTable) tTable.elementAt(i); 1238 1239 lTable.select(i); 1240 displayTable(t); 1241 t.transferAlter(); 1242 } 1243 1244 stopTime = System.currentTimeMillis(); 1245 1246 trace("Transfer finished successfully in: " 1247 + (stopTime - startTime) / 1000.00 + " sec"); 1248 1249 if (iTransferMode == TRFM_TRANSFER) { 1250 bContinue.setLabel("Quit"); 1251 bContinue.setEnabled(true); 1252 bContinue.invalidate(); 1253 } else { 1254 bStart.setLabel("Quit"); 1255 bStart.setEnabled(true); 1256 bStart.invalidate(); 1257 } 1258 } catch (Exception e) { 1259 String last = tMessage.getText(); 1260 1261 trace("Transfer stopped - " + last + " / / Error: " 1262 + e.getMessage()); 1263 e.printStackTrace(); 1264 } 1265 1266 if (iTransferMode == TRFM_TRANSFER) { 1267 bContinue.setEnabled((CurrentAlter < tTable.size())); 1268 } 1269 1270 updateEnabled(true); 1271 System.gc(); 1272 } 1273 1274 protected void Exit() { 1275 1276 cleanup(); 1277 fMain.dispose(); 1278 1279 if (bMustExit) { 1280 System.exit(0); 1281 } 1282 } 1283} 1284 | Popular Tags |