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> { ...@@ -71,7 +71,7 @@ public class Column extends ChildEntity<Table> {
String type = null; String type = null;
if (tapSchema.getName().equals(parentSchema.getName())) { 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(); type = tableModel.get(getName()).getType();
} }
......
...@@ -59,11 +59,11 @@ public class Table extends ChildEntity<Schema> implements EntitiesContainer<Colu ...@@ -59,11 +59,11 @@ public class Table extends ChildEntity<Schema> implements EntitiesContainer<Colu
private TableModel getModel() { private TableModel getModel() {
if (tapSchema.getName().equals(parentSchema.getName())) { if (tapSchema.getName().equals(parentSchema.getName())) {
return tapSchema.getTapSchemaModel().get(simpleName); return tapSchema.getTapSchemaModel().getTable(simpleName);
} }
if (tapSchema.isHasObscore() && parentSchema.getName().equals("ivoa") if (tapSchema.isHasObscore() && parentSchema.getName().equals("ivoa")
&& simpleName.equals("obscore")) { && simpleName.equals("obscore")) {
return tapSchema.getIvoaSchemaModel().get(simpleName); return tapSchema.getIvoaSchemaModel().getTable(simpleName);
} }
return null; return null;
} }
......
...@@ -660,7 +660,7 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { ...@@ -660,7 +660,7 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable {
} }
public final TableModel getTableModel(String tableName) { public final TableModel getTableModel(String tableName) {
return getTapSchemaModel().getTables().get(tableName); return getTapSchemaModel().getTable(tableName);
} }
public Map<String, Object> getSchemaMetadata(String schemaName) { public Map<String, Object> getSchemaMetadata(String schemaName) {
...@@ -697,11 +697,11 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable { ...@@ -697,11 +697,11 @@ public class TapSchema implements EntitiesContainer<Schema>, Serializable {
private void fillColumnProperties(SchemaModel schemaModel, String schemaName) { private void fillColumnProperties(SchemaModel schemaModel, String schemaName) {
// check only on std, but valid also for principal (it depends on TS version) // 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 schema = getChild(schemaName);
schema.setValue(DESCRIPTION_KEY, schemaModel.getDescription()); schema.setValue(DESCRIPTION_KEY, schemaModel.getDescription());
for (TableModel tableModel : schemaModel.getTables().values()) { for (TableModel tableModel : schemaModel.getTables()) {
Table table = schema.getChild(tableModel.getName()); Table table = schema.getChild(tableModel.getName());
table.setValue(DESCRIPTION_KEY, tableModel.getDescription()); table.setValue(DESCRIPTION_KEY, tableModel.getDescription());
for (ColumnModel propertyModel : tableModel.getColumns()) { for (ColumnModel propertyModel : tableModel.getColumns()) {
......
...@@ -142,7 +142,7 @@ public abstract class DBBrokerTemplate implements DBBroker { ...@@ -142,7 +142,7 @@ public abstract class DBBrokerTemplate implements DBBroker {
execute(getCreateDatabaseQuery(tapSchemaName), conn); execute(getCreateDatabaseQuery(tapSchemaName), conn);
for (TableModel tableModel : tapSchemaModel.getTables().values()) { for (TableModel tableModel : tapSchemaModel.getTables()) {
createTable(tapSchemaName, tableModel, conn); createTable(tapSchemaName, tableModel, conn);
} }
...@@ -174,7 +174,7 @@ public abstract class DBBrokerTemplate implements DBBroker { ...@@ -174,7 +174,7 @@ public abstract class DBBrokerTemplate implements DBBroker {
public void createIvoaSchemaStructure(SchemaModel ivoaSchemaModel) throws SQLException { public void createIvoaSchemaStructure(SchemaModel ivoaSchemaModel) throws SQLException {
try (Connection conn = dataSource.getConnection()) { try (Connection conn = dataSource.getConnection()) {
execute(getCreateDatabaseQuery(ivoaSchemaModel.getName()), conn); execute(getCreateDatabaseQuery(ivoaSchemaModel.getName()), conn);
for (TableModel tableModel : ivoaSchemaModel.getTables().values()) { for (TableModel tableModel : ivoaSchemaModel.getTables()) {
createTable(ivoaSchemaModel.getName(), tableModel, conn); createTable(ivoaSchemaModel.getName(), tableModel, conn);
} }
} }
...@@ -681,11 +681,11 @@ public abstract class DBBrokerTemplate implements DBBroker { ...@@ -681,11 +681,11 @@ public abstract class DBBrokerTemplate implements DBBroker {
} }
for (SchemaModel tapSchemaModel : SchemaModels.getTapSchemaModels()) { for (SchemaModel tapSchemaModel : SchemaModels.getTapSchemaModels()) {
if (match(tapSchemaModel.get(TapSchema.SCHEMAS_TABLE), schemasColumns) if (match(tapSchemaModel.getTable(TapSchema.SCHEMAS_TABLE), schemasColumns)
&& match(tapSchemaModel.get(TapSchema.TABLES_TABLE), tablesColumns) && match(tapSchemaModel.getTable(TapSchema.TABLES_TABLE), tablesColumns)
&& match(tapSchemaModel.get(TapSchema.COLUMNS_TABLE), columnsColumns) && match(tapSchemaModel.getTable(TapSchema.COLUMNS_TABLE), columnsColumns)
&& match(tapSchemaModel.get(TapSchema.KEYS_TABLE), keyColumns) && match(tapSchemaModel.getTable(TapSchema.KEYS_TABLE), keyColumns)
&& match(tapSchemaModel.get(TapSchema.KEY_COLUMNS_TABLE), keyColumnsColumns)) { && match(tapSchemaModel.getTable(TapSchema.KEY_COLUMNS_TABLE), keyColumnsColumns)) {
return tapSchemaModel.getVersion(); return tapSchemaModel.getVersion();
} }
} }
......
...@@ -156,24 +156,26 @@ public class MySQLDBBroker extends DBBrokerTemplate { ...@@ -156,24 +156,26 @@ public class MySQLDBBroker extends DBBrokerTemplate {
boolean first = true; boolean first = true;
for (ColumnModel cm : tableModel.getColumns()) { for (ColumnModel cm : tableModel.getColumns()) {
if (!first) { if (cm.isMandatory()) {
querySb.append(",\n"); if (!first) {
} querySb.append(",\n");
first = false; }
first = false;
querySb.append(cm.getName()); querySb.append(cm.getName());
querySb.append(" "); querySb.append(" ");
String mySQLType = TypesMapping.getMySQLTypeFromADQLType(cm.getType()).toUpperCase(); String mySQLType = TypesMapping.getMySQLTypeFromADQLType(cm.getType()).toUpperCase();
querySb.append(mySQLType); querySb.append(mySQLType);
if (mySQLType.equals("VARCHAR") || mySQLType.equals("CHAR")) { if (mySQLType.equals("VARCHAR") || mySQLType.equals("CHAR")) {
appendSize(querySb, cm.getSize()); appendSize(querySb, cm.getSize());
} }
if (cm.isNullable()) { if (cm.isNullable()) {
querySb.append(" NULL"); querySb.append(" NULL");
} else { } else {
querySb.append(" NOT NULL"); querySb.append(" NOT NULL");
}
} }
} }
......
...@@ -48,65 +48,67 @@ import org.slf4j.LoggerFactory; ...@@ -48,65 +48,67 @@ import org.slf4j.LoggerFactory;
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>} * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/ */
public class PostgresDBBroker extends DBBrokerTemplate { public class PostgresDBBroker extends DBBrokerTemplate {
private final static Logger LOG = LoggerFactory.getLogger(PostgresDBBroker.class); private final static Logger LOG = LoggerFactory.getLogger(PostgresDBBroker.class);
private final String pgDatabaseName; private final String pgDatabaseName;
public PostgresDBBroker(DataSource dataSource, String pgDatabaseName, String tapSchemaVersion) { public PostgresDBBroker(DataSource dataSource, String pgDatabaseName, String tapSchemaVersion) {
super(dataSource, '"', tapSchemaVersion); super(dataSource, '"', tapSchemaVersion);
this.pgDatabaseName = pgDatabaseName; this.pgDatabaseName = pgDatabaseName;
} }
@Override @Override
protected void createTable(String schemaName, TableModel tableModel, Connection conn) throws SQLException { protected void createTable(String schemaName, TableModel tableModel, Connection conn) throws SQLException {
StringBuilder querySb = new StringBuilder(); StringBuilder querySb = new StringBuilder();
querySb.append("CREATE TABLE IF NOT EXISTS "); querySb.append("CREATE TABLE IF NOT EXISTS ");
querySb.append(escape(schemaName)); querySb.append(escape(schemaName));
querySb.append("."); querySb.append(".");
querySb.append(escape(tableModel.getName())); querySb.append(escape(tableModel.getName()));
querySb.append(" (\n"); querySb.append(" (\n");
boolean first = true; boolean first = true;
for (ColumnModel cm : tableModel.getColumns()) { for (ColumnModel cm : tableModel.getColumns()) {
if (!first) { if (cm.isMandatory()) {
querySb.append(",\n"); if (!first) {
} querySb.append(",\n");
first = false; }
first = false;
querySb.append(cm.getName());
querySb.append(" "); querySb.append(cm.getName());
querySb.append(" ");
String pgsqlType = TypesMapping.getPostgresSQLTypeFromADQLType(cm.getType()).toLowerCase();
querySb.append(pgsqlType); String pgsqlType = TypesMapping.getPostgresSQLTypeFromADQLType(cm.getType()).toLowerCase();
if (pgsqlType.equals("character varying") || pgsqlType.equals("character")) { querySb.append(pgsqlType);
appendSize(querySb, cm.getSize()); if (pgsqlType.equals("character varying") || pgsqlType.equals("character")) {
} appendSize(querySb, cm.getSize());
}
if (cm.isNullable()) {
querySb.append(" NULL"); if (cm.isNullable()) {
} else { querySb.append(" NULL");
querySb.append(" NOT NULL"); } else {
querySb.append(" NOT NULL");
}
} }
} }
querySb.append(")"); querySb.append(")");
String query = querySb.toString(); String query = querySb.toString();
try (Statement stat = conn.createStatement()) { try (Statement stat = conn.createStatement()) {
LOG.debug("Executing query: {}", query); LOG.debug("Executing query: {}", query);
stat.executeUpdate(query); stat.executeUpdate(query);
} }
} }
@Override @Override
protected String getAddPrimaryKeyQuery(String tapSchemaName, String tableName, String[] keyColumns) { protected String getAddPrimaryKeyQuery(String tapSchemaName, String tableName, String[] keyColumns) {
return String.format("ALTER TABLE ONLY %s.%s ADD CONSTRAINT %s_pkey PRIMARY KEY (%s)", return String.format("ALTER TABLE ONLY %s.%s ADD CONSTRAINT %s_pkey PRIMARY KEY (%s)",
escape(tapSchemaName), escape(tableName), tableName, buildColumnsList(keyColumns)); escape(tapSchemaName), escape(tableName), tableName, buildColumnsList(keyColumns));
} }
@Override @Override
protected String getAddForeignKeyQuery(String tapSchemaName, String tableName, String[] fromKeyColumns, String targetTableName, String[] toKeyColumns) { protected String getAddForeignKeyQuery(String tapSchemaName, String tableName, String[] fromKeyColumns, String targetTableName, String[] toKeyColumns) {
// Building univocal constraint name // Building univocal constraint name
...@@ -119,12 +121,12 @@ public class PostgresDBBroker extends DBBrokerTemplate { ...@@ -119,12 +121,12 @@ public class PostgresDBBroker extends DBBrokerTemplate {
escape(tapSchemaName), escape(tableName), constraintNameSb.toString(), buildColumnsList(fromKeyColumns), escape(tapSchemaName), escape(tableName), constraintNameSb.toString(), buildColumnsList(fromKeyColumns),
escape(tapSchemaName), escape(targetTableName), buildColumnsList(toKeyColumns)); escape(tapSchemaName), escape(targetTableName), buildColumnsList(toKeyColumns));
} }
@Override @Override
protected String getCreateDatabaseQuery(String databaseName) { protected String getCreateDatabaseQuery(String databaseName) {
return "CREATE SCHEMA IF NOT EXISTS " + escape(databaseName); return "CREATE SCHEMA IF NOT EXISTS " + escape(databaseName);
} }
@Override @Override
protected String getSchemaTablesQuery(String schemaName) { protected String getSchemaTablesQuery(String schemaName) {
return String.format("SELECT tablename FROM pg_catalog.pg_tables where schemaname = '%s'", schemaName); return String.format("SELECT tablename FROM pg_catalog.pg_tables where schemaname = '%s'", schemaName);
...@@ -144,14 +146,14 @@ public class PostgresDBBroker extends DBBrokerTemplate { ...@@ -144,14 +146,14 @@ public class PostgresDBBroker extends DBBrokerTemplate {
} }
return sb.toString(); return sb.toString();
} }
@Override @Override
public Map<String, Map<String, Object>> getAllColumnsOriginalMetadata(String schemaName, String tableSimpleName) throws SQLException { public Map<String, Map<String, Object>> getAllColumnsOriginalMetadata(String schemaName, String tableSimpleName) throws SQLException {
Map<String, Map<String, Object>> allColumnsMetadata = new HashMap<>(); Map<String, Map<String, Object>> allColumnsMetadata = new HashMap<>();
StringBuilder querySb = new StringBuilder(); 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("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("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"); 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 { ...@@ -162,16 +164,16 @@ public class PostgresDBBroker extends DBBrokerTemplate {
querySb.append("' AND t.tablename = '"); querySb.append("' AND t.tablename = '");
querySb.append(tableSimpleName); querySb.append(tableSimpleName);
querySb.append("'"); querySb.append("'");
String query = querySb.toString(); String query = querySb.toString();
LOG.debug("Executing query {}", query); LOG.debug("Executing query {}", query);
try (Connection conn = dataSource.getConnection(); try (Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement(); Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(query)) { ResultSet resultSet = statement.executeQuery(query)) {
while (resultSet.next()) { while (resultSet.next()) {
Map<String, Object> cm = new HashMap<>(); Map<String, Object> cm = new HashMap<>();
// Column name // Column name
...@@ -192,11 +194,11 @@ public class PostgresDBBroker extends DBBrokerTemplate { ...@@ -192,11 +194,11 @@ public class PostgresDBBroker extends DBBrokerTemplate {
} }
cm.put(Column.PRIMARY_KEY, primaryKey); cm.put(Column.PRIMARY_KEY, primaryKey);
cm.put(Column.INDEXED_KEY, indexed); cm.put(Column.INDEXED_KEY, indexed);
Integer size = null; Integer size = null;
int arraydimension = 0; int arraydimension = 0;
String datatype; String datatype;
String type = resultSet.getString("data_type").toUpperCase(); String type = resultSet.getString("data_type").toUpperCase();
boolean isArray = false; boolean isArray = false;
if ("ARRAY".equals(type)) { if ("ARRAY".equals(type)) {
...@@ -208,7 +210,7 @@ public class PostgresDBBroker extends DBBrokerTemplate { ...@@ -208,7 +210,7 @@ public class PostgresDBBroker extends DBBrokerTemplate {
// numbers inside brakets, so this case will be approximated to *x* // numbers inside brakets, so this case will be approximated to *x*
arraydimension = resultSet.getInt("arraydim"); arraydimension = resultSet.getInt("arraydim");
} }
datatype = TypesMapping.getADQLTypeFromPostgresType(type, getTapSchemaVersion()); datatype = TypesMapping.getADQLTypeFromPostgresType(type, getTapSchemaVersion());
if (!isArray && (datatype.equals(ADQL.VARCHAR.name()) || datatype.equals(ADQL.CHAR.name()))) { if (!isArray && (datatype.equals(ADQL.VARCHAR.name()) || datatype.equals(ADQL.CHAR.name()))) {
size = resultSet.getInt("character_maximum_length"); size = resultSet.getInt("character_maximum_length");
...@@ -217,10 +219,10 @@ public class PostgresDBBroker extends DBBrokerTemplate { ...@@ -217,10 +219,10 @@ public class PostgresDBBroker extends DBBrokerTemplate {
// Adding size at the end of datatype // Adding size at the end of datatype
datatype += String.format("(%s)", size); datatype += String.format("(%s)", size);
} }
cm.put(Column.DATATYPE_KEY, datatype); cm.put(Column.DATATYPE_KEY, datatype);
cm.put(Column.SIZE_KEY, size); cm.put(Column.SIZE_KEY, size);
String arraySize = null; String arraySize = null;
if (isArray) { if (isArray) {
arraySize = formatArraySize(arraydimension); arraySize = formatArraySize(arraydimension);
...@@ -231,21 +233,21 @@ public class PostgresDBBroker extends DBBrokerTemplate { ...@@ -231,21 +233,21 @@ public class PostgresDBBroker extends DBBrokerTemplate {
arraySize += "*"; arraySize += "*";
} }
} }
cm.put(Column.ARRAYSIZE_KEY, arraySize); cm.put(Column.ARRAYSIZE_KEY, arraySize);
allColumnsMetadata.put(columnName, cm); allColumnsMetadata.put(columnName, cm);
} }
} }
return allColumnsMetadata; return allColumnsMetadata;
} }
@Override @Override
public List<Key> getKeys(TapSchema tapSchema, String schemaName) throws SQLException { public List<Key> getKeys(TapSchema tapSchema, String schemaName) throws SQLException {
StringBuilder queryKeysSb = new StringBuilder(); StringBuilder queryKeysSb = new StringBuilder();
queryKeysSb.append("SELECT\n"); queryKeysSb.append("SELECT\n");
queryKeysSb.append("conname AS constraint_name,\n"); queryKeysSb.append("conname AS constraint_name,\n");
queryKeysSb.append("conrelid::regclass AS from_table, \n"); queryKeysSb.append("conrelid::regclass AS from_table, \n");
...@@ -258,33 +260,33 @@ public class PostgresDBBroker extends DBBrokerTemplate { ...@@ -258,33 +260,33 @@ public class PostgresDBBroker extends DBBrokerTemplate {
queryKeysSb.append("OR (confrelid::regclass || '' LIKE '"); queryKeysSb.append("OR (confrelid::regclass || '' LIKE '");
queryKeysSb.append(schemaName); queryKeysSb.append(schemaName);
queryKeysSb.append(".%'))"); queryKeysSb.append(".%'))");
String queryKeys = queryKeysSb.toString(); String queryKeys = queryKeysSb.toString();
try (Connection connection = dataSource.getConnection(); try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(queryKeys)) { ResultSet resultSet = statement.executeQuery(queryKeys)) {
LOG.debug("Executing query {}", queryKeys); LOG.debug("Executing query {}", queryKeys);
List<Key> keys = new ArrayList<>(); List<Key> keys = new ArrayList<>();
while (resultSet.next()) { while (resultSet.next()) {
String constraintName = resultSet.getString("constraint_name"); String constraintName = resultSet.getString("constraint_name");
String fromTableCompleteName = resultSet.getString("from_table"); String fromTableCompleteName = resultSet.getString("from_table");
String targetTableCompleteName = resultSet.getString("target_table"); String targetTableCompleteName = resultSet.getString("target_table");
String fromSchema = fromTableCompleteName.split(Pattern.quote("."))[0]; String fromSchema = fromTableCompleteName.split(Pattern.quote("."))[0];
String targetSchema = targetTableCompleteName.split(Pattern.quote("."))[0]; String targetSchema = targetTableCompleteName.split(Pattern.quote("."))[0];
Map<String, Object> keyMetadata = new HashMap<>(); Map<String, Object> keyMetadata = new HashMap<>();
keyMetadata.put(Key.FROM_TABLE_KEY, fromTableCompleteName); keyMetadata.put(Key.FROM_TABLE_KEY, fromTableCompleteName);
keyMetadata.put(Key.TARGET_TABLE_KEY, targetTableCompleteName); keyMetadata.put(Key.TARGET_TABLE_KEY, targetTableCompleteName);
Key key = new Key(tapSchema, keyMetadata); Key key = new Key(tapSchema, keyMetadata);
keys.add(key); keys.add(key);
StringBuilder queryFromKCSb = new StringBuilder(); StringBuilder queryFromKCSb = new StringBuilder();
queryFromKCSb.append("SELECT\n"); queryFromKCSb.append("SELECT\n");
queryFromKCSb.append("c.column_name AS key_column\n"); queryFromKCSb.append("c.column_name AS key_column\n");
...@@ -302,7 +304,7 @@ public class PostgresDBBroker extends DBBrokerTemplate { ...@@ -302,7 +304,7 @@ public class PostgresDBBroker extends DBBrokerTemplate {
// conkey conrelid // conkey conrelid
String queryFromKC = queryFromKCSb.toString(); String queryFromKC = queryFromKCSb.toString();
StringBuilder queryTargetKCSb = new StringBuilder(); StringBuilder queryTargetKCSb = new StringBuilder();
queryTargetKCSb.append("SELECT\n"); queryTargetKCSb.append("SELECT\n");
queryTargetKCSb.append("c.column_name AS key_column\n"); queryTargetKCSb.append("c.column_name AS key_column\n");
...@@ -320,16 +322,16 @@ public class PostgresDBBroker extends DBBrokerTemplate { ...@@ -320,16 +322,16 @@ public class PostgresDBBroker extends DBBrokerTemplate {
// as above, but with confkey and confrelid and different c.table_schema where condition // as above, but with confkey and confrelid and different c.table_schema where condition
String queryTargetKC = queryTargetKCSb.toString(); String queryTargetKC = queryTargetKCSb.toString();
try (Statement statFromKC = connection.createStatement(); try (Statement statFromKC = connection.createStatement();
Statement statTargetKC = connection.createStatement()) { Statement statTargetKC = connection.createStatement()) {
try (ResultSet rsFromKC = statFromKC.executeQuery(queryFromKC); try (ResultSet rsFromKC = statFromKC.executeQuery(queryFromKC);
ResultSet rsTargetKC = statTargetKC.executeQuery(queryTargetKC)) { ResultSet rsTargetKC = statTargetKC.executeQuery(queryTargetKC)) {
LOG.debug("Executing query {}", queryFromKC); LOG.debug("Executing query {}", queryFromKC);
LOG.debug("Executing query {}", queryTargetKC); LOG.debug("Executing query {}", queryTargetKC);