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.modules.db.explorer.infos;
043:
044: import java.sql.Connection;
045: import java.sql.DatabaseMetaData;
046: import java.sql.ResultSet;
047: import java.sql.SQLException;
048: import java.util.HashMap;
049: import java.util.HashSet;
050: import java.util.Set;
051: import java.util.Vector;
052:
053: import java.util.logging.Level;
054: import java.util.logging.Logger;
055:
056: import org.netbeans.lib.ddl.impl.DriverSpecification;
057: import org.netbeans.api.db.explorer.DatabaseException;
058: import org.netbeans.modules.db.explorer.DatabaseNodeChildren;
059: import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
060:
061: public class IndexListNodeInfo extends DatabaseNodeInfo {
062: static final long serialVersionUID = 5809643799834921044L;
063:
064: public void initChildren(Vector children) throws DatabaseException {
065: try {
066: String table = (String) get(DatabaseNode.TABLE);
067: DriverSpecification drvSpec = getDriverSpecification();
068: Connection con = getConnection();
069: DatabaseMetaData dmd = con.getMetaData();
070: ResultSet rs = dmd.getIndexInfo(drvSpec.getCatalog(),
071: drvSpec.getSchema(), table, false, true);
072: if (rs != null) {
073: Set ixmap = new HashSet();
074: IndexNodeInfo info;
075: while (rs.next()) {
076: HashMap rset = getRow(rs);
077: if (rset == null)
078: continue;
079: if (rset.get(new Integer(6)) != null) {
080: info = (IndexNodeInfo) DatabaseNodeInfo
081: .createNodeInfo(this ,
082: DatabaseNode.INDEX, rset);
083: if (info != null) {
084: if (!ixmap.contains(info.getName())) {
085: ixmap.add(info.getName());
086: info.put("index", info.getName()); //NOI18N
087: children.add(info);
088: }
089: } else
090: throw new Exception(bundle().getString(
091: "EXC_UnableToCreateIndexNodeInfo")); //NOI18N
092: }
093: }
094: rs.close();
095: }
096: } catch (Exception e) {
097: throw new DatabaseException(e);
098: }
099: }
100:
101: public HashMap getRow(ResultSet rs) {
102: HashMap rset = new HashMap();
103: Object value;
104:
105: try {
106: int count = rs.getMetaData().getColumnCount();
107:
108: for (int i = 1; i <= count; i++) {
109: value = null;
110: try {
111: value = rs.getString(i);
112: } catch (SQLException exc) {
113: Logger.getLogger("global").log(Level.INFO, null,
114: exc);
115: rset = null;
116: break;
117: }
118: rset.put(new Integer(i), value);
119: }
120: } catch (SQLException exc) {
121: Logger.getLogger("global").log(Level.INFO, null, exc);
122: rset = null;
123: }
124:
125: return rset;
126: }
127:
128: public void addIndex(String name) throws DatabaseException {
129: try {
130: String table = (String) get(DatabaseNode.TABLE);
131:
132: DriverSpecification drvSpec = getDriverSpecification();
133: drvSpec.getIndexInfo(table, false, true);
134: ResultSet rs = drvSpec.getResultSet();
135: if (rs != null) {
136: HashMap rset = new HashMap();
137: IndexNodeInfo info = null;
138: String findex;
139: while (rs.next()) {
140: rset = drvSpec.getRow();
141: findex = (String) rset.get(new Integer(6));
142: if (findex != null)
143: if (findex.equalsIgnoreCase(name))
144: info = (IndexNodeInfo) DatabaseNodeInfo
145: .createNodeInfo(this ,
146: DatabaseNode.INDEX, rset);
147: rset.clear();
148: }
149: rs.close();
150:
151: if (info != null)
152: ((DatabaseNodeChildren) getNode().getChildren())
153: .createSubnode(info, true);
154: //refresh list of columns due to the column's icons
155: getParent().refreshChildren();
156: }
157: } catch (Exception e) {
158: e.printStackTrace();
159: throw new DatabaseException(e.getMessage());
160: }
161: }
162:
163: }
|