1 package com.quantum.actions;
3 import java.io.IOException;
4 import java.sql.Connection;
5 import java.sql.SQLException;
8 import com.quantum.ImageStore;
9 import com.quantum.Messages;
10 import com.quantum.model.Bookmark;
11 import com.quantum.model.BookmarkCollection;
12 import com.quantum.sql.MultiSQLServer;
13 import com.quantum.sql.SQLResultSetCollection;
14 import com.quantum.sql.SQLResultSetResults;
15 import com.quantum.sql.SQLResults;
16 import com.quantum.sql.SQLUpdateResults;
17 import com.quantum.ui.dialog.ExceptionDisplayDialog;
18 import com.quantum.ui.dialog.SQLExceptionDialog;
19 import com.quantum.ui.dialog.SimpleSelectionDialog;
20 import com.quantum.util.connection.ConnectionUtil;
21 import com.quantum.view.LogProxy;
22 import com.quantum.view.tableview.TableView;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.jface.action.IStatusLineManager;
27 import org.eclipse.jface.dialogs.ErrorDialog;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.actions.SelectionListenerAction;
35 public abstract class BaseExecuteAction extends SelectionListenerAction {
39 int resultUpdateCount = 0;
41 int resultsDisplayed = 0;
42 double queryDuration= 0.0;
44 public void measure(SQLResults results) {
45 if (results == null) {
48 queryDuration += results.getTime()/1000.0;
50 if (results.isResultSet()) {
53 resultUpdateCount += ((SQLUpdateResults) results).getUpdateCount();
58 public boolean hasErrors() {
59 return this.errorCount > 0;
62 Object[] getResults() {
64 new Integer(this.resultCount),
65 new Integer(this.resultUpdateCount),
66 new Integer(this.resultsDisplayed),
67 new Integer(this.errorCount),
68 new Double(this.queryDuration),
73 private ConnectionUtil connectionUtil = new ConnectionUtil();
75 protected abstract Shell getShell();
77 protected Connection getConnection(Bookmark bookmark) {
78 return this.connectionUtil.getConnection(bookmark, getShell());
81 String execute1 = Messages.getString(BaseExecuteAction.class, "execute1");
82 String execute2 = Messages.getString(BaseExecuteAction.class, "execute2");
84 protected BaseExecuteAction(String text) {
88 protected BaseExecuteAction() {
93 Bookmark bookmark = getBookmark();
94 if (bookmark != null) {
102 protected void execute(Bookmark bookmark) {
103 if (bookmark != null) {
105 getStatusLineManager().setErrorMessage(null);
106 Connection connection = getConnection(bookmark);
107 if (connection != null) {
108 execute(bookmark, connection);
110 } catch (IOException e) {
111 ExceptionDisplayDialog.openError(getShell(), null, null, e);
112 } catch (SQLException e) {
113 SQLExceptionDialog.openException(getShell(), bookmark, e);
114 } catch (CoreException e) {
115 ErrorDialog.openError(getShell(), null, null, e.getStatus());
123 * @throws IOException
124 * @throws CoreException
126 protected void execute(Bookmark bookmark, Connection connection)
127 throws IOException, CoreException, SQLException {
128 getStatusLineManager().setMessage(execute1);
129 MultiSQLServer server = MultiSQLServer.getInstance();
131 Metrics metrics = new Metrics();
133 List queries = getQueries();
134 IProgressMonitor progressBar = getStatusLineManager().getProgressMonitor();
135 progressBar.beginTask("queries", queries.size());
136 for (int i = 0; i < queries.size(); i++) {
137 getStatusLineManager().setMessage((i % 2 == 0) ? execute1 : execute2);
139 String query = (String) queries.get(i);
140 System.out.println(">" + query + "<"); //$NON-NLS-1$ //$NON-NLS-2$
141 if (query != null && query.trim().length() > 0) {
142 SQLResults results = getSQLResults(bookmark, connection, server, query);
143 metrics.measure(results);
144 if (results != null) {
145 bookmark.addQuery(query);
146 if (results.isResultSet()) {
147 SQLResultSetCollection.getInstance().addSQLResultSet(
148 (SQLResultSetResults) results);
153 progressBar.worked(i);
156 displayFinalStatusMessage(metrics);
162 private void activateTableView() {
163 TableView.getInstance();
168 * @throws CoreException
169 * @throws IOException
171 protected abstract List getQueries() throws IOException, CoreException;
176 protected abstract IStatusLineManager getStatusLineManager();
181 private void displayFinalStatusMessage(Metrics metrics) {
182 String message = Messages.getString(
183 BaseExecuteAction.class,
184 "done", //$NON-NLS-1$
185 metrics.getResults());
186 if (metrics.hasErrors()) {
187 getStatusLineManager().setErrorMessage(
188 ImageStore.getImage(ImageStore.STOP), message);
190 getStatusLineManager().setMessage(message);
200 private SQLResults getSQLResults(Bookmark bookmark, Connection connection, MultiSQLServer server, String query) {
201 SQLResults results = null;
203 results = server.execute(bookmark, connection, query);
204 } catch (SQLException e) {
205 LogProxy log = LogProxy.getInstance();
206 log.addText(LogProxy.ERROR,
207 "Error Executing: " + query + ":" + e.toString(), e); //$NON-NLS-1$ //$NON-NLS-2$
208 SQLExceptionDialog.openException(getShell(), bookmark, e);
213 protected Bookmark getBookmark() {
214 Bookmark bookmark = null;
215 SimpleSelectionDialog dialog = new SimpleSelectionDialog(
217 Messages.getString(BaseExecuteAction.class, "selectBookmark"),
218 BookmarkCollection.getInstance().getBookmarks(),
219 ImageStore.getImage(ImageStore.BOOKMARK));
220 if (SimpleSelectionDialog.OK == dialog.open()) {
221 IStructuredSelection selection = dialog.getSelection();
223 bookmark = (Bookmark) selection.getFirstElement();