Skip to content

Instantly share code, notes, and snippets.

@sebersole
Created October 26, 2015 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebersole/ec53bbc02419fb261825 to your computer and use it in GitHub Desktop.
Save sebersole/ec53bbc02419fb261825 to your computer and use it in GitHub Desktop.
Index: hibernate-core/src/main/java/org/hibernate/mapping/Column.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- hibernate-core/src/main/java/org/hibernate/mapping/Column.java (date 1445457150000)
+++ hibernate-core/src/main/java/org/hibernate/mapping/Column.java (revision )
@@ -9,11 +9,11 @@
import java.io.Serializable;
import java.util.Locale;
-import org.hibernate.HibernateException;
import org.hibernate.MappingException;
+import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.function.SQLFunctionRegistry;
-import org.hibernate.engine.spi.Mapping;
+import org.hibernate.engine.jdbc.Size;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.sql.Template;
@@ -21,6 +21,7 @@
* A column of a relational database table
*
* @author Gavin King
+ * @author Steve Ebersole
*/
public class Column implements Selectable, Serializable, Cloneable {
@@ -28,118 +29,118 @@
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;
- private String name;
+ private Identifier logicalName;
+ private Identifier physicalName;
+
+ private boolean isIdentity;
+
+ private String sqlType;
+ private Integer jdbcTypeCode;
+ private final Size size = new Size();
+
private boolean nullable = true;
private boolean unique;
- private String sqlType;
- private Integer sqlTypeCode;
- private boolean quoted;
- int uniqueInteger;
private String checkConstraint;
private String comment;
private String defaultValue;
private String customWrite;
private String customRead;
+ int uniqueInteger;
+
+
public Column() {
}
- public Column(String columnName) {
- setName( columnName );
+ /**
+ * Get the logical name of this column. This is the name (implicitly or explicitly)
+ * from the mappings before any physical naming strategy transformations are applied.
+ *
+ * @return The logical name.
+ *
+ * @see #getPhysicalName()
+ */
+ public Identifier getLogicalName() {
+ return logicalName;
}
- public int getLength() {
- return length;
+ public void setLogicalName(Identifier logicalName) {
+ this.logicalName = logicalName;
}
- public void setLength(int length) {
- this.length = length;
+ /**
+ * Get the physical name of this column. This is the name of the column as it is
+ * in the underlying database table.
+ *
+ * @return The physical column name
+ *
+ * @see #getLogicalName()
+ */
+ public Identifier getPhysicalName() {
+ return physicalName;
}
- public Value getValue() {
- return value;
+ public void setPhysicalName(Identifier physicalName) {
+ this.physicalName = physicalName;
}
- public void setValue(Value value) {
- this.value = value;
+ /**
+ * Is this column an IDENTITY value?
+ *
+ * @return {@code true} indicates it is an IDENTITY value; {@code false} indicates
+ * it is not
+ */
+ public boolean isIdentity() {
+ return isIdentity;
}
- public String getName() {
- return name;
+ public void markAsIdentity(boolean isIdentity) {
+ this.isIdentity = isIdentity;
}
- public void setName(String name) {
- if (
- StringHelper.isNotEmpty( name ) &&
- Dialect.QUOTE.indexOf( name.charAt( 0 ) ) > -1 //TODO: deprecated, remove eventually
- ) {
- quoted = true;
- this.name = name.substring( 1, name.length() - 1 );
- }
- else {
- this.name = name;
- }
- }
-
/**
- * returns quoted name as it would be in the mapping file.
+ * The database-specific SQL type of the column as used to create it.
+ *
+ * @return The database-specific SQL type
*/
- public String getQuotedName() {
- return quoted ?
- "`" + name + "`" :
- name;
+ public String getSqlType() {
+ return sqlType;
}
- public String getQuotedName(Dialect d) {
- return quoted ?
- d.openQuote() + name + d.closeQuote() :
- name;
+ public void setSqlType(String sqlType) {
+ this.sqlType = sqlType;
}
- @Override
- public String getAlias(Dialect dialect) {
- final int lastLetter = StringHelper.lastIndexOfLetter( name );
- final String suffix = Integer.toString( uniqueInteger ) + '_';
-
- String alias = name;
- if ( lastLetter == -1 ) {
- alias = "column";
+ /**
+ * Returns the JDBC type code of the column. If {@code null}, the type code
+ * is not (yet) known.
+ *
+ * @return The JDBC type code.
+ *
+ * @see java.sql.Types
+ */
+ public Integer getJdbcTypeCode() {
+ return jdbcTypeCode;
- }
+ }
- else if ( name.length() > lastLetter + 1 ) {
- alias = name.substring( 0, lastLetter + 1 );
- }
- boolean useRawName = name.length() + suffix.length() <= dialect.getMaxAliasLength()
- && !quoted && !name.toLowerCase( Locale.ROOT ).equals( "rowid" );
- if ( !useRawName ) {
- if ( suffix.length() >= dialect.getMaxAliasLength() ) {
- throw new MappingException(
- String.format(
- "Unique suffix [%s] length must be less than maximum [%d]",
- suffix, dialect.getMaxAliasLength()
- )
- );
+ public void setJdbcTypeCode(Integer typeCode) {
+ jdbcTypeCode = typeCode;
- }
+ }
- if ( alias.length() + suffix.length() > dialect.getMaxAliasLength() ) {
- alias = alias.substring( 0, dialect.getMaxAliasLength() - suffix.length() );
- }
- }
- return alias + suffix;
- }
/**
- * Generate a column alias that is unique across multiple tables
+ * The sizing arguments for the JDBC type code.
+ *
+ * @return The column type size arguments
*/
- @Override
- public String getAlias(Dialect dialect, Table table) {
- return getAlias( dialect ) + table.getUniqueInteger() + '_';
+ public Size getSize() {
+ return size;
}
+ /**
+ * Does the column allow {@code NULL} values?
+ *
+ * @return {@code true} indicates it does allow {@code NULL}; {@code false} indicates it does not
+ */
public boolean isNullable() {
return nullable;
}
@@ -148,129 +149,120 @@
this.nullable = nullable;
}
- public int getTypeIndex() {
- return typeIndex;
+ /**
+ * Is the column (by itself) unique?
+ *
+ * @return {@code true} indicates it is unique; {@code false} indicates it is not.
+ */
+ public boolean isUnique() {
+ return unique;
}
- public void setTypeIndex(int typeIndex) {
- this.typeIndex = typeIndex;
+ public void setUnique(boolean unique) {
+ this.unique = unique;
}
- public boolean isUnique() {
- return unique;
+ public String getCheckConstraint() {
+ return checkConstraint;
}
- @Override
- public int hashCode() {
- //used also for generation of FK names!
- return isQuoted() ?
- name.hashCode() :
- name.toLowerCase( Locale.ROOT ).hashCode();
+ public void setCheckConstraint(String checkConstraint) {
+ this.checkConstraint = checkConstraint;
}
- @Override
- public boolean equals(Object object) {
- return object instanceof Column && equals( (Column) object );
+ public boolean hasCheckConstraint() {
+ return checkConstraint != null;
}
- @SuppressWarnings("SimplifiableIfStatement")
- public boolean equals(Column column) {
- if ( null == column ) {
- return false;
+ public String getComment() {
+ return comment;
- }
+ }
- if ( this == column ) {
- return true;
- }
- return isQuoted() ?
- name.equals( column.name ) :
- name.equalsIgnoreCase( column.name );
+ public void setComment(String comment) {
+ this.comment = comment;
}
- public int getSqlTypeCode(Mapping mapping) throws MappingException {
- org.hibernate.type.Type type = getValue().getType();
- try {
- int sqlTypeCode = type.sqlTypes( mapping )[getTypeIndex()];
- if ( getSqlTypeCode() != null && getSqlTypeCode() != sqlTypeCode ) {
- throw new MappingException( "SQLType code's does not match. mapped as " + sqlTypeCode + " but is " + getSqlTypeCode() );
+ public String getDefaultValue() {
+ return defaultValue;
- }
+ }
- 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.
- * <p/>
- * 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 setDefaultValue(String defaultValue) {
+ this.defaultValue = defaultValue;
}
- public void setSqlTypeCode(Integer typeCode) {
- sqlTypeCode = typeCode;
+ public String getCustomWrite() {
+ return customWrite;
}
- public String getSqlType(Dialect dialect, Mapping mapping) throws HibernateException {
- if ( sqlType == null ) {
- sqlType = dialect.getTypeName( getSqlTypeCode( mapping ), getLength(), getPrecision(), getScale() );
+ public void setCustomWrite(String customWrite) {
+ this.customWrite = customWrite;
- }
+ }
- return sqlType;
- }
- public String getSqlType() {
- return sqlType;
+ public String getCustomRead() {
+ return customRead;
}
- public void setSqlType(String sqlType) {
- this.sqlType = sqlType;
+ public void setCustomRead(String customRead) {
+ this.customRead = customRead;
}
- public void setUnique(boolean unique) {
- this.unique = unique;
+ @Override
+ public boolean isFormula() {
+ return false;
}
- public boolean isQuoted() {
- return quoted;
+ @Override
+ public String getText(Dialect d) {
+ return getPhysicalName().render( d );
}
@Override
- public String toString() {
- return getClass().getName() + '(' + getName() + ')';
+ public String getText() {
+ return getPhysicalName().render();
}
- public String getCheckConstraint() {
- return checkConstraint;
+ @Override
+ public String getAlias(Dialect dialect) {
+ final String name = physicalName.render( dialect );
+ final int lastLetter = StringHelper.lastIndexOfLetter( name );
+ final String suffix = Integer.toString( uniqueInteger ) + '_';
+
+ String alias = name;
+ if ( lastLetter == -1 ) {
+ alias = "column";
- }
+ }
+ else if ( name.length() > lastLetter + 1 ) {
+ alias = name.substring( 0, lastLetter + 1 );
+ }
- public void setCheckConstraint(String checkConstraint) {
- this.checkConstraint = checkConstraint;
+ boolean useRawName = name.length() + suffix.length() <= dialect.getMaxAliasLength()
+ && !physicalName.isQuoted() && !name.toLowerCase( Locale.ROOT ).equals( "rowid" );
+ if ( !useRawName ) {
+ if ( suffix.length() >= dialect.getMaxAliasLength() ) {
+ throw new MappingException(
+ String.format(
+ "Unique suffix [%s] length must be less than maximum [%d]",
+ suffix, dialect.getMaxAliasLength()
+ )
+ );
- }
+ }
+ if ( alias.length() + suffix.length() > dialect.getMaxAliasLength() ) {
+ alias = alias.substring( 0, dialect.getMaxAliasLength() - suffix.length() );
+ }
+ }
+ return alias + suffix;
+ }
- public boolean hasCheckConstraint() {
- return checkConstraint != null;
+ @Override
+ public String getAlias(Dialect dialect, Table table) {
+ return getAlias( dialect ) + table.getUniqueInteger() + '_';
}
@Override
public String getTemplate(Dialect dialect, SQLFunctionRegistry functionRegistry) {
return hasCustomRead()
? Template.renderWhereStringTemplate( customRead, dialect, functionRegistry )
- : Template.TEMPLATE + '.' + getQuotedName( dialect );
+ : Template.TEMPLATE + '.' + getPhysicalName().render( dialect );
}
public boolean hasCustomRead() {
@@ -278,7 +270,7 @@
}
public String getReadExpr(Dialect dialect) {
- return hasCustomRead() ? customRead : getQuotedName( dialect );
+ return hasCustomRead() ? customRead : getPhysicalName().render( dialect );
}
public String getWriteExpr() {
@@ -286,94 +278,54 @@
}
@Override
- public boolean isFormula() {
- return false;
+ public int hashCode() {
+ return physicalName.render().hashCode();
}
@Override
- public String getText(Dialect d) {
- return getQuotedName( d );
+ public boolean equals(Object object) {
+ return object instanceof Column && equals( (Column) object );
}
- @Override
- public String getText() {
- return getName();
+ @SuppressWarnings("SimplifiableIfStatement")
+ public boolean equals(Column column) {
+ if ( null == column ) {
+ return false;
- }
+ }
-
- public int getPrecision() {
- return precision;
+ if ( this == column ) {
+ return true;
- }
+ }
- public void setPrecision(int scale) {
- this.precision = scale;
+ return physicalName.equals( column.physicalName );
}
- public int getScale() {
- return scale;
+ @Override
+ public String toString() {
+ return "Column(" + physicalName.render() + ')';
}
- public void setScale(int scale) {
- this.scale = scale;
- }
+ public Column makeCopy() {
+ Column copy = new Column();
+ copy.setLogicalName( getLogicalName() );
+ copy.setPhysicalName( getPhysicalName() );
+ copy.markAsIdentity( isIdentity() );
+ copy.setJdbcTypeCode( getJdbcTypeCode() );
+ copy.setSqlType( getSqlType() );
+ copy.getSize().setLength( getSize().getLength() );
+ copy.getSize().setPrecision( getSize().getPrecision() );
+ copy.getSize().setScale( getSize().getScale() );
+ copy.getSize().setLobMultiplier( getSize().getLobMultiplier() );
+ copy.setNullable( isNullable() );
+ copy.setUnique( isUnique() );
+ copy.setCheckConstraint( getCheckConstraint() );
+ copy.setComment( getComment() );
+ copy.setDefaultValue( getDefaultValue() );
+ copy.setCustomRead( getCustomRead() );
+ copy.setCustomWrite( getCustomWrite() );
- public String getComment() {
- return comment;
- }
+ //usually useless
+ copy.uniqueInteger = uniqueInteger;
- public void setComment(String comment) {
- this.comment = comment;
- }
-
- public String getDefaultValue() {
- return defaultValue;
- }
-
- public void setDefaultValue(String defaultValue) {
- this.defaultValue = defaultValue;
- }
-
- public String getCustomWrite() {
- return customWrite;
- }
-
- public void setCustomWrite(String customWrite) {
- this.customWrite = customWrite;
- }
-
- public String getCustomRead() {
- return customRead;
- }
-
- public void setCustomRead(String customRead) {
- this.customRead = customRead;
- }
-
- public String getCanonicalName() {
- return quoted ? name : name.toLowerCase( Locale.ROOT );
- }
-
- /**
- * Shallow copy, the value is not copied
- */
- @Override
- public Column clone() {
- Column copy = new Column();
- copy.setLength( length );
- copy.setScale( scale );
- copy.setValue( value );
- copy.setTypeIndex( typeIndex );
- copy.setName( getQuotedName() );
- copy.setNullable( nullable );
- copy.setPrecision( precision );
- copy.setUnique( unique );
- copy.setSqlType( sqlType );
- copy.setSqlTypeCode( sqlTypeCode );
- copy.uniqueInteger = uniqueInteger; //usually useless
- copy.setCheckConstraint( checkConstraint );
- copy.setComment( comment );
- copy.setDefaultValue( defaultValue );
- copy.setCustomRead( customRead );
- copy.setCustomWrite( customWrite );
return copy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment