001: package net.sourceforge.squirrel_sql.plugins.oracle.expander;
002:
003: /*
004: * Copyright (C) 2002 Colin Bell
005: * colbell@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.fw.sql.DatabaseObjectInfo;
028: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
029: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
030: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
031:
032: import net.sourceforge.squirrel_sql.client.session.ISession;
033: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.INodeExpander;
034: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.ObjectTreeNode;
035:
036: /**
037: * This class handles the expanding of an Oracle specific object type node.
038: *
039: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
040: */
041: public class ObjectTypeExpander implements INodeExpander {
042: /** SQL that retrieves the objects for the object types. */
043: private static String SQL = "select object_name from sys.all_objects where object_type = ?"
044: + " and owner = ? and object_name like ? order by object_name";
045:
046: /** Type of the objects to be displayed in the child nodes. */
047: private ObjectType _objectType;
048:
049: /**
050: * Ctor.
051: *
052: * @param objectType Object type to be displayed.
053: *
054: * @throws IllegalArgumentException
055: * Thrown if <TT>null</TT> objectType passed.
056: */
057: ObjectTypeExpander(ObjectType objectType) {
058: super ();
059: if (objectType == null) {
060: throw new IllegalArgumentException("ObjectType == null");
061: }
062: _objectType = objectType;
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 String catalogName = parentDbinfo.getCatalogName();
082: final String schemaName = parentDbinfo.getSchemaName();
083: childNodes
084: .addAll(createNodes(session, catalogName, schemaName));
085: return childNodes;
086: }
087:
088: private List<ObjectTreeNode> createNodes(ISession session,
089: String catalogName, String schemaName) throws SQLException {
090: final ISQLConnection conn = session.getSQLConnection();
091: final SQLDatabaseMetaData md = conn.getSQLMetaData();
092: final List<ObjectTreeNode> childNodes = new ArrayList<ObjectTreeNode>();
093: String objFilter = session.getProperties().getObjectFilter();
094:
095: // Add node for each object.
096: PreparedStatement pstmt = conn.prepareStatement(SQL);
097: try {
098: pstmt.setString(1, _objectType._objectTypeColumnData);
099: pstmt.setString(2, schemaName);
100: pstmt.setString(3, objFilter != null
101: && objFilter.length() > 0 ? objFilter : "%");
102: ResultSet rs = pstmt.executeQuery();
103: try {
104: while (rs.next()) {
105: IDatabaseObjectInfo dbinfo = new DatabaseObjectInfo(
106: catalogName, schemaName, rs.getString(1),
107: _objectType._childDboType, md);
108: childNodes.add(new ObjectTreeNode(session, dbinfo));
109: }
110: } finally {
111: rs.close();
112: }
113: } finally {
114: pstmt.close();
115: }
116: return childNodes;
117: }
118: }
|