--- FM2SQL/src/Convert.java 2005/03/21 10:32:03 1.5 +++ FM2SQL/src/Convert.java 2005/04/19 10:49:05 1.10 @@ -93,9 +93,13 @@ class Convert public void executeCommand() throws SQLException, Exception { + System.out.println("Executing command: \n"); + System.out.println(command); + java.sql.Connection con = bean.getConnection(); Statement stm = con.createStatement(); stm.execute(command); + stm.close(); } } @@ -115,7 +119,7 @@ class Convert final static int numIntervalls = 4; - static boolean debug = false; + static boolean debug = !false; static boolean isGUI = true; @@ -129,6 +133,10 @@ class Convert static Vector postSQLCommands = new Vector(); + static final String versionID = new String("FM2SQL Version 0.9.1b\n"); + + private static boolean noError = false; + public static void main(String args[]) throws IOException { /* @@ -138,32 +146,50 @@ class Convert * //System.out.println(new String(b,"UTF-8")); } catch * (UnsupportedEncodingException e) { e.printStackTrace(); } */ + File tmpPath = new File(System.getProperty("java.io.tmpdir")); isGUI = false; FileOutputStream file = null; if (args.length != 1) { + System.out.println(versionID); System.out.println("Usage: java Convert "); System.exit(-1); } + File temp = null; try { - File temp = File.createTempFile("fm2sql", ".txt"); + temp = new File(tmpPath, "fm2sql.txt"); + int count = 1; + while (temp.exists()) + { + temp = new File(tmpPath, "fm2sql" + generateSuffix(count++) + ".txt"); + } file = new FileOutputStream(temp); } catch (FileNotFoundException e1) { e1.printStackTrace(); } PrintStream stream = new PrintStream(file, true); + // write info for user to stdout + System.out.println(versionID); + System.out.println("Loading " + args[0] + "...."); + System.out.println("Log will be written to " + temp.getCanonicalPath()); + if (!debug) { System.setOut(stream); System.setErr(stream); } + System.out.println(versionID); + System.out.println("Using config file : " + args[0] + "...."); + StringBuffer sb = readXMLFile(args[0]); parseXMLConfig(sb); if (!(new File(args[0]).exists())) - System.exit(0); + { + System.exit(0); + } System.out.println("Finished!"); // convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null); } @@ -238,8 +264,9 @@ class Convert String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString(); query = (selects != null) ? selects.get(tbIndex).toString() : query; // if vectors[1].get(i) != null) - if (layout != "") + if (!layout.equals("")) { + query = addLayoutToQuery(names, query, tbIndex, layout); } @@ -417,7 +444,7 @@ class Convert public static void convert(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception { - + // TODO implement convert_temp mode!!! FM2SQL.ProgressDialog dialog = null; if (isGUI) @@ -462,7 +489,7 @@ class Convert String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString(); query = (selects != null) ? selects.get(tbIndex).toString() : "select * from " + bean.getQC() + names.get(tbIndex).toString() + bean.getQC(); - if (layout != "") + if (layout.intern() != "") { query = addLayoutToQuery(names, query, tbIndex, layout); @@ -502,8 +529,12 @@ class Convert ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex).toString(), query, names.get(tbIndex).toString()); destTableName = prop.destTableName; + if (mode == Convert.DataBase.CONVERT_TEMP_MODE) + { + destTableName += "_temp"; + } fieldNames = prop.fieldNames; - if (mode == Convert.DataBase.CONVERT_MODE) + if (mode == Convert.DataBase.CONVERT_MODE || mode == Convert.DataBase.CONVERT_TEMP_MODE) { if (tables.indexOf(destTableName) >= 0) @@ -511,7 +542,6 @@ class Convert stm.executeUpdate("drop table " + beanDest.getQC() + destTableName + beanDest.getQC()); tables.remove(destTableName); System.out.println("dropped table" + destTableName); - } if ((tables.indexOf(destTableName) < 0)) { @@ -525,7 +555,7 @@ class Convert command = new StringBuffer(50); command.append("CREATE TABLE "); 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("("); String type = null; @@ -577,7 +607,6 @@ class Convert command.append(") "); command.append(" values ( "); - // add a question marks for every field for (int i = 0; i < bean.getColumnNames().size() - 1; ++i) command.append("?,"); @@ -674,6 +703,7 @@ class Convert long startTime = System.currentTimeMillis(); bean.makeQuery(query, 0); + System.err.println("query for whole table done"); command = writeDatainDestTable(dialog, command, j, pstm, rowCount, delimiter); long endTime = System.currentTimeMillis(); System.out.println("Time for old convert elapsed " + (endTime - startTime)); @@ -681,10 +711,12 @@ class Convert } if (isGUI) resetGUI(dialog); + noError = true; } } catch (Exception e) { - System.out.println("Error while connecting to database " + e); + System.out.println("Error while connecting to database " + e.getMessage()); + noError = false; if (isGUI) { showExceptionDialog(dialog, command, e); @@ -694,6 +726,10 @@ class Convert e.printStackTrace(); } + } catch (Error e) + { + System.out.println(e); + e.printStackTrace(); } } @@ -981,9 +1017,19 @@ class Convert try { databases = getXMLConfig(sb); - + + // destination DataBase object + DataBase dbDest = ((DataBase) databases.lastElement()); + DBBean database = ((DataBase) databases.lastElement()).bean; databases.remove(databases.size() - 1); + + for (Iterator iterator = dbDest.preCommands.iterator(); iterator.hasNext();) + { + SQLCommand sqlCommand = (SQLCommand) iterator.next(); + sqlCommand.executeCommand(); + } + // databases.add(database); for (Iterator iter = databases.iterator(); iter.hasNext();) { @@ -994,6 +1040,24 @@ class Convert sqlCommand.executeCommand(); } int mode = db.mode; + if (mode == DataBase.CONVERT_TEMP_MODE) + { + convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids, + mode, db.delimiter); + if (noError) + { + System.out.println("no Error occured "); + // db.bean.setURL(database.url); + // db.bean.setUserAndPasswd(database.user,database.passwd); + // + // Convert.user = db.bean.user; + // Convert.passwd = db.bean.passwd; + // userDest = database.user; + // passwdDest = database.passwd; + // synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, + // db.ids, mode, db.delimiter, new Vector(db.htIndex.values())); + } + } if (mode == DataBase.CONVERT_MODE || mode == DataBase.APPEND_MODE) convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids, mode, db.delimiter); @@ -1024,6 +1088,11 @@ class Convert } } + for (Iterator iterator = dbDest.postCommands.iterator(); iterator.hasNext();) + { + SQLCommand sqlCommand = (SQLCommand) iterator.next(); + sqlCommand.executeCommand(); + } } catch (Exception e) { @@ -1143,6 +1212,9 @@ class Convert else if (modeString.equals("synchronize")) mode = DataBase.SYNCHRONIZE_MODE; + else if (modeString.equals("convert_temp")) + mode = DataBase.CONVERT_TEMP_MODE; + // if(node3!=null) // System.out.println(node3.name); @@ -1184,9 +1256,9 @@ class Convert id = ""; if (name.equals("")) throw new Error("parse error table tag attribute must not be empty"); - tables.add(name); - layouts.add(layout); - ids.add(id); + tables.add(name.intern()); + layouts.add(layout.intern()); + ids.add(id.intern()); String query = (node5 == null) ? "" : node5.getCharacters(); if (query.equals("")) System.err.println("Warning empty select tag or select tag missing !!"); @@ -1404,7 +1476,7 @@ class Convert Vector layouts; - Vector tables=new Vector(); + Vector tables = new Vector(); Vector ids; @@ -1430,6 +1502,8 @@ class Convert final static int SYNCHRONIZE_MODE = 5; + final static int CONVERT_TEMP_MODE = 6; + int mode = -1; public DataBase(DBBean bean, Vector tables, Vector layouts, Vector selects, Vector creates, @@ -1495,8 +1569,9 @@ class Convert { while (count < preCommands.size()) { - SQLCommand sqlcommand=(SQLCommand)preCommands.get(count); - buffr.write(" " + sqlcommand.command + "\n"); + SQLCommand sqlcommand = (SQLCommand) preCommands.get(count); + buffr.write(" " + sqlcommand.command + + "\n"); count++; } } @@ -1505,9 +1580,10 @@ class Convert count = 0; while (count < postCommands.size()) { - SQLCommand sqlcommand=(SQLCommand)postCommands.get(count); - - buffr.write(" " + sqlcommand.command + "\n"); + SQLCommand sqlcommand = (SQLCommand) postCommands.get(count); + + buffr.write(" " + sqlcommand.command + + "\n"); count++; } } @@ -3638,7 +3714,7 @@ class Convert // retrieve field_names from select statement // TODO problem with different fieldNames in create statement will // overwrite them - if (query.indexOf("*") < 0) + if (query.indexOf("*") < 0 && create.equals(""))// quick hack for hartmut { int selectEndIndex = query.indexOf("from"); StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ","); @@ -3677,7 +3753,7 @@ class Convert // retrieve field_names from select statement if (query.indexOf("*") < 0) { - int selectEndIndex = query.indexOf("from"); + int selectEndIndex = query.lastIndexOf("from"); StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ","); int numFields = tokenizer.countTokens(); fieldNames = new String[numFields]; @@ -3685,7 +3761,11 @@ class Convert while (tokenizer.hasMoreTokens()) { String fieldName = tokenizer.nextToken().trim(); - fieldNames[fieldIndex] = beanDest.getQC() + convertText(fieldName) + beanDest.getQC(); + String text = convertText(fieldName); + if (text.indexOf("\"") >= 0) + fieldNames[fieldIndex] = convertText(fieldName); + else + fieldNames[fieldIndex] = beanDest.getQC() + convertText(fieldName) + beanDest.getQC(); // System.out.println("field "+ fieldNames[fieldIndex]); fieldIndex++; } @@ -3830,4 +3910,17 @@ class Convert // ****** end Test ****** } + + public final static String generateSuffix(final int step) + { + String fileString = null; + if (step < 10) + fileString = "00" + step; + else if (step < 100) + fileString = "0" + step; + else + fileString = step + ""; + return fileString; + } + } \ No newline at end of file