FileDocCategorySizeDatePackage
FieldDefinition.javaAPI DocGlassfish v2 API14268Tue May 22 16:54:54 BST 2007oracle.toplink.essentials.tools.schemaframework

FieldDefinition

public class FieldDefinition extends Object implements Serializable, Cloneable

Purpose: Define a database field definition for creation within a table. This differs from DatabaseField in that it is used only table creation not a runtime.

Responsibilities:

  • Store the name, java type, size and sub-size. The sizes are optional and the name of the java class is used for the type.

Fields Summary
protected String
name
protected Class
type
Java type class for the field. Particular database type is generated based on platform from this.
protected String
typeName
Generic database type name for the field, which can be used instead of the Java class 'type'. This is translated to a particular database type based on platform.
protected String
typeDefinition
Database-specific complete type definition like "VARCHAR2(50) UNIQUE NOT NULL". If this is given, other additional type constraint fields(size, unique, null) are meaningless.
protected int
size
protected int
subSize
protected boolean
shouldAllowNull
protected boolean
isIdentity
protected boolean
isPrimaryKey
protected boolean
isUnique
protected String
additional
protected String
constraint
protected String
foreignKeyFieldName
Constructors Summary
public FieldDefinition()

        this.name = "";
        this.size = 0;
        this.subSize = 0;
        this.shouldAllowNull = true;
        this.isIdentity = false;
        this.isPrimaryKey = false;
        this.isUnique = false;
    
public FieldDefinition(String name, Class type)

        this.name = name;
        this.type = type;
        this.size = 0;
        this.subSize = 0;
        shouldAllowNull = true;
        isIdentity = false;
        isPrimaryKey = false;
        isUnique = false;
    
public FieldDefinition(String name, Class type, int size)

        this();
        this.name = name;
        this.type = type;
        this.size = size;
    
public FieldDefinition(String name, Class type, int size, int subSize)

        this();
        this.name = name;
        this.type = type;
        this.size = size;
        this.subSize = subSize;
    
public FieldDefinition(String name, String typeName)

        this();
        this.name = name;
        this.typeName = typeName;
    
Methods Summary
public voidappendDBString(java.io.Writer writer, oracle.toplink.essentials.internal.sessions.AbstractSession session, oracle.toplink.essentials.tools.schemaframework.TableDefinition table)
INTERNAL: Append the database field definition string to the table creation statement.

        try {
            writer.write(getName());
            writer.write(" ");

            if (getTypeDefinition() != null) { //apply user-defined complete type definition
                writer.write(getTypeDefinition());

            } else {
                // compose type definition - type name, size, unique, identity, constraints...
                FieldTypeDefinition fieldType;
                
                if (getType() != null) { //translate Java 'type'
                    fieldType = session.getPlatform().getFieldTypeDefinition(getType());
                    if (fieldType == null) {
                        throw ValidationException.javaTypeIsNotAValidDatabaseType(getType());
                    }
                } else if (getTypeName() != null) { //translate generic type name
                    Hashtable fieldTypes = session.getPlatform().getClassTypes();
                    Class type = (Class)fieldTypes.get(getTypeName());
                    if (type == null) { // if unknown type name, use as it is
                        fieldType = new FieldTypeDefinition(getTypeName());
                    } else {
                        fieldType = session.getPlatform().getFieldTypeDefinition(type);
                        if (fieldType == null) {
                            throw ValidationException.javaTypeIsNotAValidDatabaseType(type);
                        }
                    }
                } else {
                    // both type and typeName is null
                    throw ValidationException.javaTypeIsNotAValidDatabaseType(null);
                }

                String qualifiedName = table.getFullName() + '." + getName();
                session.getPlatform().printFieldTypeSize(writer, this, fieldType, session, qualifiedName);
                session.getPlatform().printFieldUnique(writer, isUnique(), session, qualifiedName);        
                if (isIdentity()) {
                    String name = table.getFullName() + '." + getName();
                    session.getPlatform().printFieldIdentityClause(writer, session, name);
                }
                if (shouldAllowNull() && fieldType.shouldAllowNull()) {
                    session.getPlatform().printFieldNullClause(writer);
                } else {
                    session.getPlatform().printFieldNotNullClause(writer);
                }
                if (getConstraint() != null) {
                    writer.write(" " + getConstraint());
                }
                if (getAdditional() != null) {
                    writer.write(" " + getAdditional());
                }
            }
        } catch (IOException ioException) {
            throw ValidationException.fileError(ioException);
        }
    
public java.lang.Objectclone()
PUBLIC:

        try {
            return super.clone();
        } catch (CloneNotSupportedException impossible) {
            return null;
        }
    
public java.lang.StringgetAdditional()
PUBLIC: Return any additional information about this field to be given when the table is created.

        return additional;
    
