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

Added (a lots of) comments to code; unused methods cleanup

parent 942178f3
Pipeline #106 passed with stage
in 44 seconds
......@@ -52,7 +52,7 @@ public class Column extends ChildEntity<Table> {
/**
* Original datatype (computed from information_schema data), used for
* consistency checking inside the method
* {@link #it.inaf.ia2.tsm.datalayer.DBBroker.getAllColumnsMetadata()}
* {@link it.inaf.ia2.tsm.datalayer.DBBroker#getAllColumnsMetadata}
*
* @see it.inaf.ia2.tsm.ConsistencyChecks
*/
......
......@@ -26,8 +26,10 @@ import java.io.Serializable;
import java.util.Objects;
/**
* Models a column during the consistency checking phase (it is used to display
* information about columns not already loaded/added into the TAP_SCHEMA).
* Models the reference to a column in the database during the consistency
* checking phase (it is used to display information about columns not already
* loaded/added into the TAP_SCHEMA or to represent a column which doesn't exist
* yet).
*
* @see it.inaf.ia2.tsm.ConsistencyChecks
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
......
......@@ -113,7 +113,7 @@ public class Key extends TapSchemaEntity implements Serializable {
/**
* Sets the identifier for this key.
*
* @see {@code getId()}
* @see #getId
*/
public void setId(String id) {
setValue(ID_KEY, id);
......
......@@ -27,8 +27,9 @@ import java.io.Serializable;
/**
* Models an inconsistency in a column definition detected in an existing
* column. This happens when the column datatype read from the database metadata
* is different from what is expected according the {@link ColumnModel} defining
* that column. To fix this issue an {@code ALTER TABLE} is necessary.
* is different from what is expected according the
* {@link it.inaf.ia2.tsm.model.ColumnModel} defining that column. To fix this
* issue an {@code ALTER TABLE} is necessary.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
......@@ -77,8 +78,8 @@ public class WrongDataType implements Serializable {
}
/**
* Returns the expected datatype, according to the {@link ColumnModel}
* associated to this column.
* Returns the expected datatype, according to the
* {@link it.inaf.ia2.tsm.model.ColumnModel} associated to this column.
*/
public String getCorrectDataType() {
return correctDataType;
......
......@@ -57,6 +57,9 @@ public interface DBBroker {
/**
* Returns the name of all schemata which structure is compatible with a
* TAP_SCHEMA schema structure.
*
* @param allSchemas a list of all schemata names to use for searching;
* names must be real schemata names (not renamed).
*/
List<String> getAllTAPSchemaNames(List<String> allSchemas) throws SQLException;
......@@ -65,6 +68,7 @@ public interface DBBroker {
* structure (e.<!-- -->g.<!-- --> the presence of the set of columns
* defined into the related XML model).
*
* @param tapSchemaName the real TAP_SCHEMA name (not the renamed one).
* @return the name of the guessed TAP_SCHEMA version.
*/
String detectVersion(String tapSchemaName) throws SQLException;
......@@ -72,11 +76,15 @@ public interface DBBroker {
/**
* Returns a list of all the schemata exposed by an existing TAP_SCHEMA.
* Queries the {@code TAP_SCHEMA.schemas} schema.
*
* @param tapSchemaName the real TAP_SCHEMA name (not the renamed one).
*/
List<String> getExposedSchemas(String tapSchemaName) throws SQLException;
/**
* Returns all the tables owned by a schema, given its name.
*
* @param schemaName the real schema name (not the renamed one).
*/
List<String> getAllTablesNames(String schemaName) throws SQLException;
......@@ -84,6 +92,7 @@ public interface DBBroker {
* Returns the table type ("table" or "view") for all the tables owned by a
* schema, given its name.
*
* @param schemaName the real schema name (not the renamed one).
* @return a {@code Map} having table names as keys and table types as
* values.
*/
......@@ -91,13 +100,16 @@ public interface DBBroker {
/**
* Returns the list of the names of the columns contained in a given table.
*
* @param schemaName the real schema name (not the renamed one).
* @param tableName the name of the table.
*/
List<String> getAllColumnsNames(String schemaName, String tableName) throws SQLException;
/**
* Returns the metadata of all the columns contained in a given table.
*
* @param schemaName the name of the schema.
* @param schemaName the real schema name (not the renamed one).
* @param tableSimpleName the name of the table.
* @param tableModel the {@link TableModel} of the owner column if it is a
* TAP_SCHEMA table or the ObsCore table, null otherwise. This is used for
......@@ -110,37 +122,157 @@ public interface DBBroker {
*/
Map<String, Map<String, Object>> getAllColumnsMetadata(String schemaName, String tableSimpleName, TableModel tableModel, DataTypeMode dataTypeMode) throws SQLException;
/**
* Returns a list of all the foreign key relationships the columns of which
* are contained in a given schema.
*
* @param schemaName the schema name as exposed by the TAP_SCHEMA
* (eventually renamed).
* @param realSchemaName the real schema name (not renamed).
*/
List<Key> getKeys(TapSchema tapSchema, String schemaName, String realSchemaName) throws SQLException;
List<Map<String, Object>> getSavedItems(String tapSchemaName, TableModel tableModel, String whereCondition, Object[] whereParams) throws SQLException;
/**
* Retrieves TAP_SCHEMA items saved into an existing TAP_SCHEMA, for a given
* TAP_SCHEMA table, given its {@link TableModel}. For example, if the
* {@code TableModel} models the {@code columns} table, this method will
* return a list representing all the rows in the {@code columns} table.
*
* @param tapSchemaName the real TAP_SCHEMA name (not the renamed one).
* @param tableModel the model for the TAP_SCHEMA table to read.
* @return A {@code List} representing all the rows in a specific TAP_SCHEMA
* table. Each element of the list is a {@code Map} having column names as
* keys and row values as values.
*/
List<Map<String, Object>> getSavedItems(String tapSchemaName, TableModel tableModel) throws SQLException;
/**
* Inserts a new row in a TAP_SCHEMA table.
*
* @param tapSchemaName the real TAP_SCHEMA name (not the renamed one).
* @param entity the entity to insert (this is used to specify the table to
* use and the values to insert).
* @param conn the database connection.
*/
void insertItem(String tapSchemaName, TapSchemaEntity entity, Connection conn) throws SQLException;
/**
* Updates an existing row in a TAP_SCHEMA table.
*
* @param tapSchemaName the real TAP_SCHEMA name (not the renamed one).
* @param entity the entity to update (this is used to specify the table to
* use and the new values to insert).
* @param conn the database connection.
* @param whereCondition a SQL {@code WHERE} condition to add to the query.
* @param whereParams a sequence of parameters for the {@code WHERE}
* condition.
*/
void updateItem(String tapSchemaName, TapSchemaEntity entity, Connection conn, String whereCondition, Object... whereParams) throws SQLException;
/**
* Creates all the tables of a TAP_SCHEMA.
*
* @param tapSchemaName the real TAP_SCHEMA name (not the renamed one).
* @param tapSchemaModel the {@link SchemaModel} used to represent the
* TAP_SCHEMA schema.
*/
void createTapSchemaStructure(String tapSchemaName, SchemaModel tapSchemaModel) throws SQLException;
/**
* Creates all the tables of an ivoa schema.
*
* @param ivoaSchemaModel the {@link SchemaModel} used to represent the ivoa
* schema.
* @param realIvoaSchemaName the real name of the ivoa schema (not the
* renamed one).
*/
void createIvoaSchemaStructure(SchemaModel ivoaSchemaModel, String realIvoaSchemaName) throws SQLException;
/**
* Saves all the TAP_SCHEMA modifications into the database (creates a new
* TAP_SCHEMA or update an existing one).
*/
void save(TapSchema tapSchema) throws SQLException;
/**
* Create a new table into the database.
*
* @param schemaName the real schema name (not the renamed one).
* @param tableModel the {@link TableModel} representing the table to
* create.
*/
void createTable(String schemaName, TableModel tableModel) throws SQLException;
/**
* Alter an existing table adding a new column.
*
* @param columnHolder a model representing a reference to a column which
* doesn't exist yet.
* @param columnModel the model used for representing the column structure.
*/
void addColumn(ColumnHolder columnHolder, ColumnModel columnModel) throws SQLException;
/**
* @param like is for using both the schema name and the complete table name
* in the query
* Retrieves all the keys where the {@code from_table} or
* {@code target_table} properties start by a given parameter, which must be
* a schema name or a complete table name ({@code schema_name} plus
* {@code table_name}). In this way it is possible to select keys that needs
* to be removed both if the consistency checking mechanism has detected an
* inexistent schema or an inexistent table.
*
* @param tapSchemaName the real TAP_SCHEMA name (not the renamed one).
* @param like a string representing a schema name or a table complete name
* ({@code schema_name} plus {@code table_name}).
*
* @see it.inaf.ia2.tsm.ConsistencyChecks
*/
Set<String> getKeysToRemove(String tapSchemaName, String like) throws SQLException;
/**
* Deletes rows from the TAP_SCHEMA.
*
* @param keysToRemoveIds a {@code Set} of all the identifiers of the keys
* to remove; the logic used to create this set is inside the
* {@link it.inaf.ia2.tsm.TapSchemaMender} class.
*
* @see it.inaf.ia2.tsm.ConsistencyChecks
* @see it.inaf.ia2.tsm.TapSchemaMender
*/
void deleteUnexistingEntities(String tapSchemaName, ConsistencyChecks consistencyChecks, Set<String> keysToRemoveIds) throws SQLException;
/**
* Retrieves all the key identifiers related to an inexistent column.
*
* @param tapSchemaName the real TAP_SCHEMA name (not the renamed one).
* @param unexistingColumn a reference to the inexistent column.
* @return a {@code Set} of key identifier
*
* @see it.inaf.ia2.tsm.ConsistencyChecks
* @see it.inaf.ia2.tsm.TapSchemaMender
*/
Set<String> getKeysToRemoveFromUnexistingColumn(String tapSchemaName, ColumnHolder unexistingColumn) throws SQLException;
/**
* Update the (wrong) property value of column exposed by a TAP_SCHEMA.
*
* @param tapSchemaName the real TAP_SCHEMA name (not the renamed one).
* @param completeTableName the complete name of the table
* ({@code schema_name} plus {@code table_name}).
* @param columnName the name of the column.
* @param key the name of the TAP_SCHEMA column property.
* @param value the new value.
*
* @see it.inaf.ia2.tsm.ConsistencyChecks
* @see it.inaf.ia2.tsm.TapSchemaMender
*/
void updateTapSchemaColumnValue(String tapSchemaName, String completeTableName, String columnName, String key, Object value) throws SQLException;
/**
* Alter an existing TAP_SCHEMA or ObsCore column having an incoherent
* datatype according to its {@link ColumnModel}.
*
* @see it.inaf.ia2.tsm.ConsistencyChecks
* @see it.inaf.ia2.tsm.TapSchemaMender
*/
void alterDataType(String schemaName, String tableName, String columnName, String adqlDataType, Integer size) throws SQLException;
}
......@@ -26,11 +26,17 @@ import it.inaf.ia2.tsm.datalayer.mysql.MySQLDBBroker;
import it.inaf.ia2.tsm.datalayer.pgsql.PostgresDBBroker;
/**
* Utility class for retrieving the desired {@link DBBroker} implementation.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class DBBrokerFactory {
/**
* Retrieves the desired {@link DBBroker}, instantiating the proper
* implementation according to the {@link DatabaseType} defined into the
* {@link DataSourceWrapper} passed as parameter.
*/
public static DBBroker getDBBroker(DataSourceWrapper dataSourceWrapper, DataTypeMode dataTypeMode) {
switch (dataSourceWrapper.getDatabaseType()) {
case MYSQL:
......
......@@ -57,6 +57,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Template class containing all {@link DBBroker} features that are not vendor
* specific.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
......@@ -116,6 +118,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
sb.append(String.format("(%s)", size));
}
/**
* {@inheritDoc}
*/
@Override
public void createTable(String schemaName, TableModel tableModel) throws SQLException {
try (Connection conn = dataSource.getConnection()) {
......@@ -146,6 +151,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
}
}
/**
* {@inheritDoc}
*/
@Override
public void createTapSchemaStructure(String tapSchemaName, SchemaModel tapSchemaModel) throws SQLException {
......@@ -181,6 +189,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
}
}
/**
* {@inheritDoc}
*/
@Override
public void createIvoaSchemaStructure(SchemaModel ivoaSchemaModel, String realIvoaSchemaName) throws SQLException {
try (Connection conn = dataSource.getConnection()) {
......@@ -191,6 +202,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
}
}
/**
* {@inheritDoc}
*/
@Override
public void save(TapSchema tapSchema) throws SQLException {
......@@ -408,8 +422,11 @@ public abstract class DBBrokerTemplate implements DBBroker {
}
}
/**
* {@inheritDoc}
*/
@Override
public List<Map<String, Object>> getSavedItems(String tapSchemaName, TableModel tableModel, String whereCondition, Object[] whereParams) throws SQLException {
public List<Map<String, Object>> getSavedItems(String tapSchemaName, TableModel tableModel) throws SQLException {
StringBuilder querySb = new StringBuilder("SELECT ");
......@@ -428,7 +445,6 @@ public abstract class DBBrokerTemplate implements DBBroker {
querySb.append(".");
querySb.append(escape(tableModel.getName()));
// TODO: Manage where condition
String query = querySb.toString();
LOG.debug("Executing query {}", query);
......@@ -455,11 +471,6 @@ public abstract class DBBrokerTemplate implements DBBroker {
}
}
@Override
public List<Map<String, Object>> getSavedItems(String tapSchemaName, TableModel tableModel) throws SQLException {
return getSavedItems(tapSchemaName, tableModel, null, null);
}
private int getSQLType(Class type) {
if (type == String.class) {
return Types.VARCHAR;
......@@ -474,6 +485,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
}
}
/**
* {@inheritDoc}
*/
@Override
public void insertItem(String tapSchemaName, TapSchemaEntity tapSchemaItem, Connection conn) throws SQLException {
......@@ -530,6 +544,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
}
}
/**
* {@inheritDoc}
*/
@Override
public void updateItem(String tapSchemaName, TapSchemaEntity tapSchemaItem, Connection conn, String whereCondition, Object... whereParams) throws SQLException {
......@@ -587,6 +604,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
protected abstract String getSchemaTablesQuery(String schemaName);
/**
* {@inheritDoc}
*/
@Override
public List<String> getAllTAPSchemaNames(List<String> allSchemata) throws SQLException {
......@@ -645,6 +665,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
return allTAPSchemas;
}
/**
* {@inheritDoc}
*/
protected abstract String getColumnNamesQuery(String tapSchemaName, String tableName);
private List<String> getColumns(String tapSchemaName, String tableName, Connection connection) throws SQLException {
......@@ -678,6 +701,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
return true;
}
/**
* {@inheritDoc}
*/
@Override
public String detectVersion(String tapSchemaName) throws SQLException {
......@@ -706,6 +732,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
protected abstract String getTableTypesQuery(String schemaName);
/**
* {@inheritDoc}
*/
@Override
public Map<String, String> getAllTableTypes(String schemaName) throws SQLException {
LOG.debug("getTablesTypes");
......@@ -728,6 +757,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
return tablesTypes;
}
/**
* {@inheritDoc}
*/
@Override
public List<String> getExposedSchemas(String tapSchemaName) throws SQLException {
......@@ -750,6 +782,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
protected abstract String getAllSchemaNamesQuery();
/**
* {@inheritDoc}
*/
@Override
public List<String> getAllSchemaNames() throws SQLException {
String query = getAllSchemaNamesQuery();
......@@ -761,6 +796,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
protected abstract String getAllTablesNamesQuery(String schemaName);
/**
* {@inheritDoc}
*/
@Override
public List<String> getAllTablesNames(String schemaName) throws SQLException {
String query = getAllTablesNamesQuery(schemaName);
......@@ -770,6 +808,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
return allTables;
}
/**
* {@inheritDoc}
*/
@Override
public List<String> getAllColumnsNames(String schemaName, String tableName) throws SQLException {
String query = getColumnNamesQuery(schemaName, tableName);
......@@ -780,6 +821,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
protected abstract Map<String, Map<String, Object>> getAllColumnsOriginalMetadata(String schemaName, String tableName) throws SQLException;
/**
* {@inheritDoc}
*/
@Override
public Map<String, Map<String, Object>> getAllColumnsMetadata(String schemaName, String tableName, TableModel tableModel, DataTypeMode dataTypeMode) throws SQLException {
......@@ -812,6 +856,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
protected abstract String getDataTypeFromADQLType(String adqlType);
/**
* {@inheritDoc}
*/
@Override
public void addColumn(ColumnHolder columnHolder, ColumnModel columnModel) throws SQLException {
......@@ -832,6 +879,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
}
}
/**
* {@inheritDoc}
*/
@Override
public Set<String> getKeysToRemove(String tapSchemaName, String like) throws SQLException {
......@@ -855,6 +905,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
return keysId;
}
/**
* {@inheritDoc}
*/
@Override
public Set<String> getKeysToRemoveFromUnexistingColumn(String tapSchemaName, ColumnHolder unexistingColumn) throws SQLException {
......@@ -895,6 +948,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
return keysToRemoveIds;
}
/**
* {@inheritDoc}
*/
@Override
public void deleteUnexistingEntities(String tapSchemaName, ConsistencyChecks consistencyChecks, Set<String> keysToRemoveIds) throws SQLException {
try (Connection conn = dataSource.getConnection()) {
......@@ -995,6 +1051,9 @@ public abstract class DBBrokerTemplate implements DBBroker {
}
}
/**
* {@inheritDoc}
*/
@Override
public void updateTapSchemaColumnValue(String tapSchemaName, String completeTableName, String columnName, String key, Object value) throws SQLException {
......
......@@ -37,7 +37,7 @@ import org.slf4j.LoggerFactory;
* {@code ObsCore} table, is stored into the source database (because one could
* perform SQL JOINS between astronomical data schemata and the ObsCore table).
* <p>
* An user asks, for example, {@link getSourceDataSource()}, and the
* An user asks, for example, {@link #getSourceDataSource()}, and the
* {@code DBWrapper} returns the correct {@code DataSource}, both if it is
* separated from the TAP_SCHEMA {@code DataSource} or if they are the same
* object.
......
......@@ -23,12 +23,24 @@
package it.inaf.ia2.tsm.datalayer;
/**
* This enum is used to specify the syntax used by a TAP_SCHEMA for representing
* the data types.
* <p>
* The {@code DataTypeMode} for a given TAP_SCHEMA is specified into the XML
* configuration as an attribute of the schema element.
*
* @see it.inaf.ia2.tsm.model.SchemaModel
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public enum DataTypeMode {
/**
* For TAP 1.0: VARCHAR, SMALLINT, INTEGER, BIGINT, BLOB, ...
*/
ADQL("ADQL"),
/**
* For TAP 1.1: char, short, int, long, unsignedByte, ...
*/
VOTABLE("VOTable");
private final String name;
......
......@@ -44,7 +44,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* MySQL implementation of a {@link it.inaf.ia2.tsm.datalayer.DBBroker}.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class MySQLDBBroker extends DBBrokerTemplate {
......@@ -91,6 +92,9 @@ public class MySQLDBBroker extends DBBrokerTemplate {
return null;
}
/**
* {@inheritDoc}
*/
@Override
public Map<String, Map<String, Object>> getAllColumnsOriginalMetadata(String schemaName, String tableName) throws SQLException {
......@@ -124,7 +128,7 @@ public class MySQLDBBroker extends DBBrokerTemplate {
// Datatype and Size
String dbType = resultSet.getString("Type").toUpperCase();
ADQL adqlType = TypesMapping.getADQLFromDBType(dbType);
ADQL adqlType = TypesMapping.getADQLFromMySQLType(dbType);
String datatype = TypesMapping.getDataTypeFromMySQLType(dbType, getDataTypeMode());
Integer size = getSize(dbType);
......@@ -210,6 +214,9 @@ public class MySQLDBBroker extends DBBrokerTemplate {
buildColumnsList(fromKeyColumns), escape(tapSchemaName), escape(targetTableName), buildColumnsList(toKeyColumns));
}