001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.ddl.impl;
043:
044: import java.sql.DatabaseMetaData;
045: import java.sql.ResultSet;
046: import java.sql.SQLException;
047: import java.util.HashMap;
048: import java.util.LinkedList;
049:
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052:
053: import org.netbeans.lib.ddl.DriverSpecificationFactory;
054:
055: public class DriverSpecification {
056:
057: /** Used DBConnection */
058: private HashMap desc;
059:
060: private String catalog, schema;
061:
062: private DatabaseMetaData dmd;
063:
064: private ResultSet rs;
065:
066: private String quoteString;
067:
068: /** Owned factory */
069: SpecificationFactory factory;
070:
071: /** Constructor */
072: public DriverSpecification(HashMap description) {
073: desc = description;
074: quoteString = null;
075: }
076:
077: public DriverSpecificationFactory getDriverSpecificationFactory() {
078: return factory;
079: }
080:
081: public void setDriverSpecificationFactory(
082: DriverSpecificationFactory fac) {
083: factory = (SpecificationFactory) fac;
084: }
085:
086: public void setCatalog(String catalog) {
087: if (catalog == null || dmd == null) {
088: this .catalog = catalog;
089: return;
090: } else
091: catalog.trim();
092:
093: ResultSet rs;
094: LinkedList list = new LinkedList();
095:
096: try {
097: rs = dmd.getCatalogs();
098: while (rs.next())
099: list.add(rs.getString(1).trim());
100: rs.close();
101: } catch (SQLException exc) {
102: Logger.getLogger("global").log(Level.INFO, null, exc);
103:
104: // this.catalog = catalog;
105: this .catalog = null; //hack for IBM ODBC driver
106: rs = null;
107: return;
108: }
109:
110: if (list.contains(catalog))
111: this .catalog = catalog;
112: else
113: this .catalog = null; //hack for Sybase ODBC driver
114: }
115:
116: public String getCatalog() {
117: return catalog;
118: }
119:
120: public void setSchema(String schema) {
121: this .schema = schema;
122: }
123:
124: public String getSchema() {
125: return schema;
126: }
127:
128: public void setMetaData(DatabaseMetaData dmd) {
129: this .dmd = dmd;
130: }
131:
132: public DatabaseMetaData getMetaData() {
133: return dmd;
134: }
135:
136: public void getTables(String tableNamePattern, String[] types)
137: throws SQLException {
138: try {
139: rs = dmd
140: .getTables(catalog, schema, tableNamePattern, types);
141: } catch (SQLException exc) {
142: rs = null;
143: throw exc;
144: }
145: }
146:
147: public void getProcedures(String procedureNamePattern)
148: throws SQLException {
149: try {
150: procedureNamePattern = quoteString(procedureNamePattern);
151: rs = dmd.getProcedures(catalog, schema,
152: procedureNamePattern);
153: } catch (SQLException exc) {
154: rs = null;
155: throw exc;
156: }
157: }
158:
159: public void getPrimaryKeys(String table) throws SQLException {
160: try {
161: table = quoteString(table);
162: rs = dmd.getPrimaryKeys(catalog, schema, table);
163: } catch (SQLException exc) {
164: rs = null;
165: throw exc;
166: }
167: }
168:
169: public void getIndexInfo(String table, boolean unique,
170: boolean approximate) throws SQLException {
171: try {
172: table = quoteString(table);
173: rs = dmd.getIndexInfo(catalog, schema, table, unique,
174: approximate);
175: } catch (SQLException exc) {
176: rs = null;
177: throw exc;
178: }
179: }
180:
181: public void getColumns(String tableNamePattern,
182: String columnNamePattern) throws SQLException {
183: try {
184: tableNamePattern = quoteString(tableNamePattern);
185: columnNamePattern = quoteString(columnNamePattern);
186: rs = dmd.getColumns(catalog, schema, tableNamePattern,
187: columnNamePattern);
188: } catch (SQLException exc) {
189: rs = null;
190: throw exc;
191: }
192: }
193:
194: public void getProcedureColumns(String procedureNamePattern,
195: String columnNamePattern) throws SQLException {
196: try {
197: procedureNamePattern = quoteString(procedureNamePattern);
198: columnNamePattern = quoteString(columnNamePattern);
199: rs = dmd.getProcedureColumns(catalog, schema,
200: procedureNamePattern, columnNamePattern);
201: } catch (SQLException exc) {
202: rs = null;
203: throw exc;
204: }
205: }
206:
207: public void getExportedKeys(String table) throws SQLException {
208: try {
209: table = quoteString(table);
210: rs = dmd.getExportedKeys(catalog, schema, table);
211: } catch (SQLException exc) {
212: rs = null;
213: throw exc;
214: }
215: }
216:
217: public void getImportedKeys(String table) throws SQLException {
218: try {
219: table = quoteString(table);
220: rs = dmd.getImportedKeys(catalog, schema, table);
221: } catch (SQLException exc) {
222: rs = null;
223: throw exc;
224: }
225: }
226:
227: public ResultSet getResultSet() {
228: return rs;
229: }
230:
231: public HashMap getRow() throws SQLException {
232: HashMap rset = new HashMap();
233: Object value;
234:
235: try {
236: int count = rs.getMetaData().getColumnCount();
237:
238: for (int i = 1; i <= count; i++) {
239: value = null;
240: try {
241: value = rs.getString(i);
242: // value = rs.getObject(i); //cannot use getObject() because of problems with MSSQL ODBC driver
243: } catch (SQLException exc) {
244: rset = null;
245: // break;
246: throw exc;
247: }
248: rset.put(new Integer(i), value);
249: }
250: } catch (SQLException exc) {
251: rset = null;
252: throw exc;
253: }
254:
255: return rset;
256: }
257:
258: //another patches
259:
260: public boolean areViewsSupported() {
261: try {
262: String productName = dmd.getDatabaseProductName().trim();
263:
264: if ("PointBase".equals(productName)) { // NOI18N
265: int driverMajorVersion = dmd.getDriverMajorVersion();
266: int driverMinorVersion = dmd.getDriverMinorVersion();
267: return ((driverMajorVersion == 4 && driverMinorVersion >= 1) || driverMajorVersion > 4);
268: } else if ("MySQL".equals(productName)) { // NOI18N
269: int databaseMajorVersion = dmd
270: .getDatabaseMajorVersion();
271: return (databaseMajorVersion >= 5);
272: } else if ("HypersonicSQL".equals(productName)) { // NOI18N
273: // XXX is this still true for HypersonicSQL?
274: return false;
275: }
276: } catch (SQLException exc) {
277: Logger.getLogger("global").log(Level.INFO, null, exc);
278: }
279:
280: return true;
281: }
282:
283: private String getQuoteString() {
284: if (quoteString == null) {
285: try {
286: quoteString = dmd.getIdentifierQuoteString();
287: if (quoteString == null || quoteString.equals(" ")) //NOI18N
288: quoteString = ""; //NOI18N
289: else
290: quoteString.trim();
291: } catch (SQLException exc) {
292: quoteString = ""; //NOI18N
293: }
294: }
295:
296: return quoteString;
297: }
298:
299: private String quoteString(String str) {
300: try {
301: if (dmd.getDatabaseProductName().trim().equals("PointBase")) { //NOI18N
302: //hack for PointBase - DatabaseMetaData methods require quoted arguments for case sensitive identifiers
303: String quoteStr = getQuoteString();
304: if (str != null && !str.equals("%")
305: && !quoteStr.equals("")) //NOI18N
306: str = quoteStr + str + quoteStr;
307: }
308: } catch (SQLException exc) {
309: //PENDING
310: }
311:
312: return str;
313: }
314:
315: public String getDBName() {
316: try {
317: return dmd.getDatabaseProductName().trim();
318: } catch (SQLException exc) {
319: //PENDING
320: return null;
321: }
322: }
323: }
|