bugs fixed in showcreatetable and samplings operations. In these operation it is useful to insert the table name in a query as "schema.table" for postgres and "db.table" for mysql.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManager@98907 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Loredana Liccardo 2014-07-23 15:11:58 +00:00
parent aa3fbea54b
commit 229f351f7d
4 changed files with 63 additions and 17 deletions

View File

@ -18,11 +18,13 @@ import org.hibernate.SessionFactory;
public class Sampler {
// query to perform sample operation on the table
private static final String queryForSampleOnTablePostgres = "select %1$s from \"%2$s\" limit 100";
// private static final String queryForSampleOnTablePostgres = "select %1$s from \"%2$s\" limit 100";
private static final String queryForSampleOnTablePostgres = "select %1$s from %2$s limit 100";
private static final String queryForSampleOnTableMysql = "select %1$s from %2$s limit 100";
// query to perform a smart sample operation randomly on the table
private static final String queryForSmartSampleOnTablePostgres = "select %1$s from \"%2$s\" order by random() limit 200";
// private static final String queryForSmartSampleOnTablePostgres = "select %1$s from \"%2$s\" order by random() limit 200";
private static final String queryForSmartSampleOnTablePostgres = "select %1$s from %2$s order by random() limit 200";
private static final String queryForSmartSampleOnTableMysql = "select %1$s from %2$s order by rand() limit 200";
// private static final String queryForSmartSampleOnTablePostgres =
// "select * from \"%1$s\" order by random() limit 200";
@ -31,7 +33,8 @@ public class Sampler {
// query to perform a smart sample operation on the table considering the
// threshold
private static final String queryForSmartSampleWithThresholdOnTablePostgres = "select %1$s from \"%2$s\" limit 200 offset %3$s";
// private static final String queryForSmartSampleWithThresholdOnTablePostgres = "select %1$s from \"%2$s\" limit 200 offset %3$s";
private static final String queryForSmartSampleWithThresholdOnTablePostgres = "select %1$s from %2$s limit 200 offset %3$s";
// private static final String queryForSmartSampleWithThresholdOnTableMysql
// = "select %1$s from %2$s limit 200 offset %3$s";
@ -40,7 +43,8 @@ public class Sampler {
// "select %1$s from \"%2$s\" order by random() limit 100";
// query to perform a smart sample operation on the table considering the
// threshold
private static final String queryForRandomSampleWithThresholdOnTablePostgres = "select %1$s from \"%2$s\" limit 100 offset %3$s";
// private static final String queryForRandomSampleWithThresholdOnTablePostgres = "select %1$s from \"%2$s\" limit 100 offset %3$s";
private static final String queryForRandomSampleWithThresholdOnTablePostgres = "select %1$s from %2$s limit 100 offset %3$s";
private static final String queryForRandomSampleOnTableMysql = "select %1$s from %2$s order by rand() limit 100";
private static final String queryForRandomSampleOnTablePostgres = "select %1$s from %2$s order by random() limit 100";
@ -91,7 +95,10 @@ public class Sampler {
// preparing the query
if (DBType.equals(POSTGRES)) {
//the full name equal to "schemaname.tablename"
tableName=schemaName+"."+tableName;
querySampleOnTable = String.format(queryForSampleOnTablePostgres,
listAttributes, tableName);
@ -99,6 +106,9 @@ public class Sampler {
if (DBType.equals(MYSQL)) {
//the full name equal to "dbname.tablename"
tableName=schemaName+"."+tableName;
querySampleOnTable = String.format(queryForSampleOnTableMysql,
listAttributes, tableName);
@ -498,13 +508,19 @@ public class Sampler {
// build the query for database postgres
if (DBType.equals(POSTGRES)) {
//the full name equal to "schemaname.tablename"
tablename=schemaName+"."+tablename;
query = String.format(queryForSmartSampleOnTablePostgres,
listAttributes, tablename);
}
// build the query for database mysql
if (DBType.equals(MYSQL)) {
//the full name equal to "dbname.tablename"
tablename=schemaName+"."+tablename;
query = String.format(queryForSmartSampleOnTableMysql,
listAttributes, tablename);
@ -886,6 +902,9 @@ public class Sampler {
// build the query for database postgres
if (DBType.equals(POSTGRES)) {
//the full name equal to "schemaname.tablename"
tablename=schemaName+"."+tablename;
query = String.format(
queryForSmartSampleWithThresholdOnTablePostgres,
listAttributes, tablename, indexes[i]);
@ -1203,6 +1222,9 @@ public class Sampler {
// order to solve a bug with the random function in postgres
if ((NumRows <= 700000) && (DBType.equals(POSTGRES))) { // Postgres
//the full name equal to "schemaname.tablename"
tableName=schemaName+"."+tableName;
querySampleOnTable = String.format(
queryForRandomSampleOnTablePostgres, listAttributes,
@ -1211,6 +1233,9 @@ public class Sampler {
}
if ((NumRows > 700000) && (DBType.equals(POSTGRES))) { // Postgres
//the full name equal to "schemaname.tablename"
tableName=schemaName+"."+tableName;
// generate an index randomly to execute the query
@ -1254,6 +1279,9 @@ public class Sampler {
}
if (DBType.equals(MYSQL)) { // MySQL
//the full name equal to "dbname.tablename"
tableName=schemaName+"."+tableName;
querySampleOnTable = String
.format(queryForRandomSampleOnTableMysql, listAttributes,

View File

@ -23,6 +23,7 @@ public class MySQLTableStructure extends AbstractTableStructure {
// Retrieve the query
String queryForIndexes = getQueryForIndexes(dbSession);
try {
@ -35,6 +36,9 @@ public class MySQLTableStructure extends AbstractTableStructure {
// String createTableStatement = (String) (((Object[])
// indexSet.get(0))[1]);
//the full name equal to "dbname.tablename"
tableName = this.databaseName+"."+tableName;
List<Object> result = connection.executeQuery(
String.format(queryForIndexes, tableName), dbSession);
@ -79,7 +83,9 @@ public class MySQLTableStructure extends AbstractTableStructure {
throws Exception {
// TODO Auto-generated method stub
String queryForIndexes = "SHOW CREATE TABLE `%1$s`;";
// String queryForIndexes = "SHOW CREATE TABLE `%1$s`;";
String queryForIndexes = "SHOW CREATE TABLE %1$s;";
return queryForIndexes;

View File

@ -610,12 +610,13 @@ public class DatabaseManagement {
}
// Method that returns the estimated number of rows
public long getNumberOfRows(String tablename) throws Exception {
public long getNumberOfRows(String tablename, String schemaName) throws Exception {
long rows;
rows = op.calculateElements(connection, DBType, tablename,
rows = op.calculateElements(connection, DBType, tablename, schemaName,
sourceDBSession);
AnalysisLogger.getLogger().debug(
"DatabaseManagement->rows' number calculated: " + rows);
@ -654,7 +655,7 @@ public class DatabaseManagement {
if (estimatedRows == 0) {
// estimatedRows = Integer.valueOf(getNumberOfRows(tableName));
estimatedRows = getNumberOfRows(tableName);
estimatedRows = getNumberOfRows(tableName, schemaName);
}
// to retrieve datatype columns of a table
@ -861,7 +862,7 @@ public class DatabaseManagement {
if (estimatedRows == 0) {
estimatedRows = getNumberOfRows(tableName);
estimatedRows = getNumberOfRows(tableName, schemaName);
}
Sampler sampler = new Sampler();

View File

@ -21,16 +21,20 @@ 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 QueryMysql = "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 `%1$s` limit 1";
// 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 explainQueryMysql = "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 MYSQL = "MySQL";
private static final String POSTGRES = "Postgres";
@ -104,7 +108,7 @@ public class DatabaseOperations {
// Method that calculate the estimated number of rows
public long calculateElements(ConnectionManager connection,
String dbType, String tablename, SessionFactory session)
String dbType, String tablename, String schemaName, SessionFactory session)
throws Exception {
long count = 0;
@ -112,11 +116,16 @@ public class DatabaseOperations {
String countingQuery = null;
if (dbType.equals(POSTGRES)) {
//the full name equal to "schemaname.tablename"
tablename=schemaName+"."+tablename;
countingQuery = String.format(QueryPostgres, tablename);
}
if (dbType.equals(MYSQL)) {
//the full name equal to "dbname.tablename"
tablename=schemaName+"."+tablename;
countingQuery = String.format(QueryMysql, tablename);
@ -164,12 +173,14 @@ public class DatabaseOperations {
String explain = null;
if (dbType.equals(POSTGRES)) {
explain = String.format(explainQueryPostgres, tablename);
}
if (dbType.equals(MYSQL)) {
explain = String.format(explainQueryMysql, tablename);
}