1 package org.hibernate.mapping; 3 4 import java.io.Serializable ; 5 6 import org.hibernate.HibernateException; 7 import org.hibernate.MappingException; 8 import org.hibernate.dialect.Dialect; 9 import org.hibernate.engine.Mapping; 10 import org.hibernate.util.StringHelper; 11 12 16 public class Column implements Selectable, Serializable { 17 18 public static final int DEFAULT_LENGTH = 255; 19 public static final int DEFAULT_PRECISION = 19; 20 public static final int DEFAULT_SCALE = 2; 21 22 private int length=DEFAULT_LENGTH; 23 private int precision=DEFAULT_PRECISION; 24 private int scale=DEFAULT_SCALE; 25 private Value value; 26 private int typeIndex = 0; 27 private String name; 28 private boolean nullable=true; 29 private boolean unique=false; 30 private String sqlType; 31 private Integer sqlTypeCode; 32 private boolean quoted=false; 33 int uniqueInteger; 34 private String checkConstraint; 35 private String comment; 36 37 public Column() { }; 38 39 public Column(String columnName) { 40 setName(columnName); 41 } 42 43 public int getLength() { 44 return length; 45 } 46 public void setLength(int length) { 47 this.length = length; 48 } 49 public Value getValue() { 50 return value; 51 } 52 public void setValue(Value value) { 53 this.value= value; 54 } 55 public String getName() { 56 return name; 57 } 58 public void setName(String name) { 59 if ( 60 name.charAt(0)=='`' | 61 Dialect.QUOTE.indexOf( name.charAt(0) ) > -1 ) { 63 quoted=true; 64 this.name=name.substring( 1, name.length()-1 ); 65 } 66 else { 67 this.name = name; 68 } 69 } 70 public String getQuotedName(Dialect d) { 71 return quoted ? 72 d.openQuote() + name + d.closeQuote() : 73 name; 74 } 75 76 81 public String getAlias(Dialect dialect) { 82 String alias = name; 83 String unique = Integer.toString(uniqueInteger) + '_'; 84 int lastLetter = StringHelper.lastIndexOfLetter(name); 85 if ( lastLetter == -1 ) { 86 alias = "column"; 87 } 88 else if ( lastLetter < name.length()-1 ) { 89 alias = name.substring(0, lastLetter+1); 90 } 91 if ( alias.length() > dialect.getMaxAliasLength() ) { 92 alias = alias.substring( 0, dialect.getMaxAliasLength() - unique.length() ); 93 } 94 boolean useRawName = name.equals(alias) && 95 !quoted && 96 !name.toLowerCase().equals("rowid"); 97 if ( useRawName ) { 98 return alias; 99 } 100 else { 101 return alias + unique; 102 } 103 } 104 105 108 public String getAlias(Dialect dialect, Table table) { 109 return getAlias(dialect) + table.getUniqueInteger() + '_'; 110 } 111 112 public boolean isNullable() { 113 return nullable; 114 } 115 116 public void setNullable(boolean nullable) { 117 this.nullable=nullable; 118 } 119 120 public int getTypeIndex() { 121 return typeIndex; 122 } 123 public void setTypeIndex(int typeIndex) { 124 this.typeIndex = typeIndex; 125 } 126 127 public int getSqlTypeCode(Mapping mapping) throws MappingException { 128 org.hibernate.type.Type type = getValue().getType(); 129 try { 130 int sqlTypeCode = type.sqlTypes(mapping)[ getTypeIndex() ]; 131 if(getSqlTypeCode()!=null && getSqlTypeCode().intValue()!=sqlTypeCode) { 132 throw new MappingException("SQLType code's does not match. mapped as " + sqlTypeCode + " but is " + getSqlTypeCode() ); 133 } 134 return sqlTypeCode; 135 } 136 catch (Exception e) { 137 throw new MappingException( 138 "Could not determine type for column " + 139 name + 140 " of type " + 141 type.getClass().getName() + 142 ": " + 143 e.getClass().getName(), 144 e 145 ); 146 } 147 } 148 149 158 public Integer getSqlTypeCode() { 159 return sqlTypeCode; 160 } 161 162 public void setSqlTypeCode(Integer typecode) { 163 sqlTypeCode=typecode; 164 } 165 166 public boolean isUnique() { 167 return unique; 168 } 169 170 171 public String getSqlType(Dialect dialect, Mapping mapping) throws HibernateException { 172 return sqlType==null ? 173 dialect.getTypeName( getSqlTypeCode(mapping), getLength(), getPrecision(), getScale() ) : 174 sqlType; 175 } 176 177 public boolean equals(Object object) { 178 return object instanceof Column && equals( (Column) object ); 179 } 180 181 public boolean equals(Column column) { 182 if (null == column) return false; 183 if (this == column) return true; 184 185 return isQuoted() ? 186 name.equals(column.name) : 187 name.equalsIgnoreCase(column.name); 188 } 189 190 public int hashCode() { 192 return isQuoted() ? 193 name.hashCode() : 194 name.toLowerCase().hashCode(); 195 } 196 197 public String getSqlType() { 198 return sqlType; 199 } 200 201 public void setSqlType(String sqlType) { 202 this.sqlType = sqlType; 203 } 204 205 public void setUnique(boolean unique) { 206 this.unique = unique; 207 } 208 209 public boolean isQuoted() { 210 return quoted; 211 } 212 213 public String toString() { 214 return getClass().getName() + '(' + getName() + ')'; 215 } 216 217 public String getCheckConstraint() { 218 return checkConstraint; 219 } 220 221 public void setCheckConstraint(String checkConstraint) { 222 this.checkConstraint = checkConstraint; 223 } 224 225 public boolean hasCheckConstraint() { 226 return checkConstraint!=null; 227 } 228 229 public String getTemplate(Dialect dialect) { 230 return getQuotedName(dialect); 231 } 232 233 public boolean isFormula() { 234 return false; 235 } 236 237 public String getText(Dialect d) { 238 return getQuotedName(d); 239 } 240 public String getText() { 241 return getName(); 242 } 243 244 public int getPrecision() { 245 return precision; 246 } 247 public void setPrecision(int scale) { 248 this.precision = scale; 249 } 250 251 public int getScale() { 252 return scale; 253 } 254 public void setScale(int scale) { 255 this.scale = scale; 256 } 257 258 public String getComment() { 259 return comment; 260 } 261 262 public void setComment(String comment) { 263 this.comment = comment; 264 } 265 266 } 267 | Popular Tags |