public java.lang.StringgetConstraint()
PUBLIC: Return any constraint of this field. i.e. "BETWEEN 0 AND 1000000".

        return constraint;
    
public java.lang.StringgetForeignKeyFieldName()
PUBLIC: Return fully-qualified foreign key field name.

deprecated
Use ForeignKeyConstraint instead.

        return foreignKeyFieldName;
    
public java.lang.StringgetName()
PUBLIC: Return the name of the field.

        return name;
    
public intgetSize()
PUBLIC: Return the size of the field, this is only required for some field types.

        return size;
    
public intgetSubSize()
PUBLIC: Return the sub-size of the field. This is used as the decimal precision for numeric values only.

        return subSize;
    
public java.lang.ClassgetType()
PUBLIC: Return the type of the field. This should be set to a java class, such as String.class, Integer.class or Date.class.

        return type;
    
public java.lang.StringgetTypeDefinition()
PUBLIC: Return the type definition of the field. This is database-specific complete type definition like "VARCHAR2(50) UNIQUE NOT NULL". If this is given, other additional type constraint fields(size, unique, null) are meaningless.

        return typeDefinition;
    
public java.lang.StringgetTypeName()
PUBLIC: Return the type name of the field. This is the generic database type name, which can be used instead of the Java class 'type'. This is translated to a particular database type based on platform.

        return typeName;
    
public booleanisIdentity()
PUBLIC: Answer whether the receiver is an identity field. Identity fields are Sybase specific, they insure that on insert a unique sequencial value is store in the row.

        return isIdentity;
    
public booleanisPrimaryKey()
PUBLIC: Answer whether the receiver is a primary key. If the table has a multipart primary key this should be set in each field.

        return isPrimaryKey;
    
public booleanisUnique()
PUBLIC: Answer whether the receiver is a unique constraint field.

        return isUnique;
    
public voidsetAdditional(java.lang.String string)
PUBLIC: Set any additional information about this field to be given when the table is created.

        additional = string;
    
public voidsetConstraint(java.lang.String string)
PUBLIC: Set any constraint of this field. i.e. "BETWEEN 0 AND 1000000".

        constraint = string;
    
public voidsetForeignKeyFieldName(java.lang.String foreignKeyFieldName)
PUBLIC: Set the foreign key field name. This value is used for a foreign key constraint.

param
foreignKeyFieldName fully-qualified field name
deprecated
Use ForeignKeyConstraint instead.

        this.foreignKeyFieldName = foreignKeyFieldName;
    
public voidsetIsIdentity(boolean value)
PUBLIC: Set whether the receiver is an identity field. Identity fields are Sybase specific, they insure that on insert a unique sequencial value is store in the row.

        isIdentity = value;
        if (value) {
            setShouldAllowNull(false);
        }
    
public voidsetIsPrimaryKey(boolean value)
PUBLIC: Set whether the receiver is a primary key. If the table has a multipart primary key this should be set in each field.

        isPrimaryKey = value;
        if (value) {
            setShouldAllowNull(false);
        }
    
public voidsetName(java.lang.String name)
PUBLIC: Set the name of the field.

        this.name = name;
    
public voidsetShouldAllowNull(boolean value)
PUBLIC: Set whether the receiver should allow null values.

        shouldAllowNull = value;
    
public voidsetSize(int size)
PUBLIC: Set the size of the field, this is only required for some field types.

        this.size = size;
    
public voidsetSubSize(int subSize)
PUBLIC: Set the sub-size of the field. This is used as the decimal precision for numeric values only.

        this.subSize = subSize;
    
public voidsetType(java.lang.Class type)
PUBLIC: Set the type of the field. This should be set to a java class, such as String.class, Integer.class or Date.class.

        this.type = type;
    
public voidsetTypeDefinition(java.lang.String typeDefinition)
PUBLIC: Set the type definition of the field. This is database-specific complete type definition like "VARCHAR2(50) UNIQUE NOT NULL". If this is given, other additional type constraint fields(size, unique, null) are meaningless.

        this.typeDefinition = typeDefinition;
    
public voidsetTypeName(java.lang.String typeName)
PUBLIC: Set the type name of the field. This is the generic database type name, which can be used instead of the Java class 'type'. This is translated to a particular database type based on platform.

        this.typeName = typeName;
    
public voidsetUnique(boolean value)
PUBLIC: Set whether the receiver is a unique constraint field.

        isUnique = value;
    
public booleanshouldAllowNull()
PUBLIC: Return whether the receiver should allow null values.

        return shouldAllowNull;
    
public java.lang.StringtoString()

        return Helper.getShortClassName(getClass()) + "(" + getName() + "(" + getType() + "))";