de.mguennewig.pobjects.metadata
Class ForeignKeyConstraint

java.lang.Object
  extended by de.mguennewig.pobjects.metadata.Constraint
      extended by de.mguennewig.pobjects.metadata.ForeignKeyConstraint

public class ForeignKeyConstraint
extends Constraint

Defines a foreign key constraint for an n-tuple of attributes into another table.

Author:
Michael Günnewig
See Also:
ClassDecl.createImplicitConstraints(), ReferenceToClass

Field Summary
static int ON_DELETE_CASCADE
           
static int ON_DELETE_NO_ACTION
           
static int ON_DELETE_RESTRICT
           
static int ON_DELETE_SET_DEFAULT
           
static int ON_DELETE_SET_NULL
           
static int ON_UPDATE_NO_ACTION
           
static int ON_UPDATE_RESTRICT
           
static int ON_UPDATE_SET_DEFAULT
           
static int ON_UPDATE_SET_NULL
           
 
Fields inherited from class de.mguennewig.pobjects.metadata.Constraint
MEMBER_NAME
 
Constructor Summary
ForeignKeyConstraint(java.lang.String schemaName, Field[] sourceFields, ClassDecl targetClass, Field[] targetFields, boolean implicit)
          Creates a new ForeignKeyConstraint.
ForeignKeyConstraint(java.lang.String schemaName, Field[] sourceFields, ClassDecl targetClass, Field[] targetFields, int onDelete, int onUpdate, boolean implicit)
          Creates a new ForeignKeyConstraint.
ForeignKeyConstraint(java.lang.String schemaName, Field sourceField, ClassDecl targetClass)
          Creates a new foreign key constraint onto the ID field of the target class.
 
Method Summary
 void check(Record obj)
          Checks whether the constraint is fulfilled for the given object.
 int getNumFields()
           
 int getOnDelete()
           
 int getOnUpdate()
           
 Field getSourceField(int i)
           
 Field[] getSourceFields()
           
 ClassDecl getTargetClass()
           
 Field getTargetField(int i)
           
 Field[] getTargetFields()
           
 java.lang.String name(ClassDecl te, int variant)
          Returns different names depending on variant for the code generator.
 
Methods inherited from class de.mguennewig.pobjects.metadata.Constraint
getSchemaName, isImplicit
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ON_DELETE_NO_ACTION

public static final int ON_DELETE_NO_ACTION
See Also:
Constant Field Values

ON_DELETE_RESTRICT

public static final int ON_DELETE_RESTRICT
See Also:
Constant Field Values

ON_DELETE_CASCADE

public static final int ON_DELETE_CASCADE
See Also:
Constant Field Values

ON_DELETE_SET_NULL

public static final int ON_DELETE_SET_NULL
See Also:
Constant Field Values

ON_DELETE_SET_DEFAULT

public static final int ON_DELETE_SET_DEFAULT
See Also:
Constant Field Values

ON_UPDATE_NO_ACTION

public static final int ON_UPDATE_NO_ACTION
See Also:
Constant Field Values

ON_UPDATE_RESTRICT

public static final int ON_UPDATE_RESTRICT
See Also:
Constant Field Values

ON_UPDATE_SET_NULL

public static final int ON_UPDATE_SET_NULL
See Also:
Constant Field Values

ON_UPDATE_SET_DEFAULT

public static final int ON_UPDATE_SET_DEFAULT
See Also:
Constant Field Values
Constructor Detail

ForeignKeyConstraint

public ForeignKeyConstraint(java.lang.String schemaName,
                            Field[] sourceFields,
                            ClassDecl targetClass,
                            Field[] targetFields,
                            boolean implicit)
Creates a new ForeignKeyConstraint.

Parameters:
schemaName - The constraint name within the database schema.
sourceFields - The set of fields which references a row in the target class.
targetClass - The table referenced by this foreign key.
targetFields - The set of fields which build a primary key in the target class.
implicit - Whether this constraint has been created implicit.
Throws:
java.lang.IllegalArgumentException - if any parameter is null.
java.lang.IllegalArgumentException - if the field lists are empty or of unequal size.

ForeignKeyConstraint

public ForeignKeyConstraint(java.lang.String schemaName,
                            Field[] sourceFields,
                            ClassDecl targetClass,
                            Field[] targetFields,
                            int onDelete,
                            int onUpdate,
                            boolean implicit)
Creates a new ForeignKeyConstraint.

Parameters:
schemaName - The constraint name within the database schema.
sourceFields - The set of fields which references a row in the target class.
targetClass - The table referenced by this foreign key.
targetFields - The set of fields which build a primary key in the target class.
onDelete - Specifies what the DBMS should do if the referenced row is deleted. Defaults to ON_DELETE_NO_ACTION.
onUpdate - Specifies what the DBMS should do if the referenced row is updated. Defaults to ON_UPDATE_NO_ACTION.
implicit - Whether this constraint has been created implicit.
Throws:
java.lang.IllegalArgumentException - if any parameter is null.
java.lang.IllegalArgumentException - if the field lists are empty or of unequal size.

ForeignKeyConstraint

public ForeignKeyConstraint(java.lang.String schemaName,
                            Field sourceField,
                            ClassDecl targetClass)
Creates a new foreign key constraint onto the ID field of the target class.

This is a shortcut for ForeignKeyConstraint(schemaName, new Field[]{ f }, target Class, new Field[]{ targetClass.getIdField() }, ((RefType)sourceField.getType()).getOnDelete(), ((RefType)sourceField.getType()).getOnUpdate(),true).

Parameters:
schemaName - The constraint name within the database schema.
sourceField - The field which references a row in the target class.
targetClass - The table referenced by this foreign key.
Throws:
java.lang.IllegalArgumentException - if any parameter is null.
java.lang.IllegalArgumentException - if the field lists are empty or of unequal size.
java.lang.NullPointerException - if targetClass is null.
See Also:
ForeignKeyConstraint(String,Field[],ClassDecl,Field[],boolean)
Method Detail

getNumFields

public final int getNumFields()

getSourceField

public final Field getSourceField(int i)

getSourceFields

public final Field[] getSourceFields()

getTargetClass

public final ClassDecl getTargetClass()

getTargetField

public final Field getTargetField(int i)

getTargetFields

public final Field[] getTargetFields()

getOnDelete

public final int getOnDelete()

getOnUpdate

public final int getOnUpdate()

check

public void check(Record obj)
           throws PObjConstraintException
Checks whether the constraint is fulfilled for the given object.

Currently checks only constraints based on 1 field which must be refer to another object. Only in this case we have the object and can check for persistence (existence).

Specified by:
check in class Constraint
Throws:
PObjConstraintException - when the constraint isn't fulfilled.

name

public java.lang.String name(ClassDecl te,
                             int variant)
Returns different names depending on variant for the code generator.

Specified by:
name in class Constraint
Returns:
variant return value
Constraint.MEMBER_NAME fkey<TableName><SourceFields>