Diff for /FM2SQL/src/Convert.java between versions 1.5 and 1.17

version 1.5, 2005/03/21 10:32:03 version 1.17, 2006/10/04 20:37:52
Line 29  import java.sql.ResultSet; Line 29  import java.sql.ResultSet;
 import java.sql.SQLException;  import java.sql.SQLException;
 import java.sql.Statement;  import java.sql.Statement;
 import java.sql.Types;  import java.sql.Types;
   import java.text.DateFormat;
 import java.text.ParseException;  import java.text.ParseException;
 import java.util.ArrayList;  import java.util.ArrayList;
   import java.util.Date;
 import java.util.Hashtable;  import java.util.Hashtable;
 import java.util.Iterator;  import java.util.Iterator;
 import java.util.List;  import java.util.List;
Line 47  import com.exploringxml.xml.Xparse; Line 49  import com.exploringxml.xml.Xparse;
   
 class Convert  class Convert
 {  {
     private static final String _TEMP = "_temp";
   
   /**    /**
    * Helper class for index creation     * Helper class for index creation
    *      * 
Line 93  class Convert Line 97  class Convert
   
     public void executeCommand() throws SQLException, Exception      public void executeCommand() throws SQLException, Exception
     {      {
         System.out.println("Executing command: \n");
         System.out.println(command);
   
       java.sql.Connection con = bean.getConnection();        java.sql.Connection con = bean.getConnection();
       Statement stm = con.createStatement();        Statement stm = con.createStatement();
       stm.execute(command);        stm.execute(command);
         stm.close();
     }      }
   }    }
   
Line 129  class Convert Line 137  class Convert
   
   static Vector postSQLCommands = new Vector();    static Vector postSQLCommands = new Vector();
   
     static final String versionID = new String("FM2SQL Version 0.9.10b\n");
   
     private static boolean noError = false;
   
   public static void main(String args[]) throws IOException    public static void main(String args[]) throws IOException
   {    {
       //    DateFormat d = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,
       //        Locale.GERMANY);
       //    System.out.println(convertText("rogoistSuper" + "_" + d.format(new Date()).replace(':', '_')));
   
     /*      /*
      * try { //byte[] b = "�".getBytes("UTF-8"); //       * try { //byte[] b = "�".getBytes("UTF-8"); //
      * System.out.println("QueryString " +b[0]+" "+b[1]+(new       * System.out.println("QueryString " +b[0]+" "+b[1]+(new
Line 138  class Convert Line 154  class Convert
      * //System.out.println(new String(b,"UTF-8")); } catch       * //System.out.println(new String(b,"UTF-8")); } catch
      * (UnsupportedEncodingException e) { e.printStackTrace(); }       * (UnsupportedEncodingException e) { e.printStackTrace(); }
      */       */
       File tmpPath = new File(System.getProperty("java.io.tmpdir"));
     isGUI = false;      isGUI = false;
     FileOutputStream file = null;      FileOutputStream file = null;
     if (args.length != 1)      if (args.length != 1)
     {      {
         System.out.println(versionID);
       System.out.println("Usage: java Convert <xml config file>");        System.out.println("Usage: java Convert <xml config file>");
       System.exit(-1);        System.exit(-1);
     }      }
       File temp = null;
     try      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);        file = new FileOutputStream(temp);
     } catch (FileNotFoundException e1)      } catch (FileNotFoundException e1)
     {      {
       e1.printStackTrace();        e1.printStackTrace();
     }      }
     PrintStream stream = new PrintStream(file, true);      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)      if (!debug)
     {      {
       System.setOut(stream);        System.setOut(stream);
       System.setErr(stream);        System.setErr(stream);
     }      }
       System.out.println(versionID);
       System.out.println("Using config file : " + args[0] + "....");
   
     StringBuffer sb = readXMLFile(args[0]);      StringBuffer sb = readXMLFile(args[0]);
     parseXMLConfig(sb);      parseXMLConfig(sb);
     if (!(new File(args[0]).exists()))      if (!(new File(args[0]).exists()))
       System.exit(0);      {
   
         System.exit(0);
       }
     System.out.println("Finished!");      System.out.println("Finished!");
     // convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null);      // convert("jdbc:fmpro:http://141.14.237.74:8050","jdbc:postgresql://erebos/test",null,null);
   }    }
