001: package net.sourceforge.squirrel_sql.plugins.mssql.action;
002:
003: /*
004: * Copyright (C) 2004 Ryan Walberg <generalpf@yahoo.com>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.io.FileWriter;
022: import java.io.IOException;
023: import java.sql.SQLException;
024: import java.util.ArrayList;
025:
026: import javax.swing.JFileChooser;
027:
028: import net.sourceforge.squirrel_sql.client.session.ISession;
029: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
030: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
031: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverProperty;
032: import net.sourceforge.squirrel_sql.fw.sql.WrappedSQLException;
033: import net.sourceforge.squirrel_sql.fw.util.*;
034:
035: import net.sourceforge.squirrel_sql.plugins.mssql.MssqlPlugin;
036: import net.sourceforge.squirrel_sql.plugins.mssql.gui.GenerateSqlDialog;
037: import net.sourceforge.squirrel_sql.fw.util.ExtensionFilter;
038: import net.sourceforge.squirrel_sql.plugins.mssql.util.MssqlIntrospector;
039:
040: public class GenerateSqlCommand implements ICommand {
041:
042: private static final StringManager s_stringMgr = StringManagerFactory
043: .getStringManager(GenerateSqlCommand.class);
044:
045: private ISession _session;
046: private final MssqlPlugin _plugin;
047:
048: private final IDatabaseObjectInfo[] _dbObjs;
049:
050: public GenerateSqlCommand(ISession session, MssqlPlugin plugin,
051: IDatabaseObjectInfo[] dbObjs) {
052: super ();
053: if (session == null)
054: throw new IllegalArgumentException("ISession == null");
055: if (plugin == null)
056: throw new IllegalArgumentException("MssqlPlugin == null");
057: if (dbObjs == null)
058: throw new IllegalArgumentException(
059: "IDatabaseObjectInfo[] is null");
060:
061: _session = session;
062: _plugin = plugin;
063: _dbObjs = dbObjs;
064: }
065:
066: public void execute() throws BaseException {
067: try {
068: /* because of the cross-catalog problem, let's not invoke this if the current catalog isn't equal
069: * to the catalog specified in the URL.
070: */
071:
072: SQLDriverProperty[] props = _session.getSQLConnection()
073: .getConnectionProperties().getDriverProperties();
074: for (int i = 0; i < props.length; i++) {
075: if (props[i].getName().equals("DBNAME")) {
076: // there's a DBNAME specified, so make sure it matches the current catalog.
077: if (!props[i].getValue().equals(
078: _session.getSQLConnection().getCatalog())) {
079:
080: String[] params = {
081: props[i].getValue(),
082: _session.getSQLConnection()
083: .getCatalog() };
084: // i18n[mmsql.catalogErr=The DBNAME of the session's URL is set to '{0}', but the session's current catalog is set to '{1}'.\n\nSQL Server doesn't support this in most cases. This is a current issue.]
085: _session.getApplication().showErrorDialog(
086: s_stringMgr.getString(
087: "mmsql.catalogErr", params));
088: return;
089: }
090: }
091: }
092:
093: GenerateSqlDialog dlog = new GenerateSqlDialog(_session,
094: _plugin, _dbObjs);
095: dlog.preselectObjects(_dbObjs);
096: dlog.pack();
097: GUIUtils.centerWithinParent(dlog);
098: if (!dlog.showGeneralSqlDialog())
099: return;
100:
101: JFileChooser fc = new JFileChooser();
102: if (dlog.getOneFile()) {
103: ExtensionFilter ef = new ExtensionFilter();
104: // i18n[mmsql.sqlScripts=SQL Scripts]
105: ef.addExtension(s_stringMgr
106: .getString("mmsql.sqlScripts"), "sql");
107: // i18n[mmsql.textFiles=Text Files]
108: ef.addExtension(s_stringMgr
109: .getString("mmsql.textFiles"), "txt");
110: fc.setFileFilter(ef);
111: } else
112: fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
113:
114: if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
115:
116: FileWriter fw = null;
117:
118: if (dlog.getOneFile()) {
119: fw = new FileWriter(fc.getSelectedFile(), false);
120:
121: if (dlog.getScriptDatabase())
122: fw.write(MssqlIntrospector
123: .generateCreateDatabaseScript(_session
124: .getSQLConnection()
125: .getCatalog(), _session
126: .getSQLConnection()));
127:
128: if (dlog.getScriptUsersAndRoles())
129: fw.write(MssqlIntrospector
130: .generateUsersAndRolesScript(_session
131: .getSQLConnection()
132: .getCatalog(), _session
133: .getSQLConnection()));
134: }
135:
136: ArrayList<IDatabaseObjectInfo> objs = dlog
137: .getSelectedItems();
138: for (int i = 0; i < objs.size(); i++) {
139: IDatabaseObjectInfo oi = objs.get(i);
140:
141: if (!dlog.getOneFile())
142: fw = new FileWriter(fc.getSelectedFile()
143: + java.io.File.separator
144: + MssqlIntrospector
145: .getFixedVersionedObjectName(oi
146: .getSimpleName())
147: + ".txt", false);
148:
149: if (dlog.getGenerateDrop())
150: fw.write(MssqlIntrospector
151: .generateDropScript(oi));
152:
153: if (dlog.getGenerateCreate()) {
154: String script = MssqlIntrospector
155: .generateCreateScript(oi, _session
156: .getSQLConnection(), dlog
157: .getScriptConstraints());
158: fw.write(script);
159: }
160:
161: if (dlog.getScriptIndexes()) {
162: String script = MssqlIntrospector
163: .generateCreateIndexesScript(oi,
164: _session.getSQLConnection());
165: fw.write(script);
166: }
167:
168: if (dlog.getScriptTriggers()) {
169: String script = MssqlIntrospector
170: .generateCreateTriggersScript(oi,
171: _session.getSQLConnection());
172: fw.write(script);
173: }
174:
175: if (dlog.getScriptPermissions()) {
176: String script = MssqlIntrospector
177: .generatePermissionsScript(oi, _session
178: .getSQLConnection());
179: fw.write(script);
180: }
181:
182: if (!dlog.getOneFile())
183: fw.close();
184: }
185: if (dlog.getOneFile())
186: fw.close();
187: }
188: } catch (SQLException ex) {
189: ex.printStackTrace();
190: throw new WrappedSQLException(ex);
191: } catch (IOException ex) {
192: ex.printStackTrace();
193: _session.getApplication().showErrorDialog(ex.getMessage());
194: }
195: }
196:
197: }
|