001: package net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.expanders;
002:
003: /*
004: * Copyright (C) 2006 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 TriggerParentExpander implements INodeExpander {
044:
045: /** Logger for this class. */
046: private static final ILogger s_log = LoggerController
047: .createLogger(TriggerParentExpander.class);
048:
049: private ITableTriggerExtractor triggerExtractor = null;
050:
051: /**
052: * Ctor.
053: *
054: */
055: public TriggerParentExpander() {
056: super ();
057: }
058:
059: /**
060: * Method for injecting the component that allows this class to work with
061: * a specific database, depending on the type of trigger extractor.
062: *
063: * @param extractor the ITableTriggerExtractor implementation to use.
064: */
065: public void setTableTriggerExtractor(
066: ITableTriggerExtractor extractor) {
067: this .triggerExtractor = extractor;
068: }
069:
070: /**
071: * Create the child nodes for the passed parent node and return them. Note
072: * that this method should <B>not </B> actually add the child nodes to the
073: * parent node as this is taken care of in the caller.
074: *
075: * @param session
076: * Current session.
077: * @param node
078: * Node to be expanded.
079: *
080: * @return A list of <TT>ObjectTreeNode</TT> objects representing the
081: * child nodes for the passed node.
082: */
083: public List<ObjectTreeNode> createChildren(ISession session,
084: ObjectTreeNode parentNode) throws SQLException {
085: final List<ObjectTreeNode> childNodes = new ArrayList<ObjectTreeNode>();
086: final IDatabaseObjectInfo parentDbinfo = parentNode
087: .getDatabaseObjectInfo();
088: final ISQLConnection conn = session.getSQLConnection();
089: final SQLDatabaseMetaData md = session.getSQLConnection()
090: .getSQLMetaData();
091: final String schemaName = parentDbinfo.getSchemaName();
092: final String catalogName = parentDbinfo.getCatalogName();
093: final IDatabaseObjectInfo tableInfo = ((TriggerParentInfo) parentDbinfo)
094: .getTableInfo();
095: PreparedStatement pstmt = null;
096: ResultSet rs = null;
097: try {
098: String tableName = tableInfo.getSimpleName();
099: String query = triggerExtractor.getTableTriggerQuery();
100: if (s_log.isDebugEnabled()) {
101: s_log.debug("Getting triggers for table " + tableName
102: + " in schema " + schemaName + " and catalog "
103: + catalogName + " - Running query: " + query);
104: }
105: pstmt = conn.prepareStatement(query);
106: triggerExtractor.bindParamters(pstmt, tableInfo);
107: rs = pstmt.executeQuery();
108: while (rs.next()) {
109: DatabaseObjectInfo doi = new DatabaseObjectInfo(
110: catalogName, schemaName, rs.getString(1),
111: DatabaseObjectType.TRIGGER, md);
112: childNodes.add(new ObjectTreeNode(session, doi));
113: }
114: } catch (SQLException e) {
115: session.showErrorMessage(e);
116: s_log.error(
117: "Unexpected exception while extracting triggers for "
118: + "parent dbinfo: " + parentDbinfo, e);
119: } finally {
120: if (rs != null)
121: try {
122: rs.close();
123: } catch (SQLException e) {
124: }
125: if (pstmt != null)
126: try {
127: pstmt.close();
128: } catch (SQLException e) {
129: }
130: }
131:
132: return childNodes;
133: }
134:
135: }
|