1 package jimm.datavision.source; 2 import jimm.datavision.*; 3 import jimm.datavision.source.sql.SQLQuery; 4 import jimm.util.XMLWriter; 5 import java.sql.Types ; 6 7 16 public class Column 17 implements Identity, Nameable, Selectable, Draggable, Writeable 18 { 19 20 public static final String DEFAULT_DATE_PARSE_FORMAT = "yyyy-MM-dd"; 21 22 protected Object id; 23 protected String name; 24 protected int type; 25 protected String dateParseFormat; 26 27 public static int typeFromString(String str) { 28 if (str == null || str.length() == 0) 29 return Types.VARCHAR; 30 31 str = str.toLowerCase(); 32 if (str.equals("number")) 33 return Types.NUMERIC; 34 if (str.equals("date")) 35 return Types.DATE; 36 return Types.VARCHAR; 37 } 38 39 public static String typeToString(int type) { 40 switch (type) { 41 case Types.BIGINT: 42 case Types.BIT: 43 case Types.DECIMAL: 44 case Types.DOUBLE: 45 case Types.INTEGER: 46 case Types.NUMERIC: 47 case Types.REAL: 48 case Types.SMALLINT: 49 case Types.TINYINT: 50 return "number"; 51 case Types.DATE: 52 case Types.TIME: 53 case Types.TIMESTAMP: 54 return "date"; 55 default: 56 return "string"; 57 } 58 } 59 60 public Column(Object id, String name, int type) { 61 this.id = id; 62 this.name = name; 63 this.type = type; 64 } 65 66 74 public boolean equals(Object obj) { 75 if (obj == null || !(obj instanceof Column)) return false; 76 if (this == obj) return true; 77 return id.equals(((Column)obj).getId()); 78 } 79 80 public int hashCode() { 81 return id.hashCode(); 82 } 83 84 90 public Object getId() { return id; } 91 92 99 public String getName() { return name; } 100 101 102 public void setName(String name) { } 103 104 112 public String getDateParseFormat() { 113 return dateParseFormat == null 114 ? DEFAULT_DATE_PARSE_FORMAT : dateParseFormat; 115 } 116 117 124 public void setDateParseFormat(String format) { dateParseFormat = format; } 125 126 131 public Table getTable() { return null; } 132 133 143 public String fullName() { 144 return id.toString(); 145 } 146 147 153 public int getType() { return type; } 154 155 public Object getValue(Report report) { 156 return report.columnValue(this); 157 } 158 159 public String fieldTypeString() { return "column"; } 160 161 public String getSelectString(SQLQuery query) { 162 return query.quoted(fullName()); 163 } 164 165 public String getSortString(SQLQuery query) { 166 return getSelectString(query); 167 } 168 169 public String dragString() { 170 return "column:" + fullName(); 171 } 172 173 public String getDisplayName() { return fullName(); } 174 175 public Selectable reloadInstance(DataSource dataSource) { 176 return dataSource.findColumn(getId()); 177 } 178 179 186 public boolean isNumeric() { 187 return type == Types.BIGINT 188 || type == Types.BIT 189 || type == Types.DECIMAL 190 || type == Types.DOUBLE 191 || type == Types.FLOAT 192 || type == Types.INTEGER 193 || type == Types.NUMERIC 194 || type == Types.REAL 195 || type == Types.SMALLINT 196 || type == Types.TINYINT; 197 } 198 199 204 public boolean isDate() { 205 return type == Types.DATE 206 || type == Types.TIME 207 || type == Types.TIMESTAMP; 208 } 209 210 215 public boolean isString() { 216 return !isNumeric() && !isDate(); 217 } 218 219 225 public String toString() { 226 return fullName(); 227 } 228 229 public void writeXML(XMLWriter out) { 230 out.startElement("column"); 231 out.attr("name", getName()); 232 out.attr("type", typeToString(type)); 233 if (dateParseFormat != null) 234 out.attr("date-format", dateParseFormat); 235 out.endElement(); 236 } 237 238 } 239 | Popular Tags |