Debian Codegraph @

src/main/org/h2/bnf/context/DbSchema.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.Connection;
9 import java.sql.DatabaseMetaData;
10 import java.sql.PreparedStatement;
11 import java.sql.ResultSet;
12 import java.sql.SQLException;
13 import java.sql.SQLSyntaxErrorException;
14 import java.util.ArrayList;
15
16 import org.h2.engine.SysProperties;
17 import org.h2.util.StringUtils;
18 import org.h2.util.Utils;
19
20 /**
21 * Contains meta data information about a database schema.
22 * This class is used by the H2 Console.
23 */
24 public class DbSchema {
25
26 private static final String COLUMNS_QUERY_H2_197 = "SELECT COLUMN_NAME, ORDINAL_POSITION, COLUMN_TYPE "
27 + "FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ?1 AND TABLE_NAME = ?2";
28
29 private static final String COLUMNS_QUERY_H2_202 = "SELECT COLUMN_NAME, ORDINAL_POSITION, "
30 + "DATA_TYPE_SQL(?1, ?2, 'TABLE', ORDINAL_POSITION) COLUMN_TYPE "
31 + "FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ?1 AND TABLE_NAME = ?2";
32
33 /**
34 * The schema name.
35 */
36 public final String name;
37
38 /**
39 * True if this is the default schema for this database.
40 */
41 public final boolean isDefault;
42
43 /**
44 * True if this is a system schema (for example the INFORMATION_SCHEMA).
45 */
46 public final boolean isSystem;
47
48 /**
49 * The quoted schema name.
50 */
51 public final String quotedName;
52
53 /**
54 * The database content container.
55 */
56 private final DbContents contents;
57
58 /**
59 * The table list.
60 */
61 private DbTableOrView[] tables;
62
63 /**
64 * The procedures list.
65 */
66 private DbProcedure[] procedures;
67
68 DbSchema(DbContents contents, String name, boolean isDefault) {
69 this.contents = contents;
70 this.name = name;
71 this.quotedName = contents.quoteIdentifier(name);
72 this.isDefault = isDefault;
73 if (name == null) {
74 // firebird
75 isSystem = true;
76 } else if ("INFORMATION_SCHEMA".equalsIgnoreCase(name)) {
77 isSystem = true;
78 } else if (!contents.isH2() &&
79 StringUtils.toUpperEnglish(name).startsWith("INFO")) {
80 isSystem = true;
81 } else if (contents.isPostgreSQL() &&
82 StringUtils.toUpperEnglish(name).startsWith("PG_")) {
83 isSystem = true;
84 } else if (contents.isDerby() && name.startsWith("SYS")) {
85 isSystem = true;
86 } else {
87 isSystem = false;
88 }
89 }
90
91 /**
92 * @return The database content container.
93 */
94 public DbContents getContents() {
95 return contents;
96 }
97
98 /**
99 * @return The table list.
100 */
101 public DbTableOrView[] getTables() {
102 return tables;
103 }
104
105 /**
106 * @return The procedure list.
107 */
108 public DbProcedure[] getProcedures() {
109 return procedures;
110 }
111
112 /**
113 * Read all tables for this schema from the database meta data.
114 *
115 * @param meta the database meta data
116 * @param tableTypes the table types to read
117 * @throws SQLException on failure
118 */
119 public void readTables(DatabaseMetaData meta, String[] tableTypes)
120 throws SQLException {
121 ResultSet rs = meta.getTables(null, name, null, tableTypes);
122 ArrayList<DbTableOrView> list = new ArrayList<>();
123 while (rs.next()) {
124 DbTableOrView table = new DbTableOrView(this, rs);
125 if (contents.isOracle() && table.getName().indexOf('$') > 0) {
126 continue;
127 }
128 list.add(table);
129 }
130 rs.close();
131 tables = list.toArray(new DbTableOrView[0]);
132 if (tables.length < SysProperties.CONSOLE_MAX_TABLES_LIST_COLUMNS) {
133 try (PreparedStatement ps = contents.isH2() ? prepareColumnsQueryH2(meta.getConnection()) : null) {
134 for (DbTableOrView tab : tables) {
135 try {
136 tab.readColumns(meta, ps);
137 } catch (SQLException e) {
138 // MySQL:
139 // View '...' references invalid table(s) or column(s)
140 // or function(s) or definer/invoker of view
141 // lack rights to use them HY000/1356
142 // ignore
143 }
144 }
145 }
146 }
147 }
148
149 private static PreparedStatement prepareColumnsQueryH2(Connection connection) throws SQLException {
150 try {
151 return connection.prepareStatement(COLUMNS_QUERY_H2_202);
152 } catch (SQLSyntaxErrorException ex) {
153 return connection.prepareStatement(COLUMNS_QUERY_H2_197);
154 }
155 }
156
157 /**
158 * Read all procedures in the database.
159 *
160 * @param meta the database meta data
161 * @throws SQLException Error while fetching procedures
162 */
163 public void readProcedures(DatabaseMetaData meta) throws SQLException {
164 ResultSet rs = meta.getProcedures(null, name, null);
165 ArrayList<DbProcedure> list = Utils.newSmallArrayList();
166 while (rs.next()) {
167 list.add(new DbProcedure(this, rs));
168 }
169 rs.close();
170 procedures = list.toArray(new DbProcedure[0]);
171 if (procedures.length < SysProperties.CONSOLE_MAX_PROCEDURES_LIST_COLUMNS) {
172 for (DbProcedure procedure : procedures) {
173 procedure.readParameters(meta);
174 }
175 }
176 }
177 }