001: package net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.expanders;
002:
003: /*
004: * Copyright (C) 2007 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.sql.PreparedStatement;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: import net.sourceforge.squirrel_sql.client.session.ISession;
028: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.INodeExpander;
029: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.ObjectTreeNode;
030: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectInfo;
031: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
032: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
033: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
034: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
035: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
036: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
037:
038: /**
039: *
040: * @author manningr
041: *
042: */
043: public class IndexParentExpander implements INodeExpander {
044: /** Logger for this class. */
045: private static final ILogger s_log = LoggerController
046: .createLogger(IndexParentExpander.class);
047:
048: /** the db-specific extractor implementation */
049: private ITableIndexExtractor extractor = null;
050:
051: /**
052: * Default ctor.
053: */
054: public IndexParentExpander() {
055: super ();
056: }
057:
058: /**
059: * Sets the db-specific index extractor.
060: *
061: * @param extractor this is provided by the plugin.
062: */
063: public void setTableIndexExtractor(ITableIndexExtractor extractor) {
064: this .extractor = extractor;
065: }
066:
067: /**
068: * Create the child nodes for the passed parent node and return them. Note
069: * that this method should <B>not</B> actually add the child nodes to the
070: * parent node as this is taken care of in the caller.
071: *
072: * @param session Current session.
073: * @param node Node to be expanded.
074: *
075: * @return A list of <TT>ObjectTreeNode</TT> objects representing the child
076: * nodes for the passed node.
077: *
078: * @throws SQLException
079: * Thrown if an SQL error occurs.
080: */
081: public List<ObjectTreeNode> createChildren(ISession session,
082: ObjectTreeNode parentNode) {
083: final List<ObjectTreeNode> childNodes = new ArrayList<ObjectTreeNode>();
084: final IDatabaseObjectInfo parentDbinfo = parentNode
085: .getDatabaseObjectInfo();
086: final IDatabaseObjectInfo tableInfo = ((IndexParentInfo) parentDbinfo)
087: .getTableInfo();
088:
089: final ISQLConnection conn = session.getSQLConnection();
090: final SQLDatabaseMetaData md = session.getSQLConnection()
091: .getSQLMetaData();
092: PreparedStatement pstmt = null;
093: ResultSet rs = null;
094: try {
095: String query = extractor.getTableIndexQuery();
096: if (s_log.isDebugEnabled()) {
097: s_log.debug("Running query for index extraction: "
098: + query);
099: }
100: pstmt = conn.prepareStatement(query);
101: extractor.bindParamters(pstmt, tableInfo);
102: rs = pstmt.executeQuery();
103: while (rs.next()) {
104: DatabaseObjectInfo doi = new DatabaseObjectInfo(
105: parentDbinfo.getCatalogName(), parentDbinfo
106: .getSchemaName(), rs.getString(1),
107: DatabaseObjectType.INDEX, md);
108: childNodes.add(new ObjectTreeNode(session, doi));
109: }
110: } catch (SQLException e) {
111: session.showErrorMessage(e);
112: s_log.error(
113: "Unexpected exception while extracting indexes for "
114: + "parent dbinfo: " + parentDbinfo, e);
115: } finally {
116: if (rs != null)
117: try {
118: rs.close();
119: } catch (SQLException e) {
120: }
121: if (pstmt != null)
122: try {
123: pstmt.close();
124: } catch (SQLException e) {
125: }
126: }
127:
128: return childNodes;
129: }
130: }
|