ca.sqlpower.architect
Class SQLRelationship

java.lang.Object
  extended by ca.sqlpower.architect.SQLObject
      extended by ca.sqlpower.architect.SQLRelationship
All Implemented Interfaces:
java.io.Serializable

public class SQLRelationship
extends SQLObject
implements java.io.Serializable

The SQLRelationship class represents a foriegn key relationship between two SQLTable objects or two groups of columns within the same table.

See Also:
Serialized Form

Nested Class Summary
static class SQLRelationship.ColumnMapping
           
static class SQLRelationship.ColumnMappingFKColumnOrderComparator
          Comparator that orders ColumnMapping objects by FK column position.
static class SQLRelationship.Deferrability
          The enumeration of all referential integrity constraint checking policies.
protected  class SQLRelationship.RelationshipManager
          Listens to all activity at and under the pkTable and fkTable.
static class SQLRelationship.UpdateDeleteRule
          Enumeration of the various rules allowed for (foreign/imported/child) columns when their parent value is updated or deleted.
 
Field Summary
protected  SQLRelationship.Deferrability deferrability
          The deferrability rule for constraint checking on this relationship.
protected  SQLRelationship.UpdateDeleteRule deleteRule
          The rule for what the DBMS should do to the child (imported) key value when its parent table (exported) row is deleted.
protected  int fkCardinality
           
static int FKCOLUMN
           
protected  SQLRelationship.RelationshipManager fkColumnManager
           
protected  SQLTable fkTable
           
protected  boolean identifying
          Value should be true if this relationship is identifying, and false if otherwise.
static int MANY
           
static int ONE
           
protected  java.lang.String physicalName
           
protected  int pkCardinality
           
static int PKCOLUMN
           
protected  SQLTable pkTable
           
protected  SQLRelationship.UpdateDeleteRule updateRule
          The rule for what the DBMS should do to the child (imported) key value when its parent table (exported) key value changes.
static int ZERO
           
 
Fields inherited from class ca.sqlpower.architect.SQLObject
children, magicDisableCount, populated, undoEventListeners
 
Constructor Summary
SQLRelationship()
           
SQLRelationship(SQLRelationship relationshipToCopy)
          A copy constructor that returns a copy of the provided SQLRelationship with the following properties copied: Name Identifying status Update rule Delete rule Deferrability
 
Method Summary
 void addMapping(SQLColumn pkColumn, SQLColumn fkColumn)
          Convenience method for adding a SQLRelationship.ColumnMapping child to this relationship.
 boolean allowsChildren()
          Relationships have ColumnMapping children.
 void attachRelationship(SQLTable pkTable, SQLTable fkTable, boolean autoGenerateMapping)
          Associates an SQLRelationship with the given SQLTable objects.
 void checkColumnLocked(SQLColumn col)
          Throws a column locked exception if col is in a columnmapping of this relationship
 boolean containsFkColumn(SQLColumn col)
           
 boolean containsPkColumn(SQLColumn col)
           
 boolean determineIdentifyingStatus()
          Some SQLRelationship objects may not have their identifying property set properly which is particularly the case then creating SQLRelationships for source database objects and then reverse engineering, so this method will determine for certain if a relationship is identifying or non-identifying.
 java.lang.Class<? extends SQLObject> getChildType()
           
 SQLRelationship.Deferrability getDeferrability()
           
 SQLRelationship.UpdateDeleteRule getDeleteRule()
           
 int getFkCardinality()
          Gets the value of fkCardinality
 SQLTable getFkTable()
           
 SQLRelationship.ColumnMapping getMappingByFkCol(SQLColumn fkcol)
           
 SQLRelationship.ColumnMapping getMappingByPkCol(SQLColumn pkcol)
           
 java.util.List<SQLRelationship.ColumnMapping> getMappings()
          Convenience method that casts children to List<ColumnMapping>.
 SQLObject getParent()
          Returns the table that holds the primary keys (the imported table).
 int getPkCardinality()
          Gets the value of pkCardinality
 SQLTable getPkTable()
           
 SQLRelationship.RelationshipManager getRelationshipManager()
          This method is for the benefit of the unit tests.
 java.lang.String getShortDisplayName()
          Returns the foreign key name.
 SQLRelationship.UpdateDeleteRule getUpdateRule()
           
 boolean isIdentifying()
          Gets the value of identifying
 boolean isPopulated()
          Returns true.
 void populate()
          This class is not a lazy-loading class.
 java.lang.String printKeyColumns(int keyType)
           
 void setDeferrability(SQLRelationship.Deferrability argDeferrability)
           
 void setDeleteRule(SQLRelationship.UpdateDeleteRule rule)
           
 void setFkCardinality(int argFkCardinality)
          Sets the value of fkCardinality
 void setFkTable(SQLTable fkt)
           
 void setIdentifying(boolean argIdentifying)
          Sets the value of identifying, and moves the FK columns into or out of the FK Table's primary key as appropriate.
