File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\server\web\DbContents.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.server.web;
8
9 import java.sql.DatabaseMetaData;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import java.sql.Statement;
13 import java.util.ArrayList;
14
15 import org.h2.command.Parser;
16 import org.h2.util.New;
17 import org.h2.util.StringUtils;
18
19 /**
20 * Keeps meta data information about a database.
21 * This class is used by the H2 Console.
22 */
23 public class DbContents {
24
25 /**
26 * The list of schemas.
27 */
28 DbSchema[] schemas;
29
30 /**
31 * The default schema.
32 */
33 DbSchema defaultSchema;
34
35 /**
36 * True if this is an Oracle database.
37 */
38 boolean isOracle;
39
40 /**
41 * True if this is a H2 database.
42 */
43 boolean isH2;
44
45 /**
46 * True if this is a PostgreSQL database.
47 */
48 boolean isPostgreSQL;
49
50 /**
51 * True if this is an Apache Derby database.
52 */
53 boolean isDerby;
54
55 /**
56 * True if this is an SQLite database.
57 */
58 boolean isSQLite;
59
60 /**
61 * True if this is a H2 database in MySQL mode.
62 */
63 private boolean isH2ModeMySQL;
64
65 /**
66 * True if this is a MySQL database.
67 */
68 private boolean isMySQL;
69
70 /**
71 * True if this is a Firebird database.
72 */
73 private boolean isFirebird;
74
75 /**
76 * True if this is a MS SQL Server database.
77 */
78 private boolean isMSSQLServer;
79
80 /**
81 * Get the column index of a column in a result set. If the column is not
82 * found, the default column index is returned.
83 * This is a workaround for a JDBC-ODBC bridge problem.
84 *
85 * @param rs the result set
86 * @param columnName the column name
87 * @param defaultColumnIndex the default column index
88 * @return the column index
89 */
90 static int findColumn(ResultSet rs, String columnName, int defaultColumnIndex) {
91 try {
92 return rs.findColumn(columnName);
93 } catch (SQLException e) {
94 return defaultColumnIndex;
95 }
96 }
97
98 /**
99 * Read the contents of this database from the database meta data.
100 *
101 * @param meta the database meta data
102 */
103 synchronized void readContents(DatabaseMetaData meta) throws SQLException {
104 String prod = StringUtils.toLowerEnglish(meta.getDatabaseProductName());
105 isSQLite = prod.indexOf("sqlite") >= 0;
106 String url = meta.getURL();
107 if (url != null) {
108 isH2 = url.startsWith("jdbc:h2:");
109 if (isH2) {
110 Statement stat = meta.getConnection().createStatement();
111 ResultSet rs = stat.executeQuery(
112 "SELECT UPPER(VALUE) FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME='MODE'");
113 rs.next();
114 if ("MYSQL".equals(rs.getString(1))) {
115 isH2ModeMySQL = true;
116 }
117 rs.close();
118 stat.close();
119 }
120 isOracle = url.startsWith("jdbc:oracle:");
121 isPostgreSQL = url.startsWith("jdbc:postgresql:");
122 // isHSQLDB = url.startsWith("jdbc:hsqldb:");
123 isMySQL = url.startsWith("jdbc:mysql:");
124 isDerby = url.startsWith("jdbc:derby:");
125 isFirebird = url.startsWith("jdbc:firebirdsql:");
126 isMSSQLServer = url.startsWith("jdbc:sqlserver:");
127 }
128 String defaultSchemaName = getDefaultSchemaName(meta);
129 String[] schemaNames = getSchemaNames(meta);
130 schemas = new DbSchema[schemaNames.length];
131 for (int i = 0; i < schemaNames.length; i++) {
132 String schemaName = schemaNames[i];
133 boolean isDefault = defaultSchemaName == null || defaultSchemaName.equals(schemaName);
134 DbSchema schema = new DbSchema(this, schemaName, isDefault);
135 if (schema.isDefault) {
136 defaultSchema = schema;
137 }
138 schemas[i] = schema;
139 String[] tableTypes = { "TABLE", "SYSTEM TABLE", "VIEW", "SYSTEM VIEW",
140 "TABLE LINK", "SYNONYM" };
141 schema.readTables(meta, tableTypes);
142 }
143 if (defaultSchema == null) {
144 String best = null;
145 for (DbSchema schema : schemas) {
146 if ("dbo".equals(schema.name)) {
147 // MS SQL Server
148 defaultSchema = schema;
149 break;
150 }
151 if (defaultSchema == null || best == null || schema.name.length() < best.length()) {
152 best = schema.name;
153 defaultSchema = schema;
154 }
155 }
156 }
157 }
158
159 private String[] getSchemaNames(DatabaseMetaData meta) throws SQLException {
160 if (isMySQL || isSQLite) {
161 return new String[] { "" };
162 } else if (isFirebird) {
163 return new String[] { null };
164 }
165 ResultSet rs = meta.getSchemas();
166 ArrayList<String> schemaList = New.arrayList();
167 while (rs.next()) {
168 String schema = rs.getString(findColumn(rs, "TABLE_SCHEM", 1));
169 if (isOracle) {
170 for (String ignore : new String[] {
171 "CTXSYS", "DIP", "DBSNMP", "DMSYS", "EXFSYS", "FLOWS_020100", "FLOWS_FILES", "MDDATA", "MDSYS",
172 "MGMT_VIEW", "OLAPSYS", "ORDSYS", "ORDPLUGINS", "OUTLN", "SI_INFORMTN_SCHEMA", "SYS", "SYSMAN",
173 "SYSTEM", "TSMSYS", "WMSYS", "XDB"
174 }) {
175 if (ignore.equals(schema)) {
176 schema = null;
177 break;
178 }
179 }
180 } else if (isMSSQLServer) {
181 for (String ignore : new String[] {
182 "sys", "db_accessadmin", "db_backupoperator", "db_datareader", "db_datawriter", "db_ddladmin",
183 "db_denydatareader", "db_denydatawriter", "db_owner", "db_securityadmin"
184 }) {
185 if (ignore.equals(schema)) {
186 schema = null;
187 break;
188 }
189 }
190 }
191 if (schema == null) {
192 continue;
193 }
194 schemaList.add(schema);
195 }
196 rs.close();
197 String[] list = new String[schemaList.size()];
198 schemaList.toArray(list);
199 return list;
200 }
201
202 private String getDefaultSchemaName(DatabaseMetaData meta) {
203 String defaultSchemaName = "";
204 try {
205 if (isOracle) {
206 return meta.getUserName();
207 } else if (isPostgreSQL) {
208 return "public";
209 } else if (isMySQL) {
210 return "";
211 } else if (isDerby) {
212 return StringUtils.toUpperEnglish(meta.getUserName());
213 } else if (isFirebird) {
214 return null;
215 }
216 ResultSet rs = meta.getSchemas();
217 int index = rs.findColumn("IS_DEFAULT");
218 while (rs.next()) {
219 if (rs.getBoolean(index)) {
220 defaultSchemaName = rs.getString("TABLE_SCHEM");
221 }
222 }
223 } catch (SQLException e) {
224 // IS_DEFAULT not found
225 }
226 return defaultSchemaName;
227 }
228
229 /**
230 * Add double quotes around an identifier if required.
231 * For the H2 database, all identifiers are quoted.
232 *
233 * @param identifier the identifier
234 * @return the quoted identifier
235 */
236 String quoteIdentifier(String identifier) {
237 if (identifier == null) {
238 return null;
239 }
240 if (isH2 && !isH2ModeMySQL) {
241 return Parser.quoteIdentifier(identifier);
242 }
243 return StringUtils.toUpperEnglish(identifier);
244 }
245
246 }
247