KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > server > web > DbContents


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.server.web;
6
7 import java.sql.DatabaseMetaData JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.util.ArrayList JavaDoc;
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 JavaDoc meta) throws SQLException JavaDoc {
21         String JavaDoc prod = StringUtils.toLowerEnglish(meta.getDatabaseProductName());
22         isSQLite = prod.indexOf("sqlite") >= 0;
23         String JavaDoc 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             // isHSQLDB = url.startsWith("jdbc:hsqldb:");
29
isMySQL = url.startsWith("jdbc:mysql:");
30             isDerby = url.startsWith("jdbc:derby:");
31             isFirebird = url.startsWith("jdbc:firebirdsql:");
32         }
33         String JavaDoc defaultSchemaName = getDefaultSchemaName(meta);
34         String JavaDoc[] schemaNames = getSchemaNames(meta);
35         schemas = new DbSchema[schemaNames.length];
36         for(int i=0; i<schemaNames.length; i++) {
37             String JavaDoc 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 JavaDoc[] tableTypes = new String JavaDoc[]{"TABLE", "SYSTEM TABLE", "VIEW", "SYSTEM VIEW", "TABLE LINK", "SYNONYM"};
45             schema.readTables(meta, tableTypes);
46         }
47         if(defaultSchema == null) {
48             String JavaDoc best = null;
49             for(int i=0; i<schemas.length; i++) {
50                 if("dbo".equals(schemas[i].name)) {
51                     // MS SQL Server
52
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 JavaDoc[] getSchemaNames(DatabaseMetaData JavaDoc meta) throws SQLException JavaDoc {
64         if(isMySQL) {
65             return new String JavaDoc[]{""};
66         } else if(isFirebird) {
67             return new String JavaDoc[]{null};
68         }
69         ResultSet JavaDoc rs = meta.getSchemas();
70         ArrayList JavaDoc schemas = new ArrayList JavaDoc();
71         while(rs.next()) {
72             String JavaDoc schema = rs.getString("TABLE_SCHEM");
73             if(schema == null) {
74                 continue;
75             }
76             schemas.add(schema);
77         }
78         rs.close();
79         String JavaDoc[] list = new String JavaDoc[schemas.size()];
80         schemas.toArray(list);
81         return list;
82     }
83     
84     private String JavaDoc getDefaultSchemaName(DatabaseMetaData JavaDoc meta) throws SQLException JavaDoc {
85         String JavaDoc 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 JavaDoc 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 JavaDoc e) {
106             // IS_DEFAULT not found
107
}
108         return defaultSchemaName;
109     }
110     
111     String JavaDoc quoteIdentifier(String JavaDoc 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 // } else {
120
// return Generator.quoteIdentifierAlways(identifier);
121
}
122     }
123     
124 }
125
Popular Tags