Commit f6295e1a authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Big refactoring in order to allow multiple TAP versions (3)

parent 8c18e736
......@@ -91,7 +91,14 @@ public class Column extends ChildEntity<Table> {
}
public boolean getIndexed() {
return getValue(INDEXED_KEY, Boolean.class);
EntityProperty indexedProp = getProperty(INDEXED_KEY);
if (indexedProp.getType() == Boolean.class) {
return getValue(INDEXED_KEY, Boolean.class);
} else if (indexedProp.getType() == Integer.class) {
return getValue(INDEXED_KEY, Integer.class) == 1;
} else {
throw new RuntimeException("Indexed property cannot be an instance of " + indexedProp.getType());
}
}
/**
......
......@@ -40,6 +40,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* TODO: Move queries into data layer classes.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
......@@ -48,28 +50,62 @@ public class ConsistencyChecks implements Serializable {
private static final long serialVersionUID = 4412404312756740093L;
private final static Logger LOG = LoggerFactory.getLogger(ConsistencyChecks.class);
private static class UnexistingKeyColumn {
public static class UnexistingKey implements Serializable {
private static final long serialVersionUID = 7891439129072900628L;
private final String keyId;
private final String fromColumn;
private final String targetColumn;
private final String fromTable;
private final String[] fromColumns;
private final String targetTable;
private final String[] targetColumns;
private UnexistingKeyColumn(String keyId, String fromColumn, String targetColumn) {
private UnexistingKey(String keyId, String fromTable, String[] fromColumns, String targetTable, String[] targetColumns) {
this.keyId = keyId;
this.fromColumn = fromColumn;
this.targetColumn = targetColumn;
this.fromTable = fromTable;
this.fromColumns = fromColumns;
this.targetTable = targetTable;
this.targetColumns = targetColumns;
}
public String getKeyId() {
return keyId;
}
public String getFromColumn() {
return fromColumn;
public String getFromTable() {
return fromTable;
}
public String[] getFromColumns() {
return fromColumns;
}
public String getTargetColumn() {
return targetColumn;
public String getTargetTable() {
return targetTable;
}
public String[] getTargetColumns() {
return targetColumns;
}
private String getColumnsString(String[] columns) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String column : columns) {
if (!first) {
sb.append(",");
}
sb.append(column);
first = false;
}
return sb.toString();
}
@Override
public String toString() {
return String.format("[%s] %s(%s) -> %s(%s)", keyId,
fromTable, getColumnsString(fromColumns),
targetTable, getColumnsString(targetColumns));
}
}
......@@ -77,16 +113,14 @@ public class ConsistencyChecks implements Serializable {
private final Set<String> unexisingSchemas;
private final Set<String> unexisingTables;
private final Map<String, String> unexisingColumns;
private final Set<String> unexistingKeys;
private final List<UnexistingKeyColumn> unexistingKeyColumns;
private final List<UnexistingKey> unexistingKeys;
public ConsistencyChecks() {
inconsistencies = new ArrayList<>();
unexisingSchemas = new HashSet<>();
unexisingTables = new HashSet<>();
unexisingColumns = new HashMap<>();
unexistingKeys = new HashSet<>();
unexistingKeyColumns = new ArrayList<>();
unexistingKeys = new ArrayList<>();
}
public void addInconsistency(InconsistentValue problemDescription) {
......@@ -121,21 +155,17 @@ public class ConsistencyChecks implements Serializable {
unexisingColumns.put(completeTableName, columnName);
}
public void addUnexistingKey(String keyId) {
public void addUnexistingKey(String keyId, String fromTable, String[] fromColumns, String targetTable, String[] targetColumns) {
if (keyId == null) {
throw new IllegalArgumentException("key_id can't be null");
}
unexistingKeys.add(keyId);
unexistingKeys.add(new UnexistingKey(keyId, fromTable, fromColumns, targetTable, targetColumns));
}
public Set<String> getUnexistingKeys() {
public List<UnexistingKey> getUnexistingKeys() {
return unexistingKeys;
}
public void addUnexistingKeyColumn(String keyId, String fromColumn, String targetColumn) {
unexistingKeyColumns.add(new UnexistingKeyColumn(keyId, fromColumn, targetColumn));
}
private Set<String> getKeysToRemove(Connection conn, String tapSchemaNameEscaped, DatabaseType dbType, String like) throws SQLException {
Set<String> ret = new HashSet<>();
String query = String.format("SELECT key_id from %s.%s WHERE from_table LIKE ? OR target_table LIKE ?", tapSchemaNameEscaped, TSMUtil.escapeName("keys", dbType));
......@@ -197,7 +227,9 @@ public class ConsistencyChecks implements Serializable {
}
}
keysToRemoveIds.addAll(unexistingKeys);
for (UnexistingKey unexistingKey : unexistingKeys) {
keysToRemoveIds.add(unexistingKey.getKeyId());
}
conn.setAutoCommit(false);
LOG.debug("Starting transaction");
......
This diff is collapsed.
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2016 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package it.inaf.ia2.tsm;
import it.inaf.ia2.tsm.datalayer.DatabaseType;
import it.inaf.ia2.tsm.datalayer.DBWrapper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility class that contains static methods for managing {@link Column}s into
* the database.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class DaoColumn {
//
// private static final Logger log = LoggerFactory.getLogger(DaoColumn.class);
//
// private static boolean equalsOneOf(String string, String... values) {
// for (String value : values) {
// if (string.equals(value)) {
// return true;
// }
// }
// return false;
// }
//
// /**
// * Returns the list of all the columns names given a schema name and a table
// * name, also if these objects have never been added into the TAP_SCHEMA.
// * This can be useful to retrieve the source database structure.
// */
// public static List<String> getAllColumnsNames(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName, String tableSimpleName) throws SQLException {
// final List<String> allColumns = new ArrayList<>();
//
// DataSource dataSource = TSMUtil.getSchemaDataSource(dbWrapper, tapSchema, schemaName);
// DatabaseType dbType = TSMUtil.getSchemaDatabaseType(dbWrapper, tapSchema, schemaName);
//
// String query;
// if (dbType == DatabaseType.MYSQL) {
// query = String.format("SHOW COLUMNS FROM `%s`.`%s`", schemaName, tableSimpleName);
// } else if (dbType == DatabaseType.POSTGRES) {
// query = "SELECT column_name FROM information_schema.columns WHERE table_schema = '" + schemaName + "' AND table_name = '" + tableSimpleName + "'";
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// log.debug("Executing query {}", query);
//
// try (Connection connection = dataSource.getConnection();
// Statement statement = connection.createStatement();
// ResultSet resultSet = statement.executeQuery(query)) {
//
// while (resultSet.next()) {
//
// String columnName;
// if (dbType == DatabaseType.MYSQL) {
// columnName = resultSet.getString("Field");
// } else if (dbType == DatabaseType.POSTGRES) {
// columnName = resultSet.getString("column_name");
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// allColumns.add(columnName);
// }
// }
//
// return allColumns;
// }
//
// /**
// * For performance reasons all columns of a {@link Table} are loaded
// * together using this method. Columns {@link Status} is at first set as
// * {@code LOADED}.
// */
// protected static List<Column> loadAllTableColumns(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName, Table table) throws SQLException {
// String tableSimpleName = table.getName();
// final List<Column> allColumns = new ArrayList<>();
//
// DataSource dataSource = TSMUtil.getSchemaDataSource(dbWrapper, tapSchema, schemaName);
// DatabaseType dbType = TSMUtil.getSchemaDatabaseType(dbWrapper, tapSchema, schemaName);
//
// String query;
// if (dbType == DatabaseType.MYSQL) {
// query = String.format("SHOW COLUMNS FROM `%s`.`%s`", schemaName, tableSimpleName);
// } else if (dbType == DatabaseType.POSTGRES) {
// query = "SELECT c.column_name, c.data_type, r.contype AS column_type, c.character_maximum_length, c.numeric_precision\n" //, c.numeric_precision_radix
// + "FROM information_schema.columns c\n"
// + "JOIN pg_catalog.pg_tables t ON c.table_schema = t.schemaname AND c.table_name = t.tablename\n"
// + "LEFT JOIN pg_catalog.pg_constraint r ON c.ordinal_position = ANY(r.conkey) AND r.conrelid = (t.schemaname || '.' || t.tablename)::regclass::oid\n"
// + "WHERE t.schemaname = '" + schemaName + "' AND t.tablename = '" + tableSimpleName + "'";
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// log.debug("Executing query {}", query);
//
// try (Connection connection = dataSource.getConnection();
// Statement statement = connection.createStatement();
// ResultSet resultSet = statement.executeQuery(query)) {
//
// while (resultSet.next()) {
//
// String columnName;
// if (dbType == DatabaseType.MYSQL) {
// columnName = resultSet.getString("Field");
// } else if (dbType == DatabaseType.POSTGRES) {
// columnName = resultSet.getString("column_name");
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// boolean indexed = false, primaryKey = false;
//
// // Key type
// if (dbType == DatabaseType.MYSQL) {
// String key = resultSet.getString("Key");
// primaryKey = key.equals("PRI");
// indexed = equalsOneOf(key, "PRI", "UNI", "MUL");
// } else if (dbType == DatabaseType.POSTGRES) {
// String columnType = resultSet.getString("column_type");
// if (columnType != null) {
// primaryKey = "p".equals(columnType);
// indexed = equalsOneOf(columnType, "p", "f", "u");
// }
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// // Datatype and Size
// int size = 0;
// String datatype;
//
// if (dbType == DatabaseType.MYSQL) {
// String type = resultSet.getString("Type").toLowerCase();
//
// if (type.startsWith("int")) {
// datatype = "adql:INTEGER";
// } else if (type.startsWith("smallint")) {
// datatype = "adql:SMALLINT";
// } else if (type.startsWith("bigint")) {
// datatype = "adql:BIGINT";
// } else if (type.startsWith("float")) {
// datatype = "adql:REAL";
// } else if (type.startsWith("char")) {
// int beginIndex = type.indexOf('(');
// int endIndex = type.indexOf(')');
// size = Integer.parseInt(type.substring(beginIndex + 1, endIndex));
// datatype = "adql:CHAR";
// } else if (type.startsWith("varchar")) {
// int beginIndex = type.indexOf('(');
// int endIndex = type.indexOf(')');
// size = Integer.parseInt(type.substring(beginIndex + 1, endIndex));
// datatype = "adql:VARCHAR";
// } else if (type.contains("timestamp")) {
// datatype = "adql:TIMESTAMP";
// } else {
// datatype = "adql:" + type.toUpperCase();
// }
// } else if (dbType == DatabaseType.POSTGRES) {
// String type = resultSet.getString("data_type");
//
// if (type.startsWith("int")) {
// datatype = "adql:INTEGER";
// } else if (type.startsWith("smallint")) {
// datatype = "adql:SMALLINT";
// } else if (type.startsWith("bigint")) {
// datatype = "adql:BIGINT";
// } else if (type.startsWith("double") || type.startsWith("real")) {
// datatype = "adql:REAL";
// } else if (type.startsWith("character varying")) {
// datatype = "adql:VARCHAR";
// size = resultSet.getInt("character_maximum_length");
// } else if (type.startsWith("char")) {
// datatype = "adql:CHAR";
// size = resultSet.getInt("character_maximum_length");
// } else if (type.contains("timestamp")) {
// datatype = "adql:TIMESTAMP";
// } else {
// datatype = "adql:" + type.toUpperCase();
// }
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// Integer arraySize = null; // TODO (v 1.1)
//
// Column column = new Column(dbWrapper, tapSchema, table, columnName, indexed, primaryKey, datatype, size, arraySize);
//
// allColumns.add(column);
// }
// }
//
// return allColumns;
// }
//
// /**
// * Retrieves saved {@code Column}s from the database and add them into the
// * specified {@code TapSchema}.
// */
// protected static void fillSavedColumns(DBWrapper dbWrapper, final TapSchema tapSchema) throws SQLException {
//
// log.debug("fillSavedColumns");
//
// SelectQueryBuilder selectQueryBuilder = new SelectQueryBuilder(dbWrapper.getTapSchemaDatabaseType(), tapSchema, TapSchema.COLUMNS_TABLE) {
//
// @Override
// protected TapSchemaEntity getEntity(ResultSet rs) throws SQLException {
// String tableCompleteName = rs.getString("table_name");
// String columnName = rs.getString("column_name");
//
// String[] tableNameSplit = tableCompleteName.split(Pattern.quote("."));
// String schemaName = tableNameSplit[0];
// String tableSimpleName = tableNameSplit[1];
//
// Schema schema = tapSchema.getChild(schemaName);
// if (schema == null) {
// return null;
// }
// Table table = schema.getChild(tableSimpleName);
// if (table == null) {
// return null;
// }
// Column column = table.addChild(columnName);
// if (column == null) {
// return null;
// }
// column.setStatus(Status.ADDED_PERSISTED);
// return column;
// }
// };
//
// selectQueryBuilder.executeQuery(dbWrapper.getTapSchemaConnection());
// }
//
// /**
// * Save a new {@code Column} into the TAP_SCHEMA schema.
// */
// protected static void insertNewColumn(DatabaseType dbType, Connection connection, TapSchema tapSchema, Column column) throws SQLException {
//
// log.debug("insertNewColumn");
//
// InsertQueryBuilder insertQueryBuilder = new InsertQueryBuilder(dbType, tapSchema, column, TapSchema.COLUMNS_TABLE);
// insertQueryBuilder.executeQuery(connection);
// }
//
// /**
// * Updates an existing {@code Column}.
// */
// protected static void updateColumn(DatabaseType dbType, Connection connection, TapSchema tapSchema, Column column) throws SQLException {
//
// UpdateQueryBuilder updateQueryBuilder = new UpdateQueryBuilder(dbType, tapSchema, column, TapSchema.COLUMNS_TABLE, "column_name = ? AND table_name = ?");
//
// String query = updateQueryBuilder.getQuery();
//
// try (PreparedStatement statement = connection.prepareStatement(query)) {
//
// log.debug("Executing query {}", query);
//
// int i = updateQueryBuilder.addStatementValues(statement);
// statement.setString(i, column.getName());
// statement.setString(i + 1, column.getTableCompleteName());
//
// statement.executeUpdate();
// }
// }
}
This diff is collapsed.
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2016 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package it.inaf.ia2.tsm;
import it.inaf.ia2.tsm.datalayer.Credentials;
import it.inaf.ia2.tsm.datalayer.DatabaseType;
import it.inaf.ia2.tsm.datalayer.DBWrapper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility class that contains static methods for managing {@link Schema}s into
* the database.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class DaoSchema {
//
// private static final Logger log = LoggerFactory.getLogger(DaoSchema.class);
//
// public static List<String> getAllSchemasNames(Credentials credentials) throws SQLException {
// DataSource ds = TSMUtil.createDataSource(credentials);
// return getAllSchemasNames(ds, credentials.getDatabaseType());
// }
//
// /**
// * Retrieve the list of the names of the all the schemas contained into the
// * database specified by the <code>DataSource</code> parameter.
// *
// * @return list of schemas names alphabetically and case insensitively
// * ordered.
// */
// public static List<String> getAllSchemasNames(DataSource dataSource, DatabaseType dbType) throws SQLException {
//
// log.debug("getAllSchemasNames");
//
// String query;
// if (dbType == DatabaseType.MYSQL) {
// query = "SHOW DATABASES";
// } else if (dbType == DatabaseType.POSTGRES) {
// query = "SELECT schema_name FROM information_schema.schemata";
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// log.debug("Executing query {}", query);
//
// List<String> allSchemas = new ArrayList<>();
//
// try (Connection connection = dataSource.getConnection();
// Statement statement = connection.createStatement();
// ResultSet resultSet = statement.executeQuery(query)) {
// while (resultSet.next()) {
// allSchemas.add(resultSet.getString(1));
// }
// }
//
// log.debug("{} schemas found", allSchemas.size());
//
// return TSMUtil.sortStringsList(allSchemas);
// }
//
// /**
// * Retrieves saved {@code Schema}s from the database and add them into the
// * specified {@code TapSchema}.
// */
// protected static void fillSavedSchemas(DBWrapper dbWrapper, final TapSchema tapSchema) throws SQLException {
//
// log.debug("fillSavedSchemas");
//
// SelectQueryBuilder selectQueryBuilder = new SelectQueryBuilder(dbWrapper.getTapSchemaDatabaseType(), tapSchema, TapSchema.SCHEMAS_TABLE) {
//
// @Override
// protected TapSchemaEntity getEntity(ResultSet rs) throws SQLException {
// String schemaName = rs.getString("schema_name");
// Schema schema = tapSchema.addChild(schemaName);
// if (schema == null) {
// return null;
// }
// schema.setStatus(Status.ADDED_PERSISTED);
// return schema;
// }
// };
//
// selectQueryBuilder.executeQuery(dbWrapper.getTapSchemaConnection());
// }
//
// /**
// * Save a new {@code Schema} into the TAP_SCHEMA schema.
// */
// protected static void insertNewSchema(DatabaseType dbType, Connection connection, TapSchema tapSchema, Schema schema) throws SQLException {
//
// log.debug("insertNewSchema");
//
// InsertQueryBuilder insertQueryBuilder = new InsertQueryBuilder(dbType, tapSchema, schema, TapSchema.SCHEMAS_TABLE);
// insertQueryBuilder.executeQuery(connection);
// }
//
// /**
// * Updates an existing {@code Schema}.
// */
// protected static void updateSchema(DatabaseType dbType, Connection connection, TapSchema tapSchema, Schema schema) throws SQLException {
//
// UpdateQueryBuilder updateQueryBuilder = new UpdateQueryBuilder(dbType, tapSchema, schema, TapSchema.SCHEMAS_TABLE, "schema_name = ?");
//
// String query = updateQueryBuilder.getQuery();
//
// try (PreparedStatement statement = connection.prepareStatement(query)) {
//
// log.debug("Executing query {}", query);
//
// int i = updateQueryBuilder.addStatementValues(statement);
// statement.setString(i, schema.getName());