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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
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
|
||||
// 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
|
||||
String queryForIndexes = getQueryForIndexes(dbSession);
|
||||
|
||||
try {
|
||||
|
||||
// List<Object> indexSet = DatabaseFactory.executeSQLQuery(
|
||||
// String.format(queryForIndexes, tableName), dbSession);
|
||||
|
||||
List<Object> indexSet = connection.executeQuery(String.format(queryForIndexes, tableName), dbSession);
|
||||
// List<Object> indexSet = DatabaseFactory.executeSQLQuery(
|
||||
// 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(
|
||||
"MySQLTableStructure->'Create Table' statement: "
|
||||
|
@ -37,7 +53,7 @@ public class MySQLTableStructure extends AbstractTableStructure {
|
|||
return createTableStatement;
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
|
||||
throw e;
|
||||
|
||||
}
|
||||
|
|
|
@ -112,16 +112,25 @@ public class DatabaseManagement {
|
|||
private String convertToJavaType(String type, String val) throws Exception {
|
||||
|
||||
type = type.toLowerCase();
|
||||
|
||||
// AnalysisLogger.getLogger()
|
||||
// .debug("In DatabaseManagement->type: "
|
||||
// + type);
|
||||
|
||||
String valConverted = val;
|
||||
|
||||
// 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);
|
||||
|
||||
}
|
||||
|
||||
|
@ -151,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);
|
||||
|
||||
}
|
||||
|
||||
|
@ -162,9 +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 + "\"";
|
||||
|
||||
// // 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");
|
||||
|
||||
tablesname = new ArrayList<String>();
|
||||
|
||||
|
||||
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());
|
||||
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->" + listvalues.get(j).toString());
|
||||
ArrayList<Object> listvalues = new ArrayList<Object>(
|
||||
((LinkedHashMap<String, Object>) element).values());
|
||||
|
||||
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) {
|
||||
// tablesname.add((String) result);
|
||||
//
|
||||
// // AnalysisLogger.getLogger().debug(
|
||||
// // "DatabaseManagement->" + (String) result);
|
||||
//
|
||||
// }
|
||||
// for (Object result : resultSet) {
|
||||
// tablesname.add((String) result);
|
||||
//
|
||||
// // AnalysisLogger.getLogger().debug(
|
||||
// // "DatabaseManagement->" + (String) result);
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
@ -468,31 +488,29 @@ public class DatabaseManagement {
|
|||
|
||||
if (resultSet != null) {
|
||||
|
||||
// for (Object result : resultSet) {
|
||||
// list.add((String) result);
|
||||
// }
|
||||
|
||||
// for (Object result : resultSet) {
|
||||
// list.add((String) result);
|
||||
// }
|
||||
|
||||
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());
|
||||
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->" + listvalues.get(j).toString());
|
||||
ArrayList<Object> listvalues = new ArrayList<Object>(
|
||||
((LinkedHashMap<String, Object>) element).values());
|
||||
|
||||
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
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->HEADERS: " + header);
|
||||
// // print check
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->HEADERS: " + header);
|
||||
|
||||
out.write(header);
|
||||
out.newLine();
|
||||
|
@ -1001,142 +1019,146 @@ public class DatabaseManagement {
|
|||
|
||||
if (result != null && result.size() != 0) {
|
||||
|
||||
|
||||
// // write operation in the file
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
|
||||
// // write operation in the file
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
String RowString = "";
|
||||
|
||||
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>(
|
||||
((LinkedHashMap<String, Object>) element).values());
|
||||
// // print check
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->values: " + listvalues);
|
||||
|
||||
// // print check
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->values: " + listvalues);
|
||||
//each row could have several column values
|
||||
Object[] row = listvalues.toArray();
|
||||
|
||||
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] = "";
|
||||
}
|
||||
|
||||
// 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] = "";
|
||||
if (row[j] == null) {
|
||||
row[j] = "";
|
||||
}
|
||||
|
||||
// to parse the obtained results in order to align
|
||||
// number
|
||||
// values with those of postgres
|
||||
String original = row[0].toString();
|
||||
String original = row[j].toString();
|
||||
|
||||
// // check value
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "In DatabaseManagement->original value: "
|
||||
// + original);
|
||||
// // check value
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "In DatabaseManagement->original value: "
|
||||
// + original);
|
||||
|
||||
String parsed = "" + row[0];
|
||||
String parsed = "" + row[j];
|
||||
|
||||
if (original != "") {
|
||||
// convert database datatypes to Java datatypes
|
||||
if (DataTypeColumns == null
|
||||
|| DataTypeColumns.size() == 0)
|
||||
parsed = convertToJavaType(row[0]
|
||||
.getClass().getName(), parsed);
|
||||
parsed = convertToJavaType(row[j].getClass()
|
||||
.getName(), parsed);
|
||||
else
|
||||
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) {
|
||||
|
||||
listColumnNamesTable = new ArrayList<String>();
|
||||
|
||||
|
||||
for (int i = 0; i < columnsSet.size(); 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());
|
||||
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->" + listvalues.get(j).toString());
|
||||
for (int j = 0; j < listvalues.size(); j++) {
|
||||
|
||||
listColumnNamesTable.add(listvalues.get(j).toString());
|
||||
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->" + listvalues.get(j).toString());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// for (Object column : columnsSet) {
|
||||
//
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->column name: " + column);
|
||||
//
|
||||
// listColumnNamesTable.add((String) column);
|
||||
// }
|
||||
// for (Object column : columnsSet) {
|
||||
//
|
||||
// AnalysisLogger.getLogger().debug(
|
||||
// "DatabaseManagement->column name: " + column);
|
||||
//
|
||||
// listColumnNamesTable.add((String) column);
|
||||
// }
|
||||
}
|
||||
|
||||
return listColumnNamesTable;
|
||||
|
|
Loading…
Reference in New Issue