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-2007 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: package org.netbeans.modules.visualweb.dataconnectivity.sql;
042:
043: import java.sql.DatabaseMetaData;
044: import java.sql.ResultSet;
045: import java.sql.SQLException;
046: import java.util.ArrayList;
047: import java.util.Locale;
048: import java.util.ResourceBundle;
049:
050: /**
051: * Just in time table meta data. Cached after retreived.
052: *
053: * @author John Kline
054: */
055: public class ProcedureMetaData {
056:
057: private static ResourceBundle rb = ResourceBundle
058: .getBundle(
059: "org.netbeans.modules.visualweb.dataconnectivity.sql.Bundle",
060: Locale.getDefault());
061:
062: public static final int PROCEDURE_CAT = 0;
063: public static final int PROCEDURE_SCHEM = 1;
064: public static final int PROCEDURE_NAME = 2;
065: public static final int REMARKS = 3;
066: public static final int PROCEDURE_TYPE = 4;
067: private static final String[] metaNames = { "PROCEDURE_CAT", // NOI18N
068: "PROCEDURE_SCHEM", // NOI18N
069: "PROCEDURE_NAME", // NOI18N
070: "REMARKS", // NOI18N
071: "PROCEDURE_TYPE" // NOI18N
072: };
073: private Object[] metaValues;
074: private ProcedureColumnMetaData[] procedureColumnMetaData;
075: private DatabaseMetaData dbmd;
076:
077: ProcedureMetaData(ResultSet resultSet, DatabaseMetaData dbmd)
078: throws SQLException {
079: this .dbmd = dbmd;
080: int exceptionCount = 0;
081: SQLException firstException = null;
082: metaValues = new Object[metaNames.length];
083: for (int i = 0; i < metaNames.length; i++) {
084: try {
085: metaValues[i] = resultSet.getObject(metaNames[i]);
086: } catch (SQLException e) {
087: metaValues[i] = null;
088: exceptionCount++;
089: if (firstException == null) {
090: firstException = e;
091: }
092: }
093: }
094: if (exceptionCount == metaNames.length) {
095: throw firstException;
096: }
097: // Delay getting column metadata until it is needed
098: procedureColumnMetaData = null;
099: }
100:
101: public Object getMetaInfo(String name) throws SQLException {
102: for (int i = 0; i < metaNames.length; i++) {
103: if (name.equals(metaNames[i])) {
104: return metaValues[i];
105: }
106: }
107: throw new SQLException(rb.getString("NAME_NOT_FOUND") + ": "
108: + name); // NOI18N
109: }
110:
111: public String getMetaInfoAsString(String name) throws SQLException {
112: Object o = getMetaInfo(name);
113: return (o == null) ? null : o.toString();
114: }
115:
116: public Object getMetaInfo(int index) throws SQLException {
117: if (index < 0 || index > metaValues.length) {
118: throw new SQLException(rb.getString("NO_SUCH_INDEX") + ": "
119: + index); // NOI18N
120: }
121: return metaValues[index];
122: }
123:
124: public String getMetaInfoAsString(int index) throws SQLException {
125: Object o = getMetaInfo(index);
126: return (o == null) ? null : o.toString();
127: }
128:
129: public ProcedureColumnMetaData[] getProcedureColumnMetaData()
130: throws SQLException {
131: if (procedureColumnMetaData == null) {
132: ArrayList list = new ArrayList();
133: ResultSet colRs = dbmd.getColumns(null,
134: (String) metaValues[PROCEDURE_SCHEM],
135: (String) metaValues[PROCEDURE_NAME], "%"); // NOI18N
136: while (colRs.next()) {
137: list.add(new ProcedureColumnMetaData(colRs));
138: }
139: colRs.close();
140: procedureColumnMetaData = (ProcedureColumnMetaData[]) list
141: .toArray(new ProcedureColumnMetaData[0]);
142: }
143: return procedureColumnMetaData;
144: }
145:
146: public ProcedureColumnMetaData getProcedureColumnMetaData(
147: String columnName) throws SQLException {
148: for (int i = 0; i < getProcedureColumnMetaData().length; i++) {
149: if (getProcedureColumnMetaData()[i].getMetaInfo(
150: ProcedureColumnMetaData.COLUMN_NAME).equals(
151: columnName)) {
152: return getProcedureColumnMetaData()[i];
153: }
154: }
155: throw new SQLException(rb.getString("COLUMN_NOT_FOUND") + ": "
156: + columnName); // NOI18N
157: }
158:
159: public String[] getColumns() throws SQLException {
160: ArrayList list = new ArrayList();
161: for (int i = 0; i < getProcedureColumnMetaData().length; i++) {
162: list
163: .add(getProcedureColumnMetaData()[i]
164: .getMetaInfoAsString(ProcedureColumnMetaData.COLUMN_NAME));
165: }
166: return (String[]) list.toArray(new String[0]);
167: }
168: }
|