de.mguennewig.pobjects.metadata
Class RefType

java.lang.Object
  extended by de.mguennewig.pobjects.metadata.AbstractType
      extended by de.mguennewig.pobjects.metadata.ScalarType
          extended by de.mguennewig.pobjects.metadata.RefType
All Implemented Interfaces:
ReferenceToClass, Type

public class RefType
extends ScalarType
implements ReferenceToClass

Reference to an object.

In SQL terms, this is a foreign key constraint on the id attribute of a table. In the application it is a pointer to an object.

A foreign key constraint will be added automatically to class declarations.

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

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 interface de.mguennewig.pobjects.metadata.Type
CLASS_NAME, TYPE_NAME
 
Constructor Summary
RefType(java.lang.String targetModule, java.lang.String targetName, ClassDecl targetClass)
          Creates a new reference with default actions for ON DELETE and ON UPDATE.
RefType(java.lang.String targetModule, java.lang.String targetName, ClassDecl targetClass, int onDelete)
          Creates a new reference with default action for ON UPDATE.
RefType(java.lang.String targetModule, java.lang.String targetName, ClassDecl targetClass, int onDelete, int onUpdate)
          Creates a new reference.
 
Method Summary
 void addMap(MapItem item)
          Adds a mapping to this type.
 void checkConstraints(Record rec, Column column)
          Checks all constraints imposed on values of this type.
 java.lang.String formatNative(java.lang.Object value)
          Map value from its `native' string representation to its internal representation based on the type.
 java.lang.Class<?> getJavaClass()
          Returns the Java class instance that is being used for the values of this type.
 int getMaxFieldSize()
          Returns the maximum field size required to enter a value of this type.
 int getOnDelete()
           
 int getOnUpdate()
           
 java.lang.Object getResult(JdbcContainer db, java.sql.ResultSet rs, int column)
          Reads the value for this type from the result set.
 int getResultInc()
          Returns the number of columns read from the ResultSet.
 ClassDecl getTargetClass()
          Returns the class declaration of the referenced table.
 java.lang.String getTargetModule()
          Returns the name of the module to which the referenced table belongs.
 java.lang.String getTargetName()
          Returns the name of the class the pointer (or foreign key) refers to.
 java.lang.String name(int variant)
          Returns different names depending on variant for the code generator.
 java.lang.Object parseNative(java.lang.String value)
          Parse value according to the `native' string representation of this type.
 void setParam(JdbcContainer db, java.sql.PreparedStatement statm, int pos, java.lang.Object value)
          Sets the parameter(s) starting at startPos.
 void setTargetClass(ClassDecl cl)
          Sets the class declaration of the referenced table.
 
Methods inherited from class de.mguennewig.pobjects.metadata.ScalarType
checkSyntax, expand, getMap, isScalar, labelToValue, valueToLabel
 
Methods inherited from class de.mguennewig.pobjects.metadata.AbstractType
format, getDescription, getTypeClass, parse, setDescription, setTypeClass
 
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

RefType

public RefType(java.lang.String targetModule,
               java.lang.String targetName,
               ClassDecl targetClass)
Creates a new reference with default actions for ON DELETE and ON UPDATE.

Parameters:
targetModule - The name of the module to which the referenced table belongs.
targetName - The name of the class the pointer (or foreign key) refers to.
targetClass - The class declaration of the referenced table. This must be given or null with a later call to setTargetClass(de.mguennewig.pobjects.metadata.ClassDecl) before any usage of this type.
See Also:
RefType(String,String,ClassDecl,int, int), setTargetClass(de.mguennewig.pobjects.metadata.ClassDecl)

RefType

public RefType(java.lang.String targetModule,
               java.lang.String targetName,
               ClassDecl targetClass,
               int onDelete)
Creates a new reference with default action for ON UPDATE.

Parameters:
targetModule - The name of the module to which the referenced table belongs.
targetName - The name of the class the pointer (or foreign key) refers to.
targetClass - The class declaration of the referenced table. This must be given or null with a later call to setTargetClass(de.mguennewig.pobjects.metadata.ClassDecl) before any usage of this type.
onDelete - Specifies what the DBMS should do if the referenced row is deleted.
See Also:
RefType(String,String,ClassDecl,int, int), setTargetClass(de.mguennewig.pobjects.metadata.ClassDecl)

RefType

public RefType(java.lang.String targetModule,
               java.lang.String targetName,
               ClassDecl targetClass,
               int onDelete,
               int onUpdate)
Creates a new reference.

