Commit 837bcb34 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

refactoring and improvements

parent 127afc04
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ import javax.persistence.IdClass;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

class ColumnId implements Serializable {

@@ -76,6 +77,7 @@ public class ColumnEntity implements Serializable, TapSchemaEntity {
    @Column(name = "id")
    private int id;

    @Transient
    private boolean primaryKey;

    protected ColumnEntity() {
+2 −2
Original line number Diff line number Diff line
@@ -42,8 +42,8 @@ public class DLUtil {
    }

    /**
     * Remove an entity from a list of <code>TapSchemaEntity</code> searching it
     * by name.
     * Remove an entity from a list of <code>TapSchemaEntity</code> instances
     * searching it by name.
     *
     * This method is protected because this operation should be performed by
     * the entity that contains the list.
+9 −9
Original line number Diff line number Diff line
@@ -394,14 +394,14 @@ public class Dao {
                    String constraintName = resultSet.getString("constraint_name");

                    String[] fromTableFullNameSplitted = resultSet.getString("from_table").split(Pattern.quote("."));
                    String fromSchema = fromTableFullNameSplitted[0];
                    String fromTable = fromTableFullNameSplitted[1];

                    String[] targetTableFullNameSplitted = resultSet.getString("target_table").split(Pattern.quote("."));
                    String targetSchema = targetTableFullNameSplitted[0];
                    String targetTable = targetTableFullNameSplitted[1];

                    KeyEntity key = new KeyEntity(
                            fromTableFullNameSplitted[0],
                            fromTableFullNameSplitted[1],
                            targetTableFullNameSplitted[0],
                            targetTableFullNameSplitted[1]
                    );
                    KeyEntity key = new KeyEntity(fromSchema, fromTable, targetSchema, targetTable);
                    schemaKeys.add(key);

                    // conkey conrelid
@@ -411,17 +411,17 @@ public class Dao {
                            + "JOIN pg_catalog.pg_constraint r ON c.ordinal_position = ANY(r.conkey)\n"
                            + "AND (c.table_schema || '.' || c.table_name) = (r.conrelid::regclass || '')\n"
                            + "WHERE r.conname = '" + constraintName + "' AND r.contype = 'f'\n"
                            + "AND c.table_schema = '" + schemaName + "'\n"
                            + "AND c.table_schema = '" + fromSchema + "'\n"
                            + "AND table_catalog = '" + databaseName + "'";

                    // as above, but with confkey and confrelid
                    // as above, but with confkey and confrelid and different c.table_schema where condition
                    String queryTargetKC = "SELECT\n"
                            + "c.column_name AS key_column\n"
                            + "FROM information_schema.columns c\n"
                            + "JOIN pg_catalog.pg_constraint r ON c.ordinal_position = ANY(r.confkey)\n"
                            + "AND (c.table_schema || '.' || c.table_name) = (r.confrelid::regclass || '')\n"
                            + "WHERE r.conname = '" + constraintName + "' AND r.contype = 'f'\n"
                            + "AND c.table_schema = '" + schemaName + "'\n"
                            + "AND c.table_schema = '" + targetSchema + "'\n"
                            + "AND table_catalog = '" + databaseName + "'";

                    try (Statement statFromKC = connection.createStatement();
+9 −0
Original line number Diff line number Diff line
@@ -102,4 +102,13 @@ public class KeyColumnEntity implements Serializable {
    public KeyEntity getKey() {
        return key;
    }

    public void setKey(KeyEntity key) {
        this.key = key;
    }

    @Override
    public String toString() {
        return String.format("%s->%s", fromColumn, targetColumn);
    }
}
+73 −25
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -13,6 +14,7 @@ import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * JPA entity for the table <code>TAP_SCHEMA.keys</code>.
@@ -55,7 +57,9 @@ public class KeyEntity implements Serializable {
    @OneToMany(mappedBy = "key", cascade = CascadeType.ALL, orphanRemoval = true)
    private final List<KeyColumnEntity> keyColumns;
    
    @Transient
    private String fromSchemaName;
    @Transient
    private String targetSchemaName;
    
    public KeyEntity() {
@@ -199,4 +203,48 @@ public class KeyEntity implements Serializable {
    public void setTargetSchemaName(String targetSchemaName) {
        this.targetSchemaName = targetSchemaName;
    }
    
    protected void reset() {
        this.keyId = null;
        for (KeyColumnEntity keyColumn : keyColumns) {
            keyColumn.setKeyId(null);
            keyColumn.setKey(null);
        }
    }
    
    @Override
    public int hashCode() {
        int hash = 5;
        hash = 83 * hash + Objects.hashCode(this.keyId);
        return hash;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final KeyEntity other = (KeyEntity) obj;
        
        if (this.keyId == null || other.keyId == null) {
            throw new UnsupportedOperationException("Cannot compare KeyEntity instances with null keyId");
        }
        
        return this.keyId.equals(other.keyId);
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("key_id=%s\n", keyId));
        for (KeyColumnEntity keyColumn : keyColumns) {
            sb.append("\t");
            sb.append(keyColumn.toString());
            sb.append("\n");
        }
        return sb.toString();
    }
}
Loading