1 5 package org.h2.server.web; 6 7 import java.sql.DatabaseMetaData ; 8 import java.sql.ResultSet ; 9 import java.sql.SQLException ; 10 import java.util.ArrayList ; 11 12 import org.h2.command.Parser; 13 import org.h2.util.StringUtils; 14 15 public class DbContents { 16 DbSchema[] schemas; 17 DbSchema defaultSchema; 18 boolean isOracle, isH2, isPostgreSQL, isMySQL, isDerby, isFirebird, isSQLite; 19 20 void readContents(DatabaseMetaData meta) throws SQLException { 21 String prod = StringUtils.toLowerEnglish(meta.getDatabaseProductName()); 22 isSQLite = prod.indexOf("sqlite") >= 0; 23 String url = meta.getURL(); 24 if(url != null) { 25 isH2 = url.startsWith("jdbc:h2:"); 26 isOracle = url.startsWith("jdbc:oracle:"); 27 isPostgreSQL = url.startsWith("jdbc:postgresql:"); 28 isMySQL = url.startsWith("jdbc:mysql:"); 30 isDerby = url.startsWith("jdbc:derby:"); 31 isFirebird = url.startsWith("jdbc:firebirdsql:"); 32 } 33 String defaultSchemaName = getDefaultSchemaName(meta); 34 String [] schemaNames = getSchemaNames(meta); 35 schemas = new DbSchema[schemaNames.length]; 36 for(int i=0; i<schemaNames.length; i++) { 37 String schemaName = schemaNames[i]; 38 boolean isDefault = defaultSchemaName==null || defaultSchemaName.equals(schemaName); 39 DbSchema schema = new DbSchema(this, schemaName, isDefault); 40 if(schema.isDefault) { 41 defaultSchema = schema; 42 } 43 schemas[i] = schema; 44 String [] tableTypes = new String []{"TABLE", "SYSTEM TABLE", "VIEW", "SYSTEM VIEW", "TABLE LINK", "SYNONYM"}; 45 schema.readTables(meta, tableTypes); 46 } 47 if(defaultSchema == null) { 48 String best = null; 49 for(int i=0; i<schemas.length; i++) { 50 if("dbo".equals(schemas[i].name)) { 51 defaultSchema = schemas[i]; 53 break; 54 } 55 if(defaultSchema == null || best == null || schemas[i].name.length() < best.length()) { 56 best = schemas[i].name; 57 defaultSchema = schemas[i]; 58 } 59 } 60 } 61 } 62 63 private String [] getSchemaNames(DatabaseMetaData meta) throws SQLException { 64 if(isMySQL) { 65 return new String []{""}; 66 } else if(isFirebird) { 67 return new String []{null}; 68 } 69 ResultSet rs = meta.getSchemas(); 70 ArrayList schemas = new ArrayList (); 71 while(rs.next()) { 72 String schema = rs.getString("TABLE_SCHEM"); 73 if(schema == null) { 74 continue; 75 } 76 schemas.add(schema); 77 } 78 rs.close(); 79 String [] list = new String [schemas.size()]; 80 schemas.toArray(list); 81 return list; 82 } 83 84 private String getDefaultSchemaName(DatabaseMetaData meta) throws SQLException { 85 String defaultSchemaName = ""; 86 try { 87 if(isOracle) { 88 return meta.getUserName(); 89 } else if(isPostgreSQL) { 90 return "public"; 91 } else if(isMySQL) { 92 return ""; 93 } else if(isDerby) { 94 return StringUtils.toUpperEnglish(meta.getUserName()); 95 } else if(isFirebird) { 96 return null; 97 } 98 ResultSet rs = meta.getSchemas(); 99 int index = rs.findColumn("IS_DEFAULT"); 100 while(rs.next()) { 101 if(rs.getBoolean(index)) { 102 defaultSchemaName = rs.getString("TABLE_SCHEM"); 103 } 104 } 105 } catch(SQLException e) { 106 } 108 return defaultSchemaName; 109 } 110 111 String quoteIdentifier(String identifier) { 112 if(identifier == null) { 113 return null; 114 } 115 if(isH2) { 116 return Parser.quoteIdentifier(identifier); 117 } else { 118 return StringUtils.toUpperEnglish(identifier); 119 } 122 } 123 124 } 125 | Popular Tags |