Line 238  class Convert Line 272  class Convert
           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();            String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
           query = (selects != null) ? selects.get(tbIndex).toString() : query;            query = (selects != null) ? selects.get(tbIndex).toString() : query;
           // if vectors[1].get(i) != null)            // if vectors[1].get(i) != null)
           if (layout != "")            if (!layout.equals(""))
           {            {
   
             query = addLayoutToQuery(names, query, tbIndex, layout);              query = addLayoutToQuery(names, query, tbIndex, layout);
   
           }            }
Line 417  class Convert Line 452  class Convert
   public static void convert(String source, String destination, Vector names, Vector layouts,    public static void convert(String source, String destination, Vector names, Vector layouts,
       Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception        Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception
   {    {
       // TODO implement convert_temp mode!!! 
     FM2SQL.ProgressDialog dialog = null;      FM2SQL.ProgressDialog dialog = null;
   
     if (isGUI)      if (isGUI)
Line 462  class Convert Line 497  class Convert
           String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();            String layout = (layouts.isEmpty()) ? "" : layouts.get(tbIndex).toString();
           query = (selects != null) ? selects.get(tbIndex).toString() : "select * from "            query = (selects != null) ? selects.get(tbIndex).toString() : "select * from "
               + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();                + bean.getQC() + names.get(tbIndex).toString() + bean.getQC();
           if (layout != "")            if (layout.intern() != "")
           {            {
             query = addLayoutToQuery(names, query, tbIndex, layout);              query = addLayoutToQuery(names, query, tbIndex, layout);
   
Line 502  class Convert Line 537  class Convert
         ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex).toString(),          ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex).toString(),
             query, names.get(tbIndex).toString());              query, names.get(tbIndex).toString());
         destTableName = prop.destTableName;          destTableName = prop.destTableName;
         fieldNames = prop.fieldNames;          if (mode == Convert.DataBase.CONVERT_TEMP_MODE)
         if (mode == Convert.DataBase.CONVERT_MODE)          {
             String tableNameSave=destTableName;
             destTableName += _TEMP;
             if(creates.get(tbIndex)!="")
         {          {
              System.out.println("Changed tablename of create statement to "+destTableName);
               StringBuffer buffer=new StringBuffer(creates.get(tbIndex).toString());
              int startIndex = buffer.indexOf(tableNameSave);
              buffer.replace(startIndex,startIndex+tableNameSave.length(),destTableName);
               creates.set(tbIndex,buffer.toString());
             }
           }
           System.out.println("destTableName is now "+destTableName);
   
           fieldNames = prop.fieldNames;
           if (mode == Convert.DataBase.CONVERT_MODE || mode == Convert.DataBase.CONVERT_TEMP_MODE)
           {
           if (tables.indexOf(destTableName) >= 0)            if (tables.indexOf(destTableName) >= 0)
           {            {
             stm.executeUpdate("drop table " + beanDest.getQC() + destTableName + beanDest.getQC());              stm.executeUpdate("drop table " + beanDest.getQC() + destTableName + beanDest.getQC());
             tables.remove(destTableName);              tables.remove(destTableName);
             System.out.println("dropped table" + destTableName);              System.out.println("dropped table" + destTableName);
   
           }            }
           if ((tables.indexOf(destTableName) < 0))            if ((tables.indexOf(destTableName) < 0))
           {            {
Line 525  class Convert Line 573  class Convert
               command = new StringBuffer(50);                command = new StringBuffer(50);
               command.append("CREATE TABLE ");                command.append("CREATE TABLE ");
               command.append(beanDest.getQC());                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(beanDest.getQC());
               command.append("(");                command.append("(");
               String type = null;                String type = null;
Line 550  class Convert Line 598  class Convert
               command.append(" )");                command.append(" )");
   
             } else              } else
               command = new StringBuffer().append(creates.get(tbIndex).toString().toLowerCase());              {
                 command = new StringBuffer().append(creates.get(tbIndex).toString());
               }
                   System.out.println("create statement: "+command.toString());
             stm.executeUpdate(command.toString());              stm.executeUpdate(command.toString());
   
           }            }
Line 577  class Convert Line 628  class Convert
         command.append(") ");          command.append(") ");
   
         command.append(" values ( ");          command.append(" values ( ");
   
         // add a question marks for every field          // add a question marks for every field
         for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)          for (int i = 0; i < bean.getColumnNames().size() - 1; ++i)
           command.append("?,");            command.append("?,");
Line 619  class Convert Line 669  class Convert
             for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)              for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
             {              {
               System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());                System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
               if (query.indexOf("where") > 0)                if (query.toLowerCase().indexOf("where") > 0)
                 tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID                  tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID
                     + "<='" + vec.get(k + deltaID) + "'";                      + "<='" + vec.get(k + deltaID) + "'";
               else                else
Line 645  class Convert Line 695  class Convert
               System.out.println(" last intervall from " + vec.get(endIndex) + " "                System.out.println(" last intervall from " + vec.get(endIndex) + " "
                   + vec.lastElement());                    + vec.lastElement());
   
               if (query.indexOf("where") > 0)                if (query.toLowerCase().indexOf("where") > 0)
                 tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and "                  tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and "
                     + tempID + "<='" + vec.lastElement() + "'";                      + tempID + "<='" + vec.lastElement() + "'";
               else                else
Line 660  class Convert Line 710  class Convert
               command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount, delimiter);                command = writeDatainDestTable(dialog, command, endIndex, pstm, rowCount, delimiter);
             }              }
             // prepare new query for next chunk              // prepare new query for next chunk
             if (query.indexOf("where") > 0)              if (query.toLowerCase().indexOf("where") > 0)
               tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";                tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
             else              else
               tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";                tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
