Commit 942178f3 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

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

parent 103d837b
Pipeline #105 passed with stage
in 22 seconds
...@@ -49,7 +49,13 @@ public class Column extends ChildEntity<Table> { ...@@ -49,7 +49,13 @@ public class Column extends ChildEntity<Table> {
public final static String COLUMN_INDEX = "column_index"; // TAP version >= 1.1 public final static String COLUMN_INDEX = "column_index"; // TAP version >= 1.1
public final static String DBNAME = "dbname"; public final static String DBNAME = "dbname";
// Original datatype (computed from information_schema data), used for consistency checking /**
* Original datatype (computed from information_schema data), used for
* consistency checking inside the method
* {@link #it.inaf.ia2.tsm.datalayer.DBBroker.getAllColumnsMetadata()}
*
* @see it.inaf.ia2.tsm.ConsistencyChecks
*/
public final static String ORIGINAL_DATATYPE_KEY = "original_datatype"; public final static String ORIGINAL_DATATYPE_KEY = "original_datatype";
private static final long serialVersionUID = 9175956487892235521L; private static final long serialVersionUID = 9175956487892235521L;
......
...@@ -206,7 +206,33 @@ public class Key extends TapSchemaEntity implements Serializable { ...@@ -206,7 +206,33 @@ public class Key extends TapSchemaEntity implements Serializable {
@Override @Override
public String toString() { public String toString() {
return TSMUtil.printKeyInfo(this); StringBuilder sb = new StringBuilder();
sb.append(String.format("[%s] %s(", getId(), getFromTableCompleteName()));
boolean first = true;
for (KeyColumn keyColumn : keyColumns) {
if (!first) {
sb.append(",");
}
first = false;
sb.append(keyColumn.getFromColumn());
}
sb.append(String.format(") -> %s(", getTargetTableCompleteName()));
first = true;
for (KeyColumn keyColumn : keyColumns) {
if (!first) {
sb.append(",");
}
first = false;
sb.append(keyColumn.getTargetColumn());
}
sb.append(")");
return sb.toString();
} }
@Override @Override
......
...@@ -25,9 +25,11 @@ package it.inaf.ia2.tsm; ...@@ -25,9 +25,11 @@ package it.inaf.ia2.tsm;
import java.io.Serializable; import java.io.Serializable;
/** /**
* Model used during consistency checking phase to represents a * Model used during consistency checking phase to represents a key that has
* * been stored into the TAP_SCHEMA but is not currently existing according to
* * the information read from the database metadata.
*
* @see it.inaf.ia2.tsm.ConsistencyChecks
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>} * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/ */
public class KeyHolder implements Serializable { public class KeyHolder implements Serializable {
...@@ -52,18 +54,32 @@ public class KeyHolder implements Serializable { ...@@ -52,18 +54,32 @@ public class KeyHolder implements Serializable {
return keyId; return keyId;
} }
/**
* Returns the complete name of table (schema name plus table name) owning
* the foreign key.
*/
public String getFromTable() { public String getFromTable() {
return fromTable; return fromTable;
} }
/**
* Returns the columns composing the foreign key.
*/
public String[] getFromColumns() { public String[] getFromColumns() {
return fromColumns; return fromColumns;
} }
/**
* Returns the complete name of table (schema name plus table name) owning
* the primary key.
*/
public String getTargetTable() { public String getTargetTable() {
return targetTable; return targetTable;
} }
/**
* Returns the columns composing the primary key.
*/
public String[] getTargetColumns() { public String[] getTargetColumns() {
return targetColumns; return targetColumns;
} }
......
...@@ -33,7 +33,7 @@ import org.slf4j.Logger; ...@@ -33,7 +33,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* The main implementation of {@link Schema}. * Represents a Schema entity belonging to a {@link TapSchema}.
* *
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>} * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/ */
...@@ -48,12 +48,22 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer< ...@@ -48,12 +48,22 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer<
private final Map<String, Table> tables; private final Map<String, Table> tables;
private Map<String, String> tablesTypes; private Map<String, String> tablesTypes;
/**
* Only for serialization.
*/
private Schema() { private Schema() {
// for serialization
super(); super();
tables = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); tables = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
} }
/**
* Default constructor.
*
* @param tapSchema the {@code TapSchema} owning this schema.
* @param name the name of this schema.
* @throws SQLException if there a problem while retrieving information
* about schema tables.
*/
public Schema(TapSchema tapSchema, String name) throws SQLException { public Schema(TapSchema tapSchema, String name) throws SQLException {
super(tapSchema, tapSchema.getTableModel(TapSchema.SCHEMAS_TABLE), tapSchema.getSchemaMetadata(name)); super(tapSchema, tapSchema.getTableModel(TapSchema.SCHEMAS_TABLE), tapSchema.getSchemaMetadata(name));
...@@ -70,6 +80,14 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer< ...@@ -70,6 +80,14 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer<
setStatus(Status.LOADED); setStatus(Status.LOADED);
} }
/**
* Returns the name of the schema as seen by the database. The real schema
* name can be different from the exposed schema name if the schema renaming
* feature has been set. Schema renaming is supported by taplib adding an
* additional column {@code dbname} to the TAP_SCHEMA {@code schema},
* {@code tables} and {@code columns} tables. Not all the TASMAN schema
* definitions includes the {@code dbname} column.
*/
public final String getRealSchemaName() { public final String getRealSchemaName() {
if (tapSchema.getDBName() != null && this.getName().equals(tapSchema.getName())) { if (tapSchema.getDBName() != null && this.getName().equals(tapSchema.getName())) {
return tapSchema.getDBName(); return tapSchema.getDBName();
...@@ -89,6 +107,8 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer< ...@@ -89,6 +107,8 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer<
} }
/** /**
* {@inheritDoc}
*
* @param tableSimpleName the name of the table, without the schema name. * @param tableSimpleName the name of the table, without the schema name.
*/ */
@Override @Override
...@@ -185,7 +205,6 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer< ...@@ -185,7 +205,6 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer<
return TSMUtil.getAddableChildrenNames(tables); return TSMUtil.getAddableChildrenNames(tables);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
...@@ -249,6 +268,14 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer< ...@@ -249,6 +268,14 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer<
return tapSchema; return tapSchema;
} }
/**
* Returns the metadata {@code Map} for a {@link Table} having this schema
* as parent.
*
* @param tableSimpleName the name of the table without its schema name.
* @return a {@code Map} having table property names as keys and table
* property values as values.
*/
public Map<String, Object> getTableMetadata(String tableSimpleName) { public Map<String, Object> getTableMetadata(String tableSimpleName) {
Map<String, Object> metadata = new HashMap<>(); Map<String, Object> metadata = new HashMap<>();
metadata.put(Table.SCHEMA_NAME_KEY, getName()); metadata.put(Table.SCHEMA_NAME_KEY, getName());
......
...@@ -22,20 +22,17 @@ ...@@ -22,20 +22,17 @@
*/ */
package it.inaf.ia2.tsm; package it.inaf.ia2.tsm;
import it.inaf.ia2.tsm.datalayer.DatabaseType;
import it.inaf.ia2.tsm.datalayer.DBWrapper;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* Utility class that contains some static methods to manage various operations * Utility class containing some static methods to manage various operations
* with the TAP_SCHEMA entities. * with the TAP_SCHEMA entities.
* *
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>} * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
...@@ -132,143 +129,6 @@ public class TSMUtil { ...@@ -132,143 +129,6 @@ public class TSMUtil {
return ret; return ret;
} }
/**
* Same as {@link DLUtil.getObject(ResultSet, String, Class<T>)}.
*/
public static <T> T getObject(ResultSet rs, int i, Class<T> type) throws SQLException {
T ret;
if (type == String.class) {
ret = (T) rs.getString(i);
} else if (type == Integer.class) {
ret = (T) (Integer) rs.getInt(i);
} else if (type == Long.class) {
ret = (T) (Long) rs.getLong(i);
} else if (type == Boolean.class) {
ret = (T) (Boolean) rs.getBoolean(i);
} else {
throw new UnsupportedOperationException("Type " + type.getCanonicalName() + " not supported by " + TSMUtil.class.getCanonicalName() + " getObject() method");
}
if (rs.wasNull()) {
return null;
}
return ret;
}
protected static DataSource getSchemaDataSource(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName) {
return schemaName.equals(tapSchema.getName()) ? dbWrapper.getTapSchemaDataSource() : dbWrapper.getSourceDataSource();
}
protected static DatabaseType getSchemaDatabaseType(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName) {
return schemaName.equals(tapSchema.getName()) ? dbWrapper.getTapSchemaDatabaseType() : dbWrapper.getSourceDatabaseType();
}
protected static boolean isTapSchema(TapSchema tapSchema, String schemaName) {
return schemaName.equals(tapSchema.getName());
}
protected static UnsupportedOperationException getUnsupportedOperationException(String version, String unsupportedFeature) {
return new UnsupportedOperationException("Version \"" + version + "\" doesn't support " + unsupportedFeature);
}
protected static String escapeName(String name, DatabaseType dbType) {
char escapeChar;
switch (dbType) {
case MYSQL:
escapeChar = '`';
break;
case POSTGRES:
escapeChar = '"';
break;
default:
throw new UnsupportedOperationException("Database type " + dbType + " not supported");
}
return String.format("%s%s%s", escapeChar, name, escapeChar);
}
protected static String getTapSchemaTableNameFromEntity(TapSchemaEntity entity) {
if (entity instanceof Schema) {
return TapSchema.SCHEMAS_TABLE;
} else if (entity instanceof Table) {
return TapSchema.TABLES_TABLE;
} else if (entity instanceof Column) {
return TapSchema.COLUMNS_TABLE;
} else if (entity instanceof Key) {
return TapSchema.KEYS_TABLE;
} else if (entity instanceof KeyColumn) {
return TapSchema.KEY_COLUMNS_TABLE;
}
LOG.warn("getTapSchemaTableNameFromEntity returns null for {}" + entity.getClass().getCanonicalName());
return null;
}
public static String getNaturalLanguageName(TapSchemaEntity entity) {
if (entity instanceof Schema) {
return "schema";
} else if (entity instanceof Table) {
return "table";
} else if (entity instanceof Column) {
return "column";
} else if (entity instanceof Key) {
return "key";
} else if (entity instanceof KeyColumn) {
return "key_column";
} else {
throw new UnsupportedOperationException("entity class " + entity.getClass().getCanonicalName() + " not supported yet");
}
}
public static String getName(TapSchemaEntity entity) {
if (entity instanceof Schema) {
return ((Schema) entity).getName();
} else if (entity instanceof Table) {
return ((Table) entity).getCompleteName();
} else if (entity instanceof Column) {
Column column = (Column) entity;
return column.getParent().getCompleteName() + "." + column.getName();
} else if (entity instanceof Key) {
return printKeyInfo((Key) entity);
} else if (entity instanceof KeyColumn) {
KeyColumn keyColumn = (KeyColumn) entity;
return String.format("%s -> %s [key: %s]",
keyColumn.getFromColumn(), keyColumn.getTargetColumn(),
printKeyInfo(keyColumn.getParent()));
} else {
throw new UnsupportedOperationException("entity class " + entity.getClass().getCanonicalName() + " not supported yet");
}
}
public static String printKeyInfo(Key key) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("[%s] %s(", key.getId(), key.getFromTableCompleteName()));
boolean first = true;
for (KeyColumn keyColumn : key.getKeyColumns()) {
if (!first) {
sb.append(",");
}
first = false;
sb.append(keyColumn.getFromColumn());
}
sb.append(String.format(") -> %s(", key.getTargetTableCompleteName()));
first = true;
for (KeyColumn keyColumn : key.getKeyColumns()) {
if (!first) {
sb.append(",");
}
first = false;
sb.append(keyColumn.getTargetColumn());
}
sb.append(")");
return sb.toString();
}
/** /**
* Utility class for joining a collection of object properties. * Utility class for joining a collection of object properties.
*/ */
......
...@@ -304,9 +304,9 @@ public class Table extends ChildEntity<Schema> implements EntitiesContainer<Colu ...@@ -304,9 +304,9 @@ public class Table extends ChildEntity<Schema> implements EntitiesContainer<Colu
} }
/** /**
* Returns the metadata about a column children of this table. Metadata are * Returns the metadata about a column having this table as parent. Metadata
* column properties the value of which can be retrieved from the database * are column properties the value of which can be retrieved from the
* metadata (or information_schema). * database metadata (or information_schema).
* *
* @param columnName the name of the column of which retrieve the metadata. * @param columnName the name of the column of which retrieve the metadata.
* @return a {@code Map} the keys of which are property names and the values * @return a {@code Map} the keys of which are property names and the values
......
...@@ -32,7 +32,7 @@ import java.util.Map; ...@@ -32,7 +32,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
* This fixes TAP_SCHEMA inconsistencies. * Handles fixes of TAP_SCHEMA inconsistencies.
* *
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>} * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/ */
...@@ -46,6 +46,9 @@ public class TapSchemaMender { ...@@ -46,6 +46,9 @@ public class TapSchemaMender {
consistencyChecks = tapSchema.getConsistencyChecks(); consistencyChecks = tapSchema.getConsistencyChecks();
} }
/**
* Fixes inconsistencies of a just loaded TAP_SCHEMA.
*/
public static void amendTapSchema(TapSchema tapSchema) throws SQLException { public static void amendTapSchema(TapSchema tapSchema) throws SQLException {
new TapSchemaMender(tapSchema).amendTapSchema(); new TapSchemaMender(tapSchema).amendTapSchema();
} }
......
...@@ -25,6 +25,7 @@ package it.inaf.ia2.tsm; ...@@ -25,6 +25,7 @@ package it.inaf.ia2.tsm;
import java.io.Serializable; import java.io.Serializable;
/** /**
* Contains all the settings specified for a TAP_SCHEMA.
* *
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>} * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/ */
...@@ -43,14 +44,28 @@ public class TapSchemaSettings implements Serializable { ...@@ -43,14 +44,28 @@ public class TapSchemaSettings implements Serializable {
ivoaSchemaName = TapSchema.STANDARD_IVOA_SCHEMA_NAME; ivoaSchemaName = TapSchema.STANDARD_IVOA_SCHEMA_NAME;
} }
/**
* TASMAN supports multiple TAP_SCHEMA versions; schemata structure change
* from one version to another and the definition of this structure is
* stored into XML files inside the {@code schema_definition} resource
* folder.
*/
public String getTapSchemaVersion() { public String getTapSchemaVersion() {
return tapSchemaVersion; return tapSchemaVersion;
} }
/**
* @see #getTapSchemaVersion()
*/
public void setTapSchemaVersion(String tapSchemaVersion) { public void setTapSchemaVersion(String tapSchemaVersion) {
this.tapSchemaVersion = tapSchemaVersion; this.tapSchemaVersion = tapSchemaVersion;
} }
/**
* Returns the name of the TAP_SCHEMA schema, as saved into the database.
* The name can be different from the exposed name if schema renaming is
* enabled.
*/
public String getTapSchemaName() { public String getTapSchemaName() {
return tapSchemaName; return tapSchemaName;
} }
...@@ -59,6 +74,9 @@ public class TapSchemaSettings implements Serializable { ...@@ -59,6 +74,9 @@ public class TapSchemaSettings implements Serializable {
this.tapSchemaName = tapSchemaName; this.tapSchemaName = tapSchemaName;
} }
/**
* Tells if the {@code ObsCore} table should be managed by TASMAN.
*/
public boolean isHasObscore() { public boolean isHasObscore() {
return hasObscore; return hasObscore;
} }
...@@ -75,6 +93,10 @@ public class TapSchemaSettings implements Serializable { ...@@ -75,6 +93,10 @@ public class TapSchemaSettings implements Serializable {
this.obscoreVersion = obscoreVersion; this.obscoreVersion = obscoreVersion;
} }
/**
* Returns the name of the ivoa schema, as saved into the database. The name
* can be different from the exposed name if schema renaming is enabled.
*/
public String getIvoaSchemaName() { public String getIvoaSchemaName() {
return ivoaSchemaName; return ivoaSchemaName;
} }
......
...@@ -28,12 +28,11 @@ import org.slf4j.Logger; ...@@ -28,12 +28,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* List of operations that have to be performed by the * Models the list of operations that have to be performed by the
* {@link it.inaf.ia2.tsm.TapSchema#save()} method, * {@link it.inaf.ia2.tsm.TapSchema#save()} method, in terms of adding, updating
* in terms of adding, updating or removing * or removing {@link it.inaf.ia2.tsm.TapSchemaEntity} entities. Could also be
* {@link it.inaf.ia2.tsm.TapSchemaEntity} entities. Could be used * used stand-alone to obtain a preview of the operations that will be performed
* stand-alone to obtain a preview of the operations that will be performed on * on the database.
* the database.
* *
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>} * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/ */
...@@ -41,8 +40,6 @@ public class UpdateOperations { ...@@ -41,8 +40,6 @@ public class UpdateOperations {
private static final Logger LOG = LoggerFactory.getLogger(UpdateOperations.class); private static final Logger LOG = LoggerFactory.getLogger(UpdateOperations.class);
private final TapSchema tapSchema;
private final List<Schema> schemasToRemove; private final List<Schema> schemasToRemove;
private final List<Schema> schemasToAdd; private final List<Schema> schemasToAdd;
private final List<Schema> schemasToUpdate; private final List<Schema> schemasToUpdate;
...@@ -71,9 +68,6 @@ public class UpdateOperations { ...@@ -71,9 +68,6 @@ public class UpdateOperations {
} }