001: package net.sourceforge.squirrel_sql.plugins.sqlscript.table_script;
002:
003: /*
004: * Copyright (C) 2001 Johan Compagner
005: * jcompagner@j-com.nl
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
026: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
027: import net.sourceforge.squirrel_sql.client.session.ISession;
028: import net.sourceforge.squirrel_sql.fw.dialects.CreateScriptPreferences;
029: import net.sourceforge.squirrel_sql.fw.dialects.DialectFactory;
030: import net.sourceforge.squirrel_sql.fw.dialects.HibernateDialect;
031: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
032: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
033: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
034: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
035: import net.sourceforge.squirrel_sql.fw.util.ICommand;
036: import net.sourceforge.squirrel_sql.fw.util.StringManager;
037: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
038: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
039: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
040: import net.sourceforge.squirrel_sql.plugins.sqlscript.FrameWorkAcessor;
041: import net.sourceforge.squirrel_sql.plugins.sqlscript.SQLScriptPlugin;
042: import net.sourceforge.squirrel_sql.plugins.sqlscript.prefs.SQLScriptPreferenceBean;
043: import net.sourceforge.squirrel_sql.plugins.sqlscript.prefs.SQLScriptPreferencesManager;
044:
045: public class CreateTableScriptCommand implements ICommand {
046: /**
047: * Current session.
048: */
049: private ISession _session;
050:
051: /**
052: * Current plugin.
053: */
054: private final SQLScriptPlugin _plugin;
055:
056: /** Logger for this class. */
057: private static ILogger s_log = LoggerController
058: .createLogger(CreateTableScriptCommand.class);
059:
060: /** i18n strings for this class */
061: private static final StringManager s_stringMgr = StringManagerFactory
062: .getStringManager(CreateTableScriptCommand.class);
063:
064: static interface i18n {
065: //i18n[CreateTableScriptCommand.jdbcOdbcMessage=JDBC-ODBC Bridge doesn't
066: //provide all of the necessary metadata. The script may be incomplete.]
067: String JDBCODBC_MESSAGE = s_stringMgr
068: .getString("CreateTableScriptCommand.jdbcOdbcMessage");
069: }
070:
071: private static SQLScriptPreferenceBean prefs = SQLScriptPreferencesManager
072: .getPreferences();
073:
074: /**
075: * Ctor specifying the current session.
076: */
077: public CreateTableScriptCommand(ISession session,
078: SQLScriptPlugin plugin) {
079: super ();
080: _session = session;
081: _plugin = plugin;
082: }
083:
084: /**
085: * Execute this command. Use the database meta data to construct a Create Table
086: * SQL script and place it in the SQL entry panel.
087: */
088: public void execute() {
089: IObjectTreeAPI api = FrameWorkAcessor.getObjectTreeAPI(
090: _session, _plugin);
091: IDatabaseObjectInfo[] dbObjs = api.getSelectedDatabaseObjects();
092: scriptTablesToSQLEntryArea(dbObjs);
093: }
094:
095: public void scriptTablesToSQLEntryArea(
096: final IDatabaseObjectInfo[] dbObjs) {
097: _session.getApplication().getThreadPool().addTask(
098: new Runnable() {
099: public void run() {
100: final String script = createTableScriptString(dbObjs);
101: if (null != script) {
102: GUIUtils
103: .processOnSwingEventThread(new Runnable() {
104: public void run() {
105: ISQLPanelAPI api = FrameWorkAcessor
106: .getSQLPanelAPI(
107: _session,
108: _plugin);
109: api.appendSQLScript(script,
110: true);
111: _session
112: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
113: }
114: });
115: }
116: }
117: });
118: }
119:
120: public String createTableScriptString(IDatabaseObjectInfo dbObj) {
121: return createTableScriptString(new IDatabaseObjectInfo[] { dbObj });
122: }
123:
124: public String createTableScriptString(IDatabaseObjectInfo[] dbObjs) {
125: StringBuilder result = new StringBuilder(1000);
126: ISQLDatabaseMetaData md = _session.getMetaData();
127: try {
128: boolean isJdbcOdbc = md.getURL().startsWith("jdbc:odbc:");
129: if (isJdbcOdbc) {
130: _session.showErrorMessage(i18n.JDBCODBC_MESSAGE);
131: s_log.error(i18n.JDBCODBC_MESSAGE);
132: }
133:
134: TableScriptConfigCtrl tscc = new TableScriptConfigCtrl(
135: _session.getApplication().getMainFrame());
136: if (1 < dbObjs.length) {
137: tscc.doModal();
138: if (false == tscc.isOk()) {
139: return null;
140: }
141: }
142:
143: CreateScriptPreferences csprefs = new CreateScriptPreferences();
144: csprefs.setConstraintsAtEnd(tscc.isConstAndIndAtEnd());
145: csprefs.setIncludeExternalReferences(tscc
146: .includeConstToTablesNotInScript());
147: csprefs.setDeleteAction(prefs.getDeleteAction());
148: csprefs.setDeleteRefAction(prefs.isDeleteRefAction());
149: csprefs.setUpdateAction(prefs.getUpdateAction());
150: csprefs.setUpdateRefAction(prefs.isUpdateRefAction());
151: csprefs.setQualifyTableNames(prefs.isQualifyTableNames());
152:
153: List<ITableInfo> tables = convertArrayToList(dbObjs);
154:
155: HibernateDialect dialect = DialectFactory.getDialect(
156: DialectFactory.SOURCE_TYPE, _session
157: .getApplication().getMainFrame(), md);
158: List<String> sqls = dialect.getCreateTableSQL(tables, md,
159: csprefs, isJdbcOdbc);
160: String sep = _session.getQueryTokenizer()
161: .getSQLStatementSeparator();
162:
163: for (String sql : sqls) {
164: result.append(sql);
165: result.append("\n");
166: result.append(sep);
167: result.append("\n");
168: }
169: } catch (Exception e) {
170: _session.showErrorMessage(e);
171: }
172: return result.toString();
173: }
174:
175: private List<ITableInfo> convertArrayToList(
176: IDatabaseObjectInfo[] dbObjs) {
177: List<ITableInfo> result = new ArrayList<ITableInfo>();
178: for (IDatabaseObjectInfo dbObj : dbObjs) {
179: if (dbObj instanceof ITableInfo) {
180: ITableInfo ti = (ITableInfo) dbObj;
181: result.add(ti);
182: }
183: }
184: return result;
185: }
186: }
|