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