KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > editor > completion > db > Catalog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.editor.completion.db;
21
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.TreeMap JavaDoc;
26
27 /**
28  *
29  * @author abadea
30  */

31 public class Catalog {
32     
33     private final DBMetaDataProvider provider;
34     private final String JavaDoc name;
35     
36     private Map JavaDoc/*<String, Schema>*/ schemas;
37     
38     Catalog(DBMetaDataProvider provider, String JavaDoc name) {
39         this.provider = provider;
40         this.name = name;
41     }
42     
43     public String JavaDoc getName() {
44         return name;
45     }
46     
47     public synchronized Schema[] getSchemas() throws SQLException JavaDoc {
48         if (schemas == null) {
49             schemas = new TreeMap JavaDoc();
50             ResultSet JavaDoc rs = null;
51             
52             if (name == null) {
53                 // assuming the current catalog when the catalog name is null
54
rs = provider.getMetaData().getSchemas();
55             } else {
56                 // DatabaseMetaData.getSchemas() can not be used to retrieved the
57
// list of schemas in a given catalog, since it (e.g. for the JTDS
58
// driver) only returns the schemas in the current catalog. The
59
// workaround is to retrieve all tables from all schemas in the given
60
// catalog and obtain a schema list from that. This is not perfect,
61
// since it will not return the schemas containig neither tables nor views.
62
rs = provider.getMetaData().getTables(name, "%", "%", new String JavaDoc[] { "TABLE", "VIEW" }); // NOI18N
63
}
64
65             try {
66                 while (rs.next()) {
67                     String JavaDoc schemaName = rs.getString("TABLE_SCHEM"); // NOI18N
68
Schema schema = new Schema(provider, this, schemaName);
69                     schemas.put(schemaName, schema);
70                 }
71             } finally {
72                 rs.close();
73             }
74         }
75         
76         return (Schema[])schemas.values().toArray(new Schema[schemas.size()]);
77     }
78     
79     public synchronized Schema getSchema(String JavaDoc name) throws SQLException JavaDoc {
80         if (schemas == null) {
81             getSchemas();
82         }
83         
84         return (Schema)schemas.get(name);
85     }
86     
87     public String JavaDoc toString() {
88         return "Catalog[name='" + name + "']"; // NOI18N
89
}
90 }
91
Popular Tags