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.SimpleSelectionDialog;
19 import com.quantum.util.connection.ConnectionUtil;
20 import com.quantum.view.LogProxy;
21 import com.quantum.view.tableview.TableView;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.IStatusLineManager;
27 import org.eclipse.jface.dialogs.ErrorDialog;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.swt.widgets.Shell;
35 public abstract class BaseExecuteAction extends Action {
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");
85 Bookmark bookmark = getBookmark();
86 if (bookmark != null) {
94 protected void execute(Bookmark bookmark) {
95 if (bookmark != null) {
97 getStatusLineManager().setErrorMessage(null);
98 Connection connection = getConnection(bookmark);
99 if (connection != null) {
100 execute(bookmark, connection);
102 } catch (IOException e) {
103 ExceptionDisplayDialog.openError(getShell(), null, null, e);
104 } catch (SQLException e) {
105 ExceptionDisplayDialog.openError(getShell(), null, null, e);
106 } catch (CoreException e) {
107 ErrorDialog.openError(getShell(), null, null, e.getStatus());
115 * @throws IOException
116 * @throws CoreException
118 protected void execute(Bookmark bookmark, Connection connection)
119 throws IOException, CoreException, SQLException {
120 getStatusLineManager().setMessage(execute1);
121 MultiSQLServer server = MultiSQLServer.getInstance();
123 Metrics metrics = new Metrics();
125 List queries = getQueries();
126 IProgressMonitor progressBar = getStatusLineManager().getProgressMonitor();
127 progressBar.beginTask("queries", queries.size());
128 for (int i = 0; i < queries.size(); i++) {
129 getStatusLineManager().setMessage((i % 2 == 0) ? execute1 : execute2);
131 String query = (String) queries.get(i);
132 System.out.println(">" + query + "<"); //$NON-NLS-1$ //$NON-NLS-2$
133 if (query != null && query.trim().length() > 0) {
134 SQLResults results = getSQLResults(bookmark, connection, server, query);
135 metrics.measure(results);
136 if (results != null) {
137 bookmark.addQuery(query);
138 if (results.isResultSet()) {
139 SQLResultSetCollection.getInstance().addSQLResultSet(
140 (SQLResultSetResults) results);
145 progressBar.worked(i);
148 displayFinalStatusMessage(metrics);
154 private void activateTableView() {
155 TableView.getInstance();
160 * @throws CoreException
161 * @throws IOException
163 protected abstract List getQueries() throws IOException, CoreException;
168 protected abstract IStatusLineManager getStatusLineManager();
173 private void displayFinalStatusMessage(Metrics metrics) {
174 String message = Messages.getString(
175 BaseExecuteAction.class,
176 "done", //$NON-NLS-1$
177 metrics.getResults());
178 if (metrics.hasErrors()) {
179 getStatusLineManager().setErrorMessage(
180 ImageStore.getImage(ImageStore.STOP), message);
182 getStatusLineManager().setMessage(message);
192 private SQLResults getSQLResults(Bookmark bookmark, Connection connection, MultiSQLServer server, String query) {
193 SQLResults results = null;
195 results = server.execute(bookmark, connection, query);
196 } catch (SQLException e) {
197 LogProxy log = LogProxy.getInstance();
198 log.addText(LogProxy.ERROR,
199 "Error Executing: " + query + ":" + e.toString(), e); //$NON-NLS-1$ //$NON-NLS-2$
200 MessageDialog.openConfirm(getShell(),
201 "Database returned error",
202 e.getLocalizedMessage()); //$NON-NLS-1$
207 protected Bookmark getBookmark() {
208 Bookmark bookmark = null;
209 SimpleSelectionDialog dialog = new SimpleSelectionDialog(
211 Messages.getString(BaseExecuteAction.class, "selectBookmark"),
212 BookmarkCollection.getInstance().getBookmarks(),
213 ImageStore.getImage(ImageStore.BOOKMARK));
214 if (SimpleSelectionDialog.OK == dialog.open()) {
215 IStructuredSelection selection = dialog.getSelection();
217 bookmark = (Bookmark) selection.getFirstElement();