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.sql.ResultSet;
023: import java.sql.ResultSetMetaData;
024: import java.sql.Statement;
025:
026: import javax.swing.SwingUtilities;
027:
028: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
029: import net.sourceforge.squirrel_sql.client.session.ISession;
030: import net.sourceforge.squirrel_sql.fw.sql.IQueryTokenizer;
031: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
032: import net.sourceforge.squirrel_sql.fw.util.StringManager;
033: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
034: import net.sourceforge.squirrel_sql.fw.util.StringUtilities;
035: import net.sourceforge.squirrel_sql.plugins.sqlscript.FrameWorkAcessor;
036: import net.sourceforge.squirrel_sql.plugins.sqlscript.SQLScriptPlugin;
037:
038: public class CreateDataScriptOfCurrentSQLCommand extends
039: CreateDataScriptCommand {
040:
041: private static final StringManager s_stringMgr = StringManagerFactory
042: .getStringManager(CreateDataScriptOfCurrentSQLCommand.class);
043:
044: /**
045: * Current plugin.
046: */
047: private final SQLScriptPlugin _plugin;
048:
049: /**
050: * Ctor specifying the current session.
051: */
052: public CreateDataScriptOfCurrentSQLCommand(ISession session,
053: SQLScriptPlugin plugin) {
054: super (session, plugin, false);
055: _plugin = plugin;
056: }
057:
058: /**
059: * Execute this command.
060: */
061: public void execute() {
062: _session.getApplication().getThreadPool().addTask(
063: new Runnable() {
064: public void run() {
065:
066: final StringBuffer sbRows = new StringBuffer(
067: 1000);
068:
069: try {
070: ISQLPanelAPI api = FrameWorkAcessor
071: .getSQLPanelAPI(_session, _plugin);
072:
073: String script = api
074: .getSQLScriptToBeExecuted();
075:
076: IQueryTokenizer qt = _session
077: .getQueryTokenizer();
078: qt.setScriptToTokenize(script);
079:
080: if (false == qt.hasQuery()) {
081: // i18n[CreateDataScriptOfCurrentSQLCommand.noQuery=No query found to create the script from.]
082: _session
083: .showErrorMessage(s_stringMgr
084: .getString("CreateTableOfCurrentSQLCommand.noQuery"));
085: return;
086: }
087:
088: ISQLConnection conn = _session
089: .getSQLConnection();
090:
091: final Statement stmt = conn
092: .createStatement();
093: try {
094: String sql = qt.nextQuery();
095:
096: ResultSet srcResult = stmt
097: .executeQuery(sql);
098: ResultSetMetaData metaData = srcResult
099: .getMetaData();
100: String sTable = metaData
101: .getTableName(1);
102: if (sTable == null || sTable.equals("")) {
103: int iFromIndex = StringUtilities
104: .getTokenBeginIndex(sql,
105: "from");
106: sTable = getNextToken(sql,
107: iFromIndex
108: + "from".length());
109: }
110: genInserts(srcResult, sTable, sbRows,
111: false);
112: } finally {
113: try {
114: stmt.close();
115: } catch (Exception e) {
116: }
117: }
118: } catch (Exception e) {
119: _session.showErrorMessage(e);
120: } finally {
121: SwingUtilities.invokeLater(new Runnable() {
122: public void run() {
123: if (sbRows.length() > 0) {
124: FrameWorkAcessor
125: .getSQLPanelAPI(
126: _session,
127: _plugin)
128: .appendSQLScript(
129: sbRows
130: .toString(),
131: true);
132:
133: _session
134: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
135: }
136: hideAbortFrame();
137: }
138: });
139: }
140: }
141: });
142: showAbortFrame();
143: }
144:
145: private String getNextToken(String selectSQL, int startPos) {
146: int curPos = startPos;
147: while (curPos < selectSQL.length()
148: && true == Character.isWhitespace(selectSQL
149: .charAt(curPos))) {
150: // Move over leading whitespaces
151: ++curPos;
152: }
153:
154: int startPosTrimed = curPos;
155:
156: while (curPos < selectSQL.length()
157: && false == Character.isWhitespace(selectSQL
158: .charAt(curPos))) {
159: ++curPos;
160: }
161:
162: return selectSQL.substring(startPosTrimed, curPos);
163: }
164:
165: }
|