001: package net.sourceforge.squirrel_sql.plugins.db2.exp;
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.sql.SQLUtilities;
036:
037: /**
038: * This class handles the expanding of the "Sequence Group"
039: * node. It will give a list of all the Sequences available in the schema.
040: *
041: * @author manningr
042: */
043: public class UDFParentExpander implements INodeExpander {
044: /** SQL used to load UDF names */
045: private static final String SQL = "SELECT name "
046: + "FROM SYSIBM.SYSFUNCTIONS " + "WHERE schema = ? "
047: + "AND name like ? " + "AND implementation is null";
048:
049: /** SQL used to load UDF names on OS/400 systems */
050: private static final String OS_400_SQL = "select routine_name "
051: + "from QSYS2.SYSFUNCS " + "where routine_schema = ? "
052: + "and routine_name like ? ";
053:
054: /** whether or not we are connected to OS/400 */
055: private boolean isOS400 = false;
056:
057: /**
058: * Default ctor.
059: */
060: public UDFParentExpander(boolean isOS400) {
061: super ();
062: this .isOS400 = isOS400;
063: }
064:
065: /**
066: * Create the child nodes for the passed parent node and return them. Note
067: * that this method should <B>not</B> actually add the child nodes to the
068: * parent node as this is taken care of in the caller.
069: *
070: * @param session Current session.
071: * @param node Node to be expanded.
072: *
073: * @return A list of <TT>ObjectTreeNode</TT> objects representing the child
074: * nodes for the passed node.
075: */
076: public List<ObjectTreeNode> createChildren(ISession session,
077: ObjectTreeNode parentNode) throws SQLException {
078: final List<ObjectTreeNode> childNodes = new ArrayList<ObjectTreeNode>();
079: final IDatabaseObjectInfo parentDbinfo = parentNode
080: .getDatabaseObjectInfo();
081: final ISQLConnection conn = session.getSQLConnection();
082: final SQLDatabaseMetaData md = session.getSQLConnection()
083: .getSQLMetaData();
084: final String catalogName = parentDbinfo.getCatalogName();
085: final String schemaName = parentDbinfo.getSchemaName();
086: final String objFilter = session.getProperties()
087: .getObjectFilter();
088:
089: String sql = SQL;
090: if (isOS400) {
091: sql = OS_400_SQL;
092: }
093: final PreparedStatement pstmt = conn.prepareStatement(sql);
094: ResultSet rs = null;
095: try {
096: pstmt.setString(1, schemaName);
097: pstmt.setString(2, objFilter != null
098: && objFilter.length() > 0 ? objFilter : "%");
099: rs = pstmt.executeQuery();
100: while (rs.next()) {
101: IDatabaseObjectInfo si = new DatabaseObjectInfo(
102: catalogName, schemaName, rs.getString(1),
103: DatabaseObjectType.UDF, md);
104: childNodes.add(new ObjectTreeNode(session, si));
105: }
106: } finally {
107: SQLUtilities.closeResultSet(rs);
108: SQLUtilities.closeStatement(pstmt);
109: }
110: return childNodes;
111: }
112: }
|