0001: /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
0002: * All rights reserved.
0003: *
0004: * Redistribution and use in source and binary forms, with or without
0005: * modification, are permitted provided that the following conditions are met:
0006: *
0007: * Redistributions of source code must retain the above copyright notice, this
0008: * list of conditions and the following disclaimer.
0009: *
0010: * Redistributions in binary form must reproduce the above copyright notice,
0011: * this list of conditions and the following disclaimer in the documentation
0012: * and/or other materials provided with the distribution.
0013: *
0014: * Neither the name of the Hypersonic SQL Group nor the names of its
0015: * contributors may be used to endorse or promote products derived from this
0016: * software without specific prior written permission.
0017: *
0018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0021: * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
0022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029: *
0030: * This software consists of voluntary contributions made by many individuals
0031: * on behalf of the Hypersonic SQL Group.
0032: *
0033: *
0034: * For work added by the HSQL Development Group:
0035: *
0036: * Copyright (c) 2001-2005, The HSQL Development Group
0037: * All rights reserved.
0038: *
0039: * Redistribution and use in source and binary forms, with or without
0040: * modification, are permitted provided that the following conditions are met:
0041: *
0042: * Redistributions of source code must retain the above copyright notice, this
0043: * list of conditions and the following disclaimer.
0044: *
0045: * Redistributions in binary form must reproduce the above copyright notice,
0046: * this list of conditions and the following disclaimer in the documentation
0047: * and/or other materials provided with the distribution.
0048: *
0049: * Neither the name of the HSQL Development Group nor the names of its
0050: * contributors may be used to endorse or promote products derived from this
0051: * software without specific prior written permission.
0052: *
0053: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0054: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0055: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0056: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
0057: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0058: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0059: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0060: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0061: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0062: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0063: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0064: */
0065:
0066: package org.hsqldb.util;
0067:
0068: import java.applet.Applet;
0069: import java.util.Enumeration;
0070: import java.util.Vector;
0071:
0072: import java.awt.BorderLayout;
0073: import java.awt.Button;
0074: import java.awt.Checkbox;
0075: import java.awt.Component;
0076: import java.awt.Dimension;
0077: import java.awt.FileDialog;
0078: import java.awt.Font;
0079: import java.awt.Frame;
0080: import java.awt.GridLayout;
0081: import java.awt.Image;
0082: import java.awt.ItemSelectable;
0083: import java.awt.Label;
0084: import java.awt.Menu;
0085: import java.awt.MenuBar;
0086: import java.awt.MenuItem;
0087: import java.awt.Panel;
0088: import java.awt.SystemColor;
0089: import java.awt.TextField;
0090: import java.awt.Toolkit;
0091: import java.awt.event.ActionEvent;
0092: import java.awt.event.ActionListener;
0093: import java.awt.event.ItemEvent;
0094: import java.awt.event.ItemListener;
0095: import java.awt.event.WindowEvent;
0096: import java.awt.event.WindowListener;
0097: import java.awt.image.MemoryImageSource;
0098:
0099: // fredt@users 20011220 - patch 481239 by xponsard@users - enhancements
0100: // enhancements to support saving and loading of transfer settings,
0101: // transfer of blobs, and catalog and schema names in source db
0102: // changes by fredt to allow saving and loading of transfer settings
0103: // fredt@users 20020215 - patch 516309 by Nicolas Bazin - enhancements
0104: // sqlbob@users 20020401 - patch 1.7.0 - reengineering
0105: // nicolas BAZIN 20020430 - add Catalog selection, correct a bug preventing table
0106: // edition, change double quotes to simple quotes for default values of CHAR type
0107: // lonbinder@users 20030426 - correct bug in prefs load/save
0108: // fredt@users 20040508 - patch 1.7.2 - bug fixes
0109:
0110: /**
0111: * Utility program (or applet) for transferring tables between different
0112: * databases via JDBC. Understands HSQLDB database particularly well.
0113: *
0114: * @author Thomas Mueller (Hypersonic SQL Group)
0115: * @version 1.7.2
0116: * @since Hypersonic SQL
0117: */
0118: public class Transfer extends Applet implements WindowListener,
0119: ActionListener, ItemListener, Traceable {
0120:
0121: Frame fMain;
0122: Image imgEmpty;
0123: DataAccessPoint sourceDb;
0124: DataAccessPoint targetDb;
0125: TransferTable tCurrent;
0126: int iMaxRows;
0127: int iSelectionStep;
0128: Vector tTable;
0129: java.awt.List lTable;
0130: String[] sSourceSchemas;
0131: String sSourceCatalog, sDestSchema, sDestCatalog;
0132: TextField tSourceTable, tDestTable, tDestDropIndex,
0133: tDestCreateIndex;
0134: TextField tDestDrop, tDestCreate, tDestDelete, tDestAlter;
0135: TextField tSourceSelect, tDestInsert;
0136: Checkbox cTransfer, cDrop, cCreate, cDelete, cInsert, cAlter;
0137: Checkbox cCreateIndex, cDropIndex;
0138: Checkbox cFKForced, cIdxForced;
0139: Button bStart, bContinue;
0140: TextField tMessage;
0141: int iTransferMode;
0142: static boolean bMustExit;
0143: int CurrentTransfer, CurrentAlter;
0144: static final int SELECT_SOURCE_CATALOG = 1;
0145: static final int SELECT_SOURCE_SCHEMA = 2;
0146: static final int SELECT_DEST_CATALOG = 3;
0147: static final int SELECT_DEST_SCHEMA = 4;
0148: static final int SELECT_SOURCE_TABLES = 5;
0149: static final int TRFM_TRANSFER = 1;
0150: static final int TRFM_DUMP = 2;
0151: static final int TRFM_RESTORE = 3;
0152:
0153: /**
0154: * Method declaration
0155: *
0156: *
0157: * @param s
0158: */
0159: public void trace(String s) {
0160:
0161: if ((s != null) && !s.equals("")) {
0162: tMessage.setText(s);
0163:
0164: if (TRACE) {
0165: System.out.println(s);
0166: }
0167: }
0168: }
0169:
0170: /**
0171: * Method declaration
0172: *
0173: */
0174: public void init() {
0175:
0176: Transfer m = new Transfer();
0177:
0178: m._main(null);
0179: }
0180:
0181: /**
0182: * Method declaration
0183: *
0184: */
0185: public static void work(String[] arg) {
0186:
0187: Transfer m = new Transfer();
0188:
0189: m._main(arg);
0190: }
0191:
0192: /**
0193: * Method declaration
0194: *
0195: *
0196: * @param arg
0197: */
0198: public static void main(String[] arg) {
0199:
0200: System.getProperties().put("sun.java2d.noddraw", "true");
0201:
0202: bMustExit = true;
0203:
0204: work(arg);
0205: }
0206:
0207: private boolean CatalogToSelect() {
0208:
0209: Vector result = null;
0210:
0211: try {
0212: lTable.removeAll();
0213:
0214: if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) {
0215: result = sourceDb.getCatalog();
0216: } else if (iSelectionStep == Transfer.SELECT_DEST_CATALOG) {
0217: result = targetDb.getCatalog();
0218: } else {
0219: Exit();
0220: }
0221:
0222: if (result.size() > 1) {
0223: lTable.setMultipleMode(true);
0224:
0225: if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) {
0226: bStart.setLabel("Select Catalog: Source");
0227: } else {
0228: bStart.setLabel("Select Catalog: Destination");
0229: }
0230:
0231: bStart.invalidate();
0232: bStart.setEnabled(true);
0233:
0234: for (Enumeration e = result.elements(); e
0235: .hasMoreElements();) {
0236: lTable.add(e.nextElement().toString());
0237: }
0238:
0239: lTable.repaint();
0240: trace("Select correct Catalog");
0241: } else {
0242: if (result.size() == 1) {
0243: if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) {
0244: sSourceCatalog = (String) result.firstElement();
0245: sSourceSchemas = null;
0246: } else {
0247: sDestCatalog = (String) result.firstElement();
0248: sDestSchema = null;
0249: }
0250: } else {
0251: if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) {
0252: sSourceCatalog = null;
0253: sSourceSchemas = null;
0254: } else {
0255: sDestCatalog = null;
0256: sDestSchema = null;
0257: }
0258: }
0259:
0260: if ((iSelectionStep == Transfer.SELECT_DEST_CATALOG)
0261: && (sDestCatalog != null)) {
0262: try {
0263: targetDb.setCatalog(sDestCatalog);
0264: } catch (Exception ex) {
0265: trace("Catalog "
0266: + sSourceCatalog
0267: + " could not be selected in the target database");
0268:
0269: sSourceCatalog = null;
0270: }
0271: }
0272:
0273: iSelectionStep++;
0274:
0275: ProcessNextStep();
0276:
0277: return false;
0278: }
0279: } catch (Exception exp) {
0280: lTable.removeAll();
0281: trace("Exception reading catalog: " + exp);
0282: exp.printStackTrace();
0283: }
0284:
0285: return (lTable.getItemCount() > 0);
0286: }
0287:
0288: private boolean SchemaToSelect() {
0289:
0290: Vector result = null;
0291:
0292: try {
0293: lTable.removeAll();
0294:
0295: if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) {
0296: result = sourceDb.getSchemas();
0297: } else if (iSelectionStep == Transfer.SELECT_DEST_SCHEMA) {
0298: result = targetDb.getSchemas();
0299: } else {
0300: Exit();
0301: }
0302:
0303: if (result.size() > 1) {
0304: lTable.setMultipleMode(true);
0305:
0306: if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) {
0307: bStart.setLabel("Select Schema: Source");
0308: } else {
0309: bStart.setLabel("Select Schema: Destination");
0310: }
0311:
0312: bStart.invalidate();
0313: bStart.setEnabled(true);
0314:
0315: for (Enumeration e = result.elements(); e
0316: .hasMoreElements();) {
0317: lTable.add(e.nextElement().toString());
0318: }
0319:
0320: lTable.repaint();
0321: trace("Select correct Schema or load Settings file");
0322: } else {
0323: if (result.size() == 1) {
0324: if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) {
0325: sSourceSchemas = new String[1];
0326: sSourceSchemas[0] = (String) result
0327: .firstElement();
0328: } else {
0329: sDestSchema = (String) result.firstElement();
0330: }
0331: } else {
0332: if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) {
0333: sSourceSchemas = null;
0334: } else {
0335: sDestSchema = null;
0336: }
0337: }
0338:
0339: if (iTransferMode == TRFM_DUMP) {
0340: iSelectionStep = Transfer.SELECT_SOURCE_TABLES;
0341: } else {
0342: iSelectionStep++;
0343: }
0344:
0345: ProcessNextStep();
0346:
0347: return false;
0348: }
0349: } catch (Exception exp) {
0350: lTable.removeAll();
0351: trace("Exception reading schemas: " + exp);
0352: exp.printStackTrace();
0353: }
0354:
0355: return (lTable.getItemCount() > 0);
0356: }
0357:
0358: /**
0359: * Method declaration
0360: *
0361: */
0362: void _main(String[] arg) {
0363:
0364: /*
0365: ** What function is asked from the transfer tool?
0366: */
0367: iTransferMode = TRFM_TRANSFER;
0368:
0369: if ((arg != null) && (arg.length > 0)) {
0370: if ((arg[0].toLowerCase().equals("-r"))
0371: || (arg[0].toLowerCase().equals("--restore"))) {
0372: iTransferMode = TRFM_RESTORE;
0373: } else if ((arg[0].toLowerCase().equals("-d"))
0374: || (arg[0].toLowerCase().equals("--dump"))) {
0375: iTransferMode = TRFM_DUMP;
0376: }
0377: }
0378:
0379: fMain = new Frame("HSQL Transfer Tool");
0380: imgEmpty = createImage(new MemoryImageSource(2, 2,
0381: new int[4 * 4], 2, 2));
0382:
0383: fMain.setIconImage(imgEmpty);
0384: fMain.addWindowListener(this );
0385: fMain.setSize(640, 480);
0386: fMain.add("Center", this );
0387:
0388: MenuBar bar = new MenuBar();
0389: String[] extras = { "Insert 10 rows only",
0390: "Insert 1000 rows only", "Insert all rows", "-",
0391: "Load Settings...", "Save Settings...", "-", "Exit" };
0392: Menu menu = new Menu("Options");
0393:
0394: addMenuItems(menu, extras);
0395: bar.add(menu);
0396: fMain.setMenuBar(bar);
0397: initGUI();
0398:
0399: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
0400: Dimension size = fMain.getSize();
0401:
0402: // (ulrivo): full size on screen with less than 640 width
0403: if (d.width >= 640) {
0404: fMain.setLocation((d.width - size.width) / 2,
0405: (d.height - size.height) / 2);
0406: } else {
0407: fMain.setLocation(0, 0);
0408: fMain.setSize(d);
0409: }
0410:
0411: fMain.setVisible(true);
0412:
0413: CurrentTransfer = CurrentAlter = 0;
0414:
0415: try {
0416: if ((iTransferMode == TRFM_DUMP)
0417: || (iTransferMode == TRFM_TRANSFER)) {
0418: sourceDb = new TransferDb(ConnectionDialog
0419: .createConnection(fMain, "Source Database"),
0420: this );
0421:
0422: if (!sourceDb.isConnected()) {
0423: Exit();
0424:
0425: return;
0426: }
0427: } else {
0428: FileDialog f = new FileDialog(fMain,
0429: "Restore FileName", FileDialog.LOAD);
0430:
0431: f.show();
0432:
0433: String sFileName = f.getFile();
0434: String Path = f.getDirectory();
0435:
0436: if ((sFileName == null) || (sFileName.equals(""))) {
0437: Exit();
0438:
0439: return;
0440: } else {
0441: sourceDb = new TransferSQLText(Path + sFileName,
0442: this );
0443: }
0444: }
0445:
0446: if ((iTransferMode == TRFM_RESTORE)
0447: || (iTransferMode == TRFM_TRANSFER)) {
0448: targetDb = new TransferDb(ConnectionDialog
0449: .createConnection(fMain, "Target Database"),
0450: this );
0451:
0452: if (!targetDb.isConnected()) {
0453: Exit();
0454:
0455: return;
0456: }
0457: } else {
0458: FileDialog f = new FileDialog(fMain, "Dump FileName",
0459: FileDialog.SAVE);
0460:
0461: f.show();
0462:
0463: String sFileName = f.getFile();
0464: String Path = f.getDirectory();
0465:
0466: if ((sFileName == null) || (sFileName.equals(""))) {
0467: Exit();
0468:
0469: return;
0470: } else {
0471: targetDb = new TransferSQLText(Path + sFileName,
0472: this );
0473: }
0474: }
0475: } catch (Exception e) {
0476: Exit();
0477: e.printStackTrace();
0478:
0479: return;
0480: }
0481:
0482: if ((iTransferMode == TRFM_DUMP)
0483: || (iTransferMode == TRFM_TRANSFER)) {
0484: iSelectionStep = SELECT_SOURCE_CATALOG;
0485: sSourceCatalog = null;
0486: } else {
0487: iSelectionStep = SELECT_DEST_CATALOG;
0488: sDestCatalog = null;
0489: }
0490:
0491: ProcessNextStep();
0492: fMain.show();
0493:
0494: return;
0495: }
0496:
0497: private void RefreshMainDisplay() {
0498:
0499: lTable.removeAll();
0500: lTable.repaint();
0501:
0502: try {
0503: tTable = sourceDb.getTables(sSourceCatalog, sSourceSchemas);
0504:
0505: for (int i = 0; i < tTable.size(); i++) {
0506: TransferTable t = (TransferTable) tTable.elementAt(i);
0507:
0508: t.setDest(sDestSchema, targetDb);
0509: t.extractTableStructure(sourceDb, targetDb);
0510: lTable.add(t.Stmts.sSourceTable);
0511: lTable.select(i);
0512: displayTable(t);
0513: }
0514:
0515: bStart.setEnabled(true);
0516:
0517: if (iTransferMode == TRFM_TRANSFER) {
0518: trace("Edit definitions and press [Start Transfer]");
0519: } else if (iTransferMode == TRFM_DUMP) {
0520: trace("Edit definitions and press [Start Dump]");
0521: }
0522: } catch (Exception e) {
0523: trace("Exception reading source tables: " + e);
0524: e.printStackTrace();
0525: }
0526:
0527: fMain.show();
0528: }
0529:
0530: /**
0531: * Method declaration
0532: *
0533: *
0534: * @param f
0535: * @param m
0536: */
0537: private void addMenuItems(Menu f, String[] m) {
0538:
0539: for (int i = 0; i < m.length; i++) {
0540: if (m[i].equals("-")) {
0541: f.addSeparator();
0542: } else {
0543: MenuItem item = new MenuItem(m[i]);
0544:
0545: item.addActionListener(this );
0546: f.add(item);
0547: }
0548: }
0549: }
0550:
0551: /**
0552: * Method declaration
0553: *
0554: *
0555: * @param e
0556: */
0557: public void itemStateChanged(ItemEvent e) {
0558:
0559: ItemSelectable item = e.getItemSelectable();
0560:
0561: if (item == lTable) {
0562: if (iSelectionStep == SELECT_SOURCE_TABLES) {
0563: String table = lTable.getSelectedItem();
0564: int selected = ((Integer) e.getItem()).intValue();
0565:
0566: for (int i = 0; i < tTable.size(); i++) {
0567: TransferTable t = (TransferTable) tTable
0568: .elementAt(i);
0569:
0570: if (t == null) {
0571: continue;
0572: }
0573:
0574: if (i == selected) {
0575: saveTable();
0576: displayTable(t);
0577: updateEnabled(true);
0578: }
0579: }
0580: }
0581: } else {
0582:
0583: // it must be a checkbox
0584: saveTable();
0585: updateEnabled(true);
0586: }
0587: }
0588:
0589: /**
0590: * Method declaration
0591: *
0592: */
0593: private void saveTable() {
0594:
0595: if (tCurrent == null) {
0596: return;
0597: }
0598:
0599: TransferTable t = tCurrent;
0600:
0601: t.Stmts.sSourceTable = tSourceTable.getText();
0602: t.Stmts.sDestTable = tDestTable.getText();
0603: t.Stmts.sDestDrop = tDestDrop.getText();
0604: t.Stmts.sDestCreateIndex = tDestCreateIndex.getText();
0605: t.Stmts.sDestDropIndex = tDestDropIndex.getText();
0606: t.Stmts.sDestCreate = tDestCreate.getText();
0607: t.Stmts.sDestDelete = tDestDelete.getText();
0608: t.Stmts.sSourceSelect = tSourceSelect.getText();
0609: t.Stmts.sDestInsert = tDestInsert.getText();
0610: t.Stmts.sDestAlter = tDestAlter.getText();
0611:
0612: //
0613: t.Stmts.bTransfer = cTransfer.getState();
0614: t.Stmts.bDrop = cDrop.getState();
0615: t.Stmts.bCreate = cCreate.getState();
0616: t.Stmts.bDelete = cDelete.getState();
0617: t.Stmts.bInsert = cInsert.getState();
0618: t.Stmts.bAlter = cAlter.getState();
0619: t.Stmts.bCreateIndex = cCreateIndex.getState();
0620: t.Stmts.bDropIndex = cDropIndex.getState();
0621:
0622: if (!t.Stmts.bTransfer) {
0623: t.Stmts.bInsert = false;
0624:
0625: cInsert.setState(false);
0626: }
0627:
0628: boolean reparsetable = ((t.Stmts.bFKForced != cFKForced
0629: .getState()) || (t.Stmts.bIdxForced != cIdxForced
0630: .getState()));
0631:
0632: t.Stmts.bFKForced = cFKForced.getState();
0633: t.Stmts.bIdxForced = cIdxForced.getState();
0634:
0635: if (reparsetable) {
0636: try {
0637: sourceDb.getTableStructure(t, targetDb);
0638: } catch (Exception e) {
0639: trace("Exception reading source tables: " + e);
0640: e.printStackTrace();
0641: }
0642: }
0643: }
0644:
0645: /**
0646: * Method declaration
0647: *
0648: *
0649: * @param t
0650: */
0651: private void displayTable(TransferTable t) {
0652:
0653: tCurrent = t;
0654:
0655: if (t == null) {
0656: return;
0657: }
0658:
0659: tSourceTable.setText(t.Stmts.sSourceTable);
0660: tDestTable.setText(t.Stmts.sDestTable);
0661: tDestDrop.setText(t.Stmts.sDestDrop);
0662: tDestCreateIndex.setText(t.Stmts.sDestCreateIndex);
0663: tDestDropIndex.setText(t.Stmts.sDestDropIndex);
0664: tDestCreate.setText(t.Stmts.sDestCreate);
0665: tDestDelete.setText(t.Stmts.sDestDelete);
0666: tSourceSelect.setText(t.Stmts.sSourceSelect);
0667: tDestInsert.setText(t.Stmts.sDestInsert);
0668: tDestAlter.setText(t.Stmts.sDestAlter);
0669: cTransfer.setState(t.Stmts.bTransfer);
0670: cDrop.setState(t.Stmts.bDrop);
0671: cCreate.setState(t.Stmts.bCreate);
0672: cDropIndex.setState(t.Stmts.bDropIndex);
0673: cCreateIndex.setState(t.Stmts.bCreateIndex);
0674: cDelete.setState(t.Stmts.bDelete);
0675: cInsert.setState(t.Stmts.bInsert);
0676: cAlter.setState(t.Stmts.bAlter);
0677: cFKForced.setState(t.Stmts.bFKForced);
0678: cIdxForced.setState(t.Stmts.bIdxForced);
0679: }
0680:
0681: /**
0682: * Method declaration
0683: *
0684: *
0685: * @param and
0686: */
0687: private void updateEnabled(boolean and) {
0688:
0689: boolean b = cTransfer.getState();
0690:
0691: tDestTable.setEnabled(and && b);
0692: tDestDrop.setEnabled(and && b && cDrop.getState());
0693: tDestCreate.setEnabled(and && b && cCreate.getState());
0694: tDestDelete.setEnabled(and && b && cDelete.getState());
0695: tDestCreateIndex
0696: .setEnabled(and && b && cCreateIndex.getState());
0697: tDestDropIndex.setEnabled(and && b && cDropIndex.getState());
0698: tSourceSelect.setEnabled(and && b);
0699: tDestInsert.setEnabled(and && b && cInsert.getState());
0700: tDestAlter.setEnabled(and && b && cAlter.getState());
0701: cDrop.setEnabled(and && b);
0702: cCreate.setEnabled(and && b);
0703: cDelete.setEnabled(and && b);
0704: cCreateIndex.setEnabled(and && b);
0705: cDropIndex.setEnabled(and && b);
0706: cInsert.setEnabled(and && b);
0707: cAlter.setEnabled(and && b);
0708: cFKForced.setEnabled(cAlter.getState());
0709: cIdxForced.setEnabled(cCreateIndex.getState());
0710: bStart.setEnabled(and);
0711:
0712: if (iTransferMode == TRFM_TRANSFER) {
0713: bContinue.setEnabled(and);
0714: }
0715: }
0716:
0717: /**
0718: * Method ProcessNextStep
0719: */
0720: private void ProcessNextStep() {
0721:
0722: switch (iSelectionStep) {
0723:
0724: case SELECT_SOURCE_CATALOG:
0725: case SELECT_DEST_CATALOG:
0726: if (CatalogToSelect()) {
0727: fMain.show();
0728:
0729: return;
0730: }
0731: break;
0732:
0733: case SELECT_DEST_SCHEMA:
0734: case SELECT_SOURCE_SCHEMA:
0735: if (SchemaToSelect()) {
0736: fMain.show();
0737:
0738: return;
0739: }
0740: break;
0741:
0742: case SELECT_SOURCE_TABLES:
0743: if (iTransferMode == TRFM_TRANSFER) {
0744: bStart.setLabel("Start Transfer");
0745: } else if (iTransferMode == TRFM_DUMP) {
0746: bStart.setLabel("Start Dump");
0747: } else if (iTransferMode == TRFM_RESTORE) {
0748: bStart.setLabel("Start Restore");
0749: }
0750:
0751: bStart.invalidate();
0752: bStart.setEnabled(false);
0753: lTable.setMultipleMode(false);
0754: RefreshMainDisplay();
0755: break;
0756:
0757: default:
0758: break;
0759: }
0760: }
0761:
0762: /**
0763: * Method declaration
0764: *
0765: *
0766: * @param ev
0767: */
0768: public void actionPerformed(ActionEvent ev) {
0769:
0770: if (ev.getSource() instanceof TextField) {
0771: saveTable();
0772:
0773: return;
0774: }
0775:
0776: String s = ev.getActionCommand();
0777: MenuItem i = new MenuItem();
0778:
0779: if (s == null) {
0780: if (ev.getSource() instanceof MenuItem) {
0781: i = (MenuItem) ev.getSource();
0782: s = i.getLabel();
0783: }
0784: }
0785:
0786: if (s == null) {
0787: }
0788:
0789: if (s.equals("Start Transfer") || s.equals("ReStart Transfer")) {
0790: bStart.setLabel("ReStart Transfer");
0791: bStart.invalidate();
0792:
0793: CurrentTransfer = 0;
0794: CurrentAlter = 0;
0795:
0796: transfer();
0797: } else if (s.equals("Continue Transfer")) {
0798: transfer();
0799: } else if (s.equals("Start Dump") || s.equals("Start Restore")) {
0800: CurrentTransfer = 0;
0801: CurrentAlter = 0;
0802:
0803: transfer();
0804: } else if (s.equals("Quit")) {
0805: Exit();
0806: } else if (s.indexOf("Select Schema") >= 0) {
0807: String[] selection = lTable.getSelectedItems();
0808:
0809: if ((selection == null) || (selection.length == 0)) {
0810: return;
0811: }
0812:
0813: if (iSelectionStep == Transfer.SELECT_SOURCE_SCHEMA) {
0814: sSourceSchemas = selection;
0815: } else {
0816: sDestSchema = selection[0];
0817: }
0818:
0819: if (iTransferMode == TRFM_DUMP) {
0820: iSelectionStep = Transfer.SELECT_SOURCE_TABLES;
0821: } else {
0822: iSelectionStep++;
0823: }
0824:
0825: ProcessNextStep();
0826: } else if (s.indexOf("Select Catalog") >= 0) {
0827: String selection = lTable.getSelectedItem();
0828:
0829: if ((selection == null) || (selection.equals(""))) {
0830: return;
0831: }
0832:
0833: if (iSelectionStep == Transfer.SELECT_SOURCE_CATALOG) {
0834: sSourceCatalog = selection;
0835: sSourceSchemas = null;
0836: } else {
0837: sDestCatalog = selection;
0838: sDestSchema = null;
0839:
0840: try {
0841: targetDb.setCatalog(sDestCatalog);
0842: } catch (Exception ex) {
0843: trace("Catalog "
0844: + sDestCatalog
0845: + " could not be selected in the target database");
0846:
0847: sDestCatalog = null;
0848: }
0849: }
0850:
0851: iSelectionStep++;
0852:
0853: ProcessNextStep();
0854: } else if (s.equals("Insert 10 rows only")) {
0855: iMaxRows = 10;
0856: } else if (s.equals("Insert 1000 rows only")) {
0857: iMaxRows = 1000;
0858: } else if (s.equals("Insert all rows")) {
0859: iMaxRows = 0;
0860: } else if (s.equals("Load Settings...")) {
0861: FileDialog f = new FileDialog(fMain, "Load Settings",
0862: FileDialog.LOAD);
0863:
0864: f.show();
0865:
0866: String file = f.getDirectory() + f.getFile();
0867:
0868: if (file != null) {
0869: LoadPrefs(file);
0870: displayTable(tCurrent);
0871: }
0872: } else if (s.equals("Save Settings...")) {
0873: FileDialog f = new FileDialog(fMain, "Save Settings",
0874: FileDialog.SAVE);
0875:
0876: f.show();
0877:
0878: String file = f.getDirectory() + f.getFile();
0879:
0880: if (file != null) {
0881: SavePrefs(file);
0882: }
0883: } else if (s.equals("Exit")) {
0884: windowClosing(null);
0885: }
0886: }
0887:
0888: /**
0889: * Method declaration
0890: *
0891: *
0892: * @param e
0893: */
0894: public void windowActivated(WindowEvent e) {
0895: }
0896:
0897: /**
0898: * Method declaration
0899: *
0900: *
0901: * @param e
0902: */
0903: public void windowDeactivated(WindowEvent e) {
0904: }
0905:
0906: /**
0907: * Method declaration
0908: *
0909: *
0910: * @param e
0911: */
0912: public void windowClosed(WindowEvent e) {
0913: }
0914:
0915: private void cleanup() {
0916:
0917: try {
0918: if (sourceDb != null) {
0919: sourceDb.close();
0920: }
0921:
0922: if (targetDb != null) {
0923: targetDb.close();
0924: }
0925: } catch (Exception e) {
0926: }
0927: }
0928:
0929: /**
0930: * Method declaration
0931: *
0932: *
0933: * @param ev
0934: */
0935: public void windowClosing(WindowEvent ev) {
0936:
0937: fMain.dispose();
0938:
0939: if (bMustExit) {
0940: System.exit(0);
0941: }
0942: }
0943:
0944: /**
0945: * Method declaration
0946: *
0947: *
0948: * @param e
0949: */
0950: public void windowDeiconified(WindowEvent e) {
0951: }
0952:
0953: /**
0954: * Method declaration
0955: *
0956: *
0957: * @param e
0958: */
0959: public void windowIconified(WindowEvent e) {
0960: }
0961:
0962: /**
0963: * Method declaration
0964: *
0965: *
0966: * @param e
0967: */
0968: public void windowOpened(WindowEvent e) {
0969: }
0970:
0971: /**
0972: * Method declaration
0973: *
0974: */
0975: private void initGUI() {
0976:
0977: Font fFont = new Font("Dialog", Font.PLAIN, 12);
0978:
0979: setLayout(new BorderLayout());
0980:
0981: Panel p = new Panel();
0982:
0983: p.setBackground(SystemColor.control);
0984: p.setLayout(new GridLayout(16, 1));
0985:
0986: tSourceTable = new TextField();
0987:
0988: tSourceTable.setEnabled(false);
0989:
0990: tDestTable = new TextField();
0991:
0992: tDestTable.addActionListener(this );
0993:
0994: tDestDrop = new TextField();
0995:
0996: tDestDrop.addActionListener(this );
0997:
0998: tDestCreate = new TextField();
0999:
1000: tDestCreate.addActionListener(this );
1001:
1002: tDestDelete = new TextField();
1003:
1004: tDestDelete.addActionListener(this );
1005:
1006: tDestCreateIndex = new TextField();
1007:
1008: tDestCreateIndex.addActionListener(this );
1009:
1010: tDestDropIndex = new TextField();
1011:
1012: tDestDropIndex.addActionListener(this );
1013:
1014: tSourceSelect = new TextField();
1015:
1016: tSourceSelect.addActionListener(this );
1017:
1018: tDestInsert = new TextField();
1019:
1020: tDestInsert.addActionListener(this );
1021:
1022: tDestAlter = new TextField();
1023:
1024: tDestAlter.addActionListener(this );
1025:
1026: cTransfer = new Checkbox("Transfer to destination table", true);
1027:
1028: cTransfer.addItemListener(this );
1029:
1030: cDrop = new Checkbox("Drop destination table (ignore error)",
1031: true);
1032:
1033: cDrop.addItemListener(this );
1034:
1035: cCreate = new Checkbox("Create destination table", true);
1036:
1037: cCreate.addItemListener(this );
1038:
1039: cDropIndex = new Checkbox(
1040: "Drop destination index (ignore error)", true);
1041:
1042: cDropIndex.addItemListener(this );
1043:
1044: cIdxForced = new Checkbox(
1045: "force Idx_ prefix for indexes names", false);
1046:
1047: cIdxForced.addItemListener(this );
1048:
1049: cCreateIndex = new Checkbox("Create destination index", true);
1050:
1051: cCreateIndex.addItemListener(this );
1052:
1053: cDelete = new Checkbox("Delete rows in destination table", true);
1054:
1055: cDelete.addItemListener(this );
1056:
1057: cInsert = new Checkbox("Insert into destination", true);
1058:
1059: cInsert.addItemListener(this );
1060:
1061: cFKForced = new Checkbox(
1062: "force FK_ prefix for foreign key names", false);
1063:
1064: cFKForced.addItemListener(this );
1065:
1066: cAlter = new Checkbox("Alter destination table", true);
1067:
1068: cAlter.addItemListener(this );
1069: p.add(createLabel("Source table"));
1070: p.add(tSourceTable);
1071: p.add(cTransfer);
1072: p.add(tDestTable);
1073: p.add(cDrop);
1074: p.add(tDestDrop);
1075: p.add(cCreate);
1076: p.add(tDestCreate);
1077: p.add(cDropIndex);
1078: p.add(tDestDropIndex);
1079: p.add(cCreateIndex);
1080: p.add(tDestCreateIndex);
1081: p.add(cDelete);
1082: p.add(tDestDelete);
1083: p.add(cAlter);
1084: p.add(tDestAlter);
1085: p.add(createLabel("Select source records"));
1086: p.add(tSourceSelect);
1087: p.add(cInsert);
1088: p.add(tDestInsert);
1089: p.add(createLabel(""));
1090: p.add(createLabel(""));
1091: p.add(cIdxForced);
1092: p.add(cFKForced);
1093: p.add(createLabel(""));
1094: p.add(createLabel(""));
1095:
1096: if (iTransferMode == TRFM_TRANSFER) {
1097: bStart = new Button("Start Transfer");
1098: bContinue = new Button("Continue Transfer");
1099:
1100: bContinue.setEnabled(false);
1101: } else if (iTransferMode == Transfer.TRFM_DUMP) {
1102: bStart = new Button("Start Dump");
1103: } else if (iTransferMode == Transfer.TRFM_RESTORE) {
1104: bStart = new Button("Start Restore");
1105: }
1106:
1107: bStart.addActionListener(this );
1108: p.add(bStart);
1109:
1110: if (iTransferMode == TRFM_TRANSFER) {
1111: bContinue.addActionListener(this );
1112: p.add(bContinue);
1113: }
1114:
1115: bStart.setEnabled(false);
1116: fMain.add("Center", createBorderPanel(p));
1117:
1118: lTable = new java.awt.List(10);
1119:
1120: lTable.addItemListener(this );
1121: fMain.add("West", createBorderPanel(lTable));
1122:
1123: tMessage = new TextField();
1124:
1125: Panel pMessage = createBorderPanel(tMessage);
1126:
1127: fMain.add("South", pMessage);
1128: }
1129:
1130: /**
1131: * Method declaration
1132: *
1133: *
1134: * @param center
1135: *
1136: * @return
1137: */
1138: private Panel createBorderPanel(Component center) {
1139:
1140: Panel p = new Panel();
1141:
1142: p.setBackground(SystemColor.control);
1143: p.setLayout(new BorderLayout());
1144: p.add("Center", center);
1145: p.add("South", createLabel(""));
1146: p.add("East", createLabel(""));
1147: p.add("West", createLabel(""));
1148: p.setBackground(SystemColor.control);
1149:
1150: return p;
1151: }
1152:
1153: /**
1154: * Method declaration
1155: *
1156: *
1157: * @param s
1158: *
1159: * @return
1160: */
1161: private Label createLabel(String s) {
1162:
1163: Label l = new Label(s);
1164:
1165: l.setBackground(SystemColor.control);
1166:
1167: return l;
1168: }
1169:
1170: private void SavePrefs(String f) {
1171: saveTable();
1172: TransferCommon.savePrefs(f, sourceDb, targetDb, this , tTable);
1173: }
1174:
1175: private void LoadPrefs(String f) {
1176:
1177: TransferTable t;
1178:
1179: trace("Parsing Settings file");
1180: bStart.setEnabled(false);
1181:
1182: if (iTransferMode == TRFM_TRANSFER) {
1183: bContinue.setEnabled(false);
1184: }
1185:
1186: tTable = TransferCommon.loadPrefs(f, sourceDb, targetDb, this );
1187: iSelectionStep = SELECT_SOURCE_TABLES;
1188:
1189: lTable.removeAll();
1190:
1191: for (int i = 0; i < tTable.size(); i++) {
1192: t = (TransferTable) tTable.elementAt(i);
1193:
1194: lTable.add(t.Stmts.sSourceTable);
1195: }
1196:
1197: t = (TransferTable) tTable.elementAt(0);
1198:
1199: displayTable(t);
1200: lTable.select(0);
1201: updateEnabled(true);
1202: lTable.invalidate();
1203:
1204: if (iTransferMode == TRFM_TRANSFER) {
1205: bStart.setLabel("Start Transfer");
1206: trace("Edit definitions and press [Start Transfer]");
1207: } else if (iTransferMode == TRFM_DUMP) {
1208: bStart.setLabel("Start Dump");
1209: trace("Edit definitions and press [Start Dump]");
1210: } else if (iTransferMode == TRFM_RESTORE) {
1211: bStart.setLabel("Start Restore");
1212: trace("Edit definitions and press [Start Restore]");
1213: }
1214:
1215: bStart.invalidate();
1216:
1217: if (iTransferMode == TRFM_TRANSFER) {
1218: bContinue.setEnabled(false);
1219: }
1220: }
1221:
1222: /**
1223: * Method declaration
1224: *
1225: */
1226: private void transfer() {
1227:
1228: saveTable();
1229: updateEnabled(false);
1230: trace("Start Transfer");
1231:
1232: int TransferIndex = CurrentTransfer;
1233: int AlterIndex = CurrentAlter;
1234: TransferTable t = null;
1235: long startTime, stopTime;
1236:
1237: startTime = System.currentTimeMillis();
1238:
1239: try {
1240: for (int i = TransferIndex; i < tTable.size(); i++) {
1241: CurrentTransfer = i;
1242: t = (TransferTable) tTable.elementAt(i);
1243:
1244: lTable.select(i);
1245: displayTable(t);
1246: t.transferStructure();
1247: t.transferData(iMaxRows);
1248: }
1249:
1250: for (int i = AlterIndex; i < tTable.size(); i++) {
1251: CurrentAlter = i;
1252: t = (TransferTable) tTable.elementAt(i);
1253:
1254: lTable.select(i);
1255: displayTable(t);
1256: t.transferAlter();
1257: }
1258:
1259: stopTime = System.currentTimeMillis();
1260:
1261: trace("Transfer finished successfully in: "
1262: + (stopTime - startTime) / 1000.00 + " sec");
1263:
1264: if (iTransferMode == TRFM_TRANSFER) {
1265: bContinue.setLabel("Quit");
1266: bContinue.setEnabled(true);
1267: bContinue.invalidate();
1268: } else {
1269: bStart.setLabel("Quit");
1270: bStart.setEnabled(true);
1271: bStart.invalidate();
1272: }
1273: } catch (Exception e) {
1274: String last = tMessage.getText();
1275:
1276: trace("Transfer stopped - " + last + " / / Error: "
1277: + e.getMessage());
1278: e.printStackTrace();
1279: }
1280:
1281: if (iTransferMode == TRFM_TRANSFER) {
1282: bContinue.setEnabled((CurrentAlter < tTable.size()));
1283: }
1284:
1285: updateEnabled(true);
1286: System.gc();
1287: }
1288:
1289: protected void Exit() {
1290:
1291: cleanup();
1292: fMain.dispose();
1293:
1294: if (bMustExit) {
1295: System.exit(0);
1296: }
1297: }
1298: }
|