diff --git a/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseManagement.java b/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseManagement.java index 8faf488..6dc65c9 100644 --- a/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseManagement.java +++ b/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseManagement.java @@ -112,25 +112,25 @@ public class DatabaseManagement { private String convertToJavaType(String type, String val) throws Exception { type = type.toLowerCase(); - -// AnalysisLogger.getLogger() -// .debug("In DatabaseManagement->type: " -// + type); - + + // AnalysisLogger.getLogger() + // .debug("In DatabaseManagement->type: " + // + type); + String valConverted = val; - -// AnalysisLogger.getLogger() -// .debug("In DatabaseManagement->val: " -// + valConverted); + + // AnalysisLogger.getLogger() + // .debug("In DatabaseManagement->val: " + // + valConverted); try { // parse to Long valConverted = "" + Long.parseLong(valConverted); -// AnalysisLogger.getLogger() -// .debug("In DatabaseManagement->parsed value Long: " -// + valConverted); + // AnalysisLogger.getLogger() + // .debug("In DatabaseManagement->parsed value Long: " + // + valConverted); } @@ -160,9 +160,9 @@ public class DatabaseManagement { valConverted = "" + Double.parseDouble(valConverted); // // check the parsed value -// AnalysisLogger.getLogger().debug( -// "In DatabaseManagement->parsed value Double: " -// + valConverted); + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->parsed value Double: " + // + valConverted); } @@ -171,24 +171,24 @@ public class DatabaseManagement { valConverted = "" + Float.parseFloat(valConverted); // // check the parsed value -// AnalysisLogger.getLogger().debug( -// "In DatabaseManagement->parsed value Float: " -// + valConverted); + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->parsed value Float: " + // + valConverted); } - if (((type.contains("string")))||(type.contains("varchar")) || (type.contains("char")) - || (type.contains("text")) || (type - .contains("character varying"))) { - - valConverted= "\"" + valConverted + "\""; - + if (((type.contains("string"))) + || (type.contains("varchar")) + || (type.contains("char")) + || (type.contains("text")) + || (type.contains("character varying"))) { + + valConverted = "\"" + valConverted + "\""; + // // check the parsed value -// AnalysisLogger.getLogger().debug( -// "In DatabaseManagement->parsed value string: " -// + valConverted); - - + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->parsed value string: " + // + valConverted); } @@ -343,38 +343,22 @@ public class DatabaseManagement { AnalysisLogger.getLogger().debug( "In DatabaseManagement->store table in a file"); - // store table in a file - // writeQueryResultIntoFile(results); - - // writeSampleTableIntoFile(results, tableName, schemaName); - - String FileName = pathFile + "QueryResult.csv"; - // List listColumnNames = getColumnNamesTable(tableName, // schemaName); // to retrieve datatype columns of a table - List DataTypeColumns = null; - // write the result in the file and in the map - - AnalysisLogger.getLogger().debug( - "In DatabaseManagement->writing the result in the file: " - + FileName); - - file = new File(FileName); - - out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( - file), "UTF-8")); - - // String header = null; - - // writeTableIntoFile(results, DataTypeColumns, header, tableName, - // schemaName, - // FileName); - - writeTableIntoFile(results, DataTypeColumns); + // // store table in a file + // String FileName = pathFile + "QueryResult.csv"; + // // write the result in the file and in the map + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->writing the result in the file: " + // + FileName); + // file = new File(FileName); + // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( + // file), "UTF-8")); + // writeTableIntoFile(results, DataTypeColumns); return results; @@ -610,13 +594,14 @@ public class DatabaseManagement { } // Method that returns the estimated number of rows - public long getNumberOfRows(String tablename, String schemaName) throws Exception { + public long getNumberOfRows(String tablename, String schemaName) + throws Exception { long rows; rows = op.calculateElements(connection, DBType, tablename, schemaName, sourceDBSession); - + AnalysisLogger.getLogger().debug( "DatabaseManagement->rows' number calculated: " + rows); @@ -718,23 +703,7 @@ public class DatabaseManagement { out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( file), "UTF-8")); - // mapResult.put("HEADERS", header); - // - // out.write(header); - // out.newLine(); - - // writeTableIntoFile(resultSet, DataTypeColumns, header, tableName, - // schemaName, - // FileName); - - // writeTableIntoFile(resultSet, DataTypeColumns, tableName, - // schemaName, - // FileName); - - writeTableIntoFile(resultSet, DataTypeColumns); - - // return resultSet; - + // writeTableIntoFile(resultSet, DataTypeColumns); } /** @@ -758,7 +727,6 @@ public class DatabaseManagement { AnalysisLogger.getLogger().debug( "DatabaseManagement->starting the Sample on table operation"); - AnalysisLogger.getLogger().debug( "DatabaseManagement->retrieving the first 100 rows"); @@ -778,57 +746,27 @@ public class DatabaseManagement { AnalysisLogger.getLogger().debug( "In DatabaseManagement->store table in a file"); - // store table in a file - - // writeSampleTableIntoFile(resultSet, tableName, schemaName); - - String FileName = pathFile + "SampleResult.csv"; - // to recover columns names list List listColumns = sampler.getListColumns(); - // String header = ""; for (int i = 0; i < listColumns.size(); i++) { - if (i != listColumns.size() - 1) { - header = header + listColumns.get(i) + ", "; - } else { - header = header + listColumns.get(i); - } } - // write the result in the file and in the map - - AnalysisLogger.getLogger().debug( - "In DatabaseManagement->writing the result in the file: " - + FileName); - - file = new File(FileName); - - out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( - file), "UTF-8")); - - // mapResult.put("HEADERS", header); - // - // out.write(header); - // out.newLine(); - - // System.out.println("HEADER:" + header); - - // writeTableIntoFile(resultSet, DataTypeColumns, header, tableName, - // schemaName, - // FileName); - // writeTableIntoFile(resultSet, DataTypeColumns, tableName, schemaName, - // FileName); - - writeTableIntoFile(resultSet, DataTypeColumns); - - // return the first 100 rows - // return resultSet; + // // store table in a file + // String FileName = pathFile + "SampleResult.csv"; + // // write the result in the file and in the map + // AnalysisLogger.getLogger().debug( + // "In DatabaseManagement->writing the result in the file: " + // + FileName); + // file = new File(FileName); + // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( + // file), "UTF-8")); + // writeTableIntoFile(resultSet, DataTypeColumns); } @@ -853,7 +791,6 @@ public class DatabaseManagement { AnalysisLogger .getLogger() .debug("DatabaseManagement->starting the Random Sample on table operation"); - AnalysisLogger.getLogger().debug( "DatabaseManagement->retrieving 100 rows"); @@ -861,7 +798,6 @@ public class DatabaseManagement { List DataTypeColumns = getDataTypeColumns(tableName, schemaName); if (estimatedRows == 0) { - estimatedRows = getNumberOfRows(tableName, schemaName); } @@ -878,14 +814,7 @@ public class DatabaseManagement { AnalysisLogger.getLogger().debug( "In DatabaseManagement->store table in a file"); - // store table in a file - - // writeSampleTableIntoFile(resultSet, tableName, schemaName); - - String FileName = pathFile + "SampleResult.csv"; - // to recover columns names list - List listColumns = sampler.getListColumns(); // //print check @@ -897,44 +826,23 @@ public class DatabaseManagement { // String header = ""; for (int i = 0; i < listColumns.size(); i++) { - if (i != listColumns.size() - 1) { - header = header + listColumns.get(i) + ", "; - } else { - header = header + listColumns.get(i); - } } - // write the result in the file and in the map - - AnalysisLogger.getLogger().debug( - "In DatabaseManagement->writing the result in the file: " - + FileName); - - file = new File(FileName); - - out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( - file), "UTF-8")); - - // mapResult.put("HEADERS", header); - // - // out.write(header); - // out.newLine(); - - // writeTableIntoFile(resultSet, DataTypeColumns, header, tableName, - // schemaName, - // FileName); - - // writeTableIntoFile(resultSet, DataTypeColumns, tableName, schemaName, - // FileName); - - writeTableIntoFile(resultSet, DataTypeColumns); - - // return resultSet; +// // store table in a file +// String FileName = pathFile + "SampleResult.csv"; +// // write the result in the file and in the map +// AnalysisLogger.getLogger().debug( +// "In DatabaseManagement->writing the result in the file: " +// + FileName); +// file = new File(FileName); +// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( +// file), "UTF-8")); +// writeTableIntoFile(resultSet, DataTypeColumns); } @@ -943,237 +851,188 @@ public class DatabaseManagement { // List DataTypeColumns, String tableName, String schemaName, // String FileName) throws Exception { - // write the table result in the file and build the map of results - private void writeTableIntoFile(List result, - List DataTypeColumns) throws Exception { - - // // file that will contain result - // BufferedWriter out; - // - // // String fileName; - // - // // fileName = "./cfg/" + "table.txt"; - // // fileName = "./files/" + "table.txt"; - // - // // fileName = - // // - // "/home/loredana/workspace/DatabasesResourcesManagerAlgorithms/cfg/" - // // + "SampleOnTable.txt"; - // - // // fileName = "./files/" + "SmartSampleOnTable.txt"; - // // File file = new File(fileName); - // - // AnalysisLogger.getLogger().debug( - // "In DatabaseManagement->writing the result in the file: " - // + FileName); - // - // file = new File(FileName); - // - // // FileWriter fw = new FileWriter(file.getAbsoluteFile()); - // // out = new BufferedWriter(fw); - // - // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( - // file), "UTF-8")); - // - // // mapResult.put("HEADER", header); - // - // out.write(header); - // out.newLine(); - - // to get columns names and result - - // write headers in the file - - // to recover columns names - - if (header.equals("")) { - ArrayList listKeys = new ArrayList( - ((LinkedHashMap) (result.get(0))).keySet()); - - for (int i = 0; i < listKeys.size(); i++) { - - if (i != listKeys.size() - 1) { - - header = header + listKeys.get(i) + ", "; - - } else { - - header = header + listKeys.get(i); - - } - } - - } - -// // print check -// AnalysisLogger.getLogger().debug( -// "DatabaseManagement->HEADERS: " + header); - - out.write(header); - out.newLine(); - - mapResult.put("HEADERS", header); - - // //print check values - // AnalysisLogger.getLogger().debug( - // "DatabaseManagement->columns names: " + listKeys); - - if (result != null && result.size() != 0) { - - // // write operation in the file - for (int i = 0; i < result.size(); i++) { - - String RowString = ""; - - Object element = result.get(i); - - //arraylist in which each element is a row result - ArrayList listvalues = new ArrayList( - ((LinkedHashMap) element).values()); - - // // print check - // AnalysisLogger.getLogger().debug( - // "DatabaseManagement->values: " + listvalues); - - //each row could have several column values - Object[] row = listvalues.toArray(); - - if (row.length >= 1) { - - for (int j = 0; j < row.length; j++) { - - if (row[j] == null) { - row[j] = ""; - } - - // to parse the obtained results in order to align - // number - // values with those of postgres - String original = row[j].toString(); - -// // check value -// AnalysisLogger.getLogger().debug( -// "In DatabaseManagement->original value: " -// + original); - - String parsed = "" + row[j]; - - if (original != "") { - // convert database datatypes to Java datatypes - if (DataTypeColumns == null - || DataTypeColumns.size() == 0) - parsed = convertToJavaType(row[j].getClass() - .getName(), parsed); - else - parsed = convertToJavaType( - DataTypeColumns.get(j), parsed); - } - - // // check value - // AnalysisLogger.getLogger().debug( - // "In DatabaseManagement->row: " + (i + 1) - // + " column: " + (j + 1) + " value= " - // + parsed); - - // write in a file - - if (j != row.length - 1) { - -// out.write("\"" + parsed + "\""); - -// out.write(","); - out.write(parsed); - out.write(","); - - // System.out.println("write column : " + j); - // RowString = RowString + parsed + " "; - if (j == 0) { - RowString = parsed; - } else { - - RowString = RowString + "," + parsed; - } - - } - if (j == row.length - 1) { - -// out.write("\"" + parsed + "\""); -// out.newLine(); - - out.write(parsed); - out.newLine(); - - // to add a row to the map - if (row.length ==1){ - RowString = parsed; - }else{ - RowString = RowString + "," + parsed; - } - - // to add a row to the map -// RowString = RowString + "," + parsed; - // mapSampleTableResult.put(String.valueOf(i), - // RowString); - - // // check value row -// AnalysisLogger.getLogger().debug( -// "writing the value: " + RowString + " key: " -// + String.valueOf(i)); - - // mapResult.put(Integer.valueOf(i), RowString); - mapResult.put(String.valueOf(i), RowString); - - } - - } - } -// else if (result.size() == 1) { -// -// // Object RowElement = (Object) result.get(0); -// -// if (row[0] == null) { -// row[0] = ""; -// } -// -// // to parse the obtained results in order to align -// // number -// // values with those of postgres -// String original = row[0].toString(); -// -// // // check value -// // AnalysisLogger.getLogger().debug( -// // "In DatabaseManagement->original value: " -// // + original); -// -// String parsed = "" + row[0]; -// -// if (original != "") { -// // convert database datatypes to Java datatypes -// if (DataTypeColumns == null -// || DataTypeColumns.size() == 0) -// parsed = convertToJavaType(row[0].getClass() -// .getName(), parsed); -// else -// parsed = convertToJavaType(DataTypeColumns.get(0), -// parsed); -// } -// -// out.write(row[0].toString()); -// out.newLine(); -// -// // to add a row to the map -// mapResult.put(String.valueOf(i), row[0].toString()); -// -// } - - } - - } - - // close the file - out.close(); - - } + // // write the table result in the file and build the map of results + // private void writeTableIntoFile(List result, + // List DataTypeColumns) throws Exception { + // + // // // file that will contain result + // // BufferedWriter out; + // // // String fileName; + // // // fileName = "./cfg/" + "table.txt"; + // // // fileName = "./files/" + "table.txt"; + // // // fileName = + // // // + // // "/home/loredana/workspace/DatabasesResourcesManagerAlgorithms/cfg/" + // // // + "SampleOnTable.txt"; + // // // fileName = "./files/" + "SmartSampleOnTable.txt"; + // // // File file = new File(fileName); + // // file = new File(FileName); + // // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( + // // file), "UTF-8")); + // + // // to get columns names and result + // // write headers in the file + // // to recover columns names + // if (header.equals("")) { + // ArrayList listKeys = new ArrayList( + // ((LinkedHashMap) (result.get(0))).keySet()); + // + // for (int i = 0; i < listKeys.size(); i++) { + // if (i != listKeys.size() - 1) { + // header = header + listKeys.get(i) + ", "; + // } else { + // header = header + listKeys.get(i); + // } + // } + // } + // + // // // print check + // // AnalysisLogger.getLogger().debug( + // // "DatabaseManagement->HEADERS: " + header); + // + // out.write(header); + // out.newLine(); + // mapResult.put("HEADERS", header); + // + // // //print check values + // // AnalysisLogger.getLogger().debug( + // // "DatabaseManagement->columns names: " + listKeys); + // + // if (result != null && result.size() != 0) { + // // // write operation in the file + // for (int i = 0; i < result.size(); i++) { + // String RowString = ""; + // Object element = result.get(i); + // + // // arraylist in which each element is a row result + // ArrayList listvalues = new ArrayList( + // ((LinkedHashMap) element).values()); + // + // // // print check + // // AnalysisLogger.getLogger().debug( + // // "DatabaseManagement->values: " + listvalues); + // + // // each row could have several column values + // Object[] row = listvalues.toArray(); + // if (row.length >= 1) { + // for (int j = 0; j < row.length; j++) { + // if (row[j] == null) { + // row[j] = ""; + // } + // // to parse the obtained results in order to align + // // number + // // values with those of postgres + // String original = row[j].toString(); + // + // // // check value + // // AnalysisLogger.getLogger().debug( + // // "In DatabaseManagement->original value: " + // // + original); + // + // String parsed = "" + row[j]; + // if (original != "") { + // // convert database datatypes to Java datatypes + // if (DataTypeColumns == null + // || DataTypeColumns.size() == 0) + // parsed = convertToJavaType(row[j].getClass() + // .getName(), parsed); + // else + // parsed = convertToJavaType( + // DataTypeColumns.get(j), parsed); + // } + // + // // // check value + // // AnalysisLogger.getLogger().debug( + // // "In DatabaseManagement->row: " + (i + 1) + // // + " column: " + (j + 1) + " value= " + // // + parsed); + // + // // write in a file + // if (j != row.length - 1) { + // // out.write("\"" + parsed + "\""); + // // out.write(","); + // out.write(parsed); + // out.write(","); + // + // // System.out.println("write column : " + j); + // // RowString = RowString + parsed + " "; + // if (j == 0) { + // RowString = parsed; + // } else { + // RowString = RowString + "," + parsed; + // } + // } + // if (j == row.length - 1) { + // // out.write("\"" + parsed + "\""); + // // out.newLine(); + // out.write(parsed); + // out.newLine(); + // + // // to add a row to the map + // if (row.length == 1) { + // RowString = parsed; + // } else { + // RowString = RowString + "," + parsed; + // } + // + // // to add a row to the map + // // RowString = RowString + "," + parsed; + // // mapSampleTableResult.put(String.valueOf(i), + // // RowString); + // + // // // check value row + // // AnalysisLogger.getLogger().debug( + // // "writing the value: " + RowString + " key: " + // // + String.valueOf(i)); + // + // // mapResult.put(Integer.valueOf(i), RowString); + // mapResult.put(String.valueOf(i), RowString); + // } + // } + // } + // // else if (result.size() == 1) { + // // + // // // Object RowElement = (Object) result.get(0); + // // + // // if (row[0] == null) { + // // row[0] = ""; + // // } + // // + // // // to parse the obtained results in order to align + // // // number + // // // values with those of postgres + // // String original = row[0].toString(); + // // + // // // // check value + // // // AnalysisLogger.getLogger().debug( + // // // "In DatabaseManagement->original value: " + // // // + original); + // // + // // String parsed = "" + row[0]; + // // + // // if (original != "") { + // // // convert database datatypes to Java datatypes + // // if (DataTypeColumns == null + // // || DataTypeColumns.size() == 0) + // // parsed = convertToJavaType(row[0].getClass() + // // .getName(), parsed); + // // else + // // parsed = convertToJavaType(DataTypeColumns.get(0), + // // parsed); + // // } + // // + // // out.write(row[0].toString()); + // // out.newLine(); + // // + // // // to add a row to the map + // // mapResult.put(String.valueOf(i), row[0].toString()); + // // + // // } + // } + // } + // // close the file + // out.close(); + // } // to retrieve datatype columns of a table private List getDataTypeColumns(String tableName, String schemaName)