Line 674  class Convert Line 724  class Convert
           long startTime = System.currentTimeMillis();            long startTime = System.currentTimeMillis();
   
           bean.makeQuery(query, 0);            bean.makeQuery(query, 0);
             System.err.println("query for whole table done");
           command = writeDatainDestTable(dialog, command, j, pstm, rowCount, delimiter);            command = writeDatainDestTable(dialog, command, j, pstm, rowCount, delimiter);
           long endTime = System.currentTimeMillis();            long endTime = System.currentTimeMillis();
           System.out.println("Time for old convert elapsed " + (endTime - startTime));            System.out.println("Time for old convert elapsed " + (endTime - startTime));
Line 681  class Convert Line 732  class Convert
         }          }
         if (isGUI)          if (isGUI)
           resetGUI(dialog);            resetGUI(dialog);
           noError = true;
           if (mode == Convert.DataBase.CONVERT_TEMP_MODE)
           {
             String originalName = destTableName.split(_TEMP)[0];
             // TODO 
             if (beanDest.getTableNames().contains(originalName))
             {
               DateFormat d = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
               d.setTimeZone(java.util.TimeZone.getTimeZone("CET"));
             
               String date = d.format(new Date());
               date = date.replace(':', '_');
               String newName = addQuotes(beanDest, convertText(originalName + "_" + date));
               // rename table to newName
               renameTable(beanDest, addQuotes(beanDest, originalName), newName);
             }
             // rename Table_temp to originalName
             renameTable(beanDest, addQuotes(beanDest, destTableName), addQuotes(beanDest,
                 originalName));
           }
       }        }
     } catch (Exception e)      } 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)        if (isGUI)
       {        {
         showExceptionDialog(dialog, command, e);          showExceptionDialog(dialog, command, e);
Line 694  class Convert Line 766  class Convert
         e.printStackTrace();          e.printStackTrace();
   
       }        }
       } catch (Error e)
       {
         noError = false;
   
         System.out.println(e);
         e.printStackTrace();
     }      }
   
   }    }
   
   /**    /**
      *
      * @param beanDest2
      * @param originalName
      * @return
      */
     private static String addQuotes(DBBean bean, String originalName)
     {
       return bean.getQC() + originalName + bean.getQC();
     }
   
     /**
    * @param names     * @param names
    * @param dialog     * @param dialog
    * @param tbIndex     * @param tbIndex
Line 750  class Convert Line 839  class Convert
       throws Exception, SQLException        throws Exception, SQLException
   {    {
     Vector row;      Vector row;
     while ((row = bean.getNextRow()) != null)      while ((row = bean.getNextRow()) != null) {
     {  
       j++;        j++;
       Object obj = null;        Object obj = null;
       for (int k = 0; k < row.size(); ++k)              for (int k = 0; k < row.size(); ++k) {
       {  
         obj = row.get(k);          obj = row.get(k);
   
         if (obj instanceof ArrayList)          if (obj instanceof ArrayList)
           obj = formatFileMakerArray((List) obj, delimiter);            obj = formatFileMakerArray((List) obj, delimiter);
   
         String str = (obj == null) ? "NULL" : obj.toString();          String str = (obj == null) ? "NULL" : obj.toString();
         if (obj instanceof Double)                  if (obj instanceof Double) {
         {  
           pstm.setDouble(k + 1, ((Double) obj).doubleValue());            pstm.setDouble(k + 1, ((Double) obj).doubleValue());
         } else if (!str.equals("NULL"))          } else if (!str.equals("NULL"))
           pstm.setString(k + 1, str);            pstm.setString(k + 1, str);
Line 772  class Convert Line 858  class Convert
       }        }
       pstm.execute();        pstm.execute();
       if (isGUI)        if (isGUI)
         dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));                  dialog.progress
                           .setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0));
       command = null;        command = null;
     } // to while loop      } // to while loop
     return command;      return command;
Line 982  class Convert Line 1069  class Convert
     {      {
       databases = getXMLConfig(sb);        databases = getXMLConfig(sb);
               
         // destination DataBase object
         DataBase dbDest = ((DataBase) databases.lastElement());
   
       DBBean database = ((DataBase) databases.lastElement()).bean;        DBBean database = ((DataBase) databases.lastElement()).bean;
       databases.remove(databases.size() - 1);        databases.remove(databases.size() - 1);
   
         for (Iterator iterator = dbDest.preCommands.iterator(); iterator.hasNext();)
         {
           SQLCommand sqlCommand = (SQLCommand) iterator.next();
           sqlCommand.executeCommand();
         }
   
       // databases.add(database);        // databases.add(database);
       for (Iterator iter = databases.iterator(); iter.hasNext();)        for (Iterator iter = databases.iterator(); iter.hasNext();)
       {        {
Line 994  class Convert Line 1091  class Convert
           sqlCommand.executeCommand();            sqlCommand.executeCommand();
         }          }
         int mode = db.mode;          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)          if (mode == DataBase.CONVERT_MODE || mode == DataBase.APPEND_MODE)
           convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids,            convertBatch(db.bean, database, db.tables, db.layouts, db.selects, db.creates, db.ids,
               mode, db.delimiter);                mode, db.delimiter);
Line 1015  class Convert Line 1130  class Convert
           passwdDest = database.passwd;            passwdDest = database.passwd;
   
           synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates,            synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates,
               db.ids, mode, db.delimiter, new Vector(db.htIndex.values()));                db.ids, mode, db.delimiter, new Vector(db.htIndex.values()), db.destIDs);
         }          }
         for (Iterator iterator = db.postCommands.iterator(); iterator.hasNext();)          for (Iterator iterator = db.postCommands.iterator(); iterator.hasNext();)
         {          {
Line 1024  class Convert Line 1139  class Convert
         }          }
   
       }        }
         for (Iterator iterator = dbDest.postCommands.iterator(); iterator.hasNext();)
         {
           SQLCommand sqlCommand = (SQLCommand) iterator.next();
           sqlCommand.executeCommand();
         }
   
     } catch (Exception e)      } catch (Exception e)
     {      {
Line 1037  class Convert Line 1157  class Convert
     }      }
   }    }
   
     /**
      *
      * @param database
      * @param db 
      * @throws Exception 
      * @throws SQLException 
      */
     private static void renameTable(DBBean database, String sourceTable, String destTable)
         throws SQLException, Exception
     {
       String command = "alter table " + sourceTable + " rename " + " to " + destTable;
       SQLCommand sqlCommand = new SQLCommand(database, command);
       sqlCommand.executeCommand();
     }
   
   public static Vector getXMLConfig(StringBuffer sb)    public static Vector getXMLConfig(StringBuffer sb)
   {    {
   
Line 1052  class Convert Line 1187  class Convert
     Vector selects = new Vector();      Vector selects = new Vector();
     Vector creates = new Vector();      Vector creates = new Vector();
     Vector ids = new Vector();      Vector ids = new Vector();
       Vector destIDs = new Vector();
   
     Vector preSQLCommands = new Vector();      Vector preSQLCommands = new Vector();
     Vector postSQLCommands = new Vector();      Vector postSQLCommands = new Vector();
   
Line 1074  class Convert Line 1211  class Convert
         selects = new Vector();          selects = new Vector();
         creates = new Vector();          creates = new Vector();
         ids = new Vector();          ids = new Vector();
           destIDs = new Vector();
   
         preSQLCommands = new Vector();          preSQLCommands = new Vector();
         postSQLCommands = new Vector();          postSQLCommands = new Vector();
   
Line 1143  class Convert Line 1282  class Convert
   
         else if (modeString.equals("synchronize"))          else if (modeString.equals("synchronize"))
           mode = DataBase.SYNCHRONIZE_MODE;            mode = DataBase.SYNCHRONIZE_MODE;
           else if (modeString.equals("convert_temp"))
             mode = DataBase.CONVERT_TEMP_MODE;
   
           System.out.println("mode ist "+mode);
         // if(node3!=null)          // if(node3!=null)
         // System.out.println(node3.name);          // System.out.println(node3.name);
   
Line 1174  class Convert Line 1317  class Convert
           String name = (String) node4.attributes.get("name");            String name = (String) node4.attributes.get("name");
           String layout = (String) node4.attributes.get("layout");            String layout = (String) node4.attributes.get("layout");
           String id = (String) node4.attributes.get("id");            String id = (String) node4.attributes.get("id");
             String destID = (String) node4.attributes.get("targetID");
           System.out.println("id was " + id);            System.out.println("id was " + id);
             System.out.println("targetID was " + destID);
   
           if (name == null)            if (name == null)
             throw new Error("parse error required table tag attribute name missing");              throw new Error("parse error required table tag attribute name missing");
Line 1182  class Convert Line 1327  class Convert
             layout = "";              layout = "";
           if (id == null)            if (id == null)
             id = "";              id = "";
             if (destID == null)
               destID = id ;
   
           if (name.equals(""))            if (name.equals(""))
             throw new Error("parse error table tag attribute must not be empty");              throw new Error("parse error table tag attribute must not be empty");
           tables.add(name);            tables.add(name.intern());
           layouts.add(layout);            layouts.add(layout.intern());
           ids.add(id);            ids.add(id.intern());
             destIDs.add(destID.intern());
           String query = (node5 == null) ? "" : node5.getCharacters();            String query = (node5 == null) ? "" : node5.getCharacters();
           if (query.equals(""))            if (query.equals(""))
             System.err.println("Warning empty select tag or  select tag missing !!");              System.err.println("Warning empty select tag or  select tag missing !!");
Line 1203  class Convert Line 1352  class Convert
         dataBase.delimiter = delimiter;          dataBase.delimiter = delimiter;
         dataBase.preCommands = new Vector(preSQLCommands);          dataBase.preCommands = new Vector(preSQLCommands);
         dataBase.postCommands = new Vector(postSQLCommands);          dataBase.postCommands = new Vector(postSQLCommands);
           dataBase.destIDs = destIDs;
         databases.add(dataBase);          databases.add(dataBase);
       }        }
       DBBean database = new DBBean();        DBBean database = new DBBean();
