1 19 20 package org.apache.cayenne.access.jdbc; 21 22 import java.io.Serializable ; 23 import java.sql.ResultSetMetaData ; 24 import java.sql.SQLException ; 25 26 import org.apache.cayenne.dba.TypesMapping; 27 import org.apache.cayenne.map.DbAttribute; 28 import org.apache.cayenne.map.ObjAttribute; 29 import org.apache.cayenne.map.ProcedureParameter; 30 import org.apache.commons.lang.builder.EqualsBuilder; 31 import org.apache.commons.lang.builder.HashCodeBuilder; 32 import org.apache.commons.lang.builder.ToStringBuilder; 33 34 40 public class ColumnDescriptor implements Serializable { 41 42 protected String tableName; 43 protected String procedureName; 44 45 protected String name; 47 protected String qualifiedColumnName; 48 49 protected String label; 51 52 protected int jdbcType; 53 protected String javaClass; 54 55 58 public ColumnDescriptor() { 59 } 60 61 66 public ColumnDescriptor(String columnName, int jdbcType, String javaClass) { 67 this.name = columnName; 68 this.qualifiedColumnName = columnName; 69 this.label = columnName; 70 this.jdbcType = jdbcType; 71 this.javaClass = javaClass; 72 } 73 74 79 public ColumnDescriptor(DbAttribute attribute, String columnAlias) { 80 this.name = attribute.getName(); 81 this.qualifiedColumnName = attribute.getAliasedName(columnAlias); 82 this.label = name; 83 this.jdbcType = attribute.getType(); 84 this.javaClass = getDefaultJavaClass(attribute.getMaxLength(), attribute 85 .getScale()); 86 87 if (attribute.getEntity() != null) { 88 this.tableName = attribute.getEntity().getName(); 89 } 90 } 91 92 95 public ColumnDescriptor(ObjAttribute objAttribute, DbAttribute dbAttribute, 96 String columnAlias) { 97 this(dbAttribute, columnAlias); 98 this.label = objAttribute.getDbAttributePath(); 99 this.javaClass = objAttribute.getType(); 100 } 101 102 107 public ColumnDescriptor(ProcedureParameter parameter) { 108 this.name = parameter.getName(); 109 this.qualifiedColumnName = name; 110 this.label = name; 111 this.jdbcType = parameter.getType(); 112 this.javaClass = getDefaultJavaClass(parameter.getMaxLength(), parameter 113 .getPrecision()); 114 115 if (parameter.getProcedure() != null) { 116 this.procedureName = parameter.getProcedure().getName(); 117 } 118 } 119 120 125 public ColumnDescriptor(ResultSetMetaData metaData, int position) throws SQLException { 126 String name = metaData.getColumnLabel(position); 127 if (name == null || name.length() == 0) { 128 name = metaData.getColumnName(position); 129 130 if (name == null || name.length() == 0) { 131 name = "column_" + position; 132 } 133 } 134 135 this.name = name; 136 this.qualifiedColumnName = name; 137 this.label = name; 138 this.jdbcType = metaData.getColumnType(position); 139 this.javaClass = metaData.getColumnClassName(position); 140 } 141 142 148 public boolean equals(Object o) { 149 if (!(o instanceof ColumnDescriptor)) { 150 return false; 151 } 152 153 ColumnDescriptor rhs = (ColumnDescriptor) o; 154 return new EqualsBuilder().append(name, rhs.name).append( 155 qualifiedColumnName, 156 rhs.qualifiedColumnName).append(procedureName, rhs.procedureName).append( 157 label, 158 rhs.label).append(tableName, rhs.tableName).isEquals(); 159 } 160 161 164 public int hashCode() { 165 return new HashCodeBuilder(23, 43) 166 .append(name) 167 .append(qualifiedColumnName) 168 .append(procedureName) 169 .append(tableName) 170 .append(label) 171 .toHashCode(); 172 } 173 174 177 public String toString() { 178 ToStringBuilder builder = new ToStringBuilder(this); 179 builder.append("namePrefix", getQualifiedColumnName()); 180 builder.append("name", getName()); 181 builder.append("label", getLabel()); 182 builder.append("tableName", getTableName()); 183 builder.append("procedureName", getProcedureName()); 184 builder.append("javaClass", getJavaClass()); 185 builder.append("jdbcType", getJdbcType()); 186 return builder.toString(); 187 } 188 189 194 public String getDefaultJavaClass(int size, int scale) { 195 return TypesMapping.getJavaBySqlType(getJdbcType(), size, scale); 196 } 197 198 203 public String getQualifiedColumnName() { 204 return qualifiedColumnName != null ? qualifiedColumnName : name; 205 } 206 207 public int getJdbcType() { 208 return jdbcType; 209 } 210 211 214 public String getName() { 215 return name; 216 } 217 218 public void setJdbcType(int i) { 219 jdbcType = i; 220 } 221 222 public void setName(String name) { 223 this.name = name; 224 } 225 226 public String getJavaClass() { 227 return javaClass; 228 } 229 230 public void setJavaClass(String string) { 231 javaClass = string; 232 } 233 234 239 public String getTableName() { 240 return tableName; 241 } 242 243 246 public void setTableName(String tableName) { 247 this.tableName = tableName; 248 } 249 250 255 public String getProcedureName() { 256 return procedureName; 257 } 258 259 262 public void setProcedureName(String procedureName) { 263 this.procedureName = procedureName; 264 } 265 266 269 public void setQualifiedColumnName(String namePrefix) { 270 this.qualifiedColumnName = namePrefix; 271 } 272 273 278 public String getLabel() { 279 return (label != null) ? label : getName(); 280 } 281 282 285 public void setLabel(String columnName) { 286 this.label = columnName; 287 } 288 } 289 | Popular Tags |