001: package net.sourceforge.squirrel_sql.plugins.sqlscript.table_script;
002:
003: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
004: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
005: import net.sourceforge.squirrel_sql.client.session.ISession;
006: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
007: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
008: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
009: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
010: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
011: import net.sourceforge.squirrel_sql.fw.util.ICommand;
012: import net.sourceforge.squirrel_sql.plugins.sqlscript.FrameWorkAcessor;
013: import net.sourceforge.squirrel_sql.plugins.sqlscript.SQLScriptPlugin;
014:
015: public class CreateSelectScriptCommand implements ICommand {
016: /**
017: * Current session.
018: */
019: private ISession _session;
020:
021: /**
022: * Current plugin.
023: */
024: private final SQLScriptPlugin _plugin;
025:
026: /**
027: * Ctor specifying the current session.
028: */
029: public CreateSelectScriptCommand(ISession session,
030: SQLScriptPlugin plugin) {
031: super ();
032: _session = session;
033: _plugin = plugin;
034: }
035:
036: public void execute() {
037: IObjectTreeAPI api = FrameWorkAcessor.getObjectTreeAPI(
038: _session, _plugin);
039: IDatabaseObjectInfo[] dbObjs = api.getSelectedDatabaseObjects();
040: scriptSelectsToSQLEntryArea(dbObjs);
041: }
042:
043: public void scriptSelectsToSQLEntryArea(
044: final IDatabaseObjectInfo[] dbObjs) {
045: _session.getApplication().getThreadPool().addTask(
046: new Runnable() {
047: public void run() {
048: final String script = createSelectScriptString(dbObjs);
049: if (null != script) {
050: GUIUtils
051: .processOnSwingEventThread(new Runnable() {
052: public void run() {
053: ISQLPanelAPI api = FrameWorkAcessor
054: .getSQLPanelAPI(
055: _session,
056: _plugin);
057: api.appendSQLScript(script,
058: true);
059: _session
060: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
061: }
062: });
063: }
064: }
065: });
066: }
067:
068: public String createSelectScriptString(IDatabaseObjectInfo[] dbObjs) {
069: StringBuffer sbScript = new StringBuffer(1000);
070: StringBuffer sbConstraints = new StringBuffer(1000);
071: ISQLConnection conn = _session.getSQLConnection();
072: try {
073: boolean isJdbcOdbc = conn.getSQLMetaData().getURL()
074: .startsWith("jdbc:odbc:");
075: if (isJdbcOdbc) {
076: // TODO I18N
077: _session
078: .showErrorMessage("JDBC-ODBC Bridge doesn't provide necessary meta data. Script will be incomplete");
079: }
080:
081: for (int k = 0; k < dbObjs.length; k++) {
082: if (false == dbObjs[k] instanceof ITableInfo) {
083: continue;
084: }
085: ITableInfo ti = (ITableInfo) dbObjs[k];
086:
087: sbScript.append("SELECT ");
088:
089: TableColumnInfo[] infos = conn.getSQLMetaData()
090: .getColumnInfo(ti);
091: for (int i = 0; i < infos.length; i++) {
092: if (0 < i) {
093: sbScript.append(',');
094: }
095: sbScript.append(infos[i].getColumnName());
096: }
097:
098: sbScript.append(" FROM ").append(
099: ScriptUtil.getTableName(ti));
100: sbScript.append(
101: ScriptUtil.getStatementSeparator(_session))
102: .append('\n');
103:
104: }
105: } catch (Exception e) {
106: _session.showErrorMessage(e);
107: }
108:
109: return sbScript.append("\n").append(sbConstraints).toString();
110: }
111: }
|