1 23 24 package org.xquark.jdbc.typing; 25 26 import java.sql.*; 27 28 import org.xquark.jdbc.typing.TypeMap; 29 30 38 public class ColumnMetaDataImpl implements ColumnMetaData 39 { 40 private static final String RCSRevision = "$Revision: 1.3 $"; 41 private static final String RCSName = "$Name: $"; 42 43 44 protected short primaryKeySeq = -1; 45 46 47 protected String refTable = null; 48 49 50 protected String refColumn = null; 51 52 53 protected String tableName; 54 55 56 protected String columnName; 57 58 59 protected DbType type = null; 60 61 62 protected TypeMap.SQLType sqlType = null; 63 64 65 protected boolean nullable; 66 67 68 protected String columnDef; 69 70 71 protected int ordinalPosition; 72 73 74 protected String typeDDLString; 75 76 protected ColumnMetaDataImpl(){}; 77 78 85 public ColumnMetaDataImpl(ResultSet rs, TypeMap typeMap, 86 JDBCProperties properties) 87 throws SQLException 88 { 89 tableName = rs.getString(3); 90 columnName = rs.getString(4); 91 sqlType = typeMap.getType(rs.getString(6)); 92 if (properties.useStaticMappingFromNativeType()) 93 type = new DbType(rs.getInt(7), rs.getInt(9), sqlType); 94 else 95 type = new DbType(rs.getInt(5), rs.getInt(7), rs.getInt(9)); 96 int tmp = rs.getInt(11); 97 nullable = (rs.getInt(11) == DatabaseMetaData.columnNullable) 98 || sqlType.isAutoFill(); columnDef = rs.getString(13); 100 if (columnDef != null) { 101 if (columnDef.length() == 0) 102 columnDef = null; else if (columnDef.charAt(0) == '\'' && columnDef.charAt(columnDef.length() - 1) == '\'') 104 columnDef = columnDef.substring(1, columnDef.length() - 1); } 106 ordinalPosition = rs.getInt(17); 107 108 switch (type.getJDBCType()) 109 { 110 case Types.CLOB: 111 case Types.BLOB: 112 type.setLOB(true); 113 case Types.LONGVARCHAR: 115 case Types.LONGVARBINARY: 116 type.setLongType(true); 117 break; 118 default: 119 } 120 121 typeDDLString = getTypeCreationString( 123 getDataType(), 124 typeMap, 125 getColumnSize(), 126 (int)getColumnSize(), 127 getDecimalDigits()); 128 } 129 130 public ColumnMetaDataImpl(ResultSetMetaData rs, int i, TypeMap typeMap, 131 JDBCProperties properties) 132 throws SQLException 133 { 134 tableName = rs.getTableName(i); 135 columnName = rs.getColumnName(i); 136 sqlType = typeMap.getType(rs.getColumnTypeName(i)); 137 if (properties.useStaticMappingFromNativeType()) 138 type = new DbType(sqlType); 139 else 140 type = new DbType(rs.getColumnType(i)); 141 nullable = (rs.isNullable(i) == ResultSetMetaData.columnNullable) 142 || sqlType.isAutoFill(); ordinalPosition = i; 144 type.setLongType(rs.isSearchable(i)); 145 type.setLOB(type.isLOB()); 146 147 type.setLength(rs.getColumnDisplaySize(i)); 148 if (type.getLength() <= 0) 149 type.setLength(rs.getPrecision(i)); 150 type.setScale(rs.getScale(i)); 151 152 typeDDLString = getTypeCreationString( 154 getDataType(), 155 typeMap, 156 getColumnSize(), 157 (int)getColumnSize(), 158 getDecimalDigits()); 159 } 160 161 164 public boolean isNullable() 165 { 166 return nullable; 167 } 168 169 public boolean isOptional() 170 { 171 return isNullable() || getDefaultValue() != null; 172 } 173 174 public String getTableName() { return tableName; } 175 176 public String getColumnName() { return columnName; } 177 178 public DbType getType() { 179 return type; 180 } 181 182 public short getDataType() { return (short)type.getJDBCType(); } 183 184 public String getTypeName() { return type.getNativeType(); } 185 186 public long getColumnSize() { return type.getLength(); } 187 188 public int getDecimalDigits() { return type.getScale(); } 189 190 public String getDefaultValue() { return columnDef; } 191 192 public int getOrdinalPosition() { return ordinalPosition; } 193 194 public boolean isPrimaryKey() { return primaryKeySeq != -1; } 195 196 public short getPrimaryKeySeq() { return primaryKeySeq; } 197 198 public String getRefTable() { return refTable; } 199 200 public String getRefColumn() { return refColumn; } 201 202 public String toString() 203 { 204 return "#<Column "+getTableName()+"."+getColumnName()+">"; 205 } 206 207 public void setPrimaryKeySeq(short seq) { primaryKeySeq = seq; } 208 209 public void setRefTable(String name) { refTable = name; } 210 211 public void setRefColumn(String name) { refColumn = name; } 212 213 public String getTypeCreationString() 214 { 215 return typeDDLString; 216 } 217 218 public boolean isLongType() 219 { 220 return type.isLongType(); 221 } 222 223 public boolean isLOB() 224 { 225 return type.isLOB(); 226 } 227 228 public static int findRealisticType(int type, long size, TypeMap typeMap) 232 { 233 switch (type) { case Types.VARCHAR : 236 case Types.LONGVARCHAR : 237 if (size <= typeMap.getSize(Types.VARCHAR)) 238 type = Types.VARCHAR; 239 else 240 type = Types.LONGVARCHAR; 241 break; 242 case Types.VARBINARY : 243 case Types.LONGVARBINARY : 244 if (size <= typeMap.getSize(Types.VARBINARY)) 245 type = Types.VARBINARY; 246 else 247 type = Types.LONGVARBINARY; 248 break; 249 default : } 251 return type; 252 } 253 254 257 public static String getTypeDDL(int dataType, TypeMap typeMap, 258 long maxLength) 259 { 260 return getTypeDDLStatement(dataType, typeMap, maxLength, -1, -1); 261 } 262 263 public static String getTypeDDL(int dataType, TypeMap typeMap, 264 int precision, int scale) 265 { 266 return getTypeDDLStatement(dataType, typeMap, -1, precision, scale); 267 } 268 269 public static String getTypeDDL(int dataType, TypeMap typeMap, 270 int precision) 271 { 272 return getTypeDDLStatement(dataType, typeMap, -1, precision, -1); 273 } 274 275 public static String getTypeDDL(int dataType, TypeMap typeMap) 276 { 277 return getTypeDDLStatement(dataType, typeMap, -1, -1, -1); 278 } 279 280 281 public static String getTypeCreationString(int dataType, TypeMap typeMap, 282 long maxLength, int precision, int scale) 283 { 284 String typeString; 285 286 287 switch (dataType) { case Types.CHAR : case Types.BINARY : 290 case Types.VARCHAR : case Types.VARBINARY : 291 case Types.DOUBLE : case Types.FLOAT : 292 case Types.NUMERIC : case Types.DECIMAL : 293 typeString = getDefaultTypeCreationString(dataType, typeMap, 294 maxLength, precision, scale); 295 break; 296 default : typeString = typeMap.getNativeTypeName(dataType); 298 if (typeMap.getTypeParam(dataType) != null) 299 typeString += typeMap.getTypeParam(dataType); 300 } 301 return typeString; 302 } 303 304 private static String getTypeDDLStatement(int dataType, TypeMap typeMap, 308 long maxLength, int precision, int scale) 309 { 310 311 if (maxLength < 0) 312 { 313 switch (dataType) { case Types.CHAR : case Types.VARCHAR : 316 case Types.VARBINARY : case Types.BINARY : 317 maxLength = typeMap.getSize(dataType); 318 break; 319 default : 320 } 321 } 322 323 return getTypeCreationString( 324 findRealisticType(dataType, maxLength, typeMap), 325 typeMap, 326 maxLength, 327 precision, 328 scale 329 ); 330 } 331 332 341 private static String getDefaultTypeCreationString(int dataType, 342 TypeMap typeMap, long maxLength, int precision, int scale) 343 { 344 String ret = null; 345 if (typeMap != null) 346 { 347 ret = typeMap.getNativeTypeName(dataType); 348 if (maxLength > 0) 349 ret += "(" + maxLength + ")"; 350 else if (precision > 0) 351 { 352 ret += "(" + precision; 353 if (scale > 0) 354 ret += "," + scale; 355 ret += ")"; 356 } 357 else if (typeMap.getTypeParam(dataType) == null) { 359 switch (dataType) { 361 case Types.NUMERIC : case Types.FLOAT : 362 case Types.DOUBLE : case Types.DECIMAL : 363 break; 365 default : ret += "(" + typeMap.getSize(dataType) + ")"; 367 } 368 } 369 } 370 return ret; 371 } 372 375 public TypeMap.SQLType getSqlType() { 376 return sqlType; 377 } 378 } 379 | Popular Tags |