--- FM2SQL/Attic/Convert.java 2004/10/29 10:28:10 1.85 +++ FM2SQL/Attic/Convert.java 2004/11/18 11:17:43 1.86 @@ -94,7 +94,7 @@ class Convert final static int numIntervalls = 4; static boolean debug = false; - + static boolean isGUI = true; public static void main(String args[]) throws IOException { @@ -113,7 +113,8 @@ class Convert System.exit(-1); } try - { File temp=File.createTempFile("fm2sql",".txt"); + { + File temp = File.createTempFile("fm2sql", ".txt"); file = new FileOutputStream(temp); } catch (FileNotFoundException e1) @@ -139,168 +140,6 @@ class Convert bean = source; beanDest = destination; convert(null, null, names, layouts, selects, creates, ids, mode, delimiter); - if (true) - return; - StringBuffer command = null; - try - { - bean.setConnection(source.url); - if (names == null) - names = bean.getTableNames(); - //Collections.sort(names); - int tbIndex = 1; - - for (tbIndex = 0; tbIndex < names.size(); ++tbIndex) - { - Vector[] result = null; - try - { - String query = "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC(); - String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString(); - query = (selects != null) ? selects.get(tbIndex).toString() : query; - //if vectors[1].get(i) != null) - if (!layout.equals("")) - { - System.out.println("before " + query + " table" + names.get(tbIndex)); - layout = " layout " + bean.getQC() + layout + bean.getQC(); - String name = names.get(tbIndex).toString(); - StringBuffer queryLayout = new StringBuffer(query); - queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout); - query = queryLayout.toString(); - System.out.println("added layout " + query); - - } - System.out.println(" performing query " + query); - //result = bean.getQueryData(query, null, 0); - bean.getConnection(); - bean.makeQuery(query, 0); - } - catch (Exception e) - { - System.out.println(e.getMessage()); - e.printStackTrace(); - continue; - } - //beanDest.setConnection("jdbc:postgresql://erebos/test3"); - beanDest.setConnection(destination.url); - - Statement stm = beanDest.getConnection().createStatement(); - - Vector tables = beanDest.getTableNames(); - // Collections.sort(tables); - System.out.println("converting table " + names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); - tables = beanDest.getTableNames(); - // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString())); - stm = beanDest.getConnection().createStatement(); - // System.exit(0); - if (mode == Convert.DataBase.CONVERT_MODE) - { - if (tables.indexOf(names.get(tbIndex)) >= 0) - { - stm.executeUpdate("drop table " + beanDest.getQC() + names.get(tbIndex) + beanDest.getQC()); - tables.remove((String) names.get(tbIndex)); - System.out.println("dropped table " + names.get(tbIndex)); - } - else if (tables.indexOf(convertText(names.get(tbIndex).toString())) >= 0) - { - stm.executeUpdate("drop table " + beanDest.getQC() + convertText((String) names.get(tbIndex)) + beanDest.getQC()); - tables.remove(convertText((String) names.get(tbIndex))); - System.out.println("dropped table " + names.get(tbIndex)); - } - - if (tables.indexOf(names.get(tbIndex)) < 0 && tables.indexOf(convertText(names.get(tbIndex).toString())) < 0) - { - if (creates.get(tbIndex).equals("") || creates.get(tbIndex).toString().toLowerCase().indexOf("create") < 0) - { - System.out.println("Warning empty or invalid create statement - creating one for you\n"); - - command = new StringBuffer(50); - command.append("CREATE TABLE "); - command.append(beanDest.getQC()); - command.append(convertText((String) names.get(tbIndex))); - command.append(beanDest.getQC()); - command.append("("); - String type = null; - Vector columnNames = bean.getColumnNames(); - for (int i = 0; i < columnNames.size() - 1; ++i) - { - type = bean.metaData.getColumnTypeName(i + 1); - // System.out.println(i+" "+result[1].get(i)+" - // "+type); - type = (type.equals("NUMBER")) ? "INT4" : type; - type = (type.equals("CONTAINER")) ? "TEXT" : type; - - command.append(beanDest.getQC() + convertText((String) columnNames.get(i)) + beanDest.getQC() + " " + type + ", "); - } - type = bean.metaData.getColumnTypeName(columnNames.size()); - type = (type.equals("NUMBER")) ? "INT4" : type; - type = (type.equals("CONTAINER")) ? "TEXT" : type; - command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type); - command.append(" )"); - } - else - command = new StringBuffer().append(creates.get(tbIndex).toString()); - - System.out.println(command); - // System.exit(0); - //command.append(DBBean.getQC()); - stm.executeUpdate(command.toString()); - - } - } - Vector row = null; - command = new StringBuffer(); - - command.append("INSERT INTO "); - command.append(beanDest.getQC()); - command.append(convertText((String) names.get(tbIndex))); - command.append(beanDest.getQC()); - command.append(" values ( "); - - for (int i = 0; i < bean.getColumnNames().size() - 1; ++i) - command.append("?,"); - command.append("?)"); - PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString()); - System.out.println(command); - while ((row = bean.getNextRow()) != null) - { - //print rows - Object obj = null; - for (int k = 0; k < row.size(); ++k) - { - obj = row.get(k); - if (obj instanceof ArrayList) - obj = formatFileMakerArray((List) obj, "\n"); - String str = (obj == null) ? "NULL" : obj.toString(); - if (!str.equals("NULL")) - pstm.setString(k + 1, str); - else - pstm.setNull(k + 1, Types.NULL); - } - pstm.execute(); - - } // to for loop - - } - } - catch (Exception e) - { - System.out.println("Error while connecting to database " + e); - //dialog.setVisible(false); - //dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - //FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream(); - java.io.PrintStream stream = new java.io.PrintStream(b); - stream.print(command + "\n\n"); - e.printStackTrace(stream); - System.err.println(b); - //FM2SQL.showErrorDialog(b.toString(), "Error occured !"); - - } - // dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - //FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - - // dialog.setVisible(false); } public static String formatFileMakerArray(List list, String delimiter) @@ -334,37 +173,26 @@ class Convert FM2SQL.ProgressDialog dialog = null; if (isGUI) { - dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance, bean); - dialog.setTitle("Conversion running ..."); - dialog.title.setText("Getting table data ..."); - dialog.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2); - dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - dialog.thread = Thread.currentThread(); + dialog = initDialog(); } // setting user and passwd bean.setUserAndPasswd(user, passwd); // setting user and passwd beanDest.setUserAndPasswd(userDest, passwdDest); - if (dialog != null) - dialog.setSize(400, 250); + StringBuffer command = null; String query = null; try { - //bean.setConnection("jdbc:fmpro:http://141.14.237.74:8050"); - //bean.setConnection("jdbc:postgresql://erebos/test","postgres","rogo"); - System.out.println("before getConnection"); - + bean.setConnection(source); - System.out.println("after getConnection"); - + if (names == null) names = bean.getTableNames(); // Collections.sort(names); int tbIndex = 1; - System.out.println("Start at table "+names.firstElement()); + System.out.println("Start at table " + names.firstElement()); for (tbIndex = 0; tbIndex < names.size(); ++tbIndex) { Vector[] result = null; @@ -377,20 +205,12 @@ class Convert //if vectors[1].get(i) != null) if (layout != "") { - layout = " layout " + bean.getQC() + layout + bean.getQC(); - String name = names.get(tbIndex).toString(); - StringBuffer queryLayout = new StringBuffer(query); - queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout); - query = queryLayout.toString(); - System.out.println("added layout " + query); + query = addLayoutToQuery(names, query, tbIndex, layout); } if (dialog != null) { - dialog.title.setText("Getting table data ..."); - dialog.table.setText(names.get(tbIndex).toString()); - dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size()); - dialog.show(); + prepareDialogforUse(names, dialog, tbIndex); } bean.getConnection(); bean.makeQuery(query, 0); @@ -416,22 +236,20 @@ class Convert else destTableName = convertText(names.get(tbIndex).toString()); - //beanDest.setConnection("jdbc:postgresql://erebos/test3"); beanDest.setConnection(destination); Statement stm = beanDest.getConnection().createStatement(); Vector tables = beanDest.getTableNames(); - // Collections.sort(tables); + System.out.println(names.get(tbIndex) + " " + tables.indexOf(convertText((String) names.get(tbIndex)))); // "//beanDest.getTypeNames()); tables = beanDest.getTableNames(); - // System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString())); stm = beanDest.getConnection().createStatement(); - // System.exit(0); if (dialog != null) dialog.title.setText("Updating table data ..."); - else System.out.println("Updating table data ..."); + else + System.out.println("Updating table data ..."); int j = -1; Vector row = null; @@ -458,27 +276,6 @@ class Convert j++; //print rows Object obj = null; - /* - * for(int k=0;k = - * 0) { stm.executeUpdate("drop table " + beanDest.getQC() + - * names.get(tbIndex) + beanDest.getQC()); tables.remove((String) - * names.get(tbIndex)); System.out.println("dropped table" + - * names.get(tbIndex)); } else if - * (tables.indexOf(convertText(names.get(tbIndex).toString())) >= 0) { - * stm.executeUpdate("drop table " + beanDest.getQC() + - * convertText((String) names.get(tbIndex)) + beanDest.getQC()); - * tables.remove(convertText((String) names.get(tbIndex))); - * System.out.println("dropped table" + names.get(tbIndex)); } - */ - if ((tables.indexOf(destTableName) < 0)) //&& - // tables.indexOf(names.get(tbIndex)) - // < 0 && - // tables.indexOf(convertText(names.get(tbIndex).toString())) - // < 0 ) + if ((tables.indexOf(destTableName) < 0)) { if (creates.get(tbIndex).equals("") || creates.get(tbIndex).toString().toLowerCase().indexOf("create") < 0) @@ -716,9 +504,6 @@ class Convert command.append(beanDest.getQC() + convertText((String) columnNames.get(columnNames.size() - 1)) + beanDest.getQC() + " " + type); command.append(" )"); - // System.out.println(command); - // System.exit(0); - //command.append(DBBean.getQC()); } else command = new StringBuffer().append(creates.get(tbIndex).toString().toLowerCase()); @@ -736,8 +521,7 @@ class Convert command.append("INSERT INTO "); command.append(beanDest.getQC()); - command.append(destTableName); //convertText((String) - // names.get(tbIndex))); + command.append(destTableName); command.append(beanDest.getQC()); command.append(" ("); for (int i = 0; i < fieldNames.length; i++) @@ -845,6 +629,8 @@ class Convert System.out.println("Time for old convert elapsed " + (endTime - startTime)); } + if (isGUI) + resetGUI(dialog); } } catch (Exception e) @@ -852,14 +638,8 @@ class Convert System.out.println("Error while connecting to database " + e); if (isGUI) { - dialog.setVisible(false); - dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - java.io.ByteArrayOutputStream b = new java.io.ByteArrayOutputStream(); - java.io.PrintStream stream = new java.io.PrintStream(b); - stream.print(command + "\n\n"); - e.printStackTrace(stream); - FM2SQL.showErrorDialog(b.toString(), "Error occured !"); + showExceptionDialog(dialog, command, e); + resetGUI(dialog); } else { @@ -867,12 +647,38 @@ class Convert } } - if (isGUI) - { - dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); - dialog.setVisible(false); - } + + } + + /** + * @param names + * @param dialog + * @param tbIndex + */ + private static void prepareDialogforUse(Vector names, FM2SQL.ProgressDialog dialog, int tbIndex) + { + dialog.title.setText("Reading table data ..."); + dialog.table.setText(names.get(tbIndex).toString()); + dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size()); + dialog.show(); + } + + /** + * @param names + * @param query + * @param tbIndex + * @param layout + * @return + */ + private static String addLayoutToQuery(Vector names, String query, int tbIndex, String layout) + { + layout = " layout " + bean.getQC() + layout + bean.getQC(); + String name = names.get(tbIndex).toString(); + StringBuffer queryLayout = new StringBuffer(query); + queryLayout.insert(queryLayout.indexOf(name) + name.length() + 1, " " + layout); + query = queryLayout.toString(); + System.out.println("added layout " + query); + return query; } /** @@ -897,36 +703,7 @@ class Convert while ((row = bean.getNextRow()) != null) { j++; - // row = (Vector) result[0].get(j); - /* - * command = new StringBuffer(); - * - * command.append("INSERT INTO "); command.append(beanDest.getQC()); - * command.append(convertText((String) names.get(tbIndex))); - * command.append(beanDest.getQC()); command.append(" values ( "); - */ - //print rows Object obj = null; - /* - * for(int k=0;k