protected  void setParent(SQLObject newParent)
          This method is useful, and has side effects.
 void setPkCardinality(int argPkCardinality)
          Sets the value of pkCardinality
 void setPkTable(SQLTable pkt)
           
 void setUpdateRule(SQLRelationship.UpdateDeleteRule rule)
           
 java.lang.String toString()
           
 
Methods inherited from class ca.sqlpower.architect.SQLObject
addChild, addChild, addChildImpl, addSQLObjectListener, addSQLObjectPreEventListener, addUndoEventListener, endCompoundEdit, fireDbChildInserted, fireDbChildPreRemove, fireDbChildRemoved, fireDbChildrenInserted, fireDbChildrenPreRemove, fireDbChildrenRemoved, fireDbObjectChanged, fireDbStructureChanged, getChild, getChildByName, getChildByNameIgnoreCase, getChildCount, getChildren, getIndexOfChildByName, getName, getPhysicalName, getSQLObjectListeners, getSQLObjectPreEventListeners, getUndoEventListeners, isMagicEnabled, removeChild, removeChild, removeImpl, removeSQLObjectListener, removeSQLObjectPreEventListener, removeUndoEventListener, setMagicEnabled, setName, setPhysicalName, setPopulated, startCompoundEdit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final int ZERO
See Also:
Constant Field Values

ONE

public static final int ONE
See Also:
Constant Field Values

MANY

public static final int MANY
See Also:
Constant Field Values

PKCOLUMN

public static final int PKCOLUMN
See Also:
Constant Field Values

FKCOLUMN

public static final int FKCOLUMN
See Also:
Constant Field Values

pkTable

protected SQLTable pkTable

fkTable

protected SQLTable fkTable

updateRule

protected SQLRelationship.UpdateDeleteRule updateRule
The rule for what the DBMS should do to the child (imported) key value when its parent table (exported) key value changes.


deleteRule

protected SQLRelationship.UpdateDeleteRule deleteRule
The rule for what the DBMS should do to the child (imported) key value when its parent table (exported) row is deleted.


deferrability

protected SQLRelationship.Deferrability deferrability
The deferrability rule for constraint checking on this relationship. Defaults to NOT_DEFERRABLE.


pkCardinality

protected int pkCardinality

fkCardinality

protected int fkCardinality

identifying

protected boolean identifying
Value should be true if this relationship is identifying, and false if otherwise.

