| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.jdbc; |
| 7 |
|
| 8 |
import java.sql.ResultSetMetaData; |
| 9 |
import java.sql.SQLException; |
| 10 |
import org.h2.message.DbException; |
| 11 |
import org.h2.message.Trace; |
| 12 |
import org.h2.message.TraceObject; |
| 13 |
import org.h2.result.ResultInterface; |
| 14 |
import org.h2.util.MathUtils; |
| 15 |
import org.h2.value.DataType; |
| 16 |
import org.h2.value.ValueToObjectConverter; |
| 17 |
|
| 18 |
/** |
| 19 |
* Represents the meta data for a ResultSet. |
| 20 |
*/ |
| 21 |
public final class JdbcResultSetMetaData extends TraceObject implements ResultSetMetaData { |
| 22 |
|
| 23 |
private final String catalog; |
| 24 |
private final JdbcResultSet rs; |
| 25 |
private final JdbcPreparedStatement prep; |
| 26 |
private final ResultInterface result; |
| 27 |
private final int columnCount; |
| 28 |
|
| 29 |
JdbcResultSetMetaData(JdbcResultSet rs, JdbcPreparedStatement prep, |
| 30 |
ResultInterface result, String catalog, Trace trace, int id) { |
| 31 |
setTrace(trace, TraceObject.RESULT_SET_META_DATA, id); |
| 32 |
this.catalog = catalog; |
| 33 |
this.rs = rs; |
| 34 |
this.prep = prep; |
| 35 |
this.result = result; |
| 36 |
this.columnCount = result.getVisibleColumnCount(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the number of columns. |
| 41 |
* |
| 42 |
* @return the number of columns |
| 43 |
* @throws SQLException if the result set is closed or invalid |
| 44 |
*/ |
| 45 |
@Override |
| 46 |
public int getColumnCount() throws SQLException { |
| 47 |
try { |
| 48 |
debugCodeCall("getColumnCount"); |
| 49 |
checkClosed(); |
| 50 |
return columnCount; |
| 51 |
} catch (Exception e) { |
| 52 |
throw logAndConvert(e); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the column label. |
| 58 |
* |
| 59 |
* @param column the column index (1,2,...) |
| 60 |
* @return the column label |
| 61 |
* @throws SQLException if the result set is closed or invalid |
| 62 |
*/ |
| 63 |
@Override |
| 64 |
public String getColumnLabel(int column) throws SQLException { |
| 65 |
try { |
| 66 |
return result.getAlias(getColumn("getColumnLabel", column)); |
| 67 |
} catch (Exception e) { |
| 68 |
throw logAndConvert(e); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the column name. |
| 74 |
* |
| 75 |
* @param column the column index (1,2,...) |
| 76 |
* @return the column name |
| 77 |
* @throws SQLException if the result set is closed or invalid |
| 78 |
*/ |
| 79 |
@Override |
| 80 |
public String getColumnName(int column) throws SQLException { |
| 81 |
try { |
| 82 |
return result.getColumnName(getColumn("getColumnName", column)); |
| 83 |
} catch (Exception e) { |
| 84 |
throw logAndConvert(e); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns the data type of a column. |
| 90 |
* See also java.sql.Type. |
| 91 |
* |
| 92 |
* @param column the column index (1,2,...) |
| 93 |
* @return the data type |
| 94 |
* @throws SQLException if the result set is closed or invalid |
| 95 |
*/ |
| 96 |
@Override |
| 97 |
public int getColumnType(int column) throws SQLException { |
| 98 |
try { |
| 99 |
return DataType.convertTypeToSQLType(result.getColumnType(getColumn("getColumnType", column))); |
| 100 |
} catch (Exception e) { |
| 101 |
throw logAndConvert(e); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns the data type name of a column. |
| 107 |
* |
| 108 |
* @param column the column index (1,2,...) |
| 109 |
* @return the data type name |
| 110 |
* @throws SQLException if the result set is closed or invalid |
| 111 |
*/ |
| 112 |
@Override |
| 113 |
public String getColumnTypeName(int column) throws SQLException { |
| 114 |
try { |
| 115 |
return result.getColumnType(getColumn("getColumnTypeName", column)).getDeclaredTypeName(); |
| 116 |
} catch (Exception e) { |
| 117 |
throw logAndConvert(e); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Returns the schema name. |
| 123 |
* |
| 124 |
* @param column the column index (1,2,...) |
| 125 |
* @return the schema name, or "" (an empty string) if not applicable |
| 126 |
* @throws SQLException if the result set is closed or invalid |
| 127 |
*/ |
| 128 |
@Override |
| 129 |
public String getSchemaName(int column) throws SQLException { |
| 130 |
try { |
| 131 |
String schema = result.getSchemaName(getColumn("getSchemaName", column)); |
| 132 |
return schema == null ? "" : schema; |
| 133 |
} catch (Exception e) { |
| 134 |
throw logAndConvert(e); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Returns the table name. |
| 140 |
* |
| 141 |
* @param column the column index (1,2,...) |
| 142 |
* @return the table name |
| 143 |
* @throws SQLException if the result set is closed or invalid |
| 144 |
*/ |
| 145 |
@Override |
| 146 |
public String getTableName(int column) throws SQLException { |
| 147 |
try { |
| 148 |
String table = result.getTableName(getColumn("getTableName", column)); |
| 149 |
return table == null ? "" : table; |
| 150 |
} catch (Exception e) { |
| 151 |
throw logAndConvert(e); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Returns the catalog name. |
| 157 |
* |
| 158 |
* @param column the column index (1,2,...) |
| 159 |
* @return the catalog name |
| 160 |
* @throws SQLException if the result set is closed or invalid |
| 161 |
*/ |
| 162 |
@Override |
| 163 |
public String getCatalogName(int column) throws SQLException { |
| 164 |
try { |
| 165 |
getColumn("getCatalogName", column); |
| 166 |
return catalog == null ? "" : catalog; |
| 167 |
} catch (Exception e) { |
| 168 |
throw logAndConvert(e); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Checks if this an autoincrement column. |
| 174 |
* |
| 175 |
* @param column the column index (1,2,...) |
| 176 |
* @return false |
| 177 |
* @throws SQLException if the result set is closed or invalid |
| 178 |
*/ |
| 179 |
@Override |
| 180 |
public boolean isAutoIncrement(int column) throws SQLException { |
| 181 |
try { |
| 182 |
return result.isIdentity(getColumn("isAutoIncrement", column)); |
| 183 |
} catch (Exception e) { |
| 184 |
throw logAndConvert(e); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Checks if this column is case sensitive. |
| 190 |
* It always returns true. |
| 191 |
* |
| 192 |
* @param column the column index (1,2,...) |
| 193 |
* @return true |
| 194 |
* @throws SQLException if the result set is closed or invalid |
| 195 |
*/ |
| 196 |
@Override |
| 197 |
public boolean isCaseSensitive(int column) throws SQLException { |
| 198 |
try { |
| 199 |
getColumn("isCaseSensitive", column); |
| 200 |
return true; |
| 201 |
} catch (Exception e) { |
| 202 |
throw logAndConvert(e); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Checks if this column is searchable. |
| 208 |
* It always returns true. |
| 209 |
* |
| 210 |
* @param column the column index (1,2,...) |
| 211 |
* @return true |
| 212 |
* @throws SQLException if the result set is closed or invalid |
| 213 |
*/ |
| 214 |
@Override |
| 215 |
public boolean isSearchable(int column) throws SQLException { |
| 216 |
try { |
| 217 |
getColumn("isSearchable", column); |
| 218 |
return true; |
| 219 |
} catch (Exception e) { |
| 220 |
throw logAndConvert(e); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Checks if this is a currency column. |
| 226 |
* It always returns false. |
| 227 |
* |
| 228 |
* @param column the column index (1,2,...) |
| 229 |
* @return false |
| 230 |
* @throws SQLException if the result set is closed or invalid |
| 231 |
*/ |
| 232 |
@Override |
| 233 |
public boolean isCurrency(int column) throws SQLException { |
| 234 |
try { |
| 235 |
getColumn("isCurrency", column); |
| 236 |
return false; |
| 237 |
} catch (Exception e) { |
| 238 |
throw logAndConvert(e); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Checks if this is nullable column. Returns |
| 244 |
* ResultSetMetaData.columnNullableUnknown if this is not a column of a |
| 245 |
* table. Otherwise, it returns ResultSetMetaData.columnNoNulls if the |
| 246 |
* column is not nullable, and ResultSetMetaData.columnNullable if it is |
| 247 |
* nullable. |
| 248 |
* |
| 249 |
* @param column the column index (1,2,...) |
| 250 |
* @return ResultSetMetaData.column* |
| 251 |
* @throws SQLException if the result set is closed or invalid |
| 252 |
*/ |
| 253 |
@Override |
| 254 |
public int isNullable(int column) throws SQLException { |
| 255 |
try { |
| 256 |
return result.getNullable(getColumn("isNullable", column)); |
| 257 |
} catch (Exception e) { |
| 258 |
throw logAndConvert(e); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Checks if this column is signed. |
| 264 |
* Returns true for numeric columns. |
| 265 |
* |
| 266 |
* @param column the column index (1,2,...) |
| 267 |
* @return true for numeric columns |
| 268 |
* @throws SQLException if the result set is closed or invalid |
| 269 |
*/ |
| 270 |
@Override |
| 271 |
public boolean isSigned(int column) throws SQLException { |
| 272 |
try { |
| 273 |
return DataType.isNumericType(result.getColumnType(getColumn("isSigned", column)).getValueType()); |
| 274 |
} catch (Exception e) { |
| 275 |
throw logAndConvert(e); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Checks if this column is read only. |
| 281 |
* It always returns false. |
| 282 |
* |
| 283 |
* @param column the column index (1,2,...) |
| 284 |
* @return false |
| 285 |
* @throws SQLException if the result set is closed or invalid |
| 286 |
*/ |
| 287 |
@Override |
| 288 |
public boolean isReadOnly(int column) throws SQLException { |
| 289 |
try { |
| 290 |
getColumn("isReadOnly", column); |
| 291 |
return false; |
| 292 |
} catch (Exception e) { |
| 293 |
throw logAndConvert(e); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Checks whether it is possible for a write on this column to succeed. |
| 299 |
* It always returns true. |
| 300 |
* |
| 301 |
* @param column the column index (1,2,...) |
| 302 |
* @return true |
| 303 |
* @throws SQLException if the result set is closed or invalid |
| 304 |
*/ |
| 305 |
@Override |
| 306 |
public boolean isWritable(int column) throws SQLException { |
| 307 |
try { |
| 308 |
getColumn("isWritable", column); |
| 309 |
return true; |
| 310 |
} catch (Exception e) { |
| 311 |
throw logAndConvert(e); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Checks whether a write on this column will definitely succeed. |
| 317 |
* It always returns false. |
| 318 |
* |
| 319 |
* @param column the column index (1,2,...) |
| 320 |
* @return false |
| 321 |
* @throws SQLException if the result set is closed or invalid |
| 322 |
*/ |
| 323 |
@Override |
| 324 |
public boolean isDefinitelyWritable(int column) throws SQLException { |
| 325 |
try { |
| 326 |
getColumn("isDefinitelyWritable", column); |
| 327 |
return false; |
| 328 |
} catch (Exception e) { |
| 329 |
throw logAndConvert(e); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Gets the Java class name of the object that will be returned |
| 335 |
* if ResultSet.getObject is called. |
| 336 |
* |
| 337 |
* @param column the column index (1,2,...) |
| 338 |
* @return the Java class name |
| 339 |
* @throws SQLException if the result set is closed or invalid |
| 340 |
*/ |
| 341 |
@Override |
| 342 |
public String getColumnClassName(int column) throws SQLException { |
| 343 |
try { |
| 344 |
int type = result.getColumnType(getColumn("getColumnClassName", column)).getValueType(); |
| 345 |
return ValueToObjectConverter.getDefaultClass(type, true).getName(); |
| 346 |
} catch (Exception e) { |
| 347 |
throw logAndConvert(e); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Gets the precision for this column. |
| 353 |
* |
| 354 |
* @param column the column index (1,2,...) |
| 355 |
* @return the precision |
| 356 |
* @throws SQLException if the result set is closed or invalid |
| 357 |
*/ |
| 358 |
@Override |
| 359 |
public int getPrecision(int column) throws SQLException { |
| 360 |
try { |
| 361 |
return MathUtils.convertLongToInt(result.getColumnType(getColumn("getPrecision", column)).getPrecision()); |
| 362 |
} catch (Exception e) { |
| 363 |
throw logAndConvert(e); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Gets the scale for this column. |
| 369 |
* |
| 370 |
* @param column the column index (1,2,...) |
| 371 |
* @return the scale |
| 372 |
* @throws SQLException if the result set is closed or invalid |
| 373 |
*/ |
| 374 |
@Override |
| 375 |
public int getScale(int column) throws SQLException { |
| 376 |
try { |
| 377 |
return result.getColumnType(getColumn("getScale", column)).getScale(); |
| 378 |
} catch (Exception e) { |
| 379 |
throw logAndConvert(e); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Gets the maximum display size for this column. |
| 385 |
* |
| 386 |
* @param column the column index (1,2,...) |
| 387 |
* @return the display size |
| 388 |
* @throws SQLException if the result set is closed or invalid |
| 389 |
*/ |
| 390 |
@Override |
| 391 |
public int getColumnDisplaySize(int column) throws SQLException { |
| 392 |
try { |
| 393 |
return result.getColumnType(getColumn("getColumnDisplaySize", column)).getDisplaySize(); |
| 394 |
} catch (Exception e) { |
| 395 |
throw logAndConvert(e); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
private void checkClosed() { |
| 400 |
if (rs != null) { |
| 401 |
rs.checkClosed(); |
| 402 |
} |
| 403 |
if (prep != null) { |
| 404 |
prep.checkClosed(); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Writes trace information and checks validity of this object and |
| 410 |
* parameter. |
| 411 |
* |
| 412 |
* @param methodName |
| 413 |
* the called method name |
| 414 |
* @param columnIndex |
| 415 |
* 1-based column index |
| 416 |
* @return 0-based column index |
| 417 |
*/ |
| 418 |
private int getColumn(String methodName, int columnIndex) { |
| 419 |
debugCodeCall(methodName, columnIndex); |
| 420 |
checkClosed(); |
| 421 |
if (columnIndex < 1 || columnIndex > columnCount) { |
| 422 |
throw DbException.getInvalidValueException("columnIndex", columnIndex); |
| 423 |
} |
| 424 |
return columnIndex - 1; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Return an object of this class if possible. |
| 429 |
* |
| 430 |
* @param iface the class |
| 431 |
* @return this |
| 432 |
*/ |
| 433 |
@Override |
| 434 |
@SuppressWarnings("unchecked") |
| 435 |
public <T> T unwrap(Class<T> iface) throws SQLException { |
| 436 |
try { |
| 437 |
if (isWrapperFor(iface)) { |
| 438 |
return (T) this; |
| 439 |
} |
| 440 |
throw DbException.getInvalidValueException("iface", iface); |
| 441 |
} catch (Exception e) { |
| 442 |
throw logAndConvert(e); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* Checks if unwrap can return an object of this class. |
| 448 |
* |
| 449 |
* @param iface the class |
| 450 |
* @return whether or not the interface is assignable from this class |
| 451 |
*/ |
| 452 |
@Override |
| 453 |
public boolean isWrapperFor(Class<?> iface) throws SQLException { |
| 454 |
return iface != null && iface.isAssignableFrom(getClass()); |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* INTERNAL |
| 459 |
*/ |
| 460 |
@Override |
| 461 |
public String toString() { |
| 462 |
return getTraceObjectName() + ": columns=" + columnCount; |
| 463 |
} |
| 464 |
|
| 465 |
} |