001: package net.sourceforge.squirrel_sql.plugins.mysql.expander;
002:
003: /*
004: * Copyright (C) 2003 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.DatabaseObjectType;
029: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
030: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
031: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData; //import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
032: //import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
033:
034: import net.sourceforge.squirrel_sql.client.session.ISession;
035: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.INodeExpander;
036: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.ObjectTreeNode;
037:
038: import net.sourceforge.squirrel_sql.plugins.mysql.MysqlPlugin;
039:
040: /**
041: * This class handles the expanding of the "user type"
042: * node. It will give a list of all the users.
043: *
044: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
045: */
046: public class UserParentExpander implements INodeExpander {
047: /** SQL used to load info. */
048: private static final String SQL = "select concat(user, '@', host) from mysql.user";
049:
050: /** Logger for this class. */
051: // private static final ILogger s_log =
052: // LoggerController.createLogger(UserParentExpander.class);
053: /** The plugin. */
054: @SuppressWarnings("unused")
055: private final MysqlPlugin _plugin;
056:
057: /**
058: * Ctor.
059: *
060: * @param plugin The controlling plugin.
061: *
062: * @throws IllegalArgumentException
063: * Thrown if <TT>null</TT> <TT>MysqlPlugin</TT> passed.
064: */
065: public UserParentExpander(MysqlPlugin plugin) {
066: super ();
067: if (plugin == null) {
068: throw new IllegalArgumentException("MysqlPlugin == null");
069: }
070:
071: _plugin = plugin;
072: }
073:
074: /**
075: * Create the child nodes for the passed parent node and return them. Note
076: * that this method should <B>not</B> actually add the child nodes to the
077: * parent node as this is taken care of in the caller.
078: *
079: * @param session Current session.
080: * @param node Node to be expanded.
081: *
082: * @return A list of <TT>ObjectTreeNode</TT> objects representing the child
083: * nodes for the passed node.
084: */
085: public List<ObjectTreeNode> createChildren(ISession session,
086: ObjectTreeNode parentNode) throws SQLException {
087: final List<ObjectTreeNode> childNodes = new ArrayList<ObjectTreeNode>();
088: final ISQLConnection conn = session.getSQLConnection();
089: final SQLDatabaseMetaData md = session.getSQLConnection()
090: .getSQLMetaData();
091: final IDatabaseObjectInfo parentDbinfo = parentNode
092: .getDatabaseObjectInfo();
093: final String schemaName = parentDbinfo.getSchemaName();
094:
095: PreparedStatement pstmt = conn.prepareStatement(SQL);
096: try {
097: ResultSet rs = pstmt.executeQuery();
098: while (rs.next()) {
099: IDatabaseObjectInfo doi = new DatabaseObjectInfo(null,
100: schemaName, rs.getString(1),
101: DatabaseObjectType.USER, md);
102: childNodes.add(new ObjectTreeNode(session, doi));
103: }
104: } finally {
105: pstmt.close();
106: }
107: return childNodes;
108: }
109: }
|