Here is our definition of identifying relationships and non-identifying relationships (as discussed in the Architect Developer's mailing list).

An 'identifying' relationship is: A foreign key relationship in which the whole primary key of the parent table is entirely contained in the primary key of the child table.

A 'non-identifying' relationship is: A foreign key relationship in which the whole primary key of the parent table is NOT entirely contained in the primary key of the child table.


physicalName

protected java.lang.String physicalName

fkColumnManager

protected SQLRelationship.RelationshipManager fkColumnManager
Constructor Detail

SQLRelationship

public SQLRelationship()

SQLRelationship

public SQLRelationship(SQLRelationship relationshipToCopy)
                throws ArchitectException
A copy constructor that returns a copy of the provided SQLRelationship with the following properties copied:
  • Name
  • Identifying status
  • Update rule
  • Delete rule
  • Deferrability
  • Parameters:
    relationshipToCopy - The SQLRelationship object to copy
    Throws:
    ArchitectException
    Method Detail

    getRelationshipManager

    public SQLRelationship.RelationshipManager getRelationshipManager()
    This method is for the benefit of the unit tests. It should never be necessary to use it in the real world.


    attachRelationship

    public void attachRelationship(SQLTable pkTable,
                                   SQLTable fkTable,
                                   boolean autoGenerateMapping)
                            throws ArchitectException
    Associates an SQLRelationship with the given SQLTable objects. Also automatically generates the PK to FK column mapping if autoGenerateMapping is set to true.

    Parameters:
    pkTable - The parent table in this relationship.
    fkTable - The child table in this relationship that contains the foreign key.
    autoGenerateMapping - Automatically generates the PK to FK column mapping if true
    Throws:
    ArchitectException

    getMappingByPkCol

    public SQLRelationship.ColumnMapping getMappingByPkCol(SQLColumn pkcol)

    containsPkColumn

    public boolean containsPkColumn(SQLColumn col)

    getMappingByFkCol

    public SQLRelationship.ColumnMapping getMappingByFkCol(SQLColumn fkcol)

    containsFkColumn

    public boolean containsFkColumn(SQLColumn col)

    getMappings

    public java.util.List<SQLRelationship.ColumnMapping> getMappings()
    Convenience method that casts children to List<ColumnMapping>.

    XXX: should be removed when SQLObject API gets generics


    printKeyColumns

    public java.lang.String printKeyColumns(int keyType)

    addMapping

    public void addMapping(SQLColumn pkColumn,
                           SQLColumn fkColumn)
                    throws ArchitectException
    Convenience method for adding a SQLRelationship.ColumnMapping child to this relationship.

    Throws:
    ArchitectException

    toString

    public java.lang.String toString()
    Overrides:
    toString in class java.lang.Object

    getParent

    public SQLObject getParent()
    Returns the table that holds the primary keys (the imported table).

    XXX this should return the parent folder of the pk table!

    Specified by:
    getParent in class SQLObject

    setParent

    protected void setParent(SQLObject newParent)
    This method is useful, and has side effects. setParent detaches the relation manager if newParent is null and reattaches the relation manager if newParent is not null

    Specified by:
    setParent in class SQLObject
    Parameters:
    newParent - If this is the same as pkTable or fkTable, this method returns normally. Otherwise, this method throws IllegalArgumentException.
    Throws:
    ArchitectException
    java.lang.IllegalArgumentException - if newParent is anything other than this relationship's pkTable.exportedKeysFolder or fkTable.importedKeysFolder

    getShortDisplayName

    public java.lang.String getShortDisplayName()
    Returns the foreign key name.

    Specified by:
    getShortDisplayName in class SQLObject

    allowsChildren

    public boolean allowsChildren()
    Relationships have ColumnMapping children.

    Specified by:
    allowsChildren in class SQLObject
    Returns:
    true

    populate

    public void populate()
    This class is not a lazy-loading class. This call does nothing.

    Specified by:
    populate in class SQLObject

    isPopulated

    public boolean isPopulated()
    Returns true.

    Overrides:
    isPopulated in class SQLObject

    getUpdateRule

    public SQLRelationship.UpdateDeleteRule getUpdateRule()

    setUpdateRule

    public void setUpdateRule(SQLRelationship.UpdateDeleteRule rule)

    getDeleteRule

    public SQLRelationship.UpdateDeleteRule getDeleteRule()

    setDeleteRule

    public void setDeleteRule(SQLRelationship.UpdateDeleteRule rule)

    getDeferrability

    public SQLRelationship.Deferrability getDeferrability()

    setDeferrability

    public void setDeferrability(SQLRelationship.Deferrability argDeferrability)

    getPkCardinality

    public int getPkCardinality()
    Gets the value of pkCardinality

    Returns:
    the value of pkCardinality

    setPkCardinality

    public void setPkCardinality(int argPkCardinality)
    Sets the value of pkCardinality

    Parameters:
    argPkCardinality - Value to assign to this.pkCardinality

    getFkCardinality

    public int getFkCardinality()
    Gets the value of fkCardinality

    Returns:
    the value of fkCardinality

    setFkCardinality

    public void setFkCardinality(int argFkCardinality)
    Sets the value of fkCardinality

    Parameters:
    argFkCardinality - Value to assign to this.fkCardinality

    isIdentifying

    public boolean isIdentifying()
    Gets the value of identifying

    Returns:
    the value of identifying

    setIdentifying

    public void setIdentifying(boolean argIdentifying)
                        throws ArchitectException
    Sets the value of identifying, and moves the FK columns into or out of the FK Table's primary key as appropriate.

    Parameters:
    argIdentifying - Value to assign to this.identifying
    Throws:
    ArchitectException

    getPkTable

    public SQLTable getPkTable()

    setPkTable

    public void setPkTable(SQLTable pkt)
                    throws ArchitectException
    Throws:
    ArchitectException

    getFkTable

    public SQLTable getFkTable()

    setFkTable

    public void setFkTable(SQLTable fkt)
                    throws ArchitectException
    Throws:
    ArchitectException

    getChildType

    public java.lang.Class<? extends SQLObject> getChildType()
    Specified by:
    getChildType in class SQLObject

    checkColumnLocked

    public void checkColumnLocked(SQLColumn col)
                           throws LockedColumnException
    Throws a column locked exception if col is in a columnmapping of this relationship

    Parameters:
    col -
    Throws:
    LockedColumnException

    determineIdentifyingStatus

    public boolean determineIdentifyingStatus()
                                       throws ArchitectException
    Some SQLRelationship objects may not have their identifying property set properly which is particularly the case then creating SQLRelationships for source database objects and then reverse engineering, so this method will determine for certain if a relationship is identifying or non-identifying. This is currently primarily being used for determining the identifying status of reverse-engineered relationships.

    Returns:
    True if this SQLRelationship is identifying. False if it is non-identifying.
    Throws:
    ArchitectException


    Copyright © 2003-2007 SQL Power Group Inc. www.sqlpower.ca