Line 1396  class Convert Line 1546  class Convert
    */     */
   public static class DataBase    public static class DataBase
   {    {
   
     DBBean bean;      DBBean bean;
   
     Vector creates;      Vector creates;
Line 1407  class Convert Line 1558  class Convert
     Vector tables=new Vector();      Vector tables=new Vector();
   
     Vector ids;      Vector ids;
       Vector destIDs;
   
     String delimiter = "//";      String delimiter = "//";
   
Line 1430  class Convert Line 1582  class Convert
   
     final static int SYNCHRONIZE_MODE = 5;      final static int SYNCHRONIZE_MODE = 5;
   
       final static int CONVERT_TEMP_MODE = 6;
   
     int mode = -1;      int mode = -1;
   
     public DataBase(DBBean bean, Vector tables, Vector layouts, Vector selects, Vector creates,      public DataBase(DBBean bean, Vector tables, Vector layouts, Vector selects, Vector creates,
Line 1471  class Convert Line 1625  class Convert
       buffr.write("      <password>" + bean.passwd + "</password>\n");        buffr.write("      <password>" + bean.passwd + "</password>\n");
       buffr.write("      <delimiter>" + delimiter + "</delimiter>\n");        buffr.write("      <delimiter>" + delimiter + "</delimiter>\n");
       String modeString = "";        String modeString = "";
       if (mode == CONVERT_MODE)        if (mode == CONVERT_TEMP_MODE)
           modeString = "convert_temp";
         else if (mode == CONVERT_MODE)
         modeString = "convert";          modeString = "convert";
       else if (mode == APPEND_MODE)        else if (mode == APPEND_MODE)
         modeString = "append";          modeString = "append";
Line 1496  class Convert Line 1652  class Convert
           while (count < preCommands.size())            while (count < preCommands.size())
           {            {
             SQLCommand sqlcommand=(SQLCommand)preCommands.get(count);              SQLCommand sqlcommand=(SQLCommand)preCommands.get(count);
             buffr.write("        <sqlcommand execute=\"before\">" + sqlcommand.command + "</sqlcommand>\n");              buffr.write("        <sqlcommand execute=\"before\">" + sqlcommand.command
                   + "</sqlcommand>\n");
             count++;              count++;
           }            }
         }          }
Line 1507  class Convert Line 1664  class Convert
           {            {
             SQLCommand sqlcommand=(SQLCommand)postCommands.get(count);              SQLCommand sqlcommand=(SQLCommand)postCommands.get(count);
                           
             buffr.write("        <sqlcommand execute=\"after\">" + sqlcommand.command + "</sqlcommand>\n");              buffr.write("        <sqlcommand execute=\"after\">" + sqlcommand.command
                   + "</sqlcommand>\n");
             count++;              count++;
           }            }
         }          }
