| 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.bnf.context; |
| 7 |
|
| 8 |
import java.sql.DatabaseMetaData; |
| 9 |
import java.sql.ResultSet; |
| 10 |
import java.sql.SQLException; |
| 11 |
|
| 12 |
/** |
| 13 |
* Keeps the meta data information of a column. |
| 14 |
* This class is used by the H2 Console. |
| 15 |
*/ |
| 16 |
public class DbColumn { |
| 17 |
|
| 18 |
private final String name; |
| 19 |
|
| 20 |
private final String quotedName; |
| 21 |
|
| 22 |
private final String dataType; |
| 23 |
|
| 24 |
private final int position; |
| 25 |
|
| 26 |
private DbColumn(DbContents contents, ResultSet rs, boolean procedureColumn) |
| 27 |
throws SQLException { |
| 28 |
name = rs.getString("COLUMN_NAME"); |
| 29 |
quotedName = contents.quoteIdentifier(name); |
| 30 |
position = rs.getInt("ORDINAL_POSITION"); |
| 31 |
if (contents.isH2() && !procedureColumn) { |
| 32 |
dataType = rs.getString("COLUMN_TYPE"); |
| 33 |
return; |
| 34 |
} |
| 35 |
String type = rs.getString("TYPE_NAME"); |
| 36 |
// a procedures column size is identified by PRECISION, for table this |
| 37 |
// is COLUMN_SIZE |
| 38 |
String precisionColumnName, scaleColumnName; |
| 39 |
if (procedureColumn) { |
| 40 |
precisionColumnName = "PRECISION"; |
| 41 |
scaleColumnName = "SCALE"; |
| 42 |
} else { |
| 43 |
precisionColumnName = "COLUMN_SIZE"; |
| 44 |
scaleColumnName = "DECIMAL_DIGITS"; |
| 45 |
} |
| 46 |
int precision = rs.getInt(precisionColumnName); |
| 47 |
if (precision > 0 && !contents.isSQLite()) { |
| 48 |
int scale = rs.getInt(scaleColumnName); |
| 49 |
if (scale > 0) { |
| 50 |
type = type + '(' + precision + ", " + scale + ')'; |
| 51 |
} else { |
| 52 |
type = type + '(' + precision + ')'; |
| 53 |
} |
| 54 |
} |
| 55 |
if (rs.getInt("NULLABLE") == DatabaseMetaData.columnNoNulls) { |
| 56 |
type += " NOT NULL"; |
| 57 |
} |
| 58 |
dataType = type; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Create a column from a DatabaseMetaData.getProcedureColumns row. |
| 63 |
* |
| 64 |
* @param contents the database contents |
| 65 |
* @param rs the result set |
| 66 |
* @return the column |
| 67 |
* @throws SQLException on failure |
| 68 |
*/ |
| 69 |
public static DbColumn getProcedureColumn(DbContents contents, ResultSet rs) |
| 70 |
throws SQLException { |
| 71 |
return new DbColumn(contents, rs, true); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Create a column from a DatabaseMetaData.getColumns row. |
| 76 |
* |
| 77 |
* @param contents the database contents |
| 78 |
* @param rs the result set |
| 79 |
* @return the column |
| 80 |
* @throws SQLException on failure |
| 81 |
*/ |
| 82 |
public static DbColumn getColumn(DbContents contents, ResultSet rs) |
| 83 |
throws SQLException { |
| 84 |
return new DbColumn(contents, rs, false); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @return The data type name (including precision and the NOT NULL flag if |
| 89 |
* applicable). |
| 90 |
*/ |
| 91 |
public String getDataType() { |
| 92 |
return dataType; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @return The column name. |
| 97 |
*/ |
| 98 |
public String getName() { |
| 99 |
return name; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @return The quoted table name. |
| 104 |
*/ |
| 105 |
public String getQuotedName() { |
| 106 |
return quotedName; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @return Column index |
| 111 |
*/ |
| 112 |
public int getPosition() { |
| 113 |
return position; |
| 114 |
} |
| 115 |
|
| 116 |
} |