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 TableMetaData {
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 TABLE_CAT = 0;
063: public static final int TABLE_SCHEM = 1;
064: public static final int TABLE_NAME = 2;
065: public static final int TABLE_TYPE = 3;
066: public static final int REMARKS = 4;
067: public static final int TYPE_CAT = 5;
068: public static final int TYPE_SCHEM = 6;
069: public static final int TYPE_NAME = 7;
070: public static final int SELF_REFERENCING_COL_NAME = 8;
071: public static final int REF_GENERATION = 9;
072: private static final String[] metaNames = { "TABLE_CAT", // NOI18N
073: "TABLE_SCHEM", // NOI18N
074: "TABLE_NAME", // NOI18N
075: "TABLE_TYPE", // NOI18N
076: "REMARKS", // NOI18N
077: "TYPE_CAT", // NOI18N
078: "TYPE_SCHEM", // NOI18N
079: "TYPE_NAME", // NOI18N
080: "SELF_REFERENCING_COL_NAME", // NOI18N
081: "REF_GENERATION" // NOI18N
082: };
083: private String[] metaValues;
084: private ColumnMetaData[] columnMetaData;
085: private DatabaseMetaData dbmd;
086:
087: TableMetaData(ResultSet resultSet, DatabaseMetaData dbmd)
088: throws SQLException {
089: this .dbmd = dbmd;
090: int exceptionCount = 0;
091: SQLException firstException = null;
092: metaValues = new String[metaNames.length];
093: for (int i = 0; i < metaNames.length; i++) {
094: try {
095: metaValues[i] = resultSet.getString(metaNames[i]);
096: } catch (SQLException e) {
097: metaValues[i] = null;
098: exceptionCount++;
099: if (firstException == null) {
100: firstException = e;
101: }
102: }
103: }
104: if (exceptionCount == metaNames.length) {
105: throw firstException;
106: }
107: // Delay getting column metadata until it is needed
108: columnMetaData = null;
109: }
110:
111: public String getMetaInfo(String name) throws SQLException {
112: for (int i = 0; i < metaNames.length; i++) {
113: if (name.equals(metaNames[i])) {
114: return metaValues[i];
115: }
116: }
117: throw new SQLException(rb.getString("NAME_NOT_FOUND") + ": "
118: + name); // NOI18N
119: }
120:
121: public String getMetaInfo(int index) throws SQLException {
122: if (index < 0 || index > metaValues.length) {
123: throw new SQLException(rb.getString("NO_SUCH_INDEX") + ": "
124: + index); // NOI18N
125: }
126: return metaValues[index];
127: }
128:
129: public ColumnMetaData[] getColumnMetaData() throws SQLException {
130: if (columnMetaData == null) {
131: ArrayList list = new ArrayList();
132: ResultSet colRs = dbmd.getColumns(null,
133: metaValues[TABLE_SCHEM], metaValues[TABLE_NAME],
134: "%"); // NOI18N
135: while (colRs.next()) {
136: list.add(new ColumnMetaData(colRs));
137: }
138: colRs.close();
139: columnMetaData = (ColumnMetaData[]) list
140: .toArray(new ColumnMetaData[0]);
141: }
142: return columnMetaData;
143: }
144:
145: public ColumnMetaData getColumnMetaData(String columnName)
146: throws SQLException {
147: for (int i = 0; i < getColumnMetaData().length; i++) {
148: if (getColumnMetaData()[i].getMetaInfo(
149: ColumnMetaData.COLUMN_NAME).equals(columnName)) {
150: return getColumnMetaData()[i];
151: }
152: }
153: throw new SQLException(rb.getString("COLUMN_NOT_FOUND") + ": "
154: + columnName); // NOI18N
155: }
156:
157: public String[] getColumns() throws SQLException {
158: ArrayList list = new ArrayList();
159: for (int i = 0; i < getColumnMetaData().length; i++) {
160: list.add(getColumnMetaData()[i]
161: .getMetaInfoAsString(ColumnMetaData.COLUMN_NAME));
162: }
163: return (String[]) list.toArray(new String[0]);
164: }
165: }
|