Line 1747  class Convert Line 1905  class Convert
               command = null;                command = null;
             }              }
             // prepare new query for next chunk              // prepare new query for next chunk
             if (query.indexOf("where") > 0)              if (query.toLowerCase().indexOf("where") > 0)
               tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";                tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
             else              else
               tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";                tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
Line 1772  class Convert Line 1930  class Convert
   
   /**    /**
    * synchronize method based on delete method code     * synchronize method based on delete method code
    *   
    * @param source     * @param source
    * @param destination     * @param destination
    * @param names     * @param names
Line 1781  class Convert Line 1938  class Convert
    * @param creates     * @param creates
    * @param ids     * @param ids
    * @param mode     * @param mode
      * @param destIDs TODO
      * 
    * @throws Exception     * @throws Exception
    */     */
   // TODO implement append,update and delete in one method    // TODO implement append,update and delete in one method
   // TODO using id based algorithm    // TODO using id based algorithm
   public static void synchronize(String source, String destination, Vector names, Vector layouts,    public static void synchronize(String source, String destination, Vector names, Vector layouts,
       Vector selects, Vector creates, Vector ids, int mode, String delimiter, Vector indexList)        Vector selects, Vector creates, Vector ids, int mode, String delimiter, Vector indexList, java.util.Vector destIDs)
       throws Exception        throws Exception
   {    {
     System.out.println(" bin in synchronize!!!");      System.out.println(" bin in synchronize!!!");
Line 1817  class Convert Line 1976  class Convert
         java.util.TreeSet myIdsDest = new TreeSet();          java.util.TreeSet myIdsDest = new TreeSet();
         int deltaID = 1;          int deltaID = 1;
         String idField = "";          String idField = "";
           String destIDField = "";
         String destTableName = "";          String destTableName = "";
   
         try          try
Line 1837  class Convert Line 1997  class Convert
           bean.getConnection();            bean.getConnection();
           bean.makeQuery(query, 50);            bean.makeQuery(query, 50);
           idField = ids.get(tbIndex).toString();            idField = ids.get(tbIndex).toString();
             destIDField = (destIDs!=null) ? destIDs.get(tbIndex).toString():convertText(idField); 
         } catch (Exception e)          } catch (Exception e)
         {          {
           System.out.println("Warning exception occured \n " + e);            System.out.println("Warning exception occured \n " + e);
Line 1877  class Convert Line 2037  class Convert
         // remove extra query parts          // remove extra query parts
         // destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);          // destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1);
         System.out.println("new Query " + tempQueryDest);          System.out.println("new Query " + tempQueryDest);
         System.out.println("idfield " + idField + " " + ids.get(tbIndex).toString());          System.out.println("idfield " + idField + " " + ids.get(tbIndex).toString()+" targetID is "+destIDField);
         if (!idField.equals(""))          if (!idField.equals(""))
         {          {
           long startTime = System.currentTimeMillis();            long startTime = System.currentTimeMillis();
Line 1894  class Convert Line 2054  class Convert
   
             myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),              myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex),
                 tempQuery, 0);                  tempQuery, 0);
             myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest, 0);              myIdsDest = beanDest.getIDVector(destIDField, destTableName, tempQueryDest, 0);
             // System.out.println("status of remove              // System.out.println("status of remove
             // "+myIds.remove("b015892"));              // "+myIds.remove("b015892"));
             System.out.println("ids found for " + idField + " " + !myIds.isEmpty());              System.out.println("ids found for " + idField + " " + !myIds.isEmpty());
