--- FM2SQL/Attic/Convert.java 2004/02/19 10:35:06 1.36 +++ FM2SQL/Attic/Convert.java 2004/04/05 10:14:01 1.53 @@ -1,3 +1,17 @@ +/* + * Convert.java -- Converter class - Filemaker to SQL Converter + * Copyright (C) 2003 Robert Gordesch (rogo@mpiwg-berlin.mpg.de) + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. Please read license.txt for the full details. A copy of + * the GPL may be found at http://www.gnu.org/copyleft/lgpl.html You should + * have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA Created on 15.09.2003 by + * rogo + */ + import java.util.*; import java.sql.*; import java.awt.Cursor; @@ -59,11 +73,11 @@ class Convert System.out.println("Finished!"); //convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null); } - public static void convertBatch(DBBean source, DBBean destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids,int mode) throws Exception + public static void convertBatch(DBBean source, DBBean destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids,int mode,String delimiter) throws Exception { bean = source; beanDest = destination; - convert(null,null,names,layouts,selects,creates,ids,mode); + convert(null,null,names,layouts,selects,creates,ids,mode,delimiter); if(true) return; StringBuffer command = null; try @@ -190,7 +204,7 @@ class Convert { obj = row.get(k); if (obj instanceof ArrayList) - obj = ((List) obj).get(0); + obj = formatFileMakerArray((List) obj,"\n"); String str = (obj == null) ? "NULL" : obj.toString(); if (!str.equals("NULL")) pstm.setString(k + 1, str); @@ -221,13 +235,35 @@ class Convert // dialog.setVisible(false); } - + public static String formatFileMakerArray(List list, String delimiter) + { + StringBuffer formattedString = new StringBuffer(); + for(int i=0;i= 0) + { + stm.executeUpdate("drop table " + beanDest.getQC() + destTableName + beanDest.getQC()); + tables.remove(destTableName); + System.out.println("dropped table" + destTableName); + + } + /* + if(destTableName.equals("")) if (tables.indexOf(names.get(tbIndex)) >= 0) { stm.executeUpdate("drop table " + beanDest.getQC() + names.get(tbIndex) + beanDest.getQC()); @@ -507,9 +592,10 @@ class Convert 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 ((tables.indexOf(destTableName) < 0)) //&& 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"); @@ -541,7 +627,7 @@ class Convert // System.exit(0); //command.append(DBBean.getQC()); } else - command = new StringBuffer().append(creates.get(tbIndex).toString()); + command = new StringBuffer().append(creates.get(tbIndex).toString().toLowerCase()); stm.executeUpdate(command.toString()); } @@ -556,10 +642,12 @@ class Convert command.append("INSERT INTO "); command.append(beanDest.getQC()); - command.append(convertText((String) names.get(tbIndex))); + command.append(destTableName); //convertText((String) names.get(tbIndex))); command.append(beanDest.getQC()); + command.append(" values ( "); + // add a question marks for every field for (int i = 0; i < bean.getColumnNames().size() - 1; ++i) command.append("?,"); command.append("?)"); @@ -570,6 +658,7 @@ class Convert int endIndex = -1; String tempQuery = query; String tempID = bean.getQC() + idField + bean.getQC(); + // if id_field not do incremental conversion else do it all at once if (!idField.equals("")) { long startTime = System.currentTimeMillis(); @@ -598,9 +687,9 @@ class Convert { System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement()); if (query.indexOf("where") > 0) - tempQuery = query + " and " + tempID + ">=" + vec.get(k) + " and " + tempID + "<=" + vec.get(k + deltaID); + tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID + "<='" + vec.get(k + deltaID)+"'"; else - tempQuery = query + "where " + tempID + ">=" + vec.get(k) + " and " + tempID + "<=" + vec.get(k + deltaID); + tempQuery = query + " where " + tempID + ">='" + vec.get(k) + "' and " + tempID + "<='" + vec.get(k + deltaID)+"'"; System.out.println(tempQuery); if(dialog!=null) dialog.title.setText("Reading table data ..."); @@ -609,10 +698,11 @@ class Convert if(dialog!=null) dialog.title.setText("Writing table data ..."); - command = writeDatainDestTable(dialog, command, k, pstm, rowCount); + command = writeDatainDestTable(dialog, command, k, pstm, rowCount,delimiter); endIndex = k + deltaID; } System.out.println(endIndex); + //all data written ? if not write last chunk of data if (endIndex == vec.size() - 1) System.out.println("fits"); else @@ -620,31 +710,33 @@ class Convert System.out.println(" last intervall from " + vec.get(endIndex) + " " + vec.lastElement()); if (query.indexOf("where") > 0) - tempQuery = query + " and " + tempID + ">=" + vec.get(endIndex) + " and " + tempID + "<=" + vec.lastElement(); + tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and " + tempID + "<='" + vec.lastElement()+"'"; else - tempQuery = query + "where " + tempID + ">=" + vec.get(endIndex) + " and " + tempID + "<=" + vec.lastElement(); + tempQuery = query + " where " + tempID + ">='" + vec.get(endIndex) + "' and " + tempID + "<='" + vec.lastElement()+"'"; System.out.println(tempQuery); if(dialog!=null) dialog.title.setText("Reading table data ..."); bean.makeQuery(tempQuery, 0); if(dialog!=null) dialog.title.setText("Writing table data ..."); - command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount); + command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount,delimiter); } + // prepare new query for next chunk if (query.indexOf("where") > 0) - tempQuery = query + " and " + tempID + ">" + vec.lastElement(); + tempQuery = query + " and " + tempID + ">'" + vec.lastElement()+"'"; else - tempQuery = query + " where " + tempID + ">" + vec.lastElement(); + tempQuery = query + " where " + tempID + ">'" + vec.lastElement()+"'"; } long endTime = System.currentTimeMillis(); System.out.println("Time for incremental convert elapsed " + (endTime - startTime)); } else { + // read and write all in one big chunk long startTime = System.currentTimeMillis(); - + bean.makeQuery(query, 0); - command = writeDatainDestTable(dialog, command, j, pstm, rowCount); + command = writeDatainDestTable(dialog, command, j, pstm, rowCount,delimiter); long endTime = System.currentTimeMillis(); System.out.println("Time for old convert elapsed " + (endTime - startTime)); @@ -676,7 +768,18 @@ class Convert dialog.setVisible(false); } } - private static StringBuffer writeDatainDestTable(FM2SQL.ProgressDialog dialog, StringBuffer command, int j, PreparedStatement pstm, int rowCount) throws Exception, SQLException + /** + * Writes data to the destination table + * @param dialog progress dialog + * @param command + * @param j data index for progress bar + * @param pstm prepared statement + * @param rowCount number of datasets + * @return command + * @throws Exception + * @throws SQLException + */ + private static StringBuffer writeDatainDestTable(FM2SQL.ProgressDialog dialog, StringBuffer command, int j, PreparedStatement pstm, int rowCount,String delimiter) throws Exception, SQLException { Vector row; while ((row = bean.getNextRow()) != null) @@ -722,7 +825,9 @@ class Convert { obj = row.get(k); if (obj instanceof ArrayList) - obj = ((List) obj).get(0); + if (obj instanceof ArrayList) + obj = formatFileMakerArray((List) obj,delimiter); + String str = (obj == null) ? "NULL" : obj.toString(); if (!str.equals("NULL")) pstm.setString(k + 1, str); @@ -739,6 +844,11 @@ class Convert return command; } + /** + * removes special characters from the input string as well as .fp5 + * @param newName String to change + * @return + */ public static String convertText(String newName) { StringBuffer alterMe = new StringBuffer(newName.trim().toLowerCase()); @@ -832,6 +942,11 @@ class Convert } return alterMe.toString(); } + /** + * Converts > and < in an entity (> or <) + * @param newName + * @return + */ public static String convertToEntities(String newName) { StringBuffer alterMe = new StringBuffer(newName.trim()); @@ -862,6 +977,11 @@ class Convert } return alterMe.toString(); } + /** + * Masks the single quote character '-->\' + * @param newName + * @return + */ public static String convertUml(String newName) { StringBuffer alterMe = new StringBuffer(newName.trim()); @@ -971,7 +1091,11 @@ class Convert } return alterMe.toString(); } - +/** + * parses the input xml file for batch conversion + * called from readXMLFile + * * @param sb + */ public static void parseXMLConfig(StringBuffer sb) { boolean finished = false; @@ -986,6 +1110,7 @@ class Convert Vector selects = new Vector(); Vector creates = new Vector(); Vector ids = new Vector(); + String delimiter = "|"; int mode = -1; try @@ -1010,7 +1135,7 @@ class Convert Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 }); Node node3 = root.find("convert/source/database", new int[] { 1, 1, i }); Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 }); - + Node delimiterNode =root.find("convert/source/database/delimiter", new int[] { 1, 1, i, 1, 1 }); if (node3 == null) throw new Error("parse error database tag missing"); if (node == null) @@ -1019,6 +1144,7 @@ class Convert throw new Error("parse error user tag missing"); if (node2 == null) throw new Error("parse error password tag missing"); + if(delimiterNode!=null) delimiter = delimiterNode.getCharacters(); String url = node.getCharacters(); String user = node1.getCharacters(); String password = node2.getCharacters(); @@ -1038,6 +1164,9 @@ class Convert mode = DataBase.APPEND_MODE; else if (modeString.equals("update")) mode = DataBase.UPDATE_MODE; + else if (modeString.equals("delete")) + mode = DataBase.DELETE_MODE; + // if(node3!=null) // System.out.println(node3.name); @@ -1086,7 +1215,9 @@ class Convert creates.add(""); } - databases.add(new DataBase(database, tables, layouts, selects, creates, ids, mode)); + DataBase dataBase = new DataBase(database, tables, layouts, selects, creates, ids, mode); + dataBase.delimiter = delimiter; + databases.add(dataBase); } DBBean database = new DBBean(); // parse dataBase @@ -1104,7 +1235,7 @@ class Convert { DataBase db = (DataBase) iter.next(); if (mode != DataBase.UPDATE_MODE) - convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids,mode); + convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids,mode,db.delimiter); else update(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids, mode); @@ -1152,7 +1283,7 @@ class Convert Vector selects = new Vector(); Vector creates = new Vector(); Vector ids = new Vector(); - + String delimiter = "|"; int mode = -1; try { @@ -1176,7 +1307,10 @@ class Convert Node node2 = root.find("convert/source/database/password", new int[] { 1, 1, i, 1, 1 }); Node node3 = root.find("convert/source/database", new int[] { 1, 1, i }); Node nodeMode = root.find("convert/source/database/mode", new int[] { 1, 1, i, 1, 1 }); - + Node delimiterNode =root.find("convert/source/database/delimiter", new int[] { 1, 1, i, 1, 1 }); + + if (delimiterNode != null) + delimiter = delimiterNode.getCharacters(); if (node3 == null) throw new Error("parse error database tag missing"); if (node == null) @@ -1204,6 +1338,8 @@ class Convert mode = DataBase.APPEND_MODE; else if (modeString.equals("update")) mode = DataBase.UPDATE_MODE; + else if (modeString.equals("delete")) + mode = DataBase.DELETE_MODE; // if(node3!=null) // System.out.println(node3.name); @@ -1254,7 +1390,9 @@ class Convert creates.add(""); } - databases.add(new DataBase(database, tables, layouts, selects, creates, ids, mode)); + DataBase dataBase=new DataBase(database, tables, layouts, selects, creates, ids, mode); + dataBase.delimiter=delimiter; + databases.add(dataBase); } DBBean database = new DBBean(); // parse dataBase @@ -1317,9 +1455,12 @@ class Convert //contents=n.contents.v i=0; } // System.out.println(n.type); - } } + /** + * reads the specified xml file + * @param xmlFile + */ public static void readXMLFile(String xmlFile) { try @@ -1342,6 +1483,13 @@ class Convert e.printStackTrace(); } } + + /** + * Helper class for XML-File parsing + * Holds the parsed data + * @author rogo + * + */ public static class DataBase { DBBean bean; @@ -1350,9 +1498,12 @@ class Convert Vector layouts; Vector tables; Vector ids; + String delimiter = "//"; final static int CONVERT_MODE = 1; final static int APPEND_MODE = 2; final static int UPDATE_MODE = 3; + final static int DELETE_MODE = 4; + int mode = -1; public DataBase(DBBean bean, Vector tables, Vector layouts, Vector selects, Vector creates, Vector ids, int mode) @@ -1366,6 +1517,11 @@ class Convert this.mode = mode; this.bean.setIDVector(ids); } + /** + * writes the data contained in this object to the buffered writer + * * @param buffr + * @throws Exception + */ public void exportToXML(BufferedWriter buffr) throws Exception { // ids=bean.getIDVector(); @@ -1373,6 +1529,7 @@ class Convert buffr.write(" " + bean.url + "\n"); buffr.write(" " + bean.user + "\n"); buffr.write(" " + bean.passwd + "\n"); + buffr.write(" "+delimiter+"\n"); String modeString = ""; if (mode == CONVERT_MODE) modeString = "convert"; @@ -1380,6 +1537,8 @@ class Convert modeString = "append"; else if (mode == UPDATE_MODE) modeString = "update"; + else if (mode == DELETE_MODE) + modeString = "delete"; buffr.write(" " + modeString + "\n"); int index = 0; @@ -1440,4 +1599,206 @@ class Convert buffw.write("\n"); buffw.close(); } -} \ No newline at end of file + public static void delete(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode) throws Exception + { + FM2SQL.ProgressDialog dialog = null; + if (FM2SQL.fmInstance != null) + { + 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(); + } + // 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"); + bean.setConnection(source); + if (names == null) + names = bean.getTableNames(); + // Collections.sort(names); + int tbIndex = 1; + + // System.out.println("Start at "+names.indexOf("archimedes_facsimiles")); + for (tbIndex = 0; tbIndex < names.size(); ++tbIndex) + { + Vector[] result = null; + java.util.TreeSet myIds = new TreeSet(); + java.util.TreeSet myIdsDest = new TreeSet(); + int deltaID = 1; + String idField = ""; + String destTableName = ""; + + try + { + 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 != "") + { + 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); + + } + dialog.title.setText("Getting table data ..."); + dialog.table.setText(names.get(tbIndex).toString()); + dialog.status.setText("Table " + (tbIndex + 1) + " of " + names.size()); + dialog.show(); + bean.getConnection(); + bean.makeQuery(query, 50); + idField = ids.get(tbIndex).toString(); + + } catch (Exception e) + { + continue; + } + // determine destTableName from createStatement or from source table name + if (!creates.get(tbIndex).equals("")) + { + String create = creates.get(tbIndex).toString().toLowerCase(); + int fromIndex = create.indexOf("table") + 5; + int toIndex = create.indexOf("("); + destTableName = create.substring(fromIndex, toIndex).replaceAll(beanDest.getQC(), "").trim(); + System.out.println("destTable " + destTableName); + + } else + destTableName = convertText(names.get(tbIndex).toString()); + + // for id kram + Vector vec = null; + Vector vecDest = null; + // tempo + beanDest.setConnection(destination); + int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query); + String tempID = bean.getQC() + idField + bean.getQC(); + String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC(); + + int endIndex = -1; + String tempQuery = query; + String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName); + String tempQueryDest = destQuery; + // remove extra query parts destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1); + System.out.println("new Query " + tempQueryDest); + if (!idField.equals("")) + { + long startTime = System.currentTimeMillis(); + int counter = -1; + while (true) + { + ++counter; + if (counter == 0 && dialog != null) + dialog.title.setText("Check if data is available"); + else if (dialog != null) + dialog.title.setText("Check if more data is available"); + myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), tempQuery, numHits); + myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest, numHits); + if (myIds.isEmpty()) + break; + vec = new Vector(myIds); + vecDest = new Vector(myIdsDest); + rowCount = vec.size(); + // Deletion will work this way + Vector deleted = new Vector(vec); + Vector linesToDelete = new Vector(vecDest); + // remove all lines that should not be deleted + linesToDelete.removeAll(deleted); + // System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIdsDest.size()); + /// @TODO complete delete task remove query show lines to be deleted let user choose if he wants that + System.out.println("number of lines to be deleted " + linesToDelete.size()); + deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls); + 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(); + + if (dialog != null) + dialog.title.setText(" Deleting table data ..."); + + int j = -1; + + Vector row = null; + command = new StringBuffer(); + + command.append("DELETE FROM"); + command.append(beanDest.getQC()); + command.append(destTableName); + //command.append(convertText((String) names.get(tbIndex))); + command.append(beanDest.getQC()); + int size = bean.getColumnNames().size(); + command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " = ?"); + PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString()); + System.out.println(command + " " + tbIndex); + //int rowCount = bean.getRowCount(query); + // int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex)); + while (true) + { + + ++j; + if (j == linesToDelete.size()) + break; + //print rows + pstm.setString(1, linesToDelete.get(j).toString()); + System.out.println(pstm.toString()); + pstm.execute(); + if (dialog != null) + dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0)); + command = null; + } + // prepare new query for next chunk + if (query.indexOf("where") > 0) + tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'"; + else + tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'"; + + } //to outer while + } // to idfield if + } // table loop + + } catch (Exception e) + { + System.out.println("Error while connecting to database " + e); + if (dialog != null) + { + 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 !"); + + } + if (dialog != null) + { + dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); + FM2SQL.fmInstance.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); + + dialog.setVisible(false); + } + } // to method + + } \ No newline at end of file