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.SQLResults;
14 import com.quantum.ui.dialog.ExceptionDisplayDialog;
15 import com.quantum.ui.dialog.SimpleSelectionDialog;
16 import com.quantum.util.connection.ConnectionUtil;
17 import com.quantum.view.LogProxy;
18 import com.quantum.view.tableview.DefaultSizes;
19 import com.quantum.view.tableview.TableView;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.IStatusLineManager;
25 import org.eclipse.jface.dialogs.ErrorDialog;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.swt.widgets.Shell;
33 public abstract class BaseExecuteAction extends Action {
37 int resultUpdateCount = 0;
39 int resultsDisplayed = 0;
40 double queryDuration= 0.0;
42 public void measure(SQLResults results) {
43 if (results == null) {
46 queryDuration += results.getTime()/1000.0; // calculate the execution time (in seconds)
48 if (results.isResultSet()) {
51 resultUpdateCount += results.getUpdateCount();
56 public boolean hasErrors() {
57 return this.errorCount > 0;
60 Object[] getResults() {
62 new Integer(this.resultCount),
63 new Integer(this.resultUpdateCount),
64 new Integer(this.resultsDisplayed),
65 new Integer(this.errorCount),
66 new Double(this.queryDuration),
71 private ConnectionUtil connectionUtil = new ConnectionUtil();
73 protected abstract Shell getShell();
75 protected Connection getConnection(Bookmark bookmark) {
76 return this.connectionUtil.getConnection(bookmark, getShell());
79 String execute1 = Messages.getString(BaseExecuteAction.class, "execute1");
80 String execute2 = Messages.getString(BaseExecuteAction.class, "execute2");
83 Bookmark bookmark = getBookmark();
84 if (bookmark != null) {
92 protected void execute(Bookmark bookmark) {
93 if (bookmark != null) {
95 getStatusLineManager().setErrorMessage(null);
96 Connection connection = getConnection(bookmark);
97 if (connection != null) {
98 execute(bookmark, connection);
100 } catch (IOException e) {
101 ExceptionDisplayDialog.openError(getShell(), null, null, e);
102 } catch (SQLException e) {
103 ExceptionDisplayDialog.openError(getShell(), null, null, e);
104 } catch (CoreException e) {
105 ErrorDialog.openError(getShell(), null, null, e.getStatus());
113 * @throws IOException
114 * @throws CoreException
116 protected void execute(Bookmark bookmark, Connection connection)
117 throws IOException, CoreException, SQLException {
118 getStatusLineManager().setMessage(execute1);
119 MultiSQLServer server = MultiSQLServer.getInstance();
121 Metrics metrics = new Metrics();
123 List queries = getQueries();
124 IProgressMonitor progressBar = getStatusLineManager().getProgressMonitor();
125 progressBar.beginTask("queries", queries.size());
126 for (int i = 0; i < queries.size(); i++) {
127 getStatusLineManager().setMessage((i % 2 == 0) ? execute1 : execute2);
129 String query = (String) queries.get(i);
130 System.out.println(">" + query + "<"); //$NON-NLS-1$ //$NON-NLS-2$
131 if (query != null && query.trim().length() > 0) {
132 SQLResults results = getSQLResults(connection, server, query);
133 metrics.measure(results);
134 if (results != null) {
135 bookmark.addQuery(query);
136 if (results.isResultSet()) {
137 TableView.getInstance().loadQuery(bookmark, results);
141 progressBar.worked(i);
144 displayFinalStatusMessage(metrics);
149 * @throws CoreException
150 * @throws IOException
152 protected abstract List getQueries() throws IOException, CoreException;
157 protected abstract IStatusLineManager getStatusLineManager();
162 private void displayFinalStatusMessage(Metrics metrics) {
163 String message = Messages.getString(
164 BaseExecuteAction.class,
165 "done", //$NON-NLS-1$ //$NON-NLS-2$
166 metrics.getResults());
167 if (metrics.hasErrors()) {
168 getStatusLineManager().setErrorMessage(
169 ImageStore.getImage(ImageStore.STOP), message);
171 getStatusLineManager().setMessage(
172 ImageStore.getImage(ImageStore.SUCCESS), message);
182 private SQLResults getSQLResults(Connection connection, MultiSQLServer server, String query) {
183 SQLResults results = null;
185 results = server.execute(connection, query, 1,
186 DefaultSizes.PAGE_SIZE, DefaultSizes.MAX_COLUMN_SIZE);
187 } catch (SQLException e) {
188 LogProxy log = LogProxy.getInstance();
189 log.addText(LogProxy.ERROR,
190 "Error Executing: " + query + ":" + e.toString(), e); //$NON-NLS-1$ //$NON-NLS-2$
191 MessageDialog.openConfirm(getShell(),
192 "Database returned error",
193 e.getLocalizedMessage()); //$NON-NLS-1$
198 protected Bookmark getBookmark() {
199 Bookmark bookmark = null;
200 SimpleSelectionDialog dialog = new SimpleSelectionDialog(
202 Messages.getString(BaseExecuteAction.class, "selectBookmark"),
203 BookmarkCollection.getInstance().getBookmarks(),
204 ImageStore.getImage(ImageStore.BOOKMARK));
205 if (SimpleSelectionDialog.OK == dialog.open()) {
206 IStructuredSelection selection = dialog.getSelection();
208 bookmark = (Bookmark) selection.getFirstElement();