001: package net.sourceforge.squirrel_sql.plugins.mysql.action;
002:
003: /*
004: * Copyright (C) 2002-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.ResultSet;
022: import java.sql.SQLException;
023: import java.sql.Statement;
024:
025: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
026: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
027: import net.sourceforge.squirrel_sql.fw.util.BaseException;
028: import net.sourceforge.squirrel_sql.fw.util.ICommand;
029: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
030: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
031:
032: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
033: import net.sourceforge.squirrel_sql.client.session.ISession;
034:
035: import net.sourceforge.squirrel_sql.plugins.mysql.MysqlPlugin;
036:
037: /**
038: * Generate a "CREATE TABLE" script for the currently
039: * selected tables.
040: *
041: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
042: */
043: class CreateMysqlTableScriptCommand implements ICommand {
044: /** Logger for this class. */
045: private final static ILogger s_log = LoggerController
046: .createLogger(CreateMysqlTableScriptCommand.class);
047:
048: /** Current session. */
049: private ISession _session;
050:
051: /** Current plugin. */
052: private final MysqlPlugin _plugin;
053:
054: /**
055: * Ctor specifying the current session.
056: */
057: public CreateMysqlTableScriptCommand(ISession session,
058: MysqlPlugin plugin) {
059: super ();
060: _session = session;
061: _plugin = plugin;
062: }
063:
064: /**
065: * Execute this command. Place the "create table" script in the SQL entry panel.
066: */
067: public void execute() throws BaseException {
068: final ISQLConnection conn = _session.getSQLConnection();
069: final StringBuffer buf = new StringBuffer(2048);
070: final String sep = " "
071: + _session.getQueryTokenizer()
072: .getSQLStatementSeparator();
073:
074: try {
075: final Statement stmt = conn.createStatement();
076: try {
077: IObjectTreeAPI api = _session.getSessionInternalFrame()
078: .getObjectTreeAPI();
079: IDatabaseObjectInfo[] dbObjs = api
080: .getSelectedDatabaseObjects();
081: for (int i = 0; i < dbObjs.length; ++i) {
082: final ResultSet rs = stmt
083: .executeQuery("show create table "
084: + dbObjs[i].getQualifiedName());
085: try {
086: if (rs.next()) {
087: buf.append(rs.getString(2)).append(sep)
088: .append('\n');
089: }
090: } finally {
091: rs.close();
092: }
093: }
094: } finally {
095: try {
096: stmt.close();
097: } catch (Exception ex) {
098: s_log.error(
099: "Error occured closing PreparedStatement",
100: ex);
101: }
102: }
103:
104: _session.getSessionInternalFrame().getSQLPanelAPI()
105: .appendSQLScript(buf.toString(), true);
106: _session
107: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
108: } catch (SQLException ex) {
109: throw new BaseException(ex);
110: }
111: }
112: }
|