1 package com.quantum.actions;
3 import java.sql.Connection;
4 import java.sql.SQLException;
5 import java.util.Vector;
7 import com.quantum.Messages;
8 import com.quantum.QuantumPlugin;
9 import com.quantum.model.Bookmark;
10 import com.quantum.sql.MultiSQLServer;
11 import com.quantum.sql.SQLParser;
12 import com.quantum.sql.SQLResults;
13 import com.quantum.view.LogProxy;
14 import com.quantum.view.SQLQueryView;
15 import com.quantum.view.bookmark.BookmarkNode;
16 import com.quantum.view.bookmark.BookmarkView;
17 import com.quantum.view.tableview.DefaultSizes;
18 import com.quantum.view.tableview.TableView;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.action.IStatusLineManager;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.ui.IViewActionDelegate;
27 import org.eclipse.ui.IViewPart;
30 * Executes a query from the QueryView
34 public class ExecuteAction extends BaseSQLAction implements IViewActionDelegate {
36 String execute1 = Messages.getString("ExecuteAction.Executing_Query3"); //$NON-NLS-1$
37 String execute2 = Messages.getString("ExecuteAction.Executing_Query2"); //$NON-NLS-1$
39 public ExecuteAction() {
40 setActionDefinitionId("com.quantum.actions.ExecuteAction");
41 setImageDescriptor(QuantumPlugin.getImageDescriptor("play.gif"));
42 setText(Messages.getString("sqlqueryview.executeQuery"));
43 setToolTipText(Messages.getString("sqlqueryview.executeQuery"));
46 public void init(IViewPart view) {
47 this.view = (SQLQueryView) view;
50 public void run(IAction action) {
55 BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
58 MessageDialog.openInformation(
59 view.getSite().getShell(),"Sorry","Please select a bookmark to use as connection.");
62 getStatusLineManager().setErrorMessage(null);
63 Connection con = getConnection();
64 getStatusLineManager().setMessage(execute1);
65 MultiSQLServer server = MultiSQLServer.getInstance();
66 getStatusLineManager().setMessage(Messages.getString("ExecuteAction.Parsing_sql_script3")); //$NON-NLS-1$
67 Vector queries = new Vector();
68 String viewQuery = view.getQuery();
69 // We parse the executable units to send to the JDBC driver
70 queries = SQLParser.parse(viewQuery);
71 for (int i = 0; i < queries.size(); i++) {
72 String query = (String) queries.elementAt(i);
73 System.out.println(query);
76 int resultUpdateCount = 0;
78 int resultsDisplayed = 0;
79 double startTime = 0.0; // stores the time when query is started
80 double queryDuration= 0.0; // stores query's execution time
82 IProgressMonitor progressBar = getStatusLineManager().getProgressMonitor();
83 progressBar.beginTask("queries", queries.size());
84 for (int i = 0; i < queries.size(); i++) {
86 getStatusLineManager().setMessage(execute1);
88 getStatusLineManager().setMessage(execute2);
91 String query = (String) queries.elementAt(i);
92 System.out.println(">" + query + "<"); //$NON-NLS-1$ //$NON-NLS-2$
93 if (con != null && !query.equals("")) { //$NON-NLS-1$
97 startTime = System.currentTimeMillis(); // Start the measure of execution time
98 results = server.execute(con, query, 1, DefaultSizes.PAGE_SIZE, DefaultSizes.MAX_COLUMN_SIZE);
99 queryDuration = (System.currentTimeMillis() - startTime)/1000; // calculate the execution time (in seconds)
100 current.getBookmark().addQuery(query);
101 } catch (SQLException e) {
103 LogProxy log = LogProxy.getInstance();
106 "Error Executing: " + query + ":" + e.toString(), e); //$NON-NLS-1$ //$NON-NLS-2$
107 MessageDialog.openConfirm(view.getSite().getShell(), "Database returned error", e.getLocalizedMessage()); //$NON-NLS-1$
111 if (results.isResultSet()) {
112 TableView.getInstance().loadQuery(current.getBookmark(), results);
115 int count = results.getUpdateCount();
117 resultUpdateCount += results.getUpdateCount();
121 System.out.println(getStatusLineManager().getProgressMonitor() == null ? "not found" : "found!");
122 progressBar.worked(i);
123 // view.setProgress(i + 1, queries.size());
126 String message = Messages.getString("ExecuteAction.Done") + "(" + //$NON-NLS-1$ //$NON-NLS-2$
127 resultCount + Messages.getString("ExecuteAction.QueriesExecuted") + //$NON-NLS-1$
128 resultUpdateCount + Messages.getString("ExecuteAction.RowsUpdated") + //$NON-NLS-1$
129 resultsDisplayed + Messages.getString("ExecuteAction.ResultsDisplayed") + //$NON-NLS-1$
130 errorCount + Messages.getString("ExecuteAction.Errors") + //$NON-NLS-1$
131 queryDuration + Messages.getString("ExecutAction.TimeExec") + ")"; //$NON-NLS-1$ //$NON-NLS-2$
132 if (errorCount > 0) {
133 getStatusLineManager().setErrorMessage(
134 QuantumPlugin.getImage("stop.gif"), message);
136 getStatusLineManager().setMessage(
137 QuantumPlugin.getImage("success.gif"), message);
144 private IStatusLineManager getStatusLineManager() {
145 return this.view.getViewSite().getActionBars().getStatusLineManager();
148 public void selectionChanged(IAction action, ISelection selection) {
150 protected Bookmark getBookmark() {
151 BookmarkNode current = BookmarkView.getInstance().getCurrentBookmark();
152 return current.getBookmark();
156 * @see com.quantum.actions.BaseSQLAction#getShell()
158 protected Shell getShell() {
159 return this.view.getViewSite().getShell();