| 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.ResultSet; |
| 11 |
import java.sql.SQLException; |
| 12 |
import java.util.ArrayList; |
| 13 |
|
| 14 |
import org.h2.engine.Session; |
| 15 |
import org.h2.jdbc.JdbcConnection; |
| 16 |
import org.h2.util.ParserUtil; |
| 17 |
import org.h2.util.StringUtils; |
| 18 |
import org.h2.util.Utils; |
| 19 |
|
| 20 |
/** |
| 21 |
* Keeps meta data information about a database. |
| 22 |
* This class is used by the H2 Console. |
| 23 |
*/ |
| 24 |
public class DbContents { |
| 25 |
|
| 26 |
private DbSchema[] schemas; |
| 27 |
private DbSchema defaultSchema; |
| 28 |
private boolean isOracle; |
| 29 |
private boolean isH2; |
| 30 |
private boolean isPostgreSQL; |
| 31 |
private boolean isDerby; |
| 32 |
private boolean isSQLite; |
| 33 |
private boolean isMySQL; |
| 34 |
private boolean isFirebird; |
| 35 |
private boolean isMSSQLServer; |
| 36 |
private boolean isDB2; |
| 37 |
|
| 38 |
private boolean databaseToUpper, databaseToLower; |
| 39 |
|
| 40 |
private boolean mayHaveStandardViews = true; |
| 41 |
|
| 42 |
/** |
| 43 |
* @return the default schema. |
| 44 |
*/ |
| 45 |
public DbSchema getDefaultSchema() { |
| 46 |
return defaultSchema; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @return true if this is an Apache Derby database. |
| 51 |
*/ |
| 52 |
public boolean isDerby() { |
| 53 |
return isDerby; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @return true if this is a Firebird database. |
| 58 |
*/ |
| 59 |
public boolean isFirebird() { |
| 60 |
return isFirebird; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @return true if this is a H2 database. |
| 65 |
*/ |
| 66 |
public boolean isH2() { |
| 67 |
return isH2; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @return true if this is a MS SQL Server database. |
| 72 |
*/ |
| 73 |
public boolean isMSSQLServer() { |
| 74 |
return isMSSQLServer; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @return true if this is a MySQL database. |
| 79 |
*/ |
| 80 |
public boolean isMySQL() { |
| 81 |
return isMySQL; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @return true if this is an Oracle database. |
| 86 |
*/ |
| 87 |
public boolean isOracle() { |
| 88 |
return isOracle; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @return true if this is a PostgreSQL database. |
| 93 |
*/ |
| 94 |
public boolean isPostgreSQL() { |
| 95 |
return isPostgreSQL; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @return true if this is an SQLite database. |
| 100 |
*/ |
| 101 |
public boolean isSQLite() { |
| 102 |
return isSQLite; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @return true if this is an IBM DB2 database. |
| 107 |
*/ |
| 108 |
public boolean isDB2() { |
| 109 |
return isDB2; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @return the list of schemas. |
| 114 |
*/ |
| 115 |
public DbSchema[] getSchemas() { |
| 116 |
return schemas; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Returns whether standard INFORMATION_SCHEMA.VIEWS may be supported. |
| 121 |
* |
| 122 |
* @return whether standard INFORMATION_SCHEMA.VIEWS may be supported |
| 123 |
*/ |
| 124 |
public boolean mayHaveStandardViews() { |
| 125 |
return mayHaveStandardViews; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @param mayHaveStandardViews |
| 130 |
* whether standard INFORMATION_SCHEMA.VIEWS is detected as |
| 131 |
* supported |
| 132 |
*/ |
| 133 |
public void setMayHaveStandardViews(boolean mayHaveStandardViews) { |
| 134 |
this.mayHaveStandardViews = mayHaveStandardViews; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Read the contents of this database from the database meta data. |
| 139 |
* |
| 140 |
* @param url the database URL |
| 141 |
* @param conn the connection |
| 142 |
* @throws SQLException on failure |
| 143 |
*/ |
| 144 |
public synchronized void readContents(String url, Connection conn) |
| 145 |
throws SQLException { |
| 146 |
isH2 = url.startsWith("jdbc:h2:"); |
| 147 |
isDB2 = url.startsWith("jdbc:db2:"); |
| 148 |
isSQLite = url.startsWith("jdbc:sqlite:"); |
| 149 |
isOracle = url.startsWith("jdbc:oracle:"); |
| 150 |
// the Vertica engine is based on PostgreSQL |
| 151 |
isPostgreSQL = url.startsWith("jdbc:postgresql:") || url.startsWith("jdbc:vertica:"); |
| 152 |
// isHSQLDB = url.startsWith("jdbc:hsqldb:"); |
| 153 |
isMySQL = url.startsWith("jdbc:mysql:"); |
| 154 |
isDerby = url.startsWith("jdbc:derby:"); |
| 155 |
isFirebird = url.startsWith("jdbc:firebirdsql:"); |
| 156 |
isMSSQLServer = url.startsWith("jdbc:sqlserver:"); |
| 157 |
if (isH2) { |
| 158 |
Session.StaticSettings settings = ((JdbcConnection) conn).getStaticSettings(); |
| 159 |
databaseToUpper = settings.databaseToUpper; |
| 160 |
databaseToLower = settings.databaseToLower; |
| 161 |
}else if (isMySQL || isPostgreSQL) { |
| 162 |
databaseToUpper = false; |
| 163 |
databaseToLower = true; |
| 164 |
} else { |
| 165 |
databaseToUpper = true; |
| 166 |
databaseToLower = false; |
| 167 |
} |
| 168 |
DatabaseMetaData meta = conn.getMetaData(); |
| 169 |
String defaultSchemaName = getDefaultSchemaName(meta); |
| 170 |
String[] schemaNames = getSchemaNames(meta); |
| 171 |
schemas = new DbSchema[schemaNames.length]; |
| 172 |
for (int i = 0; i < schemaNames.length; i++) { |
| 173 |
String schemaName = schemaNames[i]; |
| 174 |
boolean isDefault = defaultSchemaName == null || |
| 175 |
defaultSchemaName.equals(schemaName); |
| 176 |
DbSchema schema = new DbSchema(this, schemaName, isDefault); |
| 177 |
if (isDefault) { |
| 178 |
defaultSchema = schema; |
| 179 |
} |
| 180 |
schemas[i] = schema; |
| 181 |
String[] tableTypes = { "TABLE", "SYSTEM TABLE", "VIEW", |
| 182 |
"SYSTEM VIEW", "TABLE LINK", "SYNONYM", "EXTERNAL" }; |
| 183 |
schema.readTables(meta, tableTypes); |
| 184 |
if (!isPostgreSQL && !isDB2) { |
| 185 |
schema.readProcedures(meta); |
| 186 |
} |
| 187 |
} |
| 188 |
if (defaultSchema == null) { |
| 189 |
String best = null; |
| 190 |
for (DbSchema schema : schemas) { |
| 191 |
if ("dbo".equals(schema.name)) { |
| 192 |
// MS SQL Server |
| 193 |
defaultSchema = schema; |
| 194 |
break; |
| 195 |
} |
| 196 |
if (defaultSchema == null || |
| 197 |
best == null || |
| 198 |
schema.name.length() < best.length()) { |
| 199 |
best = schema.name; |
| 200 |
defaultSchema = schema; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
private String[] getSchemaNames(DatabaseMetaData meta) throws SQLException { |
| 207 |
if (isMySQL || isSQLite) { |
| 208 |
return new String[] { "" }; |
| 209 |
} else if (isFirebird) { |
| 210 |
return new String[] { null }; |
| 211 |
} |
| 212 |
ResultSet rs = meta.getSchemas(); |
| 213 |
ArrayList<String> schemaList = Utils.newSmallArrayList(); |
| 214 |
while (rs.next()) { |
| 215 |
String schema = rs.getString("TABLE_SCHEM"); |
| 216 |
String[] ignoreNames = null; |
| 217 |
if (isOracle) { |
| 218 |
ignoreNames = new String[] { "CTXSYS", "DIP", "DBSNMP", |
| 219 |
"DMSYS", "EXFSYS", "FLOWS_020100", "FLOWS_FILES", |
| 220 |
"MDDATA", "MDSYS", "MGMT_VIEW", "OLAPSYS", "ORDSYS", |
| 221 |
"ORDPLUGINS", "OUTLN", "SI_INFORMTN_SCHEMA", "SYS", |
| 222 |
"SYSMAN", "SYSTEM", "TSMSYS", "WMSYS", "XDB" }; |
| 223 |
} else if (isMSSQLServer) { |
| 224 |
ignoreNames = new String[] { "sys", "db_accessadmin", |
| 225 |
"db_backupoperator", "db_datareader", "db_datawriter", |
| 226 |
"db_ddladmin", "db_denydatareader", |
| 227 |
"db_denydatawriter", "db_owner", "db_securityadmin" }; |
| 228 |
} else if (isDB2) { |
| 229 |
ignoreNames = new String[] { "NULLID", "SYSFUN", |
| 230 |
"SYSIBMINTERNAL", "SYSIBMTS", "SYSPROC", "SYSPUBLIC", |
| 231 |
// not empty, but not sure what they contain |
| 232 |
"SYSCAT", "SYSIBM", "SYSIBMADM", |
| 233 |
"SYSSTAT", "SYSTOOLS", |
| 234 |
}; |
| 235 |
|
| 236 |
} |
| 237 |
if (ignoreNames != null) { |
| 238 |
for (String ignore : ignoreNames) { |
| 239 |
if (ignore.equals(schema)) { |
| 240 |
schema = null; |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
if (schema == null) { |
| 246 |
continue; |
| 247 |
} |
| 248 |
schemaList.add(schema); |
| 249 |
} |
| 250 |
rs.close(); |
| 251 |
return schemaList.toArray(new String[0]); |
| 252 |
} |
| 253 |
|
| 254 |
private String getDefaultSchemaName(DatabaseMetaData meta) { |
| 255 |
String defaultSchemaName = ""; |
| 256 |
try { |
| 257 |
if (isH2) { |
| 258 |
return meta.storesLowerCaseIdentifiers() ? "public" : "PUBLIC"; |
| 259 |
} else if (isOracle) { |
| 260 |
return meta.getUserName(); |
| 261 |
} else if (isPostgreSQL) { |
| 262 |
return "public"; |
| 263 |
} else if (isMySQL) { |
| 264 |
return ""; |
| 265 |
} else if (isDerby) { |
| 266 |
return StringUtils.toUpperEnglish(meta.getUserName()); |
| 267 |
} else if (isFirebird) { |
| 268 |
return null; |
| 269 |
} |
| 270 |
} catch (SQLException e) { |
| 271 |
// Ignore |
| 272 |
} |
| 273 |
return defaultSchemaName; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Add double quotes around an identifier if required. |
| 278 |
* |
| 279 |
* @param identifier the identifier |
| 280 |
* @return the quoted identifier |
| 281 |
*/ |
| 282 |
public String quoteIdentifier(String identifier) { |
| 283 |
if (identifier == null) { |
| 284 |
return null; |
| 285 |
} |
| 286 |
if (ParserUtil.isSimpleIdentifier(identifier, databaseToUpper, databaseToLower)) { |
| 287 |
return identifier; |
| 288 |
} |
| 289 |
return StringUtils.quoteIdentifier(identifier); |
| 290 |
} |
| 291 |
|
| 292 |
} |