1 30 31 32 package org.hsqldb.jdbc; 33 34 import java.sql.ResultSetMetaData ; 35 import java.sql.SQLException ; 36 37 import org.hsqldb.Result; 38 import org.hsqldb.ResultConstants; 39 import org.hsqldb.Trace; 40 import org.hsqldb.Types; 41 import org.hsqldb.persist.HsqlProperties; 42 43 48 94 public class jdbcResultSetMetaData implements ResultSetMetaData { 95 96 100 private jdbcColumnMetaData[] columnMetaData; 101 102 103 private int columnCount; 104 105 109 private boolean useColumnName; 110 111 115 private static final boolean TRACE = false; 116 117 127 jdbcResultSetMetaData(jdbcResultSet rs, 128 HsqlProperties props) throws SQLException { 129 init(rs, props); 130 } 131 132 142 jdbcResultSetMetaData(Result r, 143 HsqlProperties props) throws SQLException { 144 init(r, props); 145 } 146 147 157 void init(jdbcResultSet rs, HsqlProperties props) throws SQLException { 158 159 if (rs == null) { 160 throw Util.sqlException(Trace.GENERAL_ERROR, 161 Trace.JDBC_NO_RESULT_SET_METADATA, null); 162 } 163 164 init(rs.rResult, props); 165 } 166 167 177 void init(Result r, HsqlProperties props) throws SQLException { 178 179 jdbcColumnMetaData cmd; 180 int type; 181 Result.ResultMetaData rmd; 182 183 if (r == null) { 184 throw Util.sqlException(Trace.GENERAL_ERROR, 185 Trace.JDBC_NO_RESULT_SET, null); 186 } 187 188 if (!r.isData()) { 189 190 return; 192 } 193 194 columnCount = r.getColumnCount(); 195 196 useColumnName = props == null ? true 198 : props.isPropertyTrue( 199 "get_column_name"); 200 columnMetaData = new jdbcColumnMetaData[columnCount]; 201 rmd = r.metaData; 202 203 for (int i = 0; i < columnCount; i++) { 204 cmd = new jdbcColumnMetaData(); 205 columnMetaData[i] = cmd; 206 207 cmd.catalogName = rmd.catalogNames[i] == null ? "" 212 : rmd 213 .catalogNames[i]; 214 cmd.schemaName = rmd.schemaNames[i] == null ? "" 215 : rmd 216 .schemaNames[i]; 217 cmd.tableName = rmd.tableNames[i] == null ? "" 218 : rmd 219 .tableNames[i]; 220 cmd.columnName = rmd.colNames[i] == null ? "" 221 : rmd.colNames[i]; 222 cmd.columnLabel = rmd.colLabels[i] == null ? "" 223 : rmd.colLabels[i]; 224 cmd.columnType = rmd.colTypes[i]; 225 cmd.columnTypeName = Types.getTypeString(cmd.columnType); 226 cmd.isWritable = rmd.isWritable[i]; 227 cmd.isReadOnly = !cmd.isWritable; 228 229 cmd.isAutoIncrement = rmd.isIdentity[i]; 231 cmd.isNullable = rmd.colNullable[i]; 232 type = cmd.columnType; 233 cmd.columnClassName = rmd.classNames[i]; 234 235 if (cmd.columnClassName == null 236 || cmd.columnClassName.length() == 0) { 237 cmd.columnClassName = Types.getColStClsName(type); 238 } 239 240 if (Types.acceptsPrecisionCreateParam(type)) { 251 if (rmd.colSizes[i] == 0) { 252 cmd.columnDisplaySize = Types.getMaxDisplaySize(type); 253 } else { 254 cmd.columnDisplaySize = rmd.colSizes[i]; 255 256 if (Types.acceptsScaleCreateParam(type)) { 257 if (rmd.colScales[i] != 0) { 258 cmd.columnDisplaySize += (1 + rmd.colScales[i]); 259 } 260 } 261 } 262 } else { 263 cmd.columnDisplaySize = Types.getMaxDisplaySize(type); 264 } 265 266 if (Types.isNumberType(type) 267 && Types.acceptsPrecisionCreateParam(type)) { 268 cmd.precision = rmd.colSizes[i]; 269 270 if (cmd.precision == 0) { 271 cmd.precision = Types.getPrecision(type); 272 } 273 } else { 274 cmd.precision = Types.getPrecision(type); 275 } 276 277 if (Types.acceptsScaleCreateParam(type)) { 283 cmd.scale = rmd.colScales[i]; 284 } 285 286 Boolean iua = Types.isUnsignedAttribute(type); 287 288 cmd.isSigned = iua != null &&!iua.booleanValue(); 289 290 Boolean ics = Types.isCaseSensitive(type); 291 292 cmd.isCaseSensitive = ics != null && ics.booleanValue(); 293 cmd.isSearchable = Types.isSearchable(type); 294 } 295 } 296 297 305 public int getColumnCount() throws SQLException { 306 return columnCount; 307 } 308 309 392 public boolean isAutoIncrement(int column) throws SQLException { 393 394 checkColumn(column); 395 396 return columnMetaData[--column].isAutoIncrement; 397 } 398 399 424 public boolean isCaseSensitive(int column) throws SQLException { 425 426 checkColumn(column); 427 428 return columnMetaData[--column].isCaseSensitive; 429 } 430 431 458 public boolean isSearchable(int column) throws SQLException { 459 460 checkColumn(column); 461 462 return columnMetaData[--column].isSearchable; 463 } 464 465 490 public boolean isCurrency(int column) throws SQLException { 491 492 checkColumn(column); 493 494 return columnMetaData[--column].isCurrency; 495 } 496 497 530 public int isNullable(int column) throws SQLException { 531 532 checkColumn(column); 533 534 return columnMetaData[--column].isNullable; 535 } 536 537 558 public boolean isSigned(int column) throws SQLException { 559 560 checkColumn(column); 561 562 return columnMetaData[--column].isSigned; 563 } 564 565 644 public int getColumnDisplaySize(int column) throws SQLException { 645 646 checkColumn(column); 647 648 return columnMetaData[--column].columnDisplaySize; 649 } 650 651 678 public String getColumnLabel(int column) throws SQLException { 679 680 checkColumn(column); 681 682 return columnMetaData[--column].columnLabel; 683 } 684 685 715 public String getColumnName(int column) throws SQLException { 716 717 checkColumn(column); 718 719 column--; 720 721 return useColumnName ? columnMetaData[column].columnName 722 : columnMetaData[column].columnLabel; 723 } 724 725 763 public String getSchemaName(int column) throws SQLException { 764 765 checkColumn(column); 766 767 return columnMetaData[--column].schemaName; 768 } 769 770 804 public int getPrecision(int column) throws SQLException { 805 806 checkColumn(column); 807 808 return columnMetaData[--column].precision; 809 } 810 811 836 public int getScale(int column) throws SQLException { 837 838 checkColumn(column); 839 840 return columnMetaData[--column].scale; 841 } 842 843 851 public String getTableName(int column) throws SQLException { 852 853 checkColumn(column); 854 855 return columnMetaData[--column].tableName; 856 } 857 858 894 public String getCatalogName(int column) throws SQLException { 895 896 checkColumn(column); 897 898 return columnMetaData[--column].catalogName; 899 } 900 901 923 public int getColumnType(int column) throws SQLException { 924 925 checkColumn(column); 926 927 int type = columnMetaData[--column].columnType; 928 929 return type == Types.VARCHAR_IGNORECASE ? Types.VARCHAR 930 : type; 931 } 932 933 943 public String getColumnTypeName(int column) throws SQLException { 944 945 checkColumn(column); 946 947 return columnMetaData[--column].columnTypeName; 948 } 949 950 976 public boolean isReadOnly(int column) throws SQLException { 977 978 checkColumn(column); 979 980 return columnMetaData[--column].isReadOnly; 981 } 982 983 1007 public boolean isWritable(int column) throws SQLException { 1008 1009 checkColumn(column); 1010 1011 return columnMetaData[--column].isWritable; 1012 } 1013 1014 1041 public boolean isDefinitelyWritable(int column) throws SQLException { 1042 1043 checkColumn(column); 1044 1045 return columnMetaData[--column].isDefinitelyWritable; 1058 } 1059 1060 1062 1091 public String getColumnClassName(int column) throws SQLException { 1092 1093 checkColumn(column); 1094 1095 return columnMetaData[--column].columnClassName; 1096 } 1097 1098 public String toString() { 1099 1100 StringBuffer sb = new StringBuffer (); 1101 1102 sb.append(super.toString()); 1103 1104 if (columnCount == 0) { 1105 sb.append("[columnCount=0]"); 1106 1107 return sb.toString(); 1108 } 1109 1110 sb.append('['); 1111 1112 for (int i = 0; i < columnCount; i++) { 1113 sb.append('\n'); 1114 sb.append(" column_"); 1115 sb.append(i + 1); 1116 sb.append('='); 1117 sb.append(columnMetaData[i]); 1118 1119 if (i + 1 < columnCount) { 1120 sb.append(','); 1121 sb.append(' '); 1122 } 1123 } 1124 1125 sb.append('\n'); 1126 sb.append(']'); 1127 1128 return sb.toString(); 1129 } 1130 1131 1133 1140 private void checkColumn(int column) throws SQLException { 1141 1142 if (column < 1 || column > columnCount) { 1143 throw Util.sqlException(Trace.COLUMN_NOT_FOUND, 1144 String.valueOf(column)); 1145 } 1146 } 1147} 1148 | Popular Tags |