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 | import java.util.ArrayList; |
12 | import org.h2.util.New; |
13 | |
14 | /** |
15 | * Contains meta data information about a table or a view. |
16 | * This class is used by the H2 Console. |
17 | */ |
18 | public class DbTableOrView { |
19 | |
20 | /** |
21 | * The schema this table belongs to. |
22 | */ |
23 | private final DbSchema schema; |
24 | |
25 | /** |
26 | * The table name. |
27 | */ |
28 | private final String name; |
29 | |
30 | /** |
31 | * The quoted table name. |
32 | */ |
33 | private final String quotedName; |
34 | |
35 | /** |
36 | * True if this represents a view. |
37 | */ |
38 | private final boolean isView; |
39 | |
40 | /** |
41 | * The column list. |
42 | */ |
43 | private DbColumn[] columns; |
44 | |
45 | public DbTableOrView(DbSchema schema, ResultSet rs) throws SQLException { |
46 | this.schema = schema; |
47 | name = rs.getString("TABLE_NAME"); |
48 | String type = rs.getString("TABLE_TYPE"); |
49 | isView = "VIEW".equals(type); |
50 | quotedName = schema.getContents().quoteIdentifier(name); |
51 | } |
52 | |
53 | /** |
54 | * @return The schema this table belongs to. |
55 | */ |
56 | public DbSchema getSchema() { |
57 | return schema; |
58 | } |
59 | |
60 | /** |
61 | * @return The column list. |
62 | */ |
63 | public DbColumn[] getColumns() { |
64 | return columns; |
65 | } |
66 | |
67 | /** |
68 | * @return The table name. |
69 | */ |
70 | public String getName() { |
71 | return name; |
72 | } |
73 | |
74 | /** |
75 | * @return True if this represents a view. |
76 | */ |
77 | public boolean isView() { |
78 | return isView; |
79 | } |
80 | |
81 | /** |
82 | * @return The quoted table name. |
83 | */ |
84 | public String getQuotedName() { |
85 | return quotedName; |
86 | } |
87 | |
88 | /** |
89 | * Read the column for this table from the database meta data. |
90 | * |
91 | * @param meta the database meta data |
92 | */ |
93 | public void readColumns(DatabaseMetaData meta) throws SQLException { |
94 | ResultSet rs = meta.getColumns(null, schema.name, name, null); |
95 | ArrayList<DbColumn> list = New.arrayList(); |
96 | while (rs.next()) { |
97 | DbColumn column = DbColumn.getColumn(schema.getContents(), rs); |
98 | list.add(column); |
99 | } |
100 | rs.close(); |
101 | columns = new DbColumn[list.size()]; |
102 | list.toArray(columns); |
103 | } |
104 | |
105 | } |