bug corrected in "showCreateTable" method of MySQLTableStructure class (result managed in a correct way using aliasToEntityOrderedMapResultTransformer class).
Bug fixed in "DatabaseManagement" class in "writeTableIntoFile" method to manage for SubmitQuery e sampling operations tables with one column. git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManager@98835 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
ba20c2730e
commit
7762f7a162
|
@ -1,5 +1,7 @@
|
||||||
package org.gcube.dataaccess.databases.structure;
|
package org.gcube.dataaccess.databases.structure;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.gcube.dataaccess.databases.utils.ConnectionManager;
|
import org.gcube.dataaccess.databases.utils.ConnectionManager;
|
||||||
|
@ -16,19 +18,33 @@ public class MySQLTableStructure extends AbstractTableStructure {
|
||||||
|
|
||||||
// Method that executes the query "show create table" in order to retrieve
|
// Method that executes the query "show create table" in order to retrieve
|
||||||
// the "create table" statement
|
// the "create table" statement
|
||||||
public String showCreateTable(ConnectionManager connection, SessionFactory dbSession) throws Exception {
|
public String showCreateTable(ConnectionManager connection,
|
||||||
|
SessionFactory dbSession) throws Exception {
|
||||||
|
|
||||||
// Retrieve the query
|
// Retrieve the query
|
||||||
String queryForIndexes = getQueryForIndexes(dbSession);
|
String queryForIndexes = getQueryForIndexes(dbSession);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// List<Object> indexSet = DatabaseFactory.executeSQLQuery(
|
// List<Object> indexSet = DatabaseFactory.executeSQLQuery(
|
||||||
// String.format(queryForIndexes, tableName), dbSession);
|
// String.format(queryForIndexes, tableName), dbSession);
|
||||||
|
|
||||||
List<Object> indexSet = connection.executeQuery(String.format(queryForIndexes, tableName), dbSession);
|
|
||||||
|
|
||||||
String createTableStatement = (String) (((Object[]) indexSet.get(0))[1]);
|
// List<Object> indexSet =
|
||||||
|
// connection.executeQuery(String.format(queryForIndexes,
|
||||||
|
// tableName), dbSession);
|
||||||
|
|
||||||
|
// String createTableStatement = (String) (((Object[])
|
||||||
|
// indexSet.get(0))[1]);
|
||||||
|
|
||||||
|
List<Object> result = connection.executeQuery(
|
||||||
|
String.format(queryForIndexes, tableName), dbSession);
|
||||||
|
|
||||||
|
Object element = result.get(0);
|
||||||
|
|
||||||
|
ArrayList<Object> listvalues = new ArrayList<Object>(
|
||||||
|
((LinkedHashMap<String, Object>) element).values());
|
||||||
|
|
||||||
|
String createTableStatement = listvalues.get(1).toString();
|
||||||
|
|
||||||
AnalysisLogger.getLogger().debug(
|
AnalysisLogger.getLogger().debug(
|
||||||
"MySQLTableStructure->'Create Table' statement: "
|
"MySQLTableStructure->'Create Table' statement: "
|
||||||
|
@ -37,7 +53,7 @@ public class MySQLTableStructure extends AbstractTableStructure {
|
||||||
return createTableStatement;
|
return createTableStatement;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
||||||
throw e;
|
throw e;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,16 +112,25 @@ public class DatabaseManagement {
|
||||||
private String convertToJavaType(String type, String val) throws Exception {
|
private String convertToJavaType(String type, String val) throws Exception {
|
||||||
|
|
||||||
type = type.toLowerCase();
|
type = type.toLowerCase();
|
||||||
|
|
||||||
|
// AnalysisLogger.getLogger()
|
||||||
|
// .debug("In DatabaseManagement->type: "
|
||||||
|
// + type);
|
||||||
|
|
||||||
String valConverted = val;
|
String valConverted = val;
|
||||||
|
|
||||||
|
// AnalysisLogger.getLogger()
|
||||||
|
// .debug("In DatabaseManagement->val: "
|
||||||
|
// + valConverted);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// parse to Long
|
// parse to Long
|
||||||
valConverted = "" + Long.parseLong(valConverted);
|
valConverted = "" + Long.parseLong(valConverted);
|
||||||
|
|
||||||
// AnalysisLogger.getLogger()
|
// AnalysisLogger.getLogger()
|
||||||
// .debug("In DatabaseManagement->parsed value Long: "
|
// .debug("In DatabaseManagement->parsed value Long: "
|
||||||
// + valConverted);
|
// + valConverted);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,9 +160,9 @@ public class DatabaseManagement {
|
||||||
valConverted = "" + Double.parseDouble(valConverted);
|
valConverted = "" + Double.parseDouble(valConverted);
|
||||||
|
|
||||||
// // check the parsed value
|
// // check the parsed value
|
||||||
// AnalysisLogger.getLogger().debug(
|
// AnalysisLogger.getLogger().debug(
|
||||||
// "In DatabaseManagement->parsed value Double: "
|
// "In DatabaseManagement->parsed value Double: "
|
||||||
// + valConverted);
|
// + valConverted);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,9 +171,24 @@ public class DatabaseManagement {
|
||||||
valConverted = "" + Float.parseFloat(valConverted);
|
valConverted = "" + Float.parseFloat(valConverted);
|
||||||
|
|
||||||
// // check the parsed value
|
// // check the parsed value
|
||||||
// AnalysisLogger.getLogger().debug(
|
// AnalysisLogger.getLogger().debug(
|
||||||
// "In DatabaseManagement->parsed value Float: "
|
// "In DatabaseManagement->parsed value Float: "
|
||||||
// + 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);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,36 +438,32 @@ public class DatabaseManagement {
|
||||||
"DatabaseManagement->table's list retrieved");
|
"DatabaseManagement->table's list retrieved");
|
||||||
|
|
||||||
tablesname = new ArrayList<String>();
|
tablesname = new ArrayList<String>();
|
||||||
|
|
||||||
for (int i = 0; i < resultSet.size(); i++) {
|
for (int i = 0; i < resultSet.size(); i++) {
|
||||||
|
|
||||||
Object element = resultSet.get(i);
|
|
||||||
|
|
||||||
ArrayList<Object> listvalues = new ArrayList<Object>(
|
|
||||||
((LinkedHashMap<String, Object>) element).values());
|
|
||||||
|
|
||||||
for (int j=0; j<listvalues.size();j++){
|
|
||||||
|
|
||||||
|
Object element = resultSet.get(i);
|
||||||
|
|
||||||
tablesname.add(listvalues.get(j).toString());
|
ArrayList<Object> listvalues = new ArrayList<Object>(
|
||||||
|
((LinkedHashMap<String, Object>) element).values());
|
||||||
// AnalysisLogger.getLogger().debug(
|
|
||||||
// "DatabaseManagement->" + listvalues.get(j).toString());
|
for (int j = 0; j < listvalues.size(); j++) {
|
||||||
|
|
||||||
|
tablesname.add(listvalues.get(j).toString());
|
||||||
|
|
||||||
|
// AnalysisLogger.getLogger().debug(
|
||||||
|
// "DatabaseManagement->" + listvalues.get(j).toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// for (Object result : resultSet) {
|
// for (Object result : resultSet) {
|
||||||
// tablesname.add((String) result);
|
// tablesname.add((String) result);
|
||||||
//
|
//
|
||||||
// // AnalysisLogger.getLogger().debug(
|
// // AnalysisLogger.getLogger().debug(
|
||||||
// // "DatabaseManagement->" + (String) result);
|
// // "DatabaseManagement->" + (String) result);
|
||||||
//
|
//
|
||||||
// }
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -468,31 +488,29 @@ public class DatabaseManagement {
|
||||||
|
|
||||||
if (resultSet != null) {
|
if (resultSet != null) {
|
||||||
|
|
||||||
// for (Object result : resultSet) {
|
// for (Object result : resultSet) {
|
||||||
// list.add((String) result);
|
// list.add((String) result);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
for (int i = 0; i < resultSet.size(); i++) {
|
for (int i = 0; i < resultSet.size(); i++) {
|
||||||
|
|
||||||
Object element = resultSet.get(i);
|
|
||||||
|
|
||||||
ArrayList<Object> listvalues = new ArrayList<Object>(
|
|
||||||
((LinkedHashMap<String, Object>) element).values());
|
|
||||||
|
|
||||||
for (int j=0; j<listvalues.size();j++){
|
|
||||||
|
|
||||||
|
Object element = resultSet.get(i);
|
||||||
|
|
||||||
list.add(listvalues.get(j).toString());
|
ArrayList<Object> listvalues = new ArrayList<Object>(
|
||||||
|
((LinkedHashMap<String, Object>) element).values());
|
||||||
// AnalysisLogger.getLogger().debug(
|
|
||||||
// "DatabaseManagement->" + listvalues.get(j).toString());
|
for (int j = 0; j < listvalues.size(); j++) {
|
||||||
|
|
||||||
|
list.add(listvalues.get(j).toString());
|
||||||
|
|
||||||
|
// AnalysisLogger.getLogger().debug(
|
||||||
|
// "DatabaseManagement->" +
|
||||||
|
// listvalues.get(j).toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -986,9 +1004,9 @@ public class DatabaseManagement {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// // print check
|
// // print check
|
||||||
// AnalysisLogger.getLogger().debug(
|
// AnalysisLogger.getLogger().debug(
|
||||||
// "DatabaseManagement->HEADERS: " + header);
|
// "DatabaseManagement->HEADERS: " + header);
|
||||||
|
|
||||||
out.write(header);
|
out.write(header);
|
||||||
out.newLine();
|
out.newLine();
|
||||||
|
@ -1001,142 +1019,146 @@ public class DatabaseManagement {
|
||||||
|
|
||||||
if (result != null && result.size() != 0) {
|
if (result != null && result.size() != 0) {
|
||||||
|
|
||||||
|
// // write operation in the file
|
||||||
|
for (int i = 0; i < result.size(); i++) {
|
||||||
|
|
||||||
// // write operation in the file
|
String RowString = "";
|
||||||
for (int i = 0; i < result.size(); i++) {
|
|
||||||
|
|
||||||
String RowString = "";
|
Object element = result.get(i);
|
||||||
|
|
||||||
Object element = result.get(i);
|
//arraylist in which each element is a row result
|
||||||
|
ArrayList<Object> listvalues = new ArrayList<Object>(
|
||||||
|
((LinkedHashMap<String, Object>) element).values());
|
||||||
|
|
||||||
ArrayList<Object> listvalues = new ArrayList<Object>(
|
// // print check
|
||||||
((LinkedHashMap<String, Object>) element).values());
|
// AnalysisLogger.getLogger().debug(
|
||||||
|
// "DatabaseManagement->values: " + listvalues);
|
||||||
|
|
||||||
// // print check
|
//each row could have several column values
|
||||||
// AnalysisLogger.getLogger().debug(
|
Object[] row = listvalues.toArray();
|
||||||
// "DatabaseManagement->values: " + listvalues);
|
|
||||||
|
|
||||||
Object[] row = listvalues.toArray();
|
if (row.length >= 1) {
|
||||||
|
|
||||||
if (row.length > 1) {
|
for (int j = 0; j < row.length; j++) {
|
||||||
|
|
||||||
for (int j = 0; j < row.length; j++) {
|
if (row[j] == null) {
|
||||||
|
row[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(",");
|
|
||||||
// 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();
|
|
||||||
|
|
||||||
// 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
|
// to parse the obtained results in order to align
|
||||||
// number
|
// number
|
||||||
// values with those of postgres
|
// values with those of postgres
|
||||||
String original = row[0].toString();
|
String original = row[j].toString();
|
||||||
|
|
||||||
// // check value
|
// // check value
|
||||||
// AnalysisLogger.getLogger().debug(
|
// AnalysisLogger.getLogger().debug(
|
||||||
// "In DatabaseManagement->original value: "
|
// "In DatabaseManagement->original value: "
|
||||||
// + original);
|
// + original);
|
||||||
|
|
||||||
String parsed = "" + row[0];
|
String parsed = "" + row[j];
|
||||||
|
|
||||||
if (original != "") {
|
if (original != "") {
|
||||||
// convert database datatypes to Java datatypes
|
// convert database datatypes to Java datatypes
|
||||||
if (DataTypeColumns == null
|
if (DataTypeColumns == null
|
||||||
|| DataTypeColumns.size() == 0)
|
|| DataTypeColumns.size() == 0)
|
||||||
parsed = convertToJavaType(row[0]
|
parsed = convertToJavaType(row[j].getClass()
|
||||||
.getClass().getName(), parsed);
|
.getName(), parsed);
|
||||||
else
|
else
|
||||||
parsed = convertToJavaType(
|
parsed = convertToJavaType(
|
||||||
DataTypeColumns.get(0), parsed);
|
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
|
||||||
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out.write(row[0].toString());
|
|
||||||
out.newLine();
|
|
||||||
|
|
||||||
// to add a row to the map
|
|
||||||
mapResult.put(String.valueOf(i), row[0].toString());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
} 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());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1279,33 +1301,32 @@ public class DatabaseManagement {
|
||||||
if (columnsSet != null) {
|
if (columnsSet != null) {
|
||||||
|
|
||||||
listColumnNamesTable = new ArrayList<String>();
|
listColumnNamesTable = new ArrayList<String>();
|
||||||
|
|
||||||
for (int i = 0; i < columnsSet.size(); i++) {
|
for (int i = 0; i < columnsSet.size(); i++) {
|
||||||
|
|
||||||
Object element = columnsSet.get(i);
|
Object element = columnsSet.get(i);
|
||||||
|
|
||||||
ArrayList<Object> listvalues = new ArrayList<Object>(
|
|
||||||
((LinkedHashMap<String, Object>) element).values());
|
|
||||||
|
|
||||||
for (int j=0; j<listvalues.size();j++){
|
|
||||||
|
|
||||||
|
ArrayList<Object> listvalues = new ArrayList<Object>(
|
||||||
|
((LinkedHashMap<String, Object>) element).values());
|
||||||
|
|
||||||
listColumnNamesTable.add(listvalues.get(j).toString());
|
for (int j = 0; j < listvalues.size(); j++) {
|
||||||
|
|
||||||
// AnalysisLogger.getLogger().debug(
|
listColumnNamesTable.add(listvalues.get(j).toString());
|
||||||
// "DatabaseManagement->" + listvalues.get(j).toString());
|
|
||||||
|
// AnalysisLogger.getLogger().debug(
|
||||||
|
// "DatabaseManagement->" + listvalues.get(j).toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// for (Object column : columnsSet) {
|
// for (Object column : columnsSet) {
|
||||||
//
|
//
|
||||||
// AnalysisLogger.getLogger().debug(
|
// AnalysisLogger.getLogger().debug(
|
||||||
// "DatabaseManagement->column name: " + column);
|
// "DatabaseManagement->column name: " + column);
|
||||||
//
|
//
|
||||||
// listColumnNamesTable.add((String) column);
|
// listColumnNamesTable.add((String) column);
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
return listColumnNamesTable;
|
return listColumnNamesTable;
|
||||||
|
|
Loading…
Reference in New Issue