Open Source Repository

Home /hibernate/hibernate-3.2.4.ga | Repository Home



org/hibernate/mapping/Column.java
//$Id: Column.java 11357 2007-03-29 01:42:35Z [email protected] $
package org.hibernate.mapping;

import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.function.SQLFunctionRegistry;
import org.hibernate.engine.Mapping;
import org.hibernate.util.StringHelper;

/**
 * A column of a relational database table
 @author Gavin King
 */
public class Column implements Selectable, Serializable, Cloneable {

  public static final int DEFAULT_LENGTH = 255;
  public static final int DEFAULT_PRECISION = 19;
  public static final int DEFAULT_SCALE = 2;

  private int length=DEFAULT_LENGTH;
  private int precision=DEFAULT_PRECISION;
  private int scale=DEFAULT_SCALE;
  private Value value;
  private int typeIndex = 0;
  private String name;
  private boolean nullable=true;
  private boolean unique=false;
  private String sqlType;
  private Integer sqlTypeCode;
  private boolean quoted=false;
  int uniqueInteger;
  private String checkConstraint;
  private String comment;
  private String defaultValue;

  public Column() { };

  public Column(String columnName) {
    setName(columnName);
  }

  public int getLength() {
    return length;
  }
  public void setLength(int length) {
    this.length = length;
  }
  public Value getValue() {
    return value;
  }
  public void setValue(Value value) {
    this.value= value;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    if (
      name.charAt(0)=='`' ||
      Dialect.QUOTE.indexOfname.charAt(0) ) > -//TODO: deprecated, remove eventually
    ) {
      quoted=true;
      this.name=name.substring1, name.length()-);
    }
    else {
      this.name = name;
    }
  }

  /** returns quoted name as it would be in the mapping file. */
  public String getQuotedName() {
    return quoted ?
        "`" + name + "`" :
        name;
  }

  public String getQuotedName(Dialect d) {
    return quoted ?
      d.openQuote() + name + d.closeQuote() :
      name;
  }
  
  /**
   * For any column name, generate an alias that is unique
   * to that column name, and also 10 characters or less
   * in length.
   */
  public String getAlias(Dialect dialect) {
    String alias = name;
    String unique = Integer.toString(uniqueInteger'_';
    int lastLetter = StringHelper.lastIndexOfLetter(name);
    if lastLetter == -) {
      alias = "column";
    }
    else if lastLetter < name.length()-) {
      alias = name.substring(0, lastLetter+1);
    }
    if alias.length() > dialect.getMaxAliasLength() ) {
      alias = alias.substring0, dialect.getMaxAliasLength() - unique.length() );
    }
    boolean useRawName = name.equals(alias&& 
      !quoted && 
      !name.toLowerCase().equals("rowid");
    if useRawName ) {
      return alias;
    }
    else {
      return alias + unique;
    }
  }
  
  /**
   * Generate a column alias that is unique across multiple tables
   */
  public String getAlias(Dialect dialect, Table table) {
    return getAlias(dialect+ table.getUniqueInteger() '_';
  }

  public boolean isNullable() {
    return nullable;
  }

  public void setNullable(boolean nullable) {
    this.nullable=nullable;
  }

  public int getTypeIndex() {
    return typeIndex;
  }
  public void setTypeIndex(int typeIndex) {
    this.typeIndex = typeIndex;
  }

  public int getSqlTypeCode(Mapping mappingthrows MappingException {
    org.hibernate.type.Type type = getValue().getType();
    try {
      int sqlTypeCode = type.sqlTypes(mapping)[ getTypeIndex() ];
      if(getSqlTypeCode()!=null && getSqlTypeCode().intValue()!=sqlTypeCode) {
        throw new MappingException("SQLType code's does not match. mapped as " + sqlTypeCode + " but is " + getSqlTypeCode() );
      }
      return sqlTypeCode;
    }
    catch (Exception e) {
      throw new MappingException(
          "Could not determine type for column " +
          name +
          " of type " +
          type.getClass().getName() +
          ": " +
          e.getClass().getName(),
          e
        );
    }
  }

  /**
   * Returns the underlying columns sqltypecode.
   * If null, it is because the sqltype code is unknown.
   
   * Use #getSqlTypeCode(Mapping) to retreive the sqltypecode used
   * for the columns associated Value/Type.
   
   @return sqltypecode if it is set, otherwise null.
   */
  public Integer getSqlTypeCode() {
    return sqlTypeCode;
  }
  
  public void setSqlTypeCode(Integer typecode) {
    sqlTypeCode=typecode;
  }
  
  public boolean isUnique() {
    return unique;
  }


  public String getSqlType(Dialect dialect, Mapping mappingthrows HibernateException {
    return sqlType==null ?
      dialect.getTypeNamegetSqlTypeCode(mapping), getLength(), getPrecision(), getScale() ) :
      sqlType;
  }

  public boolean equals(Object object) {
    return object instanceof Column && equals( (Columnobject );
  }

  public boolean equals(Column column) {
    if (null == columnreturn false;
    if (this == columnreturn true;

    return isQuoted() 
      name.equals(column.name:
      name.equalsIgnoreCase(column.name);
  }

  //used also for generation of FK names!
  public int hashCode() {
    return isQuoted() 
      name.hashCode() 
      name.toLowerCase().hashCode();
  }

  public String getSqlType() {
    return sqlType;
  }

  public void setSqlType(String sqlType) {
    this.sqlType = sqlType;
  }

  public void setUnique(boolean unique) {
    this.unique = unique;
  }

  public boolean isQuoted() {
    return quoted;
  }

  public String toString() {
    return getClass().getName() '(' + getName() ')';
  }

  public String getCheckConstraint() {
    return checkConstraint;
  }

  public void setCheckConstraint(String checkConstraint) {
    this.checkConstraint = checkConstraint;
  }

  public boolean hasCheckConstraint() {
    return checkConstraint!=null;
  }

  public String getTemplate(Dialect dialect, SQLFunctionRegistry functionRegistry) {
    return getQuotedName(dialect);
  }

  public boolean isFormula() {
    return false;
  }

  public String getText(Dialect d) {
    return getQuotedName(d);
  }
  public String getText() {
    return getName();
  }
  
  public int getPrecision() {
    return precision;
  }
  public void setPrecision(int scale) {
    this.precision = scale;
  }

  public int getScale() {
    return scale;
  }
  public void setScale(int scale) {
    this.scale = scale;
  }

  public String getComment() {
    return comment;
  }

  public void setComment(String comment) {
    this.comment = comment;
  }

  public String getDefaultValue() {
    return defaultValue;
  }

  public void setDefaultValue(String defaultValue) {
    this.defaultValue = defaultValue;
  }

  public String getCanonicalName() {
    return quoted ? name : name.toLowerCase();
  }

  /**
   * Shallow copy, the value is not copied
   */
  protected Object clone() {
    Column copy = new Column();
    copy.setLengthlength );
    copy.setScalescale );
    copy.setValuevalue );
    copy.setTypeIndextypeIndex );
    copy.setNamegetQuotedName() );
    copy.setNullablenullable );
    copy.setPrecisionprecision );
    copy.setUniqueunique );
    copy.setSqlTypesqlType );
    copy.setSqlTypeCodesqlTypeCode );
    copy.uniqueInteger = uniqueInteger; //usually useless
    copy.setCheckConstraintcheckConstraint );
    copy.setCommentcomment );
    copy.setDefaultValuedefaultValue );
    return copy;
  }

}