package com.quantum.model;

import com.quantum.sql.SQLHelper;

/**
 * @author BC
 */
class ColumnImpl implements Column, Comparable {

    private int size;
    private boolean nullable;
    private int primaryKeyOrder;
    private String name;
    private Entity entity;
    private int numberOfFractionalDigits;
    private String typeName;
    private int type;
    private int position;
    private String remarks;
    
    ColumnImpl(Entity entity, String name, String typeName, int type, 
        int size, int numberOfFractionalDigits, boolean nullable, int position,
        String remarks) {
           
        this.entity = entity;
        this.name = name;
        this.typeName = typeName;
        this.type = type;
        this.size = size;
        this.numberOfFractionalDigits = numberOfFractionalDigits;
        this.nullable = nullable;
        this.position = position;
        this.remarks = remarks;
    }

    /**
     * @see com.quantum.model.Column#getPrimaryKeyOrder()
     */
    public int getPrimaryKeyOrder() {
        return this.primaryKeyOrder;
    }

    /**
     * @see com.quantum.model.Column#isPrimaryKey()
     */
    public boolean isPrimaryKey() {
        return getPrimaryKeyOrder() > 0;
    }

    /**
     * @see com.quantum.model.Column#getName()
     */
    public String getName() {
        return this.name;
    }

    /**
     * @see com.quantum.model.Column#getTypeName()
     */
    public String getTypeName() {
        return this.typeName;
    }

    /**
     * @see com.quantum.model.Column#isReal()
     */
    public boolean isReal() {
        return false;
    }

    /**
     * @see com.quantum.model.Column#isNullable()
     */
    public boolean isNullable() {
        return this.nullable;
    }

    /**
     * @see com.quantum.model.Column#isNumeric()
     */
    public boolean isNumeric() {
        return SQLHelper.isNumeric(this.type);
    }

    /**
     * @see com.quantum.model.Column#getType()
     */
    public int getType() {
        return this.type;
    }

    /**
     * @see com.quantum.model.Column#getParentEntity()
     */
    public Entity getParentEntity() {
        return this.entity;
    }

    /**
     * @see com.quantum.model.Column#getQualifiedTableName()
     */
    public String getQualifiedTableName() {
        return this.entity.getQualifiedName();
    }
    /**
     * @param i
     */
    void setPrimaryKeyOrder(int i) {
        this.primaryKeyOrder = i;
    }

    /**
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(Object o) {
        ColumnImpl that = (ColumnImpl) o;
        if (this.isPrimaryKey() && that.isPrimaryKey()) {
            return this.primaryKeyOrder - that.primaryKeyOrder;
        } else if (this.isPrimaryKey()) {
            return -1;
        } else if (that.isPrimaryKey()) {
            return 1;
        } else {
            return this.position - that.position;
        }
    }
    /**
     * @return
     */
    public int getSize() {
        return size;
    }

    /**
     * @return
     */
    public int getNumberOfFractionalDigits() {
        return numberOfFractionalDigits;
    }

    /**
     * @return
     */
    public String getRemarks() {
        return this.remarks == null ? "" : this.remarks;
    }

}