Commit 8f95d727 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Improvements on XML models inheritance

parent fca053a6
......@@ -71,7 +71,7 @@ public class Column extends ChildEntity<Table> {
String type = null;
if (tapSchema.getName().equals(parentSchema.getName())) {
TableModel tableModel = tapSchema.getTapSchemaModel().get(parentTable.getName());
TableModel tableModel = tapSchema.getTapSchemaModel().getTable(parentTable.getName());
type = tableModel.get(getName()).getType();
}
......
......@@ -59,11 +59,11 @@ public class Table extends ChildEntity<Schema> implements EntitiesContainer<Colu
private TableModel getModel() {
if (tapSchema.getName().equals(parentSchema.getName())) {
return tapSchema.getTapSchemaModel().get(simpleName);
return tapSchema.getTapSchemaModel().getTable(simpleName);
}
if (tapSchema.isHasObscore() && parentSchema.getName().equals("ivoa")
&& simpleName.equals("obscore")) {
return tapSchema.getIvoaSchemaModel().get(simpleName);
return tapSchema.getIvoaSchemaModel().getTable(simpleName);
}
return null;
}
......
......@@ -660,7 +660,7 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable {
}
public final TableModel getTableModel(String tableName) {
return getTapSchemaModel().getTables().get(tableName);
return getTapSchemaModel().getTable(tableName);
}
public Map<String, Object> getSchemaMetadata(String schemaName) {
......@@ -697,11 +697,11 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable {
private void fillColumnProperties(SchemaModel schemaModel, String schemaName) {
// check only on std, but valid also for principal (it depends on TS version)
boolean useIntegerAsBool = getTapSchemaModel().get(COLUMNS_TABLE).get(Column.STD_KEY).getJavaType() == Integer.class;
boolean useIntegerAsBool = getTapSchemaModel().getTable(COLUMNS_TABLE).get(Column.STD_KEY).getJavaType() == Integer.class;
Schema schema = getChild(schemaName);
schema.setValue(DESCRIPTION_KEY, schemaModel.getDescription());
for (TableModel tableModel : schemaModel.getTables().values()) {
for (TableModel tableModel : schemaModel.getTables()) {
Table table = schema.getChild(tableModel.getName());
table.setValue(DESCRIPTION_KEY, tableModel.getDescription());
for (ColumnModel propertyModel : tableModel.getColumns()) {
......
......@@ -142,7 +142,7 @@ public abstract class DBBrokerTemplate implements DBBroker {
execute(getCreateDatabaseQuery(tapSchemaName), conn);
for (TableModel tableModel : tapSchemaModel.getTables().values()) {
for (TableModel tableModel : tapSchemaModel.getTables()) {
createTable(tapSchemaName, tableModel, conn);
}
......@@ -174,7 +174,7 @@ public abstract class DBBrokerTemplate implements DBBroker {
public void createIvoaSchemaStructure(SchemaModel ivoaSchemaModel) throws SQLException {
try (Connection conn = dataSource.getConnection()) {
execute(getCreateDatabaseQuery(ivoaSchemaModel.getName()), conn);
for (TableModel tableModel : ivoaSchemaModel.getTables().values()) {
for (TableModel tableModel : ivoaSchemaModel.getTables()) {
createTable(ivoaSchemaModel.getName(), tableModel, conn);
}
}
......@@ -681,11 +681,11 @@ public abstract class DBBrokerTemplate implements DBBroker {
}
for (SchemaModel tapSchemaModel : SchemaModels.getTapSchemaModels()) {
if (match(tapSchemaModel.get(TapSchema.SCHEMAS_TABLE), schemasColumns)
&& match(tapSchemaModel.get(TapSchema.TABLES_TABLE), tablesColumns)
&& match(tapSchemaModel.get(TapSchema.COLUMNS_TABLE), columnsColumns)
&& match(tapSchemaModel.get(TapSchema.KEYS_TABLE), keyColumns)
&& match(tapSchemaModel.get(TapSchema.KEY_COLUMNS_TABLE), keyColumnsColumns)) {
if (match(tapSchemaModel.getTable(TapSchema.SCHEMAS_TABLE), schemasColumns)
&& match(tapSchemaModel.getTable(TapSchema.TABLES_TABLE), tablesColumns)
&& match(tapSchemaModel.getTable(TapSchema.COLUMNS_TABLE), columnsColumns)
&& match(tapSchemaModel.getTable(TapSchema.KEYS_TABLE), keyColumns)
&& match(tapSchemaModel.getTable(TapSchema.KEY_COLUMNS_TABLE), keyColumnsColumns)) {
return tapSchemaModel.getVersion();
}
}
......
......@@ -156,24 +156,26 @@ public class MySQLDBBroker extends DBBrokerTemplate {
boolean first = true;
for (ColumnModel cm : tableModel.getColumns()) {
if (!first) {
querySb.append(",\n");
}
first = false;
if (cm.isMandatory()) {
if (!first) {
querySb.append(",\n");
}
first = false;
querySb.append(cm.getName());
querySb.append(" ");
querySb.append(cm.getName());
querySb.append(" ");
String mySQLType = TypesMapping.getMySQLTypeFromADQLType(cm.getType()).toUpperCase();
querySb.append(mySQLType);
if (mySQLType.equals("VARCHAR") || mySQLType.equals("CHAR")) {
appendSize(querySb, cm.getSize());
}
String mySQLType = TypesMapping.getMySQLTypeFromADQLType(cm.getType()).toUpperCase();
querySb.append(mySQLType);
if (mySQLType.equals("VARCHAR") || mySQLType.equals("CHAR")) {
appendSize(querySb, cm.getSize());
}
if (cm.isNullable()) {
querySb.append(" NULL");
} else {
querySb.append(" NOT NULL");
if (cm.isNullable()) {
querySb.append(" NULL");
} else {
querySb.append(" NOT NULL");
}
}
}
......
......@@ -48,65 +48,67 @@ import org.slf4j.LoggerFactory;
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class PostgresDBBroker extends DBBrokerTemplate {
private final static Logger LOG = LoggerFactory.getLogger(PostgresDBBroker.class);
private final String pgDatabaseName;
public PostgresDBBroker(DataSource dataSource, String pgDatabaseName, String tapSchemaVersion) {
super(dataSource, '"', tapSchemaVersion);
this.pgDatabaseName = pgDatabaseName;
}
@Override
protected void createTable(String schemaName, TableModel tableModel, Connection conn) throws SQLException {
StringBuilder querySb = new StringBuilder();
querySb.append("CREATE TABLE IF NOT EXISTS ");
querySb.append(escape(schemaName));
querySb.append(".");
querySb.append(escape(tableModel.getName()));
querySb.append(" (\n");
boolean first = true;
for (ColumnModel cm : tableModel.getColumns()) {
if (!first) {
querySb.append(",\n");
}
first = false;
querySb.append(cm.getName());
querySb.append(" ");
String pgsqlType = TypesMapping.getPostgresSQLTypeFromADQLType(cm.getType()).toLowerCase();
querySb.append(pgsqlType);
if (pgsqlType.equals("character varying") || pgsqlType.equals("character")) {
appendSize(querySb, cm.getSize());
}
if (cm.isNullable()) {
querySb.append(" NULL");
} else {
querySb.append(" NOT NULL");
if (cm.isMandatory()) {
if (!first) {
querySb.append(",\n");
}
first = false;
querySb.append(cm.getName());
querySb.append(" ");
String pgsqlType = TypesMapping.getPostgresSQLTypeFromADQLType(cm.getType()).toLowerCase();
querySb.append(pgsqlType);
if (pgsqlType.equals("character varying") || pgsqlType.equals("character")) {
appendSize(querySb, cm.getSize());
}
if (cm.isNullable()) {
querySb.append(" NULL");
} else {
querySb.append(" NOT NULL");
}
}
}
querySb.append(")");
String query = querySb.toString();
try (Statement stat = conn.createStatement()) {
LOG.debug("Executing query: {}", query);
stat.executeUpdate(query);
}
}
@Override
protected String getAddPrimaryKeyQuery(String tapSchemaName, String tableName, String[] keyColumns) {
return String.format("ALTER TABLE ONLY %s.%s ADD CONSTRAINT %s_pkey PRIMARY KEY (%s)",
escape(tapSchemaName), escape(tableName), tableName, buildColumnsList(keyColumns));
}
@Override
protected String getAddForeignKeyQuery(String tapSchemaName, String tableName, String[] fromKeyColumns, String targetTableName, String[] toKeyColumns) {
// Building univocal constraint name
......@@ -119,12 +121,12 @@ public class PostgresDBBroker extends DBBrokerTemplate {
escape(tapSchemaName), escape(tableName), constraintNameSb.toString(), buildColumnsList(fromKeyColumns),
escape(tapSchemaName), escape(targetTableName), buildColumnsList(toKeyColumns));
}
@Override
protected String getCreateDatabaseQuery(String databaseName) {
return "CREATE SCHEMA IF NOT EXISTS " + escape(databaseName);
}
@Override
protected String getSchemaTablesQuery(String schemaName) {
return String.format("SELECT tablename FROM pg_catalog.pg_tables where schemaname = '%s'", schemaName);
......@@ -144,14 +146,14 @@ public class PostgresDBBroker extends DBBrokerTemplate {
}
return sb.toString();
}
@Override
public Map<String, Map<String, Object>> getAllColumnsOriginalMetadata(String schemaName, String tableSimpleName) throws SQLException {
Map<String, Map<String, Object>> allColumnsMetadata = new HashMap<>();
StringBuilder querySb = new StringBuilder();
querySb.append("SELECT c.column_name, c.data_type, pg_catalog.format_type(a.atttypid, a.atttypmod), r.contype AS column_type, c.character_maximum_length, c.numeric_precision, a.attndims AS arraydim\n");
querySb.append("FROM information_schema.columns c\n");
querySb.append("JOIN pg_catalog.pg_tables t ON c.table_schema = t.schemaname AND c.table_name = t.tablename\n");
......@@ -162,16 +164,16 @@ public class PostgresDBBroker extends DBBrokerTemplate {
querySb.append("' AND t.tablename = '");
querySb.append(tableSimpleName);
querySb.append("'");
String query = querySb.toString();
LOG.debug("Executing query {}", query);
try (Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
while (resultSet.next()) {
Map<String, Object> cm = new HashMap<>();
// Column name
......@@ -192,11 +194,11 @@ public class PostgresDBBroker extends DBBrokerTemplate {
}
cm.put(Column.PRIMARY_KEY, primaryKey);
cm.put(Column.INDEXED_KEY, indexed);
Integer size = null;
int arraydimension = 0;
String datatype;
String type = resultSet.getString("data_type").toUpperCase();
boolean isArray = false;
if ("ARRAY".equals(type)) {
......@@ -208,7 +210,7 @@ public class PostgresDBBroker extends DBBrokerTemplate {
// numbers inside brakets, so this case will be approximated to *x*
arraydimension = resultSet.getInt("arraydim");
}
datatype = TypesMapping.getADQLTypeFromPostgresType(type, getTapSchemaVersion());
if (!isArray && (datatype.equals(ADQL.VARCHAR.name()) || datatype.equals(ADQL.CHAR.name()))) {
size = resultSet.getInt("character_maximum_length");
......@@ -217,10 +219,10 @@ public class PostgresDBBroker extends DBBrokerTemplate {
// Adding size at the end of datatype
datatype += String.format("(%s)", size);
}
cm.put(Column.DATATYPE_KEY, datatype);
cm.put(Column.SIZE_KEY, size);
String arraySize = null;
if (isArray) {
arraySize = formatArraySize(arraydimension);
......@@ -231,21 +233,21 @@ public class PostgresDBBroker extends DBBrokerTemplate {
arraySize += "*";
}
}
cm.put(Column.ARRAYSIZE_KEY, arraySize);
allColumnsMetadata.put(columnName, cm);
}
}
return allColumnsMetadata;
}
@Override
public List<Key> getKeys(TapSchema tapSchema, String schemaName) throws SQLException {
StringBuilder queryKeysSb = new StringBuilder();
queryKeysSb.append("SELECT\n");
queryKeysSb.append("conname AS constraint_name,\n");
queryKeysSb.append("conrelid::regclass AS from_table, \n");
......@@ -258,33 +260,33 @@ public class PostgresDBBroker extends DBBrokerTemplate {
queryKeysSb.append("OR (confrelid::regclass || '' LIKE '");
queryKeysSb.append(schemaName);
queryKeysSb.append(".%'))");
String queryKeys = queryKeysSb.toString();
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(queryKeys)) {
LOG.debug("Executing query {}", queryKeys);
List<Key> keys = new ArrayList<>();
while (resultSet.next()) {
String constraintName = resultSet.getString("constraint_name");
String fromTableCompleteName = resultSet.getString("from_table");
String targetTableCompleteName = resultSet.getString("target_table");
String fromSchema = fromTableCompleteName.split(Pattern.quote("."))[0];
String targetSchema = targetTableCompleteName.split(Pattern.quote("."))[0];
Map<String, Object> keyMetadata = new HashMap<>();
keyMetadata.put(Key.FROM_TABLE_KEY, fromTableCompleteName);
keyMetadata.put(Key.TARGET_TABLE_KEY, targetTableCompleteName);
Key key = new Key(tapSchema, keyMetadata);
keys.add(key);
StringBuilder queryFromKCSb = new StringBuilder();
queryFromKCSb.append("SELECT\n");
queryFromKCSb.append("c.column_name AS key_column\n");
......@@ -302,7 +304,7 @@ public class PostgresDBBroker extends DBBrokerTemplate {
// conkey conrelid
String queryFromKC = queryFromKCSb.toString();
StringBuilder queryTargetKCSb = new StringBuilder();
queryTargetKCSb.append("SELECT\n");
queryTargetKCSb.append("c.column_name AS key_column\n");
......@@ -320,16 +322,16 @@ public class PostgresDBBroker extends DBBrokerTemplate {
// as above, but with confkey and confrelid and different c.table_schema where condition
String queryTargetKC = queryTargetKCSb.toString();
try (Statement statFromKC = connection.createStatement();
Statement statTargetKC = connection.createStatement()) {
try (ResultSet rsFromKC = statFromKC.executeQuery(queryFromKC);
ResultSet rsTargetKC = statTargetKC.executeQuery(queryTargetKC)) {
LOG.debug("Executing query {}", queryFromKC);
LOG.debug("Executing query {}", queryTargetKC);
while (rsFromKC.next()) {
if (rsTargetKC.next()) {
key.addKeyColumn(
......@@ -341,11 +343,11 @@ public class PostgresDBBroker extends DBBrokerTemplate {
}
}
}
return keys;
}
}
@Override
protected String getTableTypesQuery(String schemaName) {
StringBuilder sb = new StringBuilder();
......@@ -361,18 +363,18 @@ public class PostgresDBBroker extends DBBrokerTemplate {
sb.append("'");
return sb.toString();
}
@Override
protected String getColumnNamesQuery(String tapSchemaName, String tableName) {
return String.format("SELECT column_name FROM information_schema.columns WHERE table_schema = '%s' AND table_name = '%s'",
tapSchemaName, tableName);
}
@Override
protected String getAllSchemaNamesQuery() {
return "SELECT schema_name FROM information_schema.schemata";
}
@Override
protected String getAllTablesNamesQuery(String schemaName) {
return String.format("SELECT tablename FROM pg_catalog.pg_tables where schemaname = '%s'", schemaName);
......
......@@ -23,6 +23,7 @@
package it.inaf.ia2.tsm.model;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
......@@ -57,7 +58,7 @@ public class ColumnModel implements Serializable {
mandatory = true;
}
@XmlElement(name = "name")
@XmlAttribute(name = "name")
public String getName() {
return name;
}
......
......@@ -23,8 +23,8 @@
package it.inaf.ia2.tsm.model;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
......@@ -39,15 +39,16 @@ public class SchemaModel implements Serializable {
private static final long serialVersionUID = 1087876778125446000L;
private final Map<String, TableModel> tables;
private final List<TableModel> tables;
private String name;
private String version;
private String extendsFrom;
private String description;
private String datatypeType;
public SchemaModel() {
tables = new HashMap<>();
tables = new ArrayList<>();
}
@XmlAttribute(name = "name")
......@@ -89,11 +90,25 @@ public class SchemaModel implements Serializable {
@XmlElements({
@XmlElement(name = "table")
})
public Map<String, TableModel> getTables() {
public List<TableModel> getTables() {
return tables;
}
public TableModel get(String tableName) {
return tables.get(tableName);
public TableModel getTable(String tableName) {
for (TableModel table : tables) {
if (tableName.equals(table.getName())) {
return table;
}
}
return null;
}
@XmlAttribute(name = "datatype")
public String getDatatypeType() {
return datatypeType;
}
public void setDatatypeType(String datatypeType) {
this.datatypeType = datatypeType;
}
}
......@@ -89,7 +89,7 @@ public class SchemaModels {
}
public static TableModel getTapSchemaTableModel(String tableName, String version) {
return getTapSchemaModel(version).getTables().get(tableName);
return getTapSchemaModel(version).getTable(tableName);
}
private static List<String> getAvailableVersions(Map<String, SchemaModel> schemaModels) {
......
......@@ -28,7 +28,6 @@ import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlTransient;
/**
*
......
......@@ -99,8 +99,12 @@ public class XMLMerger {
private Element checkElement(Element inheritedParent, Element child) {
Element inheritedElement = null;
String name = child.getAttribute("name");
Element inheritedElement = findElementByNameAttribute(inheritedParent, name);
if (name != null && !name.isEmpty()) {
inheritedElement = findElementByNameAttribute(inheritedParent, name);
}
if (child.getNodeName().equals("remove")) {
if (inheritedElement != null) {
......
......@@ -23,46 +23,40 @@ Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-->
<schema name="tap_schema" version="1.0-IA2" extends="1.0">
<table name="schemas">
<column>
<name>schemaID</name>
<column name="schemaID">
<type>BIGINT</type>
<updatable>true</updatable>
<standard>false</standard>
</column>
</table>
<table name="tables">
<column>
<name>tableID</name>