1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://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 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 | String type = rs.getString("TYPE_NAME"); |
31 | // a procedures column size is identified by PRECISION, for table this |
32 | // is COLUMN_SIZE |
33 | String precisionColumnName; |
34 | if (procedureColumn) { |
35 | precisionColumnName = "PRECISION"; |
36 | } else { |
37 | precisionColumnName = "COLUMN_SIZE"; |
38 | } |
39 | int precision = rs.getInt(precisionColumnName); |
40 | position = rs.getInt("ORDINAL_POSITION"); |
41 | boolean isSQLite = contents.isSQLite(); |
42 | if (precision > 0 && !isSQLite) { |
43 | type += "(" + precision; |
44 | String scaleColumnName; |
45 | if (procedureColumn) { |
46 | scaleColumnName = "SCALE"; |
47 | } else { |
48 | scaleColumnName = "DECIMAL_DIGITS"; |
49 | } |
50 | int prec = rs.getInt(scaleColumnName); |
51 | if (prec > 0) { |
52 | type += ", " + prec; |
53 | } |
54 | type += ")"; |
55 | } |
56 | if (rs.getInt("NULLABLE") == DatabaseMetaData.columnNoNulls) { |
57 | type += " NOT NULL"; |
58 | } |
59 | dataType = type; |
60 | } |
61 | |
62 | /** |
63 | * Create a column from a DatabaseMetaData.getProcedureColumns row. |
64 | * |
65 | * @param contents the database contents |
66 | * @param rs the result set |
67 | * @return the column |
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 | */ |
81 | public static DbColumn getColumn(DbContents contents, ResultSet rs) |
82 | throws SQLException { |
83 | return new DbColumn(contents, rs, false); |
84 | } |
85 | |
86 | /** |
87 | * @return The data type name (including precision and the NOT NULL flag if |
88 | * applicable). |
89 | */ |
90 | public String getDataType() { |
91 | return dataType; |
92 | } |
93 | |
94 | /** |
95 | * @return The column name. |
96 | */ |
97 | public String getName() { |
98 | return name; |
99 | } |
100 | |
101 | /** |
102 | * @return The quoted table name. |
103 | */ |
104 | public String getQuotedName() { |
105 | return quotedName; |
106 | } |
107 | |
108 | /** |
109 | * @return Column index |
110 | */ |
111 | public int getPosition() { |
112 | return position; |
113 | } |
114 | |
115 | } |