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

Fixes

parent 6bc2765d
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
@@ -37,6 +37,50 @@ public class DaoColumn {
        return false;
    }

    /**
     * Returns the list of all the columns names given a schema name and a table
     * name, also if these objects have never been added into the TAP_SCHEMA.
     * This can be useful to retrieve the source database structure.
     */
    public static List<String> getAllColumnsNames(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName, String tableSimpleName) throws SQLException {
        final List<String> allColumns = new ArrayList<>();

        DataSource dataSource = TSMUtil.getSchemaDataSource(dbWrapper, tapSchema, schemaName);
        DatabaseType dbType = TSMUtil.getSchemaDatabaseType(dbWrapper, tapSchema, schemaName);

        String query;
        if (dbType == DatabaseType.MYSQL) {
            query = String.format("SHOW COLUMNS FROM `%s`.`%s`", schemaName, tableSimpleName);
        } else if (dbType == DatabaseType.POSTGRES) {
            query = "SELECT column_name FROM information_schema.columns WHERE table_schema = '" + schemaName + "' AND table_name = '" + tableSimpleName + "'";
        } else {
            throw new UnsupportedOperationException("Database type " + dbType + " not supported");
        }

        log.debug("Executing query {}", query);

        try (Connection connection = dataSource.getConnection();
                Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery(query)) {

            while (resultSet.next()) {

                String columnName;
                if (dbType == DatabaseType.MYSQL) {
                    columnName = resultSet.getString("Field");
                } else if (dbType == DatabaseType.POSTGRES) {
                    columnName = resultSet.getString("column_name");
                } else {
                    throw new UnsupportedOperationException("Database type " + dbType + " not supported");
                }

                allColumns.add(columnName);
            }
        }

        return allColumns;
    }

    /**
     * For performance reasons all columns of a {@link Table} are loaded
     * together using this method. Columns {@link Status} is at first set as
+8 −8
Original line number Diff line number Diff line
@@ -340,13 +340,6 @@ public class DaoKey {
                    }
                }

                if (!fictitiousKeys.isEmpty()) {
                    log.debug("{} fictitious keys found", fictitiousKeys.size());
                    for (Key key : fictitiousKeys) {
                        log.debug("   {}", key);
                    }
                }

                // filling fictitious keys columns
                for (Key key : fictitiousKeys) {
                    try (PreparedStatement statementKeyColumns = conn.prepareStatement(queryKeyColumns)) {
@@ -361,7 +354,7 @@ public class DaoKey {
                                String fromColumn = rsKeyColumns.getString(KeyColumn.FROM_COLUMN_KEY);
                                String targetColumn = rsKeyColumns.getString(KeyColumn.TARGET_COLUMN_KEY);

                                KeyColumnImpl keyColumn = new KeyColumnImpl(dbWrapper, tapSchema, key, fromColumn, targetColumn);
                                KeyColumn keyColumn = ((KeyImpl) key).addKeyColumn(fromColumn, targetColumn);
                                if (supportKeyColumnID) {
                                    keyColumn.initProperty(KeyColumn.KEY_COLUMN_ID_KEY, TSMUtil.getObject(rsKeyColumns, KeyColumn.KEY_COLUMN_ID_KEY, Long.class));
                                }
@@ -373,6 +366,13 @@ public class DaoKey {
                    ((TapSchemaImpl) tapSchema).getAllKeys().add(key);
                }

                if (!fictitiousKeys.isEmpty()) {
                    log.debug("{} fictitious keys found", fictitiousKeys.size());
                    for (Key key : fictitiousKeys) {
                        log.debug("   {}", key);
                    }
                }

                // Check if there are remaining keys with keyId = null (valid keys
                // that weren't saved into the TAP_SCHEMA).
                int keyId = ((TapSchemaImpl) tapSchema).getMaxKeyId() + 1;
+3 −2
Original line number Diff line number Diff line
@@ -32,12 +32,13 @@ public class DaoTable {

    /**
     * Retrieve the list of the names of all the tables contained in a schema,
     * given their related <code>DataSource</code> and schema name.
     * given its name, also if the schema has never been added into the
     * TAP_SCHEMA.
     *
     * @return list of all tables names alphabetically and case insensitively
     * ordered.
     */
    protected static List<String> getAllTablesNames(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName) throws SQLException {
    public static List<String> getAllTablesNames(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName) throws SQLException {

        DataSource dataSource = TSMUtil.getSchemaDataSource(dbWrapper, tapSchema, schemaName);
        DatabaseType dbType = TSMUtil.getSchemaDatabaseType(dbWrapper, tapSchema, schemaName);
+6 −17
Original line number Diff line number Diff line
@@ -242,17 +242,7 @@ public class TapSchemaImpl implements TapSchema, Serializable {
     */
    public int getMaxKeyId() {
        int maxKeyId = 0;
        for (Schema schema : getChildren()) {
            for (Table table : schema.getChildren()) {
                for (Key key : table.getAllFromKeys()) {
                    if (key.getId() != null) {
                        int keyId = Integer.parseInt(key.getId());
                        if (keyId > maxKeyId) {
                            maxKeyId = keyId;
                        }
                    }
                }
                for (Key key : table.getAllTargetKeys()) {
        for (Key key : allKeys) {
            if (key.getId() != null) {
                int keyId = Integer.parseInt(key.getId());
                if (keyId > maxKeyId) {
@@ -260,8 +250,6 @@ public class TapSchemaImpl implements TapSchema, Serializable {
                }
            }
        }
            }
        }
        return maxKeyId;
    }

@@ -327,6 +315,7 @@ public class TapSchemaImpl implements TapSchema, Serializable {
        targetTable.addTargetKey(key);

        allKeys.add(key);
        checkKeys();
    }

    /**
+99 −90
Original line number Diff line number Diff line
package it.inaf.oats.ia2.tapschemamanager.api;

import it.inaf.oats.ia2.tapschemamanager.api.TapSchemaImpl;
import it.inaf.oats.ia2.tapschemamanager.api.contract.DatabaseType;
import it.inaf.oats.ia2.tapschemamanager.api.Credentials;
import it.inaf.oats.ia2.tapschemamanager.api.DBWrapper;
import it.inaf.oats.ia2.tapschemamanager.api.UpdateOperations;
import it.inaf.oats.ia2.tapschemamanager.api.TapSchemaFactory;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Column;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Key;
import it.inaf.oats.ia2.tapschemamanager.api.contract.KeyColumn;
@@ -216,29 +211,6 @@ public class TestAll {
        }
    }

    @Test
    public void testTapSchemaSerialization() throws Exception {
        for (DBWrapper dbWrapper : dbWrappers) {
            if (dbWrapper.getTapSchemaDatabaseType() == DatabaseType.MYSQL) { // currently "tng_TAP_SCHEMA" exists in a MySQL instance
                TapSchema tapSchema = TapSchemaFactory.getTapSchema(TapSchemaVersion.TAP_SCHEMA_1_IA2, dbWrapper, "test_tap_schema", true);
                
                File temp = File.createTempFile("test_tap_schema", ".ser");
                
                try (FileOutputStream fileOut = new FileOutputStream(temp);
                        ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
                    out.writeObject(tapSchema);
                }
                
                try (FileInputStream fileIn = new FileInputStream(temp);
                        ObjectInputStream in = new ObjectInputStream(fileIn)) {
                    tapSchema = (TapSchema) in.readObject();
                }
                
                log.debug(tapSchema.toString());
            }
        }
    }
    
    private boolean allKeysHaveDifferentId(TapSchema tapSchema) {
        boolean differentKeySameId = false;
        Map<String, Key> keys = new HashMap<>();
@@ -281,8 +253,8 @@ public class TestAll {
    }

    @Test
    public void createNewAndUpdate2() throws SQLException {
        log.info("TEST createNewAndUpdate2 STARTED");
    public void createNewAndUpdate() throws SQLException {
        log.info("TEST createNewAndUpdate STARTED");

        try {
            removeTestingDatabases();
@@ -679,6 +651,20 @@ public class TestAll {
                        }
                    }
                }

                // Test adding ficitious key
                sch0table0.addChild("value1");
                sch0table2.addChild("value1");
                ((TapSchemaImpl) tapSchema).addFictitiousKey(sch0table2, new String[]{"value1"}, sch0table0, new String[]{"value1"});
                operations = new UpdateOperations(tapSchema);
                assertEquals(1, operations.getKeysToAdd().size());
                tapSchema.save();

                tapSchema = TapSchemaFactory.getTapSchema(TapSchemaVersion.TAP_SCHEMA_1_IA2, dbWrapper, "test_tap_schema", true);

                sch0table2 = tapSchema.getChild("sch0").getChild("table2");
                assertEquals(1, sch0table2.getVisibleFromKeys().size());
                checkKey(sch0table2.getVisibleFromKeys().get(0), "sch0.table2", "value1", "sch0.table0", "value1", true);
            }
        } catch (SQLException e) {
            throw e;
@@ -686,4 +672,27 @@ public class TestAll {
            //removeTestingDatabases(credentials);
        }
    }

    @Test
    public void testTapSchemaSerialization() throws Exception {
        for (DBWrapper dbWrapper : dbWrappers) {
            if (dbWrapper.getTapSchemaDatabaseType() == DatabaseType.MYSQL) { // currently "tng_TAP_SCHEMA" exists in a MySQL instance
                TapSchema tapSchema = TapSchemaFactory.getTapSchema(TapSchemaVersion.TAP_SCHEMA_1_IA2, dbWrapper, "test_tap_schema", true);

                File temp = File.createTempFile("test_tap_schema", ".ser");

                try (FileOutputStream fileOut = new FileOutputStream(temp);
                        ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
                    out.writeObject(tapSchema);
                }

                try (FileInputStream fileIn = new FileInputStream(temp);
                        ObjectInputStream in = new ObjectInputStream(fileIn)) {
                    tapSchema = (TapSchema) in.readObject();
                }

                log.debug(tapSchema.toString());
            }
        }
    }
}
Loading