Parameters:
targetModule - The name of the module to which the referenced table belongs.
targetName - The name of the class the pointer (or foreign key) refers to.
targetClass - The class declaration of the referenced table. This must be given or null with a later call to setTargetClass(de.mguennewig.pobjects.metadata.ClassDecl) before any usage of this type.
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.
See Also:
setTargetClass(de.mguennewig.pobjects.metadata.ClassDecl)
Method Detail

getOnDelete

public final int getOnDelete()

getOnUpdate

public final int getOnUpdate()

getTargetModule

public final java.lang.String getTargetModule()
Returns the name of the module to which the referenced table belongs.

Specified by:
getTargetModule in interface ReferenceToClass
See Also:
ReferenceToClass.getTargetName(), ReferenceToClass.getTargetClass()

getTargetName

public final java.lang.String getTargetName()
Returns the name of the class the pointer (or foreign key) refers to.

Specified by:
getTargetName in interface ReferenceToClass

getTargetClass

public final ClassDecl getTargetClass()
Returns the class declaration of the referenced table.

Specified by:
getTargetClass in interface ReferenceToClass
See Also:
ReferenceToClass.setTargetClass(ClassDecl), ReferenceToClass.getTargetName()

setTargetClass

public final void setTargetClass(ClassDecl cl)
Sets the class declaration of the referenced table.

A call to this method is only necessary if the class declaration has not been passed to the constructor to solve circle problems.

Specified by:
setTargetClass in interface ReferenceToClass

addMap

public void addMap(MapItem item)
Adds a mapping to this type.

Specified by:
addMap in interface Type
Overrides:
addMap in class ScalarType
Throws:
java.lang.UnsupportedOperationException - always as the values of this type can't be mapped.
See Also:
Type.getJavaClass()

checkConstraints

public void checkConstraints(Record rec,
                             Column column)
Checks all constraints imposed on values of this type.

This method does nothing in the current implementation.

Specified by:
checkConstraints in interface Type
Overrides:
checkConstraints in class ScalarType
Parameters:
rec - The record which contains column.
column - The column of rec which has this type.

getJavaClass

public java.lang.Class<?> getJavaClass()
Returns the Java class instance that is being used for the values of this type.

Specified by:
getJavaClass in interface Type

getMaxFieldSize

public int getMaxFieldSize()
Returns the maximum field size required to enter a value of this type.

This is the maximum number of characters required to enter a string representation of all valid values of this type. The typical size of an input field may be much smaller.

Specified by:
getMaxFieldSize in interface Type
Overrides:
getMaxFieldSize in class ScalarType

formatNative

public java.lang.String formatNative(java.lang.Object value)
Map value from its `native' string representation to its internal representation based on the type.

This mapping is a one-to-one mapping of string values to the type's value set, without any translation based on MapItems associated with the type.

For example, an integer type's implementation would accept a value like `1' and translate it to the string `&dblquote;1&dblquote;', independent of any label associated with the value `1'.

The value null is always mapped to the empty string.

Specified by:
formatNative in interface Type
Overrides:
formatNative in class AbstractType
See Also:
Type.getJavaClass()

parseNative

public java.lang.Object parseNative(java.lang.String value)
Parse value according to the `native' string representation of this type.

This representation is a one-to-one mapping of the type's value set to strings, without any translation based on MapItems associated with the type.

For example, an integer type's implementation would accept a value like `"1"' and translate it to an integer object, but would reject a label like `"Yes"' that may be associated with the value `1'.

This method only parses the null value and throws an error for all other values, because the value can be an Id or the value of the visual field or even a user defined value.

Specified by:
parseNative in interface Type
Throws:
java.lang.UnsupportedOperationException - when the parameter does not represent the null.
See Also:
Type.addMap(MapItem), Type.parse(String)

getResult

public java.lang.Object getResult(JdbcContainer db,
                                  java.sql.ResultSet rs,
                                  int column)
Reads the value for this type from the result set.

Specified by:
getResult in interface Type
Overrides:
getResult in class ScalarType
See Also:
Type.getResultInc()

getResultInc

public int getResultInc()
Returns the number of columns read from the ResultSet.

Specified by:
getResultInc in interface Type
Overrides:
getResultInc in class ScalarType
See Also:
Type.getResult(JdbcContainer,ResultSet,int)

setParam

public void setParam(JdbcContainer db,
                     java.sql.PreparedStatement statm,
                     int pos,
                     java.lang.Object value)
              throws java.sql.SQLException
Sets the parameter(s) starting at startPos.

This method is the counterpart to Type.getResult(JdbcContainer,ResultSet,int) and Type.getResultInc().

Specified by:
setParam in interface Type
Throws:
java.sql.SQLException - if any database error occurs.

name

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

Specified by:
name in interface Type
Overrides:
name in class AbstractType