KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > ddl > impl > DriverSpecification


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.lib.ddl.impl;
21
22 import java.sql.DatabaseMetaData JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.LinkedList JavaDoc;
27
28 import org.openide.ErrorManager;
29
30 import org.netbeans.lib.ddl.DriverSpecificationFactory;
31
32 public class DriverSpecification {
33
34     /** Used DBConnection */
35     private HashMap JavaDoc desc;
36
37     private String JavaDoc catalog, schema;
38
39     private DatabaseMetaData JavaDoc dmd;
40
41     private ResultSet JavaDoc rs;
42
43     private String JavaDoc quoteString;
44
45     /** Owned factory */
46     SpecificationFactory factory;
47
48     /** Constructor */
49     public DriverSpecification(HashMap JavaDoc description) {
50         desc = description;
51         quoteString = null;
52     }
53
54     public DriverSpecificationFactory getDriverSpecificationFactory() {
55         return factory;
56     }
57
58     public void setDriverSpecificationFactory(DriverSpecificationFactory fac) {
59         factory = (SpecificationFactory) fac;
60     }
61
62     public void setCatalog(String JavaDoc catalog) {
63         if (catalog == null || dmd == null) {
64             this.catalog = catalog;
65             return;
66         } else
67             catalog.trim();
68
69         ResultSet JavaDoc rs;
70         LinkedList JavaDoc list = new LinkedList JavaDoc();
71
72         try {
73             rs = dmd.getCatalogs();
74             while (rs.next())
75                 list.add(rs.getString(1).trim());
76             rs.close();
77         } catch (SQLException JavaDoc exc) {
78             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
79             
80 // this.catalog = catalog;
81
this.catalog = null; //hack for IBM ODBC driver
82
rs = null;
83             return;
84         }
85
86         if (list.contains(catalog))
87             this.catalog = catalog;
88         else
89             this.catalog = null; //hack for Sybase ODBC driver
90
}
91
92     public String JavaDoc getCatalog() {
93         return catalog;
94     }
95
96     public void setSchema(String JavaDoc schema) {
97         this.schema = schema;
98     }
99
100     public String JavaDoc getSchema() {
101         return schema;
102     }
103
104     public void setMetaData(DatabaseMetaData JavaDoc dmd) {
105         this.dmd = dmd;
106     }
107     
108     public DatabaseMetaData JavaDoc getMetaData() {
109         return dmd;
110     }
111
112     public void getTables(String JavaDoc tableNamePattern, String JavaDoc[] types) throws SQLException JavaDoc {
113         try {
114             tableNamePattern = quoteString(tableNamePattern);
115             rs = dmd.getTables(catalog, schema, tableNamePattern, types);
116         } catch (SQLException JavaDoc exc) {
117             rs = null;
118             throw exc;
119         }
120     }
121
122     public void getProcedures(String JavaDoc procedureNamePattern) throws SQLException JavaDoc {
123         try {
124             procedureNamePattern = quoteString(procedureNamePattern);
125             rs = dmd.getProcedures(catalog, schema, procedureNamePattern);
126         } catch (SQLException JavaDoc exc) {
127             rs = null;
128             throw exc;
129         }
130     }
131
132     public void getPrimaryKeys(String JavaDoc table) throws SQLException JavaDoc {
133         try {
134             table = quoteString(table);
135             rs = dmd.getPrimaryKeys(catalog, schema, table);
136         } catch (SQLException JavaDoc exc) {
137             rs = null;
138             throw exc;
139         }
140     }
141
142     public void getIndexInfo(String JavaDoc table, boolean unique, boolean approximate) throws SQLException JavaDoc {
143         try {
144             table = quoteString(table);
145             rs = dmd.getIndexInfo(catalog, schema, table, unique, approximate);
146         } catch (SQLException JavaDoc exc) {
147             rs = null;
148             throw exc;
149         }
150     }
151
152     public void getColumns(String JavaDoc tableNamePattern, String JavaDoc columnNamePattern) throws SQLException JavaDoc {
153         try {
154             tableNamePattern = quoteString(tableNamePattern);
155             columnNamePattern = quoteString(columnNamePattern);
156             rs = dmd.getColumns(catalog, schema, tableNamePattern, columnNamePattern);
157         } catch (SQLException JavaDoc exc) {
158             rs = null;
159             throw exc;
160         }
161     }
162
163     public void getProcedureColumns(String JavaDoc procedureNamePattern, String JavaDoc columnNamePattern) throws SQLException JavaDoc {
164         try {
165             procedureNamePattern = quoteString(procedureNamePattern);
166             columnNamePattern = quoteString(columnNamePattern);
167             rs = dmd.getProcedureColumns(catalog, schema, procedureNamePattern, columnNamePattern);
168         } catch (SQLException JavaDoc exc) {
169             rs = null;
170             throw exc;
171         }
172     }
173
174     public void getExportedKeys(String JavaDoc table) throws SQLException JavaDoc {
175         try {
176             table = quoteString(table);
177             rs = dmd.getExportedKeys(catalog, schema, table);
178         } catch (SQLException JavaDoc exc) {
179             rs = null;
180             throw exc;
181         }
182     }
183
184     public void getImportedKeys(String JavaDoc table) throws SQLException JavaDoc {
185         try {
186             table = quoteString(table);
187             rs = dmd.getImportedKeys(catalog, schema, table);
188         } catch (SQLException JavaDoc exc) {
189             rs = null;
190             throw exc;
191         }
192     }
193
194     public ResultSet JavaDoc getResultSet() {
195         return rs;
196     }
197
198     public HashMap JavaDoc getRow() throws SQLException JavaDoc {
199         HashMap JavaDoc rset = new HashMap JavaDoc();
200         Object JavaDoc value;
201
202         try {
203             int count = rs.getMetaData().getColumnCount();
204
205             for (int i = 1; i <= count; i++) {
206                 value = null;
207                 try {
208                     value = rs.getString(i);
209 // value = rs.getObject(i); //cannot use getObject() because of problems with MSSQL ODBC driver
210
} catch (SQLException JavaDoc exc) {
211                     rset = null;
212                     // break;
213
throw exc;
214                 }
215                 rset.put(new Integer JavaDoc(i), value);
216             }
217         } catch (SQLException JavaDoc exc) {
218             rset = null;
219             throw exc;
220         }
221
222         return rset;
223     }
224
225     //another patches
226

227     public boolean areViewsSupported() {
228         try {
229             String JavaDoc productName = dmd.getDatabaseProductName().trim();
230             
231             if ("PointBase".equals(productName)) { // NOI18N
232
int driverMajorVersion = dmd.getDriverMajorVersion();
233                 int driverMinorVersion = dmd.getDriverMinorVersion();
234                 return ((driverMajorVersion == 4 && driverMinorVersion >= 1) || driverMajorVersion > 4);
235             } else if ("MySQL".equals(productName)) { // NOI18N
236
int databaseMajorVersion = dmd.getDatabaseMajorVersion();
237                 return (databaseMajorVersion >= 5);
238             } else if ("HypersonicSQL".equals(productName)) { // NOI18N
239
// XXX is this still true for HypersonicSQL?
240
return false;
241             }
242         } catch(SQLException JavaDoc exc) {
243             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
244         }
245         
246         return true;
247     }
248
249     private String JavaDoc getQuoteString() {
250         if (quoteString == null) {
251             try {
252                 quoteString = dmd.getIdentifierQuoteString();
253                 if (quoteString == null || quoteString.equals(" ")) //NOI18N
254
quoteString = ""; //NOI18N
255
else
256                     quoteString.trim();
257             } catch (SQLException JavaDoc exc) {
258                 quoteString = ""; //NOI18N
259
}
260         }
261         
262         return quoteString;
263     }
264     
265     private String JavaDoc quoteString(String JavaDoc str) {
266         try {
267             if (dmd.getDatabaseProductName().trim().equals("PointBase")) { //NOI18N
268
//hack for PointBase - DatabaseMetaData methods require quoted arguments for case sensitive identifiers
269
String JavaDoc quoteStr = getQuoteString();
270                 if (str != null && !str.equals("%") && !quoteStr.equals("")) //NOI18N
271
str = quoteStr + str + quoteStr;
272             }
273         } catch (SQLException JavaDoc exc) {
274             //PENDING
275
}
276         
277         return str;
278     }
279     
280     public String JavaDoc getDBName() {
281         try {
282             return dmd.getDatabaseProductName().trim();
283         } catch (SQLException JavaDoc exc) {
284             //PENDING
285
return null;
286         }
287     }
288 }
289
Popular Tags