diff --git a/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseOperations.java b/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseOperations.java index 4bd16b4..3de5332 100644 --- a/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseOperations.java +++ b/src/main/java/org/gcube/dataaccess/databases/utils/DatabaseOperations.java @@ -21,19 +21,24 @@ public class DatabaseOperations { // private static final String QueryPostgres = // "select count(*) from \"%1$s\" limit 1"; -// private static final String QueryPostgres = "select count(*) from (select * from \"%1$s\" limit 1) as a"; + // private static final String QueryPostgres = + // "select count(*) from (select * from \"%1$s\" limit 1) as a"; private static final String QueryPostgres = "select count(*) from (select * from %1$s limit 1) as a"; -// private static final String QueryMysql = "select count(*) from (select * from `%1$s` limit 1) as a"; + // private static final String QueryMysql = + // "select count(*) from (select * from `%1$s` limit 1) as a"; private static final String QueryMysql = "select count(*) from (select * from %1$s limit 1) as a"; // private static final String QueryMysql = // "select count(*) from `%1$s` limit 1"; + private static final String ActualRowsNumberQueryPostgres = "select count(*) from (select * from %1$s) as a"; // private static final String Query = "select * from %1$s limit 1"; // private static final String countQuery = "select count(*) from %1$s"; // private static final String explainQuery = "explain select * from %1$s"; -// private static final String explainQueryPostgres = "explain select * from \"%1$s\""; + // private static final String explainQueryPostgres = + // "explain select * from \"%1$s\""; private static final String explainQueryPostgres = "explain select * from %1$s"; -// private static final String explainQueryMysql = "explain select * from `%1$s`"; + // private static final String explainQueryMysql = + // "explain select * from `%1$s`"; private static final String explainQueryMysql = "explain select * from %1$s"; private static final String MYSQL = "MySQL"; @@ -105,154 +110,185 @@ public class DatabaseOperations { return DBType; } - + // Method that calculate the estimated number of rows - public long calculateElements(ConnectionManager connection, - String dbType, String tablename, String schemaName, SessionFactory session) - throws Exception { + public long calculateElements(ConnectionManager connection, String dbType, + String tablename, String schemaName, SessionFactory session) + throws Exception { - long count = 0; + long count = 0; - String countingQuery = null; + String countingQuery = null; - if (dbType.equals(POSTGRES)) { - - //the full name equal to "schemaname.tablename" - tablename=schemaName+"."+ "\"" +tablename+ "\""; + if (dbType.equals(POSTGRES)) { - countingQuery = String.format(QueryPostgres, tablename); + // the full name equal to "schemaname.tablename" + tablename = schemaName + "." + "\"" + tablename + "\""; - } - if (dbType.equals(MYSQL)) { - //the full name equal to "dbname.tablename" - tablename=schemaName+"."+tablename; + countingQuery = String.format(QueryPostgres, tablename); - countingQuery = String.format(QueryMysql, tablename); + } + if (dbType.equals(MYSQL)) { + // the full name equal to "dbname.tablename" + tablename = schemaName + "." + tablename; - } + countingQuery = String.format(QueryMysql, tablename); - AnalysisLogger.getLogger().debug( - "DatabaseOperations->calculating rows' number with the query: " - + countingQuery); + } - List result; + AnalysisLogger.getLogger().debug( + "DatabaseOperations->calculating rows' number with the query: " + + countingQuery); - // try { - // result = DatabaseFactory.executeSQLQuery(countingQuery, session); + List result; - result = connection.executeQuery(countingQuery, session); + // try { + // result = DatabaseFactory.executeSQLQuery(countingQuery, session); - // if ((result != null) && (result.size() > 0)) { - if (result != null) { - - Object element = result.get(0); - - ArrayList listvalues = new ArrayList(((LinkedHashMap) element).values()); - - + result = connection.executeQuery(countingQuery, session); - // System.out.println("Dimension: " + result.size()); + // if ((result != null) && (result.size() > 0)) { + if (result != null) { -// Integer numElem = Integer.valueOf(result.get(0).toString()); - -// Long numElemvalue = Long.valueOf(result.get(0).toString()); - - Long numElemvalue = Long.valueOf(listvalues.get(0).toString()); - - long numElem = numElemvalue.longValue(); + Object element = result.get(0); - // if (numElem.intValue() == 0){ throw new - // Exception("The table has not rows");} + ArrayList listvalues = new ArrayList( + ((LinkedHashMap) element).values()); - if (numElem > 0) { + // System.out.println("Dimension: " + result.size()); - AnalysisLogger - .getLogger() - .debug("DatabaseOperations->the database has at least a row.Calculating rows' number through an estimation"); + // Integer numElem = Integer.valueOf(result.get(0).toString()); - String explain = null; + // Long numElemvalue = Long.valueOf(result.get(0).toString()); - if (dbType.equals(POSTGRES)) { - + Long numElemvalue = Long.valueOf(listvalues.get(0).toString()); - explain = String.format(explainQueryPostgres, tablename); + long numElem = numElemvalue.longValue(); - } - if (dbType.equals(MYSQL)) { - - - explain = String.format(explainQueryMysql, tablename); + // if (numElem.intValue() == 0){ throw new + // Exception("The table has not rows");} - } + if (numElem > 0) { - // call query with explain function + AnalysisLogger + .getLogger() + .debug("DatabaseOperations->the database has at least a row.Calculating rows' number through an estimation"); + String explain = null; + + if (dbType.equals(POSTGRES)) { + + explain = String.format(explainQueryPostgres, tablename); + + } + if (dbType.equals(MYSQL)) { + + explain = String.format(explainQueryMysql, tablename); + + } + + // call query with explain function + + AnalysisLogger.getLogger().debug( + "DatabaseOperations->calculating rows' number with the query: " + + explain); + + List resultinfo; + + // resultinfo = DatabaseFactory.executeSQLQuery(explain, + // session); + + resultinfo = connection.executeQuery(explain, session); + + // recovery result + + if (dbType.equals(MYSQL)) { + + // Object[] resultArray = (Object[]) (resultinfo.get(0)); + + Object elem = resultinfo.get(0); + + ArrayList values = new ArrayList( + ((LinkedHashMap) elem).values()); + + // //print check + // AnalysisLogger.getLogger().debug( + // "DatabaseOperations->VALUE: " + values); + + BigInteger value = (BigInteger) values.get(8); + + // BigInteger value = (BigInteger) resultArray[8]; + + count = value.longValue(); + + } + + if (dbType.equals(POSTGRES)) { + + String var = resultinfo.get(0).toString(); + + int beginindex = var.indexOf("rows"); + + int lastindex = var.indexOf("width"); + + var = var.substring(beginindex + 5, lastindex - 1); + + Long value = Long.valueOf(var); + + count = value.longValue(); + AnalysisLogger.getLogger().debug( - "DatabaseOperations->calculating rows' number with the query: " - + explain); + "DatabaseOperations-> rows' number with explain function: " + + count); - List resultinfo; + // threshold fixed to 100000 value in order to count an + // actual rows' number if the estimated rows is < 100000 + // otherwise because PGAdmin returns a wrong rows number for + // a table with a little rows number. - // resultinfo = DatabaseFactory.executeSQLQuery(explain, - // session); + if (count < 100000) { + AnalysisLogger.getLogger().debug( + "DatabaseOperations->rows' number with explain function less than the threshold with value 100000"); + // to count an actual rows number + AnalysisLogger.getLogger().debug( + "DatabaseOperations->calculating the actual rows' number with the query: " + + String.format( + ActualRowsNumberQueryPostgres, + tablename)); - resultinfo = connection.executeQuery(explain, session); + List resultRowsNumber; + resultRowsNumber = connection.executeQuery(String + .format(ActualRowsNumberQueryPostgres, + tablename), session); - // recovery result - - if (dbType.equals(MYSQL)) { - -// Object[] resultArray = (Object[]) (resultinfo.get(0)); - - Object elem = resultinfo.get(0); - - ArrayList values = new ArrayList( - ((LinkedHashMap) elem).values()); - -// //print check -// AnalysisLogger.getLogger().debug( -// "DatabaseOperations->VALUE: " + values); - - BigInteger value = (BigInteger) values.get(8); - -// BigInteger value = (BigInteger) resultArray[8]; - - count = value.longValue(); - - - - } - - if (dbType.equals(POSTGRES)) { - - String var = resultinfo.get(0).toString(); - - int beginindex = var.indexOf("rows"); - - int lastindex = var.indexOf("width"); - - var = var.substring(beginindex + 5, lastindex - 1); - - Long value = Long.valueOf(var); - - count = value.longValue(); + if (resultRowsNumber != null) { + Object elem = resultRowsNumber.get(0); + ArrayList listValues = new ArrayList( + ((LinkedHashMap) elem) + .values()); + Long numElemValue = Long.valueOf(listValues.get(0) + .toString()); + count = numElemValue.longValue(); + } } } } - // } catch (Exception e) { - // TODO Auto-generated catch block - // e.printStackTrace(); - // } - - AnalysisLogger.getLogger().debug( - "DatabaseOperations->rows' number calculated: " + count); - - return count; - } + // } catch (Exception e) { + // TODO Auto-generated catch block + // e.printStackTrace(); + // } + + AnalysisLogger.getLogger().debug( + "DatabaseOperations->rows' number calculated: " + count); + + return count; + + } + }