All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sirius.db.jdbc.schema.ForeignKey Maven / Gradle / Ivy

Go to download

Provides a modern and highly flexible ORM and lightweight connectivity for JDBC, MongoDB, Redis, Elasticsearch.

There is a newer version: 7.4
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.db.jdbc.schema;

import com.google.common.collect.Lists;
import sirius.kernel.commons.ComparableTuple;
import sirius.kernel.commons.Strings;

import java.util.Collections;
import java.util.List;

/**
 * Represents a foreign key.
 */
public class ForeignKey {
    private String name;
    private List> keyFields = Lists.newArrayList();
    private List> foreignKeyFields = Lists.newArrayList();
    private String foreignTable;

    /**
     * Returns the name of the foreign key.
     *
     * @return the name of the foreign key
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the foreign key.
     *
     * @param name then name of the foreign key
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the name of the referenced table.
     *
     * @return the name of the referenced table
     */
    public String getForeignTable() {
        return foreignTable;
    }

    /**
     * Sets the name of the referenced table.
     *
     * @param foreignTable the name of the referenced table
     */
    public void setForeignTable(String foreignTable) {
        this.foreignTable = foreignTable;
    }

    /**
     * Returns the columns that make up the key.
     *
     * @return the columns of the key
     */
    public List getColumns() {
        List columns = Lists.newArrayList();
        for (ComparableTuple field : keyFields) {
            columns.add(field.getSecond());
        }
        return columns;
    }

    /**
     * Returns the columns matched in the referenced table.
     *
     * @return the columns matched in the referenced table
     */
    public List getForeignColumns() {
        List columns = Lists.newArrayList();
        for (ComparableTuple field : foreignKeyFields) {
            columns.add(field.getSecond());
        }
        return columns;
    }

    @Override
    public String toString() {
        return name + "(" + Strings.join(getColumns(), ", ") + ") -> " + foreignTable + " (" + Strings.join(
                getForeignColumns(),
                ", ") + ")";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof ForeignKey)) {
            return false;
        }
        return ((ForeignKey) obj).name.equalsIgnoreCase(name);
    }

    @Override
    public int hashCode() {
        return name == null ? 0 : name.hashCode();
    }

    /**
     * Adds a column to the key
     *
     * @param pos   the position to add at
     * @param field the field or column to add
     */
    public void addColumn(int pos, String field) {
        keyFields.add(ComparableTuple.create(pos, field));
        Collections.sort(keyFields);
    }

    /**
     * Adds a foreign column which has to be matched be the local ones.
     *
     * @param pos   the position to add at
     * @param field the field or column to add
     */
    public void addForeignColumn(int pos, String field) {
        foreignKeyFields.add(ComparableTuple.create(pos, field));
        Collections.sort(foreignKeyFields);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy