1 56 57 package org.objectstyle.cayenne.access.jdbc; 58 59 import java.io.Serializable ; 60 import java.sql.ResultSetMetaData ; 61 import java.sql.SQLException ; 62 63 import org.apache.commons.lang.builder.EqualsBuilder; 64 import org.apache.commons.lang.builder.HashCodeBuilder; 65 import org.apache.commons.lang.builder.ToStringBuilder; 66 import org.objectstyle.cayenne.dba.TypesMapping; 67 import org.objectstyle.cayenne.map.DbAttribute; 68 import org.objectstyle.cayenne.map.ObjAttribute; 69 import org.objectstyle.cayenne.map.ProcedureParameter; 70 71 77 public class ColumnDescriptor implements Serializable { 78 79 protected String tableName; 80 protected String procedureName; 81 82 protected String name; 84 protected String qualifiedColumnName; 85 86 protected String label; 88 89 92 protected boolean primaryKey; 93 94 protected int jdbcType; 95 protected String javaClass; 96 97 100 public ColumnDescriptor() { 101 } 102 103 108 public ColumnDescriptor(String columnName, int jdbcType, String javaClass) { 109 this.name = columnName; 110 this.qualifiedColumnName = columnName; 111 this.label = columnName; 112 this.jdbcType = jdbcType; 113 this.javaClass = javaClass; 114 } 115 116 117 122 public ColumnDescriptor(ObjAttribute objAttribute, DbAttribute dbAttribute) { 123 this(objAttribute, dbAttribute, null); 124 } 125 126 131 public ColumnDescriptor(DbAttribute attribute, String columnAlias) { 132 this.name = attribute.getName(); 133 this.qualifiedColumnName = attribute.getAliasedName(columnAlias); 134 this.label = name; 135 this.jdbcType = attribute.getType(); 136 this.primaryKey = attribute.isPrimaryKey(); 137 this.javaClass = getDefaultJavaClass(attribute.getMaxLength(), attribute 138 .getPrecision()); 139 140 if (attribute.getEntity() != null) { 141 this.tableName = attribute.getEntity().getName(); 142 } 143 } 144 145 148 public ColumnDescriptor(ObjAttribute objAttribute, DbAttribute dbAttribute, 149 String columnAlias) { 150 this(dbAttribute, columnAlias); 151 this.label = objAttribute.getDbAttributePath(); 152 this.javaClass = objAttribute.getType(); 153 } 154 155 160 public ColumnDescriptor(ProcedureParameter parameter) { 161 this.name = parameter.getName(); 162 this.qualifiedColumnName = name; 163 this.label = name; 164 this.jdbcType = parameter.getType(); 165 this.javaClass = getDefaultJavaClass(parameter.getMaxLength(), parameter 166 .getPrecision()); 167 168 if (parameter.getProcedure() != null) { 169 this.procedureName = parameter.getProcedure().getName(); 170 } 171 } 172 173 178 public ColumnDescriptor(ResultSetMetaData metaData, int position) throws SQLException { 179 String name = metaData.getColumnLabel(position); 180 if (name == null || name.length() == 0) { 181 name = metaData.getColumnName(position); 182 183 if (name == null || name.length() == 0) { 184 name = "column_" + position; 185 } 186 } 187 188 this.name = name; 189 this.qualifiedColumnName = name; 190 this.label = name; 191 this.jdbcType = metaData.getColumnType(position); 192 this.javaClass = getDefaultJavaClass(metaData.getColumnDisplaySize(position), 193 metaData.getScale(position)); 194 } 195 196 202 public boolean equals(Object o) { 203 if (!(o instanceof ColumnDescriptor)) { 204 return false; 205 } 206 207 ColumnDescriptor rhs = (ColumnDescriptor) o; 208 return new EqualsBuilder() 209 .append(name, rhs.name) 210 .append(qualifiedColumnName, rhs.qualifiedColumnName) 211 .append(procedureName, rhs.procedureName) 212 .append(label, rhs.label) 213 .append(tableName, rhs.tableName) 214 .isEquals(); 215 } 216 217 220 public int hashCode() { 221 return new HashCodeBuilder(23, 43) 222 .append(name) 223 .append(qualifiedColumnName) 224 .append(procedureName) 225 .append(tableName) 226 .append(label) 227 .toHashCode(); 228 } 229 230 233 public String toString() { 234 ToStringBuilder builder = new ToStringBuilder(this); 235 builder.append("namePrefix", getQualifiedColumnName()); 236 builder.append("name", getName()); 237 builder.append("label", getLabel()); 238 builder.append("tableName", getTableName()); 239 builder.append("procedureName", getProcedureName()); 240 builder.append("javaClass", getJavaClass()); 241 builder.append("jdbcType", getJdbcType()); 242 return builder.toString(); 243 } 244 245 250 public String getDefaultJavaClass(int size, int scale) { 251 return TypesMapping.getJavaBySqlType(getJdbcType(), size, scale); 252 } 253 254 259 public String getQualifiedColumnName() { 260 return qualifiedColumnName != null ? qualifiedColumnName : name; 261 } 262 263 public int getJdbcType() { 264 return jdbcType; 265 } 266 267 270 public String getName() { 271 return name; 272 } 273 274 public void setJdbcType(int i) { 275 jdbcType = i; 276 } 277 278 public void setName(String name) { 279 this.name = name; 280 } 281 282 285 public boolean isPrimaryKey() { 286 return primaryKey; 287 } 288 289 public String getJavaClass() { 290 return javaClass; 291 } 292 293 296 public void setPrimaryKey(boolean b) { 297 primaryKey = b; 298 } 299 300 public void setJavaClass(String string) { 301 javaClass = string; 302 } 303 304 309 public String getTableName() { 310 return tableName; 311 } 312 313 316 public void setTableName(String tableName) { 317 this.tableName = tableName; 318 } 319 320 325 public String getProcedureName() { 326 return procedureName; 327 } 328 329 332 public void setProcedureName(String procedureName) { 333 this.procedureName = procedureName; 334 } 335 336 339 public void setQualifiedColumnName(String namePrefix) { 340 this.qualifiedColumnName = namePrefix; 341 } 342 343 348 public String getLabel() { 349 return (label != null) ? label : getName(); 350 } 351 352 355 public void setLabel(String columnName) { 356 this.label = columnName; 357 } 358 } | Popular Tags |