Line 1907  class Convert Line 2067  class Convert
             Vector deleted = new Vector(vec);              Vector deleted = new Vector(vec);
   
             TreeSet linesToAppend = new TreeSet(vec);              TreeSet linesToAppend = new TreeSet(vec);
             linesToAppend.addAll(vec);   //           linesToAppend.addAll(vec);
             linesToDelete = new TreeSet(vecDest);              linesToDelete = new TreeSet(vecDest);
             // remove all lines that are already in dest database              // remove all lines that are already in dest database
             linesToAppend.removeAll(vecDest);              linesToAppend.removeAll(vecDest);
Line 1941  class Convert Line 2101  class Convert
             for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)              for (int k = 0; k < vec.size() - deltaID; k = k + deltaID)
             {              {
               System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());                System.out.println(vec.get(k) + " " + vec.get(k + deltaID) + " " + vec.lastElement());
               if (query.indexOf("where") > 0)                if (query.toLowerCase().indexOf("where") > 0)
                 tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID                  tempQuery = query + " and " + tempID + ">='" + vec.get(k) + "' and " + tempID
                     + "<='" + vec.get(k + deltaID) + "'";                      + "<='" + vec.get(k + deltaID) + "'";
               else                else
Line 1971  class Convert Line 2131  class Convert
               System.out.println(" last intervall from " + vec.get(endIndex) + " "                System.out.println(" last intervall from " + vec.get(endIndex) + " "
                   + vec.lastElement());                    + vec.lastElement());
   
               if (query.indexOf("where") > 0)                if (query.toLowerCase().indexOf("where") > 0)
                 tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and "                  tempQuery = query + " and " + tempID + ">='" + vec.get(endIndex) + "' and "
                     + tempID + "<='" + vec.lastElement() + "'";                      + tempID + "<='" + vec.lastElement() + "'";
               else                else
Line 1990  class Convert Line 2150  class Convert
               // numIntervalls) + " " + myIdsDest.size());                // numIntervalls) + " " + myIdsDest.size());
             }              }
             // prepare new query for next chunk              // prepare new query for next chunk
             if (query.indexOf("where") > 0)              if (query.toLowerCase().indexOf("where") > 0)
               tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";                tempQuery = query + " and " + tempID + ">'" + vec.lastElement() + "'";
             else              else
               tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";                tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'";
