version 1.46, 2004/03/12 11:48:57
|
version 1.53, 2004/04/05 10:14:01
|
Line 1
|
Line 1
|
|
/* |
|
* 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.util.*; |
import java.sql.*; |
import java.sql.*; |
import java.awt.Cursor; |
import java.awt.Cursor; |
Line 249 class Convert
|
Line 263 class Convert
|
FM2SQL.ProgressDialog dialog = null; |
FM2SQL.ProgressDialog dialog = null; |
if (FM2SQL.fmInstance != null) |
if (FM2SQL.fmInstance != null) |
{ |
{ |
dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance); |
dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance,bean); |
dialog.setTitle("Conversion running ..."); |
dialog.setTitle("Conversion running ..."); |
dialog.title.setText("Getting table data ..."); |
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.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2); |
Line 456 class Convert
|
Line 470 class Convert
|
|
|
if (FM2SQL.fmInstance != null) |
if (FM2SQL.fmInstance != null) |
{ |
{ |
dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance); |
dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance,bean); |
dialog.setTitle("Conversion running ..."); |
dialog.setTitle("Conversion running ..."); |
dialog.title.setText("Getting table data ..."); |
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.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2); |
Line 1150 class Convert
|
Line 1164 class Convert
|
mode = DataBase.APPEND_MODE; |
mode = DataBase.APPEND_MODE; |
else if (modeString.equals("update")) |
else if (modeString.equals("update")) |
mode = DataBase.UPDATE_MODE; |
mode = DataBase.UPDATE_MODE; |
|
else if (modeString.equals("delete")) |
|
mode = DataBase.DELETE_MODE; |
|
|
// if(node3!=null) |
// if(node3!=null) |
// System.out.println(node3.name); |
// System.out.println(node3.name); |
|
|
Line 1321 class Convert
|
Line 1338 class Convert
|
mode = DataBase.APPEND_MODE; |
mode = DataBase.APPEND_MODE; |
else if (modeString.equals("update")) |
else if (modeString.equals("update")) |
mode = DataBase.UPDATE_MODE; |
mode = DataBase.UPDATE_MODE; |
|
else if (modeString.equals("delete")) |
|
mode = DataBase.DELETE_MODE; |
|
|
// if(node3!=null) |
// if(node3!=null) |
// System.out.println(node3.name); |
// System.out.println(node3.name); |
|
|
Line 1517 class Convert
|
Line 1537 class Convert
|
modeString = "append"; |
modeString = "append"; |
else if (mode == UPDATE_MODE) |
else if (mode == UPDATE_MODE) |
modeString = "update"; |
modeString = "update"; |
|
else if (mode == DELETE_MODE) |
|
modeString = "delete"; |
|
|
buffr.write(" <mode>" + modeString + "</mode>\n"); |
buffr.write(" <mode>" + modeString + "</mode>\n"); |
int index = 0; |
int index = 0; |
Line 1582 class Convert
|
Line 1604 class Convert
|
FM2SQL.ProgressDialog dialog = null; |
FM2SQL.ProgressDialog dialog = null; |
if (FM2SQL.fmInstance != null) |
if (FM2SQL.fmInstance != null) |
{ |
{ |
dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance); |
dialog = new FM2SQL.ProgressDialog(FM2SQL.fmInstance,bean); |
dialog.setTitle("Conversion running ..."); |
dialog.setTitle("Conversion running ..."); |
dialog.title.setText("Getting table data ..."); |
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.setLocation(FM2SQL.fmInstance.getLocationOnScreen().x + (FM2SQL.fmInstance.getWidth() - 400) / 2, FM2SQL.fmInstance.getLocationOnScreen().y + (FM2SQL.fmInstance.getHeight() - 250) / 2); |
Line 1663 class Convert
|
Line 1685 class Convert
|
Vector vecDest = null; |
Vector vecDest = null; |
// tempo |
// tempo |
beanDest.setConnection(destination); |
beanDest.setConnection(destination); |
|
|
int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query); |
int rowCount = (idField != "") ? myIds.size() : bean.getRowCount(query); |
String tempID = bean.getQC() + idField + bean.getQC(); |
String tempID = bean.getQC() + idField + bean.getQC(); |
String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC(); |
String tempIDdest = beanDest.getQC() + convertText(idField) + beanDest.getQC(); |
Line 1672 class Convert
|
Line 1693 class Convert
|
String tempQuery = query; |
String tempQuery = query; |
String destQuery =query.replaceAll(names.get(tbIndex).toString(),destTableName); |
String destQuery =query.replaceAll(names.get(tbIndex).toString(),destTableName); |
String tempQueryDest = destQuery; |
String tempQueryDest = destQuery; |
|
// remove extra query parts destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1); |
System.out.println("new Query "+tempQueryDest); |
System.out.println("new Query "+tempQueryDest); |
if (!idField.equals("")) |
if (!idField.equals("")) |
{ |
{ |
Line 1693 class Convert
|
Line 1715 class Convert
|
rowCount = vec.size(); |
rowCount = vec.size(); |
// Deletion will work this way |
// Deletion will work this way |
Vector deleted=new Vector(vec); |
Vector deleted=new Vector(vec); |
deleted.remove(0); |
|
Vector linesToDelete=new Vector(vecDest); |
Vector linesToDelete=new Vector(vecDest); |
// remove all lines that should not be deleted |
// remove all lines that should not be deleted |
linesToDelete.removeAll(deleted); |
linesToDelete.removeAll(deleted); |
Line 1701 class Convert
|
Line 1722 class Convert
|
/// @TODO complete delete task remove query show lines to be deleted let user choose if he wants that |
/// @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()); |
System.out.println("number of lines to be deleted "+linesToDelete.size()); |
deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls); |
deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls); |
if (vec.size() <= numIntervalls) |
|
{ |
|
endIndex = 0; |
|
deltaID = vec.size(); |
|
} |
|
for (int k = 0; k < vec.size() - deltaID; k = k + deltaID) |
|
{ |
|
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) + "'"; |
|
else |
|
tempQuery = query + " where " + tempID + ">='" + vec.get(k) + "' and " + tempID + "<='" + vec.get(k + deltaID) + "'"; |
|
if (destQuery.indexOf("where") > 0) |
|
tempQueryDest = destQuery + " and " + tempIDdest + ">='" + vec.get(k) + "' and " + tempIDdest + "<='" + vec.get(k + deltaID) + "'"; |
|
else |
|
tempQueryDest = destQuery + " where " + tempIDdest + ">='" + vecDest.get(k) + "' and " + tempIDdest + "<='" + vecDest.get(k + deltaID) + "'"; |
|
|
|
System.out.println(tempQuery); |
|
System.out.println(tempQueryDest); |
|
|
|
if (dialog != null) |
|
dialog.title.setText("Reading table data ..."); |
|
|
|
bean.makeQuery(tempQuery, deltaID); |
|
beanDest.makeQuery(tempQueryDest, deltaID); |
|
|
|
if (dialog != null) |
|
dialog.title.setText("Writing table data ..."); |
|
|
|
// 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 |
|
{ |
|
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() + "'"; |
|
else |
|
tempQuery = query + " where " + tempID + ">='" + vec.get(endIndex) + "' and " + tempID + "<='" + vec.lastElement() + "'"; |
|
if (destQuery.indexOf("where") > 0) |
|
tempQueryDest = destQuery + " and " + tempIDdest + ">='" + vec.get(endIndex) + "' and " + tempIDdest + "<='" + vec.lastElement() + "'"; |
|
else |
|
tempQueryDest = destQuery + " where " + tempIDdest + ">='" + vec.get(endIndex) + "' and " + tempIDdest + "<='" + vec.lastElement() + "'"; |
|
|
|
System.out.println(tempQuery); |
|
if (dialog != null) |
|
dialog.title.setText("Reading table data ..."); |
|
bean.makeQuery(tempQuery, 0); |
|
beanDest.makeQuery(tempQueryDest, 0); |
|
|
|
if (dialog != null) |
|
dialog.title.setText("Writing table data ..."); |
|
// 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() + "'"; |
|
else |
|
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,delimiter); |
|
long endTime = System.currentTimeMillis(); |
|
System.out.println("Time for old convert elapsed " + (endTime - startTime)); |
|
|
|
}*/ |
|
|
|
if (true) |
|
System.exit(0); |
|
//beanDest.setConnection("jdbc:postgresql://erebos/test3"); |
|
beanDest.setConnection(destination); |
beanDest.setConnection(destination); |
|
|
Statement stm = beanDest.getConnection().createStatement(); |
Statement stm = beanDest.getConnection().createStatement(); |
Line 1794 class Convert
|
Line 1732 class Convert
|
tables = beanDest.getTableNames(); |
tables = beanDest.getTableNames(); |
// System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString())); |
// System.out.println(beanDest.getTableNames(beanDest.getCatalogs().get(2).toString())); |
stm = beanDest.getConnection().createStatement(); |
stm = beanDest.getConnection().createStatement(); |
// System.exit(0); |
|
|
|
if (dialog != null) |
if (dialog != null) |
dialog.title.setText("Updating table data ..."); |
dialog.title.setText(" Deleting table data ..."); |
|
|
int j = -1; |
int j = -1; |
|
|
Vector row = null; |
Vector row = null; |
command = new StringBuffer(); |
command = new StringBuffer(); |
|
|
command.append("UPDATE "); |
command.append("DELETE FROM"); |
command.append(beanDest.getQC()); |
command.append(beanDest.getQC()); |
command.append(destTableName); |
command.append(destTableName); |
//command.append(convertText((String) names.get(tbIndex))); |
//command.append(convertText((String) names.get(tbIndex))); |
command.append(beanDest.getQC()); |
command.append(beanDest.getQC()); |
command.append(" SET "); |
|
|
|
int size = bean.getColumnNames().size(); |
int size = bean.getColumnNames().size(); |
for (int i = 0; i < size - 1; ++i) |
|
command.append(beanDest.getQC() + convertText((String) bean.getColumnNames().get(i)) + beanDest.getQC() + " = ? ,"); |
|
command.append(convertText((String) bean.getColumnNames().get(size - 1)) + " = ? "); |
|
command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " = ?"); |
command.append("WHERE " + convertText(ids.get(tbIndex).toString()) + " = ?"); |
PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString()); |
PreparedStatement pstm = beanDest.getConnection().prepareStatement(command.toString()); |
System.out.println(command + " " + tbIndex); |
System.out.println(command + " " + tbIndex); |
//int rowCount = bean.getRowCount(query); |
//int rowCount = bean.getRowCount(query); |
int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex)); |
// int idIndex = bean.getColumnNames().indexOf(ids.get(tbIndex)); |
while ((row = bean.getNextRow()) != null) |
while (true) |
{ |
|
j++; |
|
//print rows |
|
Object obj = null; |
|
/* for(int k=0;k<row.size()-1;++k) |
|
{ |
{ |
obj = row.get(k); |
|
//System.out.println("row "+obj+" "+k); |
|
if(obj!=null&&!(obj instanceof ArrayList)) |
|
command.append("'"+convertUml(obj.toString())+"',"); |
|
else if(obj!=null&& obj instanceof ArrayList) |
|
command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"',"); |
|
else command.append("NULL,"); |
|
} |
|
obj = row.get(row.size() - 1); |
|
if (obj != null && !(obj instanceof ArrayList)) |
|
command.append("'"+convertUml(obj.toString())+"')"); |
|
else |
|
if(obj!=null&& obj instanceof ArrayList) |
|
command.append("'"+convertUml(((ArrayList)obj).get(0).toString())+"')"); //command.append(obj.toString()+")"); |
|
else command.append("NULL)"); |
|
*/ |
|
//command.append("'"+row.get(row.size()-1)+"')"); |
|
//command.append(" )"); |
|
// for(int k=0;k<row.size();++k) |
|
|
|
// System.out.println(); |
++j; |
// System.out.println(command+" "+j+" "+row.size()+" "+ ((Vector)result2[0].get(j)).size()); |
if (j == linesToDelete.size()) |
// System.out.println(command); |
break; |
for (int k = 0; k < row.size(); ++k) |
//print rows |
{ |
pstm.setString(1, linesToDelete.get(j).toString()); |
obj = row.get(k); |
System.out.println(pstm.toString()); |
if (obj instanceof ArrayList) |
|
obj = ((List) obj).get(0); |
|
String str = (obj == null) ? "NULL" : obj.toString(); |
|
if (!str.equals("NULL")) |
|
pstm.setString(k + 1, str); |
|
else |
|
pstm.setNull(k + 1, Types.NULL); |
|
} |
|
pstm.setString(row.size() + 1, row.get(idIndex).toString()); |
|
//System.out.println(pstm.toString()); |
|
// System.exit(0); |
|
pstm.execute(); |
pstm.execute(); |
//stm.executeUpdate(command.toString()); |
|
if (dialog != null) |
if (dialog != null) |
dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0)); |
dialog.progress.setValue((int) (((double) (j + 1) / (double) rowCount) * 100.0)); |
// System.out.println( (int)(((double)(j+1)/(double)result[0].size())*100.0)+" "+result[0].size()+" "+j); |
|
command = null; |
command = null; |
} // to for loop |
|
|
|
} |
} |
|
// 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) |
} catch (Exception e) |
{ |
{ |
System.out.println("Error while connecting to database " + e); |
System.out.println("Error while connecting to database " + e); |