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> {
public final static String COLUMN_INDEX = "column_index"; // TAP version >= 1.1
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";
private static final long serialVersionUID = 9175956487892235521L;
......
......@@ -206,7 +206,33 @@ public class Key extends TapSchemaEntity implements Serializable {
@Override
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
......
......@@ -25,9 +25,11 @@ package it.inaf.ia2.tsm;
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>}
*/
public class KeyHolder implements Serializable {
......@@ -52,18 +54,32 @@ public class KeyHolder implements Serializable {
return keyId;
}
/**
* Returns the complete name of table (schema name plus table name) owning
* the foreign key.
*/
public String getFromTable() {
return fromTable;
}
/**
* Returns the columns composing the foreign key.
*/
public String[] getFromColumns() {
return fromColumns;
}
/**
* Returns the complete name of table (schema name plus table name) owning
* the primary key.
*/
public String getTargetTable() {
return targetTable;
}
/**
* Returns the columns composing the primary key.
*/
public String[] getTargetColumns() {
return targetColumns;
}
......
......@@ -33,7 +33,7 @@ import org.slf4j.Logger;
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>}
*/
......@@ -48,12 +48,22 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer<
private final Map<String, Table> tables;
private Map<String, String> tablesTypes;
/**
* Only for serialization.
*/
private Schema() {
// for serialization
super();
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 {
super(tapSchema, tapSchema.getTableModel(TapSchema.SCHEMAS_TABLE), tapSchema.getSchemaMetadata(name));
......@@ -70,6 +80,14 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer<
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() {
if (tapSchema.getDBName() != null && this.getName().equals(tapSchema.getName())) {
return tapSchema.getDBName();
......@@ -89,6 +107,8 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer<
}
/**
* {@inheritDoc}
*
* @param tableSimpleName the name of the table, without the schema name.
*/
@Override
......@@ -185,7 +205,6 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer<
return TSMUtil.getAddableChildrenNames(tables);
}
/**
* {@inheritDoc}
*/
......@@ -249,6 +268,14 @@ public class Schema extends ChildEntity<TapSchema> implements EntitiesContainer<
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) {
Map<String, Object> metadata = new HashMap<>();
metadata.put(Table.SCHEMA_NAME_KEY, getName());
......
......@@ -22,20 +22,17 @@
*/
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.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
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.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
......@@ -132,143 +129,6 @@ public class TSMUtil {
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.
*/
......
......@@ -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
* column properties the value of which can be retrieved from the database
* metadata (or information_schema).
* Returns the metadata about a column having this table as parent. Metadata
* are column properties the value of which can be retrieved from the
* database metadata (or information_schema).
*
* @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
......
......@@ -32,7 +32,7 @@ import java.util.Map;
import java.util.Set;
/**
* This fixes TAP_SCHEMA inconsistencies.
* Handles fixes of TAP_SCHEMA inconsistencies.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
......@@ -46,6 +46,9 @@ public class TapSchemaMender {
consistencyChecks = tapSchema.getConsistencyChecks();
}
/**
* Fixes inconsistencies of a just loaded TAP_SCHEMA.
*/
public static void amendTapSchema(TapSchema tapSchema) throws SQLException {
new TapSchemaMender(tapSchema).amendTapSchema();
}
......
......@@ -25,6 +25,7 @@ package it.inaf.ia2.tsm;
import java.io.Serializable;
/**
* Contains all the settings specified for a TAP_SCHEMA.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
......@@ -43,14 +44,28 @@ public class TapSchemaSettings implements Serializable {
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() {
return tapSchemaVersion;
}
/**
* @see #getTapSchemaVersion()
*/
public void setTapSchemaVersion(String 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() {
return tapSchemaName;
}
......@@ -59,6 +74,9 @@ public class TapSchemaSettings implements Serializable {
this.tapSchemaName = tapSchemaName;
}
/**
* Tells if the {@code ObsCore} table should be managed by TASMAN.
*/
public boolean isHasObscore() {
return hasObscore;
}
......@@ -75,6 +93,10 @@ public class TapSchemaSettings implements Serializable {
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() {
return ivoaSchemaName;
}
......
......@@ -28,12 +28,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* List of operations that have to be performed by the
* {@link it.inaf.ia2.tsm.TapSchema#save()} method,
* in terms of adding, updating or removing
* {@link it.inaf.ia2.tsm.TapSchemaEntity} entities. Could be used
* stand-alone to obtain a preview of the operations that will be performed on
* the database.
* Models the list of operations that have to be performed by the
* {@link it.inaf.ia2.tsm.TapSchema#save()} method, in terms of adding, updating
* or removing {@link it.inaf.ia2.tsm.TapSchemaEntity} entities. Could also be
* used stand-alone to obtain a preview of the operations that will be performed
* on the database.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
......@@ -41,8 +40,6 @@ public class UpdateOperations {
private static final Logger LOG = LoggerFactory.getLogger(UpdateOperations.class);
private final TapSchema tapSchema;
private final List<Schema> schemasToRemove;
private final List<Schema> schemasToAdd;
private final List<Schema> schemasToUpdate;
......@@ -71,9 +68,6 @@ public class UpdateOperations {
}
public UpdateOperations(TapSchema tapSchema) {
this.tapSchema = tapSchema;
schemasToRemove = new ArrayList<>();
schemasToAdd = new ArrayList<>();
schemasToUpdate = new ArrayList<>();
......
......@@ -27,8 +27,8 @@ 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 which 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 ColumnModel} defining
* that column. To fix this issue an {@code ALTER TABLE} is necessary.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
......
......@@ -28,7 +28,7 @@ package it.inaf.ia2.tsm.datalayer;
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public enum ADQL {
VARCHAR,
CHAR,
BOOLEAN,
......@@ -46,13 +46,21 @@ public enum ADQL {
POINT;
/**
* For not standard types
* Returns a string representing a datatype which can't be interpreted as an
* ADQL datatype (that is in case of not standard data types). Removes
* possible size appended at the end of datatype.
*/
public static String getDataType(String dataType) {
// removing size from datatype.
return dataType.toUpperCase().replaceAll("\\(.+\\)", "");
}
/**
* Tells if a datatype has a variable length.
*
* @param adql value of the datatype using ADQL syntax
* @return true if the datatype has a variable length, false otherwise.
*/
public static boolean isVariable(ADQL adql) {
return adql.equals(VARCHAR) || adql.equals(VARBINARY) || adql.equals(CLOB) || adql.equals(BLOB);
}
......
......@@ -90,6 +90,9 @@ public class Credentials implements Serializable {
this.port = port;
}
/**
* The username used for connecting to the database.
*/
@XmlAttribute(name = "username", required = true)
public String getUsername() {
return this.username;
......@@ -99,6 +102,9 @@ public class Credentials implements Serializable {
this.username = username;
}
/**
* The password used for connecting to the database.
*/
@XmlAttribute(name = "password", required = true)
public String getPassword() {
return this.password;
......@@ -109,7 +115,7 @@ public class Credentials implements Serializable {
}
/**
* The kind of RDBMS used.
* The kind of RDBMS used (e.<!-- -->g.<!-- --> MySQL, PostgreSQL, etc).
*/
@XmlAttribute(name = "database-type", required = true)
public DatabaseType getDatabaseType() {
......@@ -121,7 +127,7 @@ public class Credentials implements Serializable {
}
/**
* <strong>Only for POSTGRES</strong>. The database to use in the
* <strong>Only for PostgreSQL</strong>. The database to use in the
* connection. The default value is "postgres".
*/
@XmlAttribute(name = "database")
......@@ -130,7 +136,7 @@ public class Credentials implements Serializable {
}
/**
* <strong>Only for POSTGRES</strong>. The database to use in the
* <strong>Only for PostgreSQL</strong>. The database to use in the
* connection.
*/
public void setDatabase(String database) {
......@@ -138,8 +144,8 @@ public class Credentials implements Serializable {
}
/**
* Set default values for <code>databaseType</code>, <code>database</code>
* and <code>port</code> properties.
* Set default values for {@code databaseType}, {@code database} and
* {@code port} fields.
*/
public final void setDefaults() {
if (databaseType == DatabaseType.MYSQL) {
......@@ -151,19 +157,6 @@ public class Credentials implements Serializable {
}
}
/**
* The class name of the RDBMS driver.
*/
public String getDatabaseDriverClass() {
switch (getDatabaseType()) {
case MYSQL:
return "com.mysql.jdbc.Driver";
case POSTGRES:
return "org.postgresql.Driver";
}
return null;
}
@Override
public String toString() {
return String.format("[%s] type=%s, hostname=%s, port=%s, username=%s, password=%s, database=%s",
......