1 24 25 package com.mckoi.database.jdbc; 26 27 import com.mckoi.database.global.ColumnDescription; 28 import com.mckoi.database.global.SQLTypes; 29 import java.sql.*; 30 import java.math.BigDecimal ; 31 32 37 38 public class MResultSetMetaData implements ResultSetMetaData { 39 40 43 private MResultSet result_set; 44 45 48 MResultSetMetaData(MResultSet result_set) { 49 this.result_set = result_set; 50 } 51 52 56 private static Class jdbcSQLClass(int sql_type) { 57 switch (sql_type) { 58 case(SQLTypes.BIT): 59 return Boolean .class; 60 case(SQLTypes.TINYINT): 61 return Byte .class; 62 case(SQLTypes.SMALLINT): 63 return Short .class; 64 case(SQLTypes.INTEGER): 65 return Integer .class; 66 case(SQLTypes.BIGINT): 67 return Long .class; 68 case(SQLTypes.FLOAT): 69 return Double .class; 70 case(SQLTypes.REAL): 71 return Float .class; 72 case(SQLTypes.DOUBLE): 73 return Double .class; 74 case(SQLTypes.NUMERIC): 75 return BigDecimal .class; 76 case(SQLTypes.DECIMAL): 77 return BigDecimal .class; 78 case(SQLTypes.CHAR): 79 return String .class; 80 case(SQLTypes.VARCHAR): 81 return String .class; 82 case(SQLTypes.LONGVARCHAR): 83 return String .class; 84 case(SQLTypes.DATE): 85 return java.sql.Date .class; 86 case(SQLTypes.TIME): 87 return java.sql.Time .class; 88 case(SQLTypes.TIMESTAMP): 89 return java.sql.Timestamp .class; 90 case(SQLTypes.BINARY): 91 return byte[].class; 92 case(SQLTypes.VARBINARY): 93 return byte[].class; 94 case(SQLTypes.LONGVARBINARY): 95 return byte[].class; 96 case(SQLTypes.NULL): 97 return Object .class; 98 case(SQLTypes.OTHER): 99 return Object .class; 100 case(SQLTypes.JAVA_OBJECT): 101 return Object .class; 102 case(SQLTypes.DISTINCT): 103 return Object .class; 105 case(SQLTypes.STRUCT): 106 return Object .class; 108 case(SQLTypes.ARRAY): 109 return Object .class; 111 case(SQLTypes.BLOB): 113 return java.sql.Blob .class; 114 case(SQLTypes.CLOB): 115 return java.sql.Clob .class; 116 case(SQLTypes.REF): 117 return Object .class; 119 default: 121 return Object .class; 122 } 123 } 124 125 127 public int getColumnCount() throws SQLException { 128 return result_set.columnCount(); 129 } 130 131 public boolean isAutoIncrement(int column) throws SQLException { 132 return false; 135 } 136 137 public boolean isCaseSensitive(int column) throws SQLException { 138 return true; 139 } 140 141 public boolean isSearchable(int column) throws SQLException { 142 return result_set.getColumn(column -1).isQuantifiable(); 143 } 144 145 public boolean isCurrency(int column) throws SQLException { 146 return false; 148 } 149 150 public int isNullable(int column) throws SQLException { 151 if (result_set.getColumn(column - 1).isNotNull()) { 152 return columnNoNulls; 153 } 154 else { 155 return columnNullable; 156 } 157 } 158 159 public boolean isSigned(int column) throws SQLException { 160 if (result_set.getColumn(column - 1).isNumericType()) { 162 return true; 163 } 164 else { 165 return false; 167 } 168 } 169 170 public int getColumnDisplaySize(int column) throws SQLException { 171 return 64; 174 } 175 176 public String getColumnLabel(int column) throws SQLException { 177 String encoded_name = result_set.getColumn(column - 1).getName(); 180 if (encoded_name.startsWith("@a")) { 181 return encoded_name.substring(2); 183 } 184 else if (encoded_name.startsWith("@f")) { 185 int p = encoded_name.lastIndexOf("."); 187 if (p > -1) { 188 return encoded_name.substring(p + 1); 189 } 190 else { 191 return encoded_name.substring(2); 192 } 193 } 194 return encoded_name; 196 } 197 198 public String getColumnName(int column) throws SQLException { 199 if (!result_set.verboseColumnNames()) { 202 return getColumnLabel(column); 203 } 204 else { 205 String encoded_name = result_set.getColumn(column - 1).getName(); 208 if (encoded_name.startsWith("@")) { 209 return encoded_name.substring(2); 211 } 212 return encoded_name; 214 } 215 } 216 217 public String getSchemaName(int column) throws SQLException { 218 ColumnDescription col = result_set.getColumn(column - 1); 219 String name = col.getName(); 220 221 char col_code = 'f'; 223 int name_start = 0; 224 if (name.startsWith("@")) { 225 col_code = name.charAt(1); 226 name_start = 2; 227 } 228 229 if (col_code == 'a') { 230 return ""; 232 } 233 else if (col_code == 'f') { 234 int delim = name.lastIndexOf("."); 236 if (delim == -1) { 237 return ""; 238 } 239 else { 240 delim = name.lastIndexOf(".", delim - 1); 241 if (delim == -1) { 242 return ""; 243 } 244 else { 245 int end_point = delim; 246 delim = name.lastIndexOf(".", delim - 1); 247 if (delim == -1) { 248 return name.substring(name_start, end_point); 249 } 250 else { 251 return name.substring(delim + 1, end_point); 252 } 253 } 254 } 255 } 256 else { 257 throw new SQLException("Unknown column code: '" + col_code + "'"); 258 } 259 } 260 261 public int getPrecision(int column) throws SQLException { 262 int size = result_set.getColumn(column - 1).getSize(); 265 if (size == -1) { 266 size = 32; 267 } 268 return size; 269 } 270 271 public int getScale(int column) throws SQLException { 272 int scale = result_set.getColumn(column - 1).getScale(); 273 if (scale == -1) { 274 scale = 0; 275 } 276 return scale; 277 } 278 279 public String getTableName(int column) throws SQLException { 280 ColumnDescription col = result_set.getColumn(column - 1); 281 String name = col.getName(); 282 283 char col_code = 'f'; 285 int name_start = 0; 286 if (name.startsWith("@")) { 287 col_code = name.charAt(1); 288 name_start = 2; 289 } 290 291 if (col_code == 'a') { 292 return ""; 294 } 295 else if (col_code == 'f') { 296 int delim = name.lastIndexOf("."); 298 if (delim == -1) { 299 return ""; 300 } 301 else { 302 int end_point = delim; 303 delim = name.lastIndexOf(".", end_point - 1); 304 if (delim == -1) { 305 return name.substring(name_start, end_point); 306 } 307 else { 308 return name.substring(delim + 1, end_point); 309 } 310 } 311 } 312 else { 313 throw new SQLException("Unknown column code: '" + col_code + "'"); 314 } 315 } 316 317 public String getCatalogName(int column) throws SQLException { 318 return ""; 320 } 321 322 public int getColumnType(int column) throws SQLException { 323 return result_set.getColumn(column - 1).getSQLType(); 324 } 325 326 public String getColumnTypeName(int column) throws SQLException { 327 return result_set.getColumn(column - 1).getSQLTypeName(); 328 } 329 330 public boolean isReadOnly(int column) throws SQLException { 331 return false; 332 } 333 334 public boolean isWritable(int column) throws SQLException { 335 return true; 336 } 337 338 public boolean isDefinitelyWritable(int column) throws SQLException { 339 return false; 340 } 341 342 344 346 public String getColumnClassName(int column) throws SQLException { 347 return jdbcSQLClass(result_set.getColumn(column - 1).getSQLType()).getName(); 350 } 352 353 355 } 356 | Popular Tags |