Line 2056  class Convert Line 2216  class Convert
           // CREATE UNIQUE INDEX title_idx ON films (title);            // CREATE UNIQUE INDEX title_idx ON films (title);
           for (Iterator iter = linesToDelete.iterator(); iter.hasNext();)            for (Iterator iter = linesToDelete.iterator(); iter.hasNext();)
           {            {
             String id = (String) iter.next();              String id = iter.next().toString();
             delPSt.setString(1, id);              delPSt.setString(1, id);
             delPSt.execute();              delPSt.execute();
   
Line 2104  class Convert Line 2264  class Convert
       dialog.title.setText("Retrieving new data");        dialog.title.setText("Retrieving new data");
     }      }
   
     Vector[] vectors = bean.getQueryData(tempQuery, deltaID);      Vector[] vectors = bean.getQueryData(tempQuery, 0);
     int count = 0, size = vectors[0].size();      int count = 0, size = vectors[0].size();
     int idIndex = vectors[1].indexOf(idField);      int idIndex = vectors[1].indexOf(idField);
     // System.out.println(idIndex + " " + vectors[1] + " " + idField);      // System.out.println(idIndex + " " + vectors[1] + " " + idField);
Line 2117  class Convert Line 2277  class Convert
       Vector line = (Vector) iter.next();        Vector line = (Vector) iter.next();
       Object lineIDIndex = line.get(idIndex);        Object lineIDIndex = line.get(idIndex);
       if (linesToAppend.contains(lineIDIndex))        if (linesToAppend.contains(lineIDIndex))
         System.out.println("line " + linesToAppend.contains(line.get(idIndex)) + " " + lineIDIndex);          System.out.println("line " + linesToAppend.contains(lineIDIndex) + " " + lineIDIndex);
       if (linesToAppend.contains(lineIDIndex))        if (linesToAppend.contains(lineIDIndex))
       {        {
         for (int l = 0; l < line.size(); ++l)          for (int l = 0; l < line.size(); ++l)
Line 2147  class Convert Line 2307  class Convert
         }          }
         updPst.setString(line.size() + 1, line.get(idIndex).toString());          updPst.setString(line.size() + 1, line.get(idIndex).toString());
         // updPst.addBatch();          // updPst.addBatch();
         // updPst.execute();         // System.out.println("updating "+line.get(idIndex).toString()+" "+line);
           updPst.execute();
       }        }
       if (dialog != null)        if (dialog != null)
       {        {
Line 3638  class Convert Line 3799  class Convert
       // retrieve field_names from select statement        // retrieve field_names from select statement
       // TODO problem with different fieldNames in create statement will        // TODO problem with different fieldNames in create statement will
       // overwrite them        // overwrite them
       if (query.indexOf("*") < 0)        if (query.indexOf("*") < 0 && create.equals(""))// quick hack for hartmut
       {        {
         int selectEndIndex = query.indexOf("from");          int selectEndIndex = query.indexOf("from");
         StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");          StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
Line 3677  class Convert Line 3838  class Convert
       // retrieve field_names from select statement        // retrieve field_names from select statement
       if (query.indexOf("*") < 0)        if (query.indexOf("*") < 0)
       {        {
         int selectEndIndex = query.indexOf("from");          int selectEndIndex = query.lastIndexOf("from");
         StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");          StringTokenizer tokenizer = new StringTokenizer(query.substring(6, selectEndIndex), ",");
         int numFields = tokenizer.countTokens();          int numFields = tokenizer.countTokens();
         fieldNames = new String[numFields];          fieldNames = new String[numFields];
Line 3685  class Convert Line 3846  class Convert
         while (tokenizer.hasMoreTokens())          while (tokenizer.hasMoreTokens())
         {          {
           String fieldName = tokenizer.nextToken().trim();            String fieldName = tokenizer.nextToken().trim();
             String text = convertText(fieldName);
             if (text.indexOf("\"") >= 0)
               fieldNames[fieldIndex] = convertText(fieldName);
             else
           fieldNames[fieldIndex] = beanDest.getQC() + convertText(fieldName) + beanDest.getQC();            fieldNames[fieldIndex] = beanDest.getQC() + convertText(fieldName) + beanDest.getQC();
           // System.out.println("field "+ fieldNames[fieldIndex]);            // System.out.println("field "+ fieldNames[fieldIndex]);
           fieldIndex++;            fieldIndex++;
Line 3830  class Convert Line 3995  class Convert
     // ****** end Test ******      // ****** 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;
     }
   
 }  }
   

Removed from v.1.5  
changed lines
  Added in v.1.17


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>