package org.hibernate.metamodel.relational;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.relational.state.ColumnRelationalState;
public class Column extends AbstractSimpleValue {
private final Identifier columnName;
private boolean nullable;
private boolean unique;
private String defaultValue;
private String checkCondition;
private String sqlType;
private String readFragment;
private String writeFragment;
private String comment;
private Size size = new Size();
protected Column(TableSpecification table, int position, String name) {
this( table, position, Identifier.toIdentifier( name ) );
}
protected Column(TableSpecification table, int position, Identifier name) {
super( table, position );
this.columnName = name;
}
public void initialize(ColumnRelationalState state, boolean forceNonNullable, boolean forceUnique) {
size.initialize( state.getSize() );
nullable = ! forceNonNullable && state.isNullable();
unique = ! forceUnique && state.isUnique();
checkCondition = state.getCheckCondition();
defaultValue = state.getDefault();
sqlType = state.getSqlType();
writeFragment = state.getCustomWriteFragment();
readFragment = state.getCustomReadFragment();
comment = state.getComment();
for ( String uniqueKey : state.getUniqueKeys() ) {
getTable().getOrCreateUniqueKey( uniqueKey ).addColumn( this );
}
for ( String index : state.getIndexes() ) {
getTable().getOrCreateIndex( index ).addColumn( this );
}
}
public Identifier getColumnName() {
return columnName;
}
public boolean isNullable() {
return nullable;
}
public void setNullable(boolean nullable) {
this.nullable = nullable;
}
public boolean isUnique() {
return unique;
}
public void setUnique(boolean unique) {
this.unique = unique;
}
public String getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
public String getCheckCondition() {
return checkCondition;
}
public void setCheckCondition(String checkCondition) {
this.checkCondition = checkCondition;
}
public String getSqlType() {
return sqlType;
}
public void setSqlType(String sqlType) {
this.sqlType = sqlType;
}
public String getReadFragment() {
return readFragment;
}
public void setReadFragment(String readFragment) {
this.readFragment = readFragment;
}
public String getWriteFragment() {
return writeFragment;
}
public void setWriteFragment(String writeFragment) {
this.writeFragment = writeFragment;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Size getSize() {
return size;
}
public void setSize(Size size) {
this.size = size;
}
@Override
public String toLoggableString() {
return getTable().getLoggableValueQualifier() + '.' + getColumnName();
}
@Override
public String getAlias(Dialect dialect) {
String alias = columnName.getName();
int lastLetter = StringHelper.lastIndexOfLetter( columnName.getName() );
if ( lastLetter == -1 ) {
alias = "column";
}
boolean useRawName =
columnName.getName().equals( alias ) &&
alias.length() <= dialect.getMaxAliasLength() &&
! columnName.isQuoted() &&
! columnName.getName().toLowerCase().equals( "rowid" );
if ( ! useRawName ) {
String unique =
new StringBuilder()
.append( getPosition() )
.append( '_' )
.append( getTable().getTableNumber() )
.append( '_' )
.toString();
if ( unique.length() >= dialect.getMaxAliasLength() ) {
throw new MappingException(
"Unique suffix [" + unique + "] length must be less than maximum [" + dialect.getMaxAliasLength() + "]"
);
}
if ( alias.length() + unique.length() > dialect.getMaxAliasLength()) {
alias = alias.substring( 0, dialect.getMaxAliasLength() - unique.length() );
}
alias = alias + unique;
}
return alias;
}
}