01: package net.sourceforge.squirrel_sql.plugins.oracle.expander;
02:
03: /*
04: * Copyright (C) 2002-2003 Colin Bell
05: * colbell@users.sourceforge.net
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: */
21: import java.sql.PreparedStatement;
22: import java.sql.ResultSet;
23: import java.sql.SQLException;
24: import java.util.ArrayList;
25: import java.util.List;
26:
27: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectInfo;
28: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
29: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
30: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
31: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
32: import net.sourceforge.squirrel_sql.plugins.oracle.OraclePlugin;
33:
34: import net.sourceforge.squirrel_sql.client.session.ISession;
35: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.INodeExpander;
36: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.ObjectTreeNode;
37:
38: /**
39: * This class handles the expanding of the "user type"
40: * node. It will give a list of all the users.
41: *
42: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
43: */
44: public class UserParentExpander implements INodeExpander {
45: /** SQL used to load info. */
46: private static final String SQL_ADMIN = "select username from dba_users order by username";
47: /** SQL used to load info. */
48: private static final String SQL_USER = "select username from user_users order by username";
49:
50: /** Is user can access to dba_users. */
51: final protected boolean isAdmin;
52:
53: public UserParentExpander(final ISession session) {
54: isAdmin = OraclePlugin
55: .checkObjectAccessible(session, SQL_ADMIN);
56: }
57:
58: /**
59: * Create the child nodes for the passed parent node and return them. Note
60: * that this method should <B>not</B> actually add the child nodes to the
61: * parent node as this is taken care of in the caller.
62: *
63: * @param session Current session.
64: * @param node Node to be expanded.
65: *
66: * @return A list of <TT>ObjectTreeNode</TT> objects representing the child
67: * nodes for the passed node.
68: */
69: public List<ObjectTreeNode> createChildren(ISession session,
70: ObjectTreeNode parentNode) throws SQLException {
71: final List<ObjectTreeNode> childNodes = new ArrayList<ObjectTreeNode>();
72: final ISQLConnection conn = session.getSQLConnection();
73: final SQLDatabaseMetaData md = session.getSQLConnection()
74: .getSQLMetaData();
75: final IDatabaseObjectInfo parentDbinfo = parentNode
76: .getDatabaseObjectInfo();
77: final String schemaName = parentDbinfo.getSchemaName();
78:
79: final PreparedStatement pstmt = conn
80: .prepareStatement(isAdmin ? SQL_ADMIN : SQL_USER);
81: try {
82: ResultSet rs = pstmt.executeQuery();
83: try {
84: while (rs.next()) {
85: IDatabaseObjectInfo doi = new DatabaseObjectInfo(
86: null, schemaName, rs.getString(1),
87: DatabaseObjectType.USER, md);
88: childNodes.add(new ObjectTreeNode(session, doi));
89: }
90: } finally {
91: rs.close();
92: }
93: } finally {
94: pstmt.close();
95: }
96: return childNodes;
97: }
98: }
|