Debian Codegraph @

src/main/org/h2/bnf/context/DbTableOrView.java

raw
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.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import java.util.ArrayList;
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 * @param ps prepared statement with custom query for H2 database, null for
93 * others
94 * @throws SQLException on failure
95 */
96 public void readColumns(DatabaseMetaData meta, PreparedStatement ps) throws SQLException {
97 ResultSet rs;
98 if (schema.getContents().isH2()) {
99 ps.setString(1, schema.name);
100 ps.setString(2, name);
101 rs = ps.executeQuery();
102 } else {
103 rs = meta.getColumns(null, schema.name, name, null);
104 }
105 ArrayList<DbColumn> list = new ArrayList<>();
106 while (rs.next()) {
107 DbColumn column = DbColumn.getColumn(schema.getContents(), rs);
108 list.add(column);
109 }
110 rs.close();
111 columns = list.toArray(new DbColumn[0]);
112 }
113
114 }